]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/propframe.cpp
Removed fstream include which seemed unnecessary, and gave errors for BC++ 5.5 anyway.
[wxWidgets.git] / contrib / utils / wxrcedit / propframe.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 "propframe.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/xrc/xml.h"
22 #include "wx/config.h"
23 #include "splittree.h"
24 #include "xmlhelpr.h"
25 #include "propframe.h"
26 #include "nodehnd.h"
27 #include "propedit.h"
28 #include "pe_basic.h"
29 #include "pe_adv.h"
30 #include "editor.h"
31
32 // ------------- support classes --------
33
34
35 class PropsTree: public wxRemotelyScrolledTreeCtrl
36 {
37 public:
38 PropsTree(wxWindow* parent, wxWindowID id, const wxPoint& pt = wxDefaultPosition,
39 const wxSize& sz = wxDefaultSize, long style = wxTR_HAS_BUTTONS)
40 : wxRemotelyScrolledTreeCtrl(parent, id, pt, sz, style),
41 m_EditCtrl(NULL) {}
42
43 void OnPaint(wxPaintEvent& event)
44 {
45 wxPaintDC dc(this);
46
47 wxTreeCtrl::OnPaint(event);
48
49 // Reset the device origin since it may have been set
50 dc.SetDeviceOrigin(0, 0);
51
52 wxPen pen(wxColour(_T("BLACK")), 1, wxSOLID);
53 dc.SetPen(pen);
54
55 dc.SetBrush(* wxTRANSPARENT_BRUSH);
56
57 wxSize clientSize = GetClientSize();
58 wxRect itemRect;
59 int cy=0;
60 wxTreeItemId h, lastH;
61 for(h=GetFirstVisibleItem();h;h=GetNextVisible(h))
62 {
63 if (h.IsOk() && GetBoundingRect(h, itemRect))
64 {
65 cy = itemRect.GetTop();
66 dc.DrawLine(0, cy, clientSize.x, cy);
67 lastH = h;
68 }
69 }
70 if (lastH.IsOk() && GetBoundingRect(lastH, itemRect))
71 {
72 cy = itemRect.GetBottom();
73 dc.DrawLine(0, cy, clientSize.x, cy);
74 }
75 }
76
77 void OnSelChange(wxTreeEvent& event)
78 {
79 if (m_EditCtrl != NULL)
80 {
81 m_EditCtrl->EndEdit();
82 m_EditCtrl = NULL;
83 }
84
85 wxTreeItemId item = event.GetItem();
86 PETreeData *dt = (PETreeData*)GetItemData(item);
87 if (dt != NULL)
88 {
89 wxRect bounding;
90 GetBoundingRect(item, bounding);
91 bounding.SetX(0);
92 bounding.SetWidth(GetCompanionWindow()->GetSize().x);
93 dt->EditCtrl->BeginEdit(bounding, item);
94 m_EditCtrl = dt->EditCtrl;
95 }
96 }
97
98 void ClearProps()
99 {
100 DeleteAllItems();
101 AddRoot(_("Properties"));
102 if (m_EditCtrl)
103 {
104 m_EditCtrl->EndEdit();
105 m_EditCtrl = NULL;
106 }
107 }
108
109 void OnScroll(wxScrollWinEvent& event)
110 {
111 event.Skip();
112 if (event.GetOrientation() == wxHORIZONTAL) return;
113 if (!m_EditCtrl) return;
114
115 wxTreeItemId id = GetSelection();
116 wxRect bounding;
117 GetBoundingRect(id, bounding);
118
119 m_EditCtrl->Move(-1, bounding.y);
120 }
121
122 PropEditCtrl *m_EditCtrl;
123
124 DECLARE_EVENT_TABLE()
125 };
126
127 BEGIN_EVENT_TABLE(PropsTree, wxRemotelyScrolledTreeCtrl)
128 EVT_PAINT(PropsTree::OnPaint)
129 EVT_TREE_SEL_CHANGED(-1, PropsTree::OnSelChange)
130 EVT_SCROLLWIN(PropsTree::OnScroll)
131 END_EVENT_TABLE()
132
133
134 class PropsValueWindow: public wxTreeCompanionWindow
135 {
136 public:
137 PropsValueWindow(wxWindow* parent, wxWindowID id = -1,
138 const wxPoint& pos = wxDefaultPosition,
139 const wxSize& sz = wxDefaultSize,
140 long style = 0)
141 : wxTreeCompanionWindow(parent, id, pos, sz, style) {}
142
143 virtual void DrawItem(wxDC& dc, wxTreeItemId id, const wxRect& rect)
144 {
145 if (m_treeCtrl)
146 {
147 PETreeData *data = (PETreeData*)m_treeCtrl->GetItemData(id);
148 wxString text;
149 if (data != NULL) text = data->EditCtrl->GetValueAsText(id);
150 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
151 dc.DrawRectangle(rect);
152 dc.SetTextForeground(* wxBLACK);
153 dc.SetBackgroundMode(wxTRANSPARENT);
154
155 int textW, textH;
156 dc.GetTextExtent(text, & textW, & textH);
157
158 int x = 5;
159 int y = rect.GetY() + wxMax(0, (rect.GetHeight() - textH) / 2);
160
161 dc.DrawText(text, x, y);
162 }
163 }
164
165 void OnClick(wxMouseEvent& event)
166 {
167 int flags;
168 wxTreeItemId item = GetTreeCtrl()->HitTest(wxPoint(1, event.GetY()), flags);
169 if (item.IsOk())
170 {
171 GetTreeCtrl()->ScrollTo(item);
172 GetTreeCtrl()->SelectItem(item);
173 }
174 }
175
176 DECLARE_EVENT_TABLE()
177 };
178
179 BEGIN_EVENT_TABLE(PropsValueWindow, wxTreeCompanionWindow)
180 EVT_LEFT_DOWN(PropsValueWindow::OnClick)
181 END_EVENT_TABLE()
182
183
184
185 // ------------- properties frame --------
186
187
188
189 PropertiesFrame* PropertiesFrame::ms_Instance = NULL;
190
191 PropertiesFrame *PropertiesFrame::Get()
192 {
193 if (ms_Instance == NULL)
194 {
195 (void)new PropertiesFrame;
196 ms_Instance->Show(TRUE);
197 }
198 return ms_Instance;
199 }
200
201 PropertiesFrame::PropertiesFrame()
202 : wxFrame(EditorFrame::Get(), -1, _("Properties"),
203 wxDefaultPosition, wxDefaultSize,
204 wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR
205 #ifdef __WXMSW__
206 | wxFRAME_TOOL_WINDOW
207 #endif
208 )
209 {
210 ms_Instance = this;
211 m_Node = NULL;
212
213 m_scrolledWindow = new wxSplitterScrolledWindow(this, -1, wxDefaultPosition,
214 wxDefaultSize, wxNO_BORDER | wxCLIP_CHILDREN | wxVSCROLL);
215 m_splitter = new wxThinSplitterWindow(m_scrolledWindow, -1, wxDefaultPosition,
216 wxDefaultSize, wxSP_3DBORDER | wxCLIP_CHILDREN /* | wxSP_LIVE_UPDATE */);
217 m_splitter->SetSashSize(2);
218 m_tree = new PropsTree(m_splitter, -1, wxDefaultPosition,
219 wxDefaultSize, wxTR_HAS_BUTTONS | wxTR_NO_LINES | wxNO_BORDER );
220 m_tree->SetIndent(2);
221
222 m_valueWindow = new PropsValueWindow(m_splitter, -1, wxDefaultPosition,
223 wxDefaultSize, wxNO_BORDER);
224 m_valueWindow->SetBackgroundColour(m_tree->GetBackgroundColour());
225 m_splitter->SplitVertically(m_tree, m_valueWindow, wxConfig::Get()->Read(_T("propertiesframe_sash"), 100));
226 //m_splitter->AdjustScrollbars();
227 m_scrolledWindow->SetTargetWindow(m_tree);
228
229 m_scrolledWindow->EnableScrolling(FALSE, FALSE);
230
231 // Let the two controls know about each other
232 m_valueWindow->SetTreeCtrl(m_tree);
233 m_tree->SetCompanionWindow(m_valueWindow);
234
235 wxConfigBase *cfg = wxConfigBase::Get();
236 SetSize(wxRect(wxPoint(cfg->Read(_T("propertiesframe_x"), -1), cfg->Read(_T("propertiesframe_y"), -1)),
237 wxSize(cfg->Read(_T("propertiesframe_w"), 200), cfg->Read(_T("propertiesframe_h"), 200))));
238
239
240 m_EditCtrls.DeleteContents(TRUE);
241 m_EditCtrls.Put(_T("bool"), new PropEditCtrlBool(this));
242 m_EditCtrls.Put(_T("coord"), new PropEditCtrlCoord(this));
243 m_EditCtrls.Put(_T("color"), new PropEditCtrlColor(this));
244 m_EditCtrls.Put(_T("dimension"), new PropEditCtrlDim(this));
245 m_EditCtrls.Put(_T("flags"), new PropEditCtrlFlags(this));
246 m_EditCtrls.Put(_T("integer"), new PropEditCtrlInt(this));
247 m_EditCtrls.Put(_T("not_implemented"), new PropEditCtrlNull(this));
248 m_EditCtrls.Put(_T("text"), new PropEditCtrlTxt(this));
249 m_EditCtrls.Put(_T("xmlid"), new PropEditCtrlXMLID(this));
250 m_EditCtrls.Put(_T("font"), new PropEditCtrlFont(this));
251 m_EditCtrls.Put(_T("choice"), new PropEditCtrlChoice(this));
252 m_EditCtrls.Put(_T("file"), new PropEditCtrlFile(this));
253 m_EditCtrls.Put(_T("imagefile"), new PropEditCtrlImageFile(this));
254 m_EditCtrls.Put(_T(""), new PropEditCtrlNull(this));
255
256 ClearProps();
257 }
258
259
260
261 PropertiesFrame::~PropertiesFrame()
262 {
263 wxConfigBase *cfg = wxConfigBase::Get();
264 cfg->Write(_T("propertiesframe_x"), (long)GetPosition().x);
265 cfg->Write(_T("propertiesframe_y"), (long)GetPosition().y);
266 cfg->Write(_T("propertiesframe_w"), (long)GetSize().x);
267 cfg->Write(_T("propertiesframe_h"), (long)GetSize().y);
268 cfg->Write(_T("propertiesframe_sash"), (long)m_splitter->GetSashPosition());
269
270 ms_Instance = NULL;
271 }
272
273
274
275 void PropertiesFrame::ShowProps(wxXmlNode *node)
276 {
277 m_Node = node;
278
279 ClearProps();
280 AddSingleProp(PropertyInfo(_T("xmlid"), _T("XMLID"), wxEmptyString));
281 AddProps(NodeHandler::Find(node)->GetPropsList(node));
282
283 m_tree->Expand(m_tree->GetRootItem());
284 m_valueWindow->Refresh();
285 }
286
287
288
289 void PropertiesFrame::ClearProps()
290 {
291 ((PropsTree*)m_tree)->ClearProps();
292 }
293
294
295
296 void PropertiesFrame::AddProps(PropertyInfoArray& plist)
297 {
298 for (size_t i = 0; i < plist.GetCount(); i++)
299 {
300 AddSingleProp(plist[i]);
301 }
302 }
303
304
305
306 void PropertiesFrame::AddSingleProp(const PropertyInfo& pinfo, wxTreeItemId *root)
307 {
308 PropEditCtrl *pec = (PropEditCtrl*)m_EditCtrls.Get(pinfo.Type);
309 wxTreeItemId tid;
310 if (root != NULL) tid = *root;
311 else tid = m_tree->GetRootItem();
312
313 if (pec == NULL)
314 wxLogError(_("Unknown property type '%s'!"), pinfo.Type.c_str());
315 else
316 pec->CreateTreeEntry(tid, pinfo);
317 }
318