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

📄 gtk_tut-9.html

📁 GTK development guide
💻 HTML
📖 第 1 页 / 共 5 页
字号:
    gtk_ruler_set_range( GTK_RULER(vruler), 0, 800, 0, 800);
</PRE>
</CODE></BLOCKQUOTE>
<P>The markings displayed on the ruler will be from 0 to 800, with a
number for every 100 pixels. If instead we wanted the ruler to range
from 7 to 16, we would code
<P>
<BLOCKQUOTE><CODE>
<PRE>
    gtk_ruler_set_range( GTK_RULER(vruler), 7, 16, 0, 20);
</PRE>
</CODE></BLOCKQUOTE>
<P>The indicator on the ruler is a small triangular mark that indicates
the position of the pointer relative to the ruler. If the ruler is
used to follow the mouse pointer, the motion_notify_event signal
should be connected to the motion_notify_event method of the ruler.
To follow all mouse movements within a window area, we would use
<P>
<BLOCKQUOTE><CODE>
<PRE>
#define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x

    gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
           (GtkSignalFunc)EVENT_METHOD(ruler, motion_notify_event),
           GTK_OBJECT(ruler) );
</PRE>
</CODE></BLOCKQUOTE>
<P>The following example creates a drawing area with a horizontal ruler
above it and a vertical ruler to the left of it. The size of the
drawing area is 600 pixels wide by 400 pixels high. The horizontal
ruler spans from 7 to 13 with a mark every 100 pixels, while the
vertical ruler spans from 0 to 400 with a mark every 100 pixels.
Placement of the drawing area and the rulers is done using a table.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start rulers rulers.c */

#include &lt;gtk/gtk.h>

#define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x

#define XSIZE  600
#define YSIZE  400

/* This routine gets control when the close button is clicked */
gint close_application( GtkWidget *widget,
                        GdkEvent  *event,
                        gpointer   data )
{
    gtk_main_quit();
    return(FALSE);
}

/* The main routine */
int main( int   argc,
          char *argv[] ) {
    GtkWidget *window, *table, *area, *hrule, *vrule;

    /* Initialize GTK and create the main window */
    gtk_init( &amp;argc, &amp;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. They
keep a stack of the messages pushed onto them, so that popping the
current message will re-display the previous text message.
<P>In order to allow different parts of an application to use the same
statusbar to display messages, the statusbar widget issues Context
Identifiers which are used to identify different "users". The message
on top of the stack is the one displayed, no matter what context it is
in. Messages are stacked in last-in-first-out order, not context
identifier 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 the
statusbar.  It returns a Message Identifier, which can be passed later
to the function gtk_statusbar_remove to remove the message with the
given Message and Context Identifiers from the statusbar's stack.
<P>The function gtk_statusbar_pop removes the message highest in the
stack with the given Context Identifier.
<P>The following example creates a statusbar and two buttons, one for
pushing items onto the statusbar, and one for popping the last item
back off.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start statusbar statusbar.c */

#include &lt;gtk/gtk.h>
#include &lt;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 (&amp;argc, &amp;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 line
text box. The text may be set with function calls that allow new text
to 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 a
new 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 currently
within 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_text
and gtk_entry_prepend_text allow the current contents to be appended
and 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 the
following 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 not
be freed using either free() or g_free()
<P>If we don't want the contents of the Entry to be changed by someone typing
into 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 the
Entry 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 be
visible, for example when a password is being entered, we can use the
following 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 following
function. This would most often be used after setting some default
text 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 to
the <CODE>activate</CODE> or <CODE>changed</CODE> signal. Activate is raised when the
user hits the enter key within the Entry widget. Changed is raised
when the text changes at all, e.g., for every character entered or
removed.
<P>The following code is an example of using an Entry widget.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start entry entry.c */

#include &lt;stdio.h>
#include &lt;gtk/gtk.h>

void enter_callback( GtkWidget *widget,
                     GtkWidget *entry )
{
  gchar *entry_text;
  entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
  printf("Ent

⌨️ 快捷键说明

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