summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/toolpalette.c
blob: 671aa77cd11fe52e14f039d183ac4c1d8968f4d5 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Tool Palette
 *
 * A tool palette widget shows groups of toolbar items as a grid of icons
 * or a list of names.
 */

#include <gtk/gtk.h>
#include <glib/gi18n.h>

static GtkWidget *window = NULL;

static void load_stock_items (GtkToolPalette *palette);
static void load_toggle_items (GtkToolPalette *palette);
static void load_special_items (GtkToolPalette *palette);

static void on_combo_orientation_changed(GtkComboBox *combo_box, gpointer user_data)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (user_data);
  GtkTreeModel *model = gtk_combo_box_get_model (combo_box);
  
  GtkTreeIter iter;
  if(!gtk_combo_box_get_active_iter(combo_box, &iter))
    return;

  gint val = 0;
  gtk_tree_model_get (model, &iter, 1, &val, -1);
  
  gtk_orientable_set_orientation (GTK_ORIENTABLE (palette), val);
}

static void on_combo_style_changed(GtkComboBox *combo_box, gpointer user_data)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (user_data);
  GtkTreeModel *model = gtk_combo_box_get_model (combo_box);
  
  GtkTreeIter iter;
  if(!gtk_combo_box_get_active_iter(combo_box, &iter))
    return;

  gint val = 0;
  gtk_tree_model_get (model, &iter, 1, &val, -1);
  
  gtk_tool_palette_set_style (palette, val);
}

GtkWidget *
do_toolpalette (GtkWidget *do_widget)
{
  GtkWidget *box = NULL;
  GtkWidget *combo_orientation = NULL;
  GtkListStore *combo_orientation_model = NULL;
  GtkWidget *combo_style = NULL;
  GtkListStore *combo_style_model = NULL;
  GtkCellRenderer *cell_renderer = NULL;
  GtkTreeIter iter;
  GtkWidget *palette = NULL;
  GtkWidget *palette_scroller = NULL;

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
                             gtk_widget_get_screen (do_widget));
      gtk_window_set_title (GTK_WINDOW (window), "Tool Palette");
      gtk_window_set_default_size (GTK_WINDOW (window), 200, 600);

      g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

      /* Add widgets to control the ToolPalette appearance: */
      box = gtk_vbox_new (FALSE, 6);
      gtk_container_add (GTK_CONTAINER (window), box);
      
      /* Orientation combo box: */
      combo_orientation_model = gtk_list_store_new (2, 
        G_TYPE_STRING, G_TYPE_INT);
      gtk_list_store_append (combo_orientation_model, &iter);
      gtk_list_store_set (combo_orientation_model, &iter,
        0, "Horizontal", 1, GTK_ORIENTATION_HORIZONTAL, -1);
      gtk_list_store_append (combo_orientation_model, &iter);
      gtk_list_store_set (combo_orientation_model, &iter,
        0, "Vertical", 1, GTK_ORIENTATION_VERTICAL, -1);
      combo_orientation = gtk_combo_box_new_with_model (  
        GTK_TREE_MODEL (combo_orientation_model));
      cell_renderer = gtk_cell_renderer_text_new ();
      gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_orientation), 
        cell_renderer, TRUE);
      gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_orientation), 
        cell_renderer, "text", 0, NULL); 
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_orientation), 
        &iter);
      gtk_box_pack_start (GTK_BOX (box), combo_orientation, FALSE, FALSE, 0);
      
      /* Style combo box: */
      combo_style_model = gtk_list_store_new (2, 
        G_TYPE_STRING, G_TYPE_INT);
      gtk_list_store_append (combo_style_model, &iter);
      gtk_list_store_set (combo_style_model, &iter,
        0, "Text", 1, GTK_TOOLBAR_TEXT, -1);
      gtk_list_store_append (combo_style_model, &iter);
      gtk_list_store_set (combo_style_model, &iter,
        0, "Both", 1, GTK_TOOLBAR_BOTH, -1);
      gtk_list_store_append (combo_style_model, &iter);
      gtk_list_store_set (combo_style_model, &iter,
        0, "Both: Horizontal", 1, GTK_TOOLBAR_BOTH_HORIZ, -1);
      gtk_list_store_append (combo_style_model, &iter);
      gtk_list_store_set (combo_style_model, &iter,
        0, "Icons", 1, GTK_TOOLBAR_ICONS, -1);
      combo_style = gtk_combo_box_new_with_model (  
        GTK_TREE_MODEL (combo_style_model));
      cell_renderer = gtk_cell_renderer_text_new ();
      gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_style), 
        cell_renderer, TRUE);
      gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_style), 
        cell_renderer, "text", 0, NULL); 
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_style), 
        &iter);
      gtk_box_pack_start (GTK_BOX (box), combo_style, FALSE, FALSE, 0);

      /* Add and fill the ToolPalette: */
      palette = gtk_tool_palette_new ();
      
      load_stock_items (GTK_TOOL_PALETTE (palette));
      load_toggle_items (GTK_TOOL_PALETTE (palette));
      load_special_items (GTK_TOOL_PALETTE (palette));
      
      palette_scroller = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (palette_scroller),
        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
      gtk_container_set_border_width (GTK_CONTAINER (palette_scroller), 6);
      gtk_container_add (GTK_CONTAINER (palette_scroller), palette);
      
      gtk_container_add (GTK_CONTAINER (box), palette_scroller);
      
      gtk_widget_show_all (box);
      
      /* Connect signals: */
      g_signal_connect (combo_orientation, "changed", 
        G_CALLBACK (on_combo_orientation_changed), palette);
      g_signal_connect (combo_style, "changed", 
        G_CALLBACK (on_combo_style_changed), palette);
      
      /* Keep the widgets in sync: */
      on_combo_orientation_changed (GTK_COMBO_BOX (combo_orientation), palette);
    }

  if (!GTK_WIDGET_VISIBLE (window))
    {
      gtk_widget_show_all (window);
    }
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}


static void
load_stock_items (GtkToolPalette *palette)
{
  GtkWidget *group_af = gtk_tool_item_group_new (_("Stock Icons (A-F)"));
  GtkWidget *group_gn = gtk_tool_item_group_new (_("Stock Icons (G-N)"));
  GtkWidget *group_or = gtk_tool_item_group_new (_("Stock Icons (O-R)"));
  GtkWidget *group_sz = gtk_tool_item_group_new (_("Stock Icons (S-Z)"));
  GtkWidget *group = NULL;

  GtkToolItem *item;
  GSList *stock_ids;
  GSList *iter;

  stock_ids = gtk_stock_list_ids ();
  stock_ids = g_slist_sort (stock_ids, (GCompareFunc) strcmp);

  gtk_container_add (GTK_CONTAINER (palette), group_af);
  gtk_container_add (GTK_CONTAINER (palette), group_gn);
  gtk_container_add (GTK_CONTAINER (palette), group_or);
  gtk_container_add (GTK_CONTAINER (palette), group_sz);

  for (iter = stock_ids; iter; iter = g_slist_next (iter))
    {
      GtkStockItem stock_item;
      gchar *id = iter->data;

      switch (id[4])
        {
          case 'a':
            group = group_af;
            break;

          case 'g':
            group = group_gn;
            break;

          case 'o':
            group = group_or;
            break;

          case 's':
            group = group_sz;
            break;
        }

      item = gtk_tool_button_new_from_stock (id);
      gtk_tool_item_set_tooltip_text (GTK_TOOL_ITEM (item), id);
      gtk_tool_item_set_is_important (GTK_TOOL_ITEM (item), TRUE);
      gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);

      if (!gtk_stock_lookup (id, &stock_item) || !stock_item.label)
        gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), id);

      g_free (id);
    }

  g_slist_free (stock_ids);
}

static void
load_toggle_items (GtkToolPalette *palette)
{
  GSList *toggle_group = NULL;
  GtkToolItem *item;
  GtkWidget *group;
  char *label;
  int i;

  group = gtk_tool_item_group_new (_("Radio Item"));
  gtk_container_add (GTK_CONTAINER (palette), group);

  for (i = 1; i <= 10; ++i)
    {
      label = g_strdup_printf ("#%d", i);
      item = gtk_radio_tool_button_new (toggle_group);
      gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), label);
      g_free (label);

      gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
      toggle_group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (item));
    }
}

static GtkToolItem *
create_entry_item (const char *text)
{
  GtkToolItem *item;
  GtkWidget *entry;

  entry = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (entry), text);
  gtk_entry_set_width_chars (GTK_ENTRY (entry), 5);

  item = gtk_tool_item_new ();
  gtk_container_add (GTK_CONTAINER (item), entry);

  return item;
}

static void
load_special_items (GtkToolPalette *palette)
{
  GtkToolItem *item;
  GtkWidget *group;

  group = gtk_tool_item_group_new (_("Advanced Features"));
  gtk_container_add (GTK_CONTAINER (palette), group);

  item = create_entry_item ("homogeneous=FALSE");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
                           "homogeneous", FALSE, NULL);

  item = create_entry_item ("homogeneous=FALSE, expand=TRUE");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
                           "homogeneous", FALSE, "expand", TRUE,
                           NULL);

  item = create_entry_item ("homogeneous=FALSE, expand=TRUE, fill=FALSE");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
                           "homogeneous", FALSE, "expand", TRUE,
                           "fill", FALSE, NULL);

  item = create_entry_item ("homogeneous=FALSE, expand=TRUE, new-row=TRUE");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
                           "homogeneous", FALSE, "expand", TRUE,
                           "new-row", TRUE, NULL);

  item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_UP);
  gtk_tool_item_set_tooltip_text (item, "Show on vertical palettes only");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_tool_item_set_visible_horizontal (item, FALSE);

  item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
  gtk_tool_item_set_tooltip_text (item, "Show on horizontal palettes only");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_tool_item_set_visible_vertical (item, FALSE);

  item = gtk_tool_button_new_from_stock (GTK_STOCK_DELETE);
  gtk_tool_item_set_tooltip_text (item, "Do not show at all");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_widget_set_no_show_all (GTK_WIDGET (item), TRUE);

  item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
  gtk_tool_item_set_tooltip_text (item, "Expanded this item");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
  gtk_container_child_set (GTK_CONTAINER (group), GTK_WIDGET (item),
                           "homogeneous", FALSE,
                           "expand", TRUE,
                           NULL);

  item = gtk_tool_button_new_from_stock (GTK_STOCK_HELP);
  gtk_tool_item_set_tooltip_text (item, "A regular item");
  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
}