~ubuntu-branches/ubuntu/raring/xmlcopyeditor/raring

« back to all changes in this revision

Viewing changes to .pc/translations.patch/src/xmlcopyeditor.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-12-13 03:23:38 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20111213032338-myalkis3ru7jxqnh
Tags: 1.2.0.6-2
* Upgraded Standards-Version from 3.8.3 to 3.9.2
* Upgraded source package to DebSrc 3
* debian/rules: Using --with-wx-config=/usr/bin/wx-config in ./configure
* debian/rules: Added targets build-indep and build-arch
* Added menu file and XMP icon

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2005-2007 Gerald Schmidt.
 
3
 *
 
4
 * This file is part of Xml Copy Editor.
 
5
 *
 
6
 * Xml Copy Editor is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 * Xml Copy Editor is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with Xml Copy Editor; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <iostream>
 
21
#include <fstream>
 
22
#include <string>
 
23
#include <wx/aboutdlg.h>
 
24
#include "xmlcopyeditor.h"
 
25
#include "readfile.h"
 
26
#include "xmldoc.h"
 
27
#include "xmlctrl.h"
 
28
#include "wraplibxml.h"
 
29
#include "xmlschemalocator.h"
 
30
#include "xsllocator.h"
 
31
#include "xmlutf8reader.h"
 
32
#include "xmlpromptgenerator.h"
 
33
#include "xmlencodingspy.h"
 
34
#include "styledialog.h"
 
35
#include "mypropertysheet.h"
 
36
#include "wraptempfilename.h"
 
37
#include "globalreplacedialog.h"
 
38
#include "replace.h"
 
39
#include "associatedialog.h"
 
40
#include "xmlassociatexsd.h"
 
41
#include "xmlassociatexsl.h"
 
42
#include "xmlassociatedtd.h"
 
43
#include "wrapdaisy.h"
 
44
#include "aboutdialog.h"
 
45
#include "pathresolver.h"
 
46
#include "locationpanel.h"
 
47
#include "insertpanel.h"
 
48
#include "xmlwordcount.h"
 
49
#include "mynotebook.h"
 
50
#include "getlinuxappdir.h"
 
51
#include "commandpanel.h"
 
52
#include "binaryfile.h"
 
53
#include "exportdialog.h"
 
54
#include <wx/aui/auibook.h>
 
55
#include <wx/richtext/richtextsymboldlg.h>
 
56
#include <wx/textctrl.h>
 
57
#include <wx/artprov.h>
 
58
#include <wx/stockitem.h>
 
59
#include <iconv.h>
 
60
 
 
61
#define ngettext wxGetTranslation
 
62
 
 
63
#ifdef NEWFINDREPLACE
 
64
#include "findreplacepanel.h"
 
65
#endif
 
66
 
 
67
#include "wrapxerces.h"
 
68
#ifndef __WXMSW__
 
69
#include "xpm/appicon.xpm"
 
70
#endif
 
71
 
 
72
BEGIN_EVENT_TABLE ( MyFrame, wxFrame )
 
73
        EVT_ACTIVATE_APP ( MyFrame::OnActivateApp )
 
74
        EVT_CLOSE ( MyFrame::OnFrameClose )
 
75
        EVT_KEY_DOWN ( MyFrame::OnKeyPressed )
 
76
        EVT_MENU ( wxID_ABOUT, MyFrame::OnAbout )
 
77
        EVT_MENU ( wxID_CLOSE, MyFrame::OnClose )
 
78
        EVT_MENU ( wxID_CLOSE_ALL, MyFrame::OnCloseAll )
 
79
        EVT_MENU ( wxID_CUT, MyFrame::OnCut )
 
80
        EVT_MENU ( wxID_COPY, MyFrame::OnCopy )
 
81
        EVT_MENU ( wxID_HELP, MyFrame::OnHelp )
 
82
        EVT_MENU ( wxID_PASTE, MyFrame::OnPaste )
 
83
        EVT_MENU ( ID_PASTE_NEW_DOCUMENT, MyFrame::OnPasteNewDocument )
 
84
        EVT_MENU ( wxID_EXIT, MyFrame::OnQuit )
 
85
        EVT_MENU ( wxID_NEW, MyFrame::OnNew )
 
86
        EVT_MENU ( wxID_OPEN, MyFrame::OnOpen )
 
87
        EVT_MENU ( wxID_SAVE, MyFrame::OnSave )
 
88
        EVT_MENU ( wxID_SAVEAS, MyFrame::OnSaveAs )
 
89
        EVT_MENU ( ID_RELOAD, MyFrame::OnReload )
 
90
        EVT_MENU ( wxID_UNDO, MyFrame::OnUndo )
 
91
        EVT_MENU ( wxID_REDO, MyFrame::OnRedo )
 
92
        EVT_MENU ( wxID_REVERT, MyFrame::OnRevert )
 
93
        EVT_MENU ( ID_INSERT_CHILD, MyFrame::OnInsertChild )
 
94
        EVT_MENU ( ID_INSERT_SIBLING, MyFrame::OnInsertSibling )
 
95
        EVT_MENU ( ID_INSERT_ENTITY, MyFrame::OnInsertEntity )
 
96
        EVT_MENU ( ID_INSERT_TWIN, MyFrame::OnInsertTwin )
 
97
        EVT_MENU ( ID_INSERT_SYMBOL, MyFrame::OnInsertSymbol )
 
98
        EVT_MENU ( ID_TOGGLE_FOLD, MyFrame::OnToggleFold )
 
99
        EVT_MENU ( ID_FOLD_ALL, MyFrame::OnFoldAll )
 
100
        EVT_MENU ( ID_UNFOLD_ALL, MyFrame::OnUnfoldAll )
 
101
        EVT_MENU ( ID_OPEN_LARGE_FILE, MyFrame::OnOpen )
 
102
        EVT_MENU ( ID_PRINT_PREVIEW, MyFrame::OnPrintPreview )
 
103
        EVT_MENU ( ID_PRINT_SETUP, MyFrame::OnPrintSetup )
 
104
        EVT_MENU ( ID_PRINT, MyFrame::OnPrint )
 
105
        EVT_MENU ( ID_WORD_COUNT, MyFrame::OnWordCount )
 
106
        EVT_MENU ( ID_IMPORT_MSWORD, MyFrame::OnImportMSWord )
 
107
        EVT_MENU ( ID_EXPORT_MSWORD, MyFrame::OnExportMSWord )
 
108
        EVT_MENU ( ID_EXPORT, MyFrame::OnExport )
 
109
        EVT_MENU ( ID_HIDE_PANE, MyFrame::OnClosePane )
 
110
        EVT_MENU ( ID_COMMAND, MyFrame::OnCommand )
 
111
        EVT_MENU ( ID_FIND, MyFrame::OnFind )
 
112
        EVT_MENU ( ID_FIND_AGAIN, MyFrame::OnFindAgain )
 
113
        EVT_MENU ( ID_GOTO, MyFrame::OnGoto )
 
114
        EVT_MENU ( ID_FEEDBACK, MyFrame::OnFeedback )
 
115
        EVT_MENU ( ID_PREVIOUS_DOCUMENT, MyFrame::OnPreviousDocument )
 
116
        EVT_MENU ( ID_NEXT_DOCUMENT, MyFrame::OnNextDocument )
 
117
        EVT_MENU ( ID_BROWSER, MyFrame::OnBrowser )
 
118
        EVT_MENU ( ID_REPLACE, MyFrame::OnFindReplace )
 
119
        EVT_MENU ( ID_GLOBAL_REPLACE, MyFrame::OnGlobalReplace )
 
120
        EVT_MENU ( ID_CHECK_WELLFORMED, MyFrame::OnCheckWellformedness )
 
121
        EVT_MENU ( ID_VALIDATE_RELAX_NG, MyFrame::OnValidateRelaxNG )
 
122
        EVT_MENU ( ID_VALIDATE_W3C_SCHEMA, MyFrame::OnValidateSchema )
 
123
        EVT_MENU ( ID_XPATH, MyFrame::OnXPath )
 
124
        EVT_MENU_RANGE ( ID_XSLT, ID_XSLT_WORDML_DOCBOOK, MyFrame::OnXslt )
 
125
        EVT_MENU ( ID_PRETTYPRINT, MyFrame::OnPrettyPrint )
 
126
        EVT_MENU ( ID_ENCODING, MyFrame::OnEncoding )
 
127
        EVT_MENU ( ID_STYLE, MyFrame::OnSpelling )
 
128
        EVT_MENU ( ID_SPELL, MyFrame::OnSpelling )
 
129
        EVT_MENU ( ID_FONT_SMALLER, MyFrame::OnFontSmaller )
 
130
        EVT_MENU ( ID_FONT_NORMAL, MyFrame::OnFontMedium )
 
131
        EVT_MENU ( ID_FONT_LARGER, MyFrame::OnFontLarger )
 
132
        EVT_MENU ( ID_OPTIONS, MyFrame::OnOptions )
 
133
        EVT_MENU ( ID_HOME, MyFrame::OnHome )
 
134
        EVT_MENU ( ID_DOWNLOAD_SOURCE, MyFrame::OnDownloadSource )
 
135
        EVT_MENU ( ID_TOOLBAR_VISIBLE, MyFrame::OnToolbarVisible )
 
136
        EVT_MENU ( ID_LOCATION_PANE_VISIBLE, MyFrame::OnLocationPaneVisible )
 
137
        EVT_MENU ( ID_PROTECT_TAGS, MyFrame::OnProtectTags )
 
138
        EVT_MENU ( ID_WRAP_WORDS, MyFrame::OnWrapWords )
 
139
        EVT_MENU_RANGE ( ID_SHOW_TAGS, ID_HIDE_TAGS, MyFrame::OnVisibilityState )
 
140
        EVT_MENU_RANGE ( ID_ASSOCIATE_DTD_PUBLIC, ID_ASSOCIATE_XSL, MyFrame::OnAssociate )
 
141
        EVT_MENU_RANGE ( wxID_FILE1, wxID_FILE9, MyFrame::OnHistoryFile )
 
142
        EVT_MENU_RANGE (
 
143
            ID_VALIDATE_PRESET1, ID_VALIDATE_PRESET9, MyFrame::OnValidatePreset )
 
144
        EVT_MENU_RANGE (
 
145
            ID_COLOR_SCHEME_DEFAULT,
 
146
            ID_COLOR_SCHEME_NONE,
 
147
            MyFrame::OnColorScheme )
 
148
        
 
149
        EVT_MENU_RANGE (
 
150
            ID_SPLIT_TAB_TOP,
 
151
            ID_SPLIT_TAB_LEFT,
 
152
            MyFrame::OnSplitTab )
 
153
        
 
154
        EVT_UPDATE_UI_RANGE ( ID_REPLACE, ID_GLOBAL_REPLACE, MyFrame::OnUpdateReplaceRange )
 
155
        EVT_FIND ( wxID_ANY, MyFrame::OnDialogFind )
 
156
        EVT_FIND_NEXT ( wxID_ANY, MyFrame::OnDialogFind )
 
157
        EVT_FIND_REPLACE ( wxID_ANY, MyFrame::OnDialogReplace )
 
158
        EVT_FIND_REPLACE_ALL ( wxID_ANY, MyFrame::OnDialogReplaceAll )
 
159
        EVT_ICONIZE ( MyFrame::OnIconize )
 
160
        EVT_UPDATE_UI ( ID_LOCATION_PANE_VISIBLE, MyFrame::OnUpdateLocationPaneVisible )
 
161
        EVT_UPDATE_UI ( wxID_CLOSE, MyFrame::OnUpdateDocRange )
 
162
        EVT_UPDATE_UI ( wxID_SAVEAS, MyFrame::OnUpdateDocRange )
 
163
        EVT_UPDATE_UI ( wxID_CLOSE_ALL, MyFrame::OnUpdateCloseAll )
 
164
        EVT_UPDATE_UI_RANGE ( ID_SPLIT_TAB_TOP, ID_SPLIT_TAB_LEFT, MyFrame::OnUpdateCloseAll )
 
165
        EVT_UPDATE_UI ( wxID_REVERT, MyFrame::OnUpdateUndo )
 
166
        EVT_UPDATE_UI ( wxID_SAVE, MyFrame::OnUpdateDocRange ) // always allow save if doc present
 
167
        EVT_UPDATE_UI ( wxID_UNDO, MyFrame::OnUpdateUndo )
 
168
        EVT_UPDATE_UI ( wxID_REDO, MyFrame::OnUpdateRedo )
 
169
        EVT_UPDATE_UI ( wxID_PASTE, MyFrame::OnUpdatePaste )
 
170
        EVT_UPDATE_UI ( wxID_CUT, MyFrame::OnUpdateCutCopy )
 
171
        EVT_UPDATE_UI ( wxID_COPY, MyFrame::OnUpdateCutCopy )
 
172
        EVT_UPDATE_UI ( ID_FIND_AGAIN, MyFrame::OnUpdateFindAgain )
 
173
        EVT_UPDATE_UI_RANGE ( ID_FIND, ID_EXPORT_MSWORD, MyFrame::OnUpdateDocRange )
 
174
        EVT_UPDATE_UI ( ID_PREVIOUS_DOCUMENT, MyFrame::OnUpdatePreviousDocument )
 
175
        EVT_UPDATE_UI ( ID_NEXT_DOCUMENT, MyFrame::OnUpdateNextDocument )
 
176
        EVT_UPDATE_UI ( ID_HIDE_PANE, MyFrame::OnUpdateClosePane )
 
177
        EVT_UPDATE_UI ( ID_RELOAD, MyFrame::OnUpdateReload )
 
178
        EVT_IDLE ( MyFrame::OnIdle )
 
179
        EVT_AUINOTEBOOK_PAGE_CLOSE ( wxID_ANY, MyFrame::OnPageClosing )
 
180
#ifdef __WXMSW__
 
181
        EVT_DROP_FILES ( MyFrame::OnDropFiles )
 
182
#endif
 
183
END_EVENT_TABLE()
 
184
 
 
185
IMPLEMENT_APP ( MyApp)
 
186
 
 
187
MyApp::MyApp() : checker ( NULL ), server ( NULL ), connection ( NULL ),
 
188
#ifdef __WXMSW__
 
189
       config ( new wxFileConfig ( _T ( ".xmlcopyeditor" ) ) )//( _T ( "SourceForge Project\\XML Copy Editor" ) ) )
 
190
#else
 
191
                config ( new wxFileConfig ( _T ( "xmlcopyeditor" ) ) )
 
192
#endif
 
193
{
 
194
        lang = 0;
 
195
 
 
196
#ifdef __WXGTK__
 
197
        int fdnull = open ( "/dev/null", O_WRONLY, 0 );
 
198
        dup2 ( fdnull, STDERR_FILENO );
 
199
#endif
 
200
        myLocale.Init();
 
201
        int systemLocale = myLocale.GetSystemLanguage();
 
202
        switch ( systemLocale )
 
203
        {
 
204
                case wxLANGUAGE_GERMAN:
 
205
                case wxLANGUAGE_GERMAN_AUSTRIAN:
 
206
                case wxLANGUAGE_GERMAN_BELGIUM:
 
207
                case wxLANGUAGE_GERMAN_LIECHTENSTEIN:
 
208
                case wxLANGUAGE_GERMAN_LUXEMBOURG:
 
209
                case wxLANGUAGE_GERMAN_SWISS:
 
210
                        systemLocale = wxLANGUAGE_GERMAN;
 
211
                        break;
 
212
                case wxLANGUAGE_CHINESE_SIMPLIFIED:
 
213
                        systemLocale = wxLANGUAGE_CHINESE_SIMPLIFIED;
 
214
                        break;
 
215
                case wxLANGUAGE_CHINESE_TRADITIONAL:
 
216
                        systemLocale = wxLANGUAGE_CHINESE_TRADITIONAL;
 
217
                        break;
 
218
                case wxLANGUAGE_CATALAN:
 
219
            systemLocale = wxLANGUAGE_CATALAN;
 
220
            break;
 
221
                case wxLANGUAGE_SPANISH:
 
222
                case wxLANGUAGE_SPANISH_ARGENTINA:
 
223
                case wxLANGUAGE_SPANISH_BOLIVIA:
 
224
                case wxLANGUAGE_SPANISH_CHILE:
 
225
                case wxLANGUAGE_SPANISH_COLOMBIA:
 
226
                case wxLANGUAGE_SPANISH_COSTA_RICA:
 
227
                case wxLANGUAGE_SPANISH_DOMINICAN_REPUBLIC:
 
228
                case wxLANGUAGE_SPANISH_ECUADOR:
 
229
                case wxLANGUAGE_SPANISH_EL_SALVADOR:
 
230
                case wxLANGUAGE_SPANISH_GUATEMALA:
 
231
                case wxLANGUAGE_SPANISH_HONDURAS:
 
232
                case wxLANGUAGE_SPANISH_MEXICAN:
 
233
                case wxLANGUAGE_SPANISH_MODERN:
 
234
                case wxLANGUAGE_SPANISH_NICARAGUA:
 
235
                case wxLANGUAGE_SPANISH_PANAMA:
 
236
                case wxLANGUAGE_SPANISH_PARAGUAY:
 
237
                case wxLANGUAGE_SPANISH_PERU:
 
238
                case wxLANGUAGE_SPANISH_PUERTO_RICO:
 
239
                case wxLANGUAGE_SPANISH_URUGUAY:
 
240
                case wxLANGUAGE_SPANISH_US:
 
241
                case wxLANGUAGE_SPANISH_VENEZUELA:
 
242
                        systemLocale = wxLANGUAGE_SPANISH;
 
243
                        break;
 
244
                case wxLANGUAGE_SLOVAK:
 
245
                        systemLocale = wxLANGUAGE_SLOVAK;
 
246
                        break;
 
247
                case wxLANGUAGE_SWEDISH:
 
248
                        systemLocale = wxLANGUAGE_SWEDISH;
 
249
                        break;
 
250
                case wxLANGUAGE_FRENCH:
 
251
                        systemLocale = wxLANGUAGE_FRENCH;
 
252
                        break;
 
253
                case wxLANGUAGE_UKRAINIAN:
 
254
                        systemLocale = wxLANGUAGE_UKRAINIAN;
 
255
                        break;
 
256
                case wxLANGUAGE_ITALIAN:
 
257
                        systemLocale = wxLANGUAGE_ITALIAN;
 
258
                        break;
 
259
                case wxLANGUAGE_RUSSIAN:
 
260
                        systemLocale = wxLANGUAGE_RUSSIAN;
 
261
                        break;
 
262
                case wxLANGUAGE_DUTCH:
 
263
                        systemLocale = wxLANGUAGE_DUTCH;
 
264
                        break;
 
265
                default:
 
266
                        systemLocale = wxLANGUAGE_ENGLISH_US;
 
267
                        break;
 
268
        }
 
269
 
 
270
        if ( config.get() )
 
271
        {
 
272
#ifdef __WXMSW__
 
273
                singleInstanceCheck = config->Read ( _T ( "singleInstanceCheck" ), true );
 
274
#else
 
275
                long longFalse = 0;
 
276
                singleInstanceCheck = config->Read ( _T ( "singleInstanceCheck" ), longFalse );
 
277
#endif
 
278
                lang = config->Read ( _T ( "lang" ), systemLocale );
 
279
        }
 
280
        else
 
281
        {
 
282
                lang = systemLocale;
 
283
#ifdef __WXMSW__
 
284
                singleInstanceCheck = true;
 
285
#else
 
286
                singleInstanceCheck = false;
 
287
#endif
 
288
        }
 
289
 
 
290
        myLocale.Init ( lang, wxLOCALE_LOAD_DEFAULT );
 
291
        wxLocale::AddCatalogLookupPathPrefix ( wxT ( "." ) );
 
292
        wxLocale::AddCatalogLookupPathPrefix ( wxT ( ".." ) );
 
293
 
 
294
#ifndef __WXMSW__
 
295
        wxString poDir = GetLinuxAppDir::run() + wxFileName::GetPathSeparator() + _T ( "po" ) + wxFileName::GetPathSeparator();
 
296
        wxLocale::AddCatalogLookupPathPrefix ( poDir );
 
297
#endif
 
298
 
 
299
        if ( !myLocale.AddCatalog ( _T ( "messages" ) ) )
 
300
                ;
 
301
 
 
302
#ifndef __WXMSW__
 
303
        {
 
304
                wxLogNull noLog;
 
305
                myLocale.AddCatalog ( _T ( "fileutils" ) );
 
306
        }
 
307
#endif
 
308
}
 
309
 
 
310
MyApp::~MyApp()
 
311
{
 
312
        delete checker;
 
313
        delete server;
 
314
        delete connection;
 
315
}
 
316
 
 
317
bool MyApp::OnInit()
 
318
{
 
319
 
 
320
        wxString name, service, hostName;
 
321
        name.Printf ( _T ( "xmlcopyeditor-%s" ), wxGetUserId().c_str() );
 
322
        service = IPC_SERVICE;
 
323
        hostName = _T ( "localhost" );
 
324
 
 
325
        if ( singleInstanceCheck )
 
326
        {
 
327
        checker = new wxSingleInstanceChecker ( name );
 
328
                while ( checker->IsAnotherRunning() )
 
329
                {
 
330
                        // attempt calling server
 
331
                        client = new MyClient();
 
332
                        connection = ( MyClientConnection * )
 
333
                                     client->MakeConnection ( hostName, service, IPC_TOPIC );
 
334
                        if ( !connection || !connection->StartAdvise ( IPC_ADVISE_NAME ) )
 
335
                                break;
 
336
                        else
 
337
                        {
 
338
                                wxString argument, what;
 
339
                                wxChar *whatBuffer;
 
340
                                what = _T ( "Data" );
 
341
                                whatBuffer = (wxChar *)what.c_str();
 
342
                                if ( this->argc > 1 )
 
343
                                {
 
344
                                        for ( int i = 1; i < this->argc; i++ )
 
345
                                        {
 
346
                                                argument = ( wxString ) this->argv[i];
 
347
                                                argument = PathResolver::run ( argument );
 
348
                                                if ( ! connection->Poke ( argument, whatBuffer ) )
 
349
                                                   break;
 
350
                                        }
 
351
                                }
 
352
                                else
 
353
                                {
 
354
                                        argument = ( wxString ) IPC_NO_FILE;
 
355
                                        connection->Poke ( argument, whatBuffer );
 
356
                                }
 
357
                                return false;
 
358
                        }
 
359
                }
 
360
        }
 
361
 
 
362
        server = new MyServer;
 
363
        server->Create ( service );
 
364
 
 
365
        MyFrame *frame;
 
366
        try
 
367
        {
 
368
                wxImage::AddHandler ( new wxPNGHandler );
 
369
                wxSystemOptions::SetOption ( _T ( "msw.remap" ), 0 );
 
370
                frame = new MyFrame (
 
371
                    _ ( "XML Copy Editor" ),
 
372
                    config.get(),
 
373
                    myLocale,
 
374
                    singleInstanceCheck,
 
375
                    lang );
 
376
                frame->Show ( true );
 
377
                if ( frame->getHandleCommandLineFlag() )
 
378
                        frame->handleCommandLine();
 
379
        }
 
380
        catch ( exception &e )
 
381
        {
 
382
                const char *what;
 
383
                what = e.what();
 
384
                wxString wideWhat, errorString;
 
385
                wideWhat = wxString ( what, wxConvLocal, strlen ( what ) );
 
386
 
 
387
                if ( wideWhat.empty() )
 
388
                        wideWhat = _ ( "(unknown error)" );
 
389
 
 
390
                errorString = _ ( "XML Copy Editor has encountered the following error and needs to close: " );
 
391
                errorString += wideWhat;
 
392
                errorString += _T ( "." );
 
393
#ifdef __WXMSW__
 
394
                ::MessageBox (
 
395
                    NULL,
 
396
                    errorString,
 
397
                    _ ( "Error" ),
 
398
                    MB_ICONERROR | MB_TASKMODAL );
 
399
#else
 
400
                wxMessageBox ( errorString, _ ( "Error" ), wxICON_ERROR );
 
401
#endif
 
402
                exit ( EXIT_FAILURE );
 
403
        }
 
404
        catch ( ... )
 
405
        {
 
406
                exit ( EXIT_FAILURE );
 
407
        }
 
408
        return true;
 
409
}
 
410
 
 
411
void MyApp::OnUnhandledException()
 
412
{
 
413
#ifdef __WXMSW__
 
414
        ::MessageBox (
 
415
            NULL,
 
416
            _ ( "XML Copy Editor has encountered an error and needs to close." ),
 
417
            _ ( "Error" ),
 
418
            MB_ICONERROR | MB_TASKMODAL );
 
419
#else
 
420
        wxMessageBox (
 
421
            _ ( "XML Copy Editor has encountered an error and needs to close." ),
 
422
            _ ( "Error" ),
 
423
            wxICON_ERROR );
 
424
#endif
 
425
        exit ( EXIT_FAILURE );
 
426
}
 
427
 
 
428
bool MyApp::OnExceptionInMainLoop()
 
429
{
 
430
        try
 
431
        {
 
432
                throw;
 
433
        }
 
434
#ifdef __WXMSW__
 
435
        catch ( bad_alloc& )
 
436
        {
 
437
                ::MessageBox (
 
438
                    NULL,
 
439
                    _ ( "The operating system has turned down a request for additional memory" ),
 
440
                    _ ( "Out of memory" ),
 
441
                    MB_ICONERROR );
 
442
                return true;
 
443
        }
 
444
#endif
 
445
        catch ( exception &e )
 
446
        {
 
447
                const char *what;
 
448
                what = e.what();
 
449
                wxString wideWhat, errorString;
 
450
                wideWhat = wxString ( what, wxConvLocal, strlen ( what ) );
 
451
 
 
452
                if ( wideWhat.empty() )
 
453
                        _ ( "(unknown error)" );
 
454
 
 
455
                errorString = _ ( "The following error has occurred: " );
 
456
                errorString += wideWhat;
 
457
                errorString += _ ( ".\n\nSelect \"Abort\" to exit, \"Retry\" to close this window and \"Ignore\" to continue." );
 
458
#ifdef __WXMSW__
 
459
                int ret = ::MessageBox (
 
460
                              NULL,
 
461
                              errorString,
 
462
                              _ ( "Error" ),
 
463
                              MB_ABORTRETRYIGNORE |
 
464
                              MB_ICONERROR |
 
465
                              MB_TASKMODAL );
 
466
                switch ( ret )
 
467
                {
 
468
                        case IDABORT:
 
469
                                exit ( EXIT_FAILURE );
 
470
                                break;
 
471
                        case IDRETRY:
 
472
                                return false;
 
473
                        case IDIGNORE:
 
474
                                return true;
 
475
                        default:
 
476
                                throw;
 
477
                }
 
478
#else
 
479
                // wxGTK does not reach this point; see HandleEvent below
 
480
                wxMessageBox (
 
481
                    errorString,
 
482
                    _ ( "Error" ),
 
483
                    wxICON_ERROR );
 
484
                return false;
 
485
#endif
 
486
        }
 
487
        catch ( ... )
 
488
        {
 
489
                wxString otherError ( _ ( "XML Copy Editor has encountered an error and needs to close." ) );
 
490
#ifdef __WXMSW__
 
491
                ::MessageBox (
 
492
                    NULL,
 
493
                    otherError,
 
494
                    _ ( "Error" ),
 
495
                    MB_ICONERROR );
 
496
                return false;
 
497
#else
 
498
                wxMessageBox (
 
499
                    otherError,
 
500
                    _ ( "Error" ),
 
501
                    wxICON_ERROR );
 
502
                return false;
 
503
#endif
 
504
        }
 
505
        return false;
 
506
}
 
507
 
 
508
#ifndef __WXMSW__
 
509
void MyApp::HandleEvent ( wxEvtHandler *handler, wxEventFunction func, wxEvent& event ) const
 
510
{
 
511
        try
 
512
        {
 
513
                wxApp::HandleEvent ( handler, func, event );
 
514
        }
 
515
        catch ( std::bad_alloc& )
 
516
        {
 
517
                wxMessageBox (
 
518
                    _ ( "The operating system has turned down a request for additional memory" ),
 
519
                    _ ( "Out of memory" ),
 
520
                    wxICON_ERROR );
 
521
                return;
 
522
        }
 
523
        catch ( std::exception& e )
 
524
        {
 
525
                std::string s ( e.what() );
 
526
                wxString ws = wxString ( s.c_str(), wxConvUTF8, s.size() );
 
527
                wxMessageBox (
 
528
                    ws,
 
529
                    _ ( "Error" ),
 
530
                    wxICON_ERROR );
 
531
                return;
 
532
        }
 
533
        catch ( ... )
 
534
        {
 
535
                throw;
 
536
        }
 
537
}
 
538
#endif
 
539
 
 
540
MyFrame::MyFrame (
 
541
    const wxString& title,
 
542
    wxFileConfig *configParameter,
 
543
    wxLocale& locale,
 
544
    bool singleInstanceCheckParameter,
 
545
    int langParameter ) :
 
546
                wxFrame ( NULL, wxID_ANY, title ),
 
547
                config ( configParameter ),
 
548
                myLocale ( locale ),
 
549
                singleInstanceCheck ( singleInstanceCheckParameter ),
 
550
                lang ( langParameter ),
 
551
                htmlPrinting ( new wxHtmlEasyPrinting (
 
552
                                   wxEmptyString,
 
553
                                   this ) ),
 
554
                findDialog ( 0 ),
 
555
#ifndef __WXMSW__
 
556
                helpController ( new wxHtmlHelpController() ),
 
557
#endif
 
558
                menuBar ( 0 ),
 
559
                toolBar ( 0 ),
 
560
                xmlMenu ( 0 ),
 
561
                mainBook ( 0 ),
 
562
                restoreFocusToNotebook ( false )
 
563
{
 
564
        manager.SetManagedWindow ( this );
 
565
 
 
566
        lastPos = 0;
 
567
        htmlReport = NULL;
 
568
        lastDoc = NULL;
 
569
 
 
570
        wxString defaultFont =
 
571
#ifdef __WXMSW__
 
572
            _T ( "Arial" );
 
573
#else
 
574
            _T ( "Bitstream Vera Sans" );
 
575
#endif
 
576
 
 
577
        bool findMatchCase;
 
578
 
 
579
        // fetch configuration
 
580
        if ( config ) // config found
 
581
        {
 
582
                history.Load ( *config );
 
583
                properties.insertCloseTag =
 
584
                    config->Read ( _T ( "insertCloseTag" ), true );
 
585
                properties.completion =
 
586
                    config->Read ( _T ( "completion" ), true );
 
587
                properties.number =
 
588
                    config->Read ( _T ( "number" ), true );
 
589
                properties.fold =
 
590
                    config->Read ( _T ( "fold" ), true );
 
591
                properties.currentLine =
 
592
                    config->Read ( _T ( "currentLine" ), true );
 
593
                properties.highlightSyntax =
 
594
                    config->Read ( _T ( "highlightSyntax" ), true );
 
595
                properties.whitespaceVisible =
 
596
                    config->Read ( _T ( "whitespaceVisible" ), true );
 
597
                properties.indentLines =
 
598
                    config->Read ( _T ( "indentLines" ), true );
 
599
                properties.toggleLineBackground =
 
600
                    config->Read ( _T ( "toggleLineBackground" ), true );
 
601
                properties.protectHiddenElements =
 
602
                    config->Read ( _T ( "protectHiddenElements" ), true );
 
603
                properties.deleteWholeTag =
 
604
                    config->Read ( _T ( "deleteWholeTag" ), true );
 
605
                properties.validateAsYouType =
 
606
                    config->Read ( _T ( "validateAsYouType" ), true );
 
607
                properties.font =
 
608
                    config->Read ( _T ( "font" ), defaultFont );
 
609
                findRegex =
 
610
                    config->Read ( _T ( "findRegex" ), true );
 
611
                xpathExpression =
 
612
                    config->Read ( _T ( "xpathExpression" ), wxEmptyString );
 
613
                lastXslStylesheet =
 
614
                    config->Read ( _T ( "lastXslStylesheet" ), wxEmptyString );
 
615
                lastRelaxNGSchema =
 
616
                    config->Read ( _T ( "lastRelaxNGSchema" ), wxEmptyString );
 
617
 
 
618
                lastXslStylesheet.Replace ( _T ( " " ), _T ( "%20" ), true );
 
619
                lastRelaxNGSchema.Replace ( _T ( " " ), _T ( "%20" ), true );
 
620
                
 
621
                exportQuiet =
 
622
                  config->Read ( _T ( "exportQuiet" ), (long)false );
 
623
                exportMp3Album =
 
624
                  config->Read ( _T ( "exportMp3Album" ), (long)false );
 
625
                
 
626
 
 
627
                applicationDir =
 
628
                    config->Read ( _T ( "applicationDir" ), wxEmptyString );
 
629
                if ( applicationDir.empty() )
 
630
                {
 
631
#ifdef __WXMSW__
 
632
                        applicationDir =
 
633
                            config->Read ( _T ( "InstallPath" ), wxGetCwd() );
 
634
#else
 
635
                        applicationDir = GetLinuxAppDir::run();
 
636
#endif
 
637
                }
 
638
                browserCommand =
 
639
                    config->Read ( _T ( "browserCommand" ), wxEmptyString );
 
640
 
 
641
                // if default value != true, type as long int
 
642
                long valZoom, longFalse;
 
643
                longFalse = false;
 
644
                valZoom = 0;
 
645
                frameWidth = frameHeight = framePosX = framePosY = 0;
 
646
 
 
647
                properties.wrap =
 
648
                    config->Read ( _T ( "wrap" ), longFalse );
 
649
 
 
650
                properties.zoom =
 
651
                    config->Read ( _T ( "zoom" ), valZoom );
 
652
 
 
653
                properties.colorScheme = config->Read ( _T ( "colorScheme" ), COLOR_SCHEME_DEFAULT );
 
654
 
 
655
                globalReplaceAllDocuments =
 
656
                    config->Read ( _T ( "globalReplaceAllDocuments" ), longFalse );
 
657
                showFullPathOnFrame =
 
658
                    config->Read ( _T ( "showFullPathOnFrame" ), longFalse );
 
659
                findMatchCase =
 
660
                    config->Read ( _T ( "findMatchCase" ), longFalse );
 
661
 
 
662
                commandSync = config->Read ( _T ( "commandSync" ), longFalse );
 
663
                commandOutput = config->Read ( _T ( "commandOutput" ), ID_COMMAND_OUTPUT_IGNORE );
 
664
                commandString = config->Read ( _T ( "commandString" ), wxEmptyString );
 
665
                
 
666
                exportStylesheet = config->Read ( _T ( "exportStylesheet" ), wxEmptyString );
 
667
                exportFolder = config->Read ( _T ( "exportFolder" ), wxEmptyString );
 
668
 
 
669
                ruleSetPreset =
 
670
                    config->Read ( _T ( "ruleSetPreset" ), _ ( "Default style" ) );
 
671
                dictionaryPreset = 
 
672
                    config->Read ( _T ( "dictionaryPreset" ), _ ( "en_US" ) );
 
673
                
 
674
                filterPreset =
 
675
                    config->Read ( _T ( "filterPreset" ), _ ( "(No filter)" ) );
 
676
                findData.SetFindString ( config->Read ( _T ( "findReplaceFind" ), _T ( "" ) ) );
 
677
                findData.SetReplaceString ( config->Read ( _T ( "findReplaceReplace" ), _T ( "" ) ) );
 
678
 
 
679
                toolbarVisible =
 
680
                    config->Read ( _T ( "toolbarVisible" ), true );
 
681
                protectTags = config->Read ( _T ( "protectTags" ), longFalse );
 
682
                visibilityState = config->Read ( _T ( "visibilityState" ), ID_SHOW_TAGS );
 
683
 
 
684
                framePosX = config->Read ( _T ( "framePosX" ), framePosX );
 
685
                framePosY = config->Read ( _T ( "framePosY" ), framePosY );
 
686
                frameWidth = config->Read ( _T ( "frameWidth" ), frameWidth );
 
687
                frameHeight = config->Read ( _T ( "frameHeight" ), frameHeight );
 
688
                rememberOpenTabs = config->Read ( _T ( "rememberOpenTabs" ), true );
 
689
                libxmlNetAccess = config->Read ( _T ( "libxmlNetAccess" ), longFalse );
 
690
                openTabsOnClose = config->Read ( _T ( "openTabsOnClose" ), _T ( "" ) );
 
691
                notebookStyle = config->Read ( _T ( "notebookStyle" ), ID_NOTEBOOK_STYLE_VC8_COLOR );
 
692
                saveBom = config->Read ( _T ( "saveBom" ), true );
 
693
                unlimitedUndo = config->Read ( _T ( "unlimitedUndo" ), true );
 
694
                layout = config->Read ( _T ( "layout" ), wxEmptyString );
 
695
                restoreLayout = config->Read ( _T ( "restoreLayout" ), true );
 
696
                showLocationPane = config->Read ( _T ( "showLocationPane" ), true );
 
697
                showInsertChildPane = config->Read ( _T ( "showInsertChildPane" ), true );
 
698
                showInsertSiblingPane = config->Read ( _T ( "showInsertSiblingPane" ), true );
 
699
                showInsertEntityPane = config->Read ( _T ( "showInsertEntityPane" ), true );
 
700
                expandInternalEntities = config->Read ( _T ( "expandInternalEntities" ), true );
 
701
 
 
702
        }
 
703
        else // config not found
 
704
        {
 
705
                properties.insertCloseTag =
 
706
                    properties.completion =
 
707
                        properties.currentLine =
 
708
                            properties.indentLines =
 
709
                                properties.protectHiddenElements =
 
710
                                    properties.toggleLineBackground =
 
711
                                        properties.deleteWholeTag =
 
712
                                            properties.highlightSyntax = true;
 
713
                properties.font = defaultFont;
 
714
                properties.wrap = properties.whitespaceVisible = false;
 
715
                properties.zoom = 0;
 
716
                properties.colorScheme = COLOR_SCHEME_DEFAULT;
 
717
#ifdef __WXMSW__
 
718
                applicationDir = wxGetCwd();
 
719
#else
 
720
                applicationDir = GetLinuxAppDir::run();//getLinuxApplicationDir();
 
721
#endif
 
722
                ruleSetPreset = _ ( "Default style" );
 
723
                dictionaryPreset = _ ( "en_US" );
 
724
                filterPreset = _ ( "No filter" );
 
725
                xpathExpression = lastXslStylesheet = lastRelaxNGSchema = wxEmptyString;
 
726
                findRegex = true;
 
727
                findMatchCase = globalReplaceAllDocuments =
 
728
                                    showFullPathOnFrame = false;
 
729
                toolbarVisible = true;
 
730
                protectTags = false;
 
731
                visibilityState = SHOW_TAGS;
 
732
                framePosX = framePosY = frameWidth = frameHeight = 0;
 
733
                rememberOpenTabs = true;
 
734
                libxmlNetAccess = false;
 
735
                openTabsOnClose = wxEmptyString;
 
736
                browserCommand = wxEmptyString;
 
737
                notebookStyle = ID_NOTEBOOK_STYLE_VC8_COLOR;
 
738
                saveBom = unlimitedUndo = true;
 
739
                layout = wxEmptyString;
 
740
                restoreLayout = true;
 
741
                showLocationPane = true;
 
742
                showInsertChildPane = true;
 
743
                showInsertSiblingPane = true;
 
744
                showInsertEntityPane = true;
 
745
                expandInternalEntities = true;
 
746
                properties.validateAsYouType = true;
 
747
 
 
748
                commandSync = false;
 
749
                commandOutput = ID_COMMAND_OUTPUT_IGNORE;
 
750
                commandString = wxEmptyString;
 
751
                
 
752
                exportStylesheet = exportFolder = wxEmptyString;
 
753
                exportQuiet = exportMp3Album = false;
 
754
 
 
755
        }
 
756
 
 
757
        largeFileProperties.completion = false;
 
758
        largeFileProperties.fold = false;
 
759
        largeFileProperties.whitespaceVisible = false;
 
760
        largeFileProperties.wrap = false;
 
761
        largeFileProperties.indentLines = false;
 
762
        largeFileProperties.protectHiddenElements = false;
 
763
        largeFileProperties.toggleLineBackground = false;
 
764
        largeFileProperties.toggleLineBackground = false;
 
765
        largeFileProperties.insertCloseTag = false;
 
766
        largeFileProperties.deleteWholeTag = false;
 
767
        largeFileProperties.highlightSyntax = false;
 
768
        largeFileProperties.validateAsYouType = false;
 
769
        largeFileProperties.number = properties.number;
 
770
        largeFileProperties.currentLine = properties.currentLine;
 
771
        largeFileProperties.font = properties.font;
 
772
        largeFileProperties.zoom = 0;
 
773
        largeFileProperties.colorScheme = COLOR_SCHEME_NONE;
 
774
 
 
775
        updatePaths();
 
776
        loadBitmaps();
 
777
 
 
778
        size_t findFlags = 0;
 
779
        findFlags |= wxFR_DOWN;
 
780
 
 
781
        if ( findMatchCase )
 
782
                findFlags |= wxFR_MATCHCASE;
 
783
 
 
784
        findData.SetFlags ( findFlags );
 
785
 
 
786
        if ( browserCommand.empty() )
 
787
        {
 
788
#ifdef __WXMSW__
 
789
                browserCommand = binDir + _T ( "navigate.exe" );
 
790
#else
 
791
                browserCommand = getLinuxBrowser();
 
792
#endif
 
793
        }
 
794
 
 
795
        // initialise document count for tab labels
 
796
        documentCount = 1;
 
797
 
 
798
        SetIcon ( wxICON ( appicon ) );
 
799
 
 
800
        CreateStatusBar();
 
801
        wxStatusBar *status = GetStatusBar();
 
802
        int widths[] = { -24, -6, -6, -6, -8 };
 
803
        status->SetFieldsCount ( 5 );
 
804
        status->SetStatusWidths ( 5, widths );
 
805
 
 
806
        if ( !frameWidth ||
 
807
                !frameHeight ||
 
808
                frameWidth < 0 ||
 
809
                frameHeight < 0 ||
 
810
                framePosX < 0 ||
 
811
                framePosY < 0 )
 
812
        {
 
813
#ifdef __WXMSW__
 
814
                Maximize();
 
815
#else
 
816
                SetSize ( 50, 50, 640, 480 );
 
817
#endif
 
818
        }
 
819
        else
 
820
        {
 
821
                SetSize ( framePosX, framePosY, frameWidth, frameHeight );
 
822
        }
 
823
 
 
824
        stylePosition = aboutPosition = wxDefaultPosition;
 
825
        styleSize = wxSize ( 720, 540 );
 
826
 
 
827
        showTopBars ( toolbarVisible );
 
828
 
 
829
        long style = wxAUI_NB_TOP |
 
830
                     wxAUI_NB_TAB_SPLIT |
 
831
                     wxAUI_NB_TAB_MOVE |
 
832
                     wxAUI_NB_WINDOWLIST_BUTTON |
 
833
                     wxAUI_NB_CLOSE_ON_ALL_TABS |
 
834
                     wxNO_BORDER;
 
835
 
 
836
        mainBook = new MyNotebook (
 
837
            this,
 
838
            ID_NOTEBOOK,
 
839
            wxDefaultPosition,
 
840
            wxDefaultSize,
 
841
            style );
 
842
 
 
843
        manager.AddPane ( mainBook, wxAuiPaneInfo().CenterPane()
 
844
                          .PaneBorder ( false ).Name ( _T ( "documentPane" ) ) );
 
845
        manager.GetPane ( mainBook ).dock_proportion = 10;
 
846
 
 
847
        // add insert child panes
 
848
        locationPanel = new LocationPanel ( this, ID_LOCATION_PANEL );
 
849
        insertChildPanel = new InsertPanel ( this, ID_INSERT_CHILD_PANEL,
 
850
                                             INSERT_PANEL_TYPE_CHILD );
 
851
        insertSiblingPanel = new InsertPanel ( this, ID_INSERT_SIBLING_PANEL,
 
852
                                               INSERT_PANEL_TYPE_SIBLING );
 
853
        insertEntityPanel = new InsertPanel ( this, ID_INSERT_ENTITY_PANEL,
 
854
                                              INSERT_PANEL_TYPE_ENTITY );
 
855
 
 
856
#ifdef __WXMSW__
 
857
        manager.AddPane ( ( wxWindow * ) locationPanel, wxRIGHT, _ ( "Current Element" ) );
 
858
        manager.AddPane ( ( wxWindow * ) insertChildPanel, wxRIGHT, _ ( "Insert Element" ) );
 
859
        manager.AddPane ( ( wxWindow * ) insertSiblingPanel, wxRIGHT, _ ( "Insert Sibling" ) );
 
860
        manager.AddPane ( ( wxWindow * ) insertEntityPanel, wxRIGHT, _ ( "Insert Entity" ) );
 
861
#else
 
862
        manager.AddPane ( ( wxWindow * ) insertEntityPanel, wxRIGHT, _ ( "Insert Entity" ) );
 
863
        manager.AddPane ( ( wxWindow * ) insertSiblingPanel, wxRIGHT, _ ( "Insert Sibling" ) );
 
864
        manager.AddPane ( ( wxWindow * ) insertChildPanel, wxRIGHT, _ ( "Insert Element" ) );
 
865
        manager.AddPane ( ( wxWindow * ) locationPanel, wxRIGHT, _ ( "Current Element" ) );
 
866
#endif
 
867
 
 
868
        manager.GetPane ( locationPanel ).Name ( _T ( "locationPane" ) ).Show (
 
869
            ( restoreLayout ) ? showLocationPane : true ).DestroyOnClose ( false ).PinButton ( true );
 
870
        manager.GetPane ( locationPanel ).dock_proportion = 1;
 
871
 
 
872
        manager.GetPane ( insertChildPanel ).Name ( _T ( "insertChildPane" ) ).Show (
 
873
            ( restoreLayout ) ? showInsertChildPane : true ).DestroyOnClose ( false ).PinButton ( true );
 
874
        manager.GetPane ( insertChildPanel ).dock_proportion = 1;
 
875
 
 
876
        manager.GetPane ( insertSiblingPanel ).Name ( _T ( "insertSiblingPane" ) ).Show (
 
877
            ( restoreLayout ) ? showInsertSiblingPane : true ).DestroyOnClose ( false ).PinButton ( true );
 
878
        manager.GetPane ( insertSiblingPanel ).dock_proportion = 1;
 
879
 
 
880
        manager.GetPane ( insertEntityPanel ).Name ( _T ( "insertEntityPane" ) ).Show (
 
881
            ( restoreLayout ) ? showInsertEntityPane : true ).DestroyOnClose ( false ).PinButton ( true );
 
882
        manager.GetPane ( insertEntityPanel ).dock_proportion = 1;
 
883
 
 
884
        // add (hidden) message pane
 
885
        htmlReport = new MyHtmlPane (
 
886
            this,
 
887
            ID_VALIDATION_PANE,
 
888
            wxDefaultPosition,
 
889
            wxSize ( -1, 48 ) );
 
890
#ifndef __WXMSW__
 
891
        const int sizeArray[] =
 
892
        {
 
893
                8, 9, 10, 11, 12, 13, 14
 
894
        };
 
895
        htmlReport->SetFonts ( wxEmptyString, wxEmptyString, sizeArray );
 
896
#endif
 
897
        htmlReport->SetBorders ( 0 );
 
898
        manager.AddPane ( htmlReport, wxAuiPaneInfo().Movable().Bottom()
 
899
                          .Hide().Name ( _T ( "messagePane" ) )
 
900
                          .DestroyOnClose ( false ).Layer ( 1 ) );
 
901
        manager.GetPane ( htmlReport ).dock_proportion = 1;
 
902
 
 
903
#ifdef NEWFINDREPLACE
 
904
        findReplacePanel = new FindReplacePanel (
 
905
            this,
 
906
            ID_FIND_REPLACE_PANEL,
 
907
            &findData,
 
908
            true,
 
909
            findRegex );
 
910
 
 
911
        manager.AddPane (
 
912
            ( wxWindow * ) findReplacePanel,
 
913
            wxAuiPaneInfo().Bottom().Hide().Caption ( wxEmptyString ).
 
914
            DestroyOnClose ( false ).Layer ( 2 ) );
 
915
#endif
 
916
 
 
917
        commandPanel = new CommandPanel (
 
918
            this,
 
919
            wxID_ANY,
 
920
            commandString, // tbd
 
921
            commandSync, // tbd
 
922
            commandOutput // tbd
 
923
        );
 
924
        manager.AddPane (
 
925
            ( wxWindow * ) commandPanel,
 
926
            wxAuiPaneInfo().Bottom().Hide().Caption ( _T ( "Command" ) ).DestroyOnClose ( false ).Layer ( 3 ) );
 
927
 
 
928
        if ( !wxFileName::DirExists ( applicationDir ) )
 
929
#ifdef __WXMSW__
 
930
                GetStatusBar()->SetStatusText ( _ ( "Cannot open application directory: see Tools, Options..., General" ) );
 
931
#else
 
932
                GetStatusBar()->SetStatusText ( _ ( "Cannot open application directory: see Edit, Preferences..., General" ) );
 
933
#endif
 
934
 
 
935
        // handle command line and, on Windows, MS Word integration
 
936
        handleCommandLineFlag = ( wxTheApp->argc > 1 ) ? true : false;
 
937
 
 
938
        if ( rememberOpenTabs && !openTabsOnClose.empty() )
 
939
                openRememberedTabs();
 
940
        else
 
941
        {
 
942
                if ( !handleCommandLineFlag )
 
943
                        newDocument ( wxEmptyString );
 
944
        }
 
945
 
 
946
#ifdef __WXMSW__
 
947
        DragAcceptFiles ( true ); // currently Windows only
 
948
#endif
 
949
 
 
950
        XmlDoc *doc = getActiveDocument();
 
951
        insertEntityPanel->update ( doc ); // NULL is ok
 
952
 
 
953
        manager.Update();
 
954
 
 
955
        /*
 
956
          defaultLayout = manager.SavePerspective();
 
957
 
 
958
          // restore layout if req'd
 
959
          if (restoreLayout && !layout.empty())
 
960
          {
 
961
            if (!manager.LoadPerspective(layout, true))
 
962
              manager.LoadPerspective(defaultLayout, true);
 
963
          }
 
964
        */
 
965
}
 
966
 
 
967
MyFrame::~MyFrame()
 
968
{
 
969
        std::vector<wxString>::iterator it;
 
970
        for ( it = tempFileVector.begin(); it != tempFileVector.end(); it++ )
 
971
                wxRemoveFile ( *it );
 
972
 
 
973
        layout = manager.SavePerspective();
 
974
        if ( !config )
 
975
                return;
 
976
        history.Save ( *config );
 
977
        config->Write ( _T ( "insertCloseTag" ), properties.insertCloseTag );
 
978
        config->Write ( _T ( "completion" ), properties.completion );
 
979
        config->Write ( _T ( "number" ), properties.number );
 
980
        config->Write ( _T ( "fold" ), properties.fold );
 
981
        config->Write ( _T ( "currentLine" ), properties.currentLine );
 
982
        config->Write ( _T ( "whitespaceVisible" ), properties.whitespaceVisible );
 
983
        config->Write ( _T ( "wrap" ), properties.wrap );
 
984
        config->Write ( _T ( "indentLines" ), properties.indentLines );
 
985
        config->Write ( _T ( "zoom" ), properties.zoom );
 
986
        config->Write ( _T ( "colorScheme" ), properties.colorScheme );
 
987
        config->Write ( _T ( "protectHiddenElements" ), properties.protectHiddenElements );
 
988
        config->Write ( _T ( "toggleLineBackground" ), properties.toggleLineBackground );
 
989
        config->Write ( _T ( "deleteWholeTag" ), properties.deleteWholeTag );
 
990
        config->Write ( _T ( "validateAsYouType" ), properties.validateAsYouType );
 
991
        config->Write ( _T ( "font" ), properties.font );
 
992
        config->Write ( _T ( "highlightSyntax" ), properties.highlightSyntax );
 
993
        config->Write ( _T ( "applicationDir" ), applicationDir );
 
994
        config->Write ( _T ( "ruleSetPreset" ), ruleSetPreset );
 
995
        config->Write ( _T ( "dictionaryPreset" ), dictionaryPreset );
 
996
        config->Write ( _T ( "filterPreset" ), filterPreset );
 
997
        config->Write ( _T ( "xpathExpression" ), xpathExpression );
 
998
        config->Write ( _T ( "findReplaceFind" ), findData.GetFindString() );
 
999
        config->Write ( _T ( "findReplaceReplace" ), findData.GetReplaceString() );
 
1000
        config->Write ( _T ( "globalReplaceAllDocuments" ), globalReplaceAllDocuments );
 
1001
        config->Write ( _T ( "showFullPathOnFrame" ), showFullPathOnFrame );
 
1002
        config->Write ( _T ( "toolbarVisible" ), toolbarVisible );
 
1003
        config->Write ( _T ( "protectTags" ), protectTags );
 
1004
        config->Write ( _T ( "visibilityState" ), visibilityState );
 
1005
        config->Write ( _T ( "browserCommand" ), browserCommand );
 
1006
        config->Write ( _T ( "showLocationPane" ), manager.GetPane ( locationPanel ).IsShown() );
 
1007
        config->Write ( _T ( "showInsertChildPane" ), manager.GetPane ( insertChildPanel ).IsShown() );
 
1008
        config->Write ( _T ( "showInsertSiblingPane" ), manager.GetPane ( insertSiblingPanel ).IsShown() );
 
1009
        config->Write ( _T ( "showInsertEntityPane" ), manager.GetPane ( insertEntityPanel ).IsShown() );
 
1010
        config->Write ( _T ( "expandInternalEntities" ), expandInternalEntities );
 
1011
        config->Write ( _T ( "findRegex" ), findReplacePanel->getRegex() );
 
1012
        config->Write ( _T ( "findMatchCase" ), ( findData.GetFlags() ) & wxFR_MATCHCASE );
 
1013
        config->Write ( _T ( "commandSync" ), commandPanel->getSync() );
 
1014
        config->Write ( _T ( "commandOutput" ), commandPanel->getOutput() );
 
1015
        config->Write ( _T ( "commandString" ), commandPanel->getCommand() );
 
1016
        config->Write ( _T ( "restoreLayout" ), restoreLayout );
 
1017
        config->Write ( _T ( "lastXslStylesheet" ), lastXslStylesheet );
 
1018
        config->Write ( _T ( "lastRelaxNGSchema" ), lastRelaxNGSchema );
 
1019
        config->Write ( _T ( "exportStylesheet" ), exportStylesheet );
 
1020
        config->Write ( _T ( "exportFolder" ), exportFolder );
 
1021
        config->Write ( _T ( "exportQuiet" ), exportQuiet );
 
1022
        config->Write ( _T ( "exportMp3Album" ), exportMp3Album );
 
1023
 
 
1024
        GetPosition ( &framePosX, &framePosY );
 
1025
        config->Write ( _T ( "framePosX" ), framePosX );
 
1026
        config->Write ( _T ( "framePosY" ), framePosY );
 
1027
        GetSize ( &frameWidth, &frameHeight );
 
1028
        config->Write ( _T ( "frameWidth" ), frameWidth );
 
1029
        config->Write ( _T ( "frameHeight" ), frameHeight );
 
1030
 
 
1031
        config->Write ( _T ( "rememberOpenTabs" ), rememberOpenTabs );
 
1032
        config->Write ( _T ( "openTabsOnClose" ), openTabsOnClose );
 
1033
        config->Write ( _T ( "libxmlNetAccess" ), libxmlNetAccess );
 
1034
 
 
1035
        config->Write ( _T ( "singleInstanceCheck" ), singleInstanceCheck );
 
1036
        config->Write ( _T ( "lang" ), lang );
 
1037
        config->Write ( _T ( "notebookStyle" ), notebookStyle );
 
1038
        config->Write ( _T ( "saveBom" ), saveBom );
 
1039
        config->Write ( _T ( "unlimitedUndo" ), unlimitedUndo );
 
1040
        manager.UnInit();
 
1041
        wxTheClipboard->Flush();
 
1042
}
 
1043
 
 
1044
wxString MyFrame::getLinuxBrowser()
 
1045
{
 
1046
        wxString s;
 
1047
        const int stringArrayLen = 10;
 
1048
        wxString stringArray[stringArrayLen];
 
1049
        stringArray[0] = _T ( "/usr/bin/x-www-browser" );
 
1050
        stringArray[1] = _T ( "/usr/bin/firefox" );
 
1051
        stringArray[2] = _T ( "/usr/bin/mozilla" );
 
1052
        stringArray[3] = _T ( "/usr/bin/opera" );
 
1053
        stringArray[4] = _T ( "/usr/bin/dillo" );
 
1054
        stringArray[5] = _T ( "/opt/gnome/bin/epiphany" );
 
1055
        stringArray[6] = _T ( "/opt/gnome/bin/galeon" );
 
1056
        stringArray[7] = _T ( "/opt/kde/bin/konqueror" );
 
1057
        stringArray[8] = _T ( "/opt/mozilla/bin/firefox" );
 
1058
        stringArray[9] = wxEmptyString; // empty option is safe
 
1059
 
 
1060
        for ( int i = 0; i < stringArrayLen; i++ )
 
1061
        {
 
1062
                s = stringArray[i];
 
1063
                if ( wxFileName::FileExists ( s ) )
 
1064
                        break;
 
1065
        }
 
1066
        return s;
 
1067
}
 
1068
 
 
1069
void MyFrame::showTopBars ( bool b )
 
1070
{
 
1071
        if ( !menuBar )
 
1072
        {
 
1073
                SetToolBar ( NULL );
 
1074
                menuBar = getMenuBar();
 
1075
                SetMenuBar ( menuBar );
 
1076
        }
 
1077
        if ( b )
 
1078
        {
 
1079
                if ( !toolBar )
 
1080
                        toolBar = getToolBar();
 
1081
                SetToolBar ( toolBar );
 
1082
        }
 
1083
        else
 
1084
        {
 
1085
                SetToolBar ( NULL );
 
1086
                delete toolBar;
 
1087
                toolBar = NULL;
 
1088
        }
 
1089
}
 
1090
 
 
1091
void MyFrame::handleCommandLine()
 
1092
{
 
1093
        bool wordFlag, styleFlag;
 
1094
        wordFlag = styleFlag = false;
 
1095
        wxChar c;
 
1096
 
 
1097
        int m_argc = wxTheApp->argc;
 
1098
        wxChar **m_argv = wxTheApp->argv;
 
1099
 
 
1100
        while ( ( --m_argc > 0 && ( *++m_argv ) [0] == L'-' ) != 0 )
 
1101
        {
 
1102
                wxString wideVersion ( ABOUT_VERSION );
 
1103
                std::string version = ( const char * ) wideVersion.mb_str ( wxConvUTF8 );
 
1104
                while ( ( c = *++m_argv[0] ) != 0 )
 
1105
                {
 
1106
                        switch ( c )
 
1107
                        {
 
1108
                                case L'w':
 
1109
                                        wordFlag = true;
 
1110
                                        break;
 
1111
                                case L's':
 
1112
                                        styleFlag = true;
 
1113
                                        break;
 
1114
                                case L'-':
 
1115
                                        if ( *++m_argv[0] == L'v' )
 
1116
                                        {
 
1117
                                                std::cout << version.c_str() << std::endl;
 
1118
                                        }
 
1119
                                        else
 
1120
                                        {
 
1121
                                                std::cout << "Usage: xmlcopyeditor [--version --help] [<file>] [<file2>]" << std::endl << std::endl;
 
1122
                                        }
 
1123
                                        exit ( 0 );
 
1124
                                default:
 
1125
                                        messagePane ( _ ( "Unknown command line switch (expecting 'w', 's', --version or --help)" ),
 
1126
                                                      CONST_STOP );
 
1127
                                        return;
 
1128
                        }
 
1129
                }
 
1130
        }
 
1131
 
 
1132
        if ( ! ( *m_argv ) )
 
1133
        {
 
1134
                messagePane ( _ ( "Command line processing incomplete: no file specified" ),
 
1135
                              CONST_STOP );
 
1136
                return;
 
1137
        }
 
1138
 
 
1139
        wxString fileName;
 
1140
 
 
1141
        // no flags specified or not Windows
 
1142
#ifdef __WXMSW__
 
1143
        if ( !styleFlag && !wordFlag )
 
1144
#endif
 
1145
        {
 
1146
                for ( ; *m_argv; ++m_argv )
 
1147
                {
 
1148
                        fileName = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
 
1149
                        fileName = PathResolver::run ( fileName );
 
1150
                        if ( isOpen ( fileName ) )
 
1151
                                continue;
 
1152
                        else if ( !openFile ( fileName ) )
 
1153
                                break;
 
1154
                }
 
1155
                return;
 
1156
        }
 
1157
 
 
1158
        // options only available on Windows
 
1159
        fileName = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
 
1160
 
 
1161
        // fetch as many parameters as possible
 
1162
        for ( ;; )
 
1163
        {
 
1164
                ++m_argv;
 
1165
                if ( ! ( *m_argv ) )
 
1166
                        break;
 
1167
                ruleSetPreset = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
 
1168
 
 
1169
                ++m_argv;
 
1170
                if ( ! ( *m_argv ) )
 
1171
                        break;
 
1172
                filterPreset = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
 
1173
 
 
1174
                ++m_argv;
 
1175
                if ( ! ( *m_argv ) )
 
1176
                        break;
 
1177
                applicationDir = wxString ( *m_argv, wxConvLocal, wcslen ( *m_argv ) );
 
1178
                updatePaths();
 
1179
 
 
1180
                break;
 
1181
        }
 
1182
        if ( wordFlag )
 
1183
                importMSWord ( fileName );
 
1184
        else
 
1185
                openFile ( fileName );
 
1186
 
 
1187
        if ( styleFlag && !ruleSetPreset.empty() && !filterPreset.empty() )
 
1188
        {
 
1189
                wxCommandEvent e;
 
1190
                OnSpelling ( e );
 
1191
        }
 
1192
}
 
1193
 
 
1194
bool MyFrame::isOpen ( const wxString& fileName )
 
1195
{
 
1196
        return ( openFileSet.find ( fileName ) != openFileSet.end() );
 
1197
}
 
1198
 
 
1199
bool MyFrame::activateTab ( const wxString& fileName )
 
1200
{
 
1201
        int pageCount = mainBook->GetPageCount();
 
1202
        XmlDoc *currentDoc;
 
1203
        for ( int i = 0; i < pageCount; ++i )
 
1204
        {
 
1205
                currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
 
1206
                if ( !currentDoc )
 
1207
                        break;
 
1208
                if ( currentDoc->getFullFileName() == fileName )
 
1209
                {
 
1210
                        mainBook->SetSelection ( i );
 
1211
                        return true;
 
1212
                }
 
1213
        }
 
1214
        return false;
 
1215
}
 
1216
 
 
1217
void MyFrame::OnAbout ( wxCommandEvent& WXUNUSED ( event ) )
 
1218
{
 
1219
        wxString description;
 
1220
        description = ABOUT_DESCRIPTION;
 
1221
        description.Append ( _T("\n\nFramework version: ") );
 
1222
        description.Append ( wxVERSION_STRING );
 
1223
        description.Append ( _T("\n") );
 
1224
 
 
1225
        wxAboutDialogInfo info;
 
1226
        info.SetName ( _ ( "XML Copy Editor" ) );
 
1227
        info.SetWebSite ( _T ( "http://xml-copy-editor.sourceforge.net" ) );
 
1228
        info.SetVersion ( ABOUT_VERSION );
 
1229
        info.SetCopyright ( ABOUT_COPYRIGHT );
 
1230
        info.AddDeveloper ( _ ( "Gerald Schmidt (development) <gnschmidt at users dot sourceforge dot net>" ) );
 
1231
        info.AddDeveloper ( _ ( "Matt Smigielski (testing) <alectrus at users dot sourceforge dot net>" ) );
 
1232
        info.AddDeveloper ( _ ( "Justin Dearing (development) <j-pimp at users dot sourceforge dot net>" ) );
 
1233
        info.AddDeveloper ( _ ( "Kev James (development) <kmjames at users dot sourceforge dot net>" ) );
 
1234
        info.AddDeveloper ( _ ( "Anh Trinh (development) <ant271 at users dot sourceforge dot net>" ) );
 
1235
        info.AddDeveloper ( _ ( "Esther L\x00F6liger (development) <eloeliger at users dot sourceforge dot net>" ) );
 
1236
        info.AddTranslator ( _ ( "Viliam Búr (Slovak) <viliam at bur dot sk>" ) );
 
1237
        info.AddTranslator ( _ ( "David Håsäther (Swedish) <hasather at gmail dot com>" ) );
 
1238
        info.AddTranslator ( _ ( "François Badier (French) <frabad at gmail dot com>" ) );
 
1239
        info.AddTranslator ( _ ( "Thomas Wenzel (German) <thowen at users dot sourceforge.net>" ) );
 
1240
        info.AddTranslator ( _ ( "SHiNE CsyFeK (Chinese Simplified) <csyfek at gmail dot com>" ) );
 
1241
        info.AddTranslator ( _ ( "HSU PICHAN, YANG SHUFUN, CHENG PAULIAN, CHUANG KUO-PING, Marcus Bingenheimer (Chinese Traditional)" ) );
 
1242
        info.AddTranslator ( _ ( "Serhij Dubyk (Ukrainian) <dubyk at library dot lviv dot ua>" ) );
 
1243
        info.AddTranslator ( _ ( "Antonio Angelo (Italian) <aangelo at users dot sourceforge dot net>" ) );
 
1244
        info.AddTranslator ( _ ( "Siarhei Kuchuk (Russian) <Cuchuk dot Sergey at gmail dot com>" ) );
 
1245
        info.AddTranslator ( _ ( "Marcos Pérez González (Spanish) <marcos_pg at yahoo dot com>" ) );
 
1246
        info.AddTranslator ( _ ( "Rob Elemans (Dutch) <relemans at gmail dot com>" ) );
 
1247
        info.AddTranslator ( _ ( "Robert Falc\xf3 Miramontes <rfalco at acett dot org>" ) );
 
1248
    info.SetLicense ( ABOUT_LICENSE );
 
1249
        info.SetDescription ( description );
 
1250
        wxAboutBox ( info );
 
1251
        XmlDoc *doc;
 
1252
        if ( ( doc = getActiveDocument() ) == NULL )
 
1253
                return;
 
1254
        doc->SetFocus();
 
1255
}
 
1256
 
 
1257
void MyFrame::OnCheckWellformedness ( wxCommandEvent& event )
 
1258
{
 
1259
    statusProgress ( wxEmptyString );
 
1260
        XmlDoc *doc;
 
1261
        if ( ( doc = getActiveDocument() ) == NULL )
 
1262
                return;
 
1263
 
 
1264
        std::string utf8Buffer;
 
1265
        getRawText ( doc, utf8Buffer );
 
1266
        if ( utf8Buffer.empty() )
 
1267
                return;
 
1268
 
 
1269
        // handle unusual encodings
 
1270
        if ( !XmlEncodingHandler::setUtf8 ( utf8Buffer ) )
 
1271
        {
 
1272
                encodingMessage();
 
1273
                return;
 
1274
        }
 
1275
 
 
1276
        doc->clearErrorIndicators();
 
1277
        statusProgress ( _ ( "Parse in progress..." ) );
 
1278
 
 
1279
        // check for well-formedness
 
1280
        auto_ptr<WrapExpat> we ( new WrapExpat() );
 
1281
        if ( !we->parse ( utf8Buffer.c_str() ) )
 
1282
        {
 
1283
                std::string error = we->getLastError();
 
1284
                wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
1285
                statusProgress ( wxEmptyString );
 
1286
                messagePane ( werror, CONST_WARNING );
 
1287
                std::pair<int, int> posPair = we->getErrorPosition();
 
1288
                -- ( posPair.first );
 
1289
                int cursorPos =
 
1290
                    doc->PositionFromLine ( posPair.first );
 
1291
                doc->SetSelection ( cursorPos, cursorPos );
 
1292
 
 
1293
                doc->setErrorIndicator ( posPair.first, posPair.second );
 
1294
                return;
 
1295
        }
 
1296
 
 
1297
        statusProgress ( wxEmptyString );
 
1298
        documentOk ( _ ( "well-formed" ) );
 
1299
}
 
1300
 
 
1301
void MyFrame::OnPageClosing ( wxAuiNotebookEvent& event ) //wxNotebookEvent& event)//wxFlatNotebookEvent& event)
 
1302
{
 
1303
        deletePageVetoed = false;
 
1304
 
 
1305
        if ( insertChildPanel && insertSiblingPanel && locationPanel )
 
1306
        {
 
1307
                insertChildPanel->update ( NULL, wxEmptyString );
 
1308
                insertSiblingPanel->update ( NULL, wxEmptyString );
 
1309
                locationPanel->update();
 
1310
                manager.Update();
 
1311
        }
 
1312
 
 
1313
        XmlDoc *doc;
 
1314
        doc = ( XmlDoc * ) mainBook->GetPage ( event.GetSelection() );
 
1315
        if ( !doc )
 
1316
                return;
 
1317
 
 
1318
        statusProgress ( wxEmptyString );
 
1319
        closePane();
 
1320
 
 
1321
        if ( doc->GetModify() ) //CanUndo())
 
1322
        {
 
1323
                int selection;
 
1324
                wxString fileName;
 
1325
                if ( ( selection = mainBook->GetSelection() ) != -1 )
 
1326
                        fileName = doc->getShortFileName();
 
1327
 
 
1328
                int answer = wxMessageBox (
 
1329
                                 _ ( "Do you want to save the changes to " ) + fileName + _T ( "?" ),
 
1330
                                 _ ( "XML Copy Editor" ),
 
1331
                                 wxYES_NO | wxCANCEL | wxICON_QUESTION,
 
1332
                                 this );
 
1333
 
 
1334
                if ( answer == wxCANCEL )
 
1335
                {
 
1336
                        event.Veto();
 
1337
                        deletePageVetoed = true;
 
1338
                        return;
 
1339
                }
 
1340
                else if ( answer == wxYES )
 
1341
                {
 
1342
                        wxCommandEvent event;
 
1343
                        OnSave ( event );
 
1344
                }
 
1345
        }
 
1346
        statusProgress ( wxEmptyString );
 
1347
 
 
1348
        openFileSet.erase ( doc->getFullFileName() );
 
1349
        event.Skip();
 
1350
}
 
1351
 
 
1352
void MyFrame::OnClose ( wxCommandEvent& WXUNUSED ( event ) )
 
1353
{
 
1354
        closeActiveDocument();
 
1355
}
 
1356
 
 
1357
void MyFrame::OnCloseAll ( wxCommandEvent& WXUNUSED ( event ) )
 
1358
{
 
1359
        if ( !mainBook )
 
1360
                return;
 
1361
        openTabsOnClose = wxEmptyString;
 
1362
 
 
1363
        // retain tab order
 
1364
        if ( rememberOpenTabs && !openFileSet.empty() )
 
1365
        {
 
1366
                XmlDoc *doc;
 
1367
                wxString fullPath;
 
1368
                size_t maxTabs = mainBook->GetPageCount();
 
1369
                for ( size_t i = 0; i < maxTabs; ++i )
 
1370
                {
 
1371
                        doc = ( XmlDoc * ) mainBook->GetPage ( i );
 
1372
                        if ( doc )
 
1373
                        {
 
1374
                                fullPath = doc->getFullFileName();
 
1375
                                if ( !fullPath.empty() )
 
1376
                                {
 
1377
                                        openTabsOnClose.Append ( fullPath );
 
1378
                                        openTabsOnClose.Append ( _T ( "|" ) );
 
1379
                                }
 
1380
                        }
 
1381
                }
 
1382
        }
 
1383
 
 
1384
        while ( closeActiveDocument() )
 
1385
                ;
 
1386
}
 
1387
 
 
1388
void MyFrame::OnClosePane ( wxCommandEvent& WXUNUSED ( event ) )
 
1389
{
 
1390
        closePane();
 
1391
        //closeFindReplacePane();
 
1392
        //closeCommandPane();
 
1393
 
 
1394
        XmlDoc *doc = getActiveDocument();
 
1395
        if ( doc )
 
1396
                doc->SetFocus();
 
1397
}
 
1398
 
 
1399
void MyFrame::closePane()
 
1400
{
 
1401
        if ( !htmlReport )
 
1402
                return;
 
1403
        manager.GetPane ( htmlReport ).Hide();
 
1404
        manager.Update();
 
1405
 
 
1406
        XmlDoc *doc;
 
1407
        if ( ( doc = getActiveDocument() ) == NULL )
 
1408
                return;
 
1409
        doc->SetFocus();
 
1410
}
 
1411
 
 
1412
void MyFrame::closeFindReplacePane()
 
1413
{
 
1414
        if ( manager.GetPane ( findReplacePanel ).IsShown() )
 
1415
                manager.GetPane ( findReplacePanel ).Hide();
 
1416
        manager.Update();
 
1417
}
 
1418
 
 
1419
void MyFrame::closeCommandPane()
 
1420
{
 
1421
        if ( manager.GetPane ( commandPanel ).IsShown() )
 
1422
                manager.GetPane ( commandPanel ).Hide();
 
1423
        manager.Update();
 
1424
}
 
1425
 
 
1426
bool MyFrame::panelHasFocus()
 
1427
{
 
1428
        XmlDoc *doc = getActiveDocument();
 
1429
        return ( !doc || ( FindFocus() != ( wxWindow * ) doc ) );
 
1430
}
 
1431
 
 
1432
void MyFrame::OnCut ( wxCommandEvent& event )
 
1433
{
 
1434
        if ( panelHasFocus() )
 
1435
        {
 
1436
                event.Skip();
 
1437
                return;
 
1438
        }
 
1439
 
 
1440
        XmlDoc *doc;
 
1441
        if ( ( doc = getActiveDocument() ) == NULL )
 
1442
                return;
 
1443
 
 
1444
        if ( protectTags )
 
1445
                doc->adjustSelection();
 
1446
 
 
1447
        doc->Cut();
 
1448
        doc->setValidationRequired ( true );
 
1449
}
 
1450
 
 
1451
void MyFrame::OnCopy ( wxCommandEvent& event )
 
1452
{
 
1453
        if ( panelHasFocus() )
 
1454
        {
 
1455
                event.Skip();
 
1456
                return;
 
1457
        }
 
1458
 
 
1459
        XmlDoc *doc;
 
1460
        if ( ( doc = getActiveDocument() ) == NULL )
 
1461
                return;
 
1462
        doc->Copy();
 
1463
}
 
1464
 
 
1465
void MyFrame::OnPaste ( wxCommandEvent& event )
 
1466
{
 
1467
        if ( panelHasFocus() )
 
1468
        {
 
1469
                event.Skip();
 
1470
                return;
 
1471
        }
 
1472
        XmlDoc *doc;
 
1473
        if ( ( doc = getActiveDocument() ) == NULL )
 
1474
                return;
 
1475
 
 
1476
        // this has to be handled here to override Scintilla's default Ctrl+V support
 
1477
        if ( protectTags )
 
1478
        {
 
1479
                if ( !wxTheClipboard->Open() || !wxTheClipboard->IsSupported ( wxDF_TEXT ) )
 
1480
                        return;
 
1481
                wxTextDataObject data;
 
1482
                wxTheClipboard->GetData ( data );
 
1483
                wxString buffer = data.GetText();
 
1484
                wxTheClipboard->Close();
 
1485
                xmliseWideTextNode ( buffer );
 
1486
                doc->adjustCursor();
 
1487
                doc->AddText ( buffer );
 
1488
        }
 
1489
        else
 
1490
                doc->Paste();
 
1491
}
 
1492
 
 
1493
void MyFrame::OnIdle ( wxIdleEvent& event )
 
1494
{
 
1495
        wxStatusBar *status = GetStatusBar();
 
1496
        if ( !status )
 
1497
                return;
 
1498
 
 
1499
    /*
 
1500
    // IPC handling: take one file from fileQueue at a time
 
1501
    if ( !fileQueue.empty() )
 
1502
    {
 
1503
      openFile ( * ( fileQueue.begin() ) );
 
1504
      fileQueue.erase( fileQueue.begin() );  
 
1505
    }
 
1506
    */
 
1507
 
 
1508
        // update attributes hidden field even if no document loaded
 
1509
        wxString currentHiddenStatus = status->GetStatusText ( STATUS_HIDDEN );
 
1510
        if ( visibilityState == HIDE_ATTRIBUTES )
 
1511
        {
 
1512
                if ( currentHiddenStatus != _ ( "Attributes hidden" ) )
 
1513
                        status->SetStatusText (
 
1514
                            _ ( "Attributes hidden" ),
 
1515
                            STATUS_HIDDEN );
 
1516
        }
 
1517
        else if ( visibilityState == HIDE_TAGS )
 
1518
        {
 
1519
                if ( currentHiddenStatus != _ ( "Tags hidden" ) )
 
1520
                        status->SetStatusText (
 
1521
                            _ ( "Tags hidden" ),
 
1522
                            STATUS_HIDDEN );
 
1523
        }
 
1524
        else
 
1525
        {
 
1526
                if ( !currentHiddenStatus.empty() )
 
1527
                        status->SetStatusText ( wxEmptyString, STATUS_HIDDEN );
 
1528
        }
 
1529
 
 
1530
        // update protected field even if no document loaded
 
1531
        wxString currentProtectedStatus = status->GetStatusText ( STATUS_PROTECTED );
 
1532
        if ( protectTags )
 
1533
        {
 
1534
                if ( currentProtectedStatus != _ ( "Tags locked" ) )
 
1535
                        status->SetStatusText (
 
1536
                            _ ( "Tags locked" ),
 
1537
                            STATUS_PROTECTED );
 
1538
        }
 
1539
        else
 
1540
        {
 
1541
                if ( !currentProtectedStatus.empty() )
 
1542
                        status->SetStatusText ( wxEmptyString, STATUS_PROTECTED );
 
1543
        }
 
1544
 
 
1545
        // check if document loaded
 
1546
        wxString frameTitle = GetTitle();
 
1547
 
 
1548
        XmlDoc *doc;
 
1549
        if ( ( doc = getActiveDocument() ) == NULL )
 
1550
        {
 
1551
                if ( lastDoc != NULL )
 
1552
                {
 
1553
                        lastDoc = NULL;
 
1554
                        status->SetStatusText ( wxEmptyString, STATUS_MODIFIED );
 
1555
                        status->SetStatusText ( wxEmptyString, STATUS_POSITION );
 
1556
                        locationPanel->update ( NULL, wxEmptyString );
 
1557
                        insertChildPanel->update ( NULL, wxEmptyString );
 
1558
                        insertSiblingPanel->update ( NULL, wxEmptyString );
 
1559
                        insertEntityPanel->update ( NULL, wxEmptyString );
 
1560
                        wxString minimal = _ ( "XML Copy Editor" );
 
1561
                        if ( frameTitle != minimal )
 
1562
                                SetTitle ( minimal );
 
1563
 
 
1564
                        closeFindReplacePane();
 
1565
 
 
1566
                        event.Skip();
 
1567
                        manager.Update();
 
1568
                }
 
1569
                return;
 
1570
        }
 
1571
 
 
1572
        if ( restoreFocusToNotebook )
 
1573
        {
 
1574
                doc->SetFocus();
 
1575
                restoreFocusToNotebook = false;
 
1576
        }
 
1577
 
 
1578
        wxString docTitle;
 
1579
        if ( doc->getFullFileName().empty() || !showFullPathOnFrame )
 
1580
                docTitle = doc->getShortFileName();
 
1581
        else
 
1582
                docTitle = doc->getFullFileName();
 
1583
 
 
1584
        docTitle += _T ( " - " );
 
1585
        docTitle += _ ( "XML Copy Editor" );
 
1586
 
 
1587
        if ( frameTitle != docTitle )
 
1588
                SetTitle ( docTitle );
 
1589
 
 
1590
    // update modified field
 
1591
        if ( !mainBook )
 
1592
                return;
 
1593
        int index = mainBook->GetSelection();
 
1594
 
 
1595
        wxString currentModifiedStatus = status->GetStatusText ( STATUS_MODIFIED );
 
1596
        wxString currentTabLabel = mainBook->GetPageText ( index );
 
1597
        if ( doc->GetModify() )
 
1598
        {
 
1599
                if ( currentModifiedStatus != _ ( "Modified" ) )
 
1600
                {
 
1601
                        status->SetStatusText ( _ ( "Modified" ), STATUS_MODIFIED );
 
1602
 
 
1603
                        if ( ! ( currentTabLabel.Mid ( 0, 1 ) == _T ( "*" ) ) )
 
1604
                        {
 
1605
                                currentTabLabel.Prepend ( _T ( "*" ) );
 
1606
                                mainBook->SetPageText ( index, currentTabLabel );
 
1607
                        }
 
1608
                }
 
1609
        }
 
1610
        else
 
1611
        {
 
1612
                if ( !currentModifiedStatus.empty() )
 
1613
                {
 
1614
                        status->SetStatusText ( _T ( "" ), STATUS_MODIFIED );
 
1615
 
 
1616
                        if ( currentTabLabel.Mid ( 0, 1 ) == _T ( "*" ) )
 
1617
                        {
 
1618
                                currentTabLabel.Remove ( 0, 1 );
 
1619
                                mainBook->SetPageText ( index, currentTabLabel );
 
1620
                        }
 
1621
                }
 
1622
        }
 
1623
 
 
1624
        // update coordinates field
 
1625
        std::pair<int, int> myControlCoordinates;
 
1626
        int current = doc->GetCurrentPos();
 
1627
        myControlCoordinates.first = doc->LineFromPosition ( current ) + 1;
 
1628
        myControlCoordinates.second = doc->GetColumn ( current ) + 1;
 
1629
 
 
1630
        if ( myControlCoordinates != controlCoordinates )
 
1631
        {
 
1632
                wxString coordinates;
 
1633
                coordinates.Printf (
 
1634
                    _ ( "Ln %i Col %i" ),
 
1635
                    myControlCoordinates.first,
 
1636
                    myControlCoordinates.second );
 
1637
                GetStatusBar()->SetStatusText ( coordinates, STATUS_POSITION );
 
1638
                controlCoordinates = myControlCoordinates;
 
1639
        }
 
1640
 
 
1641
        // update parent element field
 
1642
        wxString parent, grandparent;
 
1643
        if ( current == lastPos && doc == lastDoc )
 
1644
                return;
 
1645
 
 
1646
        lastPos = current;
 
1647
        lastDoc = doc;
 
1648
 
 
1649
 
 
1650
        // don't try to find parent if pane is not shown
 
1651
        if ( !manager.GetPane ( insertChildPanel ).IsShown() && !properties.validateAsYouType )
 
1652
                return;
 
1653
 
 
1654
        int parentCloseAngleBracket = -1;
 
1655
        if ( !doc->canInsertAt ( current ) )
 
1656
                parent = grandparent = wxEmptyString;
 
1657
        else
 
1658
        {
 
1659
                parentCloseAngleBracket = doc->getParentCloseAngleBracket ( current );
 
1660
                parent = doc->getLastElementName ( parentCloseAngleBracket );
 
1661
        }
 
1662
 
 
1663
        if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() )
 
1664
        {
 
1665
                // tbd: limit to parent element
 
1666
                doc->backgroundValidate();
 
1667
        }
 
1668
 
 
1669
 
 
1670
        if ( parent == lastParent )
 
1671
                return;
 
1672
        lastParent = parent;
 
1673
 
 
1674
        bool mustUpdate = false;
 
1675
        if ( locationPanel && insertChildPanel && insertEntityPanel )
 
1676
        {
 
1677
                locationPanel->update ( doc, parent );
 
1678
                insertChildPanel->update ( doc, parent );
 
1679
                insertEntityPanel->update ( doc );
 
1680
                mustUpdate = true;
 
1681
        }
 
1682
 
 
1683
        if ( parent.empty() )
 
1684
        {
 
1685
                if ( insertSiblingPanel )
 
1686
                        insertSiblingPanel->update ( doc, wxEmptyString );
 
1687
                if ( mustUpdate )
 
1688
                        manager.Update();
 
1689
                return;
 
1690
        }
 
1691
 
 
1692
        if ( !manager.GetPane ( insertSiblingPanel ).IsShown() )
 
1693
        {
 
1694
                if ( mustUpdate )
 
1695
                        manager.Update();
 
1696
                return;
 
1697
        }
 
1698
 
 
1699
        // try to fetch grandparent if necessary/possible
 
1700
        if ( !parent.empty() && parentCloseAngleBracket != -1 )
 
1701
        {
 
1702
                int grandParentCloseAngleBracket;
 
1703
                grandParentCloseAngleBracket =
 
1704
                    doc->getParentCloseAngleBracket (
 
1705
                        doc->getTagStartPos ( parentCloseAngleBracket ) );
 
1706
                grandparent = doc->getLastElementName ( grandParentCloseAngleBracket );
 
1707
 
 
1708
                if ( insertSiblingPanel )
 
1709
                        insertSiblingPanel->update ( doc, parent, grandparent );
 
1710
                if ( grandparent != lastGrandparent )
 
1711
                {
 
1712
                        mustUpdate = true;
 
1713
                        lastGrandparent = grandparent;
 
1714
                }
 
1715
 
 
1716
        }
 
1717
        if ( mustUpdate )
 
1718
                manager.Update();
 
1719
}
 
1720
 
 
1721
void MyFrame::OnInsertChild ( wxCommandEvent& event )
 
1722
{
 
1723
        if ( !insertChildPanel )
 
1724
                return;
 
1725
 
 
1726
        wxAuiPaneInfo info = manager.GetPane ( insertChildPanel );
 
1727
        if ( !info.IsOk() )
 
1728
        {
 
1729
                return;
 
1730
        }
 
1731
        if ( !info.IsShown() )
 
1732
        {
 
1733
                manager.GetPane ( insertChildPanel ).Show ( true );
 
1734
                manager.Update();
 
1735
        }
 
1736
        insertChildPanel->setEditFocus();
 
1737
}
 
1738
 
 
1739
void MyFrame::OnInsertSibling ( wxCommandEvent& event )
 
1740
{
 
1741
        if ( !insertSiblingPanel )
 
1742
                return;
 
1743
 
 
1744
        wxAuiPaneInfo info = manager.GetPane ( insertSiblingPanel );
 
1745
        if ( !info.IsOk() )
 
1746
        {
 
1747
                return;
 
1748
        }
 
1749
 
 
1750
        if ( !info.IsShown() )
 
1751
        {
 
1752
                manager.GetPane ( insertSiblingPanel ).Show ( true );
 
1753
                manager.Update();
 
1754
        }
 
1755
        insertSiblingPanel->setEditFocus();
 
1756
}
 
1757
 
 
1758
void MyFrame::OnInsertEntity ( wxCommandEvent& event )
 
1759
{
 
1760
        if ( !insertEntityPanel )
 
1761
                return;
 
1762
 
 
1763
        wxAuiPaneInfo info = manager.GetPane ( insertEntityPanel );
 
1764
        if ( !info.IsOk() )
 
1765
        {
 
1766
                return;
 
1767
        }
 
1768
 
 
1769
        if ( !info.IsShown() )
 
1770
        {
 
1771
                manager.GetPane ( insertEntityPanel ).Show ( true );
 
1772
                manager.Update();
 
1773
        }
 
1774
        insertEntityPanel->setEditFocus();
 
1775
}
 
1776
 
 
1777
void MyFrame::OnInsertSymbol ( wxCommandEvent& event )
 
1778
{
 
1779
        XmlDoc *doc;
 
1780
        if ( ( doc = getActiveDocument() ) == NULL )
 
1781
                return;
 
1782
        wxSymbolPickerDialog dlg ( _T ( "*" ), wxEmptyString, properties.font, this );
 
1783
 
 
1784
        if ( dlg.ShowModal() == wxID_OK )
 
1785
        {
 
1786
                if ( dlg.HasSelection() )
 
1787
                {
 
1788
                        doc->AddText ( dlg.GetSymbol() );
 
1789
                }
 
1790
        }
 
1791
}
 
1792
 
 
1793
void MyFrame::OnInsertTwin ( wxCommandEvent& event )
 
1794
{
 
1795
        XmlDoc *doc;
 
1796
        if ( ( doc = getActiveDocument() ) == NULL )
 
1797
                return;
 
1798
 
 
1799
        wxString parent = doc->getParent();
 
1800
        if ( !doc->insertSibling ( parent, parent ) )
 
1801
        {
 
1802
                wxString msg;
 
1803
                msg.Printf ( _T ( "Cannot insert twin '%s'" ), parent.c_str() );
 
1804
                messagePane ( msg, CONST_STOP );
 
1805
        }
 
1806
        doc->setValidationRequired ( true );
 
1807
        doc->SetFocus();
 
1808
}
 
1809
 
 
1810
void MyFrame::OnPasteNewDocument ( wxCommandEvent& event )
 
1811
{
 
1812
        if ( !wxTheClipboard->Open() )
 
1813
        {
 
1814
                messagePane ( _ ( "Cannot open clipboard" ), CONST_STOP );
 
1815
                return;
 
1816
        }
 
1817
        if ( !wxTheClipboard->IsSupported ( wxDF_TEXT ) )
 
1818
        {
 
1819
                messagePane ( _ ( "Cannot paste as new document: no text on clipboard" ), CONST_STOP );
 
1820
                return;
 
1821
        }
 
1822
        wxTextDataObject data;
 
1823
        wxTheClipboard->GetData ( data );
 
1824
 
 
1825
        wxString buffer = data.GetText();
 
1826
        xmliseWideTextNode ( buffer );
 
1827
 
 
1828
        buffer.Prepend ( _T ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>" ) );
 
1829
        buffer.Append ( _T ( "</root>\n" ) );
 
1830
 
 
1831
        newDocument ( buffer );
 
1832
        wxTheClipboard->Close();
 
1833
}
 
1834
 
 
1835
void MyFrame::OnDialogFind ( wxFindDialogEvent& event )
 
1836
{
 
1837
        findAgain ( event.GetFindString(), event.GetFlags() );
 
1838
}
 
1839
 
 
1840
void MyFrame::OnDialogReplace ( wxFindDialogEvent& event )
 
1841
{
 
1842
        statusProgress ( wxEmptyString );
 
1843
        XmlDoc *doc;
 
1844
        if ( ( doc = getActiveDocument() ) == NULL )
 
1845
                return;
 
1846
 
 
1847
        int regexWidth = 0;
 
1848
        if ( findReplacePanel->getRegex() )
 
1849
        {
 
1850
                regexWidth = doc->ReplaceTargetRE ( event.GetReplaceString() );
 
1851
        }
 
1852
        else
 
1853
        {
 
1854
                doc->ReplaceTarget ( event.GetReplaceString() );
 
1855
        }
 
1856
        OnDialogFind ( event );
 
1857
}
 
1858
 
 
1859
void MyFrame::OnDialogReplaceAll ( wxFindDialogEvent& event )
 
1860
{
 
1861
        statusProgress ( wxEmptyString );
 
1862
        XmlDoc *doc;
 
1863
        if ( ( doc = getActiveDocument() ) == NULL )
 
1864
                return;
 
1865
 
 
1866
        int flags = 0;
 
1867
        if ( event.GetFlags() & wxFR_WHOLEWORD )
 
1868
                flags |= wxSTC_FIND_WHOLEWORD;
 
1869
        if ( event.GetFlags() & wxFR_MATCHCASE )
 
1870
                flags |= wxSTC_FIND_MATCHCASE;
 
1871
        if ( findReplacePanel->getRegex() )
 
1872
                flags |= wxSTC_FIND_REGEXP;
 
1873
 
 
1874
        doc->SetTargetStart ( 0 );
 
1875
        doc->SetTargetEnd ( doc->GetLength() );
 
1876
        doc->SetSearchFlags ( flags );
 
1877
 
 
1878
        int newLocation, replacementCount, regexWidth;
 
1879
        newLocation = replacementCount = regexWidth = 0;
 
1880
 
 
1881
        while ( ( newLocation = doc->SearchInTarget ( event.GetFindString() ) ) != -1 )
 
1882
        {
 
1883
                if ( findReplacePanel->getRegex() )
 
1884
                {
 
1885
                        regexWidth = doc->ReplaceTargetRE ( event.GetReplaceString() );
 
1886
                        doc->SetTargetStart ( newLocation + regexWidth );
 
1887
                }
 
1888
                else
 
1889
                {
 
1890
                        doc->ReplaceTarget ( event.GetReplaceString() );
 
1891
                        doc->SetTargetStart ( newLocation + event.GetReplaceString().size() );
 
1892
                }
 
1893
                doc->SetTargetEnd ( doc->GetLength() );
 
1894
                ++replacementCount;
 
1895
        }
 
1896
        wxString msg;
 
1897
 
 
1898
        msg.Printf (
 
1899
            ngettext ( L"%i replacement made", L"%i replacements made", replacementCount ),
 
1900
            replacementCount );
 
1901
        statusProgress ( msg );
 
1902
}
 
1903
 
 
1904
void MyFrame::OnPrintSetup ( wxCommandEvent &WXUNUSED ( event ) )
 
1905
{
 
1906
        if ( !htmlPrinting.get() )
 
1907
                return;
 
1908
        htmlPrinting->PageSetup();
 
1909
}
 
1910
 
 
1911
void MyFrame::OnPrintPreview ( wxCommandEvent &WXUNUSED ( event ) )
 
1912
{
 
1913
        XmlDoc *doc;
 
1914
        if ( !htmlPrinting.get() || ( doc = getActiveDocument() ) == NULL )
 
1915
                return;
 
1916
        wxString shortFileName, header;
 
1917
        shortFileName = doc->getShortFileName();
 
1918
        if ( !shortFileName.empty() )
 
1919
                header = shortFileName + _T ( " " );
 
1920
        header += _T ( "(@PAGENUM@/@PAGESCNT@)<hr>" );
 
1921
 
 
1922
        htmlPrinting->SetHeader (
 
1923
            header,
 
1924
            wxPAGE_ALL );
 
1925
        statusProgress ( _ ( "Preparing Print Preview..." ) );
 
1926
        wxString htmlBuffer = getHtmlBuffer();
 
1927
        statusProgress ( wxEmptyString );
 
1928
        if ( ! ( htmlPrinting->PreviewText ( htmlBuffer ) ) )
 
1929
                ;
 
1930
}
 
1931
 
 
1932
void MyFrame::OnPrint ( wxCommandEvent &WXUNUSED ( event ) )
 
1933
{
 
1934
        XmlDoc *doc;
 
1935
        if ( !htmlPrinting.get() || ( doc = getActiveDocument() ) == NULL )
 
1936
                return;
 
1937
        wxString shortFileName, header;
 
1938
        shortFileName = doc->getShortFileName();
 
1939
        if ( !shortFileName.empty() )
 
1940
                header = shortFileName + _T ( " " );
 
1941
        header += _T ( "(@PAGENUM@/@PAGESCNT@)<hr>" );
 
1942
 
 
1943
        htmlPrinting->SetHeader (
 
1944
            header,
 
1945
            wxPAGE_ALL );
 
1946
        statusProgress ( _ ( "Preparing to print..." ) );
 
1947
        wxString htmlBuffer = getHtmlBuffer();
 
1948
        statusProgress ( wxEmptyString );
 
1949
        if ( ! ( htmlPrinting->PrintText ( htmlBuffer ) ) )
 
1950
                ;
 
1951
}
 
1952
 
 
1953
wxString MyFrame::getHtmlBuffer()
 
1954
{
 
1955
        XmlDoc *doc;
 
1956
        if ( ( doc = getActiveDocument() ) == NULL )
 
1957
                return _T ( "<html><head></head><body></body></html>" );
 
1958
        wxString buffer, htmlBuffer;
 
1959
        buffer = doc->GetText();
 
1960
        size_t size = buffer.size();
 
1961
        htmlBuffer.reserve ( size * 2 );
 
1962
 
 
1963
        htmlBuffer = _T ( "<html><body><p>" );
 
1964
        bool startOfLine = true;
 
1965
        for ( size_t i = 0; i < size; ++i )
 
1966
        {
 
1967
                wchar_t c = buffer[i];
 
1968
                switch ( c )
 
1969
                {
 
1970
                        case L' ':
 
1971
                                htmlBuffer += ( startOfLine ) ? _T ( "&nbsp;" ) : _T ( " " );
 
1972
                                break;
 
1973
                        case L'\t':
 
1974
                                htmlBuffer += _T ( "&nbsp;&nbsp;" );
 
1975
                                break;
 
1976
                        case L'<':
 
1977
                                htmlBuffer += _T ( "&lt;" );
 
1978
                                startOfLine = false;
 
1979
                                break;
 
1980
                        case L'>':
 
1981
                                htmlBuffer += _T ( "&gt;" );
 
1982
                                startOfLine = false;
 
1983
                                break;
 
1984
                        case L'\n':
 
1985
                                htmlBuffer += _T ( "<br>" );
 
1986
                                startOfLine = true;
 
1987
                                break;
 
1988
                        case L'&':
 
1989
                                htmlBuffer + _T ( "&amp;" );
 
1990
                                startOfLine = false;
 
1991
                                break;
 
1992
                        default:
 
1993
                                htmlBuffer += c;
 
1994
                                startOfLine = false;
 
1995
                                break;
 
1996
                }
 
1997
        }
 
1998
        htmlBuffer += _T ( "</p></body></html>" );
 
1999
        return htmlBuffer;
 
2000
}
 
2001
 
 
2002
void MyFrame::OnFind ( wxCommandEvent& WXUNUSED ( event ) )
 
2003
{
 
2004
#ifdef NEWFINDREPLACE
 
2005
        manager.GetPane ( findReplacePanel ).Caption ( _ ( "Find" ) );
 
2006
        bool visible = manager.GetPane ( findReplacePanel ).IsShown();
 
2007
        if ( !visible )
 
2008
        {
 
2009
                manager.GetPane ( findReplacePanel ).Show();
 
2010
        }
 
2011
        manager.Update();
 
2012
        findReplacePanel->refresh();
 
2013
        findReplacePanel->setReplaceVisible ( false );
 
2014
        findReplacePanel->focusOnFind();
 
2015
        return;
 
2016
#endif
 
2017
 
 
2018
        if ( findDialog.get() )
 
2019
        {
 
2020
                findDialog = std::auto_ptr<wxFindReplaceDialog> ( 0 );
 
2021
        }
 
2022
        findDialog = ( std::auto_ptr<wxFindReplaceDialog> ( new wxFindReplaceDialog (
 
2023
                           this,
 
2024
                           &findData,
 
2025
                           _ ( "Find" ) ) ) );
 
2026
        findDialog->Show();
 
2027
}
 
2028
 
 
2029
void MyFrame::OnImportMSWord ( wxCommandEvent& event )
 
2030
{
 
2031
#ifndef __WXMSW__
 
2032
        messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
 
2033
        return;
 
2034
#endif
 
2035
 
 
2036
        std::auto_ptr<wxFileDialog> fd ( new wxFileDialog (
 
2037
                                             this,
 
2038
                                             _ ( "Import Microsoft Word Document" ),
 
2039
                                             _T ( "" ),
 
2040
                                             _T ( "" ),
 
2041
                                             _T ( "Microsoft Word (*.doc)|*.doc" ),
 
2042
                                             wxOPEN | wxFILE_MUST_EXIST | wxCHANGE_DIR
 
2043
                                         ) );
 
2044
        if ( fd->ShowModal() == wxID_CANCEL )
 
2045
                return;
 
2046
 
 
2047
        wxString path = fd->GetPath();
 
2048
 
 
2049
        if ( path == _T ( "" ) )
 
2050
                return;
 
2051
 
 
2052
        importMSWord ( path );
 
2053
}
 
2054
 
 
2055
void MyFrame::OnExport ( wxCommandEvent& event )
 
2056
{
 
2057
        statusProgress ( wxEmptyString );
 
2058
        closePane();
 
2059
 
 
2060
        XmlDoc *doc;
 
2061
        if ( ( doc = getActiveDocument() ) == NULL )
 
2062
                return;
 
2063
 
 
2064
    wxString testDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "daisy" );
 
2065
    bool downloadLink = !wxDirExists ( testDir );
 
2066
 
 
2067
    std::auto_ptr<ExportDialog> ed ( new ExportDialog (
 
2068
        this,
 
2069
        exportStylesheet,
 
2070
        exportFolder,
 
2071
        exportQuiet,
 
2072
        true, //suppressOptional
 
2073
        true, //html
 
2074
        true, //epub
 
2075
        true, //rtf
 
2076
        true, //doc
 
2077
        true, //fullDaisy
 
2078
        exportMp3Album,
 
2079
        downloadLink ) );
 
2080
    int ret = ed->ShowModal();
 
2081
    
 
2082
    if ( ret != wxID_OK )
 
2083
        return;
 
2084
 
 
2085
    exportStylesheet = ed->getUrlString();
 
2086
    exportFolder = ed->getFolderString();
 
2087
    exportQuiet = ed->getQuiet();
 
2088
    exportMp3Album = ed->getMp3Album();
 
2089
 
 
2090
        std::string rawBufferUtf8;
 
2091
        getRawText ( doc, rawBufferUtf8 );
 
2092
        if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
 
2093
        {
 
2094
                encodingMessage();
 
2095
                return;
 
2096
        }
 
2097
 
 
2098
        WrapTempFileName tempFileName ( doc->getFullFileName() );
 
2099
 
 
2100
        ofstream rawBufferStream ( tempFileName.name().c_str() );
 
2101
        if ( !rawBufferStream )
 
2102
                return;
 
2103
        rawBufferStream << rawBufferUtf8;
 
2104
        rawBufferStream.close();
 
2105
 
 
2106
    wxString tempFile= tempFileName.wideName();
 
2107
    
 
2108
    WrapDaisy wd ( this, daisyDir, doc->getFullFileName() );
 
2109
    if ( !wd.run ( tempFile, exportStylesheet, exportFolder, exportQuiet, exportMp3Album, true, true, true, true ) )
 
2110
    {
 
2111
        messagePane ( _ ("[b]DAISY export stopped[/b]: ") + wd.getLastError(), CONST_STOP );
 
2112
        return;
 
2113
    }
 
2114
        messagePane ( _ ( "DAISY export completed. Output files are stored in: [b]" ) + exportFolder + _T ( "[/b]." ), CONST_INFO );
 
2115
}
 
2116
 
 
2117
void MyFrame::importMSWord ( const wxString& path )
 
2118
{
 
2119
#ifndef __WXMSW__
 
2120
        messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
 
2121
        return;
 
2122
#endif
 
2123
 
 
2124
        WrapTempFileName tempFileName ( _T ( "" ) ), swapFileName ( _T ( "" ) );
 
2125
        wxString completeSwapFileName = swapFileName.wideName() + _T ( ".doc" );
 
2126
        if ( !wxCopyFile ( path, completeSwapFileName, true ) )
 
2127
        {
 
2128
                wxString message;
 
2129
                message.Printf ( _ ( "Cannot open [b]%s[/b] for import" ), path.c_str() );
 
2130
                messagePane ( message, CONST_STOP );
 
2131
                return;
 
2132
        }
 
2133
 
 
2134
        wxString cmd = binDir +
 
2135
                       _T ( "doc2xml.exe \"" ) +
 
2136
                       completeSwapFileName + _T ( "\" \"" ) +
 
2137
                       tempFileName.wideName() + _T ( "\"" );
 
2138
 
 
2139
        statusProgress ( _ ( "Import in progress..." ) );
 
2140
        int result = wxExecute ( cmd, wxEXEC_SYNC );
 
2141
 
 
2142
        wxRemoveFile ( completeSwapFileName ); // necessary because .doc extension added
 
2143
 
 
2144
        statusProgress ( wxEmptyString );
 
2145
        wxString message;
 
2146
        wxString versionMessage (
 
2147
            _ ( "(lossless conversion requires version 2003 or later)" ) );
 
2148
 
 
2149
        switch ( result )
 
2150
        {
 
2151
                case 0:
 
2152
                        break;
 
2153
                case 1:
 
2154
                        messagePane ( _ ( "Cannot start Microsoft Word" ), CONST_STOP );
 
2155
                        return;
 
2156
                case 2:
 
2157
                        messagePane (
 
2158
                            _ ( "A more recent version of Microsoft Word is required" ), CONST_STOP );
 
2159
                        return;
 
2160
                case 3:
 
2161
                        message.Printf ( _T ( "Microsoft Word cannot open [b]%s[/b]" ), path.c_str() );
 
2162
                        messagePane ( message + path, CONST_STOP );
 
2163
                        return;
 
2164
                case 4:
 
2165
                        message.Printf ( _ ( "Microsoft Word cannot save [b]%s[/b] as XML" ), path.c_str() );
 
2166
                        messagePane ( message, CONST_STOP );
 
2167
                        return;
 
2168
                case 5:
 
2169
                        messagePane (
 
2170
                            _ ( "Microsoft Word cannot save this document as WordprocessingML " ) +
 
2171
                            versionMessage,
 
2172
                            CONST_INFO );
 
2173
                        break;
 
2174
                default:
 
2175
                        break;
 
2176
        }
 
2177
 
 
2178
        statusProgress ( _ ( "Opening imported file..." ) );
 
2179
        std::string buffer;
 
2180
 
 
2181
        wxString displayBuffer;
 
2182
 
 
2183
        if ( result != 5 ) // Word 2003 or later
 
2184
        {
 
2185
                std::auto_ptr<WrapLibxml> prettyPrinter ( new WrapLibxml ( libxmlNetAccess ) );
 
2186
                prettyPrinter->parse ( tempFileName.name(), true );
 
2187
                buffer = prettyPrinter->getOutput();
 
2188
                displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
 
2189
        }
 
2190
        else // earlier versions
 
2191
        {
 
2192
                if ( !ReadFile::run ( tempFileName.name(), buffer ) )
 
2193
                {
 
2194
                        statusProgress ( wxEmptyString );
 
2195
                        messagePane ( _ ( "Cannot open imported file" ), CONST_STOP );
 
2196
                        return;
 
2197
                }
 
2198
                displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
 
2199
                displayBuffer.Remove ( 0, 1 ); // remove byte order mark
 
2200
                xmliseWideTextNode ( displayBuffer );
 
2201
 
 
2202
                displayBuffer.Prepend (
 
2203
                    _T ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>" ) );
 
2204
                displayBuffer.Append ( _T ( "</root>\n" ) );
 
2205
        }
 
2206
 
 
2207
        newDocument ( displayBuffer, tempFileName.wideName() );
 
2208
        statusProgress ( wxEmptyString );
 
2209
}
 
2210
 
 
2211
void MyFrame::OnExportMSWord ( wxCommandEvent& event )
 
2212
{
 
2213
#ifndef __WXMSW__
 
2214
        messagePane ( _ ( "This functionality requires Microsoft Windows" ) );
 
2215
        return;
 
2216
#endif
 
2217
        statusProgress ( wxEmptyString );
 
2218
 
 
2219
        // fetch document contents
 
2220
        XmlDoc *doc;
 
2221
        if ( ( doc = getActiveDocument() ) == NULL )
 
2222
                return;
 
2223
 
 
2224
        WrapTempFileName wtfn ( _T ( "" ) );
 
2225
        wxString sourceFileName = doc->getFullFileName();
 
2226
 
 
2227
        if ( sourceFileName.empty() )
 
2228
        {
 
2229
                sourceFileName = wtfn.wideName();
 
2230
                std::fstream ofs ( wtfn.name().c_str() );
 
2231
                if ( !ofs )
 
2232
                        return;
 
2233
 
 
2234
                std::string utf8Buffer;
 
2235
                getRawText ( doc, utf8Buffer );
 
2236
                ofs << utf8Buffer;
 
2237
                ofs.close();
 
2238
        }
 
2239
        else if ( doc->GetModify() ) //CanUndo())
 
2240
        {
 
2241
                modifiedMessage();
 
2242
                return;
 
2243
        }
 
2244
 
 
2245
        std::auto_ptr<wxFileDialog> fd ( new wxFileDialog (
 
2246
                                             this,
 
2247
                                             _ ( "Export Microsoft Word Document" ),
 
2248
                                             _T ( "" ),
 
2249
                                             _T ( "" ),
 
2250
                                             _T ( "Microsoft Word (*.doc)|*.doc" ),
 
2251
                                             wxSAVE | wxOVERWRITE_PROMPT ) );
 
2252
        fd->ShowModal();
 
2253
 
 
2254
        wxString path = fd->GetPath();
 
2255
 
 
2256
        if ( path == _T ( "" ) )
 
2257
                return;
 
2258
 
 
2259
        wxString cmd = binDir +
 
2260
                       _T ( "xml2doc.exe -v \"" ) +
 
2261
                       sourceFileName + _T ( "\" \"" ) +
 
2262
                       path + _T ( "\"" );
 
2263
 
 
2264
        statusProgress ( _ ( "Export in progress..." ) );
 
2265
        int result = wxExecute ( cmd, wxEXEC_SYNC );
 
2266
        statusProgress ( wxEmptyString );
 
2267
        wxString message;
 
2268
        switch ( result )
 
2269
        {
 
2270
                case 1:
 
2271
                        messagePane ( _ ( "Cannot start Microsoft Word" ), CONST_STOP );
 
2272
                        return;
 
2273
                case 2:
 
2274
                        messagePane (
 
2275
                            _ ( "A more recent version of Microsoft Word is required" ), CONST_STOP );
 
2276
                        return;
 
2277
                case 3:
 
2278
                        message.Printf ( _ ( "Microsoft Word cannot save %s" ), path.c_str() );
 
2279
                        messagePane ( message, CONST_STOP );
 
2280
                        return;
 
2281
                case 0:
 
2282
                        break;
 
2283
                default:
 
2284
                        break;
 
2285
        }
 
2286
}
 
2287
 
 
2288
void MyFrame::OnBrowser ( wxCommandEvent& WXUNUSED ( event ) )
 
2289
{
 
2290
        statusProgress ( wxEmptyString );
 
2291
 
 
2292
        // fetch document contents
 
2293
        XmlDoc *doc;
 
2294
        if ( ( doc = getActiveDocument() ) == NULL )
 
2295
                return;
 
2296
 
 
2297
        wxString sourceFileName = doc->getFullFileName();
 
2298
        WrapTempFileName wtfn ( sourceFileName, _T ( ".html" ) );
 
2299
 
 
2300
        if ( sourceFileName.empty() || doc->GetModify() )
 
2301
        {
 
2302
                sourceFileName = wtfn.wideName();
 
2303
 
 
2304
                std::ofstream ofs ( ( const char * ) wtfn.name().c_str() );
 
2305
                if ( !ofs )
 
2306
                {
 
2307
                        messagePane ( _ ( "Cannot save temporary file" ), CONST_STOP );
 
2308
                        return;
 
2309
                }
 
2310
 
 
2311
                std::string utf8Buffer;
 
2312
                getRawText ( doc, utf8Buffer );
 
2313
                ofs << utf8Buffer;
 
2314
                ofs.close();
 
2315
 
 
2316
                // keep files until application closes
 
2317
                tempFileVector.push_back ( sourceFileName );
 
2318
                tempFileVector.push_back ( wtfn.originalWideName() );
 
2319
                wtfn.setKeepFiles ( true );
 
2320
        }
 
2321
 
 
2322
        navigate ( sourceFileName );
 
2323
}
 
2324
 
 
2325
void MyFrame::OnHelp ( wxCommandEvent& event )
 
2326
{
 
2327
#ifdef __WXMSW__
 
2328
        wxString cmd = _T ( "hh.exe \"" ) + helpDir + _T ( "xmlcopyeditor.chm\"" );
 
2329
        wxExecute ( cmd );
 
2330
#else
 
2331
        wxString helpFileName = helpDir + _T ( "xmlcopyeditor.hhp" );
 
2332
        helpController->AddBook ( wxFileName ( helpFileName ) );
 
2333
        helpController->DisplayContents();
 
2334
#endif
 
2335
}
 
2336
 
 
2337
void MyFrame::OnSplitTab ( wxCommandEvent& event )
 
2338
{
 
2339
/*
 
2340
        int id = event.GetId();
 
2341
        XmlDoc *doc = getActiveDocument();
 
2342
        if ( !doc )
 
2343
                return;
 
2344
        wxString fileName = doc->getFullFileName();
 
2345
 
 
2346
        // mainBook->GetSelection() is currently unreliable, so fetch by title
 
2347
 
 
2348
            int pageCount = mainBook->GetPageCount();
 
2349
            XmlDoc *currentDoc;
 
2350
            int currentSelection = -1;
 
2351
            for ( int i = 0; i < pageCount; ++i )
 
2352
            {
 
2353
                currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
 
2354
                if ( !currentDoc )
 
2355
                    break;
 
2356
                if ( currentDoc->getFullFileName() == fileName )
 
2357
                {
 
2358
                    currentSelection = i;
 
2359
                }
 
2360
            }
 
2361
            if ( currentSelection == -1 )
 
2362
                return;
 
2363
        */
 
2364
        int currentSelection, direction;
 
2365
        currentSelection = mainBook->GetSelection();
 
2366
        direction = wxAUI_NB_RIGHT;
 
2367
/*
 
2368
        switch ( id )
 
2369
        {
 
2370
                            ID_SPLIT_TAB_TOP:
 
2371
                                direction = wxAUI_NB_TOP;
 
2372
                                break;
 
2373
                            ID_SPLIT_TAB_RIGHT:
 
2374
                                direction = wxAUI_NB_RIGHT;
 
2375
                                break;
 
2376
                            ID_SPLIT_TAB_BOTTOM:
 
2377
                                direction = wxAUI_NB_BOTTOM;
 
2378
                                break;
 
2379
                            ID_SPLIT_TAB_LEFT:
 
2380
                                direction = wxAUI_NB_LEFT;
 
2381
                                break;
 
2382
                default:
 
2383
                        direction = wxAUI_NB_RIGHT;
 
2384
                        break;
 
2385
        }
 
2386
*/
 
2387
        mainBook->Split ( currentSelection, direction );
 
2388
}
 
2389
 
 
2390
void MyFrame::OnColorScheme ( wxCommandEvent& event )
 
2391
{
 
2392
        int id = event.GetId();
 
2393
        switch ( id )
 
2394
        {
 
2395
                case ID_COLOR_SCHEME_DEFAULT:
 
2396
                        properties.colorScheme = COLOR_SCHEME_DEFAULT;
 
2397
                        break;
 
2398
                case ID_COLOR_SCHEME_DEFAULT_BACKGROUND:
 
2399
                        properties.colorScheme = COLOR_SCHEME_DEFAULT_BACKGROUND;
 
2400
                        break;
 
2401
                case ID_COLOR_SCHEME_REDUCED_GLARE:
 
2402
                        properties.colorScheme = COLOR_SCHEME_REDUCED_GLARE;
 
2403
                        break;
 
2404
                case ID_COLOR_SCHEME_NONE:
 
2405
                        properties.colorScheme = COLOR_SCHEME_NONE;
 
2406
                        break;
 
2407
                default:
 
2408
                        return;
 
2409
        }
 
2410
        colorSchemeMenu->Check ( id, true );
 
2411
 
 
2412
        XmlDoc *doc;
 
2413
        if ( ( doc = getActiveDocument() ) == NULL )
 
2414
                return;
 
2415
 
 
2416
        properties.zoom = doc->GetZoom(); // ensure temp changes to font size are kept
 
2417
 
 
2418
        applyEditorProperties ( false );
 
2419
        doc->SetFocus();
 
2420
}
 
2421
 
 
2422
void MyFrame::OnFontSmaller ( wxCommandEvent& event )
 
2423
{
 
2424
        XmlDoc *doc;
 
2425
        if ( ( doc = getActiveDocument() ) == NULL )
 
2426
                return;
 
2427
        doc->ZoomOut();
 
2428
        properties.zoom = doc->GetZoom();
 
2429
        applyEditorProperties ( true );
 
2430
        doc->SetFocus();
 
2431
}
 
2432
 
 
2433
void MyFrame::OnFontMedium ( wxCommandEvent& event )
 
2434
{
 
2435
        XmlDoc *doc;
 
2436
        if ( ( doc = getActiveDocument() ) == NULL )
 
2437
                return;
 
2438
        doc->SetZoom ( 0 );
 
2439
        properties.zoom = doc->GetZoom();
 
2440
        applyEditorProperties ( true );
 
2441
        doc->SetFocus();
 
2442
}
 
2443
 
 
2444
void MyFrame::OnFontLarger ( wxCommandEvent& event )
 
2445
{
 
2446
        XmlDoc *doc;
 
2447
        if ( ( doc = getActiveDocument() ) == NULL )
 
2448
                return;
 
2449
        doc->ZoomIn();
 
2450
        properties.zoom = doc->GetZoom();
 
2451
        applyEditorProperties ( true );
 
2452
        doc->SetFocus();
 
2453
}
 
2454
 
 
2455
void MyFrame::OnOptions ( wxCommandEvent& WXUNUSED ( event ) )
 
2456
{
 
2457
        // ensure font size does not change after
 
2458
        XmlDoc *doc = getActiveDocument();
 
2459
        if ( doc )
 
2460
        {
 
2461
                properties.zoom = doc->GetZoom();
 
2462
        }
 
2463
 
 
2464
        wxString title
 
2465
#ifdef __WXMSW__
 
2466
        ( _ ( "Options" ) );
 
2467
#else
 
2468
        ( _ ( "Preferences" ) );
 
2469
#endif
 
2470
        std::auto_ptr<MyPropertySheet> mpsd ( new MyPropertySheet (
 
2471
                                                  this,
 
2472
                                                  properties,
 
2473
                                                  applicationDir,
 
2474
                                                  browserCommand,
 
2475
                                                  rememberOpenTabs,
 
2476
                                                  libxmlNetAccess,
 
2477
                                                  singleInstanceCheck,
 
2478
                                                  saveBom,
 
2479
                                                  unlimitedUndo,
 
2480
                                                  restoreLayout,
 
2481
                                                  expandInternalEntities,
 
2482
                                                  showFullPathOnFrame,
 
2483
                                                  lang,
 
2484
                                                  wxID_ANY,
 
2485
                                                  title ) );
 
2486
        if ( mpsd->ShowModal() == wxID_OK )
 
2487
        {
 
2488
                properties = mpsd->getProperties();
 
2489
                applyEditorProperties();
 
2490
                applicationDir = mpsd->getApplicationDir();
 
2491
                browserCommand = mpsd->getBrowserCommand();
 
2492
                rememberOpenTabs = mpsd->getRememberOpenTabs();
 
2493
                libxmlNetAccess = mpsd->getLibxmlNetAccess();
 
2494
                singleInstanceCheck = mpsd->getSingleInstanceCheck();
 
2495
                saveBom = mpsd->getSaveBom();
 
2496
                unlimitedUndo = mpsd->getUnlimitedUndo();
 
2497
                restoreLayout = mpsd->getRestoreLayout();
 
2498
                expandInternalEntities = mpsd->getExpandInternalEntities();
 
2499
                showFullPathOnFrame = mpsd->getShowFullPathOnFrame();
 
2500
                lang = mpsd->getLang();
 
2501
                updatePaths();
 
2502
        }
 
2503
        if ( doc )
 
2504
                doc->SetFocus();
 
2505
}
 
2506
 
 
2507
void MyFrame::OnHistoryFile ( wxCommandEvent& event )
 
2508
{
 
2509
        wxString f ( history.GetHistoryFile ( event.GetId() - wxID_FILE1 ) );
 
2510
        if ( !f.empty() )
 
2511
                openFile ( f );
 
2512
}
 
2513
 
 
2514
void MyFrame::OnGoto ( wxCommandEvent& WXUNUSED ( event ) )
 
2515
{
 
2516
        statusProgress ( wxEmptyString );
 
2517
 
 
2518
        XmlDoc *doc;
 
2519
        if ( ( doc = getActiveDocument() ) == NULL )
 
2520
                return;
 
2521
 
 
2522
        wxTextEntryDialog *dlg = new wxTextEntryDialog (
 
2523
            this,
 
2524
            _ ( "Enter line number:" ),
 
2525
            _ ( "Go To" ) );
 
2526
        int ret = dlg->ShowModal();
 
2527
        if ( ret == wxID_CANCEL )
 
2528
                return;
 
2529
        wxString val = dlg->GetValue();
 
2530
        long line;
 
2531
        if ( !val.ToLong ( &line ) || line < 1 )
 
2532
        {
 
2533
                wxString msg;
 
2534
                msg.Printf ( _ ( "'%s' is not a valid line number" ), val.c_str() );
 
2535
                messagePane ( msg, CONST_STOP );
 
2536
                return;
 
2537
        }
 
2538
        --line;
 
2539
        doc->GotoLine ( ( int ) line );
 
2540
        doc->SetFocus();
 
2541
}
 
2542
 
 
2543
void MyFrame::OnFindAgain ( wxCommandEvent& event )
 
2544
{
 
2545
        //findAgain(findData.GetFindString(), findData.GetFlags());
 
2546
        findReplacePanel->OnFindNext ( event );
 
2547
}
 
2548
 
 
2549
void MyFrame::OnCommand ( wxCommandEvent& WXUNUSED ( event ) )
 
2550
{
 
2551
        bool visible = manager.GetPane ( commandPanel ).IsShown();
 
2552
        if ( !visible )
 
2553
        {
 
2554
                manager.GetPane ( commandPanel ).Show();
 
2555
        }
 
2556
        manager.Update();
 
2557
        commandPanel->focusOnCommand();
 
2558
}
 
2559
 
 
2560
void MyFrame::OnFindReplace ( wxCommandEvent& WXUNUSED ( event ) )
 
2561
{
 
2562
#ifdef NEWFINDREPLACE
 
2563
        manager.GetPane ( findReplacePanel ).Caption ( _ ( "Replace" ) );
 
2564
        bool visible = manager.GetPane ( findReplacePanel ).IsShown();
 
2565
        if ( !visible )
 
2566
        {
 
2567
                manager.GetPane ( findReplacePanel ).Show();
 
2568
        }
 
2569
        manager.Update();
 
2570
        findReplacePanel->refresh();
 
2571
        findReplacePanel->setReplaceVisible ( true );
 
2572
        findReplacePanel->focusOnFind();
 
2573
        return;
 
2574
#endif
 
2575
 
 
2576
 
 
2577
        if ( findDialog.get() )
 
2578
        {
 
2579
                findDialog = std::auto_ptr<wxFindReplaceDialog> ( 0 );
 
2580
        }
 
2581
        findDialog = std::auto_ptr<wxFindReplaceDialog> ( new wxFindReplaceDialog (
 
2582
                         this,
 
2583
                         &findData,
 
2584
                         _ ( "Find and Replace" ),
 
2585
                         wxFR_REPLACEDIALOG ) );
 
2586
        findDialog->Show();
 
2587
}
 
2588
 
 
2589
void MyFrame::OnGlobalReplace ( wxCommandEvent& event )
 
2590
{
 
2591
        XmlDoc *doc;
 
2592
        if ( ( doc = getActiveDocument() ) == NULL )
 
2593
                return;
 
2594
 
 
2595
        size_t flags = findData.GetFlags();
 
2596
        std::auto_ptr<GlobalReplaceDialog> grd ( new GlobalReplaceDialog (
 
2597
                    this,
 
2598
                    findData.GetFindString(),
 
2599
                    findData.GetReplaceString(),
 
2600
                    flags & wxFR_MATCHCASE,
 
2601
                    globalReplaceAllDocuments,
 
2602
                    findRegex ) );
 
2603
        int res = grd->ShowModal();
 
2604
 
 
2605
        flags = 0;
 
2606
        flags |= wxFR_DOWN;
 
2607
        if ( grd->getMatchCase() )
 
2608
                flags |= wxFR_MATCHCASE;
 
2609
        findRegex = grd->getRegex();
 
2610
        globalReplaceAllDocuments = grd->getAllDocuments();
 
2611
 
 
2612
        findData.SetFindString ( grd->getFindString() );
 
2613
        findData.SetReplaceString ( grd->getReplaceString() );
 
2614
        findData.SetFlags ( flags );
 
2615
        findReplacePanel->setRegex ( findRegex );
 
2616
        findReplacePanel->setMatchCase ( flags & wxFR_MATCHCASE );
 
2617
        findReplacePanel->refresh();
 
2618
 
 
2619
        if ( res != wxID_OK )
 
2620
        {
 
2621
                return;
 
2622
        }
 
2623
 
 
2624
        int globalMatchCount, pageCount;
 
2625
        globalMatchCount = 0;
 
2626
        pageCount = mainBook->GetPageCount();
 
2627
        XmlDoc *currentDoc = getActiveDocument();
 
2628
        if ( !currentDoc )
 
2629
                return;
 
2630
 
 
2631
        for ( int i = 0; i < pageCount; ++i )
 
2632
        {
 
2633
                std::string bufferUtf8;
 
2634
                if ( !globalReplaceAllDocuments )
 
2635
                {
 
2636
                        getRawText ( currentDoc, bufferUtf8 );
 
2637
                }
 
2638
                else
 
2639
                {
 
2640
                        currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
 
2641
                        if ( !currentDoc )
 
2642
                                return;
 
2643
                        getRawText ( currentDoc, bufferUtf8 );
 
2644
                }
 
2645
 
 
2646
                size_t flags = findData.GetFlags();
 
2647
                if ( !findRegex )
 
2648
                {
 
2649
                        std::string findUtf8, replaceUtf8;
 
2650
                        findUtf8 =findData.GetFindString().mb_str ( wxConvUTF8 );
 
2651
                        replaceUtf8 = findData.GetReplaceString().mb_str ( wxConvUTF8 );
 
2652
                        globalMatchCount += Replace::run (
 
2653
                                                bufferUtf8,
 
2654
                                                findUtf8,
 
2655
                                                replaceUtf8,
 
2656
                                                flags & wxFR_MATCHCASE );
 
2657
                        currentDoc->SetTextRaw ( bufferUtf8.c_str() );
 
2658
                        currentDoc->setValidationRequired ( true );
 
2659
                }
 
2660
                else
 
2661
                {
 
2662
                        try
 
2663
                        {
 
2664
                                std::auto_ptr<WrapRegex> wr ( new WrapRegex (
 
2665
                                                                  ( const char * ) findData.GetFindString().mb_str ( wxConvUTF8 ),
 
2666
                                                                  flags & wxFR_MATCHCASE,
 
2667
                                                                  ( const char * ) findData.GetReplaceString().mb_str ( wxConvUTF8 ) ) );
 
2668
 
 
2669
                                int matchCount;
 
2670
                                std::string outputBuffer = wr->replaceGlobal ( bufferUtf8, &matchCount );
 
2671
                                globalMatchCount += matchCount;
 
2672
                                currentDoc->SetTextRaw ( outputBuffer.c_str() );
 
2673
                                currentDoc->setValidationRequired ( true );
 
2674
                        }
 
2675
                        catch ( std::exception& e )
 
2676
                        {
 
2677
                                wxString wideError = wxString ( e.what(), wxConvUTF8, strlen ( e.what() ) );
 
2678
                                messagePane ( _ ( "Cannot replace: " ) + wideError, CONST_STOP );
 
2679
                                return;
 
2680
                        }
 
2681
                }
 
2682
                if ( !globalReplaceAllDocuments )
 
2683
                        break;
 
2684
        }
 
2685
        wxString msg;
 
2686
 
 
2687
        msg.Printf (
 
2688
            ngettext ( L"%i replacement made", L"%i replacements made", globalMatchCount ),
 
2689
            globalMatchCount );
 
2690
 
 
2691
        statusProgress ( msg );
 
2692
}
 
2693
 
 
2694
void MyFrame::OnFrameClose ( wxCloseEvent& event )
 
2695
{
 
2696
        wxCommandEvent e;
 
2697
        OnCloseAll ( e );
 
2698
        if ( mainBook->GetPageCount() )
 
2699
        {
 
2700
                event.Veto();
 
2701
                return;
 
2702
        }
 
2703
        event.Skip();
 
2704
}
 
2705
 
 
2706
void MyFrame::OnNew ( wxCommandEvent& WXUNUSED ( event ) )
 
2707
{
 
2708
        wxString defaultSelection, typeSelection, templateFile;
 
2709
        defaultSelection = _ ( "XML document (*.xml)" );
 
2710
        wxArrayString templateArray;
 
2711
        if ( wxFileName::DirExists ( templateDir ) )
 
2712
        {
 
2713
                wxString templateMask, name, extension, entry;
 
2714
                templateMask = templateDir + wxFileName::GetPathSeparator() + _T ( "*.*" );
 
2715
                templateFile = wxFindFirstFile ( templateMask, wxFILE );
 
2716
                wxFileName fn;
 
2717
 
 
2718
                if ( !templateFile.empty() )
 
2719
                {
 
2720
                        fn.Assign ( templateFile );
 
2721
                        name = fn.GetName();
 
2722
                        extension = fn.GetExt();
 
2723
 
 
2724
                        entry.Printf ( _T ( "%s (*.%s)" ), name.c_str(), extension.c_str() );
 
2725
                        templateArray.Add ( entry );
 
2726
 
 
2727
                        for ( ;; )
 
2728
                        {
 
2729
                                templateFile = wxFindNextFile();
 
2730
                                if ( templateFile.empty() )
 
2731
                                        break;
 
2732
                                fn.Assign ( templateFile );
 
2733
                                name = fn.GetName();
 
2734
                                extension = fn.GetExt();
 
2735
 
 
2736
                                entry.Printf ( _T ( "%s (*.%s)" ), name.c_str(), extension.c_str() );
 
2737
                                templateArray.Add ( entry );
 
2738
                        }
 
2739
                }
 
2740
                templateArray.Sort();
 
2741
                templateArray.Insert ( defaultSelection, 0 );
 
2742
 
 
2743
                const int arraySize = templateArray.GetCount();
 
2744
 
 
2745
                wxString choiceArray[arraySize + 1];
 
2746
                for ( int i = 0; i < arraySize; ++i )
 
2747
                        * ( choiceArray + i ) = templateArray.Item ( i );
 
2748
 
 
2749
                wxSingleChoiceDialog scd (
 
2750
                    this, _ ( "Choose a document type:" ), _ ( "New Document" ), arraySize, choiceArray );
 
2751
                if ( scd.ShowModal() == wxID_CANCEL )
 
2752
                {
 
2753
                        XmlDoc *doc = getActiveDocument();
 
2754
                        if ( doc )
 
2755
                                doc->SetFocus();
 
2756
                        return;
 
2757
                }
 
2758
                typeSelection = scd.GetStringSelection();
 
2759
        }
 
2760
 
 
2761
        if ( typeSelection == defaultSelection )
 
2762
        {
 
2763
                wxString emptyString ( _T ( "" ) );
 
2764
                newDocument ( emptyString );
 
2765
                return;
 
2766
        }
 
2767
 
 
2768
        typeSelection.Replace ( _T ( " (*" ), wxEmptyString );
 
2769
        typeSelection.Replace ( _T ( ")" ), wxEmptyString );
 
2770
        templateFile = templateDir + typeSelection;
 
2771
        std::string templateFileLocal, buffer;
 
2772
        templateFileLocal = templateFile.mb_str ( wxConvLocal );
 
2773
        ReadFile::run ( templateFileLocal, buffer );
 
2774
        wxString documentContents = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
 
2775
 
 
2776
        newDocument ( documentContents,
 
2777
                      wxString ( templateFileLocal.c_str(), wxConvUTF8, templateFileLocal.size() ) );
 
2778
}
 
2779
 
 
2780
void MyFrame::newDocument ( const wxString& s, const wxString& path, bool canSave )
 
2781
{
 
2782
        std::string bufferUtf8 = ( const char * ) s.mb_str ( wxConvUTF8 );
 
2783
        std::string pathUtf8 = ( const char * ) path.mb_str ( wxConvUTF8 );
 
2784
        newDocument ( bufferUtf8, pathUtf8, canSave );
 
2785
}
 
2786
 
 
2787
void MyFrame::newDocument ( const std::string& s, const std::string& path, bool canSave )
 
2788
{
 
2789
        XmlDoc *doc;
 
2790
 
 
2791
        wxString documentLabel;
 
2792
        documentLabel.Printf ( _ ( "Document%i" ), documentCount++ );
 
2793
 
 
2794
        std::string auxPath = getAuxPath ( path );
 
2795
 
 
2796
        Freeze();
 
2797
        doc = ( s.empty() ) ?
 
2798
              new XmlDoc (
 
2799
                  mainBook,
 
2800
                  properties,
 
2801
                  &protectTags,
 
2802
                  visibilityState,
 
2803
                  FILE_TYPE_XML,
 
2804
                  wxID_ANY,
 
2805
                  NULL, 0 // new: NULL pointer leads to default document
 
2806
              )
 
2807
              : new XmlDoc (
 
2808
                  mainBook,
 
2809
                  properties,
 
2810
                  &protectTags,
 
2811
                  visibilityState,
 
2812
                  FILE_TYPE_XML,
 
2813
                  wxID_ANY,
 
2814
                  s.c_str(), // modified
 
2815
                  s.size(), // new
 
2816
                  catalogPath,
 
2817
                  catalogUtilityPath,
 
2818
                  path,
 
2819
                  auxPath );
 
2820
        mainBook->AddPage ( ( wxWindow * ) doc, documentLabel, true );
 
2821
        Thaw();
 
2822
 
 
2823
        mainBook->Refresh();
 
2824
        if ( properties.completion )
 
2825
                doc->updatePromptMaps();
 
2826
        doc->setShortFileName ( documentLabel );
 
2827
        doc->SetFocus();
 
2828
        manager.Update();
 
2829
        locationPanel->update ( doc, wxEmptyString );
 
2830
        insertChildPanel->update ( doc, wxEmptyString );
 
2831
        insertSiblingPanel->update ( doc, wxEmptyString );
 
2832
        insertEntityPanel->update ( doc );
 
2833
        if ( properties.validateAsYouType )
 
2834
                doc->backgroundValidate();
 
2835
}
 
2836
 
 
2837
void MyFrame::OnOpen ( wxCommandEvent& event )
 
2838
{
 
2839
        bool largeFile;
 
2840
        largeFile = ( event.GetId() == ID_OPEN_LARGE_FILE );
 
2841
 
 
2842
        wxString defaultFile, defaultDir;
 
2843
        XmlDoc *doc;
 
2844
        if ( ( doc = getActiveDocument() ) != NULL )
 
2845
        {
 
2846
                defaultFile = doc->getFullFileName();
 
2847
                if ( !defaultFile.empty() )
 
2848
                {
 
2849
                        wxFileName fn ( defaultFile );
 
2850
                        defaultDir = fn.GetPath();
 
2851
                }
 
2852
        }
 
2853
 
 
2854
        wxFileDialog *fd = new wxFileDialog (
 
2855
            this,
 
2856
            ( largeFile ) ? _ ( "Open Large Document" ) : _ ( "Open" ),
 
2857
            defaultDir,
 
2858
            wxEmptyString,
 
2859
            FILE_FILTER,
 
2860
            wxOPEN | wxMULTIPLE | wxFILE_MUST_EXIST | wxCHANGE_DIR
 
2861
        );
 
2862
 
 
2863
 
 
2864
        if ( fd->ShowModal() == wxID_CANCEL )
 
2865
                return;
 
2866
 
 
2867
        wxArrayString paths;
 
2868
        fd->GetPaths ( paths );
 
2869
        size_t count = paths.Count();
 
2870
        if ( !count )
 
2871
                return;
 
2872
        for ( size_t i = 0; i < count; ++i )
 
2873
                if ( !openFile ( paths[i], largeFile ) )
 
2874
                        break;
 
2875
}
 
2876
 
 
2877
bool MyFrame::openFile ( wxString& fileName, bool largeFile )
 
2878
{
 
2879
#ifndef __WXMSW__
 
2880
        // truncate string up to file:/ portion added by GNOME
 
2881
        wxString filePrefix = _T ( "file:" );
 
2882
        int index = fileName.Find ( filePrefix.c_str() );
 
2883
        if ( index != -1 )
 
2884
        {
 
2885
                fileName = fileName.Mid ( ( size_t ) index + filePrefix.Length() );
 
2886
        }
 
2887
#endif
 
2888
 
 
2889
        if ( !wxFileName::FileExists ( fileName ) )
 
2890
        {
 
2891
                wxString message;
 
2892
                message.Printf ( _ ( "Cannot open %s." ), fileName.c_str() );
 
2893
                messagePane ( message, CONST_STOP );
 
2894
                return false;
 
2895
        }
 
2896
 
 
2897
        if ( openFileSet.count ( fileName ) )
 
2898
        {
 
2899
                wxString message;
 
2900
                message.Printf ( _ ( "%s is already open" ), fileName.c_str() );
 
2901
                statusProgress ( message );
 
2902
                activateTab ( fileName );
 
2903
                return false;
 
2904
        }
 
2905
 
 
2906
        wxString directory, name, extension;
 
2907
        wxFileName::SplitPath ( fileName, NULL, &directory, &name, &extension );
 
2908
 
 
2909
        if ( !extension.empty() )
 
2910
        {
 
2911
                name += _T ( "." );
 
2912
                name += extension;
 
2913
        }
 
2914
 
 
2915
        wxString wideError;
 
2916
        std::string buffer;
 
2917
        pair<int, int> posPair;
 
2918
        XmlDoc *doc;
 
2919
 
 
2920
        int type = getFileType ( fileName );
 
2921
        std::string auxPath = getAuxPath ( ( const char * ) fileName.mb_str ( wxConvLocal ) );
 
2922
 
 
2923
        char *docBuffer = 0;
 
2924
        size_t docBufferLen = 0;
 
2925
        bool fileEmpty = false;
 
2926
 
 
2927
        statusProgress ( _T ( "Opening file..." ) );
 
2928
        //wxMemoryMappedFile *memorymap = NULL;
 
2929
        BinaryFile *binaryfile = NULL;
 
2930
 
 
2931
        binaryfile = new BinaryFile ( ( const char * ) fileName.mb_str ( wxConvLocal ) );
 
2932
        if ( !binaryfile->getData() )
 
2933
        {
 
2934
                wxString message;
 
2935
                message.Printf ( _ ( "Cannot open %s" ), fileName.c_str() );
 
2936
                messagePane ( message, CONST_STOP );
 
2937
                statusProgress ( wxEmptyString );
 
2938
                return false;
 
2939
        }
 
2940
        /*
 
2941
            memorymap = new wxMemoryMappedFile(
 
2942
              fileName,
 
2943
              true, // readOnly
 
2944
              true // fread
 
2945
            );
 
2946
        */
 
2947
 
 
2948
        /*
 
2949
          catch (wxMemoryMappedFileEmptyException&)
 
2950
          {
 
2951
            fileEmpty = true;
 
2952
          }
 
2953
        */
 
2954
 
 
2955
        bool isUtf8 = false;
 
2956
 
 
2957
        if ( !fileEmpty )
 
2958
        {
 
2959
                docBuffer = ( char * ) binaryfile->getData();//memorymap->GetStream();
 
2960
                docBufferLen = binaryfile->getDataLen();//memorymap->GetMapSize();
 
2961
        }
 
2962
        else
 
2963
        {
 
2964
                docBuffer = NULL;
 
2965
                docBufferLen = 0;
 
2966
                isUtf8 = true;
 
2967
        }
 
2968
 
 
2969
        statusProgress ( wxEmptyString );
 
2970
 
 
2971
        char *iconvBuffer = 0;
 
2972
        size_t iconvBufferLen = 0;
 
2973
 
 
2974
        char *finalBuffer;
 
2975
        size_t finalBufferLen;
 
2976
 
 
2977
        // adjust for UTF-8 BOM
 
2978
        if ( docBuffer &&
 
2979
                ( unsigned char ) docBuffer[0] == 0xEF &&
 
2980
                ( unsigned char ) docBuffer[1] == 0xBB &&
 
2981
                ( unsigned char ) docBuffer[2] == 0xBF )
 
2982
        {
 
2983
                docBuffer += 3;
 
2984
                docBufferLen -= 3;
 
2985
                isUtf8 = true;
 
2986
        }
 
2987
 
 
2988
        // no UTF-8 BOM found
 
2989
        std::string encoding;
 
2990
        if ( !isUtf8 || !binaryfile->getDataLen() )
 
2991
        {
 
2992
                XmlEncodingSpy es;
 
2993
                es.parse ( docBuffer, docBufferLen );
 
2994
                encoding = es.getEncoding();
 
2995
                if ( encoding == "UTF-8" ||
 
2996
                        encoding == "utf-8" ||
 
2997
                        encoding == "US-ASCII" ||
 
2998
                        encoding == "us-ascii" ) // US-ASCII is a subset of UTF-8
 
2999
                        isUtf8 = true;
 
3000
        }
 
3001
 
 
3002
        // convert buffer if not UTF-8
 
3003
        if ( isUtf8 )
 
3004
        {
 
3005
                finalBuffer = docBuffer;
 
3006
                finalBufferLen = docBufferLen;
 
3007
        }
 
3008
        else
 
3009
        {
 
3010
                // clear any other BOMs
 
3011
 
 
3012
                if ( docBuffer && // UTF-32 BE
 
3013
                        ( unsigned char ) docBuffer[0] == 0x00 &&
 
3014
                        ( unsigned char ) docBuffer[1] == 0x00 &&
 
3015
                        ( unsigned char ) docBuffer[2] == 0xFE &&
 
3016
                        ( unsigned char ) docBuffer[3] == 0xFF )
 
3017
                {
 
3018
                        docBuffer += 4;
 
3019
                        docBufferLen -= 4;
 
3020
                }
 
3021
                if ( docBuffer && // UTF-32 LE
 
3022
                        ( unsigned char ) docBuffer[0] == 0xFF &&
 
3023
                        ( unsigned char ) docBuffer[1] == 0xFE &&
 
3024
                        ( unsigned char ) docBuffer[2] == 0x00 &&
 
3025
                        ( unsigned char ) docBuffer[3] == 0x00 )
 
3026
                {
 
3027
                        docBuffer += 4;
 
3028
                        docBufferLen -= 4;
 
3029
                }
 
3030
 
 
3031
                if ( docBuffer && //UTF-16 BE
 
3032
                        ( unsigned char ) docBuffer[0] == 0xFE &&
 
3033
                        ( unsigned char ) docBuffer[1] == 0xFF )
 
3034
                {
 
3035
                        docBuffer += 2;
 
3036
                        docBufferLen -= 2;
 
3037
                }
 
3038
                if ( docBuffer && //UTF-16 LE
 
3039
                        ( unsigned char ) docBuffer[0] == 0xFF &&
 
3040
                        ( unsigned char ) docBuffer[1] == 0xFE )
 
3041
                {
 
3042
                        docBuffer += 2;
 
3043
                        docBufferLen -= 2;
 
3044
                }
 
3045
 
 
3046
                if ( !encoding.size() ) // Expat couldn't parse file (e.g. UTF-32)
 
3047
                {
 
3048
                        encoding = getApproximateEncoding ( docBuffer, docBufferLen );
 
3049
                }
 
3050
 
 
3051
                wxString wideEncoding = wxString (
 
3052
                                            encoding.c_str(),
 
3053
                                            wxConvLocal,
 
3054
                                            encoding.size() );
 
3055
                iconv_t cd = iconv_open ( "UTF-8", encoding.c_str() );
 
3056
                if ( cd == ( iconv_t )-1 )
 
3057
                {
 
3058
                        wxString message;
 
3059
                        message.Printf ( _ ( "Cannot open %s: unknown encoding %s" ),
 
3060
                                         fileName.c_str(),
 
3061
                                         wideEncoding.c_str() );
 
3062
                        messagePane ( message, CONST_STOP );
 
3063
                        delete binaryfile;//memorymap;
 
3064
                        return false;
 
3065
                };
 
3066
 
 
3067
                int iconvLenMultiplier = 4; // worst case scenario
 
3068
                if ( encoding == "ISO-8859-1" ||
 
3069
                        encoding == "UTF-16" ||
 
3070
                        encoding == "UTF-16BE" ||
 
3071
                        encoding == "UTF-16LE" )
 
3072
                {
 
3073
                        iconvLenMultiplier = 2;
 
3074
                }
 
3075
                else if ( encoding == "UTF-32" ||
 
3076
                          encoding == "UTF-32BE" ||
 
3077
                          encoding == "UTF-32LE" )
 
3078
                {
 
3079
                        iconvLenMultiplier = 1;
 
3080
                }
 
3081
 
 
3082
                size_t iconvBufferLeft, docBufferLeft;
 
3083
                iconvBufferLen = iconvBufferLeft = docBufferLen * iconvLenMultiplier + 1;
 
3084
                docBufferLeft = docBufferLen;
 
3085
                iconvBuffer = new char[iconvBufferLen];
 
3086
                finalBuffer = iconvBuffer; // iconvBuffer will be incremented by iconv
 
3087
                size_t nconv;
 
3088
 
 
3089
                nconv = iconv (
 
3090
                            cd,
 
3091
#ifdef __WXMSW__
 
3092
                            ( const char ** )
 
3093
#endif
 
3094
                            &docBuffer,
 
3095
                            &docBufferLeft,
 
3096
                            &iconvBuffer,
 
3097
                            &iconvBufferLeft );
 
3098
 
 
3099
                *iconvBuffer = '\0';
 
3100
 
 
3101
                iconv_close ( cd );
 
3102
 
 
3103
                if ( nconv == ( size_t )-1 )
 
3104
                {
 
3105
                        wxString message;
 
3106
                        message.Printf ( _ ( "Cannot open %s: conversion from encoding %s failed" ),
 
3107
                                         fileName.c_str(),
 
3108
                                         wideEncoding.c_str() );
 
3109
                        messagePane ( message, CONST_STOP );
 
3110
                        delete[] finalBuffer;
 
3111
                        delete binaryfile; //delete memorymap;
 
3112
                        return false;
 
3113
                }
 
3114
                finalBufferLen = iconvBufferLen - iconvBufferLeft;
 
3115
        }
 
3116
 
 
3117
        statusProgress ( _ ( "Creating document view..." ) );
 
3118
        Freeze();
 
3119
        doc = new XmlDoc (
 
3120
            mainBook,
 
3121
            ( largeFile ) ? largeFileProperties: properties,
 
3122
            &protectTags,
 
3123
            visibilityState,
 
3124
            ( !binaryfile->getDataLen() ) ? FILE_TYPE_XML : type,
 
3125
            wxID_ANY,
 
3126
            finalBuffer,
 
3127
            finalBufferLen,
 
3128
            catalogPath,
 
3129
            catalogUtilityPath,
 
3130
            ( const char * ) fileName.mb_str ( wxConvLocal ),
 
3131
            auxPath );
 
3132
#ifdef __WXMSW__
 
3133
        doc->SetUndoCollection ( false );
 
3134
        doc->SetUndoCollection ( true );
 
3135
#endif
 
3136
 
 
3137
        doc->setFullFileName ( fileName );
 
3138
        doc->setShortFileName ( name );
 
3139
        doc->setDirectory ( directory );
 
3140
        openFileSet.insert ( fileName );
 
3141
        history.AddFileToHistory ( fileName );
 
3142
        updateFileMenu();
 
3143
        wxFileName ofn ( fileName );
 
3144
        doc->setLastModified ( ofn.GetModificationTime() );
 
3145
 
 
3146
        mainBook->AddPage ( ( wxWindow * ) doc, name, _T ( "" ) );
 
3147
        Thaw();
 
3148
        statusProgress ( wxEmptyString );
 
3149
 
 
3150
        mainBook->Refresh();
 
3151
 
 
3152
        wxFileName fn ( fileName );
 
3153
        doc->setLastModified ( fn.GetModificationTime() );
 
3154
        doc->SetFocus();
 
3155
 
 
3156
        if ( type != FILE_TYPE_XML || !binaryfile->getDataLen() )
 
3157
        {
 
3158
                delete binaryfile;//memorymap;
 
3159
                return true;
 
3160
        }
 
3161
 
 
3162
        // NOW parse the document, but don't create a UTF-8 copy
 
3163
        statusProgress ( _T ( "Parsing document..." ) );
 
3164
        std::auto_ptr<WrapExpat> we ( new WrapExpat() );
 
3165
 
 
3166
        bool optimisedParseSuccess = false;
 
3167
 
 
3168
        // omit XML declaration
 
3169
        if ( !isUtf8 && finalBufferLen &&
 
3170
                finalBuffer[0] == '<' &&
 
3171
                finalBuffer[1] == '?' &&
 
3172
                finalBuffer[2] == 'x' &&
 
3173
                finalBuffer[3] == 'm' &&
 
3174
                finalBuffer[4] == 'l' )
 
3175
        {
 
3176
                for ( ; *finalBuffer && finalBufferLen; finalBuffer++ && finalBufferLen-- )
 
3177
                {
 
3178
                        if ( *finalBuffer == '>' )
 
3179
                        {
 
3180
                                finalBuffer++;
 
3181
                                finalBufferLen--;
 
3182
                                break;
 
3183
                        }
 
3184
                }
 
3185
        }
 
3186
 
 
3187
        if ( finalBuffer )
 
3188
        {
 
3189
                optimisedParseSuccess = we->parse ( finalBuffer, finalBufferLen );
 
3190
                statusProgress ( wxEmptyString );
 
3191
        }
 
3192
 
 
3193
        // NOW update prompt maps if necessary
 
3194
        if ( !largeFile && ( properties.completion || properties.validateAsYouType ) )
 
3195
        {
 
3196
                statusProgress ( _T ( "Compiling autocompletion lists..." ) );
 
3197
                doc->updatePromptMaps ( finalBuffer, finalBufferLen );
 
3198
                statusProgress ( wxEmptyString );
 
3199
        }
 
3200
 
 
3201
        if ( !largeFile && ( properties.validateAsYouType && doc->getGrammarFound() ) )
 
3202
        {
 
3203
                statusProgress ( _T ( "Validating document..." ) );
 
3204
                //doc->backgroundValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen );
 
3205
                doc->backgroundValidate();
 
3206
                statusProgress ( wxEmptyString );
 
3207
        }
 
3208
 
 
3209
        if ( !optimisedParseSuccess )
 
3210
        {
 
3211
                std::string error = we->getLastError();
 
3212
                wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
3213
                posPair = we->getErrorPosition();
 
3214
                -- ( posPair.first );
 
3215
                messagePane ( wideError, CONST_WARNING );
 
3216
 
 
3217
                int newPosition = doc->PositionFromLine ( posPair.first );
 
3218
                doc->SetSelection ( newPosition, newPosition );
 
3219
                doc->SetFocus();
 
3220
        }
 
3221
        else
 
3222
        {
 
3223
                closePane();
 
3224
        }
 
3225
        delete binaryfile; //delete memorymap;
 
3226
        return true;
 
3227
}
 
3228
 
 
3229
std::string MyFrame::getApproximateEncoding ( char *docBuffer,
 
3230
        size_t docBufferLen )
 
3231
{
 
3232
        std::string line, encoding;
 
3233
        char *it;
 
3234
        size_t i;
 
3235
 
 
3236
        // grab first line
 
3237
        for (
 
3238
            i = 0, it = docBuffer;
 
3239
            i < docBufferLen && *it != '\n' && i < BUFSIZ;
 
3240
            i++, it++ )
 
3241
        {
 
3242
                if ( *it )
 
3243
                        line += *it;
 
3244
        }
 
3245
 
 
3246
        std::pair<int, int> limits = XmlEncodingHandler::getEncodingValueLimits ( line );
 
3247
 
 
3248
        if ( limits.first == -1 || limits.second == -1 )
 
3249
                return "";
 
3250
 
 
3251
        return line.substr ( limits.first, limits.second );
 
3252
}
 
3253
 
 
3254
void MyFrame::OnToggleFold ( wxCommandEvent& WXUNUSED ( event ) )
 
3255
{
 
3256
        XmlDoc *doc;
 
3257
        if ( ( doc = getActiveDocument() ) == NULL )
 
3258
                return;
 
3259
        doc->toggleFold();
 
3260
}
 
3261
 
 
3262
void MyFrame::OnFoldAll ( wxCommandEvent& WXUNUSED ( event ) )
 
3263
{
 
3264
        XmlDoc *doc;
 
3265
        if ( ( doc = getActiveDocument() ) == NULL )
 
3266
                return;
 
3267
        doc->foldAll();
 
3268
        doc->SetFocus();
 
3269
}
 
3270
 
 
3271
void MyFrame::OnUnfoldAll ( wxCommandEvent& WXUNUSED ( event ) )
 
3272
{
 
3273
        XmlDoc *doc;
 
3274
        if ( ( doc = getActiveDocument() ) == NULL )
 
3275
                return;
 
3276
        doc->unfoldAll();
 
3277
        doc->SetFocus();
 
3278
}
 
3279
 
 
3280
 
 
3281
void MyFrame::OnQuit ( wxCommandEvent& WXUNUSED ( event ) )
 
3282
{
 
3283
        Close ( true );
 
3284
}
 
3285
 
 
3286
void MyFrame::OnUndo ( wxCommandEvent& WXUNUSED ( event ) )
 
3287
{
 
3288
        XmlDoc *doc;
 
3289
        if ( ( doc = getActiveDocument() ) == NULL )
 
3290
                return;
 
3291
        doc->Undo();
 
3292
        doc->setValidationRequired ( true );
 
3293
        doc->SetFocus();
 
3294
}
 
3295
 
 
3296
void MyFrame::OnRedo ( wxCommandEvent& WXUNUSED ( event ) )
 
3297
{
 
3298
        XmlDoc *doc;
 
3299
        if ( ( doc = getActiveDocument() ) == NULL )
 
3300
                return;
 
3301
        doc->Redo();
 
3302
        doc->setValidationRequired ( true );
 
3303
        doc->SetFocus();
 
3304
}
 
3305
 
 
3306
void MyFrame::OnRevert ( wxCommandEvent& WXUNUSED ( event ) )
 
3307
{
 
3308
        XmlDoc *doc;
 
3309
        if ( ( doc = getActiveDocument() ) == NULL )
 
3310
                return;
 
3311
 
 
3312
        while ( doc->GetModify() )
 
3313
        {
 
3314
                if ( !doc->CanUndo() )
 
3315
                        return;
 
3316
                doc->Undo();
 
3317
                doc->setValidationRequired ( true );
 
3318
        }
 
3319
        doc->SetFocus();
 
3320
}
 
3321
 
 
3322
void MyFrame::OnSpelling ( wxCommandEvent& event )
 
3323
{
 
3324
        XmlDoc *doc;
 
3325
        if ( ( doc = getActiveDocument() ) == NULL )
 
3326
                return;
 
3327
 
 
3328
        statusProgress ( wxEmptyString );
 
3329
        closePane();
 
3330
 
 
3331
#ifdef __WXMSW__
 
3332
        doc->SetUndoCollection ( false );
 
3333
        doc->SetUndoCollection ( true );
 
3334
#endif
 
3335
 
 
3336
        int id, type;
 
3337
        id = event.GetId();
 
3338
        type = (id == ID_STYLE) ? ID_TYPE_STYLE : ID_TYPE_SPELL;
 
3339
                
 
3340
 
 
3341
        std::string rawBufferUtf8;
 
3342
        getRawText ( doc, rawBufferUtf8 );
 
3343
        bool success = true; // always true for now: well-formedness not req'd 
 
3344
 
 
3345
        auto_ptr<StyleDialog> sd ( new StyleDialog (
 
3346
                                       this,
 
3347
                                       wxICON ( appicon ),
 
3348
                                       rawBufferUtf8,
 
3349
                                       doc->getShortFileName(),
 
3350
                                       ruleSetDir,
 
3351
                                       filterDir,
 
3352
                                       browserCommand,
 
3353
                                                   ( type == ID_TYPE_SPELL ) ? dictionaryPreset : ruleSetPreset,
 
3354
                                       filterPreset,
 
3355
 #ifdef __WXMSW__
 
3356
                                   aspellDataPath,
 
3357
                                   aspellDictPath,
 
3358
 #endif
 
3359
                                                       type,
 
3360
                                       ( success ) ? false : true,
 
3361
                                       stylePosition,
 
3362
                                       styleSize ) );
 
3363
 
 
3364
        if ( sd->ShowModal() == wxID_OK )
 
3365
        {
 
3366
                std::string bufferUtf8 = sd->getEditedString();
 
3367
                if ( bufferUtf8.empty() )
 
3368
                        messagePane ( _ ( "Edited document empty" ), CONST_STOP );
 
3369
                else
 
3370
                        doc->SetTextRaw ( bufferUtf8.c_str() );
 
3371
        }
 
3372
        
 
3373
        // update presets if report has been created (even if followed by cancel)
 
3374
        if (type == ID_TYPE_STYLE)
 
3375
        {
 
3376
                ruleSetPreset = sd->getRuleSetPreset();
 
3377
                filterPreset = sd->getFilterPreset();
 
3378
        }
 
3379
        else
 
3380
                dictionaryPreset = sd->getRuleSetPreset();
 
3381
                
 
3382
#ifdef __WXMSW__
 
3383
        stylePosition = sd->getPosition();
 
3384
        styleSize = sd->getSize();
 
3385
#endif
 
3386
}
 
3387
 
 
3388
void MyFrame::OnPreviousDocument ( wxCommandEvent& WXUNUSED ( event ) )
 
3389
{
 
3390
        if ( !getActiveDocument() )
 
3391
                return;
 
3392
 
 
3393
        statusProgress ( wxEmptyString );
 
3394
        closePane();
 
3395
 
 
3396
        int currentSelection = mainBook->GetSelection();
 
3397
        if ( currentSelection < 1 )
 
3398
                return;
 
3399
        mainBook->SetSelection ( --currentSelection );
 
3400
        XmlDoc *doc = getActiveDocument();
 
3401
        if ( doc )
 
3402
                doc->SetFocus();
 
3403
}
 
3404
 
 
3405
void MyFrame::OnNextDocument ( wxCommandEvent& WXUNUSED ( event ) )
 
3406
{
 
3407
        if ( !getActiveDocument() )
 
3408
                return;
 
3409
 
 
3410
        statusProgress ( wxEmptyString );
 
3411
        closePane();
 
3412
 
 
3413
        int currentSelection = mainBook->GetSelection();
 
3414
        int maxSelection = mainBook->GetPageCount();
 
3415
        if ( currentSelection >= ( maxSelection - 1 ) )
 
3416
                return;
 
3417
        mainBook->SetSelection ( ++currentSelection );
 
3418
        XmlDoc *doc = getActiveDocument();
 
3419
        if ( doc )
 
3420
                doc->SetFocus();
 
3421
}
 
3422
 
 
3423
void MyFrame::OnSave ( wxCommandEvent& event )
 
3424
{
 
3425
        save();
 
3426
}
 
3427
 
 
3428
void MyFrame::save()
 
3429
{
 
3430
        XmlDoc *doc;
 
3431
        if ( ( doc = getActiveDocument() ) == NULL )
 
3432
                return;
 
3433
 
 
3434
        wxString fileName;
 
3435
        if ( ( fileName = doc->getFullFileName() ) == _T ( "" ) )
 
3436
        {
 
3437
                wxCommandEvent event;
 
3438
                OnSaveAs ( event );
 
3439
                return;
 
3440
        }
 
3441
 
 
3442
        if ( !saveFile ( doc, fileName, true ) )
 
3443
                ; // handle messages in saveFile
 
3444
}
 
3445
 
 
3446
void MyFrame::OnReload ( wxCommandEvent& event )
 
3447
{
 
3448
        reloadTab();
 
3449
}
 
3450
 
 
3451
void MyFrame::reloadTab()
 
3452
{
 
3453
        XmlDoc *doc;
 
3454
        if ( ( doc = getActiveDocument() ) == NULL )
 
3455
                return;
 
3456
        wxString fileName = doc->getFullFileName();
 
3457
 
 
3458
        if ( closeActiveDocument() )
 
3459
                openFile ( fileName );
 
3460
}
 
3461
 
 
3462
void MyFrame::OnSaveAs ( wxCommandEvent& event )
 
3463
{
 
3464
        saveAs();
 
3465
}
 
3466
 
 
3467
void MyFrame::saveAs()
 
3468
{
 
3469
        XmlDoc *doc;
 
3470
        if ( ( doc = getActiveDocument() ) == NULL )
 
3471
                return;
 
3472
 
 
3473
        wxString defaultFile, defaultDir;
 
3474
        defaultFile = doc->getFullFileName();
 
3475
        if ( !defaultFile.empty() )
 
3476
        {
 
3477
                wxFileName fn ( defaultFile );
 
3478
                defaultDir = fn.GetPath();
 
3479
        }
 
3480
        auto_ptr<wxFileDialog> fd ( new wxFileDialog (
 
3481
                                        this,
 
3482
                                        _ ( "Save As" ),
 
3483
                                        defaultDir,
 
3484
                                        defaultFile,
 
3485
                                        FILE_FILTER,
 
3486
                                        wxSAVE | wxOVERWRITE_PROMPT ) );
 
3487
        if ( fd->ShowModal() == wxID_CANCEL )
 
3488
                return;
 
3489
 
 
3490
        wxString path = fd->GetPath();
 
3491
 
 
3492
        if (
 
3493
            openFileSet.count ( path ) &&
 
3494
            path != doc->getFullFileName() )
 
3495
        {
 
3496
                wxString message;
 
3497
                message.Printf ( _ ( "%s is already open" ), path.c_str() );
 
3498
                messagePane ( message, CONST_STOP );
 
3499
                return;
 
3500
        }
 
3501
 
 
3502
        wxString name = fd->GetFilename();
 
3503
        wxString directory;
 
3504
        wxFileName::SplitPath ( path, &directory, NULL, NULL );
 
3505
        if ( path == _T ( "" ) )
 
3506
                return;
 
3507
 
 
3508
        if ( !saveFile ( doc, path, false ) )
 
3509
                return;
 
3510
 
 
3511
        // if already named, remove from set of open files
 
3512
        openFileSet.erase ( doc->getFullFileName() );
 
3513
 
 
3514
        doc->setFullFileName ( path );
 
3515
        doc->setShortFileName ( name );
 
3516
        doc->setDirectory ( directory );
 
3517
 
 
3518
        history.AddFileToHistory ( path ); // update history
 
3519
        updateFileMenu();
 
3520
 
 
3521
        int selection;
 
3522
        if ( ( selection = mainBook->GetSelection() ) == -1 )
 
3523
                return;
 
3524
        mainBook->SetPageText ( selection, name );
 
3525
}
 
3526
 
 
3527
void MyFrame::OnUpdateCloseAll ( wxUpdateUIEvent& event )
 
3528
{
 
3529
        event.Enable ( mainBook->GetPageCount() > 1 );
 
3530
}
 
3531
 
 
3532
void MyFrame::OnUpdateReload ( wxUpdateUIEvent& event )
 
3533
{
 
3534
        XmlDoc *doc;
 
3535
        if ( ( doc = getActiveDocument() ) == NULL )
 
3536
        {
 
3537
                event.Enable ( false );
 
3538
                return;
 
3539
        }
 
3540
        event.Enable ( !doc->getFullFileName().empty() );
 
3541
}
 
3542
 
 
3543
void MyFrame::OnUpdateCutCopy ( wxUpdateUIEvent& event )
 
3544
{
 
3545
        XmlDoc *doc;
 
3546
        if ( ( doc = getActiveDocument() ) == NULL )
 
3547
        {
 
3548
                event.Enable ( false );
 
3549
                return;
 
3550
        }
 
3551
        event.Enable ( true );
 
3552
}
 
3553
 
 
3554
void MyFrame::OnUpdateLocationPaneVisible ( wxUpdateUIEvent& event )
 
3555
{
 
3556
        if ( !viewMenu )
 
3557
                return;
 
3558
        wxAuiPaneInfo info = manager.GetPane ( locationPanel );
 
3559
        event.Check ( info.IsShown() );
 
3560
}
 
3561
 
 
3562
void MyFrame::OnUpdateSavedOnly ( wxUpdateUIEvent& event )
 
3563
{
 
3564
        XmlDoc *doc;
 
3565
        if ( ( doc = getActiveDocument() ) == NULL )
 
3566
        {
 
3567
                event.Enable ( false );
 
3568
                return;
 
3569
        }
 
3570
        event.Enable (
 
3571
            ( doc->getFullFileName().empty() ) ? false : true );
 
3572
}
 
3573
 
 
3574
void MyFrame::OnUpdateDocRange ( wxUpdateUIEvent& event )
 
3575
{
 
3576
        XmlDoc *doc;
 
3577
        if ( ( doc = getActiveDocument() ) == NULL )
 
3578
        {
 
3579
                event.Enable ( false );
 
3580
                return;
 
3581
        }
 
3582
        event.Enable ( true );
 
3583
}
 
3584
 
 
3585
void MyFrame::OnUpdateReplaceRange ( wxUpdateUIEvent& event )
 
3586
{
 
3587
        XmlDoc *doc;
 
3588
        if ( ( doc = getActiveDocument() ) == NULL ) // || protectTags)
 
3589
        {
 
3590
                event.Enable ( false );
 
3591
                return;
 
3592
        }
 
3593
        event.Enable ( true );
 
3594
}
 
3595
 
 
3596
void MyFrame::OnUpdateFindAgain ( wxUpdateUIEvent& event )
 
3597
{
 
3598
        if ( !getActiveDocument() || findData.GetFindString().empty() )
 
3599
        {
 
3600
                event.Enable ( false );
 
3601
                return;
 
3602
        }
 
3603
        event.Enable ( true );
 
3604
}
 
3605
 
 
3606
void MyFrame::OnUpdateUndo ( wxUpdateUIEvent& event )
 
3607
{
 
3608
        XmlDoc *doc;
 
3609
        if ( ( doc = getActiveDocument() ) == NULL )
 
3610
        {
 
3611
                event.Enable ( false );
 
3612
                return;
 
3613
        }
 
3614
#ifdef __WXMSW__
 
3615
        event.Enable((doc->CanUndo()) ? true : false);
 
3616
#else
 
3617
        event.Enable ( ( doc->GetModify() ) ? true : false );
 
3618
#endif
 
3619
}
 
3620
 
 
3621
void MyFrame::OnUpdateRedo ( wxUpdateUIEvent& event )
 
3622
{
 
3623
        XmlDoc *doc;
 
3624
        if ( ( doc = getActiveDocument() ) == NULL )
 
3625
        {
 
3626
                event.Enable ( false );
 
3627
                return;
 
3628
        }
 
3629
        event.Enable ( doc->CanRedo() );
 
3630
}
 
3631
 
 
3632
void MyFrame::OnUpdatePaste ( wxUpdateUIEvent& event )
 
3633
{
 
3634
        XmlDoc *doc;
 
3635
        if ( ( doc = getActiveDocument() ) == NULL )
 
3636
        {
 
3637
                event.Enable ( false );
 
3638
                return;
 
3639
        }
 
3640
        event.Enable ( true );
 
3641
}
 
3642
 
 
3643
void MyFrame::OnUpdatePreviousDocument ( wxUpdateUIEvent& event )
 
3644
{
 
3645
        if ( !getActiveDocument() )
 
3646
        {
 
3647
                event.Enable ( false );
 
3648
                return;
 
3649
        }
 
3650
        int currentDocument = mainBook->GetSelection();
 
3651
        event.Enable ( ( currentDocument < 1 ) ? false : true );
 
3652
}
 
3653
 
 
3654
void MyFrame::OnUpdateNextDocument ( wxUpdateUIEvent& event )
 
3655
{
 
3656
        if ( !getActiveDocument() )
 
3657
        {
 
3658
                event.Enable ( false );
 
3659
                return;
 
3660
        }
 
3661
        int currentDocument = mainBook->GetSelection();
 
3662
        int maxDocument = mainBook->GetPageCount();
 
3663
        event.Enable ( ( currentDocument >= ( maxDocument - 1 ) ) ? false : true );
 
3664
}
 
3665
 
 
3666
void MyFrame::OnUpdateClosePane ( wxUpdateUIEvent& event )
 
3667
{
 
3668
        wxAuiPaneInfo i1, i2, i3;
 
3669
        i1 = manager.GetPane ( htmlReport );
 
3670
        i2 = manager.GetPane ( findReplacePanel );
 
3671
        i3 = manager.GetPane ( commandPanel );
 
3672
        event.Enable ( i1.IsShown() || i2.IsShown() || i3.IsShown() );
 
3673
}
 
3674
 
 
3675
void MyFrame::OnValidateDTD ( wxCommandEvent& event )
 
3676
{
 
3677
        statusProgress ( wxEmptyString );
 
3678
 
 
3679
        // fetch document contents
 
3680
        XmlDoc *doc;
 
3681
        if ( ( doc = getActiveDocument() ) == NULL )
 
3682
                return;
 
3683
 
 
3684
        updatePaths(); // needed to ensure catalog is available
 
3685
 
 
3686
        wxString fname = doc->getFullFileName();
 
3687
 
 
3688
        WrapTempFileName wtfn ( fname );
 
3689
        if ( fname.empty() || doc->GetModify() )
 
3690
        {
 
3691
                wxString wideBuffer = doc->GetText();
 
3692
 
 
3693
                std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
 
3694
                if ( !saveRawUtf8 (
 
3695
                            wtfn.name(),
 
3696
                            buffer ) )
 
3697
                {
 
3698
                        messagePane (
 
3699
                            _ ( "Cannot save temporary copy for validation; please save or discard changes" ),
 
3700
                            CONST_STOP );
 
3701
                        return;
 
3702
                }
 
3703
                fname = wtfn.wideName();
 
3704
        }
 
3705
 
 
3706
        std::string fnameLocal = ( const char * ) fname.mb_str ( wxConvLocal );
 
3707
 
 
3708
        doc->clearErrorIndicators();
 
3709
        statusProgress ( _ ( "DTD Validation in progress..." ) );
 
3710
 
 
3711
        auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
 
3712
 
 
3713
        if ( !wl->validate ( fnameLocal ) )
 
3714
        {
 
3715
                std::string error = wl->getLastError();
 
3716
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
3717
                statusProgress ( wxEmptyString );
 
3718
                messagePane ( wideError, CONST_WARNING );
 
3719
 
 
3720
                std::pair<int, int> posPair = wl->getErrorPosition();
 
3721
                -- ( posPair.first );
 
3722
                int cursorPos =
 
3723
                    doc->PositionFromLine ( posPair.first );
 
3724
                doc->SetSelection ( cursorPos, cursorPos );
 
3725
 
 
3726
                // shallow validate all
 
3727
                doc->backgroundValidate(); // has to come first as it deletes all indicators
 
3728
                doc->setErrorIndicator ( posPair.first, posPair.second );
 
3729
 
 
3730
                return;
 
3731
        }
 
3732
        statusProgress ( wxEmptyString );
 
3733
        documentOk ( _ ( "valid" ) );
 
3734
}
 
3735
 
 
3736
void MyFrame::OnValidateRelaxNG ( wxCommandEvent& event )
 
3737
{
 
3738
        statusProgress ( wxEmptyString );
 
3739
 
 
3740
        XmlDoc *doc;
 
3741
        if ( ( doc = getActiveDocument() ) == NULL )
 
3742
                return;
 
3743
 
 
3744
        wxString fileName = doc->getFullFileName();
 
3745
 
 
3746
        wxString defaultFile, defaultDir;
 
3747
        defaultFile = doc->getFullFileName();
 
3748
        if ( !defaultFile.empty() )
 
3749
        {
 
3750
                wxFileName fn ( defaultFile );
 
3751
                defaultDir = fn.GetPath();
 
3752
        }
 
3753
        AssociateDialog ad (
 
3754
            this,
 
3755
            _ ( "Select RELAX NG grammar" ),
 
3756
            _ ( "Choose a file:" ),
 
3757
            _ ( "RELAX NG grammar" ),
 
3758
            _T ( "*.*" ),
 
3759
            lastRelaxNGSchema );
 
3760
        if ( ad.ShowModal() != wxID_OK )
 
3761
                return;
 
3762
 
 
3763
        wxString path = lastRelaxNGSchema = ad.getUrl();
 
3764
 
 
3765
        if ( path.empty() )
 
3766
        {
 
3767
                statusProgress ( wxEmptyString );
 
3768
                return;
 
3769
        }
 
3770
 
 
3771
        validateRelaxNG ( doc, path, fileName );
 
3772
}
 
3773
 
 
3774
void MyFrame::validateRelaxNG (
 
3775
    XmlDoc *doc,
 
3776
    const wxString& schemaName,
 
3777
    wxString& fileName ) // not const: may change if empty/document modified
 
3778
{
 
3779
        statusProgress ( wxEmptyString );
 
3780
 
 
3781
        if ( !doc )
 
3782
                return;
 
3783
 
 
3784
        WrapTempFileName wtfn ( fileName );
 
3785
        if ( fileName.empty() || doc->GetModify() )
 
3786
        {
 
3787
                wxString wideBuffer = doc->GetText();
 
3788
 
 
3789
                std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
 
3790
                if ( !saveRawUtf8 (
 
3791
                            wtfn.name(),
 
3792
                            buffer ) )
 
3793
                {
 
3794
                        messagePane (
 
3795
                            _ ( "Cannot save temporary copy for validation; please save or discard changes" ),
 
3796
                            CONST_STOP );
 
3797
                        return;
 
3798
                }
 
3799
                fileName = wtfn.wideName();
 
3800
        }
 
3801
 
 
3802
        doc->clearErrorIndicators();
 
3803
        statusProgress ( _ ( "RELAX NG validation in progress..." ) );
 
3804
 
 
3805
        auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
 
3806
 
 
3807
        std::string schemaFileNameLocal = ( const char * ) schemaName.mb_str ( wxConvLocal );
 
3808
        std::string fileNameLocal = ( const char * ) fileName.mb_str ( wxConvLocal );
 
3809
        if ( !wl->validateRelaxNG ( schemaFileNameLocal, fileNameLocal ) )
 
3810
        {
 
3811
                std::string error = wl->getLastError();
 
3812
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
3813
                statusProgress ( wxEmptyString );
 
3814
 
 
3815
                std::pair<int, int> posPair = wl->getErrorPosition();
 
3816
                -- ( posPair.first );
 
3817
 
 
3818
                int cursorPos =
 
3819
                    doc->PositionFromLine ( posPair.first );
 
3820
                doc->SetSelection ( cursorPos, cursorPos );
 
3821
                doc->setErrorIndicator ( posPair.first, posPair.second );
 
3822
                messagePane ( wideError, CONST_WARNING );
 
3823
                doc->SetFocus();
 
3824
                return;
 
3825
        }
 
3826
 
 
3827
        statusProgress ( wxEmptyString );
 
3828
        documentOk ( _ ( "valid" ) );
 
3829
        doc->SetFocus();
 
3830
}
 
3831
 
 
3832
void MyFrame::OnValidatePreset ( wxCommandEvent& event )
 
3833
{
 
3834
        XmlDoc *doc;
 
3835
        if ( ( doc = getActiveDocument() ) == NULL )
 
3836
                return;
 
3837
 
 
3838
        wxString fileName = doc->getFullFileName();
 
3839
 
 
3840
        int id = event.GetId();
 
3841
        wxString schemaFileName = validationPresetMap[id];
 
3842
        if ( schemaFileName.empty() )
 
3843
                return;
 
3844
        validateRelaxNG ( doc, schemaFileName, fileName );
 
3845
}
 
3846
 
 
3847
void MyFrame::OnValidateSchema ( wxCommandEvent& event )
 
3848
{
 
3849
        statusProgress ( wxEmptyString );
 
3850
 
 
3851
        XmlDoc *doc;
 
3852
        if ( ( doc = getActiveDocument() ) == NULL )
 
3853
                return;
 
3854
 
 
3855
        // branch: if no XML Schema found, use LibXML DTD parser instead
 
3856
        // so the catalog is read - switch when Xerces-C implements
 
3857
        // XMLCatalogResolver
 
3858
#ifdef __WXMSW__
 
3859
        {
 
3860
                std::string rawBuffer, schemaLocation;
 
3861
                getRawText ( doc, rawBuffer );
 
3862
                auto_ptr<XmlSchemaLocator> xsl ( new XmlSchemaLocator() );
 
3863
                xsl->parse ( rawBuffer.c_str() );
 
3864
                if ( ( xsl->getSchemaLocation() ) . empty() )
 
3865
                {
 
3866
                        OnValidateDTD ( event );
 
3867
                        return;
 
3868
                }
 
3869
        }
 
3870
#endif
 
3871
 
 
3872
        wxString fileName;
 
3873
        std::string tempFileNameLocal;
 
3874
        fileName = doc->getFullFileName();
 
3875
 
 
3876
        WrapTempFileName wtfn ( fileName );
 
3877
        if ( fileName.empty() || doc->GetModify() )
 
3878
        {
 
3879
                wxString wideBuffer = doc->GetText();
 
3880
 
 
3881
                std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
 
3882
                if ( !saveRawUtf8 (
 
3883
                            wtfn.name(),
 
3884
                            buffer ) )
 
3885
                {
 
3886
                        messagePane (
 
3887
                            _ ( "Cannot save temporary copy for validation; please save or discard changes" ),
 
3888
                            CONST_STOP );
 
3889
                        return;
 
3890
                }
 
3891
                fileName = wtfn.wideName();
 
3892
        }
 
3893
 
 
3894
        statusProgress ( _ ( "Validation in progress..." ) );
 
3895
        doc->clearErrorIndicators();
 
3896
 
 
3897
        std::string error;
 
3898
        wxString wideError;
 
3899
 
 
3900
        std::auto_ptr<WrapXerces> validator (
 
3901
                                        new WrapXerces( catalogPath, catalogUtilityPath )
 
3902
                              );
 
3903
        std::string fileNameLocal = ( const char * ) fileName.mb_str ( wxConvLocal );
 
3904
        if ( !validator->validate ( fileNameLocal ) )
 
3905
        {
 
3906
                statusProgress ( wxEmptyString );
 
3907
                error = validator->getLastError();
 
3908
                wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
3909
                messagePane ( wideError, CONST_WARNING );
 
3910
                std::pair<int, int> posPair = validator->getErrorPosition();
 
3911
 
 
3912
                int cursorPos =
 
3913
                    doc->PositionFromLine ( posPair.first - 1 );
 
3914
                doc->SetSelection ( cursorPos, cursorPos );
 
3915
                doc->setErrorIndicator ( posPair.first - 1, 0 );
 
3916
        }
 
3917
        else
 
3918
                documentOk ( _ ( "valid" ) );
 
3919
}
 
3920
 
 
3921
void MyFrame::OnXPath ( wxCommandEvent& event )
 
3922
{
 
3923
        statusProgress ( wxEmptyString );
 
3924
        closePane();
 
3925
 
 
3926
        XmlDoc *doc;
 
3927
        if ( ( doc = getActiveDocument() ) == NULL )
 
3928
                return;
 
3929
 
 
3930
        auto_ptr<wxTextEntryDialog> dlg ( new wxTextEntryDialog (
 
3931
                                              this,
 
3932
                                              _ ( "Enter XPath:" ),
 
3933
                                              _ ( "Evaluate XPath" ),
 
3934
                                              xpathExpression ) );
 
3935
 
 
3936
        int ret = dlg->ShowModal();
 
3937
        if ( ret == wxID_CANCEL )
 
3938
                return;
 
3939
        xpathExpression = dlg->GetValue();
 
3940
        std::string valUtf8 = ( const char * ) xpathExpression.mb_str ( wxConvUTF8 );
 
3941
 
 
3942
        // fetch document contents
 
3943
        std::string rawBufferUtf8;
 
3944
        getRawText ( doc, rawBufferUtf8 );
 
3945
        if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
 
3946
        {
 
3947
                encodingMessage();
 
3948
                return;
 
3949
        }
 
3950
 
 
3951
        WrapTempFileName tempFileName ( doc->getFullFileName() );
 
3952
 
 
3953
        ofstream rawBufferStream ( tempFileName.name().c_str() );
 
3954
        if ( !rawBufferStream )
 
3955
                return;
 
3956
        rawBufferStream << rawBufferUtf8;
 
3957
        rawBufferStream.close();
 
3958
 
 
3959
        auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
 
3960
 
 
3961
        bool success = wl->xpath ( valUtf8, tempFileName.name() );
 
3962
 
 
3963
        if ( !success )
 
3964
        {
 
3965
                std::string error = wl->getLastError();
 
3966
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
3967
                if ( !wideError.empty() )
 
3968
                        wideError.Prepend ( _T ( ": " ) );
 
3969
                wideError.Prepend ( _ ( "Cannot evaluate XPath" ) );
 
3970
 
 
3971
                messagePane ( wideError, CONST_WARNING );
 
3972
                return;
 
3973
        }
 
3974
 
 
3975
        statusProgress ( wxEmptyString );
 
3976
        std::string buffer = wl->getOutput();
 
3977
 
 
3978
        if ( buffer.empty() )
 
3979
        {
 
3980
                messagePane ( _ ( "No matching nodes found" ), CONST_WARNING );
 
3981
                return;
 
3982
        }
 
3983
        newDocument ( buffer );
 
3984
        statusProgress ( wxEmptyString );
 
3985
}
 
3986
 
 
3987
void MyFrame::OnXslt ( wxCommandEvent& event )
 
3988
{
 
3989
        statusProgress ( wxEmptyString );
 
3990
        closePane();
 
3991
 
 
3992
        // fetch document contents
 
3993
        XmlDoc *doc;
 
3994
        if ( ( doc = getActiveDocument() ) == NULL )
 
3995
                return;
 
3996
        std::string rawBufferUtf8;
 
3997
        getRawText ( doc, rawBufferUtf8 );
 
3998
        if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
 
3999
        {
 
4000
                encodingMessage();
 
4001
                return;
 
4002
        }
 
4003
 
 
4004
        WrapTempFileName tempFileName ( doc->getFullFileName() );
 
4005
 
 
4006
        ofstream rawBufferStream ( tempFileName.name().c_str() );
 
4007
        if ( !rawBufferStream )
 
4008
                return;
 
4009
        rawBufferStream << rawBufferUtf8;
 
4010
        rawBufferStream.close();
 
4011
 
 
4012
        wxString path;
 
4013
 
 
4014
        int id = event.GetId();
 
4015
        if ( id == ID_XSLT )
 
4016
        {
 
4017
                XslLocator xl;
 
4018
                xl.parse ( rawBufferUtf8 );
 
4019
                std::string location = xl.getXslLocation();
 
4020
 
 
4021
                path = wxString ( location.c_str(), wxConvUTF8, location.size() );
 
4022
                path.Replace ( _T ( "%20" ), _T ( " " ), true );
 
4023
 
 
4024
                path = PathResolver::run ( path, doc->getFullFileName() );
 
4025
 
 
4026
                if ( !wxFileName::FileExists ( path ) )
 
4027
                {
 
4028
                        if ( !path.empty() )
 
4029
                        {
 
4030
                                wxString message;
 
4031
                                message.Printf ( _ ( "Cannot open stylesheet %s" ), path.c_str() );
 
4032
                                messagePane ( message, CONST_WARNING );
 
4033
                        }
 
4034
 
 
4035
                        wxString defaultFile, defaultDir;
 
4036
                        defaultFile = doc->getFullFileName();
 
4037
                        if ( !defaultFile.empty() )
 
4038
                        {
 
4039
                                wxFileName fn ( defaultFile );
 
4040
                                defaultDir = fn.GetPath();
 
4041
                        }
 
4042
                        AssociateDialog ad (
 
4043
                            this,
 
4044
                            _ ( "Select stylesheet" ),
 
4045
                            _ ( "Choose a file:" ),
 
4046
                            _ ( "XSLT stylesheet" ),
 
4047
                            _T ( "*.xsl;*.xslt" ),
 
4048
                            lastXslStylesheet );
 
4049
                        if ( ad.ShowModal() != wxID_OK )
 
4050
                                return;
 
4051
 
 
4052
                        path = lastXslStylesheet = ad.getUrl();
 
4053
 
 
4054
 
 
4055
                        if ( path.empty() ) // Cancel selected
 
4056
                        {
 
4057
                                statusProgress ( wxEmptyString );
 
4058
                                return;
 
4059
                        }
 
4060
                }
 
4061
        }
 
4062
        else
 
4063
        {
 
4064
                wxString sep;
 
4065
                sep.Append ( wxFileName::GetPathSeparator() );
 
4066
                switch ( id )
 
4067
                {
 
4068
                        case ID_XSLT_TEI_FO:
 
4069
                                path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "fo" ) + sep +
 
4070
                                       _T ( "tei.xsl" );
 
4071
                                break;
 
4072
                        case ID_XSLT_TEI_HTML:
 
4073
                                path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "html" ) + sep +
 
4074
                                       _T ( "tei.xsl" );
 
4075
                                break;
 
4076
                        case ID_XSLT_TEI_XHTML:
 
4077
                                path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "xhtml" ) + sep +
 
4078
                                       _T ( "tei.xsl" );
 
4079
                                break;
 
4080
                        case ID_XSLT_TEI_LATEX:
 
4081
                                path = applicationDir + sep + _T ( "tei" ) + sep + _T ( "latex" ) + sep +
 
4082
                                       _T ( "tei.xsl" );
 
4083
                                break;
 
4084
                        case ID_XSLT_DOCBOOK_FO:
 
4085
                                path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "fo" ) + sep +
 
4086
                                       _T ( "docbook.xsl" );
 
4087
                                break;
 
4088
                        case ID_XSLT_DOCBOOK_HTML:
 
4089
                                path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "html" ) + sep +
 
4090
                                       _T ( "docbook.xsl" );
 
4091
                                break;
 
4092
                        case ID_XSLT_DOCBOOK_XHTML:
 
4093
                                path = applicationDir + sep + _T ( "docbook" ) + sep + _T ( "xhtml" ) + sep +
 
4094
                                       _T ( "docbook.xsl" );
 
4095
                                break;
 
4096
                        default:
 
4097
                                break;
 
4098
                }
 
4099
        }
 
4100
        statusProgress ( _ ( "XSL transformation in progress..." ) );
 
4101
 
 
4102
        std::string stylefnameLocal = ( const char * ) path.mb_str ( wxConvLocal );
 
4103
 
 
4104
        auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
 
4105
        if ( !wl->xslt ( stylefnameLocal, tempFileName.name() ) )
 
4106
        {
 
4107
                std::string error = wl->getLastError();
 
4108
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
4109
                wideError.Prepend ( _ ( "Cannot transform: " ) );
 
4110
                statusProgress ( wxEmptyString );
 
4111
                messagePane ( wideError, CONST_WARNING );
 
4112
                return;
 
4113
        }
 
4114
        std::string buffer = wl->getOutput();
 
4115
        if ( buffer.empty() )
 
4116
        {
 
4117
                messagePane ( _ ( "Output document empty" ), CONST_WARNING );
 
4118
                return;
 
4119
        }
 
4120
        statusProgress ( wxEmptyString );
 
4121
        newDocument ( buffer );
 
4122
}
 
4123
 
 
4124
void MyFrame::OnPrettyPrint ( wxCommandEvent& event )
 
4125
{
 
4126
        statusProgress ( wxEmptyString );
 
4127
        closePane();
 
4128
 
 
4129
        // fetch document contents
 
4130
        XmlDoc *doc;
 
4131
        if ( ( doc = getActiveDocument() ) == NULL )
 
4132
                return;
 
4133
 
 
4134
        int line = doc->LineFromPosition ( doc->GetCurrentPos() );
 
4135
 
 
4136
        std::string rawBufferUtf8;
 
4137
        getRawText ( doc, rawBufferUtf8 );
 
4138
 
 
4139
        std::string encoding = XmlEncodingHandler::get ( rawBufferUtf8 );
 
4140
 
 
4141
        if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8, true ) )
 
4142
        {
 
4143
                encodingMessage();
 
4144
                return;
 
4145
        }
 
4146
 
 
4147
        statusProgress ( _ ( "Pretty-printing in progress..." ) );
 
4148
 
 
4149
        for ( int i = 0; i < 2; i++ ) // perform two iterations
 
4150
        {
 
4151
                WrapTempFileName tempFileName ( doc->getFullFileName() );
 
4152
 
 
4153
                ofstream rawBufferStream ( tempFileName.name().c_str() );
 
4154
                if ( !rawBufferStream )
 
4155
                        return;
 
4156
                rawBufferStream << rawBufferUtf8;
 
4157
                rawBufferStream.close();
 
4158
 
 
4159
                auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess, catalogPath ) );
 
4160
                bool success = wl->parse ( tempFileName.name(), true );
 
4161
 
 
4162
                if ( !success )
 
4163
                {
 
4164
                        std::string error = wl->getLastError();
 
4165
                        wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
4166
                        wideError.Prepend ( _ ( "Cannot pretty-print: " ) );
 
4167
                        statusProgress ( wxEmptyString );
 
4168
                        messagePane ( wideError, CONST_WARNING );
 
4169
                        return;
 
4170
                }
 
4171
                rawBufferUtf8 = wl->getOutput();
 
4172
        }
 
4173
 
 
4174
        statusProgress ( wxEmptyString );
 
4175
        if ( rawBufferUtf8.empty() )
 
4176
                messagePane (
 
4177
                    _ ( "Pretty-print unsuccessful: output document empty" ),
 
4178
                    CONST_STOP );
 
4179
        else
 
4180
        {
 
4181
                /*
 
4182
                    if (encoding != "UTF-8")
 
4183
                    {
 
4184
                      std::string output = getEncodedBuffer(rawBufferUtf8, encoding);
 
4185
                      if (!output.empty())
 
4186
                        rawBufferUtf8 = output;
 
4187
                    }
 
4188
                */
 
4189
                if ( encoding != "UTF-8" && !encoding.empty() )
 
4190
                {
 
4191
                        XmlEncodingHandler::set ( rawBufferUtf8, encoding );
 
4192
                }
 
4193
                doc->SetTextRaw ( rawBufferUtf8.c_str() );
 
4194
                statusProgress ( wxEmptyString );
 
4195
        }
 
4196
 
 
4197
        doc->setValidationRequired ( true );
 
4198
        doc->GotoLine ( line );
 
4199
        doc->SetFocus();
 
4200
}
 
4201
 
 
4202
void MyFrame::OnEncoding ( wxCommandEvent& event )
 
4203
{
 
4204
        statusProgress ( wxEmptyString );
 
4205
        closePane();
 
4206
 
 
4207
        // fetch document contents
 
4208
        XmlDoc *doc;
 
4209
        if ( ( doc = getActiveDocument() ) == NULL )
 
4210
                return;
 
4211
 
 
4212
        std::vector<wxString> encodingVector;
 
4213
        encodingVector.push_back ( _T ( "UTF-8" ) );
 
4214
        encodingVector.push_back ( _T ( "UTF-16" ) );
 
4215
        encodingVector.push_back ( _T ( "UTF-16LE" ) );
 
4216
        encodingVector.push_back ( _T ( "UTF-16BE" ) );
 
4217
        encodingVector.push_back ( _T ( "ISO-8859-1" ) );
 
4218
        encodingVector.push_back ( _T ( "US-ASCII" ) );
 
4219
        const int vectorSize = encodingVector.size();
 
4220
        wxString choiceArray[vectorSize + 1];
 
4221
        for ( int i = 0; i < vectorSize; ++i )
 
4222
                * ( choiceArray + i ) = encodingVector.at ( i );
 
4223
        wxSingleChoiceDialog scd (
 
4224
            this, _ ( "Choose an encoding:" ), _ ( "Encoding" ), vectorSize, choiceArray );
 
4225
 
 
4226
        if ( scd.ShowModal() == wxID_CANCEL )
 
4227
                return;
 
4228
 
 
4229
        wxString selection;
 
4230
        std::string selectionUtf8, bufferUtf8;
 
4231
 
 
4232
        selection = scd.GetStringSelection();
 
4233
        selectionUtf8 = selection.mb_str ( wxConvUTF8 );
 
4234
 
 
4235
        getRawText ( doc, bufferUtf8 );
 
4236
        XmlEncodingHandler::setUtf8 ( bufferUtf8, true );
 
4237
 
 
4238
        WrapTempFileName tempFileName ( _T ( "" ) );
 
4239
 
 
4240
        std::auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
 
4241
        int res;
 
4242
 
 
4243
        WrapTempFileName sourceFileName ( doc->getFullFileName() );
 
4244
        saveRawUtf8 ( sourceFileName.name(), bufferUtf8 );
 
4245
 
 
4246
        res = wl->saveEncodingFromFile ( sourceFileName.name(), tempFileName.name(), selectionUtf8 );
 
4247
        if ( res == -1 )
 
4248
        {
 
4249
                std::string error = wl->getLastError();
 
4250
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
4251
                wideError.Prepend ( _ ( "Cannot set encoding: " ) );
 
4252
                messagePane ( wideError, CONST_STOP );
 
4253
                return;
 
4254
        }
 
4255
 
 
4256
        std::string newBuffer;
 
4257
        bool success = ReadFile::run ( tempFileName.name(), newBuffer );
 
4258
        if ( !success )
 
4259
        {
 
4260
                messagePane ( _ ( "Cannot set encoding (cannot open temporary file)" ),
 
4261
                              CONST_STOP );
 
4262
                return;
 
4263
        }
 
4264
 
 
4265
        std::auto_ptr<XmlUtf8Reader> xur ( new XmlUtf8Reader (
 
4266
                                               false,
 
4267
                                               expandInternalEntities,
 
4268
                                               newBuffer.size() ) );
 
4269
        if ( !xur->parse ( newBuffer ) )
 
4270
        {
 
4271
                messagePane ( _ ( "Cannot set encoding (cannot parse temporary file)" ),
 
4272
                              CONST_STOP );
 
4273
                return;
 
4274
        }
 
4275
 
 
4276
        doc->SetTextRaw ( xur->getBuffer().c_str() );
 
4277
        doc->setValidationRequired ( true );
 
4278
        doc->SetFocus();
 
4279
}
 
4280
 
 
4281
void MyFrame::OnHome ( wxCommandEvent& event )
 
4282
{
 
4283
        navigate ( _T ( "http://xml-copy-editor.sourceforge.net" ) );
 
4284
}
 
4285
 
 
4286
void MyFrame::OnDownloadSource ( wxCommandEvent& event )
 
4287
{
 
4288
        navigate ( _T ( "http://xml-copy-editor.svn.sourceforge.net/viewvc/xml-copy-editor/" ) );
 
4289
}
 
4290
 
 
4291
void MyFrame::OnToolbarVisible ( wxCommandEvent& event )
 
4292
{
 
4293
        if ( !viewMenu )
 
4294
                return;
 
4295
        toolbarVisible = ( toolbarVisible ) ? false : true;
 
4296
        viewMenu->Check ( ID_TOOLBAR_VISIBLE, toolbarVisible );
 
4297
        showTopBars ( toolbarVisible );
 
4298
        manager.Update();
 
4299
}
 
4300
 
 
4301
void MyFrame::OnWrapWords ( wxCommandEvent& event )
 
4302
{
 
4303
        if ( !viewMenu )
 
4304
                return;
 
4305
 
 
4306
        bool wrapWords;
 
4307
        wrapWords = ( properties.wrap ) ? false : true;
 
4308
 
 
4309
        viewMenu->Check ( ID_WRAP_WORDS, wrapWords );
 
4310
        properties.wrap = wrapWords;
 
4311
 
 
4312
        // update all documents
 
4313
        int pageCount = mainBook->GetPageCount();
 
4314
        XmlDoc *currentDoc;
 
4315
        for ( int i = 0; i < pageCount; ++i )
 
4316
        {
 
4317
                currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
 
4318
                if ( !currentDoc )
 
4319
                        break;
 
4320
                currentDoc->SetWrapMode ( wrapWords );
 
4321
        }
 
4322
}
 
4323
 
 
4324
void MyFrame::OnLocationPaneVisible ( wxCommandEvent& event )
 
4325
{
 
4326
        wxAuiPaneInfo info = manager.GetPane ( locationPanel );
 
4327
        bool visible = ( info.IsShown() ) ? false : true;
 
4328
        manager.GetPane ( locationPanel ).Show ( visible );
 
4329
        manager.Update();
 
4330
 
 
4331
        XmlDoc *doc;
 
4332
        if ( ( doc = getActiveDocument() ) == NULL )
 
4333
                return;
 
4334
        doc->SetFocus();
 
4335
}
 
4336
 
 
4337
void MyFrame::OnProtectTags ( wxCommandEvent& event )
 
4338
{
 
4339
        if ( !xmlMenu )
 
4340
                return;
 
4341
        protectTags = ( protectTags ) ? false : true;
 
4342
        if ( xmlMenu )
 
4343
                xmlMenu->Check ( ID_PROTECT_TAGS, protectTags );
 
4344
        if ( toolBar )
 
4345
                toolBar->ToggleTool ( ID_PROTECT_TAGS, protectTags );
 
4346
 
 
4347
        XmlDoc *doc;
 
4348
        if ( ( doc = getActiveDocument() ) == NULL )
 
4349
                return;
 
4350
 
 
4351
        if ( protectTags )
 
4352
                doc->adjustCursor(); // apply to all open docs?
 
4353
 
 
4354
        doc->SetFocus();
 
4355
}
 
4356
 
 
4357
void MyFrame::OnVisibilityState ( wxCommandEvent& event )
 
4358
{
 
4359
 
 
4360
        int id;
 
4361
        id = event.GetId();
 
4362
        switch ( id )
 
4363
        {
 
4364
                case ID_SHOW_TAGS:
 
4365
                        visibilityState = SHOW_TAGS;
 
4366
                        //GetStatusBar()->SetStatusText(wxEmptyString, STATUS_PARENT);
 
4367
                        break;
 
4368
                case ID_HIDE_ATTRIBUTES:
 
4369
                        visibilityState = HIDE_ATTRIBUTES;
 
4370
                        //GetStatusBar()->SetStatusText(wxEmptyString, STATUS_PARENT);
 
4371
                        break;
 
4372
                case ID_HIDE_TAGS:
 
4373
                        visibilityState = HIDE_TAGS;
 
4374
                        break;
 
4375
                default:
 
4376
                        visibilityState = SHOW_TAGS;
 
4377
                        break;
 
4378
        }
 
4379
        if ( viewMenu )
 
4380
                viewMenu->Check ( id, true );
 
4381
 
 
4382
        // iterate over all open documents
 
4383
        int pageCount = mainBook->GetPageCount();
 
4384
        XmlDoc *currentDoc;
 
4385
        for ( int i = 0; i < pageCount; ++i )
 
4386
        {
 
4387
                currentDoc = ( XmlDoc * ) mainBook->GetPage ( i );
 
4388
                if ( !currentDoc )
 
4389
                        break;
 
4390
                currentDoc->applyVisibilityState ( visibilityState );
 
4391
        }
 
4392
 
 
4393
        if ( visibilityState == HIDE_ATTRIBUTES || visibilityState == HIDE_TAGS )
 
4394
        {
 
4395
                if ( properties.protectHiddenElements && !protectTags )
 
4396
                {
 
4397
                        wxCommandEvent e;
 
4398
                        OnProtectTags ( e );
 
4399
                }
 
4400
        }
 
4401
 
 
4402
        // fetch current document
 
4403
        XmlDoc *doc;
 
4404
        if ( ( doc = getActiveDocument() ) == NULL )
 
4405
                return;
 
4406
 
 
4407
        // set focus for current document
 
4408
        doc->SetFocus();
 
4409
}
 
4410
 
 
4411
void MyFrame::OnFeedback ( wxCommandEvent& event )
 
4412
{
 
4413
        wxString forumUrl =
 
4414
            _T ( "https://sourceforge.net/forum/forum.php?forum_id=475215" );
 
4415
        navigate ( forumUrl );
 
4416
}
 
4417
 
 
4418
void MyFrame::navigate ( const wxString& url )
 
4419
{
 
4420
        wxString testString = browserCommand;
 
4421
        testString.Replace ( _T ( " -remote" ), wxEmptyString, true );
 
4422
        if ( browserCommand.empty() )
 
4423
        {
 
4424
                messagePane (
 
4425
                    _ ( "Cannot open in browser: no browser defined (see Tools, Options..., General)" ),
 
4426
                    CONST_STOP );
 
4427
                return;
 
4428
        }
 
4429
        else if ( !wxFileName::FileExists ( testString ) )
 
4430
        {
 
4431
                wxString message;
 
4432
                message.Printf (
 
4433
                    _ ( "Cannot open in browser: %s not found (see Tools, Options..., General)" ),
 
4434
                    testString.c_str() );
 
4435
                messagePane ( message, CONST_STOP );
 
4436
                return;
 
4437
        }
 
4438
        wxString cmd;
 
4439
        cmd = browserCommand + _T ( " \"" ) + url + _T ( "\"" );
 
4440
        wxExecute ( cmd, wxEXEC_ASYNC ); // make ASYNC an option?
 
4441
}
 
4442
 
 
4443
void MyFrame::findAgain ( wxString s, int flags )
 
4444
{
 
4445
        findReplacePanel->flagNotFound ( false );
 
4446
 
 
4447
        if ( s.empty() )
 
4448
                return;
 
4449
 
 
4450
        statusProgress ( wxEmptyString );
 
4451
        XmlDoc *doc;
 
4452
        if ( ( doc = getActiveDocument() ) == NULL )
 
4453
                return;
 
4454
 
 
4455
        // update regex parameter to keep global replace in sync
 
4456
        findRegex = findReplacePanel->getRegex();
 
4457
 
 
4458
        int newLocation;
 
4459
        int myFlags = 0;
 
4460
        if ( flags & wxFR_WHOLEWORD )
 
4461
                myFlags |= wxSTC_FIND_WHOLEWORD;
 
4462
        if ( flags & wxFR_MATCHCASE )
 
4463
                myFlags |= wxSTC_FIND_MATCHCASE;
 
4464
        if ( findReplacePanel->getRegex() )
 
4465
                myFlags |= wxSTC_FIND_REGEXP;
 
4466
 
 
4467
        bool incrementalFind =
 
4468
            ( findReplacePanel->getIncrementalFind() ) ? true : false;
 
4469
 
 
4470
        //doc->SetYCaretPolicy(wxSTC_CARET_SLOP | wxSTC_CARET_STRICT, 10);
 
4471
 
 
4472
        if ( flags & wxFR_DOWN ) // find next
 
4473
        {
 
4474
                doc->SetTargetStart ( ( incrementalFind ) ?
 
4475
                                      doc->GetSelectionStart() : doc->GetSelectionEnd() );
 
4476
                doc->SetTargetEnd ( doc->GetLength() );
 
4477
                doc->SetSearchFlags ( myFlags );
 
4478
                newLocation = doc->SearchInTarget ( s );
 
4479
 
 
4480
                // try once more from top
 
4481
                if ( newLocation == -1 )
 
4482
                {
 
4483
                        doc->SetTargetStart ( 0 );
 
4484
                        doc->SetTargetEnd ( doc->GetLength() );
 
4485
                        newLocation = doc->SearchInTarget ( s );
 
4486
                }
 
4487
        }
 
4488
        else // find previous
 
4489
        {
 
4490
                doc->SetCurrentPos (
 
4491
                    ( doc->GetSelectionStart() ) ? doc->GetSelectionStart() - 1 : 0 );
 
4492
                doc->SearchAnchor();
 
4493
                newLocation = doc->SearchPrev ( myFlags, s );
 
4494
        }
 
4495
 
 
4496
        //doc->SetYCaretPolicy(wxSTC_CARET_SLOP, 0);
 
4497
 
 
4498
 
 
4499
        if ( newLocation == -1 )
 
4500
        {
 
4501
                findReplacePanel->flagNotFound ( true );
 
4502
                wxString err;
 
4503
                err.Printf ( _ ( "Cannot find '%s'" ), s.c_str() );
 
4504
                doc->SetSelectionEnd ( doc->GetSelectionStart() );
 
4505
 
 
4506
                statusProgress ( err );
 
4507
 
 
4508
                // must clear target to prevent replace affecting whole document
 
4509
                doc->SetTargetStart ( 0 );
 
4510
                doc->SetTargetEnd ( 0 );
 
4511
 
 
4512
                return;
 
4513
        }
 
4514
 
 
4515
        doc->SetSelection ( doc->GetTargetStart(), doc->GetTargetEnd() );
 
4516
        doc->EnsureCaretVisible();
 
4517
}
 
4518
 
 
4519
bool MyFrame::closeActiveDocument()
 
4520
{
 
4521
        statusProgress ( wxEmptyString );
 
4522
        closePane();
 
4523
 
 
4524
        int selection = mainBook->GetSelection();
 
4525
        if ( selection == -1 || !mainBook->GetPageCount() ) // GetPageCount needed for wxAuiNotebook
 
4526
                return false;
 
4527
 
 
4528
        locationPanel->update ( NULL, wxEmptyString );
 
4529
        insertChildPanel->update ( NULL, wxEmptyString );
 
4530
        insertSiblingPanel->update ( NULL, wxEmptyString );
 
4531
 
 
4532
            // workaround -- wxAuiNotebook: send virtual close event? DeletePage doesn't generate one
 
4533
            wxAuiNotebookEvent e;
 
4534
            e.SetSelection ( selection );
 
4535
            OnPageClosing ( e );
 
4536
            if ( deletePageVetoed )
 
4537
                return false;
 
4538
 
 
4539
            mainBook->DeletePage ( selection ); // check this is still correct
 
4540
            return true;
 
4541
 
 
4542
        // apparently fixed betw. 2.8.0 and 2.8.6, so from v. 1.1.0.3, BUT this is once more:
 
4543
        // 1.1.0.5: reverted to above workaround - no confirmation dialog?
 
4544
        // watch memory usage for DeletePage call
 
4545
        //mainBook->DeletePage ( selection );
 
4546
        //return ( !deletePageVetoed );
 
4547
}
 
4548
 
 
4549
bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified )
 
4550
{
 
4551
        if ( !doc )
 
4552
                return false;
 
4553
 
 
4554
        statusProgress ( wxEmptyString );
 
4555
 
 
4556
        if ( checkLastModified )
 
4557
        {
 
4558
                wxFileName fn ( fileName );
 
4559
                if ( fn.IsOk() )
 
4560
                {
 
4561
                        wxDateTime myLastModified = fn.GetModificationTime();
 
4562
                        if ( !myLastModified.IsEqualTo ( doc->getLastModified() ) )
 
4563
                        {
 
4564
                                int choice = wxMessageBox (
 
4565
                                                 _ ( "File has been modified by another application.\nDo you want to proceed?" ),
 
4566
                                                 _ ( "Confirmation" ),
 
4567
                                                 wxICON_QUESTION | wxYES_NO | wxCANCEL );
 
4568
                                if ( choice != wxYES )
 
4569
                                        return false;
 
4570
                        }
 
4571
                }
 
4572
        }
 
4573
 
 
4574
        int bytes = 0;
 
4575
        std::string utf8Buffer, encoding, fileNameLocal;
 
4576
        bool isXml = true;
 
4577
        try
 
4578
        {
 
4579
                getRawText ( doc, utf8Buffer );
 
4580
                XmlEncodingSpy es;
 
4581
                es.parse ( utf8Buffer );
 
4582
                encoding = es.getEncoding();
 
4583
 
 
4584
                fileNameLocal = fileName.mb_str ( wxConvLocal );
 
4585
 
 
4586
                closePane();
 
4587
                bool success;
 
4588
                success = true;
 
4589
                if ( getFileType ( fileName ) != FILE_TYPE_XML )
 
4590
                {
 
4591
                        isXml = false;
 
4592
                }
 
4593
 
 
4594
                // raw file conditions
 
4595
                if ( doc->getType() == FILE_TYPE_BINARY )
 
4596
                {
 
4597
                        success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
 
4598
                        if ( success )
 
4599
                                bytes = utf8Buffer.size();
 
4600
                        else
 
4601
                        {
 
4602
                                wxString message;
 
4603
                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4604
                                messagePane ( message, CONST_STOP );
 
4605
                                return false;
 
4606
                        }
 
4607
 
 
4608
                }
 
4609
                else if ( !isXml && encoding.empty() )
 
4610
                {
 
4611
                        success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
 
4612
                        if ( success )
 
4613
                                bytes = utf8Buffer.size();
 
4614
                        else
 
4615
                        {
 
4616
                                wxString message;
 
4617
                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4618
                                messagePane ( message, CONST_STOP );
 
4619
                                return false;
 
4620
                        }
 
4621
                }
 
4622
                else if ( encoding == "UTF-8" )
 
4623
                {
 
4624
                        auto_ptr<WrapExpat> we ( new WrapExpat() );
 
4625
 
 
4626
                        if ( !we->parse ( utf8Buffer ) )
 
4627
                        {
 
4628
                                std::string error = we->getLastError();
 
4629
                                wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
4630
                                if ( we->isEncodingError() )
 
4631
                                        ;
 
4632
                                messagePane ( werror, CONST_WARNING );
 
4633
                        }
 
4634
                        success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
 
4635
                        if ( success )
 
4636
                                bytes = utf8Buffer.size();
 
4637
                        else
 
4638
                        {
 
4639
                                wxString message;
 
4640
                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4641
                                messagePane ( message, CONST_STOP );
 
4642
                                return false;
 
4643
                        }
 
4644
                }
 
4645
                else
 
4646
                {
 
4647
                        wxString wideEncoding = wxString ( encoding.c_str(), wxConvLocal, encoding.size() );
 
4648
 
 
4649
                        // IF Unicode, use iconv to convert buffer
 
4650
                        if ( encoding == "UTF-16" || encoding == "UTF-16LE" ||
 
4651
                                encoding == "UTF-16BE" || encoding == "UTF-32" || encoding == "UTF-32LE" ||
 
4652
                                encoding == "UTF-32BE" )
 
4653
                        {
 
4654
                                iconv_t cd = iconv_open ( encoding.c_str(), "UTF-8" );
 
4655
                                if ( cd == ( iconv_t )-1 )
 
4656
                                {
 
4657
                                        success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
 
4658
                                        if ( success )
 
4659
                                        {
 
4660
                                                bytes = utf8Buffer.size();
 
4661
                                                wxString message;
 
4662
                                                message.Printf (
 
4663
                                                    _ ( "%s saved in default encoding UTF-8: unknown encoding %s" ),
 
4664
                                                    fileName.c_str(),
 
4665
                                                    wideEncoding.c_str() );
 
4666
                                                messagePane ( message, CONST_WARNING );
 
4667
                                        }
 
4668
                                        else
 
4669
                                        {
 
4670
                                                wxString message;
 
4671
                                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4672
                                                messagePane ( message, CONST_STOP );
 
4673
                                                return false;
 
4674
                                        }
 
4675
                                }
 
4676
                                else
 
4677
                                {
 
4678
                                        size_t utf8BufferLen = utf8Buffer.size();
 
4679
 
 
4680
                                        size_t iconvBufferLen, iconvBufferLeft, utf8BufferLeft;
 
4681
                                        int iconvLenMultiplier = 4; // worst case scenario
 
4682
                                        if ( encoding == "UTF-16" ||
 
4683
                                                encoding == "UTF-16BE" ||
 
4684
                                                encoding == "UTF-16LE" )
 
4685
                                        {
 
4686
                                                iconvLenMultiplier = 2;
 
4687
                                        }
 
4688
                                        else if ( encoding == "UTF-32" ||
 
4689
                                                  encoding == "UTF-32BE" ||
 
4690
                                                  encoding == "UTF-32LE" )
 
4691
                                        {
 
4692
                                                iconvLenMultiplier = 4;
 
4693
                                        }
 
4694
 
 
4695
                                        iconvBufferLen = iconvBufferLeft =
 
4696
                                                             utf8BufferLen * iconvLenMultiplier + 4; // worst case scenario
 
4697
 
 
4698
                                        char *finalBuffer;
 
4699
                                        char *iconvBuffer = new char[iconvBufferLen];
 
4700
 
 
4701
                                        utf8BufferLeft = utf8BufferLen;
 
4702
                                        iconvBuffer = new char[iconvBufferLen];
 
4703
                                        finalBuffer = iconvBuffer; // iconvBuffer will be incremented by iconv
 
4704
                                        size_t nconv;
 
4705
 
 
4706
#ifdef __WXMSW__
 
4707
                                        const char *
 
4708
#else
 
4709
                                        char *
 
4710
#endif
 
4711
                                        utf8BufferPtr =
 
4712
                                            ( char * )
 
4713
                                            utf8Buffer.c_str();
 
4714
 
 
4715
                                        nconv = iconv (
 
4716
                                                    cd,
 
4717
#ifdef __WXMSW__
 
4718
                                                    ( const char ** )
 
4719
#endif
 
4720
                                                    &utf8BufferPtr,
 
4721
                                                    &utf8BufferLeft,
 
4722
                                                    &iconvBuffer,
 
4723
                                                    &iconvBufferLeft );
 
4724
 
 
4725
                                        iconv_close ( cd );
 
4726
 
 
4727
                                        if ( nconv == ( size_t )-1 ) // conversion failed
 
4728
                                        {
 
4729
                                                delete[] finalBuffer;
 
4730
                                                success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
 
4731
                                                if ( success )
 
4732
                                                {
 
4733
                                                        bytes = utf8Buffer.size();
 
4734
                                                        wxString message;
 
4735
                                                        message.Printf (
 
4736
                                                            _ ( "%s saved in default encoding UTF-8: conversion to %s failed" ),
 
4737
                                                            fileName.c_str(),
 
4738
                                                            wideEncoding.c_str() );
 
4739
                                                        messagePane ( message, CONST_WARNING );
 
4740
                                                }
 
4741
                                                else
 
4742
                                                {
 
4743
                                                        wxString message;
 
4744
                                                        message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4745
                                                        messagePane ( message, CONST_STOP );
 
4746
                                                        return false;
 
4747
                                                }
 
4748
                                        }
 
4749
                                        else
 
4750
                                        {
 
4751
                                                size_t finalBufferLen = iconvBufferLen - iconvBufferLeft;
 
4752
 
 
4753
                                                std::ofstream ofs ( fileNameLocal.c_str(), std::ios::out | std::ios::binary );
 
4754
                                                if ( !ofs )
 
4755
                                                {
 
4756
                                                        delete[] finalBuffer;
 
4757
                                                        wxString message;
 
4758
                                                        message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4759
                                                        messagePane ( message, CONST_STOP );
 
4760
                                                        return false;
 
4761
                                                }
 
4762
 
 
4763
                                                // iconv adds boms for UTF-16 & UTF-32 automatically
 
4764
 
 
4765
                                                ofs.write ( finalBuffer, finalBufferLen );
 
4766
                                                ofs.close();
 
4767
                                                delete[] finalBuffer;
 
4768
                                                bytes = finalBufferLen;
 
4769
                                        }
 
4770
                                }
 
4771
                        }
 
4772
                        else // all other encodings handled by Libxml
 
4773
                        {
 
4774
                                XmlEncodingHandler::setUtf8 ( utf8Buffer );
 
4775
                                auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
 
4776
 
 
4777
                                WrapTempFileName sourceFileName ( fileName );
 
4778
                                saveRawUtf8 ( sourceFileName.name(), utf8Buffer );
 
4779
                                int result = wl->saveEncodingFromFile ( sourceFileName.name(), fileNameLocal, encoding );
 
4780
                                if ( result == -1 )
 
4781
                                {
 
4782
                                        success = saveRawUtf8 ( fileNameLocal, utf8Buffer, false, isXml );
 
4783
                                        if ( success )
 
4784
                                        {
 
4785
                                                std::string libxmlError = wl->getLastError();
 
4786
                                                bytes = utf8Buffer.size();
 
4787
                                                wxString msg, wideEncoding, wideError;
 
4788
                                                wideEncoding =
 
4789
                                                    wxString ( encoding.c_str(), wxConvUTF8, encoding.size() );
 
4790
                                                wideError = wxString ( libxmlError.c_str(), wxConvUTF8, libxmlError.size() );
 
4791
                                                if ( wideError.empty() )
 
4792
                                                        wideError = _ ( "unknown error" );
 
4793
 
 
4794
                                                msg.Printf ( _ ( "Cannot save document in %s: %s (saved in default encoding UTF-8)" ),
 
4795
                                                             wideEncoding.c_str(), wideError.c_str() );
 
4796
                                                messagePane ( msg, CONST_INFO );
 
4797
                                        }
 
4798
                                        else
 
4799
                                        {
 
4800
                                                wxString message;
 
4801
                                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4802
                                                messagePane ( message, CONST_STOP );
 
4803
                                                return false;
 
4804
                                        }
 
4805
                                }
 
4806
                                else
 
4807
                                        bytes = result;
 
4808
                        }
 
4809
                }
 
4810
        } // try
 
4811
        catch ( std::bad_alloc& )
 
4812
        {
 
4813
                if ( encoding != "UTF-8" )
 
4814
                {
 
4815
                        int answer = wxMessageBox (
 
4816
                                         _ ( "Out of memory: attempt to save in default encoding UTF-8?" ),
 
4817
                                         _ ( "Out of memory" ),
 
4818
                                         wxYES_NO | wxCANCEL | wxICON_QUESTION,
 
4819
                                         this );
 
4820
                        if ( answer == wxCANCEL || answer == wxNO )
 
4821
                                return false;
 
4822
 
 
4823
                        bool success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
 
4824
                        if ( success )
 
4825
                        {
 
4826
                                bytes = utf8Buffer.size();
 
4827
                                wxString message;
 
4828
                                message.Printf (
 
4829
                                    _ ( "%s saved in default encoding UTF-8" ),
 
4830
                                    fileName.c_str() );
 
4831
                                messagePane ( message, CONST_INFO );
 
4832
                        }
 
4833
                        else
 
4834
                        {
 
4835
                                wxString message;
 
4836
                                message.Printf ( _ ( "Cannot save %s" ), fileName.c_str() );
 
4837
                                messagePane ( message, CONST_STOP );
 
4838
                                return false;
 
4839
                        }
 
4840
                }
 
4841
        }
 
4842
 
 
4843
        doc->SetFocus();
 
4844
        doc->SetSavePoint();
 
4845
 
 
4846
        if ( properties.validateAsYouType && isXml )
 
4847
        {
 
4848
                doc->clearErrorIndicators();
 
4849
                //doc->backgroundValidate ( utf8Buffer.c_str(), doc->getFullFileName().mb_str(wxConvUTF8), utf8Buffer.size() );
 
4850
                doc->backgroundValidate();
 
4851
        }
 
4852
 
 
4853
        if ( !unlimitedUndo )
 
4854
                doc->EmptyUndoBuffer();
 
4855
        wxFileName fn ( fileName );
 
4856
        if ( fn.IsOk() )
 
4857
                doc->setLastModified ( fn.GetModificationTime() );
 
4858
        openFileSet.insert ( fileName );
 
4859
        displaySavedStatus ( bytes );
 
4860
        return true;
 
4861
}
 
4862
 
 
4863
bool MyFrame::saveRawUtf8 (
 
4864
    const std::string& fileNameLocal,
 
4865
    std::string& bufferUtf8,
 
4866
    bool ignoreEncoding,
 
4867
    bool isXml )
 
4868
{
 
4869
        ofstream ofs ( fileNameLocal.c_str(), std::ios::out | std::ios::binary );
 
4870
        if ( !ofs )
 
4871
                return false;
 
4872
 
 
4873
        if ( !ignoreEncoding && isXml )
 
4874
                XmlEncodingHandler::setUtf8 ( bufferUtf8, true );
 
4875
 
 
4876
        if ( saveBom && isXml )
 
4877
        {
 
4878
                char bom[4];
 
4879
                bom[0] = 0xEF;
 
4880
                bom[1] = 0xBB;
 
4881
                bom[2] = 0xBF;
 
4882
                bom[3] = 0;
 
4883
                ofs.write ( bom, 3 );
 
4884
        }
 
4885
        ofs.write ( bufferUtf8.c_str(), bufferUtf8.size() );
 
4886
        ofs.close();
 
4887
        return true;
 
4888
}
 
4889
 
 
4890
void MyFrame::displaySavedStatus ( int bytes )
 
4891
{
 
4892
        wxString unit;
 
4893
        float result = 0;
 
4894
        if ( bytes > 1000000 )
 
4895
        {
 
4896
                result = bytes / 1000000;
 
4897
                unit = _ ( "MB" );
 
4898
        }
 
4899
        else if ( bytes > 1000 )
 
4900
        {
 
4901
                result = bytes / 1000;
 
4902
                unit = _ ( "kB" );
 
4903
        }
 
4904
        else if ( bytes >= 0 )
 
4905
        {
 
4906
                result = bytes;
 
4907
                unit = ngettext ( L"byte", L"bytes", bytes );
 
4908
        }
 
4909
        else
 
4910
                return;
 
4911
 
 
4912
        wxString msg;
 
4913
 
 
4914
        msg.Printf (
 
4915
            _ ( "%g %s saved" ),
 
4916
            result,
 
4917
            unit.c_str() );
 
4918
        statusProgress ( msg );
 
4919
}
 
4920
 
 
4921
bool MyFrame::getHandleCommandLineFlag()
 
4922
{
 
4923
        return handleCommandLineFlag;
 
4924
}
 
4925
 
 
4926
wxMenuBar *MyFrame::getMenuBar()
 
4927
{
 
4928
        fileMenu = new wxMenu; // use class-wide data member
 
4929
        updateFileMenu ( false );
 
4930
 
 
4931
        // edit menu
 
4932
        wxMenu *editMenu = new wxMenu;
 
4933
 
 
4934
        wxMenuItem *undoItem =
 
4935
            new wxMenuItem ( NULL, wxID_UNDO, _ ( "&Undo\tCtrl+Z" ), _ ( "Undo" ) );
 
4936
        undoItem->SetBitmap ( undo16Bitmap );
 
4937
 
 
4938
        wxMenuItem *redoItem =
 
4939
            new wxMenuItem ( NULL, wxID_REDO, _ ( "&Redo\tCtrl+Y" ), _ ( "Redo" ) );
 
4940
        redoItem->SetBitmap ( redo16Bitmap );
 
4941
 
 
4942
        wxMenuItem *cutItem =
 
4943
            new wxMenuItem ( NULL, wxID_CUT, _ ( "&Cut\tCtrl+X" ), _ ( "Cut" ) );
 
4944
        cutItem->SetBitmap ( cutBitmap );
 
4945
 
 
4946
        wxMenuItem *copyItem =
 
4947
            new wxMenuItem ( NULL, wxID_COPY, _ ( "C&opy\tCtrl+C" ), _ ( "Copy" ) );
 
4948
        copyItem->SetBitmap ( copyBitmap );
 
4949
 
 
4950
        wxMenuItem *pasteItem =
 
4951
            new wxMenuItem ( NULL, wxID_PASTE, _ ( "&Paste\tCtrl+V" ), _ ( "Paste" ) );
 
4952
        pasteItem->SetBitmap ( pasteBitmap );
 
4953
 
 
4954
        wxMenuItem *pasteNewDocumentItem =
 
4955
            new wxMenuItem (
 
4956
            NULL,
 
4957
            ID_PASTE_NEW_DOCUMENT,
 
4958
            _ ( "P&aste As New Document" ),
 
4959
            _ ( "Paste As New Document" ) );
 
4960
        pasteNewDocumentItem->SetBitmap ( wxNullBitmap );
 
4961
 
 
4962
        wxMenuItem *findItem =
 
4963
            new wxMenuItem ( NULL, ID_FIND, _ ( "&Find...\tCtrl+F" ), _ ( "Find..." ) );
 
4964
        findItem->SetBitmap ( searchBitmap );
 
4965
 
 
4966
        wxMenuItem *findAgainItem =
 
4967
            new wxMenuItem ( NULL, ID_FIND_AGAIN, _ ( "F&ind Again\tF3" ), _ ( "Find Again" ) );
 
4968
        findAgainItem->SetBitmap ( wxNullBitmap );
 
4969
 
 
4970
        wxMenuItem *replaceItem =
 
4971
            new wxMenuItem ( NULL, ID_REPLACE, _ ( "&Replace...\tCtrl+R" ), _ ( "Replace..." ) );
 
4972
        replaceItem->SetBitmap ( wxNullBitmap );
 
4973
 
 
4974
        wxMenuItem *globalReplaceItem =
 
4975
            new wxMenuItem (
 
4976
            NULL,
 
4977
            ID_GLOBAL_REPLACE,
 
4978
            _ ( "&Global Replace...\tCtrl+Shift+R" ),
 
4979
            _ ( "Global Replace..." ) );
 
4980
        globalReplaceItem->SetBitmap ( wxNullBitmap );
 
4981
 
 
4982
        wxMenuItem *gotoItem =
 
4983
            new wxMenuItem ( NULL, ID_GOTO, _ ( "G&o To...\tCtrl+G" ), _ ( "Go To..." ) );
 
4984
        gotoItem->SetBitmap ( wxNullBitmap );
 
4985
 
 
4986
        editMenu->Append ( undoItem );
 
4987
        editMenu->Append ( redoItem );
 
4988
        editMenu->AppendSeparator();
 
4989
        editMenu->Append ( cutItem );
 
4990
        editMenu->Append ( copyItem );
 
4991
        editMenu->Append ( pasteItem );
 
4992
        editMenu->Append ( pasteNewDocumentItem );
 
4993
        editMenu->AppendSeparator();
 
4994
        editMenu->Append ( findItem );
 
4995
        editMenu->Append ( findAgainItem );
 
4996
        editMenu->Append ( replaceItem );
 
4997
        editMenu->Append ( globalReplaceItem );
 
4998
        editMenu->AppendSeparator();
 
4999
        editMenu->Append ( gotoItem );
 
5000
 
 
5001
#ifndef __WXMSW__
 
5002
        wxMenuItem *preferencesItem =
 
5003
            new wxMenuItem ( NULL, ID_OPTIONS, _ ( "Pr&eferences..." ), _ ( "Preferences..." ) );
 
5004
        editMenu->AppendSeparator();
 
5005
        editMenu->Append ( preferencesItem );
 
5006
#endif
 
5007
 
 
5008
        // font size menu
 
5009
        wxMenu *fontSizeMenu = new wxMenu;
 
5010
        fontSizeMenu->Append (
 
5011
            ID_FONT_LARGER, _ ( "Increase\tCtrl+U" ), _ ( "Increase" ) );
 
5012
        fontSizeMenu->Append (
 
5013
            ID_FONT_SMALLER, _ ( "Decrease\tCtrl+D" ), _ ( "Decrease" ) );
 
5014
        fontSizeMenu->AppendSeparator();
 
5015
        fontSizeMenu->Append ( ID_FONT_NORMAL, _ ( "Normal\tCtrl+0" ), _ ( "Normal" ) );
 
5016
 
 
5017
        // color scheme menu
 
5018
        colorSchemeMenu = new wxMenu;
 
5019
        colorSchemeMenu->AppendRadioItem (
 
5020
            ID_COLOR_SCHEME_DEFAULT, _ ( "&Default" ), _ ( "Default" ) );
 
5021
        colorSchemeMenu->AppendRadioItem (
 
5022
            ID_COLOR_SCHEME_REDUCED_GLARE,
 
5023
            _ ( "&Blue background, white text" ),
 
5024
            _ ( "Blue background, white text" ) );
 
5025
        colorSchemeMenu->AppendRadioItem (
 
5026
            ID_COLOR_SCHEME_DEFAULT_BACKGROUND,
 
5027
            _ ( "&Light" ),
 
5028
            _ ( "Light" ) );
 
5029
        colorSchemeMenu->AppendRadioItem (
 
5030
            ID_COLOR_SCHEME_NONE,
 
5031
            _ ( "&None" ),
 
5032
            _ ( "None" ) );
 
5033
 
 
5034
        switch ( properties.colorScheme )
 
5035
        {
 
5036
                case COLOR_SCHEME_DEFAULT:
 
5037
                        colorSchemeMenu->Check ( ID_COLOR_SCHEME_DEFAULT, true );
 
5038
                        break;
 
5039
                case COLOR_SCHEME_DEFAULT_BACKGROUND:
 
5040
                        colorSchemeMenu->Check ( ID_COLOR_SCHEME_DEFAULT_BACKGROUND, true );
 
5041
                        break;
 
5042
                case COLOR_SCHEME_REDUCED_GLARE:
 
5043
                        colorSchemeMenu->Check ( ID_COLOR_SCHEME_REDUCED_GLARE, true );
 
5044
                        break;
 
5045
                case COLOR_SCHEME_NONE:
 
5046
                        colorSchemeMenu->Check ( ID_COLOR_SCHEME_NONE, true );
 
5047
                        break;
 
5048
                default:
 
5049
                        break;
 
5050
        }
 
5051
 
 
5052
         /* WAIT FOR AUI LIBRARY TO SUPPORT THIS - currently always splits left
 
5053
        wxMenu *splitTabMenu = new wxMenu;
 
5054
        splitTabMenu->Append ( ID_SPLIT_TAB_TOP, _ ( "&Top" ), _ ( "Top" ));
 
5055
        splitTabMenu->Append ( ID_SPLIT_TAB_RIGHT, _ ( "&Right" ), _ ( "Right" ));
 
5056
        splitTabMenu->Append ( ID_SPLIT_TAB_BOTTOM, _ ( "&Bottom" ), _ ( "Bottom" ));
 
5057
        splitTabMenu->Append ( ID_SPLIT_TAB_LEFT, _ ( "&Left" ), _ ( "Left" ));
 
5058
         */
 
5059
 
 
5060
        viewMenu = new wxMenu; // use class-wide data member
 
5061
        viewMenu->Append ( ID_PREVIOUS_DOCUMENT, _ ( "&Previous Document\tCtrl+PgUp" ), _ ( "Previous Document" ) );
 
5062
        viewMenu->Append ( ID_NEXT_DOCUMENT, _ ( "&Next Document\tCtrl+PgDn" ), _ ( "Next Document" ) );
 
5063
 
 
5064
        //viewMenu->Append ( wxID_ANY, _ ( "&Split Tab" ), splitTabMenu );
 
5065
 
 
5066
        viewMenu->Append ( ID_BROWSER, _ ( "&Browser\tCtrl+B" ), _ ( "Browser" ) );
 
5067
        viewMenu->AppendSeparator();
 
5068
        viewMenu->AppendRadioItem (
 
5069
            ID_SHOW_TAGS,
 
5070
            _ ( "&Show Tags and Attributes\tCtrl+T" ), _ ( "Show Tags and Attributes" ) );
 
5071
        viewMenu->AppendRadioItem (
 
5072
            ID_HIDE_ATTRIBUTES,
 
5073
            _ ( "&Hide Attributes Only\tCtrl+Shift+A" ), _ ( "Hide Attributes Only" ) );
 
5074
        viewMenu->AppendRadioItem (
 
5075
            ID_HIDE_TAGS,
 
5076
            _ ( "H&ide Tags and Attributes\tCtrl+Shift+T" ), _ ( "Hide Tags and Attributes" ) );
 
5077
 
 
5078
        switch ( visibilityState )
 
5079
        {
 
5080
                case SHOW_TAGS:
 
5081
                        viewMenu->Check ( ID_SHOW_TAGS, true );
 
5082
                        break;
 
5083
                case HIDE_TAGS:
 
5084
                        viewMenu->Check ( ID_HIDE_TAGS, true );
 
5085
                        break;
 
5086
                case HIDE_ATTRIBUTES:
 
5087
                        viewMenu->Check ( ID_HIDE_ATTRIBUTES, true );
 
5088
                        break;
 
5089
                default:
 
5090
                        viewMenu->Check ( ID_SHOW_TAGS, true );
 
5091
                        break;
 
5092
        }
 
5093
        viewMenu->AppendSeparator();
 
5094
        viewMenu->Append (
 
5095
            ID_TOGGLE_FOLD, _ ( "&Toggle Fold\tCtrl+Alt+T" ), _ ( "Toggle Fold" ) );
 
5096
        viewMenu->Append (
 
5097
            ID_FOLD_ALL, _ ( "&Fold Tags\tCtrl+Shift+F" ), _ ( "Fold Tags" ) );
 
5098
        viewMenu->Append (
 
5099
            ID_UNFOLD_ALL, _ ( "&Unfold Tags\tCtrl+Shift+U" ), _T ( "Unfold Tags" ) );
 
5100
        viewMenu->AppendSeparator();
 
5101
        viewMenu->AppendCheckItem (
 
5102
            ID_WRAP_WORDS, _ ( "&Wrap Words\tCtrl+W" ), _T ( "Wrap Words" ) );
 
5103
        viewMenu->Check ( ID_WRAP_WORDS, properties.wrap );
 
5104
        viewMenu->Append ( wxID_ANY, _ ( "&Color Scheme" ), colorSchemeMenu );
 
5105
        viewMenu->Append ( wxID_ANY, _ ( "&Text Size" ), fontSizeMenu );
 
5106
        viewMenu->AppendSeparator();
 
5107
 
 
5108
        viewMenu->AppendCheckItem (
 
5109
            ID_LOCATION_PANE_VISIBLE,
 
5110
            _ ( "S&how Current Element Pane" ),
 
5111
            _ ( "Show Current Element Pane" ) );
 
5112
        viewMenu->Check ( ID_LOCATION_PANE_VISIBLE, false );
 
5113
        viewMenu->AppendCheckItem (
 
5114
            ID_TOOLBAR_VISIBLE, _ ( "Sh&ow Toolbar" ), _ ( "Show Toolbar" ) );
 
5115
        viewMenu->Check ( ID_TOOLBAR_VISIBLE, toolbarVisible );
 
5116
        viewMenu->Append (
 
5117
            ID_HIDE_PANE, _ ( "C&lose Message Pane\tAlt+C" ), _ ( "Close Message Pane" ) );
 
5118
 
 
5119
        // insert menu
 
5120
        wxMenu *insertMenu = new wxMenu;
 
5121
        insertMenu->Append ( ID_INSERT_CHILD, _ ( "&Element...\tCtrl+I" ), _ ( "Element..." ) );
 
5122
        insertMenu->Append ( ID_INSERT_SIBLING, _ ( "&Sibling...\tCtrl+Shift+I" ), _ ( "Sibling..." ) );
 
5123
        insertMenu->Append ( ID_INSERT_ENTITY, _ ( "&Entity...\tCtrl+E" ), _ ( "Entity..." ) );
 
5124
        insertMenu->AppendSeparator();
 
5125
        insertMenu->Append ( ID_INSERT_TWIN, _ ( "&Twin\tCtrl+Enter" ), _ ( "Twin" ) );
 
5126
        insertMenu->AppendSeparator();
 
5127
        insertMenu->Append ( ID_INSERT_SYMBOL, _ ( "S&ymbol..." ), _ ( "Symbol..." ) );
 
5128
 
 
5129
        // validation menu
 
5130
        wxMenu *validationMenu = new wxMenu;
 
5131
        /*
 
5132
            validationMenu->Append ( ID_VALIDATE_DTD, _ ( "&DTD\tF4" ), _ ( "DTD" ) );
 
5133
        */
 
5134
        validationMenu->Append (
 
5135
            ID_VALIDATE_W3C_SCHEMA, _ ( "&DTD/XML Schema\tF5" ), _ ( "DTD/XML Schema" ) );
 
5136
        validationMenu->AppendSeparator();
 
5137
        validationMenu->Append (
 
5138
            ID_VALIDATE_RELAX_NG, _ ( "&RELAX NG...\tF6" ), _ ( "RELAX NG..." ) );
 
5139
 
 
5140
        wxMenu *associateMenu = new wxMenu;
 
5141
        associateMenu->Append ( ID_ASSOCIATE_DTD_PUBLIC, _ ( "&Public DTD..." ), _ ( "Public DTD..." ) );
 
5142
        associateMenu->Append ( ID_ASSOCIATE_DTD_SYSTEM, _ ( "&System DTD..." ), _ ( "System DTD..." ) );
 
5143
        associateMenu->Append ( ID_ASSOCIATE_W3C_SCHEMA, _ ( "&XML Schema..." ), _ ( "XML Schema..." ) );
 
5144
        associateMenu->Append ( ID_ASSOCIATE_XSL, _ ( "XS&LT stylesheet..." ), _ ( "XSLT stylesheet..." ) );
 
5145
 
 
5146
        if ( wxFileName::DirExists ( rngDir ) )
 
5147
        {
 
5148
                wxString rngMask, rngFile, displayName, shortcutString;
 
5149
                rngMask = rngDir + wxFileName::GetPathSeparator() + _T ( "*.rng" );
 
5150
                rngFile = wxFindFirstFile ( rngMask, wxFILE );
 
5151
 
 
5152
                int id = ID_VALIDATE_PRESET1;
 
5153
 
 
5154
                if ( !rngFile.empty() )
 
5155
                {
 
5156
                        validationPresetMap.insert ( make_pair ( id, rngFile ) );
 
5157
                        wxFileName::SplitPath ( rngFile, NULL, NULL, &displayName, NULL );
 
5158
                        displayName.Replace ( _T ( ".rng" ), _T ( "" ) );
 
5159
                        shortcutString.Printf ( _ ( "\tCtrl+%i" ), ( id - ID_VALIDATE_PRESET1 ) + 1 );
 
5160
 
 
5161
                        validationMenu->Append ( id, displayName + shortcutString, displayName );
 
5162
 
 
5163
                        for ( id = ID_VALIDATE_PRESET2; id <= ID_VALIDATE_PRESET9; ++id )
 
5164
                        {
 
5165
                                rngFile = wxFindNextFile();
 
5166
                                if ( rngFile.empty() )
 
5167
                                        break;
 
5168
                                validationPresetMap.insert ( make_pair ( id, rngFile ) );
 
5169
                                wxFileName::SplitPath ( rngFile, NULL, NULL, &displayName, NULL );
 
5170
                                shortcutString.Printf ( _ ( "\tCtrl+%i" ), ( id - ID_VALIDATE_PRESET1 ) + 1 );
 
5171
                                displayName.Replace ( _T ( ".rng" ), _T ( "" ) );
 
5172
                                validationMenu->Append ( id, displayName + shortcutString, displayName );
 
5173
                        }
 
5174
                }
 
5175
        }
 
5176
 
 
5177
        // xsl menu
 
5178
        /*
 
5179
        wxMenu *xslMenu = new wxMenu;
 
5180
        xslMenu->Append ( ID_XSLT, _ ( "&XSL Transform...\tF8" ),
 
5181
                          _ ( "XSL Transform..." ) );
 
5182
        xslMenu->AppendSeparator();
 
5183
        xslMenu->Append (
 
5184
            ID_XSLT_DOCBOOK_HTML,
 
5185
            _ ( "&DocBook to HTML\tAlt+1" ), _ ( "DocBook to HTML" ) );
 
5186
        xslMenu->Append (
 
5187
            ID_XSLT_DOCBOOK_XHTML,
 
5188
            _ ( "&DocBook to XHTML\tAlt+2" ), _ ( "DocBook to XHTML" ) );
 
5189
        xslMenu->Append (
 
5190
            ID_XSLT_DOCBOOK_FO,
 
5191
            _ ( "D&ocBook to XSL-FO\tAlt+3" ), _ ( "DocBook to XSL-FO" ) );
 
5192
        xslMenu->Append (
 
5193
            ID_XSLT_TEI_HTML,
 
5194
            _ ( "&TEI to HTML\tAlt+4" ), _ ( "TEI to HTML" ) );
 
5195
        xslMenu->Append (
 
5196
            ID_XSLT_TEI_LATEX,
 
5197
            _ ( "T&EI to LaTeX\tAlt+5" ), _ ( "TEI to LaTeX" ) );
 
5198
        xslMenu->Append (
 
5199
            ID_XSLT_TEI_XHTML,
 
5200
            _ ( "TE&I to XHTML\tAlt+6" ), _ ( "TEI to XHTML" ) );
 
5201
        xslMenu->Append (
 
5202
            ID_XSLT_TEI_FO,
 
5203
            _ ( "TEI to &XSL-FO\tAlt+7" ), _ ( "TEI to XSL-FO" ) );
 
5204
        */
 
5205
 
 
5206
        // xml menu
 
5207
        xmlMenu = new wxMenu; // use class-wide data member
 
5208
        xmlMenu->Append (
 
5209
            ID_CHECK_WELLFORMED,
 
5210
            _ ( "&Check Well-formedness\tF2" ), _ ( "Check Well-formedness" ) );
 
5211
        xmlMenu->Append (
 
5212
            wxID_ANY,
 
5213
            _ ( "&Validate" ),
 
5214
            validationMenu );
 
5215
        xmlMenu->AppendSeparator();
 
5216
        xmlMenu->Append (
 
5217
            wxID_ANY,
 
5218
            _ ( "&Associate" ),
 
5219
            associateMenu );
 
5220
        xmlMenu->AppendSeparator();
 
5221
        xmlMenu->Append ( ID_XSLT, _ ( "&XSL Transform...\tF8" ),
 
5222
                          _ ( "XSL Transform..." ) );
 
5223
        xmlMenu->Append (
 
5224
            ID_XPATH,
 
5225
            _ ( "&Evaluate XPath...\tF9" ),
 
5226
            _ ( "Evaluate XPath..." ) );
 
5227
 
 
5228
        xmlMenu->AppendSeparator();
 
5229
        xmlMenu->Append (
 
5230
            ID_PRETTYPRINT,
 
5231
            _ ( "&Pretty-print\tF11" ), _ ( "Pretty-print" ) );
 
5232
        xmlMenu->AppendSeparator();
 
5233
        xmlMenu->AppendCheckItem (
 
5234
            ID_PROTECT_TAGS,
 
5235
            _ ( "&Lock Tags\tCtrl+L" ),
 
5236
            _ ( "Lock Tags" ) );
 
5237
        xmlMenu->Check ( ID_PROTECT_TAGS, protectTags );
 
5238
        xmlMenu->AppendSeparator();
 
5239
        xmlMenu->Append (
 
5240
            ID_ENCODING,
 
5241
            _ ( "E&ncoding..." ), _ ( "Encoding..." ) );
 
5242
 
 
5243
        // tools menu
 
5244
        wxMenu *toolsMenu = new wxMenu;
 
5245
 
 
5246
        wxMenuItem *spellingItem =
 
5247
            new wxMenuItem (
 
5248
            NULL,
 
5249
            ID_SPELL,
 
5250
            _ ( "&Spelling...\tF7" ),
 
5251
            _ ( "Spelling..." ) );
 
5252
        spellingItem->SetBitmap ( spelling16Bitmap );
 
5253
        
 
5254
        wxMenuItem *styleItem = 
 
5255
            new wxMenuItem (
 
5256
            NULL,
 
5257
            ID_STYLE,
 
5258
            _ ( "&Style...\tShift+F7" ),
 
5259
            _ ( "Style..." ) );
 
5260
        styleItem->SetBitmap ( wxNullBitmap );
 
5261
 
 
5262
        wxMenuItem *wordCountItem =
 
5263
            new wxMenuItem (
 
5264
            NULL,
 
5265
            ID_WORD_COUNT,
 
5266
            _ ( "&Word Count" ),
 
5267
            _ ( "Word Count" ) );
 
5268
        wordCountItem->SetBitmap ( wxNullBitmap );
 
5269
 
 
5270
        wxMenuItem *commandItem =
 
5271
            new wxMenuItem (
 
5272
            NULL,
 
5273
            ID_COMMAND,
 
5274
            _ ( "&Command\tCtrl+Alt+C" ),
 
5275
            _ ( "Command" ) );
 
5276
        commandItem->SetBitmap ( wxNullBitmap );
 
5277
 
 
5278
        toolsMenu->Append ( spellingItem );
 
5279
        toolsMenu->Append ( styleItem );
 
5280
        toolsMenu->Append ( wordCountItem );
 
5281
        toolsMenu->AppendSeparator();
 
5282
        toolsMenu->Append ( commandItem );
 
5283
 
 
5284
#ifdef __WXMSW__
 
5285
        toolsMenu->AppendSeparator();
 
5286
        wxMenuItem *optionsItem =
 
5287
            new wxMenuItem (
 
5288
            NULL,
 
5289
            ID_OPTIONS,
 
5290
            _ ( "&Options..." ),
 
5291
            _ ( "Options..." ) );
 
5292
        optionsItem->SetBitmap ( wxNullBitmap );
 
5293
        toolsMenu->Append ( optionsItem );
 
5294
#endif
 
5295
 
 
5296
        // help menu
 
5297
        wxMenu *helpMenu = new wxMenu;
 
5298
 
 
5299
        wxMenuItem *helpItem =
 
5300
            new wxMenuItem ( NULL, wxID_HELP,
 
5301
                             _ ( "&XML Copy Editor Help\tF1" ), _ ( "Help" ) );
 
5302
        helpItem->SetBitmap ( helpBitmap );
 
5303
        wxMenuItem *homeItem =
 
5304
            new wxMenuItem ( NULL, ID_HOME,
 
5305
                             _ ( "&Home Page" ), _ ( "Home Page" ) );
 
5306
        homeItem->SetBitmap ( wxNullBitmap );
 
5307
        wxMenuItem *feedbackItem =
 
5308
            new wxMenuItem ( NULL, ID_FEEDBACK, _ ( "&Forum" ), _ ( "Forum" ) );
 
5309
        feedbackItem->SetBitmap ( wxNullBitmap );
 
5310
        wxMenuItem *aboutItem =
 
5311
            new wxMenuItem ( NULL, wxID_ABOUT,
 
5312
                             _ ( "&About XML Copy Editor" ), _ ( "About" ) );
 
5313
        aboutItem->SetBitmap ( wxNullBitmap );
 
5314
        wxMenuItem *downloadSourceItem =
 
5315
            new wxMenuItem ( NULL, ID_DOWNLOAD_SOURCE,
 
5316
                             _ ( "&Browse Source" ), _ ( "Browse Source" ) );
 
5317
        downloadSourceItem->SetBitmap ( wxNullBitmap );
 
5318
        helpMenu->Append ( helpItem );
 
5319
        helpMenu->AppendSeparator();
 
5320
        helpMenu->Append ( homeItem );
 
5321
        helpMenu->Append ( feedbackItem );
 
5322
        helpMenu->Append ( downloadSourceItem );
 
5323
        helpMenu->AppendSeparator();
 
5324
        helpMenu->Append ( aboutItem );
 
5325
 
 
5326
        wxMenuBar *menuBar = new wxMenuBar ( wxMB_DOCKABLE );
 
5327
        menuBar->Append ( fileMenu, _ ( "&File" ) );
 
5328
        menuBar->Append ( editMenu, _ ( "&Edit" ) );
 
5329
        menuBar->Append ( viewMenu, _ ( "&View" ) );
 
5330
        menuBar->Append ( insertMenu, _ ( "&Insert" ) );
 
5331
        menuBar->Append ( xmlMenu, _ ( "&XML" ) );
 
5332
        menuBar->Append ( toolsMenu, _ ( "&Tools" ) );
 
5333
        menuBar->Append ( helpMenu, _ ( "&Help" ) );
 
5334
        return menuBar;
 
5335
}
 
5336
 
 
5337
void MyFrame::updateFileMenu ( bool deleteExisting )
 
5338
{
 
5339
        if ( deleteExisting )
 
5340
        {
 
5341
                wxMenuItemList list = fileMenu->GetMenuItems();
 
5342
                size_t count = list.size();
 
5343
                for ( size_t i = 0; i < count; ++i )
 
5344
                        fileMenu->Delete ( list[i] );
 
5345
        }
 
5346
 
 
5347
        wxMenuItem *newItem =
 
5348
            new wxMenuItem ( NULL, wxID_NEW, _ ( "&New...\tCtrl+N" ), _ ( "New..." ) );
 
5349
        newItem->SetBitmap ( new16Bitmap );
 
5350
        wxMenuItem *openItem =
 
5351
            new wxMenuItem ( NULL, wxID_OPEN, _ ( "&Open...\tCtrl+O" ), _ ( "Open..." ) );
 
5352
        openItem->SetBitmap ( open16Bitmap );
 
5353
        wxMenuItem *openLargeFileItem =
 
5354
            new wxMenuItem ( NULL, ID_OPEN_LARGE_FILE,
 
5355
                             _ ( "O&pen Large Document...\tCtrl+Shift+O" ), _ ( "Open Large Document..." ) );
 
5356
        openLargeFileItem->SetBitmap ( wxNullBitmap );
 
5357
 
 
5358
        wxMenuItem *closeItem =
 
5359
            new wxMenuItem ( NULL, wxID_CLOSE, _ ( "&Close\tCtrl+F4" ), _ ( "Close" ) );
 
5360
        closeItem->SetBitmap ( wxNullBitmap );
 
5361
        wxMenuItem *closeAllItem =
 
5362
            new wxMenuItem ( NULL, wxID_CLOSE_ALL, _ ( "C&lose All" ), _ ( "Close All" ) );
 
5363
        closeAllItem->SetBitmap ( wxNullBitmap );
 
5364
        wxMenuItem *saveItem =
 
5365
            new wxMenuItem ( NULL, wxID_SAVE, _ ( "&Save\tCtrl+S" ), _ ( "Save" ) );
 
5366
        saveItem->SetBitmap ( save16Bitmap );
 
5367
 
 
5368
        wxMenuItem *saveAsItem =
 
5369
            new wxMenuItem ( NULL, wxID_SAVEAS, _ ( "S&ave As...\tF12" ), _ ( "Save As..." ) );
 
5370
        saveAsItem->SetBitmap ( wxNullBitmap );
 
5371
#ifdef __WXMSW__
 
5372
        wxMenuItem *exportItem =
 
5373
        new wxMenuItem ( NULL, ID_EXPORT, _ ( "&DAISY Export..." ), _ ( "DAISY Export..." ) );
 
5374
    exportItem->SetBitmap ( wxNullBitmap );
 
5375
#endif
 
5376
        wxMenuItem *reloadItem =
 
5377
            new wxMenuItem ( NULL, ID_RELOAD, _ ( "&Reload" ), _ ( "Reload" ) );
 
5378
        reloadItem->SetBitmap ( wxNullBitmap );
 
5379
        wxMenuItem *revertItem =
 
5380
            new wxMenuItem ( NULL, wxID_REVERT, _ ( "&Revert" ), _ ( "Revert" ) );
 
5381
        revertItem->SetBitmap ( wxNullBitmap );
 
5382
        wxMenuItem *printSetupItem =
 
5383
            new wxMenuItem ( NULL, ID_PRINT_SETUP, _ ( "Pa&ge Setup..." ), _ ( "Page Setup..." ) );
 
5384
        printSetupItem->SetBitmap ( wxNullBitmap );
 
5385
        wxMenuItem *printPreviewItem =
 
5386
            new wxMenuItem ( NULL, ID_PRINT_PREVIEW, _ ( "Pr&int Preview..." ), _ ( "Print Preview..." ) );
 
5387
        printPreviewItem->SetBitmap ( printPreviewBitmap );
 
5388
        wxMenuItem *printItem =
 
5389
            new wxMenuItem ( NULL, ID_PRINT, _ ( "Pri&nt...\tCtrl+P" ), _ ( "Print..." ) );
 
5390
        printItem->SetBitmap ( print16Bitmap );
 
5391
        wxMenuItem *importMSWordItem =
 
5392
            new wxMenuItem (
 
5393
            NULL, ID_IMPORT_MSWORD, _ ( "I&mport Microsoft Word Document..." ) );
 
5394
        importMSWordItem->SetBitmap ( wxNullBitmap );
 
5395
        wxMenuItem *exportMSWordItem =
 
5396
            new wxMenuItem (
 
5397
            NULL, ID_EXPORT_MSWORD, _ ( "Expor&t Microsoft Word Document..." ) );
 
5398
        exportMSWordItem->SetBitmap ( wxNullBitmap );
 
5399
 
 
5400
        wxMenuItem *exitItem =
 
5401
            new wxMenuItem ( NULL, wxID_EXIT, _ ( "E&xit" ), _ ( "Exit" ) );
 
5402
        exitItem->SetBitmap ( wxNullBitmap );
 
5403
 
 
5404
        fileMenu->Append ( newItem );
 
5405
        fileMenu->Append ( openItem );
 
5406
        fileMenu->Append ( openLargeFileItem );
 
5407
        fileMenu->AppendSeparator();
 
5408
        fileMenu->Append ( closeItem );
 
5409
        fileMenu->Append ( closeAllItem );
 
5410
        fileMenu->Append ( saveItem );
 
5411
        fileMenu->Append ( saveAsItem );
 
5412
    fileMenu->AppendSeparator();
 
5413
        fileMenu->Append ( reloadItem );
 
5414
        fileMenu->Append ( revertItem );
 
5415
        fileMenu->AppendSeparator();
 
5416
        fileMenu->Append ( printSetupItem );
 
5417
        fileMenu->Append ( printPreviewItem );
 
5418
        fileMenu->Append ( printItem );
 
5419
#ifdef __WXMSW__
 
5420
        fileMenu->AppendSeparator();
 
5421
        fileMenu->Append ( exportItem );
 
5422
        fileMenu->Append ( importMSWordItem );
 
5423
        fileMenu->Append ( exportMSWordItem );
 
5424
#endif
 
5425
        history.AddFilesToMenu ( fileMenu );
 
5426
        fileMenu->AppendSeparator();
 
5427
        fileMenu->Append ( exitItem );
 
5428
}
 
5429
 
 
5430
wxToolBar *MyFrame::getToolBar()
 
5431
{
 
5432
        wxToolBar *toolBar = new wxToolBar (
 
5433
            this,
 
5434
            ID_TOOLBAR,
 
5435
            wxDefaultPosition,
 
5436
            wxDefaultSize,
 
5437
            wxTB_FLAT |
 
5438
            wxTB_HORIZONTAL |
 
5439
            wxTB_DOCKABLE );
 
5440
        int w, h;
 
5441
        w = saveBitmap.GetWidth(), h = saveBitmap.GetHeight();
 
5442
        toolBar->SetToolBitmapSize ( wxSize ( w, h ) );
 
5443
 
 
5444
        toolBar->AddTool (
 
5445
            wxID_NEW,
 
5446
            _ ( "New" ),
 
5447
            newBitmap,
 
5448
            _ ( "New" ) );
 
5449
        toolBar->AddTool (
 
5450
            wxID_OPEN,
 
5451
            _ ( "Open" ),
 
5452
            openBitmap,
 
5453
            _ ( "Open" ) );
 
5454
        toolBar->AddTool (
 
5455
            wxID_SAVE,
 
5456
            _ ( "Save" ),
 
5457
            saveBitmap,
 
5458
            wxNullBitmap,
 
5459
            wxITEM_NORMAL,
 
5460
            _ ( "Save" ) );
 
5461
        toolBar->AddTool (
 
5462
            ID_PRINT,
 
5463
            _ ( "Print" ),
 
5464
            printBitmap,
 
5465
            wxNullBitmap,
 
5466
            wxITEM_NORMAL,
 
5467
            _ ( "Print" ) );
 
5468
        toolBar->AddTool (
 
5469
            ID_CHECK_WELLFORMED,
 
5470
            _ ( "Check Well-formedness" ),
 
5471
            checkWellformedBitmap,
 
5472
            wxNullBitmap,
 
5473
            wxITEM_NORMAL,
 
5474
            _ ( "Check Well-formedness" ) );
 
5475
        toolBar->AddTool (
 
5476
            ID_VALIDATE_W3C_SCHEMA,
 
5477
            _ ( "Validate" ),
 
5478
            checkValidBitmap,
 
5479
            wxNullBitmap,
 
5480
            wxITEM_NORMAL,
 
5481
            _ ( "Validate" ) );
 
5482
        toolBar->AddTool (
 
5483
            ID_BROWSER,
 
5484
            _ ( "Browser" ),
 
5485
            internetBitmap,
 
5486
            wxNullBitmap,
 
5487
            wxITEM_NORMAL,
 
5488
            _ ( "Browser" ) );
 
5489
        toolBar->AddTool (
 
5490
            ID_SPELL,
 
5491
            _ ( "Spelling" ),
 
5492
            spellingBitmap,
 
5493
            wxNullBitmap,
 
5494
            wxITEM_NORMAL,
 
5495
            _ ( "Spelling" ) );
 
5496
 
 
5497
        toolBar->AddCheckTool (
 
5498
            ID_PROTECT_TAGS,
 
5499
            _ ( "Lock Tags" ),
 
5500
            hyperlinkBitmap,
 
5501
            wxNullBitmap,
 
5502
            _ ( "Lock Tags" ) );
 
5503
        toolBar->ToggleTool (
 
5504
            ID_PROTECT_TAGS, protectTags );
 
5505
 
 
5506
        toolBar->Realize();
 
5507
        return toolBar;
 
5508
}
 
5509
 
 
5510
XmlDoc *MyFrame::getActiveDocument()
 
5511
{
 
5512
        if ( !mainBook->GetPageCount() )
 
5513
                return NULL;
 
5514
        return ( XmlDoc * ) mainBook->GetPage ( mainBook->GetSelection() );
 
5515
}
 
5516
 
 
5517
void MyFrame::addSafeSeparator ( wxToolBar *toolBar )
 
5518
{
 
5519
        wxStaticText *staticControl = new wxStaticText (
 
5520
            toolBar,
 
5521
            wxID_ANY,
 
5522
            _T ( "  " ) );
 
5523
        toolBar->AddControl ( staticControl );
 
5524
}
 
5525
 
 
5526
void MyFrame::statusProgress ( const wxString& s )
 
5527
{
 
5528
        wxStatusBar *status = GetStatusBar();
 
5529
        if ( !status )
 
5530
                return;
 
5531
        status->SetStatusText ( s, 0 );
 
5532
}
 
5533
 
 
5534
void MyFrame::messagePane ( const wxString& s, int iconType, bool forcePane )
 
5535
{
 
5536
        statusProgress ( wxEmptyString );
 
5537
        wxString paneTitle;
 
5538
        switch ( iconType )
 
5539
        {
 
5540
                case ( CONST_INFO ) :
 
5541
                        paneTitle = _ ( "Information" );
 
5542
                        break;
 
5543
                case ( CONST_WARNING ) :
 
5544
                        paneTitle = _ ( "Warning" );
 
5545
                        break;
 
5546
                case ( CONST_STOP ) :
 
5547
                                                paneTitle = _ ( "Stopped" );
 
5548
                        break;
 
5549
                case ( CONST_QUESTION ) :
 
5550
                        paneTitle = _ ( "Question" );
 
5551
                        break;
 
5552
                default:
 
5553
                        paneTitle = _ ( "Message" );
 
5554
                        break;
 
5555
        }
 
5556
 
 
5557
        wxAuiPaneInfo info = manager.GetPane ( htmlReport );
 
5558
        if ( !info.IsShown() )
 
5559
    {
 
5560
                manager.GetPane ( htmlReport ).Show ( true );
 
5561
                manager.Update();
 
5562
        }
 
5563
 
 
5564
        manager.GetPane ( htmlReport ).Caption ( paneTitle );
 
5565
 
 
5566
        wxString htmlString = s;
 
5567
        htmlString.Replace ( _T ( "&" ), _T ( "&amp;" ), true );
 
5568
        htmlString.Replace ( _T ( "<" ), _T ( "&lt;" ), true );
 
5569
        htmlString.Replace ( _T ( ">" ), _T ( "&gt;" ), true );
 
5570
        
 
5571
    htmlString.Replace ( _T("[br/]"), _T("<br/>"), true );
 
5572
    htmlString.Replace ( _T("[b]"), _T("<b>"), true );
 
5573
    htmlString.Replace ( _T("[/b]"), _T("</b>"), true );
 
5574
    htmlString.Replace ( _T("[i]"), _T("<i>"), true );
 
5575
    htmlString.Replace ( _T("[/i]"), _T("</i>"), true );
 
5576
    
 
5577
 
 
5578
        wxString htmlBuffer;
 
5579
        htmlBuffer += _T ( "<html><body><table><tr><td width=\"5%\"><img src=\"" );
 
5580
        switch ( iconType )
 
5581
        {
 
5582
                case ( CONST_INFO ) :
 
5583
                                                htmlBuffer += pngDir;
 
5584
                        htmlBuffer += _T ( "stock_dialog-info-32.png" );
 
5585
                        break;
 
5586
                case ( CONST_WARNING ) :
 
5587
                                                htmlBuffer += pngDir;
 
5588
                        htmlBuffer += _T ( "stock_dialog-warning-32.png" );
 
5589
                        break;
 
5590
                case ( CONST_STOP ) :
 
5591
                                                htmlBuffer += pngDir;
 
5592
                        htmlBuffer += _T ( "stock_dialog-stop-32.png" );
 
5593
                        break;
 
5594
                case ( CONST_QUESTION ) :
 
5595
                                                htmlBuffer += pngDir;
 
5596
                        htmlBuffer += _T ( "stock_dialog-question-32.png" );
 
5597
                        break;
 
5598
                default:
 
5599
                        break;
 
5600
        }
 
5601
        htmlBuffer += _T ( "\"></td><td width=\"95%\">" );
 
5602
        htmlBuffer += htmlString;
 
5603
        htmlBuffer += _T ( "</td></tr></table></body></html>" );
 
5604
 
 
5605
        htmlReport->SetPage ( htmlBuffer );
 
5606
 
 
5607
        manager.Update();
 
5608
}
 
5609
 
 
5610
void MyFrame::documentOk ( const wxString& status )
 
5611
{
 
5612
        XmlDoc *doc;
 
5613
        if ( ( doc = getActiveDocument() ) == NULL )
 
5614
                return;
 
5615
        wxString message;
 
5616
        message.Printf ( _ ( "%s is %s" ),
 
5617
                         doc->getShortFileName().c_str(),
 
5618
                         status.c_str() );
 
5619
        messagePane ( message, CONST_INFO );
 
5620
}
 
5621
 
 
5622
void MyFrame::applyEditorProperties ( bool zoomOnly )
 
5623
{
 
5624
        XmlDoc *doc;
 
5625
        size_t documentCount = mainBook->GetPageCount();
 
5626
        for ( size_t i = 0; i < documentCount; i++ )
 
5627
        {
 
5628
                doc = ( XmlDoc * ) mainBook->GetPage ( i );
 
5629
                if ( doc )
 
5630
                {
 
5631
                        doc->applyProperties ( properties, zoomOnly );
 
5632
                        if ( !properties.validateAsYouType )
 
5633
                                doc->clearErrorIndicators();
 
5634
                }
 
5635
        }
 
5636
}
 
5637
 
 
5638
void MyFrame::modifiedMessage()
 
5639
{
 
5640
        messagePane (
 
5641
            _ ( "Document has been modified: save or discard changes" ),
 
5642
            CONST_STOP );
 
5643
}
 
5644
 
 
5645
void MyFrame::xmliseWideTextNode ( wxString& s )
 
5646
{
 
5647
        s.Replace ( _T ( "&" ), _T ( "&amp;" ), true );
 
5648
        s.Replace ( _T ( "<" ), _T ( "&lt;" ), true );
 
5649
        s.Replace ( _T ( ">" ), _T ( "&gt;" ), true );
 
5650
}
 
5651
 
 
5652
int MyFrame::getFileType ( const wxString& fileName )
 
5653
{
 
5654
        std::string fileNameLocal, fileNameLocalLC;
 
5655
        fileNameLocal = fileName.mb_str ( wxConvLocal );
 
5656
        fileNameLocalLC = CaseHandler::lowerCase ( fileNameLocal );
 
5657
 
 
5658
        if ( fileNameLocalLC.find ( ".dtd" ) != std::string::npos ||
 
5659
                fileNameLocalLC.find ( ".ent" ) != std::string::npos )
 
5660
                return FILE_TYPE_DTD;
 
5661
        else if ( fileNameLocalLC.find ( ".css" ) != std::string::npos )
 
5662
                return FILE_TYPE_CSS;
 
5663
        else if ( fileNameLocalLC.find ( ".php" ) != std::string::npos )
 
5664
                return FILE_TYPE_PHP;
 
5665
        else if ( fileNameLocalLC.find ( ".exe" ) != std::string::npos )
 
5666
                return FILE_TYPE_BINARY;
 
5667
        else if ( fileNameLocalLC.find ( ".rnc" ) != std::string::npos )
 
5668
                return FILE_TYPE_RNC;
 
5669
        return FILE_TYPE_XML;
 
5670
}
 
5671
 
 
5672
long MyFrame::getNotebookStyleMask()
 
5673
{
 
5674
        /*
 
5675
        if (notebookStyleMenu->IsChecked(ID_NOTEBOOK_STYLE_FLAT))
 
5676
          return wxFNB_FANCY_TABS | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_X_ON_TAB;
 
5677
        else if (notebookStyleMenu->IsChecked(ID_NOTEBOOK_STYLE_VC8))
 
5678
          return wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS |
 
5679
            wxFNB_X_ON_TAB | wxFNB_DROPDOWN_TABS_LIST | wxFNB_NO_NAV_BUTTONS;
 
5680
        else
 
5681
          return wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_COLORFUL_TABS |
 
5682
            wxFNB_X_ON_TAB | wxFNB_DROPDOWN_TABS_LIST | wxFNB_NO_NAV_BUTTONS;
 
5683
          //wxFNB_BACKGROUND_GRADIENT | wxFNB_VC8 | wxFNB_MOUSE_MIDDLE_CLOSES_TABS | wxFNB_COLORFUL_TABS;
 
5684
        */
 
5685
        return 0;
 
5686
}
 
5687
 
 
5688
bool MyFrame::isSpecialFileType ( const wxString& fileName )
 
5689
{
 
5690
        std::string fileNameLocal, fileNameLocalLC;
 
5691
        fileNameLocal = fileName.mb_str ( wxConvLocal );
 
5692
        fileNameLocalLC = CaseHandler::lowerCase ( fileNameLocal );
 
5693
 
 
5694
        return (
 
5695
                   fileNameLocalLC.find ( ".dtd" ) != std::string::npos ||
 
5696
                   fileNameLocalLC.find ( ".css" ) != std::string::npos ||
 
5697
                   fileNameLocalLC.find ( ".php" ) != std::string::npos );
 
5698
}
 
5699
 
 
5700
void MyFrame::encodingMessage()
 
5701
{
 
5702
        wxString msg = _ ( "Encoding should be one of " );
 
5703
        msg += ENCODING_INFO;
 
5704
        messagePane ( msg, CONST_STOP );
 
5705
}
 
5706
 
 
5707
void MyFrame::updatePaths()
 
5708
{
 
5709
        ruleSetDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "rulesets" );
 
5710
        filterDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "filters" );
 
5711
        templateDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "templates" ) +
 
5712
                      wxFileName::GetPathSeparator();
 
5713
        binDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "bin" ) +
 
5714
                 wxFileName::GetPathSeparator();
 
5715
        helpDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "help" ) +
 
5716
                  wxFileName::GetPathSeparator();
 
5717
        rngDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "rng" ) +
 
5718
                 wxFileName::GetPathSeparator();
 
5719
        htmlDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "html" ) +
 
5720
                  wxFileName::GetPathSeparator();
 
5721
        pngDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "png" ) +
 
5722
                 wxFileName::GetPathSeparator();
 
5723
    daisyDir = applicationDir + wxFileName::GetPathSeparator() + _T ( "daisy" ) +
 
5724
        wxFileName::GetPathSeparator();
 
5725
        wxString wideCatalogPath =
 
5726
            applicationDir + wxFileName::GetPathSeparator() + _T ( "catalog" ) +
 
5727
            wxFileName::GetPathSeparator() + _T ( "catalog" );
 
5728
        catalogPath = wideCatalogPath.mb_str ( wxConvLocal );
 
5729
        wxString wideCatalogUtilityPath = applicationDir + wxFileName::GetPathSeparator() + _T ( "xmlcatalog" );
 
5730
        catalogUtilityPath = wideCatalogUtilityPath.mb_str ( wxConvLocal );
 
5731
        wxString wideXslDtdPath =
 
5732
            applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
 
5733
            wxFileName::GetPathSeparator() + _T ( "xslt10.dtd" );
 
5734
        xslDtdPath = wideXslDtdPath.mb_str ( wxConvLocal );
 
5735
        wxString wideRssDtdPath =
 
5736
            applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
 
5737
            wxFileName::GetPathSeparator() + _T ( "rss2.dtd" );
 
5738
        rssDtdPath = wideRssDtdPath.mb_str ( wxConvLocal );
 
5739
        wxString wideXtmDtdPath =
 
5740
            applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
 
5741
            wxFileName::GetPathSeparator() + _T ( "xtm1.dtd" );
 
5742
        xtmDtdPath = wideXtmDtdPath.mb_str ( wxConvLocal );
 
5743
        wxString wideLzxDtdPath =
 
5744
            applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
 
5745
            wxFileName::GetPathSeparator() + _T ( "lzx.dtd" );
 
5746
        lzxDtdPath = wideLzxDtdPath.mb_str ( wxConvLocal );
 
5747
        wxString wideXliffDtdPath =
 
5748
            applicationDir + wxFileName::GetPathSeparator() + _T ( "dtd" ) +
 
5749
            wxFileName::GetPathSeparator() + _T ( "xliff.dtd" );
 
5750
        xliffDtdPath = wideXliffDtdPath.mb_str ( wxConvLocal );
 
5751
        wxString wideAspellDataPath = applicationDir + wxFileName::GetPathSeparator() +
 
5752
        _T ( "aspell" ) + wxFileName::GetPathSeparator() + _T ( "data" );
 
5753
    aspellDataPath = wideAspellDataPath.mb_str ( wxConvLocal );
 
5754
        wxString wideAspellDictPath = applicationDir + wxFileName::GetPathSeparator() +
 
5755
        _T ( "aspell" ) + wxFileName::GetPathSeparator() + _T ( "dict" );
 
5756
    aspellDictPath = wideAspellDictPath.mb_str ( wxConvLocal );
 
5757
}
 
5758
 
 
5759
void MyFrame::OnAssociate ( wxCommandEvent& event )
 
5760
{
 
5761
        XmlDoc *doc;
 
5762
        if ( ( doc = getActiveDocument() ) == NULL )
 
5763
                return;
 
5764
 
 
5765
        wxString title, label, type, extension, path, defaulturl, defaultaux;
 
5766
        wxString auxiliaryLabel;
 
5767
        int id = event.GetId();
 
5768
        switch ( id )
 
5769
        {
 
5770
                case ID_ASSOCIATE_DTD_PUBLIC:
 
5771
                        type = _ ( "Public DTD" );
 
5772
                        extension = _T ( "*.dtd" );
 
5773
                        defaulturl = lastDtdPublic;
 
5774
                        defaultaux = lastDtdPublicAux;
 
5775
                        break;
 
5776
                case ID_ASSOCIATE_DTD_SYSTEM:
 
5777
                        type = _ ( "System DTD" );
 
5778
                        extension = _T ( "*.dtd" );
 
5779
                        defaulturl = lastDtdSystem;
 
5780
                        defaultaux = _T ( "" );
 
5781
                        break;
 
5782
                case ID_ASSOCIATE_W3C_SCHEMA:
 
5783
                        type = _ ( "XML Schema" );
 
5784
                        extension = _T ( "*.xsd" );
 
5785
                        defaulturl = lastSchema;
 
5786
                        defaultaux = _T ( "" );
 
5787
                        break;
 
5788
                case ID_ASSOCIATE_XSL:
 
5789
                        type = _ ( "XSLT stylesheet" );
 
5790
                        extension = _T ( "*.xsl;*.xslt" );
 
5791
                        defaulturl = lastXslStylesheet;
 
5792
                        defaultaux = _T ( "" );
 
5793
                        break;
 
5794
                default:
 
5795
                        return;
 
5796
        }
 
5797
 
 
5798
        std::string utf8Buffer;
 
5799
        getRawText ( doc, utf8Buffer );
 
5800
        std::auto_ptr<WrapExpat> wellformedparser ( new WrapExpat() );
 
5801
        if ( !wellformedparser->parse ( utf8Buffer ) )
 
5802
        {
 
5803
                std::string error = wellformedparser->getLastError();
 
5804
                wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
5805
                wxString message;
 
5806
                message.Printf (
 
5807
                    _ ( "Cannot associate %s: %s" ),
 
5808
                    type.c_str(),
 
5809
                    wideError.c_str() );
 
5810
                messagePane ( message,
 
5811
                              CONST_STOP );
 
5812
                return;
 
5813
        }
 
5814
 
 
5815
        title.Printf ( _ ( "Associate %s" ), type.c_str() );
 
5816
        label = _ ( "Choose a file:" );
 
5817
 
 
5818
        bool auxiliaryBox =
 
5819
            ( id == ID_ASSOCIATE_DTD_PUBLIC );
 
5820
        //(id == ID_ASSOCIATE_W3C_SCHEMA_NS || id == ID_ASSOCIATE_DTD_PUBLIC);
 
5821
        if ( auxiliaryBox )
 
5822
        {
 
5823
                auxiliaryLabel = _ ( "Choose a public identifier:" );
 
5824
                /*
 
5825
                      (id == ID_ASSOCIATE_DTD_PUBLIC) ? _("Choose a public identifier:") :
 
5826
                        _("Choose a namespace:");
 
5827
                */
 
5828
        }
 
5829
 
 
5830
        AssociateDialog ad (
 
5831
            this,
 
5832
            title,
 
5833
            label,
 
5834
            type,
 
5835
            extension,
 
5836
            defaulturl,
 
5837
            auxiliaryBox,
 
5838
            auxiliaryLabel,
 
5839
            defaultaux );
 
5840
        if ( ad.ShowModal() != wxID_OK )
 
5841
                return;
 
5842
 
 
5843
        path = ad.getUrl();
 
5844
 
 
5845
        wxString aux, schemaPathMemory;
 
5846
 
 
5847
        if ( auxiliaryBox )
 
5848
        {
 
5849
                aux = ad.getAux();
 
5850
        }
 
5851
 
 
5852
        std::string utf8Path = ( const char * ) path.mb_str ( wxConvUTF8 );
 
5853
 
 
5854
        std::string modifiedBuffer;
 
5855
 
 
5856
        // remember choice
 
5857
        switch ( id )
 
5858
        {
 
5859
                case ID_ASSOCIATE_W3C_SCHEMA:
 
5860
                        lastSchema = path;
 
5861
                        break;
 
5862
                case ID_ASSOCIATE_DTD_PUBLIC:
 
5863
                        lastDtdPublic = path;
 
5864
                        lastDtdPublicAux = aux;
 
5865
                        break;
 
5866
                case ID_ASSOCIATE_DTD_SYSTEM:
 
5867
                        lastDtdSystem = path;
 
5868
                        break;
 
5869
                case ID_ASSOCIATE_XSL:
 
5870
                        lastXslStylesheet = path;
 
5871
                        break;
 
5872
                default:
 
5873
                        break;
 
5874
        }
 
5875
 
 
5876
        if ( id == ID_ASSOCIATE_W3C_SCHEMA )
 
5877
        {
 
5878
                std::auto_ptr<XmlAssociateXsd> parser ( new XmlAssociateXsd ( utf8Path ) );
 
5879
                if ( !parser->parse ( utf8Buffer ) )
 
5880
                        return;
 
5881
                modifiedBuffer = parser->getBuffer();
 
5882
        }
 
5883
        else if ( id == ID_ASSOCIATE_DTD_SYSTEM || id == ID_ASSOCIATE_DTD_PUBLIC )
 
5884
        {
 
5885
                std::auto_ptr<XmlAssociateDtd> parser ( new XmlAssociateDtd (
 
5886
                                                            utf8Path,
 
5887
                                                            ( auxiliaryBox ) ? ( const char * ) aux.mb_str ( wxConvUTF8 ) : "" ) );
 
5888
                if ( !parser->parse ( utf8Buffer ) )
 
5889
                        return;
 
5890
                modifiedBuffer = parser->getBuffer();
 
5891
        }
 
5892
        else if ( id == ID_ASSOCIATE_XSL )
 
5893
        {
 
5894
                std::auto_ptr<XmlAssociateXsl> parser ( new XmlAssociateXsl (
 
5895
                                                            utf8Path ) );
 
5896
                if ( !parser->parse ( utf8Buffer ) )
 
5897
                        return;
 
5898
                modifiedBuffer = parser->getBuffer();
 
5899
        }
 
5900
        else
 
5901
                return;
 
5902
        doc->SetTextRaw ( modifiedBuffer.c_str() );
 
5903
        doc->SetFocus();
 
5904
}
 
5905
 
 
5906
void MyFrame::openRememberedTabs()
 
5907
{
 
5908
        if ( openTabsOnClose.empty() )
 
5909
                return;
 
5910
        wchar_t *s, *it;
 
5911
        s = it = ( wchar_t * ) openTabsOnClose.wc_str();
 
5912
        std::vector<wxString> v;
 
5913
        wxString buffer = wxEmptyString;
 
5914
        for ( ; *it; it++ )
 
5915
        {
 
5916
                if ( *it == L'|' )
 
5917
                {
 
5918
                        if ( !buffer.empty() )
 
5919
                        {
 
5920
                                v.push_back ( buffer );
 
5921
                        }
 
5922
                        buffer = wxEmptyString;
 
5923
                }
 
5924
                else
 
5925
                        buffer += *it;
 
5926
        }
 
5927
        if ( !buffer.empty() )
 
5928
        {
 
5929
                v.push_back ( buffer );
 
5930
        }
 
5931
 
 
5932
        if ( v.empty() )
 
5933
                return;
 
5934
 
 
5935
        std::vector<wxString>::iterator vit;
 
5936
        for ( vit = v.begin(); vit != v.end(); vit++ )
 
5937
        {
 
5938
                if ( !openFile ( *vit ) )
 
5939
                        break;
 
5940
        }
 
5941
        XmlDoc *doc;
 
5942
        if ( ( doc = getActiveDocument() ) != NULL )
 
5943
                doc->SetFocus();
 
5944
}
 
5945
 
 
5946
void MyFrame::getRawText ( XmlDoc *doc, std::string& buffer )
 
5947
{
 
5948
        if ( !doc )
 
5949
        {
 
5950
                buffer = "";
 
5951
                return;
 
5952
        }
 
5953
        buffer = doc->myGetTextRaw();
 
5954
}
 
5955
 
 
5956
void MyFrame::OnWordCount ( wxCommandEvent& event )
 
5957
{
 
5958
        XmlDoc *doc;
 
5959
        if ( ( doc = getActiveDocument() ) == NULL )
 
5960
                return;
 
5961
        wxString wideBuffer;
 
5962
        std::string buffer;
 
5963
        wideBuffer = doc->GetText();
 
5964
        buffer = wideBuffer.mb_str ( wxConvUTF8 );
 
5965
 
 
5966
        auto_ptr<XmlWordCount> xwc ( new XmlWordCount() );
 
5967
        wxString msg;
 
5968
        if ( !xwc->parse ( buffer.c_str() ) )
 
5969
        {
 
5970
                std::string error = xwc->getLastError();
 
5971
                wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
 
5972
                statusProgress ( wxEmptyString );
 
5973
 
 
5974
                msg.Printf ( _ ( "Cannot count words: %s" ), werror.c_str() );
 
5975
                messagePane ( msg, CONST_STOP );
 
5976
                return;
 
5977
        }
 
5978
 
 
5979
        int count = xwc->getWordCount();
 
5980
 
 
5981
        msg.Printf (
 
5982
            ngettext ( L"%s contains %i word", L"%s contains %i words", count ),
 
5983
            doc->getShortFileName().c_str(), count );
 
5984
 
 
5985
        messagePane ( msg, CONST_INFO, true );
 
5986
        doc->SetFocus();
 
5987
}
 
5988
 
 
5989
void MyFrame::removeUtf8Bom ( std::string& buffer )
 
5990
{
 
5991
        if ( buffer.size() > 3 &&
 
5992
                ( unsigned char ) buffer[0] == 0xEF &&
 
5993
                ( unsigned char ) buffer[1] == 0xBB &&
 
5994
                ( unsigned char ) buffer[2] == 0xBF )
 
5995
        {
 
5996
                buffer.erase ( 0, 3 );
 
5997
        }
 
5998
}
 
5999
 
 
6000
void MyFrame::loadBitmaps()
 
6001
{
 
6002
#ifdef __WXMSW__
 
6003
        // toolbar icons
 
6004
        newBitmap = wxBITMAP ( stock_new );
 
6005
        openBitmap = wxBITMAP ( stock_open );
 
6006
        saveBitmap = wxBITMAP ( stock_save );
 
6007
        printBitmap = wxBITMAP ( stock_print );
 
6008
        spellingBitmap = wxBITMAP ( stock_spellcheck );
 
6009
        internetBitmap = wxBITMAP ( stock_internet );
 
6010
        hyperlinkBitmap = wxBITMAP ( stock_hyperlink );
 
6011
        filtersBitmap = wxBITMAP ( stock_filters );
 
6012
        checkWellformedBitmap = wxBITMAP ( stock_calc-accept );
 
6013
        checkValidBitmap = wxBITMAP ( stock_calc-accept-green );
 
6014
 
 
6015
        // menu icons
 
6016
        new16Bitmap = wxBITMAP ( stock_new_16 );
 
6017
        open16Bitmap = wxBITMAP ( stock_open_16 );
 
6018
        save16Bitmap = wxBITMAP ( stock_save_16 );
 
6019
        printPreviewBitmap = wxBITMAP ( stock_print_preview_16 );
 
6020
        print16Bitmap = wxBITMAP ( stock_print_16 );
 
6021
        undo16Bitmap = wxBITMAP ( stock_undo_16 );
 
6022
        redo16Bitmap = wxBITMAP ( stock_redo_16 );
 
6023
        cutBitmap = wxBITMAP ( stock_cut_16 );
 
6024
        copyBitmap = wxBITMAP ( stock_copy_16 );
 
6025
        pasteBitmap = wxBITMAP ( stock_paste_16 );
 
6026
        findBitmap = wxBITMAP ( stock_search_16 );
 
6027
        spelling16Bitmap = wxBITMAP ( stock_spellcheck_16 );
 
6028
        helpBitmap = wxBITMAP ( stock_help_16 );
 
6029
#else
 
6030
        // toolbar icons
 
6031
        newBitmap = wxArtProvider::GetBitmap ( wxART_NEW, wxART_TOOLBAR );
 
6032
        openBitmap = wxArtProvider::GetBitmap ( wxART_FILE_OPEN, wxART_TOOLBAR );
 
6033
        saveBitmap = wxArtProvider::GetBitmap ( wxART_FILE_SAVE, wxART_TOOLBAR );
 
6034
        printBitmap = wxArtProvider::GetBitmap ( wxART_PRINT, wxART_TOOLBAR );
 
6035
        spellingBitmap = wxArtProvider::GetBitmap ( _T ( "gtk-spell-check" ), wxART_TOOLBAR );
 
6036
 
 
6037
        // no stock icons for the following
 
6038
        internetBitmap.LoadFile ( pngDir + _T ( "stock_internet.png" ), wxBITMAP_TYPE_PNG );
 
6039
        hyperlinkBitmap.LoadFile ( pngDir + _T ( "stock_hyperlink.png" ), wxBITMAP_TYPE_PNG );
 
6040
        checkWellformedBitmap.LoadFile ( pngDir + _T ( "stock_calc-accept.png" ), wxBITMAP_TYPE_PNG );
 
6041
        checkValidBitmap.LoadFile ( pngDir + _T ( "stock_calc-accept-green.png" ), wxBITMAP_TYPE_PNG );
 
6042
 
 
6043
        // menu icons
 
6044
        new16Bitmap = wxNullBitmap;
 
6045
        open16Bitmap = wxNullBitmap;
 
6046
        save16Bitmap = wxNullBitmap;
 
6047
        printPreviewBitmap = wxNullBitmap;
 
6048
        print16Bitmap = wxNullBitmap;
 
6049
        undo16Bitmap = wxNullBitmap;
 
6050
        redo16Bitmap = wxNullBitmap;
 
6051
        cutBitmap = wxNullBitmap;
 
6052
        copyBitmap = wxNullBitmap;
 
6053
        pasteBitmap = wxNullBitmap;
 
6054
        findBitmap = wxNullBitmap;
 
6055
        spelling16Bitmap = wxNullBitmap;
 
6056
        helpBitmap = wxNullBitmap;
 
6057
#endif
 
6058
}
 
6059
 
 
6060
#ifdef __WXMSW__
 
6061
void MyFrame::OnDropFiles ( wxDropFilesEvent& event )
 
6062
{
 
6063
        int no = event.GetNumberOfFiles();
 
6064
        wxString *iterator = event.GetFiles();
 
6065
 
 
6066
        if ( !no || !iterator )
 
6067
                return;
 
6068
 
 
6069
        for ( int i = 0; i < no; i++, iterator++ )
 
6070
        {
 
6071
                if ( !openFile ( *iterator ) )
 
6072
                        break;
 
6073
        }
 
6074
}
 
6075
#endif
 
6076
 
 
6077
std::string MyFrame::getAuxPath ( const std::string& fileName )
 
6078
{
 
6079
        if ( fileName.find ( ".xsl" ) != std::string::npos ||
 
6080
                fileName.find ( ".XSL" ) != std::string::npos )
 
6081
                return xslDtdPath;
 
6082
        else if ( fileName.find ( ".rss" ) != std::string::npos ||
 
6083
                  fileName.find ( ".RSS" ) != std::string::npos )
 
6084
                return rssDtdPath;
 
6085
        else if ( fileName.find ( ".xtm" ) != std::string::npos ||
 
6086
                  fileName.find ( ".xtmm" ) != std::string::npos ||
 
6087
                  fileName.find ( ".XTM" ) != std::string::npos ||
 
6088
                  fileName.find ( ".XTMM" ) != std::string::npos )
 
6089
                return xtmDtdPath;
 
6090
        else if ( fileName.find ( ".lzx" ) != std::string::npos ||
 
6091
                  fileName.find ( ".LZX" ) != std::string::npos )
 
6092
                return lzxDtdPath;
 
6093
        else if ( fileName.find ( ".xlf" ) != std::string::npos ||
 
6094
                  fileName.find ( ".XLF" ) != std::string::npos )
 
6095
                return xliffDtdPath;
 
6096
        return "";
 
6097
}
 
6098
 
 
6099
void MyFrame::OnActivateApp ( wxActivateEvent& event )
 
6100
{
 
6101
        event.Skip();
 
6102
        if ( !mainBook || !event.GetActive() )
 
6103
                return;
 
6104
        restoreFocusToNotebook = true;
 
6105
}
 
6106
 
 
6107
void MyFrame::OnIconize ( wxIconizeEvent& event )
 
6108
{
 
6109
        event.Skip();
 
6110
        if ( event.Iconized() )
 
6111
                return;
 
6112
        restoreFocusToNotebook = true;
 
6113
}
 
6114
 
 
6115
void MyFrame::OnKeyPressed ( wxKeyEvent& event )
 
6116
{
 
6117
        event.Skip();
 
6118
}
 
6119
 
 
6120
void MyFrame::setStrictScrolling ( bool b )
 
6121
{
 
6122
        XmlDoc *doc;
 
6123
        doc = getActiveDocument();
 
6124
        if ( !doc )
 
6125
                return;
 
6126
        doc->SetYCaretPolicy ( ( b ) ? ( wxSTC_CARET_STRICT | wxSTC_CARET_SLOP ) : wxSTC_CARET_EVEN,
 
6127
                               ( b ) ? 10 : 0 );
 
6128
}
 
6129
 
 
6130
void MyFrame::addToFileQueue ( wxString& fileName )
 
6131
{
 
6132
     fileQueue.push_back ( fileName );
 
6133
}