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 XML_CTRL_H
22 #define XML_CTRL_H
23 #define DEFAULT_XML_DECLARATION L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
24 #define DEFAULT_XML_DECLARATION_UTF8 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
25 
26 #include <wx/wx.h>
27 #include <wx/stc/stc.h>
28 #include <string>
29 #include <set>
30 #include <map>
31 
32 class ValidationThread;
33 class XmlPromptGenerator;
34 
35 struct XmlCtrlProperties
36 {
37 	bool completion;
38 	bool fold;
39 	bool foldCompact;
40 	bool number;
41 	bool currentLine;
42 	bool whitespaceVisible;
43 	bool wrap;
44 	bool indentLines;
45 	bool protectHiddenElements;
46 	bool toggleLineBackground;
47 	bool insertCloseTag;
48 	bool deleteWholeTag;
49 	bool validateAsYouType;
50 	bool highlightSyntax;
51 	int zoom, colorScheme;
52 	wxString font;
53 };
54 
55 enum VisibilityStates
56 {
57 	SHOW_TAGS,
58 	HIDE_ATTRIBUTES,
59 	HIDE_TAGS
60 };
61 
62 enum ColorSchemes
63 {
64 	COLOR_SCHEME_DEFAULT,
65 	COLOR_SCHEME_DEFAULT_BACKGROUND,
66 	COLOR_SCHEME_REDUCED_GLARE,
67 	COLOR_SCHEME_REDUCED_GLARE_BACKGROUND,
68 	COLOR_SCHEME_NONE
69 };
70 
71 enum TagTypes
72 {
73 	TAG_TYPE_OPEN,
74 	TAG_TYPE_CLOSE,
75 	TAG_TYPE_EMPTY,
76 	TAG_TYPE_OTHER,
77 	TAG_TYPE_ERROR
78 };
79 
80 enum BackgroundStates
81 {
82 	BACKGROUND_STATE_NORMAL,
83 	BACKGROUND_STATE_LIGHT
84 };
85 
86 enum XmlFileTypes
87 {
88 	FILE_TYPE_XML,
89 	FILE_TYPE_DTD,
90 	FILE_TYPE_CSS,
91 	FILE_TYPE_PHP,
92 	FILE_TYPE_RNC,
93 	FILE_TYPE_BINARY
94 };
95 
96 class XmlCtrl: public wxStyledTextCtrl
97 {
98 	public:
99 		XmlCtrl (
100 		    wxWindow *parent,
101 		    const XmlCtrlProperties &propertiesParameter,
102 		    bool *protectTagsParameter,
103 		    int visibilityStateParameter = SHOW_TAGS,
104 		    int typeParameter = FILE_TYPE_XML,
105 		    wxWindowID id = wxID_ANY,
106 		    const char *buffer = NULL,
107 		    size_t bufferLen = 0,
108 		    const wxString& basePath = wxEmptyString,
109 		    const wxString& auxPath = wxEmptyString,
110 		    const wxPoint &position = wxDefaultPosition,
111 		    const wxSize& size = wxDefaultSize,
112 		    long style = 0 );
113 		~XmlCtrl();
114 		int getType();
getParentCloseAngleBracket(int pos)115 		int getParentCloseAngleBracket ( int pos )
116 		{
117 			return findPreviousStartTag ( pos, 1, '>' );
118 		}
119 		int findNextEndTag (
120 		    int pos,
121 		    unsigned depth = 1,
122 		    int character = '>',
123 		    int range = USHRT_MAX * 4 );
124 		int findPreviousStartTag (
125 		    int pos,
126 		    unsigned depth = 1,
127 		    int character = '<',
128 		    int range = USHRT_MAX * 4 );
129 		void applyProperties (
130 		    const XmlCtrlProperties &propertiesParameter,
131 		    bool zoomOnly = false );
132 		void applyVisibilityState ( int state = SHOW_TAGS );
133 		void updatePromptMaps();
134 		void updatePromptMaps ( const char *utf8Buffer, size_t bufferLen );
135 		void adjustCursor();
136 		void adjustSelection();
137 		void foldAll();
138 		void unfoldAll();
139 		void toggleFold();
140 		bool insertChild ( const wxString& child );
141 		bool insertSibling ( const wxString& sibling, const wxString& parent );
142 		bool insertEntity ( const wxString& entity );
143 		bool getGrammarFound();
144 		void setErrorIndicator ( int line, int column );
145 		void clearErrorIndicators ( int maxLine = 0 );
146 		wxString getParent();
147 		wxString getLastElementName ( int pos );
148 		const std::set<wxString> &getChildren ( const wxString& parent );
149 		const std::set<wxString> &getEntitySet();
150 		const std::set<std::string> &getAttributes ( const wxString& parent );
151 		wxString getElementStructure ( const wxString& parent );
152 		bool canInsertAt ( int pos );
153 		int getTagStartPos ( int pos );
154 		void toggleLineBackground();
155 		bool backgroundValidate (  );
156 		bool backgroundValidate (
157 			const char *buffer,
158 			const wxString &system,
159 			size_t bufferLen );
160 		std::string myGetTextRaw(); // alternative to faulty stc implementation
161 		bool getValidationRequired();
162 		void setValidationRequired ( bool b );
163 		bool selectCurrentElement();
164 		void toggleComment();
165 		wxString getCurrentXPath();
166 		int getPrevNonSpaceChar ( int curPos, int *charPos );
167 	private:
168 		ValidationThread *validationThread; // used for background validation
169 		XmlPromptGenerator *mPromptGeneratorThread;
170 
171 		int type;
172 		bool *protectTags;
173 		bool validationRequired, grammarFound;
174 		int visibilityState;
175 		int controlState;
176 		int currentMaxLine;
177 		int lineBackgroundState;
178 		wxColour baseBackground, alternateBackground;
179 		std::map<wxString, std::map<wxString, std::set<wxString> > >
180 		attributeMap;
181 		std::map<wxString, std::set<wxString> > requiredAttributeMap;
182 		std::map<wxString, std::set<wxString> > elementMap;
183 		std::set<wxString> entitySet;
184 		std::map<wxString, wxString> elementStructureMap;
185 		wxString basePath, auxPath;
186 		XmlCtrlProperties properties;
187 		wxString getLastAttributeName ( int pos );
188 		int getAttributeStartPos ( int pos );
189 		int getAttributeSectionEndPos ( int pos, int range = USHRT_MAX );
190 		int getTagType ( int pos );
191 		int getLexerStyleAt ( int pos );
192 		bool isCloseTag ( int pos );
193 		bool canMoveLeftAt ( int pos );
194 		bool canMoveRightAt ( int pos );
195 		wxString getOpenTag ( const wxString& element );
196 		void handleOpenAngleBracket ( wxKeyEvent& event );
197 		void handleCloseAngleBracket ( wxKeyEvent& event );
198 		void handleEquals ( wxKeyEvent& event );
199 		void handleSpace ( wxKeyEvent& event );
200 		void handleAmpersand ( wxKeyEvent& event );
201 		void handleForwardSlash ( wxKeyEvent& event );
202 		void handleBackspace ( wxKeyEvent& event );
203 		void handleDelete ( wxKeyEvent& event );
204 		void OnMarginClick ( wxStyledTextEvent& event );
205 		void OnChar ( wxKeyEvent& event );
206 		void OnIdle ( wxIdleEvent& event );
207 		void OnValidationCompleted (wxCommandEvent &event);
208 		void OnKeyPressed ( wxKeyEvent& event );
209 		void OnMouseLeftDown ( wxMouseEvent& event );
210 		void OnMouseLeftUp ( wxMouseEvent& event );
211 		void OnMouseRightUp ( wxMouseEvent& event );
212 		void OnMiddleDown ( wxMouseEvent& event );
213 		void OnPromptGenerated ( wxNotifyEvent &event );
214 		void OnKillFocus ( wxFocusEvent &event );
215 		void insertNewLine();
216 		void adjustNoColumnWidth();
217 		void adjustPosRight();
218 		void adjustPosLeft();
219 		void setColorScheme ( int scheme );
220 		void expandFoldsToLevel ( int level, bool expand );
221 		void protectHeadLine();
222 
223 		DECLARE_NO_COPY_CLASS ( XmlCtrl )
224 		DECLARE_EVENT_TABLE()
225 };
226 
227 #endif
228