1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
| #include "spinbuttondiscret.h"
/* spinbuttondiscret.c */
/* Properties enum */
enum
{
P_0, /* Padding */
P_SHOW_LABEL, //propriété pour afficher un label à côté du spin button
};
/* Internal API */
void gtk_spinbutton_discret_load_discret_list (GtkSpinButtonDiscret *spin,GList *discret_list);
void gtk_spinbutton_discret_show_label(GtkSpinButtonDiscret *spin, gboolean show_label);
static void gtk_spinbutton_discret_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gtk_spinbutton_discret_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void gtk_spinbutton_discret_size_request(GtkWidget *widget, GtkRequisition *requisition);
static void gtk_spinbutton_discret_get_preferred_height (GtkWidget *widget, gint *minimal_height, gint *natural_height);
static void gtk_spinbutton_discret_get_preferred_width (GtkWidget *widget, gint *minimal_width, gint *natural_width);
static void gtk_spinbutton_discret_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
static gboolean gtk_spinbutton_discret_draw (GtkWidget * widget, cairo_t *cr);
static void gtk_spinbutton_discret_realize(GtkWidget *widget);
/* Define type */
G_DEFINE_TYPE_WITH_PRIVATE(GtkSpinButtonDiscret, gtk_spinbutton_discret, GTK_TYPE_SPIN_BUTTON)
/** signification des paramètres pour G_DEFINE_TYPE(GtkSpinButtonDiscret, gtk_spinbutton_discret, GTK_TYPE_SPIN_BUTTON)
* le même préfixe GtkSpinButtonDiscret que pour la classe GtkSpinButtonDiscretClass
* implementation de la fonction (gtk_spinbutton_discret)_get_type avec un protocole standard
* define a parent class pointer accessible from the whole .c file
* a noter que GTK_TYPE_SPIN_BUTTON a été trouvé dans le code source gtkspinbutton.c qui est consultable
**/
/* Public API */
/**
* gtk_spin_button_new:
* @adjustment: (allow-none): the #GtkAdjustment object that this spin
* button should use, or %NULL
* @climb_rate: specifies how much the spin button changes when an arrow
* is clicked on
* @digits: the number of decimal places to display
*
* Creates a new #GtkSpinButton.
*
* Returns: The new spin button as a #GtkWidget
* classic engine
GtkWidget * gtk_spin_button_new (GtkAdjustment *adjustment,
gdouble climb_rate,
guint digits)
{
GtkSpinButton *spin;
if (adjustment)
g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
spin = g_object_new (GTK_TYPE_SPIN_BUTTON, NULL);
gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
return GTK_WIDGET (spin);
}
* GtkSpinButton::input:
* @spin_button: the object on which the signal was emitted
* @new_value: (out) (type double): return location for the new value
*
* The ::input signal can be used to influence the conversion of
* the users input into a double value. The signal handler is
* expected to use gtk_entry_get_text() to retrieve the text of
* the entry and set @new_value to the new value.
*
* The default conversion uses g_strtod().
*
* Returns: %TRUE for a successful conversion, %FALSE if the input
* was not handled, and %GTK_INPUT_ERROR if the conversion failed.
*
* spinbutton_signals[INPUT] =
g_signal_new (I_("input"),
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSpinButtonClass, input),
NULL, NULL,
_gtk_marshal_INT__POINTER,
G_TYPE_INT, 1,
G_TYPE_POINTER);
*
* GtkSpinButton::output:
* @spin_button: the object on which the signal was emitted
*
* The ::output signal can be used to change to formatting
* of the value that is displayed in the spin buttons entry.
* |[<!-- language="C" -->
* // show leading zeros
* static gboolean
* on_output (GtkSpinButton *spin,
* gpointer data)
* {
* GtkAdjustment *adjustment;
* gchar *text;
* int value;
*
* adjustment = gtk_spin_button_get_adjustment (spin);
* value = (int)gtk_adjustment_get_value (adjustment);
* text = g_strdup_printf ("%02d", value);
* gtk_entry_set_text (GTK_ENTRY (spin), text);
* g_free (text);
*
* return TRUE;
* }
* ]|
*
* Returns: %TRUE if the value has been displayed
*/
GtkWidget *gtk_spinbutton_discret_new(GtkAdjustment *adjustment,
gdouble climb_rate,
guint digits,
GList * discret_list)
{
/** retourne une instance Gobject de type gtk_spinbutton_discret_get_type() **/
#ifdef CB_TEST
g_print ("création de gtk_spinbutton_discret_new\n");
#endif
GtkSpinButton *spin;
if (adjustment)
g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
spin = g_object_new (SPINBUTTON_TYPE_DISCRET, NULL);
//affectation de de la liste
gtk_spinbutton_discret_load_discret_list (GTK_SPINBUTTONDISCRET(spin), discret_list);
gtk_spin_button_configure (spin, adjustment, climb_rate, digits);
GtkSpinButtonDiscretPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(spin, SPINBUTTON_TYPE_DISCRET, GtkSpinButtonDiscretPrivate);
g_print ("\n gtk_spinbutton_discret_new SUCCESS ligne= %d\n", __LINE__);
gboolean test = TRUE;
if (test)
return GTK_WIDGET (spin);
else
return GTK_WIDGET (priv->box);
}
/* Initialization */
static void gtk_spinbutton_discret_class_init(GtkSpinButtonDiscretClass *klass)
{
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_class_init\n");
#endif
GObjectClass *g_class;
GtkWidgetClass *widget_class;
GParamSpec *pspec;
g_class = G_OBJECT_CLASS(klass);
widget_class = GTK_WIDGET_CLASS(klass);
/* Override widget class methods */
g_class->set_property = gtk_spinbutton_discret_set_property;
g_class->get_property = gtk_spinbutton_discret_get_property;
widget_class->realize = gtk_spinbutton_discret_realize;
widget_class->get_preferred_width = gtk_spinbutton_discret_get_preferred_width;
widget_class->get_preferred_height = gtk_spinbutton_discret_get_preferred_height;
widget_class->size_allocate = gtk_spinbutton_discret_size_allocate;
widget_class->draw = gtk_spinbutton_discret_draw;
/* Install property */
pspec = g_param_spec_boolean("label", "Label",
"display label at right spin", FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property(g_class, P_SHOW_LABEL, pspec);
g_print ("\n gtk_spinbutton_discret_class_init ligne SUCCESS %d\n", __LINE__);
/* Add private data */
g_type_class_add_private(g_class, sizeof(GtkSpinButtonDiscretPrivate));
}
static void gtk_spinbutton_discret_init(GtkSpinButtonDiscret *spin)
{
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_init\n");
#endif
GtkSpinButtonDiscretPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(spin, SPINBUTTON_TYPE_DISCRET, GtkSpinButtonDiscretPrivate);
//GtkSpinButtonDiscretPrivate *priv = gtk_form_get_instance_private (spin);
gtk_widget_set_has_window(GTK_WIDGET(spin), TRUE);
/* Set default values */
priv->discret_list = NULL; // la liste des élément discrets
priv->show_label = TRUE;//drapeau pour afficher ou pas le chiffre correspondant au rang affiché dans la liste
priv->widget_label = NULL; // le label qui affiche ce chiffre
priv->window = NULL;
priv->adjustement =NULL;
/* Create cache for faster access */
spin->priv = priv;
}
static gint spin_button_input (GtkSpinButton *spinbutton, gdouble * new_val, gpointer data)
{
/** peu documenté un tel callback permet par exemple d'actualiser
* la valeur numérique interne du SpinButton (pointé avec new_val) à partir de l'entry interne du spinbutton
*
* l'entrée est gtk_entry_get_text (GTK_ENTRY (spinbutton)
* la sortie est new_val
*
* en l'état je ne sais pas traiter l'erreur en return GTK_INPUT_ERROR
**/
gint index = 0;
gchar *candidate_char, *wanted_char;
gboolean found = FALSE;
GList * list = (GList*) data; // si je remplace gpointer data par ???? je simplifie le code
index = g_list_length(list);
while (list)
{
candidate_char = g_ascii_strup (list->data,-1);
/** comme le spinbutton a un gtk_entry qui a été chargé avec une valeur de texte
* on peut le comparer a une valeur dans un tableau. A partir du rang trouvé **/
wanted_char = g_ascii_strup (gtk_entry_get_text (GTK_ENTRY (spinbutton)), -1);
if (strstr (candidate_char, wanted_char) == candidate_char)
{
g_print(" on cherchait %s et on l'a trouvé au rang %d\n",wanted_char,index);
found = TRUE;
}
g_free (wanted_char);
g_free (candidate_char);
if (found)
break;
index--;
list=g_list_next(list);
}
if (!found)
{
*new_val = 0.0;
return GTK_INPUT_ERROR;
}
// actualisation ici
*new_val = (gdouble) index;
g_print( " la valeur de new_val est %d\n",index);
return TRUE;
}
static gint spin_button_output (GtkSpinButton *spin_button, gpointer * data)
{
/** ce callback actualise une valeur de texte du gtkentry
* d'un spinbutton
*
* la donnée d'entrée numérique est celle de l' adjustement du spinbutton
* la sortie est l'actualisation du Gtkentry avec une valeur du tableau
* **/
GtkAdjustment *adjustment;
gdouble value;
GList * list = (GList*) data;
gint index = g_list_length(list);
adjustment = gtk_spin_button_get_adjustment (spin_button);
value = gtk_adjustment_get_value (adjustment);
g_print( " la valeur de value du spinbutton est %d\n", (gint) value);
while (list)
{
if (index == (gint )value)
if (strcmp (list->data, gtk_entry_get_text (GTK_ENTRY (spin_button))))
gtk_entry_set_text (GTK_ENTRY (spin_button), list->data);
list=g_list_next(list);
index--;
}
return TRUE;
}
static gboolean value_to_label (G_GNUC_UNUSED GBinding *binding,
const GValue *from,
GValue *to,
G_GNUC_UNUSED gpointer user_data)
{
/** synchronise le label à partir de l'adjustement **/
g_value_take_string (to, g_strdup_printf ("%g", g_value_get_double (from)));
return TRUE;
}
/* Overriden virtual methods */
static void gtk_spinbutton_discret_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_set_property\n");
#endif
GtkSpinButtonDiscret *spin = GTK_SPINBUTTONDISCRET(object);
switch(prop_id)
{
case P_SHOW_LABEL:
gtk_spinbutton_discret_show_label(spin, g_value_get_boolean(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void gtk_spinbutton_discret_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_get_property\n");
#endif
GtkSpinButtonDiscret *spin = GTK_SPINBUTTONDISCRET(object);
switch(prop_id)
{
case P_SHOW_LABEL:
g_value_set_boolean(value, spin->priv->show_label);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void gtk_spinbutton_discret_size_request(GtkWidget *widget, GtkRequisition *requisition)
{
/** nous stockons la taille préférée de notre widget, cette fonction ne sert qu'a cela
* mais requisition sera par la suite consulté **/
g_return_if_fail (GTK_SPINBUTTONDISCRET (widget));
g_return_if_fail (requisition != NULL);
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_size_request\n");
#endif
requisition->width = 100; // provisoire WIDTH, );
requisition->height = 100; // provisoire , HEIGHT);
}
static void gtk_spinbutton_discret_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
/** Cette fonction sera appelée par le conteneur dans lequel se trouvera notre widget+ le label,
* une fois que les contraintes qui sont imposées au conteneur sont fixées,
* ça permet au conteneur de redimensionner au mieux notre widget GtkSpinButtonDiscret,
* d'après le résultat obtenu dans le second argument de la fonction avec
* gtk_widget_set_allocation(widget, allocation);
* puis nous lançons la demande de déplacement, et re-dimensionnement,
* avec l'appel à gdk_window_move_resize.
* C'est un protocole qu'on retrouve dans les divers exemples **/
g_return_if_fail (GTK_SPINBUTTONDISCRET (widget));
g_return_if_fail (allocation != NULL);
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_size_allocate\n");
#endif
GtkSpinButtonDiscretPrivate *priv = GTK_SPINBUTTONDISCRET(widget)->priv;
gtk_widget_set_allocation(widget, allocation);
if (gtk_widget_get_realized(widget))
{
//g_print ("dans gtk_spinbutton_discret_size_allocate les tailles private sont %d et %d \n",WIDTH,HEIGHT);
g_print ("dans gtk_spinbutton_discret_size_allocate les tailles allocation sont %d et %d \n",allocation->width,allocation->height);
gdk_window_move_resize( priv->window,
allocation->x,
allocation->y,
100,100); // provisoire WIDTH, HEIGHT);
}
}
static void gtk_spinbutton_discret_get_preferred_width (GtkWidget *widget, gint *minimal_width, gint *natural_width)
{
/** la fonction établit une largeur compatible avec le spinbuuton plus le label image **/
g_return_if_fail (GTK_SPINBUTTONDISCRET (widget));
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_get_preferred_width\n");
#endif
GtkRequisition requisition;
gtk_spinbutton_discret_size_request (widget, &requisition);
*minimal_width = *natural_width = requisition.width;
}
static void gtk_spinbutton_discret_get_preferred_height (GtkWidget *widget, gint *minimal_height, gint *natural_height)
{
/** la fonction établit une hauteur compatible avec le spinbuuton plus le label image **/
g_return_if_fail (GTK_SPINBUTTONDISCRET (widget));
#ifdef CB_TEST
g_print ("gtk_spinbuttondiscret_get_preferred_height\n");
#endif
GtkRequisition requisition;
gtk_spinbutton_discret_size_request (widget, &requisition);
*minimal_height = *natural_height = requisition.height;
}
void gtk_spinbutton_discret_show_label(GtkSpinButtonDiscret *spin, gboolean show_label)
{
g_return_if_fail (GTK_SPINBUTTONDISCRET(spin));
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_show_label\n");
#endif
spin->priv->show_label = show_label;
/** finalement la commande la plus simple est de jouer
* avec la visibilité du widget label **/
gtk_widget_set_visible(GTK_WIDGET(spin->priv->widget_label),show_label);
}
gboolean gtk_spinbutton_discret_get_label(GtkSpinButtonDiscret *spin)
{
g_return_val_if_fail(GTK_SPINBUTTONDISCRET(spin), 0);
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_get_label\n");
#endif
return(spin->priv->show_label);
}
void gtk_spinbutton_discret_load_discret_list (GtkSpinButtonDiscret *spin,GList *discret_list)
{
g_return_if_fail (GTK_SPINBUTTONDISCRET(spin));
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_load_discret_list\n");
#endif
spin->priv->discret_list = discret_list;
}
static void gtk_spinbutton_discret_realize(GtkWidget *widget)
{
/** cette fonction est au coeur du dispositif c'est ici que la fenêtre est créé
* avec les bonnes dimensions
* notamment la couche de dessin est préparé avec le spinbutton plus son label
* le widget label sera visible ou pas et créé dans tous les cas
**/
g_return_if_fail (GTK_SPINBUTTONDISCRET (widget));
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_realize\n");
#endif
GtkSpinButtonDiscretPrivate *priv = GTK_SPINBUTTONDISCRET(widget)->priv;
GtkAllocation allocation;
GdkWindowAttr attributes;
guint attrs_mask;
gtk_widget_set_realized(widget, TRUE);
gtk_widget_get_allocation(widget, &allocation);
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.wclass = GDK_INPUT_OUTPUT;
//attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
attributes.event_mask = gtk_widget_get_events (widget);
attributes.event_mask |= GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
| GDK_POINTER_MOTION_MASK;
attrs_mask = GDK_WA_X | GDK_WA_Y;
priv->window = gdk_window_new(gtk_widget_get_parent_window(widget),
&attributes, attrs_mask);
gdk_window_set_user_data(priv->window, widget);
gtk_widget_set_window(widget, priv->window);
/**widget->style = gtk_style_attach(gtk_widget_get_style( widget ),
priv->window);
//gtk_style_set_background(widget->style, priv->window, GTK_STATE_NORMAL);**/
/* Creation du conteneur principal */
GtkWidget *main_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
priv->widget_label = gtk_label_new(NULL);
gtk_widget_set_no_show_all(priv->widget_label,TRUE);
GtkWidget * legende_label = gtk_label_new("Numéro du mois");
gtk_widget_set_no_show_all(legende_label,TRUE);
int expand = TRUE ;
int fill = TRUE;
int padding =1;
GtkWidget * spinbutton = gtk_spin_button_new(priv->adjustement,1,1);
//pour le premier spin month
gtk_box_pack_start (GTK_BOX (main_box), spinbutton, !expand, fill, padding);
GtkWidget * Separator = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
gtk_box_pack_start (GTK_BOX (main_box), Separator, FALSE, FALSE, 0);
Separator = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
gtk_box_pack_start (GTK_BOX (main_box), legende_label, !expand, fill, padding);
gtk_box_pack_start (GTK_BOX (main_box), priv->widget_label, !expand, fill, padding);
priv->box = main_box;
/** The output signal can be used to change to formatting of the value that
* is displayed in the spin buttons entry. **/
g_signal_connect (spinbutton, "output", G_CALLBACK (spin_button_output), priv->discret_list);
/** par rapport à la demo gtk3-demo module spinbutton j'ai rajouté un paramètre pour le callback
* g_signal_connect (spinbutton, "input", G_CALLBACK (month_spin_input));
**/
g_signal_connect (spinbutton, "input", G_CALLBACK (spin_button_input), priv->discret_list);
/** la méthode suivante vue dans gtk3-demo module spinbutton
* la synchro est automatique du spinbutton vers le label et c'est
* la fonction value_to_label qui réalise l'action à faire
* Gobject va collecter la valeur du champ value du adjustement_button_month pour
* la ""coller"" dans le champ "label" celle du label month_label**/
g_object_bind_property_full (priv->adjustement, "value",
priv->widget_label, "label",
G_BINDING_SYNC_CREATE,
value_to_label,
NULL,
NULL, NULL);
g_print ("\n gtk_spinbutton_discret_realize ligne SUCCESS %d\n", __LINE__);
}
static gboolean gtk_spinbutton_discret_draw (GtkWidget * widget, cairo_t *cairo)
{
g_return_val_if_fail (GTK_SPINBUTTONDISCRET (widget),FALSE);
#ifdef CB_TEST
g_print ("gtk_spinbutton_discret_draw\n");
#endif
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
//GtkSpinButtonDiscretPrivate *priv = GTK_SPINBUTTONDISCRET(widget)->priv;
gint center_x = (allocation.width / 2) ;
gint center_y = (allocation.height / 2);
/** comment on s'y prend pour dessiner les différents widgets **/
cairo_arc(cairo, center_x, center_y, 50, 0, 2 * 3.14);
cairo_stroke(cairo);
return TRUE;
} |
Partager