colorsel.c 3.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11

#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>

GtkWidget *colorseldlg = NULL;
GtkWidget *drawingarea = NULL;
GdkColor color;

/* Color changed handler */

Matthias Clasen's avatar
Matthias Clasen committed
12
13
static void color_changed_cb( GtkWidget         *widget,
                              GtkColorSelection *colorsel )
14
15
16
17
18
19
20
21
22
{
  GdkColor ncolor;

  gtk_color_selection_get_current_color (colorsel, &ncolor);
  gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &ncolor);       
}

/* Drawingarea event handler */

Matthias Clasen's avatar
Matthias Clasen committed
23
24
25
static gboolean area_event( GtkWidget *widget,
                            GdkEvent  *event,
                            gpointer   client_data )
26
27
28
29
{
  gint handled = FALSE;
  gint response;
  GtkColorSelection *colorsel;
30
  GtkColorSelectionDialog *selection_dialog;
31
32
33
34
35
36
37
38
39
40
41
42

  /* Check if we've received a button pressed event */

  if (event->type == GDK_BUTTON_PRESS)
    {
      handled = TRUE;

       /* Create color selection dialog */
      if (colorseldlg == NULL)
        colorseldlg = gtk_color_selection_dialog_new ("Select background color");

      /* Get the ColorSelection widget */
43
44
      selection_dialog = GTK_COLOR_SELECTION_DIALOG (colorseldlg);
      colorsel = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_colorsel (selection_dialog));
45
46
47
48
49

      gtk_color_selection_set_previous_color (colorsel, &color);
      gtk_color_selection_set_current_color (colorsel, &color);
      gtk_color_selection_set_has_palette (colorsel, TRUE);

50
      /* Connect to the "color-changed" signal, set the client-data
51
       * to the colorsel widget */
52
53
54
      g_signal_connect (colorsel, "color-changed",
                        G_CALLBACK (color_changed_cb),
                        colorsel);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

      /* Show the dialog */
      response = gtk_dialog_run (GTK_DIALOG (colorseldlg));

      if (response == GTK_RESPONSE_OK)
        gtk_color_selection_get_current_color (colorsel, &color);
      else 
        gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &color);

      gtk_widget_hide (colorseldlg);
    }

  return handled;
}

/* Close down and exit handler */

Matthias Clasen's avatar
Matthias Clasen committed
72
73
74
static gboolean destroy_window( GtkWidget *widget,
                                GdkEvent  *event,
                                gpointer   client_data )
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
  gtk_main_quit ();
  return TRUE;
}

/* Main */

gint main( gint   argc,
           gchar *argv[] )
{
  GtkWidget *window;

  /* Initialize the toolkit, remove gtk-related commandline stuff */

  gtk_init (&argc, &argv);

  /* Create toplevel window, set title and policies */

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Color selection test");
95
  gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
96
97
98

  /* Attach to the "delete" and "destroy" events so we can exit */

99
  g_signal_connect (window, "delete_event",
100
                    G_CALLBACK (destroy_window), (gpointer) window);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  
  /* Create drawingarea, set size and catch button events */

  drawingarea = gtk_drawing_area_new ();

  color.red = 0;
  color.blue = 65535;
  color.green = 0;
  gtk_widget_modify_bg (drawingarea, GTK_STATE_NORMAL, &color);       

  gtk_widget_set_size_request (GTK_WIDGET (drawingarea), 200, 200);

  gtk_widget_set_events (drawingarea, GDK_BUTTON_PRESS_MASK);

115
  g_signal_connect (drawingarea, "event",
116
	            G_CALLBACK (area_event), (gpointer) drawingarea);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  
  /* Add drawingarea to window, then show them both */

  gtk_container_add (GTK_CONTAINER (window), drawingarea);

  gtk_widget_show (drawingarea);
  gtk_widget_show (window);
  
  /* Enter the gtk main loop (this never returns) */

  gtk_main ();

  /* Satisfy grumpy compilers */

  return 0;
}