~canonical-dx-team/unity/unity.fix-ql-losing-focus

672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
1
/*
2
 * Copyright (C) 2010 Canonical Ltd
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 3 as
6
 * published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Authored by: Mirco Müller <mirco.mueller@canonical.com>
853.4.21 by Neil Jagdish Patel
[merge] trunk
17
 *              Neil Jagdish Patel <neil.patel@canonical.com>
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
18
 */
19
853.4.20 by Neil Jagdish Patel
Fix the home button logo to match theme
20
#include "config.h"
21
853.4.19 by Neil Jagdish Patel
Add support for non Ambiance/Radiance window buttons
22
#include <math.h>
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
23
#include <gtk/gtk.h>
24
853.4.19 by Neil Jagdish Patel
Add support for non Ambiance/Radiance window buttons
25
#include <Nux/Nux.h>
26
#include <NuxGraphics/GraphicsEngine.h>
27
#include <NuxImage/CairoGraphics.h>
28
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
29
#include "PanelStyle.h"
30
853.4.11 by Neil Jagdish Patel
Start moving over the renderering to gtk colours
31
static PanelStyle *_style = NULL;
32
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
33
PanelStyle::PanelStyle ()
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
34
: _theme_name (NULL)
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
35
{
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
36
  _offscreen = gtk_offscreen_window_new ();
37
  gtk_widget_set_name (_offscreen, "UnityPanelWidget");
38
  gtk_widget_set_size_request (_offscreen, 100, 24);
853.4.15 by Neil Jagdish Patel
Add support for swtiching themes properly
39
  gtk_widget_show_all (_offscreen);
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
40
41
  g_signal_connect (gtk_settings_get_default (), "notify::gtk-theme-name",
42
                    G_CALLBACK (PanelStyle::OnStyleChanged), this);
853.4.11 by Neil Jagdish Patel
Start moving over the renderering to gtk colours
43
44
  Refresh ();
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
45
}
46
47
PanelStyle::~PanelStyle ()
48
{
49
  gtk_widget_destroy (_offscreen);
853.4.11 by Neil Jagdish Patel
Start moving over the renderering to gtk colours
50
51
  if (_style == this)
52
    _style = NULL;
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
53
54
  g_free (_theme_name);
853.4.11 by Neil Jagdish Patel
Start moving over the renderering to gtk colours
55
}
56
57
PanelStyle *
58
PanelStyle::GetDefault ()
59
{
60
  if (G_UNLIKELY (!_style))
61
    _style = new PanelStyle ();
62
63
  return _style;
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
64
}
65
66
void
67
PanelStyle::Refresh ()
68
{
672.1.4 by Mirco Müller
applied some requested style-changes to the code
69
  GtkStyle*  style    = NULL;
70
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
71
  if (_theme_name)
72
    g_free (_theme_name);
73
74
  _theme_name = NULL;
75
  g_object_get (gtk_settings_get_default (), "gtk-theme-name", &_theme_name, NULL);
76
  
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
77
  style = gtk_widget_get_style (_offscreen);
672.1.2 by Mirco Müller
trying to hooking up the correct colors
78
853.4.11 by Neil Jagdish Patel
Start moving over the renderering to gtk colours
79
  _text.SetRed ((float) style->text[0].red / (float) 0xffff);
80
  _text.SetGreen ((float) style->text[0].green / (float) 0xffff);
81
  _text.SetBlue ((float) style->text[0].blue / (float) 0xffff);
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
82
  _text.SetAlpha (1.0f);
83
871.4.24 by Neil Jagdish Patel
Fix Panel font rendering to be clearer, fix entry to be larger and render correctly
84
  _text_shadow.SetRed ((float) style->text[3].red / (float) 0xffff);
85
  _text_shadow.SetGreen ((float) style->text[3].green / (float) 0xffff);
86
  _text_shadow.SetBlue ((float) style->text[3].blue / (float) 0xffff);
672.1.4 by Mirco Müller
applied some requested style-changes to the code
87
  _text_shadow.SetAlpha (1.0f);
672.1.1 by Mirco Müller
new style-handling class for the panel implemented as a singleton
88
871.4.24 by Neil Jagdish Patel
Fix Panel font rendering to be clearer, fix entry to be larger and render correctly
89
  _line.SetRed ((float) style->dark[0].red / (float) 0xffff);
90
  _line.SetGreen ((float) style->dark[0].green / (float) 0xffff);
91
  _line.SetBlue ((float) style->dark[0].blue / (float) 0xffff);
92
  _line.SetAlpha (1.0f);
93
853.4.13 by Neil Jagdish Patel
Draw the active indicator with gtk colours too
94
  _bg_top.SetRed ((float) style->bg[1].red / (float) 0xffff);
95
  _bg_top.SetGreen ((float) style->bg[1].green / (float) 0xffff);
96
  _bg_top.SetBlue ((float) style->bg[1].blue / (float) 0xffff);
97
  _bg_top.SetAlpha (1.0f);
98
99
  _bg_bottom.SetRed ((float) style->bg[0].red / (float) 0xffff);
100
  _bg_bottom.SetGreen ((float) style->bg[0].green / (float) 0xffff);
101
  _bg_bottom.SetBlue ((float) style->bg[0].blue / (float) 0xffff);
102
  _bg_bottom.SetAlpha (1.0f);
103
853.4.14 by Neil Jagdish Patel
More styling fixes
104
  changed.emit ();
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
105
}
106
107
nux::Color&
108
PanelStyle::GetTextColor ()
109
{
110
  return _text;
111
}
112
113
nux::Color&
114
PanelStyle::GetBackgroundTop ()
115
{
116
  return _bg_top;
117
}
118
119
nux::Color&
120
PanelStyle::GetBackgroundBottom ()
121
{
122
  return _bg_bottom;
123
}
124
125
nux::Color&
126
PanelStyle::GetTextShadow ()
127
{
128
  return _text_shadow;
129
}
130
871.4.24 by Neil Jagdish Patel
Fix Panel font rendering to be clearer, fix entry to be larger and render correctly
131
nux::Color&
132
PanelStyle::GetLineColor ()
133
{
134
  return _line;
135
}
136
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
137
void
138
PanelStyle::OnStyleChanged (GObject*    gobject,
139
                            GParamSpec* pspec,
140
                            gpointer    data)
141
{
142
  PanelStyle* self = (PanelStyle*) data;
143
144
  self->Refresh ();
145
}
146
147
GdkPixbuf *
148
PanelStyle::GetBackground (int width, int height)
149
{
150
  gtk_widget_set_size_request (_offscreen, width, height);
853.4.15 by Neil Jagdish Patel
Add support for swtiching themes properly
151
  gdk_window_process_updates (gtk_widget_get_window (_offscreen), TRUE);
152
  
853.4.10 by Neil Jagdish Patel
Update the style branch, add ability to draw the panel background from the theme
153
  return gtk_offscreen_window_get_pixbuf (GTK_OFFSCREEN_WINDOW (_offscreen));
154
}
155
853.4.17 by Neil Jagdish Patel
correctly repaint everything
156
nux::BaseTexture *
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
157
PanelStyle::GetWindowButton (WindowButtonType type, WindowState state)
853.4.17 by Neil Jagdish Patel
correctly repaint everything
158
{
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
159
#define ICON_LOCATION "/usr/share/themes/%s/metacity-1/%s%s.png"
160
  nux::BaseTexture * texture = NULL;
161
  const char *names[] = { "close", "minimize", "unmaximize" };
162
  const char *states[] = { "", "_focused_prelight", "_focused_pressed" };
163
164
  // I wish there was a magic bullet here, but not all themes actually set the panel to be
165
  // the same style as the window titlebars (e.g. Clearlooks) so we can just grab the 
166
  // metacity window buttons as that would look horrible
167
  if (g_strcmp0 (_theme_name, "Ambiance") == 0
168
      || g_strcmp0 (_theme_name, "Radiance") == 0)
169
  {
170
    char      *filename;
171
    GdkPixbuf *pixbuf;
172
    GError    *error = NULL;
173
174
    filename = g_strdup_printf (ICON_LOCATION, _theme_name, names[type], states[state]);
175
176
    pixbuf = gdk_pixbuf_new_from_file (filename, &error);
177
    if (error)
178
    {
179
      g_warning ("Unable to load window button %s: %s", filename, error->message);
180
      g_error_free (error);
181
      error = NULL;
182
    }
183
    else
184
      texture = nux::CreateTexture2DFromPixbuf (pixbuf, true);
185
186
    g_free (filename);
187
    g_object_unref (pixbuf);
188
  }
189
  else
190
  {
853.4.19 by Neil Jagdish Patel
Add support for non Ambiance/Radiance window buttons
191
    texture = GetWindowButtonForTheme (type, state);
192
  }
193
194
  return texture;
195
}
196
197
nux::BaseTexture *
198
PanelStyle::GetWindowButtonForTheme (WindowButtonType type, WindowState state)
199
{
200
  nux::BaseTexture *texture = NULL;
201
  int width = 18, height = 18;
202
  float w = width/3.0f;
203
  float h = height/3.0f;
204
  nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, 22, 22);
205
  cairo_t *cr;
206
  nux::Color main = _text;
207
208
  if (type == WINDOW_BUTTON_CLOSE)
209
  {
210
    main = nux::Color (1.0f, 0.3f, 0.3f, 0.8f);
211
  }
212
213
  if (state == WINDOW_STATE_PRELIGHT)
214
    main = main * 1.2f;
215
  else if (state == WINDOW_STATE_PRESSED)
216
    main = main * 0.8f;
217
  
218
  cr  = cairo_graphics.GetContext();
219
  cairo_translate (cr, 0.5, 0.5);
220
  cairo_set_line_width (cr, 1.5f);
221
222
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
223
  cairo_paint (cr);
224
225
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
226
  
227
  cairo_set_source_rgba (cr, main.GetRed (), main.GetGreen (), main.GetBlue (), main.GetAlpha ());
228
229
  cairo_arc (cr, width/2.0f, height/2.0f, (width - 2)/2.0f, 0.0f, 360 * (M_PI/180));
230
  cairo_stroke (cr);
231
232
  if (type == WINDOW_BUTTON_CLOSE)
233
  {
234
    cairo_move_to (cr, w, h);
235
    cairo_line_to (cr, width - w, height - h);
236
    cairo_move_to (cr, width -w, h);
237
    cairo_line_to (cr, w, height - h);
238
  }
239
  else if (type == WINDOW_BUTTON_MINIMIZE)
240
  {
241
    cairo_move_to (cr, w, height/2.0f);
242
    cairo_line_to (cr, width - w, height/2.0f);
243
  }
244
  else
245
  {
246
    cairo_move_to (cr, w, h);
247
    cairo_line_to (cr, width - w, h);
248
    cairo_line_to (cr, width - w, height - h);
249
    cairo_line_to (cr, w, height -h);
250
    cairo_close_path (cr);
251
  }
252
253
  cairo_stroke (cr);
254
255
  cairo_destroy (cr);
256
  
257
  nux::NBitmapData* bitmap =  cairo_graphics.GetBitmap();
258
  texture = nux::GetThreadGLDeviceFactory ()->CreateSystemCapableTexture ();
259
  texture->Update(bitmap);
260
  delete bitmap;
853.4.18 by Neil Jagdish Patel
Implement panel button theming support for Ambiance and Radiance
261
262
  return texture;
853.4.17 by Neil Jagdish Patel
correctly repaint everything
263
}
853.4.20 by Neil Jagdish Patel
Fix the home button logo to match theme
264
265
GdkPixbuf *
266
PanelStyle::GetHomeButton ()
267
{
268
  GdkPixbuf *pixbuf = NULL;
269
270
  if (g_str_has_prefix (_theme_name, "Ambiance"))
271
    pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR"/bfb.png", NULL);
272
  else
273
    pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
274
                                       "start-here",
275
                                       24,
276
                                       (GtkIconLookupFlags)0,
277
                                       NULL); 
278
  if (pixbuf == NULL)
279
    pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
280
                                       "distributor-logo",
281
                                       24,
282
                                       (GtkIconLookupFlags)0,
283
                                       NULL);
284
  return pixbuf;
285
}