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"
32 // ------------- support classes --------
35 class PropsTree
: public wxRemotelyScrolledTreeCtrl
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
),
43 void OnPaint(wxPaintEvent
& event
)
47 wxTreeCtrl::OnPaint(event
);
49 // Reset the device origin since it may have been set
50 dc
.SetDeviceOrigin(0, 0);
52 wxPen
pen(wxColour(_T("BLACK")), 1, wxSOLID
);
55 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
57 wxSize clientSize
= GetClientSize();
60 wxTreeItemId h
, lastH
;
61 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
63 if (h
.IsOk() && GetBoundingRect(h
, itemRect
))
65 cy
= itemRect
.GetTop();
66 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
70 if (lastH
.IsOk() && GetBoundingRect(lastH
, itemRect
))
72 cy
= itemRect
.GetBottom();
73 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
77 void OnSelChange(wxTreeEvent
& event
)
79 if (m_EditCtrl
!= NULL
)
81 m_EditCtrl
->EndEdit();
85 wxTreeItemId item
= event
.GetItem();
86 PETreeData
*dt
= (PETreeData
*)GetItemData(item
);
90 GetBoundingRect(item
, bounding
);
92 bounding
.SetWidth(GetCompanionWindow()->GetSize().x
);
93 dt
->EditCtrl
->BeginEdit(bounding
, item
);
94 m_EditCtrl
= dt
->EditCtrl
;
101 AddRoot(_("Properties"));
104 m_EditCtrl
->EndEdit();
109 void OnScroll(wxScrollWinEvent
& event
)
112 if (event
.GetOrientation() == wxHORIZONTAL
) return;
113 if (!m_EditCtrl
) return;
115 wxTreeItemId id
= GetSelection();
117 GetBoundingRect(id
, bounding
);
119 m_EditCtrl
->Move(-1, bounding
.y
);
122 PropEditCtrl
*m_EditCtrl
;
124 DECLARE_EVENT_TABLE()
127 BEGIN_EVENT_TABLE(PropsTree
, wxRemotelyScrolledTreeCtrl
)
128 EVT_PAINT(PropsTree::OnPaint
)
129 EVT_TREE_SEL_CHANGED(-1, PropsTree::OnSelChange
)
130 EVT_SCROLLWIN(PropsTree::OnScroll
)
134 class PropsValueWindow
: public wxTreeCompanionWindow
137 PropsValueWindow(wxWindow
* parent
, wxWindowID id
= -1,
138 const wxPoint
& pos
= wxDefaultPosition
,
139 const wxSize
& sz
= wxDefaultSize
,
141 : wxTreeCompanionWindow(parent
, id
, pos
, sz
, style
) {}
143 virtual void DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
147 PETreeData
*data
= (PETreeData
*)m_treeCtrl
->GetItemData(id
);
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
);
156 dc
.GetTextExtent(text
, & textW
, & textH
);
159 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
161 dc
.DrawText(text
, x
, y
);
165 void OnClick(wxMouseEvent
& event
)
168 wxTreeItemId item
= GetTreeCtrl()->HitTest(wxPoint(1, event
.GetY()), flags
);
171 GetTreeCtrl()->ScrollTo(item
);
172 GetTreeCtrl()->SelectItem(item
);
176 DECLARE_EVENT_TABLE()
179 BEGIN_EVENT_TABLE(PropsValueWindow
, wxTreeCompanionWindow
)
180 EVT_LEFT_DOWN(PropsValueWindow::OnClick
)
185 // ------------- properties frame --------
189 PropertiesFrame
* PropertiesFrame::ms_Instance
= NULL
;
191 PropertiesFrame
*PropertiesFrame::Get()
193 if (ms_Instance
== NULL
)
195 (void)new PropertiesFrame
;
196 ms_Instance
->Show(TRUE
);
201 PropertiesFrame::PropertiesFrame()
202 : wxFrame(EditorFrame::Get(), -1, _("Properties"),
203 wxDefaultPosition
, wxDefaultSize
,
204 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
206 | wxFRAME_TOOL_WINDOW
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);
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
);
229 m_scrolledWindow
->EnableScrolling(FALSE
, FALSE
);
231 // Let the two controls know about each other
232 m_valueWindow
->SetTreeCtrl(m_tree
);
233 m_tree
->SetCompanionWindow(m_valueWindow
);
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))));
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));
261 PropertiesFrame::~PropertiesFrame()
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());
275 void PropertiesFrame::ShowProps(wxXmlNode
*node
)
280 AddSingleProp(PropertyInfo(_T("xmlid"), _T("XMLID"), wxEmptyString
));
281 AddProps(NodeHandler::Find(node
)->GetPropsList(node
));
283 m_tree
->Expand(m_tree
->GetRootItem());
284 m_valueWindow
->Refresh();
289 void PropertiesFrame::ClearProps()
291 ((PropsTree
*)m_tree
)->ClearProps();
296 void PropertiesFrame::AddProps(PropertyInfoArray
& plist
)
298 for (size_t i
= 0; i
< plist
.GetCount(); i
++)
300 AddSingleProp(plist
[i
]);
306 void PropertiesFrame::AddSingleProp(const PropertyInfo
& pinfo
, wxTreeItemId
*root
)
308 PropEditCtrl
*pec
= (PropEditCtrl
*)m_EditCtrls
.Get(pinfo
.Type
);
310 if (root
!= NULL
) tid
= *root
;
311 else tid
= m_tree
->GetRootItem();
314 wxLogError(_("Unknown property type '%s'!"), pinfo
.Type
.c_str());
316 pec
->CreateTreeEntry(tid
, pinfo
);