rubixibuc
rubixibuc

Reputation: 7427

GTK and scrolling text view

This is what I have so far

GtkWidget* createConsoleBox()
{
        GtkWidget* textArea = gtk_text_view_new();
        GtkWidget* scrollbar = gtk_vscrollbar_new(gtk_text_view_get_vadjustment(GTK_TEXT_VIEW(textArea)));
        GtkWidget* textEntry = gtk_entry_new();

        GtkWidget* console = gtk_table_new(3, 2, FALSE);

        gtk_table_attach_defaults(GTK_TABLE(console), textArea, 0, 1, 0, 1);
        gtk_table_attach_defaults(GTK_TABLE(console), scrollbar, 1, 2, 0, 1);

        gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 2, 1, 2);

        return console;

}

I want the text view to be scrollable as the text begins to fill the box, but the box keeps on expanding to accommodate more text. How to do I limit the size of the text view and create a scrollable text view.

Thanks in advance :-)

Upvotes: 5

Views: 17966

Answers (2)

ptomato
ptomato

Reputation: 57920

I'm afraid you've misunderstood how scrollbars work in GTK; usually you don't create a scrollbar directly, but you place the widget you would like to scroll in a GtkScrolledWindow. This creates scrollbars automatically and connects them to the widget inside the scrolled window; in your case, the text view.

Here's what your createConsoleBox() function should look like:

GtkWidget* createConsoleBox()
{
    GtkWidget* textArea = gtk_text_view_new();
    GtkWidget* scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
    GtkWidget* textEntry = gtk_entry_new();
    GtkWidget* console = gtk_table_new(3, 1, FALSE);

    gtk_container_add(GTK_CONTAINER(scrolledwindow), textArea);
    gtk_table_attach_defaults(GTK_TABLE(console), scrolledwindow, 0, 1, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 1, 1, 2);

    return console;
}

Upvotes: 14

RedComet
RedComet

Reputation: 1202

What you experience is the result of the widget asking more space to its parent container. Unless the parent container has some rules forbidding the expansion, it will give as much space as child widget asks.

A common way to avoid this is to set a given size for the child widget with gtk_widget_set_size_request(), followed by some way to make sure the parent can give shrink or grow, depending on the parent properties.

This sample code show one way to accomplish this.

#include <gtk/gtk.h>

GtkWidget* createConsoleBox()
{
    GtkWidget* textArea = gtk_text_view_new();
    GtkWidget* scrollbar= gtk_vscrollbar_new(gtk_text_view_get_vadjustment(GTK_TEXT_VIEW(textArea)));
    GtkWidget* textEntry = gtk_entry_new();

    GtkWidget* console = gtk_table_new(3, 2, FALSE);

    gtk_table_attach_defaults(GTK_TABLE(console), textArea, 0, 1, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(console), scrollbar, 1, 2, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 2, 1, 2);
    //This code sets the preferred size for the widget, so it does not ask for extra space
    gtk_widget_set_size_request(textArea, 320, 240);

    return console;
}


int main(int argc,char* argv[]){
    GtkWidget *window;
    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Simple Sample");
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);// does not  matter this size
    gtk_container_add(GTK_CONTAINER(window), createConsoleBox());
    gtk_widget_show_all(window);
    gtk_window_set_resizable(GTK_WINDOW(window),FALSE);//because of this
    gtk_main();
    return 0;
}

gtk_window_set_resizable() is meant to make the window un-resizeable by the user (the App can still resize it), but has the extra propertie of tighting up the window to the size of its child widget. Each GtkContainer has it way of setting up expansion, tighness, etc. Is only matter of experimentation to find the right one for your needs. If the window resizable property had been set to TRUE, the textarea would still have the given size for it, the container would just put a lot of extra space between the individual widgets inside the GtkTable.

Upvotes: 1

Related Questions