testactions.c 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* testactions.c
 * Copyright (C) 2003  Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
19
#define GDK_DISABLE_DEPRECATION_WARNINGS
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <gtk/gtk.h>

static GtkActionGroup *action_group = NULL;
static GtkToolbar *toolbar = NULL;

static void
activate_action (GtkAction *action)
{
  const gchar *name = gtk_action_get_name (action);
  const gchar *typename = G_OBJECT_TYPE_NAME (action);

  g_message ("Action %s (type=%s) activated", name, typename);
}

static void
toggle_action (GtkAction *action)
{
  const gchar *name = gtk_action_get_name (action);
  const gchar *typename = G_OBJECT_TYPE_NAME (action);

  g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
	     gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
}

44 45 46 47 48 49 50 51 52 53 54 55

static void
radio_action (GtkAction *action)
{
  const gchar *name = gtk_action_get_name (action);
  const gchar *typename = G_OBJECT_TYPE_NAME (action);

  g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename,
	     gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
	     gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
}

56 57 58 59 60 61 62 63 64 65 66 67 68
static void
recent_action (GtkAction *action)
{
  const gchar *name = gtk_action_get_name (action);
  const gchar *typename = G_OBJECT_TYPE_NAME (action);
  gchar *uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (action));

  g_message ("Action %s (type=%s) activated (uri=%s)",
             name, typename,
             uri ? uri : "no item selected");
  g_free (uri);
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static void
toggle_cnp_actions (GtkAction *action)
{
  gboolean sensitive;

  sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
  action = gtk_action_group_get_action (action_group, "cut");
  g_object_set (action, "sensitive", sensitive, NULL);
  action = gtk_action_group_get_action (action_group, "copy");
  g_object_set (action, "sensitive", sensitive, NULL);
  action = gtk_action_group_get_action (action_group, "paste");
  g_object_set (action, "sensitive", sensitive, NULL);

  action = gtk_action_group_get_action (action_group, "toggle-cnp");
  if (sensitive)
84
    g_object_set (action, "label", "Disable Cut and paste ops", NULL);
85
  else
86
    g_object_set (action, "label", "Enable Cut and paste ops", NULL);
87 88 89 90 91 92 93 94 95
}

static void
show_accel_dialog (GtkAction *action)
{
  g_message ("Sorry, accel dialog not available");
}

static void
96
toolbar_style (GtkAction *action)
97 98 99 100
{
  GtkToolbarStyle style;

  g_return_if_fail (toolbar != NULL);
101 102 103 104

  radio_action (action);

  style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
105 106 107 108 109

  gtk_toolbar_set_style (toolbar, style);
}

static void
110
toolbar_size_small (GtkAction *action)
111
{
112
  g_return_if_fail (toolbar != NULL);
113

114 115 116 117 118 119
  gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
}

static void
toolbar_size_large (GtkAction *action)
{
120 121
  g_return_if_fail (toolbar != NULL);

122
  gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
123 124 125
}

/* convenience functions for declaring actions */
126 127 128
static GtkActionEntry entries[] = {
  { "Menu1Action", NULL, "Menu _1" },
  { "Menu2Action", NULL, "Menu _2" },
129
  { "Menu3Action", NULL, "_Dynamic Menu" },
130

131 132
  { "attach", "mail-attachment", "_Attachment...", "<Control>m",
    "Attach a file", G_CALLBACK (activate_action) },
133 134 135 136 137 138 139 140 141 142 143 144 145 146
  { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
    "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
  { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
    "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
  { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
    "Paste the text from the clipboard", G_CALLBACK (activate_action) },
  { "quit", GTK_STOCK_QUIT,  NULL, "<control>Q",
    "Quit the application", G_CALLBACK (gtk_main_quit) },
  { "customise-accels", NULL, "Customise _Accels", NULL,
    "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
  { "toolbar-small-icons", NULL, "Small Icons", NULL, 
    NULL, G_CALLBACK (toolbar_size_small) },
  { "toolbar-large-icons", NULL, "Large Icons", NULL,
    NULL, G_CALLBACK (toolbar_size_large) }
147 148 149
};
static guint n_entries = G_N_ELEMENTS (entries);

150 151 152 153 154 155 156 157 158 159
static GtkToggleActionEntry toggle_entries[] = {
  { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
    "Change to bold face", 
    G_CALLBACK (toggle_action), FALSE },
  { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
    "Change the sensitivity of the cut, copy and paste actions",
    G_CALLBACK (toggle_cnp_actions), TRUE },
};
static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);

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
enum {
  JUSTIFY_LEFT,
  JUSTIFY_CENTER,
  JUSTIFY_RIGHT,
  JUSTIFY_FILL
};

static GtkRadioActionEntry justify_entries[] = {
  { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
    "Left justify the text", JUSTIFY_LEFT },
  { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
    "Center justify the text", JUSTIFY_CENTER },
  { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
    "Right justify the text", JUSTIFY_RIGHT },
  { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
    "Fill justify the text", JUSTIFY_FILL }
};
static guint n_justify_entries = G_N_ELEMENTS (justify_entries);

static GtkRadioActionEntry toolbar_entries[] = {
  { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
  { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
  { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
  { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
};
static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);

187 188 189
/* XML description of the menus for the test app.  The parser understands
 * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
static const gchar *ui_info =
190 191 192 193 194
"  <menubar>\n"
"    <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
"      <menuitem name=\"cut\" action=\"cut\" />\n"
"      <menuitem name=\"copy\" action=\"copy\" />\n"
"      <menuitem name=\"paste\" action=\"paste\" />\n"
195
"      <separator name=\"sep1\" />\n"
196 197
"      <menuitem name=\"bold1\" action=\"bold\" />\n"
"      <menuitem name=\"bold2\" action=\"bold\" />\n"
198
"      <separator name=\"sep2\" />\n"
199
"      <menuitem name=\"recent\" action=\"recent\" />\n"
200
"      <separator name=\"sep3\" />\n"
201 202
"      <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
"      <separator name=\"sep4\" />\n"
203 204 205 206 207 208
"      <menuitem name=\"quit\" action=\"quit\" />\n"
"    </menu>\n"
"    <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
"      <menuitem name=\"cut\" action=\"cut\" />\n"
"      <menuitem name=\"copy\" action=\"copy\" />\n"
"      <menuitem name=\"paste\" action=\"paste\" />\n"
209
"      <separator name=\"sep5\"/>\n"
210 211
"      <menuitem name=\"bold\" action=\"bold\" />\n"
"      <separator name=\"sep6\"/>\n"
212 213 214 215
"      <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
"      <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
"      <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
"      <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
216
"      <separator name=\"sep7\"/>\n"
217 218
"      <menuitem  name=\"customise-accels\" action=\"customise-accels\" />\n"
"      <separator name=\"sep8\"/>\n"
219 220 221 222
"      <menuitem action=\"toolbar-icons\" />\n"
"      <menuitem action=\"toolbar-text\" />\n"
"      <menuitem action=\"toolbar-both\" />\n"
"      <menuitem action=\"toolbar-both-horiz\" />\n"
223
"      <separator name=\"sep9\"/>\n"
224 225 226
"      <menuitem action=\"toolbar-small-icons\" />\n"
"      <menuitem action=\"toolbar-large-icons\" />\n"
"    </menu>\n"
227
"    <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
228 229
"  </menubar>\n"
"  <toolbar name=\"toolbar\">\n"
230
"    <toolitem name=\"attach\" action=\"attach\" />\n"
231 232 233
"    <toolitem name=\"cut\" action=\"cut\" />\n"
"    <toolitem name=\"copy\" action=\"copy\" />\n"
"    <toolitem name=\"paste\" action=\"paste\" />\n"
234
"    <toolitem name=\"recent\" action=\"recent\" />\n"
235
"    <separator name=\"sep10\" />\n"
236 237
"    <toolitem name=\"bold\" action=\"bold\" />\n"
"    <separator name=\"sep11\" />\n"
238 239 240 241
"    <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
"    <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
"    <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
"    <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
242
"    <separator name=\"sep12\"/>\n"
243
"    <toolitem name=\"quit\" action=\"quit\" />\n"
244 245 246 247 248 249
"  </toolbar>\n"
"  <popup name=\"popup\">\n"
"    <menuitem name=\"popcut\" action=\"cut\" />\n"
"    <menuitem name=\"popcopy\" action=\"copy\" />\n"
"    <menuitem name=\"poppaste\" action=\"paste\" />\n"
"  </popup>\n";
250 251

static void
252
add_widget (GtkUIManager *merge,
253 254 255 256
	    GtkWidget   *widget,
	    GtkContainer *container)
{

257
  gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
258 259 260 261 262 263 264 265 266
  gtk_widget_show (widget);

  if (GTK_IS_TOOLBAR (widget)) 
    {
      toolbar = GTK_TOOLBAR (widget);
      gtk_toolbar_set_show_arrow (toolbar, TRUE);
    }
}

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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static guint ui_id = 0;
static GtkActionGroup *dag = NULL;

static void
ensure_update (GtkUIManager *manager)
{
  GTimer *timer;
  double seconds;
  gulong microsecs;
  
  timer = g_timer_new ();
  g_timer_start (timer);
  
  gtk_ui_manager_ensure_update (manager);
  
  g_timer_stop (timer);
  seconds = g_timer_elapsed (timer, &microsecs);
  g_timer_destroy (timer);
  
  g_print ("Time: %fs\n", seconds);
}

static void
add_cb (GtkWidget *button,
	GtkUIManager *manager)
{
  GtkWidget *spinbutton;
  GtkAction *action;
  int i, num;
  char *name, *label;
  
  if (ui_id != 0 || dag != NULL)
    return;
  
  spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
  num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
  
  dag = gtk_action_group_new ("DynamicActions");
  gtk_ui_manager_insert_action_group (manager, dag, 0);
  
  ui_id = gtk_ui_manager_new_merge_id (manager);
  
  for (i = 0; i < num; i++)
    {
      name = g_strdup_printf ("DynAction%u", i);
      label = g_strdup_printf ("Dynamic Item %d", i);
      
      action = g_object_new (GTK_TYPE_ACTION,
			     "name", name,
			     "label", label,
			     NULL);
      gtk_action_group_add_action (dag, action);
      g_object_unref (action);
      
      gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
			     name, name,
			     GTK_UI_MANAGER_MENUITEM, FALSE);
    }
  
  ensure_update (manager);
}

static void
remove_cb (GtkWidget *button,
	   GtkUIManager *manager)
{
  if (ui_id == 0 || dag == NULL)
    return;
  
  gtk_ui_manager_remove_ui (manager, ui_id);
  ensure_update (manager);
  ui_id = 0;
  
  gtk_ui_manager_remove_action_group (manager, dag);
  g_object_unref (dag);
  dag = NULL;
}

345 346 347
static void
create_window (GtkActionGroup *action_group)
{
348
  GtkUIManager *merge;
349 350
  GtkWidget *window;
  GtkWidget *box;
351
  GtkWidget *hbox, *spinbutton, *button;
352 353
  GError *error = NULL;

354 355
  merge = gtk_ui_manager_new ();

356 357 358
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
  gtk_window_set_title (GTK_WINDOW (window), "Action Test");
359
  g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge);
360 361
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

362
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
363 364 365
  gtk_container_add (GTK_CONTAINER (window), box);
  gtk_widget_show (box);

366
  gtk_ui_manager_insert_action_group (merge, action_group, 0);
367 368 369
  g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);

  gtk_window_add_accel_group (GTK_WINDOW (window), 
370
			      gtk_ui_manager_get_accel_group (merge));
371

372
  if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
373 374 375 376 377
    {
      g_message ("building menus failed: %s", error->message);
      g_error_free (error);
    }

378
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
  
  spinbutton = gtk_spin_button_new_with_range (100, 10000, 100);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);
  
  button = gtk_button_new_with_label ("Add");
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);
  
  g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
  g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
  
  button = gtk_button_new_with_label ("Remove");
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);
  
  g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
  
399 400 401 402 403 404
  gtk_widget_show (window);
}

int
main (int argc, char **argv)
{
405 406
  GtkAction *action;

407 408 409 410 411
  gtk_init (&argc, &argv);

  if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
    gtk_accel_map_load ("accels");

412 413 414 415 416 417 418 419 420 421
  action = gtk_recent_action_new ("recent",
                                  "Open Recent", "Open recent files",
                                  NULL);
  g_signal_connect (action, "item-activated",
                    G_CALLBACK (recent_action),
                    NULL);
  g_signal_connect (action, "activate",
                    G_CALLBACK (recent_action),
                    NULL);

422
  action_group = gtk_action_group_new ("TestActions");
423 424 425 426 427 428 429 430 431
  gtk_action_group_add_actions (action_group, 
				entries, n_entries, 
				NULL);
  gtk_action_group_add_toggle_actions (action_group, 
				       toggle_entries, n_toggle_entries, 
				       NULL);
  gtk_action_group_add_radio_actions (action_group, 
				      justify_entries, n_justify_entries, 
				      JUSTIFY_LEFT,
432
				      G_CALLBACK (radio_action), NULL);
433 434 435
  gtk_action_group_add_radio_actions (action_group, 
				      toolbar_entries, n_toolbar_entries, 
				      GTK_TOOLBAR_BOTH,
436
				      G_CALLBACK (toolbar_style), NULL);
437
  gtk_action_group_add_action_with_accel (action_group, action, NULL);
438

439 440 441 442
  create_window (action_group);

  gtk_main ();

443 444 445 446 447 448 449 450 451 452 453 454 455 456
#ifdef DEBUG_UI_MANAGER
  {
    GList *action;

    for (action = gtk_action_group_list_actions (action_group);
	 action; 
	 action = action->next)
      {
	GtkAction *a = action->data;
	g_print ("action %s ref count %d\n", 
		 gtk_action_get_name (a), G_OBJECT (a)->ref_count);
      }
  }
#endif
457 458
  
  g_object_unref (action);
459 460 461 462 463 464
  g_object_unref (action_group);

  gtk_accel_map_save ("accels");

  return 0;
}