📄 gtk_tut-9.html
字号:
/* Initialize GTK and create the main window */ gtk_init( &argc, &argv ); window = gtk_window_new( GTK_WINDOW_TOPLEVEL ); gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC( close_application ), NULL); gtk_container_set_border_width (GTK_CONTAINER (window), 10); /* Create a table for placing the ruler and the drawing area */ table = gtk_table_new( 3, 2, FALSE ); gtk_container_add( GTK_CONTAINER(window), table ); area = gtk_drawing_area_new(); gtk_drawing_area_size( (GtkDrawingArea *)area, XSIZE, YSIZE ); gtk_table_attach( GTK_TABLE(table), area, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 ); gtk_widget_set_events( area, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ); /* The horizontal ruler goes on top. As the mouse moves across the * drawing area, a motion_notify_event is passed to the * appropriate event handler for the ruler. */ hrule = gtk_hruler_new(); gtk_ruler_set_metric( GTK_RULER(hrule), GTK_PIXELS ); gtk_ruler_set_range( GTK_RULER(hrule), 7, 13, 0, 20 ); gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event", (GtkSignalFunc)EVENT_METHOD(hrule, motion_notify_event), GTK_OBJECT(hrule) ); /* GTK_WIDGET_CLASS(GTK_OBJECT(hrule)->klass)->motion_notify_event, */ gtk_table_attach( GTK_TABLE(table), hrule, 1, 2, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, GTK_FILL, 0, 0 ); /* The vertical ruler goes on the left. As the mouse moves across * the drawing area, a motion_notify_event is passed to the * appropriate event handler for the ruler. */ vrule = gtk_vruler_new(); gtk_ruler_set_metric( GTK_RULER(vrule), GTK_PIXELS ); gtk_ruler_set_range( GTK_RULER(vrule), 0, YSIZE, 10, YSIZE ); gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event", (GtkSignalFunc) GTK_WIDGET_CLASS(GTK_OBJECT(vrule)->klass)-> motion_notify_event, GTK_OBJECT(vrule) ); gtk_table_attach( GTK_TABLE(table), vrule, 0, 1, 1, 2, GTK_FILL, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0 ); /* Now show everything */ gtk_widget_show( area ); gtk_widget_show( hrule ); gtk_widget_show( vrule ); gtk_widget_show( table ); gtk_widget_show( window ); gtk_main(); return(0);}/* example-end */</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss9.8">9.8 Statusbars</A></H2><P>Statusbars are simple widgets used to display a text message. Theykeep a stack of the messages pushed onto them, so that popping thecurrent message will re-display the previous text message.<P>In order to allow different parts of an application to use the samestatusbar to display messages, the statusbar widget issues ContextIdentifiers which are used to identify different 'users'. The messageon top of the stack is the one displayed, no matter what context it isin. Messages are stacked in last-in-first-out order, not contextidentifier order.<P>A statusbar is created with a call to:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_statusbar_new( void );</PRE></CODE></BLOCKQUOTE><P>A new Context Identifier is requested using a call to the following function with a short textual description of the context:<P><BLOCKQUOTE><CODE><PRE>guint gtk_statusbar_get_context_id( GtkStatusbar *statusbar, const gchar *context_description );</PRE></CODE></BLOCKQUOTE><P>There are three functions that can operate on statusbars:<P><BLOCKQUOTE><CODE><PRE>guint gtk_statusbar_push( GtkStatusbar *statusbar, guint context_id, gchar *text );void gtk_statusbar_pop( GtkStatusbar *statusbar) guint context_id );void gtk_statusbar_remove( GtkStatusbar *statusbar, guint context_id, guint message_id ); </PRE></CODE></BLOCKQUOTE><P>The first, gtk_statusbar_push, is used to add a new message to thestatusbar. It returns a Message Identifier, which can be passed laterto the function gtk_statusbar_remove to remove the message with thegiven Message and Context Identifiers from the statusbar's stack.<P>The function gtk_statusbar_pop removes the message highest in thestack with the given Context Identifier.<P>The following example creates a statusbar and two buttons, one forpushing items onto the statusbar, and one for popping the last itemback off.<P><BLOCKQUOTE><CODE><PRE>/* example-start statusbar statusbar.c */#include <gtk/gtk.h>#include <glib.h>GtkWidget *status_bar;void push_item (GtkWidget *widget, gpointer data){ static int count = 1; char buff[20]; g_snprintf(buff, 20, "Item %d", count++); gtk_statusbar_push( GTK_STATUSBAR(status_bar), GPOINTER_TO_INT(data), buff); return;}void pop_item (GtkWidget *widget, gpointer data){ gtk_statusbar_pop( GTK_STATUSBAR(status_bar), GPOINTER_TO_INT(data) ); return;}int main (int argc, char *argv[]){ GtkWidget *window; GtkWidget *vbox; GtkWidget *button; gint context_id; gtk_init (&argc, &argv); /* create a new window */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_usize( GTK_WIDGET (window), 200, 100); gtk_window_set_title(GTK_WINDOW (window), "GTK Statusbar Example"); gtk_signal_connect(GTK_OBJECT (window), "delete_event", (GtkSignalFunc) gtk_exit, NULL); vbox = gtk_vbox_new(FALSE, 1); gtk_container_add(GTK_CONTAINER(window), vbox); gtk_widget_show(vbox); status_bar = gtk_statusbar_new(); gtk_box_pack_start (GTK_BOX (vbox), status_bar, TRUE, TRUE, 0); gtk_widget_show (status_bar); context_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(status_bar), "Statusbar example"); button = gtk_button_new_with_label("push item"); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC (push_item), GINT_TO_POINTER(context_id) ); gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2); gtk_widget_show(button); button = gtk_button_new_with_label("pop last item"); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC (pop_item), GINT_TO_POINTER(context_id) ); gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2); gtk_widget_show(button); /* always display the window as the last step so it all splashes on * the screen at once. */ gtk_widget_show(window); gtk_main (); return 0;}/* example-end */</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss9.9">9.9 Text Entries</A></H2><P>The Entry widget allows text to be typed and displayed in a single linetext box. The text may be set with function calls that allow new textto replace, prepend or append the current contents of the Entry widget.<P>There are two functions for creating Entry widgets:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_entry_new( void );GtkWidget *gtk_entry_new_with_max_length( guint16 max );</PRE></CODE></BLOCKQUOTE><P>The first just creates a new Entry widget, whilst the second creates anew Entry and sets a limit on the length of the text within the Entry.<P>There are several functions for altering the text which is currentlywithin the Entry widget.<P><BLOCKQUOTE><CODE><PRE>void gtk_entry_set_text( GtkEntry *entry, const gchar *text );void gtk_entry_append_text( GtkEntry *entry, const gchar *text );void gtk_entry_prepend_text( GtkEntry *entry, const gchar *text );</PRE></CODE></BLOCKQUOTE><P>The function gtk_entry_set_text sets the contents of the Entry widget,replacing the current contents. The functions gtk_entry_append_textand gtk_entry_prepend_text allow the current contents to be appendedand prepended to.<P>The next function allows the current insertion point to be set.<P><BLOCKQUOTE><CODE><PRE>void gtk_entry_set_position( GtkEntry *entry, gint position );</PRE></CODE></BLOCKQUOTE><P>The contents of the Entry can be retrieved by using a call to thefollowing function. This is useful in the callback functions described below.<P><BLOCKQUOTE><CODE><PRE>gchar *gtk_entry_get_text( GtkEntry *entry );</PRE></CODE></BLOCKQUOTE><P>The value returned by this function is used internally, and must notbe freed using either free() or g_free()<P>If we don't want the contents of the Entry to be changed by someone typinginto it, we can change its editable state.<P><BLOCKQUOTE><CODE><PRE>void gtk_entry_set_editable( GtkEntry *entry, gboolean editable );</PRE></CODE></BLOCKQUOTE><P>The function above allows us to toggle the editable state of theEntry widget by passing in a TRUE or FALSE value for the <CODE>editable</CODE>argument.<P>If we are using the Entry where we don't want the text entered to bevisible, for example when a password is being entered, we can use thefollowing function, which also takes a boolean flag.<P><BLOCKQUOTE><CODE><PRE>void gtk_entry_set_visibility( GtkEntry *entry, gboolean visible );</PRE></CODE></BLOCKQUOTE><P>A region of the text may be set as selected by using the followingfunction. This would most often be used after setting some defaulttext in an Entry, making it easy for the user to remove it.<P><BLOCKQUOTE><CODE><PRE>void gtk_entry_select_region( GtkEntry *entry, gint start, gint end );</PRE></CODE></BLOCKQUOTE><P>If we want to catch when the user has entered text, we can connect tothe <CODE>activate</CODE> or <CODE>changed</CODE> signal. Activate is raised when theuser hits the enter key within the Entry widget. Changed is raisedwhen the text changes at all, e.g. for every character entered orremoved.<P>The following code is an example of using an Entry widget.<P><BLOCKQUOTE><CODE><PRE>/* example-start entry entry.c */#include <gtk/gtk.h>void enter_callback(GtkWidget *widget, GtkWidget *entry){ gchar *entry_text; entry_text = gtk_entry_get_text(GTK_ENTRY(entry)); printf("Entry contents: %s\n", entry_text);}void entry_toggle_editable (GtkWidget *checkbutton, GtkWidget *entry){ gtk_entry_set_editable(GTK_ENTRY(entry), GTK_TOGGLE_BUTTON(checkbutton)->active);}void entry_toggle_visibility (GtkWidget *checkbutton, GtkWidget *entry){ gtk_entry_set_visibility(GTK_ENTRY(entry), GTK_TOGGLE_BUTTON(checkbutton)->active);}int main (int argc, char *argv[]){ GtkWidget *window; GtkWidget *vbox, *hbox; GtkWidget *entry; GtkWidget *button; GtkWidget *check; gtk_init (&argc, &argv); /* create a new window */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_usize( GTK_WIDGET (window), 200, 100); gtk_window_set_title(GTK_WINDOW (window), "GTK Entry"); gtk_signal_connect(GTK_OBJECT (window), "delete_event", (GtkSignalFunc) gtk_exit, NULL); vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); gtk_widget_show (vbox); entry = gtk_entry_new_with_max_length (50); gtk_signal_connect(GTK_OBJECT(entry), "activate", GTK_SIGNAL_FUNC(enter_callback), entry); gtk_entry_set_text (GTK_ENTRY (entry), "hello"); gtk_entry_append_text (GTK_ENTRY (entry), " world"); gtk_entry_select_region (GTK_ENTRY (entry), 0, GTK_ENTRY(entry)->text_length); gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0); gtk_widget_show (entry); hbox = gtk_hbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (vbox), hbox); gtk_widget_show (hbox); check = gtk_check_button_new_with_label("Editable"); gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0); gtk_signal_connect (GTK_OBJECT(check), "toggled", GTK_SIGNAL_FUNC(entry_toggle_editable), entry); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE); gtk_widget_show (check); check = gtk_check_button_new_with_label("Visible"); gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0); gtk_signal_connect (GTK_OBJECT(check), "toggled", GTK_SIGNAL_FUNC(entry_toggle_visibility), entry); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE); gtk_widget_show (check); button = gtk_button_new_with_label ("Close"); gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FU
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -