How catch mouse wheel up/down events using GTK3? - c

how do I implement mouse wheel up/down events using C GTK3?
I have adapted this code in order to handle mouse scoll events:
#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;
}
if (event->button == 3) {
gtk_widget_queue_draw(widget);
}
return TRUE;
}
static gboolean mouse_scroll (GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
{
printf("scrolled up! \n");
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);
gtk_widget_add_events(window, GDK_SCROLL_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);
g_signal_connect(window, "scroll-event",
G_CALLBACK(mouse_scroll), 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 only catches a general mouse event, irrespective of whether the mouse is scrolled up or down. I want to know how can I get it to printf("scrolled up! \n") only when the mouse wheel is scrolled up.
Any ideas?

The *event parameter in mouse_scroll should point to GdkEventScroll structure which has direction member:
GdkScrollDirection direction; the direction to scroll to (one of
GDK_SCROLL_UP, GDK_SCROLL_DOWN, GDK_SCROLL_LEFT,
GDK_SCROLL_RIGHT or GDK_SCROLL_SMOOTH).

Related

How to handle double-click events in GTK+3?

I was wondering how to produce double clicks?
Take the following code that draws lines using single clicks http://zetcode.com/gfx/cairo/basicdrawing/:
#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;
}
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;
}
How do I get this program to respond to double clicks events instead of the single clicks?
I cannot find it in this list https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button.signals.clicked.
From GdkEventButton:
For double-clicks the order of events will be:
GDK_BUTTON_PRESS
GDK_BUTTON_RELEASE
GDK_BUTTON_PRESS
GDK_2BUTTON_PRESS
GDK_BUTTON_RELEASE
Note that the first click is received just like a normal button press,
while the second click results in a GDK_2BUTTON_PRESS being received
just after the GDK_BUTTON_PRESS.
(...)
For a double click to occur, the second button press must occur within 1/4 of a second of the first.
Each GdkEvent has a GdkEventType field that you can check for GDK_2BUTTON_PRESS or GDK_DOUBLE_BUTTON_PRESS (alias added in 3.6):
a mouse button has been double-clicked (clicked twice within a short
period of time). Note that each click also generates a
GDK_BUTTON_PRESS event.
in button-press-event callback.
#include <gtk/gtk.h>
gboolean clicked(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
if(event->type == GDK_DOUBLE_BUTTON_PRESS)
printf("double\n");
return TRUE;
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
label = gtk_label_new("label");
gtk_container_add(GTK_CONTAINER(window), label);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
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_widget_show_all(window);
gtk_main ();
return 0;
}

GTK3: Resize Image while retaining aspect-ratio

When I'm resizing the GtkWindow, i want the GtkImage to resize as well while keeping the same aspect ratio as before.
I can't find any good examples on how to set this up with GTK3
This is what i've tried so far:
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
GtkAllocation *allocation = g_new0 (GtkAllocation, 1);
gboolean resize_image(GtkWidget *widget, GdkEvent *event, GtkWidget *window) {
GdkPixbuf *pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(widget));
if (pixbuf == NULL) {
g_printerr("Failed to resize image\n");
return 1;
}
gtk_widget_get_allocation(GTK_WIDGET(widget), allocation);
pixbuf = gdk_pixbuf_scale_simple(pixbuf, widget->allocation.width, widget->allocation.height, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(widget), pixbuf);
return FALSE;
}
int main(int argc, char **argv) {
GtkWidget *window = NULL;
GtkWidget *image = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new_from_file("image.jpg");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(image, "draw", G_CALLBACK(resize_image), (gpointer)window);
gtk_container_add(GTK_CONTAINER(window), image);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
This code should just resize the Pixbuf to the size of the parent, but it doesn't work, i get these errors:
GdkPixbuf-CRITICAL **: gdk_pixbuf_get_width: assertion 'GDK_IS_PIXBUF
(pixbuf)' failed
GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT
(object)' failed
Even if this code would work, i wouldn't be able to keep the same aspect ratio, how to achieve this?
Thanks in Advance
Well, a trick to do this is using a GtkLayout between the image and the window.
Also, instead of using the draw signal, use the size-allocate signal.
We will load a GdkPixbuf and use it as reference for scaling, otherwise the quality would deteriorate with cumulative 'resizing'.
A simple approach would be:
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
struct _resize_widgets {
GtkWidget *image;
GdkPixbuf *pixbuf;
};
typedef struct _resize_widgets ResizeWidgets;
gboolean resize_image(GtkWidget *widget, GdkRectangle *allocation, gpointer user_data) {
int x,y,w,h;
GdkPixbuf *pxbscaled;
GtkWidget *image = (GtkWidget *) ((ResizeWidgets *) user_data)->image;
GdkPixbuf *pixbuf= (GdkPixbuf *) ((ResizeWidgets *) user_data)->pixbuf;
x = 0;
y = 0;
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);
if (w < allocation->width) {
x = (allocation->width - w) / 2;
gtk_layout_move(GTK_LAYOUT(widget), image, x, y);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pxbscaled);
g_object_unref (pxbscaled);
return FALSE;
}
int main(int argc, char **argv) {
GtkWidget *window = NULL;
GtkWidget *image = NULL;
GtkWidget *container = NULL;
GdkPixbuf *pixbuf = NULL;
ResizeWidgets *widgets;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
container = gtk_layout_new(NULL, NULL);
image = gtk_image_new();
pixbuf = gdk_pixbuf_new_from_file ("image.png", NULL);
if (pixbuf == NULL) {
g_printerr("Failed to resize image\n");
return 1;
}
widgets = g_new0(ResizeWidgets, 1);
widgets->image = image;
widgets->pixbuf = pixbuf;
gtk_container_add(GTK_CONTAINER(window), container);
gtk_layout_put(GTK_LAYOUT(container), image, 0, 0);
gtk_widget_set_size_request (GTK_WIDGET(window), 20, 20);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(container, "size-allocate", G_CALLBACK(resize_image), widgets);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
g_object_unref (pixbuf);
g_free(widgets);
return 0;
}
Compile with gcc -o main main.cpkg-config --cflags --libs gtk+-3.0`
This will keep aspect ratio. If that is not desired, then the resize_image callback handler would be:
gboolean resize_image(GtkWidget *widget, GdkRectangle *allocation, gpointer user_data) {
int x,y,w,h;
GdkPixbuf *pxbscaled;
GtkWidget *image = (GtkWidget *) ((ResizeWidgets *) user_data)->image;
GdkPixbuf *pixbuf= (GdkPixbuf *) ((ResizeWidgets *) user_data)->pixbuf;
pxbscaled = gdk_pixbuf_scale_simple(pixbuf, allocation->width, allocation->height, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pxbscaled);
g_object_unref (pxbscaled);
return FALSE;
}
This is a simple hack, there are other options obviously, like cairo for drawing or simply checking Eye of Gnome (eog) implementation which is much more complete.

Is this simplification of GTK+ code correct?

I found the following GTK+3 code in Zetcode. It creates an animation using the cairo library while displaying an image:
#include <cairo.h>
#include <gtk/gtk.h>
/* compile with
*
* gcc spectrum.c -o spectrum `pkg-config --cflags --libs gtk+-3.0`
*
* */
static void do_drawing(cairo_t *);
struct {
gboolean timer;
cairo_surface_t *image;
cairo_surface_t *surface;
gint img_width;
gint img_height;
} glob;
static void init_vars()
{
glob.image = cairo_image_surface_create_from_png("beckov.png");
glob.img_width = cairo_image_surface_get_width(glob.image);
glob.img_height = cairo_image_surface_get_height(glob.image);
glob.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
glob.img_width, glob.img_height);
glob.timer = TRUE;
}
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_t *ic;
static gint count = 0;
ic = cairo_create(glob.surface);
gint i, j;
for (i = 0; i <= glob.img_height; i+=7) {
for (j = 0 ; j < count; j++) {
cairo_move_to(ic, 0, i+j);
cairo_line_to(ic, glob.img_width, i+j);
}
}
count++;
if (count == 8) glob.timer = FALSE;
cairo_set_source_surface(cr, glob.image, 10, 10);
cairo_mask_surface(cr, glob.surface, 10, 10);
cairo_stroke(ic);
cairo_destroy(ic);
}
static gboolean time_handler(GtkWidget *widget)
{
if (!glob.timer) return FALSE;
gtk_widget_queue_draw(widget);
return TRUE;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
init_vars();
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER (window), darea);
g_signal_connect(G_OBJECT(darea), "draw",
G_CALLBACK(on_draw_event), NULL);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 325, 250);
gtk_window_set_title(GTK_WINDOW(window), "Spectrum");
g_timeout_add(400, (GSourceFunc) time_handler, (gpointer) window);
gtk_widget_show_all(window);
gtk_main();
cairo_surface_destroy(glob.image);
cairo_surface_destroy(glob.surface);
return 0;
}
I achieve exatly the same result if I remove the do_drawing() function and move its code to the on_draw_event(), like:
#include <cairo.h>
#include <gtk/gtk.h>
/* compile with
*
* gcc spectrum.c -o spectrum `pkg-config --cflags --libs gtk+-3.0`
*
* */
struct {
gboolean timer;
cairo_surface_t *image;
cairo_surface_t *surface;
gint img_width;
gint img_height;
} glob;
static void init_vars()
{
glob.image = cairo_image_surface_create_from_png("beckov.png");
glob.img_width = cairo_image_surface_get_width(glob.image);
glob.img_height = cairo_image_surface_get_height(glob.image);
glob.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
glob.img_width, glob.img_height);
glob.timer = TRUE;
}
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
cairo_t *ic;
static gint count = 0;
ic = cairo_create(glob.surface);
gint i, j;
for (i = 0; i <= glob.img_height; i+=7) {
for (j = 0 ; j < count; j++) {
cairo_move_to(ic, 0, i+j);
cairo_line_to(ic, glob.img_width, i+j);
}
}
count++;
if (count == 8) glob.timer = FALSE;
cairo_set_source_surface(cr, glob.image, 10, 10);
cairo_mask_surface(cr, glob.surface, 10, 10);
cairo_stroke(ic);
cairo_destroy(ic);
return FALSE;
}
static gboolean time_handler(GtkWidget *widget)
{
if (!glob.timer) return FALSE;
gtk_widget_queue_draw(widget);
return TRUE;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
init_vars();
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER (window), darea);
g_signal_connect(G_OBJECT(darea), "draw",
G_CALLBACK(on_draw_event), NULL);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 325, 250);
gtk_window_set_title(GTK_WINDOW(window), "Spectrum");
g_timeout_add(400, (GSourceFunc) time_handler, (gpointer) window);
gtk_widget_show_all(window);
gtk_main();
cairo_surface_destroy(glob.image);
cairo_surface_destroy(glob.surface);
return 0;
}
So... I wonder... Am I missing something here (loss of generality)?
Or was the call to do_drawing() in function on_draw_event() of the original code redundant?
Thanks
Yes. It's correct on compiler's side.
But it's a good practice for functions to solve exactly one task. do_drawing is busy with drawing lines and pixels, on_draw_event is busy with processing events. Probably in this code snippet there is no real reason to make a separate function, but usually on_draw_event would be much more complicated.

GTK Application doesn't quit from dialog

I'm programming my first bigger GTK+-Application and i have some troubles with exiting the application.
I want to have a Quit-Button in a dialog box, because normally you should run the program in full-screen-mode.
First I tried to call "gtk_main_quit" direktly from a signal but it also didn't work. Now i tried it through an event, the console output works but "gtk_main_quit" doesn't do anything!
Can somebody explain what I'm doing wrong? If you want to give me some tips for better coding, I will really welcome that, too!
Thanks for you help in advance!
#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>
static gboolean gtk_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_message("delete event occured\n");
gtk_main_quit();
return TRUE;
}
static void check_toggle_fullscreen (GtkToggleButton *checkButton_fullscreen, GtkWindow *window)
{
if (gtk_toggle_button_get_active(checkButton_fullscreen))
{
gtk_window_fullscreen(GTK_WINDOW(window));
}
else
{
gtk_window_unfullscreen(GTK_WINDOW(window));
}
}
static gboolean double_clicked (GtkWidget *eventbox, GdkEventButton *event, GtkWindow *window)
{
GtkWidget *dialog, *hbox, *checkButton_fullscreen, *image, *button_preferences, *button_closeApp;
dialog = gtk_dialog_new_with_buttons("Schnelleinstellung", window, GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
checkButton_fullscreen = gtk_check_button_new_with_label("Fullscreen");
image = gtk_image_new_from_stock(GTK_STOCK_FULLSCREEN, GTK_ICON_SIZE_BUTTON);
hbox = gtk_hbox_new(FALSE, 10);
gtk_box_pack_start_defaults (GTK_BOX(hbox), image);
gtk_box_pack_start_defaults (GTK_BOX(hbox), checkButton_fullscreen);
if (gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(window))) & GDK_WINDOW_STATE_FULLSCREEN)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkButton_fullscreen), TRUE);
}
button_closeApp = gtk_button_new_from_stock(GTK_STOCK_QUIT);
button_preferences = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
//Fill dialog with content
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_preferences);
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_closeApp);
g_signal_connect(G_OBJECT(checkButton_fullscreen), "toggled", G_CALLBACK(check_toggle_fullscreen), (gpointer)window);
g_signal_connect(G_OBJECT(button_closeApp), "clicked", G_CALLBACK(gtk_delete_event), NULL);
if (event-> type == GDK_2BUTTON_PRESS)
{
gtk_widget_show_all(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *vbox2;
GtkWidget *label[5];
GtkWidget *frame1, *frame2;
GtkWidget *eventbox;
PangoFontDescription *font;
gtk_init(&argc, &argv);
//Window_TOPLEVEL
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Abnahme");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
gtk_window_set_decorated(GTK_WINDOW(window), TRUE);
vbox = gtk_vbox_new(FALSE, 10);
vbox2 = gtk_vbox_new(FALSE, 10);
frame1 = gtk_frame_new("Naechster Skid");
frame2 = gtk_frame_new("Warteschlange");
eventbox = gtk_event_box_new();
gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_IN);
gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_IN);
label[0] = gtk_label_new("1");
font = pango_font_description_from_string("Arial 40");
gtk_widget_modify_font(label[0], font);
gtk_label_set_markup(GTK_LABEL(label[0]), "<b>Erster Skid</b>");
int j = 1;
while(j<5)
{
gchar buffer[10];
label[j] = gtk_label_new("0");
g_snprintf(buffer, sizeof(buffer), "%i",j+1);
gtk_label_set_markup(GTK_LABEL(label[j]), buffer);
gtk_widget_modify_font(label[j], font);
j++;
}
//Level 0
gtk_container_add(GTK_CONTAINER(window), eventbox);
gtk_event_box_set_above_child(GTK_EVENT_BOX(eventbox), TRUE);
gtk_container_add(GTK_CONTAINER(eventbox), vbox);
//Level 1
gtk_box_pack_start(GTK_BOX(vbox),frame1, TRUE, TRUE, 0);
//Level 2
gtk_container_add(GTK_CONTAINER(frame1), label[0]);
//Level 1
gtk_box_pack_start(GTK_BOX(vbox),frame2, TRUE, TRUE, 0);
//Level 2
gtk_container_add(GTK_CONTAINER(frame2), vbox2);
//Level 3
int i = 1;
while(i<5)
{
gtk_box_pack_start(GTK_BOX(vbox2), label[i], TRUE, TRUE, 0);
i++;
}
//Signals
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_delete_event), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(double_clicked), (gpointer) window);
gtk_widget_set_events(eventbox, GDK_BUTTON_PRESS_MASK);
gtk_widget_realize(eventbox);
/* Enter the main loop */
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
gtk_dialog_run() enters a recursive main loop. gtk_main_quit() only exits the innermost recursion of the main loop.
I don't know if GTK+ provides a clean way to do what you want; you might have to do that yourself somehow.
To delete gtk_dialog_run() and gtk_widget_destroy() like under the code is available to quit dialog button.
if (event-> type == GDK_2BUTTON_PRESS)
{
gtk_widget_show_all(dialog);
/*
* gtk_dialog_run(GTK_DIALOG(dialog));
* gtk_widget_destroy(dialog);
*/
}
It seems that the reason is running dialog is blocking to quit app.

GTK Timer - How to make a timer within a frame

How do g_timer_new works?
is it possible to do a
char timerz[50];
GTimer *timer
g_timer_start(GTimer *timer);
strcpy(timerz,(g_timer_elapsed(GTimer *timer))
Or what Could I do to have a timer in a gtk_frame???
Have a nice day! :D
You can use g_timeout_add or g_timeout_add_seconds which will call a timeout function at regular interval. Please note that this timeout function can be delayed due to event processing and should not be relied on for precise timing. Check this developer page for more info on main loop.
Here is an example to get you started (you can improvise over this). In the example the timeout is set in seconds thus using g_timeout_add_seconds, use g_timeout_add if you need millisecond precision.
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
/* Determines if to continue the timer or not */
static gboolean continue_timer = FALSE;
/* Determines if the timer has started */
static gboolean start_timer = FALSE;
/* Display seconds expired */
static int sec_expired = 0;
static void
_quit_cb (GtkWidget *button, gpointer data)
{
(void)button; (void)data; /*Avoid compiler warnings*/
gtk_main_quit();
return;
}
static gboolean
_label_update(gpointer data)
{
GtkLabel *label = (GtkLabel*)data;
char buf[256];
memset(&buf, 0x0, 256);
snprintf(buf, 255, "Time elapsed: %d secs", ++sec_expired);
gtk_label_set_label(label, buf);
return continue_timer;
}
static void
_start_timer (GtkWidget *button, gpointer data)
{
(void)button;/*Avoid compiler warnings*/
GtkWidget *label = data;
if(!start_timer)
{
g_timeout_add_seconds(1, _label_update, label);
start_timer = TRUE;
continue_timer = TRUE;
}
}
static void
_pause_resume_timer (GtkWidget *button, gpointer data)
{
(void)button;/*Avoid compiler warnings*/
if(start_timer)
{
GtkWidget *label = data;
continue_timer = !continue_timer;
if(continue_timer)
{
g_timeout_add_seconds(1, _label_update, label);
}
else
{
/*Decrementing because timer will be hit one more time before expiring*/
sec_expired--;
}
}
}
static void
_reset_timer (GtkWidget *button, gpointer data)
{
(void)button; (void)data;/*Avoid compiler warnings*/
/*Setting to -1 instead of 0, because timer will be triggered once more before expiring*/
sec_expired = -1;
continue_timer = FALSE;
start_timer = FALSE;
}
int main(void)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *start_button;
GtkWidget *pause_resume_button;
GtkWidget *reset_button;
GtkWidget *quit_button;
GtkWidget *label;
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add(GTK_CONTAINER(window), vbox);
label = gtk_label_new("Time elapsed: 0 secs");
start_button = gtk_button_new_with_label("Start");
g_signal_connect(G_OBJECT(start_button), "clicked", G_CALLBACK(_start_timer), label);
pause_resume_button = gtk_button_new_with_label("Pause/Resume");
g_signal_connect(G_OBJECT(pause_resume_button), "clicked", G_CALLBACK(_pause_resume_timer), label);
reset_button = gtk_button_new_with_label("Reset");
g_signal_connect(G_OBJECT(reset_button), "clicked", G_CALLBACK(_reset_timer), label);
quit_button = gtk_button_new_with_label("Quit");
g_signal_connect(G_OBJECT(quit_button), "clicked", G_CALLBACK(_quit_cb), NULL);
gtk_box_pack_start (GTK_BOX(vbox), label, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), start_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), pause_resume_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), reset_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), quit_button, 0, 0, 0);
gtk_widget_show_all(window);
g_timeout_add_seconds(1, _label_update, label);
continue_timer = TRUE;
start_timer = TRUE;
gtk_main();
return 0;
}
Hope this helps!
vbox = gtk_vbox_new (FALSE, 2);
In this line, "vbox" needs to be replaced with "box".
int main(int argc, char **argv);
gtk_init(&argc, &argv);

Resources