]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/preview.cpp
various bugfixes
[wxWidgets.git] / contrib / utils / wxrcedit / preview.cpp
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"
21 #include "wx/xml/xml.h"
22 #include "wx/xml/xmlres.h"
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
30 #include "wx/xml/xh_menu.h"
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()
64 : wxFrame(NULL, -1, _("Preview"))
65 {
66 m_Dirty = FALSE;
67 ms_Instance = this;
68 m_Node = NULL;
69
70 SetMenuBar(new wxMenuBar());
71
72 m_RC = new wxXmlResource;
73 // these handlers take precedence over std. ones:
74 m_RC->AddHandler(new MyMenubarHandler(GetMenuBar()));
75 // std handlers:
76 m_RC->InitAllHandlers();
77 m_TmpFile = wxGetTempFileName(_T("wxrcedit"));
78 m_RC->Load(m_TmpFile);
79
80 wxConfigBase *cfg = wxConfigBase::Get();
81 SetSize(wxRect(wxPoint(cfg->Read(_T("previewframe_x"), -1), cfg->Read(_T("previewframe_y"), -1)),
82 wxSize(cfg->Read(_T("previewframe_w"), 400), cfg->Read(_T("previewframe_h"), 400))));
83
84 m_Splitter = new wxSplitterWindow(this, -1);
85 m_LogCtrl = new wxTextCtrl(m_Splitter, -1, wxEmptyString, wxDefaultPosition,
86 wxDefaultSize, wxTE_MULTILINE);
87 m_ScrollWin = new wxScrolledWindow(m_Splitter, -1);
88 m_ScrollWin->SetBackgroundColour(_T("light steel blue"));
89 m_Splitter->SplitHorizontally(m_ScrollWin, m_LogCtrl, cfg->Read(_T("previewframe_sash"), 300));
90
91 CreateStatusBar();
92 }
93
94
95
96 PreviewFrame::~PreviewFrame()
97 {
98 wxConfigBase *cfg = wxConfigBase::Get();
99 cfg->Write(_T("previewframe_x"), (long)GetPosition().x);
100 cfg->Write(_T("previewframe_y"), (long)GetPosition().y);
101 cfg->Write(_T("previewframe_w"), (long)GetSize().x);
102 cfg->Write(_T("previewframe_h"), (long)GetSize().y);
103 cfg->Write(_T("previewframe_sash"), (long)m_Splitter->GetSashPosition());
104
105 ms_Instance = NULL;
106
107 delete m_RC;
108 wxRemoveFile(m_TmpFile);
109 }
110
111
112
113 void PreviewFrame::MakeDirty()
114 {
115 if (m_Node == NULL) return;
116 if (m_Dirty) return;
117 m_Dirty = TRUE;
118 m_LogCtrl->Clear();
119 m_LogCtrl->SetValue(_("Resource modified.\n"
120 "Move mouse cursor over the preview window to refresh it."));
121 }
122
123
124
125 void PreviewFrame::Preview(wxXmlNode *node)
126 {
127 while (node->GetParent()->GetParent() != NULL) node = node->GetParent();
128
129 {
130 wxXmlDocument doc;
131 doc.SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, _T("resource")));
132 doc.GetRoot()->AddChild(new wxXmlNode(*node));
133
134 if (XmlGetClass(doc.GetRoot()->GetChildren()) == _T("wxDialog"))
135 XmlSetClass(doc.GetRoot()->GetChildren(), _T("wxPanel"));
136
137 doc.Save(m_TmpFile, wxXML_IO_BIN);
138 // wxXmlResource will detect change automatically
139 }
140
141
142 //if (m_Node != node)
143 {
144 SetToolBar(NULL);
145 for (int i = (int)(GetMenuBar()->GetMenuCount())-1; i >= 0; i--)
146 delete GetMenuBar()->Remove(i);
147 m_ScrollWin->DestroyChildren();
148 }
149
150 m_Node = node;
151
152 m_LogCtrl->Clear();
153 wxLogTextCtrl mylog(m_LogCtrl);
154 wxLog *oldlog = wxLog::SetActiveTarget(&mylog);
155
156 wxString oldcwd = wxGetCwd();
157 wxSetWorkingDirectory(wxPathOnly(EditorFrame::Get()->GetFileName()));
158
159 if (XmlGetClass(node) == _T("wxMenuBar") || XmlGetClass(node) == _T("wxMenu"))
160 PreviewMenu();
161 else if (XmlGetClass(node) == _T("wxToolBar"))
162 PreviewToolbar();
163 else if (XmlGetClass(node) == _T("wxPanel") || XmlGetClass(node) == _T("wxDialog"))
164 PreviewPanel();
165
166 wxSetWorkingDirectory(oldcwd);
167 wxLog::SetActiveTarget(oldlog);
168
169 m_Dirty = FALSE;
170 }
171
172
173
174 void PreviewFrame::PreviewMenu()
175 {
176 wxMenuBar *mbar;
177
178 if (XmlGetClass(m_Node) == _T("wxMenuBar"))
179 mbar = m_RC->LoadMenuBar(m_Node->GetPropVal(_T("name"), _T("-1")));
180 else
181 {
182 wxMenu *m = m_RC->LoadMenu(m_Node->GetPropVal(_T("name"), _T("-1")));
183 if (m != NULL) GetMenuBar()->Append(m, _("(menu)"));
184 }
185
186 if (mbar == NULL)
187 wxLogError(_("Cannot preview the menu -- XML resource corrupted."));
188 }
189
190
191
192 void PreviewFrame::PreviewToolbar()
193 {
194 SetToolBar(m_RC->LoadToolBar(this, m_Node->GetPropVal(_T("name"), _T("-1"))));
195 }
196
197
198
199 void PreviewFrame::PreviewPanel()
200 {
201 wxPanel *panel = m_RC->LoadPanel(m_ScrollWin, m_Node->GetPropVal(_T("name"), _T("-1")));
202
203 if (panel == NULL)
204 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
205 else
206 {
207 m_ScrollWin->SetScrollbars(1, 1, panel->GetSize().x, panel->GetSize().y,
208 0, 0, TRUE);
209 }
210 }
211
212
213
214 BEGIN_EVENT_TABLE(PreviewFrame, wxFrame)
215 EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter)
216 END_EVENT_TABLE()
217
218 void PreviewFrame::OnMouseEnter(wxMouseEvent& event)
219 {
220 if (m_Dirty) Preview(m_Node);
221 }