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

📄 sec-creatingawidgetfromscratch.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<HTML><HEAD><TITLE>Creating a widget from scratch</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Writing Your Own Widgets"HREF="ch-writingyourownwidgets.html"><LINKREL="PREVIOUS"TITLE="Creating a Composite widget"HREF="sec-creatingacompositewidget.html"><LINKREL="NEXT"TITLE="Learning More"HREF="sec-learningmore.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%">  <tr>    <td bgcolor="#FFFFFF" valign=top nowrap>    <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A>      <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR>      </td>  <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-creatingacompositewidget.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 24. Writing Your Own Widgets</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-learningmore.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-CREATINGAWIDGETFROMSCRATCH">24.4. Creating a widget from scratch</A></H1><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN2672">24.4.1. Introduction</A></H2><P>In this section, we'll learn more about how widgets display themselveson the screen and interact with events. As an example of this, we'llcreate an analog dial widget with a pointer that the user can drag toset the value.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN2675">24.4.2. Displaying a widget on the screen</A></H2><P>There are several steps that are involved in displaying on the screen.After the widget is created with a call to <TTCLASS="LITERAL">WIDGETNAME_new()</TT>,several more functions are needed:</P><P></P><UL><LI><P> <TTCLASS="LITERAL">WIDGETNAME_realize()</TT> is responsible for creating an Xwindow for the widget if it has one.</P></LI><LI><P> <TTCLASS="LITERAL">WIDGETNAME_map()</TT> is invoked after the user calls<TTCLASS="LITERAL">gtk_widget_show()</TT>. It is responsible for making sure the widgetis actually drawn on the screen (<ICLASS="EMPHASIS">mapped</I>). For a container class,it must also make calls to <TTCLASS="LITERAL">map()</TT>&#62; functions of any child widgets.</P></LI><LI><P> <TTCLASS="LITERAL">WIDGETNAME_draw()</TT> is invoked when <TTCLASS="LITERAL">gtk_widget_draw()</TT>is called for the widget or one of its ancestors. It makes the actualcalls to the drawing functions to draw the widget on the screen. Forcontainer widgets, this function must make calls to<TTCLASS="LITERAL">gtk_widget_draw()</TT> for its child widgets.</P></LI><LI><P> <TTCLASS="LITERAL">WIDGETNAME_expose()</TT> is a handler for expose events for thewidget. It makes the necessary calls to the drawing functions to drawthe exposed portion on the screen. For container widgets, thisfunction must generate expose events for its child widgets which don'thave their own windows. (If they have their own windows, then X willgenerate the necessary expose events.)</P></LI></UL><P>You might notice that the last two functions are quite similar - eachis responsible for drawing the widget on the screen. In fact manytypes of widgets don't really care about the difference between thetwo. The default <TTCLASS="LITERAL">draw()</TT> function in the widget class simplygenerates a synthetic expose event for the redrawn area. However, sometypes of widgets can save work by distinguishing between the twofunctions. For instance, if a widget has multiple X windows, thensince expose events identify the exposed window, it can redraw onlythe affected window, which is not possible for calls to <TTCLASS="LITERAL">draw()</TT>.</P><P>Container widgets, even if they don't care about the difference forthemselves, can't simply use the default <TTCLASS="LITERAL">draw()</TT> function becausetheir child widgets might care about the difference. However,it would be wasteful to duplicate the drawing code between the twofunctions. The convention is that such widgets have a function called<TTCLASS="LITERAL">WIDGETNAME_paint()</TT> that does the actual work of drawing thewidget, that is then called by the <TTCLASS="LITERAL">draw()</TT> and <TTCLASS="LITERAL">expose()</TT>functions.</P><P>In our example approach, since the dial widget is not a containerwidget, and only has a single window, we can take the simplestapproach and use the default <TTCLASS="LITERAL">draw()</TT> function and only implementan <TTCLASS="LITERAL">expose()</TT> function.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN2708">24.4.3. The origins of the Dial Widget</A></H2><P>Just as all land animals are just variants on the first amphibian thatcrawled up out of the mud, GTK widgets tend to start off as variantsof some other, previously written widget. Thus, although this sectionis entitled "Creating a Widget from Scratch", the Dial widget reallybegan with the source code for the Range widget. This was picked as astarting point because it would be nice if our Dial had the sameinterface as the Scale widgets which are just specialized descendantsof the Range widget. So, though the source code is presented below infinished form, it should not be implied that it was written, <ICLASS="EMPHASIS">abinitio</I> in this fashion. Also, if you aren't yet familiar withhow scale widgets work from the application writer's point of view, itwould be a good idea to look them over before continuing.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN2712">24.4.4. The Basics</A></H2><P>Quite a bit of our widget should look pretty familiar from theTictactoe widget. First, we have a header file:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* GTK - The GIMP Toolkit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#ifndef __GTK_DIAL_H__#define __GTK_DIAL_H__#include &#60;gdk/gdk.h&#62;#include &#60;gtk/gtkadjustment.h&#62;#include &#60;gtk/gtkwidget.h&#62;#ifdef __cplusplusextern "C" {#endif /* __cplusplus */#define GTK_DIAL(obj)          GTK_CHECK_CAST (obj, gtk_dial_get_type (), GtkDial)#define GTK_DIAL_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, gtk_dial_get_type (), GtkDialClass)#define GTK_IS_DIAL(obj)       GTK_CHECK_TYPE (obj, gtk_dial_get_type ())typedef struct _GtkDial        GtkDial;typedef struct _GtkDialClass   GtkDialClass;struct _GtkDial{  GtkWidget widget;  /* update policy (GTK_UPDATE_[CONTINUOUS/DELAYED/DISCONTINUOUS]) */  guint policy : 2;  /* Button currently pressed or 0 if none */  guint8 button;  /* Dimensions of dial components */  gint radius;  gint pointer_width;  /* ID of update timer, or 0 if none */  guint32 timer;  /* Current angle */  gfloat angle;  /* Old values from adjustment stored so we know when something changes */  gfloat old_value;  gfloat old_lower;  gfloat old_upper;  /* The adjustment object that stores the data for this dial */  GtkAdjustment *adjustment;};struct _GtkDialClass{  GtkWidgetClass parent_class;};

⌨️ 快捷键说明

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