|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
Control what happens when GtkTextView is resized: keep the bottom part visibleI have a question concerning how to set which part of a GtkTextBuffer is
displayed when a GtkTextView is resized. To explain the situation, consider this sample code: ----8<---- /* gcc -o tvtest tvtest.c `pkg-config --libs --cflags gtk+-2.0` */ #include <gtk/gtk.h> int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *vpaned; GtkWidget *text1; GtkWidget *scr1; GtkTextBuffer *buffer; GtkWidget *text2; GtkWidget *scr2; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request(window, 500, 300); g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(gtk_main_quit), NULL); vpaned = gtk_vpaned_new(); text1 = gtk_text_view_new(); buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1)); gtk_text_buffer_set_text(buffer, "Lorem ipsum dolor sit amet,\n" "consectetur adipisicing elit,\n" "sed do eiusmod tempor incididunt\n" "ut labore et dolore magna aliqua.\n" "Ut enim ad minim veniam,\n" "quis nostrud exercitation ullamco laboris\n" "nisi ut aliquip ex ea commodo consequat.\n" "Duis aute irure dolor in reprehenderit\n" "in voluptate velit esse cillum dolore\n" "eu fugiat nulla pariatur.\n" "Excepteur sint occaecat cupidatat non proident,\n" "sunt in culpa qui officia\n" "deserunt mollit anim id est laborum.", -1); text2 = gtk_text_view_new_with_buffer(buffer); scr1 = gtk_scrolled_window_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(scr1), text1); gtk_paned_add1(GTK_PANED(vpaned), scr1); scr2 = gtk_scrolled_window_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(scr2), text2); gtk_paned_add2(GTK_PANED(vpaned), scr2); gtk_container_add(GTK_CONTAINER(window), vpaned); gtk_widget_show_all(window); gtk_main(); return 0; } ----8<---- When the division in the GtkPaned is moved, what happens is that the top of the GtkTextViews remains the same (thus keeps displaying the same thing), while the bottom part shrinks, hiding some text at the bottom if the widget gets smaller, or displaying more text at the bottom if it is enlarged. What I would like to do is the opposite: for the GtkTextView in the bottom, I'd like that the bottom of the widget remains the same, but changes in size are reflected at the top: if the widget shrinks, then less text at the top should be displayed, if it grows, then more text at the top should be displayed. The bottom should remain fixed displaying always the same part. Another way to visualize is that by changing the division in the GtkPaned it should cover more of the top of the bottom GtkTextView, instead of pushing it down. Does anyone have a solution for that? -- Money is truthful. If a man speaks of his honor, make him pay cash. -- Lazarus Long Eduardo M KALINOWSKI eduardo@... _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleEduardo M KALINOWSKI wrote:
> I have a question concerning how to set which part of a GtkTextBuffer is > displayed when a GtkTextView is resized. > > To explain the situation, consider this sample code: > > ----8<---- > /* gcc -o tvtest tvtest.c `pkg-config --libs --cflags gtk+-2.0` */ > #include <gtk/gtk.h> > > int main(int argc, char *argv[]) > { > GtkWidget *window; > GtkWidget *vpaned; > GtkWidget *text1; > GtkWidget *scr1; > GtkTextBuffer *buffer; > GtkWidget *text2; > GtkWidget *scr2; > > gtk_init(&argc, &argv); > > window = gtk_window_new(GTK_WINDOW_TOPLEVEL); > gtk_widget_set_size_request(window, 500, 300); > g_signal_connect(G_OBJECT(window), "delete-event", > G_CALLBACK(gtk_main_quit), NULL); > > vpaned = gtk_vpaned_new(); > > text1 = gtk_text_view_new(); > buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1)); > gtk_text_buffer_set_text(buffer, > "Lorem ipsum dolor sit amet,\n" > "consectetur adipisicing elit,\n" > "sed do eiusmod tempor incididunt\n" > "ut labore et dolore magna aliqua.\n" > "Ut enim ad minim veniam,\n" > "quis nostrud exercitation ullamco laboris\n" > "nisi ut aliquip ex ea commodo consequat.\n" > "Duis aute irure dolor in reprehenderit\n" > "in voluptate velit esse cillum dolore\n" > "eu fugiat nulla pariatur.\n" > "Excepteur sint occaecat cupidatat non > proident,\n" > "sunt in culpa qui officia\n" > "deserunt mollit anim id est laborum.", -1); > > text2 = gtk_text_view_new_with_buffer(buffer); > > scr1 = gtk_scrolled_window_new(NULL, NULL); > gtk_container_add(GTK_CONTAINER(scr1), text1); > gtk_paned_add1(GTK_PANED(vpaned), scr1); > > scr2 = gtk_scrolled_window_new(NULL, NULL); > gtk_container_add(GTK_CONTAINER(scr2), text2); > gtk_paned_add2(GTK_PANED(vpaned), scr2); > > gtk_container_add(GTK_CONTAINER(window), vpaned); > > gtk_widget_show_all(window); > gtk_main(); > > return 0; > } > ----8<---- > > When the division in the GtkPaned is moved, what happens is that the top > of the GtkTextViews remains the same (thus keeps displaying the same > thing), while the bottom part shrinks, hiding some text at the bottom if > the widget gets smaller, or displaying more text at the bottom if it is > enlarged. > > What I would like to do is the opposite: for the GtkTextView in the > bottom, I'd like that the bottom of the widget remains the same, but > changes in size are reflected at the top: if the widget shrinks, then > less text at the top should be displayed, if it grows, then more text at > the top should be displayed. The bottom should remain fixed displaying > always the same part. > > Another way to visualize is that by changing the division in the > GtkPaned it should cover more of the top of the bottom GtkTextView, > instead of pushing it down. > > Sorry to be posting again, but does anyone have any tips? -- Eduardo M KALINOWSKI eduardo@... _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleBind a callback to the "size-allocate" signal of the scrolled window:
#include <gtk/gtk.h> void cb_autoscroll_to_end( GtkWidget* widget, GtkAllocation* allocation, gpointer user_data) { GtkAdjustment *vericalAdjust; GtkAllocation hscrollAlloc; GtkWidget *hscrollBar; /* the horizontal scroll bar of the scrolled window */ double upper, height; /* get the vertical adjustment of the scrolled window */ vericalAdjust = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW( widget ) ); /* get the upper bound of the adjustment - you want your text to end at that point */ upper = gtk_adjustment_get_upper( vericalAdjust ); height = allocation->height; /* if there is a scroll bar at the bottom, it will steal some of the available pixels for the text */ hscrollBar = gtk_scrolled_window_get_hscrollbar( GTK_SCROLLED_WINDOW( widget ) ); gtk_widget_get_allocation( hscrollBar, &hscrollAlloc ); /* as long as (the scroll widget height - height of the horiz scrollbar) is the same as the upper bound of the adjustment, the whole text can be seen without obstruction and you do not need to auto scroll */ if ( ( height - hscrollAlloc.height ) >= upper ) { gtk_adjustment_set_value( vericalAdjust, 0 ); } else { /* otherwise you need to scroll down to the point where */ double newPosition = upper - ( height - hscrollAlloc.height ); gtk_adjustment_set_value( vericalAdjust, newPosition ); } } After you create the scroll window in main(), connect like: g_signal_connect( scr1, "size-allocate", G_CALLBACK( cb_autoscroll_to_end ), NULL ); It will work with every scroll bar policy (always, none, auto).... Cheers! On 11/09/2009 05:12 AM, Eduardo M KALINOWSKI wrote: > I have a question concerning how to set which part of a GtkTextBuffer is > displayed when a GtkTextView is resized. > > To explain the situation, consider this sample code: > > ----8<---- > /* gcc -o tvtest tvtest.c `pkg-config --libs --cflags gtk+-2.0` */ > #include<gtk/gtk.h> > > int main(int argc, char *argv[]) > { > GtkWidget *window; > GtkWidget *vpaned; > GtkWidget *text1; > GtkWidget *scr1; > GtkTextBuffer *buffer; > GtkWidget *text2; > GtkWidget *scr2; > > gtk_init(&argc,&argv); > > window = gtk_window_new(GTK_WINDOW_TOPLEVEL); > gtk_widget_set_size_request(window, 500, 300); > g_signal_connect(G_OBJECT(window), "delete-event", > G_CALLBACK(gtk_main_quit), NULL); > > vpaned = gtk_vpaned_new(); > > text1 = gtk_text_view_new(); > buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1)); > gtk_text_buffer_set_text(buffer, > "Lorem ipsum dolor sit amet,\n" > "consectetur adipisicing elit,\n" > "sed do eiusmod tempor incididunt\n" > "ut labore et dolore magna aliqua.\n" > "Ut enim ad minim veniam,\n" > "quis nostrud exercitation ullamco laboris\n" > "nisi ut aliquip ex ea commodo consequat.\n" > "Duis aute irure dolor in reprehenderit\n" > "in voluptate velit esse cillum dolore\n" > "eu fugiat nulla pariatur.\n" > "Excepteur sint occaecat cupidatat non > proident,\n" > "sunt in culpa qui officia\n" > "deserunt mollit anim id est laborum.", -1); > > text2 = gtk_text_view_new_with_buffer(buffer); > > scr1 = gtk_scrolled_window_new(NULL, NULL); > gtk_container_add(GTK_CONTAINER(scr1), text1); > gtk_paned_add1(GTK_PANED(vpaned), scr1); > > scr2 = gtk_scrolled_window_new(NULL, NULL); > gtk_container_add(GTK_CONTAINER(scr2), text2); > gtk_paned_add2(GTK_PANED(vpaned), scr2); > > gtk_container_add(GTK_CONTAINER(window), vpaned); > > gtk_widget_show_all(window); > gtk_main(); > > return 0; > } > ----8<---- > > When the division in the GtkPaned is moved, what happens is that the top > of the GtkTextViews remains the same (thus keeps displaying the same > thing), while the bottom part shrinks, hiding some text at the bottom if > the widget gets smaller, or displaying more text at the bottom if it is > enlarged. > > What I would like to do is the opposite: for the GtkTextView in the > bottom, I'd like that the bottom of the widget remains the same, but > changes in size are reflected at the top: if the widget shrinks, then > less text at the top should be displayed, if it grows, then more text at > the top should be displayed. The bottom should remain fixed displaying > always the same part. > > Another way to visualize is that by changing the division in the > GtkPaned it should cover more of the top of the bottom GtkTextView, > instead of pushing it down. > > Does anyone have a solution for that? > > gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleTodor Todorov wrote:
> Bind a callback to the "size-allocate" signal of the scrolled window: > > #include <gtk/gtk.h> > > void cb_autoscroll_to_end( GtkWidget* widget, GtkAllocation* allocation, > gpointer user_data) > { > <snip> > } > > After you create the scroll window in main(), connect like: > > g_signal_connect( scr1, "size-allocate", G_CALLBACK( > cb_autoscroll_to_end ), NULL ); > > It will work with every scroll bar policy (always, none, auto).... > Thanks for that, it works for keeping always the bottom part displayed. However, I'd like something slightly more complex: if the scroll bar is not at its end, that is, some other part of the text view is being shown, I'd like that part to remain at the end, shrinking the bottom widget at the top but keeping the bottom part displaying the same thing, instead of resizing the bottom and moving it. One way to do that might be to check the value of the GtkAdjustment before it starts to be resized and then, in a callback like you posted, set the new value to display the desired part. But how can one obtain the value before the resizing starts? -- <Culus> Saens demonstrates no less than 3 tcp/ip bugs in 2.2.3 Eduardo M KALINOWSKI eduardo@... _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleWell, since resizing the widget by itself does not do any scrolling,
whichever value you get from the adjustment _is_ the current scroll value. In the same way you have the function *_get_upper(), you have *_get_lower() and *_get_value()... Just change the computation however you need it to be. Did you even looked up the functions I used in the API docs? You would have seen the rest.... Hope that helps. On 11/13/2009 12:49 PM, Eduardo M KALINOWSKI wrote: > Todor Todorov wrote: > >> Bind a callback to the "size-allocate" signal of the scrolled window: >> >> #include<gtk/gtk.h> >> >> void cb_autoscroll_to_end( GtkWidget* widget, GtkAllocation* allocation, >> gpointer user_data) >> { >> <snip> >> } >> >> After you create the scroll window in main(), connect like: >> >> g_signal_connect( scr1, "size-allocate", G_CALLBACK( >> cb_autoscroll_to_end ), NULL ); >> >> It will work with every scroll bar policy (always, none, auto).... >> >> > Thanks for that, it works for keeping always the bottom part displayed. > > However, I'd like something slightly more complex: if the scroll bar is > not at its end, that is, some other part of the text view is being > shown, I'd like that part to remain at the end, shrinking the bottom > widget at the top but keeping the bottom part displaying the same thing, > instead of resizing the bottom and moving it. > > One way to do that might be to check the value of the GtkAdjustment > before it starts to be resized and then, in a callback like you posted, > set the new value to display the desired part. But how can one obtain > the value before the resizing starts? > > gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleTodor Todorov wrote:
> Well, since resizing the widget by itself does not do any scrolling, > whichever value you get from the adjustment _is_ the current scroll > value. In the same way you have the function *_get_upper(), you have > *_get_lower() and *_get_value()... Just change the computation however > you need it to be. Did you even looked up the functions I used in the > API docs? You would have seen the rest.... > You're right, I was wrong about the requirements. I've come to the conclusion that what is necessary is to keep the value (upper - (value + page_size)) constant when resizing happens. This value represents the size of the part of the scrollable widget (a GtkTextView in this case) that remains below the currently displayed portion. That means adjusting the value (which as you said by default is not changed). Well, upper never changes when resizing a widget, so it's only necessary to keep (value + page_size) constant. But page_size _does_ change when resizing happens, so a question very similar to my previous one still holds: is there a way to know the value that the adjustment's page_size had before the resize operation, to compare with the new value caused by the resizing, so that the calculations can be done? I've been able to solve it by storing page_size in a variable at the end of the function and reusing it in the next call, but this is far from elegant. Is there some better way? -- Mere nonexistence is a feeble excuse for declaring a thing unseeable. You *can* see dragons. You just have to look in the right direction. -- John Hasler Eduardo M KALINOWSKI eduardo@... _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
|
|
Re: Control what happens when GtkTextView is resized: keep the bottom part visibleI don't believe there is a way to get the size before the change. I do
not believe that there is much requirement for that. If you are looking for a more elegant solution, you would have to reimplement GtkViewport or GtkScrolledWindow to keep track of the visible view now and before. Regards, Todor On 11/14/2009 07:10 AM, Eduardo M KALINOWSKI wrote: > Todor Todorov wrote: > >> Well, since resizing the widget by itself does not do any scrolling, >> whichever value you get from the adjustment _is_ the current scroll >> value. In the same way you have the function *_get_upper(), you have >> *_get_lower() and *_get_value()... Just change the computation however >> you need it to be. Did you even looked up the functions I used in the >> API docs? You would have seen the rest.... >> >> > You're right, I was wrong about the requirements. > > I've come to the conclusion that what is necessary is to keep the value > (upper - (value + page_size)) constant when resizing happens. This value > represents the size of the part of the scrollable widget (a GtkTextView > in this case) that remains below the currently displayed portion. That > means adjusting the value (which as you said by default is not changed). > > Well, upper never changes when resizing a widget, so it's only necessary > to keep (value + page_size) constant. But page_size _does_ change when > resizing happens, so a question very similar to my previous one still > holds: is there a way to know the value that the adjustment's page_size > had before the resize operation, to compare with the new value caused by > the resizing, so that the calculations can be done? > > I've been able to solve it by storing page_size in a variable at the end > of the function and reusing it in the next call, but this is far from > elegant. Is there some better way? > > gtk-app-devel-list mailing list gtk-app-devel-list@... http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list |
| Free embeddable forum powered by Nabble | Forum Help |