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/xrc/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
),
44 // VS: Don't do it, it is *extremely* ugly.
45 // FIXME: find a better solution.
47 //make text larger so controls will fit properly
48 wxFont font
= GetFont();
49 font
.SetPointSize(font
.GetPointSize()*1.5);
54 void OnPaint(wxPaintEvent
& event
)
58 wxTreeCtrl::OnPaint(event
);
60 // Reset the device origin since it may have been set
61 dc
.SetDeviceOrigin(0, 0);
63 wxPen
pen(wxColour(_T("BLACK")), 1, wxSOLID
);
66 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
68 wxSize clientSize
= GetClientSize();
71 wxTreeItemId h
, lastH
;
72 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
74 if (h
.IsOk() && GetBoundingRect(h
, itemRect
))
76 cy
= itemRect
.GetTop();
78 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
82 if (lastH
.IsOk() && GetBoundingRect(lastH
, itemRect
))
84 cy
= itemRect
.GetBottom();
85 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
89 void OnSelChange(wxTreeEvent
& event
)
91 if (m_EditCtrl
!= NULL
)
93 m_EditCtrl
->EndEdit();
97 wxTreeItemId item
= event
.GetItem();
98 PETreeData
*dt
= (PETreeData
*)GetItemData(item
);
102 GetBoundingRect(item
, bounding
);
105 bounding
.SetWidth(GetCompanionWindow()->GetSize().x
);
106 dt
->EditCtrl
->BeginEdit(bounding
, item
);
107 m_EditCtrl
= dt
->EditCtrl
;
114 AddRoot(_("Properties"));
117 m_EditCtrl
->EndEdit();
122 void OnScroll(wxScrollWinEvent
& event
)
125 if (event
.GetOrientation() == wxHORIZONTAL
) return;
126 if (!m_EditCtrl
) return;
128 wxTreeItemId id
= GetSelection();
130 GetBoundingRect(id
, bounding
);
132 m_EditCtrl
->Move(-1, bounding
.y
);
135 PropEditCtrl
*m_EditCtrl
;
137 DECLARE_EVENT_TABLE()
140 BEGIN_EVENT_TABLE(PropsTree
, wxRemotelyScrolledTreeCtrl
)
141 EVT_PAINT(PropsTree::OnPaint
)
142 EVT_TREE_SEL_CHANGED(-1, PropsTree::OnSelChange
)
143 EVT_SCROLLWIN(PropsTree::OnScroll
)
147 class PropsValueWindow
: public wxTreeCompanionWindow
150 PropsValueWindow(wxWindow
* parent
, wxWindowID id
= -1,
151 const wxPoint
& pos
= wxDefaultPosition
,
152 const wxSize
& sz
= wxDefaultSize
,
154 : wxTreeCompanionWindow(parent
, id
, pos
, sz
, style
) {}
156 virtual void DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
160 PETreeData
*data
= (PETreeData
*)m_treeCtrl
->GetItemData(id
);
162 if (data
!= NULL
) text
= data
->EditCtrl
->GetValueAsText(id
);
163 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
164 dc
.DrawRectangle(rect
);
165 dc
.SetTextForeground(* wxBLACK
);
166 dc
.SetBackgroundMode(wxTRANSPARENT
);
169 dc
.GetTextExtent(text
, & textW
, & textH
);
172 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
174 dc
.DrawText(text
, x
, y
);
178 void OnClick(wxMouseEvent
& event
)
181 wxTreeItemId item
= GetTreeCtrl()->HitTest(wxPoint(1, event
.GetY()), flags
);
184 GetTreeCtrl()->SelectItem(item
);
188 DECLARE_EVENT_TABLE()
191 BEGIN_EVENT_TABLE(PropsValueWindow
, wxTreeCompanionWindow
)
192 EVT_LEFT_DOWN(PropsValueWindow::OnClick
)
197 // ------------- properties frame --------
201 PropertiesFrame
* PropertiesFrame::ms_Instance
= NULL
;
203 PropertiesFrame
*PropertiesFrame::Get()
205 if (ms_Instance
== NULL
)
207 (void)new PropertiesFrame
;
208 ms_Instance
->Show(TRUE
);
213 PropertiesFrame::PropertiesFrame()
214 : wxFrame(EditorFrame::Get(), -1, _("Properties"),
215 wxDefaultPosition
, wxDefaultSize
,
216 wxDEFAULT_FRAME_STYLE
| wxFRAME_NO_TASKBAR
218 | wxFRAME_TOOL_WINDOW
225 m_scrolledWindow
= new wxSplitterScrolledWindow(this, -1, wxDefaultPosition
,
226 wxDefaultSize
, wxNO_BORDER
| wxCLIP_CHILDREN
| wxVSCROLL
);
227 m_splitter
= new wxThinSplitterWindow(m_scrolledWindow
, -1, wxDefaultPosition
,
228 wxDefaultSize
, wxSP_3DBORDER
| wxCLIP_CHILDREN
/* | wxSP_LIVE_UPDATE */);
229 m_splitter
->SetSashSize(2);
230 m_tree
= new PropsTree(m_splitter
, -1, wxDefaultPosition
,
231 wxDefaultSize
, wxTR_HAS_BUTTONS
| wxTR_NO_LINES
| wxNO_BORDER
);
232 m_tree
->SetIndent(2);
234 m_valueWindow
= new PropsValueWindow(m_splitter
, -1, wxDefaultPosition
,
235 wxDefaultSize
, wxNO_BORDER
);
236 m_valueWindow
->SetBackgroundColour(m_tree
->GetBackgroundColour());
237 m_splitter
->SplitVertically(m_tree
, m_valueWindow
, wxConfig::Get()->Read(_T("propertiesframe_sash"), 100));
238 //m_splitter->AdjustScrollbars();
239 m_scrolledWindow
->SetTargetWindow(m_tree
);
241 m_scrolledWindow
->EnableScrolling(FALSE
, FALSE
);
243 // Let the two controls know about each other
244 m_valueWindow
->SetTreeCtrl(m_tree
);
245 m_tree
->SetCompanionWindow(m_valueWindow
);
247 wxConfigBase
*cfg
= wxConfigBase::Get();
248 SetSize(wxRect(wxPoint(cfg
->Read(_T("propertiesframe_x"), -1), cfg
->Read(_T("propertiesframe_y"), -1)),
249 wxSize(cfg
->Read(_T("propertiesframe_w"), 200), cfg
->Read(_T("propertiesframe_h"), 200))));
252 m_EditCtrls
.DeleteContents(TRUE
);
253 m_EditCtrls
.Put(_T("bool"), new PropEditCtrlBool(this));
254 m_EditCtrls
.Put(_T("coord"), new PropEditCtrlCoord(this));
255 m_EditCtrls
.Put(_T("color"), new PropEditCtrlColor(this));
256 m_EditCtrls
.Put(_T("dimension"), new PropEditCtrlDim(this));
257 m_EditCtrls
.Put(_T("flags"), new PropEditCtrlFlags(this));
258 m_EditCtrls
.Put(_T("integer"), new PropEditCtrlInt(this));
259 m_EditCtrls
.Put(_T("not_implemented"), new PropEditCtrlNull(this));
260 m_EditCtrls
.Put(_T("text"), new PropEditCtrlTxt(this));
261 m_EditCtrls
.Put(_T("xmlid"), new PropEditCtrlXRCID(this));
262 m_EditCtrls
.Put(_T("font"), new PropEditCtrlFont(this));
263 m_EditCtrls
.Put(_T("choice"), new PropEditCtrlChoice(this));
264 m_EditCtrls
.Put(_T("file"), new PropEditCtrlFile(this));
265 m_EditCtrls
.Put(_T("imagefile"), new PropEditCtrlImageFile(this));
266 m_EditCtrls
.Put(_T(""), new PropEditCtrlNull(this));
273 PropertiesFrame::~PropertiesFrame()
275 wxConfigBase
*cfg
= wxConfigBase::Get();
276 cfg
->Write(_T("propertiesframe_x"), (long)GetPosition().x
);
277 cfg
->Write(_T("propertiesframe_y"), (long)GetPosition().y
);
278 cfg
->Write(_T("propertiesframe_w"), (long)GetSize().x
);
279 cfg
->Write(_T("propertiesframe_h"), (long)GetSize().y
);
280 cfg
->Write(_T("propertiesframe_sash"), (long)m_splitter
->GetSashPosition());
287 void PropertiesFrame::ShowProps(wxXmlNode
*node
)
292 AddSingleProp(PropertyInfo(_T("xmlid"), _T("XRCID"), wxEmptyString
));
293 AddProps(NodeHandler::Find(node
)->GetPropsList(node
));
295 m_tree
->Expand(m_tree
->GetRootItem());
296 m_valueWindow
->Refresh();
301 void PropertiesFrame::ClearProps()
303 ((PropsTree
*)m_tree
)->ClearProps();
308 void PropertiesFrame::AddProps(PropertyInfoArray
& plist
)
310 for (size_t i
= 0; i
< plist
.GetCount(); i
++)
312 AddSingleProp(plist
[i
]);
318 void PropertiesFrame::AddSingleProp(const PropertyInfo
& pinfo
, wxTreeItemId
*root
)
320 PropEditCtrl
*pec
= (PropEditCtrl
*)m_EditCtrls
.Get(pinfo
.Type
);
322 if (root
!= NULL
) tid
= *root
;
323 else tid
= m_tree
->GetRootItem();
326 wxLogError(_("Unknown property type '%s'!"), pinfo
.Type
.c_str());
328 pec
->CreateTreeEntry(tid
, pinfo
);