]>
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
;
50 list
.Add(wxGetHomeDir() + "/.wxrcedit");
53 wxString path
= list
.FindValidPath(filename
);
54 if (path
.IsEmpty()) return;
58 if (!tf
.IsOpened()) return;
60 for (size_t i
= 0; i
< tf
.GetLineCount(); i
++)
62 if (tf
[i
].IsEmpty() || tf
[i
][0] == '#') continue;
63 wxStringTokenizer
tkn(tf
[i
], ' ');
64 wxString s
= tkn
.GetNextToken();
66 nd
= tkn
.GetNextToken();
67 else if (s
== "childtype")
68 cht
= tkn
.GetNextToken();
70 tkn
.GetNextToken().ToLong(&icn
);
71 else if (s
== "derived")
73 if (tkn
.GetNextToken() == "from")
75 s
= tkn
.GetNextToken();
80 else if (s
== "abstract")
84 s
= tkn
.GetNextToken();
85 if (s
== "sizer") tp
= HANDLER_SIZER
;
86 else if (s
== "sizeritem") tp
= HANDLER_SIZERITEM
;
87 else if (s
== "panel") tp
= HANDLER_PANEL
;
88 else /*if (s == "normal")*/ tp
= HANDLER_NORMAL
;
93 pi
.Name
= tkn
.GetNextToken();
95 wxString typ
= tkn
.GetNextToken();
96 if (tkn
.HasMoreTokens()) pi
.MoreInfo
= tkn
.GetNextToken();
97 if (typ
== "color") pi
.Type
= PROP_COLOR
;
98 else if (typ
== "flags") pi
.Type
= PROP_FLAGS
;
99 else if (typ
== "bool") pi
.Type
= PROP_BOOL
;
100 else if (typ
== "integer") pi
.Type
= PROP_INTEGER
;
101 else if (typ
== "coord") pi
.Type
= PROP_COORD
;
102 else /*if (typ == "text")*/ pi
.Type
= PROP_TEXT
;
105 for (size_t j
= 0; j
< Props
.GetCount(); j
++)
107 if (Props
[j
].Name
== pi
.Name
)
109 if (Props
[j
].Type
== pi
.Type
&& pi
.Type
== PROP_FLAGS
)
110 Props
[j
].MoreInfo
<< ',' << pi
.MoreInfo
;
117 if (!fnd
) Props
.Add(pi
);
121 if (!nd
.IsEmpty()) Node
= nd
;
122 if (!cht
.IsEmpty()) ChildType
= cht
;
123 if (tp
!= HANDLER_NONE
) Type
= tp
;
124 if (icn
!= -1) Icon
= icn
;
130 NodeHandler
*NodeHandler::CreateFromFile(const wxString
& filename
, EditorFrame
*frame
)
132 NodeHandler
*hnd
= NULL
;
134 if (s_AllNodes
== NULL
) s_AllNodes
= new NodeInfoArray
;
136 NodeInfo
*ni
= new NodeInfo
;
137 ni
->Type
= HANDLER_NONE
;
140 s_AllNodes
->Add(*ni
); // add a copy
142 if (ni
->Type
== HANDLER_NONE
|| ni
->Node
.IsEmpty() || ni
->Abstract
)
148 hnd
= new NodeHandlerPanel(frame
, ni
);
151 hnd
= new NodeHandlerSizer(frame
, ni
);
153 case HANDLER_SIZERITEM
:
154 hnd
= new NodeHandlerSizerItem(frame
, ni
);
157 hnd
= new NodeHandler(frame
, ni
);
169 PropertyHandler
* NodeHandler::s_PropHandlers
[PROP_TYPES_CNT
] = {NULL
};
170 int NodeHandler::s_RefCnt
= 0;
171 NodeInfoArray
* NodeHandler::s_AllNodes
= NULL
;
174 NodeHandler::NodeHandler(EditorFrame
*frame
, NodeInfo
*ni
) :
177 if (s_RefCnt
++ == 0) CreatePropHandlers();
181 void NodeHandler::CreatePropHandlers()
183 s_PropHandlers
[PROP_TEXT
] = new TextPropertyHandler
;
184 s_PropHandlers
[PROP_FLAGS
] = new FlagsPropertyHandler
;
185 s_PropHandlers
[PROP_COLOR
] = new TextPropertyHandler
;
186 s_PropHandlers
[PROP_BOOL
] = new BoolPropertyHandler
;
187 s_PropHandlers
[PROP_INTEGER
] = new TextPropertyHandler
;
188 s_PropHandlers
[PROP_COORD
] = new CoordPropertyHandler
;
192 NodeHandler::~NodeHandler()
196 for (int i
= 0; i
< PROP_TYPES_CNT
; i
++)
197 delete s_PropHandlers
[i
];
198 delete s_AllNodes
; s_AllNodes
= NULL
;
205 bool NodeHandler::CanHandle(wxXmlNode
*node
)
207 return (m_NodeInfo
->Node
== node
->GetName());
212 wxTreeItemId
NodeHandler::CreateTreeNode(wxTreeCtrl
*treectrl
,
216 int icon
= GetTreeIcon(node
);
218 treectrl
->AppendItem(parent
, GetTreeString(node
),
219 icon
, icon
, new XmlTreeData(node
));
220 if (parent
== treectrl
->GetRootItem())
221 treectrl
->SetItemBold(item
);
227 wxString
NodeHandler::GetTreeString(wxXmlNode
*node
)
229 wxString xmlid
= node
->GetPropVal("name", "");
231 return node
->GetName();
233 return (node
->GetName() + " '" + xmlid
+ "'");
238 void NodeHandler::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
242 for (size_t i
= 0; i
< m_NodeInfo
->Props
.GetCount(); i
++)
244 PropertyInfo
*p
= &(m_NodeInfo
->Props
[i
]);
245 index
= s_PropHandlers
[p
->Type
]->CreateListItem(listctrl
, node
, p
);
246 listctrl
->SetItemData(index
, (long)new PropsListInfo(
247 index
, s_PropHandlers
[p
->Type
], node
,
254 wxPanel
*NodeHandler::CreatePropEditPanel(wxWindow
*parent
, wxListCtrl
*listctrl
, int index
)
256 PropsListInfo
*pli
= (PropsListInfo
*)listctrl
->GetItemData(index
);
258 return pli
->m_Handler
->CreateEditPanel(parent
, pli
);
263 wxArrayString
& NodeHandler::GetChildTypes()
265 if (m_ChildTypes
.IsEmpty())
267 wxString basetype
= m_NodeInfo
->ChildType
;
269 for (size_t i
= 0; i
< s_AllNodes
->GetCount(); i
++)
271 NodeInfo
&ni
= (*s_AllNodes
)[i
];
273 if (ni
.Node
== basetype
&& !ni
.Abstract
)
274 m_ChildTypes
.Add(ni
.Node
);
276 if (ni
.DerivedFrom
.Index(basetype
) != wxNOT_FOUND
&& !ni
.Abstract
)
277 m_ChildTypes
.Add(ni
.Node
);
287 void NodeHandler::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
290 wxLogError(_("Cannot insert child into this type of node!"));
300 wxTreeItemId
NodeHandlerPanel::CreateTreeNode(wxTreeCtrl
*treectrl
,
304 wxTreeItemId root
= NodeHandler::CreateTreeNode(treectrl
, parent
, node
);
306 wxXmlNode
*n
= XmlFindNode(node
, "children");
308 if (n
) n
= n
->GetChildren();
312 if (n
->GetType() == wxXML_ELEMENT_NODE
)
313 EditorFrame::Get()->CreateTreeNode(treectrl
, root
, n
);
316 treectrl
->Expand(root
);
322 void NodeHandlerPanel::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
324 wxXmlNode
*cnd
= XmlFindNode(parent
, "children");
327 cnd
= new wxXmlNode(wxXML_ELEMENT_NODE
, "children");
328 parent
->AddChild(cnd
);
331 cnd
->InsertChild(node
, insert_before
);
334 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
341 void NodeHandlerSizer::InsertNode(wxXmlNode
*parent
, wxXmlNode
*node
, wxXmlNode
*insert_before
)
343 wxXmlNode
*cnd
= XmlFindNode(parent
, "children");
346 cnd
= new wxXmlNode(wxXML_ELEMENT_NODE
, "children");
347 parent
->AddChild(cnd
);
350 if (node
->GetName() == "spacer")
353 cnd
->InsertChild(node
, insert_before
);
359 wxXmlNode
*itemnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "sizeritem");
360 wxXmlNode
*winnode
= new wxXmlNode(wxXML_ELEMENT_NODE
, "window");
361 itemnode
->AddChild(winnode
);
362 winnode
->AddChild(node
);
365 cnd
->InsertChild(itemnode
, insert_before
);
367 cnd
->AddChild(itemnode
);
369 EditorFrame::Get()->NotifyChanged(CHANGED_TREE
);
374 int NodeHandlerSizer::GetTreeIcon(wxXmlNode
*node
)
376 if (XmlReadValue(node
, "orient") == "wxVERTICAL") return 2;
382 wxTreeItemId
NodeHandlerSizerItem::CreateTreeNode(wxTreeCtrl
*treectrl
,
388 root
= EditorFrame::Get()->CreateTreeNode(treectrl
, parent
, GetRealNode(node
));
389 ((XmlTreeData
*)treectrl
->GetItemData(root
))->Node
= node
;
391 treectrl
->Expand(root
);
397 void NodeHandlerSizerItem::CreatePropsList(wxListCtrl
*listctrl
, wxXmlNode
*node
)
399 NodeHandler::CreatePropsList(listctrl
, node
);
400 int item
= listctrl
->GetItemCount();
401 listctrl
->InsertItem(item
, "------");
402 listctrl
->SetItemImage(item
, 0, 0);
403 EditorFrame::Get()->CreatePropsList(listctrl
, GetRealNode(node
));
408 wxString
NodeHandlerSizerItem::GetTreeString(wxXmlNode
*node
)
410 wxXmlNode
*n
= GetRealNode(node
);
411 return EditorFrame::Get()->FindHandler(n
)->GetTreeString(n
);
416 int NodeHandlerSizerItem::GetTreeIcon(wxXmlNode
*node
)
418 wxXmlNode
*n
= GetRealNode(node
);
419 return EditorFrame::Get()->FindHandler(n
)->GetTreeIcon(n
);
424 wxXmlNode
*NodeHandlerSizerItem::GetRealNode(wxXmlNode
*node
)
426 wxXmlNode
*n
= XmlFindNode(node
, "window");
428 if (n
) n
= n
->GetChildren();
432 if (n
->GetType() == wxXML_ELEMENT_NODE
)