?? cha-dialogs.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> User Communication: Dialogs </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Building a Gnome Application" href= "build-app.html"> <link rel="PREVIOUS" title="Finishing Touches" href="z94.html"> <link rel="NEXT" title="Modal Dialogs" href= "sec-modaldialogs.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z94.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="sec-modaldialogs.html"><font color="#0000ff" size="2"><b>Next >>></b></font></a> </td> </tr> </table> </div> <div class="CHAPTER"> <h1> <a name="CHA-DIALOGS">User Communication: Dialogs</a> </h1> <div class="TOC"> <dl> <dt> <b>Table of Contents</b> </dt> <dt> <a href="cha-dialogs.html#Z97">The <tt class= "CLASSNAME">GnomeDialog</tt> Widget</a> </dt> <dt> <a href="sec-modaldialogs.html">Modal Dialogs</a> </dt> <dt> <a href="z101.html">A Dialog Example</a> </dt> <dt> <a href="cha-dialogs-special.html">Special Dialog Types</a> </dt> <dt> <a href="sec-dialogs-convenience.html">Convenience Routines</a> </dt> </dl> </div> <p> Dialogs are a continuous annoyance in plain GTK+; every time you want to tell the user anything, you have to create a window, create some buttons, create a label, pack the buttons and label into the window, set up callbacks, remember to capture <span class="SYMBOL"> "delete_event"</span>, and so on. It's a pain. Gnome saves you from this pain, with an easy-to-use general-purpose dialog widget, and several subclasses of it that implement common dialog types. Gnome also has easy functions for using modal dialogs. </p> <div class="SECT1"> <h1 class="SECT1"> <a name="Z97">The <tt class="CLASSNAME">GnomeDialog</tt> Widget</a> </h1> <p> Since dialogs in plain GTK+ are painstakingly constructed from scratch, there are at least as many ways to write a dialog as there are programmers. The programmer must decide where to place the dialog on the screen, how much padding to have, whether to put a separator above the buttons, what container to put the buttons in, what the keyboard shortcuts are, and so on. The premise of <tt class="CLASSNAME">GnomeDialog</tt> is that the programmer should not have to care about these things; if they're variable at all, the user should configure them the way they want. From the programmer's perspective, dialogs "just work." </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z98">Creating a Dialog</a> </h2> <p> A <tt class="CLASSNAME">GnomeDialog</tt> is easy to create. Here's a summary of the basic steps, more detail follows: </p> <ol type="1"> <li> <p> Read <a href="cha-dialogs-special.html">the section called <i>Special Dialog Types</i></a> and decide whether one of the special dialog subclasses is appropriate. If so, skip the below steps and create that subclass instead. </p> </li> <li> <p> Create the widget with <tt class="FUNCTION"> gnome_dialog_new()</tt>. Pass this function the title of the dialog (displayed by the window manager) and the name of each button you'd like to have. </p> </li> <li> <p> Populate <span class="STRUCTNAME"> GNOME_DIALOG(dialog)->vbox</span> with the contents of your dialog. </p> </li> <li> <p> Plan how your dialog will work. You can connect to the <span class="SYMBOL">"close"</span> or <span class="SYMBOL">"clicked"</span> signals, as appropriate. You can have the dialog hide or destroy itself when closed. You can also have the dialog automatically close when clicked, or handle this yourself. There are a number of ways the user can interact with a dialog, so it's important to be sure the combination of settings you choose will work no matter what the user does. </p> </li> </ol> <p> To create a dialog, use <tt class="FUNCTION"> gnome_dialog_new()</tt>, shown in <a href= "cha-dialogs.html#FL-DIALOGNEW">Figure 1</a>. The argument list is a <span class="STRUCTNAME"> NULL</span>-terminated list of buttons to insert in the dialog. For example, you might say: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> GtkWidget* dialog;dialog = gnome_dialog_new(_("My Dialog Title"), _("OK"), _("Cancel"), NULL); </pre> </td> </tr> </table> <p> This creates a dialog titled "My Dialog Title" with an OK and a Cancel button; the strings are marked for translation with the <tt class="FUNCTION">_()</tt> macro. The OK button will be the leftmost button in the dialog. </p> <div class="FIGURE"> <a name="FL-DIALOGNEW"></a> <div class="FUNCSYNOPSIS"> <a name="FL-DIALOGNEW.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO"> #include <libgnomeui/gnome-dialog.h> </pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">GtkWidget* <tt class= "FUNCTION">gnome_dialog_new</tt></code>(const gchar* <tt class="PARAMETER"><i>title</i></tt>, <tt class="PARAMETER"><i>...</i></tt>);</code> </p> </div> <p> <b>Figure 1. <tt class="CLASSNAME">GnomeDialog</tt> Constructor</b> </p> </div> <p> The <tt class="CLASSNAME">GnomeDialog</tt> API numbers the buttons you add starting with 0; you use these numbers to refer to the buttons later, since you don't have a pointer to the automatically-created button widgets. In this case, the OK button is button 0, and the Cancel button is button 1. (Note that this is standard Gnome practice---OK or Yes goes first, then Cancel or No. In fact <tt class="FILENAME"> libgnomeui/gnome-uidefs.h</tt> contains the macros <tt class="FUNCTION">GNOME_YES</tt>, <tt class="FUNCTION"> GNOME_OK</tt>, <tt class="FUNCTION">GNOME_NO</tt>, and <tt class="FUNCTION">GNOME_CANCEL</tt> which represent the dialog button numbers for these items in a two-button dialog.) </p> <p> The above example, which specifies buttons called "OK" and "Cancel," is not quite correct for production code. Gnome provides a set of "stock buttons" for common button names. These ensure everyone uses "OK" instead of "Ok" or "OK!"; they allow translators to translate common strings only once; and they often insert icons in the buttons, making them more attractive and recognizable to users. You should always use stock buttons if possible. </p> <p> You can use stock buttons in <tt class="FUNCTION"> gnome_dialog_new()</tt>. Simply substitute the stock button macros for the button names: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> dialog = gnome_dialog_new(_("My Dialog Title"), GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_CANCEL, NULL); </pre> </td> </tr> </table> <p> Gnome includes many stock buttons, stock menu items, and stock pixmaps---it's a good idea to check these out so you don't reinvent the wheel. There's a complete list in <tt class="FILENAME"> libgnomeui/gnome-stock.h</tt>. </p> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z99">Filling in the Dialog</a> </h2> <p> After creating a dialog, you'll want to put something inside. If you just want a label inside, probably you should use <tt class="CLASSNAME">GnomeMessageBox</tt> or one of the convenience routines (such as <tt class= "FUNCTION">gnome_ok_dialog()</tt>) instead of constructing the dialog manually. Otherwise, filling a dialog is very simple: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> GtkWidget* button;/* ... create dialog as shown earlier ... */button = gtk_button_new_with_label(_("Push Me"));gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox)), button, TRUE, TRUE, 0); </pre> </td> </tr> </table> <p> Of course you can pack the contents of <span class= "STRUCTNAME">dialog->vbox</span> using the packing options of your choice. The above code is just an example. </p> <p> <a href="cha-dialogs.html#FIG-GNOMEDIALOG">Figure 2</a> shows a dialog from the Gnumeric spreadsheet, with its components labelled. </p> <div class="FIGURE"> <a name="FIG-GNOMEDIALOG"></a> <p> <img src="figures/gnumeric-dialog.png"> </p> <p> <b>Figure 2. A <tt class="CLASSNAME">GnomeDialog</tt> from the Gnumeric spreadsheet</b> </p> </div> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z100">Handling <tt class="CLASSNAME"> GnomeDialog</tt> Signals</a> </h2> <p> Now for the tricky part. You have to prepare yourself to handle anything the user might do to your dialog. Here's a brief list of possibilities; it's worth going over the list whenever you create a dialog: </p> <ul> <li> <p> Closing the dialog by pressing the Escape key </p> </li> <li> <p> Closing the dialog by clicking the window manager's close decoration </p> </li> <li> <p> Clicking one of the dialog's buttons </p> </li> <li> <p> Interacting with the contents of the dialog </p> </li> <li>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -