📄 sec-combobox.html
字号:
<HTML><HEAD><TITLE>Combo Box</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="Miscellaneous Widgets"HREF="ch-miscwidgets.html"><LINKREL="PREVIOUS"TITLE="Spin Buttons"HREF="sec-spinbuttons.html"><LINKREL="NEXT"TITLE="Calendar"HREF="sec-calendar.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-spinbuttons.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Miscellaneous Widgets</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-calendar.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-COMBOBOX">9.11. Combo Box</A></H1><P>The combo box is another fairly simple widget that is really just acollection of other widgets. From the user's point of view, the widgetconsists of a text entry box and a pull down menu from which the usercan select one of a set of predefined entries. Alternatively, the usercan type a different option directly into the text box.</P><P>The following extract from the structure that defines a Combo Boxidentifies several of the components:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">struct _GtkCombo { GtkHBox hbox; GtkWidget *entry; GtkWidget *button; GtkWidget *popup; GtkWidget *popwin; GtkWidget *list; ... };</PRE></TD></TR></TABLE><P>As you can see, the Combo Box has two principal parts that you reallycare about: an entry and a list.</P><P>First off, to create a combo box, use:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_combo_new( void );</PRE></TD></TR></TABLE><P>Now, if you want to set the string in the entry section of the combobox, this is done by manipulating the <TTCLASS="LITERAL">entry</TT> widget directly:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), "My String.");</PRE></TD></TR></TABLE><P>To set the values in the popdown list, one uses the function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_combo_set_popdown_strings( GtkCombo *combo, GList *strings );</PRE></TD></TR></TABLE><P>Before you can do this, you have to assemble a GList of the stringsthat you want. GList is a linked list implementation that is part of<AHREF="ch-glib.html">GLib</A>, a library supporing GTK. For themoment, the quick and dirty explanation is that you need to set up aGList pointer, set it equal to NULL, then append strings to it with</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GList *g_list_append( GList *glist, gpointer data );</PRE></TD></TR></TABLE><P>It is important that you set the initial GList pointer to NULL. Thevalue returned from the g_list_append function must be used as the newpointer to the GList.</P><P>Here's a typical code segment for creating a set of options:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> GList *glist=NULL; glist = g_list_append(glist, "String 1"); glist = g_list_append(glist, "String 2"); glist = g_list_append(glist, "String 3"); glist = g_list_append(glist, "String 4"); gtk_combo_set_popdown_strings( GTK_COMBO(combo), glist) ; /* can free glist now, combo takes a copy */</PRE></TD></TR></TABLE><P>The combo widget makes a copy of the strings passed to it in the gliststructure. As a result, you need to make sure you free the memory usedby the list if that is appropriate for your application.</P><P>At this point you have a working combo box that has been set up.There are a few aspects of its behavior that you can change. Theseare accomplished with the functions: </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_combo_set_use_arrows( GtkCombo *combo, gint val );void gtk_combo_set_use_arrows_always( GtkCombo *combo, gint val );void gtk_combo_set_case_sensitive( GtkCombo *combo, gint val );</PRE></TD></TR></TABLE><P><TTCLASS="LITERAL">gtk_combo_set_use_arrows()</TT> lets the user change the value in theentry using the up/down arrow keys. This doesn't bring up the list, butrather replaces the current text in the entry with the next list entry(up or down, as your key choice indicates). It does this by searchingin the list for the item corresponding to the current value in theentry and selecting the previous/next item accordingly. Usually in anentry the arrow keys are used to change focus (you can do that anywayusing TAB). Note that when the current item is the last of the listand you press arrow-down it changes the focus (the same applies withthe first item and arrow-up).</P><P>If the current value in the entry is not in the list, then thefunction of <TTCLASS="LITERAL">gtk_combo_set_use_arrows()</TT> is disabled.</P><P><TTCLASS="LITERAL">gtk_combo_set_use_arrows_always()</TT> similarly allows the use thethe up/down arrow keys to cycle through the choices in the dropdownlist, except that it wraps around the values in the list, completelydisabling the use of the up and down arrow keys for changing focus.</P><P><TTCLASS="LITERAL">gtk_combo_set_case_sensitive()</TT> toggles whether or not GTKsearches for entries in a case sensitive manner. This is used when theCombo widget is asked to find a value from the list using the currententry in the text box. This completion can be performed in either acase sensitive or insensitive manner, depending upon the use of thisfunction. The Combo widget can also simply complete the current entryif the user presses the key combination MOD-1 and "Tab". MOD-1 isoften mapped to the "Alt" key, by the <TTCLASS="LITERAL">xmodmap</TT> utility. Note,however that some window managers also use this key combination, whichwill override its use within GTK.</P><P>Now that we have a combo box, tailored to look and act how we want it,all that remains is being able to get data from the combo box. This isrelatively straightforward. The majority of the time, all you aregoing to care about getting data from is the entry. The entry isaccessed simply by <TTCLASS="LITERAL">GTK_ENTRY(GTK_COMBO(combo)->entry)</TT>. Thetwo principal things that you are going to want to do with it areattach to the activate signal, which indicates that the user haspressed the Return or Enter key, and read the text. The first isaccomplished using something like:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> gtk_signal_connect(GTK_OBJECT(GTK_COMB(combo)->entry), "activate", GTK_SIGNAL_FUNC (my_callback_function), my_data);</PRE></TD></TR></TABLE><P>Getting the text at any arbitrary time is accomplished by simply usingthe entry function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gchar *gtk_entry_get_text(GtkEntry *entry);</PRE></TD></TR></TABLE><P>Such as:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> char *string; string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));</PRE></TD></TR></TABLE><P>That's about all there is to it. There is a function</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_combo_disable_activate(GtkCombo *combo);</PRE></TD></TR></TABLE><P>that will disable the activate signal on the entry widget in the combobox. Personally, I can't think of why you'd want to use it, but itdoes exist.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-spinbuttons.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-calendar.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Spin Buttons</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-miscwidgets.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Calendar</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -