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