-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamic_menus.pro
183 lines (124 loc) · 5.65 KB
/
dynamic_menus.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
PRO Dynamic_Menus_List_Events, event
; This event handler responds to animal selections from
; the list widget. The selected item is added to the
; droplist and pull-down menu widgets.
Widget_Control, event.top, Get_UValue=info, /No_Copy
; How many animals currently on the list?
Widget_Control, event.id, Get_UValue=animals
numAnimals = N_Elements(animals)
; Get the selected animal.
selectedAnimal = animals[event.index]
; Special case if only one animal is left.
IF numAnimals EQ 1 AND event.index EQ 0 THEN BEGIN
newAnimals = ['None']
Widget_Control, info.listID, Set_Value=newAnimals, $
Set_UValue=newAnimals
Widget_Control, event.top, Set_UValue=info, /No_Copy
RETURN
ENDIF
; All other cases handled here.
CASE event.index OF
0: newAnimals = animals[1:numAnimals-1]
numAnimals-1: newAnimals = animals[0:numAnimals-2]
ELSE: newAnimals = [animals[0:event.index-1], animals[event.index+1:*]]
ENDCASE
; Update the animals in the list widget.
Widget_Control, info.listID, Set_Value=newAnimals, $
Set_UValue=newAnimals
; Add the selected animal to the droplist widget by first destroying
; the old droplist widget and re-creating a new one.
Widget_Control, info.dropID, /Destroy, Get_UValue=currentAnimals
info.dropID = Widget_Droplist(info.dropbase, Title='Current Animal Menu', $
Value=[currentAnimals, animals[event.index]], $
UValue=[currentAnimals, animals[event.index]], $
Event_Pro='Dynamic_Menus_Droplist_Events')
Widget_Control, info.dropID, Set_Droplist_Select=N_Elements(currentAnimals)
; Add the selected animal to the pull-down menu widget.
; The procedure here is to add a new button to the existing ones.
Widget_Control, info.menuID, Get_UValue=currentAnimals
; Build a new menu button.
currentAnimals = [currentAnimals, animals[event.index]]
lastIndex = N_Elements(currentAnimals) - 1
*info.buttons = [*info.buttons, Widget_Button(info.menuID, $
Value=currentAnimals[lastIndex])]
; Update the animals in the menu list.
Widget_Control, info.menuID, Set_UValue=currentAnimals
Widget_Control, event.top, Set_UValue=info, /No_Copy
END
;--------------------------------------------------------
PRO Dynamic_Menus_Droplist_Events, event
; Handles events from the droplist widget.
; Which animal was selected? Print it.
Widget_Control, event.id, Get_UValue=animals
Print, 'Current DropList Selection: ', animals[event.index]
END
;--------------------------------------------------------
PRO Dynamic_Menus_DropMenu_Events, event
; Handles events from the pull-down menu widget.
Widget_Control, event.top, Get_UValue=info, /No_Copy
; Which animal was selected? Print it.
Widget_Control, event.id, Get_Value=thisAnimal
Print, 'Pull-Down Menu Selection: ', thisAnimal
; Update the current selection in the droplist widget.
Widget_Control, info.dropID, Get_UValue=animals
thisAnimalIndex = Where(animals EQ thisAnimal)
Widget_Control, info.dropID, Set_Droplist_Select=thisAnimalIndex[0]
Widget_Control, event.top, Set_UValue=info, /No_Copy
END
;--------------------------------------------------------
PRO Dynamic_Menus_Cleanup, tlb
Widget_Control, tlb, Get_UValue=info
Ptr_Free, info.buttons
END
;--------------------------------------------------------
PRO Dynamic_Menus
; This program shows how to dynamically add to and
; delete items from list widgets, pull-down menu
; button, and droplist widgets.
; Create the widgets for the program.
tlb = Widget_Base(Column=1, /Base_Align_Center, $
MBar=menuID, Title='Dynamic Menus')
; Create the list widget.
animals = ['Cat', 'Dog', 'Cow', 'Eagle', 'Fox']
listbase = Widget_Base(tlb, Column=1)
label = Widget_Label(listbase, Value='Add These Animals to Menus')
listID = Widget_List(listbase, YSize = 5, Value=animals, $
UValue=animals, Event_Pro='Dynamic_Menus_List_Events')
; Create the droplist widget.
dropbase = Widget_Base(tlb, Column=1)
dropValues = 'Coyote'
dropID = Widget_Droplist(dropbase, Title='Current Animal Menu', $
Value=dropValues, UValue=dropValues, $
Event_Pro='Dynamic_Menus_Droplist_Events')
; Create the pull-down menu widget.
menubase = Widget_Base(tlb, /Base_Align_Center)
menuID = Widget_Button(menubase, Value='Select Animal Menu', $
/Menu, UValue=['Coyote'], $
Event_Pro='Dynamic_Menus_DropMenu_Events')
buttons = IntArr(1)
buttons[0] = Widget_Button(menuID, Value='Coyote')
; Center the top-level base.
Device, Get_Screen_Size=screenSize
xCenter = screenSize(0) / 2
yCenter = screenSize(1) / 2
geom = Widget_Info(tlb, /Geometry)
xHalfSize = geom.Scr_XSize / 2
yHalfSize = geom.Scr_YSize / 2
Widget_Control, tlb, XOffset = xCenter-xHalfSize, $
YOffset = yCenter-yHalfSize
; Realize the widgets.
Widget_Control, tlb, /Realize
; Create the info structure to hold program information.
info={ dropID:dropID, $ ; The identifier of the droplist widget.
dropbase:dropbase, $ ; The droplist base widget.
menuID:menuID, $ ; The identifier of the pull-down menu widget.
menubase:menubase, $ ; The pull-down menu base widget.
listID:listID, $ ; The identifier of the list widget.
listbase:listbase, $ ; The list base widget.
buttons:Ptr_New(buttons) $ ; The IDs of the buttons in the pull-down menu.
}
Widget_Control, tlb, Set_UValue=info, /No_Copy
; Run the program as a non-blocking widget.
XManager, 'dynamic_menus', tlb, /No_Block, Cleanup='Dynamic_Menus_Cleanup'
END
;--------------------------------------------------------