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
| wxFRAME_TOOL_WINDOW
)
209 m_scrolledWindow
= new wxSplitterScrolledWindow(this, -1, wxDefaultPosition
,
210 wxDefaultSize
, wxNO_BORDER
| wxCLIP_CHILDREN
| wxVSCROLL
);
211 m_splitter
= new wxThinSplitterWindow(m_scrolledWindow
, -1, wxDefaultPosition
,
212 wxDefaultSize
, wxSP_3DBORDER
| wxCLIP_CHILDREN
/* | wxSP_LIVE_UPDATE */);
213 m_splitter
->SetSashSize(2);
214 m_tree
= new PropsTree(m_splitter
, -1, wxDefaultPosition
,
215 wxDefaultSize
, wxTR_HAS_BUTTONS
| wxTR_NO_LINES
| wxNO_BORDER
);
216 m_tree
->SetIndent(2);
218 m_valueWindow
= new PropsValueWindow(m_splitter
, -1, wxDefaultPosition
,
219 wxDefaultSize
, wxNO_BORDER
);
220 m_valueWindow
->SetBackgroundColour(m_tree
->GetBackgroundColour());
221 m_splitter
->SplitVertically(m_tree
, m_valueWindow
, wxConfig::Get()->Read(_T("propertiesframe_sash"), 100));
222 //m_splitter->AdjustScrollbars();
223 m_scrolledWindow
->SetTargetWindow(m_tree
);
225 m_scrolledWindow
->EnableScrolling(FALSE
, FALSE
);
227 // Let the two controls know about each other
228 m_valueWindow
->SetTreeCtrl(m_tree
);
229 m_tree
->SetCompanionWindow(m_valueWindow
);
231 wxConfigBase
*cfg
= wxConfigBase::Get();
232 SetSize(wxRect(wxPoint(cfg
->Read(_T("propertiesframe_x"), -1), cfg
->Read(_T("propertiesframe_y"), -1)),
233 wxSize(cfg
->Read(_T("propertiesframe_w"), 200), cfg
->Read(_T("propertiesframe_h"), 200))));
236 m_EditCtrls
.DeleteContents(TRUE
);
237 m_EditCtrls
.Put(_T("bool"), new PropEditCtrlBool(this));
238 m_EditCtrls
.Put(_T("coord"), new PropEditCtrlCoord(this));
239 m_EditCtrls
.Put(_T("color"), new PropEditCtrlColor(this));
240 m_EditCtrls
.Put(_T("dimension"), new PropEditCtrlDim(this));
241 m_EditCtrls
.Put(_T("flags"), new PropEditCtrlFlags(this));
242 m_EditCtrls
.Put(_T("integer"), new PropEditCtrlInt(this));
243 m_EditCtrls
.Put(_T("not_implemented"), new PropEditCtrlNull(this));
244 m_EditCtrls
.Put(_T("text"), new PropEditCtrlTxt(this));
245 m_EditCtrls
.Put(_T("xmlid"), new PropEditCtrlXMLID(this));
246 m_EditCtrls
.Put(_T("font"), new PropEditCtrlFont(this));
247 m_EditCtrls
.Put(_T("choice"), new PropEditCtrlChoice(this));
248 m_EditCtrls
.Put(_T("file"), new PropEditCtrlFile(this));
249 m_EditCtrls
.Put(_T("imagefile"), new PropEditCtrlImageFile(this));
250 m_EditCtrls
.Put(_T(""), new PropEditCtrlNull(this));
257 PropertiesFrame::~PropertiesFrame()
259 wxConfigBase
*cfg
= wxConfigBase::Get();
260 cfg
->Write(_T("propertiesframe_x"), (long)GetPosition().x
);
261 cfg
->Write(_T("propertiesframe_y"), (long)GetPosition().y
);
262 cfg
->Write(_T("propertiesframe_w"), (long)GetSize().x
);
263 cfg
->Write(_T("propertiesframe_h"), (long)GetSize().y
);
264 cfg
->Write(_T("propertiesframe_sash"), (long)m_splitter
->GetSashPosition());
271 void PropertiesFrame::ShowProps(wxXmlNode
*node
)
276 AddSingleProp(PropertyInfo(_T("xmlid"), _T("XMLID"), wxEmptyString
));
277 AddProps(NodeHandler::Find(node
)->GetPropsList(node
));
279 m_tree
->Expand(m_tree
->GetRootItem());
280 m_valueWindow
->Refresh();
285 void PropertiesFrame::ClearProps()
287 ((PropsTree
*)m_tree
)->ClearProps();
292 void PropertiesFrame::AddProps(PropertyInfoArray
& plist
)
294 for (size_t i
= 0; i
< plist
.GetCount(); i
++)
296 AddSingleProp(plist
[i
]);
302 void PropertiesFrame::AddSingleProp(const PropertyInfo
& pinfo
, wxTreeItemId
*root
)
304 PropEditCtrl
*pec
= (PropEditCtrl
*)m_EditCtrls
.Get(pinfo
.Type
);
306 if (root
!= NULL
) tid
= *root
;
307 else tid
= m_tree
->GetRootItem();
310 wxLogError(_("Unknown property type '%s'!"), pinfo
.Type
.c_str());
312 pec
->CreateTreeEntry(tid
, pinfo
);