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
)
101 EVT_CLOSE(EditorFrame::OnCloseWindow
)
106 #if defined(__UNIX__)
107 #include "bitmaps/preview.xpm"
108 #include "bitmaps/close.xpm"
109 #include "bitmaps/save.xpm"
110 #include "bitmaps/open.xpm"
112 #include "bitmaps/control.xpm"
113 #include "bitmaps/vsizer.xpm"
114 #include "bitmaps/hsizer.xpm"
115 #include "bitmaps/panel.xpm"
116 #include "bitmaps/gsizer.xpm"
117 #include "bitmaps/resicon.xpm"
122 EditorFrame
*EditorFrame::ms_Instance
= NULL
;
124 EditorFrame::EditorFrame(wxFrame
*parent
, const wxString
& filename
)
125 : wxFrame(parent
, -1, filename
+ _("- wxWindows resources editor"))
132 wxConfigBase
*cfg
= wxConfigBase::Get();
134 SetSize(wxRect(wxPoint(cfg
->Read("editor_x", -1), cfg
->Read("editor_y", -1)),
135 wxSize(cfg
->Read("editor_w", 400), cfg
->Read("editor_h", 400))));
137 m_SelectedNode
= NULL
;
139 m_FileName
= wxEmptyString
;
141 wxMenu
*menuFile
= new wxMenu
;
142 menuFile
->Append(ID_NEW
, "&New");
143 menuFile
->Append(ID_OPEN
, "&Open\tCtrl-O");
144 menuFile
->Append(ID_SAVE
, "&Save\tCtrl-S");
145 menuFile
->Append(ID_SAVEAS
, "Save &as...");
146 menuFile
->AppendSeparator();
147 menuFile
->Append(ID_EXIT
, "E&xit\tAlt-X");
149 wxMenu
*menuEdit
= new wxMenu
;
150 menuEdit
->Append(ID_CUT
, "Cut\tCtrl-X");
151 menuEdit
->Append(ID_COPY
, "Copy\tCtrl-C");
152 menuEdit
->Append(ID_PASTE_SYBLING
, "Paste as sybling\tCtrl-V");
153 menuEdit
->Append(ID_PASTE_CHILD
, "Paste as child");
154 menuEdit
->AppendSeparator();
155 menuEdit
->Append(ID_DELETE_NODE
, "Delete");
157 menuEdit
->Enable(ID_PASTE_SYBLING
, FALSE
);
158 menuEdit
->Enable(ID_PASTE_CHILD
, FALSE
);
160 wxMenuBar
*menuBar
= new wxMenuBar();
161 menuBar
->Append(menuFile
, "&File");
162 menuBar
->Append(menuEdit
, "&Edit");
166 wxToolBar
*toolBar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
| wxTB_FLAT
);
167 toolBar
->SetMargins(2, 2);
168 toolBar
->SetToolBitmapSize(wxSize(24, 24));
169 toolBar
-> AddTool(ID_EXIT
, wxBITMAP(close
), wxNullBitmap
,
170 FALSE
, -1, -1, (wxObject
*) NULL
,
171 _("Quit the editor"));
172 toolBar
-> AddTool(ID_OPEN
, wxBITMAP(open
), wxNullBitmap
,
173 FALSE
, -1, -1, (wxObject
*) NULL
,
174 _("Open XML resource file"));
175 toolBar
-> AddTool(ID_SAVE
, wxBITMAP(save
), wxNullBitmap
,
176 FALSE
, -1, -1, (wxObject
*) NULL
,
178 toolBar
-> AddTool(ID_PREVIEW
, wxBITMAP(preview
), wxNullBitmap
,
179 FALSE
, -1, -1, (wxObject
*) NULL
,
181 toolBar
-> Realize();
183 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
185 // Create tree control:
186 m_TreeCtrl
= new EditorTreeCtrl(this, ID_TREE
, this);
187 wxImageList
*imgList
= new wxImageList(16, 16);
188 imgList
->Add(wxICON(control
));
189 imgList
->Add(wxICON(panel
));
190 imgList
->Add(wxICON(vsizer
));
191 imgList
->Add(wxICON(hsizer
));
192 imgList
->Add(wxICON(gsizer
));
193 imgList
->Add(wxICON(resicon
));
194 m_TreeCtrl
->AssignImageList(imgList
);
195 sizer
->Add(m_TreeCtrl
, 1, wxEXPAND
);
209 EditorFrame::~EditorFrame()
211 PreviewFrame::Get()->Close();
212 PropertiesFrame::Get()->Close();
214 wxConfigBase
*cfg
= wxConfigBase::Get();
216 cfg
->Write(_T("editor_x"), (long)GetPosition().x
);
217 cfg
->Write(_T("editor_y"), (long)GetPosition().y
);
218 cfg
->Write(_T("editor_w"), (long)GetSize().x
);
219 cfg
->Write(_T("editor_h"), (long)GetSize().y
);
227 void EditorFrame::LoadFile(const wxString
& filename
)
229 if (!AskToSave()) return;
234 m_Resource
= new wxXmlDocument
;
237 if (!m_Resource
->Load(filename
))
242 wxLogError("Error parsing " + filename
);
246 m_FileName
= filename
;
254 void EditorFrame::SaveFile(const wxString
& filename
)
256 m_FileName
= filename
;
258 if (!m_Resource
->Save(filename
, wxXML_IO_LIBXML
))
259 wxLogError(_("Error saving ") + filename
);
268 void EditorFrame::NewFile()
270 if (!AskToSave()) return;
275 m_Resource
= new wxXmlDocument
;
276 m_Resource
->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE
, _("resource")));
285 void EditorFrame::RefreshTitle()
288 if (m_Modified
) s
<< _T("* ");
291 s
<< _T(" - ") << wxFileNameFromPath(m_FileName
);
297 void EditorFrame::RefreshTree()
299 wxXmlNode
*sel
= m_SelectedNode
;
301 m_TreeCtrl
->DeleteAllItems();
302 wxTreeItemId root
= m_TreeCtrl
->AddRoot("Resource: " + wxFileNameFromPath(m_FileName
), 5, 5);
304 wxXmlNode
*n
= m_Resource
->GetRoot()->GetChildren();
307 if (n
->GetType() == wxXML_ELEMENT_NODE
)
308 CreateTreeNode(m_TreeCtrl
, root
, n
);
312 m_TreeCtrl
->Expand(root
);
319 static void RecursivelyExpand(wxTreeCtrl
*t
, wxTreeItemId item
)
323 wxTreeItemId id
= t
->GetFirstChild(item
, cookie
);
326 RecursivelyExpand(t
, id
);
327 id
= t
->GetNextChild(item
, cookie
);
331 bool EditorFrame::SelectNode(wxXmlNode
*node
, wxTreeItemId
*root
)
335 wxTreeItemId rootitem
= m_TreeCtrl
->GetRootItem();
336 return SelectNode(node
, &rootitem
);
344 item
= m_TreeCtrl
->GetFirstChild(*root
, cookie
);
347 dt
= (XmlTreeData
*)(m_TreeCtrl
->GetItemData(item
));
348 nd
= (dt
) ? dt
->Node
: NULL
;
351 RecursivelyExpand(m_TreeCtrl
, *root
);
352 m_TreeCtrl
->SelectItem(item
);
353 m_TreeCtrl
->EnsureVisible(item
);
356 if (m_TreeCtrl
->ItemHasChildren(item
) && SelectNode(node
, &item
))
358 item
= m_TreeCtrl
->GetNextChild(*root
, cookie
);
366 wxTreeItemId
EditorFrame::CreateTreeNode(wxTreeCtrl
*treectrl
, wxTreeItemId parent
, wxXmlNode
*node
)
370 wxTreeItemId invalid
;
374 return NodeHandler::Find(node
)->CreateTreeNode(treectrl
, parent
, node
);
379 void EditorFrame::NotifyChanged(int change_type
)
381 if (change_type
& CHANGED_TREE
)
384 if (change_type
& CHANGED_TREE_SELECTED
)
386 wxTreeItemId sel
= m_TreeCtrl
->GetSelection();
387 m_TreeCtrl
->SetItemText(sel
,
388 NodeHandler::Find(m_SelectedNode
)->GetTreeString(m_SelectedNode
));
391 if (change_type
& CHANGED_TREE_SELECTED_ICON
)
393 wxTreeItemId sel
= m_TreeCtrl
->GetSelection();
394 int icon
= NodeHandler::Find(m_SelectedNode
)->GetTreeIcon(m_SelectedNode
);
395 m_TreeCtrl
->SetItemImage(sel
, icon
);
404 PreviewFrame::Get()->MakeDirty();
409 void EditorFrame::OnTreeSel(wxTreeEvent
& event
)
411 XmlTreeData
*dt
= (XmlTreeData
*)(m_TreeCtrl
->GetItemData(event
.GetItem()));
412 wxXmlNode
*node
= (dt
) ? dt
->Node
: NULL
;
414 m_SelectedNode
= node
;
416 PropertiesFrame::Get()->ShowProps(node
);
418 if (m_TreeCtrl
->GetParent(event
.GetItem()) == m_TreeCtrl
->GetRootItem())
420 wxTreeItemId it
= event
.GetOldItem();
422 if (it
.IsOk() && m_TreeCtrl
->GetRootItem() != it
)
424 while (m_TreeCtrl
->GetParent(it
) != m_TreeCtrl
->GetRootItem())
425 it
= m_TreeCtrl
->GetParent(it
);
426 m_TreeCtrl
->Collapse(it
);
428 RecursivelyExpand(m_TreeCtrl
, event
.GetItem());
430 PreviewFrame::Get()->Preview(node
);
436 void EditorFrame::OnToolbar(wxCommandEvent
& event
)
438 switch (event
.GetId())
442 XmlTreeData
* dt
= (XmlTreeData
*)m_TreeCtrl
->GetItemData(m_TreeCtrl
->GetSelection());;
443 if (dt
!= NULL
&& dt
->Node
!= NULL
)
444 PreviewFrame::Get()->Preview(dt
->Node
);
458 wxString cwd
= wxGetCwd(); // workaround for 2.2
459 wxString name
= wxFileSelector(_("Open XML resource"), _T(""), _T(""), _T(""), _("XML resources (*.xrc)|*.xrc"), wxOPEN
| wxFILE_MUST_EXIST
);
460 wxSetWorkingDirectory(cwd
);
467 if (m_FileName
!= "") { SaveFile(m_FileName
); break;}
472 wxString cwd
= wxGetCwd(); // workaround for 2.2
473 wxString name
= wxFileSelector(_("Save as"), _T(""), m_FileName
, _T(""), _("XML resources (*.xrc)|*.xrc"), wxSAVE
| wxOVERWRITE_PROMPT
);
474 wxSetWorkingDirectory(cwd
);
476 SaveFile((m_FileName
= name
));
480 case ID_DELETE_NODE
:
482 DeleteSelectedNode();
490 void EditorFrame::DeleteSelectedNode()
492 XmlTreeData
*dt
= (XmlTreeData
*)
493 (m_TreeCtrl
->GetItemData(m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
494 wxXmlNode
*n
= (dt
) ? dt
->Node
: NULL
;
496 m_SelectedNode
->GetParent()->RemoveChild(m_SelectedNode
);
497 NotifyChanged(CHANGED_TREE
);
503 void EditorFrame::OnNewNode(wxCommandEvent
& event
)
505 if (event
.GetId() >= ID_NEWSYBNODE
)
508 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
509 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
511 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
513 wxXmlNode
*nd
= pardt
->Node
;
515 wxXmlNode
*realnode
= NodeHandler::Find(nd
)->GetRealNode(nd
);
516 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
517 wxString name
= hnd
->GetChildTypes()[event
.GetId()-ID_NEWSYBNODE
];
519 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
520 node
->AddProperty(_T("class"), name
);
522 hnd
->InsertNode(realnode
, node
, m_SelectedNode
);
523 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
524 SelectNode(node
, &root
);
529 else if (event
.GetId() >= ID_NEWNODE
)
531 wxXmlNode
*realnode
= NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
);
532 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
533 wxString name
= hnd
->GetChildTypes()[event
.GetId()-ID_NEWNODE
];
535 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
536 node
->AddProperty(_T("class"), name
);
538 hnd
->InsertNode(realnode
, node
);
539 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
540 SelectNode(node
, &root
);
546 switch (event
.GetId())
548 case ID_NEWDIALOG
: name
= _T("wxDialog"); break;
549 case ID_NEWPANEL
: name
= _T("wxPanel"); break;
550 case ID_NEWMENU
: name
= _T("wxMenu"); break;
551 case ID_NEWMENUBAR
: name
= _T("wxMenuBar"); break;
552 case ID_NEWTOOLBAR
: name
= _T("wxToolBar"); break;
553 default : return; // never occurs
556 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, _T("object"));
557 node
->AddProperty(_T("class"), name
);
558 m_Resource
->GetRoot()->AddChild(node
);
559 NotifyChanged(CHANGED_TREE
);
566 void EditorFrame::OnRightClickTree(wxPoint pos
)
568 wxMenu
*popup
= new wxMenu
;
570 if (m_SelectedNode
== NULL
|| m_SelectedNode
== m_Resource
->GetRoot())
572 popup
->Append(ID_NEWDIALOG
, _("New wxDialog"));
573 popup
->Append(ID_NEWPANEL
, _("New wxPanel"));
574 popup
->Append(ID_NEWMENU
, _("New wxMenu"));
575 popup
->Append(ID_NEWMENUBAR
, _("New wxMenuBar"));
576 popup
->Append(ID_NEWTOOLBAR
, _("New wxToolBar"));
584 NodeHandler::Find(NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
))->
587 has_children
= !arr
.IsEmpty();
590 wxMenu
*news
= new wxMenu
;
591 wxMenu
*news2
= news
;
592 for (size_t i
= 0; i
< arr
.GetCount(); i
++)
594 news2
->Append(i
+ ID_NEWNODE
, arr
[i
]);
595 #ifdef __WXGTK__ // doesn't support Break
598 wxMenu
*m
= new wxMenu
;
599 news2
->Append(ID_NEWNODE
+arr
.GetCount(), _("More..."), m
);
603 if (i
% 16 == 15) news2
->Break();
606 popup
->Append(ID_NEWNODE
-1, _("New child"), news
);
612 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
613 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
614 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
616 wxXmlNode
*nd
= pardt
->Node
;
618 NodeHandler::Find(NodeHandler::Find(nd
)->GetRealNode(nd
))->
623 wxMenu
*news
= new wxMenu
;
624 wxMenu
*news2
= news
;
625 for (size_t i
= 0; i
< arr
.GetCount(); i
++)
627 news2
->Append(i
+ ID_NEWSYBNODE
, arr
[i
]);
628 #ifdef __WXGTK__ // doesn't support Break
631 wxMenu
*m
= new wxMenu
;
632 news2
->Append(ID_NEWSYBNODE
+arr
.GetCount(), _("More..."), m
);
636 if (i
% 16 == 15) news2
->Break();
639 popup
->Append(ID_NEWSYBNODE
-1, _("New sybling"), news
);
644 popup
->AppendSeparator();
645 popup
->Append(ID_CUT
, _("Cut"));
646 popup
->Append(ID_COPY
, _("Copy"));
647 popup
->Append(ID_PASTE_SYBLING
, _("Paste as sybling"));
648 popup
->Append(ID_PASTE_CHILD
, _("Paste as child"));
649 popup
->AppendSeparator();
650 popup
->Append(ID_DELETE_NODE
, _("Delete"));
651 popup
->Enable(ID_PASTE_SYBLING
, m_Clipboard
!= NULL
);
652 popup
->Enable(ID_PASTE_CHILD
, has_children
&& m_Clipboard
!= NULL
);
655 m_TreeCtrl
->PopupMenu(popup
, pos
);
661 void EditorFrame::OnClipboardAction(wxCommandEvent
& event
)
663 switch (event
.GetId())
668 m_Clipboard
= new wxXmlNode(*m_SelectedNode
);
669 GetMenuBar()->Enable(ID_PASTE_SYBLING
, TRUE
);
670 GetMenuBar()->Enable(ID_PASTE_CHILD
, TRUE
);
671 if (event
.GetId() == ID_CUT
) DeleteSelectedNode();
674 case ID_PASTE_SYBLING
:
677 (XmlTreeData
*)(m_TreeCtrl
->GetItemData(
678 m_TreeCtrl
->GetParent(m_TreeCtrl
->GetSelection())));
680 if (pardt
&& pardt
->Node
&& pardt
->Node
!= m_Resource
->GetRoot())
682 wxXmlNode
*nd
= pardt
->Node
;
684 wxXmlNode
*realnode
= NodeHandler::Find(nd
)->GetRealNode(nd
);
685 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
686 wxXmlNode
*node
= new wxXmlNode(*m_Clipboard
);
687 hnd
->InsertNode(realnode
, node
, m_SelectedNode
);
688 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
689 SelectNode(node
, &root
);
695 wxXmlNode
*realnode
= NodeHandler::Find(m_SelectedNode
)->GetRealNode(m_SelectedNode
);
696 NodeHandler
*hnd
= NodeHandler::Find(realnode
);
697 wxXmlNode
*node
= new wxXmlNode(*m_Clipboard
);
698 hnd
->InsertNode(realnode
, node
);
699 wxTreeItemId root
= m_TreeCtrl
->GetSelection();
700 SelectNode(node
, &root
);
708 bool EditorFrame::AskToSave()
709 // asks the user to save current document (if modified)
710 // returns FALSE if user cancelled the action, TRUE of he choosed
713 if (!m_Modified
) return TRUE
;
715 int res
= wxMessageBox(_("File modified. Do you want to save changes?"), _("Save changes"),
716 wxYES_NO
| wxCANCEL
| wxCENTRE
| wxICON_QUESTION
);
718 SaveFile(m_FileName
);
719 return (res
!= wxCANCEL
);
724 void EditorFrame::OnCloseWindow(wxCloseEvent
&)
726 if (!AskToSave()) return;