The libgnomeui library is the library you'll be using most, it includes the basic UI framework of your application and other X and GTK+ specific things, such as session management, and many utility widgets. It also contains the GnomeCanvas widget, which deserves separate treatment. This is the library that makes the programmers life easy. With plain GTK+ you have to do a lot of things by yourself, reinventing the wheel every time, but this library takes care of the UI setup for you and still allows the user to configure that behavior and have it be consistent over different applications.
GnomeApp is the basic widget behind each app. It is the main window of the application, containing the document being worked on and the applications menus, tool-bars and status bars. It also remembers the docked positions of menu bars and tool-bars and such for you so that the user gets the window the way he left it when he left the application last time.
Creating a new GnomeApp widget is as easy as calling gnome_app_new with the application name, which is usually the name of the executable or something else that is unique to your application and the title of the main window. Then you create the content of the main window and add it to the GnomeApp widget by calling gnome_app_set_contents with your contents as the argument.
Adding menu-bars, tool-bars and status-bars is equally easy, you call gnome_app_set_toolbar, gnome_app_set_menus or gnome_app_set_statusbar. gnome_app_set_toolbar is for simple applications that have only one tool-bar, for more complicated applications you need to use gnome_app_add_toolbar, which allows you to add as many docked tool-bars as you need.
Most of the time, you don't really want to create your menu-bars and tool-bars by yourself. You can use functions from libgnomeui/gnome-app-helper.h to construct menus and tool-bars for you. All you need is to fill in a couple of structures with the your information, and call gnome_app_create_menus or gnome_app_create_toolbar with that structure and voila, your application has menus and tool-bars. Sometimes you wish to pass a data pointer to all the callbacks from those structures to work with, then you'd use the gnome_app_create_toolbar_with_data and gnome_app_create_menus_with_data, and pass an extra parameter which will be passed in the data field of the callbacks.
Here is the definition of the structure you need to fill (actually you fill in an array of such structures). Also note I included the enums that you will need to fill that structure.
/* These values identify the type of pixmap used in an item */ typedef enum { GNOME_APP_PIXMAP_NONE, /* No pixmap specified */ GNOME_APP_PIXMAP_STOCK, /* Use a stock pixmap (GnomeStock) */ GNOME_APP_PIXMAP_DATA, /* Use a pixmap from inline xpm data */ GNOME_APP_PIXMAP_FILENAME /* Use a pixmap from the specified filename */ } GnomeUIPixmapType; /* This is the structure that defines an item in a menu bar * or tool-bar. The idea is to create an array of such * structures with the information needed to create menus or * tool-bars. The most convenient way to create such a structure * is to use the GNOMEUIINFO_* macros provided below. */ typedef struct { GnomeUIInfoType type; /* Type of item */ gchar *label; /* String to use in the label */ gchar *hint; /* For tool-bar items, the tool-tip. For menu items, the status bar message */ gpointer moreinfo; /* For an item, toggle-item, or radio-item, this is a pointer to the function to call when the item is activated. For a subtree, a pointer to another array of GnomeUIInfo structures. For a radio-item lead entry, a pointer to an array of GnomeUIInfo structures for the radio item group. For a help item, specifies the help node to load (i.e. the application's identifier) or NULL for the main program's name. For builder data, points to the GnomeUIBuilderData structure for the following items */ gpointer user_data; /* Data pointer to pass to callbacks */ gpointer unused_data; /* Reserved for future expansion, should be NULL */ GnomeUIPixmapType pixmap_type; /* Type of pixmap for the item */ gpointer pixmap_info; /* Pointer to the pixmap * information: * * For GNOME_APP_PIXMAP_STOCK, a * pointer to the stock icon name. * * For GNOME_APP_PIXMAP_DATA, a * pointer to the inline xpm data. * * For GNOME_APP_PIXMAP_FILENAME, a * pointer to the filename string. */ guint accelerator_key; /* Accelerator key, or 0 for none */ GdkModifierType ac_mods; /* Mask of modifier keys for the accelerator */ GtkWidget *widget; /* Filled in by gnome_app_create*, you can use this to tweak the widgets once they have been created */ } GnomeUIInfo; |
Most of the time, menu entries are very simple, so one can just use one of the simple macros provided. For example, for the end of a menu, one would use the GNOMEUIINFO_END macro, for a separator one uses the GNOMEUIINFO_SEPARATOR macro. Now for the actual items there are also macros, which require you to fill in less info. For example if you have an item that you provide an xpm format data for, you can use the GNOMEUIINFO_ITEM(label, tooltip, callback, xpm_data) macro, where label is the text of the label, tool-tip is the tool-tip that the user gets when he goes over that item (or it can be NULL), callback is the function that gets called when the user presses that item, and the xpm_data is a pointer to an xpm data you want to use as the icon. If you have no icon you can just use the GNOMEUIINFO_ITEM_NONE(label, tooltip, callback) macro. If what you are adding is a standard item for which there is a stock icon (we'll talk about those next), you can use the GNOMEUIINFO_ITEM_STOCK(label, tooltip, callback, stock_id) macro where the stock_id is the id of the stock icon you want to use. Then for your main menu bar, or to put sub-menus inside your menus, you can use GNOMEUIINFO_SUBTREE(label, tree) and GNOMEUIINFO_SUBTREE_STOCK(label, tree, stock_id), where the tree is the array of GnomeUIInfo structures that you want to use as that sub-menu. There are a few other macros, but most of the time you will get by with just these macros, so you don't need to learn the entire structure of the GnomeUIInfo.
Just about all application contain a couple of standard menu items, so to keep things more consistent there are a bunch of macros that fill in everything for you except for the callback function and the data. The advantage of using the macros is consistency across applications, user customization, and translation.
Most of these macros have the form: GNOMEUIINFO_MENU_<name>_ITEM (callback, data). However, there is an exception, the "New xxx" item. The GNOME styleguide Requires that you put what the "New" thing is into the item name. Not to mention that it will have a different hint as well. So the "New xxx" item has the structure of: GNOMEUIINFO_MENU_NEW_ITEM(label, hint, callback, data). The "label" should start with "New ". Also note that if you have more new items, you need to use the "New" subtree macro, which is explained later.
the File menu
"New" menu item (you need to provide label and hint yourself here)
"Open" menu item
"Save" menu item
"Save as..." menu item
"Revert" menu item
"Print" menu item
"Print Setup" menu item
"Close" menu item
"Exit" menu item
the Edit menu
"Cut" menu item
"Copy" menu item
"Paste" menu item
"Select" menu item
"Clear" menu item
"Undo" menu item
"Redo" menu item
"Find" menu item
"Find Again" menu item
"Replace" menu item
"Properties" menu item
the Settings menu
"Preferences" menu item
the Windows menu
"New window" menu item
"Close window" menu item
the Help menu
"About" menu item
the Game menu
"New game" menu item
"Pause game" menu item
"Restart game" menu item
"Undo move" menu item
"Redo move" menu item
"Hint" menu item
"Scores" menu item
"End game" menu item
We have already entioned a "New" subtree. For this you should use the GNOMEUIINFO_MENU_NEW_SUBTREE (tree) macro, where the tree argument is another GnomeUIInfo structure array of the different new items.
There are also the standard top level menus. Again you pass the array of GnomeUIInfo structures to the macro.
"File" menu
"Edit" menu
"View" menu
"Settings" menu
"Files" menu
"Windows" menu
"Help" menu
"Game" menu
Sometimes you may want to refer to menu path of these menus, such as for adding items to a "Windows" menu. For this you should use the macros of the form GNOME_MENU_<name>_STRING and GNOME_MENU_<name>_PATH. These will expand to the appropriate string. The macro ending with _STRING will expand to just the menu name, and the macro ending with _PATH to the menu name followed by a "/". The <name> can be one of the following: FILE, EDIT, VIEW, SETTINGS, NEW, FILES or WINDOWS.
Your application should contain a help menu, the help menu can be defined as:
GNOMEUIINFO_HELP("app_name"), GNOMEUIINFO_MENU_ABOUT_ITEM(callback, data), GNOMEUIINFO_END |
Here is a very simple application that makes use of these:
/* * A simple Gnome program, outside of GNOME tree, not using i18n * uiinfo.c */ /* the very basic gnome include */ #include <gnome.h> /* a callback for the buttons */ static void a_callback(GtkWidget *button, gpointer data) { /*just print a string so that we know we got there*/ g_print("Inside Callback\\n"); } GnomeUIInfo file_menu[] = { GNOMEUIINFO_MENU_EXIT_ITEM(gtk_main_quit,NULL), GNOMEUIINFO_END }; GnomeUIInfo some_menu[] = { GNOMEUIINFO_ITEM_NONE("_Menuitem","Just a menuitem", a_callback), GNOMEUIINFO_SEPARATOR, GNOMEUIINFO_ITEM_NONE("M_enuitem2","Just a menuitem", a_callback), GNOMEUIINFO_END }; GnomeUIInfo menubar[] = { GNOMEUIINFO_MENU_FILE_TREE(file_menu), GNOMEUIINFO_SUBTREE("_Some menu",some_menu), GNOMEUIINFO_END }; GnomeUIInfo toolbar[] = { GNOMEUIINFO_ITEM_STOCK("Exit","Exit the application", gtk_main_quit, GNOME_STOCK_PIXMAP_EXIT), GNOMEUIINFO_END }; int main(int argc, char *argv[]) { GtkWidget *app; GtkWidget *button; GtkWidget *hbox; GtkWidget *label; /* Initialize GNOME, this is very similar to gtk_init */ gnome_init ("menu-basic-example", "0.1", argc, argv); /* Create a Gnome app widget, which sets up a basic window for your application */ app = gnome_app_new ("menu-basic-example", "Basic GNOME Application"); /* bind "delete_event", which is the event we get when the user closes the window with the window manager, to gtk_main_quit, which is a function that causes the gtk_main loop to exit, and consequently to quit the application */ gtk_signal_connect (GTK_OBJECT (app), "delete_event", GTK_SIGNAL_FUNC (gtk_main_quit), NULL); /*make a label as the contents*/ label = gtk_label_new("BLAH BLAH BLAH BLAH BLAH"); /*add the label as contents of the window*/ gnome_app_set_contents (GNOME_APP (app), label); /*create the menus for the application*/ gnome_app_create_menus (GNOME_APP (app), menubar); /*create the tool-bar for the application*/ gnome_app_create_toolbar (GNOME_APP (app), toolbar); /* show everything inside this app widget and the app widget itself */ gtk_widget_show_all(app); /* enter the main loop */ gtk_main (); return 0; } |
You have probably noticed the underlines in the labels for the menu items, these specify the accelerators for that menu. That's really all you need to do to add accelerators for menu items. The way accelerators work is very similar to the other windowing systems out there, alt-<key> if you are not browsing the menus or just the <key> if you have the menu open.
Since most of the time you will want to use standard buttons and menu items (such as Open or Save as...), and you want to provide icons with the menu items or tool-bar buttons or just dialog buttons, to make it easier to navigate, you can use some of the predefined icons from gnome-libs. These are called Stock Icons. You have already seen an example of how to use stock menu icons and regular stock icons in menus and tool-bars (you just use the proper define from libgnomeui/gnome-stock.h). There are also stock buttons, where you can get back a button widget based on a stock description.
Here is a list of the normal gnome stock icons, these are regular sized for use in tool-bars and other places where you need a normal sized icon. They are given as defines of string constants and their meaning should be obvious.
#define GNOME_STOCK_PIXMAP_NEW "New" #define GNOME_STOCK_PIXMAP_OPEN "Open" #define GNOME_STOCK_PIXMAP_CLOSE "Close" #define GNOME_STOCK_PIXMAP_REVERT "Revert" #define GNOME_STOCK_PIXMAP_SAVE "Save" #define GNOME_STOCK_PIXMAP_SAVE_AS "Save As" #define GNOME_STOCK_PIXMAP_CUT "Cut" #define GNOME_STOCK_PIXMAP_COPY "Copy" #define GNOME_STOCK_PIXMAP_PASTE "Paste" #define GNOME_STOCK_PIXMAP_PROPERTIES "Properties" #define GNOME_STOCK_PIXMAP_PREFERENCES "Preferences" #define GNOME_STOCK_PIXMAP_HELP "Help" #define GNOME_STOCK_PIXMAP_SCORES "Scores" #define GNOME_STOCK_PIXMAP_PRINT "Print" #define GNOME_STOCK_PIXMAP_SEARCH "Search" #define GNOME_STOCK_PIXMAP_SRCHRPL "Search/Replace" #define GNOME_STOCK_PIXMAP_BACK "Back" #define GNOME_STOCK_PIXMAP_FORWARD "Forward" #define GNOME_STOCK_PIXMAP_FIRST "First" #define GNOME_STOCK_PIXMAP_LAST "Last" #define GNOME_STOCK_PIXMAP_HOME "Home" #define GNOME_STOCK_PIXMAP_STOP "Stop" #define GNOME_STOCK_PIXMAP_REFRESH "Refresh" #define GNOME_STOCK_PIXMAP_UNDO "Undo" #define GNOME_STOCK_PIXMAP_REDO "Redo" #define GNOME_STOCK_PIXMAP_TIMER "Timer" #define GNOME_STOCK_PIXMAP_TIMER_STOP "Timer Stopped" #define GNOME_STOCK_PIXMAP_MAIL "Mail" #define GNOME_STOCK_PIXMAP_MAIL_RCV "Receive Mail" #define GNOME_STOCK_PIXMAP_MAIL_SND "Send Mail" #define GNOME_STOCK_PIXMAP_MAIL_RPL "Reply to Mail" #define GNOME_STOCK_PIXMAP_MAIL_FWD "Forward Mail" #define GNOME_STOCK_PIXMAP_MAIL_NEW "New Mail" #define GNOME_STOCK_PIXMAP_TRASH "Trash" #define GNOME_STOCK_PIXMAP_TRASH_FULL "Trash Full" #define GNOME_STOCK_PIXMAP_UNDELETE "Undelete" #define GNOME_STOCK_PIXMAP_SPELLCHECK "Spellchecker" #define GNOME_STOCK_PIXMAP_MIC "Microphone" #define GNOME_STOCK_PIXMAP_LINE_IN "Line In" #define GNOME_STOCK_PIXMAP_CDROM "Cdrom" #define GNOME_STOCK_PIXMAP_VOLUME "Volume" #define GNOME_STOCK_PIXMAP_BOOK_RED "Book Red" #define GNOME_STOCK_PIXMAP_BOOK_GREEN "Book Green" #define GNOME_STOCK_PIXMAP_BOOK_BLUE "Book Blue" #define GNOME_STOCK_PIXMAP_BOOK_YELLOW "Book Yellow" #define GNOME_STOCK_PIXMAP_BOOK_OPEN "Book Open" #define GNOME_STOCK_PIXMAP_ABOUT "About" #define GNOME_STOCK_PIXMAP_QUIT "Quit" #define GNOME_STOCK_PIXMAP_MULTIPLE "Multiple" #define GNOME_STOCK_PIXMAP_NOT "Not" #define GNOME_STOCK_PIXMAP_CONVERT "Convert" #define GNOME_STOCK_PIXMAP_JUMP_TO "Jump To" #define GNOME_STOCK_PIXMAP_UP "Up" #define GNOME_STOCK_PIXMAP_DOWN "Down" #define GNOME_STOCK_PIXMAP_TOP "Top" #define GNOME_STOCK_PIXMAP_BOTTOM "Bottom" #define GNOME_STOCK_PIXMAP_ATTACH "Attach" #define GNOME_STOCK_PIXMAP_INDEX "Index" #define GNOME_STOCK_PIXMAP_FONT "Font" #define GNOME_STOCK_PIXMAP_EXEC "Exec" #define GNOME_STOCK_PIXMAP_ALIGN_LEFT "Left" #define GNOME_STOCK_PIXMAP_ALIGN_RIGHT "Right" #define GNOME_STOCK_PIXMAP_ALIGN_CENTER "Center" #define GNOME_STOCK_PIXMAP_ALIGN_JUSTIFY "Justify" #define GNOME_STOCK_PIXMAP_TEXT_BOLD "Bold" #define GNOME_STOCK_PIXMAP_TEXT_ITALIC "Italic" #define GNOME_STOCK_PIXMAP_TEXT_UNDERLINE "Underline" #define GNOME_STOCK_PIXMAP_TEXT_STRIKEOUT "Strikeout" #define GNOME_STOCK_PIXMAP_EXIT GNOME_STOCK_PIXMAP_QUIT |
For menus you want to use the _MENU_ variety of the stock pixmaps. These are smaller and these should be the ones you use for the stock menu items in your GnomeUIInfo definitions.
#define GNOME_STOCK_MENU_BLANK "Menu_" #define GNOME_STOCK_MENU_NEW "Menu_New" #define GNOME_STOCK_MENU_SAVE "Menu_Save" #define GNOME_STOCK_MENU_SAVE_AS "Menu_Save As" #define GNOME_STOCK_MENU_REVERT "Menu_Revert" #define GNOME_STOCK_MENU_OPEN "Menu_Open" #define GNOME_STOCK_MENU_CLOSE "Menu_Close" #define GNOME_STOCK_MENU_QUIT "Menu_Quit" #define GNOME_STOCK_MENU_CUT "Menu_Cut" #define GNOME_STOCK_MENU_COPY "Menu_Copy" #define GNOME_STOCK_MENU_PASTE "Menu_Paste" #define GNOME_STOCK_MENU_PROP "Menu_Properties" #define GNOME_STOCK_MENU_PREF "Menu_Preferences" #define GNOME_STOCK_MENU_ABOUT "Menu_About" #define GNOME_STOCK_MENU_SCORES "Menu_Scores" #define GNOME_STOCK_MENU_UNDO "Menu_Undo" #define GNOME_STOCK_MENU_REDO "Menu_Redo" #define GNOME_STOCK_MENU_PRINT "Menu_Print" #define GNOME_STOCK_MENU_SEARCH "Menu_Search" #define GNOME_STOCK_MENU_SRCHRPL "Menu_Search/Replace" #define GNOME_STOCK_MENU_BACK "Menu_Back" #define GNOME_STOCK_MENU_FORWARD "Menu_Forward" #define GNOME_STOCK_MENU_FIRST "Menu_First" #define GNOME_STOCK_MENU_LAST "Menu_Last" #define GNOME_STOCK_MENU_HOME "Menu_Home" #define GNOME_STOCK_MENU_STOP "Menu_Stop" #define GNOME_STOCK_MENU_REFRESH "Menu_Refresh" #define GNOME_STOCK_MENU_MAIL "Menu_Mail" #define GNOME_STOCK_MENU_MAIL_RCV "Menu_Receive Mail" #define GNOME_STOCK_MENU_MAIL_SND "Menu_Send Mail" #define GNOME_STOCK_MENU_MAIL_RPL "Menu_Reply to Mail" #define GNOME_STOCK_MENU_MAIL_FWD "Menu_Forward Mail" #define GNOME_STOCK_MENU_MAIL_NEW "Menu_New Mail" #define GNOME_STOCK_MENU_TRASH "Menu_Trash" #define GNOME_STOCK_MENU_TRASH_FULL "Menu_Trash Full" #define GNOME_STOCK_MENU_UNDELETE "Menu_Undelete" #define GNOME_STOCK_MENU_TIMER "Menu_Timer" #define GNOME_STOCK_MENU_TIMER_STOP "Menu_Timer Stopped" #define GNOME_STOCK_MENU_SPELLCHECK "Menu_Spellchecker" #define GNOME_STOCK_MENU_MIC "Menu_Microphone" #define GNOME_STOCK_MENU_LINE_IN "Menu_Line In" #define GNOME_STOCK_MENU_CDROM "Menu_Cdrom" #define GNOME_STOCK_MENU_VOLUME "Menu_Volume" #define GNOME_STOCK_MENU_BOOK_RED "Menu_Book Red" #define GNOME_STOCK_MENU_BOOK_GREEN "Menu_Book Green" #define GNOME_STOCK_MENU_BOOK_BLUE "Menu_Book Blue" #define GNOME_STOCK_MENU_BOOK_YELLOW "Menu_Book Yellow" #define GNOME_STOCK_MENU_BOOK_OPEN "Menu_Book Open" #define GNOME_STOCK_MENU_CONVERT "Menu_Convert" #define GNOME_STOCK_MENU_JUMP_TO "Menu_Jump To" #define GNOME_STOCK_MENU_UP "Menu_Up" #define GNOME_STOCK_MENU_DOWN "Menu_Down" #define GNOME_STOCK_MENU_TOP "Menu_Top" #define GNOME_STOCK_MENU_BOTTOM "Menu_Bottom" #define GNOME_STOCK_MENU_ATTACH "Menu_Attach" #define GNOME_STOCK_MENU_INDEX "Menu_Index" #define GNOME_STOCK_MENU_FONT "Menu_Font" #define GNOME_STOCK_MENU_EXEC "Menu_Exec" #define GNOME_STOCK_MENU_ALIGN_LEFT "Menu_Left" #define GNOME_STOCK_MENU_ALIGN_RIGHT "Menu_Right" #define GNOME_STOCK_MENU_ALIGN_CENTER "Menu_Center" #define GNOME_STOCK_MENU_ALIGN_JUSTIFY "Menu_Justify" #define GNOME_STOCK_MENU_TEXT_BOLD "Menu_Bold" #define GNOME_STOCK_MENU_TEXT_ITALIC "Menu_Italic" #define GNOME_STOCK_MENU_TEXT_UNDERLINE "Menu_Underline" #define GNOME_STOCK_MENU_TEXT_STRIKEOUT "Menu_Strikeout" #define GNOME_STOCK_MENU_EXIT GNOME_STOCK_MENU_QUIT |
Then there are stock buttons. These are for use in your dialogs (see the next section).
#define GNOME_STOCK_BUTTON_OK "Button_Ok" #define GNOME_STOCK_BUTTON_CANCEL "Button_Cancel" #define GNOME_STOCK_BUTTON_YES "Button_Yes" #define GNOME_STOCK_BUTTON_NO "Button_No" #define GNOME_STOCK_BUTTON_CLOSE "Button_Close" #define GNOME_STOCK_BUTTON_APPLY "Button_Apply" #define GNOME_STOCK_BUTTON_HELP "Button_Help" #define GNOME_STOCK_BUTTON_NEXT "Button_Next" #define GNOME_STOCK_BUTTON_PREV "Button_Prev" #define GNOME_STOCK_BUTTON_UP "Button_Up" #define GNOME_STOCK_BUTTON_DOWN "Button_Down" #define GNOME_STOCK_BUTTON_FONT "Button_Font" |
If you need to create you own custom dialog, gnome-dialog is the way to do it. It can handle both modal and non-modal dialogs, although, it's definitely much more friendly to the users of your program if you use a non-modal dialog box, if at all possible, although non-modal dialog boxes tend to have problems associated with them, and sometimes can cause strange bugs, for example if a non-modal dialog box is associated with a window, you'd better bind the destroy signal of the window and set it to destroy the dialog box as well, since otherwise it could hang around even though the window or document it was supposed to act on is already dead. However modal dialogs (while definitely easier to program) are usually pretty annoying to use, so avoid them if you at all can.
To make a new GnomeDialog widget, just use the gnome_dialog_new function. You pass the title of the dialog as the first argument, and then multiple arguments as the button titles terminated by a NULL. The button titles can also be the GNOME_STOCK_BUTTON_* definitions if you want stock buttons on your dialog. Then you need to add content to the dialog, the dialog is created with a vertical box (GtkVBox) for you to use, just by using GNOME_DIALOG(dialog)->vbox. Into that you add your content. At this point you have to decide if you want to do a modal dialog or a non-modal dialog.
In case you want to do a modal dialog, all you need to do is to call gnome_dialog_run_and_close function and it will run the dialog, wait for a user to press a button or close the dialog, and then close the dialog. This function will return the number of the button that was pressed or -1 if the dialog was just closed. In case you don't want to close the dialog when just any button is pressed, you use the gnome_dialog_run function, and after you get a result, do what you need to do for that particular button press. Then if you want to run the dialog more, you just loop back to gnome_dialog_run, and if you want to close, you run gnome_dialog_close. Here's an example of the second scheme.
GtkWidget *dlg; int i; ... /*create a new dialog, DON'T forget the NULL on the end, it is very important!*/ dlg = gnome_dialog_new("A Dialog", GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_APPLY, GNOME_STOCK_BUTTON_CLOSE, NULL); ... /*add some content to the dialog here*/ ... /*set up an infinite loop*/ for(;;) { i = gnome_dialog_run(GNOME_DIALOG(dlg)); if(i == 0 || i == 2) { /*the user pressed OK or close, so we will get out of the loop and close the dialog, or the user pressed */ gnome_dialog_close(GNOME_DIALOG(dlg)); break; } else if(i < 0) { /*the user closed the dialog from the window manager*/ break; } else if(i == 1) { /*user pressed apply we don't want to close*/ ... } } |
If you are doing a non-modal dialog box, things get a little more complicated. You create the dialog as above, but then you bind the clicked signal of the GnomeDialog widget. That signal has as it's second argument the button number that was pressed. After that you should use the gnome_dialog_set_close function to tell GnomeDialog that we want to close the dialog when the user first presses any button, if you want that behavior, otherwise you'll have to do gnome_dialog_close in the clicked signal handler for the buttons you want to close on. After that is set up you just gtk_widget_show the dialog. An example follows:
/*the clicked signal handler*/ static void dialog_clicked(GnomeDialog *dlg, int button, gpointer data) { switch(button) { case 1: /*user pressed apply*/ ... return; case 0: /*user pressed OK*/ ... /*fall though to close*/ case 2: /*user pressed close*/ gnome_dialog_close(dlg); break; } } /*somewhere else in the source file*/ ... GtkWidget *dlg; ... /*create a new dialog, DON'T forget the NULL on the end, it is very important!*/ dlg = gnome_dialog_new("A Dialog", GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_APPLY, GNOME_STOCK_BUTTON_CLOSE, NULL); ... /*add some content to the dialog here*/ ... /*bind the clicked handler*/ gtk_signal_connect(GTK_OBJECT(dlg),"clicked", GTK_SIGNAL_FUNC(dialog_clicked), NULL); /*show the dialog, note that this is not a modal dialog, so the program doesn't block here, but continues*/ gtk_widget_show(dlg); |
GnomeMessageBox is an object derived from GnomeDialog. As such you use it in the exact same manner, the only difference here is that it automatically sets up the insides of the dialog to be a single label and an icon of the selected message box type. The message box types are as follows:
#define GNOME_MESSAGE_BOX_INFO "info" #define GNOME_MESSAGE_BOX_WARNING "warning" #define GNOME_MESSAGE_BOX_ERROR "error" #define GNOME_MESSAGE_BOX_QUESTION "question" #define GNOME_MESSAGE_BOX_GENERIC "generic" |
If you have some properties to set in your application, you should use a GnomePropertyBox dialog for the preferences to make the applications more consistent. Again this object is derived from GnomeDialog so it's use is similar. But GnomePropertyBox defines some new signals, namely apply and help. They both get passed the page number as the second argument. For help you should use this to display the proper help page, however for apply, this was created for adding a per-page apply button, which was not realized yet, so you should ignore any apply signal with the page number other then -1, which is the global apply. This can be done with a simple if statement at the top of your apply routine. You can choose to be per-page apply ready, by doing a per-page apply in your code, but it is not sure if this code will ever get completed. It should be safe to do just the global apply as that is the only thing implemented in gnome-libs 1.0.
To use property dialogs, you call gnome_property_box_new, which will create a completely new dialog for you with a notebook and the four buttons. OK, which will call your apply handler for all pages and then for the -1 page, and then it will close the dialog, Apply, which will call the apply handler for all pages and then for the -1 page, Close, which will just close the dialog, and Help which will call your help handler if you bound it. You then connect the apply signal to your apply handler, and most likely the destroy signal on the property box to destroy the data associated with the property box when it closes. You then create the different pages for your property box and add them with, gnome_property_box_append_page, which takes your page as the second argument and a label as the third (usually this will be just a GtkLabel). You also want to connect the different signals for the widgets on your pages, to mark the property box as changed (otherwise the Apply and OK buttons will not be sensitive). You do this by calling gnome_property_box_changed every time the user changed something with the widgets. For example on entry (and derived) widgets you connect to the changed signal. Example follows:
/*apply handler*/ static void property_apply(GnomePropertyBox *box, int page_num, gpointer data) { /*ignore page numbers other then -1*/ if(page_num!=-1) return; /*do your apply routine here*/ ... } ... /*somewhere else in the source file*/ GtkWidget *pbox; GtkWidget *widget; ... pbox = gnome_property_box_new(); gtk_signal_connect(GTK_OBJECT(pbox),"apply", GTK_SIGNAL_FUNC(property_apply),NULL); ... /*you create a page for the property box and added it to the container called widget*/ gnome_property_box_append_page(GNOME_PROPERTY_BOX(pbox), widget, gtk_label_new("SomePage")); /*then add other pages in similar manner*/ ... /*we show the dialog box*/ gtk_widget_show_all(pbox); |
Gnome doesn't have it's own file picking dialog, although this is planned for the future, for now you need to use the regular GTK+ file dialog.
Use of the file dialog is very simple. You create the dialog with gtk_file_selection_new, passing it the title of the dialog box as the argument. After this you bind the clicked signal on the OK and Cancel buttons. For example for a loading dialog box, you could check that the file is of the correct type when the user presses OK and if so then close the dialog (usually with gtk_widget_destroy). Or for saving dialog, you could ask if the file exists. File selection dialog boxes are usually safe and simple to do non-modal. Just make sure you'd destroy the file dialog box when the object or window it's supposed to work with. Here's the routine that invokes the save as dialog for Achtung, which is a presentation program we're working on.
void presentation_save_as (AchtungPresentation *p) { GtkFileSelection *fsel; g_return_if_fail (p != NULL); g_return_if_fail (p->doc != NULL); fsel = (GtkFileSelection *) gtk_file_selection_new (_("Save presentation as")); if (p->real_file && p->filename) gtk_file_selection_set_filename (fsel, p->filename); gtk_object_set_data(GTK_OBJECT(fsel),"p",p); /* Connect the signals for Ok and Cancel */ gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked", GTK_SIGNAL_FUNC (save_ok), fsel); gtk_signal_connect_object (GTK_OBJECT (fsel->cancel_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT(fsel)); gtk_window_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE); /*if the presentation dies so do it's dialogs*/ gtk_signal_connect_object_while_alive (GTK_OBJECT (p), "destroy", GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT(fsel)); gtk_widget_show (GTK_WIDGET (fsel)); } |
Sometimes, especially in properties dialogs, you want fields for entering text, files, pixmaps, icons or double precision numbers. This is what the gnome-*entry widgets do.
This is an entry for regular text, but it includes history of previously entered values. Note that this widget is not derived from GtkEntry, but owns such a widget. This means that you can't use GtkEntry methods on this object directly, but you need to get a pointer to the GtkEntry object inside GnomeEntry. When you call gnome_entry_new, you pass a history_id string to it. This is a unique identifier to identify this entry, or this type of entries in your application. All the entries that share this history_id will have common history of values. After you create a GnomeEntry you use the gnome_entry_gtk_entry function to get a pointer to the GtkEntry object inside and bind any signals or manipulate text with that instead. Here is an example:
GtkWidget *gnome_e; GtkWidget *gtk_e; ... gnome_entry_new("text1"); gtk_e = gnome_entry_gtk_entry(GNOME_ENTRY(gnome_e)); gtk_signal_connect(GTK_OBJECT(gtk_e),"changed", GTK_SIGNAL_FUNC(entry_changed), NULL); |
GnomeEntry is a basis for GnomeFileEntry. Again it is not derived, but GnomeEntry is owned by GnomeFileEntry. This type of hierarchy is throughout all the gnome entry widgets. GnomeFileEntry adds a browse button on the right side of the entry, and also accepts file drops from the file manager for example. It's use is extremely similar to GnomeEntry. You create the entry with gnome_file_entry_new. The first argument is the history_id of the GnomeEntry, and the second argument is the title of the browse dialog box. To get the GtkEntry, you again use the gtk_entry method, named gnome_file_entry_gtk_entry. To finally get the filename, you can get the exact text from the GtkEntry, or you might use a convenience method, gnome_file_entry_get_full_path, which takes a flag file_must_exist as it's second argument. If this flag is set, the function returns NULL if the file doesn't exists. If the flag is not set or the file does exist, the function returns the full path to the file.
This is an entry for entering pixmaps (Images) of any size. It again includes (not derives from) GnomeFileEntry, so it can do everything the file entry can (including accepting drops). However this entry adds a preview box for the pixmap above the entry. Also it's file selection dialog includes a preview box to the right side of the file list. It's use is again very similar to the entries above. You call gnome_pixmap_entry_new with the same arguments as GnomeFileEntry, with an added flag, do_preview. This flag specifies if the preview box is visible or not. But be careful, it doesn't save memory not to show the preview, it just saves space. Again you use a gnome_pixmap_entry_gtk_entry to get the GtkEntry widget. To get a filename of the the pixmap, if it could be loaded as an image for the preview (using imlib), you can use gnome_pixmap_entry_get_filename, which returns NULL if the pixmap files doesn't exist or could not be loaded, and the full filename otherwise.
The icon entry is very similar to the GnomePixmapEntry, but it is meant for images in the standard 48x48 icon size. Also instead of the preview box, there is a button with the image scaled to 48x48. If you press the button you get a listing of images from the same directory as the current icon. To create an icon entry use gnome_icon_entry_new with history_id and browse_dialog_title string arguments. Once you need an existing icon that is a real image, you use gnome_icon_entry_get_filename which works just like gnome_pixmap_entry_get_filename. You can also get the GtkEntry by using gnome_icon_entry_gtk_entry.
GnomeNumberEntry is an entry widget for entering double precision numbers with a calculator. Most of the time for number entries you want to use the GtkSpinButton widget, however for applications such as mortgage calculators, or finance programs, where calculations are necessary, you will want to use this entry type. Basically it's a GnomeEntry widget with a button on the right side of it which calls up a dialog with a calculator. The user can use the calculator and press OK and the number entry is updated to what it was on the calculator. To create a number entry widget, just use gnome_number_entry_new, passing it the history_id as the first argument and the title of the calculator dialog as the second argument. To get the GtkEntry widget just use gnome_number_entry_gtk_entry. To get the number as a double value, use gnome_number_entry_get_number method.
When you need to use images in your apps, most likely you'll want the GnomePixmap widget. It's advantage is that it makes using images much easier without having to learn imlib, which is the image library used by this widget.
There are numerous new functions for GnomePixmap, depending on the source of the pixmap. The most used will probably be gnome_pixmap_new_from_file which takes a filename which is an image loadable by imlib and creates a pixmap widget for you. There is also gnome_pixmap_new_from_file_at_size to which you pass also the size to which the image should be scaled. If you have already loaded the image with imlib (in case you wanted to do other things to the pixmap first), you can use gnome_pixmap_new_from_imlib and gnome_pixmap_new_from_imlib_at_size. Which take a GdkImlibImage as the first argument. If you already have a pixmap widget and want to change the image inside it, you can use the gnome_pixmap_load_* which have almost the same syntax as the new functions, except that you pass the GnomePixmap as the first argument, and then the rest of the arguments as above, and of course replace the _new_from_ for _load_.
Here's an example of it's use:
GtkWidget *pix; ... /*load somefile.png and scale it to 48x48*/ pix = gnome_pixmap_new_from_file_at_size("somefile.png",48,48); /*now you can pack pix somewhere just like any other widget*/ ... /*now we want to change the files to otherfile.png and do no scaling*/ gnome_pixmap_load_file(GNOME_PIXMAP(pix),"otherfile.png"); |
Your app should be able to save it's settings and restore them when the user restarts your application, it should also be able to do this for several different sessions. For instance the user might have a normal session, but sometimes log into a special session where he has different settings in applications. gnome-libs actually hides the ugly details of this. For the most part you do not need to worry about the real details of session management, unless you wish to do something very clever or if your app does some complicated state saving. To do simple session saving all you need is the following code (mostly taken from gnome-hello-4-SM example program):
/*the save_yourself handler, you can safely ignore most of the parameters, and just save your session and return TRUE*/ static int save_yourself(GnomeClient *client, int phase, GnomeSaveStyle save_style, int shutdown, GnomeInteractStyle interact_style, int fast, gpointer client_data) { /*get the prefix for our config*/ char *prefix= gnome_client_get_config_prefix (client); /*this is a "discard" command for discarding data from a saved session, usually this will work*/ char *argv[]= { "rm", "-r", NULL }; /* Save the state using gnome-config stuff. */ gnome_config_push_prefix (prefix); gnome_config_set_int("Section/Key",some_value); ... gnome_config_pop_prefix (); gnome_config_sync(); /* Here is the real SM code. We set the argv to the parameters needed to restart/discard the session that we've just saved and call the gnome_session_set_*_command to tell the session manager it. */ argv[2]= gnome_config_get_real_path (prefix); gnome_client_set_discard_command (client, 3, argv); /* Set commands to clone and restart this application. Note that we use the same values for both -- the session management code will automatically add whatever magic option is required to set the session id on startup. The client_data was set to the command used to start this application when save_yourself handler was connected. */ argv[0]= (gchar*) client_data; gnome_client_set_clone_command (client, 1, argv); gnome_client_set_restart_command (client, 1, argv); return TRUE; } static void die (GnomeClient *client, gpointer client_data) { /* Just exit in a friendly way. We don't need to save any state here, because the session manager should have sent us a save_yourself-message before. */ gtk_exit (0); } ... GnomeClient *client; ... /*this is somewhere in your main function presumably. make sure this is done AFTER the gnome_init call!*/ /* Get the master client, that was hopefully connected to the session manager int the 'gnome_init' call. All communication to the session manager will be done with this master client. */ client = gnome_master_client (); /* Arrange to be told when something interesting happens. */ gtk_signal_connect (GTK_OBJECT (client), "save_yourself", GTK_SIGNAL_FUNC (save_yourself), (gpointer) argv[0]); gtk_signal_connect (GTK_OBJECT (client), "die", GTK_SIGNAL_FUNC (die), NULL); /*check if we are connected to a session manager*/ if (GNOME_CLIENT_CONNECTED (client)) { /*we are connected, we will get the prefix under which we saved our session last time and load up our data*/ gnome_config_push_prefix (gnome_client_get_config_prefix (client)); some_value = gnome_config_get_int("Section/Key=0"); gnome_config_pop_prefix (); } else { /*we are not connected to any session manager, here you will just initialize your session like you normally do without a session manager*/ ... } |
If your app handles documents, most likely you will want it to handle multiple documents at one time. Gnome provides an MDI model that is customizable by the user and simple to use. They can use three models of the document display. Either a notebook style which is the most useful one, where documents can be docked in notebooks, and can be dragged out into separate windows if desired. Or a toplevel style where each document is a separate toplevel window. Or finally a modal style where there is only one window and the documents must be switched though a menu. (Note that the examples here are taken from the gnome-hello-7-mdi example app in gnome-libs, slightly modified)
To use the MDI features. You basically replace the the gnome_app_new call with gnome_mdi_new with the same arguments as gnome_app_new. To add menus and tool-bar, you use gnome_mdi_set_menubar_template and gnome_mdi_set_toolbar_template with the GnomeUIInfo as the argument. For MDI, these aren't the actual menus, as it will add it's own items to the menus of each child. After this you set where the menu additions take place. You call gnome_mdi_set_child_menu_path to the toplevel menu name after which the child's own menus are inserted. This is the "File" menu in most cases. Then you want to specify the path (menu name) to the menu into which you want to insert a list of the children, you do this by calling gnome_mdi_set_child_list_path with the name of the menu and add a '/' on the end of it to specify that you want to insert those items into the menu, not after the menu. Example:
GtkWidget *mdi; ... mdi = gnome_mdi_new("gnome-hello-7-mdi", "GNOME MDI Hello"); ... /*main_menu and toolbar_info are the menu and tool-bar descriptions*/ gnome_mdi_set_menubar_template(mdi, main_menu); gnome_mdi_set_toolbar_template(mdi, toolbar_info); /* and document menu and document list paths (see gnome-app-helper menu insertion routines for details) */ gnome_mdi_set_child_menu_path(GNOME_MDI(mdi), "File"); gnome_mdi_set_child_list_path(GNOME_MDI(mdi), "Children/"); |
Then you should open the main toplevel window with gnome_mdi_open_toplevel. This will open a toplevel window without any children. If you wish to use MDI's session management functionality, you can define a function that creates a child given it's name. This is done with the gnome_mdi_restore_state method, which takes the config path as the second argument and a function pointer to a function which takes a string and returns a new GnomeMDIChild widget (a widget sub-classed from GnomeMDIChild actually). Say for example you are using the session management shown above, so you could use:
gnome_config_push_prefix (gnome_client_get_config_prefix (client)); restart_ok = gnome_mdi_restore_state(GNOME_MDI(mdi), "MDI Session", my_child_new_from_config); gnome_config_pop_prefix (); |
You should also bind the destroy signal of the mdi object to do gtk_main_quit when the mdi is destroyed.
For complicated apps, all children should be derived from the virtual GnomeMDIChild object. For simple apps, you don't need to derive a new object, you can just use the GnomeMDIGenericChild, and use the fact that you can store arbitrary data on arbitrary GtkObjects to store your own data on the object.
To use the generic child object, you create it with gnome_mdi_generic_child_new to which you pass the name of the child. When you get the object, you will need to set it up for your use. First you add a function for creating new views of the same data. A view is just a different window displaying the same file or data. This is done with a call to gnome_mdi_generic_child_set_view_creator to which you pass a pointer to a creator function which takes the child widget and a data pointer as arguments and returns a data widget, which is not the actual child widget, but actually the child of the GnomeMDIGenericChild widget. After this you set the template for the child's menus with gnome_mdi_child_set_menu_template, to which you pass the GnomeUIInfo array pointer of the child menu definitions. Then you should call gnome_mdi_generic_child_set_config_func to set a function which returns a newly allocated string to save in the config file. This string will be used to load up the child next time you start and do the gnome_mdi_restore_state call. It should probably be a filename of the document, or some string from which you can completely recreate that window/document. Then you need to call gnome_mdi_generic_child_set_label_func with a pointer to a function that takes the GnomeMDIGenericChild as the first argument, the old label widget pointer as the second argument, which would be null if no label widget was yet set, and a data argument. This function can either create a new label and destroy the old one, or just set the label if the label exists. The label can be any widget, for example the gnome-hello-7-mdi example code uses a horizontal box widget into which it adds a pixmap and a gtk label. After this if you need to add the child to the mdi yourself, if you are loading a new file for example, you use gnome_mdi_add_child and gnome_mdi_add_view, to add a new child and a new view to the mdi. If you are creating a new child from the gnome_mdi_restore_state function, you should just return the child, the mdi will take care of adding it and adding the appropriate views. You also probably want to set some data on the child widget at this time to store your data with the object.
Here's a short example of creating a new child, for a more complete example you should look at the gnome-hello-7-mdi included with the gnome-libs distribution.
GnomeMDI *mdi; ... GnomeMDIGenericChild *child; ... /*create a new child named 'name'*/ if((child = gnome_mdi_generic_child_new(name)) != NULL) { /*creator of a view*/ gnome_mdi_generic_child_set_view_creator (child, my_child_create_view, NULL); /*set a menu template for child menu*/ gnome_mdi_child_set_menu_template (GNOME_MDI_CHILD(child), main_child_menu); /*set function to get config string*/ gnome_mdi_generic_child_set_config_func (child, my_child_get_config_string, NULL); /*set function that sets or creates a label*/ gnome_mdi_generic_child_set_label_func (child, my_child_set_label, NULL); /* add the child to MDI */ gnome_mdi_add_child(mdi, GNOME_MDI_CHILD(child)); /* and add a new view of the child */ gnome_mdi_add_view(mdi, GNOME_MDI_CHILD(child)); } |