]>
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
;
152 s_AllNodes
->Add(*ni
); // add a copy
154 if (ni
->Type
== HANDLER_NONE
|| ni
->Node
.IsEmpty() || ni
->Abstract
)
160 hnd
= new NodeHandlerPanel(frame
, ni
);
163 hnd
= new NodeHandlerSizer(frame
, ni
);
165 case HANDLER_SIZERITEM
:
166 hnd
= new NodeHandlerSizerItem(frame
, ni
);
168 case HANDLER_NOTEBOOK
:
169 hnd
= new NodeHandlerNotebook(frame
, ni
);
171 case HANDLER_NOTEBOOKPAGE
:
172 hnd
= new NodeHandlerNotebookPage(frame
, ni
);
175 hnd
= new NodeHandler(frame
, ni
);
187 PropertyHandler
* NodeHandler::s_PropHandlers
[PROP_TYPES_CNT
] = {NULL
};
188 int NodeHandler::s_RefCnt
= 0;
189 NodeInfoArray
* NodeHandler::s_AllNodes
= NULL
;
192 NodeHandler::NodeHandler(EditorFrame
*frame
, NodeInfo
*ni
) :
195 if (s_RefCnt
++ == 0) CreatePropHandlers();
199 void NodeHandler::CreatePropHandlers()
201 /* ADD NEW PROPERTY TYPES HERE
202 (search for other occurences of this comment in _all_ files) */
203 s_PropHandlers
[PROP_TEXT
] = new TextPropertyHandler
;
204 s_PropHandlers
[PROP_FLAGS
] = new FlagsPropertyHandler
;
205 s_PropHandlers
[PROP_COLOR
] = new TextPropertyHandler
;
206 s_PropHandlers
[PROP_BOOL
] = new BoolPropertyHandler
;
207 s_PropHandlers
[PROP_INTEGER
] = new TextPropertyHandler
;
208 s_PropHandlers
[PROP_COORD
] = new CoordPropertyHandler
;
209 s_PropHandlers
[PROP_DIMENSION
] = new DimensionPropertyHandler
;
210 s_PropHandlers
[PROP_NOT_IMPLEMENTED
] = new NotImplPropertyHandler
;
214 NodeHandler::~NodeHandler()
218 for (int i
= 0; i
< PROP_TYPES_CNT
; i
++)
219 delete s_PropHandlers
[i
];
220 delete s_AllNodes
; s_AllNodes
= NULL
;
227 bool NodeHandler::CanHandle(wxXmlNode
*node
)
229 return (m_NodeInfo
->Node
== node
->GetName());
234 wxTreeItemId
NodeHandler::CreateTreeNode(wxTreeCtrl
*treectrl
,
238 int icon
= GetTreeIcon(node
);
240 treectrl
->AppendItem(parent
, GetTreeString(node
),
241 icon
, icon
, new XmlTreeData(node
));
242 if (parent
== treectrl
->GetRootItem())
243 treectrl
->SetItemBold(item
);
249 wxString
NodeHandler::GetTreeString(wxXmlNode
*node
)
251 wxString xmlid
= node
->GetPropVal("name", "");
253 return node
->GetName();
255 return (node
->GetName() + " '" + xmlid
+ "'");
260 void NodeHandler::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
264 for (size_t i
= 0; i
< m_NodeInfo
->Props
.GetCount(); i
++)
266 PropertyInfo
*p
= &(m_NodeInfo
->Props
[i
]);
267 index
= s_PropHandlers
[p
->Type
]->CreateListItem(listctrl
, node
, p
);
268 listctrl
->SetItemData(index
, (long)new PropsListInfo(
269 index
, s_PropHandlers
[p
->Type
], node
,
276 wxPanel
*NodeHandler::CreatePropEditPanel(wxWindow
*parent
, wxListCtrl
*listctrl
, int index
)
278 PropsListInfo
*pli
= (PropsListInfo
*)listctrl
->GetItemData(index
);
280 return pli
->m_Handler
->CreateEditPanel(parent
, pli
);
285 wxArrayString
& NodeHandler::GetChildTypes()
287 if (m_ChildTypes
.IsEmpty())
289 wxString basetype
= m_NodeInfo
->ChildType
;
291 for (size_t i
= 0; i
< s_AllNodes
->GetCount(); i
++)
293 NodeInfo
&ni
= (*s_AllNodes
)[i
];
295 if (ni
.Node
== basetype
&& !ni
.Abstract
)
296 m_ChildTypes
.Add(ni
.Node
);
298 if (ni
.DerivedFrom
.Index(basetype
) != wxNOT_FOUND
&& !ni
.Abstract
)
299 m_ChildTypes
.Add(ni
.Node
);
309 void NodeHandler::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
312 wxLogError(_("Cannot insert child into this type of node!"));
322 wxTreeItemId
NodeHandlerPanel::CreateTreeNode(wxTreeCtrl
*treectrl
,
326 wxTreeItemId root
= NodeHandler::CreateTreeNode(treectrl
, parent
, node
);
328 wxXmlNode
*n
= XmlFindNode(node
, "children");
330 if (n
) n
= n
->GetChildren();
334 if (n
->GetType() == wxXML_ELEMENT_NODE
)
335 EditorFrame::Get()->CreateTreeNode(treectrl
, root
, n
);
338 treectrl
->Expand(root
);
344 void NodeHandlerPanel::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
346 wxXmlNode
*cnd
= XmlFindNode(parent
, "children");
349 cnd
= new wxXmlNode(wxXML_ELEMENT_NODE
, "children");
350 parent
->AddChild(cnd
);
353 cnd
->InsertChild(node
, insert_before
);
356 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
363 void NodeHandlerSizer::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
365 wxXmlNode
*cnd
= XmlFindNode(parent
, "children");
368 cnd
= new wxXmlNode(wxXML_ELEMENT_NODE
, "children");
369 parent
->AddChild(cnd
);
372 if (node
->GetName() == "spacer" || node
->GetName() == "sizeritem")
375 cnd
->InsertChild(node
, insert_before
);
381 wxXmlNode
*itemnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "sizeritem");
382 wxXmlNode
*winnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "window");
383 itemnode
->AddChild(winnode
);
384 winnode
->AddChild(node
);
387 cnd
->InsertChild(itemnode
, insert_before
);
389 cnd
->AddChild(itemnode
);
391 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
396 int NodeHandlerSizer::GetTreeIcon(wxXmlNode
*node
)
398 int orig
= NodeHandler::GetTreeIcon(node
);
401 if (XmlReadValue(node
, "orient") == "wxVERTICAL") return 2;
409 wxTreeItemId
NodeHandlerSizerItem::CreateTreeNode(wxTreeCtrl
*treectrl
,
415 root
= EditorFrame::Get()->CreateTreeNode(treectrl
, parent
, GetRealNode(node
));
416 ((XmlTreeData
*)treectrl
->GetItemData(root
))->Node
= node
;
418 treectrl
->Expand(root
);
424 void NodeHandlerSizerItem::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
426 NodeHandler::CreatePropsList(listctrl
, node
);
427 int item
= listctrl
->GetItemCount();
428 listctrl
->InsertItem(item
, "------");
429 listctrl
->SetItemImage(item
, 0, 0);
430 EditorFrame::Get()->CreatePropsList(listctrl
, GetRealNode(node
));
435 wxString
NodeHandlerSizerItem::GetTreeString(wxXmlNode
*node
)
437 wxXmlNode
*n
= GetRealNode(node
);
438 return EditorFrame::Get()->FindHandler(n
)->GetTreeString(n
);
443 int NodeHandlerSizerItem::GetTreeIcon(wxXmlNode
*node
)
445 wxXmlNode
*n
= GetRealNode(node
);
446 return EditorFrame::Get()->FindHandler(n
)->GetTreeIcon(n
);
451 wxXmlNode
*NodeHandlerSizerItem::GetRealNode(wxXmlNode
*node
)
453 wxXmlNode
*n
= XmlFindNode(node
, "window");
455 if (n
) n
= n
->GetChildren();
459 if (n
->GetType() == wxXML_ELEMENT_NODE
)
471 void NodeHandlerNotebook::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
473 wxXmlNode
*cnd
= XmlFindNode(parent
, "children");
476 cnd
= new wxXmlNode(wxXML_ELEMENT_NODE
, "children");
477 parent
->AddChild(cnd
);
483 if (node
->GetName() == "notebookpage")
487 itemnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "notebookpage");
488 wxXmlNode
*winnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "window");
489 itemnode
->AddChild(winnode
);
490 winnode
->AddChild(node
);
494 cnd
->InsertChild(itemnode
, insert_before
);
496 cnd
->AddChild(itemnode
);
498 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);