]> git.saurik.com Git - wxWidgets.git/blame - contrib/utils/wxrcedit/preview.cpp
use default position, not 0, 0
[wxWidgets.git] / contrib / utils / wxrcedit / preview.cpp
CommitLineData
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
32class MyMenubarHandler : public wxMenuBarXmlHandler
33{
34 public:
35 MyMenubarHandler(wxMenuBar *mbar) : m_MenuBar(mbar) {}
36
37 wxObject *DoCreateResource()
38 {
f80ea77b 39 //wxMenuBar *menubar = new wxMenuBar(GetStyle());
e066e256
VS
40 CreateChildren(m_MenuBar);
41 return m_MenuBar;
42 }
f80ea77b 43
e066e256
VS
44 private:
45 wxMenuBar *m_MenuBar;
46};
47
48
49
50
51PreviewFrame* PreviewFrame::ms_Instance = NULL;
52
53PreviewFrame *PreviewFrame::Get()
54{
f80ea77b 55 if (ms_Instance == NULL)
e066e256
VS
56 {
57 (void)new PreviewFrame;
f80ea77b 58 ms_Instance->Show(true);
e066e256
VS
59 }
60 return ms_Instance;
61}
62
63PreviewFrame::PreviewFrame()
f80ea77b
WS
64 : wxFrame(EditorFrame::Get(), wxID_ANY, _("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{
f80ea77b 72 m_Dirty = false;
e066e256
VS
73 ms_Instance = this;
74 m_Node = NULL;
f80ea77b 75
e066e256 76 SetMenuBar(new wxMenuBar());
f80ea77b 77
43ef6d9e 78 m_RC = NULL;
e066e256 79 m_TmpFile = wxGetTempFileName(_T("wxrcedit"));
43ef6d9e 80 ResetResource();
e066e256
VS
81
82 wxConfigBase *cfg = wxConfigBase::Get();
422d0ff0 83 SetSize(wxRect(wxPoint(cfg->Read(_T("previewframe_x"), wxDefaultCoord), cfg->Read(_T("previewframe_y"), wxDefaultCoord)),
e066e256
VS
84 wxSize(cfg->Read(_T("previewframe_w"), 400), cfg->Read(_T("previewframe_h"), 400))));
85
f80ea77b 86 m_Splitter = new wxSplitterWindow(this, wxID_ANY);
fa0d3447 87#if wxUSE_LOG
f80ea77b 88 m_LogCtrl = new wxTextCtrl(m_Splitter, wxID_ANY, wxEmptyString, wxDefaultPosition,
e066e256 89 wxDefaultSize, wxTE_MULTILINE);
fa0d3447 90#endif // wxUSE_LOG
f80ea77b 91 m_ScrollWin = new wxScrolledWindow(m_Splitter, wxID_ANY);
e066e256 92 m_ScrollWin->SetBackgroundColour(_T("light steel blue"));
fa0d3447 93#if wxUSE_LOG
e066e256 94 m_Splitter->SplitHorizontally(m_ScrollWin, m_LogCtrl, cfg->Read(_T("previewframe_sash"), 300));
fa0d3447 95#endif // wxUSE_LOG
e066e256 96
d96cdd4a 97#if wxUSE_STATUSBAR
e066e256 98 CreateStatusBar();
d96cdd4a 99#endif // wxUSE_STATUSBAR
aaa2b34e 100
0d832cf7
VS
101#ifdef __WXMSW__
102 SendSizeEvent(); // force resize for WXMSW
103#endif
e066e256
VS
104}
105
106
107
43ef6d9e
VS
108void PreviewFrame::ResetResource()
109{
110 delete m_RC;
111 m_RC = new wxXmlResource;
112 // these handlers take precedence over std. ones:
113 m_RC->AddHandler(new MyMenubarHandler(GetMenuBar()));
114 // std handlers:
115 m_RC->InitAllHandlers();
116 wxRemoveFile(m_TmpFile);
117 m_RC->Load(m_TmpFile);
118}
119
e066e256
VS
120PreviewFrame::~PreviewFrame()
121{
f80ea77b 122 wxConfigBase *cfg = wxConfigBase::Get();
e066e256
VS
123 cfg->Write(_T("previewframe_x"), (long)GetPosition().x);
124 cfg->Write(_T("previewframe_y"), (long)GetPosition().y);
125 cfg->Write(_T("previewframe_w"), (long)GetSize().x);
126 cfg->Write(_T("previewframe_h"), (long)GetSize().y);
127 cfg->Write(_T("previewframe_sash"), (long)m_Splitter->GetSashPosition());
f80ea77b 128
e066e256 129 ms_Instance = NULL;
f80ea77b 130
e066e256
VS
131 delete m_RC;
132 wxRemoveFile(m_TmpFile);
133}
134
135
f80ea77b 136
26607f41
VS
137void PreviewFrame::MakeDirty()
138{
139 if (m_Node == NULL) return;
140 if (m_Dirty) return;
f80ea77b 141 m_Dirty = true;
fa0d3447 142#if wxUSE_LOG
26607f41 143 m_LogCtrl->Clear();
19d0f58d 144 m_LogCtrl->SetValue(_("Resource modified.\nMove mouse cursor over the preview window to refresh it."));
fa0d3447 145#endif // wxUSE_LOG
26607f41
VS
146}
147
148
149
2b5f62a0 150void PreviewFrame::Preview(wxXmlNode *node, wxXmlDocument *orig_doc)
e066e256 151{
2b5f62a0
VZ
152 wxString version = orig_doc->GetRoot()->GetPropVal(wxT("version"), wxT("0.0.0.0"));
153
e066e256
VS
154 while (node->GetParent()->GetParent() != NULL) node = node->GetParent();
155
156 {
157 wxXmlDocument doc;
43ef6d9e
VS
158 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, _T("resource"));
159 root->AddProperty(new wxXmlProperty(wxT("version"),version,NULL));
160 doc.SetRoot(root);
e066e256 161 doc.GetRoot()->AddChild(new wxXmlNode(*node));
2b5f62a0 162 doc.SetFileEncoding(orig_doc->GetFileEncoding());
e066e256 163
f80ea77b
WS
164 if (XmlGetClass(doc.GetRoot()->GetChildren()) == _T("wxDialog"))
165 XmlSetClass(doc.GetRoot()->GetChildren(), _T("wxPanel"));
e066e256 166
f80ea77b
WS
167 if (XmlGetClass(doc.GetRoot()->GetChildren()) == _T("wxFrame"))
168 XmlSetClass(doc.GetRoot()->GetChildren(), _T("wxPanel"));
6267a331 169
4d876ee3 170 doc.Save(m_TmpFile);
e066e256
VS
171 // wxXmlResource will detect change automatically
172 }
173
174
175 //if (m_Node != node)
176 {
177 SetToolBar(NULL);
178 for (int i = (int)(GetMenuBar()->GetMenuCount())-1; i >= 0; i--)
179 delete GetMenuBar()->Remove(i);
180 m_ScrollWin->DestroyChildren();
181 }
182
183 m_Node = node;
2b5f62a0 184 m_Doc = orig_doc;
e066e256 185
fa0d3447 186#if wxUSE_LOG
e066e256
VS
187 m_LogCtrl->Clear();
188 wxLogTextCtrl mylog(m_LogCtrl);
189 wxLog *oldlog = wxLog::SetActiveTarget(&mylog);
fa0d3447 190#endif // wxUSE_LOG
f80ea77b 191
e066e256
VS
192 wxString oldcwd = wxGetCwd();
193 wxSetWorkingDirectory(wxPathOnly(EditorFrame::Get()->GetFileName()));
f80ea77b 194
e066e256
VS
195 if (XmlGetClass(node) == _T("wxMenuBar") || XmlGetClass(node) == _T("wxMenu"))
196 PreviewMenu();
197 else if (XmlGetClass(node) == _T("wxToolBar"))
198 PreviewToolbar();
199 else if (XmlGetClass(node) == _T("wxPanel") || XmlGetClass(node) == _T("wxDialog"))
200 PreviewPanel();
6267a331 201 else if (XmlGetClass(node) == _T("wxFrame"))
f80ea77b 202 PreviewWXFrame();
6267a331 203
e066e256 204 wxSetWorkingDirectory(oldcwd);
fa0d3447 205#if wxUSE_LOG
e066e256 206 wxLog::SetActiveTarget(oldlog);
fa0d3447 207#endif // wxUSE_LOG
f80ea77b
WS
208
209 m_Dirty = false;
e066e256
VS
210}
211
212
213
214void PreviewFrame::PreviewMenu()
215{
859e5b17 216 wxMenuBar *mbar = NULL;
e066e256
VS
217
218 if (XmlGetClass(m_Node) == _T("wxMenuBar"))
219 mbar = m_RC->LoadMenuBar(m_Node->GetPropVal(_T("name"), _T("-1")));
220 else
221 {
222 wxMenu *m = m_RC->LoadMenu(m_Node->GetPropVal(_T("name"), _T("-1")));
223 if (m != NULL) GetMenuBar()->Append(m, _("(menu)"));
224 }
225
226 if (mbar == NULL)
227 wxLogError(_("Cannot preview the menu -- XML resource corrupted."));
228}
229
230
231
232void PreviewFrame::PreviewToolbar()
233{
234 SetToolBar(m_RC->LoadToolBar(this, m_Node->GetPropVal(_T("name"), _T("-1"))));
235}
236
237
238
239void PreviewFrame::PreviewPanel()
240{
241 wxPanel *panel = m_RC->LoadPanel(m_ScrollWin, m_Node->GetPropVal(_T("name"), _T("-1")));
6267a331 242
e066e256
VS
243 if (panel == NULL)
244 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
245 else
246 {
247 m_ScrollWin->SetScrollbars(1, 1, panel->GetSize().x, panel->GetSize().y,
f80ea77b 248 0, 0, true);
e066e256
VS
249 }
250}
26607f41 251
6267a331
VS
252void PreviewFrame::PreviewWXFrame()
253{
f80ea77b
WS
254 //for this to work the frame MUST have a child panel!
255
256 wxXmlNode* child = m_Node;
257 wxString name;
258
259 while( child != NULL)
260 {
261 name = child->GetPropVal(_T("name"), _T("-1"));
262
263 if(name != _T("-1"))
264 {
265 wxXmlNode* parent = child->GetParent();
266 if(parent->GetPropVal(_T("class"),_T("-1")) == _T("wxPanel"))
267 break;
268 }
269 child = child->GetNext();
270 }
6267a331
VS
271
272 wxPanel *panel = m_RC->LoadPanel(m_ScrollWin, name);
f80ea77b 273
6267a331
VS
274 if (panel == NULL)
275 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
276 else
277 {
278 m_ScrollWin->SetScrollbars(1, 1, panel->GetSize().x, panel->GetSize().y,
f80ea77b 279 0, 0, true);
6267a331
VS
280 }
281
282}
26607f41 283
26607f41
VS
284BEGIN_EVENT_TABLE(PreviewFrame, wxFrame)
285 EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter)
286END_EVENT_TABLE()
287
859e5b17 288void PreviewFrame::OnMouseEnter(wxMouseEvent& WXUNUSED(event))
26607f41 289{
2b5f62a0 290 if (m_Dirty) Preview(m_Node,m_Doc);
26607f41 291}