Why do I get a segmentation fault in my GTK+ signal callback? - c

I am trying to measure the size of a GTK label:
#include <gtk/gtk.h>
static void map_event(GtkWidget *window, gpointer lab) {
g_print( "In the callback..\n" );
GtkWidget *label = GTK_WIDGET(lab);
g_print( "Everything is ok..\n" );
}
static void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window1");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 280);
GtkWidget *grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
GtkWidget *label = gtk_label_new("Hello world!");
gtk_grid_attach(GTK_GRID (grid), label, 0,0,1,1);
g_signal_connect (window, "map-event", G_CALLBACK(map_event), label);
gtk_widget_show_all (window);
}
int main (int argc, char **argv) {
GtkApplication *app = gtk_application_new (
"org.gtk.example", G_APPLICATION_FLAGS_NONE );
g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
This gives output:
In the callback..
Segmentation fault (core dumped)
If I comment out the line:
GtkWidget *label = GTK_WIDGET(lab);
there is no segmentation fault, the label shows up and the output is:
In the callback..
Everything is ok..
What am I missing here?

map-event has following signature, so you are missing GdkEvent* argument:
gboolean
user_function (GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
Unfortunately, GTK+ is written in C, so it lacks type-safe callback functions, so it's easy to make mistakes such as this.

You don't respect the signal signature. Each signal is associated with a pre-defined function prototype you must respect, otherwise you'll just read garbage. Here you just made up out of your mind the callback signature so things won't work as expected.
The signal is like a delicious fruit delivery service. By connecting to that signal, you signed a contract that subscribe you to the fruit delivery service. Fruit will be delivered only when the fruit is ripe. The delivery man will:
come in front of your home
drop some fruit boxes for you
knock at your door
go back to its truck
The contract also specifies that:
box #1 will contain bananas
box #2 will contain apples
box #3 will contain oranges
Those boxes are like the arguments of your callback. The map-event takes 3 arguments, thus the 3 boxes.
One day, you hear knocking at the door. You open the door, see the boxes, open box #2 and get annoyed saying "damn, I said I wanted oranges!". The thing is that you're mixing apples and oranges: by contract, oranges are in box #3 and you're looking for them in box #2.
So give a look at the documentation of each signal you want to connect to. That's the only way to write the right callback. Here you forgot one input parameter as well as the return value. In the case of map-event, that return value can be seen as you going to the truck to say if you want to continue or stop the deliveries.

Related

Create and save a file in GTK2 using C

I'm following the instructions of the official Raspberry Pi book called An introduction to C & GUI Programming (link).
It uses GTK2 to create a gui in C.
I encountered some problems trying the code that should save a file. Here the code of the book (same I used):
#include <gtk/gtk.h>
static void save_file (GtkWidget *btn, gpointer ptr)
{
GtkWidget *sch = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (ptr), GTK_FILE_CHOOSER_ACTION_SAVE, "Cancel", 0, "OK", 1, NULL);
if (gtk_dialog_run (GTK_DIALOG (sch)) == 1)
{
printf ("%s selected\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (sch)));
}
gtk_widget_destroy (sch);
}
void end_program (GtkWidget *wid, gpointer ptr)
{
gtk_main_quit ();
}
int main (int argc, char * argv[])
{
gtk_init (&argc, &argv);
GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *btn = gtk_button_new_with_label ("Close window");
g_signal_connect (btn, "clicked", G_CALLBACK (end_program), NULL);
g_signal_connect (win, "delete_event", G_CALLBACK (end_program), NULL);
GtkWidget *vbox = gtk_vbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (win), box);
GtkWidget *fc_btn = gtk_button_new_with_label ("Save file");
g_signal_connect (fc_btn, "clicked", G_CALLBACK (save_file), win);
gtk_box_pack_start (GTK_BOX (vbox), fc_btn, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (vbox), btn, TRUE, TRUE, 0);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
The books says that this program should open a window with a button that, if clicked, opens a new window where I can insert the name of the file and with OK I should be able to save it.
The resulting file path is printed correctly inside the terminal.
If I enter inside the path where I saved the file, the file doesn't exist! It's not hidded neither saved with a different name.
Is there something missing in this piece of code?
I left you a comment above. As noted, I reviewed the sample code in the PDF version of the book in your link. The purpose of that code is to present a file chooser dialog widget and confirm the name of the file you either select or type in by printing the full path in the console. The program, as is, has no mechanism for actually writing a file out to your storage medium. So that is why you do not see a file when you view the folder on your system. If you want as a minimum, a file actually written, a little more code needs to be added. Just to give you one really simple example of what that might look like, I made a revision to the "save_file" function in your sample program as noted in the following code snippet.
static void save_file (GtkWidget *btn, gpointer ptr)
{
GtkWidget *sch = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (ptr), GTK_FILE_CHOOSER_ACTION_SAVE, "Cancel", 0, "OK", 1, NULL);
if (gtk_dialog_run (GTK_DIALOG (sch)) == 1)
{
printf ("%s selected\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (sch)));
char cmd[1024];
strcpy(cmd, "touch ");
strcat(cmd, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (sch)));
system(cmd);
}
gtk_widget_destroy (sch);
}
FYI, I built this revised program on a Linux system using the "touch" command which will either update an existing file's timestamp or create a new empty file and the "system" command which allows one to execute a program as though one was using a terminal. You mentioned a Raspberry Pi system, which I believe uses Linux or a Linux like operating system, so these commands should work.
For further examples and tutorials, you might want to check out some videos out on the web. The following link is not a specific recommendation, but it was one of the first videos I found out on the web that walks through the various steps of C coding with GTK including references to the GTK file chooser dialog.
"https://www.youtube.com/watch?v=EdJVkr87LSk&list=PLMkSWKN9VsZH562FmV8sMvMu_sVZsYAt6"
You might want to review those videos to see if they might help you.
I hope that clarifies things for you.
Regards.

GTK2+ error: GTK_IS_CONTAINER & GTK_IS_WIDGET failed

It's my first post here, I made this account because I'm kind of stumped.
I'm trying to practise passing structures as arguments to callbacks, and for that purpose I created a simple program with a button in a window. In the first iteration, pressing the button with label "Button 1" will change it into a different button with label "Button 2", while in the second the change is done by hovering over the buttons.
Below is the code for the first iteration
#include <gtk/gtk.h>
typedef struct {
GtkWidget *button1;
GtkWidget *button2;
GtkWidget *window;
} example;
void callback_func (GtkWidget *ignored, example *test) {
GtkWidget *window=test->window;
GtkWidget *changebutton1=test->button1;
GtkWidget *changebutton2=test->button2;
gtk_container_remove(GTK_CONTAINER(window),changebutton1);
gtk_container_add(GTK_CONTAINER(window),changebutton2);
gtk_widget_show_all(window);
}
void callback_func2 (GtkWidget *ignored, example *test) {
GtkWidget *window=test->window;
GtkWidget *changebutton1=test->button1;
GtkWidget *changebutton2=test->button2;
gtk_container_remove(GTK_CONTAINER(window),changebutton2);
gtk_container_add(GTK_CONTAINER(window),changebutton1);
gtk_widget_show_all(window);
}
int main(int argc, char *argv[]) {
example test;
gtk_init(&argc,&argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *changebutton1=gtk_button_new_with_label("Button 1");
GtkWidget *changebutton2=gtk_button_new_with_label("Button 2");
test.window=window;
test.button1=changebutton1;
test.button2=changebutton2;
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(window),changebutton1);
g_signal_connect (G_OBJECT (changebutton1), "clicked", G_CALLBACK (callback_func), (gpointer*)&test);
g_signal_connect (G_OBJECT (changebutton2),"clicked",G_CALLBACK(callback_func2),(gpointer*)&test);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
When running the above code, by clicking the button once, the buttons switch properly, but when I click it again (now calling callback_func2 instead of callback_func, which are essentially identical save for the buttons switching place) I get this:
(gtktesting.exe:92024): Gtk-CRITICAL **: gtk_container_add: assertion `GTK_IS_WIDGET (widget)' failed
If I replace the "clicked" events with "enter_notify_event" and "leave_notify_event", the first change fails as well, now yielding more errors.
Anybody know what's going on?
Also, a bonus question. I pretty much copied and pasted the base of this code from another post, since I had been trying to write it by myself unsuccessfully and I wanted to test if it works at all. I noticed that the second argument in the callbacks is "example *test". Can anybody explain the "example" type? It's only the name of the stucture, and I've no idea what it's doing there.
tl;dr
Try:
test.window = g_object_ref(window);
test.button1 = g_object_ref(changebutton1);
test.button2 = g_object_ref(changebutton2);
Full explaination
Objects (i.e. subclasses of GObject) in GTK+ are reference counted. That means, every object (e.g. Widget) has "reference count" - number of pointers that point to it. When number reaches 0 - object is deallocated. Objects are created with reference count of 1. As C does not have smart pointers C++ does nor anything similar, reference counting has to be done manually. User has to call g_object_ref in order to retain a reference (I use words "pointer" and "reference" interchangeably), and g_object_unref when user is done with a reference. That ensures that no object is destroyed while being in use.
GtkWidget is special, as it begins life with "floating" reference. That means that first time widget is referenced its reference count does not increase - its "floating" reference is "sunk". After that it behaves like any other GObject.
When you create your buttons, they are created with reference count 1 ("floating"). When they are added to their container, their references are still 1 (but "sunk"). This means, buttons are owned by the container that they're added to.
Now, when you remove button from container:
gtk_container_remove(GTK_CONTAINER(window),changebutton1);
reference count for changebutton1 is decreased, drops to 0, which forces object destruction, and test.button1 is now dangling pointer.
To overcome this, use g_object_ref anytime you want to store pointer to GObject. That way you express "test participates in ownership of changebutton1" (or, "test is interested in keeping changebutton1 alive).
When you are done with window, button1 and button2, call g_object_unref on them.
Bonus question
Also, a bonus question. I pretty much copied and pasted the base of
this code from another post, since I had been trying to write it by
myself unsuccessfully and I wanted to test if it works at all. I
noticed that the second argument in the callbacks is "example *test".
Can anybody explain the "example" type? It's only the name of the
stucture, and I've no idea what it's doing there.
example is defined here:
typedef struct {
GtkWidget *button1;
GtkWidget *button2;
GtkWidget *window;
} example; // declares type "example"
GObject signal system is designed in a sucha way that it allows passing arbitrary pointer as a last argument to callback, so a programmer can pass extra information from place where signal connection is created (g_signal_connect), to a callback.

GTK+ 2.0 C weird structure and g_signal_connect_swapped

I have following declarations:
typedef struct window_and_search_entry
{
GtkWidget *window;
GtkWidget *search_entry;
} WINDOW_AND_SEARCH_ENTRY;
and then in main:
GtkWidget *window;
GtkWidget *search_entry;
...
WINDOW_AND_SEARCH_ENTRY window_and_search_entry;
window_and_search_entry.window = window;
window_and_search_entry.search_entry = search_entry;
and that two:
g_signal_connect_swapped(G_OBJECT(search_entry), "activate", G_CALLBACK(analyse), (gpointer) &window_and_search_entry);
g_signal_connect_swapped(G_OBJECT(do_it_button), "clicked", G_CALLBACK(analyse), (gpointer) &window_and_search_entry);
And I want to create function which takes text, and window, makes some operations on it and if an error occurs print it out with other function which takes window as parameter
ELEMENT *analyse(GtkWidget *widget, gpointer user_data)
{
//((WINDOW_AND_SEARCH_ENTRY *) user_data)->search_entry;
GtkWidget *a = ((WINDOW_AND_SEARCH_ENTRY*)(user_data))->search_entry;
const char *text = gtk_entry_get_text(GTK_ENTRY(a));
g_print("%s\n", text);
ELEMENT *heap[100];
int index = 0;
return heap[1];
}
I tried many variants but I get "Segmentation fault" after having something typed in entry_box followed by enter or button. I want to print the text to console. Please help me, thanks.
Your analyse callback requires g_signal_connect, not g_signal_connect_swapped. Also there is no need to cast pointers: the G_CALLBACK() macro already cast the whole function, so this would suffice:
ELEMENT *analyse(GtkWidget *widget, WINDOW_AND_SEARCH_ENTRY *data)
With g_signal_connect_swapped it would be:
ELEMENT *analyse(WINDOW_AND_SEARCH_ENTRY *data, GtkWidget *widget);
This swapped version is only a C convenience for already existing functions, e.g. you can do something like this:
g_signal_connect_swapped(G_OBJECT(search_entry), "activate",
G_CALLBACK(g_print), "Activate signal called");

How to Implement a button-press-event on GtkTable

Searching the web for answers dosen't get me through my problem:
I wan't my GtkTable to throw an event, if i click one cell.
Since there is no click event, accept for GtkButton's, i wanted to implement a GDK_BUTTON_PRESS_MASK and GDK_BUTTON_RELEASE_MASK to catch the position of the mouse on the Table during click.
Works great with GtkDrawingArea!
Tryed the snipet bellow, but nothing happend, maybe someone can give me a clue :)
little sample:
static void table_press(GtkWidget *widget, GdkEventButton *event)
{
printf("table pressed");
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget* table;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW (window), "table click");
table = gtk_table_new(2, 5, TRUE);
gtk_container_add(GTK_CONTAINER (window), table);
gtk_widget_add_events(table, GDK_BUTTON_PRESS_MASK);
g_signal_connect(GTK_OBJECT (table), "button-press-event",
G_CALLBACK (table_press), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show_all(window);
gtk_main();
main_exit();
return 0;
}
You don't receive events because GtkTable does not have a GdkWindow associated with it. You can use GtkEventBox which lets you accept events on widgets that would not normally accept events. This is derived from GtkBin so the interesting code would look like this.
table = gtk_table_new(2, 5, TRUE);
event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER (window), event_box);
gtk_container_add(GTK_CONTAINER (event_box), table);
g_signal_connect(GTK_OBJECT (event_box), "button-press-event",
G_CALLBACK (table_press), NULL);

Detect which key is pressed in C

I'm trying to find a way to find out which key is pressed down in C. This will be in a graphical environment, written in GTK2, but I don't think the answer lies there. I think I might be able to do this using Xlib, but I haven't been able to find anything conclusive on this.
Does anyone have any suggestions on how to do this?
I've managed to catch a keypress using the follow code:
GtkWidget *window;
void gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),NULL);
However, I would like to identify which key is pressed. From the link posted by Aditya Kumar, I know the answer lies with using GdkEventKey, since it is a structure which has a keyval field, but I cannot seem to get the syntax right. What is the correct way of getting this number?
This is a method I've tried:
static void hello( GtkWidget *widget,
guint data ){
g_print ("Hello World, %d was pressed\n",data);}
I tried supplying "data" by doing this when I catch the key_release_event:
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),GdkEventKey.keyval);
However, I get a compiler error like so:
hello.c:85:5: error: expected ‘)’ before ‘.’ token
hello.c:85:5: error: expected expression before ‘,’ token
You are correct with your original syntax.
g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), NULL);
Where the key_event function looks something like (note I am using the gdk_keyval_name to convert the keyval int value to a string for printing):
static gboolean
key_event(GtkWidget *widget,
GdkEventKey *event)
{
g_printerr("%s\n",
gdk_keyval_name (event->keyval));
return FALSE;
}
Here's a complete example program:
#include <gtk/gtk.h>
static gboolean
key_event(GtkWidget *widget,
GdkEventKey *event)
{
g_printerr("%s\n",
gdk_keyval_name (event->keyval));
return FALSE;
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
while looking at the gdk reference manual i think you can capture the keyboard events using this unless you specifically want to have a 'C' program.
Here is the link to help you out.
http://www.gtk.org/api/2.6/gdk/gdk-Keyboard-Handling.html
event->keyval is a pointer to a struct, where keyval contains a integer value for the key pressed, this has been used above in a function gdk_keyval_name (event->keyval) which gets a actual name for the key.

Resources