| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: regtest.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 | #include "wx/msw/imaglist.h" |
| 33 | |
| 34 | #include "wx/tokenzr.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // application type |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | class RegApp : public wxApp |
| 40 | { |
| 41 | public: |
| 42 | bool OnInit(); |
| 43 | }; |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | // image list with registry icons |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | class RegImageList : public wxImageList |
| 49 | { |
| 50 | public: |
| 51 | enum Icon |
| 52 | { |
| 53 | Root, |
| 54 | ClosedKey, |
| 55 | OpenedKey, |
| 56 | TextValue, |
| 57 | BinaryValue, |
| 58 | }; |
| 59 | |
| 60 | RegImageList(); |
| 61 | }; |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | // our control |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | class RegTreeCtrl : public wxTreeCtrl |
| 67 | { |
| 68 | public: |
| 69 | // ctor & dtor |
| 70 | RegTreeCtrl(wxWindow *parent, wxWindowID id); |
| 71 | virtual ~RegTreeCtrl(); |
| 72 | |
| 73 | // notifications |
| 74 | void OnDeleteItem (wxTreeEvent& event); |
| 75 | void OnItemExpanding(wxTreeEvent& event); |
| 76 | void OnSelChanged (wxTreeEvent& event); |
| 77 | |
| 78 | void OnBeginEdit (wxTreeEvent& event); |
| 79 | void OnEndEdit (wxTreeEvent& event); |
| 80 | |
| 81 | void OnBeginDrag (wxTreeEvent& event); |
| 82 | void OnEndDrag (wxTreeEvent& event); |
| 83 | |
| 84 | void OnRightClick (wxMouseEvent& event); |
| 85 | void OnChar (wxKeyEvent& event); |
| 86 | void OnIdle (wxIdleEvent& event); |
| 87 | |
| 88 | // forwarded notifications (by the frame) |
| 89 | void OnMenuTest(); |
| 90 | |
| 91 | // operations |
| 92 | void GoTo(const wxString& location); |
| 93 | void Refresh(); |
| 94 | void DeleteSelected(); |
| 95 | void ShowProperties(); |
| 96 | void CreateNewKey(const wxString& strName); |
| 97 | void CreateNewTextValue(const wxString& strName); |
| 98 | void CreateNewBinaryValue(const wxString& strName); |
| 99 | |
| 100 | // information |
| 101 | bool IsKeySelected() const; |
| 102 | |
| 103 | private: |
| 104 | // structure describing a registry key/value |
| 105 | class TreeNode : public wxTreeItemData |
| 106 | { |
| 107 | WX_DEFINE_ARRAY(TreeNode *, TreeChildren); |
| 108 | public: |
| 109 | RegTreeCtrl *m_pTree; // must be !NULL |
| 110 | TreeNode *m_pParent; // NULL only for the root node |
| 111 | long m_id; // the id of the tree control item |
| 112 | wxString m_strName; // name of the key/value |
| 113 | TreeChildren m_aChildren; // array of subkeys/values |
| 114 | bool m_bKey; // key or value? |
| 115 | wxRegKey *m_pKey; // only may be !NULL if m_bKey == true |
| 116 | |
| 117 | // trivial accessors |
| 118 | long Id() const { return m_id; } |
| 119 | bool IsRoot() const { return m_pParent == NULL; } |
| 120 | bool IsKey() const { return m_bKey; } |
| 121 | TreeNode *Parent() const { return m_pParent; } |
| 122 | |
| 123 | // notifications |
| 124 | bool OnExpand(); |
| 125 | void OnCollapse(); |
| 126 | |
| 127 | // operations |
| 128 | void Refresh(); |
| 129 | bool DeleteChild(TreeNode *child); |
| 130 | void DestroyChildren(); |
| 131 | const wxChar *FullName() const; |
| 132 | |
| 133 | // get the associated key: make sure the pointer is !NULL |
| 134 | wxRegKey& Key() { if ( !m_pKey ) OnExpand(); return *m_pKey; } |
| 135 | |
| 136 | // dtor deletes all children |
| 137 | ~TreeNode(); |
| 138 | }; |
| 139 | |
| 140 | wxImageList *m_imageList; |
| 141 | wxMenu *m_pMenuPopup; |
| 142 | |
| 143 | TreeNode *m_pRoot; |
| 144 | |
| 145 | TreeNode *m_draggedItem; // the item being dragged |
| 146 | bool m_copyOnDrop; // if FALSE, then move |
| 147 | |
| 148 | bool m_restoreStatus; // after OnItemExpanding() |
| 149 | |
| 150 | wxString m_nameOld; // the initial value of item being renamed |
| 151 | |
| 152 | TreeNode *GetNode(const wxTreeEvent& event) |
| 153 | { return (TreeNode *)GetItemData((WXHTREEITEM)event.GetItem()); } |
| 154 | |
| 155 | public: |
| 156 | // create a new node and insert it to the tree |
| 157 | TreeNode *InsertNewTreeNode(TreeNode *pParent, |
| 158 | const wxString& strName, |
| 159 | int idImage = RegImageList::ClosedKey, |
| 160 | const wxString *pstrValue = NULL); |
| 161 | // add standard registry keys |
| 162 | void AddStdKeys(); |
| 163 | |
| 164 | private: |
| 165 | DECLARE_EVENT_TABLE() |
| 166 | }; |
| 167 | |
| 168 | // ---------------------------------------------------------------------------- |
| 169 | // the main window of our application |
| 170 | // ---------------------------------------------------------------------------- |
| 171 | class RegFrame : public wxFrame |
| 172 | { |
| 173 | public: |
| 174 | // ctor & dtor |
| 175 | RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h); |
| 176 | virtual ~RegFrame(); |
| 177 | |
| 178 | // callbacks |
| 179 | void OnQuit (wxCommandEvent& event); |
| 180 | void OnAbout(wxCommandEvent& event); |
| 181 | void OnTest (wxCommandEvent& event); |
| 182 | |
| 183 | void OnGoTo (wxCommandEvent& event); |
| 184 | |
| 185 | void OnExpand (wxCommandEvent& event); |
| 186 | void OnCollapse(wxCommandEvent& event); |
| 187 | void OnToggle (wxCommandEvent& event); |
| 188 | void OnRefresh (wxCommandEvent& event); |
| 189 | |
| 190 | void OnDelete (wxCommandEvent& event); |
| 191 | void OnNewKey (wxCommandEvent& event); |
| 192 | void OnNewText (wxCommandEvent& event); |
| 193 | void OnNewBinary(wxCommandEvent& event); |
| 194 | |
| 195 | void OnInfo (wxCommandEvent& event); |
| 196 | |
| 197 | DECLARE_EVENT_TABLE() |
| 198 | |
| 199 | private: |
| 200 | RegTreeCtrl *m_treeCtrl; |
| 201 | }; |
| 202 | |
| 203 | // ---------------------------------------------------------------------------- |
| 204 | // various ids |
| 205 | // ---------------------------------------------------------------------------- |
| 206 | |
| 207 | enum |
| 208 | { |
| 209 | Menu_Quit = 100, |
| 210 | Menu_About, |
| 211 | Menu_Test, |
| 212 | Menu_GoTo, |
| 213 | Menu_Expand, |
| 214 | Menu_Collapse, |
| 215 | Menu_Toggle, |
| 216 | Menu_Refresh, |
| 217 | Menu_New, |
| 218 | Menu_NewKey, |
| 219 | Menu_NewText, |
| 220 | Menu_NewBinary, |
| 221 | Menu_Delete, |
| 222 | Menu_Info, |
| 223 | |
| 224 | Ctrl_RegTree = 200, |
| 225 | }; |
| 226 | |
| 227 | // ---------------------------------------------------------------------------- |
| 228 | // event tables |
| 229 | // ---------------------------------------------------------------------------- |
| 230 | |
| 231 | BEGIN_EVENT_TABLE(RegFrame, wxFrame) |
| 232 | EVT_MENU(Menu_Test, RegFrame::OnTest) |
| 233 | EVT_MENU(Menu_About, RegFrame::OnAbout) |
| 234 | EVT_MENU(Menu_Quit, RegFrame::OnQuit) |
| 235 | EVT_MENU(Menu_GoTo, RegFrame::OnGoTo) |
| 236 | EVT_MENU(Menu_Expand, RegFrame::OnExpand) |
| 237 | EVT_MENU(Menu_Collapse, RegFrame::OnCollapse) |
| 238 | EVT_MENU(Menu_Toggle, RegFrame::OnToggle) |
| 239 | EVT_MENU(Menu_Refresh, RegFrame::OnRefresh) |
| 240 | EVT_MENU(Menu_Delete, RegFrame::OnDelete) |
| 241 | EVT_MENU(Menu_NewKey, RegFrame::OnNewKey) |
| 242 | EVT_MENU(Menu_NewText, RegFrame::OnNewText) |
| 243 | EVT_MENU(Menu_NewBinary,RegFrame::OnNewBinary) |
| 244 | EVT_MENU(Menu_Info, RegFrame::OnInfo) |
| 245 | END_EVENT_TABLE() |
| 246 | |
| 247 | BEGIN_EVENT_TABLE(RegTreeCtrl, wxTreeCtrl) |
| 248 | EVT_TREE_DELETE_ITEM (Ctrl_RegTree, RegTreeCtrl::OnDeleteItem) |
| 249 | EVT_TREE_ITEM_EXPANDING(Ctrl_RegTree, RegTreeCtrl::OnItemExpanding) |
| 250 | EVT_TREE_SEL_CHANGED (Ctrl_RegTree, RegTreeCtrl::OnSelChanged) |
| 251 | |
| 252 | EVT_TREE_BEGIN_LABEL_EDIT(Ctrl_RegTree, RegTreeCtrl::OnBeginEdit) |
| 253 | EVT_TREE_END_LABEL_EDIT (Ctrl_RegTree, RegTreeCtrl::OnEndEdit) |
| 254 | |
| 255 | EVT_TREE_BEGIN_DRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag) |
| 256 | EVT_TREE_BEGIN_RDRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag) |
| 257 | EVT_TREE_END_DRAG (Ctrl_RegTree, RegTreeCtrl::OnEndDrag) |
| 258 | |
| 259 | EVT_CHAR (RegTreeCtrl::OnChar) |
| 260 | EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick) |
| 261 | EVT_IDLE (RegTreeCtrl::OnIdle) |
| 262 | END_EVENT_TABLE() |
| 263 | |
| 264 | // ============================================================================ |
| 265 | // implementation |
| 266 | // ============================================================================ |
| 267 | |
| 268 | // ---------------------------------------------------------------------------- |
| 269 | // global functions |
| 270 | // ---------------------------------------------------------------------------- |
| 271 | |
| 272 | // create the "registry operations" menu |
| 273 | wxMenu *CreateRegistryMenu() |
| 274 | { |
| 275 | wxMenu *pMenuNew = new wxMenu; |
| 276 | pMenuNew->Append(Menu_NewKey, "&Key", "Create a new key"); |
| 277 | pMenuNew->AppendSeparator(); |
| 278 | pMenuNew->Append(Menu_NewText, "&Text value", "Create a new text value"); |
| 279 | pMenuNew->Append(Menu_NewBinary, "&Binary value", "Create a new binary value"); |
| 280 | |
| 281 | wxMenu *pMenuReg = new wxMenu; |
| 282 | pMenuReg->Append(Menu_New, "&New", pMenuNew); |
| 283 | pMenuReg->Append(Menu_Delete, "&Delete...", "Delete selected key/value"); |
| 284 | pMenuReg->AppendSeparator(); |
| 285 | pMenuReg->Append(Menu_GoTo, "&Go to...\tCtrl-G", "Go to registry key"); |
| 286 | pMenuReg->Append(Menu_Expand, "&Expand", "Expand current key"); |
| 287 | pMenuReg->Append(Menu_Collapse, "&Collapse", "Collapse current key"); |
| 288 | pMenuReg->Append(Menu_Toggle, "&Toggle", "Toggle current key"); |
| 289 | pMenuReg->AppendSeparator(); |
| 290 | pMenuReg->Append(Menu_Refresh, "&Refresh", "Refresh the subtree"); |
| 291 | pMenuReg->AppendSeparator(); |
| 292 | pMenuReg->Append(Menu_Info, "&Properties","Information about current selection"); |
| 293 | |
| 294 | return pMenuReg; |
| 295 | } |
| 296 | |
| 297 | // ---------------------------------------------------------------------------- |
| 298 | // application class |
| 299 | // ---------------------------------------------------------------------------- |
| 300 | IMPLEMENT_APP(RegApp) |
| 301 | |
| 302 | // `Main program' equivalent, creating windows and returning main app frame |
| 303 | bool RegApp::OnInit() |
| 304 | { |
| 305 | // create the main frame window and show it |
| 306 | RegFrame *frame = new RegFrame(NULL, "wxRegTest", 50, 50, 600, 350); |
| 307 | frame->Show(TRUE); |
| 308 | |
| 309 | SetTopWindow(frame); |
| 310 | |
| 311 | return TRUE; |
| 312 | } |
| 313 | |
| 314 | // ---------------------------------------------------------------------------- |
| 315 | // RegFrame |
| 316 | // ---------------------------------------------------------------------------- |
| 317 | |
| 318 | RegFrame::RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h) |
| 319 | : wxFrame(parent, -1, title, wxPoint(x, y), wxSize(w, h)) |
| 320 | { |
| 321 | // this reduces flicker effects |
| 322 | SetBackgroundColour(wxColour(255, 255, 255)); |
| 323 | |
| 324 | // set the icon |
| 325 | // ------------ |
| 326 | SetIcon(wxIcon("app_icon")); |
| 327 | |
| 328 | // create menu |
| 329 | // ----------- |
| 330 | wxMenu *pMenuFile = new wxMenu; |
| 331 | pMenuFile->Append(Menu_Test, "Te&st", "Test key creation"); |
| 332 | pMenuFile->AppendSeparator(); |
| 333 | pMenuFile->Append(Menu_About, "&About...", "Show an extraordinarly beautiful dialog"); |
| 334 | pMenuFile->AppendSeparator(); |
| 335 | pMenuFile->Append(Menu_Quit, "E&xit", "Quit this program"); |
| 336 | |
| 337 | wxMenuBar *pMenu = new wxMenuBar; |
| 338 | pMenu->Append(pMenuFile, "&File"); |
| 339 | pMenu->Append(CreateRegistryMenu(), "&Registry"); |
| 340 | SetMenuBar(pMenu); |
| 341 | |
| 342 | // create child controls |
| 343 | // --------------------- |
| 344 | m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree); |
| 345 | |
| 346 | // create the status line |
| 347 | // ---------------------- |
| 348 | CreateStatusBar(2); |
| 349 | } |
| 350 | |
| 351 | RegFrame::~RegFrame() |
| 352 | { |
| 353 | // this makes deletion of it *much* quicker |
| 354 | m_treeCtrl->Hide(); |
| 355 | } |
| 356 | |
| 357 | void RegFrame::OnQuit(wxCommandEvent& event) |
| 358 | { |
| 359 | Close(TRUE); |
| 360 | } |
| 361 | |
| 362 | void RegFrame::OnAbout(wxCommandEvent& event) |
| 363 | { |
| 364 | wxMessageDialog dialog(this, |
| 365 | "wxRegistry sample\n" |
| 366 |