GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Automatically scroll to end of GtkTextView

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Thu May 08, 2008 12:26 pm    Post subject: Automatically scroll to end of GtkTextView Reply with quote

This little example shows how you can use gtk_text_view_scroll_to_iter () to automatically scroll to the end of a GtkTextView when you insert text. This would commonly be used to implement a status log or something of that nature.

This example uses a GtkBuilder XML file created using Glade and therefore requires GTK+ 2.12 or greater.

Compile using:
Code: (Plaintext)
1
cc -Wall -g main.c -o example -export-dynamic `pkg-config --cflags --libs gtk+-2.0`


ui.xml
Code: (XML/HTML)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0"?>
<interface>
  <object
class="GtkWindow" id="window">
    <property
name="border_width">4</property>
    <signal
name="destroy" handler="gtk_main_quit"/>
    <child>
      <object
class="GtkVBox" id="vbox1">
        <property
name="visible">True</property>
        <property
name="spacing">4</property>
        <child>
          <object
class="GtkHBox" id="hbox1">
            <property
name="visible">True</property>
            <child>
              <object
class="GtkEntry" id="entry">
                <property
name="visible">True</property>
                <property
name="can_focus">True</property>
              </object>
            </child>
            <child>
              <object
class="GtkButton" id="button">
                <property
name="visible">True</property>
                <property
name="can_focus">True</property>
                <property
name="receives_default">True</property>
                <property
name="label" translatable="yes">Log Message</property>
                <signal
name="clicked" handler="on_button_clicked"/>
              </object>
              <packing>
                <property
name="expand">False</property>
                <property
name="fill">False</property>
                <property
name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property
name="expand">False</property>
          </packing>
        </child>
        <child>
          <object
class="GtkScrolledWindow" id="scrolledwindow1">
            <property
name="visible">True</property>
            <property
name="can_focus">True</property>
            <property
name="extension_events">GDK_EXTENSION_EVENTS_ALL</property>
            <property
name="shadow_type">GTK_SHADOW_ETCHED_IN</property>
            <child>
              <object
class="GtkTextView" id="textview">
                <property
name="visible">True</property>
                <property
name="can_focus">True</property>
              </object>
            </child>
          </object>
          <packing>
            <property
name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>


main.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Example logging to a GtkTextView and automatically scrolling to the newly
inserted text.

Written for www.gtkforums.com by Micah Carrick

Compile using:
cc -Wall -g main.c -o example -export-dynamic `pkg-config --cflags --libs gtk+-2.0`
*/

#include <gtk/gtk.h>

/* structure to hold references to our widgets */
typedef struct
{       
        GtkWidget               *entry;
        GtkWidget               *textview;
} MyWidgets;
               
/* call back function for when the button is pressed */
void
on_button_clicked (GtkWidget* widget, gpointer user_data)
{
        MyWidgets               *widgets;
        gchar                   *message;       
        GtkTextBuffer           *buffer;
        GtkTextIter             iter;
       
        /* cast the gpointer user_data into the MyWidgets structure */
       
widgets = (MyWidgets *)user_data;
       
        /* build message to log appending a new line char to the end */
       
message = g_strconcat (gtk_entry_get_text (GTK_ENTRY (widgets->entry)),
                               "\n", NULL);
           
        /* get the text buffer */
       
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widgets->textview));
       
        /* get end iter */
       
gtk_text_buffer_get_end_iter (buffer, &iter);
       
        /* add the message to the text buffer */
       
gtk_text_buffer_insert (buffer, &iter, message, -1);
        g_free (message);
       
        /* scroll to end iter */
       
gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (widgets->textview),
                                      &iter, 0.0, FALSE, 0, 0);
}

int
main (int argc, char *argv[])
{
       
        GtkWidget               *window;
        GtkBuilder              *builder;
        MyWidgets               *widgets;
        PangoFontDescription    *font_desc;
        GError                  *err = NULL;
       
        /* allocate space for widgets struct */
       
widgets = g_slice_new (MyWidgets);
       
        /* initialize the GTK+ library */
       
gtk_init (&argc, &argv);

        /*  Build UI from xml description */
       
builder = gtk_builder_new ();
        gtk_builder_add_from_file (builder, "ui.xml", &err);
       
        if (err)
        {
            /* There was an error building the interface */
           
g_error (err->message);
            g_error_free (err);
            g_slice_free (MyWidgets, widgets);
            return 1;
        }
       
        /* get widgets from GladeXML object */
       
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
        widgets->entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry"));
        widgets->textview = GTK_WIDGET (gtk_builder_get_object (builder,
                                                                "textview"));
       
        /* connect callback functions passing MyWidgets struct as user_dada */
       
gtk_builder_connect_signals (builder, widgets);
       
        /* free memory used by builder object and XML */
       
g_object_unref (G_OBJECT (builder));     
             
        /* use a fixed width font on the text view so the output looks proper */
       
font_desc = pango_font_description_from_string ("monospace 10");
        gtk_widget_modify_font (widgets->textview, font_desc);     
        pango_font_description_free (font_desc);     
             
        /* show the main window */
       
gtk_widget_show (window);

        gtk_main ();
       
        /* free space for widget struct */
       
g_slice_free (MyWidgets, widgets);
       
        return 0;     
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP