Re: Help spinbuttons, Paulo Silva

2015-04-09 Thread Stefan Salewski
On Thu, 2015-04-09 at 23:17 +0930, Roger Matthews wrote:
 Hi Paulo, or anybody else interested in helping,
 I've read chapter 2 of Foundations of GTK+ Development concerning
 signals and callback functions as suggested (see below) but am still
 getting this error:

Hello,

I think you used wrong parameters for your callback function: Try this
modified example:

#include stdio.h
#include gtk/gtk.h
 
static void get_new_number(GtkWidget *widget, gpointer data)
{
float value;
  value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widget));
printf(value = %f\n, value);
}
 
int main(int argc, char *argv[])
{
GtkWidget *window, *spin_float;
GtkAdjustment *float_pt;
 
gtk_init (argc, argv);
 
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (window, 150, 100);
 
float_pt = GTK_ADJUSTMENT (gtk_adjustment_new (0.5, 0.0, 1.0, 0.1, 0.5,
0.5));
 
spin_float = gtk_spin_button_new (float_pt, 0.1, 1);
 
gtk_container_add (GTK_CONTAINER (window), spin_float);
gtk_widget_show_all (window);
 
g_signal_connect (spin_float, value-changed, G_CALLBACK
(get_new_number), NULL);
  
gtk_main ();
 
 return 0;
}

$ gcc t.c -o test `pkg-config --cflags --libs gtk+-3.0`
$ ./test 
value = 0.40
value = 0.30
value = 0.20

Please note that there was no callback to close the window in your
example, so we have to press CTRL C to terminate the program. Generally,
if you already have the Krause book, you may find examples for Spin
Buttons too, maybe not printed completely, but full code should be
available in Internet somewhere. But it is GTK2 still.

And note that you do not have to use C language for GTK toolkit -- there
are bindings available fpr many other languages, some have even
tutorials, i.e. Ruby or Python. These may be easier for beginners.




___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Help spinbuttons, Paulo Silva

2015-04-09 Thread Roger Matthews

Hi Paulo, or anybody else interested in helping,
I've read chapter 2 of Foundations of GTK+ Development concerning signals and 
callback functions as suggested (see below) but am still getting this error:
 
(page90v2.exe:2289): Gtk-CRITICAL **: gtk_spin_button_get_value: assertion 
'GTK_IS_SPIN_BUTTON (spin_button)' failed
 
every time one of the up or down arrows in the spinbutton is pressed. Here is 
the code:
 
#include stdio.h
#include gtk/gtk.h
 
/*double number_double;*/
 
 static void get_new_number(GtkWidget *widget, gpointer spin_float)
{
/*number_double = */ gtk_spin_button_get_value(spin_float);
}
 
/*static void print_new_number(GtkWidget *widget, gpointer data)
{
  printf(print_new_number %f\n, number_double);
}*/
 
int main(int argc, char *argv[])
{
GtkWidget *window, *spin_float;
GtkAdjustment *float_pt;
 
/*gdouble number_double;*/
 
gtk_init (argc, argv);
 
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), Spin Buttons);
gtk_container_set_border_width (GTK_CONTAINER(window), 10);
gtk_widget_set_size_request (window, 150, 100);
 
float_pt = GTK_ADJUSTMENT (gtk_adjustment_new (0.5, 0.0, 1.0, 0.1, 0.5, 0.5));
 
spin_float = gtk_spin_button_new (float_pt, 0.1, 1);
 
gtk_container_add (GTK_CONTAINER (window), spin_float);
gtk_widget_show_all (window);
 
  g_signal_connect (spin_float, value-changed, G_CALLBACK (get_new_number), 
NULL);
 /*g_signal_connect (window, value-changed, G_CALLBACK (print_new_number), 
NULL);*/
 
 printf(printf Before gtk_main, double %p\n, /*number_double*/spin_float);
 
gtk_main ();
 
  printf(After gtk_main, double %p\n, /*number_double*/spin_float);
 return 0;
}
//
/*
$ gedit page90v2.c
 
$ gcc `pkg-config --cflags gtk+-3.0` -o page90v2.exe page90v2.c `pkg-config 
--libs gtk+-3.0`
 
$ ./page90.exe
 (page90v2.exe:2289): Gtk-CRITICAL **: gtk_spin_button_get_value: assertion 
'GTK_IS_SPIN_BUTTON (spin_button)' failed
 
(page90v2.exe:2289): Gtk-CRITICAL **: gtk_spin_button_get_value: assertion 
'GTK_IS_SPIN_BUTTON (spin_button)' failed
^C
//
Paulo's reply:
 



Hello Roger.
I think you misunderstand the concept of callback functions. In your example 
there is no appropriated callback function to deal with the changes of spin 
button. And another thing, the 3rd parameters to register a callback function 
must be a pointer.
I suggest to take a look at chapter 2 under Signals and Callbacks section.
José Paulo
/**/
My original email:

2015-03-16 23:24 GMT-03:00 Roger Matthews roger.matth...@hotmail.com:




/* I want a window containing many spinbuttons for both
integers and floating-point numbers, these may then be either 1) left
unchanged, or 2) changed to a new value. Then, whether changed or unchanged,
use the integers and floats as input parameters into various calculations. As
it is the printf() statements only show the unchanged values. */

/*The example below is taken from page 90 of
Foundations of GTK+ Development by Andrew Krause*/

#include stdio.h
#include gtk/gtk.h

int main(int argc, char *argv[])
{
GtkWidget *window, *spin_int, *spin_float;
GtkAdjustment *integer, *float_pt;

gint number_int;
gdouble number_double;

gtk_init (argc, argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), Spin Buttons);
gtk_container_set_border_width (GTK_CONTAINER(window), 10);
gtk_widget_set_size_request (window, 150, 100);

integer = GTK_ADJUSTMENT (gtk_adjustment_new (5.0, 0.0,
10.0, 1.0, 2.0, 2.0));
float_pt = GTK_ADJUSTMENT (gtk_adjustment_new (0.5, 0.0, 1.0,
0.1, 0.5, 0.5));

spin_int = gtk_spin_button_new (integer, 1.0, 0);
spin_float = gtk_spin_button_new (float_pt, 0.1, 1);

gtk_container_add (GTK_CONTAINER (window), spin_int);
gtk_container_add (GTK_CONTAINER (window), spin_float);

gtk_widget_show_all (window);

number_int=gtk_spin_button_get_value_as_int(spin_int);
number_double=gtk_spin_button_get_value(spin_float);

g_signal_connect(integer, change-value, G_CALLBACK (spin_int), number_int);

g_print (Hello World\n);
g_print(g_print Before gtk_main, integer %d\n, number_int);
printf(printf Before gtk_main, integer %d\n, number_int);
printf(printf Before gtk_main, double %f\n, number_double);

gtk_main ();

 printf(Hello World\n);

 printf(After gtk_main %d\n, number_int);

return 0;
}

//

/*Below are the commands to edit, compile and execute the
program, and the warnings, messages and output to the terminal. At this stage
the warnings are unimportant, I merely want to be able to read or save both the
unchanged and changed values displayed in the spinbutton(s).*/

/*

$ gedit page90.c

$ gcc `pkg-config --cflags gtk+-3.0` -o page90.exe page90.c
`pkg-config --libs gtk+-3.0`

$