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 #include "locationpanel.h"
22 #include "xmldoc.h"
23
BEGIN_EVENT_TABLE(LocationPanel,wxPanel)24 BEGIN_EVENT_TABLE ( LocationPanel, wxPanel )
25 EVT_SYS_COLOUR_CHANGED ( LocationPanel::OnSysColourChanged )
26 END_EVENT_TABLE()
27
28 LocationPanel::LocationPanel ( wxWindow *parentWindowParameter, int id ) :
29 wxPanel ( parentWindowParameter, id )
30 {
31 parentWindow = ( MyFrame * ) parentWindowParameter;
32
33 int width = 150;
34 SetSize ( wxSize ( width, -1 ) );
35
36 sizer = new wxBoxSizer ( wxVERTICAL );
37 SetSizer ( sizer );
38
39 edit = new wxTextCtrl (
40 this,
41 wxID_ANY,
42 wxEmptyString,
43 wxDefaultPosition,
44 wxDefaultSize,
45 wxTE_READONLY );
46
47 wxFont normalFont = wxSystemSettings::GetFont ( wxSYS_DEFAULT_GUI_FONT );
48 wxFont boldFont = normalFont;
49 boldFont.SetWeight ( wxFONTWEIGHT_BOLD );
50 edit->SetFont ( boldFont );
51
52 structureEdit = new wxStyledTextCtrl (
53 this,
54 wxID_ANY,
55 wxDefaultPosition,
56 wxDefaultSize);
57 for (int i = 0 ; i < 3; i++ )
58 structureEdit->SetMarginWidth ( i, 0 );
59 structureEdit->SetReadOnly ( true );
60 //structureEdit->SetWrapMode ( wxSTC_WRAP_WORD );
61 //structureEdit->SetWrapVisualFlags ( wxSTC_WRAPVISUALFLAG_START );
62 structureEdit->SetTabWidth ( 2 );
63 structureEdit->SetIndentationGuides ( true );
64 structureEdit->SetLexer ( wxSTC_LEX_NULL );
65
66 wxSysColourChangedEvent event;
67 OnSysColourChanged ( event );
68
69 sizer->Add ( edit, 0, wxGROW | wxTOP, 0 );
70 sizer->Add ( structureEdit, 1, wxGROW | wxTOP, 0 );
71 sizer->Layout();
72 structureEdit->Show ( false );
73 }
74
update(XmlDoc * doc,const wxString & parentParameter)75 void LocationPanel::update (
76 XmlDoc *doc,
77 const wxString& parentParameter )
78 {
79 parent = parentParameter;
80 wxString previous = edit->GetValue();
81
82 if ( !doc )
83 {
84 edit->SetValue ( wxEmptyString );
85 structureEdit->Show ( false );
86 return;
87 }
88 else
89 {
90 wxString structure = doc->getElementStructure ( parent );
91
92 if (!structure.empty () )
93 {
94 indentStructure( structure );
95 structureEdit->Show ( true );
96 structureEdit->SetReadOnly ( false );
97 structureEdit->SetText ( structure );
98 structureEdit->SetReadOnly ( true );
99 sizer->Layout();
100 }
101 else
102 {
103 structureEdit->Show ( false );
104 }
105 }
106
107 if ( parentParameter == previous )
108 return;
109 previous = parentParameter;
110 edit->SetValue ( parent );
111
112 }
113
indentStructure(wxString & structure)114 void LocationPanel::indentStructure ( wxString& structure )
115 {
116 wxString indented;
117 wxString::const_iterator s = structure.begin();
118 int indent = 0;
119 const static wxString indentMark ( _T("\t") );
120
121 int count = 0;
122 bool justSeenContent = false;
123 for ( ; *s; ++s, count++)
124 {
125 if (*s == '(')
126 {
127 if ( count && justSeenContent )
128 {
129 indented += '\n';
130 }
131 else if (!justSeenContent)
132 indented += *s;
133 for ( int i = 0; i < indent; i++ )
134 {
135 indented += indentMark;
136 }
137 if (justSeenContent)
138 indented += *s;
139
140 indent++;
141
142 indented += '\n';
143
144 for (int i = 0; indent && i < indent; i++)
145 indented += indentMark;
146 justSeenContent = false;
147 }
148 else if (*s == ')')
149 {
150 if ( justSeenContent )
151 {
152 indented += '\n';
153 }
154 indent--;
155 for (int i = 0; indent && i < indent; i++)
156 indented += indentMark;
157 indented += *s;
158 indented += '\n';
159 if (*( s + 1 ) && *(s + 1) != ')' )
160 {
161 for (int i = 0; i < indent; i++)
162 indented += indentMark;
163 }
164 justSeenContent = false;
165 }
166 else
167 {
168 if ( *s == '|' && justSeenContent)
169 indented += ' ';
170 indented += *s;
171 if ( *s == ',' || *s == '|' )
172 indented += ' ';
173 justSeenContent = true;
174 }
175 }
176 structure = indented;
177 }
178
OnSysColourChanged(wxSysColourChangedEvent & WXUNUSED (event))179 void LocationPanel::OnSysColourChanged ( wxSysColourChangedEvent &WXUNUSED ( event ) )
180 {
181 wxColor clrWnd = wxSystemSettings::GetColour ( wxSYS_COLOUR_WINDOW );
182 wxColor clrText = wxSystemSettings::GetColour ( wxSYS_COLOUR_WINDOWTEXT );
183 structureEdit->StyleSetForeground ( wxSTC_STYLE_DEFAULT, clrText );
184 structureEdit->StyleSetBackground ( wxSTC_STYLE_DEFAULT, clrWnd );
185 structureEdit->StyleClearAll();
186 }
187