~timo-jyrinki/unity/ubuntu5180

« back to all changes in this revision

Viewing changes to src/PanelIndicatorObjectEntryView.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-11-03 11:44:21 UTC
  • mto: (55.350.1 unity-3.0.panel)
  • mto: This revision was merged to the branch mainline in revision 271.
  • Revision ID: neil.patel@canonical.com-20101103114421-uvvrsb782fds5pbi
Import the work done so far with Compiz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: Neil Jagdish Patel <neil.patel@canonical.com>
 
17
 */
 
18
 
 
19
#include "Nux/Nux.h"
 
20
#include "Nux/HLayout.h"
 
21
#include "Nux/VLayout.h"
 
22
 
 
23
#include "NuxGraphics/GLThread.h"
 
24
#include "Nux/BaseWindow.h"
 
25
#include "Nux/WindowCompositor.h"
 
26
 
 
27
#include "PanelIndicatorObjectEntryView.h"
 
28
 
 
29
#include <glib.h>
 
30
#include <pango/pangocairo.h>
 
31
#include <gtk/gtk.h>
 
32
#include <time.h>
 
33
 
 
34
#define PANEL_HEIGHT 24
 
35
#define PADDING 6
 
36
#define SPACING 3
 
37
 
 
38
static void draw_menu_bg (cairo_t *cr, int width, int height);
 
39
 
 
40
 
 
41
PanelIndicatorObjectEntryView::PanelIndicatorObjectEntryView (IndicatorObjectEntryProxy *proxy)
 
42
: TextureArea (NUX_TRACKER_LOCATION),
 
43
  _proxy (proxy),
 
44
  _util_cg (CAIRO_FORMAT_ARGB32, 1, 1)
 
45
{
 
46
  _proxy->Updated.connect (sigc::mem_fun (this, &PanelIndicatorObjectEntryView::Refresh));
 
47
  //InputArea::OnMouseDown.connect (sigc::mem_fun (this, &PanelIndicatorObjectEntryView::OnMouseDown));
 
48
  InputArea::OnMouseUp.connect (sigc::mem_fun (this, &PanelIndicatorObjectEntryView::OnMouseDown));
 
49
 
 
50
  Refresh ();
 
51
}
 
52
 
 
53
PanelIndicatorObjectEntryView::~PanelIndicatorObjectEntryView ()
 
54
{
 
55
}
 
56
 
 
57
void
 
58
PanelIndicatorObjectEntryView::OnMouseDown (int x, int y, long button_flags, long key_flags)
 
59
{
 
60
  //printf ("OnMouseDown: %d %d %ld %ld\n", x, y, button_flags, key_flags);
 
61
  //printf ("Geometry   : %d %d %d %d\n", GetGeometry ().x, GetGeometry ().y, GetGeometry ().width, GetGeometry ().height);
 
62
 
 
63
  _proxy->ShowMenu (GetGeometry ().x, PANEL_HEIGHT, time (NULL));
 
64
}
 
65
 
 
66
static char *
 
67
fix_string (const char *string)
 
68
{
 
69
  if (string == NULL)
 
70
    return NULL;
 
71
 
 
72
  char buf[256];
 
73
  int buf_pos = 0;
 
74
  int i = 0;
 
75
  int len = strlen (string);
 
76
 
 
77
  for (i = 0; i < len; i++)
 
78
    {
 
79
      if (string[i] != '_')
 
80
        {
 
81
          buf[buf_pos] = string[i];
 
82
          buf_pos++;
 
83
        }
 
84
    }
 
85
  buf[buf_pos] = '\0';
 
86
 
 
87
  return g_strdup (buf);
 
88
}
 
89
 
 
90
// We need to do a couple of things here:
 
91
// 1. Figure out our width
 
92
// 2. Figure out if we're active
 
93
// 3. Paint something
 
94
void
 
95
PanelIndicatorObjectEntryView::Refresh ()
 
96
{
 
97
  GdkPixbuf            *pixbuf = _proxy->GetPixbuf ();
 
98
  char                 *label = fix_string (_proxy->GetLabel ());
 
99
  PangoLayout          *layout = NULL;
 
100
  PangoFontDescription *desc = NULL;
 
101
  GtkSettings          *settings = gtk_settings_get_default ();
 
102
  cairo_t              *cr;
 
103
  char                 *font_description = NULL;
 
104
  GdkScreen            *screen = gdk_screen_get_default ();
 
105
  int                   dpi = 0;
 
106
 
 
107
  int  x = 0;
 
108
  int  y = 0;
 
109
  int  width = 0;
 
110
  int  height = PANEL_HEIGHT;
 
111
  int  icon_width = 0;
 
112
  int  text_width = 0;
 
113
  int  text_height = 0;
 
114
 
 
115
 
 
116
  // First lets figure out our size
 
117
  if (pixbuf && _proxy->icon_visible)
 
118
  {
 
119
    width = gdk_pixbuf_get_width (pixbuf);
 
120
    icon_width = width;
 
121
  }
 
122
 
 
123
  if (label && _proxy->label_visible)
 
124
  {
 
125
    PangoContext *cxt;
 
126
    PangoRectangle log_rect;
 
127
 
 
128
    cr = _util_cg.GetContext ();
 
129
 
 
130
    g_object_get (settings,
 
131
                  "gtk-font-name", &font_description,
 
132
                  "gtk-xft-dpi", &dpi,
 
133
                  NULL);
 
134
    desc = pango_font_description_from_string (font_description);
 
135
    pango_font_description_set_weight (desc, PANGO_WEIGHT_NORMAL);
 
136
 
 
137
    layout = pango_cairo_create_layout (cr);
 
138
    pango_layout_set_font_description (layout, desc);
 
139
    pango_layout_set_text (layout, label, -1);
 
140
    
 
141
    cxt = pango_layout_get_context (layout);
 
142
    pango_cairo_context_set_font_options (cxt, gdk_screen_get_font_options (screen));
 
143
    pango_cairo_context_set_resolution (cxt, (float)dpi/(float)PANGO_SCALE);
 
144
    pango_layout_context_changed (layout);
 
145
 
 
146
    pango_layout_get_extents (layout, NULL, &log_rect);
 
147
    text_width = log_rect.width / PANGO_SCALE;
 
148
    text_height = log_rect.height / PANGO_SCALE;
 
149
 
 
150
    if (icon_width)
 
151
      width += SPACING;
 
152
    width += text_width;
 
153
 
 
154
    pango_font_description_free (desc);
 
155
    g_free (font_description);
 
156
    cairo_destroy (cr);
 
157
  }
 
158
 
 
159
  if (width)
 
160
    width += PADDING *2;
 
161
 
 
162
  SetMinimumWidth (width);
 
163
 
 
164
  nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, width, height);
 
165
  cr = cairo_graphics.GetContext();
 
166
  cairo_set_line_width (cr, 1);
 
167
 
 
168
  if (_proxy->GetActive ())
 
169
    draw_menu_bg (cr, width, height);
 
170
 
 
171
  x = PADDING;
 
172
  y = 0;
 
173
 
 
174
  if (_proxy->GetPixbuf () && _proxy->icon_visible)
 
175
  {
 
176
    gdk_cairo_set_source_pixbuf (cr, pixbuf, x, (height - gdk_pixbuf_get_height (pixbuf))/2);
 
177
    cairo_paint (cr);
 
178
 
 
179
    x += icon_width + SPACING;
 
180
  }
 
181
 
 
182
  if (label && _proxy->label_visible)
 
183
  {
 
184
    pango_cairo_update_layout (cr, layout);
 
185
 
 
186
    // Once for the homies that couldn't be here
 
187
    cairo_set_source_rgb (cr, 50/255.0f, 50/255.0f, 45/255.0f);
 
188
    cairo_move_to (cr, x, ((height - text_height)/2)-1);
 
189
    pango_cairo_show_layout (cr, layout);
 
190
    cairo_stroke (cr);
 
191
 
 
192
    // Once again for the homies that could
 
193
    cairo_set_source_rgba (cr, 223/255.0f, 219/255.0f, 210/255.0f, 1.0f);
 
194
    cairo_move_to (cr, x, (height - text_height)/2);
 
195
    pango_cairo_show_layout (cr, layout);
 
196
    cairo_stroke (cr);
 
197
  }
 
198
 
 
199
  cairo_destroy (cr);
 
200
  if (layout)
 
201
    g_object_unref (layout);
 
202
 
 
203
  nux::NBitmapData* bitmap =  cairo_graphics.GetBitmap();
 
204
 
 
205
  nux::BaseTexture* texture2D = nux::GetThreadGLDeviceFactory ()->CreateSystemCapableTexture ();
 
206
  texture2D->Update(bitmap);
 
207
  delete bitmap;
 
208
  
 
209
  nux::TexCoordXForm texxform;
 
210
  texxform.SetTexCoordType (nux::TexCoordXForm::OFFSET_COORD);
 
211
  texxform.SetWrap (nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
 
212
  
 
213
  nux::ROPConfig rop; 
 
214
  rop.Blend = true;                       // Enable the blending. By default rop.Blend is false.
 
215
  rop.SrcBlend = GL_ONE;                  // Set the source blend factor.
 
216
  rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;  // Set the destination blend factor.
 
217
  nux::TextureLayer* texture_layer = new nux::TextureLayer (texture2D->GetDeviceTexture(),
 
218
                                                            texxform,           // The Oject that defines the texture wraping and coordinate transformation.
 
219
                                                            nux::Color::White,  // The color used to modulate the texture.
 
220
                                                            false,  // Write the alpha value of the texture to the destination buffer.
 
221
                                                            rop     // Use the given raster operation to set the blending when the layer is being rendered.
 
222
                                                            );
 
223
 
 
224
  delete texture2D;
 
225
  
 
226
  SetPaintLayer (texture_layer);
 
227
 
 
228
  NeedRedraw ();
 
229
 
 
230
  if (label)
 
231
    g_free (label);
 
232
}
 
233
 
 
234
static void
 
235
draw_menu_bg (cairo_t *cr, int width, int height)
 
236
{
 
237
  int radius = 4;
 
238
  double x = 0;
 
239
  double y = 0;
 
240
  double xos = 1.5;
 
241
  double yos = 1.5;
 
242
  /* FIXME */
 
243
  double mpi = 3.14159265358979323846;
 
244
 
 
245
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
 
246
 
 
247
  cairo_set_line_width (cr, 1.0);
 
248
 
 
249
  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.2);
 
250
 
 
251
  cairo_move_to (cr, x+xos+radius, y+yos);
 
252
  cairo_arc (cr, x+xos+width-xos*2-radius, y+yos+radius, radius, mpi*1.5, mpi*2);
 
253
  cairo_line_to (cr, x+xos+width-xos*2, y+yos+height-yos*2+2);
 
254
  cairo_line_to (cr, x+xos, y+yos+height-yos*2+2);
 
255
  cairo_arc (cr, x+xos+radius, y+yos+radius, radius, mpi, mpi*1.5);
 
256
 
 
257
  cairo_pattern_t * pat = cairo_pattern_create_linear (x+xos, y, x+xos, y+height-yos*2+2);
 
258
  cairo_pattern_add_color_stop_rgba (pat, 0.0, 83/255.0f, 82/255.0f, 78/255.0f, 1.0f);
 
259
  cairo_pattern_add_color_stop_rgba (pat, 1.0, 66/255.0f, 65/255.0f, 63/255.0f, 1.0f);
 
260
  cairo_set_source (cr, pat);
 
261
  cairo_fill_preserve (cr);
 
262
  cairo_pattern_destroy (pat);
 
263
 
 
264
  pat = cairo_pattern_create_linear (x+xos, y, x+xos, y+height-yos*2+2);
 
265
  cairo_pattern_add_color_stop_rgba (pat, 0.0, 62/255.0f, 61/255.0f, 58/255.0f, 1.0f);
 
266
  cairo_pattern_add_color_stop_rgba (pat, 1.0, 54/255.0f, 54/255.0f, 52/255.0f, 1.0f);
 
267
  cairo_set_source (cr, pat);
 
268
  cairo_stroke (cr);
 
269
  cairo_pattern_destroy (pat);
 
270
 
 
271
  xos++;
 
272
  yos++;
 
273
 
 
274
  /* enlarging the area to not draw the lightborder at bottom, ugly trick :P */
 
275
  cairo_move_to (cr, x+radius+xos, y+yos);
 
276
  cairo_arc (cr, x+xos+width-xos*2-radius, y+yos+radius, radius, mpi*1.5, mpi*2);
 
277
  cairo_line_to (cr, x+xos+width-xos*2, y+yos+height-yos*2+3);
 
278
  cairo_line_to (cr, x+xos, y+yos+height-yos*2+3);
 
279
  cairo_arc (cr, x+xos+radius, y+yos+radius, radius, mpi, mpi*1.5);
 
280
 
 
281
  pat = cairo_pattern_create_linear (x+xos, y, x+xos, y+height-yos*2+3);
 
282
  cairo_pattern_add_color_stop_rgba (pat, 0.0, 92/255.0f, 90/255.0f, 85/255.0f, 1.0f);
 
283
  cairo_pattern_add_color_stop_rgba (pat, 1.0, 70/255.0f, 69/255.0f, 66/255.0f, 1.0f);
 
284
  cairo_set_source (cr, pat);
 
285
  cairo_stroke (cr);
 
286
  cairo_pattern_destroy (pat);
 
287
}