colorsel.c 3.02 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
  gdk_cairo_set_source_rgba (cr, &color);
24
  cairo_paint (cr);
25 26 27 28

  return TRUE;
}

29 30 31 32 33 34
static void
response_cb (GtkDialog *dialog,
             gint       response_id,
             gpointer   user_data)
{
  if (response_id == GTK_RESPONSE_OK)
35
    gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
36

37
  gtk_widget_destroy (GTK_WIDGET (dialog));
38 39
}

40 41
static void
change_color_callback (GtkWidget *button,
42
                       gpointer   data)
43 44
{
  GtkWidget *dialog;
45

46
  dialog = gtk_color_chooser_dialog_new ("Changing color", GTK_WINDOW (window));
47
  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
48
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (dialog), &color);
49

50 51
  g_signal_connect (dialog, "response",
                    G_CALLBACK (response_cb), NULL);
52

53
  gtk_widget_show_all (dialog);
54 55 56
}

GtkWidget *
57
do_colorsel (GtkWidget *do_widget)
58 59 60
{
  GtkWidget *vbox;
  GtkWidget *button;
61

62 63 64
  if (!window)
    {
      color.red = 0;
65
      color.blue = 1;
66
      color.green = 0;
67 68
      color.alpha = 1;

69
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
70
      gtk_window_set_screen (GTK_WINDOW (window),
71
                             gtk_widget_get_screen (do_widget));
72
      gtk_window_set_title (GTK_WINDOW (window), "Color Chooser");
73

74
      g_signal_connect (window, "destroy",
75
                        G_CALLBACK (gtk_widget_destroyed), &window);
76 77 78

      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

79
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
80 81 82 83 84 85
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (window), vbox);

      /*
       * Create the color swatch area
       */
86

87 88 89
      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);
90

91
      da = gtk_drawing_area_new ();
92

93
      g_signal_connect (da, "draw", G_CALLBACK (draw_callback), NULL);
94

95
      /* set a minimum size */
Havoc Pennington's avatar
Havoc Pennington committed
96
      gtk_widget_set_size_request (da, 200, 200);
97

98 99 100
      gtk_container_add (GTK_CONTAINER (frame), da);

      button = gtk_button_new_with_mnemonic ("_Change the above color");
101 102
      gtk_widget_set_halign (button, GTK_ALIGN_END);
      gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
103

104
      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
105

106
      g_signal_connect (button, "clicked",
107
                        G_CALLBACK (change_color_callback), NULL);
108 109
    }

110
  if (!gtk_widget_get_visible (window))
111
    gtk_widget_show_all (window);
112
  else
113
    gtk_widget_destroy (window);
114 115 116

  return window;
}