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