1616-- ----------------------------------------------------------------------------
1717
1818with 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;
3130with 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;
3435
3536package body Default_Preferences.Enums is
3637
@@ -57,7 +58,35 @@ package body Default_Preferences.Enums is
5758 -- Called when an enumeration preference with a radio button group
5859 -- has changed.
5960
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
6190 (Pref : not null access Enum_Preference_Record'Class;
6291 Manager : not null access Preferences_Manager_Record'Class;
6392 Choices : not null GNAT.Strings.String_List_Access)
@@ -93,7 +122,86 @@ package body Default_Preferences.Enums is
93122 -- Create_Combo_Box --
94123 -- --------------------
95124
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
97205 (Pref : not null access Enum_Preference_Record'Class;
98206 Manager : not null access Preferences_Manager_Record'Class;
99207 Choices : not null GNAT.Strings.String_List_Access)
@@ -122,7 +230,7 @@ package body Default_Preferences.Enums is
122230 Set_GObject_To_Update (Pref, GObject (Combo));
123231
124232 return Combo;
125- end Create_Combo_Box ;
233+ end Create_Enum_Combo_Box ;
126234
127235 -- ---------------------------------
128236 -- Create_Enum_Radio_Buttons_Box --
@@ -192,6 +300,41 @@ package body Default_Preferences.Enums is
192300 Data.Manager.Notify_Pref_Changed (Data.Pref);
193301 end Enum_Radio_Changed ;
194302
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+
195338 -- ----------
196339 -- Create --
197340 -- ----------
@@ -200,19 +343,24 @@ package body Default_Preferences.Enums is
200343 (Manager : access Preferences_Manager_Record'Class;
201344 Path : Preference_Path;
202345 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 )
206350 return Choice_Preference
207351 is
208352 Result : constant Choice_Preference := new Choice_Preference_Record;
209353 begin
210354 Result.Choices := Choices;
211- Result.Enum_Value := Default;
355+ Result.Default_Choice := Default;
356+ Result.Current_Choice := Default;
357+ Result.Combo_Threshold := Combo_Threshold;
212358
213359 -- If the preference should be displayed with radio buttons, place them
214360 -- 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
216364 Manager.Register
217365 (Name => Name,
218366 Path => Path,
@@ -261,7 +409,7 @@ package body Default_Preferences.Enums is
261409
262410 overriding procedure Free (Pref : in out Choice_Preference_Record) is
263411 begin
264- Free (Pref.Choices);
412+ Clear (Pref.Choices);
265413 Free (Preference_Record (Pref));
266414 end Free ;
267415
@@ -274,12 +422,14 @@ package body Default_Preferences.Enums is
274422 Manager : access Preferences_Manager_Record'Class)
275423 return Gtk.Widget.Gtk_Widget
276424 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,
279429 Manager => Manager,
280430 Choices => Pref.Choices))
281431 else
282- Gtk_Widget (Create_Enum_Radio_Buttons_Box (Pref => Pref,
432+ Gtk_Widget (Create_Choices_Radio_Buttons_Box (Pref => Pref,
283433 Manager => Manager,
284434 Choices => Pref.Choices)));
285435
@@ -290,10 +440,8 @@ package body Default_Preferences.Enums is
290440 overriding function Get_Pref
291441 (Pref : access Choice_Preference_Record) return String is
292442 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);
297445 end Get_Pref ;
298446
299447 -- ------------
@@ -303,15 +451,16 @@ package body Default_Preferences.Enums is
303451 overriding procedure Set_Pref
304452 (Pref : access Choice_Preference_Record;
305453 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));
307459 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 ;
315464 end Set_Pref ;
316465
317466 -- --------------------------
@@ -333,7 +482,7 @@ package body Default_Preferences.Enums is
333482 overriding function Editor_Needs_Label
334483 (Pref : not null access Choice_Preference_Record) return Boolean
335484 is
336- (Pref.Choices' Length > Needs_Combo_Threshold );
485+ (Pref.Choices. Length > Pref.Combo_Threshold );
337486
338487 -- ------------
339488 -- Generics --
@@ -478,7 +627,7 @@ package body Default_Preferences.Enums is
478627
479628 if Enumeration_Choices'Last > Needs_Combo_Threshold then
480629 Widget := Gtk_Widget
481- (Create_Combo_Box (Pref => Pref,
630+ (Create_Enum_Combo_Box (Pref => Pref,
482631 Manager => Manager,
483632 Choices => Choices));
484633 else
0 commit comments