1 /* Size Groups
2  *
3  * SizeGroup provides a mechanism for grouping a number of
4  * widgets together so they all request the same amount of space.
5  * This is typically useful when you want a column of widgets to
6  * have the same size, but you can't use a Table widget.
7  *
8  * Note that size groups only affect the amount of space requested,
9  * not the size that the widgets finally receive. If you want the
10  * widgets in a SizeGroup to actually be the same size, you need
11  * to pack them in such a way that they get the size they request
12  * and not more. For example, if you are packing your widgets
13  * into a table, you would not include the Gtk.Fill flag.
14  */
15 
16 using System;
17 using Gtk;
18 
19 namespace GtkDemo
20 {
21 	[Demo ("Size Group", "DemoSizeGroup.cs")]
22 	public class DemoSizeGroup : Dialog
23 	{
24 		private SizeGroup sizeGroup;
25 
26 		static string [] colors = { "Red", "Green", "Blue" };
27 		static string [] dashes = { "Solid", "Dashed", "Dotted" };
28 		static string [] ends = { "Square", "Round", "Arrow" };
29 
DemoSizeGroup()30 		public DemoSizeGroup () : base ("SizeGroup", null, DialogFlags.DestroyWithParent,
31 						Gtk.Stock.Close, Gtk.ResponseType.Close)
32 		{
33 			Resizable = false;
34 
35  			VBox vbox = new VBox (false, 5);
36  			this.ContentArea.PackStart (vbox, true, true, 0);
37  			vbox.BorderWidth = 5;
38 
39  			sizeGroup = new SizeGroup (SizeGroupMode.Horizontal);
40 
41 			// Create one frame holding color options
42  			Frame frame = new Frame ("Color Options");
43  			vbox.PackStart (frame, true, true, 0);
44 
45  			Table table = new Table (2, 2, false);
46  			table.BorderWidth = 5;
47  			table.RowSpacing = 5;
48  			table.ColumnSpacing = 10;
49  			frame.Add (table);
50 
51  			AddRow (table, 0, sizeGroup, "_Foreground", colors);
52  			AddRow (table, 1, sizeGroup, "_Background", colors);
53 
54 			// And another frame holding line style options
55 			frame = new Frame ("Line Options");
56 			vbox.PackStart (frame, false, false, 0);
57 
58 			table = new Table (2, 2, false);
59 			table.BorderWidth = 5;
60 			table.RowSpacing = 5;
61 			table.ColumnSpacing = 10;
62 			frame.Add (table);
63 
64  			AddRow (table, 0, sizeGroup, "_Dashing", dashes);
65  			AddRow (table, 1, sizeGroup, "_Line ends", ends);
66 
67 			// And a check button to turn grouping on and off
68   			CheckButton checkButton = new CheckButton ("_Enable grouping");
69   			vbox.PackStart (checkButton, false, false, 0);
70   			checkButton.Active = true;
71  			checkButton.Toggled += new EventHandler (ToggleGrouping);
72 
73 			ShowAll ();
74 		}
75 
76 		// Convenience function to create a combo box holding a number of strings
CreateComboBox(string [] strings)77 		private ComboBox CreateComboBox (string [] strings)
78 		{
79 			ComboBoxText combo = new ComboBoxText ();
80 
81 			foreach (string str in strings)
82 				combo.AppendText (str);
83 
84 			combo.Active = 0;
85 			return combo;
86 		}
87 
AddRow(Table table, uint row, SizeGroup sizeGroup, string labelText, string [] options)88  		private void AddRow (Table table, uint row, SizeGroup sizeGroup, string labelText, string [] options)
89  		{
90  			Label label = new Label (labelText);
91  			label.SetAlignment (0, 1);
92 
93 			table.Attach (label,
94 				      0, 1, row, row + 1,
95 				      AttachOptions.Expand | AttachOptions.Fill, 0,
96 				      0, 0);
97 
98 			ComboBox combo = CreateComboBox (options);
99 			label.MnemonicWidget = combo;
100 
101 			sizeGroup.AddWidget (combo);
102 			table.Attach (combo,
103 				      1, 2, row, row + 1,
104 				      0, 0,
105 				      0, 0);
106 		}
107 
ToggleGrouping(object o, EventArgs args)108  		private void ToggleGrouping (object o, EventArgs args)
109  		{
110 			ToggleButton checkButton = (ToggleButton)o;
111 
112 			// SizeGroupMode.None is not generally useful, but is useful
113 			// here to show the effect of SizeGroupMode.Horizontal by
114 			// contrast
115  			SizeGroupMode mode;
116 
117  			if (checkButton.Active)
118  				mode = SizeGroupMode.Horizontal;
119  			else
120  				mode = SizeGroupMode.None;
121 
122  			sizeGroup.Mode = mode;
123  		}
124 
OnResponse(Gtk.ResponseType responseId)125 		protected override void OnResponse (Gtk.ResponseType responseId)
126 		{
127 			Destroy ();
128 		}
129 	}
130 }
131