?? z170.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> GnomeAppBar: A Trivial Composite Widget </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="Writing a GtkWidget" href= "cha-widget.html"> <link rel="PREVIOUS" title="GtkVBox: A Windowless Container" href="z166.html"> <link rel="NEXT" title="Other Examples" href="z171.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="z166.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="z171.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> </table> </div> <div class="SECT1"> <h1 class="SECT1"> <a name="Z170"><tt class="CLASSNAME">GnomeAppBar</tt>: A Trivial Composite Widget</a> </h1> <p> This section quickly describes the <tt class="CLASSNAME"> GnomeAppBar</tt> widget; <tt class="CLASSNAME"> GnomeAppBar</tt> demonstrates how to bundle a pre-packed container and some special functionality into a single new object. <a href="z91.html#SEC-APPBAR">the section called <i><tt class="CLASSNAME">GnomeAppBar</tt></i> in the chapter called <i>The Main Window: <tt class="CLASSNAME"> GnomeApp</tt></i></a> describes <tt class="CLASSNAME"> GnomeAppBar</tt> from a user's point of view. </p> <p> A composite widget derives from some kind of container, then adds child widgets and sets up callbacks to implement some sort of functionality. <tt class="CLASSNAME"> GnomeAppBar</tt> derives from <tt class="CLASSNAME"> GtkHBox</tt>; the box is packed with a progress bar and/or a status line. <tt class="CLASSNAME">GnomeAppBar</tt> has members in its instance struct to store a stack of status messages, and it adds some signals to the class struct for use with its "interactive" mode. </p> <p> As an aside, <tt class="CLASSNAME">GnomeAppBar</tt> does not follow the GTK+/Gnome naming conventions; because <span class="STRUCTNAME">Bar</span> is capitalized, the functions and macros should have an underscore, i.e. <span class= "STRUCTNAME">app_bar</span> rather than <span class= "STRUCTNAME">appbar</span>. Don't copy this aspect of the widget. </p> <p> Here's the implementation of <tt class="FUNCTION"> gnome_appbar_new()</tt>: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> GtkWidget* gnome_appbar_new (gboolean has_progress, gboolean has_status, GnomePreferencesType interactivity){ GnomeAppBar * ab = gtk_type_new (gnome_appbar_get_type ()); gnome_appbar_construct(ab, has_progress, has_status, interactivity); return GTK_WIDGET(ab);}voidgnome_appbar_construct(GnomeAppBar * ab, gboolean has_progress, gboolean has_status, GnomePreferencesType interactivity){ GtkBox *box; g_return_if_fail( ((has_status == FALSE) && (interactivity == GNOME_PREFERENCES_NEVER)) || (has_status == TRUE)); box = GTK_BOX (ab); box->spacing = GNOME_PAD_SMALL; box->homogeneous = FALSE; if (has_progress) ab->progress = gtk_progress_bar_new(); else ab->progress = NULL; /* * If the progress meter goes on the right then we place it after we * create the status line. */ if (has_progress && !gnome_preferences_get_statusbar_meter_on_right ()) gtk_box_pack_start (box, ab->progress, FALSE, FALSE, 0); if ( has_status ) { if ( (interactivity == GNOME_PREFERENCES_ALWAYS) || ( (interactivity == GNOME_PREFERENCES_USER) && gnome_preferences_get_statusbar_interactive()) ) { ab->interactive = TRUE; ab->status = gtk_entry_new(); gtk_signal_connect (GTK_OBJECT(ab->status), "delete_text", GTK_SIGNAL_FUNC(entry_delete_text_cb), ab); gtk_signal_connect (GTK_OBJECT(ab->status), "insert_text", GTK_SIGNAL_FUNC(entry_insert_text_cb), ab); gtk_signal_connect_after(GTK_OBJECT(ab->status), "key_press_event", GTK_SIGNAL_FUNC(entry_key_press_cb), ab); gtk_signal_connect(GTK_OBJECT(ab->status), "activate", GTK_SIGNAL_FUNC(entry_activate_cb), ab); /* no prompt now */ gtk_entry_set_editable(GTK_ENTRY(ab->status), FALSE); gtk_box_pack_start (box, ab->status, TRUE, TRUE, 0); } else { GtkWidget * frame; ab->interactive = FALSE; frame = gtk_frame_new (NULL); gtk_frame_set_shadow_type (GTK_FRAME(frame), GTK_SHADOW_IN); ab->status = gtk_label_new (""); gtk_misc_set_alignment (GTK_MISC (ab->status), 0.0, 0.0); gtk_box_pack_start (box, frame, TRUE, TRUE, 0); gtk_container_add (GTK_CONTAINER(frame), ab->status); gtk_widget_show (frame); } } else { ab->status = NULL; ab->interactive = FALSE; } if (has_progress && gnome_preferences_get_statusbar_meter_on_right ()) gtk_box_pack_start (box, ab->progress, FALSE, FALSE, 0); if (ab->status) gtk_widget_show (ab->status); if (ab->progress) gtk_widget_show(ab->progress);} </pre> </td> </tr> </table> <p> Most of this code could be in the instance initializer; it's in the constructor instead because it's dependent on the arguments passed to <tt class="FUNCTION"> gnome_appbar_new()</tt>. There's not much to explain here; the code is straightforward. Do notice that <tt class= "FUNCTION">gtk_widget_show()</tt> is called for each child widget; this ensures that the right thing happens when the user calls <tt class="FUNCTION">gtk_widget_show()</tt> on <tt class="CLASSNAME">GnomeAppBar</tt>. Another approach would be to override the map method and map all children (normally, containers such as <tt class="CLASSNAME"> GtkBox</tt> only map children that have been shown). When you're writing a composite container, keep the <tt class= "FUNCTION">gtk_widget_show_all()</tt> function in mind; never rely on hiding child widgets, because the user might accidentally show them. </p> <p> A composite widget is just a special case of extending a base widget with additional functionality. You can extend widgets without adding new children to them; for example, <tt class="CLASSNAME">GtkClock</tt> extends <tt class= "CLASSNAME">GtkLabel</tt> by constantly changing the label to reflect the time. </p> </div> <div class="NAVFOOTER"> <br> <br> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z166.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="z171.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> <tr> <td colspan="2" align="left"> <font color="#000000" size="2"><b><tt class= "CLASSNAME">GtkVBox</tt>: A Windowless Container</b></font> </td> <td colspan="2" align="right"> <font color="#000000" size="2"><b>Other Examples</b></font> </td> </tr> </table> </div> </body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -