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
| #include <stdlib.h> /* définition de EXIT_SUCCESS */
#include <string.h>
#include <gtk/gtk.h>
/* Prototypes des fonctions de callback */
static gboolean cb_enter_notify_eventbox (GtkEventBox *, GdkEvent *, GtkWindow *);
static gboolean cb_leave_notify_eventbox (GtkEventBox *, GdkEvent *, GtkWindow *);
static gboolean cb_enter_notify_popup (GtkWindow *, GdkEvent *, gpointer);
static gboolean cb_leave_notify_popup (GtkWindow *, GdkEvent *, gpointer);
static void cb_value_scale_changed (GtkRange *, gpointer);
static gboolean end_timeout (GtkWindow *);
/* Déclaration des objets globaux */
gboolean eventbox_has_pointer = FALSE;
gboolean popup_has_pointer = FALSE;
GtkLabel *current_value;
int main (int argc, char *argv[])
{
/* Initialisation de GTK */
gtk_init (&argc, &argv);
/* Déclaration des objets */
GtkWindow *window, *popup;
GtkGrid *grid;
GtkEventBox *eventbox1, *eventbox2;
GtkFrame *frame1, *frame2;
GtkLabel *label1, *label2, *value1, *value2;
GtkAdjustment *adj;
GtkScale *scale;
/* Initialisation des objets */
window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
grid = GTK_GRID (gtk_grid_new ());
label1 = GTK_LABEL (gtk_label_new ("Valeur1 : "));
eventbox1 = GTK_EVENT_BOX (gtk_event_box_new ());
frame1 = GTK_FRAME (gtk_frame_new (NULL));
value1 = GTK_LABEL (gtk_label_new ("100%"));
label2 = GTK_LABEL (gtk_label_new (" Valeur2 : "));
eventbox2 = GTK_EVENT_BOX (gtk_event_box_new ());
frame2 = GTK_FRAME (gtk_frame_new (NULL));
value2 = GTK_LABEL (gtk_label_new ("50%"));
popup = GTK_WINDOW (gtk_window_new (GTK_WINDOW_POPUP));
adj = GTK_ADJUSTMENT (gtk_adjustment_new (0, 0, 100, 1, 0, 0));
scale = GTK_SCALE (gtk_scale_new (GTK_ORIENTATION_VERTICAL, adj));
/* Paramétrage des objets */
gtk_window_set_title (window, "Mon exemple 2");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (GTK_WIDGET (window), 600, 600);
gtk_grid_set_row_spacing (grid, 5);
gtk_grid_set_column_homogeneous (grid, FALSE);
gtk_grid_set_row_homogeneous (grid, FALSE);
gtk_widget_set_halign (GTK_WIDGET (grid), GTK_ALIGN_START);
gtk_widget_set_valign (GTK_WIDGET (grid), GTK_ALIGN_START);
gtk_label_set_width_chars (value1, 5);
gtk_label_set_justify (value1, GTK_JUSTIFY_RIGHT);
gtk_label_set_width_chars (value2, 5);
gtk_label_set_justify (value2, GTK_JUSTIFY_RIGHT);
gtk_window_set_decorated (popup, FALSE);
gtk_widget_set_size_request (GTK_WIDGET (popup), 1, 140);
gtk_window_set_resizable (popup, FALSE);
gtk_scale_set_draw_value (scale, FALSE);
gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
/* Attachement des objets les uns dans les autres */
gtk_container_add (GTK_CONTAINER (frame1), GTK_WIDGET (value1));
gtk_container_add (GTK_CONTAINER (eventbox1), GTK_WIDGET (frame1));
gtk_container_add (GTK_CONTAINER (frame2), GTK_WIDGET (value2));
gtk_container_add (GTK_CONTAINER (eventbox2), GTK_WIDGET (frame2));
gtk_grid_attach (grid, GTK_WIDGET (label1), 0 , 0, 1, 1);
gtk_grid_attach_next_to (grid, GTK_WIDGET (eventbox1), GTK_WIDGET (label1), GTK_POS_RIGHT, 1, 1);
gtk_grid_attach_next_to (grid, GTK_WIDGET (label2), GTK_WIDGET (eventbox1), GTK_POS_RIGHT, 1, 1);
gtk_grid_attach_next_to (grid, GTK_WIDGET (eventbox2), GTK_WIDGET (label2), GTK_POS_RIGHT, 1, 1);
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
gtk_container_add (GTK_CONTAINER (popup), GTK_WIDGET (scale));
/* Déclaration des fonctions de callback */
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (eventbox1), "enter-notify-event", G_CALLBACK (cb_enter_notify_eventbox), (gpointer) popup);
g_signal_connect (G_OBJECT (eventbox1), "leave-notify-event", G_CALLBACK (cb_leave_notify_eventbox), (gpointer) popup);
g_signal_connect (G_OBJECT (eventbox2), "enter-notify-event", G_CALLBACK (cb_enter_notify_eventbox), (gpointer) popup);
g_signal_connect (G_OBJECT (eventbox2), "leave-notify-event", G_CALLBACK (cb_leave_notify_eventbox), (gpointer) popup);
g_signal_connect (G_OBJECT (popup), "enter-notify-event", G_CALLBACK (cb_enter_notify_popup), NULL);
g_signal_connect (G_OBJECT (popup), "leave-notify-event", G_CALLBACK (cb_leave_notify_popup), NULL);
g_signal_connect (G_OBJECT (scale), "value_changed", G_CALLBACK (cb_value_scale_changed), NULL);
/* Affiche les widgets */
gtk_widget_show_all (GTK_WIDGET (window));
gtk_widget_show (GTK_WIDGET (scale));
/* Et c'est parti ! */
gtk_main ();
return EXIT_SUCCESS;
}
static gboolean cb_enter_notify_eventbox (GtkEventBox *eventbox, GdkEvent *event, GtkWindow *popup)
{
/* Déclaration des variables locales à la fonction */
int int_value;
GtkWindow *window;
GtkFrame *frame;
GtkLabel *value;
GtkScale *scale;
GtkAllocation allocation;
gint window_x, window_y, widget_x, widget_y;
/* Note que le pointeur est au dessus de l'enventbox */
eventbox_has_pointer = TRUE;
/* Retrouve les objets */
frame = GTK_FRAME (gtk_bin_get_child (GTK_BIN (eventbox)));
value = GTK_LABEL (gtk_bin_get_child (GTK_BIN (frame)));
scale = GTK_SCALE (gtk_bin_get_child (GTK_BIN (popup)));
/* Note le label concerné */
current_value = value;
/* Affecte la valeur courante du label au curseur */
sscanf (gtk_label_get_text (value), "%d", &int_value);
gtk_range_set_value (GTK_RANGE (scale), (gdouble) int_value);
/* Calcule la position du coin en haut et à droite du frame */
window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (eventbox)));
gdk_window_get_position (gtk_widget_get_window (GTK_WIDGET (window)), &window_x, &window_y);
gtk_widget_get_allocation (GTK_WIDGET (frame), &allocation);
gtk_widget_translate_coordinates (GTK_WIDGET (frame), GTK_WIDGET (window), window_x + allocation.width, window_y, &widget_x, &widget_y);
/* Déplace la fenêtre popup juste à droite du frame */
gtk_window_move (popup, widget_x, widget_y);
/* affiche le popup */
gtk_widget_show (GTK_WIDGET (popup));
(void) event;
return TRUE;
}
static gboolean cb_leave_notify_eventbox (GtkEventBox *eventbox, GdkEvent *event, GtkWindow *popup)
{
/* Note que le pointeur a quitté l'enventbox */
eventbox_has_pointer = FALSE;
/* Lance le timeout (1 dixième de seconde) */
g_timeout_add (100, (GSourceFunc) end_timeout, (gpointer) popup);
(void) eventbox;
(void) event;
return TRUE;
}
static gboolean cb_enter_notify_popup (GtkWindow *popup, GdkEvent *event, gpointer user_data)
{
/* Note que le pointeur est au dessus du popup */
popup_has_pointer = TRUE;
(void) popup;
(void) event;
(void) user_data;
return TRUE;
}
static gboolean cb_leave_notify_popup (GtkWindow *popup, GdkEvent *event, gpointer user_data)
{
/* Note que le pointeur a quitté la fenêtre popup */
popup_has_pointer = FALSE;
/* Lance le timeout (1 dixième de seconde) */
g_timeout_add (100, (GSourceFunc) end_timeout, (gpointer) popup);
(void) user_data;
(void) event;
return TRUE;
}
static void cb_value_scale_changed (GtkRange *scale, gpointer user_data)
{
/* Déclare une chaîne de caractères pour le label (avec de la marge) */
gchar value_text[10];
/* récupère la valeur du curseur et la recopie dans le label */
sprintf (value_text, "%3.0f%%", gtk_range_get_value (scale));
gtk_label_set_text (current_value, value_text);
(void) user_data;
}
static gboolean end_timeout (GtkWindow *popup)
{
/* Si le pointeur de la souris n'est pas au dessus de l'un des widgets */
if ( ! (eventbox_has_pointer || popup_has_pointer))
{
/* Masque la fenêtre popup */
gtk_widget_hide (GTK_WIDGET (popup));
}
return FALSE;
} |
Partager