]> git.saurik.com Git - wxWidgets.git/blame - samples/regtest/regtest.cpp
Applied patch #845884 (Fixes Unicode compilation of stc sample with Watcom).
[wxWidgets.git] / samples / regtest / regtest.cpp
CommitLineData
bbf1f0e5 1///////////////////////////////////////////////////////////////////////////////
23f681ec 2// Name: regtest.cpp
bbf1f0e5
KB
3// Purpose: wxRegKey class demo
4// Author: Vadim Zeitlin
23f681ec 5// Modified by:
bbf1f0e5
KB
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"
42c5812d 32#include "wx/msw/imaglist.h"
bbf1f0e5 33
c41ea66a
VZ
34#include "wx/tokenzr.h"
35
bbf1f0e5
KB
36// ----------------------------------------------------------------------------
37// application type
38// ----------------------------------------------------------------------------
39class RegApp : public wxApp
23f681ec 40{
bbf1f0e5 41public:
23f681ec 42 bool OnInit();
bbf1f0e5
KB
43};
44
45// ----------------------------------------------------------------------------
46// image list with registry icons
47// ----------------------------------------------------------------------------
48class RegImageList : public wxImageList
49{
50public:
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// ----------------------------------------------------------------------------
66class RegTreeCtrl : public wxTreeCtrl
67{
68public:
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
c41ea66a
VZ
78 void OnBeginEdit (wxTreeEvent& event);
79 void OnEndEdit (wxTreeEvent& event);
80
23f681ec
VZ
81 void OnBeginDrag (wxTreeEvent& event);
82 void OnEndDrag (wxTreeEvent& event);
83
bbf1f0e5
KB
84 void OnRightClick (wxMouseEvent& event);
85 void OnChar (wxKeyEvent& event);
23f681ec 86 void OnIdle (wxIdleEvent& event);
bbf1f0e5
KB
87
88 // forwarded notifications (by the frame)
89 void OnMenuTest();
90
91 // operations
c41ea66a 92 void GoTo(const wxString& location);
5888ef1e 93 void Refresh();
bbf1f0e5 94 void DeleteSelected();
23f681ec 95 void ShowProperties();
bbf1f0e5
KB
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
bbf1f0e5 103private:
bbf1f0e5 104 // structure describing a registry key/value
23f681ec 105 class TreeNode : public wxTreeItemData
bbf1f0e5 106 {
38d6b957 107 WX_DEFINE_ARRAY_PTR(TreeNode *, TreeChildren);
42c5812d 108 public:
23f681ec
VZ
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();
a60b1f5d 131 const wxChar *FullName() const;
23f681ec
VZ
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();
bbf1f0e5
KB
138 };
139
23f681ec 140 wxImageList *m_imageList;
bbf1f0e5 141 wxMenu *m_pMenuPopup;
23f681ec 142
bbf1f0e5 143 TreeNode *m_pRoot;
23f681ec
VZ
144
145 TreeNode *m_draggedItem; // the item being dragged
146 bool m_copyOnDrop; // if FALSE, then move
147
148 bool m_restoreStatus; // after OnItemExpanding()
bbf1f0e5 149
c41ea66a
VZ
150 wxString m_nameOld; // the initial value of item being renamed
151
bbf1f0e5 152 TreeNode *GetNode(const wxTreeEvent& event)
42c5812d 153 { return (TreeNode *)GetItemData((WXHTREEITEM)event.GetItem()); }
bbf1f0e5
KB
154
155public:
156 // create a new node and insert it to the tree
23f681ec 157 TreeNode *InsertNewTreeNode(TreeNode *pParent,
bbf1f0e5
KB
158 const wxString& strName,
159 int idImage = RegImageList::ClosedKey,
160 const wxString *pstrValue = NULL);
161 // add standard registry keys
162 void AddStdKeys();
23f681ec
VZ
163
164private:
f6bcfd97 165 DECLARE_EVENT_TABLE()
bbf1f0e5
KB
166};
167
168// ----------------------------------------------------------------------------
169// the main window of our application
170// ----------------------------------------------------------------------------
171class RegFrame : public wxFrame
23f681ec 172{
bbf1f0e5
KB
173public:
174 // ctor & dtor
600683ca 175 RegFrame(wxFrame *parent, wxChar *title, int x, int y, int w, int h);
23f681ec
VZ
176 virtual ~RegFrame();
177
bbf1f0e5
KB
178 // callbacks
179 void OnQuit (wxCommandEvent& event);
180 void OnAbout(wxCommandEvent& event);
181 void OnTest (wxCommandEvent& event);
182
c41ea66a
VZ
183 void OnGoTo (wxCommandEvent& event);
184
bbf1f0e5
KB
185 void OnExpand (wxCommandEvent& event);
186 void OnCollapse(wxCommandEvent& event);
187 void OnToggle (wxCommandEvent& event);
5888ef1e 188 void OnRefresh (wxCommandEvent& event);
bbf1f0e5
KB
189
190 void OnDelete (wxCommandEvent& event);
191 void OnNewKey (wxCommandEvent& event);
192 void OnNewText (wxCommandEvent& event);
193 void OnNewBinary(wxCommandEvent& event);
194
23f681ec 195 void OnInfo (wxCommandEvent& event);
bbf1f0e5 196
f6bcfd97 197 DECLARE_EVENT_TABLE()
bbf1f0e5
KB
198
199private:
200 RegTreeCtrl *m_treeCtrl;
201};
202
203// ----------------------------------------------------------------------------
204// various ids
205// ----------------------------------------------------------------------------
206
207enum
208{
209 Menu_Quit = 100,
210 Menu_About,
211 Menu_Test,
c41ea66a 212 Menu_GoTo,
bbf1f0e5
KB
213 Menu_Expand,
214 Menu_Collapse,
215 Menu_Toggle,
5888ef1e 216 Menu_Refresh,
bbf1f0e5 217 Menu_New,
23f681ec
VZ
218 Menu_NewKey,
219 Menu_NewText,
bbf1f0e5
KB
220 Menu_NewBinary,
221 Menu_Delete,
23f681ec 222 Menu_Info,
bbf1f0e5
KB
223
224 Ctrl_RegTree = 200,
225};
226
227// ----------------------------------------------------------------------------
228// event tables
229// ----------------------------------------------------------------------------
230
231BEGIN_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)
c41ea66a 235 EVT_MENU(Menu_GoTo, RegFrame::OnGoTo)
bbf1f0e5
KB
236 EVT_MENU(Menu_Expand, RegFrame::OnExpand)
237 EVT_MENU(Menu_Collapse, RegFrame::OnCollapse)
238 EVT_MENU(Menu_Toggle, RegFrame::OnToggle)
5888ef1e 239 EVT_MENU(Menu_Refresh, RegFrame::OnRefresh)
bbf1f0e5
KB
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)
23f681ec 244 EVT_MENU(Menu_Info, RegFrame::OnInfo)
bbf1f0e5
KB
245END_EVENT_TABLE()
246
247BEGIN_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)
c41ea66a
VZ
251
252 EVT_TREE_BEGIN_LABEL_EDIT(Ctrl_RegTree, RegTreeCtrl::OnBeginEdit)
253 EVT_TREE_END_LABEL_EDIT (Ctrl_RegTree, RegTreeCtrl::OnEndEdit)
254
23f681ec
VZ
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)
bbf1f0e5
KB
258
259 EVT_CHAR (RegTreeCtrl::OnChar)
260 EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick)
23f681ec 261 EVT_IDLE (RegTreeCtrl::OnIdle)
bbf1f0e5
KB
262END_EVENT_TABLE()
263
264// ============================================================================
265// implementation
266// ============================================================================
267
268// ----------------------------------------------------------------------------
269// global functions
270// ----------------------------------------------------------------------------
271
272// create the "registry operations" menu
273wxMenu *CreateRegistryMenu()
274{
275 wxMenu *pMenuNew = new wxMenu;
600683ca 276 pMenuNew->Append(Menu_NewKey, _T("&Key"), _T("Create a new key"));
bbf1f0e5 277 pMenuNew->AppendSeparator();
600683ca
MB
278 pMenuNew->Append(Menu_NewText, _T("&Text value"), _T("Create a new text value"));
279 pMenuNew->Append(Menu_NewBinary, _T("&Binary value"), _T("Create a new binary value"));
bbf1f0e5
KB
280
281 wxMenu *pMenuReg = new wxMenu;
600683ca
MB
282 pMenuReg->Append(Menu_New, _T("&New"), pMenuNew);
283 pMenuReg->Append(Menu_Delete, _T("&Delete..."), _T("Delete selected key/value"));
bbf1f0e5 284 pMenuReg->AppendSeparator();
600683ca
MB
285 pMenuReg->Append(Menu_GoTo, _T("&Go to...\tCtrl-G"), _T("Go to registry key"));
286 pMenuReg->Append(Menu_Expand, _T("&Expand"), _T("Expand current key"));
287 pMenuReg->Append(Menu_Collapse, _T("&Collapse"), _T("Collapse current key"));
288 pMenuReg->Append(Menu_Toggle, _T("&Toggle"), _T("Toggle current key"));
23f681ec 289 pMenuReg->AppendSeparator();
600683ca 290 pMenuReg->Append(Menu_Refresh, _T("&Refresh"), _T("Refresh the subtree"));
5888ef1e 291 pMenuReg->AppendSeparator();
600683ca 292 pMenuReg->Append(Menu_Info, _T("&Properties"),_T("Information about current selection"));
bbf1f0e5
KB
293
294 return pMenuReg;
295}
296
297// ----------------------------------------------------------------------------
298// application class
299// ----------------------------------------------------------------------------
300IMPLEMENT_APP(RegApp)
301
302// `Main program' equivalent, creating windows and returning main app frame
303bool RegApp::OnInit()
304{
305 // create the main frame window and show it
600683ca 306 RegFrame *frame = new RegFrame(NULL, _T("wxRegTest"), 50, 50, 600, 350);
a42b93aa 307 frame->Show(TRUE);
23f681ec 308
bbf1f0e5
KB
309 SetTopWindow(frame);
310
a42b93aa 311 return TRUE;
bbf1f0e5
KB
312}
313
314// ----------------------------------------------------------------------------
315// RegFrame
316// ----------------------------------------------------------------------------
317
600683ca 318RegFrame::RegFrame(wxFrame *parent, wxChar *title, int x, int y, int w, int h)
bbf1f0e5
KB
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 // ------------
600683ca 326 SetIcon(wxIcon(_T("app_icon")));
bbf1f0e5
KB
327
328 // create menu
329 // -----------
330 wxMenu *pMenuFile = new wxMenu;
600683ca 331 pMenuFile->Append(Menu_Test, _T("Te&st"), _T("Test key creation"));
bbf1f0e5 332 pMenuFile->AppendSeparator();
600683ca 333 pMenuFile->Append(Menu_About, _T("&About..."), _T("Show an extraordinarly beautiful dialog"));
bbf1f0e5 334 pMenuFile->AppendSeparator();
600683ca 335 pMenuFile->Append(Menu_Quit, _T("E&xit"), _T("Quit this program"));
bbf1f0e5
KB
336
337 wxMenuBar *pMenu = new wxMenuBar;
600683ca
MB
338 pMenu->Append(pMenuFile, _T("&File"));
339 pMenu->Append(CreateRegistryMenu(), _T("&Registry"));
bbf1f0e5
KB
340 SetMenuBar(pMenu);
341
342 // create child controls
343 // ---------------------
344 m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree);
345
346 // create the status line
347 // ----------------------
bbf1f0e5 348 CreateStatusBar(2);
bbf1f0e5
KB
349}
350
23f681ec 351RegFrame::~RegFrame()
bbf1f0e5 352{
23f681ec
VZ
353 // this makes deletion of it *much* quicker
354 m_treeCtrl->Hide();
bbf1f0e5
KB
355}
356
357void RegFrame::OnQuit(wxCommandEvent& event)
358{
359 Close(TRUE);
360}
361
362void RegFrame::OnAbout(wxCommandEvent& event)
363{
23f681ec 364 wxMessageDialog dialog(this,
600683ca
MB
365 _T("wxRegistry sample\n")
366