~ubuntu-branches/debian/sid/bijiben/sid

« back to all changes in this revision

Viewing changes to src/bjb-color-button.c

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2013-03-26 21:19:36 UTC
  • Revision ID: package-import@ubuntu.com-20130326211936-tu8mpy82juohw8m2
Tags: upstream-3.8.0
ImportĀ upstreamĀ versionĀ 3.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* bjb-color-button.c
 
2
 * Copyright (C) Pierre-Yves Luyten 2012 <py@luyten.fr>
 
3
 * 
 
4
 * bijiben is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation, either version 3 of the License, or
 
7
 * (at your option) any later version.
 
8
 * 
 
9
 * anjuta is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 * See the GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
 
 
19
/* This is mostly gtk-color-button.c
 
20
 * Just overrides the dialog to choose specific palette
 
21
 * But a dedicated dialog might be better */
 
22
 
 
23
#include <glib/gi18n.h>
 
24
 
 
25
#include "bjb-color-button.h"
 
26
 
 
27
// Bijiben probably wants something like 6 light colors
 
28
#define BJB_NUM_COLORS 4
 
29
 
 
30
static gchar *palette_str[BJB_NUM_COLORS] = {
 
31
  "rgb(239, 242, 209)", // eff2d1 from the mockup
 
32
  "rgb(210, 219, 230)", // d2dbe6 from the mockup
 
33
  "rgb(229, 230, 210)", //
 
34
  "rgb(235, 239, 244)", //
 
35
};
 
36
 
 
37
struct _BjbColorButtonPrivate
 
38
{
 
39
  GdkRGBA palette [BJB_NUM_COLORS];
 
40
  GtkWidget *dialog;
 
41
  gchar *title;
 
42
  GdkRGBA rgba;
 
43
};
 
44
 
 
45
#define BJB_COLOR_BUTTON_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BJB_TYPE_COLOR_BUTTON, BjbColorButtonPrivate))
 
46
 
 
47
 
 
48
G_DEFINE_TYPE (BjbColorButton, bjb_color_button, GTK_TYPE_COLOR_BUTTON);
 
49
 
 
50
static gboolean
 
51
dialog_destroy (GtkWidget *widget,
 
52
                gpointer   data)
 
53
{
 
54
  BjbColorButton *button = BJB_COLOR_BUTTON (data);
 
55
 
 
56
  button->priv->dialog = NULL;
 
57
 
 
58
  return FALSE;
 
59
}
 
60
 
 
61
static void
 
62
dialog_response (GtkDialog *dialog,
 
63
                 gint       response,
 
64
                 gpointer   data)
 
65
{
 
66
  BjbColorButton *button = BJB_COLOR_BUTTON (data);
 
67
  BjbColorButtonPrivate *priv = button->priv;
 
68
 
 
69
  if (response == GTK_RESPONSE_CANCEL)
 
70
    gtk_widget_hide (GTK_WIDGET (dialog));
 
71
 
 
72
  else if (response == GTK_RESPONSE_OK)
 
73
  {
 
74
      gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog),
 
75
                                  &priv->rgba);
 
76
 
 
77
      gtk_widget_hide (GTK_WIDGET (dialog));
 
78
      gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &priv->rgba);
 
79
      g_signal_emit_by_name (button, "color-set");
 
80
  }
 
81
}
 
82
 
 
83
static void
 
84
bjb_color_button_clicked (GtkButton *b)
 
85
{
 
86
  BjbColorButton *button = BJB_COLOR_BUTTON (b);
 
87
  BjbColorButtonPrivate *priv = button->priv;
 
88
  GtkWidget *dialog;
 
89
  gint i;
 
90
 
 
91
  /* if dialog already exists, make sure it's shown and raised */
 
92
  if (!button->priv->dialog)
 
93
    {
 
94
      /* Create the dialog and connects its buttons */
 
95
      GtkWidget *parent;
 
96
 
 
97
      parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
 
98
 
 
99
      button->priv->dialog = dialog = gtk_color_chooser_dialog_new (button->priv->title, NULL);
 
100
 
 
101
      if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
 
102
        {
 
103
          if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
 
104
            gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
 
105
 
 
106
          gtk_window_set_modal (GTK_WINDOW (dialog),
 
107
                                gtk_window_get_modal (GTK_WINDOW (parent)));
 
108
        }
 
109
 
 
110
      for (i=0 ; i< BJB_NUM_COLORS ; i++)
 
111
      {
 
112
        GdkRGBA color;
 
113
  
 
114
        if (gdk_rgba_parse (&color, palette_str[i]))
 
115
          priv->palette [i] = color;
 
116
      }
 
117
 
 
118
      gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (dialog),
 
119
                                     GTK_ORIENTATION_HORIZONTAL,
 
120
                                     BJB_NUM_COLORS,
 
121
                                     BJB_NUM_COLORS,
 
122
                                     priv->palette);
 
123
 
 
124
 
 
125
      g_signal_connect (dialog, "response",
 
126
                        G_CALLBACK (dialog_response), button);
 
127
      g_signal_connect (dialog, "destroy",
 
128
                        G_CALLBACK (dialog_destroy), button);
 
129
    }
 
130
 
 
131
  gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &priv->rgba);
 
132
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button->priv->dialog),
 
133
                              &button->priv->rgba);
 
134
 
 
135
  gtk_window_present (GTK_WINDOW (button->priv->dialog));
 
136
}
 
137
 
 
138
 
 
139
 
 
140
static void
 
141
bjb_color_button_init (BjbColorButton *self)
 
142
{
 
143
  BjbColorButtonPrivate *priv = BJB_COLOR_BUTTON_GET_PRIVATE(self);
 
144
  self->priv = priv;
 
145
 
 
146
  priv->title = _("Choose a color for note");
 
147
}
 
148
 
 
149
static void
 
150
bjb_color_button_constructed (GObject *obj)
 
151
{
 
152
  G_OBJECT_CLASS (bjb_color_button_parent_class)->constructed (obj);
 
153
}
 
154
 
 
155
static void
 
156
bjb_color_button_finalize (GObject *object)
 
157
{
 
158
  G_OBJECT_CLASS (bjb_color_button_parent_class)->finalize (object);
 
159
}
 
160
 
 
161
static void
 
162
bjb_color_button_class_init (BjbColorButtonClass *klass)
 
163
{
 
164
  GObjectClass *object_class = G_OBJECT_CLASS (klass); 
 
165
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
 
166
    
 
167
  g_type_class_add_private (klass, sizeof (BjbColorButtonPrivate));
 
168
 
 
169
  object_class->constructed = bjb_color_button_constructed;
 
170
  object_class->finalize = bjb_color_button_finalize;
 
171
 
 
172
  /* Override std::gtk_color_button */
 
173
  button_class->clicked = bjb_color_button_clicked;
 
174
}
 
175
 
 
176
GtkWidget *
 
177
bjb_color_button_new (void)
 
178
{
 
179
  return g_object_new (BJB_TYPE_COLOR_BUTTON, NULL);
 
180
}