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

📄 gtkfaq-5.html

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 HTML
📖 第 1 页 / 共 2 页
字号:
                     GTK_SIGNAL_FUNC(signal_handler_event),                     NULL);  /* something else */}</PRE></CODE></BLOCKQUOTE><P>and, Owen Taylor wrote:<P>Note that a single button press will be received beforehand, andif you are doing this for a button, you will therefore also get a"clicked" signal for the button. (This is going to be true forany toolkit, since computers aren't good at reading one'smind.)<P><H2><A NAME="ss5.7">5.7 By the way, what are the differences between signals and events?</A></H2><P>First of all, Havoc Pennington gives a rather complete description of thedifferences between events and signals in his free book (two chapters canbe found at <A HREF="http://www106.pair.com/rhp/sample_chapters.html">http://www106.pair.com/rhp/sample_chapters.html</A>).<P>Moreover, Havoc posted this to the <CODE>gtk-list</CODE><BLOCKQUOTE>Events are a stream of messages received from the X server. They drive theGtk main loop; which more or less amounts to "wait for events, processthem" (not exactly, it is really more general than that and can wait onmany different input streams at once). Events are a Gdk/Xlib concept.<P>Signals are a feature of GtkObject and its subclasses. They have nothingto do with any input stream; really a signal is just a way to keep a listof callbacks around and invoke them ("emit" the signal). There are lots ofdetails and extra features of course. Signals are emitted by objectinstances, and are entirely unrelated to the Gtk main loop.Conventionally, signals are emitted "when something changes" about theobject emitting the signal.<P>Signals and events only come together because GtkWidget happens to emitsignals when it gets events. This is purely a convenience, so you canconnect callbacks to be invoked when a particular widget receives aparticular event. There is nothing about this that makes signals andevents inherently related concepts, any more than emitting a signal whenyou click a button makes button clicking and signals related concepts.</BLOCKQUOTE><P><H2><A NAME="ss5.8">5.8 I have my signal connected to the the (whatever) event, but it seems I don't catch it. What's wrong?</A></H2><P>There is some special initialisation to do in order to catch some particular events. In fact, you must set the correct event mask bit of yourwidget before getting some particular events.<P>For example, <P><BLOCKQUOTE><CODE><PRE>  gtk_widget_add_events(window, GDK_KEY_RELEASE_MASK);</PRE></CODE></BLOCKQUOTE><P>lets you catch the key release events. If you want to catch every events,simply us the GDK_ALL_EVENTS_MASK event mask.<P>All the event masks are defined in the <CODE>gdktypes.h</CODE> file.<P><H2><A NAME="ss5.9">5.9 Is it possible to get some text displayed which is truncated to fit inside its allocation? </A></H2><P>GTK's behavior (no clipping) is a consequence of its attempts toconserve X resources. Label widgets (among others) don't get their ownX window - they just draw their contents on their parent's window.While it might be possible to have clipping occur by setting the clipmask before drawing the text, this would probably cause a substantialperformance penalty.<P>Its possible that, in the long term, the best solution to suchproblems might be just to change gtk to give labels X windows.A short term workaround is to put the label widget inside anotherwidget that does get it's own window - one possible candidate wouldbe the viewport widget.<P><BLOCKQUOTE><CODE><PRE>viewport = gtk_viewport (NULL, NULL);gtk_widget_set_usize (viewport, 50, 25);gtk_viewport_set_shadow_type (GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);gtk_widget_show(viewport);label = gtk_label ("a really long label that won't fit");gtk_container_add (GTK_CONTAINER(viewport), label);gtk_widget_show (label);</PRE></CODE></BLOCKQUOTE><P>If you were doing this for a bunch of widgets, you might want tocopy gtkviewport.c and strip out the adjustment and shadowfunctionality (perhaps you could call it GtkClipper).<P><P><H2><A NAME="ss5.10">5.10 How do I make my window modal? / How do I make a single window active?</A></H2><P>After you create your window, do <CODE>gtk_grab_add(my_window)</CODE>. And after closing the window do <CODE>gtk_grab_remove(my_window)</CODE>.<P><H2><A NAME="ss5.11">5.11 Why doesn't my widget (e.g. progressbar) update?</A></H2><P><P>You are probably doing all the changes within a function without returning control to <CODE>gtk_main()</CODE>. This may be the case if you do some lengthy calculation in your code. Most drawing updates are only placed on a queue, which is processed within <CODE>gtk_main()</CODE>. You canforce the drawing queue to be processed using something like:<P><BLOCKQUOTE><CODE><PRE>while (gtk_events_pending())        gtk_main_iteration();</PRE></CODE></BLOCKQUOTE><P>inside you're function that changes the widget.<P>What the above snippet does is run all pending events and high priority idle functions, then return immediately (the drawing is done in a high priority idle function).<P><H2><A NAME="ss5.12">5.12 How do I attach data to some GTK+ object/widget?</A></H2><P>First of all, the attached data is stored in the object_data field of a GtkObject. The type of this field is GData, which is defined in glib.h. So you should read the gdataset.c file in your glib source directory very carefully.<P>There are two (easy) ways to attach some data to a gtk object. Using <CODE>gtk_object_set_data()</CODE> and <CODE>gtk_object_get_data()</CODE> seems to be the most common way to do this, as it provides a powerfull interface to connect objects and data.<P><BLOCKQUOTE><CODE><PRE>void gtk_object_set_data(GtkObject *object, const gchar *key, gpointer data);gpointer gtk_object_get_data(GtkObject *object, const gchar *key);</PRE></CODE></BLOCKQUOTE><P>Since a short example is better than any lengthy speech:<P><BLOCKQUOTE><CODE><PRE>struct my_struct        p1,p2,*result;GtkWidget               *w;gtk_object_set_data(GTK_OBJECT(w),"p1 data",(gpointer)&amp;p1);gtk_object_set_data(GTK_OBJECT(w),"p2 data",(gpointer)&amp;p2);result = gtk_object_get_data(GTK_OBJECT(w),"p1 data");</PRE></CODE></BLOCKQUOTE><P>The <CODE>gtk_object_set_user_data()</CODE> and <CODE>gtk_object_get_user_data()</CODE>functions does exactly the same thingas the functions above, but does not let you specify the "key" parameter.Instead, it uses a standard "user_data" key. Note that the use of thesefunctions is deprecated in 1.2. They only provide a compatibility mode with some old gtk packages.<P><H2><A NAME="ss5.13">5.13 How do I remove the data I have attached to an object?</A></H2><P>When attaching the data to the object, you can use the <CODE>gtk_object_set_data_full()</CODE>function. The three first arguments of the function are the same as in<CODE>gtk_object_set_data()</CODE>. The fourth one is a pointer to a callback functionwhich is called when the data is destroyed. The data is destroyed whenyou:<P><UL><LI> destroy the object</LI><LI> replace the data with a new one (with the same key)</LI><LI> replace the data with NULL (with the same key)</LI></UL><P><H2><A NAME="ss5.14">5.14 How could I get any widgets position?</A></H2><P>As Tim Janik pointed out, there are different cases, and each case requiresa different solution.<P><UL><LI>  If you want the position of a widget relative to its parent, you shoulduse <CODE>widget->allocation.x</CODE> and <CODE>widget->allocation.y</CODE>.</LI><LI>  If you want the position of a window relative to the X root window,you should use <CODE>gdk_window_get_geometry()</CODE> or <CODE>gdk_window_get_origin()</CODE>.</LI><LI>  Last but not least, if you want to get a Window Manager frame position,you should use <CODE>gdk_window_get_deskrelative_origin()</CODE>.</LI></UL><P><H2><A NAME="ss5.15">5.15 How do I set the size of a widget/window? How do I prevent the user resizing my window?</A></H2><P>The <CODE>gtk_widget_set_uposition()</CODE> function is used to set theposition of any widget.<P>The <CODE>gtk_widget_set_usize()</CODE> function is used to set thesize of a widget. In order to use all the features that are provided bythis function when it acts on a window, you may want to use the<CODE>gtk_window_set_policy</CODE> function. The definition of these functionsis:<P><BLOCKQUOTE><CODE><PRE>void        gtk_widget_set_usize (GtkWidget *widget,                                  gint width,                                  gint height);void        gtk_window_set_policy (GtkWindow *window,                                   gint allow_shrink,                                   gint allow_grow,                                   gint auto_shrink);</PRE></CODE></BLOCKQUOTE><P><CODE>Auto_shrink</CODE> will automatically shrink the window when therequested size of the child widgets goes below the current size of thewindow. <CODE>Allow_shrink</CODE> will give the user the authorisation tomake the window smaller that it should normally be. <CODE>Allow_grow</CODE>will give the user will have the ability to make the windowbigger. The default values for these parameters are:<P><BLOCKQUOTE><CODE><PRE>allow_shrink = FALSEallow_grow   = TRUEauto_shrink  = FALSE</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss5.16">5.16 How do I add a popup menu to my GTK+ application?</A></H2><P>The <CODE>menu</CODE> example in the examples/menu directory of the GTK+ distributionimplements a popup menu with this technique :<P><BLOCKQUOTE><CODE><PRE>static gint button_press (GtkWidget *widget, GdkEvent *event){    if (event->type == GDK_BUTTON_PRESS) {        GdkEventButton *bevent = (GdkEventButton *) event;         gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,                        bevent->button, bevent->time);        /* Tell calling code that we have handled this event; the buck         * stops here. */        return TRUE;    }    /* Tell calling code that we have not handled this event; pass it on. */    return FALSE;}</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss5.17">5.17 How do I disable or enable a widget, such as a button?</A></H2><P>To disable (or to enable) a widget, use the <CODE>gtk_widget_set_sensitive()</CODE>function. The first parameter is you widget pointer. The second parameteris a boolean value: when this value is TRUE, the widget is enabled.<P><P><H2><A NAME="ss5.18">5.18 Shouldn't the text argument in the gtk_clist_* functions be declared const?</A></H2><P>For example:<PRE>gint gtk_clist_prepend (GtkCList    *clist,                        gchar       *text[]);</PRE><P>Answer: No, while a type "gchar*" (pointer to char) can automaticallybe cast into "const gchar*" (pointer to const char), this does notapply for "gchar *[]" (array of an unspecified number of pointers tochar) into "const gchar *[]" (array of an unspecified number ofpointers to const char).<P>The type qualifier "const" may be subject to automatic casting, butin the array case, it is not the array itself that needs the (const)qualified cast, but its members, thus changing the whole type.<P><HR NOSHADE><A HREF="gtkfaq-6.html">Next</A><A HREF="gtkfaq-4.html">Previous</A><A HREF="gtkfaq.html#toc5">Contents</A></BODY></HTML>

⌨️ 快捷键说明

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