1 /////////////////////////////////////////////////////////////////////////////
2 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "editor.h"
11 #pragma implementation "treedt.h"
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/xml/xml.h"
23 #include "wx/xml/xmlres.h"
24 #include "wx/splitter.h"
25 #include "wx/config.h"
27 #include "wx/listctrl.h"
28 #include "wx/imaglist.h"
35 #include "propframe.h"
39 class EditorTreeCtrl
: public wxTreeCtrl
42 EditorTreeCtrl(wxWindow
*parent
, int id
, EditorFrame
*frame
)
43 : wxTreeCtrl(parent
, id
), m_EdFrame(frame
) {}
46 EditorFrame
*m_EdFrame
;
48 void OnRightClick(wxMouseEvent
&event
)
51 m_EdFrame
->m_TreeCtrl
->HitTest(event
.GetPosition());
54 m_EdFrame
->m_TreeCtrl
->SelectItem(item
);
55 m_EdFrame
->OnRightClickTree(event
.GetPosition());
61 BEGIN_EVENT_TABLE(EditorTreeCtrl
, wxTreeCtrl
)
62 EVT_RIGHT_DOWN(EditorTreeCtrl::OnRightClick
)
68 ID_PREVIEW
= wxID_HIGHEST
+ 100,
88 ID_NEWNODE
= wxID_HIGHEST
+ 10000, // safely out of XMLID range :)
89 ID_NEWSYBNODE
= ID_NEWNODE
+ 20000
96 BEGIN_EVENT_TABLE(EditorFrame
, wxFrame
)
97 EVT_TREE_SEL_CHANGED(ID_TREE
, EditorFrame::OnTreeSel
)
98 EVT_TOOL_RANGE(ID_PREVIEW
, ID_EXIT
, EditorFrame::OnToolbar
)
99 EVT_MENU_RANGE(ID_NEWDIALOG
, ID_NEWSYBNODE
+ 1000, EditorFrame::OnNewNode
)
100 EVT_MENU_RANGE(ID_CUT
, ID_COPY
, EditorFrame::OnClipboardAction
)
105 #if defined(__UNIX__)
106 #include "bitmaps/preview.xpm"
107 #include "bitmaps/close.xpm"
108 #include "bitmaps/save.xpm"
109 #include "bitmaps/open.xpm"
111 #include "bitmaps/control.xpm"
112 #include "bitmaps/vsizer.xpm"
113 #include "bitmaps/hsizer.xpm"
114 #include "bitmaps/panel.xpm"
115 #include "bitmaps/gsizer.xpm"
116 #include "bitmaps/resicon.xpm"
121 EditorFrame
*EditorFrame::ms_Instance
= NULL
;
123 EditorFrame::EditorFrame(wxFrame
*parent
, const wxString
& filename
)
124 : wxFrame(parent
, -1, filename
+ _("- wxWindows resources editor"))
130 wxConfigBase
*cfg
= wxConfigBase::Get();
132 SetSize(wxRect(wxPoint(cfg
->Read("editor_x", -1), cfg
->Read("editor_y", -1)),
133 wxSize(cfg
->Read("editor_w", 400), cfg
->Read("editor_h", 400))));
135 m_SelectedNode
= NULL
;
137 m_FileName
= wxEmptyString
;
139 wxMenu
*menuFile
= new wxMenu
;
140 menuFile
->Append(ID_NEW
, "&New");
141 menuFile
->Append(ID_OPEN
, "&Open\tCtrl-O");
142 menuFile
->Append(ID_SAVE
, "&Save\tCtrl-S");
143 menuFile
->Append(ID_SAVEAS
, "Save &as...");
144 menuFile
->AppendSeparator();
145 menuFile
->Append(ID_EXIT
, "E&xit\tAlt-X");
147 wxMenu
*menuEdit
= new wxMenu
;
148 menuEdit
->Append(ID_CUT
, "Cut\tCtrl-X");
149 menuEdit
->Append(ID_COPY
, "Copy\tCtrl-C");
150 menuEdit
->Append(ID_PASTE_SYBLING
, "Paste as sybling\tCtrl-V");
151 menuEdit
->Append(ID_PASTE_CHILD
, "Paste as child");
152 menuEdit
->AppendSeparator();
153 menuEdit
->Append(ID_DELETE_NODE
, "Delete");
155 menuEdit
->Enable(ID_PASTE_SYBLING
, FALSE
);
156 menuEdit
->Enable(ID_PASTE_CHILD
, FALSE
);
158 wxMenuBar
*menuBar
= new wxMenuBar();
159 menuBar
->Append(menuFile
, "&File");
160 menuBar
->Append(menuEdit
, "&Edit");
164 wxToolBar
*toolBar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
| wxTB_FLAT
);
165 toolBar
->SetMargins(2, 2);
166 toolBar
->SetToolBitmapSize(wxSize(24, 24));
167 toolBar
-> AddTool(ID_EXIT
, wxBITMAP(close
), wxNullBitmap
,
168 FALSE
, -1, -1, (wxObject
*) NULL
,
169 _("Quit the editor"));
170 toolBar
-> AddTool(ID_OPEN
, wxBITMAP(open
), wxNullBitmap
,
171 FALSE
, -1, -1, (wxObject
*) NULL
,
172 _("Open XML resource file"));
173 toolBar
-> AddTool(ID_SAVE
, wxBITMAP(save
), wxNullBitmap
,
174 FALSE
, -1, -1, (wxObject
*) NULL
,
176 toolBar
-> AddTool(ID_PREVIEW
, wxBITMAP(preview
), wxNullBitmap
,
177 FALSE
, -1, -1, (wxObject
*) NULL
,
179 toolBar
-> Realize();
181 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
183 // Create tree control:
184 m_TreeCtrl
= new EditorTreeCtrl(this, ID_TREE
, this);
185 wxImageList
*imgList
= new wxImageList(16, 16);
186 imgList
->Add(wxICON(control
));
187 imgList
->Add(wxICON(panel
));
188 imgList
->Add(wxICON(vsizer
));
189 imgList
->Add(wxICON(hsizer
));
190 imgList
->Add(wxICON(gsizer
));
191 imgList
->Add(wxICON(resicon
));
192 m_TreeCtrl
->AssignImageList(imgList
);
193 sizer
->Add(m_TreeCtrl
, 1, wxEXPAND
);
207 EditorFrame::~EditorFrame()
209 PreviewFrame::Get()->Close();
210 PropertiesFrame::Get()->Close();
212 wxConfigBase
*cfg
= wxConfigBase::Get();
214 cfg
->Write(_T("editor_x"), (long)GetPosition().x
);
215 cfg
->Write(_T("editor_y"), (long)GetPosition().y
);
216 cfg
->Write(_T("editor_w"), (long)GetSize().x
);
217 cfg
->Write(_T("editor_h"), (long)GetSize().y
);
225 void EditorFrame::LoadFile(const wxString
& filename
)
230 m_Resource
= new wxXmlDocument
;
232 if (!m_Resource
->Load(filename
))
237 wxLogError("Error parsing " + filename
);
241 m_FileName
= filename
;
243 SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName
));
249 void EditorFrame::SaveFile(const wxString
& filename
)
251 m_FileName
= filename
;
252 SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName
));
254 if (!m_Resource
->Save(filename
, wxXML_IO_LIBXML
))
255 wxLogError("Error saving " + filename
);
260 void EditorFrame::NewFile()
265 m_Resource
= new wxXmlDocument
;
266 m_Resource
->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE
, "resource"));
274 void EditorFrame::RefreshTree()
276 wxXmlNode
*sel
= m_SelectedNode
;
278 m_TreeCtrl
->DeleteAllItems();
279 wxTreeItemId root
= m_TreeCtrl
->AddRoot("Resource: " + wxFileNameFromPath(m_FileName
), 5, 5);
281 wxXmlNode
*n
= m_Resource
->GetRoot()->GetChildren();
284 if (n
->GetType() == wxXML_ELEMENT_NODE
)
285 CreateTreeNode(m_TreeCtrl
, root
, n
);
289 m_TreeCtrl
->Expand(root
);
295 bool EditorFrame::SelectNode(wxXmlNode
*node
, wxTreeItemId
*root
)
299 wxTreeItemId rootitem
= m_TreeCtrl
->GetRootItem();
300 return SelectNode(node
, &rootitem
);
308 item
= m_TreeCtrl
->GetFirstChild(*root
, cookie
);
311 dt
= (XmlTreeData
*)(m_TreeCtrl
->GetItemData(item
));
312 nd
= (dt
) ? dt
->Node
: NULL
;
315 m_TreeCtrl
->SelectItem(item
);
316 m_TreeCtrl
->EnsureVisible(item
);
319 if (m_TreeCtrl
->ItemHasChildren(item
) && SelectNode(node
, &item
))
321 item
= m_TreeCtrl
->GetNextChild(*root
, cookie
);
328 wxTreeItemId
EditorFrame::CreateTreeNode(wxTreeCtrl
*treectrl
, wxTreeItemId parent
, wxXmlNode
*node
)
332 wxTreeItemId invalid
;
336 return NodeHandler::Find(node
)->CreateTreeNode(treectrl
, parent
, node
);
341 void EditorFrame::NotifyChanged(int change_type
)
343 if (change_type
& CHANGED_TREE
)
346 if (change_type
& CHANGED_TREE_SELECTED
)
348 wxTreeItemId sel
= m_TreeCtrl
->GetSelection();
349 m_TreeCtrl
->SetItemText(sel
,
350 NodeHandler::Find(m_SelectedNode
)->GetTreeString(m_SelectedNode
));
353 if (change_type
& CHANGED_TREE_SELECTED_ICON
)
355 wxTreeItemId sel
= m_TreeCtrl
->GetSelection();
356 int icon
= NodeHandler::Find(m_SelectedNode
)->GetTreeIcon(m_SelectedNode
);
357 m_TreeCtrl
->SetItemImage(sel
, icon
);
363 static void RecursivelyExpand(wxTreeCtrl
*t
, wxTreeItemId item
)
367 wxTreeItemId id
= t
->GetFirstChild(item
, cookie
);
370 RecursivelyExpand(t
, id
);
371 id
= t
->GetNextChild(item
, cookie
);
375 void EditorFrame::OnTreeSel(wxTreeEvent
& event
)
377 XmlTreeData
*dt
= (XmlTreeData
*)(m_TreeCtrl
->GetItemData(event
.GetItem()));
378 wxXmlNode
*node
= (dt
) ? dt
->Node
: NULL
;
380 m_SelectedNode
= node
;
382 PropertiesFrame::Get()->ShowProps(node
);
384 if (m_TreeCtrl
->GetParent(event
.GetItem()) == m_TreeCtrl
->GetRootItem())
386 wxTreeItemId it
= event
.GetOldItem();
388 if (it
.IsOk() && m_TreeCtrl
->GetRootItem() != it
)
390 while (m_TreeCtrl
->GetParent(it
) != m_TreeCtrl
->GetRootItem())
391 it
= m_TreeCtrl
->GetParent(it
);
392 m_TreeCtrl
->Collapse(it
);
394 RecursivelyExpand(m_TreeCtrl
, event
.GetItem());
396 PreviewFrame::Get()->Preview(node
);
402 void EditorFrame::OnToolbar(wxCommandEvent
& event
)
404 switch (event
.GetId())
408 XmlTreeData
* dt
= (XmlTreeData
*)m_TreeCtrl
->GetItemData(m_TreeCtrl
->GetSelection());;
409 if (dt
!= NULL
&& dt
->Node
!= NULL
)
410 PreviewFrame::Get()->Preview(dt
->Node
);
424 wxString name
= wxFileSelector(_("Open XML resource"), _T(""), _T(""), _T(""), _("XML resources (*.xrc)|*.xrc"), wxOPEN
| wxFILE_MUST_EXIST
);
431 if (m_FileName
!= "") { SaveFile(m_FileName
); break;}
436 wxString name
= wxFileSelector(_("Save as"), _T(""), m_FileName
, _T(""), _("XML resources (*.xrc)|*.xrc"), wxSAVE
| wxOVERWRITE_PROMPT
);
438 SaveFile((m_FileName
= name
));
442 case ID_DELETE_NODE
:
444 DeleteSelectedNode();
452 void EditorFrame::DeleteSelectedNode()
454 XmlTreeData
*dt
= (XmlTreeData
*)
455 (m_TreeCtrl
->GetItemData(m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
456 wxXmlNode
*n
= (dt
) ? dt
->Node
: NULL
;
458 m_SelectedNode
->GetParent()->RemoveChild(m_SelectedNode
);
459 NotifyChanged(CHANGED_TREE
);
465 void EditorFrame::OnNewNode(wxCommandEvent
& event
)
467 if (event
.GetId() >= ID_NEWSYBNODE
)
470 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
471 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
473 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
475 wxXmlNode
*nd
= pardt
->Node
;
477 wxXmlNode
*realnode
= NodeHandler::Find(nd
)->GetRealNode(nd
);
478 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
479 wxString name
= hnd
->GetChildTypes()[event
.GetId()-ID_NEWSYBNODE
];
481 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
482 node
->AddProperty(_T("class"), name
);
484 hnd
->InsertNode(realnode
, node
, m_SelectedNode
);
485 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
486 SelectNode(node
, &root
);
491 else if (event
.GetId() >= ID_NEWNODE
)
493 wxXmlNode
*realnode
= NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
);
494 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
495 wxString name
= hnd
->GetChildTypes()[event
.GetId()-ID_NEWNODE
];
497 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
498 node
->AddProperty(_T("class"), name
);
500 hnd
->InsertNode(realnode
, node
);
501 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
502 SelectNode(node
, &root
);
508 switch (event
.GetId())
510 case ID_NEWDIALOG
: name
= _T("wxDialog"); break;
511 case ID_NEWPANEL
: name
= _T("wxPanel"); break;
512 case ID_NEWMENU
: name
= _T("wxMenu"); break;
513 case ID_NEWMENUBAR
: name
= _T("wxMenuBar"); break;
514 case ID_NEWTOOLBAR
: name
= _T("wxToolBar"); break;
515 default : return; // never occurs
518 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
519 node
->AddProperty(_T("class"), name
);
520 m_Resource
->GetRoot()->AddChild(node
);
521 NotifyChanged(CHANGED_TREE
);
528 void EditorFrame::OnRightClickTree(wxPoint pos
)
530 wxMenu
*popup
= new wxMenu
;
532 if (m_SelectedNode
== NULL
|| m_SelectedNode
== m_Resource
->GetRoot())
534 popup
->Append(ID_NEWDIALOG
, _("New wxDialog"));
535 popup
->Append(ID_NEWPANEL
, _("New wxPanel"));
536 popup
->Append(ID_NEWMENU
, _("New wxMenu"));
537 popup
->Append(ID_NEWMENUBAR
, _("New wxMenuBar"));
538 popup
->Append(ID_NEWTOOLBAR
, _("New wxToolBar"));
546 NodeHandler::Find(NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
))->
549 has_children
= !arr
.IsEmpty();
552 wxMenu
*news
= new wxMenu
;
553 wxMenu
*news2
= news
;
554 for (size_t i
= 0; i
< arr
.GetCount(); i
++)
556 news2
->Append(i
+ ID_NEWNODE
, arr
[i
]);
557 #ifdef __WXGTK__ // doesn't support Break
560 wxMenu
*m
= new wxMenu
;
561 news2
->Append(ID_NEWNODE
+arr
.GetCount(), _("More..."), m
);
565 if (i
% 16 == 15) news2
->Break();
568 popup
->Append(ID_NEWNODE
-1, _("New child"), news
);
574 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
575 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
576 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
578 wxXmlNode
*nd
= pardt
->Node
;
580 NodeHandler::Find(NodeHandler::Find(nd
)->GetRealNode(nd
))->
585 wxMenu
*news
= new wxMenu
;
586 wxMenu
*news2
= news
;
587 for (size_t i
= 0; i
< arr
.GetCount(); i
++)
589 news2
->Append(i
+ ID_NEWSYBNODE
, arr
[i
]);
590 #ifdef __WXGTK__ // doesn't support Break
593 wxMenu
*m
= new wxMenu
;
594 news2
->Append(ID_NEWSYBNODE
+arr
.GetCount(), _("More..."), m
);
598 if (i
% 16 == 15) news2
->Break();
601 popup
->Append(ID_NEWSYBNODE
-1, _("New sybling"), news
);
606 popup
->AppendSeparator();
607 popup
->Append(ID_CUT
, _("Cut"));
608 popup
->Append(ID_COPY
, _("Copy"));
609 popup
->Append(ID_PASTE_SYBLING
, _("Paste as sybling"));
610 popup
->Append(ID_PASTE_CHILD
, _("Paste as child"));
611 popup
->AppendSeparator();
612 popup
->Append(ID_DELETE_NODE
, _("Delete"));
613 popup
->Enable(ID_PASTE_SYBLING
, m_Clipboard
!= NULL
);
614 popup
->Enable(ID_PASTE_CHILD
, has_children
&& m_Clipboard
!= NULL
);
617 m_TreeCtrl
->PopupMenu(popup
, pos
);
623 void EditorFrame::OnClipboardAction(wxCommandEvent
& event
)
625 switch (event
.GetId())
630 m_Clipboard
= new wxXmlNode(*m_SelectedNode
);
631 GetMenuBar()->Enable(ID_PASTE_SYBLING
, TRUE
);
632 GetMenuBar()->Enable(ID_PASTE_CHILD
, TRUE
);
633 if (event
.GetId() == ID_CUT
) DeleteSelectedNode();
636 case ID_PASTE_SYBLING
:
639 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
640 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
642 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
644 wxXmlNode
*nd
= pardt
->Node
;
646 wxXmlNode
*realnode
= NodeHandler::Find(nd
)->GetRealNode(nd
);
647 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
648 wxXmlNode
*node
= new wxXmlNode(*m_Clipboard
);
649 hnd
->InsertNode(realnode
, node
, m_SelectedNode
);
650 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
651 SelectNode(node
, &root
);
657 wxXmlNode
*realnode
= NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
);
658 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
659 wxXmlNode
*node
= new wxXmlNode(*m_Clipboard
);
660 hnd
->InsertNode(realnode
, node
);
661 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
662 SelectNode(node
, &root
);