3 * A tool palette widget shows groups of toolbar items as a grid of icons
8 #include <glib/gi18n.h>
10 static GtkWidget *window = NULL;
12 static void load_stock_items (GtkToolPalette *palette);
13 static void load_toggle_items (GtkToolPalette *palette);
14 static void load_special_items (GtkToolPalette *palette);
16 static void on_combo_orientation_changed(GtkComboBox *combo_box, gpointer user_data)
18 GtkToolPalette *palette = GTK_TOOL_PALETTE (user_data);
19 GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW (GTK_WIDGET (palette)->parent);
20 GtkTreeModel *model = gtk_combo_box_get_model (combo_box);
23 if(!gtk_combo_box_get_active_iter(combo_box, &iter))
27 gtk_tree_model_get (model, &iter, 1, &val, -1);
29 gtk_orientable_set_orientation (GTK_ORIENTABLE (palette), val);
31 if (val == GTK_ORIENTATION_HORIZONTAL)
32 gtk_scrolled_window_set_policy (sw, GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
34 gtk_scrolled_window_set_policy (sw, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
37 static void on_combo_style_changed(GtkComboBox *combo_box, gpointer user_data)
39 GtkToolPalette *palette = GTK_TOOL_PALETTE (user_data);
40 GtkTreeModel *model = gtk_combo_box_get_model (combo_box);
43 if(!gtk_combo_box_get_active_iter(combo_box, &iter))
47 gtk_tree_model_get (model, &iter, 1, &val, -1);
50 gtk_tool_palette_unset_style (palette);
52 gtk_tool_palette_set_style (palette, val);
56 do_toolpalette (GtkWidget *do_widget)
58 GtkWidget *box = NULL;
59 GtkWidget *combo_orientation = NULL;
60 GtkListStore *combo_orientation_model = NULL;
61 GtkWidget *combo_style = NULL;
62 GtkListStore *combo_style_model = NULL;
63 GtkCellRenderer *cell_renderer = NULL;
65 GtkWidget *palette = NULL;
66 GtkWidget *palette_scroller = NULL;
70 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
71 gtk_window_set_screen (GTK_WINDOW (window),
72 gtk_widget_get_screen (do_widget));
73 gtk_window_set_title (GTK_WINDOW (window), "Tool Palette");
74 gtk_window_set_default_size (GTK_WINDOW (window), 200, 600);
76 g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
77 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
79 /* Add widgets to control the ToolPalette appearance: */
80 box = gtk_vbox_new (FALSE, 6);
81 gtk_container_add (GTK_CONTAINER (window), box);
83 /* Orientation combo box: */
84 combo_orientation_model = gtk_list_store_new (2,
85 G_TYPE_STRING, G_TYPE_INT);
86 gtk_list_store_append (combo_orientation_model, &iter);
87 gtk_list_store_set (combo_orientation_model, &iter,
88 0, "Horizontal", 1, GTK_ORIENTATION_HORIZONTAL, -1);
89 gtk_list_store_append (combo_orientation_model, &iter);
90 gtk_list_store_set (combo_orientation_model, &iter,
91 0, "Vertical", 1, GTK_ORIENTATION_VERTICAL, -1);
92 combo_orientation = gtk_combo_box_new_with_model (
93 GTK_TREE_MODEL (combo_orientation_model));
94 cell_renderer = gtk_cell_renderer_text_new ();
95 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_orientation),
97 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_orientation),
98 cell_renderer, "text", 0, NULL);
99 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_orientation),
101 gtk_box_pack_start (GTK_BOX (box), combo_orientation, FALSE, FALSE, 0);
103 /* Style combo box: */
104 combo_style_model = gtk_list_store_new (2,
105 G_TYPE_STRING, G_TYPE_INT);
106 gtk_list_store_append (combo_style_model, &iter);
107 gtk_list_store_set (combo_style_model, &iter,
108 0, "Text", 1, GTK_TOOLBAR_TEXT, -1);
109 gtk_list_store_append (combo_style_model, &iter);
110 gtk_list_store_set (combo_style_model, &iter,
111 0, "Both", 1, GTK_TOOLBAR_BOTH, -1);
112 gtk_list_store_append (combo_style_model, &iter);
113 gtk_list_store_set (combo_style_model, &iter,
114 0, "Both: Horizontal", 1, GTK_TOOLBAR_BOTH_HORIZ, -1);
115 gtk_list_store_append (combo_style_model, &iter);
116 gtk_list_store_set (combo_style_model, &iter,
117 0, "Icons", 1, GTK_TOOLBAR_ICONS, -1);
118 gtk_list_store_append (combo_style_model, &iter);
119 gtk_list_store_set (combo_style_model, &iter,
120 0, "Default", 1, -1 /* A custom meaning for this demo. */, -1);
121 combo_style = gtk_combo_box_new_with_model (
122 GTK_TREE_MODEL (combo_style_model));
123 cell_renderer = gtk_cell_renderer_text_new ();
124 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_style),
125 cell_renderer, TRUE);
126 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_style),
127 cell_renderer, "text", 0, NULL);
128 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_style),
130 gtk_box_pack_start (GTK_BOX (box), combo_style, FALSE, FALSE, 0);
132 /* Add and fill the ToolPalette: */
133 palette = gtk_tool_palette_new ();
135 load_stock_items (GTK_TOOL_PALETTE (palette));
136 load_toggle_items (GTK_TOOL_PALETTE (palette));
137 load_special_items (GTK_TOOL_PALETTE (palette));
139 palette_scroller = gtk_scrolled_window_new (NULL, NULL);
140 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (palette_scroller),
141 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
142 gtk_container_set_border_width (GTK_CONTAINER (palette_scroller), 6);
143 gtk_container_add (GTK_CONTAINER (palette_scroller), palette);
145 gtk_container_add (GTK_CONTAINER (box), palette_scroller);
147 gtk_widget_show_all (box);
149 /* Connect signals: */
150 g_signal_connect (combo_orientation, "changed",
151 G_CALLBACK (on_combo_orientation_changed), palette);
152 g_signal_connect (combo_style, "changed",
153 G_CALLBACK (on_combo_style_changed), palette);
155 /* Keep the widgets in sync: */
156 on_combo_orientation_changed (GTK_COMBO_BOX (combo_orientation), palette);
159 if (!GTK_WIDGET_VISIBLE (window))
161 gtk_widget_show_all (window);
165 gtk_widget_destroy (window);
174 load_stock_items (GtkToolPalette *palette)
176 GtkWidget *group_af = gtk_tool_item_group_new (_("Stock Icons (A-F)"));
177 GtkWidget *group_gn = gtk_tool_item_group_new (_("Stock Icons (G-N)"));
178 GtkWidget *group_or = gtk_tool_item_group_new (_("Stock Icons (O-R)"));
179 GtkWidget *group_sz = gtk_tool_item_group_new (_("Stock Icons (S-Z)"));
180 GtkWidget *group = NULL;
186 stock_ids = gtk_stock_list_ids ();
187 stock_ids = g_slist_sort (stock_ids, (GCompareFunc) strcmp);
189 gtk_container_add (GTK_CONTAINER (palette), group_af);
190 gtk_container_add (GTK_CONTAINER (palette), group_gn);
191 gtk_container_add (GTK_CONTAINER (palette), group_or);
192 gtk_container_add (GTK_CONTAINER (palette), group_sz);
194 for (iter = stock_ids; iter; iter = g_slist_next (iter))
196 GtkStockItem stock_item;
197 gchar *id = iter->data;
218 item = gtk_tool_button_new_from_stock (id);
219 gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (item), id);
220 gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
221 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
223 if (!gtk_stock_lookup (id, &stock_item) || !stock_item.label)
224 gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), id);
229 g_slist_free (stock_ids);
233 load_toggle_items (GtkToolPalette *palette)
235 GSList *toggle_group = NULL;
241 group = gtk_tool_item_group_new (_("Radio Item"));
242 gtk_container_add (GTK_CONTAINER (palette), group);
244 for (i = 1; i <= 10; ++i)
246 label = g_strdup_printf ("#%d", i);
247 item = gtk_radio_tool_button_new (toggle_group);
248 gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), label);
251 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
252 toggle_group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (item));
257 create_entry_item (const char *text)
262 entry = gtk_entry_new ();
263 gtk_entry_set_text (GTK_ENTRY (entry), text);
264 gtk_entry_set_width_chars (GTK_ENTRY (entry), 5);
266 item = gtk_tool_item_new ();
267 gtk_container_add (GTK_CONTAINER (item), entry);
273 load_special_items (GtkToolPalette *palette)
277 GtkWidget *label_button;
279 group = gtk_tool_item_group_new (NULL);
280 label_button = gtk_button_new_with_label (_("Advanced Features"));
281 gtk_widget_show (label_button);
282 gtk_tool_item_group_set_label_widget (GTK_TOOL_ITEM_GROUP (group),
284 gtk_container_add (GTK_CONTAINER (palette), group);
286 item = create_entry_item ("homogeneous=FALSE");
287 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
288 gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
289 "homogeneous", FALSE, NULL);
291 item = create_entry_item ("homogeneous=FALSE, expand=TRUE");
292 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
293 gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
294 "homogeneous", FALSE, "expand", TRUE,
297 item = create_entry_item ("homogeneous=FALSE, expand=TRUE, fill=FALSE");
298 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
299 gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
300 "homogeneous", FALSE, "expand", TRUE,
301 "fill", FALSE, NULL);
303 item = create_entry_item ("homogeneous=FALSE, expand=TRUE, new-row=TRUE");
304 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
305 gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
306 "homogeneous", FALSE, "expand", TRUE,
307 "new-row", TRUE, NULL);
309 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_UP);
310 gtk_tool_item_set_tooltip_text (item, "Show on vertical palettes only");
311 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
312 gtk_tool_item_set_visible_horizontal (item, FALSE);
314 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
315 gtk_tool_item_set_tooltip_text (item, "Show on horizontal palettes only");
316 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
317 gtk_tool_item_set_visible_vertical (item, FALSE);
319 item = gtk_tool_button_new_from_stock (GTK_STOCK_DELETE);
320 gtk_tool_item_set_tooltip_text (item, "Do not show at all");
321 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
322 gtk_widget_set_no_show_all (GTK_WIDGET (item), TRUE);
324 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
325 gtk_tool_item_set_tooltip_text (item, "Expanded this item");
326 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
327 gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
328 "homogeneous", FALSE,
332 item = gtk_tool_button_new_from_stock (GTK_STOCK_HELP);
333 gtk_tool_item_set_tooltip_text (item, "A regular item");
334 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);