16
16
-- ----------------------------------------------------------------------------
17
17
18
18
with Ada.Strings.Fixed ;
19
- with Ada.Strings.Maps ; use Ada.Strings.Maps;
20
- with GNATCOLL.Utils ; use GNATCOLL.Utils;
21
- with GNAT.Strings ; use GNAT.Strings;
22
-
23
- with Case_Handling ; use Case_Handling;
24
- with GUI_Utils ; use GUI_Utils;
25
-
26
- with Gtk.Box ; use Gtk.Box;
27
- with Gtk.Combo_Box ; use Gtk.Combo_Box;
28
- with Gtk.Combo_Box_Text ; use Gtk.Combo_Box_Text;
29
- with Gtk.Enums ; use Gtk.Enums;
30
- with Gtk.Radio_Button ; use Gtk.Radio_Button;
19
+ with Ada.Strings.Maps ; use Ada.Strings.Maps;
20
+ with GNAT.Strings ; use GNAT.Strings;
21
+
22
+ with Case_Handling ; use Case_Handling;
23
+ with GUI_Utils ; use GUI_Utils;
24
+
25
+ with Gtk.Box ; use Gtk.Box;
26
+ with Gtk.Combo_Box ; use Gtk.Combo_Box;
27
+ with Gtk.Combo_Box_Text ; use Gtk.Combo_Box_Text;
28
+ with Gtk.Enums ; use Gtk.Enums;
29
+ with Gtk.Radio_Button ; use Gtk.Radio_Button;
31
30
with Gtk.Toggle_Button ;
32
- with Gtk.Widget ; use Gtk.Widget;
33
- with Glib.Object ; use Glib.Object;
31
+ with Gtk.Widget ; use Gtk.Widget;
32
+ with Glib.Object ; use Glib.Object;
33
+ with VSS.Strings.Conversions ; use VSS.Strings.Conversions;
34
+ with VSS.String_Vectors ; use VSS.String_Vectors;
34
35
35
36
package body Default_Preferences.Enums is
36
37
@@ -57,7 +58,35 @@ package body Default_Preferences.Enums is
57
58
-- Called when an enumeration preference with a radio button group
58
59
-- has changed.
59
60
60
- function Create_Combo_Box
61
+ procedure Choice_Combo_Changed
62
+ (Widget : access GObject_Record'Class;
63
+ Data : Manager_Preference);
64
+ -- Called when an enumeration preference with a combo box has changed.
65
+
66
+ procedure Choice_Radio_Changed
67
+ (Widget : access GObject_Record'Class;
68
+ Data : Manager_Preference);
69
+ -- Called when an enumeration preference with a radio button group
70
+ -- has changed.
71
+
72
+ function Create_Choices_Combo_Box
73
+ (Pref : not null access Choice_Preference_Record'Class;
74
+ Manager : not null access Preferences_Manager_Record'Class;
75
+ Choices : VSS.String_Vectors.Virtual_String_Vector)
76
+ return Gtk_Combo_Box_Text;
77
+ -- Create a combo box listing all the given choices, and updating the given
78
+ -- pref when the selected Value changes.
79
+
80
+ function Create_Choices_Radio_Buttons_Box
81
+ (Pref : not null access Choice_Preference_Record'Class;
82
+ Manager : not null access Preferences_Manager_Record'Class;
83
+ Choices : VSS.String_Vectors.Virtual_String_Vector)
84
+ return Gtk_Box;
85
+ -- Create a horizontal box containing radio buttons listing all the given
86
+ -- choices, and updating the given pref when the selected radio button
87
+ -- changes.
88
+
89
+ function Create_Enum_Combo_Box
61
90
(Pref : not null access Enum_Preference_Record'Class;
62
91
Manager : not null access Preferences_Manager_Record'Class;
63
92
Choices : not null GNAT.Strings.String_List_Access)
@@ -93,7 +122,86 @@ package body Default_Preferences.Enums is
93
122
-- Create_Combo_Box --
94
123
-- --------------------
95
124
96
- function Create_Combo_Box
125
+ function Create_Choices_Combo_Box
126
+ (Pref : not null access Choice_Preference_Record'Class;
127
+ Manager : not null access Preferences_Manager_Record'Class;
128
+ Choices : VSS.String_Vectors.Virtual_String_Vector)
129
+ return Gtk_Combo_Box_Text
130
+ is
131
+ I : Gint := 0 ;
132
+ Combo : Gtk_Combo_Box_Text;
133
+ begin
134
+ Gtk_New (Combo);
135
+
136
+ for Choice of Choices loop
137
+ Combo.Append_Text (Enum_Value_To_Label (To_UTF_8_String (Choice)));
138
+
139
+ if To_Lowercase.Transform (Choice) =
140
+ To_Lowercase.Transform (Pref.Current_Choice)
141
+ then
142
+ Combo.Set_Active (I);
143
+ end if ;
144
+
145
+ I := I + 1 ;
146
+ end loop ;
147
+
148
+ Preference_Handlers.Connect
149
+ (Combo, Gtk.Combo_Box.Signal_Changed,
150
+ Choice_Combo_Changed'Access ,
151
+ User_Data => (Preferences_Manager (Manager), Preference (Pref)));
152
+
153
+ Set_GObject_To_Update (Pref, GObject (Combo));
154
+
155
+ return Combo;
156
+ end Create_Choices_Combo_Box ;
157
+
158
+ -- ------------------------------------
159
+ -- Create_Choices_Radio_Buttons_Box --
160
+ -- ------------------------------------
161
+
162
+ function Create_Choices_Radio_Buttons_Box
163
+ (Pref : not null access Choice_Preference_Record'Class;
164
+ Manager : not null access Preferences_Manager_Record'Class;
165
+ Choices : VSS.String_Vectors.Virtual_String_Vector)
166
+ return Gtk_Box
167
+ is
168
+ Radio_Box : Gtk_Box;
169
+ Radio :
170
+ array (Choices.First_Index .. Choices.Last_Index) of Enum_Radio_Button;
171
+ begin
172
+ Gtk_New_Hbox (Radio_Box, Homogeneous => False);
173
+
174
+ for K in Choices.First_Index .. Choices.Last_Index loop
175
+ Radio (K) := new Enum_Radio_Button_Record;
176
+ Initialize
177
+ (Radio_Button => Gtk_Radio_Button (Radio (K)),
178
+ Group => Radio (Radio'First),
179
+ Label => Enum_Value_To_Label
180
+ (To_UTF_8_String (Choices.Element (K))));
181
+ Radio (K).Enum_Value := K - Choices.First_Index;
182
+ Radio_Box.Pack_Start (Radio (K), Expand => False);
183
+
184
+ Preference_Handlers.Connect
185
+ (Radio (K), Gtk.Toggle_Button.Signal_Toggled,
186
+ Choice_Radio_Changed'Access ,
187
+ User_Data =>
188
+ (Preferences_Manager (Manager), Preference (Pref)));
189
+
190
+ if To_Lowercase.Transform (Pref.Current_Choice)
191
+ = To_Lowercase.Transform (Choices.Element (K))
192
+ then
193
+ Radio (K).Set_Active (True);
194
+ end if ;
195
+ end loop ;
196
+
197
+ return Radio_Box;
198
+ end Create_Choices_Radio_Buttons_Box ;
199
+
200
+ -- --------------------
201
+ -- Create_Combo_Box --
202
+ -- --------------------
203
+
204
+ function Create_Enum_Combo_Box
97
205
(Pref : not null access Enum_Preference_Record'Class;
98
206
Manager : not null access Preferences_Manager_Record'Class;
99
207
Choices : not null GNAT.Strings.String_List_Access)
@@ -122,7 +230,7 @@ package body Default_Preferences.Enums is
122
230
Set_GObject_To_Update (Pref, GObject (Combo));
123
231
124
232
return Combo;
125
- end Create_Combo_Box ;
233
+ end Create_Enum_Combo_Box ;
126
234
127
235
-- ---------------------------------
128
236
-- Create_Enum_Radio_Buttons_Box --
@@ -192,6 +300,41 @@ package body Default_Preferences.Enums is
192
300
Data.Manager.Notify_Pref_Changed (Data.Pref);
193
301
end Enum_Radio_Changed ;
194
302
303
+ -- ------------------------
304
+ -- Choice_Combo_Changed --
305
+ -- ------------------------
306
+
307
+ procedure Choice_Combo_Changed
308
+ (Widget : access GObject_Record'Class;
309
+ Data : Manager_Preference)
310
+ is
311
+ Combo : constant Gtk_Combo_Box_Text :=
312
+ Gtk_Combo_Box_Text (Widget);
313
+ Pref : constant Choice_Preference := Choice_Preference (Data.Pref);
314
+ begin
315
+ Pref.Current_Choice :=
316
+ Pref.Choices.Element
317
+ (Natural (Get_Active (Combo)) + Pref.Choices.First_Index);
318
+ Data.Manager.Notify_Pref_Changed (Data.Pref);
319
+ end Choice_Combo_Changed ;
320
+
321
+ -- ------------------------
322
+ -- Choice_Radio_Changed --
323
+ -- ------------------------
324
+
325
+ procedure Choice_Radio_Changed
326
+ (Widget : access GObject_Record'Class;
327
+ Data : Manager_Preference)
328
+ is
329
+ Radio : constant Enum_Radio_Button := Enum_Radio_Button (Widget);
330
+ Pref : constant Choice_Preference := Choice_Preference (Data.Pref);
331
+ begin
332
+ Pref.Current_Choice :=
333
+ Pref.Choices.Element
334
+ (Natural (Radio.Enum_Value + Pref.Choices.First_Index));
335
+ Data.Manager.Notify_Pref_Changed (Data.Pref);
336
+ end Choice_Radio_Changed ;
337
+
195
338
-- ----------
196
339
-- Create --
197
340
-- ----------
@@ -200,19 +343,24 @@ package body Default_Preferences.Enums is
200
343
(Manager : access Preferences_Manager_Record'Class;
201
344
Path : Preference_Path;
202
345
Name, Label, Doc : String;
203
- Choices : GNAT.Strings.String_List_Access;
204
- Default : Integer;
205
- Priority : Integer := -1 )
346
+ Choices : VSS.String_Vectors.Virtual_String_Vector;
347
+ Default : VSS.Strings.Virtual_String;
348
+ Priority : Integer := -1 ;
349
+ Combo_Threshold : Integer := 3 )
206
350
return Choice_Preference
207
351
is
208
352
Result : constant Choice_Preference := new Choice_Preference_Record;
209
353
begin
210
354
Result.Choices := Choices;
211
- Result.Enum_Value := Default;
355
+ Result.Default_Choice := Default;
356
+ Result.Current_Choice := Default;
357
+ Result.Combo_Threshold := Combo_Threshold;
212
358
213
359
-- If the preference should be displayed with radio buttons, place them
214
360
-- in a group named using the preference's label.
215
- if Choices'Length > Needs_Combo_Threshold then
361
+ if Combo_Threshold /= -1
362
+ and then Choices.Length > Result.Combo_Threshold
363
+ then
216
364
Manager.Register
217
365
(Name => Name,
218
366
Path => Path,
@@ -261,7 +409,7 @@ package body Default_Preferences.Enums is
261
409
262
410
overriding procedure Free (Pref : in out Choice_Preference_Record) is
263
411
begin
264
- Free (Pref.Choices);
412
+ Clear (Pref.Choices);
265
413
Free (Preference_Record (Pref));
266
414
end Free ;
267
415
@@ -274,12 +422,14 @@ package body Default_Preferences.Enums is
274
422
Manager : access Preferences_Manager_Record'Class)
275
423
return Gtk.Widget.Gtk_Widget
276
424
is
277
- (if Pref.Choices'Length > Needs_Combo_Threshold then
278
- Gtk_Widget (Create_Combo_Box (Pref => Pref,
425
+ (if Pref.Combo_Threshold /= -1
426
+ and then Pref.Choices.Length > Pref.Combo_Threshold
427
+ then
428
+ Gtk_Widget (Create_Choices_Combo_Box (Pref => Pref,
279
429
Manager => Manager,
280
430
Choices => Pref.Choices))
281
431
else
282
- Gtk_Widget (Create_Enum_Radio_Buttons_Box (Pref => Pref,
432
+ Gtk_Widget (Create_Choices_Radio_Buttons_Box (Pref => Pref,
283
433
Manager => Manager,
284
434
Choices => Pref.Choices)));
285
435
@@ -290,10 +440,8 @@ package body Default_Preferences.Enums is
290
440
overriding function Get_Pref
291
441
(Pref : access Choice_Preference_Record) return String is
292
442
begin
293
- return Pref.Choices (Pref.Enum_Value + Pref.Choices'First).all ;
294
- exception
295
- when Constraint_Error =>
296
- return Pref.Choices (Pref.Choices'First).all ;
443
+ return
444
+ VSS.Strings.Conversions.To_UTF_8_String (Pref.Current_Choice);
297
445
end Get_Pref ;
298
446
299
447
-- ------------
@@ -303,15 +451,16 @@ package body Default_Preferences.Enums is
303
451
overriding procedure Set_Pref
304
452
(Pref : access Choice_Preference_Record;
305
453
Manager : access Preferences_Manager_Record'Class;
306
- Value : String) is
454
+ Value : String)
455
+ is
456
+ V : constant VSS.Strings.Virtual_String :=
457
+ To_Lowercase.Transform
458
+ (VSS.Strings.Conversions.To_Virtual_String (Value));
307
459
begin
308
- for C in Pref.Choices'Range loop
309
- if Equal (Pref.Choices (C).all , Value, Case_Sensitive => False) then
310
- Pref.Enum_Value := C - Pref.Choices'First;
311
- Manager.Notify_Pref_Changed (Pref);
312
- exit ;
313
- end if ;
314
- end loop ;
460
+ if To_Lowercase.Transform (Pref.Current_Choice) /= V then
461
+ Pref.Current_Choice := V;
462
+ Manager.Notify_Pref_Changed (Pref);
463
+ end if ;
315
464
end Set_Pref ;
316
465
317
466
-- --------------------------
@@ -333,7 +482,7 @@ package body Default_Preferences.Enums is
333
482
overriding function Editor_Needs_Label
334
483
(Pref : not null access Choice_Preference_Record) return Boolean
335
484
is
336
- (Pref.Choices' Length > Needs_Combo_Threshold );
485
+ (Pref.Choices. Length > Pref.Combo_Threshold );
337
486
338
487
-- ------------
339
488
-- Generics --
@@ -478,7 +627,7 @@ package body Default_Preferences.Enums is
478
627
479
628
if Enumeration_Choices'Last > Needs_Combo_Threshold then
480
629
Widget := Gtk_Widget
481
- (Create_Combo_Box (Pref => Pref,
630
+ (Create_Enum_Combo_Box (Pref => Pref,
482
631
Manager => Manager,
483
632
Choices => Choices));
484
633
else
0 commit comments