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

📄 sec-spinbuttons.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
📖 第 1 页 / 共 2 页
字号:
><P><TTCLASS="LITERAL">GTK_SPIN_PAGE_FORWARD</TT> and <TTCLASS="LITERAL">GTK_SPIN_PAGE_BACKWARD</TT> simplyalter the value of the Spin Button by <TTCLASS="LITERAL">increment</TT>.</P><P><TTCLASS="LITERAL">GTK_SPIN_HOME</TT> sets the value of the Spin Button to the bottom ofthe Adjustments range.</P><P><TTCLASS="LITERAL">GTK_SPIN_END</TT> sets the value of the Spin Button to the top of theAdjustments range.</P><P><TTCLASS="LITERAL">GTK_SPIN_USER_DEFINED</TT> simply alters the value of the Spin Buttonby the specified amount.</P><P>We move away from functions for setting and retreving the range attributesof the Spin Button now, and move onto functions that effect theappearance and behaviour of the Spin Button widget itself.</P><P>The first of these functions is used to constrain the text box of theSpin Button such that it may only contain a numeric value. Thisprevents a user from typing anything other than numeric values intothe text box of a Spin Button:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_set_numeric( GtkSpinButton *spin_button,                                  gboolean       numeric );</PRE></TD></TR></TABLE><P>You can set whether a Spin Button will wrap around between the upperand lower range values with the following function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_set_wrap( GtkSpinButton *spin_button,                               gboolean       wrap );</PRE></TD></TR></TABLE><P>You can set a Spin Button to round the value to the nearest<TTCLASS="LITERAL">step_increment</TT>, which is set within the Adjustment object usedwith the Spin Button. This is accomplished with the followingfunction:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_set_snap_to_ticks( GtkSpinButton  *spin_button,                                        gboolean        snap_to_ticks );</PRE></TD></TR></TABLE><P>The update policy of a Spin Button can be changed with the followingfunction:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_set_update_policy( GtkSpinButton  *spin_button,                                    GtkSpinButtonUpdatePolicy policy );</PRE></TD></TR></TABLE><P></P><P>The possible values of <TTCLASS="LITERAL">policy</TT> are either <TTCLASS="LITERAL">GTK_UPDATE_ALWAYS</TT> or<TTCLASS="LITERAL">GTK_UPDATE_IF_VALID</TT>.</P><P>These policies affect the behavior of a Spin Button when parsinginserted text and syncing its value with the values of theAdjustment.</P><P>In the case of <TTCLASS="LITERAL">GTK_UPDATE_IF_VALID</TT> the Spin Button only valuegets changed if the text input is a numeric value that is within therange specified by the Adjustment. Otherwise the text is reset to thecurrent value.</P><P>In case of <TTCLASS="LITERAL">GTK_UPDATE_ALWAYS</TT> we ignore errors while convertingtext into a numeric value.</P><P>The appearance of the buttons used in a Spin Button can be changedusing the following function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_set_shadow_type( GtkSpinButton *spin_button,                                      GtkShadowType  shadow_type );</PRE></TD></TR></TABLE><P>As usual, the <TTCLASS="LITERAL">shadow_type</TT> can be one of:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">  GTK_SHADOW_IN  GTK_SHADOW_OUT  GTK_SHADOW_ETCHED_IN  GTK_SHADOW_ETCHED_OUT</PRE></TD></TR></TABLE><P>Finally, you can explicitly request that a Spin Button update itself:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_spin_button_update( GtkSpinButton  *spin_button );</PRE></TD></TR></TABLE><P>It's example time again.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start spinbutton spinbutton.c */#include &#60;stdio.h&#62;#include &#60;gtk/gtk.h&#62;static GtkWidget *spinner1;void toggle_snap( GtkWidget     *widget,		  GtkSpinButton *spin ){  gtk_spin_button_set_snap_to_ticks (spin, GTK_TOGGLE_BUTTON (widget)-&#62;active);}void toggle_numeric( GtkWidget *widget,		     GtkSpinButton *spin ){  gtk_spin_button_set_numeric (spin, GTK_TOGGLE_BUTTON (widget)-&#62;active);}void change_digits( GtkWidget *widget,	            GtkSpinButton *spin ){  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinner1),                              gtk_spin_button_get_value_as_int (spin));}void get_value( GtkWidget *widget,		gpointer data ){  gchar buf[32];  GtkLabel *label;  GtkSpinButton *spin;  spin = GTK_SPIN_BUTTON (spinner1);  label = GTK_LABEL (gtk_object_get_user_data (GTK_OBJECT (widget)));  if (GPOINTER_TO_INT (data) == 1)    sprintf (buf, "%d", gtk_spin_button_get_value_as_int (spin));  else    sprintf (buf, "%0.*f", spin-&#62;digits,             gtk_spin_button_get_value_as_float (spin));  gtk_label_set_text (label, buf);}int main( int   argc,          char *argv[] ){  GtkWidget *window;  GtkWidget *frame;  GtkWidget *hbox;  GtkWidget *main_vbox;  GtkWidget *vbox;  GtkWidget *vbox2;  GtkWidget *spinner2;  GtkWidget *spinner;  GtkWidget *button;  GtkWidget *label;  GtkWidget *val_label;  GtkAdjustment *adj;  /* Initialise GTK */  gtk_init(&#38;argc, &#38;argv);  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  gtk_signal_connect (GTK_OBJECT (window), "destroy",		      GTK_SIGNAL_FUNC (gtk_main_quit),		      NULL);  gtk_window_set_title (GTK_WINDOW (window), "Spin Button");  main_vbox = gtk_vbox_new (FALSE, 5);  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 10);  gtk_container_add (GTK_CONTAINER (window), main_vbox);    frame = gtk_frame_new ("Not accelerated");  gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0);    vbox = gtk_vbox_new (FALSE, 0);  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);  gtk_container_add (GTK_CONTAINER (frame), vbox);    /* Day, month, year spinners */    hbox = gtk_hbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 5);    vbox2 = gtk_vbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5);    label = gtk_label_new ("Day :");  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);  gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0);    adj = (GtkAdjustment *) gtk_adjustment_new (1.0, 1.0, 31.0, 1.0,					      5.0, 0.0);  spinner = gtk_spin_button_new (adj, 0, 0);  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE);  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinner),				   GTK_SHADOW_OUT);  gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0);    vbox2 = gtk_vbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5);    label = gtk_label_new ("Month :");  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);  gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0);    adj = (GtkAdjustment *) gtk_adjustment_new (1.0, 1.0, 12.0, 1.0,					      5.0, 0.0);  spinner = gtk_spin_button_new (adj, 0, 0);  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE);  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinner),				   GTK_SHADOW_ETCHED_IN);  gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0);    vbox2 = gtk_vbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5);    label = gtk_label_new ("Year :");  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);  gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0);    adj = (GtkAdjustment *) gtk_adjustment_new (1998.0, 0.0, 2100.0,					      1.0, 100.0, 0.0);  spinner = gtk_spin_button_new (adj, 0, 0);  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), FALSE);  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinner),				   GTK_SHADOW_IN);  gtk_widget_set_usize (spinner, 55, 0);  gtk_box_pack_start (GTK_BOX (vbox2), spinner, FALSE, TRUE, 0);    frame = gtk_frame_new ("Accelerated");  gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0);    vbox = gtk_vbox_new (FALSE, 0);  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);  gtk_container_add (GTK_CONTAINER (frame), vbox);    hbox = gtk_hbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5);    vbox2 = gtk_vbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5);    label = gtk_label_new ("Value :");  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);  gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0);    adj = (GtkAdjustment *) gtk_adjustment_new (0.0, -10000.0, 10000.0,					      0.5, 100.0, 0.0);  spinner1 = gtk_spin_button_new (adj, 1.0, 2);  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner1), TRUE);  gtk_widget_set_usize (spinner1, 100, 0);  gtk_box_pack_start (GTK_BOX (vbox2), spinner1, FALSE, TRUE, 0);    vbox2 = gtk_vbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 5);    label = gtk_label_new ("Digits :");  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);  gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, TRUE, 0);    adj = (GtkAdjustment *) gtk_adjustment_new (2, 1, 5, 1, 1, 0);  spinner2 = gtk_spin_button_new (adj, 0.0, 0);  gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner2), TRUE);  gtk_signal_connect (GTK_OBJECT (adj), "value_changed",		      GTK_SIGNAL_FUNC (change_digits),		      (gpointer) spinner2);  gtk_box_pack_start (GTK_BOX (vbox2), spinner2, FALSE, TRUE, 0);    hbox = gtk_hbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5);    button = gtk_check_button_new_with_label ("Snap to 0.5-ticks");  gtk_signal_connect (GTK_OBJECT (button), "clicked",		      GTK_SIGNAL_FUNC (toggle_snap),		      spinner1);  gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);    button = gtk_check_button_new_with_label ("Numeric only input mode");  gtk_signal_connect (GTK_OBJECT (button), "clicked",		      GTK_SIGNAL_FUNC (toggle_numeric),		      spinner1);  gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);    val_label = gtk_label_new ("");    hbox = gtk_hbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 5);  button = gtk_button_new_with_label ("Value as Int");  gtk_object_set_user_data (GTK_OBJECT (button), val_label);  gtk_signal_connect (GTK_OBJECT (button), "clicked",		      GTK_SIGNAL_FUNC (get_value),		      GINT_TO_POINTER (1));  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5);    button = gtk_button_new_with_label ("Value as Float");  gtk_object_set_user_data (GTK_OBJECT (button), val_label);  gtk_signal_connect (GTK_OBJECT (button), "clicked",		      GTK_SIGNAL_FUNC (get_value),		      GINT_TO_POINTER (2));  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5);    gtk_box_pack_start (GTK_BOX (vbox), val_label, TRUE, TRUE, 0);  gtk_label_set_text (GTK_LABEL (val_label), "0");    hbox = gtk_hbox_new (FALSE, 0);  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, TRUE, 0);    button = gtk_button_new_with_label ("Close");  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",			     GTK_SIGNAL_FUNC (gtk_widget_destroy),			     GTK_OBJECT (window));  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 5);  gtk_widget_show_all (window);  /* Enter the event loop */  gtk_main ();      return(0);}/* example-end */</PRE></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-textentries.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-combobox.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Text Entries</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-miscwidgets.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Combo Box</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

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