| 12
 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
 
 |  
/* -*- Mode: C; indent-tabs-mode: nil; -*-
 *
 * Pour GTK+-2 : exemple d'utilisation de gdk_draw_line() 
 * pour tracer des lignes dans une GtkTable
 */
 
#include <gtk/gtk.h>
#include <gdk/gdk.h>
 
#define NB_ROWS 4
#define NB_COLS 5
 
/* Le niveau de gris des lignes entre 0(noir) et 65535 (blanc) */
#define FG 35000
static GdkColor fg = {0 ,FG ,FG ,FG};
 
/* Pour choisir le ou les cotés à tracer */
enum { HAUT = 1 ,BAS = 2 ,GAUCHE = 4 ,DROIT  = 8 } Cote;
 
/* Le réflexe  (callback) qui sert pour tracer les lignes 
 */
gboolean  event_CB (GtkWidget *w_label ,GdkEvent  *event ,gpointer user_data)
{
  gint x ,y ,width ,height ,depth;
  GdkWindow *win = w_label->window;
  GdkGC     *gc  = gdk_gc_new (win);
  int      cotes = GPOINTER_TO_INT (user_data);
 
  gdk_window_get_geometry    (win ,&x ,&y ,&width ,&height ,&depth);
  gdk_gc_set_line_attributes (gc ,/* epaisseur= */ 3 
                              ,GDK_LINE_SOLID ,GDK_CAP_BUTT ,GDK_JOIN_MITER);
  gdk_gc_set_rgb_fg_color    (gc ,&fg);
  gdk_gc_set_function        (gc ,GDK_COPY);
 
  if (cotes & BAS )    gdk_draw_line (win ,gc ,0     ,height ,width ,height);
  if (cotes & HAUT)    gdk_draw_line (win ,gc ,0     ,0      ,width ,0);
  if (cotes & GAUCHE)  gdk_draw_line (win ,gc ,0     ,0      ,0     ,height);
  if (cotes & DROIT)   gdk_draw_line (win ,gc ,width ,0      ,width ,height);
 
  g_object_unref (gc);
  return FALSE;
}
 
 
int main (int argc ,char *argv[])
{
  GtkWidget *w_main ,*w_table ,*w_event ,*w_label;
  gchar     *numero;
  int i ,j;
 
  int cotes = GAUCHE  | DROIT;  /* on ne trace que les verticales */
 
  gtk_init (&argc ,&argv);
  w_main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
  w_table = gtk_table_new (NB_ROWS ,NB_COLS ,TRUE);
  gtk_container_add (GTK_CONTAINER (w_main) ,w_table);
 
  for (i = 0; i < NB_ROWS; i++) {
    for (j = 0; j < NB_COLS; j++) {
      numero  = g_strdup_printf (" (%d , %d) " ,i ,j);
      w_label = gtk_label_new (numero);
      g_free (numero);
      /* comme un GtkLabel est un GTK_NO_WINDOW widget, 
       * il faut l'insérer dans une GtkEventBox */
      w_event = gtk_event_box_new ();   
      gtk_container_add (GTK_CONTAINER (w_event) ,w_label );
      gtk_table_attach_defaults (GTK_TABLE(w_table) ,w_event  ,j ,j+1 ,i ,i+1); 
      g_signal_connect (w_label ,"event" ,G_CALLBACK (event_CB) ,GINT_TO_POINTER (cotes));
    }
  }
  g_signal_connect (G_OBJECT (w_main) ,"destroy"      ,(GCallback)gtk_main_quit ,NULL);
  g_signal_connect (G_OBJECT (w_main) ,"delete-event" ,(GCallback)gtk_main_quit ,NULL);
  gtk_widget_show_all (w_main);
  gtk_main ();
  return 0;
} | 
Partager