colorsel.c 3.39 KB
Newer Older
1
/* Color Chooser
2
 *
3 4 5 6
 * A GtkColorChooser lets the user choose a color. There are several
 * implementations of the GtkColorChooser interface in GTK+. The
 * GtkColorChooserDialog is a prebuilt dialog containing a
 * GtkColorChooserWidget.
7 8 9 10 11 12
 */

#include <gtk/gtk.h>

static GtkWidget *window = NULL;
static GtkWidget *da;
13
static GdkRGBA color;
14 15
static GtkWidget *frame;

16
/* draw callback for the drawing area
17 18
 */
static gboolean
19
draw_callback (GtkWidget *widget,
20 21
               cairo_t   *cr,
               gpointer   data)
22
{
23
  GtkStyleContext *context;
24
  GdkRGBA rgba;
25

26
  context = gtk_widget_get_style_context (widget);
27 28
  gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &rgba);
  gdk_cairo_set_source_rgba (cr, &rgba);
29
  cairo_paint (cr);
30 31 32 33

  return TRUE;
}

34 35 36 37 38 39 40 41 42 43
static void
response_cb (GtkDialog *dialog,
             gint       response_id,
             gpointer   user_data)
{
  if (response_id == GTK_RESPONSE_OK)
    {
      gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
      gtk_widget_override_background_color (da, 0, &color);
    }
44 45

  gtk_widget_hide (GTK_WIDGET (dialog));
46 47
}

48 49
static void
change_color_callback (GtkWidget *button,
50
                       gpointer   data)
51 52
{
  GtkWidget *dialog;
53

54 55
  dialog = gtk_color_chooser_dialog_new ("Changing color", GTK_WINDOW (window));
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (dialog), &color);
56

57 58 59 60
  g_signal_connect (dialog,
                    "response",
                    G_CALLBACK (response_cb),
                    NULL);
61

62
  gtk_widget_show_all (dialog);
63 64 65
}

GtkWidget *
66
do_colorsel (GtkWidget *do_widget)
67 68 69
{
  GtkWidget *vbox;
  GtkWidget *button;
70

71 72 73
  if (!window)
    {
      color.red = 0;
74
      color.blue = 1;
75
      color.green = 0;
76 77
      color.alpha = 1;

78
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
79
      gtk_window_set_screen (GTK_WINDOW (window),
80
                             gtk_widget_get_screen (do_widget));
81
      gtk_window_set_title (GTK_WINDOW (window), "Color Chooser");
82

83
      g_signal_connect (window, "destroy",
84
                        G_CALLBACK (gtk_widget_destroyed), &window);
85 86 87

      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

88
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
89 90 91 92 93 94
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (window), vbox);

      /*
       * Create the color swatch area
       */
95 96


97 98 99
      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
      gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
100

101
      da = gtk_drawing_area_new ();
102

103
      g_signal_connect (da, "draw", G_CALLBACK (draw_callback), NULL);
104

105
      /* set a minimum size */
Havoc Pennington's avatar
Havoc Pennington committed
106
      gtk_widget_set_size_request (da, 200, 200);
107
      /* set the color */
108 109
      gtk_widget_override_background_color (da, 0, &color);

110 111 112
      gtk_container_add (GTK_CONTAINER (frame), da);

      button = gtk_button_new_with_mnemonic ("_Change the above color");
113 114
      gtk_widget_set_halign (button, GTK_ALIGN_END);
      gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
115

116
      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
117

118
      g_signal_connect (button, "clicked",
119
                        G_CALLBACK (change_color_callback), NULL);
120 121
    }

122
  if (!gtk_widget_get_visible (window))
123 124 125 126 127 128 129 130 131 132 133
    {
      gtk_widget_show_all (window);
    }
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}