amitjain wrote:

Example Attached!!!!
----- Original Message ----- From: "mili" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, November 14, 2002 8:04 PM
Subject: Can anybody give me an example of scrolling of GtkTextView?


Hi, anybody

Can you give me an example of scrolling a GtkTextView to the end of all texts? I want the lass line could be seen in the screen.

Use gtk_text_view_scroll_to_iter() function to do this. Example coming attached.

Olexiy

/*
        Shows how to launch threads and feed GtkTextView from them.
        Can be compiled with:

        gcc -O2 -Wall `pkg-config --cflags --libs gtk+-2.0 gthread-2.0` logger.c -o 
logger


        Olexiy Avramchenko
*/

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>


static int threads_count=0;

/*
        Simple thread function. It generates 1000 lines of text and puts their into
        GtkTextView widget.
*/
static void logger(GtkTextView *tv)
{
int             i, tnumber=++threads_count;
char            s[256];
GtkTextBuffer   *buffer;
GtkTextIter     iter;

        if (!tv)
                return;

        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));

        for (i=0;i<1000;i++) {
                sprintf(s, "thread N%d, message N%d\n", tnumber, i);

                /* using gdk_threads* pair to protect these calls */
                gdk_threads_enter();
                gtk_text_buffer_get_end_iter(buffer, &iter);
                gtk_text_buffer_insert(buffer, &iter, s, -1);
                gdk_threads_leave();
        }
}

/*
        Callback on "Log" button "clicked" signal, just launches another thread.
*/
static void start_log(GtkWidget *button, GtkWidget *tv)
{
GThread *tid;

        tid = g_thread_create((GThreadFunc)logger, tv, TRUE, NULL);
}

/*
        Clears the content of GtkTextView,
*/
static void clear_log(GtkWidget *button, GtkWidget *tv)
{
GtkTextBuffer   *buffer;
GtkTextIter     start, end;

        if (!tv)
                return;

        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));

        gtk_text_buffer_get_bounds(buffer, &start, &end);
        gtk_text_buffer_delete(buffer, &start, &end);
}

/*
        Scrolls GtkTextView till bottom.
*/
static void scroll_log(GtkWidget *button, GtkWidget *tv)
{
GtkTextBuffer   *buffer;
GtkTextIter     end;

        if (!tv)
                return;

        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));

        gtk_text_buffer_get_end_iter(buffer, &end);
        gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(tv), &end, 0.0, FALSE, 0.0,0.0);
}


int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *button;
GtkWidget *tv;
GtkWidget *sw;

        /* initalizing thread stuff */
        g_thread_init(NULL);
        gdk_threads_init();

        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
        gtk_window_set_title(GTK_WINDOW(window), "l0ggeR");
        g_signal_connect(window, "delete_event",G_CALLBACK(gtk_main_quit), NULL);

        tv = gtk_text_view_new();
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tv), FALSE);

        vbox = gtk_vbox_new(FALSE, 2);
        gtk_container_add(GTK_CONTAINER(window), vbox);

        hbox = gtk_hbox_new(FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE,FALSE, 2);

        button = gtk_button_new_with_label("Log");
        gtk_box_pack_start(GTK_BOX(hbox), button, FALSE,FALSE, 2);
        g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(start_log), tv);

        button = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
        gtk_box_pack_start(GTK_BOX(hbox), button, FALSE,FALSE, 2);
        g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(clear_log), tv);

        button = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
        gtk_box_pack_start(GTK_BOX(hbox), button, FALSE,FALSE, 2);
        g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(scroll_log), tv);

        button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
        gtk_box_pack_start(GTK_BOX(hbox), button, FALSE,FALSE, 2);
        g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);

        sw = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(
                                        GTK_SCROLLED_WINDOW (sw),
                                        GTK_POLICY_AUTOMATIC,
                                        GTK_POLICY_AUTOMATIC
        );
        gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE,TRUE, 0);

        gtk_container_add(GTK_CONTAINER(sw), tv);

        gtk_widget_show_all(window);

        /* guarding gtk_main with gdk_threads* pair */
        gdk_threads_enter();
        gtk_main();
        gdk_threads_leave();

        return 0;
}

Reply via email to