]>
git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/nodehnd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "nodehnd.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/filefn.h"
24 #include "wx/arrimpl.cpp"
25 #include "wx/textfile.h"
26 #include "wx/tokenzr.h"
27 #include "wx/listctrl.h"
33 WX_DEFINE_OBJARRAY(NodeInfoArray
);
37 void NodeInfo::Read(const wxString
& filename
)
39 HandlerType tp
= HANDLER_NONE
;
47 // if modifying, don't forget to modify it in all places --
48 // search for wxINSTALL_PREFIX in editor.cpp
52 list
.Add(wxGetHomeDir() + "/.wxrcedit");
53 #ifdef wxINSTALL_PREFIX
54 list
.Add(wxINSTALL_PREFIX
"/share/wx/wxrcedit");
58 wxString path
= list
.FindValidPath(filename
);
59 if (path
.IsEmpty()) return;
64 if (!tf
.IsOpened()) return;
66 for (size_t i
= 0; i
< tf
.GetLineCount(); i
++)
68 if (tf
[i
].IsEmpty() || tf
[i
][0] == '#') continue;
69 wxStringTokenizer
tkn(tf
[i
], ' ');
70 wxString s
= tkn
.GetNextToken();
72 nd
= tkn
.GetNextToken();
73 else if (s
== "childtype")
74 cht
= tkn
.GetNextToken();
76 tkn
.GetNextToken().ToLong(&icn
);
77 else if (s
== "derived")
79 if (tkn
.GetNextToken() == "from")
81 s
= tkn
.GetNextToken();
86 else if (s
== "abstract")
90 s
= tkn
.GetNextToken();
91 if (s
== "sizer") tp
= HANDLER_SIZER
;
92 else if (s
== "sizeritem") tp
= HANDLER_SIZERITEM
;
93 else if (s
== "panel") tp
= HANDLER_PANEL
;
94 else if (s
== "notebook") tp
= HANDLER_NOTEBOOK
;
95 else if (s
== "notebookpage") tp
= HANDLER_NOTEBOOKPAGE
;
96 else /*if (s == "normal")*/ tp
= HANDLER_NORMAL
;
101 pi
.Name
= tkn
.GetNextToken();
103 wxString typ
= tkn
.GetNextToken();
104 if (tkn
.HasMoreTokens()) pi
.MoreInfo
= tkn
.GetNextToken();
105 /* ADD NEW PROPERTY TYPES HERE
106 (search for other occurences of this comment in _all_ files) */
107 if (typ
== "color") pi
.Type
= PROP_COLOR
;
108 else if (typ
== "flags") pi
.Type
= PROP_FLAGS
;
109 else if (typ
== "bool") pi
.Type
= PROP_BOOL
;
110 else if (typ
== "integer") pi
.Type
= PROP_INTEGER
;
111 else if (typ
== "coord") pi
.Type
= PROP_COORD
;
112 else if (typ
== "dimension") pi
.Type
= PROP_DIMENSION
;
113 else if (typ
== "not_implemented") pi
.Type
= PROP_NOT_IMPLEMENTED
;
114 else /*if (typ == "text")*/ pi
.Type
= PROP_TEXT
;
117 for (size_t j
= 0; j
< Props
.GetCount(); j
++)
119 if (Props
[j
].Name
== pi
.Name
)
121 if (Props
[j
].Type
== pi
.Type
&& pi
.Type
== PROP_FLAGS
)
122 Props
[j
].MoreInfo
<< ',' << pi
.MoreInfo
;
129 if (!fnd
) Props
.Add(pi
);
133 if (!nd
.IsEmpty()) Node
= nd
;
134 if (!cht
.IsEmpty()) ChildType
= cht
;
135 if (tp
!= HANDLER_NONE
) Type
= tp
;
136 if (icn
!= -1) Icon
= icn
;
142 NodeHandler
*NodeHandler::CreateFromFile(const wxString
& filename
, EditorFrame
*frame
)
144 NodeHandler
*hnd
= NULL
;
146 if (s_AllNodes
== NULL
) s_AllNodes
= new NodeInfoArray
;
148 NodeInfo
*ni
= new NodeInfo
;
149 ni
->Type
= HANDLER_NONE
;
153 // maybe we already parsed it?
154 for (size_t i
= 0; i
< s_AllNodes
->GetCount(); i
++)
155 if ((*s_AllNodes
)[i
].Node
== ni
->Node
) return NULL
;
157 s_AllNodes
->Add(*ni
); // add a copy
159 if (ni
->Type
== HANDLER_NONE
|| ni
->Node
.IsEmpty() || ni
->Abstract
)
165 hnd
= new NodeHandlerPanel(frame
, ni
);
168 hnd
= new NodeHandlerSizer(frame
, ni
);
170 case HANDLER_SIZERITEM
:
171 hnd
= new NodeHandlerSizerItem(frame
, ni
);
173 case HANDLER_NOTEBOOK
:
174 hnd
= new NodeHandlerNotebook(frame
, ni
);
176 case HANDLER_NOTEBOOKPAGE
:
177 hnd
= new NodeHandlerNotebookPage(frame
, ni
);
180 hnd
= new NodeHandler(frame
, ni
);
192 PropertyHandler
* NodeHandler::s_PropHandlers
[PROP_TYPES_CNT
] = {NULL
};
193 int NodeHandler::s_RefCnt
= 0;
194 NodeInfoArray
* NodeHandler::s_AllNodes
= NULL
;
197 NodeHandler::NodeHandler(EditorFrame
*frame
, NodeInfo
*ni
) :
200 if (s_RefCnt
++ == 0) CreatePropHandlers();
204 void NodeHandler::CreatePropHandlers()
206 /* ADD NEW PROPERTY TYPES HERE
207 (search for other occurences of this comment in _all_ files) */
208 s_PropHandlers
[PROP_TEXT
] = new TextPropertyHandler
;
209 s_PropHandlers
[PROP_FLAGS
] = new FlagsPropertyHandler
;
210 s_PropHandlers
[PROP_COLOR
] = new TextPropertyHandler
;
211 s_PropHandlers
[PROP_BOOL
] = new BoolPropertyHandler
;
212 s_PropHandlers
[PROP_INTEGER
] = new TextPropertyHandler
;
213 s_PropHandlers
[PROP_COORD
] = new CoordPropertyHandler
;
214 s_PropHandlers
[PROP_DIMENSION
] = new DimensionPropertyHandler
;
215 s_PropHandlers
[PROP_NOT_IMPLEMENTED
] = new NotImplPropertyHandler
;
219 NodeHandler::~NodeHandler()
223 for (int i
= 0; i
< PROP_TYPES_CNT
; i
++)
224 delete s_PropHandlers
[i
];
225 delete s_AllNodes
; s_AllNodes
= NULL
;
232 bool NodeHandler::CanHandle(wxXmlNode
*node
)
234 return (m_NodeInfo
->Node
== XmlGetClass(node
));
239 wxTreeItemId
NodeHandler::CreateTreeNode(wxTreeCtrl
*treectrl
,
243 int icon
= GetTreeIcon(node
);
245 treectrl
->AppendItem(parent
, GetTreeString(node
),
246 icon
, icon
, new XmlTreeData(node
));
247 if (parent
== treectrl
->GetRootItem())
248 treectrl
->SetItemBold(item
);
254 wxString
NodeHandler::GetTreeString(wxXmlNode
*node
)
256 wxString xmlid
= node
->GetPropVal(_T("name"), wxEmptyString
);
258 return XmlGetClass(node
);
260 return XmlGetClass(node
) + _T(" '") + xmlid
+ _T("'");
265 void NodeHandler::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
269 for (size_t i
= 0; i
< m_NodeInfo
->Props
.GetCount(); i
++)
271 PropertyInfo
*p
= &(m_NodeInfo
->Props
[i
]);
272 index
= s_PropHandlers
[p
->Type
]->CreateListItem(listctrl
, node
, p
);
273 listctrl
->SetItemData(index
, (long)new PropsListInfo(
274 index
, s_PropHandlers
[p
->Type
], node
,
281 wxPanel
*NodeHandler::CreatePropEditPanel(wxWindow
*parent
, wxListCtrl
*listctrl
, int index
)
283 PropsListInfo
*pli
= (PropsListInfo
*)listctrl
->GetItemData(index
);
285 return pli
->m_Handler
->CreateEditPanel(parent
, pli
);
290 wxArrayString
& NodeHandler::GetChildTypes()
292 if (m_ChildTypes
.IsEmpty())
294 wxString basetype
= m_NodeInfo
->ChildType
;
296 for (size_t i
= 0; i
< s_AllNodes
->GetCount(); i
++)
298 NodeInfo
&ni
= (*s_AllNodes
)[i
];
300 if (ni
.Node
== basetype
&& !ni
.Abstract
)
301 m_ChildTypes
.Add(ni
.Node
);
303 if (ni
.DerivedFrom
.Index(basetype
) != wxNOT_FOUND
&& !ni
.Abstract
)
304 m_ChildTypes
.Add(ni
.Node
);
314 void NodeHandler::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
317 wxLogError(_("Cannot insert child into this type of node!"));
327 wxTreeItemId
NodeHandlerPanel::CreateTreeNode(wxTreeCtrl
*treectrl
,
331 wxTreeItemId root
= NodeHandler::CreateTreeNode(treectrl
, parent
, node
);
333 wxXmlNode
*n
= XmlFindNode(node
, "object");
337 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
338 n
->GetName() == _T("object"))
339 EditorFrame::Get()->CreateTreeNode(treectrl
, root
, n
);
342 treectrl
->Expand(root
);
348 void NodeHandlerPanel::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
351 parent
->InsertChild(node
, insert_before
);
353 parent
->AddChild(node
);
354 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
361 void NodeHandlerSizer::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
363 if (XmlGetClass(node
) == _T("spacer") || XmlGetClass(node
) == _T("sizeritem"))
366 parent
->InsertChild(node
, insert_before
);
368 parent
->AddChild(node
);
372 wxXmlNode
*itemnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
373 itemnode
->AddProperty(_T("class"), _T("sizeritem"));
374 itemnode
->AddChild(node
);
377 parent
->InsertChild(itemnode
, insert_before
);
379 parent
->AddChild(itemnode
);
381 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
386 int NodeHandlerSizer::GetTreeIcon(wxXmlNode
*node
)
388 int orig
= NodeHandler::GetTreeIcon(node
);
391 if (XmlReadValue(node
, _T("orient")) == _T("wxVERTICAL")) return 2;
399 wxTreeItemId
NodeHandlerSizerItem::CreateTreeNode(wxTreeCtrl
*treectrl
,
405 root
= EditorFrame::Get()->CreateTreeNode(treectrl
, parent
, GetRealNode(node
));
406 ((XmlTreeData
*)treectrl
->GetItemData(root
))->Node
= node
;
408 treectrl
->Expand(root
);
414 void NodeHandlerSizerItem::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
416 NodeHandler::CreatePropsList(listctrl
, node
);
417 int item
= listctrl
->GetItemCount();
418 listctrl
->InsertItem(item
, "------");
419 listctrl
->SetItemImage(item
, 0, 0);
420 EditorFrame::Get()->CreatePropsList(listctrl
, GetRealNode(node
));
425 wxString
NodeHandlerSizerItem::GetTreeString(wxXmlNode
*node
)
427 wxXmlNode
*n
= GetRealNode(node
);
428 return EditorFrame::Get()->FindHandler(n
)->GetTreeString(n
);
433 int NodeHandlerSizerItem::GetTreeIcon(wxXmlNode
*node
)
435 wxXmlNode
*n
= GetRealNode(node
);
436 return EditorFrame::Get()->FindHandler(n
)->GetTreeIcon(n
);
441 wxXmlNode
*NodeHandlerSizerItem::GetRealNode(wxXmlNode
*node
)
443 return XmlFindNode(node
, _T("object"));
451 void NodeHandlerNotebook::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
456 if (XmlGetClass(node
) == _T("notebookpage"))
460 itemnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
461 itemnode
->AddProperty(_T("class"), _T("notebookpage"));
462 itemnode
->AddChild(node
);
466 parent
->InsertChild(itemnode
, insert_before
);
468 parent
->AddChild(itemnode
);
470 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);