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