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

📄 gtk_tut-7.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: Adjustments </TITLE> <LINK HREF="gtk_tut-8.html" REL=next> <LINK HREF="gtk_tut-6.html" REL=previous> <LINK HREF="gtk_tut.html#toc7" REL=contents></HEAD><BODY BGCOLOR="#FFFFFF"><A HREF="gtk_tut-8.html">Next</A><A HREF="gtk_tut-6.html">Previous</A><A HREF="gtk_tut.html#toc7">Contents</A><HR NOSHADE><H2><A NAME="sec_Adjustment"></A> <A NAME="s7">7. Adjustments </A></H2><P>GTK+ has various widgets that can be visually adjusted by the userusing the mouse or the keyboard, such as the range widgets, describedin the <A HREF="gtk_tut-8.html#sec_Range_Widgets">Range Widgets</A>section. There are also a few widgets that display some adjustableportion of a larger area of data, such as the text widget and theviewport widget.<P>Obviously, an application needs to be able to react to changes theuser makes in range widgets. One way to do this would be to have eachwidget emit its own type of signal when its adjustment changes, andeither pass the new value to the signal handler, or require it to lookinside the widget's data structure in order to ascertain the value.But you may also want to connect the adjustments of several widgetstogether, so that adjusting one adjusts the others. The most obviousexample of this is connecting a scrollbar to a panning viewport or ascrolling text area. If each widget has its own way of setting orgetting the adjustment value, then the programmer may have to writetheir own signal handlers to translate between the output of onewidget's signal and the "input" of another's adjustment settingfunction.<P>GTK+ solves this problem using the GtkAdjustment object, which is away for widgets to store and pass adjustment information in anabstract and flexible form. The most obvious use of GtkAdjustment isto store the configuration parameters and values of range widgets,such as scrollbars and scale controls. However, since GtkAdjustmentsare derived from GtkObject, they have some special powers beyond thoseof normal data structures. Most importantly, they can emit signals,just like widgets, and these signals can be used not only to allowyour program to react to user input on adjustable widgets, but also topropagate adjustment values transparently between adjustable widgets.<P><H2><A NAME="ss7.1">7.1 Creating an Adjustment</A></H2><P>You create an adjustment using:<P><BLOCKQUOTE><CODE><PRE>GtkObject *gtk_adjustment_new( gfloat value,                               gfloat lower,                               gfloat upper,                               gfloat step_increment,                               gfloat page_increment,                               gfloat page_size );</PRE></CODE></BLOCKQUOTE><P>The <CODE>value</CODE> argument is the initial value you want to give to theadjustment, usually corresponding to the topmost or leftmost positionof an adjustable widget. The <CODE>lower</CODE> argument specifies the lowestvalue which the adjustment can hold. The <CODE>step_increment</CODE> argumentspecifies the "smaller" of the two increments by which the user canchange the value, while the <CODE>page_increment</CODE> is the "larger" one.The <CODE>page_size</CODE> argument usually corresponds somehow to the visiblearea of a panning widget. The <CODE>upper</CODE> argument is used to representthe bottom most or right most coordinate in a panning widget'schild. Therefore it is <EM>not</EM> always the largest number that<CODE>value</CODE> can take, since the <CODE>page_size</CODE> of such widgets isusually non-zero.<P><H2><A NAME="ss7.2">7.2 Using Adjustments the Easy Way</A></H2><P>The adjustable widgets can be roughly divided into those which use andrequire specific units for these values and those which treat them asarbitrary numbers. The group which treats the values as arbitrarynumbers includes the range widgets (scrollbars and scales, theprogress bar widget, and the spin button widget). These widgets areall the widgets which are typically "adjusted" directly by the userwith the mouse or keyboard. They will treat the <CODE>lower</CODE> and<CODE>upper</CODE> values of an adjustment as a range within which the usercan manipulate the adjustment's <CODE>value</CODE>. By default, they will onlymodify the <CODE>value</CODE> of an adjustment.<P>The other group includes the text widget, the viewport widget, thecompound list widget, and the scrolled window widget. All of thesewidgets use pixel values for their adjustments. These are also allwidgets which are typically "adjusted" indirectly using scrollbars.While all widgets which use adjustments can either create their ownadjustments or use ones you supply, you'll generally want to let thisparticular category of widgets create its own adjustments. Usually,they will eventually override all the values except the <CODE>value</CODE>itself in whatever adjustments you give them, but the results are, ingeneral, undefined (meaning, you'll have to read the source code tofind out, and it may be different from widget to widget).<P>Now, you're probably thinking, since text widgets and viewports insiston setting everything except the <CODE>value</CODE> of their adjustments,while scrollbars will <EM>only</EM> touch the adjustment's <CODE>value</CODE>, ifyou <EM>share</EM> an adjustment object between a scrollbar and a textwidget, manipulating the scrollbar will automagically adjust the textwidget?  Of course it will! Just like this:<P><BLOCKQUOTE><CODE><PRE>  /* creates its own adjustments */  text = gtk_text_new (NULL, NULL);  /* uses the newly-created adjustment for the scrollbar as well */  vscrollbar = gtk_vscrollbar_new (GTK_TEXT(text)->vadj);</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss7.3">7.3 Adjustment Internals</A></H2><P>Ok, you say, that's nice, but what if I want to create my own handlersto respond when the user adjusts a range widget or a spin button, andhow do I get at the value of the adjustment in these handlers?  Toanswer these questions and more, let's start by taking a look at<CODE>struct _GtkAdjustment</CODE> itself:<P><BLOCKQUOTE><CODE><PRE>struct _GtkAdjustment{  GtkData data;    gfloat lower;  gfloat upper;  gfloat value;  gfloat step_increment;  gfloat page_increment;  gfloat page_size;};     </PRE></CODE></BLOCKQUOTE><P>The first thing you should know is that there aren't any handy-dandymacros or accessor functions for getting the <CODE>value</CODE> out of aGtkAdjustment, so you'll have to (horror of horrors) do it like a<EM>real</EM> C programmer.  Don't worry - the <CODE>GTK_ADJUSTMENT(Object)</CODE> macro does run-time type checking (as do all the GTK+type-casting macros, actually).<P>Since, when you set the <CODE>value</CODE> of an adjustment, you generallywant the change to be reflected by every widget that uses thisadjustment, GTK+ provides this convenience function to do this:<P><BLOCKQUOTE><CODE><PRE>void gtk_adjustment_set_value( GtkAdjustment *adjustment,                               gfloat         value );</PRE></CODE></BLOCKQUOTE><P>As mentioned earlier, GtkAdjustment is a subclass of GtkObject justlike all the various widgets, and thus it is able to emit signals.This is, of course, why updates happen automagically when you share anadjustment object between a scrollbar and another adjustable widget;all adjustable widgets connect signal handlers to their adjustment's<CODE>value_changed</CODE> signal, as can your program. Here's the definitionof this signal in <CODE>struct _GtkAdjustmentClass</CODE>:<P><BLOCKQUOTE><CODE><PRE>  void (* value_changed) (GtkAdjustment *adjustment);</PRE></CODE></BLOCKQUOTE><P>The various widgets that use the GtkAdjustment object will emit thissignal on an adjustment whenever they change its value. This happensboth when user input causes the slider to move on a range widget, aswell as when the program explicitly changes the value with<CODE>gtk_adjustment_set_value()</CODE>. So, for example, if you have a scalewidget, and you want to change the rotation of a picture whenever itsvalue changes, you would create a callback like this:<P><BLOCKQUOTE><CODE><PRE>void cb_rotate_picture (GtkAdjustment *adj, GtkWidget *picture){  set_picture_rotation (picture, adj->value);...</PRE></CODE></BLOCKQUOTE><P>and connect it to the scale widget's adjustment like this:<P><BLOCKQUOTE><CODE><PRE>gtk_signal_connect (GTK_OBJECT (adj), "value_changed",                    GTK_SIGNAL_FUNC (cb_rotate_picture), picture);</PRE></CODE></BLOCKQUOTE><P>What about when a widget reconfigures the <CODE>upper</CODE> or <CODE>lower</CODE>fields of its adjustment, such as when a user adds more text to a textwidget?  In this case, it emits the <CODE>changed</CODE> signal, which lookslike this:<P><BLOCKQUOTE><CODE><PRE>  void (* changed)       (GtkAdjustment *adjustment);</PRE></CODE></BLOCKQUOTE><P>Range widgets typically connect a handler to this signal, whichchanges their appearance to reflect the change - for example, the sizeof the slider in a scrollbar will grow or shrink in inverse proportionto the difference between the <CODE>lower</CODE> and <CODE>upper</CODE> values of itsadjustment.<P>You probably won't ever need to attach a handler to this signal,unless you're writing a new type of range widget.  However, if youchange any of the values in a GtkAdjustment directly, you should emitthis signal on it to reconfigure whatever widgets are using it, likethis:<P><BLOCKQUOTE><CODE><PRE>gtk_signal_emit_by_name (GTK_OBJECT (adjustment), "changed");</PRE></CODE></BLOCKQUOTE><P>Now go forth and adjust!<HR NOSHADE><A HREF="gtk_tut-8.html">Next</A><A HREF="gtk_tut-6.html">Previous</A><A HREF="gtk_tut.html#toc7">Contents</A></BODY></HTML>

⌨️ 快捷键说明

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