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