GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 12:41 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to scroll text view to show cursor? (solved)
PostPosted: Mon Dec 09, 2013 7:54 pm 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 6:04 pm
Posts: 14
I'm writing a simple console style window which I have working. I haven't been able to figure how to automatically scroll the text view so that the cursor and last inserted text always appears on screen. I figured I should place a marker after every text insertion then call for the text_view to scroll. But that doesn't work. Here's my code for the scrollable console window and a printf() function to work with it:

Code:
GtkWidget *console_text_view;
GtkTextBuffer *console_buffer;
GtkTextMark *console_mark;

void gui_printf(const char *control, ...)
{
   va_list parms;
   char text[TEXT_BUFFER_SIZE];

   va_start(parms, control);
   vsprintf(text, control, parms);
   gtk_text_buffer_insert_at_cursor(console_buffer, text, -1);
   console_mark = gtk_text_buffer_get_insert(GTK_TEXT_BUFFER(console_buffer)); // <---- ADDED THIS
   gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(console_text_view), console_mark);
   va_end(parms);
}

GtkWidget *install_console(int width, int height)
{
   GtkWidget *scroll_window;
   GtkWidget *viewport;
   GtkAdjustment *horizontal, *vertical;
   
   scroll_window = gtk_scrolled_window_new(NULL, NULL);
   gtk_container_set_border_width(GTK_CONTAINER(scroll_window), 5);
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
      GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   gtk_widget_set_size_request(scroll_window, width, height);
   horizontal   = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scroll_window));
   vertical   = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scroll_window));
   viewport   = gtk_viewport_new(horizontal, vertical);
   
   console_text_view = gtk_text_view_new();
   console_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(console_text_view));
   gtk_container_add(GTK_CONTAINER(scroll_window), console_text_view);
   
   gtk_text_buffer_set_text(console_buffer,
   "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n",
   -1);
   // this is just to show 80 columns of text

   //console_mark = gtk_text_mark_new(NULL, FALSE); <-- DELETED THIS

   return scroll_window;
}


I've tried a bunch of things but I'm stumped. I'm obviously missing something simple. What I need is a gtk_text_view_scroll_screen_to_cursor() function.

PS. I'm using Gtk 3.6 so functions such as gtk_text_buffer_get_end_iter(buffer, &iter); are deprecated.

I found a solution by using gtk_text_buffer_get_insert() which returns the marker which can then be passed to gtk_text_view_scroll_mark_onscreen() to cause the scrolling...


Top
 Profile  
 
 Post subject: Re: How to scroll text view to show cursor? (solved)
PostPosted: Thu Dec 12, 2013 9:15 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello,

Glad you found your solution. My only concern is that you are using a fixed size buffer with vsprintf(). This can lead to buffer over-flows a source of hard to find bugs and security problems.

Glib (a library that GTK builds on) offers alternatives that allocate space on the heap so avoiding the buffer overflow problem. https://developer.gnome.org/glib/stable ... tions.html

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group