]>
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/xml/xmlres.h"
23 #include "wx/config.h"
25 #include "wx/splitter.h"
30 #include "wx/xml/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(), -1, _("Preview"),
65 wxDefaultPosition
, wxDefaultSize
,
66 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
76 SetMenuBar(new wxMenuBar());
78 m_RC
= new wxXmlResource
;
79 // these handlers take precedence over std. ones:
80 m_RC
->AddHandler(new MyMenubarHandler(GetMenuBar()));
82 m_RC
->InitAllHandlers();
83 m_TmpFile
= wxGetTempFileName(_T("wxrcedit"));
84 m_RC
->Load(m_TmpFile
);
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))));
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));
99 SetSize(GetSize()); // refresh: MSW needs it
104 PreviewFrame::~PreviewFrame()
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());
116 wxRemoveFile(m_TmpFile
);
121 void PreviewFrame::MakeDirty()
123 if (m_Node
== NULL
) return;
127 m_LogCtrl
->SetValue(_("Resource modified.\n"
128 "Move mouse cursor over the preview window to refresh it."));
133 void PreviewFrame::Preview(wxXmlNode
*node
)
135 while (node
->GetParent()->GetParent() != NULL
) node
= node
->GetParent();
139 doc
.SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE
, _T("resource")));
140 doc
.GetRoot()->AddChild(new wxXmlNode(*node
));
142 if (XmlGetClass(doc
.GetRoot()->GetChildren()) == _T("wxDialog"))
143 XmlSetClass(doc
.GetRoot()->GetChildren(), _T("wxPanel"));
145 doc
.Save(m_TmpFile
, wxXML_IO_BIN
);
146 // wxXmlResource will detect change automatically
150 //if (m_Node != node)
153 for (int i
= (int)(GetMenuBar()->GetMenuCount())-1; i
>= 0; i
--)
154 delete GetMenuBar()->Remove(i
);
155 m_ScrollWin
->DestroyChildren();
161 wxLogTextCtrl
mylog(m_LogCtrl
);
162 wxLog
*oldlog
= wxLog::SetActiveTarget(&mylog
);
164 wxString oldcwd
= wxGetCwd();
165 wxSetWorkingDirectory(wxPathOnly(EditorFrame::Get()->GetFileName()));
167 if (XmlGetClass(node
) == _T("wxMenuBar") || XmlGetClass(node
) == _T("wxMenu"))
169 else if (XmlGetClass(node
) == _T("wxToolBar"))
171 else if (XmlGetClass(node
) == _T("wxPanel") || XmlGetClass(node
) == _T("wxDialog"))
174 wxSetWorkingDirectory(oldcwd
);
175 wxLog::SetActiveTarget(oldlog
);
182 void PreviewFrame::PreviewMenu()
186 if (XmlGetClass(m_Node
) == _T("wxMenuBar"))
187 mbar
= m_RC
->LoadMenuBar(m_Node
->GetPropVal(_T("name"), _T("-1")));
190 wxMenu
*m
= m_RC
->LoadMenu(m_Node
->GetPropVal(_T("name"), _T("-1")));
191 if (m
!= NULL
) GetMenuBar()->Append(m
, _("(menu)"));
195 wxLogError(_("Cannot preview the menu -- XML resource corrupted."));
200 void PreviewFrame::PreviewToolbar()
202 SetToolBar(m_RC
->LoadToolBar(this, m_Node
->GetPropVal(_T("name"), _T("-1"))));
207 void PreviewFrame::PreviewPanel()
209 wxPanel
*panel
= m_RC
->LoadPanel(m_ScrollWin
, m_Node
->GetPropVal(_T("name"), _T("-1")));
212 wxLogError(_("Cannot preview the panel -- XML resource corrupted."));
215 m_ScrollWin
->SetScrollbars(1, 1, panel
->GetSize().x
, panel
->GetSize().y
,
222 BEGIN_EVENT_TABLE(PreviewFrame
, wxFrame
)
223 EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter
)
226 void PreviewFrame::OnMouseEnter(wxMouseEvent
& event
)
228 if (m_Dirty
) Preview(m_Node
);