In order to test my understanding of other bits of Gtk, I would like to write a program which always has an event ready for the main loop to consume. I wrote this short program to try doing this:
#include <gtk/gtk.h>
static void toggle(GtkWidget *check, gpointer data)
{
gboolean checked;
g_object_get(check, "active", &checked, NULL);
g_object_set(check, "active", !checked, NULL);
}
int main(int argc, char *argv[])
{
GtkWidget *window, *check;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
check = gtk_check_button_new();
g_signal_connect(check, "toggled", G_CALLBACK(toggle), NULL);
gtk_container_add(GTK_CONTAINER(window), check);
gtk_widget_show_all(window);
gtk_main();
}
When I run this program and click the check box, it segfaults. What gives? What is the right way to keep the main loop busy?
(Side note: it reliably toggles 2048 times before segfaulting -- a suspiciously round number.)
Within your toggle handler, you're setting checked, which causes a toggle signal to be emitted, which re-invokes the handler...
#11564 0xb775ba50 in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#11565 0xb776e5d0 in ?? () from /usr/lib/libgobject-2.0.so.0
#11566 0xb77774d6 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#11567 0xb7777682 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#11568 0xb7e067ba in gtk_toggle_button_toggled ()
I didn't follow all the way down, but I can see how >11000 frames will lead to a segfault.
To answer your other question: I think the way to keep the main loop full would be with a g_idle_add() call:
#include <gtk/gtk.h>
static void toggle(GtkWidget *check, gpointer data)
{
g_print(".");
}
GtkWidget *window, *check;
static gboolean
toggle_it()
{
gboolean checked;
g_object_get(check, "active", &checked, NULL);
g_object_set(check, "active", !checked, NULL);
return TRUE;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
check = gtk_check_button_new();
g_signal_connect(check, "toggled", G_CALLBACK(toggle), NULL);
gtk_container_add(GTK_CONTAINER(window), check);
gtk_widget_show_all(window);
g_idle_add((GSourceFunc)toggle_it, NULL);
gtk_main();
}
Related
I am having some trouble drawing images with gtk2. I have tried this code:
#include <gtk/gtk.h>
static gboolean button_press_callback (GtkWidget *event_box, GdkEventButton *event, gpointer data)
{
g_print ("Event box clicked at coordinates %f,%f\n",
event->x, event->y);
/* Returning TRUE means we handled the event, so the signal
* emission should be stopped (don't call any further
* callbacks that may be connected). Return FALSE
* to continue invoking callbacks.
*/
return TRUE;
}
static GtkWidget*
create_image (void)
{
GtkWidget *image;
GtkWidget *event_box;
image = gtk_image_new_from_file ("image.png");
}
int main(int argc, char const *argv[])
{
create_image();
return 0;
}
It will not draw any images onscreen, infact I don`t see any window at all. Also, what is the best way to store an image in a variable for future use?
I suggest you to look at the gtk tutorial https://developer.gnome.org/gtk-tutorial/stable/, a lot of things are missing for your code to display here a sample on how to display a simple picture in a window :
#include <gtk/gtk.h>
GtkWidget* create_gui()
{
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL); // create the application window
GtkWidget *img = gtk_image_new_from_file("image.png"); // image shall be in the same dir
gtk_container_add(GTK_CONTAINER(win), img); // add the image to the window
g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(gtk_main_quit), NULL); // end the application if user close the window
return win;
}
int main(int argc, char** argv) {
GtkWidget* win;
gtk_init(&argc, &argv);
win = create_gui();
gtk_widget_show_all(win); // display the window
gtk_main(); // start the event loop
return 0;
}
BTW, gtk 2 is no longer being maintained, I suggest you start with gtk3 if you can
Consider the following code:
#include <gtk/gtk.h>
static void stop(GtkWidget *window, GdkEventKey *key, gboolean *key_held)
{
*key_held = FALSE;
g_print("stopped!\n");
}
static void counter(GtkWidget *window, GdkEventKey *key, gpointer user_data)
{
gboolean key_held = TRUE;
gulong signal_ID = g_signal_connect(window, "key-release-event", G_CALLBACK(stop), &key_held);
for (unsigned long int i = 0;key_held;i++)
{
g_print("%li\n", i);
}
g_signal_handler_disconnect(window, signal_ID);
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_add_events(window, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "key-press-event", G_CALLBACK(counter), NULL);
gtk_widget_show(window);
gtk_main();
}
It's simple: the counter starts to count from 0 when the user presses a key and continues to count until the user release the key...
Or that's at least how it's supposed to work. What actually happens is that when a user presses a key, the counter starts and doesn't stop when the user release the key. The only way to stop counting is to terminate the program. Also, not only the "key-release-event" is not triggered but "delete-event" isn't too: I have to press Ctrl+C to terminate the program.
Here is a program in which there are two buttons (Connect and Disconnect). While clicking on 'connect' button, it calls 'button1_clicked_do_job()' function having while with some condition. On the other hand 'disconnect' button is used to change the condition of while. The problem is the Disconnect button is not working. How do I activate disconnect while 'connect' callback function didn't get returned to main()?
Here is the code:
#include<gtk/gtk.h>
static int check=1;
void button2_clicked_disconnect(GtkWidget *widget, gpointer data2)
{
check=0; //Change the condition of while
}
void button1_clicked_do_job(GtkWidget *widget, gpointer data1)
{
do{
g_print("Accept Connection\n");
sleep(2);
} while(check==1);
g_print("Reject Connection\n");
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button1;
GtkWidget *button2;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 250, 150);
gtk_window_set_title(GTK_WINDOW(window), "Connect_N__Disconnect");
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
button1 = gtk_button_new_with_label("Connect");
gtk_widget_set_size_request(button1, 80, 30);
gtk_fixed_put(GTK_FIXED(fixed), button1, 30, 50);
button2 = gtk_button_new_with_label("Disconnect");
gtk_widget_set_size_request(button2, 80, 30);
gtk_fixed_put(GTK_FIXED(fixed), button2, 130, 50);
g_signal_connect(G_OBJECT(button1), "clicked",
G_CALLBACK(button1_clicked_do_job), NULL);
g_signal_connect(G_OBJECT(button2), "clicked",
G_CALLBACK(button2_clicked_disconnect), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Generally speaking you should not run blocking code (like a long running while-loop) in the main thread because it makes the app unresponsive -- all functions should preferably return in milliseconds. In your case you also expect the main loop to call your other event handler but never give the main loop a chance to do it: the while loop just keeps executing forever. In other words: the first handler needs to return before the main loop can call the second one.
How to fix that depends on what your actual problem is: Using asynchronous code or idle handlers should be the first option, and a worker thread is another (but please only add threads when you know that you actually need them). It's also possible to "pump" the event loop manually but that should really be the last option. See main loop reference for details.
Your example would look something like this:
gboolean check_connection (gpointer user_data)
{
/* g_print what you want based on check value */
if (check == 0)
return FALSE;
return TRUE;
}
void button1_clicked_do_job(GtkWidget *widget, gpointer data1)
{
g_timeout_add_seconds (2, check_connection, NULL);
}
After a handler of an instance has been blocked with g_signal_handler_block, is it possible to check if the handler is still being blocked or has been unblocked by g_signal_handler_unblock in the meantime, apart from storing the state in a boolean variable for example?
I hoped something like that would be possible
g_signal_handler_block (selection, handler_id_row_selected);
if (g_signal_handler_is_blocked (selection, handler_id_row_selected))
g_print ("is still blocked");
But a "g_signal_handler_is_blocked" function does not exist. g_signal_handler_is_connected is not the right function to use, since the signal handler remains connected, thus the function returns TRUE.
I have tried g_signal_handler_find (), since there is G_SIGNAL_MATCH_UNBLOCKED as one of the match types, but it has not worked yet. Even though I have rewritten my code anyway, I still would like to know if it is possible, since i use the blocking/unblocking relatively often.
g_signal_handler_find here is working as expected. Here is my test case:
#include <gtk/gtk.h>
gboolean
g_signal_handlers_is_blocked_by_func(gpointer instance, GFunc func, gpointer data)
{
return g_signal_handler_find(instance,
G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA | G_SIGNAL_MATCH_UNBLOCKED,
0, 0, NULL, func, data) == 0;
}
static void
handler(void)
{
g_print("handler called\n");
}
static void
switch_blocking(GtkWidget *button)
{
GFunc func = (GFunc) handler;
if (g_signal_handlers_is_blocked_by_func(button, func, NULL)) {
g_signal_handlers_unblock_by_func(button, func, NULL);
g_print("handler unblocked\n");
} else {
g_signal_handlers_block_by_func(button, func, NULL);
g_print("handler blocked\n");
}
}
int
main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Click me");
g_signal_connect_after(button, "clicked", G_CALLBACK(switch_blocking), NULL);
g_signal_connect(button, "clicked", G_CALLBACK(handler), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
This question already has an answer here:
save current window as image using gtk#
(1 answer)
Closed 4 years ago.
This is a piece of code which creates a window:
#include <gtk/gtk.h>
static GtkWidget* createWindow()
{
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
gtk_widget_set_name(window, "GtkLauncher");
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
return window;
}
int main(int argc, char* argv[])
{
GtkWidget *main_window;
gtk_init(&argc, &argv);
if (!g_thread_supported())
g_thread_init(NULL);
main_window = createWindow();
gtk_widget_grab_focus(main_window);
gtk_widget_show_all(main_window);
gtk_main();
return 0;
}
And in here: Convert a GTK python script to C , i got how to take a screenshot.
gdk_get_default_root_window() will give me the screenshot of the desktop.
gdk_screen_get_active_window (gdk_screen_get_default()) will give me the screenshot of any active window.
Is there any way to take the screenshot of the window being created in the code above??
I think this should do it, although you may need to iterate the main loop after showing the window to get it to paint properly, in which case you'll need some more code (I haven't tested this)
#include <unistd.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>
static GtkWidget* createWindow()
{
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
gtk_widget_set_name(window, "GtkLauncher");
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
return window;
}
int main(int argc, char **argv)
{
gdk_init(&argc, &argv);
GtkWidget *main_window = createWindow();
gtk_widget_show_all(main_window);
// may not need this, it also may not be enough either
while (gtk_events_pending ()) gtk_main_iteration ();
GdkWindow *w = GDK_WINDOW(main_window);
gint width, height;
gdk_drawable_get_size(GDK_DRAWABLE(w), &width, &height);
GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL,
GDK_DRAWABLE(w),
NULL,
0,0,0,0,width,height);
if(pb != NULL) {
gdk_pixbuf_save(pb, "screenshot.png", "png", NULL);
g_print("Screenshot saved to screenshot.png.\n");
} else {
g_print("Unable to get the screenshot.\n");
}
return 0;
}
If it doesn't work you'll have to move the screenshot taking into an event handler that connects to some event (I'm not sure which probably window-state-event then you have to look at the event to figure out when to take the screenshot)