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
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * Xml Copy Editor is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Xml Copy Editor; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #ifndef XMLCOPYEDITOR_H
22 #define XMLCOPYEDITOR_H
23 
24 #ifdef __WXOSX__
25 #define wxMAC_USE_NATIVE_TOOLBAR 1
26 #endif
27 
28 #define NEWFINDREPLACE 1
29 
30 #include <wx/wx.h>
31 #include <wx/fdrepdlg.h>
32 #include <wx/filename.h>
33 #include <wx/wxhtml.h>
34 #include <wx/image.h>
35 #include <wx/imagpng.h>
36 #include <wx/utils.h>
37 #include <wx/docview.h>
38 #include <wx/propdlg.h>
39 #include <wx/generic/propdlg.h>
40 #include <wx/sysopt.h>
41 #include <wx/datetime.h>
42 #include <wx/log.h>
43 #include <wx/clipbrd.h>
44 #include <wx/strconv.h>
45 #include <wx/html/helpctrl.h>
46 #include <wx/snglinst.h>
47 #include <wx/ipc.h>
48 #include <wx/intl.h>
49 #include <wx/fileconf.h>
50 #include <utility>
51 #include <string>
52 #include <set>
53 #include <map>
54 #include <vector>
55 #include <stdexcept>
56 #include <boost/scoped_ptr.hpp>
57 #include "xmldoc.h"
58 #include "myhtmlpane.h"
59 #include "xmlencodinghandler.h"
60 #include <wx/aui/framemanager.h>
61 #include <xercesc/util/XercesDefs.hpp>
62 
63 enum
64 {
65 	STATUS_HIDDEN = 1,
66 	STATUS_PROTECTED,
67 	STATUS_MODIFIED,
68 	STATUS_POSITION,
69 	ID_TOOLBAR = wxID_HIGHEST + 1,
70 	ID_XML_TOOLBAR,
71 	ID_NOTEBOOK,
72 	ID_LOCATION_PANEL,
73 	ID_INSERT_CHILD_PANEL,
74 	ID_INSERT_SIBLING_PANEL,
75 	ID_INSERT_ENTITY_PANEL,
76 	ID_FIND_REPLACE_PANEL,
77 	ID_FIND_PANEL,
78 	ID_COMMAND,
79 	ID_VALIDATION_PANE,
80 	ID_LOCATION_PANE_VISIBLE,
81 	ID_PREVIOUS_DOCUMENT,
82 	ID_NEXT_DOCUMENT,
83 	ID_OPTIONS,
84 	ID_IMPORT_MSWORD,
85 	ID_CLOSE_MESSAGE_PANE,
86 	ID_CLOSE_FIND_REPLACE_PANE,
87 	ID_CLOSE_COMMAND_PANE,
88 	ID_HOME,
89 	ID_PASTE_NEW_DOCUMENT,
90 	ID_PRINT_SETUP,
91 	ID_FEEDBACK,
92 	ID_TOOLBAR_VISIBLE,
93 	ID_PROTECT_TAGS,
94 	ID_SHOW_TAGS,
95 	ID_HIDE_ATTRIBUTES,
96 	ID_HIDE_TAGS,
97 	ID_REPLACE,
98 	ID_GLOBAL_REPLACE,
99 	ID_COLOR_SCHEME_DEFAULT,
100 	ID_COLOR_SCHEME_DEFAULT_BACKGROUND,
101 	ID_COLOR_SCHEME_REDUCED_GLARE,
102 	ID_COLOR_SCHEME_NONE,
103 	ID_NOTEBOOK_STYLE_FLAT,
104 	ID_NOTEBOOK_STYLE_VC8,
105 	ID_NOTEBOOK_STYLE_VC8_COLOR,
106 	ID_DOWNLOAD_SOURCE,
107 	ID_OPEN_LARGE_FILE,
108 	ID_RELOAD,
109 	ID_WRAP_WORDS,
110 	// IDs to be activated only if a document is open
111 	ID_SPLIT_TAB_TOP,
112 	ID_SPLIT_TAB_RIGHT,
113 	ID_SPLIT_TAB_BOTTOM,
114 	ID_SPLIT_TAB_LEFT,
115 	ID_FIND,
116 	ID_FIND_AGAIN,
117 	ID_GOTO,
118 	ID_TOGGLE_COMMENT,
119 	ID_PRINT,
120 	ID_WORD_COUNT,
121 	ID_PRINT_PREVIEW,
122 	ID_INSERT_CHILD,
123 	ID_INSERT_SIBLING,
124 	ID_INSERT_TWIN,
125 	ID_INSERT_ENTITY,
126 	ID_INSERT_SYMBOL,
127 	ID_CHECK_WELLFORMED,
128 	ID_VALIDATE_DTD,
129 	ID_VALIDATE_RELAX_NG,
130 	ID_VALIDATE_W3C_SCHEMA,
131 	ID_CREATE_SCHEMA,
132 	ID_DTD_TO_SCHEMA,
133 	ID_XPATH,
134 	ID_COPY_XPATH,
135 	ID_XSLT,
136 	ID_XSLT_TEI_FO,
137 	ID_XSLT_TEI_HTML,
138 	ID_XSLT_TEI_XHTML,
139 	ID_XSLT_TEI_LATEX,
140 	ID_XSLT_DOCBOOK_FO,
141 	ID_XSLT_DOCBOOK_HTML,
142 	ID_XSLT_DOCBOOK_XHTML,
143 	ID_XSLT_DOCBOOK_WORDML,
144 	ID_XSLT_WORDML_DOCBOOK,
145 	ID_ASSOCIATE_DTD_PUBLIC,
146 	ID_ASSOCIATE_DTD_SYSTEM,
147 	ID_ASSOCIATE_W3C_SCHEMA,
148 	ID_ASSOCIATE_W3C_SCHEMA_NS,
149 	ID_ASSOCIATE_XSL,
150 	ID_PRETTYPRINT,
151 	ID_ENCODING,
152 	ID_SPELL,
153 	ID_STYLE,
154 	ID_FONT_SMALLER,
155 	ID_FONT_NORMAL,
156 	ID_FONT_LARGER,
157 	ID_BROWSER,
158 	ID_TOGGLE_FOLD,
159 	ID_FOLD_ALL,
160 	ID_UNFOLD_ALL,
161 	ID_VALIDATE_PRESET1,
162 	ID_VALIDATE_PRESET2,
163 	ID_VALIDATE_PRESET3,
164 	ID_VALIDATE_PRESET4,
165 	ID_VALIDATE_PRESET5,
166 	ID_VALIDATE_PRESET6,
167 	ID_VALIDATE_PRESET7,
168 	ID_VALIDATE_PRESET8,
169 	ID_VALIDATE_PRESET9,
170 	ID_EXPORT,
171 	ID_EXPORT_MSWORD,
172 	// icon constants
173 	CONST_WARNING,
174 	CONST_STOP,
175 	CONST_INFO,
176 	CONST_QUESTION
177 };
178 
179 class MyServer;
180 
181 class MyApp : public wxApp
182 {
183 	public:
184 		MyApp();
185 		~MyApp();
186 		virtual bool OnInit();
187 		virtual void OnUnhandledException();
188 		virtual bool OnExceptionInMainLoop();
189 #ifndef __WXMSW__
190 		virtual void HandleEvent ( wxEvtHandler *handler, wxEventFunction func, wxEvent& event ) const;
191 #endif
192 		const std::set<const wxLanguageInfo *> &getAvailableTranslations (
193 		    const wxArrayString *catalogLookupPathPrefixes = NULL,
194 		    const wxString *catelog = NULL );
195 	protected:
196 		wxLocale myLocale;
197 	private:
198 		wxSingleInstanceChecker *checker;
199 		MyServer *server;
200 		bool singleInstanceCheck;
201 		int lang;
202 		boost::scoped_ptr<wxFileConfig> config;
203 };
204 
205 // forward declarations
206 class MyNotebook;
207 class wxAuiNotebookEvent;
208 class LocationPanel;
209 class InsertPanel;
210 class CommandPanel;
211 
212 #ifdef NEWFINDREPLACE
213 class FindReplacePanel;
214 #endif
215 
216 class MyFrame : public wxFrame
217 {
218 	public:
219 		MyFrame (
220 		    const wxString& title,
221 		    wxFileConfig *configParameter,
222 		    wxLocale& locale,
223 		    bool singleInstanceCheck,
224 		    int langParameter );
225 		~MyFrame();
226 		void OnActivateApp ( wxActivateEvent& event );
227 		void OnAbout ( wxCommandEvent& event );
228 		void OnCheckWellformedness ( wxCommandEvent& event );
229 		void OnClose ( wxCommandEvent& event );
230 		void OnCloseAll ( wxCommandEvent& event );
231 		void OnCloseMessagePane ( wxCommandEvent& event );
232 		void OnCloseFindReplacePane ( wxCommandEvent& event );
233 		void OnCloseCommandPane ( wxCommandEvent& event );
234 		void OnCut ( wxCommandEvent& event );
235 		void OnCopy ( wxCommandEvent& event );
236 		void OnPaste ( wxCommandEvent& event );
237 		void OnPasteNewDocument ( wxCommandEvent& event );
238 		void OnPrintSetup ( wxCommandEvent &event );
239 		void OnPrintPreview ( wxCommandEvent &event );
240 		void OnPrint ( wxCommandEvent &event );
241 		void OnFind ( wxCommandEvent& event );
242 		void OnFindAgain ( wxCommandEvent& event );
243 		void OnFindReplace ( wxCommandEvent& event );
244 		void OnCommand ( wxCommandEvent& event );
245 		void OnGlobalReplace ( wxCommandEvent& event );
246 		void OnToggleComment ( wxCommandEvent& event );
247 		void OnWordCount ( wxCommandEvent& event );
248 		void OnFeedback ( wxCommandEvent& event );
249 		void OnSplitTab ( wxCommandEvent& event );
250 		void OnFontSmaller ( wxCommandEvent& event );
251 		void OnFontMedium ( wxCommandEvent& event );
252 		void OnFontLarger ( wxCommandEvent& event );
253 		void OnImportMSWord ( wxCommandEvent& event );
254 		void OnExport ( wxCommandEvent& event );
255 		void OnInsertChild ( wxCommandEvent& event );
256 		void OnInsertSibling ( wxCommandEvent& event );
257 		void OnInsertTwin ( wxCommandEvent& event );
258 		void OnInsertEntity ( wxCommandEvent& event );
259 		void OnInsertSymbol ( wxCommandEvent& event );
260 		void OnExportMSWord ( wxCommandEvent& event );
261 		void OnBrowser ( wxCommandEvent& event );
262 		void OnHelp ( wxCommandEvent& event );
263 		void OnGoto ( wxCommandEvent& event );
264 		void OnIconize ( wxIconizeEvent& event );
265 		void OnNew ( wxCommandEvent& event );
266 		void OnOpen ( wxCommandEvent& event );
267 		void OnPrettyPrint ( wxCommandEvent& event );
268 		void OnEncoding ( wxCommandEvent& event );
269 		void OnQuit ( wxCommandEvent& event );
270 		void OnSave ( wxCommandEvent& event );
271 		void OnSaveAs ( wxCommandEvent& event );
272 		void OnReload ( wxCommandEvent& event );
273 		void OnUndo ( wxCommandEvent& event );
274 		void OnRedo ( wxCommandEvent& event );
275 		void OnSpelling ( wxCommandEvent& event );
276 		//void OnStyle ( wxCommandEvent& event );
277 		void OnPreviousDocument ( wxCommandEvent& event );
278 		void OnNextDocument ( wxCommandEvent& event );
279 		void OnOptions ( wxCommandEvent& event );
280 		void OnHistoryFile ( wxCommandEvent& event );
281 		void OnDialogFind ( wxFindDialogEvent& event );
282 		void OnDialogReplace ( wxFindDialogEvent& event );
283 		void OnDialogReplaceAll ( wxFindDialogEvent& event );
284 		void OnFrameClose ( wxCloseEvent& event );
285 		void OnIdle ( wxIdleEvent& event );
286 		void OnUpdateCloseMessagePane ( wxUpdateUIEvent& event );
287 		void OnUpdateCloseFindReplacePane ( wxUpdateUIEvent& event );
288 		void OnUpdateCloseCommandPane ( wxUpdateUIEvent& event );
289 		void OnUpdateCloseAll ( wxUpdateUIEvent& event );
290 		void OnUpdateUndo ( wxUpdateUIEvent& event );
291 		void OnUpdatePreviousDocument ( wxUpdateUIEvent& event );
292 		void OnUpdateSavedOnly ( wxUpdateUIEvent& event );
293 		void OnUpdateNextDocument ( wxUpdateUIEvent& event );
294 		void OnUpdateRedo ( wxUpdateUIEvent& event );
295 		void OnUpdateCutCopy ( wxUpdateUIEvent& event );
296 		void OnUpdateFindAgain ( wxUpdateUIEvent& event );
297 		void OnUpdatePaste ( wxUpdateUIEvent& event );
298 		void OnUpdateToggleComment ( wxUpdateUIEvent& event );
299 		void OnUpdateDocRange ( wxUpdateUIEvent& event );
300 		void OnUpdateReplaceRange ( wxUpdateUIEvent& event );
301 		void OnUpdateReload ( wxUpdateUIEvent& event );
302 		void OnUpdateLocationPaneVisible ( wxUpdateUIEvent& event );
303 		void OnValidateDTD ( wxCommandEvent& event );
304 		void OnValidateRelaxNG ( wxCommandEvent& event );
305 		void OnValidateSchema ( wxCommandEvent& event );
306 		void OnCreateSchema ( wxCommandEvent& event );
307 		void OnDtd2Schema ( wxCommandEvent& event );
308 		void OnXPath ( wxCommandEvent& event );
309 		void OnXslt ( wxCommandEvent& event );
310 		void OnValidatePreset ( wxCommandEvent& event );
311 		void OnHome ( wxCommandEvent& event );
312 		void OnDownloadSource ( wxCommandEvent& event );
313 		void OnKeyPressed ( wxKeyEvent& event );
314 		void OnToolbarVisible ( wxCommandEvent& event );
315 		void OnLocationPaneVisible ( wxCommandEvent& event );
316 		void OnProtectTags ( wxCommandEvent& event );
317 		void OnVisibilityState ( wxCommandEvent& event );
318 		void OnColorScheme ( wxCommandEvent& event );
319 		void OnAssociate ( wxCommandEvent& event );
320 		void OnPageClosing ( wxAuiNotebookEvent& event );
321 		void OnToggleFold ( wxCommandEvent& event );
322 		void OnFoldAll ( wxCommandEvent& event );
323 		void OnUnfoldAll ( wxCommandEvent& event );
324 		void OnRevert ( wxCommandEvent& event );
325 		void OnWrapWords ( wxCommandEvent& event );
326 #ifdef __WXMSW__
327 		void OnDropFiles ( wxDropFilesEvent& event );
328 #endif
329 		void OnPromptGenerated ( wxNotifyEvent &event );
330 		void OnCopyXPath ( wxCommandEvent &event );
331 		void OnUpdateCopyXPath ( wxUpdateUIEvent& event );
332 		void setStrictScrolling ( bool b );
333 		void validateRelaxNG (
334 		    XmlDoc *doc,
335 		    const wxString& schemaName,
336 		    wxString& fileName );
337 		void closeMessagePane();
338 		void closeFindReplacePane();
339 		void closeCommandPane();
340 		bool closeActiveDocument();
341 		bool getHandleCommandLineFlag();
342 
343 		// public to allow access outside MyFrame constructor
344 		void handleCommandLine();
getNotebook()345 		MyNotebook *getNotebook()
346 		{
347 			return mainBook;
348 		}
349 
350 		// public to allow access from CommandPanel
351 		XmlDoc *getActiveDocument();
352 		void newDocument ( const wxString& s, const wxString& path = wxEmptyString, bool canSave = false );
353 		void newDocument ( const std::string& s, const wxString& path = wxEmptyString, bool canSave = false );
354 		void statusProgress ( const wxString& s );
355 
356 		// public to allow InsertPanel access
357 		void messagePane ( const wxString& s,
358 		                   int iconType = CONST_INFO,
359 		                   bool forcePane = false );
360 
361 		// public to allow IPC access
362 		bool openFile ( const wxString &fileName, bool largeFile = false );
363 		bool isOpen ( const wxString& fileName );
364 		bool activateTab ( const wxString& fileName );
365 		void reloadTab();
366 		void addToFileQueue ( wxString& fileName );
367 		void openRememberedTabs();
368 	private:
369 		wxAuiManager manager;
370 		wxFileConfig *config; // owned by MyApp
371         wxLocale& myLocale;
372 		bool singleInstanceCheck;
373 		int lang, lastPos;
374 #ifndef __WXDEBUG__
375 		wxLogNull logTarget;
376 #endif
377 		boost::scoped_ptr<wxHtmlEasyPrinting> htmlPrinting;
378 		boost::scoped_ptr<wxFindReplaceDialog> findDialog;
379 		boost::scoped_ptr<wxHtmlHelpController> helpController;
380 
381 		wxBoxSizer *frameSizer;
382 		wxMenuBar *menuBar;
383 		wxToolBar *toolBar;
384 		LocationPanel *locationPanel;
385 		InsertPanel *insertChildPanel, *insertSiblingPanel, *insertEntityPanel;
386 
387 #ifdef NEWFINDREPLACE
388 		FindReplacePanel *findReplacePanel;
389 #endif
390 		CommandPanel *commandPanel;
391 
392 		XmlDoc *lastDoc;
393 		wxMenu *fileMenu, *xmlMenu, *viewMenu, *colorSchemeMenu;
394 		std::vector<wxMenu *> menuVector;
395 		MyNotebook *mainBook;
396 		MyHtmlPane *htmlReport;
397 
398 		wxString catalogPath, xslDtdPath, rssDtdPath, lzxDtdPath, xtmDtdPath,
399 		xliffDtdPath, aspellDataPath, aspellDictPath;
400 		std::map<std::string, std::map<std::string, std::set<std::string> > >
401 		promptMap;
402 		std::map<int, wxString> validationPresetMap;
403 #ifdef __WXMSW__
404 		struct MyCompare
405 				: public std::binary_function<wxString, wxString, bool>
406 		{
operatorMyCompare407 			bool operator() ( const wxString &x, const wxString &y ) const
408 			{
409 				return x.CmpNoCase ( y ) < 0;
410 			}
411 		};
412 		std::set<wxString, MyCompare> openFileSet;
413 #else
414 		std::set<wxString> openFileSet;
415 #endif
416 		std::set<wxString> openLargeFileSet;
417 		std::vector<wxString> tempFileVector, fileQueue;
418 		int documentCount,
419 		framePosX,
420 		framePosY,
421 		frameWidth,
422 		frameHeight,
423 		notebookStyle,
424 		visibilityState,
425 		commandOutput;
426 		wxPoint stylePosition, aboutPosition;
427 		wxSize styleSize;
428 		wxString applicationDir,
429 		  ruleSetPreset,
430 		  dictionaryPreset,
431 		  filterPreset,
432 		  ruleSetDir,
433 		  filterDir,
434 		  binDir,
435 		  templateDir,
436 		  helpDir,
437 		  rngDir,
438 		  htmlDir,
439 		  pngDir,
440 		  xpmDir,
441 		  daisyDir,
442 		  xpathExpression,
443 		  lastDtdPublic,
444 		  lastDtdSystem,
445 		  lastSchema,
446 		  lastSchemaNamespace,
447 		  lastXslStylesheet,
448 		  lastSchemaNamespaceAux,
449 		  lastRelaxNGSchema,
450 		  lastDtdPublicAux,
451 		  openTabsOnClose,
452 		  layout,
453 		  defaultLayout,
454 		  lastParent,
455 		  lastGrandparent,
456 		  commandString,
457 		  exportStylesheet,
458 		  exportFolder,
459 		  lastSymbol;
460 		wxString mLastDir;
461 		bool globalReplaceAllDocuments,
462 		toolbarVisible,
463 		protectTags,
464 		handleCommandLineFlag,
465 		rememberOpenTabs,
466 		libxmlNetAccess,
467 		deletePageVetoed,
468 		saveBom,
469 		unlimitedUndo,
470 #ifdef __WXMSW__
471 		useCoolBar,
472 		useCoolBarOnStart,
473 #endif
474 		restoreLayout,
475 		showLocationPane,
476 		showInsertChildPane,
477 		showInsertSiblingPane,
478 		showInsertEntityPane,
479 		expandInternalEntities,
480 		validateAsYouType,
481 		restoreFocusToNotebook,
482 		showFullPathOnFrame,
483 		findRegex,
484 		commandSync,
485 #if defined(XERCES_HAVE_SSE2_INTRINSIC) && defined(__WXDEBUG__)
486 		xercescSSE2Warning,
487 #endif
488 		exportQuiet,
489 		exportMp3Album,
490 		exportSuppressOptional,
491 		exportHtml,
492 		exportEpub,
493 		exportRtf,
494 		exportDoc,
495 		exportFullDaisy;
496 		wxBitmap newBitmap,
497 		new16Bitmap,
498 		openBitmap,
499 		open16Bitmap,
500 		searchBitmap,
501 		saveBitmap,
502 		saveGrBitmap,
503 		save16Bitmap,
504 		printPreviewBitmap,
505 		printBitmap,
506 		print16Bitmap,
507 		undoBitmap,
508 		undo16Bitmap,
509 		redoBitmap,
510 		redo16Bitmap,
511 		cutBitmap,
512 		copyBitmap,
513 		pasteBitmap,
514 		findBitmap,
515 		spellingBitmap,
516 		spellingGrBitmap,
517 		spelling16Bitmap,
518 		helpBitmap,
519 		internetBitmap,
520 		internetGrBitmap,
521 		hyperlinkBitmap,
522 		filtersBitmap,
523 		checkWellformedBitmap,
524 		checkValidBitmap;
525 		wxFileHistory history;
526 		wxFindReplaceData findData;
527 		XmlCtrlProperties properties, largeFileProperties;
528 
529 		// member functions
530 		bool panelHasFocus();
531 		bool saveFile (
532 		    XmlDoc *doc,
533 		    wxString& fileName,
534 		    bool checkLastModified = true );
535 		int getFileType ( const wxString& fileName );
536 		long getNotebookStyleMask();
537 		bool isSpecialFileType ( const wxString& fileName );
538 		wxString getHtmlBuffer();
539 		void encodingMessage();
540 		void save();
541 		void saveAs();
542 		void displaySavedStatus ( int bytes );
543 		void addSafeSeparator ( wxToolBar *toolBar );
544 		void findAgain ( wxString s, int flags );
545 		void updateFileMenu ( bool deleteExisting = true );
546 		void documentOk ( const wxString& status );
547 		void applyEditorProperties ( bool zoomOnly = false );
548 		void xmliseWideTextNode ( wxString& s );
549 		void updatePaths();
550 		void importMSWord ( const wxString& path );
551 		void showTopBars ( bool b );
552 		void modifiedMessage();
553 		void loadBitmaps();
554 		void getRawText ( XmlDoc *doc, std::string& buffer );
555 		void updateToolbar();
556 		std::string getApproximateEncoding ( char *docBuffer, size_t docBufferLen );
557 		bool saveRawUtf8 (
558 		    const std::string& fileNameLocal,
559 		    std::string& bufferUtf8,
560 		    bool ignoreEncoding = false,
561 		    bool isXml = true );
562 		void removeUtf8Bom ( std::string& buffer );
563 		wxString getAuxPath ( const wxString& fileName );
564 		wxMenuBar *getMenuBar();
565 		wxToolBar *getToolBar();
566 		void validatePaths();
567 
568 		DECLARE_EVENT_TABLE()
569 };
570 
571 #endif
572