The main loop for GTK http://developer.gnome.org/gtk3/stable/gtk3-General.html
is based on the Glib main loop see http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html
. This is where the call-back functions will be called from on the many events that may happen such as the "clicked" on your button.
Each call-back function will have a particular way of being called, so the proto-type for the "clicked" signal for a GtkButton is
void user_function (GtkButton *button, gpointer user_data)
Signal call-back functions can also be called with many arguments, for example the "move-cursor" signal for GtkTextView
void user_function (GtkTextView *text_view,
You must follow the proto-types for each signal as it is in the documentation otherwise you may end up with very unexpected problems or crashes. Most signal call-back functions do not have a return value and if they do it is usually information for the GTK/Glib main loop mainly to say disconnect this signal or to carry on with other signal call-back functions or not.
The arguments passed to a call-back function will be any information relevant to that signal. So for the "clicked" for GtkWidget this is a pointer to the GtkButton the signal happened to. Then there is "gpointer user_data" this can be used for your own purposes and the value passed to the call-back functions is the value passed when you connected the signal. "user_data" can be an integer value if suitable cast. The way to do that is how "asdfgh2091" pointed out, doing it this way makes it very clear that what you are passing will be an integer and will do the casting properly for you. As it is a pointer it can point to anything, so it can pointer to a function, point to an area of data which could be anything such as an array of data or a structure.
Going back to your original call-back function for the "clicked" signal for GtkButton, you have done some things right and some things wrong.
- The value you have returned from the call-back will be ignored as the signal is expecting the call-back function not to return a value. You would be better to check for any errors, bounds checking or other things within the call-back function.
- You have used g_signal_connect_swapped() to connect your signal. This basically places the "user_data" as the first argument. In your case you have chosen to ignore the other arguments, which is OK, but for some people this can be seen as bad.
As Paul said in his post a GtkSpinButton http://developer.gnome.org/gtk3/stable/GtkSpinButton.html
would be better for setting data for a stepper motor. This way you can set the input range (minimum and maximum values) have only integer values allowed have up/down buttons and have all this handled for you by GTK. All you have to do is handle the higher level stuff of getting when the value changes and setting your stepper motor.
Also it is worth reading on the Glib library functions so that you know what is there. Much of it is very useful with helper functions for data structures, string handling (ASCII and UTF8 including conversion to and from other formats), threads, and many other utility functions. I have seen too many people write the same bit of code of between 5 to 20 lines repeatedly some times in slightly different ways each time and with this code not being easy to read. This can often be replaced with a single function call which is easy to see what it does.