]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: registry.cpp | |
3 | // Purpose: wxRegKey class demo | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 03.04.98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/wx.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/log.h" | |
30 | #include "wx/treectrl.h" | |
31 | #include "wx/msw/registry.h" | |
32 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // application type | |
35 | // ---------------------------------------------------------------------------- | |
36 | class RegApp : public wxApp | |
37 | { | |
38 | public: | |
39 | bool OnInit(void); | |
40 | }; | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // image list with registry icons | |
44 | // ---------------------------------------------------------------------------- | |
45 | class RegImageList : public wxImageList | |
46 | { | |
47 | public: | |
48 | enum Icon | |
49 | { | |
50 | Root, | |
51 | ClosedKey, | |
52 | OpenedKey, | |
53 | TextValue, | |
54 | BinaryValue, | |
55 | }; | |
56 | ||
57 | RegImageList(); | |
58 | }; | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // our control | |
62 | // ---------------------------------------------------------------------------- | |
63 | class RegTreeCtrl : public wxTreeCtrl | |
64 | { | |
65 | public: | |
66 | // ctor & dtor | |
67 | RegTreeCtrl(wxWindow *parent, wxWindowID id); | |
68 | virtual ~RegTreeCtrl(); | |
69 | ||
70 | // notifications | |
71 | void OnDeleteItem (wxTreeEvent& event); | |
72 | void OnItemExpanding(wxTreeEvent& event); | |
73 | void OnSelChanged (wxTreeEvent& event); | |
74 | ||
75 | void OnRightClick (wxMouseEvent& event); | |
76 | void OnChar (wxKeyEvent& event); | |
77 | ||
78 | // forwarded notifications (by the frame) | |
79 | void OnMenuTest(); | |
80 | ||
81 | // operations | |
82 | void DeleteSelected(); | |
83 | void CreateNewKey(const wxString& strName); | |
84 | void CreateNewTextValue(const wxString& strName); | |
85 | void CreateNewBinaryValue(const wxString& strName); | |
86 | ||
87 | // information | |
88 | bool IsKeySelected() const; | |
89 | ||
90 | DECLARE_EVENT_TABLE(); | |
91 | ||
92 | private: | |
93 | // array of children of the node | |
94 | struct TreeNode; | |
95 | WX_DEFINE_ARRAY(TreeNode *, TreeChildren); | |
96 | ||
97 | // structure describing a registry key/value | |
98 | struct TreeNode | |
99 | { | |
100 | RegTreeCtrl *m_pTree; // must be !NULL | |
101 | TreeNode *m_pParent; // NULL only for the root node | |
102 | long m_id; // the id of the tree control item | |
103 | wxString m_strName; // name of the key/value | |
104 | TreeChildren m_aChildren; // array of subkeys/values | |
105 | bool m_bKey; // key or value? | |
106 | wxRegKey *m_pKey; // only may be !NULL if m_bKey == true | |
107 | long m_lDummy; // dummy subkey (to make expansion possible) | |
108 | ||
109 | // ctor | |
110 | TreeNode() { m_lDummy = 0; } | |
111 | ||
112 | // trivial accessors | |
113 | long Id() const { return m_id; } | |
114 | bool IsRoot() const { return m_pParent == NULL; } | |
115 | bool IsKey() const { return m_bKey; } | |
116 | TreeNode *Parent() const { return m_pParent; } | |
117 | ||
118 | // notifications | |
119 | bool OnExpand(); | |
120 | void OnCollapse(); | |
121 | ||
122 | // operations | |
123 | void Refresh() { OnCollapse(); OnExpand(); } | |
124 | void AddDummy(); | |
125 | void DestroyChildren(); | |
126 | const char *FullName() const; | |
127 | ||
128 | // get the associated key: make sure the pointer is !NULL | |
129 | wxRegKey& Key() { if ( !m_pKey ) OnExpand(); return *m_pKey; } | |
130 | ||
131 | // dtor deletes all children | |
132 | ~TreeNode(); | |
133 | }; | |
134 | ||
135 | wxMenu *m_pMenuPopup; | |
136 | TreeNode *m_pRoot; | |
137 | wxImageList *m_imageList; | |
138 | ||
139 | TreeNode *GetNode(const wxTreeEvent& event) | |
140 | { return (TreeNode *)GetItemData(event.m_item.m_itemId); } | |
141 | ||
142 | public: | |
143 | // create a new node and insert it to the tree | |
144 | TreeNode *InsertNewTreeNode(TreeNode *pParent, | |
145 | const wxString& strName, | |
146 | int idImage = RegImageList::ClosedKey, | |
147 | const wxString *pstrValue = NULL); | |
148 | // add standard registry keys | |
149 | void AddStdKeys(); | |
150 | }; | |
151 | ||
152 | // ---------------------------------------------------------------------------- | |
153 | // the main window of our application | |
154 | // ---------------------------------------------------------------------------- | |
155 | class RegFrame : public wxFrame | |
156 | { | |
157 | public: | |
158 | // ctor & dtor | |
159 | RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h); | |
160 | virtual ~RegFrame(void); | |
161 | ||
162 | // callbacks | |
163 | void OnQuit (wxCommandEvent& event); | |
164 | void OnAbout(wxCommandEvent& event); | |
165 | void OnTest (wxCommandEvent& event); | |
166 | ||
167 | void OnExpand (wxCommandEvent& event); | |
168 | void OnCollapse(wxCommandEvent& event); | |
169 | void OnToggle (wxCommandEvent& event); | |
170 | ||
171 | void OnDelete (wxCommandEvent& event); | |
172 | void OnNewKey (wxCommandEvent& event); | |
173 | void OnNewText (wxCommandEvent& event); | |
174 | void OnNewBinary(wxCommandEvent& event); | |
175 | ||
176 | bool OnClose () { return TRUE; } | |
177 | ||
178 | DECLARE_EVENT_TABLE(); | |
179 | ||
180 | private: | |
181 | RegTreeCtrl *m_treeCtrl; | |
182 | }; | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // various ids | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | enum | |
189 | { | |
190 | Menu_Quit = 100, | |
191 | Menu_About, | |
192 | Menu_Test, | |
193 | Menu_Expand, | |
194 | Menu_Collapse, | |
195 | Menu_Toggle, | |
196 | Menu_New, | |
197 | Menu_NewKey, | |
198 | Menu_NewText, | |
199 | Menu_NewBinary, | |
200 | Menu_Delete, | |
201 | ||
202 | Ctrl_RegTree = 200, | |
203 | }; | |
204 | ||
205 | // ---------------------------------------------------------------------------- | |
206 | // event tables | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
209 | BEGIN_EVENT_TABLE(RegFrame, wxFrame) | |
210 | EVT_MENU(Menu_Test, RegFrame::OnTest) | |
211 | EVT_MENU(Menu_About, RegFrame::OnAbout) | |
212 | EVT_MENU(Menu_Quit, RegFrame::OnQuit) | |
213 | EVT_MENU(Menu_Expand, RegFrame::OnExpand) | |
214 | EVT_MENU(Menu_Collapse, RegFrame::OnCollapse) | |
215 | EVT_MENU(Menu_Toggle, RegFrame::OnToggle) | |
216 | EVT_MENU(Menu_Delete, RegFrame::OnDelete) | |
217 | EVT_MENU(Menu_NewKey, RegFrame::OnNewKey) | |
218 | EVT_MENU(Menu_NewText, RegFrame::OnNewText) | |
219 | EVT_MENU(Menu_NewBinary,RegFrame::OnNewBinary) | |
220 | END_EVENT_TABLE() | |
221 | ||
222 | BEGIN_EVENT_TABLE(RegTreeCtrl, wxTreeCtrl) | |
223 | EVT_TREE_DELETE_ITEM (Ctrl_RegTree, RegTreeCtrl::OnDeleteItem) | |
224 | EVT_TREE_ITEM_EXPANDING(Ctrl_RegTree, RegTreeCtrl::OnItemExpanding) | |
225 | EVT_TREE_SEL_CHANGED (Ctrl_RegTree, RegTreeCtrl::OnSelChanged) | |
226 | ||
227 | EVT_CHAR (RegTreeCtrl::OnChar) | |
228 | EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick) | |
229 | END_EVENT_TABLE() | |
230 | ||
231 | // ============================================================================ | |
232 | // implementation | |
233 | // ============================================================================ | |
234 | ||
235 | // ---------------------------------------------------------------------------- | |
236 | // global functions | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
239 | // create the "registry operations" menu | |
240 | wxMenu *CreateRegistryMenu() | |
241 | { | |
242 | wxMenu *pMenuNew = new wxMenu; | |
243 | pMenuNew->Append(Menu_NewKey, "&Key", "Create a new key"); | |
244 | pMenuNew->AppendSeparator(); | |
245 | pMenuNew->Append(Menu_NewText, "&Text value", "Create a new text value"); | |
246 | pMenuNew->Append(Menu_NewBinary, "&Binary value", "Create a new binary value"); | |
247 | ||
248 | wxMenu *pMenuReg = new wxMenu; | |
249 | pMenuReg->Append(Menu_New, "&New", pMenuNew); | |
250 | pMenuReg->Append(Menu_Delete, "&Delete...", "Delete selected key/value"); | |
251 | pMenuReg->AppendSeparator(); | |
252 | pMenuReg->Append(Menu_Expand, "&Expand", "Expand current key"); | |
253 | pMenuReg->Append(Menu_Collapse, "&Collapse", "Collapse current key"); | |
254 | pMenuReg->Append(Menu_Toggle, "&Toggle", "Toggle current key"); | |
255 | ||
256 | return pMenuReg; | |
257 | } | |
258 | ||
259 | // ---------------------------------------------------------------------------- | |
260 | // application class | |
261 | // ---------------------------------------------------------------------------- | |
262 | IMPLEMENT_APP(RegApp) | |
263 | ||
264 | // `Main program' equivalent, creating windows and returning main app frame | |
265 | bool RegApp::OnInit() | |
266 | { | |
267 | // create the main frame window and show it | |
268 | RegFrame *frame = new RegFrame(NULL, "wxRegKey Test", 50, 50, 600, 350); | |
269 | frame->Show(true); | |
270 | ||
271 | SetTopWindow(frame); | |
272 | ||
273 | return true; | |
274 | } | |
275 | ||
276 | // ---------------------------------------------------------------------------- | |
277 | // RegFrame | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
280 | RegFrame::RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h) | |
281 | : wxFrame(parent, -1, title, wxPoint(x, y), wxSize(w, h)) | |
282 | { | |
283 | // this reduces flicker effects | |
284 | SetBackgroundColour(wxColour(255, 255, 255)); | |
285 | ||
286 | // set the icon | |
287 | // ------------ | |
288 | SetIcon(wxIcon("app_icon")); | |
289 | ||
290 | // create menu | |
291 | // ----------- | |
292 | wxMenu *pMenuFile = new wxMenu; | |
293 | pMenuFile->Append(Menu_Test, "Te&st", "Test key creation"); | |
294 | pMenuFile->AppendSeparator(); | |
295 | pMenuFile->Append(Menu_About, "&About...", "Show an extraordinarly beautiful dialog"); | |
296 | pMenuFile->AppendSeparator(); | |
297 | pMenuFile->Append(Menu_Quit, "E&xit", "Quit this program"); | |
298 | ||
299 | wxMenuBar *pMenu = new wxMenuBar; | |
300 | pMenu->Append(pMenuFile, "&File"); | |
301 | pMenu->Append(CreateRegistryMenu(), "&Registry"); | |
302 | SetMenuBar(pMenu); | |
303 | ||
304 | // create child controls | |
305 | // --------------------- | |
306 | m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree); | |
307 | ||
308 | // create the status line | |
309 | // ---------------------- | |
310 | int aWidths[2]; | |
311 | aWidths[0] = 200; | |
312 | aWidths[1] = -1; | |
313 | CreateStatusBar(2); | |
314 | SetStatusWidths(2, aWidths); | |
315 | } | |
316 | ||
317 | RegFrame::~RegFrame(void) | |
318 | { | |
319 | } | |
320 | ||
321 | void RegFrame::OnQuit(wxCommandEvent& event) | |
322 | { | |
323 | Close(TRUE); | |
324 | } | |
325 | ||
326 | void RegFrame::OnAbout(wxCommandEvent& event) | |
327 | { | |
328 | wxMessageDialog dialog(this, "wxRegistry sample\n(c) 1998 Vadim Zeitlin", | |
329 | "About wxRegistry", wxOK); | |
330 | ||
331 | dialog.ShowModal(); | |
332 | } | |
333 | ||
334 | void RegFrame::OnTest(wxCommandEvent& event) | |
335 | { | |
336 | m_treeCtrl->OnMenuTest(); | |
337 | } | |
338 | ||
339 | void RegFrame::OnExpand(wxCommandEvent& event) | |
340 | { | |
341 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_EXPAND); | |
342 | } | |
343 | ||
344 | void RegFrame::OnCollapse(wxCommandEvent& event) | |
345 | { | |
346 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_COLLAPSE); | |
347 | } | |
348 | ||
349 | void RegFrame::OnToggle(wxCommandEvent& event) | |
350 | { | |
351 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_TOGGLE); | |
352 | } | |
353 | ||
354 | void RegFrame::OnDelete(wxCommandEvent& event) | |
355 | { | |
356 | m_treeCtrl->DeleteSelected(); | |
357 | } | |
358 | ||
359 | void RegFrame::OnNewKey(wxCommandEvent& event) | |
360 | { | |
361 | if ( m_treeCtrl->IsKeySelected() ) { | |
362 | m_treeCtrl->CreateNewKey( | |
363 | wxGetTextFromUser("Enter the name of the new key")); | |
364 | } | |
365 | } | |
366 | ||
367 | void RegFrame::OnNewText(wxCommandEvent& event) | |
368 | { | |
369 | if ( m_treeCtrl->IsKeySelected() ) { | |
370 | m_treeCtrl->CreateNewTextValue( | |
371 | wxGetTextFromUser("Enter the name for the new text value")); | |
372 | } | |
373 | } | |
374 | ||
375 | void RegFrame::OnNewBinary(wxCommandEvent& event) | |
376 | { | |
377 | if ( m_treeCtrl->IsKeySelected() ) { | |
378 | m_treeCtrl->CreateNewBinaryValue( | |
379 | wxGetTextFromUser("Enter the name for the new binary value")); | |
380 | } | |
381 | } | |
382 | ||
383 | // ---------------------------------------------------------------------------- | |
384 | // RegImageList | |
385 | // ---------------------------------------------------------------------------- | |
386 | RegImageList::RegImageList() : wxImageList(16, 16, TRUE) | |
387 | { | |
388 | // should be in sync with enum RegImageList::RegIcon | |
389 | static const char *aszIcons[] = { "key1","key2","key3","value1","value2" }; | |
390 | wxString str = "icon_"; | |
391 | for ( uint n = 0; n < WXSIZEOF(aszIcons); n++ ) { | |
392 | Add(wxIcon(str + aszIcons[n], wxBITMAP_TYPE_ICO_RESOURCE)); | |
393 | } | |
394 | } | |
395 | ||
396 | // ---------------------------------------------------------------------------- | |
397 | // RegTreeCtrl | |
398 | // ---------------------------------------------------------------------------- | |
399 | ||
400 | // create a new tree item and insert it into the tree | |
401 | RegTreeCtrl::TreeNode *RegTreeCtrl::InsertNewTreeNode(TreeNode *pParent, | |
402 | const wxString& strName, | |
403 | int idImage, | |
404 | const wxString *pstrValue) | |
405 | { | |
406 | // create new item & insert it | |
407 | TreeNode *pNewNode = new TreeNode; | |
408 | pNewNode->m_pTree = this; | |
409 | pNewNode->m_pParent = pParent; | |
410 | pNewNode->m_strName = strName; | |
411 | pNewNode->m_bKey = pstrValue == NULL; | |
412 | pNewNode->m_pKey = NULL; | |
413 | pNewNode->m_id = InsertItem(pParent ? pParent->m_id : 0, | |
414 | pNewNode->IsKey() ? strName : *pstrValue, | |
415 | idImage); | |
416 | ||
417 | wxASSERT_MSG( pNewNode->m_id, "can't create tree control item!"); | |
418 | ||
419 | // save the pointer in the item | |
420 | if ( !SetItemData(pNewNode->m_id, (long)pNewNode) ) { | |
421 | wxFAIL_MSG("can't store item's data in tree control!"); | |
422 | } | |
423 | ||
424 | // add it to the list of parent's children | |
425 | if ( pParent != NULL ) { | |
426 | pParent->m_aChildren.Add(pNewNode); | |
427 | } | |
428 | ||
429 | // force the [+] button (@@@ not very elegant...) | |
430 | if ( pNewNode->IsKey() ) | |
431 | pNewNode->AddDummy(); | |
432 | ||
433 | return pNewNode; | |
434 | } | |
435 | ||
436 | RegTreeCtrl::RegTreeCtrl(wxWindow *parent, wxWindowID id) | |
437 | : wxTreeCtrl(parent, id, wxDefaultPosition, wxDefaultSize, | |
438 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER) | |
439 | { | |
440 | // create the image list | |
441 | // --------------------- | |
442 | m_imageList = new RegImageList; | |
443 | SetImageList(m_imageList, wxIMAGE_LIST_NORMAL); | |
444 | ||
445 | // create root keys | |
446 | // ---------------- | |
447 | m_pRoot = InsertNewTreeNode(NULL, "Registry Root", RegImageList::Root); | |
448 | ||
449 | // create popup menu | |
450 | // ----------------- | |
451 | m_pMenuPopup = CreateRegistryMenu(); | |
452 | } | |
453 | ||
454 | RegTreeCtrl::~RegTreeCtrl() | |
455 | { | |
456 | delete m_pMenuPopup; | |
457 | delete m_pRoot; | |
458 | delete m_imageList; | |
459 | } | |
460 | ||
461 | void RegTreeCtrl::AddStdKeys() | |
462 | { | |
463 | for ( uint ui = 0; ui < wxRegKey::nStdKeys; ui++ ) { | |
464 | InsertNewTreeNode(m_pRoot, wxRegKey::GetStdKeyName(ui)); | |
465 | } | |
466 | } | |
467 | ||
468 | // ---------------------------------------------------------------------------- | |
469 | // notifications | |
470 | // ---------------------------------------------------------------------------- | |
471 | ||
472 | void RegTreeCtrl::OnRightClick(wxMouseEvent& event) | |
473 | { | |
474 | int iFlags; | |
475 | long lId = HitTest(wxPoint(event.GetX(), event.GetY()), iFlags); | |
476 | if ( iFlags & wxTREE_HITTEST_ONITEMLABEL ) { | |
477 | // popup menu only if an item was clicked | |
478 | wxASSERT( lId != 0 ); | |
479 | SelectItem(lId); | |
480 | PopupMenu(m_pMenuPopup, event.GetX(), event.GetY()); | |
481 | } | |
482 | } | |
483 | ||
484 | ||
485 | void RegTreeCtrl::OnDeleteItem(wxTreeEvent& event) | |
486 | { | |
487 | } | |
488 | ||
489 | // test the key creation functions | |
490 | void RegTreeCtrl::OnMenuTest() | |
491 | { | |
492 | long lId = GetSelection(); | |
493 | TreeNode *pNode = (TreeNode *)GetItemData(lId); | |
494 | ||
c99a3a1d | 495 | wxCHECK_RET( pNode != NULL, "tree item without data?" ); |
bbf1f0e5 KB |
496 | |
497 | if ( pNode->IsRoot() ) { | |
498 | wxLogError("Can't create a subkey under the root key."); | |
499 | return; | |
500 | } | |
501 | if ( !pNode->IsKey() ) { | |
502 | wxLogError("Can't create a subkey under a value!"); | |
503 | return; | |
504 | } | |
505 | ||
506 | wxRegKey key1(pNode->Key(), "key1"); | |
507 | if ( key1.Create() ) { | |
508 | wxRegKey key2a(key1, "key2a"), key2b(key1, "key2b"); | |
509 | if ( key2a.Create() && key2b.Create() ) { | |
510 | // put some values under the newly created keys | |
511 | key1.SetValue("first_term", "10"); | |
512 | key1.SetValue("second_term", "7"); | |
513 | key2a = "this is the unnamed value"; | |
514 | key2b.SetValue("sum", 17); | |
515 | ||
516 | // refresh tree | |
517 | pNode->Refresh(); | |
518 | wxLogStatus("Test keys successfully added."); | |
519 | return; | |
520 | } | |
521 | } | |
522 | ||
523 | wxLogError("Creation of test keys failed."); | |
524 | } | |
525 | ||
526 | void RegTreeCtrl::OnChar(wxKeyEvent& event) | |
527 | { | |
528 | if ( event.KeyCode() == WXK_DELETE ) | |
529 | DeleteSelected(); | |
530 | else | |
531 | wxTreeCtrl::OnChar(event); | |
532 | } | |
533 | ||
534 | void RegTreeCtrl::OnSelChanged(wxTreeEvent& event) | |
535 | { | |
536 | wxFrame *pFrame = (wxFrame *)(wxWindow::GetParent()); | |
537 | pFrame->SetStatusText(GetNode(event)->FullName(), 1); | |
538 | } | |
539 | ||
540 | void RegTreeCtrl::OnItemExpanding(wxTreeEvent& event) | |
541 | { | |
542 | TreeNode *pNode = GetNode(event); | |
543 | bool bExpanding = event.m_code == wxTREE_EXPAND_EXPAND; | |
544 | ||
545 | // expansion might take some time | |
546 | wxSetCursor(*wxHOURGLASS_CURSOR); | |
547 | wxLogStatus("Working..."); | |
548 | wxYield(); // to give the status line a chance to refresh itself | |
549 | ||
550 | if ( pNode->IsKey() ) { | |
551 | if ( bExpanding ) { | |
552 | // expanding: add subkeys/values | |
553 | if ( !pNode->OnExpand() ) | |
554 | return; | |
555 | } | |
556 | else { | |
557 | // collapsing: clean up | |
558 | pNode->OnCollapse(); | |
559 | } | |
560 | ||
561 | // change icon for non root key | |
562 | if ( !pNode->IsRoot() ) { | |
563 | int idIcon = bExpanding ? RegImageList::OpenedKey | |
564 | : RegImageList::ClosedKey; | |
565 | SetItemImage(pNode->Id(), idIcon, idIcon); | |
566 | } | |
567 | } | |
568 | ||
569 | wxLogStatus("Ok"); | |
570 | wxSetCursor(*wxSTANDARD_CURSOR); | |
571 | } | |
572 | ||
573 | // ---------------------------------------------------------------------------- | |
574 | // TreeNode implementation | |
575 | // ---------------------------------------------------------------------------- | |
576 | bool RegTreeCtrl::TreeNode::OnExpand() | |
577 | { | |
578 | // remove dummy item | |
579 | if ( m_lDummy != 0 ) | |
580 | m_pTree->DeleteItem(m_lDummy); | |
581 | ||
582 | if ( IsRoot() ) { | |
583 | // we're the root key | |
584 | m_pTree->AddStdKeys(); | |
585 | return true; | |
586 | } | |
587 | ||
588 | if ( Parent()->IsRoot() ) { | |
589 | // we're a standard key | |
590 | m_pKey = new wxRegKey(m_strName); | |
591 | } | |
592 | else { | |
593 | // we're a normal key | |
594 | m_pKey = new wxRegKey(*(Parent()->m_pKey), m_strName); | |
595 | } | |
596 | ||
597 | if ( !m_pKey->Open() ) { | |
598 | m_lDummy = 0; | |
599 | wxLogError("The key '%s' can't be opened.", FullName()); | |
600 | return false; | |
601 | } | |
602 | ||
603 | // enumeration variables | |
604 | long l; | |
605 | wxString str; | |
606 | bool bCont; | |
607 | ||
608 | // enumerate all subkeys | |
609 | bCont = m_pKey->GetFirstKey(str, l); | |
610 | while ( bCont ) { | |
611 | m_pTree->InsertNewTreeNode(this, str, RegImageList::ClosedKey); | |
612 | bCont = m_pKey->GetNextKey(str, l); | |
613 | } | |
614 | ||
615 | // enumerate all values | |
616 | bCont = m_pKey->GetFirstValue(str, l); | |
617 | while ( bCont ) { | |
618 | wxString strItem; | |
619 | if (str.IsEmpty()) | |
620 | strItem = "<default>"; | |
621 | else | |
622 | strItem = str; | |
623 | strItem += " = "; | |
624 | ||
625 | // determine the appropriate icon | |
626 | RegImageList::Icon icon; | |
627 | switch ( m_pKey->GetValueType(str) ) { | |
628 | case wxRegKey::Type_String: | |
629 | case wxRegKey::Type_Expand_String: | |
630 | case wxRegKey::Type_Multi_String: | |
631 | { | |
632 | wxString strValue; | |
633 | icon = RegImageList::TextValue; | |
634 | m_pKey->QueryValue(str, strValue); | |
635 | strItem += strValue; | |
636 | } | |
637 | break; | |
638 | ||
639 | case wxRegKey::Type_None: | |
640 | // @@ handle the error... | |
641 | icon = RegImageList::BinaryValue; | |
642 | break; | |
643 | ||
644 | case wxRegKey::Type_Dword: | |
645 | { | |
646 | char szBuf[128]; | |
647 | long l; | |
648 | m_pKey->QueryValue(str, &l); | |
649 | sprintf(szBuf, "%lx", l); | |
650 | strItem += szBuf; | |
651 | } | |
652 | ||
653 | // fall through | |
654 | ||
655 | default: | |
656 | icon = RegImageList::BinaryValue; | |
657 | } | |
658 | ||
659 | m_pTree->InsertNewTreeNode(this, str, icon, &strItem); | |
660 | bCont = m_pKey->GetNextValue(str, l); | |
661 | } | |
662 | ||
663 | return true; | |
664 | } | |
665 | ||
666 | void RegTreeCtrl::TreeNode::OnCollapse() | |
667 | { | |
668 | bool bHasChildren = !m_aChildren.IsEmpty(); | |
669 | DestroyChildren(); | |
670 | if ( bHasChildren ) | |
671 | AddDummy(); | |
672 | else | |
673 | m_lDummy = 0; | |
674 | ||
675 | delete m_pKey; | |
676 | m_pKey = NULL; | |
677 | } | |
678 | ||
679 | void RegTreeCtrl::TreeNode::AddDummy() | |
680 | { | |
681 | // insert dummy item forcing appearance of [+] button | |
682 | m_lDummy = m_pTree->InsertItem(Id(), ""); | |
683 | } | |
684 | ||
685 | void RegTreeCtrl::TreeNode::DestroyChildren() | |
686 | { | |
687 | // destroy all children | |
688 | uint nCount = m_aChildren.Count(); | |
689 | for ( uint n = 0; n < nCount; n++ ) { | |
690 | long lId = m_aChildren[n]->Id(); | |
691 | delete m_aChildren[n]; | |
692 | m_pTree->DeleteItem(lId); | |
693 | } | |
694 | ||
695 | m_aChildren.Empty(); | |
696 | } | |
697 | ||
698 | RegTreeCtrl::TreeNode::~TreeNode() | |
699 | { | |
700 | DestroyChildren(); | |
701 | ||
702 | delete m_pKey; | |
703 | } | |
704 | ||
705 | const char *RegTreeCtrl::TreeNode::FullName() const | |
706 | { | |
707 | static wxString s_strName; | |
708 | ||
709 | if ( IsRoot() ) { | |
710 | return "Registry Root"; | |
711 | } | |
712 | else { | |
713 | // our own registry key might not (yet) exist or we might be a value, | |
714 | // so just use the parent's and concatenate | |
715 | s_strName = Parent()->FullName(); | |
716 | s_strName << '\\' << m_strName; | |
717 | ||
718 | return s_strName; | |
719 | } | |
720 | } | |
721 | ||
722 | // ---------------------------------------------------------------------------- | |
723 | // operations on RegTreeCtrl | |
724 | // ---------------------------------------------------------------------------- | |
725 | ||
726 | void RegTreeCtrl::DeleteSelected() | |
727 | { | |
728 | long lCurrent = GetSelection(), | |
729 | lParent = GetParent(lCurrent); | |
730 | ||
731 | if ( lParent == 0 ) { | |
732 | wxLogError("Can't delete root key."); | |
733 | return; | |
734 | } | |
735 | ||
736 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent), | |
737 | *pParent = (TreeNode *)GetItemData(lParent); | |
738 | ||
c99a3a1d | 739 | wxCHECK_RET( pCurrent && pParent, "either node or parent without data?" ); |
bbf1f0e5 KB |
740 | |
741 | if ( pParent->IsRoot() ) { | |
742 | wxLogError("Can't delete standard key."); | |
743 | return; | |
744 | } | |
745 | ||
746 | if ( pCurrent->IsKey() ) { | |
747 | if ( wxMessageBox("Do you really want to delete this key?", | |
748 | "Confirmation", | |
749 | wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { | |
750 | return; | |
751 | } | |
752 | ||
753 | // must close key before deleting it | |
754 | pCurrent->OnCollapse(); | |
755 | ||
756 | if ( pParent->Key().DeleteKey(pCurrent->m_strName) ) | |
757 | pParent->Refresh(); | |
758 | } | |
759 | else { | |
760 | if ( wxMessageBox("Do you really want to delete this value?", | |
761 | "Confirmation", | |
762 | wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { | |
763 | return; | |
764 | } | |
765 | ||
766 | if ( pParent->Key().DeleteValue(pCurrent->m_strName) ) | |
767 | pParent->Refresh(); | |
768 | } | |
769 | } | |
770 | ||
771 | void RegTreeCtrl::CreateNewKey(const wxString& strName) | |
772 | { | |
773 | long lCurrent = GetSelection(); | |
774 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
775 | ||
c99a3a1d | 776 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); |
bbf1f0e5 KB |
777 | |
778 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
779 | ||
780 | if ( pCurrent->IsRoot() ) { | |
781 | wxLogError("Can't create a new key under the root key."); | |
782 | return; | |
783 | } | |
784 | ||
785 | wxRegKey key(pCurrent->Key(), strName); | |
786 | if ( key.Create() ) | |
787 | pCurrent->Refresh(); | |
788 | } | |
789 | ||
790 | void RegTreeCtrl::CreateNewTextValue(const wxString& strName) | |
791 | { | |
792 | long lCurrent = GetSelection(); | |
793 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
794 | ||
c99a3a1d | 795 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); |
bbf1f0e5 KB |
796 | |
797 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
798 | ||
799 | if ( pCurrent->IsRoot() ) { | |
800 | wxLogError("Can't create a new value under the root key."); | |
801 | return; | |
802 | } | |
803 | ||
804 | if ( pCurrent->Key().SetValue(strName, "") ) | |
805 | pCurrent->Refresh(); | |
806 | } | |
807 | ||
808 | void RegTreeCtrl::CreateNewBinaryValue(const wxString& strName) | |
809 | { | |
810 | long lCurrent = GetSelection(); | |
811 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
812 | ||
c99a3a1d | 813 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); |
bbf1f0e5 KB |
814 | |
815 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
816 | ||
817 | if ( pCurrent->IsRoot() ) { | |
818 | wxLogError("Can't create a new value under the root key."); | |
819 | return; | |
820 | } | |
821 | ||
822 | if ( pCurrent->Key().SetValue(strName, 0) ) | |
823 | pCurrent->Refresh(); | |
824 | } | |
825 | ||
826 | bool RegTreeCtrl::IsKeySelected() const | |
827 | { | |
828 | long lCurrent = GetSelection(); | |
829 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
830 | ||
c99a3a1d | 831 | wxCHECK( pCurrent != NULL, false ); |
bbf1f0e5 KB |
832 | |
833 | return pCurrent->IsKey(); | |
834 | } |