]>
git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/preview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "preview.h"
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
21 #include "wx/xml/xml.h"
22 #include "wx/xrc/xmlres.h"
23 #include "wx/config.h"
25 #include "wx/splitter.h"
30 #include "wx/xrc/xh_menu.h"
32 class MyMenubarHandler
: public wxMenuBarXmlHandler
35 MyMenubarHandler(wxMenuBar
*mbar
) : m_MenuBar(mbar
) {}
37 wxObject
*DoCreateResource()
39 //wxMenuBar *menubar = new wxMenuBar(GetStyle());
40 CreateChildren(m_MenuBar
);
51 PreviewFrame
* PreviewFrame::ms_Instance
= NULL
;
53 PreviewFrame
*PreviewFrame::Get()
55 if (ms_Instance
== NULL
)
57 (void)new PreviewFrame
;
58 ms_Instance
->Show(true);
63 PreviewFrame::PreviewFrame()
64 : wxFrame(EditorFrame::Get(), wxID_ANY
, _("Preview"),
65 wxDefaultPosition
, wxDefaultSize
,
66 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
76 SetMenuBar(new wxMenuBar());
79 m_TmpFile
= wxGetTempFileName(_T("wxrcedit"));
82 wxConfigBase
*cfg
= wxConfigBase::Get();
83 SetSize(wxRect(wxPoint(cfg
->Read(_T("previewframe_x"), wxDefaultCoord
), cfg
->Read(_T("previewframe_y"), wxDefaultCoord
)),
84 wxSize(cfg
->Read(_T("previewframe_w"), 400), cfg
->Read(_T("previewframe_h"), 400))));
86 m_Splitter
= new wxSplitterWindow(this, wxID_ANY
);
87 m_LogCtrl
= new wxTextCtrl(m_Splitter
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
88 wxDefaultSize
, wxTE_MULTILINE
);
89 m_ScrollWin
= new wxScrolledWindow(m_Splitter
, wxID_ANY
);
90 m_ScrollWin
->SetBackgroundColour(_T("light steel blue"));
91 m_Splitter
->SplitHorizontally(m_ScrollWin
, m_LogCtrl
, cfg
->Read(_T("previewframe_sash"), 300));
95 #endif // wxUSE_STATUSBAR
98 SendSizeEvent(); // force resize for WXMSW
104 void PreviewFrame::ResetResource()
107 m_RC
= new wxXmlResource
;
108 // these handlers take precedence over std. ones:
109 m_RC
->AddHandler(new MyMenubarHandler(GetMenuBar()));
111 m_RC
->InitAllHandlers();
112 wxRemoveFile(m_TmpFile
);
113 m_RC
->Load(m_TmpFile
);
116 PreviewFrame::~PreviewFrame()
118 wxConfigBase
*cfg
= wxConfigBase::Get();
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());
128 wxRemoveFile(m_TmpFile
);
133 void PreviewFrame::MakeDirty()
135 if (m_Node
== NULL
) return;
139 m_LogCtrl
->SetValue(_("Resource modified.\nMove mouse cursor over the preview window to refresh it."));
144 void PreviewFrame::Preview(wxXmlNode
*node
, wxXmlDocument
*orig_doc
)
146 wxString version
= orig_doc
->GetRoot()->GetPropVal(wxT("version"), wxT("0.0.0.0"));
148 while (node
->GetParent()->GetParent() != NULL
) node
= node
->GetParent();
152 wxXmlNode
*root
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("resource"));
153 root
->AddProperty(new wxXmlProperty(wxT("version"),version
,NULL
));
155 doc
.GetRoot()->AddChild(new wxXmlNode(*node
));
156 doc
.SetFileEncoding(orig_doc
->GetFileEncoding());
158 if (XmlGetClass(doc
.GetRoot()->GetChildren()) == _T("wxDialog"))
159 XmlSetClass(doc
.GetRoot()->GetChildren(), _T("wxPanel"));
161 if (XmlGetClass(doc
.GetRoot()->GetChildren()) == _T("wxFrame"))
162 XmlSetClass(doc
.GetRoot()->GetChildren(), _T("wxPanel"));
165 // wxXmlResource will detect change automatically
169 //if (m_Node != node)
172 for (int i
= (int)(GetMenuBar()->GetMenuCount())-1; i
>= 0; i
--)
173 delete GetMenuBar()->Remove(i
);
174 m_ScrollWin
->DestroyChildren();
181 wxLogTextCtrl
mylog(m_LogCtrl
);
182 wxLog
*oldlog
= wxLog::SetActiveTarget(&mylog
);
184 wxString oldcwd
= wxGetCwd();
185 wxSetWorkingDirectory(wxPathOnly(EditorFrame::Get()->GetFileName()));
187 if (XmlGetClass(node
) == _T("wxMenuBar") || XmlGetClass(node
) == _T("wxMenu"))
189 else if (XmlGetClass(node
) == _T("wxToolBar"))
191 else if (XmlGetClass(node
) == _T("wxPanel") || XmlGetClass(node
) == _T("wxDialog"))
193 else if (XmlGetClass(node
) == _T("wxFrame"))
196 wxSetWorkingDirectory(oldcwd
);
197 wxLog::SetActiveTarget(oldlog
);
204 void PreviewFrame::PreviewMenu()
206 wxMenuBar
*mbar
= NULL
;
208 if (XmlGetClass(m_Node
) == _T("wxMenuBar"))
209 mbar
= m_RC
->LoadMenuBar(m_Node
->GetPropVal(_T("name"), _T("-1")));
212 wxMenu
*m
= m_RC
->LoadMenu(m_Node
->GetPropVal(_T("name"), _T("-1")));
213 if (m
!= NULL
) GetMenuBar()->Append(m
, _("(menu)"));
217 wxLogError(_("Cannot preview the menu -- XML resource corrupted."));
222 void PreviewFrame::PreviewToolbar()
224 SetToolBar(m_RC
->LoadToolBar(this, m_Node
->GetPropVal(_T("name"), _T("-1"))));
229 void PreviewFrame::PreviewPanel()
231 wxPanel
*panel
= m_RC
->LoadPanel(m_ScrollWin
, m_Node
->GetPropVal(_T("name"), _T("-1")));
234 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
237 m_ScrollWin
->SetScrollbars(1, 1, panel
->GetSize().x
, panel
->GetSize().y
,
242 void PreviewFrame::PreviewWXFrame()
244 //for this to work the frame MUST have a child panel!
246 wxXmlNode
* child
= m_Node
;
249 while( child
!= NULL
)
251 name
= child
->GetPropVal(_T("name"), _T("-1"));
255 wxXmlNode
* parent
= child
->GetParent();
256 if(parent
->GetPropVal(_T("class"),_T("-1")) == _T("wxPanel"))
259 child
= child
->GetNext();
262 wxPanel
*panel
= m_RC
->LoadPanel(m_ScrollWin
, name
);
265 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
268 m_ScrollWin
->SetScrollbars(1, 1, panel
->GetSize().x
, panel
->GetSize().y
,
274 BEGIN_EVENT_TABLE(PreviewFrame
, wxFrame
)
275 EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter
)
278 void PreviewFrame::OnMouseEnter(wxMouseEvent
& WXUNUSED(event
))
280 if (m_Dirty
) Preview(m_Node
,m_Doc
);