I have noticed that GtkTextView's preferred size doesn't change immediately after its contents are changed, but only sometime later in event processing.
In following example, when you click the button, the text in GtkTextView is updated via gtk_text_buffer_set_text(). The new text is long enough to cause a line wrap, and so the preferred height of the control should change. But the gtk_widget_get_preferred_size() still reports old preferred height. Later, in 'size-allocate' event handler, the gtk_widget_get_preferred_size() is already reporting correct size for new contents.
I am trying to understand what happens between the return from button's 'clicked' event handler and 'size-allocate' event handler, that causes GtkTextView's preferred size to update to reflect its changed contents.
#include <gtk/gtk.h>
static bool button_clicked(GtkWidget* obj, GtkWidget* view)
{
// set text in GtkTextView to
const char* long_text="aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa aaaaa";
printf("----------------------------\n");
printf("in button_clicked(): setting text\n");
GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
gtk_text_buffer_set_text(buffer, long_text, -1);
GtkRequisition req;
gtk_widget_get_preferred_size(view, &req, NULL);
printf("in button_clicked(): preferred height = %u\n", req.height);
return false;
}
static bool on_size_allocate(GtkWidget* obj, GdkRectangle *allocation, GtkWidget* view)
{
GtkRequisition req;
gtk_widget_get_preferred_size(view, &req, NULL);
printf("in on_size_allocate(): preferred height = %u\n", req.height);
return false;
}
static void activate(GtkApplication* app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW (window), "Wrapped text");
gtk_window_set_default_size(GTK_WINDOW (window), 200, 200);
gtk_widget_show_all(window);
GtkTextBuffer* buffer = gtk_text_buffer_new(NULL);
GtkWidget* view = GTK_WIDGET(gtk_text_view_new_with_buffer(buffer));
g_object_unref(buffer);
gtk_widget_set_size_request(view, 180, -1);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
g_signal_connect(view, "size-allocate", G_CALLBACK(on_size_allocate), view);
GtkWidget *button = gtk_button_new_with_label("Set Text");
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), view);
GtkWidget* grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), view, 0, 1, 1, 1);
gtk_widget_show_all(window);
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
This displays:
in on_size_allocate(): preferred height = 17
----------------------------
in button_clicked(): setting text
in button_clicked(): preferred height = 17
in on_size_allocate(): preferred height = 34
Related
I am trying to modify the cairo graphics rendering I found in http://zetcode.com/gfx/cairo/basicdrawing/ so that it updates the graphics when I click the drawing area with the left mouse button. Currently the code in the website uses the right mouse button click to update the graphics.
I have tried adding gtk_widget_queue_draw(widget); to gboolean clicked(), when event->button == 1:
#include <cairo.h>
#include <gtk/gtk.h>
static void do_drawing(cairo_t *);
struct {
int count;
double coordx[100];
double coordy[100];
} glob;
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
do_drawing(cr);
return FALSE;
}
static void do_drawing(cairo_t *cr)
{
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 0.5);
int i, j;
for (i = 0; i <= glob.count - 1; i++ ) {
for (j = 0; j <= glob.count - 1; j++ ) {
cairo_move_to(cr, glob.coordx[i], glob.coordy[i]);
cairo_line_to(cr, glob.coordx[j], glob.coordy[j]);
}
}
glob.count = 0;
cairo_stroke(cr);
}
static gboolean clicked(GtkWidget *widget, GdkEventButton *event,
gpointer user_data)
{
if (event->button == 1) {
glob.coordx[glob.count] = event->x;
glob.coordy[glob.count++] = event->y;
gtk_widget_queue_draw(widget);
}
if (event->button == 3) {
gtk_widget_queue_draw(widget);
}
return TRUE;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
glob.count = 0;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
g_signal_connect(G_OBJECT(darea), "draw",
G_CALLBACK(on_draw_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "button-press-event",
G_CALLBACK(clicked), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
gtk_window_set_title(GTK_WINDOW(window), "Lines");
gtk_widget_show_all(window);
gtk_main();
return 0;
}
However, this does not work since now the graphics aren't updated at all.
Is there a way of updating the graphics by pressing the left mouse button (button 1) and simultaneously carry out inserting the coordinates to struct glob as before?
Thanks!
You are setting glob.count = 0 after drawing the lines, however glob.count is only updated by one in the clicked() callback, so it will never draw more than a single line. Also the first line is from point (glob.coordx[0], glob.coordy[0]) to (glob.coordx[0], glob.coordy[0]) (that is: it starts and ends at the same point) so it has no length, and will not be visible.
Solution: remove the line glob.count = 0; from do_drawing().
g_signal_connect(window, "button-press-event", G_CALLBACK(clicked), NULL);
When your callback is called, the window (which emits "button-press-event") is passed as first parameter. The queue_draw call queues redraw of window. However, redraw of DrawingArea is not requested and will not happen.
You can:
Queue redraw of DrawingArea
Connect to "button-press-event" of DrawingArea
Without any changes of code: press a mouse button and resize the window. Gtk will see, that DrawingArea has changed (it's resized too) and queue redraw of this widget.
I have a C GTK3 program that has a notebook with two images. I want to be able to grab the corner of the window and adjust the size of the image currently displayed. What I currently have is a program that once started, the window keeps growing until I kill it from the terminal using ctrl-c. I put a sleep call in the callback to slow it down, but it still grows. How do I stop the window from growing unless I "grab" a corner of the window and adjust it myself?
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
GtkWidget *notebook;
gboolean resize_image(GtkWidget *widget, GdkRectangle *allocation,
gpointer user_data)
{
int w,h, pagenum;
GdkPixbuf *pxbscaled;
GtkWidget *image;
GdkPixbuf *pixbuf;
pagenum = gtk_notebook_get_current_page (GTK_NOTEBOOK(notebook));
image = gtk_notebook_get_nth_page (GTK_NOTEBOOK(notebook), pagenum);
// GtkImageType image_type = gtk_image_get_storage_type
// (GTK_IMAGE(image));
pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));
h = allocation->height;
w = (gdk_pixbuf_get_width(pixbuf) * h) / gdk_pixbuf_get_height(pixbuf);
pxbscaled = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
printf("Allocation height %d width %d.\n", h, w);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pxbscaled);
g_object_unref (pxbscaled);
sleep(2);
return FALSE;
}
static gboolean delete( GtkWidget *widget,
GtkWidget *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget *label;
GtkWidget *image;
int i;
char bufferl[32];
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
// gtk_widget_set_size_request (GTK_WIDGET(window), 800, 480);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
table = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), table);
/* Create notebook, place position of tabs */
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
gtk_grid_attach (GTK_GRID (table), notebook, 0, 6, 3, 3);
gtk_widget_show (notebook);
/* Append pages to the notebook */
for (i = 0; i < 2; i++) {
sprintf(bufferl, "Page %d", i + 1);
if (i == 0) {
image = gtk_image_new_from_file("image1.jpg");
} else {
image = gtk_image_new_from_file("image2.jpg");
}
gtk_widget_set_halign(image, GTK_ALIGN_START);
gtk_widget_set_valign(image, GTK_ALIGN_START);
g_signal_connect(window, "size-allocate",
G_CALLBACK(resize_image), NULL);
label = gtk_label_new (bufferl);
gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
image, label);
}
/* Create a close button */
button = gtk_button_new_with_label ("close");
g_signal_connect (button, "clicked",
G_CALLBACK (delete), NULL);
gtk_grid_attach (GTK_GRID (table), button, 0, 10, 1, 1);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
A user will need to provide image1.jpg and image2.jpg. Edit out the sleep call will result in the program filling the screen extremely quickly.
EDIT: I have also asked this question on the gtk mailing list.
The window with the image was growing because I was applying the size of the window to the image. Hence the image got larger and thus made the window get larger. Which continued in an endless progression, the "size-allocate" signal was constantly being called.
I fixed it by limiting the allocation height in the call back, by multiplying it by 0.75.
Now I can expand and contract the window with ease and it does not grow out of control.
The image does get ugly quite quickly, but that is another problem.
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;
}
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.
I've done this simple app in Gtk, just to test things out... I come from swing so redefining a draw event function is normal for me... Anyway seems not to work:
#include <gtk-2.0/gtk/gtk.h>
#include <gtk-2.0/gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
#include<string.h>
#include<stdio.h>
#include <iostream>
GdkPixbuf *imm;
void destroy(GtkWidget *widget, gpointer data) {
gtk_main_quit();
}
gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,
gpointer data) {
gdk_draw_pixbuf((GdkDrawable*) widget, widget->style->white_gc, imm, 0, 0,
0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
return FALSE;
}
int main(int argc, char** argv) {
char* filename = new char[1000];
GError *error = NULL;
GtkWidget *window;
gtk_set_locale();
gtk_init(&argc, &argv);
if (argv[1] == NULL) {
std::cout << "Err.";
return -1;
}
strcpy(filename, argv[1]);
imm = gdk_pixbuf_new_from_file(filename, &error);
if (!imm) {
std::cout << "err closing";
return 0;
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request((GtkWidget*) window, 500, 350);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
g_signal_connect(window, "expose-event",
G_CALLBACK(on_expose_event), NULL);
gtk_container_set_border_width(GTK_CONTAINER (window), 10);
gtk_widget_show(window);
gtk_main();
return 0;
}
..in fact at runtinme says (on line gdk_draw_pixbuf(....)):
(cConvolve:5011): Gdk-CRITICAL **: gdk_draw_pixbuf: assertion `GDK_IS_DRAWABLE (drawable)' failed
is it because the pixbuf is not good??? Or is it because I can't draw to the window like this?
It's because you cast GtkWidget to GdkDrawable, whereas GtkWidget doesn't inherit from GdkDrawable. Use
gdk_draw_pixbuf(GDK_DRAWABLE(gtk_widget_get_window(widget)), blah blah...);
Anyway in normal GTK use you don't have to do any drawing in expose handlers. To display an image, just use the GtkImage widget:
GtkImage *image = gtk_image_new_from_file(filename);
gtk_container_add(GTK_CONTAINER(window), image);
Widgets don't inherit from GdkDrawable, you need to get the drawable from the widget's window.
Thanks. But my purpose was to try drawing on the window bg... anyway, even if I dont' get anymore asserts, it doesn't work:
gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,
gpointer data) {
int w, h;
w = gdk_pixbuf_get_width(imm);
h = gdk_pixbuf_get_height(imm);
gdk_draw_pixbuf((GdkDrawable*) widget->window, widget->style->fg_gc[ GTK_STATE_NORMAL ], imm, 0, 0,
0, 0, w, h, GDK_RGB_DITHER_NORMAL, 0, 0);
return FALSE;
}
How do I initialize a GtkScrolledWindow to avoid scrollbars by growing as much as possible?
By default it seems to be as small as possible.
Demo code (Quit with SIGINT):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* content;
GtkWidget* sw;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), sw);
gtk_widget_show(sw);
content = gtk_button_new_with_label("This is a very, very"
"very very very very very very very very long text");
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
gtk_widget_show(content);
gtk_widget_show(window);
gtk_main();
return 0;
}
I think this both works and is not an ugly hack (much):
int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* content;
GtkWidget* sw;
GtkRequisition size;
GtkWidget* viewport;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), sw);
gtk_widget_show(sw);
content = gtk_button_new_with_label("This is a very, very"
"very very very very very very very very long text");
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
gtk_widget_show(content);
gtk_widget_show(window);
viewport = gtk_widget_get_ancestor(content, GTK_TYPE_VIEWPORT);
gtk_widget_size_request(viewport, &size);
gtk_widget_set_size_request(sw, size.width, size.height);
gtk_main();
return 0;
}
I'm not sure what the proper way to do this is, but I've found that what works best for me is to set the size of the window, and the widgets contained within it usually size themselves correctly:
gtk_window_set_default_size(GTK_WINDOW(window), 1000, 500);
Alternatively, you can set the size of the GtkScrolledWindow:
gtk_widget_set_size_request(window, 500, 250);
Note that in especially this last case, localisation, font sizes, and other such details probably have to be considered when calculationg the size in pixels.