wxruby 1.9.3-i386-mswin32 → 1.9.4-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/LICENSE +53 -53
  2. data/README +299 -299
  3. data/lib/wx/classes/evthandler.rb +9 -0
  4. data/lib/wx/classes/timer.rb +25 -0
  5. data/lib/wx/version.rb +1 -1
  6. data/lib/wx.rb +42 -42
  7. data/lib/wxruby2.exp +0 -0
  8. data/lib/wxruby2.lib +0 -0
  9. data/lib/wxruby2.so +0 -0
  10. data/samples/SAMPLES-LICENSE.TXT +17 -17
  11. data/samples/bigdemo/About.rbw +39 -39
  12. data/samples/bigdemo/ColorPanel.rbw +25 -25
  13. data/samples/bigdemo/GridSimple.rbw +80 -80
  14. data/samples/bigdemo/MDIDemo.rbw +59 -59
  15. data/samples/bigdemo/PopupMenu.rbw +151 -151
  16. data/samples/bigdemo/Sizers.rbw +545 -545
  17. data/samples/bigdemo/bigdemo.rb +826 -826
  18. data/samples/bigdemo/demoTemplate.rbw +37 -37
  19. data/samples/bigdemo/icons/choice.xpm +27 -27
  20. data/samples/bigdemo/icons/combo.xpm +27 -27
  21. data/samples/bigdemo/icons/copy.xpm +25 -25
  22. data/samples/bigdemo/icons/cut.xpm +24 -24
  23. data/samples/bigdemo/icons/gauge.xpm +27 -27
  24. data/samples/bigdemo/icons/help.xpm +25 -25
  25. data/samples/bigdemo/icons/list.xpm +27 -27
  26. data/samples/bigdemo/icons/mondrian.xpm +44 -44
  27. data/samples/bigdemo/icons/new.xpm +24 -24
  28. data/samples/bigdemo/icons/ogl.ico +0 -0
  29. data/samples/bigdemo/icons/ogl.xpm +45 -45
  30. data/samples/bigdemo/icons/open.xpm +26 -26
  31. data/samples/bigdemo/icons/paste.xpm +38 -38
  32. data/samples/bigdemo/icons/preview.xpm +26 -26
  33. data/samples/bigdemo/icons/print.xpm +26 -26
  34. data/samples/bigdemo/icons/radio.xpm +27 -27
  35. data/samples/bigdemo/icons/robert.xpm +415 -415
  36. data/samples/bigdemo/icons/save.xpm +25 -25
  37. data/samples/bigdemo/icons/smiles.xpm +39 -39
  38. data/samples/bigdemo/icons/smiley.xpm +42 -42
  39. data/samples/bigdemo/icons/stattext.xpm +24 -24
  40. data/samples/bigdemo/icons/test2.bmp +0 -0
  41. data/samples/bigdemo/icons/test2.xpm +79 -79
  42. data/samples/bigdemo/icons/text.xpm +27 -27
  43. data/samples/bigdemo/icons/tog1.xpm +38 -38
  44. data/samples/bigdemo/icons/tog2.xpm +38 -38
  45. data/samples/bigdemo/icons/wxwin16x16.xpm +25 -25
  46. data/samples/bigdemo/tips.txt +7 -7
  47. data/samples/bigdemo/utils.rb +11 -11
  48. data/samples/bigdemo/wxButton.rbw +66 -66
  49. data/samples/bigdemo/wxCalendarCtrl.rbw +72 -72
  50. data/samples/bigdemo/wxCheckBox.rbw +52 -52
  51. data/samples/bigdemo/wxCheckListBox.rbw +77 -77
  52. data/samples/bigdemo/wxChoice.rbw +49 -49
  53. data/samples/bigdemo/wxColourDialog.rbw +34 -34
  54. data/samples/bigdemo/wxComboBox.rbw +79 -79
  55. data/samples/bigdemo/wxDialog.rbw +92 -92
  56. data/samples/bigdemo/wxDirDialog.rbw +32 -32
  57. data/samples/bigdemo/wxFileDialog.rbw +39 -39
  58. data/samples/bigdemo/wxFileDialog_Save.rbw +38 -38
  59. data/samples/bigdemo/wxFindReplaceDialog.rbw +85 -85
  60. data/samples/bigdemo/wxFontDialog.rbw +176 -176
  61. data/samples/bigdemo/wxFrame.rbw +55 -55
  62. data/samples/bigdemo/wxGauge.rbw +73 -73
  63. data/samples/bigdemo/wxGrid.rbw +68 -68
  64. data/samples/bigdemo/wxListBox.rbw +142 -142
  65. data/samples/bigdemo/wxListCtrl_virtual.rbw +109 -109
  66. data/samples/bigdemo/wxMDIWindows.rbw +52 -52
  67. data/samples/bigdemo/wxMenu.rbw +238 -238
  68. data/samples/bigdemo/wxMessageDialog.rbw +30 -30
  69. data/samples/bigdemo/wxMultipleChoiceDialog.rbw +34 -34
  70. data/samples/bigdemo/wxNotebook.rbw +138 -138
  71. data/samples/bigdemo/wxProgressDialog.rbw +45 -45
  72. data/samples/bigdemo/wxRadioBox.rbw +74 -74
  73. data/samples/bigdemo/wxRadioButton.rbw +127 -127
  74. data/samples/bigdemo/wxScrolledMessageDialog.rbw +59 -59
  75. data/samples/bigdemo/wxScrolledWindow.rbw +201 -201
  76. data/samples/bigdemo/wxSingleChoiceDialog.rbw +35 -35
  77. data/samples/bigdemo/wxSlider.rbw +44 -44
  78. data/samples/bigdemo/wxSpinButton.rbw +52 -52
  79. data/samples/bigdemo/wxSpinCtrl.rbw +53 -53
  80. data/samples/bigdemo/wxSplitterWindow.rbw +65 -65
  81. data/samples/bigdemo/wxStaticBitmap.rbw +53 -53
  82. data/samples/bigdemo/wxStaticText.rbw +57 -57
  83. data/samples/bigdemo/wxStatusBar.rbw +128 -128
  84. data/samples/bigdemo/wxTextCtrl.rbw +151 -151
  85. data/samples/bigdemo/wxTextEntryDialog.rbw +34 -34
  86. data/samples/bigdemo/wxToggleButton.rbw +51 -51
  87. data/samples/bigdemo/wxToolBar.rbw +133 -133
  88. data/samples/bigdemo/wxTreeCtrl.rbw +192 -192
  89. data/samples/calendar/calendar.rb +275 -275
  90. data/samples/caret/caret.rb +286 -286
  91. data/samples/caret/mondrian.xpm +44 -44
  92. data/samples/controls/controls.rb +1140 -1140
  93. data/samples/controls/get_item_sample.rb +87 -87
  94. data/samples/controls/icons/choice.xpm +27 -27
  95. data/samples/controls/icons/combo.xpm +27 -27
  96. data/samples/controls/icons/gauge.xpm +27 -27
  97. data/samples/controls/icons/list.xpm +27 -27
  98. data/samples/controls/icons/radio.xpm +27 -27
  99. data/samples/controls/icons/stattext.xpm +24 -24
  100. data/samples/controls/icons/text.xpm +27 -27
  101. data/samples/controls/mondrian.xpm +44 -44
  102. data/samples/dialogs/dialogs.rb +724 -724
  103. data/samples/dialogs/tips.txt +18 -18
  104. data/samples/drawing/graphics_drawing.rb +22 -18
  105. data/samples/drawing/images.rb +48 -48
  106. data/samples/etc/activation.rb +108 -108
  107. data/samples/etc/choice.rb +72 -72
  108. data/samples/etc/miniframe.rb +84 -84
  109. data/samples/etc/sash.rb +135 -135
  110. data/samples/etc/scrollwin.rb +116 -116
  111. data/samples/etc/system_settings.rb +258 -258
  112. data/samples/etc/threaded.rb +5 -8
  113. data/samples/etc/wizard.rb +79 -79
  114. data/samples/html/html.rb +14 -10
  115. data/samples/listbook/listbook.rb +181 -181
  116. data/samples/listbook/listbook.xrc +370 -370
  117. data/samples/mdi/mdi.rb +87 -87
  118. data/samples/minimal/minimal.rb +85 -85
  119. data/samples/minimal/nothing.rb +21 -21
  120. data/samples/text/textctrl.rb +124 -124
  121. data/samples/text/unicode.rb +238 -238
  122. data/samples/text/utf8.txt +14 -14
  123. data/samples/treectrl/treectrl.rb +1180 -1180
  124. data/samples/xrc/samples.xrc +46 -46
  125. data/samples/xrc/xrc_sample.rb +107 -107
  126. metadata +3 -3
@@ -1,1180 +1,1180 @@
1
- #!/usr/bin/env ruby
2
- # wxRuby2 Sample Code. Copyright (c) 2004-2006 Kevin B. Smith
3
- # Freely reusable code: see SAMPLES-LICENSE.TXT for details
4
-
5
- begin
6
- require 'wx'
7
- rescue LoadError => no_wx_err
8
- begin
9
- require 'rubygems'
10
- require 'wx'
11
- rescue LoadError
12
- raise no_wx_err
13
- end
14
- end
15
-
16
- # This sample shows off the features of the TreeCtrl widget. The actual
17
- # features vary somewhat across platforms; for example, the button
18
- # styles and connecting lines between items are not available on OS X.
19
- # Also, some items remain to be implemented - for example, setting item
20
- # icons and item data.
21
-
22
- # Just some event ids for the numerous menu items
23
- TreeTest_TogButtons,
24
- TreeTest_TogTwist,
25
- TreeTest_TogLines,
26
- TreeTest_TogEdit,
27
- TreeTest_TogHideRoot,
28
- TreeTest_TogRootLines,
29
- TreeTest_TogBorder,
30
- TreeTest_TogFullHighlight,
31
- TreeTest_SetFgColour,
32
- TreeTest_SetBgColour,
33
- TreeTest_ResetStyle,
34
- TreeTest_Dump,
35
- TreeTest_DumpSelected,
36
- TreeTest_Count,
37
- TreeTest_CountRec,
38
- TreeTest_Sort,
39
- TreeTest_SortRev,
40
- TreeTest_SetBold,
41
- TreeTest_ClearBold,
42
- TreeTest_Rename,
43
- TreeTest_Delete,
44
- TreeTest_DeleteChildren,
45
- TreeTest_DeleteAll,
46
- TreeTest_Recreate,
47
- TreeTest_ToggleImages,
48
- TreeTest_ToggleButtons,
49
- TreeTest_SetImageSize,
50
- TreeTest_ToggleSel,
51
- TreeTest_CollapseAndReset,
52
- TreeTest_EnsureVisible,
53
- TreeTest_AddItem,
54
- TreeTest_InsertItem,
55
- TreeTest_IncIndent,
56
- TreeTest_DecIndent,
57
- TreeTest_IncSpacing,
58
- TreeTest_DecSpacing,
59
- TreeTest_ToggleIcon,
60
- TreeTest_Select,
61
- TreeTest_Unselect = (Wx::ID_HIGHEST..Wx::ID_HIGHEST+39).to_a
62
-
63
- TreeCtrlIcon_File,
64
- TreeCtrlIcon_FileSelected,
65
- TreeCtrlIcon_Folder,
66
- TreeCtrlIcon_FolderSelected,
67
- TreeCtrlIcon_FolderOpened = 0,1,2,3,4
68
-
69
- class MyTreeCtrl < Wx::TreeCtrl
70
- def initialize(parent, args)
71
- super
72
-
73
- @reverse_sort = false
74
-
75
- create_image_list
76
-
77
- # add some items to the tree
78
- add_test_items_to_tree(5, 2)
79
-
80
- # TreeCtrl supports a large number of different events...
81
- evt_tree_begin_drag self, :on_begin_drag
82
- evt_tree_begin_rdrag self, :on_begin_rdrag
83
- evt_tree_end_drag self, :on_end_drag
84
- evt_tree_begin_label_edit self, :on_begin_label_edit
85
- evt_tree_end_label_edit self, :on_end_label_edit
86
- evt_tree_delete_item self, :on_delete_item
87
- evt_tree_set_info self, :on_set_info
88
- evt_tree_item_expanded self, :on_item_expanded
89
- evt_tree_item_expanding self, :on_item_expanding
90
- evt_tree_item_collapsed self, :on_item_collapsed
91
- evt_tree_item_collapsing self, :on_item_collapsing
92
- evt_tree_item_right_click self, :on_item_right_click
93
- evt_tree_sel_changed self, :on_sel_changed
94
- evt_tree_sel_changing self, :on_sel_changing
95
- evt_tree_key_down self, :on_tree_key_down
96
- evt_tree_item_activated self, :on_item_activated
97
-
98
- evt_right_dclick :on_rmouse_dclick
99
- evt_right_up :on_rmouse_up
100
- end
101
-
102
- def show_info(id)
103
- Wx::log_message("Item '%s': %sselected, %sexpanded, %sbold,\n" +
104
- "%d children (%d immediately under selected item).",
105
- get_item_text( id ),
106
- bool_to_str( is_selected( id ) ),
107
- bool_to_str( is_expanded( id ) ),
108
- bool_to_str( is_bold( id ) ),
109
- get_children_count( id ),
110
- get_children_count(id, false) )
111
- end
112
-
113
- def bool_to_str(bool)
114
- bool ? " " : "not "
115
- end
116
-
117
- def do_sort_children(item, reverse = false)
118
- @reverse_sort = reverse
119
- sort_children(item)
120
- end
121
-
122
- def do_ensure_visible()
123
- ensure_visible(@last_item)
124
- end
125
-
126
- def image_size()
127
- return @image_size
128
- end
129
-
130
- def is_test_item(item)
131
- # the test item is the first child folder
132
- return item_parent(item) == root_item && ! prev_sibling(item)
133
- end
134
-
135
- def create_image_list
136
- @imageSize = 16
137
-
138
- # Make an image list containing small icons
139
- images = Wx::ImageList.new(32, 32, true)
140
- # should correspond to TreeCtrlIcon_xxx enum
141
- Wx::BusyCursor.busy do
142
- icons = (1 .. 5).map do | i |
143
- icon_file = File.join(File.dirname(__FILE__), "icon#{i}.xpm")
144
- Wx::Bitmap.new(icon_file, Wx::BITMAP_TYPE_XPM)
145
- end
146
- icons.each { | ic | images.add(ic) }
147
- self.image_list = images
148
- end
149
- end
150
-
151
- def unset_image_list
152
- self.image_list = nil
153
- end
154
-
155
- def create_buttons_image_list()
156
- # Make an image list containing small icons
157
- images = Wx::ImageList.new(16, 16, true)
158
-
159
- # should correspond to TreeCtrlIcon_xxx enum
160
- Wx::BusyCursor.busy do |x|
161
- icons = [
162
- Wx::Icon.new("icon3.xpm"), # closed
163
- Wx::Icon.new("icon3.xpm"), # closed, selected
164
- Wx::Icon.new("icon5.xpm"), # open
165
- Wx::Icon.new("icon5.xpm") ] # open, selected
166
-
167
- icons.each do | ic |
168
- orig_size = ic.get_width()
169
- if size == orig_size
170
- images.add(ic)
171
- else
172
- resized = ic.convert_to_image.rescale(size, size)
173
- images.add( Wx::Bitmap.new(resized) )
174
- end
175
- end
176
-
177
- self.buttons_image_list = images
178
- end
179
- end
180
-
181
- def on_compare_items(item1,item2)
182
- if @reverse_sort
183
- # just exchange 1st and 2nd items
184
- return super(item2, item1)
185
- else
186
- return super(item1, item2)
187
- end
188
- end
189
-
190
- def add_items_recursively(parent_id, num_children, depth, folder)
191
- if depth > 0
192
- has_children = depth > 1
193
-
194
- for n in 0 ... num_children
195
- # at depth 1 elements won't have any more children
196
- if has_children
197
- str = sprintf("%s child %d", "Folder", n + 1)
198
- else
199
- str = sprintf("%s child %d.%d", "File", folder, n + 1)
200
- end
201
- # here we pass to append_item() normal and selected item images (we
202
- # suppose that selected image follows the normal one in the enum)
203
- if Wx::THE_APP.show_images
204
- image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder
205
- imageSel = image + 1
206
- else
207
- image = imageSel = -1
208
- end
209
- id = append_item(parent_id, str, image, imageSel)
210
-
211
- # and now we also set the expanded one (only for the folders)
212
- if has_children && Wx::get_app.show_images()
213
- set_item_image( id, TreeCtrlIcon_FolderOpened,
214
- Wx::TREE_ITEM_ICON_EXPANDED)
215
- end
216
-
217
- # remember the last child for OnEnsureVisible()
218
- if ! has_children && n == num_children - 1
219
- @last_item = id
220
- end
221
-
222
- add_items_recursively(id, num_children, depth - 1, n + 1)
223
- end
224
- end
225
- end
226
-
227
- def add_test_items_to_tree(num_children,depth)
228
- image = Wx::THE_APP.show_images ? TreeCtrlIcon_Folder : -1
229
- root_id = add_root("Root",image, image)
230
- if image != -1
231
- set_item_image( root_id, TreeCtrlIcon_FolderOpened,
232
- Wx::TREE_ITEM_ICON_EXPANDED)
233
- end
234
-
235
- add_items_recursively(root_id, num_children, depth, 0)
236
-
237
- # set some colours/fonts for testing
238
- # note that font sizes can also be varied, but only on platforms
239
- # that use the generic TreeCtrl - OS X and GTK, and only if
240
- # Wx::TR_HAS_VARIABLE_ROW_HEIGHT style was used in the constructor
241
- font = get_font
242
- font.style = Wx::FONTSTYLE_ITALIC
243
- set_item_font(root_id, font)
244
-
245
- ids = get_children(root_id)
246
-
247
- # make the first item blue
248
- set_item_text_colour(ids[0], Wx::BLUE)
249
-
250
- # make the third item red on grey
251
- if Wx::PLATFORM == "WXMSW"
252
- set_item_text_colour(ids[2], Wx::RED)
253
- set_item_background_colour(ids[2], Wx::LIGHT_GREY)
254
- end
255
- end
256
-
257
- def get_items_recursively(parent_id, cookie)
258
- if cookie == -1
259
- id, cookie = get_first_child(parent_id)
260
- else
261
- id, cookie = get_next_child(parent_id, cookie)
262
- end
263
- if not id.nonzero?
264
- return nil
265
- end
266
-
267
- text = item_text(id)
268
- Wx::log_message(text)
269
-
270
- if item_has_children(id)
271
- get_items_recursively(id,-1)
272
- end
273
- get_items_recursively(parent_id, cookie)
274
- end
275
-
276
- def do_toggle_icon(item)
277
- old_img = get_item_image(item)
278
- if old_img == TreeCtrlIcon_Folder
279
- new_img = TreeCtrlIcon_File
280
- else
281
- new_img = TreeCtrlIcon_Folder
282
- end
283
-
284
- set_item_image(item, new_img)
285
- end
286
-
287
- def on_begin_rdrag(event)
288
- Wx::log_message("OnBeginRDrag")
289
- event.skip
290
- end
291
-
292
- def on_delete_item(event)
293
- Wx::log_message("OnDeleteItem")
294
- event.skip
295
- end
296
-
297
- def on_get_info(event)
298
- Wx::log_message("OnGetInfo")
299
- event.skip
300
- end
301
-
302
- def on_set_info(event)
303
- Wx::log_message("OnSetInfo")
304
- event.skip
305
- end
306
-
307
- def on_item_expanded(event)
308
- Wx::log_message("OnItemExpanded")
309
- event.skip
310
- end
311
-
312
- def on_item_expanding(event)
313
- Wx::log_message("OnItemExpanding")
314
- event.skip
315
- end
316
-
317
- def on_item_collapsed(event)
318
- Wx::log_message("OnItemCollapsed")
319
- event.skip
320
- end
321
-
322
- def on_sel_changed(event)
323
- Wx::log_message("OnSelChanged")
324
- event.skip
325
- end
326
-
327
- def on_sel_changing(event)
328
- Wx::log_message("OnSelChanging")
329
- event.skip
330
- end
331
-
332
- def log_key_event(name,event)
333
- keycode = event.key_code
334
-
335
- case keycode
336
- when Wx::K_BACK
337
- key = "BACK"
338
- when Wx::K_TAB
339
- key = "TAB"
340
- when Wx::K_RETURN
341
- key = "RETURN"
342
- when Wx::K_ESCAPE
343
- key = "ESCAPE"
344
- when Wx::K_SPACE
345
- key = "SPACE"
346
- when Wx::K_DELETE
347
- key = "DELETE"
348
- when Wx::K_START
349
- key = "START"
350
- when Wx::K_LBUTTON
351
- key = "LBUTTON"
352
- when Wx::K_RBUTTON
353
- key = "RBUTTON"
354
- when Wx::K_CANCEL
355
- key = "CANCEL"
356
- when Wx::K_MBUTTON
357
- key = "MBUTTON"
358
- when Wx::K_CLEAR
359
- key = "CLEAR"
360
- when Wx::K_SHIFT
361
- key = "SHIFT"
362
- when Wx::K_ALT
363
- key = "ALT"
364
- when Wx::K_CONTROL
365
- key = "CONTROL"
366
- when Wx::K_MENU
367
- key = "MENU"
368
- when Wx::K_PAUSE
369
- key = "PAUSE"
370
- when Wx::K_CAPITAL
371
- key = "CAPITAL"
372
- when Wx::K_PRIOR
373
- key = "PRIOR"
374
- when Wx::K_NEXT
375
- key = "NEXT"
376
- when Wx::K_END
377
- key = "END"
378
- when Wx::K_HOME
379
- key = "HOME"
380
- when Wx::K_LEFT
381
- key = "LEFT"
382
- when Wx::K_UP
383
- key = "UP"
384
- when Wx::K_RIGHT
385
- key = "RIGHT"
386
- when Wx::K_DOWN
387
- key = "DOWN"
388
- when Wx::K_SELECT
389
- key = "SELECT"
390
- when Wx::K_PRINT
391
- key = "PRINT"
392
- when Wx::K_EXECUTE
393
- key = "EXECUTE"
394
- when Wx::K_SNAPSHOT
395
- key = "SNAPSHOT"
396
- when Wx::K_INSERT
397
- key = "INSERT"
398
- when Wx::K_HELP
399
- key = "HELP"
400
- when Wx::K_NUMPAD0
401
- key = "NUMPAD0"
402
- when Wx::K_NUMPAD1
403
- key = "NUMPAD1"
404
- when Wx::K_NUMPAD2
405
- key = "NUMPAD2"
406
- when Wx::K_NUMPAD3
407
- key = "NUMPAD3"
408
- when Wx::K_NUMPAD4
409
- key = "NUMPAD4"
410
- when Wx::K_NUMPAD5
411
- key = "NUMPAD5"
412
- when Wx::K_NUMPAD6
413
- key = "NUMPAD6"
414
- when Wx::K_NUMPAD7
415
- key = "NUMPAD7"
416
- when Wx::K_NUMPAD8
417
- key = "NUMPAD8"
418
- when Wx::K_NUMPAD9
419
- key = "NUMPAD9"
420
- when Wx::K_MULTIPLY
421
- key = "MULTIPLY"
422
- when Wx::K_ADD
423
- key = "ADD"
424
- when Wx::K_SEPARATOR
425
- key = "SEPARATOR"
426
- when Wx::K_SUBTRACT
427
- key = "SUBTRACT"
428
- when Wx::K_DECIMAL
429
- key = "DECIMAL"
430
- when Wx::K_DIVIDE
431
- key = "DIVIDE"
432
- when Wx::K_F1
433
- key = "F1"
434
- when Wx::K_F2
435
- key = "F2"
436
- when Wx::K_F3
437
- key = "F3"
438
- when Wx::K_F4
439
- key = "F4"
440
- when Wx::K_F5
441
- key = "F5"
442
- when Wx::K_F6
443
- key = "F6"
444
- when Wx::K_F7
445
- key = "F7"
446
- when Wx::K_F8
447
- key = "F8"
448
- when Wx::K_F9
449
- key = "F9"
450
- when Wx::K_F10
451
- key = "F10"
452
- when Wx::K_F11
453
- key = "F11"
454
- when Wx::K_F12
455
- key = "F12"
456
- when Wx::K_F13
457
- key = "F13"
458
- when Wx::K_F14
459
- key = "F14"
460
- when Wx::K_F15
461
- key = "F15"
462
- when Wx::K_F16
463
- key = "F16"
464
- when Wx::K_F17
465
- key = "F17"
466
- when Wx::K_F18
467
- key = "F18"
468
- when Wx::K_F19
469
- key = "F19"
470
- when Wx::K_F20
471
- key = "F20"
472
- when Wx::K_F21
473
- key = "F21"
474
- when Wx::K_F22
475
- key = "F22"
476
- when Wx::K_F23
477
- key = "F23"
478
- when Wx::K_F24
479
- key = "F24"
480
- when Wx::K_NUMLOCK
481
- key = "NUMLOCK"
482
- when Wx::K_SCROLL
483
- key = "SCROLL"
484
- when Wx::K_PAGEUP
485
- key = "PAGEUP"
486
- when Wx::K_PAGEDOWN
487
- key = "PAGEDOWN"
488
- when Wx::K_NUMPAD_SPACE
489
- key = "NUMPAD_SPACE"
490
- when Wx::K_NUMPAD_TAB
491
- key = "NUMPAD_TAB"
492
- when Wx::K_NUMPAD_ENTER
493
- key = "NUMPAD_ENTER"
494
- when Wx::K_NUMPAD_F1
495
- key = "NUMPAD_F1"
496
- when Wx::K_NUMPAD_F2
497
- key = "NUMPAD_F2"
498
- when Wx::K_NUMPAD_F3
499
- key = "NUMPAD_F3"
500
- when Wx::K_NUMPAD_F4
501
- key = "NUMPAD_F4"
502
- when Wx::K_NUMPAD_HOME
503
- key = "NUMPAD_HOME"
504
- when Wx::K_NUMPAD_LEFT
505
- key = "NUMPAD_LEFT"
506
- when Wx::K_NUMPAD_UP
507
- key = "NUMPAD_UP"
508
- when Wx::K_NUMPAD_RIGHT
509
- key = "NUMPAD_RIGHT"
510
- when Wx::K_NUMPAD_DOWN
511
- key = "NUMPAD_DOWN"
512
- when Wx::K_NUMPAD_PRIOR
513
- key = "NUMPAD_PRIOR"
514
- when Wx::K_NUMPAD_PAGEUP
515
- key = "NUMPAD_PAGEUP"
516
- when Wx::K_NUMPAD_PAGEDOWN
517
- key = "NUMPAD_PAGEDOWN"
518
- when Wx::K_NUMPAD_END
519
- key = "NUMPAD_END"
520
- when Wx::K_NUMPAD_BEGIN
521
- key = "NUMPAD_BEGIN"
522
- when Wx::K_NUMPAD_INSERT
523
- key = "NUMPAD_INSERT"
524
- when Wx::K_NUMPAD_DELETE
525
- key = "NUMPAD_DELETE"
526
- when Wx::K_NUMPAD_EQUAL
527
- key = "NUMPAD_EQUAL"
528
- when Wx::K_NUMPAD_MULTIPLY
529
- key = "NUMPAD_MULTIPLY"
530
- when Wx::K_NUMPAD_ADD
531
- key = "NUMPAD_ADD"
532
- when Wx::K_NUMPAD_SEPARATOR
533
- key = "NUMPAD_SEPARATOR"
534
- when Wx::K_NUMPAD_SUBTRACT
535
- key = "NUMPAD_SUBTRACT"
536
- when Wx::K_NUMPAD_DECIMAL
537
- key = "NUMPAD_DECIMAL"
538
- else
539
- if keycode >= 32 && keycode <127
540
- key = sprintf("'%s'", keycode.chr)
541
- elsif keycode > 0 && keycode < 27
542
- key = sprintf("Ctrl-%c", ?A + keycode - 1)
543
- else
544
- key = sprintf("unknown (%d)", keycode)
545
- end
546
- end
547
-
548
- Wx::log_message( "%s event: %s (flags = %s%s%s%s)",
549
- name, key,
550
- event.control_down() ? 'C' : '-',
551
- event.alt_down() ? 'A' : '-',
552
- event.shift_down() ? 'S' : '-',
553
- event.meta_down() ? 'M' : '-')
554
- end
555
-
556
- def on_tree_key_down(event)
557
- log_key_event("Tree key down ", event.key_event )
558
- event.skip
559
- end
560
-
561
- def on_begin_drag(event)
562
- # need to explicitly allow drag
563
- if event.item != root_item()
564
- @dragged_item = event.item
565
- Wx::log_message( "OnBeginDrag: started dragging %s",
566
- item_text(@dragged_item))
567
- event.allow
568
- else
569
- Wx::log_message("OnBeginDrag: selected item can't be dragged.")
570
- end
571
- end
572
-
573
- def on_end_drag(event)
574
- src_item = @dragged_item
575
- dest_item = event.item
576
- @dragged_item = 0
577
-
578
- if dest_item.nonzero? && ! item_has_children(dest_item)
579
- # copy to the parent then
580
- dest_item = item_parent(dest_item)
581
- end
582
-
583
- unless dest_item.nonzero?()
584
- Wx::log_message("OnEndDrag: can't drop here.")
585
- return nil
586
- end
587
-
588
- text = item_text(src_item)
589
- Wx::log_message("OnEndDrag: '%s' copied to '%s'.",
590
- text, item_text(dest_item))
591
-
592
- # just do append here - we could also insert it just before/after the item
593
- # on which it was dropped, but self requires slightly more work... we also
594
- # completely ignore the client data and icon of the old item but could
595
- # copy them as well.
596
- #
597
- # Finally, we only copy one item here but we might copy the entire tree if
598
- # we were dragging a folder.
599
- image = Wx::THE_APP.show_images() ? TreeCtrlIcon_File : -1
600
- append_item(dest_item, text, image)
601
- end
602
-
603
- def on_begin_label_edit(event)
604
- Wx::log_message("OnBeginLabelEdit")
605
-
606
- # for testing, prevent self item's label editing
607
- if is_test_item(event.item)
608
- Wx::message_box("The demo prevents you editing this item.")
609
- event.veto()
610
- end
611
- end
612
-
613
- def on_end_label_edit(event)
614
- Wx::log_message("OnEndLabelEdit")
615
- if event.edit_cancelled?
616
- Wx::log_message("Label edit was cancelled by user")
617
- return
618
- end
619
-
620
- # For a demo, don't allow anything except letters in the labels
621
- if event.label =~ /[^\w.]/
622
- msg = "Only letters, numbers and full stops are allowed in the label."
623
- Wx::message_box(msg)
624
- Wx::log_message("Label edit was cancelled by demo")
625
- event.veto
626
- end
627
- end
628
-
629
- def on_item_collapsing(event)
630
- Wx::log_message("OnItemCollapsing")
631
-
632
- # for testing, prevent the user from collapsing the first child folder
633
- if is_test_item(event.item)
634
- Wx::message_box("You can't collapse self item.")
635
- event.veto
636
- end
637
- end
638
-
639
- # show some info about activated item
640
- def on_item_activated(event)
641
- if item_id = event.item
642
- show_info(item_id)
643
- end
644
- Wx::log_message("OnItemActivated")
645
- end
646
-
647
- def on_item_right_click(event)
648
- show_popup_menu(event.item, event.point)
649
- end
650
-
651
- def on_rmouse_up(event)
652
- pos = event.position
653
- item = hit_test(pos)
654
- show_popup_menu(item, pos)
655
- end
656
-
657
- def show_popup_menu(id, pos)
658
- title = ""
659
- if id.nonzero?
660
- title << "Menu for " << item_text(id)
661
- else
662
- title = "Menu for no particular item"
663
- end
664
-
665
- menu = Wx::Menu.new(title)
666
- menu.append(Wx::ID_ABOUT, "&About...")
667
- menu.append(TreeTest_Dump, "&Dump")
668
- popup_menu(menu, pos)
669
- end
670
-
671
- def on_rmouse_dclick(event)
672
- id = hit_test(event.get_position())
673
- if not id
674
- Wx::log_message("No item under mouse")
675
- else
676
- item = get_item_text(id)
677
- if item
678
- Wx::log_message("Item '%s' under mouse", item)
679
- end
680
- end
681
- end
682
- end
683
-
684
-
685
- class MyFrame < Wx::Frame
686
- def initialize(title, x, y, w, h)
687
- super( nil, :title => title, :pos => [ x, y ], :size => [ w, h ] )
688
- @treectrl = nil
689
- @textctrl = nil
690
- @s_num = 0
691
-
692
- # This reduces flicker effects - even better would be to define
693
- # OnEraseBackground to do nothing. When the tree control's scrollbars are
694
- # show or hidden, the frame is sent a background erase event.
695
- self.background_colour = Wx::WHITE
696
-
697
- # Make a menubar
698
- file_menu = Wx::Menu.new
699
- style_menu = Wx::Menu.new
700
- tree_menu = Wx::Menu.new
701
- item_menu = Wx::Menu.new
702
-
703
- file_menu.append(Wx::ID_ABOUT, "&About...")
704
- file_menu.append_separator()
705
- file_menu.append(Wx::ID_EXIT, "E&xit\tAlt-X")
706
-
707
- style_menu.append(TreeTest_TogButtons,
708
- "Toggle &normal buttons", "", Wx::ITEM_CHECK)
709
- style_menu.append(TreeTest_TogTwist,
710
- "Toggle &twister buttons", "", Wx::ITEM_CHECK)
711
- style_menu.append(TreeTest_ToggleButtons,
712
- "Toggle image &buttons", "", Wx::ITEM_CHECK)
713
- style_menu.append_separator()
714
- style_menu.append(TreeTest_TogLines,
715
- "Toggle &connecting lines", "", Wx::ITEM_CHECK)
716
- style_menu.append(TreeTest_TogRootLines,
717
- "Toggle &lines at root", "", Wx::ITEM_CHECK)
718
- style_menu.append(TreeTest_TogHideRoot,
719
- "Toggle &hidden root", "", Wx::ITEM_CHECK)
720
- style_menu.append(TreeTest_TogBorder,
721
- "Toggle &item border", "", Wx::ITEM_CHECK)
722
- style_menu.append(TreeTest_TogFullHighlight,
723
- "Toggle &full row highlight", "", Wx::ITEM_CHECK)
724
- style_menu.append(TreeTest_TogEdit,
725
- "Toggle &editing allowed", "", Wx::ITEM_CHECK)
726
- style_menu.append(TreeTest_ToggleSel,
727
- "Toggle multiple &selection", "", Wx::ITEM_CHECK)
728
- style_menu.append(TreeTest_ToggleImages,
729
- "Toggle show ima&ges", "", Wx::ITEM_CHECK)
730
- style_menu.append(TreeTest_SetImageSize, "Set image si&ze...")
731
- style_menu.append_separator()
732
- style_menu.append(TreeTest_SetFgColour, "Set &foreground colour...")
733
- style_menu.append(TreeTest_SetBgColour, "Set &background colour...")
734
- style_menu.append_separator()
735
- style_menu.append(TreeTest_ResetStyle, "&Reset to default\tF10")
736
-
737
- tree_menu.append(TreeTest_Recreate, "&Recreate the tree")
738
- tree_menu.append(TreeTest_CollapseAndReset, "C&ollapse and reset")
739
- tree_menu.append_separator()
740
- tree_menu.append(TreeTest_AddItem, "Append a &new item")
741
- tree_menu.append(TreeTest_InsertItem, "&Insert a new item")
742
- tree_menu.append(TreeTest_Delete, "&Delete selected item")
743
- tree_menu.append(TreeTest_DeleteChildren, "Delete &children")
744
- tree_menu.append(TreeTest_DeleteAll, "Delete &all items")
745
- tree_menu.append_separator()
746
- tree_menu.append(TreeTest_Count, "Count children of current item")
747
- tree_menu.append(TreeTest_CountRec, "Recursively count children of current item")
748
- tree_menu.append_separator()
749
- tree_menu.append(TreeTest_Sort, "Sort children")
750
- tree_menu.append(TreeTest_SortRev, "Reverse sort children")
751
- tree_menu.append_separator()
752
- tree_menu.append(TreeTest_EnsureVisible, "Make the last item &visible")
753
- tree_menu.append_separator()
754
- tree_menu.append(TreeTest_IncIndent, "Increase indentation\tAlt-I")
755
- tree_menu.append(TreeTest_DecIndent, "Reduce indentation\tAlt-R")
756
- tree_menu.append_separator()
757
- tree_menu.append(TreeTest_IncSpacing, "Increase spacing\tCtrl-I")
758
- tree_menu.append(TreeTest_DecSpacing, "Reduce spacing\tCtrl-R")
759
-
760
- item_menu.append(TreeTest_Dump, "&Dump item children")
761
- item_menu.append(TreeTest_Rename, "&Rename item...")
762
-
763
- item_menu.append_separator()
764
- item_menu.append(TreeTest_SetBold, "Make item &bold")
765
- item_menu.append(TreeTest_ClearBold, "Make item &not bold")
766
- item_menu.append_separator()
767
- item_menu.append(TreeTest_ToggleIcon, "Toggle the item's &icon")
768
-
769
- item_menu.append_separator()
770
- item_menu.append(TreeTest_DumpSelected, "Dump selected items\tAlt-D")
771
- item_menu.append(TreeTest_Select, "Select current item\tAlt-S")
772
- item_menu.append(TreeTest_Unselect, "Unselect everything\tAlt-U")
773
-
774
- menu_bar = Wx::MenuBar.new
775
- menu_bar.append(file_menu, "&File")
776
- menu_bar.append(style_menu, "&Style")
777
- menu_bar.append(tree_menu, "&Tree")
778
- menu_bar.append(item_menu, "&Item")
779
- self.menu_bar = menu_bar
780
-
781
-
782
- # create the controls
783
- @textctrl = Wx::TextCtrl.new( self,
784
- :text => '',
785
- :style => Wx::TE_MULTILINE|Wx::SUNKEN_BORDER)
786
-
787
- create_tree_with_default_style()
788
-
789
- menu_bar.check(TreeTest_ToggleImages, true)
790
-
791
- # create a status bar with 3 panes
792
- create_status_bar(3)
793
- set_status_text("", 0)
794
-
795
- # set our text control as the log target
796
- logWindow = Wx::LogTextCtrl.new(@textctrl)
797
- Wx::Log::active_target = logWindow
798
-
799
- evt_size :on_size
800
- evt_close :on_close
801
-
802
- evt_menu Wx::ID_EXIT, :on_quit
803
- evt_menu Wx::ID_ABOUT, :on_about
804
-
805
- evt_menu TreeTest_TogButtons, :on_tog_buttons
806
- evt_menu TreeTest_TogTwist, :on_tog_twist
807
- evt_menu TreeTest_TogLines, :on_tog_lines
808
- evt_menu TreeTest_TogEdit, :on_tog_edit
809
- evt_menu TreeTest_TogHideRoot, :on_tog_hide_root
810
- evt_menu TreeTest_TogRootLines, :on_tog_root_lines
811
- evt_menu TreeTest_TogBorder, :on_tog_border
812
- evt_menu TreeTest_TogFullHighlight, :on_tog_full_highlight
813
- evt_menu TreeTest_SetFgColour, :on_set_fg_colour
814
- evt_menu TreeTest_SetBgColour, :on_set_bg_colour
815
- evt_menu TreeTest_ResetStyle, :on_reset_style
816
-
817
- evt_menu TreeTest_Dump, :on_dump
818
- evt_menu TreeTest_DumpSelected, :on_dump_selected
819
- evt_menu TreeTest_Select, :on_select
820
- evt_menu TreeTest_Unselect, :on_unselect
821
- evt_menu TreeTest_ToggleSel, :on_toggle_sel
822
- evt_menu TreeTest_Rename, :on_rename
823
- evt_menu TreeTest_Count, :on_count
824
- evt_menu TreeTest_CountRec, :on_count_rec
825
- evt_menu TreeTest_Sort, :on_sort
826
- evt_menu TreeTest_SortRev, :on_sort_rev
827
- evt_menu TreeTest_SetBold, :on_set_bold
828
- evt_menu TreeTest_ClearBold, :on_clear_bold
829
- evt_menu TreeTest_Delete, :on_delete
830
- evt_menu TreeTest_DeleteChildren, :on_delete_children
831
- evt_menu TreeTest_DeleteAll, :on_delete_all
832
- evt_menu TreeTest_Recreate, :on_recreate
833
- evt_menu TreeTest_ToggleImages, :on_toggle_images
834
- evt_menu TreeTest_ToggleButtons, :on_toggle_buttons
835
- evt_menu TreeTest_SetImageSize, :on_set_image_size
836
- evt_menu TreeTest_CollapseAndReset, :on_collapse_and_reset
837
- evt_menu TreeTest_EnsureVisible, :on_ensure_visible
838
- evt_menu TreeTest_AddItem, :on_add_item
839
- evt_menu TreeTest_InsertItem, :on_insert_item
840
- evt_menu TreeTest_IncIndent, :on_inc_indent
841
- evt_menu TreeTest_DecIndent, :on_dec_indent
842
- evt_menu TreeTest_IncSpacing, :on_inc_spacing
843
- evt_menu TreeTest_DecSpacing, :on_dec_spacing
844
- evt_menu TreeTest_ToggleIcon, :on_toggle_icon
845
- end
846
-
847
- def create_tree_with_default_style()
848
-
849
- style = Wx::TR_DEFAULT_STYLE|Wx::TR_EDIT_LABELS|
850
- Wx::TR_TWIST_BUTTONS|Wx::TR_ROW_LINES|Wx::TR_FULL_ROW_HIGHLIGHT
851
-
852
- create_tree(style|Wx::SUNKEN_BORDER)
853
-
854
- # as we don't know what Wx::TR_DEFAULT_STYLE could contain, test for
855
- # everything
856
- mbar = menu_bar
857
- mbar.check(TreeTest_TogButtons, (style & Wx::TR_HAS_BUTTONS) != 0)
858
- mbar.check(TreeTest_TogButtons, (style & Wx::TR_TWIST_BUTTONS) != 0)
859
- mbar.check(TreeTest_TogLines, (style & Wx::TR_NO_LINES) == 0)
860
- mbar.check(TreeTest_TogRootLines, (style & Wx::TR_LINES_AT_ROOT) != 0)
861
- mbar.check(TreeTest_TogHideRoot, (style & Wx::TR_HIDE_ROOT) != 0)
862
- mbar.check(TreeTest_TogEdit, (style & Wx::TR_EDIT_LABELS) != 0)
863
- mbar.check(TreeTest_TogBorder, (style & Wx::TR_ROW_LINES) != 0)
864
- mbar.check(TreeTest_TogFullHighlight, (style & Wx::TR_FULL_ROW_HIGHLIGHT) != 0)
865
- end
866
-
867
- def create_tree(style)
868
- @treectrl = MyTreeCtrl.new(self, :style => style)
869
- resize
870
- end
871
-
872
- def tog_style(id,flag)
873
-
874
- style = @treectrl.window_style_flag ^ flag
875
-
876
- # most treectrl styles can't be changed on the fly using the native
877
- # control and the tree must be recreated
878
- @treectrl.destroy
879
- create_tree(style)
880
- menu_bar.check(id, (style & flag) != 0)
881
- end
882
-
883
- def on_tog_buttons(event)
884
- tog_style(event.id, Wx::TR_HAS_BUTTONS)
885
- end
886
-
887
- def on_tog_twist(event)
888
- tog_style(event.id, Wx::TR_TWIST_BUTTONS)
889
- end
890
-
891
- def on_tog_lines(event)
892
- tog_style(event.id, Wx::TR_NO_LINES)
893
- end
894
-
895
- def on_tog_edit(event)
896
- tog_style(event.id, Wx::TR_EDIT_LABELS)
897
- end
898
-
899
- def on_tog_hide_root(event)
900
- tog_style(event.id, Wx::TR_HIDE_ROOT)
901
- end
902
-
903
- def on_tog_root_lines(event)
904
- tog_style(event.id, Wx::TR_LINES_AT_ROOT)
905
- end
906
-
907
- def on_tog_border(event)
908
- tog_style(event.id, Wx::TR_ROW_LINES)
909
- end
910
-
911
- def on_tog_full_highlight(event)
912
- tog_style(event.id, Wx::TR_FULL_ROW_HIGHLIGHT)
913
- end
914
-
915
- def on_reset_style(event)
916
- create_tree_with_default_style()
917
- end
918
-
919
- def on_set_bold(event)
920
- do_set_bold(true)
921
- end
922
-
923
- def on_clear_bold(event)
924
- do_set_bold(false)
925
- end
926
-
927
- def on_sort(event)
928
- do_sort
929
- end
930
-
931
- def on_sort_rev(event)
932
- do_sort(true)
933
- end
934
-
935
- def on_size(event)
936
- if @treectrl && @textctrl
937
- resize
938
- end
939
- event.skip
940
- end
941
-
942
- def resize
943
- my_size = self.client_size
944
- @treectrl.size = [ my_size.x, 2 * my_size.y / 3 ]
945
- end
946
-
947
- def on_close(event)
948
- Wx::Log::active_target = nil
949
- destroy
950
- end
951
-
952
- def on_quit(event)
953
- close(true)
954
- end
955
-
956
- def on_about(event)
957
- Wx::message_box( "Tree test sample\n" +
958
- "(c) Julian Smart 1997, Vadim Zeitlin 1998",
959
- "About tree test",
960
- Wx::OK|Wx::ICON_INFORMATION, self)
961
- end
962
-
963
- def check_item(item)
964
- unless item.nonzero?
965
- Wx::message_box("Please select some item first!",
966
- "Tree sample error",
967
- Wx::OK|Wx::ICON_EXCLAMATION, self)
968
- return false
969
- end
970
- true
971
- end
972
-
973
- def on_rename(event)
974
- item = @treectrl.selection
975
- return nil unless check_item( item )
976
- # TODO demonstrate creating a custom edit control...
977
- @treectrl.edit_label(item)
978
- end
979
-
980
- def on_count(event)
981
- item = @treectrl.selection
982
- return nil unless check_item( item )
983
- i = @treectrl.children_count( item, false )
984
- Wx::log_message("%d children", i)
985
- end
986
-
987
- def on_count_rec(event)
988
- item = @treectrl.selection
989
- return nil unless check_item( item )
990
- Wx::log_message("%d children", @treectrl.children_count( item ))
991
- end
992
-
993
- def do_sort(reverse = false)
994
- item = @treectrl.selection
995
- return nil unless check_item( item )
996
- @treectrl.do_sort_children(item, reverse)
997
- end
998
-
999
- def on_dump(event)
1000
- root = @treectrl.selection
1001
- return nil unless check_item( root )
1002
- @treectrl.get_items_recursively(root, -1)
1003
- end
1004
-
1005
- def on_toggle_sel(event)
1006
- tog_style(event.id, Wx::TR_MULTIPLE)
1007
- end
1008
-
1009
- def on_dump_selected(event)
1010
- if ( @treectrl.window_style & Wx::TR_MULTIPLE ) == 0
1011
- item_id = @treectrl.selection
1012
- if item_id.nonzero?
1013
- Wx::log_message("<TreeItem '%s'>",
1014
- @treectrl.get_item_text(item_id))
1015
- else
1016
- Wx::log_message("No tree item selected")
1017
- end
1018
- else
1019
- Wx::log_message("NOT IMPLEMENTED: Multiple selections not available")
1020
- # Requires Wx::List - Ruby Array typemap, pointer handling, 29/08/2006
1021
- # Currently raises incorrect number of args, 1 for 0
1022
- # selected = @treectrl.get_selections()
1023
- # selected_items.each do | item_id |
1024
- # Wx::log_message("\t%s", @treectrl.get_item_text(item_id))
1025
- # end
1026
- end
1027
- end
1028
-
1029
- def on_select(event)
1030
- @treectrl.select_item(@treectrl.selection)
1031
- end
1032
-
1033
- def on_unselect(event)
1034
- @treectrl.unselect_all()
1035
- end
1036
-
1037
- def do_set_bold(bold = true)
1038
- item = @treectrl.selection
1039
- return nil unless check_item( item )
1040
- @treectrl.set_item_bold(item, bold)
1041
- end
1042
-
1043
- def on_delete(event)
1044
- item = @treectrl.selection
1045
- return nil unless check_item( item )
1046
- @treectrl.delete(item)
1047
- end
1048
-
1049
- def on_delete_children(event)
1050
- item = @treectrl.selection
1051
- return nil unless check_item( item )
1052
- @treectrl.delete_children(item)
1053
- end
1054
-
1055
- def on_delete_all(event)
1056
- @treectrl.delete_all_items()
1057
- end
1058
-
1059
- def on_recreate(event)
1060
- on_delete_all(event)
1061
- @treectrl.add_test_items_to_tree(5, 2)
1062
- end
1063
-
1064
- def on_set_image_size(event)
1065
- size = get_number_from_user("Enter the size for the images to use",
1066
- "Size: ",
1067
- "TreeCtrl sample",
1068
- @treectrl.image_size())
1069
- if size == -1
1070
- return nil
1071
- end
1072
-
1073
- @treectrl.create_image_list(size)
1074
- Wx::THE_APP.show_images = true
1075
- end
1076
-
1077
- def on_toggle_images(event)
1078
- if Wx::get_app.show_images
1079
- @treectrl.unset_image_list
1080
- Wx::get_app.show_images = false
1081
- else
1082
- @treectrl.create_image_list
1083
- Wx::get_app.show_images = true
1084
- end
1085
- end
1086
-
1087
- def on_toggle_buttons(event)
1088
- end
1089
-
1090
- def on_collapse_and_reset(event)
1091
- @treectrl.collapse_and_reset(@treectrl.get_root_item())
1092
- end
1093
-
1094
- def on_ensure_visible(event)
1095
- @treectrl.do_ensure_visible()
1096
- end
1097
-
1098
- def on_insert_item(event)
1099
- image = Wx::THE_APP.show_images() ? TreeCtrlIcon_File : -1
1100
- @treectrl.insert_item(@treectrl.root_item, -1, "2nd item", image)
1101
- end
1102
-
1103
- def on_add_item(event)
1104
- @s_num += 1
1105
- text = sprintf("Item #%d", @s_num)
1106
- @treectrl.append_item(@treectrl.root_item, text)
1107
- end
1108
-
1109
- def on_inc_indent(event)
1110
- if @treectrl.indent < 100
1111
- @treectrl.indent += 5
1112
- end
1113
- end
1114
-
1115
- def on_dec_indent(event)
1116
- if @treectrl.indent > 10
1117
- @treectrl.indent -= 5
1118
- end
1119
- end
1120
-
1121
- def on_inc_spacing(event)
1122
- if @treectrl.spacing < 100
1123
- @treectrl.spacing += 5
1124
- end
1125
- end
1126
-
1127
- def on_dec_spacing(event)
1128
- if @treectrl.spacing > 10
1129
- @treectrl.spacing -= 5
1130
- end
1131
- end
1132
-
1133
- def on_toggle_icon(event)
1134
- item = @treectrl.selection
1135
- return nil unless check_item( item )
1136
- @treectrl.do_toggle_icon(item)
1137
- end
1138
-
1139
- # TODO
1140
- def on_set_fg_colour(event)
1141
- Wx::log_message('NOT IMPLEMENTED')
1142
- return
1143
- col = Wx::get_colour_from_user(self, @treectrl.foreground_colour)
1144
- if col.ok?
1145
- @treectrl.foreground_colour = col
1146
- end
1147
- end
1148
-
1149
- # TODO
1150
- def on_set_bg_colour(event)
1151
- Wx::log_message('NOT IMPLEMENTED')
1152
- return
1153
- col = Wx::get_colour_from_user(self, @treectrl.background_colour)
1154
- if col.ok?
1155
- @treectrl.background_colour = col
1156
- end
1157
- end
1158
-
1159
- end
1160
-
1161
- class MyApp < Wx::App
1162
- attr_accessor :show_images, :show_buttons
1163
- def initialize
1164
- self.show_images = true
1165
- self.show_buttons = false
1166
- super
1167
- end
1168
-
1169
- def on_init
1170
- # Create the main frame window
1171
- frame = MyFrame.new("TreeCtrl Test", 50, 50, 450, 600)
1172
- # show the frame
1173
- frame.show(true)
1174
- # set_top_window(frame)
1175
-
1176
- end
1177
- end
1178
-
1179
- app = MyApp.new
1180
- app.main_loop
1
+ #!/usr/bin/env ruby
2
+ # wxRuby2 Sample Code. Copyright (c) 2004-2006 Kevin B. Smith
3
+ # Freely reusable code: see SAMPLES-LICENSE.TXT for details
4
+
5
+ begin
6
+ require 'wx'
7
+ rescue LoadError => no_wx_err
8
+ begin
9
+ require 'rubygems'
10
+ require 'wx'
11
+ rescue LoadError
12
+ raise no_wx_err
13
+ end
14
+ end
15
+
16
+ # This sample shows off the features of the TreeCtrl widget. The actual
17
+ # features vary somewhat across platforms; for example, the button
18
+ # styles and connecting lines between items are not available on OS X.
19
+ # Also, some items remain to be implemented - for example, setting item
20
+ # icons and item data.
21
+
22
+ # Just some event ids for the numerous menu items
23
+ TreeTest_TogButtons,
24
+ TreeTest_TogTwist,
25
+ TreeTest_TogLines,
26
+ TreeTest_TogEdit,
27
+ TreeTest_TogHideRoot,
28
+ TreeTest_TogRootLines,
29
+ TreeTest_TogBorder,
30
+ TreeTest_TogFullHighlight,
31
+ TreeTest_SetFgColour,
32
+ TreeTest_SetBgColour,
33
+ TreeTest_ResetStyle,
34
+ TreeTest_Dump,
35
+ TreeTest_DumpSelected,
36
+ TreeTest_Count,
37
+ TreeTest_CountRec,
38
+ TreeTest_Sort,
39
+ TreeTest_SortRev,
40
+ TreeTest_SetBold,
41
+ TreeTest_ClearBold,
42
+ TreeTest_Rename,
43
+ TreeTest_Delete,
44
+ TreeTest_DeleteChildren,
45
+ TreeTest_DeleteAll,
46
+ TreeTest_Recreate,
47
+ TreeTest_ToggleImages,
48
+ TreeTest_ToggleButtons,
49
+ TreeTest_SetImageSize,
50
+ TreeTest_ToggleSel,
51
+ TreeTest_CollapseAndReset,
52
+ TreeTest_EnsureVisible,
53
+ TreeTest_AddItem,
54
+ TreeTest_InsertItem,
55
+ TreeTest_IncIndent,
56
+ TreeTest_DecIndent,
57
+ TreeTest_IncSpacing,
58
+ TreeTest_DecSpacing,
59
+ TreeTest_ToggleIcon,
60
+ TreeTest_Select,
61
+ TreeTest_Unselect = (Wx::ID_HIGHEST..Wx::ID_HIGHEST+39).to_a
62
+
63
+ TreeCtrlIcon_File,
64
+ TreeCtrlIcon_FileSelected,
65
+ TreeCtrlIcon_Folder,
66
+ TreeCtrlIcon_FolderSelected,
67
+ TreeCtrlIcon_FolderOpened = 0,1,2,3,4
68
+
69
+ class MyTreeCtrl < Wx::TreeCtrl
70
+ def initialize(parent, args)
71
+ super
72
+
73
+ @reverse_sort = false
74
+
75
+ create_image_list
76
+
77
+ # add some items to the tree
78
+ add_test_items_to_tree(5, 2)
79
+
80
+ # TreeCtrl supports a large number of different events...
81
+ evt_tree_begin_drag self, :on_begin_drag
82
+ evt_tree_begin_rdrag self, :on_begin_rdrag
83
+ evt_tree_end_drag self, :on_end_drag
84
+ evt_tree_begin_label_edit self, :on_begin_label_edit
85
+ evt_tree_end_label_edit self, :on_end_label_edit
86
+ evt_tree_delete_item self, :on_delete_item
87
+ evt_tree_set_info self, :on_set_info
88
+ evt_tree_item_expanded self, :on_item_expanded
89
+ evt_tree_item_expanding self, :on_item_expanding
90
+ evt_tree_item_collapsed self, :on_item_collapsed
91
+ evt_tree_item_collapsing self, :on_item_collapsing
92
+ evt_tree_item_right_click self, :on_item_right_click
93
+ evt_tree_sel_changed self, :on_sel_changed
94
+ evt_tree_sel_changing self, :on_sel_changing
95
+ evt_tree_key_down self, :on_tree_key_down
96
+ evt_tree_item_activated self, :on_item_activated
97
+
98
+ evt_right_dclick :on_rmouse_dclick
99
+ evt_right_up :on_rmouse_up
100
+ end
101
+
102
+ def show_info(id)
103
+ Wx::log_message("Item '%s': %sselected, %sexpanded, %sbold,\n" +
104
+ "%d children (%d immediately under selected item).",
105
+ get_item_text( id ),
106
+ bool_to_str( is_selected( id ) ),
107
+ bool_to_str( is_expanded( id ) ),
108
+ bool_to_str( is_bold( id ) ),
109
+ get_children_count( id ),
110
+ get_children_count(id, false) )
111
+ end
112
+
113
+ def bool_to_str(bool)
114
+ bool ? " " : "not "
115
+ end
116
+
117
+ def do_sort_children(item, reverse = false)
118
+ @reverse_sort = reverse
119
+ sort_children(item)
120
+ end
121
+
122
+ def do_ensure_visible()
123
+ ensure_visible(@last_item)
124
+ end
125
+
126
+ def image_size()
127
+ return @image_size
128
+ end
129
+
130
+ def is_test_item(item)
131
+ # the test item is the first child folder
132
+ return item_parent(item) == root_item && ! prev_sibling(item)
133
+ end
134
+
135
+ def create_image_list
136
+ @imageSize = 16
137
+
138
+ # Make an image list containing small icons
139
+ images = Wx::ImageList.new(32, 32, true)
140
+ # should correspond to TreeCtrlIcon_xxx enum
141
+ Wx::BusyCursor.busy do
142
+ icons = (1 .. 5).map do | i |
143
+ icon_file = File.join(File.dirname(__FILE__), "icon#{i}.xpm")
144
+ Wx::Bitmap.new(icon_file, Wx::BITMAP_TYPE_XPM)
145
+ end
146
+ icons.each { | ic | images.add(ic) }
147
+ self.image_list = images
148
+ end
149
+ end
150
+
151
+ def unset_image_list
152
+ self.image_list = nil
153
+ end
154
+
155
+ def create_buttons_image_list()
156
+ # Make an image list containing small icons
157
+ images = Wx::ImageList.new(16, 16, true)
158
+
159
+ # should correspond to TreeCtrlIcon_xxx enum
160
+ Wx::BusyCursor.busy do |x|
161
+ icons = [
162
+ Wx::Icon.new("icon3.xpm"), # closed
163
+ Wx::Icon.new("icon3.xpm"), # closed, selected
164
+ Wx::Icon.new("icon5.xpm"), # open
165
+ Wx::Icon.new("icon5.xpm") ] # open, selected
166
+
167
+ icons.each do | ic |
168
+ orig_size = ic.get_width()
169
+ if size == orig_size
170
+ images.add(ic)
171
+ else
172
+ resized = ic.convert_to_image.rescale(size, size)
173
+ images.add( Wx::Bitmap.new(resized) )
174
+ end
175
+ end
176
+
177
+ self.buttons_image_list = images
178
+ end
179
+ end
180
+
181
+ def on_compare_items(item1,item2)
182
+ if @reverse_sort
183
+ # just exchange 1st and 2nd items
184
+ return super(item2, item1)
185
+ else
186
+ return super(item1, item2)
187
+ end
188
+ end
189
+
190
+ def add_items_recursively(parent_id, num_children, depth, folder)
191
+ if depth > 0
192
+ has_children = depth > 1
193
+
194
+ for n in 0 ... num_children
195
+ # at depth 1 elements won't have any more children
196
+ if has_children
197
+ str = sprintf("%s child %d", "Folder", n + 1)
198
+ else
199
+ str = sprintf("%s child %d.%d", "File", folder, n + 1)
200
+ end
201
+ # here we pass to append_item() normal and selected item images (we
202
+ # suppose that selected image follows the normal one in the enum)
203
+ if Wx::THE_APP.show_images
204
+ image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder
205
+ imageSel = image + 1
206
+ else
207
+ image = imageSel = -1
208
+ end
209
+ id = append_item(parent_id, str, image, imageSel)
210
+
211
+ # and now we also set the expanded one (only for the folders)
212
+ if has_children && Wx::get_app.show_images()
213
+ set_item_image( id, TreeCtrlIcon_FolderOpened,
214
+ Wx::TREE_ITEM_ICON_EXPANDED)
215
+ end
216
+
217
+ # remember the last child for OnEnsureVisible()
218
+ if ! has_children && n == num_children - 1
219
+ @last_item = id
220
+ end
221
+
222
+ add_items_recursively(id, num_children, depth - 1, n + 1)
223
+ end
224
+ end
225
+ end
226
+
227
+ def add_test_items_to_tree(num_children,depth)
228
+ image = Wx::THE_APP.show_images ? TreeCtrlIcon_Folder : -1
229
+ root_id = add_root("Root",image, image)
230
+ if image != -1
231
+ set_item_image( root_id, TreeCtrlIcon_FolderOpened,
232
+ Wx::TREE_ITEM_ICON_EXPANDED)
233
+ end
234
+
235
+ add_items_recursively(root_id, num_children, depth, 0)
236
+
237
+ # set some colours/fonts for testing
238
+ # note that font sizes can also be varied, but only on platforms
239
+ # that use the generic TreeCtrl - OS X and GTK, and only if
240
+ # Wx::TR_HAS_VARIABLE_ROW_HEIGHT style was used in the constructor
241
+ font = get_font
242
+ font.style = Wx::FONTSTYLE_ITALIC
243
+ set_item_font(root_id, font)
244
+
245
+ ids = get_children(root_id)
246
+
247
+ # make the first item blue
248
+ set_item_text_colour(ids[0], Wx::BLUE)
249
+
250
+ # make the third item red on grey
251
+ if Wx::PLATFORM == "WXMSW"
252
+ set_item_text_colour(ids[2], Wx::RED)
253
+ set_item_background_colour(ids[2], Wx::LIGHT_GREY)
254
+ end
255
+ end
256
+
257
+ def get_items_recursively(parent_id, cookie)
258
+ if cookie == -1
259
+ id, cookie = get_first_child(parent_id)
260
+ else
261
+ id, cookie = get_next_child(parent_id, cookie)
262
+ end
263
+ if not id.nonzero?
264
+ return nil
265
+ end
266
+
267
+ text = item_text(id)
268
+ Wx::log_message(text)
269
+
270
+ if item_has_children(id)
271
+ get_items_recursively(id,-1)
272
+ end
273
+ get_items_recursively(parent_id, cookie)
274
+ end
275
+
276
+ def do_toggle_icon(item)
277
+ old_img = get_item_image(item)
278
+ if old_img == TreeCtrlIcon_Folder
279
+ new_img = TreeCtrlIcon_File
280
+ else
281
+ new_img = TreeCtrlIcon_Folder
282
+ end
283
+
284
+ set_item_image(item, new_img)
285
+ end
286
+
287
+ def on_begin_rdrag(event)
288
+ Wx::log_message("OnBeginRDrag")
289
+ event.skip
290
+ end
291
+
292
+ def on_delete_item(event)
293
+ Wx::log_message("OnDeleteItem")
294
+ event.skip
295
+ end
296
+
297
+ def on_get_info(event)
298
+ Wx::log_message("OnGetInfo")
299
+ event.skip
300
+ end
301
+
302
+ def on_set_info(event)
303
+ Wx::log_message("OnSetInfo")
304
+ event.skip
305
+ end
306
+
307
+ def on_item_expanded(event)
308
+ Wx::log_message("OnItemExpanded")
309
+ event.skip
310
+ end
311
+
312
+ def on_item_expanding(event)
313
+ Wx::log_message("OnItemExpanding")
314
+ event.skip
315
+ end
316
+
317
+ def on_item_collapsed(event)
318
+ Wx::log_message("OnItemCollapsed")
319
+ event.skip
320
+ end
321
+
322
+ def on_sel_changed(event)
323
+ Wx::log_message("OnSelChanged")
324
+ event.skip
325
+ end
326
+
327
+ def on_sel_changing(event)
328
+ Wx::log_message("OnSelChanging")
329
+ event.skip
330
+ end
331
+
332
+ def log_key_event(name,event)
333
+ keycode = event.key_code
334
+
335
+ case keycode
336
+ when Wx::K_BACK
337
+ key = "BACK"
338
+ when Wx::K_TAB
339
+ key = "TAB"
340
+ when Wx::K_RETURN
341
+ key = "RETURN"
342
+ when Wx::K_ESCAPE
343
+ key = "ESCAPE"
344
+ when Wx::K_SPACE
345
+ key = "SPACE"
346
+ when Wx::K_DELETE
347
+ key = "DELETE"
348
+ when Wx::K_START
349
+ key = "START"
350
+ when Wx::K_LBUTTON
351
+ key = "LBUTTON"
352
+ when Wx::K_RBUTTON
353
+ key = "RBUTTON"
354
+ when Wx::K_CANCEL
355
+ key = "CANCEL"
356
+ when Wx::K_MBUTTON
357
+ key = "MBUTTON"
358
+ when Wx::K_CLEAR
359
+ key = "CLEAR"
360
+ when Wx::K_SHIFT
361
+ key = "SHIFT"
362
+ when Wx::K_ALT
363
+ key = "ALT"
364
+ when Wx::K_CONTROL
365
+ key = "CONTROL"
366
+ when Wx::K_MENU
367
+ key = "MENU"
368
+ when Wx::K_PAUSE
369
+ key = "PAUSE"
370
+ when Wx::K_CAPITAL
371
+ key = "CAPITAL"
372
+ when Wx::K_PRIOR
373
+ key = "PRIOR"
374
+ when Wx::K_NEXT
375
+ key = "NEXT"
376
+ when Wx::K_END
377
+ key = "END"
378
+ when Wx::K_HOME
379
+ key = "HOME"
380
+ when Wx::K_LEFT
381
+ key = "LEFT"
382
+ when Wx::K_UP
383
+ key = "UP"
384
+ when Wx::K_RIGHT
385
+ key = "RIGHT"
386
+ when Wx::K_DOWN
387
+ key = "DOWN"
388
+ when Wx::K_SELECT
389
+ key = "SELECT"
390
+ when Wx::K_PRINT
391
+ key = "PRINT"
392
+ when Wx::K_EXECUTE
393
+ key = "EXECUTE"
394
+ when Wx::K_SNAPSHOT
395
+ key = "SNAPSHOT"
396
+ when Wx::K_INSERT
397
+ key = "INSERT"
398
+ when Wx::K_HELP
399
+ key = "HELP"
400
+ when Wx::K_NUMPAD0
401
+ key = "NUMPAD0"
402
+ when Wx::K_NUMPAD1
403
+ key = "NUMPAD1"
404
+ when Wx::K_NUMPAD2
405
+ key = "NUMPAD2"
406
+ when Wx::K_NUMPAD3
407
+ key = "NUMPAD3"
408
+ when Wx::K_NUMPAD4
409
+ key = "NUMPAD4"
410
+ when Wx::K_NUMPAD5
411
+ key = "NUMPAD5"
412
+ when Wx::K_NUMPAD6
413
+ key = "NUMPAD6"
414
+ when Wx::K_NUMPAD7
415
+ key = "NUMPAD7"
416
+ when Wx::K_NUMPAD8
417
+ key = "NUMPAD8"
418
+ when Wx::K_NUMPAD9
419
+ key = "NUMPAD9"
420
+ when Wx::K_MULTIPLY
421
+ key = "MULTIPLY"
422
+ when Wx::K_ADD
423
+ key = "ADD"
424
+ when Wx::K_SEPARATOR
425
+ key = "SEPARATOR"
426
+ when Wx::K_SUBTRACT
427
+ key = "SUBTRACT"
428
+ when Wx::K_DECIMAL
429
+ key = "DECIMAL"
430
+ when Wx::K_DIVIDE
431
+ key = "DIVIDE"
432
+ when Wx::K_F1
433
+ key = "F1"
434
+ when Wx::K_F2
435
+ key = "F2"
436
+ when Wx::K_F3
437
+ key = "F3"
438
+ when Wx::K_F4
439
+ key = "F4"
440
+ when Wx::K_F5
441
+ key = "F5"
442
+ when Wx::K_F6
443
+ key = "F6"
444
+ when Wx::K_F7
445
+ key = "F7"
446
+ when Wx::K_F8
447
+ key = "F8"
448
+ when Wx::K_F9
449
+ key = "F9"
450
+ when Wx::K_F10
451
+ key = "F10"
452
+ when Wx::K_F11
453
+ key = "F11"
454
+ when Wx::K_F12
455
+ key = "F12"
456
+ when Wx::K_F13
457
+ key = "F13"
458
+ when Wx::K_F14
459
+ key = "F14"
460
+ when Wx::K_F15
461
+ key = "F15"
462
+ when Wx::K_F16
463
+ key = "F16"
464
+ when Wx::K_F17
465
+ key = "F17"
466
+ when Wx::K_F18
467
+ key = "F18"
468
+ when Wx::K_F19
469
+ key = "F19"
470
+ when Wx::K_F20
471
+ key = "F20"
472
+ when Wx::K_F21
473
+ key = "F21"
474
+ when Wx::K_F22
475
+ key = "F22"
476
+ when Wx::K_F23
477
+ key = "F23"
478
+ when Wx::K_F24
479
+ key = "F24"
480
+ when Wx::K_NUMLOCK
481
+ key = "NUMLOCK"
482
+ when Wx::K_SCROLL
483
+ key = "SCROLL"
484
+ when Wx::K_PAGEUP
485
+ key = "PAGEUP"
486
+ when Wx::K_PAGEDOWN
487
+ key = "PAGEDOWN"
488
+ when Wx::K_NUMPAD_SPACE
489
+ key = "NUMPAD_SPACE"
490
+ when Wx::K_NUMPAD_TAB
491
+ key = "NUMPAD_TAB"
492
+ when Wx::K_NUMPAD_ENTER
493
+ key = "NUMPAD_ENTER"
494
+ when Wx::K_NUMPAD_F1
495
+ key = "NUMPAD_F1"
496
+ when Wx::K_NUMPAD_F2
497
+ key = "NUMPAD_F2"
498
+ when Wx::K_NUMPAD_F3
499
+ key = "NUMPAD_F3"
500
+ when Wx::K_NUMPAD_F4
501
+ key = "NUMPAD_F4"
502
+ when Wx::K_NUMPAD_HOME
503
+ key = "NUMPAD_HOME"
504
+ when Wx::K_NUMPAD_LEFT
505
+ key = "NUMPAD_LEFT"
506
+ when Wx::K_NUMPAD_UP
507
+ key = "NUMPAD_UP"
508
+ when Wx::K_NUMPAD_RIGHT
509
+ key = "NUMPAD_RIGHT"
510
+ when Wx::K_NUMPAD_DOWN
511
+ key = "NUMPAD_DOWN"
512
+ when Wx::K_NUMPAD_PRIOR
513
+ key = "NUMPAD_PRIOR"
514
+ when Wx::K_NUMPAD_PAGEUP
515
+ key = "NUMPAD_PAGEUP"
516
+ when Wx::K_NUMPAD_PAGEDOWN
517
+ key = "NUMPAD_PAGEDOWN"
518
+ when Wx::K_NUMPAD_END
519
+ key = "NUMPAD_END"
520
+ when Wx::K_NUMPAD_BEGIN
521
+ key = "NUMPAD_BEGIN"
522
+ when Wx::K_NUMPAD_INSERT
523
+ key = "NUMPAD_INSERT"
524
+ when Wx::K_NUMPAD_DELETE
525
+ key = "NUMPAD_DELETE"
526
+ when Wx::K_NUMPAD_EQUAL
527
+ key = "NUMPAD_EQUAL"
528
+ when Wx::K_NUMPAD_MULTIPLY
529
+ key = "NUMPAD_MULTIPLY"
530
+ when Wx::K_NUMPAD_ADD
531
+ key = "NUMPAD_ADD"
532
+ when Wx::K_NUMPAD_SEPARATOR
533
+ key = "NUMPAD_SEPARATOR"
534
+ when Wx::K_NUMPAD_SUBTRACT
535
+ key = "NUMPAD_SUBTRACT"
536
+ when Wx::K_NUMPAD_DECIMAL
537
+ key = "NUMPAD_DECIMAL"
538
+ else
539
+ if keycode >= 32 && keycode <127
540
+ key = sprintf("'%s'", keycode.chr)
541
+ elsif keycode > 0 && keycode < 27
542
+ key = sprintf("Ctrl-%c", ?A + keycode - 1)
543
+ else
544
+ key = sprintf("unknown (%d)", keycode)
545
+ end
546
+ end
547
+
548
+ Wx::log_message( "%s event: %s (flags = %s%s%s%s)",
549
+ name, key,
550
+ event.control_down() ? 'C' : '-',
551
+ event.alt_down() ? 'A' : '-',
552
+ event.shift_down() ? 'S' : '-',
553
+ event.meta_down() ? 'M' : '-')
554
+ end
555
+
556
+ def on_tree_key_down(event)
557
+ log_key_event("Tree key down ", event.key_event )
558
+ event.skip
559
+ end
560
+
561
+ def on_begin_drag(event)
562
+ # need to explicitly allow drag
563
+ if event.item != root_item()
564
+ @dragged_item = event.item
565
+ Wx::log_message( "OnBeginDrag: started dragging %s",
566
+ item_text(@dragged_item))
567
+ event.allow
568
+ else
569
+ Wx::log_message("OnBeginDrag: selected item can't be dragged.")
570
+ end
571
+ end
572
+
573
+ def on_end_drag(event)
574
+ src_item = @dragged_item
575
+ dest_item = event.item
576
+ @dragged_item = 0
577
+
578
+ if dest_item.nonzero? && ! item_has_children(dest_item)
579
+ # copy to the parent then
580
+ dest_item = item_parent(dest_item)
581
+ end
582
+
583
+ unless dest_item.nonzero?()
584
+ Wx::log_message("OnEndDrag: can't drop here.")
585
+ return nil
586
+ end
587
+
588
+ text = item_text(src_item)
589
+ Wx::log_message("OnEndDrag: '%s' copied to '%s'.",
590
+ text, item_text(dest_item))
591
+
592
+ # just do append here - we could also insert it just before/after the item
593
+ # on which it was dropped, but self requires slightly more work... we also
594
+ # completely ignore the client data and icon of the old item but could
595
+ # copy them as well.
596
+ #
597
+ # Finally, we only copy one item here but we might copy the entire tree if
598
+ # we were dragging a folder.
599
+ image = Wx::THE_APP.show_images() ? TreeCtrlIcon_File : -1
600
+ append_item(dest_item, text, image)
601
+ end
602
+
603
+ def on_begin_label_edit(event)
604
+ Wx::log_message("OnBeginLabelEdit")
605
+
606
+ # for testing, prevent self item's label editing
607
+ if is_test_item(event.item)
608
+ Wx::message_box("The demo prevents you editing this item.")
609
+ event.veto()
610
+ end
611
+ end
612
+
613
+ def on_end_label_edit(event)
614
+ Wx::log_message("OnEndLabelEdit")
615
+ if event.edit_cancelled?
616
+ Wx::log_message("Label edit was cancelled by user")
617
+ return
618
+ end
619
+
620
+ # For a demo, don't allow anything except letters in the labels
621
+ if event.label =~ /[^\w.]/
622
+ msg = "Only letters, numbers and full stops are allowed in the label."
623
+ Wx::message_box(msg)
624
+ Wx::log_message("Label edit was cancelled by demo")
625
+ event.veto
626
+ end
627
+ end
628
+
629
+ def on_item_collapsing(event)
630
+ Wx::log_message("OnItemCollapsing")
631
+
632
+ # for testing, prevent the user from collapsing the first child folder
633
+ if is_test_item(event.item)
634
+ Wx::message_box("You can't collapse self item.")
635
+ event.veto
636
+ end
637
+ end
638
+
639
+ # show some info about activated item
640
+ def on_item_activated(event)
641
+ if item_id = event.item
642
+ show_info(item_id)
643
+ end
644
+ Wx::log_message("OnItemActivated")
645
+ end
646
+
647
+ def on_item_right_click(event)
648
+ show_popup_menu(event.item, event.point)
649
+ end
650
+
651
+ def on_rmouse_up(event)
652
+ pos = event.position
653
+ item = hit_test(pos)
654
+ show_popup_menu(item, pos)
655
+ end
656
+
657
+ def show_popup_menu(id, pos)
658
+ title = ""
659
+ if id.nonzero?
660
+ title << "Menu for " << item_text(id)
661
+ else
662
+ title = "Menu for no particular item"
663
+ end
664
+
665
+ menu = Wx::Menu.new(title)
666
+ menu.append(Wx::ID_ABOUT, "&About...")
667
+ menu.append(TreeTest_Dump, "&Dump")
668
+ popup_menu(menu, pos)
669
+ end
670
+
671
+ def on_rmouse_dclick(event)
672
+ id = hit_test(event.get_position())
673
+ if not id
674
+ Wx::log_message("No item under mouse")
675
+ else
676
+ item = get_item_text(id)
677
+ if item
678
+ Wx::log_message("Item '%s' under mouse", item)
679
+ end
680
+ end
681
+ end
682
+ end
683
+
684
+
685
+ class MyFrame < Wx::Frame
686
+ def initialize(title, x, y, w, h)
687
+ super( nil, :title => title, :pos => [ x, y ], :size => [ w, h ] )
688
+ @treectrl = nil
689
+ @textctrl = nil
690
+ @s_num = 0
691
+
692
+ # This reduces flicker effects - even better would be to define
693
+ # OnEraseBackground to do nothing. When the tree control's scrollbars are
694
+ # show or hidden, the frame is sent a background erase event.
695
+ self.background_colour = Wx::WHITE
696
+
697
+ # Make a menubar
698
+ file_menu = Wx::Menu.new
699
+ style_menu = Wx::Menu.new
700
+ tree_menu = Wx::Menu.new
701
+ item_menu = Wx::Menu.new
702
+
703
+ file_menu.append(Wx::ID_ABOUT, "&About...")
704
+ file_menu.append_separator()
705
+ file_menu.append(Wx::ID_EXIT, "E&xit\tAlt-X")
706
+
707
+ style_menu.append(TreeTest_TogButtons,
708
+ "Toggle &normal buttons", "", Wx::ITEM_CHECK)
709
+ style_menu.append(TreeTest_TogTwist,
710
+ "Toggle &twister buttons", "", Wx::ITEM_CHECK)
711
+ style_menu.append(TreeTest_ToggleButtons,
712
+ "Toggle image &buttons", "", Wx::ITEM_CHECK)
713
+ style_menu.append_separator()
714
+ style_menu.append(TreeTest_TogLines,
715
+ "Toggle &connecting lines", "", Wx::ITEM_CHECK)
716
+ style_menu.append(TreeTest_TogRootLines,
717
+ "Toggle &lines at root", "", Wx::ITEM_CHECK)
718
+ style_menu.append(TreeTest_TogHideRoot,
719
+ "Toggle &hidden root", "", Wx::ITEM_CHECK)
720
+ style_menu.append(TreeTest_TogBorder,
721
+ "Toggle &item border", "", Wx::ITEM_CHECK)
722
+ style_menu.append(TreeTest_TogFullHighlight,
723
+ "Toggle &full row highlight", "", Wx::ITEM_CHECK)
724
+ style_menu.append(TreeTest_TogEdit,
725
+ "Toggle &editing allowed", "", Wx::ITEM_CHECK)
726
+ style_menu.append(TreeTest_ToggleSel,
727
+ "Toggle multiple &selection", "", Wx::ITEM_CHECK)
728
+ style_menu.append(TreeTest_ToggleImages,
729
+ "Toggle show ima&ges", "", Wx::ITEM_CHECK)
730
+ style_menu.append(TreeTest_SetImageSize, "Set image si&ze...")
731
+ style_menu.append_separator()
732
+ style_menu.append(TreeTest_SetFgColour, "Set &foreground colour...")
733
+ style_menu.append(TreeTest_SetBgColour, "Set &background colour...")
734
+ style_menu.append_separator()
735
+ style_menu.append(TreeTest_ResetStyle, "&Reset to default\tF10")
736
+
737
+ tree_menu.append(TreeTest_Recreate, "&Recreate the tree")
738
+ tree_menu.append(TreeTest_CollapseAndReset, "C&ollapse and reset")
739
+ tree_menu.append_separator()
740
+ tree_menu.append(TreeTest_AddItem, "Append a &new item")
741
+ tree_menu.append(TreeTest_InsertItem, "&Insert a new item")
742
+ tree_menu.append(TreeTest_Delete, "&Delete selected item")
743
+ tree_menu.append(TreeTest_DeleteChildren, "Delete &children")
744
+ tree_menu.append(TreeTest_DeleteAll, "Delete &all items")
745
+ tree_menu.append_separator()
746
+ tree_menu.append(TreeTest_Count, "Count children of current item")
747
+ tree_menu.append(TreeTest_CountRec, "Recursively count children of current item")
748
+ tree_menu.append_separator()
749
+ tree_menu.append(TreeTest_Sort, "Sort children")
750
+ tree_menu.append(TreeTest_SortRev, "Reverse sort children")
751
+ tree_menu.append_separator()
752
+ tree_menu.append(TreeTest_EnsureVisible, "Make the last item &visible")
753
+ tree_menu.append_separator()
754
+ tree_menu.append(TreeTest_IncIndent, "Increase indentation\tAlt-I")
755
+ tree_menu.append(TreeTest_DecIndent, "Reduce indentation\tAlt-R")
756
+ tree_menu.append_separator()
757
+ tree_menu.append(TreeTest_IncSpacing, "Increase spacing\tCtrl-I")
758
+ tree_menu.append(TreeTest_DecSpacing, "Reduce spacing\tCtrl-R")
759
+
760
+ item_menu.append(TreeTest_Dump, "&Dump item children")
761
+ item_menu.append(TreeTest_Rename, "&Rename item...")
762
+
763
+ item_menu.append_separator()
764
+ item_menu.append(TreeTest_SetBold, "Make item &bold")
765
+ item_menu.append(TreeTest_ClearBold, "Make item &not bold")
766
+ item_menu.append_separator()
767
+ item_menu.append(TreeTest_ToggleIcon, "Toggle the item's &icon")
768
+
769
+ item_menu.append_separator()
770
+ item_menu.append(TreeTest_DumpSelected, "Dump selected items\tAlt-D")
771
+ item_menu.append(TreeTest_Select, "Select current item\tAlt-S")
772
+ item_menu.append(TreeTest_Unselect, "Unselect everything\tAlt-U")
773
+
774
+ menu_bar = Wx::MenuBar.new
775
+ menu_bar.append(file_menu, "&File")
776
+ menu_bar.append(style_menu, "&Style")
777
+ menu_bar.append(tree_menu, "&Tree")
778
+ menu_bar.append(item_menu, "&Item")
779
+ self.menu_bar = menu_bar
780
+
781
+
782
+ # create the controls
783
+ @textctrl = Wx::TextCtrl.new( self,
784
+ :text => '',
785
+ :style => Wx::TE_MULTILINE|Wx::SUNKEN_BORDER)
786
+
787
+ create_tree_with_default_style()
788
+
789
+ menu_bar.check(TreeTest_ToggleImages, true)
790
+
791
+ # create a status bar with 3 panes
792
+ create_status_bar(3)
793
+ set_status_text("", 0)
794
+
795
+ # set our text control as the log target
796
+ logWindow = Wx::LogTextCtrl.new(@textctrl)
797
+ Wx::Log::active_target = logWindow
798
+
799
+ evt_size :on_size
800
+ evt_close :on_close
801
+
802
+ evt_menu Wx::ID_EXIT, :on_quit
803
+ evt_menu Wx::ID_ABOUT, :on_about
804
+
805
+ evt_menu TreeTest_TogButtons, :on_tog_buttons
806
+ evt_menu TreeTest_TogTwist, :on_tog_twist
807
+ evt_menu TreeTest_TogLines, :on_tog_lines
808
+ evt_menu TreeTest_TogEdit, :on_tog_edit
809
+ evt_menu TreeTest_TogHideRoot, :on_tog_hide_root
810
+ evt_menu TreeTest_TogRootLines, :on_tog_root_lines
811
+ evt_menu TreeTest_TogBorder, :on_tog_border
812
+ evt_menu TreeTest_TogFullHighlight, :on_tog_full_highlight
813
+ evt_menu TreeTest_SetFgColour, :on_set_fg_colour
814
+ evt_menu TreeTest_SetBgColour, :on_set_bg_colour
815
+ evt_menu TreeTest_ResetStyle, :on_reset_style
816
+
817
+ evt_menu TreeTest_Dump, :on_dump
818
+ evt_menu TreeTest_DumpSelected, :on_dump_selected
819
+ evt_menu TreeTest_Select, :on_select
820
+ evt_menu TreeTest_Unselect, :on_unselect
821
+ evt_menu TreeTest_ToggleSel, :on_toggle_sel
822
+ evt_menu TreeTest_Rename, :on_rename
823
+ evt_menu TreeTest_Count, :on_count
824
+ evt_menu TreeTest_CountRec, :on_count_rec
825
+ evt_menu TreeTest_Sort, :on_sort
826
+ evt_menu TreeTest_SortRev, :on_sort_rev
827
+ evt_menu TreeTest_SetBold, :on_set_bold
828
+ evt_menu TreeTest_ClearBold, :on_clear_bold
829
+ evt_menu TreeTest_Delete, :on_delete
830
+ evt_menu TreeTest_DeleteChildren, :on_delete_children
831
+ evt_menu TreeTest_DeleteAll, :on_delete_all
832
+ evt_menu TreeTest_Recreate, :on_recreate
833
+ evt_menu TreeTest_ToggleImages, :on_toggle_images
834
+ evt_menu TreeTest_ToggleButtons, :on_toggle_buttons
835
+ evt_menu TreeTest_SetImageSize, :on_set_image_size
836
+ evt_menu TreeTest_CollapseAndReset, :on_collapse_and_reset
837
+ evt_menu TreeTest_EnsureVisible, :on_ensure_visible
838
+ evt_menu TreeTest_AddItem, :on_add_item
839
+ evt_menu TreeTest_InsertItem, :on_insert_item
840
+ evt_menu TreeTest_IncIndent, :on_inc_indent
841
+ evt_menu TreeTest_DecIndent, :on_dec_indent
842
+ evt_menu TreeTest_IncSpacing, :on_inc_spacing
843
+ evt_menu TreeTest_DecSpacing, :on_dec_spacing
844
+ evt_menu TreeTest_ToggleIcon, :on_toggle_icon
845
+ end
846
+
847
+ def create_tree_with_default_style()
848
+
849
+ style = Wx::TR_DEFAULT_STYLE|Wx::TR_EDIT_LABELS|
850
+ Wx::TR_TWIST_BUTTONS|Wx::TR_ROW_LINES|Wx::TR_FULL_ROW_HIGHLIGHT
851
+
852
+ create_tree(style|Wx::SUNKEN_BORDER)
853
+
854
+ # as we don't know what Wx::TR_DEFAULT_STYLE could contain, test for
855
+ # everything
856
+ mbar = menu_bar
857
+ mbar.check(TreeTest_TogButtons, (style & Wx::TR_HAS_BUTTONS) != 0)
858
+ mbar.check(TreeTest_TogButtons, (style & Wx::TR_TWIST_BUTTONS) != 0)
859
+ mbar.check(TreeTest_TogLines, (style & Wx::TR_NO_LINES) == 0)
860
+ mbar.check(TreeTest_TogRootLines, (style & Wx::TR_LINES_AT_ROOT) != 0)
861
+ mbar.check(TreeTest_TogHideRoot, (style & Wx::TR_HIDE_ROOT) != 0)
862
+ mbar.check(TreeTest_TogEdit, (style & Wx::TR_EDIT_LABELS) != 0)
863
+ mbar.check(TreeTest_TogBorder, (style & Wx::TR_ROW_LINES) != 0)
864
+ mbar.check(TreeTest_TogFullHighlight, (style & Wx::TR_FULL_ROW_HIGHLIGHT) != 0)
865
+ end
866
+
867
+ def create_tree(style)
868
+ @treectrl = MyTreeCtrl.new(self, :style => style)
869
+ resize
870
+ end
871
+
872
+ def tog_style(id,flag)
873
+
874
+ style = @treectrl.window_style_flag ^ flag
875
+
876
+ # most treectrl styles can't be changed on the fly using the native
877
+ # control and the tree must be recreated
878
+ @treectrl.destroy
879
+ create_tree(style)
880
+ menu_bar.check(id, (style & flag) != 0)
881
+ end
882
+
883
+ def on_tog_buttons(event)
884
+ tog_style(event.id, Wx::TR_HAS_BUTTONS)
885
+ end
886
+
887
+ def on_tog_twist(event)
888
+ tog_style(event.id, Wx::TR_TWIST_BUTTONS)
889
+ end
890
+
891
+ def on_tog_lines(event)
892
+ tog_style(event.id, Wx::TR_NO_LINES)
893
+ end
894
+
895
+ def on_tog_edit(event)
896
+ tog_style(event.id, Wx::TR_EDIT_LABELS)
897
+ end
898
+
899
+ def on_tog_hide_root(event)
900
+ tog_style(event.id, Wx::TR_HIDE_ROOT)
901
+ end
902
+
903
+ def on_tog_root_lines(event)
904
+ tog_style(event.id, Wx::TR_LINES_AT_ROOT)
905
+ end
906
+
907
+ def on_tog_border(event)
908
+ tog_style(event.id, Wx::TR_ROW_LINES)
909
+ end
910
+
911
+ def on_tog_full_highlight(event)
912
+ tog_style(event.id, Wx::TR_FULL_ROW_HIGHLIGHT)
913
+ end
914
+
915
+ def on_reset_style(event)
916
+ create_tree_with_default_style()
917
+ end
918
+
919
+ def on_set_bold(event)
920
+ do_set_bold(true)
921
+ end
922
+
923
+ def on_clear_bold(event)
924
+ do_set_bold(false)
925
+ end
926
+
927
+ def on_sort(event)
928
+ do_sort
929
+ end
930
+
931
+ def on_sort_rev(event)
932
+ do_sort(true)
933
+ end
934
+
935
+ def on_size(event)
936
+ if @treectrl && @textctrl
937
+ resize
938
+ end
939
+ event.skip
940
+ end
941
+
942
+ def resize
943
+ my_size = self.client_size
944
+ @treectrl.size = [ my_size.x, 2 * my_size.y / 3 ]
945
+ end
946
+
947
+ def on_close(event)
948
+ Wx::Log::active_target = nil
949
+ destroy
950
+ end
951
+
952
+ def on_quit(event)
953
+ close(true)
954
+ end
955
+
956
+ def on_about(event)
957
+ Wx::message_box( "Tree test sample\n" +
958
+ "(c) Julian Smart 1997, Vadim Zeitlin 1998",
959
+ "About tree test",
960
+ Wx::OK|Wx::ICON_INFORMATION, self)
961
+ end
962
+
963
+ def check_item(item)
964
+ unless item.nonzero?
965
+ Wx::message_box("Please select some item first!",
966
+ "Tree sample error",
967
+ Wx::OK|Wx::ICON_EXCLAMATION, self)
968
+ return false
969
+ end
970
+ true
971
+ end
972
+
973
+ def on_rename(event)
974
+ item = @treectrl.selection
975
+ return nil unless check_item( item )
976
+ # TODO demonstrate creating a custom edit control...
977
+ @treectrl.edit_label(item)
978
+ end
979
+
980
+ def on_count(event)
981
+ item = @treectrl.selection
982
+ return nil unless check_item( item )
983
+ i = @treectrl.children_count( item, false )
984
+ Wx::log_message("%d children", i)
985
+ end
986
+
987
+ def on_count_rec(event)
988
+ item = @treectrl.selection
989
+ return nil unless check_item( item )
990
+ Wx::log_message("%d children", @treectrl.children_count( item ))
991
+ end
992
+
993
+ def do_sort(reverse = false)
994
+ item = @treectrl.selection
995
+ return nil unless check_item( item )
996
+ @treectrl.do_sort_children(item, reverse)
997
+ end
998
+
999
+ def on_dump(event)
1000
+ root = @treectrl.selection
1001
+ return nil unless check_item( root )
1002
+ @treectrl.get_items_recursively(root, -1)
1003
+ end
1004
+
1005
+ def on_toggle_sel(event)
1006
+ tog_style(event.id, Wx::TR_MULTIPLE)
1007
+ end
1008
+
1009
+ def on_dump_selected(event)
1010
+ if ( @treectrl.window_style & Wx::TR_MULTIPLE ) == 0
1011
+ item_id = @treectrl.selection
1012
+ if item_id.nonzero?
1013
+ Wx::log_message("<TreeItem '%s'>",
1014
+ @treectrl.get_item_text(item_id))
1015
+ else
1016
+ Wx::log_message("No tree item selected")
1017
+ end
1018
+ else
1019
+ Wx::log_message("NOT IMPLEMENTED: Multiple selections not available")
1020
+ # Requires Wx::List - Ruby Array typemap, pointer handling, 29/08/2006
1021
+ # Currently raises incorrect number of args, 1 for 0
1022
+ # selected = @treectrl.get_selections()
1023
+ # selected_items.each do | item_id |
1024
+ # Wx::log_message("\t%s", @treectrl.get_item_text(item_id))
1025
+ # end
1026
+ end
1027
+ end
1028
+
1029
+ def on_select(event)
1030
+ @treectrl.select_item(@treectrl.selection)
1031
+ end
1032
+
1033
+ def on_unselect(event)
1034
+ @treectrl.unselect_all()
1035
+ end
1036
+
1037
+ def do_set_bold(bold = true)
1038
+ item = @treectrl.selection
1039
+ return nil unless check_item( item )
1040
+ @treectrl.set_item_bold(item, bold)
1041
+ end
1042
+
1043
+ def on_delete(event)
1044
+ item = @treectrl.selection
1045
+ return nil unless check_item( item )
1046
+ @treectrl.delete(item)
1047
+ end
1048
+
1049
+ def on_delete_children(event)
1050
+ item = @treectrl.selection
1051
+ return nil unless check_item( item )
1052
+ @treectrl.delete_children(item)
1053
+ end
1054
+
1055
+ def on_delete_all(event)
1056
+ @treectrl.delete_all_items()
1057
+ end
1058
+
1059
+ def on_recreate(event)
1060
+ on_delete_all(event)
1061
+ @treectrl.add_test_items_to_tree(5, 2)
1062
+ end
1063
+
1064
+ def on_set_image_size(event)
1065
+ size = get_number_from_user("Enter the size for the images to use",
1066
+ "Size: ",
1067
+ "TreeCtrl sample",
1068
+ @treectrl.image_size())
1069
+ if size == -1
1070
+ return nil
1071
+ end
1072
+
1073
+ @treectrl.create_image_list(size)
1074
+ Wx::THE_APP.show_images = true
1075
+ end
1076
+
1077
+ def on_toggle_images(event)
1078
+ if Wx::get_app.show_images
1079
+ @treectrl.unset_image_list
1080
+ Wx::get_app.show_images = false
1081
+ else
1082
+ @treectrl.create_image_list
1083
+ Wx::get_app.show_images = true
1084
+ end
1085
+ end
1086
+
1087
+ def on_toggle_buttons(event)
1088
+ end
1089
+
1090
+ def on_collapse_and_reset(event)
1091
+ @treectrl.collapse_and_reset(@treectrl.get_root_item())
1092
+ end
1093
+
1094
+ def on_ensure_visible(event)
1095
+ @treectrl.do_ensure_visible()
1096
+ end
1097
+
1098
+ def on_insert_item(event)
1099
+ image = Wx::THE_APP.show_images() ? TreeCtrlIcon_File : -1
1100
+ @treectrl.insert_item(@treectrl.root_item, -1, "2nd item", image)
1101
+ end
1102
+
1103
+ def on_add_item(event)
1104
+ @s_num += 1
1105
+ text = sprintf("Item #%d", @s_num)
1106
+ @treectrl.append_item(@treectrl.root_item, text)
1107
+ end
1108
+
1109
+ def on_inc_indent(event)
1110
+ if @treectrl.indent < 100
1111
+ @treectrl.indent += 5
1112
+ end
1113
+ end
1114
+
1115
+ def on_dec_indent(event)
1116
+ if @treectrl.indent > 10
1117
+ @treectrl.indent -= 5
1118
+ end
1119
+ end
1120
+
1121
+ def on_inc_spacing(event)
1122
+ if @treectrl.spacing < 100
1123
+ @treectrl.spacing += 5
1124
+ end
1125
+ end
1126
+
1127
+ def on_dec_spacing(event)
1128
+ if @treectrl.spacing > 10
1129
+ @treectrl.spacing -= 5
1130
+ end
1131
+ end
1132
+
1133
+ def on_toggle_icon(event)
1134
+ item = @treectrl.selection
1135
+ return nil unless check_item( item )
1136
+ @treectrl.do_toggle_icon(item)
1137
+ end
1138
+
1139
+ # TODO
1140
+ def on_set_fg_colour(event)
1141
+ Wx::log_message('NOT IMPLEMENTED')
1142
+ return
1143
+ col = Wx::get_colour_from_user(self, @treectrl.foreground_colour)
1144
+ if col.ok?
1145
+ @treectrl.foreground_colour = col
1146
+ end
1147
+ end
1148
+
1149
+ # TODO
1150
+ def on_set_bg_colour(event)
1151
+ Wx::log_message('NOT IMPLEMENTED')
1152
+ return
1153
+ col = Wx::get_colour_from_user(self, @treectrl.background_colour)
1154
+ if col.ok?
1155
+ @treectrl.background_colour = col
1156
+ end
1157
+ end
1158
+
1159
+ end
1160
+
1161
+ class MyApp < Wx::App
1162
+ attr_accessor :show_images, :show_buttons
1163
+ def initialize
1164
+ self.show_images = true
1165
+ self.show_buttons = false
1166
+ super
1167
+ end
1168
+
1169
+ def on_init
1170
+ # Create the main frame window
1171
+ frame = MyFrame.new("TreeCtrl Test", 50, 50, 450, 600)
1172
+ # show the frame
1173
+ frame.show(true)
1174
+ # set_top_window(frame)
1175
+
1176
+ end
1177
+ end
1178
+
1179
+ app = MyApp.new
1180
+ app.main_loop