1 /////////////////////////////////////////////////////////////////////////////
2 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "propframe.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/config.h"
23 #include "splittree.h"
25 #include "propframe.h"
31 // ------------- support classes --------
34 class PropsTree
: public wxRemotelyScrolledTreeCtrl
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
),
42 void OnPaint(wxPaintEvent
& event
)
46 wxTreeCtrl::OnPaint(event
);
48 // Reset the device origin since it may have been set
49 dc
.SetDeviceOrigin(0, 0);
51 wxPen
pen(wxColour(_T("BLACK")), 1, wxSOLID
);
54 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
56 wxSize clientSize
= GetClientSize();
59 wxTreeItemId h
, lastH
;
60 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
62 if (h
.IsOk() && GetBoundingRect(h
, itemRect
))
64 cy
= itemRect
.GetTop();
65 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
69 if (lastH
.IsOk() && GetBoundingRect(lastH
, itemRect
))
71 cy
= itemRect
.GetBottom();
72 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
76 void OnSelChange(wxTreeEvent
& event
)
78 if (m_EditCtrl
!= NULL
)
80 m_EditCtrl
->EndEdit();
84 wxTreeItemId item
= event
.GetItem();
85 PETreeData
*dt
= (PETreeData
*)GetItemData(item
);
89 GetBoundingRect(item
, bounding
);
91 bounding
.SetWidth(GetCompanionWindow()->GetSize().x
);
92 dt
->EditCtrl
->BeginEdit(bounding
, item
);
93 m_EditCtrl
= dt
->EditCtrl
;
100 AddRoot(_("Properties"));
103 m_EditCtrl
->EndEdit();
108 PropEditCtrl
*m_EditCtrl
;
110 DECLARE_EVENT_TABLE()
113 BEGIN_EVENT_TABLE(PropsTree
, wxRemotelyScrolledTreeCtrl
)
114 EVT_PAINT(PropsTree::OnPaint
)
115 EVT_TREE_SEL_CHANGED(-1, PropsTree::OnSelChange
)
119 class PropsValueWindow
: public wxTreeCompanionWindow
122 PropsValueWindow(wxWindow
* parent
, wxWindowID id
= -1,
123 const wxPoint
& pos
= wxDefaultPosition
,
124 const wxSize
& sz
= wxDefaultSize
,
126 : wxTreeCompanionWindow(parent
, id
, pos
, sz
, style
) {}
128 virtual void DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
132 PETreeData
*data
= (PETreeData
*)m_treeCtrl
->GetItemData(id
);
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
);
141 dc
.GetTextExtent(text
, & textW
, & textH
);
144 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
146 dc
.DrawText(text
, x
, y
);
150 void OnClick(wxMouseEvent
& event
)
153 wxTreeItemId item
= GetTreeCtrl()->HitTest(wxPoint(1, event
.GetY()), flags
);
156 GetTreeCtrl()->ScrollTo(item
);
157 GetTreeCtrl()->SelectItem(item
);
161 DECLARE_EVENT_TABLE()
164 BEGIN_EVENT_TABLE(PropsValueWindow
, wxTreeCompanionWindow
)
165 EVT_LEFT_DOWN(PropsValueWindow::OnClick
)
170 // ------------- properties frame --------
174 PropertiesFrame
* PropertiesFrame::ms_Instance
= NULL
;
176 PropertiesFrame
*PropertiesFrame::Get()
178 if (ms_Instance
== NULL
)
180 (void)new PropertiesFrame
;
181 ms_Instance
->Show(TRUE
);
186 PropertiesFrame::PropertiesFrame()
187 : wxFrame(NULL
, -1, _("Properties"))
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);
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
);
208 m_scrolledWindow
->EnableScrolling(FALSE
, FALSE
);
210 // Let the two controls know about each other
211 m_valueWindow
->SetTreeCtrl(m_tree
);
212 m_tree
->SetCompanionWindow(m_valueWindow
);
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))));
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 PropEditCtrlFlags(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("file"), new PropEditCtrlFile(this));
232 m_EditCtrls
.Put(_T("imagefile"), new PropEditCtrlImageFile(this));
233 m_EditCtrls
.Put(_T(""), new PropEditCtrlNull(this));
240 PropertiesFrame::~PropertiesFrame()
242 wxConfigBase
*cfg
= wxConfigBase::Get();
243 cfg
->Write(_T("propertiesframe_x"), (long)GetPosition().x
);
244 cfg
->Write(_T("propertiesframe_y"), (long)GetPosition().y
);
245 cfg
->Write(_T("propertiesframe_w"), (long)GetSize().x
);
246 cfg
->Write(_T("propertiesframe_h"), (long)GetSize().y
);
247 cfg
->Write(_T("propertiesframe_sash"), (long)m_splitter
->GetSashPosition());
254 void PropertiesFrame::ShowProps(wxXmlNode
*node
)
259 AddSingleProp(PropertyInfo(_T("xmlid"), _T("XMLID"), wxEmptyString
));
260 AddProps(NodeHandler::Find(node
)->GetPropsList(node
));
262 m_tree
->Expand(m_tree
->GetRootItem());
263 m_valueWindow
->Refresh();
268 void PropertiesFrame::ClearProps()
270 ((PropsTree
*)m_tree
)->ClearProps();
275 void PropertiesFrame::AddProps(PropertyInfoArray
& plist
)
277 for (size_t i
= 0; i
< plist
.GetCount(); i
++)
279 AddSingleProp(plist
[i
]);
285 void PropertiesFrame::AddSingleProp(const PropertyInfo
& pinfo
, wxTreeItemId
*root
)
287 PropEditCtrl
*pec
= (PropEditCtrl
*)m_EditCtrls
.Get(pinfo
.Type
);
289 if (root
!= NULL
) tid
= *root
;
290 else tid
= m_tree
->GetRootItem();
293 wxLogError(_("Unknown property type '%s'!"), pinfo
.Type
.c_str());
295 pec
->CreateTreeEntry(tid
, pinfo
);