xml-copy-editor-code/src/xmlcopyeditor.cpp

6034 lines
184 KiB
C++
Raw Normal View History

/*
* Copyright 2005-2007 Gerald Schmidt.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* Xml Copy Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
2007-09-07 23:17:30 +02:00
#include <iostream>
#include <fstream>
#include <string>
#include <wx/aboutdlg.h>
#include "xmlcopyeditor.h"
#include "readfile.h"
#include "xmldoc.h"
#include "xmlctrl.h"
#include "wraplibxml.h"
#include "xmlschemalocator.h"
#include "xsllocator.h"
#include "xmlutf8reader.h"
#include "xmlpromptgenerator.h"
#include "xmlencodingspy.h"
#include "styledialog.h"
#include "mypropertysheet.h"
#include "wraptempfilename.h"
#include "globalreplacedialog.h"
#include "replace.h"
#include "associatedialog.h"
#include "xmlassociatexsd.h"
#include "xmlassociatexsl.h"
#include "xmlassociatedtd.h"
#include "aboutdialog.h"
#include "pathresolver.h"
#include "locationpanel.h"
#include "insertpanel.h"
#include "xmlwordcount.h"
#include "mynotebook.h"
#include "getlinuxappdir.h"
#include "commandpanel.h"
#include "binaryfile.h"
#include <wx/aui/auibook.h>
#include <wx/richtext/richtextsymboldlg.h>
#include <wx/textctrl.h>
#include <iconv.h>
#define ngettext wxGetTranslation
#ifdef NEWFINDREPLACE
2007-09-08 00:25:30 +02:00
#include "findreplacepanel.h"
2007-09-07 23:17:30 +02:00
#endif
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
#include <wx/msw/uxtheme.h>
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
#include "wrapxerces.h"
#include "xpm/appicon.xpm"
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
BEGIN_EVENT_TABLE ( MyFrame, wxFrame )
EVT_ACTIVATE_APP ( MyFrame::OnActivateApp )
EVT_CLOSE ( MyFrame::OnFrameClose )
EVT_KEY_DOWN ( MyFrame::OnKeyPressed )
EVT_MENU ( wxID_ABOUT, MyFrame::OnAbout )
EVT_MENU ( wxID_CLOSE, MyFrame::OnClose )
EVT_MENU ( wxID_CLOSE_ALL, MyFrame::OnCloseAll )
EVT_MENU ( wxID_CUT, MyFrame::OnCut )
EVT_MENU ( wxID_COPY, MyFrame::OnCopy )
EVT_MENU ( wxID_HELP, MyFrame::OnHelp )
EVT_MENU ( wxID_PASTE, MyFrame::OnPaste )
EVT_MENU ( ID_PASTE_NEW_DOCUMENT, MyFrame::OnPasteNewDocument )
EVT_MENU ( wxID_EXIT, MyFrame::OnQuit )
EVT_MENU ( wxID_NEW, MyFrame::OnNew )
EVT_MENU ( wxID_OPEN, MyFrame::OnOpen )
EVT_MENU ( wxID_SAVE, MyFrame::OnSave )
EVT_MENU ( wxID_SAVEAS, MyFrame::OnSaveAs )
EVT_MENU ( wxID_UNDO, MyFrame::OnUndo )
EVT_MENU ( wxID_REDO, MyFrame::OnRedo )
EVT_MENU ( wxID_REVERT, MyFrame::OnRevert )
EVT_MENU ( ID_INSERT_CHILD, MyFrame::OnInsertChild )
EVT_MENU ( ID_INSERT_SIBLING, MyFrame::OnInsertSibling )
EVT_MENU ( ID_INSERT_ENTITY, MyFrame::OnInsertEntity )
EVT_MENU ( ID_INSERT_TWIN, MyFrame::OnInsertTwin )
EVT_MENU ( ID_INSERT_SYMBOL, MyFrame::OnInsertSymbol )
EVT_MENU ( ID_TOGGLE_FOLD, MyFrame::OnToggleFold )
EVT_MENU ( ID_FOLD_ALL, MyFrame::OnFoldAll )
EVT_MENU ( ID_UNFOLD_ALL, MyFrame::OnUnfoldAll )
EVT_MENU ( ID_OPEN_LARGE_FILE, MyFrame::OnOpen )
EVT_MENU ( ID_PRINT_PREVIEW, MyFrame::OnPrintPreview )
EVT_MENU ( ID_PRINT_SETUP, MyFrame::OnPrintSetup )
EVT_MENU ( ID_PRINT, MyFrame::OnPrint )
EVT_MENU ( ID_WORD_COUNT, MyFrame::OnWordCount )
EVT_MENU ( ID_IMPORT_MSWORD, MyFrame::OnImportMSWord )
EVT_MENU ( ID_EXPORT_MSWORD, MyFrame::OnExportMSWord )
EVT_MENU ( ID_HIDE_PANE, MyFrame::OnClosePane )
EVT_MENU ( ID_COMMAND, MyFrame::OnCommand )
EVT_MENU ( ID_FIND, MyFrame::OnFind )
EVT_MENU ( ID_FIND_AGAIN, MyFrame::OnFindAgain )
EVT_MENU ( ID_GOTO, MyFrame::OnGoto )
EVT_MENU ( ID_FEEDBACK, MyFrame::OnFeedback )
EVT_MENU ( ID_PREVIOUS_DOCUMENT, MyFrame::OnPreviousDocument )
EVT_MENU ( ID_NEXT_DOCUMENT, MyFrame::OnNextDocument )
EVT_MENU ( ID_BROWSER, MyFrame::OnBrowser )
EVT_MENU ( ID_REPLACE, MyFrame::OnFindReplace )
EVT_MENU ( ID_GLOBAL_REPLACE, MyFrame::OnGlobalReplace )
EVT_MENU ( ID_CHECK_WELLFORMED, MyFrame::OnCheckWellformedness )
EVT_MENU ( ID_VALIDATE_DTD, MyFrame::OnValidateDTD )
EVT_MENU ( ID_VALIDATE_RELAX_NG, MyFrame::OnValidateRelaxNG )
EVT_MENU ( ID_VALIDATE_W3C_SCHEMA, MyFrame::OnValidateSchema )
EVT_MENU ( ID_XPATH, MyFrame::OnXPath )
EVT_MENU_RANGE ( ID_XSLT, ID_XSLT_WORDML_DOCBOOK, MyFrame::OnXslt )
EVT_MENU ( ID_PRETTYPRINT, MyFrame::OnPrettyPrint )
EVT_MENU ( ID_ENCODING, MyFrame::OnEncoding )
EVT_MENU ( ID_SPELL, MyFrame::OnSpelling )
EVT_MENU ( ID_FONT_SMALLER, MyFrame::OnFontSmaller )
EVT_MENU ( ID_FONT_NORMAL, MyFrame::OnFontMedium )
EVT_MENU ( ID_FONT_LARGER, MyFrame::OnFontLarger )
EVT_MENU ( ID_OPTIONS, MyFrame::OnOptions )
EVT_MENU ( ID_HOME, MyFrame::OnHome )
EVT_MENU ( ID_DOWNLOAD_SOURCE, MyFrame::OnDownloadSource )
EVT_MENU ( ID_TOOLBAR_VISIBLE, MyFrame::OnToolbarVisible )
EVT_MENU ( ID_LOCATION_PANE_VISIBLE, MyFrame::OnLocationPaneVisible )
EVT_MENU ( ID_PROTECT_TAGS, MyFrame::OnProtectTags )
EVT_MENU ( ID_WRAP_WORDS, MyFrame::OnWrapWords )
EVT_MENU_RANGE ( ID_SHOW_TAGS, ID_HIDE_TAGS, MyFrame::OnVisibilityState )
EVT_MENU_RANGE ( ID_ASSOCIATE_DTD_PUBLIC, ID_ASSOCIATE_XSL, MyFrame::OnAssociate )
EVT_MENU_RANGE ( wxID_FILE1, wxID_FILE9, MyFrame::OnHistoryFile )
EVT_MENU_RANGE (
ID_VALIDATE_PRESET1, ID_VALIDATE_PRESET9, MyFrame::OnValidatePreset )
EVT_MENU_RANGE (
2007-09-07 23:17:30 +02:00
ID_COLOR_SCHEME_DEFAULT,
ID_COLOR_SCHEME_NONE,
2007-09-08 00:25:30 +02:00
MyFrame::OnColorScheme )
EVT_UPDATE_UI_RANGE ( ID_REPLACE, ID_GLOBAL_REPLACE, MyFrame::OnUpdateReplaceRange )
EVT_FIND ( wxID_ANY, MyFrame::OnDialogFind )
EVT_FIND_NEXT ( wxID_ANY, MyFrame::OnDialogFind )
EVT_FIND_REPLACE ( wxID_ANY, MyFrame::OnDialogReplace )
EVT_FIND_REPLACE_ALL ( wxID_ANY, MyFrame::OnDialogReplaceAll )
EVT_ICONIZE ( MyFrame::OnIconize )
EVT_UPDATE_UI ( ID_LOCATION_PANE_VISIBLE, MyFrame::OnUpdateLocationPaneVisible )
EVT_UPDATE_UI ( wxID_CLOSE, MyFrame::OnUpdateDocRange )
EVT_UPDATE_UI ( wxID_SAVEAS, MyFrame::OnUpdateDocRange )
EVT_UPDATE_UI ( wxID_CLOSE_ALL, MyFrame::OnUpdateCloseAll )
EVT_UPDATE_UI ( wxID_REVERT, MyFrame::OnUpdateSaveUndo )
EVT_UPDATE_UI ( wxID_SAVE, MyFrame::OnUpdateDocRange ) // always allow save if doc present
EVT_UPDATE_UI ( wxID_UNDO, MyFrame::OnUpdateSaveUndo )
EVT_UPDATE_UI ( wxID_REDO, MyFrame::OnUpdateRedo )
EVT_UPDATE_UI ( wxID_PASTE, MyFrame::OnUpdatePaste )
EVT_UPDATE_UI ( wxID_CUT, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( wxID_COPY, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( ID_FIND_AGAIN, MyFrame::OnUpdateFindAgain )
EVT_UPDATE_UI_RANGE ( ID_FIND, ID_EXPORT_MSWORD, MyFrame::OnUpdateDocRange )
EVT_UPDATE_UI ( ID_PREVIOUS_DOCUMENT, MyFrame::OnUpdatePreviousDocument )
EVT_UPDATE_UI ( ID_NEXT_DOCUMENT, MyFrame::OnUpdateNextDocument )
EVT_UPDATE_UI ( ID_HIDE_PANE, MyFrame::OnUpdateClosePane )
EVT_IDLE ( MyFrame::OnIdle )
EVT_AUINOTEBOOK_PAGE_CLOSE ( wxID_ANY, MyFrame::OnPageClosing )
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
EVT_DROP_FILES ( MyFrame::OnDropFiles )
2007-09-07 23:17:30 +02:00
#endif
END_EVENT_TABLE()
2007-09-08 00:25:30 +02:00
IMPLEMENT_APP ( MyApp )
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
MyApp::MyApp() : checker ( NULL ), server ( NULL ), connection ( NULL ),
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
config ( new wxConfig ( _T ( "SourceForge Project\\XML Copy Editor" ) ) )
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
config ( new wxConfig ( _T ( "xmlcopyeditor" ) ) )
2007-09-07 23:17:30 +02:00
#endif
{
2007-09-08 00:25:30 +02:00
lang = 0;
2007-09-07 23:17:30 +02:00
#ifdef __WXGTK__
2007-09-08 00:25:30 +02:00
int fdnull = open ( "/dev/null", O_WRONLY, 0 );
dup2 ( fdnull, STDERR_FILENO );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
myLocale.Init();
int systemLocale = myLocale.GetSystemLanguage();
switch ( systemLocale )
{
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_GERMAN:
case wxLANGUAGE_GERMAN_AUSTRIAN:
case wxLANGUAGE_GERMAN_BELGIUM:
case wxLANGUAGE_GERMAN_LIECHTENSTEIN:
case wxLANGUAGE_GERMAN_LUXEMBOURG:
case wxLANGUAGE_GERMAN_SWISS:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_GERMAN;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_CHINESE_SIMPLIFIED:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_CHINESE_SIMPLIFIED;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_CHINESE_TRADITIONAL:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_CHINESE_TRADITIONAL;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_SPANISH:
case wxLANGUAGE_SPANISH_ARGENTINA:
case wxLANGUAGE_SPANISH_BOLIVIA:
case wxLANGUAGE_SPANISH_CHILE:
case wxLANGUAGE_SPANISH_COLOMBIA:
case wxLANGUAGE_SPANISH_COSTA_RICA:
case wxLANGUAGE_SPANISH_DOMINICAN_REPUBLIC:
case wxLANGUAGE_SPANISH_ECUADOR:
case wxLANGUAGE_SPANISH_EL_SALVADOR:
case wxLANGUAGE_SPANISH_GUATEMALA:
case wxLANGUAGE_SPANISH_HONDURAS:
case wxLANGUAGE_SPANISH_MEXICAN:
case wxLANGUAGE_SPANISH_MODERN:
case wxLANGUAGE_SPANISH_NICARAGUA:
case wxLANGUAGE_SPANISH_PANAMA:
case wxLANGUAGE_SPANISH_PARAGUAY:
case wxLANGUAGE_SPANISH_PERU:
case wxLANGUAGE_SPANISH_PUERTO_RICO:
case wxLANGUAGE_SPANISH_URUGUAY:
case wxLANGUAGE_SPANISH_US:
case wxLANGUAGE_SPANISH_VENEZUELA:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_SPANISH;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_SLOVAK:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_SLOVAK;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_SWEDISH:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_SWEDISH;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_FRENCH:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_FRENCH;
break;
2007-09-07 23:17:30 +02:00
case wxLANGUAGE_UKRAINIAN:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_UKRAINIAN;
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
systemLocale = wxLANGUAGE_ENGLISH_US;
break;
}
if ( config.get() )
{
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
singleInstanceCheck = config->Read ( _T ( "singleInstanceCheck" ), true );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
long longFalse = 0;
singleInstanceCheck = config->Read ( _T ( "singleInstanceCheck" ), longFalse );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
lang = config->Read ( _T ( "lang" ), systemLocale );
}
else
{
lang = systemLocale;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
singleInstanceCheck = true;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
singleInstanceCheck = false;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
myLocale.Init ( lang, wxLOCALE_LOAD_DEFAULT );
wxLocale::AddCatalogLookupPathPrefix ( wxT ( "." ) );
wxLocale::AddCatalogLookupPathPrefix ( wxT ( ".." ) );
2007-09-07 23:17:30 +02:00
#ifdef __LINUX__
2007-09-08 00:25:30 +02:00
wxString poDir = GetLinuxAppDir::run() + wxFileName::GetPathSeparator() + _T ( "po" );
wxLocale::AddCatalogLookupPathPrefix ( poDir );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
2007-09-10 00:18:54 +02:00
if ( !myLocale.AddCatalog ( _T ( "messages" ) ) )
;
2007-09-07 23:17:30 +02:00
#ifdef __LINUX__
2007-09-08 00:25:30 +02:00
{
wxLogNull noLog;
myLocale.AddCatalog ( _T ( "fileutils" ) );
}
2007-09-07 23:17:30 +02:00
#endif
}
MyApp::~MyApp()
{
2007-09-08 00:25:30 +02:00
delete checker;
delete server;
delete connection;
2007-09-07 23:17:30 +02:00
}
bool MyApp::OnInit()
{
2007-09-08 00:25:30 +02:00
wxString name, service, hostName;
name.Printf ( _T ( "xmlcopyeditor-%s" ), wxGetUserId().c_str() );
service = IPC_SERVICE;
hostName = _T ( "localhost" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( singleInstanceCheck )
{
checker = new wxSingleInstanceChecker ( name );
while ( checker->IsAnotherRunning() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
// attempt calling server
client = new MyClient();
connection = ( MyClientConnection * )
client->MakeConnection ( hostName, service, IPC_TOPIC );
if ( !connection || !connection->StartAdvise ( IPC_ADVISE_NAME ) )
break;
else
{
wxString argument;
if ( this->argc > 1 )
{
for ( int i = 1; i < this->argc; i++ )
{
argument = ( wxString ) this->argv[i];
argument = PathResolver::run ( argument );
connection->Poke ( argument, _T ( "Data" ) );
}
}
else
{
argument = ( wxString ) IPC_NO_FILE;
connection->Poke ( argument, _T ( "Data" ) );
}
return false;
}
2007-09-07 23:17:30 +02:00
}
}
2007-09-08 00:25:30 +02:00
server = new MyServer;
server->Create ( service );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
MyFrame *frame;
try
{
wxImage::AddHandler ( new wxPNGHandler );
wxSystemOptions::SetOption ( _T ( "msw.remap" ), 0 );
frame = new MyFrame (
_ ( "XML Copy Editor" ),
config.get(),
myLocale,
singleInstanceCheck,
lang );
frame->Show ( true );
if ( frame->getHandleCommandLineFlag() )
frame->handleCommandLine();
}
catch ( exception &e )
{
const char *what;
what = e.what();
wxString wideWhat, errorString;
wideWhat = wxString ( what, wxConvLocal, strlen ( what ) );
if ( wideWhat.empty() )
wideWhat = _ ( "(unknown error)" );
errorString = _ ( "XML Copy Editor has encountered the following error and needs to close: " );
errorString += wideWhat;
errorString += _T ( "." );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
::MessageBox (
NULL,
errorString,
_ ( "Error" ),
MB_ICONERROR | MB_TASKMODAL );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
wxMessageBox ( errorString, _ ( "Error" ), wxICON_ERROR );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
exit ( EXIT_FAILURE );
}
catch ( ... )
{
exit ( EXIT_FAILURE );
}
return true;
2007-09-07 23:17:30 +02:00
}
void MyApp::OnUnhandledException()
{
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
::MessageBox (
NULL,
_ ( "XML Copy Editor has encountered an error and needs to close." ),
_ ( "Error" ),
MB_ICONERROR | MB_TASKMODAL );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
wxMessageBox (
_ ( "XML Copy Editor has encountered an error and needs to close." ),
_ ( "Error" ),
wxICON_ERROR );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
exit ( EXIT_FAILURE );
2007-09-07 23:17:30 +02:00
}
bool MyApp::OnExceptionInMainLoop()
{
2007-09-08 00:25:30 +02:00
try
{
throw;
}
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
catch ( bad_alloc& )
{
::MessageBox (
NULL,
_ ( "The operating system has turned down a request for additional memory" ),
_ ( "Out of memory" ),
MB_ICONERROR );
return true;
}
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
catch ( exception &e )
{
const char *what;
what = e.what();
wxString wideWhat, errorString;
wideWhat = wxString ( what, wxConvLocal, strlen ( what ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( wideWhat.empty() )
_ ( "(unknown error)" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
errorString = _ ( "The following error has occurred: " );
errorString += wideWhat;
errorString += _ ( ".\n\nSelect \"Abort\" to exit, \"Retry\" to close this window and \"Ignore\" to continue." );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
int ret = ::MessageBox (
NULL,
errorString,
_ ( "Error" ),
MB_ABORTRETRYIGNORE |
MB_ICONERROR |
MB_TASKMODAL );
switch ( ret )
{
case IDABORT:
exit ( EXIT_FAILURE );
break;
case IDRETRY:
return false;
case IDIGNORE:
return true;
default:
throw;
}
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
// wxGTK does not reach this point; see HandleEvent below
wxMessageBox (
errorString,
_ ( "Error" ),
wxICON_ERROR );
return false;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
catch ( ... )
{
wxString otherError ( _ ( "XML Copy Editor has encountered an error and needs to close." ) );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
::MessageBox (
NULL,
otherError,
_ ( "Error" ),
MB_ICONERROR );
return false;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
wxMessageBox (
otherError,
_ ( "Error" ),
wxICON_ERROR );
return false;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
return false;
2007-09-07 23:17:30 +02:00
}
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
void MyApp::HandleEvent ( wxEvtHandler *handler, wxEventFunction func, wxEvent& event ) const
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
try
{
wxApp::HandleEvent ( handler, func, event );
}
catch ( std::bad_alloc& )
{
wxMessageBox (
_ ( "The operating system has turned down a request for additional memory" ),
_ ( "Out of memory" ),
wxICON_ERROR );
return;
}
catch ( std::exception& e )
{
std::string s ( e.what() );
wxString ws = wxString ( s.c_str(), wxConvUTF8, s.size() );
wxMessageBox (
ws,
_ ( "Error" ),
wxICON_ERROR );
return;
}
catch ( ... )
{
throw;
}
2007-09-07 23:17:30 +02:00
}
#endif
2007-09-08 00:25:30 +02:00
MyFrame::MyFrame (
const wxString& title,
wxConfig *configParameter,
wxLocale& locale,
bool singleInstanceCheckParameter,
int langParameter ) :
wxFrame ( NULL, wxID_ANY, title ),
config ( configParameter ),
myLocale ( locale ),
singleInstanceCheck ( singleInstanceCheckParameter ),
lang ( langParameter ),
htmlPrinting ( new wxHtmlEasyPrinting (
wxEmptyString,
this ) ),
findDialog ( 0 ),
2007-09-07 23:17:30 +02:00
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
helpController ( new wxHtmlHelpController() ),
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
menuBar ( 0 ),
toolBar ( 0 ),
xmlMenu ( 0 ),
mainBook ( 0 ),
restoreFocusToNotebook ( false )
{
manager.SetManagedWindow ( this );
lastPos = 0;
htmlReport = NULL;
lastDoc = NULL;
wxString defaultFont =
#ifdef __WXMSW__
_T ( "Arial" );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
_T ( "Bitstream Vera Sans" );
2007-09-07 23:17:30 +02:00
#endif
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
coolBar = NULL;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
bool findMatchCase;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch configuration
if ( config ) // config found
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
history.Load ( *config );
properties.insertCloseTag =
config->Read ( _T ( "insertCloseTag" ), true );
properties.completion =
config->Read ( _T ( "completion" ), true );
properties.number =
config->Read ( _T ( "number" ), true );
properties.fold =
config->Read ( _T ( "fold" ), true );
properties.currentLine =
config->Read ( _T ( "currentLine" ), true );
properties.highlightSyntax =
config->Read ( _T ( "highlightSyntax" ), true );
properties.whitespaceVisible =
config->Read ( _T ( "whitespaceVisible" ), true );
properties.indentLines =
config->Read ( _T ( "indentLines" ), true );
properties.toggleLineBackground =
config->Read ( _T ( "toggleLineBackground" ), true );
properties.protectHiddenElements =
config->Read ( _T ( "protectHiddenElements" ), true );
properties.deleteWholeTag =
config->Read ( _T ( "deleteWholeTag" ), true );
properties.validateAsYouType =
config->Read ( _T ( "validateAsYouType" ), true );
properties.font =
config->Read ( _T ( "font" ), defaultFont );
findRegex =
config->Read ( _T ( "findRegex" ), true );
xpathExpression =
config->Read ( _T ( "xpathExpression" ), wxEmptyString );
lastXslStylesheet =
config->Read ( _T ( "lastXslStylesheet" ), wxEmptyString );
lastRelaxNGSchema =
config->Read ( _T ( "lastRelaxNGSchema" ), wxEmptyString );
lastXslStylesheet.Replace ( _T ( " " ), _T ( "%20" ), true );
lastRelaxNGSchema.Replace ( _T ( " " ), _T ( "%20" ), true );
applicationDir =
config->Read ( _T ( "applicationDir" ), wxEmptyString );
if ( applicationDir.empty() )
{
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
applicationDir =
config->Read ( _T ( "InstallPath" ), wxGetCwd() );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
applicationDir = GetLinuxAppDir::run();
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
browserCommand =
config->Read ( _T ( "browserCommand" ), wxEmptyString );
// if default value != true, type as long int
long valZoom, longFalse;
longFalse = false;
valZoom = 0;
frameWidth = frameHeight = framePosX = framePosY = 0;
properties.wrap =
config->Read ( _T ( "wrap" ), longFalse );
properties.zoom =
config->Read ( _T ( "zoom" ), valZoom );
properties.colorScheme = config->Read ( _T ( "colorScheme" ), COLOR_SCHEME_DEFAULT );
globalReplaceAllDocuments =
config->Read ( _T ( "globalReplaceAllDocuments" ), longFalse );
showFullPathOnFrame =
config->Read ( _T ( "showFullPathOnFrame" ), longFalse );
findMatchCase =
config->Read ( _T ( "findMatchCase" ), longFalse );
commandSync = config->Read ( _T ( "commandSync" ), longFalse );
commandOutput = config->Read ( _T ( "commandOutput" ), ID_COMMAND_OUTPUT_IGNORE );
commandString = config->Read ( _T ( "commandString" ), wxEmptyString );
ruleSetPreset =
config->Read ( _T ( "ruleSetPreset" ), _ ( "Default dictionary and style" ) );
filterPreset =
config->Read ( _T ( "filterPreset" ), _ ( "(No filter)" ) );
findData.SetFindString ( config->Read ( _T ( "findReplaceFind" ), _T ( "" ) ) );
findData.SetReplaceString ( config->Read ( _T ( "findReplaceReplace" ), _T ( "" ) ) );
toolbarVisible =
config->Read ( _T ( "toolbarVisible" ), true );
protectTags = config->Read ( _T ( "protectTags" ), longFalse );
visibilityState = config->Read ( _T ( "visibilityState" ), ID_SHOW_TAGS );
framePosX = config->Read ( _T ( "framePosX" ), framePosX );
framePosY = config->Read ( _T ( "framePosY" ), framePosY );
frameWidth = config->Read ( _T ( "frameWidth" ), frameWidth );
frameHeight = config->Read ( _T ( "frameHeight" ), frameHeight );
rememberOpenTabs = config->Read ( _T ( "rememberOpenTabs" ), true );
libxmlNetAccess = config->Read ( _T ( "libxmlNetAccess" ), longFalse );
openTabsOnClose = config->Read ( _T ( "openTabsOnClose" ), _T ( "" ) );
notebookStyle = config->Read ( _T ( "notebookStyle" ), ID_NOTEBOOK_STYLE_VC8_COLOR );
saveBom = config->Read ( _T ( "saveBom" ), true );
unlimitedUndo = config->Read ( _T ( "unlimitedUndo" ), true );
layout = config->Read ( _T ( "layout" ), wxEmptyString );
restoreLayout = config->Read ( _T ( "restoreLayout" ), true );
showLocationPane = config->Read ( _T ( "showLocationPane" ), true );
showInsertChildPane = config->Read ( _T ( "showInsertChildPane" ), true );
showInsertSiblingPane = config->Read ( _T ( "showInsertSiblingPane" ), true );
showInsertEntityPane = config->Read ( _T ( "showInsertEntityPane" ), true );
expandInternalEntities = config->Read ( _T ( "expandInternalEntities" ), true );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
useCoolBar = config->Read ( _T ( "useCoolBar" ), true );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
else // config not found
{
properties.insertCloseTag =
properties.completion =
properties.currentLine =
properties.indentLines =
properties.protectHiddenElements =
properties.toggleLineBackground =
properties.deleteWholeTag =
properties.highlightSyntax = true;
properties.font = defaultFont;
properties.wrap = properties.whitespaceVisible = false;
properties.zoom = 0;
properties.colorScheme = COLOR_SCHEME_DEFAULT;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
applicationDir = wxGetCwd();
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
applicationDir = GetLinuxAppDir::run();//getLinuxApplicationDir();
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
ruleSetPreset = _ ( "Default dictionary and style" );
filterPreset = _ ( "No filter" );
xpathExpression = lastXslStylesheet = lastRelaxNGSchema = wxEmptyString;
findRegex = true;
findMatchCase = globalReplaceAllDocuments =
showFullPathOnFrame = false;
toolbarVisible = true;
protectTags = false;
visibilityState = SHOW_TAGS;
framePosX = framePosY = frameWidth = frameHeight = 0;
rememberOpenTabs = true;
libxmlNetAccess = false;
openTabsOnClose = wxEmptyString;
browserCommand = wxEmptyString;
notebookStyle = ID_NOTEBOOK_STYLE_VC8_COLOR;
saveBom = unlimitedUndo = true;
layout = wxEmptyString;
restoreLayout = true;
showLocationPane = true;
showInsertChildPane = true;
showInsertSiblingPane = true;
showInsertEntityPane = true;
expandInternalEntities = true;
properties.validateAsYouType = true;
commandSync = false;
commandOutput = ID_COMMAND_OUTPUT_IGNORE;
commandString = wxEmptyString;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
useCoolBar = true;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
largeFileProperties.completion = false;
largeFileProperties.fold = false;
largeFileProperties.whitespaceVisible = false;
largeFileProperties.wrap = false;
largeFileProperties.indentLines = false;
largeFileProperties.protectHiddenElements = false;
largeFileProperties.toggleLineBackground = false;
largeFileProperties.toggleLineBackground = false;
largeFileProperties.insertCloseTag = false;
largeFileProperties.deleteWholeTag = false;
largeFileProperties.highlightSyntax = false;
largeFileProperties.validateAsYouType = false;
largeFileProperties.number = properties.number;
largeFileProperties.currentLine = properties.currentLine;
largeFileProperties.font = properties.font;
largeFileProperties.zoom = 0;
largeFileProperties.colorScheme = COLOR_SCHEME_NONE;
updatePaths();
loadBitmaps();
size_t findFlags = 0;
findFlags |= wxFR_DOWN;
if ( findMatchCase )
findFlags |= wxFR_MATCHCASE;
findData.SetFlags ( findFlags );
if ( browserCommand.empty() )
{
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
browserCommand = binDir + _T ( "navigate.exe" );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
browserCommand = getLinuxBrowser();
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// initialise document count for tab labels
documentCount = 1;
SetIcon ( wxICON ( appicon ) );
CreateStatusBar();
wxStatusBar *status = GetStatusBar();
int widths[] = { -24, -6, -6, -6, -8 };
status->SetFieldsCount ( 5 );
status->SetStatusWidths ( 5, widths );
if ( !frameWidth ||
!frameHeight ||
frameWidth < 0 ||
frameHeight < 0 ||
framePosX < 0 ||
framePosY < 0 )
{
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
Maximize();
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
SetSize ( 50, 50, 640, 480 );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
}
else
{
SetSize ( framePosX, framePosY, frameWidth, frameHeight );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
stylePosition = aboutPosition = wxDefaultPosition;
styleSize = wxSize ( 720, 540 );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
useCoolBarOnStart = useCoolBar;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
showTopBars ( toolbarVisible );
long style = wxAUI_NB_TOP |
wxAUI_NB_TAB_SPLIT |
wxAUI_NB_TAB_MOVE |
wxAUI_NB_WINDOWLIST_BUTTON |
wxAUI_NB_CLOSE_ON_ALL_TABS |
wxNO_BORDER;
mainBook = new MyNotebook (
this,
ID_NOTEBOOK,
wxDefaultPosition,
wxDefaultSize,
style );
manager.AddPane ( mainBook, wxAuiPaneInfo().CenterPane()
.PaneBorder ( false ).Name ( _T ( "documentPane" ) ) );
manager.GetPane ( mainBook ).dock_proportion = 10;
// add insert child panes
locationPanel = new LocationPanel ( this, ID_LOCATION_PANEL );
insertChildPanel = new InsertPanel ( this, ID_INSERT_CHILD_PANEL,
INSERT_PANEL_TYPE_CHILD );
insertSiblingPanel = new InsertPanel ( this, ID_INSERT_SIBLING_PANEL,
INSERT_PANEL_TYPE_SIBLING );
insertEntityPanel = new InsertPanel ( this, ID_INSERT_ENTITY_PANEL,
INSERT_PANEL_TYPE_ENTITY );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
manager.AddPane ( ( wxWindow * ) locationPanel, wxRIGHT, _ ( "Current Element" ) );
manager.AddPane ( ( wxWindow * ) insertChildPanel, wxRIGHT, _ ( "Insert Element" ) );
manager.AddPane ( ( wxWindow * ) insertSiblingPanel, wxRIGHT, _ ( "Insert Sibling" ) );
manager.AddPane ( ( wxWindow * ) insertEntityPanel, wxRIGHT, _ ( "Insert Entity" ) );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
manager.AddPane ( ( wxWindow * ) insertEntityPanel, wxRIGHT, _ ( "Insert Entity" ) );
manager.AddPane ( ( wxWindow * ) insertSiblingPanel, wxRIGHT, _ ( "Insert Sibling" ) );
manager.AddPane ( ( wxWindow * ) insertChildPanel, wxRIGHT, _ ( "Insert Element" ) );
manager.AddPane ( ( wxWindow * ) locationPanel, wxRIGHT, _ ( "Current Element" ) );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
manager.GetPane ( locationPanel ).Name ( _T ( "locationPane" ) ).Show (
( restoreLayout ) ? showLocationPane : true ).DestroyOnClose ( false ).PinButton ( true );
manager.GetPane ( locationPanel ).dock_proportion = 1;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
manager.GetPane ( insertChildPanel ).Name ( _T ( "insertChildPane" ) ).Show (
( restoreLayout ) ? showInsertChildPane : true ).DestroyOnClose ( false ).PinButton ( true );
manager.GetPane ( insertChildPanel ).dock_proportion = 1;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
manager.GetPane ( insertSiblingPanel ).Name ( _T ( "insertSiblingPane" ) ).Show (
( restoreLayout ) ? showInsertSiblingPane : true ).DestroyOnClose ( false ).PinButton ( true );
manager.GetPane ( insertSiblingPanel ).dock_proportion = 1;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
manager.GetPane ( insertEntityPanel ).Name ( _T ( "insertEntityPane" ) ).Show (
( restoreLayout ) ? showInsertEntityPane : true ).DestroyOnClose ( false ).PinButton ( true );
manager.GetPane ( insertEntityPanel ).dock_proportion = 1;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// add (hidden) message pane
htmlReport = new MyHtmlPane (
this,
ID_VALIDATION_PANE,
wxDefaultPosition,
wxSize ( -1, 48 ) );
2007-09-07 23:17:30 +02:00
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
const int sizeArray[] =
{
8, 9, 10, 11, 12, 13, 14
};
htmlReport->SetFonts ( wxEmptyString, wxEmptyString, sizeArray );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
htmlReport->SetBorders ( 0 );
manager.AddPane ( htmlReport, wxAuiPaneInfo().Movable().Bottom()
.Hide().Name ( _T ( "messagePane" ) )
.DestroyOnClose ( false ).Layer ( 1 ) );
manager.GetPane ( htmlReport ).dock_proportion = 1;
2007-09-07 23:17:30 +02:00
#ifdef NEWFINDREPLACE
2007-09-08 00:25:30 +02:00
findReplacePanel = new FindReplacePanel (
this,
ID_FIND_REPLACE_PANEL,
&findData,
true,
findRegex );
manager.AddPane (
( wxWindow * ) findReplacePanel,
wxAuiPaneInfo().Bottom().Hide().Caption ( wxEmptyString ).
DestroyOnClose ( false ).Layer ( 2 ) );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
commandPanel = new CommandPanel (
this,
wxID_ANY,
commandString, // tbd
commandSync, // tbd
commandOutput // tbd
);
manager.AddPane (
( wxWindow * ) commandPanel,
wxAuiPaneInfo().Bottom().Hide().Caption ( _T ( "Command" ) ).DestroyOnClose ( false ).Layer ( 3 ) );
if ( !wxFileName::DirExists ( applicationDir ) )
GetStatusBar()->SetStatusText ( _ ( "Cannot open application directory: see Tools, Options..., General" ) );
// handle command line and, on Windows, MS Word integration
handleCommandLineFlag = ( wxTheApp->argc > 1 ) ? true : false;
if ( rememberOpenTabs && !openTabsOnClose.empty() )
openRememberedTabs();
else
{
if ( !handleCommandLineFlag )
newDocument ( wxEmptyString );
}
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
DragAcceptFiles ( true ); // currently Windows only
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
XmlDoc *doc = getActiveDocument();
insertEntityPanel->update ( doc ); // NULL is ok
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
manager.Update();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
defaultLayout = manager.SavePerspective();
// restore layout if req'd
if (restoreLayout && !layout.empty())
{
if (!manager.LoadPerspective(layout, true))
manager.LoadPerspective(defaultLayout, true);
}
*/
2007-09-07 23:17:30 +02:00
}
MyFrame::~MyFrame()
{
2007-09-08 00:25:30 +02:00
std::vector<wxString>::iterator it;
for ( it = tempFileVector.begin(); it != tempFileVector.end(); it++ )
wxRemoveFile ( *it );
layout = manager.SavePerspective();
if ( !config )
return;
history.Save ( *config );
config->Write ( _T ( "insertCloseTag" ), properties.insertCloseTag );
config->Write ( _T ( "completion" ), properties.completion );
config->Write ( _T ( "number" ), properties.number );
config->Write ( _T ( "fold" ), properties.fold );
config->Write ( _T ( "currentLine" ), properties.currentLine );
config->Write ( _T ( "whitespaceVisible" ), properties.whitespaceVisible );
config->Write ( _T ( "wrap" ), properties.wrap );
config->Write ( _T ( "indentLines" ), properties.indentLines );
config->Write ( _T ( "zoom" ), properties.zoom );
config->Write ( _T ( "colorScheme" ), properties.colorScheme );
config->Write ( _T ( "protectHiddenElements" ), properties.protectHiddenElements );
config->Write ( _T ( "toggleLineBackground" ), properties.toggleLineBackground );
config->Write ( _T ( "deleteWholeTag" ), properties.deleteWholeTag );
config->Write ( _T ( "validateAsYouType" ), properties.validateAsYouType );
config->Write ( _T ( "font" ), properties.font );
config->Write ( _T ( "highlightSyntax" ), properties.highlightSyntax );
config->Write ( _T ( "applicationDir" ), applicationDir );
config->Write ( _T ( "ruleSetPreset" ), ruleSetPreset );
config->Write ( _T ( "filterPreset" ), filterPreset );
config->Write ( _T ( "xpathExpression" ), xpathExpression );
config->Write ( _T ( "findReplaceFind" ), findData.GetFindString() );
config->Write ( _T ( "findReplaceReplace" ), findData.GetReplaceString() );
config->Write ( _T ( "globalReplaceAllDocuments" ), globalReplaceAllDocuments );
config->Write ( _T ( "showFullPathOnFrame" ), showFullPathOnFrame );
config->Write ( _T ( "toolbarVisible" ), toolbarVisible );
config->Write ( _T ( "protectTags" ), protectTags );
config->Write ( _T ( "visibilityState" ), visibilityState );
config->Write ( _T ( "browserCommand" ), browserCommand );
config->Write ( _T ( "layout" ), layout );
config->Write ( _T ( "showLocationPane" ), manager.GetPane ( locationPanel ).IsShown() );
config->Write ( _T ( "showInsertChildPane" ), manager.GetPane ( insertChildPanel ).IsShown() );
config->Write ( _T ( "showInsertSiblingPane" ), manager.GetPane ( insertSiblingPanel ).IsShown() );
config->Write ( _T ( "showInsertEntityPane" ), manager.GetPane ( insertEntityPanel ).IsShown() );
config->Write ( _T ( "expandInternalEntities" ), expandInternalEntities );
config->Write ( _T ( "findRegex" ), findReplacePanel->getRegex() );
config->Write ( _T ( "findMatchCase" ), ( findData.GetFlags() ) & wxFR_MATCHCASE );
config->Write ( _T ( "commandSync" ), commandPanel->getSync() );
config->Write ( _T ( "commandOutput" ), commandPanel->getOutput() );
config->Write ( _T ( "commandString" ), commandPanel->getCommand() );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
config->Write ( _T ( "useCoolBar" ), useCoolBar );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
config->Write ( _T ( "restoreLayout" ), restoreLayout );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
config->Write ( _T ( "lastXslStylesheet" ), lastXslStylesheet );
config->Write ( _T ( "lastRelaxNGSchema" ), lastRelaxNGSchema );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
GetPosition ( &framePosX, &framePosY );
config->Write ( _T ( "framePosX" ), framePosX );
config->Write ( _T ( "framePosY" ), framePosY );
GetSize ( &frameWidth, &frameHeight );
config->Write ( _T ( "frameWidth" ), frameWidth );
config->Write ( _T ( "frameHeight" ), frameHeight );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
config->Write ( _T ( "rememberOpenTabs" ), rememberOpenTabs );
config->Write ( _T ( "openTabsOnClose" ), openTabsOnClose );
config->Write ( _T ( "libxmlNetAccess" ), libxmlNetAccess );
config->Write ( _T ( "singleInstanceCheck" ), singleInstanceCheck );
config->Write ( _T ( "lang" ), lang );
config->Write ( _T ( "notebookStyle" ), notebookStyle );
config->Write ( _T ( "saveBom" ), saveBom );
config->Write ( _T ( "unlimitedUndo" ), unlimitedUndo );
manager.UnInit();
2007-09-07 23:17:30 +02:00
}
wxString MyFrame::getLinuxBrowser()
{
2007-09-08 00:25:30 +02:00
wxString s;
const int stringArrayLen = 9;
wxString stringArray[stringArrayLen];
stringArray[0] = _T ( "/usr/bin/firefox" );
stringArray[1] = _T ( "/usr/bin/mozilla" );
stringArray[2] = _T ( "/usr/bin/opera" );
stringArray[3] = _T ( "/usr/bin/dillo" );
stringArray[4] = _T ( "/opt/gnome/bin/epiphany" );
stringArray[5] = _T ( "/opt/gnome/bin/galeon" );
stringArray[6] = _T ( "/opt/kde/bin/konqueror" );
stringArray[7] = _T ( "/opt/mozilla/bin/firefox" );
stringArray[8] = wxEmptyString; // empty option is safe
for ( int i = 0; i < stringArrayLen; i++ )
{
s = stringArray[i];
if ( wxFileName::FileExists ( s ) )
break;
}
return s;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::showTopBars ( bool b )
2007-09-07 23:17:30 +02:00
{
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
if ( useCoolBarOnStart )
{
if ( coolBar )
{
manager.DetachPane ( coolBar );
manager.Update();
coolBar->ShowBand ( 1, b );
manager.AddPane ( coolBar, wxAuiPaneInfo().Top().CaptionVisible ( false ).Name ( _T ( "coolBar" ) ) );
}
else
{
toolBar = getToolBar();
SetToolBar ( NULL );
if ( toolBar && protectTags )
toolBar->ToggleTool ( ID_PROTECT_TAGS, protectTags );
menuBar = getMenuBar();
coolBar = new wxCoolBar ( this, -1 );
coolBar->AddBand ( menuBar, false, wxEmptyString, true );
coolBar->AddBand ( toolBar, true, wxEmptyString, true );
coolBar->ShowBand ( 1, b );
manager.AddPane ( coolBar, wxAuiPaneInfo().Top().CaptionVisible ( false ).Name ( _T ( "coolBar" ) ) );
}
return;
}
#endif
if ( !menuBar )
{
SetToolBar ( NULL );
menuBar = getMenuBar();
SetMenuBar ( menuBar );
}
if ( b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !toolBar )
toolBar = getToolBar();
SetToolBar ( toolBar );
2007-09-07 23:17:30 +02:00
}
else
{
2007-09-08 00:25:30 +02:00
SetToolBar ( NULL );
delete toolBar;
toolBar = NULL;
2007-09-07 23:17:30 +02:00
}
}
void MyFrame::handleCommandLine()
{
2007-09-08 00:25:30 +02:00
bool wordFlag, styleFlag;
wordFlag = styleFlag = false;
wxChar c;
int m_argc = wxTheApp->argc;
wxChar **m_argv = wxTheApp->argv;
while ( ( --m_argc > 0 && ( *++m_argv ) [0] == L'-' ) != 0 )
{
wxString wideVersion ( ABOUT_VERSION );
std::string version = ( const char * ) wideVersion.mb_str ( wxConvUTF8 );
while ( ( c = *++m_argv[0] ) != 0 )
{
switch ( c )
{
case L'w':
wordFlag = true;
break;
case L's':
styleFlag = true;
break;
case L'-':
if ( *++m_argv[0] == L'v' )
{
std::cout << version.c_str() << std::endl;
}
else
{
std::cout << "Usage: xmlcopyeditor [--version --help -ws] [<file>] [<file2>]" << std::endl << "Options -w (import Microsoft Word document) and -s (open Spelling and style check) are provided for integration with Microsoft Office and only available on Windows" << std::endl;
}
exit ( 0 );
default:
messagePane ( _ ( "Unknown command line switch (expecting 'w', 's', --version or --help)" ),
CONST_STOP );
return;
}
}
}
if ( ! ( *m_argv ) )
{
messagePane ( _ ( "Command line processing incomplete: no file specified" ),
CONST_STOP );
return;
}
wxString fileName;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// no flags specified or not Windows
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
if ( !styleFlag && !wordFlag )
2007-09-07 23:17:30 +02:00
#endif
{
2007-09-08 00:25:30 +02:00
for ( ; *m_argv; ++m_argv )
{
fileName = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
fileName = PathResolver::run ( fileName );
if ( isOpen ( fileName ) )
continue;
else if ( !openFile ( fileName ) )
break;
}
return;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
// options only available on Windows
fileName = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch as many parameters as possible
for ( ;; )
{
++m_argv;
if ( ! ( *m_argv ) )
break;
ruleSetPreset = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
++m_argv;
if ( ! ( *m_argv ) )
break;
filterPreset = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
++m_argv;
if ( ! ( *m_argv ) )
break;
applicationDir = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
updatePaths();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
break;
}
if ( wordFlag )
importMSWord ( fileName );
else
openFile ( fileName );
if ( styleFlag && !ruleSetPreset.empty() && !filterPreset.empty() )
{
wxCommandEvent e;
OnSpelling ( e );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
bool MyFrame::isOpen ( const wxString& fileName )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
return ( openFileSet.find ( fileName ) != openFileSet.end() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::activateTab ( const wxString& fileName )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int pageCount = mainBook->GetPageCount();
XmlDoc *currentDoc;
for ( int i = 0; i < pageCount; ++i )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( !currentDoc )
break;
if ( currentDoc->getFullFileName() == fileName )
{
mainBook->SetSelection ( i );
break;
}
2007-09-07 23:17:30 +02:00
}
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnAbout ( wxCommandEvent& WXUNUSED ( event ) )
{
wxAboutDialogInfo info;
info.SetName ( _ ( "XML Copy Editor" ) );
info.SetWebSite ( _T ( "http://xml-copy-editor.sourceforge.net" ) );
info.SetVersion ( ABOUT_VERSION );
info.SetCopyright ( ABOUT_COPYRIGHT );
info.AddDeveloper ( _ ( "Gerald Schmidt (development) <gnschmidt@users.sourceforge.net>" ) );
info.AddDeveloper ( _ ( "Matt Smigielski (testing) <alectrus@users.sourceforge.net>" ) );
2007-09-13 00:44:19 +02:00
info.AddDeveloper ( _ ( "Justin Dearing (development) <j-pimp@users.sourceforge.net>" ) );
2007-09-08 00:25:30 +02:00
info.AddTranslator ( _ ( "Viliam Búr (Slovak) <viliam@bur.sk>" ) );
info.AddTranslator ( _ ( "David Håsäther (Swedish) <hasather@gmail.com>" ) );
info.AddTranslator ( _ ( "François Badier (French) <frabad@gmail.com>" ) );
info.AddTranslator ( _ ( "Thomas Wenzel (German) <thowen@users.sourceforge.net>" ) );
info.AddTranslator ( _ ( "SHiNE CsyFeK (Chinese Simplified) <csyfek@gmail.com>" ) );
info.AddTranslator ( _ ( "HSU PICHAN, YANG SHUFUN, CHENG PAULIAN, CHUANG KUO-PING, Marcus Bingenheimer (Chinese Traditional)" ) );
info.AddTranslator ( _ ( "Serhij Dubyk (Ukrainian) <dubyk@library.lviv.ua>" ) );
info.SetLicense ( ABOUT_LICENSE );
info.SetDescription ( ABOUT_DESCRIPTION );
wxAboutBox ( info );
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnCheckWellformedness ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string utf8Buffer;
getRawText ( doc, utf8Buffer );
if ( utf8Buffer.empty() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// handle unusual encodings
if ( !XmlEncodingHandler::setUtf8 ( utf8Buffer ) )
{
encodingMessage();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->clearErrorIndicators();
statusProgress ( _ ( "Parse in progress..." ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// check for well-formedness
auto_ptr<WrapExpat> we ( new WrapExpat() );
if ( !we->parse ( utf8Buffer.c_str() ) )
{
std::string error = we->getLastError();
wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
statusProgress ( wxEmptyString );
messagePane ( werror, CONST_WARNING );
std::pair<int, int> posPair = we->getErrorPosition();
-- ( posPair.first );
int cursorPos =
doc->PositionFromLine ( posPair.first );
doc->SetSelection ( cursorPos, cursorPos );
doc->setErrorIndicator ( posPair.first, posPair.second );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
documentOk ( _ ( "well-formed" ) );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPageClosing ( wxAuiNotebookEvent& event ) //wxNotebookEvent& event)//wxFlatNotebookEvent& event)
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
deletePageVetoed = false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( insertChildPanel && insertSiblingPanel && locationPanel )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
insertChildPanel->update ( NULL, wxEmptyString );
insertSiblingPanel->update ( NULL, wxEmptyString );
locationPanel->update();
manager.Update();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
doc = ( XmlDoc * ) mainBook->GetPage ( event.GetSelection() );
if ( !doc )
return;
statusProgress ( wxEmptyString );
closePane();
if ( doc->GetModify() ) //CanUndo())
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int selection;
wxString fileName;
if ( ( selection = mainBook->GetSelection() ) != -1 )
fileName = doc->getShortFileName();
int answer = wxMessageBox (
_ ( "Do you want to save the changes to " ) + fileName + _T ( "?" ),
_ ( "XML Copy Editor" ),
wxYES_NO | wxCANCEL | wxICON_QUESTION,
this );
if ( answer == wxCANCEL )
{
event.Veto();
deletePageVetoed = true;
return;
}
else if ( answer == wxYES )
{
wxCommandEvent event;
OnSave ( event );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
openFileSet.erase ( doc->getFullFileName() );
event.Skip();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnClose ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
closeActiveDocument();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnCloseAll ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !mainBook )
return;
openTabsOnClose = wxEmptyString;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// retain tab order
if ( rememberOpenTabs && !openFileSet.empty() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
wxString fullPath;
size_t maxTabs = mainBook->GetPageCount();
for ( size_t i = 0; i < maxTabs; ++i )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
doc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( doc )
{
fullPath = doc->getFullFileName();
if ( !fullPath.empty() )
{
openTabsOnClose.Append ( fullPath );
openTabsOnClose.Append ( _T ( "|" ) );
}
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
while ( closeActiveDocument() )
;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnClosePane ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
closePane();
//closeFindReplacePane();
//closeCommandPane();
XmlDoc *doc = getActiveDocument();
if ( doc )
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
void MyFrame::closePane()
{
2007-09-08 00:25:30 +02:00
if ( !htmlReport )
return;
manager.GetPane ( htmlReport ).Hide();
manager.Update();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
void MyFrame::closeFindReplacePane()
{
2007-09-08 00:25:30 +02:00
if ( manager.GetPane ( findReplacePanel ).IsShown() )
manager.GetPane ( findReplacePanel ).Hide();
manager.Update();
2007-09-07 23:17:30 +02:00
}
void MyFrame::closeCommandPane()
{
2007-09-08 00:25:30 +02:00
if ( manager.GetPane ( commandPanel ).IsShown() )
manager.GetPane ( commandPanel ).Hide();
manager.Update();
2007-09-07 23:17:30 +02:00
}
bool MyFrame::panelHasFocus()
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc = getActiveDocument();
return ( !doc || ( FindFocus() != ( wxWindow * ) doc ) );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnCut ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( panelHasFocus() )
{
event.Skip();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( protectTags )
doc->adjustSelection();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->Cut();
doc->setValidationRequired ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnCopy ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( panelHasFocus() )
{
event.Skip();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->Copy();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPaste ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( panelHasFocus() )
{
event.Skip();
return;
}
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// this has to be handled here to override Scintilla's default Ctrl+V support
if ( protectTags )
{
if ( !wxTheClipboard->Open() || !wxTheClipboard->IsSupported ( wxDF_TEXT ) )
return;
wxTextDataObject data;
wxTheClipboard->GetData ( data );
wxString buffer = data.GetText();
xmliseWideTextNode ( buffer );
doc->adjustCursor();
doc->AddText ( buffer );
}
else
doc->Paste();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
XmlDoc *doc;
doc = getActiveDocument();
if (doc && protectTags)
doc->adjustCursor();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->setValidationRequired(true);
event.Skip(); // new
*/
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnIdle ( wxIdleEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxStatusBar *status = GetStatusBar();
if ( !status )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// update attributes hidden field even if no document loaded
wxString currentHiddenStatus = status->GetStatusText ( STATUS_HIDDEN );
if ( visibilityState == HIDE_ATTRIBUTES )
{
if ( currentHiddenStatus != _ ( "Attributes hidden" ) )
status->SetStatusText (
_ ( "Attributes hidden" ),
STATUS_HIDDEN );
}
else if ( visibilityState == HIDE_TAGS )
{
if ( currentHiddenStatus != _ ( "Tags hidden" ) )
status->SetStatusText (
_ ( "Tags hidden" ),
STATUS_HIDDEN );
}
else
{
if ( !currentHiddenStatus.empty() )
status->SetStatusText ( wxEmptyString, STATUS_HIDDEN );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// update protected field even if no document loaded
wxString currentProtectedStatus = status->GetStatusText ( STATUS_PROTECTED );
if ( protectTags )
{
if ( currentProtectedStatus != _ ( "Tags locked" ) )
status->SetStatusText (
_ ( "Tags locked" ),
STATUS_PROTECTED );
}
else
{
if ( !currentProtectedStatus.empty() )
status->SetStatusText ( wxEmptyString, STATUS_PROTECTED );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// check if document loaded
wxString frameTitle = GetTitle();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( lastDoc != NULL )
{
lastDoc = NULL;
status->SetStatusText ( wxEmptyString, STATUS_MODIFIED );
status->SetStatusText ( wxEmptyString, STATUS_POSITION );
locationPanel->update ( NULL, wxEmptyString );
insertChildPanel->update ( NULL, wxEmptyString );
insertSiblingPanel->update ( NULL, wxEmptyString );
insertEntityPanel->update ( NULL, wxEmptyString );
wxString minimal = _ ( "XML Copy Editor" );
if ( frameTitle != minimal )
SetTitle ( minimal );
closeFindReplacePane();
event.Skip();
manager.Update();
}
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( restoreFocusToNotebook )
{
doc->SetFocus();
restoreFocusToNotebook = false;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
wxString docTitle;
if ( doc->getFullFileName().empty() || !showFullPathOnFrame )
docTitle = doc->getShortFileName();
else
docTitle = doc->getFullFileName();
docTitle += _T ( " - " );
docTitle += _ ( "XML Copy Editor" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( frameTitle != docTitle )
SetTitle ( docTitle );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// update modified field
if ( !mainBook )
return;
int index = mainBook->GetSelection();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString currentModifiedStatus = status->GetStatusText ( STATUS_MODIFIED );
wxString currentTabLabel = mainBook->GetPageText ( index );
if ( doc->GetModify() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( currentModifiedStatus != _ ( "Modified" ) )
{
status->SetStatusText ( _ ( "Modified" ), STATUS_MODIFIED );
if ( ! ( currentTabLabel.Mid ( 0, 1 ) == _T ( "*" ) ) )
{
currentTabLabel.Prepend ( _T ( "*" ) );
mainBook->SetPageText ( index, currentTabLabel );
}
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
else
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !currentModifiedStatus.empty() )
{
status->SetStatusText ( _T ( "" ), STATUS_MODIFIED );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( currentTabLabel.Mid ( 0, 1 ) == _T ( "*" ) )
{
currentTabLabel.Remove ( 0, 1 );
mainBook->SetPageText ( index, currentTabLabel );
}
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
// update coordinates field
std::pair<int, int> myControlCoordinates;
int current = doc->GetCurrentPos();
myControlCoordinates.first = doc->LineFromPosition ( current ) + 1;
myControlCoordinates.second = doc->GetColumn ( current ) + 1;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( myControlCoordinates != controlCoordinates )
{
wxString coordinates;
coordinates.Printf (
_ ( "Ln %i Col %i" ),
myControlCoordinates.first,
myControlCoordinates.second );
GetStatusBar()->SetStatusText ( coordinates, STATUS_POSITION );
controlCoordinates = myControlCoordinates;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// update parent element field
wxString parent, grandparent;
if ( current == lastPos && doc == lastDoc )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
lastPos = current;
lastDoc = doc;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// don't try to find parent if pane is not shown
if ( !manager.GetPane ( insertChildPanel ).IsShown() && !properties.validateAsYouType )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int parentCloseAngleBracket = -1;
if ( !doc->canInsertAt ( current ) )
parent = grandparent = wxEmptyString;
else
{
parentCloseAngleBracket = doc->getParentCloseAngleBracket ( current );
parent = doc->getLastElementName ( parentCloseAngleBracket );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() )
{
// tbd: limit to parent element
doc->shallowValidate ( doc->LineFromPosition ( current ), true );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( parent == lastParent )
return;
lastParent = parent;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool mustUpdate = false;
if ( locationPanel && insertChildPanel && insertEntityPanel )
{
locationPanel->update ( doc, parent );
insertChildPanel->update ( doc, parent );
insertEntityPanel->update ( doc );
mustUpdate = true;
}
if ( parent.empty() )
{
if ( insertSiblingPanel )
insertSiblingPanel->update ( doc, wxEmptyString );
if ( mustUpdate )
manager.Update();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !manager.GetPane ( insertSiblingPanel ).IsShown() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( mustUpdate )
manager.Update();
return;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
// try to fetch grandparent if necessary/possible
if ( !parent.empty() && parentCloseAngleBracket != -1 )
{
int grandParentCloseAngleBracket;
grandParentCloseAngleBracket =
doc->getParentCloseAngleBracket (
doc->getTagStartPos ( parentCloseAngleBracket ) );
grandparent = doc->getLastElementName ( grandParentCloseAngleBracket );
if ( insertSiblingPanel )
insertSiblingPanel->update ( doc, parent, grandparent );
if ( grandparent != lastGrandparent )
{
mustUpdate = true;
lastGrandparent = grandparent;
}
}
if ( mustUpdate )
manager.Update();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnInsertChild ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !insertChildPanel )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo info = manager.GetPane ( insertChildPanel );
if ( !info.IsOk() )
{
return;
}
if ( !info.IsShown() )
{
manager.GetPane ( insertChildPanel ).Show ( true );
manager.Update();
}
insertChildPanel->setEditFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnInsertSibling ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !insertSiblingPanel )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo info = manager.GetPane ( insertSiblingPanel );
if ( !info.IsOk() )
{
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !info.IsShown() )
{
manager.GetPane ( insertSiblingPanel ).Show ( true );
manager.Update();
}
insertSiblingPanel->setEditFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnInsertEntity ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !insertEntityPanel )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo info = manager.GetPane ( insertEntityPanel );
if ( !info.IsOk() )
{
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !info.IsShown() )
{
manager.GetPane ( insertEntityPanel ).Show ( true );
manager.Update();
}
insertEntityPanel->setEditFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnInsertSymbol ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
wxSymbolPickerDialog dlg ( _T ( "*" ), wxEmptyString, properties.font, this );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( dlg.ShowModal() == wxID_OK )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( dlg.HasSelection() )
{
doc->AddText ( dlg.GetSymbol() );
}
2007-09-07 23:17:30 +02:00
}
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnInsertTwin ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString parent = doc->getParent();
if ( !doc->insertSibling ( parent, parent ) )
{
wxString msg;
msg.Printf ( _T ( "Cannot insert twin '%s'" ), parent.c_str() );
messagePane ( msg, CONST_STOP );
}
doc->setValidationRequired ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPasteNewDocument ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !wxTheClipboard->Open() )
{
messagePane ( _ ( "Cannot open clipboard" ), CONST_STOP );
return;
}
if ( !wxTheClipboard->IsSupported ( wxDF_TEXT ) )
{
messagePane ( _ ( "Cannot paste as new document: no text on clipboard" ), CONST_STOP );
return;
}
wxTextDataObject data;
wxTheClipboard->GetData ( data );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString buffer = data.GetText();
xmliseWideTextNode ( buffer );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
buffer.Prepend ( _T ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>" ) );
buffer.Append ( _T ( "</root>\n" ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
newDocument ( buffer );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnDialogFind ( wxFindDialogEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
findAgain ( event.GetFindString(), event.GetFlags() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnDialogReplace ( wxFindDialogEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int regexWidth = 0;
if ( findReplacePanel->getRegex() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
regexWidth = doc->ReplaceTargetRE ( event.GetReplaceString() );
//doc->SetTargetStart(newLocation + regexWidth);
2007-09-07 23:17:30 +02:00
}
else
{
2007-09-08 00:25:30 +02:00
doc->ReplaceTarget ( event.GetReplaceString() );
//doc->SetTargetStart(newLocation + event.GetReplaceString().size());
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
/*
if (doc->GetSelectionStart() != doc->GetSelectionEnd())
doc->ReplaceSelection(event.GetReplaceString());
*/
OnDialogFind ( event );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnDialogReplaceAll ( wxFindDialogEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
int flags = 0;
if ( event.GetFlags() & wxFR_WHOLEWORD )
flags |= wxSTC_FIND_WHOLEWORD;
if ( event.GetFlags() & wxFR_MATCHCASE )
flags |= wxSTC_FIND_MATCHCASE;
if ( findReplacePanel->getRegex() )
flags |= wxSTC_FIND_REGEXP;
doc->SetTargetStart ( 0 );
doc->SetTargetEnd ( doc->GetLength() );
doc->SetSearchFlags ( flags );
int newLocation, replacementCount, regexWidth;
newLocation = replacementCount = regexWidth = 0;
while ( ( newLocation = doc->SearchInTarget ( event.GetFindString() ) ) != -1 )
{
if ( findReplacePanel->getRegex() )
{
regexWidth = doc->ReplaceTargetRE ( event.GetReplaceString() );
doc->SetTargetStart ( newLocation + regexWidth );
}
else
{
doc->ReplaceTarget ( event.GetReplaceString() );
doc->SetTargetStart ( newLocation + event.GetReplaceString().size() );
}
doc->SetTargetEnd ( doc->GetLength() );
++replacementCount;
}
wxString msg;
msg.Printf (
ngettext ( L"%i replacement made", L"%i replacements made", replacementCount ),
replacementCount );
statusProgress ( msg );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPrintSetup ( wxCommandEvent &WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !htmlPrinting.get() )
return;
htmlPrinting->PageSetup();
}
void MyFrame::OnPrintPreview ( wxCommandEvent &WXUNUSED ( event ) )
{
XmlDoc *doc;
if ( !htmlPrinting.get() || ( doc = getActiveDocument() ) == NULL )
return;
wxString shortFileName, header;
shortFileName = doc->getShortFileName();
if ( !shortFileName.empty() )
header = shortFileName + _T ( " " );
header += _T ( "(@PAGENUM@/@PAGESCNT@)<hr>" );
htmlPrinting->SetHeader (
header,
wxPAGE_ALL );
statusProgress ( _ ( "Preparing Print Preview..." ) );
wxString htmlBuffer = getHtmlBuffer();
statusProgress ( wxEmptyString );
if ( ! ( htmlPrinting->PreviewText ( htmlBuffer ) ) )
;
}
void MyFrame::OnPrint ( wxCommandEvent &WXUNUSED ( event ) )
{
XmlDoc *doc;
if ( !htmlPrinting.get() || ( doc = getActiveDocument() ) == NULL )
return;
wxString shortFileName, header;
shortFileName = doc->getShortFileName();
if ( !shortFileName.empty() )
header = shortFileName + _T ( " " );
header += _T ( "(@PAGENUM@/@PAGESCNT@)<hr>" );
htmlPrinting->SetHeader (
header,
wxPAGE_ALL );
statusProgress ( _ ( "Preparing to print..." ) );
wxString htmlBuffer = getHtmlBuffer();
statusProgress ( wxEmptyString );
if ( ! ( htmlPrinting->PrintText ( htmlBuffer ) ) )
;
2007-09-07 23:17:30 +02:00
}
wxString MyFrame::getHtmlBuffer()
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return _T ( "<html><head></head><body></body></html>" );
wxString buffer, htmlBuffer;
buffer = doc->GetText();
size_t size = buffer.size();
htmlBuffer.reserve ( size * 2 );
htmlBuffer = _T ( "<html><body><p>" );
bool startOfLine = true;
for ( size_t i = 0; i < size; ++i )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wchar_t c = buffer[i];
switch ( c )
{
case L' ':
htmlBuffer += ( startOfLine ) ? _T ( "&nbsp;" ) : _T ( " " );
break;
case L'\t':
htmlBuffer += _T ( "&nbsp;&nbsp;" );
break;
case L'<':
htmlBuffer += _T ( "&lt;" );
startOfLine = false;
break;
case L'>':
htmlBuffer += _T ( "&gt;" );
startOfLine = false;
break;
case L'\n':
htmlBuffer += _T ( "<br>" );
startOfLine = true;
break;
case L'&':
htmlBuffer + _T ( "&amp" );
startOfLine = false;
break;
default:
htmlBuffer += c;
startOfLine = false;
break;
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
htmlBuffer += _T ( "</p></body></html>" );
return htmlBuffer;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFind ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
#ifdef NEWFINDREPLACE
2007-09-08 00:25:30 +02:00
manager.GetPane ( findReplacePanel ).Caption ( _ ( "Find" ) );
bool visible = manager.GetPane ( findReplacePanel ).IsShown();
if ( !visible )
{
manager.GetPane ( findReplacePanel ).Show();
}
manager.Update();
findReplacePanel->refresh();
findReplacePanel->setReplaceVisible ( false );
findReplacePanel->focusOnFind();
return;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
if ( findDialog.get() )
{
findDialog = std::auto_ptr<wxFindReplaceDialog> ( 0 );
}
findDialog = ( std::auto_ptr<wxFindReplaceDialog> ( new wxFindReplaceDialog (
this,
&findData,
_ ( "Find" ) ) ) );
findDialog->Show();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnImportMSWord ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
return;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
std::auto_ptr<wxFileDialog> fd ( new wxFileDialog (
this,
_ ( "Import Microsoft Word Document" ),
_T ( "" ),
_T ( "" ),
_T ( "Microsoft Word (*.doc)|*.doc" ),
wxOPEN | wxFILE_MUST_EXIST | wxCHANGE_DIR
/*
#ifdef __WXMSW__
| wxHIDE_READONLY
#endif
*/
) );
if ( fd->ShowModal() == wxID_CANCEL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString path = fd->GetPath();
if ( path == _T ( "" ) )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
importMSWord ( path );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::importMSWord ( const wxString& path )
2007-09-07 23:17:30 +02:00
{
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
return;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( _T ( "" ) ), swapFileName ( _T ( "" ) );
wxString completeSwapFileName = swapFileName.wideName() + _T ( ".doc" );
if ( !wxCopyFile ( path, completeSwapFileName, true ) )
{
wxString message;
message.Printf ( _ ( "Cannot open %s for import" ), path.c_str() );
messagePane ( message, CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString cmd = binDir +
_T ( "doc2xml.exe \"" ) +
completeSwapFileName + _T ( "\" \"" ) +
tempFileName.wideName() + _T ( "\"" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _ ( "Import in progress..." ) );
int result = wxExecute ( cmd, wxEXEC_SYNC );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxRemoveFile ( completeSwapFileName ); // necessary because .doc extension added
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
wxString message;
wxString versionMessage (
_ ( "(lossless conversion requires version 2003 or later)" ) );
switch ( result )
{
2007-09-07 23:17:30 +02:00
case 0:
2007-09-08 00:25:30 +02:00
break;
2007-09-07 23:17:30 +02:00
case 1:
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "Cannot start Microsoft Word" ), CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 2:
2007-09-08 00:25:30 +02:00
messagePane (
_ ( "A more recent version of Microsoft Word is required" ), CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 3:
2007-09-08 00:25:30 +02:00
message.Printf ( _T ( "Microsoft Word cannot open %s" ), path.c_str() );
messagePane ( message + path, CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 4:
2007-09-08 00:25:30 +02:00
message.Printf ( _ ( "Microsoft Word cannot save %s as XML" ), path.c_str() );
messagePane ( message, CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 5:
2007-09-08 00:25:30 +02:00
messagePane (
_ ( "Microsoft Word cannot save this document as WordprocessingML " ) +
versionMessage,
CONST_INFO );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _ ( "Opening imported file..." ) );
std::string buffer;
wxString displayBuffer;
if ( result != 5 ) // Word 2003 or later
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
std::auto_ptr<WrapLibxml> prettyPrinter ( new WrapLibxml ( libxmlNetAccess ) );
prettyPrinter->parse ( tempFileName.name(), true );
buffer = prettyPrinter->getOutput();
displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
else // earlier versions
{
if ( !ReadFile::run ( tempFileName.name(), buffer ) )
{
statusProgress ( wxEmptyString );
messagePane ( _ ( "Cannot open imported file" ), CONST_STOP );
return;
}
displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
displayBuffer.Remove ( 0, 1 ); // remove byte order mark
xmliseWideTextNode ( displayBuffer );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
displayBuffer.Prepend (
_T ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>" ) );
displayBuffer.Append ( _T ( "</root>\n" ) );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
newDocument ( displayBuffer, tempFileName.wideName() );
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnExportMSWord ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
return;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName wtfn ( _T ( "" ) );
wxString sourceFileName = doc->getFullFileName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( sourceFileName.empty() )
{
sourceFileName = wtfn.wideName();
std::fstream ofs ( wtfn.name().c_str() );
if ( !ofs )
return;
std::string utf8Buffer;
getRawText ( doc, utf8Buffer );
ofs << utf8Buffer;
ofs.close();
}
else if ( doc->GetModify() ) //CanUndo())
{
modifiedMessage();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::auto_ptr<wxFileDialog> fd ( new wxFileDialog (
this,
_ ( "Export Microsoft Word Document" ),
_T ( "" ),
_T ( "" ),
_T ( "Microsoft Word (*.doc)|*.doc" ),
wxSAVE | wxOVERWRITE_PROMPT ) );
fd->ShowModal();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString path = fd->GetPath();
if ( path == _T ( "" ) )
return;
wxString cmd = binDir +
_T ( "xml2doc.exe -v \"" ) +
sourceFileName + _T ( "\" \"" ) +
path + _T ( "\"" );
statusProgress ( _ ( "Export in progress..." ) );
int result = wxExecute ( cmd, wxEXEC_SYNC );
statusProgress ( wxEmptyString );
wxString message;
switch ( result )
{
2007-09-07 23:17:30 +02:00
case 1:
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "Cannot start Microsoft Word" ), CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 2:
2007-09-08 00:25:30 +02:00
messagePane (
_ ( "A more recent version of Microsoft Word is required" ), CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 3:
2007-09-08 00:25:30 +02:00
message.Printf ( _ ( "Microsoft Word cannot save %s" ), path.c_str() );
messagePane ( message, CONST_STOP );
return;
2007-09-07 23:17:30 +02:00
case 0:
2007-09-08 00:25:30 +02:00
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnBrowser ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString sourceFileName = doc->getFullFileName();
WrapTempFileName wtfn ( sourceFileName, _T ( ".html" ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( sourceFileName.empty() || doc->GetModify() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
sourceFileName = wtfn.wideName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::ofstream ofs ( ( const char * ) wtfn.name().c_str() );
if ( !ofs )
{
messagePane ( _ ( "Cannot save temporary file" ), CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string utf8Buffer;
getRawText ( doc, utf8Buffer );
ofs << utf8Buffer;
ofs.close();
// keep files until application closes
tempFileVector.push_back ( sourceFileName );
tempFileVector.push_back ( wtfn.originalWideName() );
wtfn.setKeepFiles ( true );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
navigate ( sourceFileName );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnHelp ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
wxString cmd = _T ( "hh.exe \"" ) + helpDir + _T ( "xmlcopyeditor.chm\"" );
wxExecute ( cmd );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
wxString helpFileName = helpDir + _T ( "xmlcopyeditor.hhp" );
helpController->AddBook ( wxFileName ( helpFileName ) );
helpController->DisplayContents();
2007-09-07 23:17:30 +02:00
#endif
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnColorScheme ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int id = event.GetId();
switch ( id )
{
2007-09-07 23:17:30 +02:00
case ID_COLOR_SCHEME_DEFAULT:
2007-09-08 00:25:30 +02:00
properties.colorScheme = COLOR_SCHEME_DEFAULT;
break;
2007-09-07 23:17:30 +02:00
case ID_COLOR_SCHEME_DEFAULT_BACKGROUND:
2007-09-08 00:25:30 +02:00
properties.colorScheme = COLOR_SCHEME_DEFAULT_BACKGROUND;
break;
2007-09-07 23:17:30 +02:00
case ID_COLOR_SCHEME_REDUCED_GLARE:
2007-09-08 00:25:30 +02:00
properties.colorScheme = COLOR_SCHEME_REDUCED_GLARE;
break;
2007-09-07 23:17:30 +02:00
case ID_COLOR_SCHEME_NONE:
2007-09-08 00:25:30 +02:00
properties.colorScheme = COLOR_SCHEME_NONE;
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
return;
}
colorSchemeMenu->Check ( id, true );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
properties.zoom = doc->GetZoom(); // ensure temp changes to font size are kept
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
applyEditorProperties ( false );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFontSmaller ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->ZoomOut();
properties.zoom = doc->GetZoom();
applyEditorProperties ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFontMedium ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->SetZoom ( 0 );
properties.zoom = doc->GetZoom();
applyEditorProperties ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFontLarger ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->ZoomIn();
properties.zoom = doc->GetZoom();
applyEditorProperties ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnOptions ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
// ensure font size does not change after
XmlDoc *doc = getActiveDocument();
if ( doc )
{
properties.zoom = doc->GetZoom();
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString title ( _ ( "Options" ) );
std::auto_ptr<MyPropertySheet> mpsd ( new MyPropertySheet (
this,
properties,
applicationDir,
browserCommand,
rememberOpenTabs,
libxmlNetAccess,
singleInstanceCheck,
saveBom,
unlimitedUndo,
restoreLayout,
expandInternalEntities,
showFullPathOnFrame,
lang,
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
useCoolBar,
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
wxID_ANY,
title ) );
if ( mpsd->ShowModal() == wxID_OK )
{
properties = mpsd->getProperties();
applyEditorProperties();
applicationDir = mpsd->getApplicationDir();
browserCommand = mpsd->getBrowserCommand();
rememberOpenTabs = mpsd->getRememberOpenTabs();
libxmlNetAccess = mpsd->getLibxmlNetAccess();
singleInstanceCheck = mpsd->getSingleInstanceCheck();
saveBom = mpsd->getSaveBom();
unlimitedUndo = mpsd->getUnlimitedUndo();
restoreLayout = mpsd->getRestoreLayout();
expandInternalEntities = mpsd->getExpandInternalEntities();
showFullPathOnFrame = mpsd->getShowFullPathOnFrame();
lang = mpsd->getLang();
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
useCoolBar = mpsd->getUseCoolBar();
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
updatePaths();
}
if ( doc )
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnHistoryFile ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString f ( history.GetHistoryFile ( event.GetId() - wxID_FILE1 ) );
if ( !f.empty() )
openFile ( f );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnGoto ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxTextEntryDialog *dlg = new wxTextEntryDialog (
this,
_ ( "Enter line number:" ),
_ ( "Go To" ) );
int ret = dlg->ShowModal();
if ( ret == wxID_CANCEL )
return;
wxString val = dlg->GetValue();
long line;
if ( !val.ToLong ( &line ) || line < 1 )
{
wxString msg;
msg.Printf ( _ ( "'%s' is not a valid line number" ), val.c_str() );
messagePane ( msg, CONST_STOP );
return;
}
--line;
doc->GotoLine ( ( int ) line );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFindAgain ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
//findAgain(findData.GetFindString(), findData.GetFlags());
findReplacePanel->OnFindNext ( event );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnCommand ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
bool visible = manager.GetPane ( commandPanel ).IsShown();
if ( !visible )
{
manager.GetPane ( commandPanel ).Show();
}
manager.Update();
commandPanel->focusOnCommand();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFindReplace ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
#ifdef NEWFINDREPLACE
2007-09-08 00:25:30 +02:00
manager.GetPane ( findReplacePanel ).Caption ( _ ( "Replace" ) );
bool visible = manager.GetPane ( findReplacePanel ).IsShown();
if ( !visible )
{
manager.GetPane ( findReplacePanel ).Show();
}
manager.Update();
findReplacePanel->refresh();
findReplacePanel->setReplaceVisible ( true );
findReplacePanel->focusOnFind();
2007-09-07 23:17:30 +02:00
return;
2007-09-08 00:25:30 +02:00
#endif
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( findDialog.get() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
findDialog = std::auto_ptr<wxFindReplaceDialog> ( 0 );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
findDialog = std::auto_ptr<wxFindReplaceDialog> ( new wxFindReplaceDialog (
this,
&findData,
_ ( "Find and Replace" ),
wxFR_REPLACEDIALOG ) );
findDialog->Show();
}
void MyFrame::OnGlobalReplace ( wxCommandEvent& event )
{
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
2007-09-07 23:17:30 +02:00
return;
2007-09-08 00:25:30 +02:00
2007-09-07 23:17:30 +02:00
size_t flags = findData.GetFlags();
2007-09-08 00:25:30 +02:00
std::auto_ptr<GlobalReplaceDialog> grd ( new GlobalReplaceDialog (
this,
findData.GetFindString(),
findData.GetReplaceString(),
flags & wxFR_MATCHCASE,
globalReplaceAllDocuments,
findRegex ) );
int res = grd->ShowModal();
flags = 0;
flags |= wxFR_DOWN;
if ( grd->getMatchCase() )
flags |= wxFR_MATCHCASE;
findRegex = grd->getRegex();
globalReplaceAllDocuments = grd->getAllDocuments();
findData.SetFindString ( grd->getFindString() );
findData.SetReplaceString ( grd->getReplaceString() );
findData.SetFlags ( flags );
findReplacePanel->setRegex ( findRegex );
findReplacePanel->setMatchCase ( flags & wxFR_MATCHCASE );
findReplacePanel->refresh();
if ( res != wxID_OK )
2007-09-07 23:17:30 +02:00
{
return;
}
2007-09-08 00:25:30 +02:00
int globalMatchCount, pageCount;
globalMatchCount = 0;
pageCount = mainBook->GetPageCount();
XmlDoc *currentDoc = getActiveDocument();
if ( !currentDoc )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
for ( int i = 0; i < pageCount; ++i )
{
std::string bufferUtf8;
if ( !globalReplaceAllDocuments )
{
getRawText ( currentDoc, bufferUtf8 );
}
else
{
currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( !currentDoc )
return;
getRawText ( currentDoc, bufferUtf8 );
}
size_t flags = findData.GetFlags();
if ( !findRegex )
{
std::string findUtf8, replaceUtf8;
findUtf8 =findData.GetFindString().mb_str ( wxConvUTF8 );
replaceUtf8 = findData.GetReplaceString().mb_str ( wxConvUTF8 );
globalMatchCount += Replace::run (
bufferUtf8,
findUtf8,
replaceUtf8,
flags & wxFR_MATCHCASE );
currentDoc->SetTextRaw ( bufferUtf8.c_str() );
}
else
{
try
{
std::auto_ptr<WrapRegex> wr ( new WrapRegex (
( const char * ) findData.GetFindString().mb_str ( wxConvUTF8 ),
flags & wxFR_MATCHCASE,
( const char * ) findData.GetReplaceString().mb_str ( wxConvUTF8 ) ) );
int matchCount;
std::string outputBuffer = wr->replaceGlobal ( bufferUtf8, &matchCount );
globalMatchCount += matchCount;
currentDoc->SetTextRaw ( outputBuffer.c_str() );
}
catch ( std::exception& e )
{
wxString wideError = wxString ( e.what(), wxConvUTF8, strlen ( e.what() ) );
messagePane ( _ ( "Cannot replace: " ) + wideError, CONST_STOP );
return;
}
}
if ( !globalReplaceAllDocuments )
break;
}
wxString msg;
msg.Printf (
ngettext ( L"%i replacement made", L"%i replacements made", globalMatchCount ),
globalMatchCount );
statusProgress ( msg );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFrameClose ( wxCloseEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxCommandEvent e;
OnCloseAll ( e );
if ( mainBook->GetPageCount() )
{
event.Veto();
return;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
event.Skip();
}
void MyFrame::OnNew ( wxCommandEvent& WXUNUSED ( event ) )
{
wxString defaultSelection, typeSelection, templateFile;
defaultSelection = _ ( "XML document (*.xml)" );
wxArrayString templateArray;
if ( wxFileName::DirExists ( templateDir ) )
{
wxString templateMask, name, extension, entry;
templateMask = templateDir + wxFileName::GetPathSeparator() + _T ( "*.*" );
templateFile = wxFindFirstFile ( templateMask, wxFILE );
wxFileName fn;
if ( !templateFile.empty() )
{
fn.Assign ( templateFile );
name = fn.GetName();
extension = fn.GetExt();
entry.Printf ( _T ( "%s (*.%s)" ), name.c_str(), extension.c_str() );
templateArray.Add ( entry );
for ( ;; )
{
templateFile = wxFindNextFile();
if ( templateFile.empty() )
break;
fn.Assign ( templateFile );
name = fn.GetName();
extension = fn.GetExt();
entry.Printf ( _T ( "%s (*.%s)" ), name.c_str(), extension.c_str() );
templateArray.Add ( entry );
}
}
templateArray.Sort();
templateArray.Insert ( defaultSelection, 0 );
const int arraySize = templateArray.GetCount();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString choiceArray[arraySize + 1];
for ( int i = 0; i < arraySize; ++i )
* ( choiceArray + i ) = templateArray.Item ( i );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxSingleChoiceDialog scd (
this, _ ( "Choose a document type:" ), _ ( "New Document" ), arraySize, choiceArray );
if ( scd.ShowModal() == wxID_CANCEL )
{
XmlDoc *doc = getActiveDocument();
if ( doc )
doc->SetFocus();
return;
}
typeSelection = scd.GetStringSelection();
}
if ( typeSelection == defaultSelection )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString emptyString ( _T ( "" ) );
newDocument ( emptyString );
return;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
typeSelection.Replace ( _T ( " (*" ), wxEmptyString );
typeSelection.Replace ( _T ( ")" ), wxEmptyString );
templateFile = templateDir + typeSelection;
std::string templateFileLocal, buffer;
templateFileLocal = templateFile.mb_str ( wxConvLocal );
ReadFile::run ( templateFileLocal, buffer );
wxString documentContents = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
newDocument ( documentContents,
wxString ( templateFileLocal.c_str(), wxConvUTF8, templateFileLocal.size() ) );
}
void MyFrame::newDocument ( const wxString& s, const wxString& path, bool canSave )
{
std::string bufferUtf8 = ( const char * ) s.mb_str ( wxConvUTF8 );
std::string pathUtf8 = ( const char * ) path.mb_str ( wxConvUTF8 );
newDocument ( bufferUtf8, pathUtf8, canSave );
}
void MyFrame::newDocument ( const std::string& s, const std::string& path, bool canSave )
{
XmlDoc *doc;
wxString documentLabel;
documentLabel.Printf ( _ ( "Document%i" ), documentCount++ );
std::string auxPath = getAuxPath ( path );
Freeze();
doc = ( s.empty() ) ?
new XmlDoc (
mainBook,
properties,
&protectTags,
visibilityState,
FILE_TYPE_XML,
wxID_ANY,
NULL, 0 // new: NULL pointer leads to default document
)
: new XmlDoc (
mainBook,
properties,
&protectTags,
visibilityState,
FILE_TYPE_XML,
wxID_ANY,
s.c_str(), // modified
s.size(), // new
catalogPath,
path,
auxPath );
mainBook->AddPage ( ( wxWindow * ) doc, documentLabel, true );
Thaw();
mainBook->Refresh();
if ( properties.completion )
doc->updatePromptMaps();
doc->setShortFileName ( documentLabel );
doc->SetFocus();
manager.Update();
locationPanel->update ( doc, wxEmptyString );
insertChildPanel->update ( doc, wxEmptyString );
insertSiblingPanel->update ( doc, wxEmptyString );
insertEntityPanel->update ( doc );
if ( properties.validateAsYouType )
doc->shallowValidate();
}
void MyFrame::OnOpen ( wxCommandEvent& event )
{
bool largeFile;
largeFile = ( event.GetId() == ID_OPEN_LARGE_FILE );
wxString defaultFile, defaultDir;
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) != NULL )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
defaultFile = doc->getFullFileName();
if ( !defaultFile.empty() )
{
wxFileName fn ( defaultFile );
defaultDir = fn.GetPath();
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
wxFileDialog *fd = new wxFileDialog (
this,
( largeFile ) ? _ ( "Open Large Document" ) : _ ( "Open" ),
defaultDir,
wxEmptyString,
FILE_FILTER,
wxOPEN | wxMULTIPLE | wxFILE_MUST_EXIST | wxCHANGE_DIR
);
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( fd->ShowModal() == wxID_CANCEL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxArrayString paths;
fd->GetPaths ( paths );
size_t count = paths.Count();
if ( !count )
return;
for ( size_t i = 0; i < count; ++i )
if ( !openFile ( paths[i], largeFile ) )
break;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
bool MyFrame::openFile ( wxString& fileName, bool largeFile )
2007-09-07 23:17:30 +02:00
{
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
// truncate string up to file:/ portion added by GNOME
wxString filePrefix = _T ( "file:" );
int index = fileName.Find ( filePrefix.c_str() );
if ( index != -1 )
{
fileName = fileName.Mid ( ( size_t ) index + filePrefix.Length() );
}
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
if ( !wxFileName::FileExists ( fileName ) )
{
wxString message;
message.Printf ( _ ( "Cannot open %s" ), fileName.c_str() );
messagePane ( message, CONST_STOP );
return false;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( openFileSet.count ( fileName ) )
{
wxString message;
message.Printf ( _ ( "%s is already open" ), fileName.c_str() );
statusProgress ( message );
activateTab ( fileName );
return false;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString directory, name, extension;
wxFileName::SplitPath ( fileName, NULL, &directory, &name, &extension );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !extension.empty() )
{
name += _T ( "." );
name += extension;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString wideError;
std::string buffer;
pair<int, int> posPair;
XmlDoc *doc;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int type = getFileType ( fileName );
std::string auxPath = getAuxPath ( ( const char * ) fileName.mb_str ( wxConvLocal ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
char *docBuffer = 0;
size_t docBufferLen = 0;
bool fileEmpty = false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _T ( "Opening file..." ) );
//wxMemoryMappedFile *memorymap = NULL;
BinaryFile *binaryfile = NULL;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
try
{
binaryfile = new BinaryFile ( ( const char * ) fileName.mb_str ( wxConvLocal ) );
/*
memorymap = new wxMemoryMappedFile(
fileName,
true, // readOnly
true // fread
);
*/
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
}
/*
catch (wxMemoryMappedFileEmptyException&)
{
fileEmpty = true;
}
*/
catch ( ... )
{
wxString message;
message.Printf ( _ ( "Cannot open %s" ), fileName.c_str() );
messagePane ( message, CONST_STOP );
statusProgress ( wxEmptyString );
return false;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool isUtf8 = false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !fileEmpty )
{
docBuffer = ( char * ) binaryfile->getData();//memorymap->GetStream();
docBufferLen = binaryfile->getDataLen();//memorymap->GetMapSize();
}
else
{
docBuffer = NULL;
docBufferLen = 0;
isUtf8 = true;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
char *iconvBuffer = 0;
size_t iconvBufferLen = 0;
char *finalBuffer;
size_t finalBufferLen;
// adjust for UTF-8 BOM
if ( docBuffer &&
( unsigned char ) docBuffer[0] == 0xEF &&
( unsigned char ) docBuffer[1] == 0xBB &&
( unsigned char ) docBuffer[2] == 0xBF )
{
docBuffer += 3;
docBufferLen -= 3;
isUtf8 = true;
}
// no UTF-8 BOM found
std::string encoding;
if ( !isUtf8 || !binaryfile->getDataLen() )
{
XmlEncodingSpy es;
es.parse ( docBuffer, docBufferLen );
encoding = es.getEncoding();
if ( encoding == "UTF-8" || encoding == "US-ASCII" ) // US-ASCII is a subset of UTF-8
isUtf8 = true;
}
// convert buffer if not UTF-8
if ( isUtf8 )
{
finalBuffer = docBuffer;
finalBufferLen = docBufferLen;
}
else
{
// clear any other BOMs
if ( docBuffer && // UTF-32 BE
( unsigned char ) docBuffer[0] == 0x00 &&
( unsigned char ) docBuffer[1] == 0x00 &&
( unsigned char ) docBuffer[2] == 0xFE &&
( unsigned char ) docBuffer[3] == 0xFF )
{
docBuffer += 4;
docBufferLen -= 4;
}
if ( docBuffer && // UTF-32 LE
( unsigned char ) docBuffer[0] == 0xFF &&
( unsigned char ) docBuffer[1] == 0xFE &&
( unsigned char ) docBuffer[2] == 0x00 &&
( unsigned char ) docBuffer[3] == 0x00 )
{
docBuffer += 4;
docBufferLen -= 4;
}
if ( docBuffer && //UTF-16 BE
( unsigned char ) docBuffer[0] == 0xFE &&
( unsigned char ) docBuffer[1] == 0xFF )
{
docBuffer += 2;
docBufferLen -= 2;
}
if ( docBuffer && //UTF-16 LE
( unsigned char ) docBuffer[0] == 0xFF &&
( unsigned char ) docBuffer[1] == 0xFE )
{
docBuffer += 2;
docBufferLen -= 2;
}
if ( !encoding.size() ) // Expat couldn't parse file (e.g. UTF-32)
{
encoding = getApproximateEncoding ( docBuffer, docBufferLen );
}
wxString wideEncoding = wxString (
encoding.c_str(),
wxConvLocal,
encoding.size() );
iconv_t cd = iconv_open ( "UTF-8", encoding.c_str() );
if ( cd == ( iconv_t )-1 )
{
wxString message;
message.Printf ( _ ( "Cannot open %s: unknown encoding %s" ),
fileName.c_str(),
wideEncoding.c_str() );
messagePane ( message, CONST_STOP );
delete binaryfile;//memorymap;
return false;
};
int iconvLenMultiplier = 4; // worst case scenario
if ( encoding == "ISO-8859-1" ||
encoding == "UTF-16" ||
encoding == "UTF-16BE" ||
encoding == "UTF-16LE" )
{
iconvLenMultiplier = 2;
}
else if ( encoding == "UTF-32" ||
encoding == "UTF-32BE" ||
encoding == "UTF-32LE" )
{
iconvLenMultiplier = 1;
}
size_t iconvBufferLeft, docBufferLeft;
iconvBufferLen = iconvBufferLeft = docBufferLen * iconvLenMultiplier + 1;
docBufferLeft = docBufferLen;
iconvBuffer = new char[iconvBufferLen];
finalBuffer = iconvBuffer; // iconvBuffer will be incremented by iconv
size_t nconv;
nconv = iconv (
cd,
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
( const char ** )
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
&docBuffer,
&docBufferLeft,
&iconvBuffer,
&iconvBufferLeft );
*iconvBuffer = '\0';
iconv_close ( cd );
if ( nconv == ( size_t )-1 )
{
wxString message;
message.Printf ( _ ( "Cannot open %s: conversion from encoding %s failed" ),
fileName.c_str(),
wideEncoding.c_str() );
messagePane ( message, CONST_STOP );
delete[] finalBuffer;
delete binaryfile; //delete memorymap;
return false;
}
finalBufferLen = iconvBufferLen - iconvBufferLeft;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _ ( "Creating document view..." ) );
Freeze();
doc = new XmlDoc (
mainBook,
( largeFile ) ? largeFileProperties: properties,
&protectTags,
visibilityState,
( !binaryfile->getDataLen() ) ? FILE_TYPE_XML : type,
wxID_ANY,
finalBuffer,
finalBufferLen,
catalogPath,
( const char * ) fileName.mb_str ( wxConvLocal ),
auxPath );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
doc->SetUndoCollection ( false );
doc->SetUndoCollection ( true );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
doc->setFullFileName ( fileName );
doc->setShortFileName ( name );
doc->setDirectory ( directory );
openFileSet.insert ( fileName );
history.AddFileToHistory ( fileName );
updateFileMenu();
wxFileName ofn ( fileName );
doc->setLastModified ( ofn.GetModificationTime() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
mainBook->AddPage ( ( wxWindow * ) doc, name, _T ( "" ) );
Thaw();
statusProgress ( wxEmptyString );
mainBook->Refresh();
wxFileName fn ( fileName );
doc->setLastModified ( fn.GetModificationTime() );
doc->SetFocus();
if ( type != FILE_TYPE_XML || !binaryfile->getDataLen() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
delete binaryfile;//memorymap;
return true;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// NOW parse the document, but don't create a UTF-8 copy
statusProgress ( _T ( "Parsing document..." ) );
std::auto_ptr<WrapExpat> we ( new WrapExpat() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool optimisedParseSuccess = false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// omit XML declaration
if ( !isUtf8 && finalBufferLen &&
finalBuffer[0] == '<' &&
finalBuffer[1] == '?' &&
finalBuffer[2] == 'x' &&
finalBuffer[3] == 'm' &&
finalBuffer[4] == 'l' )
{
for ( ; *finalBuffer && finalBufferLen; finalBuffer++ && finalBufferLen-- )
{
if ( *finalBuffer == '>' )
{
finalBuffer++;
finalBufferLen--;
break;
}
}
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( finalBuffer )
{
optimisedParseSuccess = we->parse ( finalBuffer, finalBufferLen );
statusProgress ( wxEmptyString );
}
// NOW update prompt maps if necessary
if ( !largeFile && ( properties.completion || properties.validateAsYouType ) )
{
statusProgress ( _T ( "Compiling autocompletion lists..." ) );
doc->updatePromptMaps ( finalBuffer, finalBufferLen );
statusProgress ( wxEmptyString );
}
if ( !largeFile && ( properties.validateAsYouType && doc->getDtdFound() ) )
{
statusProgress ( _T ( "Validating document..." ) );
doc->shallowValidate ( finalBuffer, finalBufferLen );
statusProgress ( wxEmptyString );
}
if ( !optimisedParseSuccess )
{
std::string error = we->getLastError();
wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
posPair = we->getErrorPosition();
-- ( posPair.first );
messagePane ( wideError, CONST_WARNING );
int newPosition = doc->PositionFromLine ( posPair.first );
doc->SetSelection ( newPosition, newPosition );
doc->SetFocus();
}
else
{
closePane();
}
delete binaryfile; //delete memorymap;
return true;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
std::string MyFrame::getApproximateEncoding ( char *docBuffer,
size_t docBufferLen )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
std::string line, encoding;
char *it;
size_t i;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// grab first line
for (
i = 0, it = docBuffer;
i < docBufferLen && *it != '\n' && i < BUFSIZ;
i++, it++ )
{
if ( *it )
line += *it;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::pair<int, int> limits = XmlEncodingHandler::getEncodingValueLimits ( line );
if ( limits.first == -1 || limits.second == -1 )
return "";
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
return line.substr ( limits.first, limits.second );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnToggleFold ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->toggleFold();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFoldAll ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->foldAll();
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUnfoldAll ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->unfoldAll();
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnQuit ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
Close ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUndo ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->Undo();
doc->setValidationRequired ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnRedo ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->Redo();
doc->setValidationRequired ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnRevert ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
while ( doc->GetModify() )
{
if ( !doc->CanUndo() )
return;
doc->Undo();
doc->setValidationRequired ( true );
}
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnSpelling ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
doc->SetUndoCollection ( false );
doc->SetUndoCollection ( true );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// handle unusual encodings
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
{
encodingMessage();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( doc->getFullFileName() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool success = wl->parse ( tempFileName.name(), true );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string bufferParameterUtf8;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !success )
{
statusProgress ( wxEmptyString );
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wideError.Prepend ( _ ( "Opening spelling and style check in read-only mode: " ) );
messagePane ( wideError, CONST_WARNING );
if ( !ReadFile::run ( tempFileName.name(), bufferParameterUtf8 ) )
return;
std::auto_ptr<WrapExpat> we ( new WrapExpat() );
bufferParameterUtf8 = we->xmliseTextNode ( bufferParameterUtf8 );
bufferParameterUtf8.insert ( 0, "<root>" );
bufferParameterUtf8 += "</root>";
}
2007-09-07 23:17:30 +02:00
else
2007-09-08 00:25:30 +02:00
{
bufferParameterUtf8 = wl->getOutput();
}
auto_ptr<StyleDialog> sd ( new StyleDialog (
this,
wxICON ( appicon ),
bufferParameterUtf8,
doc->getShortFileName(),
ruleSetDir,
filterDir,
browserCommand,
ruleSetPreset,
filterPreset,
( success ) ? false : true,
stylePosition,
styleSize ) );
if ( sd->ShowModal() == wxID_OK )
{
std::string bufferUtf8 = sd->getEditedString();
if ( bufferUtf8.empty() )
messagePane ( _ ( "Edited document empty" ), CONST_STOP );
else
doc->SetTextRaw ( bufferUtf8.c_str() );
}
// update presets if report has been created (even if followed by cancel)
ruleSetPreset = sd->getRuleSetPreset();
filterPreset = sd->getFilterPreset();
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
stylePosition = sd->getPosition();
styleSize = sd->getSize();
2007-09-07 23:17:30 +02:00
#endif
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPreviousDocument ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !getActiveDocument() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int currentSelection = mainBook->GetSelection();
if ( currentSelection < 1 )
return;
mainBook->SetSelection ( --currentSelection );
XmlDoc *doc = getActiveDocument();
if ( doc )
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnNextDocument ( wxCommandEvent& WXUNUSED ( event ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !getActiveDocument() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int currentSelection = mainBook->GetSelection();
int maxSelection = mainBook->GetPageCount();
if ( currentSelection >= ( maxSelection - 1 ) )
return;
mainBook->SetSelection ( ++currentSelection );
XmlDoc *doc = getActiveDocument();
if ( doc )
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnSave ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
save();
2007-09-07 23:17:30 +02:00
}
void MyFrame::save()
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
if (!doc->GetModify())
return;
*/
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString fileName;
if ( ( fileName = doc->getFullFileName() ) == _T ( "" ) )
{
wxCommandEvent event;
OnSaveAs ( event );
return;
}
if ( !saveFile ( doc, fileName, true ) )
; // handle messages in saveFile
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnSaveAs ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
saveAs();
2007-09-07 23:17:30 +02:00
}
void MyFrame::saveAs()
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString defaultFile, defaultDir;
defaultFile = doc->getFullFileName();
if ( !defaultFile.empty() )
{
wxFileName fn ( defaultFile );
defaultDir = fn.GetPath();
}
auto_ptr<wxFileDialog> fd ( new wxFileDialog (
this,
_ ( "Save As" ),
defaultDir,
defaultFile,
FILE_FILTER,
wxSAVE | wxOVERWRITE_PROMPT ) );
if ( fd->ShowModal() == wxID_CANCEL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString path = fd->GetPath();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if (
openFileSet.count ( path ) &&
path != doc->getFullFileName() )
{
wxString message;
message.Printf ( _ ( "%s is already open" ), path.c_str() );
messagePane ( message, CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString name = fd->GetFilename();
wxString directory;
wxFileName::SplitPath ( path, &directory, NULL, NULL );
if ( path == _T ( "" ) )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !saveFile ( doc, path, false ) )
return;
// if already named, remove from set of open files
openFileSet.erase ( doc->getFullFileName() );
doc->setFullFileName ( path );
doc->setShortFileName ( name );
doc->setDirectory ( directory );
history.AddFileToHistory ( path ); // update history
updateFileMenu();
int selection;
if ( ( selection = mainBook->GetSelection() ) == -1 )
return;
mainBook->SetPageText ( selection, name );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateCloseAll ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
event.Enable ( mainBook->GetPageCount() > 1 );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateCutCopy ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
event.Enable ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateLocationPaneVisible ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !viewMenu )
return;
wxAuiPaneInfo info = manager.GetPane ( locationPanel );
event.Check ( info.IsShown() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateSavedOnly ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
event.Enable (
( doc->getFullFileName().empty() ) ? false : true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateDocRange ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
event.Enable ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateReplaceRange ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL ) // || protectTags)
{
event.Enable ( false );
return;
}
event.Enable ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateFindAgain ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !getActiveDocument() || findData.GetFindString().empty() )
{
event.Enable ( false );
return;
}
event.Enable ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateSaveUndo ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
//event.Enable((doc->CanUndo()) ? true : false);
event.Enable ( ( doc->GetModify() ) ? true : false );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateRedo ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
event.Enable ( doc->CanRedo() );
}
void MyFrame::OnUpdatePaste ( wxUpdateUIEvent& event )
{
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
{
event.Enable ( false );
return;
}
event.Enable ( true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdatePreviousDocument ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !getActiveDocument() )
{
event.Enable ( false );
return;
}
int currentDocument = mainBook->GetSelection();
event.Enable ( ( currentDocument < 1 ) ? false : true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateNextDocument ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !getActiveDocument() )
{
event.Enable ( false );
return;
}
int currentDocument = mainBook->GetSelection();
int maxDocument = mainBook->GetPageCount();
event.Enable ( ( currentDocument >= ( maxDocument - 1 ) ) ? false : true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnUpdateClosePane ( wxUpdateUIEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo i1, i2, i3;
i1 = manager.GetPane ( htmlReport );
i2 = manager.GetPane ( findReplacePanel );
i3 = manager.GetPane ( commandPanel );
event.Enable ( i1.IsShown() || i2.IsShown() || i3.IsShown() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnValidateDTD ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
updatePaths(); // needed to ensure catalog is available
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString fname = doc->getFullFileName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName wtfn ( fname );
if ( fname.empty() || doc->GetModify() )
{
wxString wideBuffer = doc->GetText();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
if ( !saveRawUtf8 (
wtfn.name(),
buffer ) )
{
messagePane (
_ ( "Cannot save temporary copy for validation; please save or discard changes" ),
CONST_STOP );
return;
}
fname = wtfn.wideName();
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string fnameLocal = ( const char * ) fname.mb_str ( wxConvLocal );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->clearErrorIndicators();
statusProgress ( _ ( "DTD validation in progress..." ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
if ( !wl->validate ( fnameLocal ) )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
statusProgress ( wxEmptyString );
messagePane ( wideError, CONST_WARNING );
std::pair<int, int> posPair = wl->getErrorPosition();
-- ( posPair.first );
int cursorPos =
doc->PositionFromLine ( posPair.first );
doc->SetSelection ( cursorPos, cursorPos );
// shallow validate all
doc->shallowValidate(); // has to come first as it deletes all indicators
doc->setErrorIndicator ( posPair.first, posPair.second );
return;
}
statusProgress ( wxEmptyString );
documentOk ( _ ( "valid" ) );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnValidateRelaxNG ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString fileName = doc->getFullFileName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString defaultFile, defaultDir;
defaultFile = doc->getFullFileName();
if ( !defaultFile.empty() )
{
wxFileName fn ( defaultFile );
defaultDir = fn.GetPath();
}
AssociateDialog ad (
this,
_ ( "Select RELAX NG grammar" ),
_ ( "Choose a file:" ),
_ ( "RELAX NG grammar" ),
_T ( "*.*" ),
lastRelaxNGSchema );
if ( ad.ShowModal() != wxID_OK )
return;
wxString path = lastRelaxNGSchema = ad.getUrl();
if ( path.empty() )
{
statusProgress ( wxEmptyString );
return;
}
validateRelaxNG ( doc, path, fileName );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::validateRelaxNG (
XmlDoc *doc,
const wxString& schemaName,
wxString& fileName ) // not const: may change if empty/document modified
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !doc )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName wtfn ( fileName );
if ( fileName.empty() || doc->GetModify() )
{
wxString wideBuffer = doc->GetText();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
if ( !saveRawUtf8 (
wtfn.name(),
buffer ) )
{
messagePane (
_ ( "Cannot save temporary copy for validation; please save or discard changes" ),
CONST_STOP );
return;
}
fileName = wtfn.wideName();
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->clearErrorIndicators();
statusProgress ( _ ( "RELAX NG validation in progress..." ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string schemaFileNameLocal = ( const char * ) schemaName.mb_str ( wxConvLocal );
std::string fileNameLocal = ( const char * ) fileName.mb_str ( wxConvLocal );
if ( !wl->validateRelaxNG ( schemaFileNameLocal, fileNameLocal ) )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
statusProgress ( wxEmptyString );
std::pair<int, int> posPair = wl->getErrorPosition();
-- ( posPair.first );
int cursorPos =
doc->PositionFromLine ( posPair.first );
doc->SetSelection ( cursorPos, cursorPos );
doc->setErrorIndicator ( posPair.first, posPair.second );
messagePane ( wideError, CONST_WARNING );
doc->SetFocus();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
documentOk ( _ ( "valid" ) );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnValidatePreset ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString fileName = doc->getFullFileName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int id = event.GetId();
wxString schemaFileName = validationPresetMap[id];
if ( schemaFileName.empty() )
return;
validateRelaxNG ( doc, schemaFileName, fileName );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnValidateSchema ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
// schema location required
std::auto_ptr<XmlSchemaLocator> xsl(new XmlSchemaLocator());
std::string utf8Buffer;
getRawText(doc, utf8Buffer);
XmlEncodingHandler::setUtf8(utf8Buffer, true);
xsl->parse(utf8Buffer);
std::string utf8Location = xsl->getSchemaLocation();
if (utf8Location.empty())
{
messagePane(_("No XML Schema found"), CONST_STOP);
return;
}
*/
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString fileName;
std::string tempFileNameLocal;
fileName = doc->getFullFileName();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName wtfn ( fileName );
if ( fileName.empty() || doc->GetModify() )
{
wxString wideBuffer = doc->GetText();
std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
if ( !saveRawUtf8 (
wtfn.name(),
buffer ) )
{
messagePane (
_ ( "Cannot save temporary copy for validation; please save or discard changes" ),
CONST_STOP );
return;
}
fileName = wtfn.wideName();
}
doc->clearErrorIndicators();
std::string error;
wxString wideError;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
// separate WrapTempFileName for output log
WrapTempFileName tempFileName ( _T ( "" ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString cmd = binDir +
_T ( "validate.exe \"" ) +
fileName + _T ( "\" \"" ) +
tempFileName.wideName() + _T ( "\"" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _ ( "Validation in progress..." ) );
int result = wxExecute ( cmd, wxEXEC_SYNC );
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
switch ( result )
{
2007-09-07 23:17:30 +02:00
case 0:
2007-09-08 00:25:30 +02:00
documentOk ( _ ( "valid" ) );
break;
2007-09-07 23:17:30 +02:00
case 1:
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "MSXML validation failed (version 4.0 or later required)" ), CONST_STOP );
break;
2007-09-07 23:17:30 +02:00
case 2:
2007-09-08 00:25:30 +02:00
if ( ReadFile::run ( tempFileName.name(), error ) )
{
wideError = wxString ( error.c_str(), wxConvLocal, error.size() );
messagePane ( wideError, CONST_WARNING );
// tbd: extract line & column numbers; use error indicators
}
else
messagePane ( _ ( "Validation error" ), CONST_WARNING );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
doc->SetFocus();
return;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
std::auto_ptr<WrapXerces> validator ( new WrapXerces() );
std::string fileNameLocal = ( const char * ) fileName.mb_str ( wxConvLocal );
if ( !validator->validate ( fileNameLocal ) )
{
error = validator->getLastError();
wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
messagePane ( wideError, CONST_WARNING );
std::pair<int, int> posPair = validator->getErrorPosition();
int cursorPos =
doc->PositionFromLine ( posPair.first - 1 );
doc->SetSelection ( cursorPos, cursorPos );
doc->setErrorIndicator ( posPair.first - 1, posPair.second );
}
else
documentOk ( _ ( "valid" ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
// handle relative locations
schemaPath = PathResolver::run(schemaPath, fileName);
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress(_("XML Schema validation in progress..."));
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<WrapLibxml> wl(new WrapLibxml(libxmlNetAccess, catalogPath));
std::string schemaFileNameLocal = (const char *)schemaPath.mb_str(wxConvLocal);
std::string fileNameLocal = (const char *)fileName.mb_str(wxConvLocal);
if (!wl->validateW3CSchema(schemaFileNameLocal, fileNameLocal))
{
2007-09-07 23:17:30 +02:00
std::string error = wl->getLastError();
wxString wideError = wxString(error.c_str(), wxConvUTF8, error.size());
statusProgress(wxEmptyString);
std::pair<int, int> posPair = wl->getErrorPosition();
--(posPair.first);
int cursorPos =
2007-09-08 00:25:30 +02:00
doc->PositionFromLine(posPair.first - 1);
2007-09-07 23:17:30 +02:00
doc->SetSelection(cursorPos, cursorPos);
doc->setErrorIndicator(posPair.first - 1, posPair.second);
messagePane(wideError, CONST_WARNING);
return;
2007-09-08 00:25:30 +02:00
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress(wxEmptyString);
documentOk(_("valid"));
*/
2007-09-07 23:17:30 +02:00
#endif
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnXPath ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<wxTextEntryDialog> dlg ( new wxTextEntryDialog (
this,
_ ( "Enter XPath:" ),
_ ( "Evaluate XPath" ),
xpathExpression ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int ret = dlg->ShowModal();
if ( ret == wxID_CANCEL )
return;
xpathExpression = dlg->GetValue();
std::string valUtf8 = ( const char * ) xpathExpression.mb_str ( wxConvUTF8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
{
encodingMessage();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( doc->getFullFileName() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool success = wl->xpath ( valUtf8, tempFileName.name() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !success )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
if ( !wideError.empty() )
wideError.Prepend ( _T ( ": " ) );
wideError.Prepend ( _ ( "Cannot evaluate XPath" ) );
messagePane ( wideError, CONST_WARNING );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
std::string buffer = wl->getOutput();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( buffer.empty() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
messagePane ( _ ( "No matching nodes found" ), CONST_WARNING );
2007-09-07 23:17:30 +02:00
return;
2007-09-08 00:25:30 +02:00
}
newDocument ( buffer );
statusProgress ( wxEmptyString );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
void MyFrame::OnXslt ( wxCommandEvent& event )
{
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
{
encodingMessage();
2007-09-07 23:17:30 +02:00
return;
}
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( doc->getFullFileName() );
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
wxString path;
int id = event.GetId();
if ( id == ID_XSLT )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XslLocator xl;
xl.parse ( rawBufferUtf8 );
std::string location = xl.getXslLocation();
path = wxString ( location.c_str(), wxConvUTF8, location.size() );
path.Replace ( _T ( "%20" ), _T ( " " ), true );
path = PathResolver::run ( path, doc->getFullFileName() );
if ( !wxFileName::FileExists ( path ) )
{
if ( !path.empty() )
{
wxString message;
message.Printf ( _ ( "Cannot open stylesheet %s" ), path.c_str() );
messagePane ( message, CONST_WARNING );
}
wxString defaultFile, defaultDir;
defaultFile = doc->getFullFileName();
if ( !defaultFile.empty() )
{
wxFileName fn ( defaultFile );
defaultDir = fn.GetPath();
}
AssociateDialog ad (
this,
_ ( "Select stylesheet" ),
_ ( "Choose a file:" ),
_ ( "XSLT stylesheet" ),
_T ( "*.xsl;*.xslt" ),
lastXslStylesheet );
if ( ad.ShowModal() != wxID_OK )
return;
path = lastXslStylesheet = ad.getUrl();
if ( path.empty() ) // Cancel selected
{
statusProgress ( wxEmptyString );
return;
}
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
else
{
wxString sep;
sep.Append ( wxFileName::GetPathSeparator() );
switch ( id )
{
case ID_XSLT_TEI_FO:
path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "fo" ) + sep +
_T ( "tei.xsl" );
break;
case ID_XSLT_TEI_HTML:
path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "html" ) + sep +
_T ( "tei.xsl" );
break;
case ID_XSLT_TEI_XHTML:
path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "xhtml" ) + sep +
_T ( "tei.xsl" );
break;
case ID_XSLT_TEI_LATEX:
path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "latex" ) + sep +
_T ( "tei.xsl" );
break;
case ID_XSLT_DOCBOOK_FO:
path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "fo" ) + sep +
_T ( "docbook.xsl" );
break;
case ID_XSLT_DOCBOOK_HTML:
path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "html" ) + sep +
_T ( "docbook.xsl" );
break;
case ID_XSLT_DOCBOOK_XHTML:
path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "xhtml" ) + sep +
_T ( "docbook.xsl" );
break;
default:
break;
}
}
statusProgress ( _ ( "XSL transformation in progress..." ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string stylefnameLocal = ( const char * ) path.mb_str ( wxConvLocal );
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
if ( !wl->xslt ( stylefnameLocal, tempFileName.name() ) )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wideError.Prepend ( _ ( "Cannot transform: " ) );
statusProgress ( wxEmptyString );
messagePane ( wideError, CONST_WARNING );
return;
}
std::string buffer = wl->getOutput();
if ( buffer.empty() )
{
messagePane ( _ ( "Output document empty" ), CONST_WARNING );
return;
}
statusProgress ( wxEmptyString );
newDocument ( buffer );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnPrettyPrint ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int line = doc->LineFromPosition ( doc->GetCurrentPos() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string encoding = XmlEncodingHandler::get ( rawBufferUtf8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8, true ) )
{
encodingMessage();
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( _ ( "Pretty-printing in progress..." ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
for ( int i = 0; i < 2; i++ ) // perform two iterations
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( doc->getFullFileName() );
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
bool success = wl->parse ( tempFileName.name(), true );
if ( !success )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wideError.Prepend ( _ ( "Cannot pretty-print: " ) );
statusProgress ( wxEmptyString );
messagePane ( wideError, CONST_WARNING );
return;
}
rawBufferUtf8 = wl->getOutput();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
if ( rawBufferUtf8.empty() )
messagePane (
_ ( "Pretty-print unsuccessful: output document empty" ),
CONST_STOP );
else
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
/*
if (encoding != "UTF-8")
{
std::string output = getEncodedBuffer(rawBufferUtf8, encoding);
if (!output.empty())
rawBufferUtf8 = output;
}
*/
if ( encoding != "UTF-8" && !encoding.empty() )
{
XmlEncodingHandler::set ( rawBufferUtf8, encoding );
}
doc->SetTextRaw ( rawBufferUtf8.c_str() );
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
doc->GotoLine ( line );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnEncoding ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// fetch document contents
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::vector<wxString> encodingVector;
encodingVector.push_back ( _T ( "UTF-8" ) );
encodingVector.push_back ( _T ( "UTF-16" ) );
encodingVector.push_back ( _T ( "UTF-16LE" ) );
encodingVector.push_back ( _T ( "UTF-16BE" ) );
encodingVector.push_back ( _T ( "ISO-8859-1" ) );
encodingVector.push_back ( _T ( "US-ASCII" ) );
const int vectorSize = encodingVector.size();
wxString choiceArray[vectorSize + 1];
for ( int i = 0; i < vectorSize; ++i )
* ( choiceArray + i ) = encodingVector.at ( i );
wxSingleChoiceDialog scd (
this, _ ( "Choose an encoding:" ), _ ( "Encoding" ), vectorSize, choiceArray );
if ( scd.ShowModal() == wxID_CANCEL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString selection;
std::string selectionUtf8, bufferUtf8;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
selection = scd.GetStringSelection();
selectionUtf8 = selection.mb_str ( wxConvUTF8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
getRawText ( doc, bufferUtf8 );
XmlEncodingHandler::setUtf8 ( bufferUtf8, true );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName tempFileName ( _T ( "" ) );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
int res;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
WrapTempFileName sourceFileName ( doc->getFullFileName() );
saveRawUtf8 ( sourceFileName.name(), bufferUtf8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
res = wl->saveEncodingFromFile ( sourceFileName.name(), tempFileName.name(), selectionUtf8 );
if ( res == -1 )
{
std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wideError.Prepend ( _ ( "Cannot set encoding: " ) );
messagePane ( wideError, CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string newBuffer;
bool success = ReadFile::run ( tempFileName.name(), newBuffer );
if ( !success )
{
messagePane ( _ ( "Cannot set encoding (cannot open temporary file)" ),
CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::auto_ptr<XmlUtf8Reader> xur ( new XmlUtf8Reader (
false,
expandInternalEntities,
newBuffer.size() ) );
if ( !xur->parse ( newBuffer ) )
{
messagePane ( _ ( "Cannot set encoding (cannot parse temporary file)" ),
CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
/*
if (wl->getLastError().empty())
{
doc->SetTextRaw(xur->getBuffer().c_str());
}
else
{
std::string err = wl->getLastError();
wxString wideErr;
wideErr = _("Cannot set encoding: ");
wideErr += wxString(err.c_str(), wxConvUTF8, err.size());
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
messagePane(wideErr, CONST_STOP);
}
*/
doc->SetTextRaw ( xur->getBuffer().c_str() );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
/*
std::string MyFrame::getEncodedBuffer(
const std::string& bufferUtf8,
const std::string& encodingUtf8)
{
WrapTempFileName tempFileName(_T(""));
std::auto_ptr<WrapLibxml> wl(new WrapLibxml(libxmlNetAccess));
int res;
// change to saveEncodingFromFile?
res = wl->saveEncoding(bufferUtf8, tempFileName.name(), encodingUtf8);
if (res == -1)
{
std::string error = wl->getLastError();
wxString wideError = wxString(error.c_str(), wxConvUTF8, error.size());
wideError.Prepend(_("Cannot set encoding: "));
messagePane(wideError, CONST_STOP);
return "";
}
std::string newBuffer;
bool success = ReadFile::run(tempFileName.name(), newBuffer);
if (!success)
{
messagePane(_("Cannot set encoding (cannot open temporary file)"),
CONST_STOP);
return "";
}
std::auto_ptr<XmlUtf8Reader> xur(new XmlUtf8Reader(
false,
expandInternalEntities,
newBuffer.size()));
if (!xur->parse(newBuffer))
{
messagePane(_("Cannot set encoding (cannot parse temporary file)"),
CONST_STOP);
return "";
}
return xur->getBuffer().c_str();
}
*/
2007-09-08 00:25:30 +02:00
void MyFrame::OnHome ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
navigate ( _T ( "http://xml-copy-editor.sourceforge.net" ) );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnDownloadSource ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
navigate ( _T ( "http://xml-copy-editor.svn.sourceforge.net/viewvc/xml-copy-editor/" ) );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnToolbarVisible ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !viewMenu )
return;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
if ( useCoolBarOnStart )
toolbarVisible = ( viewMenu->IsChecked ( ID_TOOLBAR_VISIBLE ) ) ? false : true;
else
toolbarVisible = ( toolbarVisible ) ? false : true;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
toolbarVisible = ( toolbarVisible ) ? false : true;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_TOOLBAR_VISIBLE, toolbarVisible );
showTopBars ( toolbarVisible );
manager.Update();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnWrapWords ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !viewMenu )
return;
bool wrapWords;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
wrapWords = ( viewMenu->IsChecked ( ID_WRAP_WORDS ) ) ? false : true;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
wrapWords = ( properties.wrap ) ? false : true;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_WRAP_WORDS, wrapWords );
properties.wrap = wrapWords;
// update all documents
int pageCount = mainBook->GetPageCount();
XmlDoc *currentDoc;
for ( int i = 0; i < pageCount; ++i )
{
currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( !currentDoc )
break;
currentDoc->SetWrapMode ( wrapWords );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnLocationPaneVisible ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo info = manager.GetPane ( locationPanel );
bool visible = ( info.IsShown() ) ? false : true;
manager.GetPane ( locationPanel ).Show ( visible );
manager.Update();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnProtectTags ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !xmlMenu )
return;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
if ( useCoolBarOnStart )
protectTags = ( xmlMenu->IsChecked ( ID_PROTECT_TAGS ) ) ? false : true;
else
protectTags = ( protectTags ) ? false : true;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
protectTags = ( protectTags ) ? false : true;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
if ( xmlMenu )
xmlMenu->Check ( ID_PROTECT_TAGS, protectTags );
if ( toolBar )
toolBar->ToggleTool ( ID_PROTECT_TAGS, protectTags );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( protectTags )
doc->adjustCursor(); // apply to all open docs?
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnVisibilityState ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int id;
id = event.GetId();
switch ( id )
{
2007-09-07 23:17:30 +02:00
case ID_SHOW_TAGS:
2007-09-08 00:25:30 +02:00
visibilityState = SHOW_TAGS;
//GetStatusBar()->SetStatusText(wxEmptyString, STATUS_PARENT);
break;
2007-09-07 23:17:30 +02:00
case ID_HIDE_ATTRIBUTES:
2007-09-08 00:25:30 +02:00
visibilityState = HIDE_ATTRIBUTES;
//GetStatusBar()->SetStatusText(wxEmptyString, STATUS_PARENT);
break;
2007-09-07 23:17:30 +02:00
case ID_HIDE_TAGS:
2007-09-08 00:25:30 +02:00
visibilityState = HIDE_TAGS;
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
visibilityState = SHOW_TAGS;
break;
}
if ( viewMenu )
viewMenu->Check ( id, true );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// iterate over all open documents
int pageCount = mainBook->GetPageCount();
XmlDoc *currentDoc;
for ( int i = 0; i < pageCount; ++i )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( !currentDoc )
break;
currentDoc->applyVisibilityState ( visibilityState );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
if ( visibilityState == HIDE_ATTRIBUTES || visibilityState == HIDE_TAGS )
{
if ( properties.protectHiddenElements && !protectTags )
{
wxCommandEvent e;
OnProtectTags ( e );
}
}
// fetch current document
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// set focus for current document
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnFeedback ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString forumUrl =
_T ( "https://sourceforge.net/forum/forum.php?forum_id=475215" );
navigate ( forumUrl );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::navigate ( const wxString& url )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString testString = browserCommand;
testString.Replace ( _T ( " -remote" ), wxEmptyString, true );
if ( browserCommand.empty() )
{
messagePane (
_ ( "Cannot open in browser: no browser defined (see Tools, Options..., General)" ),
CONST_STOP );
return;
}
else if ( !wxFileName::FileExists ( testString ) )
{
wxString message;
message.Printf (
_ ( "Cannot open in browser: %s not found (see Tools, Options..., General)" ),
testString.c_str() );
messagePane ( message, CONST_STOP );
return;
}
wxString cmd;
cmd = browserCommand + _T ( " \"" ) + url + _T ( "\"" );
wxExecute ( cmd, wxEXEC_ASYNC ); // make ASYNC an option?
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::findAgain ( wxString s, int flags )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
findReplacePanel->flagNotFound ( false );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( s.empty() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// update regex parameter to keep global replace in sync
findRegex = findReplacePanel->getRegex();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int newLocation;
int myFlags = 0;
if ( flags & wxFR_WHOLEWORD )
myFlags |= wxSTC_FIND_WHOLEWORD;
if ( flags & wxFR_MATCHCASE )
myFlags |= wxSTC_FIND_MATCHCASE;
if ( findReplacePanel->getRegex() )
myFlags |= wxSTC_FIND_REGEXP;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool incrementalFind =
( findReplacePanel->getIncrementalFind() ) ? true : false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
//doc->SetYCaretPolicy(wxSTC_CARET_SLOP | wxSTC_CARET_STRICT, 10);
if ( flags & wxFR_DOWN ) // find next
{
doc->SetTargetStart ( ( incrementalFind ) ?
doc->GetSelectionStart() : doc->GetSelectionEnd() );
doc->SetTargetEnd ( doc->GetLength() );
doc->SetSearchFlags ( myFlags );
newLocation = doc->SearchInTarget ( s );
// try once more from top
if ( newLocation == -1 )
{
doc->SetTargetStart ( 0 );
doc->SetTargetEnd ( doc->GetLength() );
newLocation = doc->SearchInTarget ( s );
}
}
else // find previous
{
doc->SetCurrentPos (
( doc->GetSelectionStart() ) ? doc->GetSelectionStart() - 1 : 0 );
doc->SearchAnchor();
newLocation = doc->SearchPrev ( myFlags, s );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
//doc->SetYCaretPolicy(wxSTC_CARET_SLOP, 0);
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( newLocation == -1 )
{
findReplacePanel->flagNotFound ( true );
wxString err;
err.Printf ( _ ( "Cannot find '%s'" ), s.c_str() );
doc->SetSelectionEnd ( doc->GetSelectionStart() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( err );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
// must clear target to prevent replace affecting whole document
doc->SetTargetStart ( 0 );
doc->SetTargetEnd ( 0 );
return;
}
doc->SetSelection ( doc->GetTargetStart(), doc->GetTargetEnd() );
doc->EnsureCaretVisible();
2007-09-07 23:17:30 +02:00
}
bool MyFrame::closeActiveDocument()
{
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
closePane();
int selection = mainBook->GetSelection();
if ( selection == -1 || !mainBook->GetPageCount() ) // GetPageCount needed for wxAuiNotebook
return false;
locationPanel->update ( NULL, wxEmptyString );
insertChildPanel->update ( NULL, wxEmptyString );
insertSiblingPanel->update ( NULL, wxEmptyString );
// workaround -- wxAuiNotebook: send virtual close event? DeletePage doesn't generate one
wxAuiNotebookEvent e;
e.SetSelection ( selection );
OnPageClosing ( e );
if ( deletePageVetoed )
return false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
mainBook->DeletePage ( selection );
return true;
// was:
// mainBook->DeletePage(selection);
// return (!deletePageVetoed);
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc )
return false;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
statusProgress ( wxEmptyString );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( checkLastModified )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxFileName fn ( fileName );
if ( fn.IsOk() )
{
wxDateTime myLastModified = fn.GetModificationTime();
if ( !myLastModified.IsEqualTo ( doc->getLastModified() ) )
{
int choice = wxMessageBox (
_ ( "File has been modified by another application.\nDo you want to proceed?" ),
_ ( "Confirmation" ),
wxICON_QUESTION | wxYES_NO | wxCANCEL );
if ( choice != wxYES )
return false;
}
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
int bytes = 0;
std::string utf8Buffer, encoding, fileNameLocal;
bool isXml = true;
try
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
getRawText ( doc, utf8Buffer );
XmlEncodingSpy es;
es.parse ( utf8Buffer );
encoding = es.getEncoding();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
fileNameLocal = fileName.mb_str ( wxConvLocal );
closePane();
bool success;
success = true;
if ( getFileType ( fileName ) != FILE_TYPE_XML )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
isXml = false;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
// raw file conditions
if ( doc->getType() == FILE_TYPE_BINARY )
{
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
if ( success )
bytes = utf8Buffer.size();
}
else if ( !isXml && encoding.empty() )
{
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
if ( success )
bytes = utf8Buffer.size();
}
else if ( encoding == "UTF-8" )
{
auto_ptr<WrapExpat> we ( new WrapExpat() );
if ( !we->parse ( utf8Buffer ) )
{
std::string error = we->getLastError();
wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
if ( we->isEncodingError() )
;
messagePane ( werror, CONST_WARNING );
}
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
if ( success )
bytes = utf8Buffer.size();
}
else
{
wxString wideEncoding = wxString ( encoding.c_str(), wxConvLocal, encoding.size() );
// IF Unicode, use iconv to convert buffer
if ( encoding == "UTF-16" || encoding == "UTF-16LE" ||
encoding == "UTF-16BE" || encoding == "UTF-32" || encoding == "UTF-32LE" ||
encoding == "UTF-32BE" )
{
iconv_t cd = iconv_open ( encoding.c_str(), "UTF-8" );
if ( cd == ( iconv_t )-1 )
{
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
if ( success )
{
bytes = utf8Buffer.size();
wxString message;
message.Printf (
_ ( "%s saved in default encoding UTF-8: unknown encoding %s" ),
fileName.c_str(),
wideEncoding.c_str() );
messagePane ( message, CONST_WARNING );
}
else
return false;
}
else
{
size_t utf8BufferLen = utf8Buffer.size();
size_t iconvBufferLen, iconvBufferLeft, utf8BufferLeft;
int iconvLenMultiplier = 4; // worst case scenario
if ( encoding == "UTF-16" ||
encoding == "UTF-16BE" ||
encoding == "UTF-16LE" )
{
iconvLenMultiplier = 2;
}
else if ( encoding == "UTF-32" ||
encoding == "UTF-32BE" ||
encoding == "UTF-32LE" )
{
iconvLenMultiplier = 4;
}
iconvBufferLen = iconvBufferLeft =
utf8BufferLen * iconvLenMultiplier + 4; // worst case scenario
char *finalBuffer;
char *iconvBuffer = new char[iconvBufferLen];
utf8BufferLeft = utf8BufferLen;
iconvBuffer = new char[iconvBufferLen];
finalBuffer = iconvBuffer; // iconvBuffer will be incremented by iconv
size_t nconv;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW_
2007-09-08 00:25:30 +02:00
const char *
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
char *
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
utf8BufferPtr =
( char * )
utf8Buffer.c_str();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
nconv = iconv (
cd,
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
( const char ** )
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
&utf8BufferPtr,
&utf8BufferLeft,
&iconvBuffer,
&iconvBufferLeft );
iconv_close ( cd );
if ( nconv == ( size_t )-1 ) // conversion failed
{
delete[] finalBuffer;
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
if ( success )
{
bytes = utf8Buffer.size();
wxString message;
message.Printf (
_ ( "%s saved in default encoding UTF-8: conversion to %s failed" ),
fileName.c_str(),
wideEncoding.c_str() );
messagePane ( message, CONST_WARNING );
}
else
{
return false;
}
}
else
{
size_t finalBufferLen = iconvBufferLen - iconvBufferLeft;
std::ofstream ofs ( fileNameLocal.c_str(), std::ios::out | std::ios::binary );
if ( !ofs )
{
delete[] finalBuffer;
return false;
}
// iconv adds boms for UTF-16 & UTF-32 automatically
ofs.write ( finalBuffer, finalBufferLen );
ofs.close();
delete[] finalBuffer;
bytes = finalBufferLen;
}
}
}
else // all other encodings handled by Libxml
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlEncodingHandler::setUtf8 ( utf8Buffer );
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
WrapTempFileName sourceFileName ( fileName );
saveRawUtf8 ( sourceFileName.name(), utf8Buffer );
int result = wl->saveEncodingFromFile ( sourceFileName.name(), fileNameLocal, encoding );
if ( result == -1 )
{
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
if ( success )
{
std::string libxmlError = wl->getLastError();
bytes = utf8Buffer.size();
wxString msg, wideEncoding, wideError;
wideEncoding =
wxString ( encoding.c_str(), wxConvUTF8, encoding.size() );
wideError = wxString ( libxmlError.c_str(), wxConvUTF8, libxmlError.size() );
if ( wideError.empty() )
wideError = _ ( "unknown error" );
msg.Printf ( _ ( "Cannot save document in %s: %s (saved in default encoding UTF-8)" ),
wideEncoding.c_str(), wideError.c_str() );
messagePane ( msg, CONST_INFO );
}
}
else
bytes = result;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
}
} // try
catch ( std::bad_alloc& )
{
if ( encoding != "UTF-8" )
{
int answer = wxMessageBox (
_ ( "Out of memory: attempt to save in default encoding UTF-8?" ),
_ ( "Out of memory" ),
wxYES_NO | wxCANCEL | wxICON_QUESTION,
this );
if ( answer == wxCANCEL || answer == wxNO )
return false;
bool success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
if ( success )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
bytes = utf8Buffer.size();
wxString message;
message.Printf (
_ ( "%s saved in default encoding UTF-8" ),
fileName.c_str() );
messagePane ( message, CONST_INFO );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
else
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString message;
message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
messagePane ( message, CONST_STOP );
return false;
2007-09-07 23:17:30 +02:00
}
}
}
2007-09-08 00:25:30 +02:00
doc->SetFocus();
doc->SetSavePoint();
if ( properties.validateAsYouType && isXml )
doc->shallowValidate ( utf8Buffer.c_str(), utf8Buffer.size() );
if ( !unlimitedUndo )
doc->EmptyUndoBuffer();
wxFileName fn ( fileName );
if ( fn.IsOk() )
doc->setLastModified ( fn.GetModificationTime() );
openFileSet.insert ( fileName );
displaySavedStatus ( bytes );
return true;
}
bool MyFrame::saveRawUtf8 (
const std::string& fileNameLocal,
std::string& bufferUtf8,
bool ignoreEncoding,
bool isXml )
{
ofstream ofs ( fileNameLocal.c_str(), std::ios::out | std::ios::binary );
if ( !ofs )
2007-09-07 23:17:30 +02:00
return false;
2007-09-08 00:25:30 +02:00
if ( !ignoreEncoding && isXml )
XmlEncodingHandler::setUtf8 ( bufferUtf8, true );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( saveBom && isXml )
{
char bom[4];
bom[0] = 0xEF;
bom[1] = 0xBB;
bom[2] = 0xBF;
bom[3] = 0;
ofs.write ( bom, 3 );
}
ofs.write ( bufferUtf8.c_str(), bufferUtf8.size() );
ofs.close();
return true;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::displaySavedStatus ( int bytes )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString unit;
float result = 0;
if ( bytes > 1000000 )
{
result = bytes / 1000000;
unit = _ ( "MB" );
}
else if ( bytes > 1000 )
{
result = bytes / 1000;
unit = _ ( "kB" );
}
else if ( bytes >= 0 )
{
result = bytes;
unit = ngettext ( L"byte", L"bytes", bytes );
}
else
return;
wxString msg;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
msg.Printf (
_ ( "%g %s saved" ),
result,
unit.c_str() );
statusProgress ( msg );
2007-09-07 23:17:30 +02:00
}
bool MyFrame::xpThemeActive()
{
#ifndef __WXMSW__
2007-09-08 00:25:30 +02:00
return false;
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
return ( wxUxThemeEngine::Get() && wxUxThemeEngine::Get()->IsThemeActive() );
2007-09-07 23:17:30 +02:00
#endif
}
bool MyFrame::getHandleCommandLineFlag()
{
2007-09-08 00:25:30 +02:00
return handleCommandLineFlag;
2007-09-07 23:17:30 +02:00
}
MyMenuBar *MyFrame::getMenuBar()
{
2007-09-08 00:25:30 +02:00
fileMenu = new wxMenu; // use class-wide data member
updateFileMenu ( false );
// edit menu
wxMenu *editMenu = new wxMenu;
wxMenuItem *undoItem =
new wxMenuItem ( NULL, wxID_UNDO, _ ( "&Undo\tCtrl+Z" ), _ ( "Undo" ) );
undoItem->SetBitmap ( undo16Bitmap );
wxMenuItem *redoItem =
new wxMenuItem ( NULL, wxID_REDO, _ ( "&Redo\tCtrl+Y" ), _ ( "Redo" ) );
redoItem->SetBitmap ( redo16Bitmap );
wxMenuItem *cutItem =
new wxMenuItem ( NULL, wxID_CUT, _ ( "&Cut\tCtrl+X" ), _ ( "Cut" ) );
cutItem->SetBitmap ( cutBitmap );
wxMenuItem *copyItem =
new wxMenuItem ( NULL, wxID_COPY, _ ( "C&opy\tCtrl+C" ), _ ( "Copy" ) );
copyItem->SetBitmap ( copyBitmap );
wxMenuItem *pasteItem =
new wxMenuItem ( NULL, wxID_PASTE, _ ( "&Paste\tCtrl+V" ), _ ( "Paste" ) );
pasteItem->SetBitmap ( pasteBitmap );
wxMenuItem *pasteNewDocumentItem =
new wxMenuItem (
NULL,
ID_PASTE_NEW_DOCUMENT,
_ ( "P&aste As New Document" ),
_ ( "Paste As New Document" ) );
pasteNewDocumentItem->SetBitmap ( wxNullBitmap );
wxMenuItem *findItem =
new wxMenuItem ( NULL, ID_FIND, _ ( "&Find...\tCtrl+F" ), _ ( "Find..." ) );
findItem->SetBitmap ( searchBitmap );
wxMenuItem *findAgainItem =
new wxMenuItem ( NULL, ID_FIND_AGAIN, _ ( "F&ind Again\tF3" ), _ ( "Find Again" ) );
findAgainItem->SetBitmap ( wxNullBitmap );
wxMenuItem *replaceItem =
new wxMenuItem ( NULL, ID_REPLACE, _ ( "&Replace...\tCtrl+R" ), _ ( "Replace..." ) );
replaceItem->SetBitmap ( wxNullBitmap );
wxMenuItem *globalReplaceItem =
new wxMenuItem (
NULL,
ID_GLOBAL_REPLACE,
_ ( "&Global Replace...\tCtrl+Shift+R" ),
_ ( "Global Replace..." ) );
globalReplaceItem->SetBitmap ( wxNullBitmap );
wxMenuItem *gotoItem =
new wxMenuItem ( NULL, ID_GOTO, _ ( "G&o To...\tCtrl+G" ), _ ( "Go To..." ) );
gotoItem->SetBitmap ( wxNullBitmap );
editMenu->Append ( undoItem );
editMenu->Append ( redoItem );
editMenu->AppendSeparator();
editMenu->Append ( cutItem );
editMenu->Append ( copyItem );
editMenu->Append ( pasteItem );
editMenu->Append ( pasteNewDocumentItem );
editMenu->AppendSeparator();
editMenu->Append ( findItem );
editMenu->Append ( findAgainItem );
editMenu->Append ( replaceItem );
editMenu->Append ( globalReplaceItem );
editMenu->AppendSeparator();
editMenu->Append ( gotoItem );
// font size menu
wxMenu *fontSizeMenu = new wxMenu;
fontSizeMenu->Append (
ID_FONT_LARGER, _ ( "Increase\tCtrl+U" ), _ ( "Increase" ) );
fontSizeMenu->Append (
ID_FONT_SMALLER, _ ( "Decrease\tCtrl+D" ), _ ( "Decrease" ) );
fontSizeMenu->AppendSeparator();
fontSizeMenu->Append ( ID_FONT_NORMAL, _ ( "Normal\tCtrl+0" ), _ ( "Normal" ) );
// color scheme menu
colorSchemeMenu = new wxMenu;
colorSchemeMenu->AppendRadioItem (
ID_COLOR_SCHEME_DEFAULT, _ ( "&Default" ), _ ( "Default" ) );
colorSchemeMenu->AppendRadioItem (
ID_COLOR_SCHEME_REDUCED_GLARE,
_ ( "&Blue background, white text" ),
_ ( "Blue background, white text" ) );
colorSchemeMenu->AppendRadioItem (
ID_COLOR_SCHEME_DEFAULT_BACKGROUND,
_ ( "&Light" ),
_ ( "Light" ) );
colorSchemeMenu->AppendRadioItem (
ID_COLOR_SCHEME_NONE,
_ ( "&None" ),
_ ( "None" ) );
switch ( properties.colorScheme )
{
2007-09-07 23:17:30 +02:00
case COLOR_SCHEME_DEFAULT:
2007-09-08 00:25:30 +02:00
colorSchemeMenu->Check ( ID_COLOR_SCHEME_DEFAULT, true );
break;
2007-09-07 23:17:30 +02:00
case COLOR_SCHEME_DEFAULT_BACKGROUND:
2007-09-08 00:25:30 +02:00
colorSchemeMenu->Check ( ID_COLOR_SCHEME_DEFAULT_BACKGROUND, true );
break;
2007-09-07 23:17:30 +02:00
case COLOR_SCHEME_REDUCED_GLARE:
2007-09-08 00:25:30 +02:00
colorSchemeMenu->Check ( ID_COLOR_SCHEME_REDUCED_GLARE, true );
break;
2007-09-07 23:17:30 +02:00
case COLOR_SCHEME_NONE:
2007-09-08 00:25:30 +02:00
colorSchemeMenu->Check ( ID_COLOR_SCHEME_NONE, true );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
viewMenu = new wxMenu; // use class-wide data member
viewMenu->Append ( ID_PREVIOUS_DOCUMENT, _ ( "&Previous Document\tCtrl+PgUp" ), _ ( "Previous Document" ) );
viewMenu->Append ( ID_NEXT_DOCUMENT, _ ( "&Next Document\tCtrl+PgDn" ), _ ( "Next Document" ) );
viewMenu->Append ( ID_BROWSER, _ ( "&Browser\tCtrl+B" ), _ ( "Browser" ) );
viewMenu->AppendSeparator();
viewMenu->AppendRadioItem (
ID_SHOW_TAGS,
_ ( "&Show Tags and Attributes\tCtrl+T" ), _ ( "Show Tags and Attributes" ) );
viewMenu->AppendRadioItem (
ID_HIDE_ATTRIBUTES,
_ ( "&Hide Attributes Only\tCtrl+Shift+A" ), _ ( "Hide Attributes Only" ) );
viewMenu->AppendRadioItem (
ID_HIDE_TAGS,
_ ( "H&ide Tags and Attributes\tCtrl+Shift+T" ), _ ( "Hide Tags and Attributes" ) );
switch ( visibilityState )
{
2007-09-07 23:17:30 +02:00
case SHOW_TAGS:
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_SHOW_TAGS, true );
break;
2007-09-07 23:17:30 +02:00
case HIDE_TAGS:
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_HIDE_TAGS, true );
break;
2007-09-07 23:17:30 +02:00
case HIDE_ATTRIBUTES:
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_HIDE_ATTRIBUTES, true );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
viewMenu->Check ( ID_SHOW_TAGS, true );
break;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
viewMenu->AppendSeparator();
viewMenu->Append (
ID_TOGGLE_FOLD, _ ( "&Toggle Fold" ), _ ( "Toggle Fold" ) );
viewMenu->Append (
ID_FOLD_ALL, _ ( "&Fold Tags\tCtrl+Shift+F" ), _ ( "Fold Tags" ) );
viewMenu->Append (
ID_UNFOLD_ALL, _ ( "&Unfold Tags\tCtrl+Shift+U" ), _T ( "Unfold Tags" ) );
viewMenu->AppendSeparator();
viewMenu->AppendCheckItem (
ID_WRAP_WORDS, _ ( "&Wrap Words\tCtrl+W" ), _T ( "Wrap Words" ) );
viewMenu->Check ( ID_WRAP_WORDS, properties.wrap );
viewMenu->Append ( wxID_ANY, _ ( "&Color Scheme" ), colorSchemeMenu );
viewMenu->Append ( wxID_ANY, _ ( "&Text Size" ), fontSizeMenu );
viewMenu->AppendSeparator();
viewMenu->AppendCheckItem (
ID_LOCATION_PANE_VISIBLE,
_ ( "S&how Current Element Pane" ),
_ ( "Show Current ElementPane" ) );
viewMenu->Check ( ID_LOCATION_PANE_VISIBLE, false );
viewMenu->AppendCheckItem (
ID_TOOLBAR_VISIBLE, _ ( "Sh&ow Toolbar" ), _ ( "Show Toolbar" ) );
viewMenu->Check ( ID_TOOLBAR_VISIBLE, toolbarVisible );
viewMenu->Append (
ID_HIDE_PANE, _ ( "C&lose Message Pane\tAlt+C" ), _ ( "Close Message Pane" ) );
// insert menu
wxMenu *insertMenu = new wxMenu;
insertMenu->Append ( ID_INSERT_CHILD, _ ( "&Element...\tCtrl+I" ), _ ( "Element..." ) );
insertMenu->Append ( ID_INSERT_SIBLING, _ ( "&Sibling...\tCtrl+Shift+I" ), _ ( "Sibling..." ) );
insertMenu->Append ( ID_INSERT_ENTITY, _ ( "&Entity...\tCtrl+E" ), _ ( "Entity..." ) );
insertMenu->AppendSeparator();
insertMenu->Append ( ID_INSERT_TWIN, _ ( "&Twin\tCtrl+Enter" ), _ ( "Twin" ) );
insertMenu->AppendSeparator();
insertMenu->Append ( ID_INSERT_SYMBOL, _ ( "S&ymbol..." ), _ ( "Symbol..." ) );
// validation menu
wxMenu *validationMenu = new wxMenu;
validationMenu->Append ( ID_VALIDATE_DTD, _ ( "&DTD\tF4" ), _ ( "DTD" ) );
validationMenu->Append (
ID_VALIDATE_W3C_SCHEMA, _ ( "&XML Schema\tF5" ), _ ( "XML Schema" ) );
validationMenu->AppendSeparator();
validationMenu->Append (
ID_VALIDATE_RELAX_NG, _ ( "&RELAX NG...\tF6" ), _ ( "RELAX NG..." ) );
wxMenu *associateMenu = new wxMenu;
associateMenu->Append ( ID_ASSOCIATE_DTD_PUBLIC, _ ( "&Public DTD..." ), _ ( "Public DTD..." ) );
associateMenu->Append ( ID_ASSOCIATE_DTD_SYSTEM, _ ( "&System DTD..." ), _ ( "System DTD..." ) );
associateMenu->Append ( ID_ASSOCIATE_W3C_SCHEMA, _ ( "&XML Schema..." ), _ ( "XML Schema..." ) );
associateMenu->Append ( ID_ASSOCIATE_XSL, _ ( "XS&LT stylesheet..." ), _ ( "XSLT stylesheet..." ) );
if ( wxFileName::DirExists ( rngDir ) )
{
wxString rngMask, rngFile, displayName, shortcutString;
rngMask = rngDir + wxFileName::GetPathSeparator() + _T ( "*.rng" );
rngFile = wxFindFirstFile ( rngMask, wxFILE );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
int id = ID_VALIDATE_PRESET1;
if ( !rngFile.empty() )
{
validationPresetMap.insert ( make_pair ( id, rngFile ) );
wxFileName::SplitPath ( rngFile, NULL, NULL, &displayName, NULL );
displayName.Replace ( _T ( ".rng" ), _T ( "" ) );
shortcutString.Printf ( _ ( "\tCtrl+%i" ), ( id - ID_VALIDATE_PRESET1 ) + 1 );
validationMenu->Append ( id, displayName + shortcutString, displayName );
for ( id = ID_VALIDATE_PRESET2; id <= ID_VALIDATE_PRESET9; ++id )
{
rngFile = wxFindNextFile();
if ( rngFile.empty() )
break;
validationPresetMap.insert ( make_pair ( id, rngFile ) );
wxFileName::SplitPath ( rngFile, NULL, NULL, &displayName, NULL );
shortcutString.Printf ( _ ( "\tCtrl+%i" ), ( id - ID_VALIDATE_PRESET1 ) + 1 );
displayName.Replace ( _T ( ".rng" ), _T ( "" ) );
validationMenu->Append ( id, displayName + shortcutString, displayName );
}
}
}
// xsl menu
wxMenu *xslMenu = new wxMenu;
xslMenu->Append ( ID_XSLT, _ ( "&XSL Transform...\tF8" ),
_ ( "XSL Transform..." ) );
xslMenu->AppendSeparator();
xslMenu->Append (
ID_XSLT_DOCBOOK_HTML,
_ ( "&DocBook to HTML\tAlt+1" ), _ ( "DocBook to HTML" ) );
xslMenu->Append (
ID_XSLT_DOCBOOK_XHTML,
_ ( "&DocBook to XHTML\tAlt+2" ), _ ( "DocBook to XHTML" ) );
xslMenu->Append (
ID_XSLT_DOCBOOK_FO,
_ ( "D&ocBook to XSL-FO\tAlt+3" ), _ ( "DocBook to XSL-FO" ) );
xslMenu->Append (
ID_XSLT_TEI_HTML,
_ ( "&TEI to HTML\tAlt+4" ), _ ( "TEI to HTML" ) );
xslMenu->Append (
ID_XSLT_TEI_LATEX,
_ ( "T&EI to LaTeX\tAlt+5" ), _ ( "TEI to LaTeX" ) );
xslMenu->Append (
ID_XSLT_TEI_XHTML,
_ ( "TE&I to XHTML\tAlt+6" ), _ ( "TEI to XHTML" ) );
xslMenu->Append (
ID_XSLT_TEI_FO,
_ ( "TEI to &XSL-FO\tAlt+7" ), _ ( "TEI to XSL-FO" ) );
// xml menu
xmlMenu = new wxMenu; // use class-wide data member
xmlMenu->Append (
ID_CHECK_WELLFORMED,
_ ( "&Check Well-formedness\tF2" ), _ ( "Check Well-formedness" ) );
xmlMenu->Append (
wxID_ANY,
_ ( "&Validate" ),
validationMenu );
xmlMenu->AppendSeparator();
xmlMenu->Append (
wxID_ANY,
_ ( "&Associate" ),
associateMenu );
xmlMenu->AppendSeparator();
xmlMenu->Append ( wxID_ANY, _ ( "&XSLT" ), xslMenu );
xmlMenu->Append (
ID_XPATH,
_ ( "&Evaluate XPath...\tF9" ),
_ ( "Evaluate XPath..." ) );
xmlMenu->AppendSeparator();
xmlMenu->Append (
ID_PRETTYPRINT,
_ ( "&Pretty-print\tF11" ), _ ( "Pretty-print" ) );
xmlMenu->AppendSeparator();
xmlMenu->AppendCheckItem (
ID_PROTECT_TAGS,
_ ( "&Lock Tags\tCtrl+L" ),
_ ( "Lock Tags" ) );
xmlMenu->Check ( ID_PROTECT_TAGS, protectTags );
xmlMenu->AppendSeparator();
xmlMenu->Append (
ID_ENCODING,
_ ( "E&ncoding..." ), _ ( "Encoding..." ) );
// tools menu
wxMenu *toolsMenu = new wxMenu;
wxMenuItem *spellingItem =
new wxMenuItem (
NULL,
ID_SPELL,
_ ( "&Spelling and Style...\tF7" ),
_ ( "Spelling and Style..." ) );
spellingItem->SetBitmap ( spelling16Bitmap );
wxMenuItem *wordCountItem =
new wxMenuItem (
NULL,
ID_WORD_COUNT,
_ ( "&Word Count" ),
_ ( "Word Count" ) );
wordCountItem->SetBitmap ( wxNullBitmap );
wxMenuItem *commandItem =
new wxMenuItem (
NULL,
ID_COMMAND,
_ ( "&Command\tCtrl+Alt+C" ),
_ ( "Command" ) );
commandItem->SetBitmap ( wxNullBitmap );
wxMenuItem *optionsItem =
new wxMenuItem (
NULL,
ID_OPTIONS,
_ ( "&Options..." ),
_ ( "Options..." ) );
optionsItem->SetBitmap ( wxNullBitmap );
toolsMenu->Append ( spellingItem );
toolsMenu->Append ( wordCountItem );
toolsMenu->AppendSeparator();
toolsMenu->Append ( commandItem );
toolsMenu->AppendSeparator();
toolsMenu->Append ( optionsItem );
// help menu
wxMenu *helpMenu = new wxMenu;
wxMenuItem *helpItem =
new wxMenuItem ( NULL, wxID_HELP,
_ ( "&XML Copy Editor Help\tF1" ), _ ( "Help" ) );
helpItem->SetBitmap ( helpBitmap );
wxMenuItem *homeItem =
new wxMenuItem ( NULL, ID_HOME,
_ ( "&Home Page" ), _ ( "Home Page" ) );
homeItem->SetBitmap ( wxNullBitmap );
wxMenuItem *feedbackItem =
new wxMenuItem ( NULL, ID_FEEDBACK, _ ( "&Forum" ), _ ( "Forum" ) );
feedbackItem->SetBitmap ( wxNullBitmap );
wxMenuItem *aboutItem =
new wxMenuItem ( NULL, wxID_ABOUT,
_ ( "&About XML Copy Editor" ), _ ( "About" ) );
aboutItem->SetBitmap ( wxNullBitmap );
wxMenuItem *downloadSourceItem =
new wxMenuItem ( NULL, ID_DOWNLOAD_SOURCE,
_ ( "&Browse Source" ), _ ( "Browse Source" ) );
downloadSourceItem->SetBitmap ( wxNullBitmap );
helpMenu->Append ( helpItem );
helpMenu->AppendSeparator();
helpMenu->Append ( homeItem );
helpMenu->Append ( feedbackItem );
helpMenu->Append ( downloadSourceItem );
helpMenu->AppendSeparator();
helpMenu->Append ( aboutItem );
MyMenuBar *menuBar = new MyMenuBar ( wxMB_DOCKABLE );
menuBar->Append ( fileMenu, _ ( "&File" ) );
menuBar->Append ( editMenu, _ ( "&Edit" ) );
menuBar->Append ( viewMenu, _ ( "&View" ) );
menuBar->Append ( insertMenu, _ ( "&Insert" ) );
menuBar->Append ( xmlMenu, _ ( "&XML" ) );
menuBar->Append ( toolsMenu, _ ( "&Tools" ) );
menuBar->Append ( helpMenu, _ ( "&Help" ) );
return menuBar;
}
void MyFrame::updateFileMenu ( bool deleteExisting )
{
if ( deleteExisting )
{
wxMenuItemList list = fileMenu->GetMenuItems();
size_t count = list.size();
for ( size_t i = 0; i < count; ++i )
fileMenu->Delete ( list[i] );
}
wxMenuItem *newItem =
new wxMenuItem ( NULL, wxID_NEW, _ ( "&New...\tCtrl+N" ), _ ( "New..." ) );
newItem->SetBitmap ( new16Bitmap );
wxMenuItem *openItem =
new wxMenuItem ( NULL, wxID_OPEN, _ ( "&Open...\tCtrl+O" ), _ ( "Open..." ) );
openItem->SetBitmap ( open16Bitmap );
wxMenuItem *openLargeFileItem =
new wxMenuItem ( NULL, ID_OPEN_LARGE_FILE,
_ ( "O&pen Large Document...\tCtrl+Shift+O" ), _ ( "Open Large Document..." ) );
openLargeFileItem->SetBitmap ( wxNullBitmap );
wxMenuItem *closeItem =
new wxMenuItem ( NULL, wxID_CLOSE, _ ( "&Close\tCtrl+F4" ), _ ( "Close" ) );
closeItem->SetBitmap ( wxNullBitmap );
wxMenuItem *closeAllItem =
new wxMenuItem ( NULL, wxID_CLOSE_ALL, _ ( "C&lose All" ), _ ( "Close All" ) );
closeAllItem->SetBitmap ( wxNullBitmap );
wxMenuItem *saveItem =
new wxMenuItem ( NULL, wxID_SAVE, _ ( "&Save\tCtrl+S" ), _ ( "Save" ) );
saveItem->SetBitmap ( save16Bitmap );
wxMenuItem *saveAsItem =
new wxMenuItem ( NULL, wxID_SAVEAS, _ ( "S&ave As...\tF12" ), _ ( "Save As..." ) );
saveAsItem->SetBitmap ( wxNullBitmap );
wxMenuItem *revertItem =
new wxMenuItem ( NULL, wxID_REVERT, _ ( "&Revert" ), _ ( "Revert" ) );
revertItem->SetBitmap ( wxNullBitmap );
wxMenuItem *printSetupItem =
new wxMenuItem ( NULL, ID_PRINT_SETUP, _ ( "Pa&ge Setup..." ), _ ( "Page Setup..." ) );
printSetupItem->SetBitmap ( wxNullBitmap );
wxMenuItem *printPreviewItem =
new wxMenuItem ( NULL, ID_PRINT_PREVIEW, _ ( "P&rint Preview..." ), _ ( "Print Preview..." ) );
printPreviewItem->SetBitmap ( printPreviewBitmap );
wxMenuItem *printItem =
new wxMenuItem ( NULL, ID_PRINT, _ ( "Pr&int...\tCtrl+P" ), _ ( "Print..." ) );
printItem->SetBitmap ( print16Bitmap );
wxMenuItem *importMSWordItem =
new wxMenuItem (
NULL, ID_IMPORT_MSWORD, _ ( "I&mport Microsoft Word Document..." ) );
importMSWordItem->SetBitmap ( wxNullBitmap );
wxMenuItem *exportMSWordItem =
new wxMenuItem (
NULL, ID_EXPORT_MSWORD, _ ( "&Export Microsoft Word Document..." ) );
exportMSWordItem->SetBitmap ( wxNullBitmap );
wxMenuItem *exitItem =
new wxMenuItem ( NULL, wxID_EXIT, _ ( "E&xit" ), _ ( "Exit" ) );
exitItem->SetBitmap ( wxNullBitmap );
fileMenu->Append ( newItem );
fileMenu->Append ( openItem );
fileMenu->Append ( openLargeFileItem );
fileMenu->AppendSeparator();
fileMenu->Append ( closeItem );
fileMenu->Append ( closeAllItem );
fileMenu->Append ( saveItem );
fileMenu->Append ( saveAsItem );
fileMenu->Append ( revertItem );
fileMenu->AppendSeparator();
fileMenu->Append ( printSetupItem );
fileMenu->Append ( printPreviewItem );
fileMenu->Append ( printItem );
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
fileMenu->AppendSeparator();
fileMenu->Append ( importMSWordItem );
fileMenu->Append ( exportMSWordItem );
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
history.AddFilesToMenu ( fileMenu );
fileMenu->AppendSeparator();
fileMenu->Append ( exitItem );
2007-09-07 23:17:30 +02:00
}
MyToolBar *MyFrame::getToolBar()
{
2007-09-08 00:25:30 +02:00
MyToolBar *myToolBar = new MyToolBar (
this,
ID_TOOLBAR,
wxDefaultPosition,
wxDefaultSize,
wxTB_FLAT |
wxTB_HORIZONTAL |
wxTB_DOCKABLE );
int w, h;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
w = saveBitmap.GetWidth(), h = saveBitmap.GetHeight();
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
w = h = 24;
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
myToolBar->SetToolBitmapSize ( wxSize ( w, h ) );
myToolBar->AddTool (
wxID_NEW,
_ ( "New" ),
newBitmap,
_ ( "New" ) );
myToolBar->AddTool (
wxID_OPEN,
_ ( "Open" ),
openBitmap,
_ ( "Open" ) );
myToolBar->AddTool (
wxID_SAVE,
_ ( "Save" ),
saveBitmap,
wxNullBitmap,
wxITEM_NORMAL,
_ ( "Save" ) );
myToolBar->AddTool (
ID_PRINT,
_ ( "Print" ),
printBitmap,
wxNullBitmap,
wxITEM_NORMAL,
_ ( "Print" ) );
myToolBar->AddTool (
ID_BROWSER,
_ ( "Browser" ),
internetBitmap,
wxNullBitmap,
wxITEM_NORMAL,
_ ( "Browser" ) );
myToolBar->AddTool (
ID_SPELL,
_ ( "Spelling and Style" ),
spellingBitmap,
wxNullBitmap,
wxITEM_NORMAL,
_ ( "Spelling and Style" ) );
myToolBar->AddCheckTool (
ID_PROTECT_TAGS,
_ ( "Lock Tags" ),
hyperlinkBitmap,
wxNullBitmap,
_ ( "Lock Tags" ) );
myToolBar->ToggleTool (
ID_PROTECT_TAGS, protectTags );
myToolBar->Realize();
return myToolBar;
2007-09-07 23:17:30 +02:00
}
XmlDoc *MyFrame::getActiveDocument()
{
2007-09-08 00:25:30 +02:00
if ( !mainBook->GetPageCount() )
return NULL;
return ( XmlDoc * ) mainBook->GetPage ( mainBook->GetSelection() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::addSafeSeparator ( wxToolBar *toolBar )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( xpThemeActive() )
{
toolBar->AddSeparator();
return;
}
wxStaticText *staticControl = new wxStaticText (
toolBar,
wxID_ANY,
_T ( " " ) );
toolBar->AddControl ( staticControl );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::statusProgress ( const wxString& s )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxStatusBar *status = GetStatusBar();
if ( !status )
return;
status->SetStatusText ( s, 0 );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::messagePane ( const wxString& s, int iconType )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxString paneTitle;
switch ( iconType )
{
case ( CONST_INFO ) :
if ( s.Length() < 50 ) // magic no. necessary?
{
statusProgress ( s );
return;
}
paneTitle = _ ( "Information" );
break;
case ( CONST_WARNING ) :
paneTitle = _ ( "Warning" );
break;
case ( CONST_STOP ) :
paneTitle = _ ( "Stopped" );
break;
case ( CONST_QUESTION ) :
paneTitle = _ ( "Question" );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
paneTitle = _ ( "Message" );
break;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxAuiPaneInfo info = manager.GetPane ( htmlReport );
if ( !info.IsShown() )
{
manager.GetPane ( htmlReport ).Show ( true );
manager.Update();
}
manager.GetPane ( htmlReport ).Caption ( paneTitle );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString htmlString = s;
htmlString.Replace ( _T ( "&" ), _T ( "&amp;" ), true );
htmlString.Replace ( _T ( "<" ), _T ( "&lt;" ), true );
htmlString.Replace ( _T ( ">" ), _T ( "&gt;" ), true );
wxString htmlBuffer;
htmlBuffer += _T ( "<html><body><table><tr><td width=\"5%\"><img src=\"" );
switch ( iconType )
{
case ( CONST_INFO ) :
htmlBuffer += pngDir;
htmlBuffer += _T ( "stock_dialog-info-32.png" );
break;
case ( CONST_WARNING ) :
htmlBuffer += pngDir;
htmlBuffer += _T ( "stock_dialog-warning-32.png" );
break;
case ( CONST_STOP ) :
htmlBuffer += pngDir;
htmlBuffer += _T ( "stock_dialog-stop-32.png" );
break;
case ( CONST_QUESTION ) :
htmlBuffer += pngDir;
htmlBuffer += _T ( "stock_dialog-question-32.png" );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
htmlBuffer += _T ( "\"></td><td width=\"95%\">" );
htmlBuffer += htmlString;
htmlBuffer += _T ( "</td></tr></table></body></html>" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
htmlReport->SetPage ( htmlBuffer );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
manager.Update();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::documentOk ( const wxString& status )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
wxString message;
message.Printf ( _ ( "%s is %s" ),
doc->getShortFileName().c_str(),
status.c_str() );
messagePane ( message, CONST_INFO );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::applyEditorProperties ( bool zoomOnly )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
size_t documentCount = mainBook->GetPageCount();
for ( size_t i = 0; i < documentCount; i++ )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
doc = ( XmlDoc * ) mainBook->GetPage ( i );
if ( doc )
{
doc->applyProperties ( properties, zoomOnly );
if ( !properties.validateAsYouType )
doc->clearErrorIndicators();
}
2007-09-07 23:17:30 +02:00
}
}
void MyFrame::modifiedMessage()
{
2007-09-08 00:25:30 +02:00
messagePane (
_ ( "Document has been modified: save or discard changes" ),
CONST_STOP );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::xmliseWideTextNode ( wxString& s )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
s.Replace ( _T ( "&" ), _T ( "&amp;" ), true );
s.Replace ( _T ( "<" ), _T ( "&lt;" ), true );
s.Replace ( _T ( ">" ), _T ( "&gt;" ), true );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
int MyFrame::getFileType ( const wxString& fileName )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
std::string fileNameLocal, fileNameLocalLC;
fileNameLocal = fileName.mb_str ( wxConvLocal );
fileNameLocalLC = CaseHandler::lowerCase ( fileNameLocal );
if ( fileNameLocalLC.find ( ".dtd" ) != std::string::npos ||
fileNameLocalLC.find ( ".ent" ) != std::string::npos )
return FILE_TYPE_DTD;
else if ( fileNameLocalLC.find ( ".css" ) != std::string::npos )
return FILE_TYPE_CSS;
else if ( fileNameLocalLC.find ( ".php" ) != std::string::npos )
return FILE_TYPE_PHP;
else if ( fileNameLocalLC.find ( ".exe" ) != std::string::npos )
return FILE_TYPE_BINARY;
else if ( fileNameLocalLC.find ( ".rnc" ) != std::string::npos )
return FILE_TYPE_RNC;
return FILE_TYPE_XML;
2007-09-07 23:17:30 +02:00
}
long MyFrame::getNotebookStyleMask()
{
2007-09-08 00:25:30 +02:00
/*
if (notebookStyleMenu->IsChecked(ID_NOTEBOOK_STYLE_FLAT))
return wxFNB_FANCY_TABS | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_X_ON_TAB;
else if (notebookStyleMenu->IsChecked(ID_NOTEBOOK_STYLE_VC8))
return wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS |
wxFNB_X_ON_TAB | wxFNB_DROPDOWN_TABS_LIST | wxFNB_NO_NAV_BUTTONS;
else
return wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_COLORFUL_TABS |
wxFNB_X_ON_TAB | wxFNB_DROPDOWN_TABS_LIST | wxFNB_NO_NAV_BUTTONS;
//wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_COLORFUL_TABS;
*/
return 0;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
bool MyFrame::isSpecialFileType ( const wxString& fileName )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
std::string fileNameLocal, fileNameLocalLC;
fileNameLocal = fileName.mb_str ( wxConvLocal );
fileNameLocalLC = CaseHandler::lowerCase ( fileNameLocal );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
return (
fileNameLocalLC.find ( ".dtd" ) != std::string::npos ||
fileNameLocalLC.find ( ".css" ) != std::string::npos ||
fileNameLocalLC.find ( ".php" ) != std::string::npos );
2007-09-07 23:17:30 +02:00
}
void MyFrame::encodingMessage()
{
2007-09-08 00:25:30 +02:00
wxString msg = _ ( "Encoding should be one of " );
msg += ENCODING_INFO;
messagePane ( msg, CONST_STOP );
2007-09-07 23:17:30 +02:00
}
void MyFrame::updatePaths()
{
2007-09-08 00:25:30 +02:00
ruleSetDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "rulesets" );
filterDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "filters" );
templateDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "templates" ) +
wxFileName::GetPathSeparator();
binDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "bin" ) +
wxFileName::GetPathSeparator();
helpDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "help" ) +
wxFileName::GetPathSeparator();
rngDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "rng" ) +
wxFileName::GetPathSeparator();
htmlDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "html" ) +
wxFileName::GetPathSeparator();
pngDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "png" ) +
wxFileName::GetPathSeparator();
wxString wideCatalogPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "catalog" ) +
wxFileName::GetPathSeparator() + _T ( "catalog" );
catalogPath = wideCatalogPath.mb_str ( wxConvLocal );
wxString wideXslDtdPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
wxFileName::GetPathSeparator() + _T ( "xslt10.dtd" );
xslDtdPath = wideXslDtdPath.mb_str ( wxConvLocal );
wxString wideRssDtdPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
wxFileName::GetPathSeparator() + _T ( "rss2.dtd" );
rssDtdPath = wideRssDtdPath.mb_str ( wxConvLocal );
wxString wideXtmDtdPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
wxFileName::GetPathSeparator() + _T ( "xtm1.dtd" );
xtmDtdPath = wideXtmDtdPath.mb_str ( wxConvLocal );
wxString wideLzxDtdPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
wxFileName::GetPathSeparator() + _T ( "lzx.dtd" );
lzxDtdPath = wideLzxDtdPath.mb_str ( wxConvLocal );
wxString wideXliffDtdPath =
applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
wxFileName::GetPathSeparator() + _T ( "xliff.dtd" );
xliffDtdPath = wideXliffDtdPath.mb_str ( wxConvLocal );
}
void MyFrame::OnAssociate ( wxCommandEvent& event )
{
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString title, label, type, extension, path, defaulturl, defaultaux;
wxString auxiliaryLabel;
int id = event.GetId();
switch ( id )
{
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_DTD_PUBLIC:
2007-09-08 00:25:30 +02:00
type = _ ( "Public DTD" );
extension = _T ( "*.dtd" );
defaulturl = lastDtdPublic;
defaultaux = lastDtdPublicAux;
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_DTD_SYSTEM:
2007-09-08 00:25:30 +02:00
type = _ ( "System DTD" );
extension = _T ( "*.dtd" );
defaulturl = lastDtdSystem;
defaultaux = _T ( "" );
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_W3C_SCHEMA:
2007-09-08 00:25:30 +02:00
type = _ ( "XML Schema" );
extension = _T ( "*.xsd" );
defaulturl = lastSchema;
defaultaux = _T ( "" );
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_XSL:
2007-09-08 00:25:30 +02:00
type = _ ( "XSLT stylesheet" );
extension = _T ( "*.xsl;*.xslt" );
defaulturl = lastXslStylesheet;
defaultaux = _T ( "" );
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::string utf8Buffer;
getRawText ( doc, utf8Buffer );
std::auto_ptr<WrapExpat> wellformedparser ( new WrapExpat() );
if ( !wellformedparser->parse ( utf8Buffer ) )
{
std::string error = wellformedparser->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wxString message;
message.Printf (
_ ( "Cannot associate %s: %s" ),
type.c_str(),
wideError.c_str() );
messagePane ( message,
CONST_STOP );
return;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
title.Printf ( _ ( "Associate %s" ), type.c_str() );
label = _ ( "Choose a file:" );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
bool auxiliaryBox =
( id == ID_ASSOCIATE_DTD_PUBLIC );
2007-09-07 23:17:30 +02:00
//(id == ID_ASSOCIATE_W3C_SCHEMA_NS || id == ID_ASSOCIATE_DTD_PUBLIC);
2007-09-08 00:25:30 +02:00
if ( auxiliaryBox )
{
auxiliaryLabel = _ ( "Choose a public identifier:" );
/*
(id == ID_ASSOCIATE_DTD_PUBLIC) ? _("Choose a public identifier:") :
_("Choose a namespace:");
*/
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
AssociateDialog ad (
this,
title,
label,
type,
extension,
defaulturl,
auxiliaryBox,
auxiliaryLabel,
defaultaux );
if ( ad.ShowModal() != wxID_OK )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
path = ad.getUrl();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
wxString aux, schemaPathMemory;
if ( auxiliaryBox )
{
aux = ad.getAux();
}
std::string utf8Path = ( const char * ) path.mb_str ( wxConvUTF8 );
std::string modifiedBuffer;
// remember choice
switch ( id )
{
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_W3C_SCHEMA:
2007-09-08 00:25:30 +02:00
lastSchema = path;
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_DTD_PUBLIC:
2007-09-08 00:25:30 +02:00
lastDtdPublic = path;
lastDtdPublicAux = aux;
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_DTD_SYSTEM:
2007-09-08 00:25:30 +02:00
lastDtdSystem = path;
break;
2007-09-07 23:17:30 +02:00
case ID_ASSOCIATE_XSL:
2007-09-08 00:25:30 +02:00
lastXslStylesheet = path;
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( id == ID_ASSOCIATE_W3C_SCHEMA )
{
std::auto_ptr<XmlAssociateXsd> parser ( new XmlAssociateXsd ( utf8Path ) );
if ( !parser->parse ( utf8Buffer ) )
return;
modifiedBuffer = parser->getBuffer();
}
else if ( id == ID_ASSOCIATE_DTD_SYSTEM || id == ID_ASSOCIATE_DTD_PUBLIC )
{
std::auto_ptr<XmlAssociateDtd> parser ( new XmlAssociateDtd (
utf8Path,
( auxiliaryBox ) ? ( const char * ) aux.mb_str ( wxConvUTF8 ) : "" ) );
if ( !parser->parse ( utf8Buffer ) )
return;
modifiedBuffer = parser->getBuffer();
}
else if ( id == ID_ASSOCIATE_XSL )
{
std::auto_ptr<XmlAssociateXsl> parser ( new XmlAssociateXsl (
utf8Path ) );
if ( !parser->parse ( utf8Buffer ) )
return;
modifiedBuffer = parser->getBuffer();
}
else
return;
doc->SetTextRaw ( modifiedBuffer.c_str() );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
void MyFrame::openRememberedTabs()
{
2007-09-08 00:25:30 +02:00
if ( openTabsOnClose.empty() )
return;
wchar_t *s, *it;
s = it = ( wchar_t * ) openTabsOnClose.wc_str();
std::vector<wxString> v;
wxString buffer = wxEmptyString;
for ( ; *it; it++ )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( *it == L'|' )
{
if ( !buffer.empty() )
{
v.push_back ( buffer );
}
buffer = wxEmptyString;
}
else
buffer += *it;
}
if ( !buffer.empty() )
{
v.push_back ( buffer );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
if ( v.empty() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
std::vector<wxString>::iterator vit;
for ( vit = v.begin(); vit != v.end(); vit++ )
{
if ( !openFile ( *vit ) )
break;
}
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) != NULL )
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::getRawText ( XmlDoc *doc, std::string& buffer )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc )
{
buffer = "";
return;
}
//wxString wideBuffer = doc->GetText();
//buffer = wideBuffer.mb_str(wxConvUTF8);
buffer = doc->myGetTextRaw();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnWordCount ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL )
return;
wxString wideBuffer;
std::string buffer;
wideBuffer = doc->GetText();
buffer = wideBuffer.mb_str ( wxConvUTF8 );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
auto_ptr<XmlWordCount> xwc ( new XmlWordCount() );
wxString msg;
if ( !xwc->parse ( buffer.c_str() ) )
{
std::string error = xwc->getLastError();
wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
statusProgress ( wxEmptyString );
msg.Printf ( _ ( "Cannot count words: %s" ), werror.c_str() );
messagePane ( msg, CONST_STOP );
return;
}
int count = xwc->getWordCount();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
msg.Printf (
ngettext ( L"%s contains %i word", L"%s contains %i words", count ),
doc->getShortFileName().c_str(), count );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
messagePane ( msg, CONST_INFO );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::removeUtf8Bom ( std::string& buffer )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( buffer.size() > 3 &&
( unsigned char ) buffer[0] == 0xEF &&
( unsigned char ) buffer[1] == 0xBB &&
( unsigned char ) buffer[2] == 0xBF )
{
buffer.erase ( 0, 3 );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
2007-09-07 23:17:30 +02:00
void MyFrame::loadBitmaps()
{
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
// toolbar icons
newBitmap = wxBITMAP ( stock_new );
openBitmap = wxBITMAP ( stock_open );
saveBitmap = wxBITMAP ( stock_save );
printBitmap = wxBITMAP ( stock_print );
spellingBitmap = wxBITMAP ( stock_spellcheck );
internetBitmap = wxBITMAP ( stock_internet );
hyperlinkBitmap = wxBITMAP ( stock_hyperlink );
filtersBitmap = wxBITMAP ( stock_filters );
// menu icons
new16Bitmap = wxBITMAP ( stock_new_16 );
open16Bitmap = wxBITMAP ( stock_open_16 );
save16Bitmap = wxBITMAP ( stock_save_16 );
printPreviewBitmap = wxBITMAP ( stock_print_preview_16 );
print16Bitmap = wxBITMAP ( stock_print_16 );
undo16Bitmap = wxBITMAP ( stock_undo_16 );
redo16Bitmap = wxBITMAP ( stock_redo_16 );
cutBitmap = wxBITMAP ( stock_cut_16 );
copyBitmap = wxBITMAP ( stock_copy_16 );
pasteBitmap = wxBITMAP ( stock_paste_16 );
findBitmap = wxBITMAP ( stock_search_16 );
spelling16Bitmap = wxBITMAP ( stock_spellcheck_16 );
helpBitmap = wxBITMAP ( stock_help_16 );
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
// toolbar icons
newBitmap.LoadFile ( pngDir + _T ( "stock_new.png" ), wxBITMAP_TYPE_PNG );
openBitmap.LoadFile ( pngDir + _T ( "stock_open.png" ), wxBITMAP_TYPE_PNG );
saveBitmap.LoadFile ( pngDir + _T ( "stock_save.png" ), wxBITMAP_TYPE_PNG );
printBitmap.LoadFile ( pngDir + _T ( "stock_print.png" ), wxBITMAP_TYPE_PNG );
spellingBitmap.LoadFile ( pngDir + _T ( "stock_spellcheck.png" ), wxBITMAP_TYPE_PNG );
internetBitmap.LoadFile ( pngDir + _T ( "stock_internet.png" ), wxBITMAP_TYPE_PNG );
hyperlinkBitmap.LoadFile ( pngDir + _T ( "stock_hyperlink.png" ), wxBITMAP_TYPE_PNG );
filtersBitmap.LoadFile ( pngDir + _T ( "stock_filters.png" ), wxBITMAP_TYPE_PNG );
// menu icons
new16Bitmap.LoadFile ( pngDir + _T ( "stock_new-16.png" ), wxBITMAP_TYPE_PNG );
open16Bitmap.LoadFile ( pngDir + _T ( "stock_open-16.png" ), wxBITMAP_TYPE_PNG );
save16Bitmap.LoadFile ( pngDir + _T ( "stock_save-16.png" ), wxBITMAP_TYPE_PNG );
printPreviewBitmap.LoadFile ( pngDir + _T ( "stock_print_preview.png" ), wxBITMAP_TYPE_PNG );
print16Bitmap.LoadFile ( pngDir + _T ( "stock_print-16.png" ), wxBITMAP_TYPE_PNG );
undo16Bitmap.LoadFile ( pngDir + _T ( "stock_undo-16.png" ), wxBITMAP_TYPE_PNG );
redo16Bitmap.LoadFile ( pngDir + _T ( "stock_redo-16.png" ), wxBITMAP_TYPE_PNG );
cutBitmap.LoadFile ( pngDir + _T ( "stock_cut-16.png" ), wxBITMAP_TYPE_PNG );
copyBitmap.LoadFile ( pngDir + _T ( "stock_copy-16.png" ), wxBITMAP_TYPE_PNG );
pasteBitmap.LoadFile ( pngDir + _T ( "stock_paste-16.png" ), wxBITMAP_TYPE_PNG );
findBitmap.LoadFile ( pngDir + _T ( "stock_find-16.png" ), wxBITMAP_TYPE_PNG );
spelling16Bitmap.LoadFile ( pngDir + _T ( "stock_spellcheck-16.png" ), wxBITMAP_TYPE_PNG );
helpBitmap.LoadFile ( pngDir + _T ( "stock_help-16.png" ), wxBITMAP_TYPE_PNG );
2007-09-07 23:17:30 +02:00
#endif
}
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
void MyFrame::OnDropFiles ( wxDropFilesEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int no = event.GetNumberOfFiles();
wxString *iterator = event.GetFiles();
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !no || !iterator )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
for ( int i = 0; i < no; i++, iterator++ )
{
if ( !openFile ( *iterator ) )
break;
}
2007-09-07 23:17:30 +02:00
}
#endif
2007-09-08 00:25:30 +02:00
std::string MyFrame::getAuxPath ( const std::string& fileName )
{
if ( fileName.find ( ".xsl" ) != std::string::npos ||
fileName.find ( ".XSL" ) != std::string::npos )
return xslDtdPath;
else if ( fileName.find ( ".rss" ) != std::string::npos ||
fileName.find ( ".RSS" ) != std::string::npos )
return rssDtdPath;
else if ( fileName.find ( ".xtm" ) != std::string::npos ||
fileName.find ( ".xtmm" ) != std::string::npos ||
fileName.find ( ".XTM" ) != std::string::npos ||
fileName.find ( ".XTMM" ) != std::string::npos )
return xtmDtdPath;
else if ( fileName.find ( ".lzx" ) != std::string::npos ||
fileName.find ( ".LZX" ) != std::string::npos )
return lzxDtdPath;
else if ( fileName.find ( ".xlf" ) != std::string::npos ||
fileName.find ( ".XLF" ) != std::string::npos )
return xliffDtdPath;
return "";
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnActivateApp ( wxActivateEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
event.Skip();
if ( !mainBook || !event.GetActive() )
return;
restoreFocusToNotebook = true;
}
void MyFrame::OnIconize ( wxIconizeEvent& event )
{
event.Skip();
if ( event.Iconized() )
return;
restoreFocusToNotebook = true;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::OnKeyPressed ( wxKeyEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
event.Skip();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void MyFrame::setStrictScrolling ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlDoc *doc;
doc = getActiveDocument();
if ( !doc )
return;
doc->SetYCaretPolicy ( ( b ) ? ( wxSTC_CARET_STRICT | wxSTC_CARET_SLOP ) : wxSTC_CARET_EVEN,
( b ) ? 10 : 0 );
2007-09-07 23:17:30 +02:00
}