]> git.saurik.com Git - wxWidgets.git/blame - contrib/utils/wxrcedit/editor.cpp
Corrected makefile target bug
[wxWidgets.git] / contrib / utils / wxrcedit / editor.cpp
CommitLineData
56d2f750
VS
1/////////////////////////////////////////////////////////////////////////////
2// Author: Vaclav Slavik
3// Created: 2000/05/05
4// RCS-ID: $Id$
5// Copyright: (c) 2000 Vaclav Slavik
6// Licence: wxWindows licence
7/////////////////////////////////////////////////////////////////////////////
8
9#ifdef __GNUG__
10 #pragma implementation "editor.h"
11 #pragma implementation "treedt.h"
12#endif
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#include "wx/wx.h"
22#include "wx/xml/xml.h"
23#include "wx/xml/xmlres.h"
24#include "wx/splitter.h"
25#include "wx/config.h"
26#include "wx/dir.h"
27
28#include "treedt.h"
29#include "editor.h"
30#include "nodehnd.h"
31#include "xmlhelpr.h"
32
33
34
35class EditorTreeCtrl : public wxTreeCtrl
36{
37 public:
38 EditorTreeCtrl(wxWindow *parent, int id, EditorFrame *frame)
39 : wxTreeCtrl(parent, id), m_EdFrame(frame) {}
40
41 private:
42 EditorFrame *m_EdFrame;
43
44 void OnRightClick(wxMouseEvent &event)
45 {
46 wxTreeItemId item =
47 m_EdFrame->m_TreeCtrl->HitTest(event.GetPosition());
48 if (item.IsOk())
49 {
50 m_EdFrame->m_TreeCtrl->SelectItem(item);
51 m_EdFrame->OnRightClickTree(event.GetPosition());
52 }
53 }
54 DECLARE_EVENT_TABLE()
55};
56
57BEGIN_EVENT_TABLE(EditorTreeCtrl, wxTreeCtrl)
58 EVT_RIGHT_DOWN(EditorTreeCtrl::OnRightClick)
59END_EVENT_TABLE()
60
61
62enum
63{
64 ID_PREVIEW = wxID_HIGHEST + 100,
65 ID_NEW,
66 ID_OPEN,
67 ID_CLOSE,
68 ID_SAVE,
69 ID_SAVEAS,
70 ID_DELETE_NODE,
71 ID_EXIT,
72 ID_TREE,
73 ID_XMLIDEDIT,
74 ID_XMLIDPICK,
75 ID_EDITCODE,
76 ID_PROPSLIST,
77 ID_CLEARPROP,
78
79 ID_NEWDIALOG,
80 ID_NEWPANEL,
81 ID_NEWMENU,
82 ID_NEWMENUBAR,
83 ID_NEWTOOLBAR,
84 ID_NEWNODE = wxID_HIGHEST + 1000,
85 ID_NEWSYBNODE = ID_NEWNODE + 2000
86};
87
88#ifdef __UNIX__
89#endif
90
91
92
93
94BEGIN_EVENT_TABLE(EditorFrame, wxFrame)
95 EVT_TREE_SEL_CHANGED(ID_TREE, EditorFrame::OnTreeSel)
96 EVT_TOOL_RANGE(ID_PREVIEW, ID_EXIT, EditorFrame::OnToolbar)
97 EVT_MENU_RANGE(ID_NEWDIALOG, ID_NEWSYBNODE + 1000, EditorFrame::OnNewNode)
98 EVT_TEXT(ID_XMLIDEDIT, EditorFrame::OnXMLIDEdit)
99 EVT_BUTTON(ID_XMLIDPICK, EditorFrame::OnXMLIDPick)
100 EVT_BUTTON(ID_EDITCODE, EditorFrame::OnEditCode)
101 EVT_BUTTON(ID_CLEARPROP, EditorFrame::OnClearProp)
102 EVT_LIST_ITEM_SELECTED(ID_PROPSLIST, EditorFrame::OnPropSel)
103END_EVENT_TABLE()
104
105
106
107#ifdef __UNIX__
108#include "bitmaps/preview.xpm"
109#include "bitmaps/close.xpm"
110#include "bitmaps/save.xpm"
111#include "bitmaps/open.xpm"
112
113#include "bitmaps/control.xpm"
114#include "bitmaps/vsizer.xpm"
115#include "bitmaps/hsizer.xpm"
116#include "bitmaps/panel.xpm"
117
118#include "bitmaps/unused.xpm"
119#include "bitmaps/used.xpm"
120#endif
121
122
123
124EditorFrame *EditorFrame::ms_Instance = NULL;
125
126EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
127 : wxFrame(parent, -1, filename + _("- wxWindows resources editor"))
128{
129 ms_Instance = this;
130
131 wxConfigBase *cfg = wxConfigBase::Get();
132
133 SetSize(wxRect(wxPoint(cfg->Read("editor_x", -1), cfg->Read("editor_y", -1)),
134 wxSize(cfg->Read("editor_w", 400), cfg->Read("editor_h", 400))));
135
136 m_SelectedNode = NULL;
137 m_Resource = NULL;
138 m_FileName = wxEmptyString;
139 m_Preview = NULL;
140 m_SelectedProp = -1;
141
142 wxMenu *menuFile = new wxMenu;
143 menuFile->Append(ID_NEW, "&New");
144 menuFile->Append(ID_OPEN, "&Open\tCtrl-O");
145 menuFile->Append(ID_SAVE, "&Save\tCtrl-S");
146 menuFile->Append(ID_SAVEAS, "Save &as...");
147 menuFile->AppendSeparator();
148 menuFile->Append(ID_EXIT, "E&xit\tAlt-X");
149
150 wxMenuBar *menuBar = new wxMenuBar();
151 menuBar->Append(menuFile, "&File");
152 SetMenuBar(menuBar);
153
154 // handlers:
155 m_Handlers.DeleteContents(TRUE);
156 RegisterHandlers(".");
157 RegisterHandlers("./df");
158 m_Handlers.Append(new NodeHandlerUnknown(this));
159
160 #ifdef __UNIX__
161 RegisterHandlers(wxGetHomeDir() + "/.wxrcedit");
162 //FIXME - add $(prefix)/share/wx/wxrcedit
163 #endif
164
165 // Create toolbar:
166 wxToolBar *toolBar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL | wxTB_FLAT);
167 toolBar->SetMargins(2, 2);
168 toolBar -> AddTool(ID_EXIT, wxBITMAP(close), wxNullBitmap,
169 FALSE, -1, -1, (wxObject *) NULL,
170 _("Quit the editor"));
171 toolBar -> AddTool(ID_OPEN, wxBITMAP(open), wxNullBitmap,
172 FALSE, -1, -1, (wxObject *) NULL,
173 _("Open XML resource file"));
174 toolBar -> AddTool(ID_SAVE, wxBITMAP(save), wxNullBitmap,
175 FALSE, -1, -1, (wxObject *) NULL,
176 _("Save XML file"));
177 toolBar -> AddTool(ID_PREVIEW, wxBITMAP(preview), wxNullBitmap,
178 FALSE, -1, -1, (wxObject *) NULL,
179 _("Preview"));
180 toolBar -> Realize();
181
182 // Create layout:
183 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
184 wxPanel *p = new wxPanel(this);
185 sizer->Add(p, 1, wxEXPAND);
186 wxSizer *sizer2 = new wxBoxSizer(wxVERTICAL);
187
188 m_Splitter = new wxSplitterWindow(p);
189 sizer2->Add(m_Splitter, 1, wxEXPAND);
190
191
192 // Create tree control:
193 m_TreeCtrl = new EditorTreeCtrl(m_Splitter, ID_TREE, this);
194 m_ImgList = new wxImageList(16, 16);
195 m_ImgList->Add(wxICON(control));
196 m_ImgList->Add(wxICON(panel));
197 m_ImgList->Add(wxICON(vsizer));
198 m_ImgList->Add(wxICON(hsizer));
199
200 m_TreeCtrl->SetImageList(m_ImgList);
201
202
203 // Create properties panel:
204 m_Splitter2 = new wxSplitterWindow(m_Splitter);
205 m_PropsPanel = new wxPanel(m_Splitter2, -1, wxDefaultPosition,
206 wxDefaultSize, wxTAB_TRAVERSAL);
207
208 wxSizer *sizer3 = new wxBoxSizer(wxVERTICAL);
209
210 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
211
212 sizer3->Add(new wxButton(m_PropsPanel, ID_EDITCODE, "Edit XML code"),
213 0, wxALL | wxEXPAND, 2);
214 sz->Add(new wxStaticText(m_PropsPanel, -1, _("XMLID name:")),
215 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 2);
216 m_XMLIDCtrl = new wxTextCtrl(m_PropsPanel, ID_XMLIDEDIT, "");
217 sz->Add(m_XMLIDCtrl, 1, wxLEFT|wxRIGHT, 2);
218 sz->Add(new wxButton(m_PropsPanel, ID_XMLIDPICK, "...", wxDefaultPosition, wxSize(16,-1)),
219 0, wxRIGHT, 2);
220 sizer3->Add(sz, 0, wxTOP|wxEXPAND, 2);
221
222 m_PropsList = new wxListCtrl(m_PropsPanel, ID_PROPSLIST, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
223
224 m_ImgListProp = new wxImageList(16, 16);
225 m_ImgListProp->Add(wxICON(unused));
226 m_ImgListProp->Add(wxICON(used));
227 m_PropsList->SetImageList(m_ImgListProp, wxIMAGE_LIST_SMALL);
228
229 m_PropsList->InsertColumn(0, _("Property"));
230 m_PropsList->InsertColumn(1, _("Value"));
231 m_PropsList->SetColumnWidth(0, cfg->Read("editor_col0", wxLIST_AUTOSIZE_USEHEADER));
232 m_PropsList->SetColumnWidth(1, cfg->Read("editor_col1", wxLIST_AUTOSIZE_USEHEADER));
233
234 sizer3->Add(m_PropsList, 1, wxALL | wxEXPAND, 2);
235
236 m_PropsPanel->SetAutoLayout(TRUE);
237 m_PropsPanel->SetSizer(sizer3);
238 m_PropsPanel->Layout();
239
240 m_PropsEditPanel = new wxScrolledWindow(m_Splitter2, -1, wxDefaultPosition,
241 wxDefaultSize, wxTAB_TRAVERSAL);
242
243 m_Splitter->SplitVertically(m_TreeCtrl, m_Splitter2);
244 m_Splitter->SetSashPosition(cfg->Read("editor_sash", 140));
245
246 m_Splitter2->SplitHorizontally(m_PropsPanel, m_PropsEditPanel);
247 m_Splitter2->SetSashPosition(cfg->Read("editor_sash2", 100));
248
249 p->SetAutoLayout(TRUE);
250 p->SetSizer(sizer2);
251
252
253
254 SetAutoLayout(TRUE);
255 SetSizer(sizer);
256
257 // Load file:
258 if (!filename)
259 NewFile();
260 else
261 LoadFile(filename);
262}
263
264
265
266EditorFrame::~EditorFrame()
267{
268 wxConfigBase *cfg = wxConfigBase::Get();
269
270 cfg->Write("editor_x", (long)GetPosition().x);
271 cfg->Write("editor_y", (long)GetPosition().y);
272 cfg->Write("editor_w", (long)GetSize().x);
273 cfg->Write("editor_h", (long)GetSize().y);
274 cfg->Write("editor_sash", (long)m_Splitter->GetSashPosition());
275 cfg->Write("editor_sash2", (long)m_Splitter2->GetSashPosition());
276 cfg->Write("editor_col0", (long)m_PropsList->GetColumnWidth(0));
277 cfg->Write("editor_col1", (long)m_PropsList->GetColumnWidth(1));
278
279 delete m_ImgList;
280 delete m_ImgListProp;
281 RefreshProps(NULL);
282}
283
284
285
286NodeHandler *EditorFrame::FindHandler(wxXmlNode *node)
287{
288 wxNode *n = m_Handlers.GetFirst();
289 while (n)
290 {
291 NodeHandler *h = (NodeHandler*) n->GetData();
292 if (h->CanHandle(node))
293 return h;
294 n = n->GetNext();
295 }
296 return NULL;
297}
298
299
300
301void EditorFrame::RegisterHandlers(const wxString& dirname)
302{
303 if (!wxDirExists(dirname)) return;
304
305 wxDir dir(dirname);
306 wxString filename;
307 bool cont;
308 NodeHandler *hnd;
309
310 cont = dir.GetFirst(&filename, "*.df");
311 while (cont)
312 {
313 hnd = NodeHandler::CreateFromFile(filename, this);
314 if (hnd) m_Handlers.Append(hnd);
315 cont = dir.GetNext(&filename);
316 }
317
318}
319
320
321
322void EditorFrame::LoadFile(const wxString& filename)
323{
324 delete m_Resource;
325
326 m_FileName = "";
327 m_Resource = new wxXmlDocument;
328
329 if (!m_Resource->Load(filename))
330 {
331 delete m_Resource;
332 m_Resource = NULL;
333 NewFile();
334 wxLogError("Error parsing " + filename);
335 }
336 else
337 {
338 m_FileName = filename;
339 RefreshTree();
340 SetTitle(m_FileName);
341 }
342}
343
344
345
346void EditorFrame::SaveFile(const wxString& filename)
347{
348 m_FileName = filename;
349 SetTitle(filename);
350
351 if (!m_Resource->Save(filename, wxXML_IO_LIBXML))
352 wxLogError("Error saving " + filename);
353}
354
355
356
357void EditorFrame::NewFile()
358{
359 delete m_Resource;
360
361 m_FileName = "";
362 m_Resource = new wxXmlDocument;
363 m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, "resource"));
364
365 RefreshTree();
366 SetTitle("unnamed");
367}
368
369
370
371void EditorFrame::RefreshPreview(wxXmlNode *node)
372{
373 wxBusyCursor bcur;
374 wxXmlResource *res = new wxXmlResource;
375 wxString tempfile;
376 wxPoint pos = wxDefaultPosition;
377
378 while (node->GetParent() != m_Resource->GetRoot())
379 node = node->GetParent();
380
381 if (m_Preview) pos = m_Preview->GetPosition();
382
383 res->InitAllHandlers();
384
385 wxGetTempFileName("xmleditor", tempfile);
386 m_Resource->Save(tempfile, wxXML_IO_BIN);
387 res->Load(tempfile, wxXML_BINARY);
388
389 if (node->GetName() == "dialog")
390 {
391 wxDialog *dlg = new wxDialog;
392 if (res->LoadDialog(dlg, this, node->GetPropVal("name", "-1")))
393 {
394 if (pos.x != -1) dlg->Move(pos);
395 dlg->Show(TRUE);
396 if (m_Preview) m_Preview->Close(TRUE);
397 m_Preview = dlg;
398 }
399 else
400 {
401 delete dlg;
402 wxLogError(_("Cannot preview the dialog -- XML resource corrupted."));
403 }
404 }
405 delete res;
406 wxRemoveFile(tempfile);
407}
408
409
410
411void EditorFrame::RefreshTree()
412{
413 wxXmlNode *sel = m_SelectedNode;
414
415 m_TreeCtrl->DeleteAllItems();
416 wxTreeItemId root = m_TreeCtrl->AddRoot("Resource: " + m_FileName);
417
418 wxXmlNode *n = m_Resource->GetRoot()->GetChildren();
419 while (n)
420 {
421 if (n->GetType() == wxXML_ELEMENT_NODE)
422 CreateTreeNode(m_TreeCtrl, root, n);
423 n = n->GetNext();
424 }
425
426 m_TreeCtrl->Expand(root);
427 SelectNode(sel);
428}
429
430
431void EditorFrame::RefreshProps(wxXmlNode *node)
432{
433 m_SelectedProp = -1;
434
435 for (int i = 0; i < m_PropsList->GetItemCount(); i++)
436 delete (wxObject*)(m_PropsList->GetItemData(i));
437
438 m_PropsList->DeleteAllItems();
439
440 if (node == NULL) return;
441
442 m_XMLIDCtrl->SetValue(FindHandler(node)->GetRealNode(node)->
443 GetPropVal("name", "-1"));
444 CreatePropsList(m_PropsList, node);
445
446 RefreshPropsEdit();
447}
448
449
450
451void EditorFrame::RefreshPropsEdit()
452{
453 m_PropsEditPanel->DestroyChildren();
454 m_PropsEditPanel->SetSizer(NULL);
455
456 if (!m_SelectedNode || m_SelectedProp == -1 ||
457 m_PropsList->GetItemData(m_SelectedProp) == 0) return;
458
459 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
460
461 sizer->Add(new wxButton(m_PropsEditPanel, ID_CLEARPROP, _("Clear")),
462 0, wxALL, 5);
463
464 sizer->Add(
465 FindHandler(m_SelectedNode)->CreatePropEditPanel(m_PropsEditPanel, m_PropsList, m_SelectedProp),
466 1, wxEXPAND, 0);
467
468 m_PropsEditPanel->SetAutoLayout(TRUE);
469 m_PropsEditPanel->SetSizer(sizer);
470 m_PropsEditPanel->Layout();
471
472 wxSize winsz = m_PropsEditPanel->GetSize();
473 sizer->SetMinSize(winsz.x, winsz.y);
474
475 wxSize minsz = sizer->GetMinSize();
476
477 m_PropsEditPanel->SetScrollbars(8, 8, 1/*minsz.x/8*/, minsz.y/8);
478}
479
480
481
482bool EditorFrame::SelectNode(wxXmlNode *node, wxTreeItemId *root)
483{
484 if (root == NULL)
485 {
486 wxTreeItemId rootitem = m_TreeCtrl->GetRootItem();
487 return SelectNode(node, &rootitem);
488 }
489
490 wxTreeItemId item;
491 XmlTreeData *dt;
492 wxXmlNode *nd;
493 long cookie;
494
495 item = m_TreeCtrl->GetFirstChild(*root, cookie);
496 while (item.IsOk())
497 {
498 dt = (XmlTreeData*)(m_TreeCtrl->GetItemData(item));
499 nd = (dt) ? dt->Node : NULL;
500 if (nd == node)
501 {
502 m_TreeCtrl->SelectItem(item);
503 m_TreeCtrl->EnsureVisible(item);
504 return TRUE;
505 }
506 if (m_TreeCtrl->ItemHasChildren(item) && SelectNode(node, &item))
507 return TRUE;
508 item = m_TreeCtrl->GetNextChild(*root, cookie);
509 }
510 return FALSE;
511}
512
513
514
515wxTreeItemId EditorFrame::CreateTreeNode(wxTreeCtrl *treectrl, wxTreeItemId parent, wxXmlNode *node)
516{
517 if (!node)
518 {
519 wxTreeItemId invalid;
520 return invalid;
521 }
522
523 return FindHandler(node)->CreateTreeNode(treectrl, parent, node);
524}
525
526
527
528void EditorFrame::CreatePropsList(wxListCtrl *treectrl, wxXmlNode *node)
529{
530 if (!node) return;
531
532 FindHandler(node)->CreatePropsList(treectrl, node);
533}
534
535
536
537void EditorFrame::NotifyChanged(int change_type)
538{
539 if (change_type & CHANGED_TREE)
540 RefreshTree();
541
542 if (change_type & CHANGED_TREE_SELECTED)
543 {
544 wxTreeItemId sel = m_TreeCtrl->GetSelection();
545 m_TreeCtrl->SetItemText(sel,
546 FindHandler(m_SelectedNode)->GetTreeString(m_SelectedNode));
547 }
548
549 if (change_type & CHANGED_TREE_SELECTED_ICON)
550 {
551 wxTreeItemId sel = m_TreeCtrl->GetSelection();
552 int icon = FindHandler(m_SelectedNode)->GetTreeIcon(m_SelectedNode);
553 m_TreeCtrl->SetItemImage(sel, icon);
554 }
555
556 if (change_type & CHANGED_PROPS_PANEL)
557 RefreshProps(m_SelectedNode);
558}
559
560
561
562void EditorFrame::OnTreeSel(wxTreeEvent& event)
563{
564 XmlTreeData *dt = (XmlTreeData*)(m_TreeCtrl->GetItemData(event.GetItem()));
565 wxXmlNode *node = (dt) ? dt->Node : NULL;
566
567 m_SelectedNode = node;
568 RefreshProps(node);
569}
570
571
572
573void EditorFrame::OnXMLIDEdit(wxCommandEvent& event)
574{
575 if (!m_SelectedNode) return;
576 wxXmlNode *node = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
577
578 node->DeleteProperty("name");
579 wxString s = m_XMLIDCtrl->GetValue();
580 if (!(s == "-1")) node->AddProperty("name", s);
581 NotifyChanged(CHANGED_TREE_SELECTED);
582}
583
584
585
586void EditorFrame::OnXMLIDPick(wxCommandEvent& event)
587{
588 if (!m_SelectedNode) return;
589 wxXmlNode *node = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
590
591 wxString choices[] = {wxString("-1")
592 #define stdID(id) , wxString(#id)
593 stdID(wxID_OK) stdID(wxID_CANCEL)
594 stdID(wxID_YES) stdID(wxID_NO)
595 stdID(wxID_APPLY) stdID(wxID_HELP)
596 stdID(wxID_HELP_CONTEXT)
597
598 stdID(wxID_OPEN) stdID(wxID_CLOSE) stdID(wxID_NEW)
599 stdID(wxID_SAVE) stdID(wxID_SAVEAS) stdID(wxID_REVERT)
600 stdID(wxID_EXIT) stdID(wxID_UNDO) stdID(wxID_REDO)
601 stdID(wxID_PRINT) stdID(wxID_PRINT_SETUP)
602 stdID(wxID_PREVIEW) stdID(wxID_ABOUT) stdID(wxID_HELP_CONTENTS)
603 stdID(wxID_HELP_COMMANDS) stdID(wxID_HELP_PROCEDURES)
604 stdID(wxID_CUT) stdID(wxID_COPY) stdID(wxID_PASTE)
605 stdID(wxID_CLEAR) stdID(wxID_FIND) stdID(wxID_DUPLICATE)
606 stdID(wxID_SELECTALL)
607 stdID(wxID_STATIC) stdID(wxID_FORWARD) stdID(wxID_BACKWARD)
608 stdID(wxID_DEFAULT) stdID(wxID_MORE) stdID(wxID_SETUP)
609 stdID(wxID_RESET)
610 #undef stdID
611 };
612
613 wxString s =
614 wxGetSingleChoice(_("Choose from predefined IDs:"), _("XMLID"),
615 38/*sizeof choices*/, choices);
616 if (!s) return;
617
618 m_XMLIDCtrl->SetValue(s);
619 node->DeleteProperty("name");
620 if (!(s == "-1")) node->AddProperty("name", s);
621 NotifyChanged(CHANGED_TREE_SELECTED);
622}
623
624
625
626void EditorFrame::OnEditCode(wxCommandEvent& event)
627{
628 if (!m_SelectedNode) return;
629
630 wxBusyCursor bcur;
631 wxDialog dlg(this, -1, _("XML code editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
632 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
633 wxTextCtrl *tc = new wxTextCtrl(&dlg, -1, "", wxDefaultPosition,
634 wxDefaultSize, wxTE_MULTILINE);
635 sizer->Add(tc, 1, wxEXPAND | wxALL, 10);
636
637 wxSizer *sz2 = new wxBoxSizer(wxHORIZONTAL);
638
639 sz2->Add(new wxButton(&dlg, wxID_OK, _("Save")), 0);
640 sz2->Add(new wxButton(&dlg, wxID_CANCEL, _("Cancel")), 0, wxLEFT, 10);
641
642 sizer->Add(sz2, 0, wxALIGN_RIGHT | wxRIGHT|wxBOTTOM, 10);
643
644 dlg.SetAutoLayout(TRUE);
645 dlg.SetSizer(sizer);
646
647 wxConfigBase *cfg = wxConfigBase::Get();
648
649 dlg.SetSize(wxRect(wxPoint(cfg->Read("xmleditor_x", -1), cfg->Read("xmleditor_y", -1)),
650 wxSize(cfg->Read("xmleditor_w", 400), cfg->Read("xmleditor_h", 400))));
651
652 wxString tempfile;
653 wxGetTempFileName("xmleditor", tempfile);
654
655 {
656 wxXmlDocument doc;
657 doc.SetRoot(new wxXmlNode(*m_SelectedNode));
658 doc.Save(tempfile, wxXML_IO_LIBXML);
659 }
660 tc->LoadFile(tempfile);
661
662 if (dlg.ShowModal() == wxID_OK)
663 {
664 tc->SaveFile(tempfile);
665 wxXmlDocument doc;
666 if (doc.Load(tempfile))
667 {
668 (*m_SelectedNode) = *doc.GetRoot();
669 NotifyChanged(CHANGED_TREE);
670 //FIXME-instead, regenerate only children
671 }
672 else wxLogError(_("Illegal XML file, canceled."));
673 }
674 wxRemoveFile(tempfile);
675
676 cfg->Write("xmleditor_x", (long)dlg.GetPosition().x);
677 cfg->Write("xmleditor_y", (long)dlg.GetPosition().y);
678 cfg->Write("xmleditor_w", (long)dlg.GetSize().x);
679 cfg->Write("xmleditor_h", (long)dlg.GetSize().y);
680}
681
682
683
684void EditorFrame::OnClearProp(wxCommandEvent& event)
685{
686 m_PropsList->SetItemImage(m_SelectedProp, 0, 0);
687 m_PropsList->SetItem(m_SelectedProp, 1, "");
688
689 PropsListInfo *pli = (PropsListInfo*)m_PropsList->GetItemData(m_SelectedProp);
690
691 wxXmlNode *nd = XmlFindNode(pli->m_Node, pli->m_PropInfo->Name);
692
693 if (nd == NULL) return;
694 nd->GetParent()->RemoveChild(nd);
695 delete nd;
696 RefreshPropsEdit();
697}
698
699
700
701void EditorFrame::OnPropSel(wxListEvent& event)
702{
703 m_SelectedProp = event.GetIndex();
704 RefreshPropsEdit();
705}
706
707
708
709void EditorFrame::OnToolbar(wxCommandEvent& event)
710{
711 switch (event.GetId())
712 {
713 case ID_PREVIEW :
714 {
715 XmlTreeData* dt = (XmlTreeData*)m_TreeCtrl->GetItemData(m_TreeCtrl->GetSelection());;
716 if (dt != NULL && dt->Node != NULL)
717 RefreshPreview(dt->Node);
718 break;
719 }
720
721 case ID_EXIT :
722 Close(TRUE);
723 break;
724
725 case ID_NEW :
726 NewFile();
727 break;
728
729 case ID_OPEN :
730 {
731 wxString name = wxFileSelector("Open XML resource", "", "", "", "XML resources|*.xml", wxOPEN | wxFILE_MUST_EXIST);
732 if (!name.IsEmpty())
733 LoadFile(name);
734 break;
735 }
736
737 case ID_SAVE :
738 if (m_FileName != "") { SaveFile(m_FileName); break;}
739 // else go to SAVEAS
740
741 case ID_SAVEAS :
742 {
743 wxString name = wxFileSelector("Save as", "", m_FileName, "", "XML resources|*.xml", wxSAVE | wxOVERWRITE_PROMPT);
744 if (!name.IsEmpty())
745 SaveFile((m_FileName = name));
746 break;
747 }
748
749 case ID_DELETE_NODE :
750 {
751 XmlTreeData *dt = (XmlTreeData*)
752 (m_TreeCtrl->GetItemData(m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
753 wxXmlNode *n = (dt) ? dt->Node : NULL;
754
755 m_SelectedNode->GetParent()->RemoveChild(m_SelectedNode);
756 NotifyChanged(CHANGED_TREE);
757 SelectNode(n);
758 break;
759 }
760 }
761}
762
763
764
765void EditorFrame::OnNewNode(wxCommandEvent& event)
766{
767 if (event.GetId() >= ID_NEWSYBNODE)
768 {
769 XmlTreeData *pardt =
770 (XmlTreeData*)(m_TreeCtrl->GetItemData(
771 m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
772
773 if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
774 {
775 wxXmlNode *nd = pardt->Node;
776
777 wxXmlNode *realnode = FindHandler(nd)->GetRealNode(nd);
778 NodeHandler *hnd = FindHandler(realnode);
779 wxString name = hnd->GetChildTypes()[event.GetId()-ID_NEWSYBNODE];
780
781 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
782 hnd->InsertNode(realnode, node, m_SelectedNode);
783 wxTreeItemId root = m_TreeCtrl->GetSelection();
784 SelectNode(node, &root);
785 }
786
787 }
788
789 else if (event.GetId() >= ID_NEWNODE)
790 {
791 wxXmlNode *realnode = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
792 NodeHandler *hnd = FindHandler(realnode);
793 wxString name = hnd->GetChildTypes()[event.GetId()-ID_NEWNODE];
794
795 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
796 hnd->InsertNode(realnode, node);
797 wxTreeItemId root = m_TreeCtrl->GetSelection();
798 SelectNode(node, &root);
799 }
800
801 else
802 {
803 wxString name;
804 switch (event.GetId())
805 {
806 case ID_NEWDIALOG : name = "dialog"; break;
807 case ID_NEWPANEL : name = "panel"; break;
808 case ID_NEWMENU : name = "menu"; break;
809 case ID_NEWMENUBAR : name = "menubar"; break;
810 case ID_NEWTOOLBAR : name = "toolbar"; break;
811 default : return; // never occurs
812 }
813
814 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
815 m_Resource->GetRoot()->AddChild(node);
816 NotifyChanged(CHANGED_TREE);
817 SelectNode(node);
818 }
819}
820
821
822
823void EditorFrame::OnRightClickTree(wxPoint pos)
824{
825 wxMenu *popup = new wxMenu;
826
827 if (m_SelectedNode == NULL || m_SelectedNode == m_Resource->GetRoot())
828 {
829 popup->Append(ID_NEWDIALOG, _("New dialog"));
830 popup->Append(ID_NEWPANEL, _("New panel"));
831 popup->Append(ID_NEWMENU, _("New menu"));
832 popup->Append(ID_NEWMENUBAR, _("New menubar"));
833 popup->Append(ID_NEWTOOLBAR, _("New toolbar"));
834 }
835
836 else
837 {
838 {
839 wxArrayString& arr =
840 FindHandler(FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode))->
841 GetChildTypes();
842
843 if (!arr.IsEmpty())
844 {
845 wxMenu *news = new wxMenu;
846 for (size_t i = 0; i < arr.GetCount(); i++)
847 {
848 news->Append(i + ID_NEWNODE, arr[i]);
849 if (i % 16 == 15) news->Break();
850 }
851 popup->Append(ID_NEWNODE-1, _("New child"), news);
852 }
853 }
854
855
856 XmlTreeData *pardt =
857 (XmlTreeData*)(m_TreeCtrl->GetItemData(
858 m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
859 if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
860 {
861 wxXmlNode *nd = pardt->Node;
862 wxArrayString& arr =
863 FindHandler(FindHandler(nd)->GetRealNode(nd))->
864 GetChildTypes();
865
866 if (!arr.IsEmpty())
867 {
868 wxMenu *news = new wxMenu;
869 for (size_t i = 0; i < arr.GetCount(); i++)
870 {
871 news->Append(i + ID_NEWSYBNODE, arr[i]);
872 if (i % 16 == 15) news->Break();
873 }
874 popup->Append(ID_NEWSYBNODE-1, _("New sybling"), news);
875 }
876 }
877
878
879 popup->AppendSeparator();
880 popup->Append(ID_DELETE_NODE, _("Delete"));
881 }
882
883 m_TreeCtrl->PopupMenu(popup, pos);
884 delete popup;
885}