/* test-ui-mananager.c : Simple tutorial for GtkUIManager * Copyright (C) 2005 Ryan McDougall, 2008 Double 12. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // cc `pkg-config --cflags --libs gtk+-2.0` test-uimanager.c -o test-uimanager #include /* Create callbacks that implement our Actions */ static void general_action () { g_print ("Menubar item or toolbar button clicked.\n"); } static void radio_action () { g_print ("Radio menubar item clicked.\n"); // you could access the current radio button choice with the gint current_value } static void quit_action () { gtk_main_quit(); } /* Create a list of entries which are passed to the Action constructor. * This is a huge convenience over building Actions by hand. */ static GtkActionEntry entries[] = { { "FileMenuAction", NULL, "_File" }, /* name, stock id, label */ { "EditMenuAction", NULL, "_Edit" }, { "ViewMenuAction", NULL, "_View" }, { "ToolsMenuAction", NULL, "_Tools" }, { "LanguageMenuAction", NULL, "_Language" }, { "HelpMenuAction", NULL, "_Help" }, // File menu { "NewAction", GTK_STOCK_NEW, /* name, stock id */ "_New", "N", /* label, accelerator */ "Start a new document", /* tooltip */ G_CALLBACK (general_action) }, { "OpenAction", GTK_STOCK_OPEN, "_Open","O", "Open a document", G_CALLBACK (general_action) }, { "SaveAction", GTK_STOCK_SAVE, "_Save","S", "Save the current document", G_CALLBACK (general_action) }, { "SaveAsAction", GTK_STOCK_SAVE_AS, "Save _As","S", "Save the current document under a new name", G_CALLBACK (general_action) }, { "SaveAllAction", GTK_STOCK_SAVE, "Save A_ll",NULL, "Save all documents", G_CALLBACK (general_action) }, { "PrintAction", GTK_STOCK_PRINT, "_Print","P", "Print the current document", G_CALLBACK (general_action) }, { "PropertiesAction", GTK_STOCK_PROPERTIES, "_Properties", NULL, "Show the properties of the current document", G_CALLBACK (general_action) }, { "CloseAction", GTK_STOCK_CLOSE, "_Close","W", "Close the current document", G_CALLBACK (general_action) }, { "CloseAllAction", GTK_STOCK_CLOSE, "Clo_se All",NULL, "Close all documents", G_CALLBACK (general_action) }, { "QuitAction", GTK_STOCK_QUIT, "_Quit", "Q", "Quit Beaver", G_CALLBACK (quit_action) }, // Edit menu { "UndoAction", GTK_STOCK_UNDO, "_Undo","Z", "Undo the last action", G_CALLBACK (general_action) }, { "RedoAction", GTK_STOCK_REDO, "_Redo","Z", "Redo the last action", G_CALLBACK (general_action) }, { "CutAction", GTK_STOCK_CUT, "Cu_t","X", "Cut the current selection", G_CALLBACK (general_action) }, { "CopyAction", GTK_STOCK_COPY, "_Copy","C", "Copy the current selection", G_CALLBACK (general_action) }, { "PasteAction", GTK_STOCK_PASTE, "_Paste","V", "Paste the copied or cutted text here", G_CALLBACK (general_action) }, { "SelectAllAction", NULL, "Select _All", "A", "Select everything in the whole document", G_CALLBACK (general_action) }, { "CompleteAction", GTK_STOCK_CLOSE, "Co_mplete","space", "Complete the current command or code tag", G_CALLBACK (general_action) }, { "ToggleReadonlyAction", NULL, "Toggle Rea_donly",NULL, "Toggle if this document is writable or not", G_CALLBACK (general_action) }, { "FindAction", GTK_STOCK_FIND, "_Find", "F", "Find a certain phrase in this file", G_CALLBACK (general_action) }, { "ReplaceAction", GTK_STOCK_FIND_AND_REPLACE, "R_eplace","R", "Find a certain phrase in this file and replace it with another one", G_CALLBACK (general_action) }, { "GoToLineAction", GTK_STOCK_JUMP_TO, "_Go to Line",NULL, "Change the cursor to a given line number", G_CALLBACK (general_action) }, { "PreferencesAction", GTK_STOCK_PREFERENCES, "Prefere_nces", NULL, "Change the preferences", G_CALLBACK (general_action) }, // View menu { "DocTabsAction", NULL, "_Doc Tabs", NULL, "Choose the placement of the document tabs", NULL }, { "ScrollbarAction", NULL, "Sc_rollbar", NULL, "Choose the placement of the scrollbar", NULL }, // Tools menu { "FormatAction", NULL, "_Format", NULL, "Convert the text to a different format", NULL }, { "ConvertThisToDosAction", GTK_STOCK_CONVERT, "Convert This to _DOS",NULL, "Convert this text to DOS formatting", G_CALLBACK (general_action) }, { "ConvertThisToMacAction", GTK_STOCK_CONVERT, "Convert This to _MAC",NULL, "Convert this text to MAC formatting", G_CALLBACK (general_action) }, { "ConvertThisToUnixAction", GTK_STOCK_CONVERT, "Convert This to _UNIX",NULL, "Convert this text to UNIX formatting", G_CALLBACK (general_action) }, { "ConvertAllToDosAction", GTK_STOCK_CONVERT, "Convert All to DOS",NULL, "Convert all text to DOS formatting", G_CALLBACK (general_action) }, { "ConvertAllToMacAction", GTK_STOCK_CONVERT, "Convert All to MAC",NULL, "Convert all text to MAC formatting", G_CALLBACK (general_action) }, { "ConvertAllToUnixAction", GTK_STOCK_CONVERT, "Convert All to UNIX",NULL, "Convert all text to UNIX formatting", G_CALLBACK (general_action) }, { "BaseConverterAction", GTK_STOCK_CONVERT, "_Base Converter",NULL, "Base Converter", G_CALLBACK (general_action) }, { "ColorPickerAction", GTK_STOCK_SELECT_COLOR, "_Color Picker",NULL, "Pick a color from the palette", G_CALLBACK (general_action) }, { "InsertTimeAction", NULL, "Insert _Time",NULL, "Insert the current time into the document", G_CALLBACK (general_action) }, { "ToUpperCaseAction", NULL, "To _Upper Case",NULL, "Convert text to upper case", G_CALLBACK (general_action) }, { "ToLowerCaseAction", NULL, "To _Lower Case",NULL, "Convert text to lower case", G_CALLBACK (general_action) }, { "CapitalizeAction", NULL, "Ca_pitalize",NULL, "Convert text to capitals", G_CALLBACK (general_action) }, { "InvertCaseAction", NULL, "In_vert Case",NULL, "Invert case of the text", G_CALLBACK (general_action) }, // Language menu { "LanguageMenuAdditions", NULL, NULL,NULL, NULL, NULL}, // Help menu { "ContentsAction", GTK_STOCK_HELP, "_Contents","F1", "Show the help contents", G_CALLBACK (general_action) }, { "AboutAction", NULL, "_About", NULL, "About this program", G_CALLBACK (general_action) }, }; /* Toggle items */ static const GtkToggleActionEntry toggle_entries[] = { { "ToolbarAction", NULL, "_Toolbar", NULL, "Show/do not show the toolbar", G_CALLBACK (general_action),FALSE }, { "StatusbarAction", NULL, "_Statusbar",NULL, "Show/do not show the statusbar", G_CALLBACK (general_action) }, { "WordWrapAction", NULL, "_Word-wrap", NULL, "Word-wrap on/off", G_CALLBACK (general_action) }, }; /* Radio items */ static const GtkRadioActionEntry radio_entries[] = { { "DocTabsTopAction", NULL, "_Top",NULL, "Put the document tabs at the top", 0 }, { "DocTabsBottomAction", NULL, "_Bottom",NULL, "Put the document tabs at the bottom", 1 }, { "DocTabsLeftAction", NULL, "_Left", NULL, "Put the document tabs at the left", 2 }, { "DocTabsRightAction", NULL, "_Right",NULL, "Put the document tabs at the right", 3 }, { "ScrollbarLeftAction", NULL, "_Left",NULL, "Put the scrollbar at the left", 0 }, { "ScrollbarRightAction", NULL, "_Right", NULL, "Put the scrollbar at the right", 1 }, }; static guint n_entries = G_N_ELEMENTS (entries); static guint n_radio_entries = G_N_ELEMENTS (radio_entries); static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries); /*---------------------------------------------------------------------------*/ int main (int argc, char** argv) { GtkWidget *window; /* The main window */ GtkWidget *menu_box; /* Packing box for the menu and toolbars */ GtkActionGroup *action_group; /* Packing group for our Actions */ GtkUIManager *menu_manager; /* The magic widget! */ GError *error; /* For reporting exceptions or errors */ GtkWidget *menubar; /* The actual menubar */ GtkWidget *toolbar; /* The actual toolbar */ /* Always the first step is to start up GTK+ itself */ gtk_init (&argc, &argv); /* Create our objects */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); menu_box = gtk_vbox_new (FALSE, 0); action_group = gtk_action_group_new ("TestActions"); gtk_action_group_set_translation_domain (action_group, "blah"); menu_manager = gtk_ui_manager_new (); /* Pack up our objects: * menu_box -> window * actions -> action_group * action_group -> menu_manager */ gtk_container_add (GTK_CONTAINER(window), menu_box); gtk_action_group_add_actions (action_group, entries, n_entries, NULL); gtk_action_group_add_radio_actions (action_group, radio_entries, n_radio_entries, 0, radio_action, NULL); //radio-menu-items gtk_action_group_add_toggle_actions (action_group, toggle_entries, n_toggle_entries, NULL); //toggle-menu-items gtk_ui_manager_insert_action_group (menu_manager, action_group, 0); /* Read in the UI from our XML file */ error = NULL; gtk_ui_manager_add_ui_from_file (menu_manager, "test-ui-manager-ui.xml", &error); if (error) { g_message ("building menus failed: %s", error->message); g_error_free (error); } /* Connect up important signals */ g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); /* Get the menubar and the toolbar and put them in the vertical packing box */ menubar = gtk_ui_manager_get_widget (menu_manager, "/MainMenu"); gtk_box_pack_start (GTK_BOX (menu_box), menubar, FALSE, FALSE, 0); toolbar = gtk_ui_manager_get_widget (menu_manager, "/MainToolbar"); gtk_box_pack_start (GTK_BOX (menu_box), toolbar, FALSE, FALSE, 0); /* Make sure that the accelerators work */ gtk_window_add_accel_group (GTK_WINDOW (window), gtk_ui_manager_get_accel_group (menu_manager)); /* Show the window and run the main loop, we're done! */ gtk_widget_show_all (window); gtk_main (); return 0; }