⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_tut.sgml

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 SGML
📖 第 1 页 / 共 5 页
字号:
                                gint       top_attach,                                gint       bottom_attach );</verb></tscreen>The X and Y options default to GTK_FILL | GTK_EXPAND, and X and Ypadding are set to 0. The rest of the arguments are identical to theprevious function.We also have gtk_table_set_row_spacing() andgtk_table_set_col_spacing().  This places spacing between the rows atthe specified row or column.<tscreen><verb>void gtk_table_set_row_spacing( GtkTable *table,                                gint      row,                                gint      spacing );</verb></tscreen>and<tscreen><verb>void gtk_table_set_col_spacing ( GtkTable *table,                                 gint      column,                                 gint      spacing );</verb></tscreen>Note that for columns, the space goes to the right of the column, andfor rows, the space goes below the row.You can also set a consistent spacing of all rows and/or columns with:<tscreen><verb>void gtk_table_set_row_spacings( GtkTable *table,                                 gint      spacing );</verb></tscreen>And,<tscreen><verb>void gtk_table_set_col_spacings( GtkTable *table,                                 gint      spacing );</verb></tscreen>Note that with these calls, the last row and last column do not getany spacing.<!-- ----------------------------------------------------------------- --><sect1>Table Packing Example<p>Here we make a window with three buttons in a 2x2 table.The first two buttons will be placed in the upper row.A third, quit button, is placed in the lower row, spanning both columns.Which means it should look something like this:<? <CENTER> ><?<IMG SRC="gtk_tut_table.gif" VSPACE="15" HSPACE="10" ALT="Table Packing Example Image" WIDTH="180" HEIGHT="120">><? </CENTER> >Here's the source code:<tscreen><verb>/* example-start table table.c */#include <gtk/gtk.h>/* Our callback. * The data passed to this function is printed to stdout */void callback( GtkWidget *widget,               gpointer   data ){    g_print ("Hello again - %s was pressed\n", (char *) data);}/* This callback quits the program */void delete_event( GtkWidget *widget,                   GdkEvent  *event,		   gpointer   data ){    gtk_main_quit ();}int main( int   argc,          char *argv[] ){    GtkWidget *window;    GtkWidget *button;    GtkWidget *table;    gtk_init (&amp;argc, &amp;argv);    /* Create a new window */    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);    /* Set the window title */    gtk_window_set_title (GTK_WINDOW (window), "Table");    /* Set a handler for delete_event that immediately     * exits GTK. */    gtk_signal_connect (GTK_OBJECT (window), "delete_event",                        GTK_SIGNAL_FUNC (delete_event), NULL);    /* Sets the border width of the window. */    gtk_container_set_border_width (GTK_CONTAINER (window), 20);    /* Create a 2x2 table */    table = gtk_table_new (2, 2, TRUE);    /* Put the table in the main window */    gtk_container_add (GTK_CONTAINER (window), table);    /* Create first button */    button = gtk_button_new_with_label ("button 1");    /* When the button is clicked, we call the "callback" function     * with a pointer to "button 1" as its argument */    gtk_signal_connect (GTK_OBJECT (button), "clicked",              GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");    /* Insert button 1 into the upper left quadrant of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);    gtk_widget_show (button);    /* Create second button */    button = gtk_button_new_with_label ("button 2");    /* When the button is clicked, we call the "callback" function     * with a pointer to "button 2" as its argument */    gtk_signal_connect (GTK_OBJECT (button), "clicked",              GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");    /* Insert button 2 into the upper right quadrant of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);    gtk_widget_show (button);    /* Create "Quit" button */    button = gtk_button_new_with_label ("Quit");    /* When the button is clicked, we call the "delete_event" function     * and the program exits */    gtk_signal_connect (GTK_OBJECT (button), "clicked",                        GTK_SIGNAL_FUNC (delete_event), NULL);    /* Insert the quit button into the both      * lower quadrants of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);    gtk_widget_show (button);    gtk_widget_show (table);    gtk_widget_show (window);    gtk_main ();    return 0;}/* example-end */</verb></tscreen><!-- ***************************************************************** --><sect>Widget Overview<!-- ***************************************************************** --><p>The general steps to creating a widget in GTK are:<enum><item> gtk_*_new - one of various functions to create a new widget.These are all detailed in this section.<item> Connect all signals and events we wish to use to theappropriate handlers.<item> Set the attributes of the widget.<item> Pack the widget into a container using the appropriate callsuch as gtk_container_add() or gtk_box_pack_start().<item> gtk_widget_show() the widget.</enum>gtk_widget_show() lets GTK know that we are done setting theattributes of the widget, and it is ready to be displayed. You mayalso use gtk_widget_hide to make it disappear again. The order inwhich you show the widgets is not important, but I suggest showing thewindow last so the whole window pops up at once rather than seeing theindividual widgets come up on the screen as they're formed. Thechildren of a widget (a window is a widget too) will not be displayeduntil the window itself is shown using the gtk_widget_show() function.<!-- ----------------------------------------------------------------- --><sect1> Casting<p>You'll notice as you go on, that GTK uses a type casting system. Thisis always done using macros that both test the ability to cast thegiven item, and perform the cast. Some common ones you will see are:<itemize><item> GTK_WIDGET(widget)<item> GTK_OBJECT(object)<item> GTK_SIGNAL_FUNC(function)<item> GTK_CONTAINER(container)<item> GTK_WINDOW(window)<item> GTK_BOX(box)</itemize>These are all used to cast arguments in functions. You'll see them in theexamples, and can usually tell when to use them simply by looking at thefunction's declaration.As you can see below in the class hierarchy, all GtkWidgets arederived from the GtkObject base class. This means you can use a widgetin any place the function asks for an object - simply use theGTK_OBJECT() macro.For example:<tscreen><verb>gtk_signal_connect( GTK_OBJECT(button), "clicked",                    GTK_SIGNAL_FUNC(callback_function), callback_data);</verb></tscreen> This casts the button into an object, and provides a cast for thefunction pointer to the callback.Many widgets are also containers. If you look in the class hierarchybelow, you'll notice that many widgets derive from the GtkContainerclass. Any one of these widgets may be used with the GTK_CONTAINERmacro to pass them to functions that ask for containers.Unfortunately, these macros are not extensively covered in thetutorial, but I recommend taking a look through the GTK headerfiles. It can be very educational. In fact, it's not difficult tolearn how a widget works just by looking at the function declarations.<!-- ----------------------------------------------------------------- --><sect1>Widget Hierarchy<p>For your reference, here is the class hierarchy tree used to implement widgets.<tscreen><verb> GtkObject  +GtkWidget  | +GtkMisc  | | +GtkLabel  | | | +GtkAccelLabel  | | | `GtkTipsQuery  | | +GtkArrow  | | +GtkImage  | | `GtkPixmap  | +GtkContainer  | | +GtkBin  | | | +GtkAlignment  | | | +GtkFrame  | | | | `GtkAspectFrame  | | | +GtkButton  | | | | +GtkToggleButton  | | | | | `GtkCheckButton  | | | | |   `GtkRadioButton  | | | | `GtkOptionMenu  | | | +GtkItem  | | | | +GtkMenuItem  | | | | | +GtkCheckMenuItem  | | | | | | `GtkRadioMenuItem  | | | | | `GtkTearoffMenuItem  | | | | +GtkListItem  | | | | `GtkTreeItem  | | | +GtkWindow  | | | | +GtkColorSelectionDialog  | | | | +GtkDialog  | | | | | `GtkInputDialog  | | | | +GtkDrawWindow  | | | | +GtkFileSelection  | | | | +GtkFontSelectionDialog  | | | | `GtkPlug  | | | +GtkEventBox  | | | +GtkHandleBox  | | | +GtkScrolledWindow  | | | `GtkViewport  | | +GtkBox  | | | +GtkButtonBox  | | | | +GtkHButtonBox  | | | | `GtkVButtonBox  | | | +GtkVBox  | | | | +GtkColorSelection  | | | | `GtkGammaCurve  | | | `GtkHBox  | | |   +GtkCombo  | | |   `GtkStatusbar  | | +GtkCList  | | | `GtkCTree  | | +GtkFixed  | | +GtkNotebook  | | | `GtkFontSelection  | | +GtkPaned  | | | +GtkHPaned  | | | `GtkVPaned  | | +GtkLayout  | | +GtkList  | | +GtkMenuShell  | | | +GtkMenuBar  | | | `GtkMenu  | | +GtkPacker  | | +GtkSocket  | | +GtkTable  | | +GtkToolbar  | | `GtkTree  | +GtkCalendar  | +GtkDrawingArea  | | `GtkCurve  | +GtkEditable  | | +GtkEntry  | | | `GtkSpinButton  | | `GtkText  | +GtkRuler  | | +GtkHRuler  | | `GtkVRuler  | +GtkRange  | | +GtkScale  | | | +GtkHScale  | | | `GtkVScale  | | `GtkScrollbar  | |   +GtkHScrollbar  | |   `GtkVScrollbar  | +GtkSeparator  | | +GtkHSeparator  | | `GtkVSeparator  | +GtkPreview  | `GtkProgress  |   `GtkProgressBar  +GtkData  | +GtkAdjustment  | `GtkTooltips  `GtkItemFactory</verb></tscreen><!-- ----------------------------------------------------------------- --><sect1>Widgets Without Windows<p>The following widgets do not have an associated window. If you want tocapture events, you'll have to use the GtkEventBox. See the section onthe <ref id="sec_EventBox" name="EventBox"> widget.<tscreen><verb>GtkAlignmentGtkArrowGtkBinGtkBoxGtkImageGtkItemGtkLabelGtkPixmapGtkScrolledWindowGtkSeparatorGtkTableGtkAspectFrameGtkFrameGtkVBoxGtkHBoxGtkVSeparatorGtkHSeparator</verb></tscreen>We'll further our exploration of GTK by examining each widge

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -