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