home *** CD-ROM | disk | FTP | other *** search
/ Freelog 65 / Freelog065.iso / BAS / Bureautique / Gnumeric / gnumeric-1.3.92-rc1.exe / ui_manager.c < prev    next >
C/C++ Source or Header  |  2004-10-15  |  9KB  |  236 lines

  1. /* UI Manager
  2.  *
  3.  * The GtkUIManager object allows the easy creation of menus
  4.  * from an array of actions and a description of the menu hierarchy.
  5.  */
  6.  
  7. #include <config.h>
  8. #include <gtk/gtk.h>
  9.  
  10. static void
  11. activate_action (GtkAction *action)
  12. {
  13.   g_message ("Action \"%s\" activated", gtk_action_get_name (action));
  14. }
  15.  
  16. static void
  17. activate_radio_action (GtkAction *action, GtkRadioAction *current)
  18. {
  19.   g_message ("Radio action \"%s\" selected", 
  20.          gtk_action_get_name (GTK_ACTION (current)));
  21. }
  22.  
  23. static GtkActionEntry entries[] = {
  24.   { "FileMenu", NULL, "_File" },               /* name, stock id, label */
  25.   { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
  26.   { "ColorMenu", NULL, "_Color"  },            /* name, stock id, label */
  27.   { "ShapeMenu", NULL, "_Shape" },             /* name, stock id, label */
  28.   { "HelpMenu", NULL, "_Help" },               /* name, stock id, label */
  29.   { "New", GTK_STOCK_NEW,                      /* name, stock id */
  30.     "_New", "<control>N",                      /* label, accelerator */
  31.     "Create a new file",                       /* tooltip */ 
  32.     G_CALLBACK (activate_action) },      
  33.   { "Open", GTK_STOCK_OPEN,                    /* name, stock id */
  34.     "_Open","<control>O",                      /* label, accelerator */     
  35.     "Open a file",                             /* tooltip */
  36.     G_CALLBACK (activate_action) }, 
  37.   { "Save", GTK_STOCK_SAVE,                    /* name, stock id */
  38.     "_Save","<control>S",                      /* label, accelerator */     
  39.     "Save current file",                       /* tooltip */
  40.     G_CALLBACK (activate_action) },
  41.   { "SaveAs", GTK_STOCK_SAVE,                  /* name, stock id */
  42.     "Save _As...", NULL,                       /* label, accelerator */     
  43.     "Save to a file",                          /* tooltip */
  44.     G_CALLBACK (activate_action) },
  45.   { "Quit", GTK_STOCK_QUIT,                    /* name, stock id */
  46.     "_Quit", "<control>Q",                     /* label, accelerator */     
  47.     "Quit",                                    /* tooltip */
  48.     G_CALLBACK (activate_action) },
  49.   { "About", NULL,                             /* name, stock id */
  50.     "_About", "<control>A",                    /* label, accelerator */     
  51.     "About",                                   /* tooltip */  
  52.     G_CALLBACK (activate_action) },
  53.   { "Logo", "demo-gtk-logo",                   /* name, stock id */
  54.      NULL, NULL,                               /* label, accelerator */     
  55.     "GTK+",                                    /* tooltip */
  56.     G_CALLBACK (activate_action) },
  57. };
  58. static guint n_entries = G_N_ELEMENTS (entries);
  59.  
  60.  
  61. static GtkToggleActionEntry toggle_entries[] = {
  62.   { "Bold", GTK_STOCK_BOLD,                    /* name, stock id */
  63.      "_Bold", "<control>B",                    /* label, accelerator */     
  64.     "Bold",                                    /* tooltip */
  65.     G_CALLBACK (activate_action), 
  66.     TRUE },                                    /* is_active */
  67. };
  68. static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
  69.  
  70. enum {
  71.   COLOR_RED,
  72.   COLOR_GREEN,
  73.   COLOR_BLUE
  74. };
  75.  
  76. static GtkRadioActionEntry color_entries[] = {
  77.   { "Red", NULL,                               /* name, stock id */
  78.     "_Red", "<control>R",                      /* label, accelerator */     
  79.     "Blood", COLOR_RED },                      /* tooltip, value */
  80.   { "Green", NULL,                             /* name, stock id */
  81.     "_Green", "<control>G",                    /* label, accelerator */     
  82.     "Grass", COLOR_GREEN },                    /* tooltip, value */
  83.   { "Blue", NULL,                              /* name, stock id */
  84.     "_Blue", "<control>B",                     /* label, accelerator */     
  85.     "Sky", COLOR_BLUE },                       /* tooltip, value */
  86. };
  87. static guint n_color_entries = G_N_ELEMENTS (color_entries);
  88.  
  89. enum {
  90.   SHAPE_SQUARE,
  91.   SHAPE_RECTANGLE,
  92.   SHAPE_OVAL
  93. };
  94.  
  95. static GtkRadioActionEntry shape_entries[] = {
  96.   { "Square", NULL,                            /* name, stock id */
  97.     "_Square", "<control>S",                   /* label, accelerator */     
  98.     "Square",  SHAPE_SQUARE },                 /* tooltip, value */
  99.   { "Rectangle", NULL,                         /* name, stock id */
  100.     "_Rectangle", "<control>R",                /* label, accelerator */     
  101.     "Rectangle", SHAPE_RECTANGLE },            /* tooltip, value */
  102.   { "Oval", NULL,                              /* name, stock id */
  103.     "_Oval", "<control>O",                     /* label, accelerator */     
  104.     "Egg", SHAPE_OVAL },                       /* tooltip, value */  
  105. };
  106. static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
  107.  
  108. static const gchar *ui_info = 
  109. "<ui>"
  110. "  <menubar name='MenuBar'>"
  111. "    <menu action='FileMenu'>"
  112. "      <menuitem action='New'/>"
  113. "      <menuitem action='Open'/>"
  114. "      <menuitem action='Save'/>"
  115. "      <menuitem action='SaveAs'/>"
  116. "      <separator/>"
  117. "      <menuitem action='Quit'/>"
  118. "    </menu>"
  119. "    <menu action='PreferencesMenu'>"
  120. "      <menu action='ColorMenu'>"
  121. "    <menuitem action='Red'/>"
  122. "    <menuitem action='Green'/>"
  123. "    <menuitem action='Blue'/>"
  124. "      </menu>"
  125. "      <menu action='ShapeMenu'>"
  126. "        <menuitem action='Square'/>"
  127. "        <menuitem action='Rectangle'/>"
  128. "        <menuitem action='Oval'/>"
  129. "      </menu>"
  130. "      <menuitem action='Bold'/>"
  131. "    </menu>"
  132. "    <menu action='HelpMenu'>"
  133. "      <menuitem action='About'/>"
  134. "    </menu>"
  135. "  </menubar>"
  136. "  <toolbar  name='ToolBar'>"
  137. "    <toolitem action='Open'/>"
  138. "    <toolitem action='Quit'/>"
  139. "    <separator action='Sep1'/>"
  140. "    <toolitem action='Logo'/>"
  141. "  </toolbar>"
  142. "</ui>";
  143.  
  144. GtkWidget *
  145. do_ui_manager (GtkWidget *do_widget)
  146. {
  147.   static GtkWidget *window = NULL;
  148.   
  149.   if (!window)
  150.     {
  151.       GtkWidget *box1;
  152.       GtkWidget *box2;
  153.       GtkWidget *separator;
  154.       GtkWidget *label;
  155.       GtkWidget *button;
  156.       GtkUIManager *ui;
  157.       GtkActionGroup *actions;
  158.       GError *error = NULL;
  159.  
  160.       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  161.       gtk_window_set_screen (GTK_WINDOW (window),
  162.                  gtk_widget_get_screen (do_widget));
  163.       
  164.       g_signal_connect (window, "destroy",
  165.             G_CALLBACK (gtk_widget_destroyed), &window);
  166.       g_signal_connect (window, "delete-event",
  167.             G_CALLBACK (gtk_true), NULL);
  168.  
  169.       actions = gtk_action_group_new ("Actions");
  170.       gtk_action_group_add_actions (actions, entries, n_entries, NULL);
  171.       gtk_action_group_add_toggle_actions (actions, 
  172.                        toggle_entries, n_toggle_entries, 
  173.                        NULL);
  174.       gtk_action_group_add_radio_actions (actions, 
  175.                       color_entries, n_color_entries, 
  176.                       COLOR_RED,
  177.                       G_CALLBACK (activate_radio_action), 
  178.                       NULL);
  179.       gtk_action_group_add_radio_actions (actions, 
  180.                       shape_entries, n_shape_entries, 
  181.                       SHAPE_OVAL,
  182.                       G_CALLBACK (activate_radio_action), 
  183.                       NULL);
  184.  
  185.       ui = gtk_ui_manager_new ();
  186.       gtk_ui_manager_insert_action_group (ui, actions, 0);
  187.       gtk_window_add_accel_group (GTK_WINDOW (window), 
  188.                   gtk_ui_manager_get_accel_group (ui));
  189.       gtk_window_set_title (GTK_WINDOW (window), "UI Manager");
  190.       gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  191.       
  192.       if (!gtk_ui_manager_add_ui_from_string (ui, ui_info, -1, &error))
  193.     {
  194.       g_message ("building menus failed: %s", error->message);
  195.       g_error_free (error);
  196.     }
  197.  
  198.       box1 = gtk_vbox_new (FALSE, 0);
  199.       gtk_container_add (GTK_CONTAINER (window), box1);
  200.       
  201.       gtk_box_pack_start (GTK_BOX (box1),
  202.               gtk_ui_manager_get_widget (ui, "/MenuBar"),
  203.               FALSE, FALSE, 0);
  204.  
  205.       label = gtk_label_new ("Type\n<alt>\nto start");
  206.       gtk_widget_set_size_request (label, 200, 200);
  207.       gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  208.       gtk_box_pack_start (GTK_BOX (box1), label, TRUE, TRUE, 0);
  209.  
  210.  
  211.       separator = gtk_hseparator_new ();
  212.       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  213.  
  214.  
  215.       box2 = gtk_vbox_new (FALSE, 10);
  216.       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  217.       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  218.  
  219.       button = gtk_button_new_with_label ("close");
  220.       g_signal_connect_swapped (button, "clicked",
  221.                 G_CALLBACK (gtk_widget_destroy), window);
  222.       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  223.       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  224.       gtk_widget_grab_default (button);
  225.  
  226.       gtk_widget_show_all (window);
  227.     }
  228.   else
  229.     {
  230.       gtk_widget_destroy (window);
  231.       window = NULL;
  232.     }
  233.  
  234.   return window;
  235. }
  236.