1

I want to have a scrollable GtkTextView in my GtkWindow, but I only want the GtkTextView part to scroll instead of the whole window. I tried to put the GTKTextView inside of a GtkScrolledWindow and put the GtkScrolledWindow inside of a GtkFixed container, but then the GtkTextView doesn't show up. When I put the GtkTextView directly inside the GtkFixed container, though, it shows up.

#include <gtk/gtk.h>

GtkWidget *window, *scrolled_window, *fixed, *log_box, *button1;
GtkTextBuffer *log_box_buffer;

static void button1_clicked(GtkWidget *widget, gpointer data) {
    printf("button1_clicked\n");
    gtk_text_buffer_insert_at_cursor(log_box_buffer, "You clicked the button.\n", 24);
}

static void app_activate(GtkApplication *app, gpointer user_data) {

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Window Title Here");
    gtk_window_set_default_size(GTK_WINDOW(window), 700, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    scrolled_window = gtk_scrolled_window_new(NULL, NULL);

    fixed = gtk_fixed_new();

    button1 = gtk_button_new_with_label("Button 1");
    g_signal_connect(button1, "clicked", G_CALLBACK(button1_clicked), NULL);

    log_box_buffer = gtk_text_buffer_new(NULL);
    log_box = gtk_text_view_new_with_buffer(log_box_buffer);

    gtk_fixed_put(GTK_FIXED(fixed), button1, 50, 50);


    /* Here I tried to put the textview inside of the scrolled window and
       add the scrolled window to the fixed container. The textview
       doesn't show up when I do this. */

    gtk_container_add(GTK_CONTAINER(scrolled_window), log_box);
    gtk_fixed_put(GTK_FIXED(fixed), scrolled_window, 200, 50);


    /* I also tried putting the textview directly in the fixed container.
       This shows up, but obviously I can't scroll it. */

//  gtk_fixed_put(GTK_FIXED(fixed), log_box, 200, 50);


    gtk_container_add(GTK_CONTAINER(window), fixed);
    gtk_widget_show_all(window);
}

int main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = gtk_application_new("the.application.id", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);

    status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref(app);
    return status;
}
Jason
  • 2,725
  • 2
  • 14
  • 22

1 Answers1

1

not sure why you want to use a fixed container. If so you must supply the width and height of the scrolled window, which contains the textview. I've compiled your code and it works. Just add:

gtk_widget_set_size_request (GTK_WIDGET(scrolled_window), 200, 200);

and this will set a size of 200 by 200, as an example, on your scrolled window.

José Fonte
  • 4,016
  • 2
  • 16
  • 28
  • I'm using the fixed container because I'm going to other stuff in the window that shouldn't scroll. This worked after a slight edit, by the way. I had to use the `GTK_WIDGET` macro instead of the `GTK_WINDOW` macro. Can you please update your answer? – Jason May 28 '17 at 11:06
  • 1
    Yes sorry i meant GTK_WIDGET not GTK_WINDOW but i retyped instead of just a copy paste... Was just a line... And i mistake it :/ thanks. – José Fonte May 28 '17 at 11:22
  • 1
    @Jason Gtk fixed is very specific maybe using layout containers would be a better fit so that the layout adjusts to window resizing, etc – José Fonte May 28 '17 at 11:26
  • 1
    Thanks for the advice. This is my first GTK app, so I'm still experimenting. I will try out different layout containers soon. – Jason May 28 '17 at 11:28