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