]>
Commit | Line | Data |
---|---|---|
e066e256 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Author: Vaclav Slavik | |
3 | // Created: 2000/05/05 | |
4 | // RCS-ID: $Id$ | |
5 | // Copyright: (c) 2000 Vaclav Slavik | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #ifdef __GNUG__ | |
10 | #pragma implementation "preview.h" | |
11 | #endif | |
12 | ||
13 | // For compilers that support precompilation, includes "wx/wx.h". | |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/wx.h" | |
c74caa09 | 21 | #include "wx/xml/xml.h" |
999d9a9f | 22 | #include "wx/xrc/xmlres.h" |
e066e256 VS |
23 | #include "wx/config.h" |
24 | #include "wx/log.h" | |
25 | #include "wx/splitter.h" | |
26 | #include "preview.h" | |
27 | #include "xmlhelpr.h" | |
28 | #include "editor.h" | |
29 | ||
999d9a9f | 30 | #include "wx/xrc/xh_menu.h" |
e066e256 VS |
31 | |
32 | class MyMenubarHandler : public wxMenuBarXmlHandler | |
33 | { | |
34 | public: | |
35 | MyMenubarHandler(wxMenuBar *mbar) : m_MenuBar(mbar) {} | |
36 | ||
37 | wxObject *DoCreateResource() | |
38 | { | |
39 | //wxMenuBar *menubar = new wxMenuBar(GetStyle()); | |
40 | CreateChildren(m_MenuBar); | |
41 | return m_MenuBar; | |
42 | } | |
43 | ||
44 | private: | |
45 | wxMenuBar *m_MenuBar; | |
46 | }; | |
47 | ||
48 | ||
49 | ||
50 | ||
51 | PreviewFrame* PreviewFrame::ms_Instance = NULL; | |
52 | ||
53 | PreviewFrame *PreviewFrame::Get() | |
54 | { | |
55 | if (ms_Instance == NULL) | |
56 | { | |
57 | (void)new PreviewFrame; | |
58 | ms_Instance->Show(TRUE); | |
59 | } | |
60 | return ms_Instance; | |
61 | } | |
62 | ||
63 | PreviewFrame::PreviewFrame() | |
aaa2b34e VS |
64 | : wxFrame(EditorFrame::Get(), -1, _("Preview"), |
65 | wxDefaultPosition, wxDefaultSize, | |
75e01429 VS |
66 | wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR |
67 | #ifdef __WXMSW__ | |
68 | | wxFRAME_TOOL_WINDOW | |
69 | #endif | |
70 | ) | |
e066e256 | 71 | { |
26607f41 | 72 | m_Dirty = FALSE; |
e066e256 VS |
73 | ms_Instance = this; |
74 | m_Node = NULL; | |
75 | ||
76 | SetMenuBar(new wxMenuBar()); | |
77 | ||
43ef6d9e | 78 | m_RC = NULL; |
e066e256 | 79 | m_TmpFile = wxGetTempFileName(_T("wxrcedit")); |
43ef6d9e | 80 | ResetResource(); |
e066e256 VS |
81 | |
82 | wxConfigBase *cfg = wxConfigBase::Get(); | |
83 | SetSize(wxRect(wxPoint(cfg->Read(_T("previewframe_x"), -1), cfg->Read(_T("previewframe_y"), -1)), | |
84 | wxSize(cfg->Read(_T("previewframe_w"), 400), cfg->Read(_T("previewframe_h"), 400)))); | |
85 | ||
86 | m_Splitter = new wxSplitterWindow(this, -1); | |
87 | m_LogCtrl = new wxTextCtrl(m_Splitter, -1, wxEmptyString, wxDefaultPosition, | |
88 | wxDefaultSize, wxTE_MULTILINE); | |
89 | m_ScrollWin = new wxScrolledWindow(m_Splitter, -1); | |
90 | m_ScrollWin->SetBackgroundColour(_T("light steel blue")); | |
91 | m_Splitter->SplitHorizontally(m_ScrollWin, m_LogCtrl, cfg->Read(_T("previewframe_sash"), 300)); | |
92 | ||
93 | CreateStatusBar(); | |
aaa2b34e | 94 | |
0d832cf7 VS |
95 | #ifdef __WXMSW__ |
96 | SendSizeEvent(); // force resize for WXMSW | |
97 | #endif | |
e066e256 VS |
98 | } |
99 | ||
100 | ||
101 | ||
43ef6d9e VS |
102 | void PreviewFrame::ResetResource() |
103 | { | |
104 | delete m_RC; | |
105 | m_RC = new wxXmlResource; | |
106 | // these handlers take precedence over std. ones: | |
107 | m_RC->AddHandler(new MyMenubarHandler(GetMenuBar())); | |
108 | // std handlers: | |
109 | m_RC->InitAllHandlers(); | |
110 | wxRemoveFile(m_TmpFile); | |
111 | m_RC->Load(m_TmpFile); | |
112 | } | |
113 | ||
e066e256 VS |
114 | PreviewFrame::~PreviewFrame() |
115 | { | |
116 | wxConfigBase *cfg = wxConfigBase::Get(); | |
117 | cfg->Write(_T("previewframe_x"), (long)GetPosition().x); | |
118 | cfg->Write(_T("previewframe_y"), (long)GetPosition().y); | |
119 | cfg->Write(_T("previewframe_w"), (long)GetSize().x); | |
120 | cfg->Write(_T("previewframe_h"), (long)GetSize().y); | |
121 | cfg->Write(_T("previewframe_sash"), (long)m_Splitter->GetSashPosition()); | |
122 | ||
123 | ms_Instance = NULL; | |
124 | ||
125 | delete m_RC; | |
126 | wxRemoveFile(m_TmpFile); | |
127 | } | |
128 | ||
129 | ||
130 | ||
26607f41 VS |
131 | void PreviewFrame::MakeDirty() |
132 | { | |
133 | if (m_Node == NULL) return; | |
134 | if (m_Dirty) return; | |
135 | m_Dirty = TRUE; | |
136 | m_LogCtrl->Clear(); | |
19d0f58d | 137 | m_LogCtrl->SetValue(_("Resource modified.\nMove mouse cursor over the preview window to refresh it.")); |
26607f41 VS |
138 | } |
139 | ||
140 | ||
141 | ||
2b5f62a0 | 142 | void PreviewFrame::Preview(wxXmlNode *node, wxXmlDocument *orig_doc) |
e066e256 | 143 | { |
2b5f62a0 VZ |
144 | wxString version = orig_doc->GetRoot()->GetPropVal(wxT("version"), wxT("0.0.0.0")); |
145 | ||
e066e256 VS |
146 | while (node->GetParent()->GetParent() != NULL) node = node->GetParent(); |
147 | ||
148 | { | |
149 | wxXmlDocument doc; | |
43ef6d9e VS |
150 | wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, _T("resource")); |
151 | root->AddProperty(new wxXmlProperty(wxT("version"),version,NULL)); | |
152 | doc.SetRoot(root); | |
e066e256 | 153 | doc.GetRoot()->AddChild(new wxXmlNode(*node)); |
2b5f62a0 | 154 | doc.SetFileEncoding(orig_doc->GetFileEncoding()); |
e066e256 VS |
155 | |
156 | if (XmlGetClass(doc.GetRoot()->GetChildren()) == _T("wxDialog")) | |
157 | XmlSetClass(doc.GetRoot()->GetChildren(), _T("wxPanel")); | |
158 | ||
6267a331 VS |
159 | if (XmlGetClass(doc.GetRoot()->GetChildren()) == _T("wxFrame")) |
160 | XmlSetClass(doc.GetRoot()->GetChildren(), _T("wxPanel")); | |
161 | ||
4d876ee3 | 162 | doc.Save(m_TmpFile); |
e066e256 VS |
163 | // wxXmlResource will detect change automatically |
164 | } | |
165 | ||
166 | ||
167 | //if (m_Node != node) | |
168 | { | |
169 | SetToolBar(NULL); | |
170 | for (int i = (int)(GetMenuBar()->GetMenuCount())-1; i >= 0; i--) | |
171 | delete GetMenuBar()->Remove(i); | |
172 | m_ScrollWin->DestroyChildren(); | |
173 | } | |
174 | ||
175 | m_Node = node; | |
2b5f62a0 | 176 | m_Doc = orig_doc; |
e066e256 VS |
177 | |
178 | m_LogCtrl->Clear(); | |
179 | wxLogTextCtrl mylog(m_LogCtrl); | |
180 | wxLog *oldlog = wxLog::SetActiveTarget(&mylog); | |
181 | ||
182 | wxString oldcwd = wxGetCwd(); | |
183 | wxSetWorkingDirectory(wxPathOnly(EditorFrame::Get()->GetFileName())); | |
184 | ||
185 | if (XmlGetClass(node) == _T("wxMenuBar") || XmlGetClass(node) == _T("wxMenu")) | |
186 | PreviewMenu(); | |
187 | else if (XmlGetClass(node) == _T("wxToolBar")) | |
188 | PreviewToolbar(); | |
189 | else if (XmlGetClass(node) == _T("wxPanel") || XmlGetClass(node) == _T("wxDialog")) | |
190 | PreviewPanel(); | |
6267a331 VS |
191 | else if (XmlGetClass(node) == _T("wxFrame")) |
192 | PreviewWXFrame(); | |
193 | ||
e066e256 VS |
194 | wxSetWorkingDirectory(oldcwd); |
195 | wxLog::SetActiveTarget(oldlog); | |
26607f41 VS |
196 | |
197 | m_Dirty = FALSE; | |
e066e256 VS |
198 | } |
199 | ||
200 | ||
201 | ||
202 | void PreviewFrame::PreviewMenu() | |
203 | { | |
859e5b17 | 204 | wxMenuBar *mbar = NULL; |
e066e256 VS |
205 | |
206 | if (XmlGetClass(m_Node) == _T("wxMenuBar")) | |
207 | mbar = m_RC->LoadMenuBar(m_Node->GetPropVal(_T("name"), _T("-1"))); | |
208 | else | |
209 | { | |
210 | wxMenu *m = m_RC->LoadMenu(m_Node->GetPropVal(_T("name"), _T("-1"))); | |
211 | if (m != NULL) GetMenuBar()->Append(m, _("(menu)")); | |
212 | } | |
213 | ||
214 | if (mbar == NULL) | |
215 | wxLogError(_("Cannot preview the menu -- XML resource corrupted.")); | |
216 | } | |
217 | ||
218 | ||
219 | ||
220 | void PreviewFrame::PreviewToolbar() | |
221 | { | |
222 | SetToolBar(m_RC->LoadToolBar(this, m_Node->GetPropVal(_T("name"), _T("-1")))); | |
223 | } | |
224 | ||
225 | ||
226 | ||
227 | void PreviewFrame::PreviewPanel() | |
228 | { | |
229 | wxPanel *panel = m_RC->LoadPanel(m_ScrollWin, m_Node->GetPropVal(_T("name"), _T("-1"))); | |
6267a331 | 230 | |
e066e256 VS |
231 | if (panel == NULL) |
232 | wxLogError(_("Cannot preview the panel -- XML resource corrupted.")); | |
233 | else | |
234 | { | |
235 | m_ScrollWin->SetScrollbars(1, 1, panel->GetSize().x, panel->GetSize().y, | |
236 | 0, 0, TRUE); | |
237 | } | |
238 | } | |
26607f41 | 239 | |
6267a331 VS |
240 | void PreviewFrame::PreviewWXFrame() |
241 | { | |
242 | //for this to work the frame MUST have a child panel! | |
243 | ||
244 | wxXmlNode* child = m_Node; | |
245 | wxString name; | |
246 | ||
247 | while( child != NULL) | |
248 | { | |
249 | name = child->GetPropVal(_T("name"), _T("-1")); | |
250 | ||
251 | if(name != _T("-1")) | |
252 | { | |
253 | wxXmlNode* parent = child->GetParent(); | |
254 | if(parent->GetPropVal(_T("class"),_T("-1")) == _T("wxPanel")) | |
255 | break; | |
256 | } | |
257 | child = child->GetNext(); | |
258 | } | |
259 | ||
260 | wxPanel *panel = m_RC->LoadPanel(m_ScrollWin, name); | |
261 | ||
262 | if (panel == NULL) | |
263 | wxLogError(_("Cannot preview the panel -- XML resource corrupted.")); | |
264 | else | |
265 | { | |
266 | m_ScrollWin->SetScrollbars(1, 1, panel->GetSize().x, panel->GetSize().y, | |
267 | 0, 0, TRUE); | |
268 | } | |
269 | ||
270 | } | |
26607f41 | 271 | |
26607f41 VS |
272 | BEGIN_EVENT_TABLE(PreviewFrame, wxFrame) |
273 | EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter) | |
274 | END_EVENT_TABLE() | |
275 | ||
859e5b17 | 276 | void PreviewFrame::OnMouseEnter(wxMouseEvent& WXUNUSED(event)) |
26607f41 | 277 | { |
2b5f62a0 | 278 | if (m_Dirty) Preview(m_Node,m_Doc); |
26607f41 | 279 | } |