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

📄 gtk_tut-5.html

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>GTK v1.2 Tutorial: Widget Overview</TITLE> <LINK HREF="gtk_tut-6.html" REL=next> <LINK HREF="gtk_tut-4.html" REL=previous> <LINK HREF="gtk_tut.html#toc5" REL=contents></HEAD><BODY BGCOLOR="#FFFFFF"><A HREF="gtk_tut-6.html">Next</A><A HREF="gtk_tut-4.html">Previous</A><A HREF="gtk_tut.html#toc5">Contents</A><HR NOSHADE><H2><A NAME="s5">5. Widget Overview</A></H2><P>The general steps to creating a widget in GTK are:<OL><LI> gtk_*_new - one of various functions to create a new widget.These are all detailed in this section.</LI><LI> Connect all signals and events we wish to use to theappropriate handlers.</LI><LI> Set the attributes of the widget.</LI><LI> Pack the widget into a container using the appropriate callsuch as gtk_container_add() or gtk_box_pack_start().</LI><LI> gtk_widget_show() the widget.</LI></OL><P>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.<P><H2><A NAME="ss5.1">5.1 Casting</A></H2><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:<P><UL><LI> GTK_WIDGET(widget)</LI><LI> GTK_OBJECT(object)</LI><LI> GTK_SIGNAL_FUNC(function)</LI><LI> GTK_CONTAINER(container)</LI><LI> GTK_WINDOW(window)</LI><LI> GTK_BOX(box)</LI></UL><P>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.<P>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.<P>For example:<P><BLOCKQUOTE><CODE><PRE>gtk_signal_connect( GTK_OBJECT(button), "clicked",                    GTK_SIGNAL_FUNC(callback_function), callback_data);</PRE></CODE></BLOCKQUOTE> <P>This casts the button into an object, and provides a cast for thefunction pointer to the callback.<P>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.<P>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.<P><H2><A NAME="ss5.2">5.2 Widget Hierarchy</A></H2><P>For your reference, here is the class hierarchy tree used to implement widgets.<P><BLOCKQUOTE><CODE><PRE> 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</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss5.3">5.3 Widgets Without Windows</A></H2><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 <A HREF="gtk_tut-10.html#sec_EventBox">EventBox</A> widget.<P><BLOCKQUOTE><CODE><PRE>GtkAlignmentGtkArrowGtkBinGtkBoxGtkImageGtkItemGtkLabelGtkPixmapGtkScrolledWindowGtkSeparatorGtkTableGtkAspectFrameGtkFrameGtkVBoxGtkHBoxGtkVSeparatorGtkHSeparator</PRE></CODE></BLOCKQUOTE><P>We'll further our exploration of GTK by examining each widget in turn,creating a few simple functions to display them. Another good sourceis the testgtk.c program that comes with GTK. It can be found ingtk/testgtk.c.<P><HR NOSHADE><A HREF="gtk_tut-6.html">Next</A><A HREF="gtk_tut-4.html">Previous</A><A HREF="gtk_tut.html#toc5">Contents</A></BODY></HTML>

⌨️ 快捷键说明

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