~didrocks/unity/launcher-bug-fix-fest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Gordon Allott <gord.allott@canonical.com>
 *
 */

#include "config.h"

#include "TextureCache.h"
#include "Nux/Nux.h"
#include "PlacesTile.h"

#define PADDING 8
#define BLUR_SIZE 5

PlacesTile::PlacesTile (NUX_FILE_LINE_DECL, const void *id) :
  View (NUX_FILE_LINE_PARAM),
  _id (id),
  _hilight_background (NULL),
  _hilight_layer (NULL),
  _last_width (0),
  _last_height (0)
{
  OnMouseDown.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseDown));
  OnMouseUp.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseUp));
  OnMouseClick.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseClick));
  OnMouseEnter.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseEnter));
  OnMouseLeave.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseLeave));
  FocusChanged.connect (sigc::mem_fun (this, &PlacesTile::OnFocusChanged));
  FocusActivated.connect (sigc::mem_fun (this, &PlacesTile::OnFocusActivated));
  _can_pass_focus_to_composite_layout = false;
}

PlacesTile::~PlacesTile ()
{
  if (_hilight_background)
  {
    _hilight_background->UnReference ();
    con_obj.disconnect ();
  }

  if (_hilight_layer)
    delete _hilight_layer;
}

const void *
PlacesTile::GetId ()
{
  return _id;
}

void
PlacesTile::OnFocusChanged (nux::Area *area)
{
  QueueDraw ();
}

nux::Geometry
PlacesTile::GetHighlightGeometry ()
{
  return GetGeometry ();
}

void
PlacesTile::DrawHighlight (const char *texid, int width, int height, nux::BaseTexture **texture)
{
  nux::Geometry base = GetGeometry ();
  nux::Geometry highlight_geo = GetHighlightGeometry ();
  nux::CairoGraphics *cairo_graphics = new nux::CairoGraphics (CAIRO_FORMAT_ARGB32,
                                                               highlight_geo.width + PADDING + (BLUR_SIZE*3),
                                                               highlight_geo.height + PADDING + (BLUR_SIZE*3));
  cairo_t *cr = cairo_graphics->GetContext();

  cairo_scale (cr, 1.0f, 1.0f);

  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);
  
  int bg_width = highlight_geo.width + PADDING;
  int bg_height = highlight_geo.height + PADDING;
 	int bg_x = BLUR_SIZE;
 	int bg_y = BLUR_SIZE;
	
  // draw the glow
	cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  cairo_set_line_width (cr, 1.0f);
  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.75f);
  cairo_graphics->DrawRoundedRectangle (cr,
                                       1.0f,
                                       bg_x,
                                       bg_y,
                                       5.0,
                                       bg_width,
                                       bg_height,
                                       true);
  cairo_fill (cr);
  cairo_graphics->BlurSurface (BLUR_SIZE - 2);
	
  // draw tiled background
  // set up clip path
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_graphics->DrawRoundedRectangle (cr,
                                        1.0,
                                        bg_x,
                                        bg_y,
                                        5.0,
                                        bg_width,
                                        bg_height,
                                        true);
  cairo_clip (cr);

  int              w, h;
  cairo_surface_t *image;
  cairo_pattern_t *pattern;

  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
  image = cairo_image_surface_create_from_png (PKGDATADIR"/places-tile-bg-tilable.png");
  w = cairo_image_surface_get_width (image);
  h = cairo_image_surface_get_height (image);


  pattern = cairo_pattern_create_for_surface (image);
  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);

  cairo_set_source (cr, pattern);

  cairo_rectangle (cr, 0, 0, base.width, base.height);
  cairo_fill (cr);

  cairo_pattern_destroy (pattern);
  cairo_surface_destroy (image);

  // draw the outline
	
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

  cairo_graphics->DrawRoundedRectangle (cr,
                                        1.0,
                                        bg_x,
                                       	bg_y,
                                        5.0,
                                        bg_width,
                                        bg_height,
                                        true);
  cairo_set_source_rgba (cr, 0.66, 0.66, 0.66, 1.0);
  cairo_set_line_width (cr, 1.0);
  cairo_stroke (cr);
  
  
  cairo_destroy (cr);

  nux::NBitmapData *bitmap =  cairo_graphics->GetBitmap();
  nux::BaseTexture *tex = nux::GetThreadGLDeviceFactory()->CreateSystemCapableTexture ();
  tex->Update (bitmap);
  *texture = tex;

  delete bitmap;
}

void
PlacesTile::OnDestroyNotify (nux::Trackable *Object)
{
  g_warning ("Texture destroyed before we were ready");
  _hilight_background = NULL;
  UpdateBackground ();
}


void
PlacesTile::UpdateBackground ()
{
  nux::Geometry base = GetGeometry ();
  nux::Geometry highlight_geo = GetHighlightGeometry ();

  if ((base.width == _last_width) && (base.height == _last_height))
    return;

  _last_width = base.width;
  _last_height = base.height;

  // try and get a texture from the texture cache
  TextureCache *cache = TextureCache::GetDefault ();
  nux::BaseTexture * hilight_tex = cache->FindTexture ("PlacesTile.HilightTexture",
                                                       highlight_geo.width, highlight_geo.height,
                                                       sigc::mem_fun (this, &PlacesTile::DrawHighlight));

  if (_hilight_background)
  {
    _hilight_background->UnReference ();
    con_obj.disconnect ();
  }

  con_obj = hilight_tex->OnDestroyed.connect (sigc::mem_fun (this, &PlacesTile::OnDestroyNotify));

  _hilight_background = hilight_tex;
  _hilight_background->Reference ();

  nux::ROPConfig rop;
  rop.Blend = true;
  rop.SrcBlend = GL_ONE;
  rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;

  nux::TexCoordXForm texxform;
  texxform.SetTexCoordType (nux::TexCoordXForm::OFFSET_COORD);
  texxform.SetWrap (nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);

  if (_hilight_layer)
    delete _hilight_layer;

  _hilight_layer = new nux::TextureLayer (_hilight_background->GetDeviceTexture(),
                                          texxform,
                                          nux::Color::White,
                                          true,
                                          rop);
}

long
PlacesTile::ProcessEvent(nux::IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
{
  return PostProcessEvent2 (ievent, TraverseInfo, ProcessEventInfo);
}

void
PlacesTile::Draw (nux::GraphicsEngine& gfxContext,
                       bool                 forceDraw)
{
  nux::Geometry base = GetGeometry ();

  nux::GetPainter ().PaintBackground (gfxContext, base);

  gfxContext.PushClippingRectangle (base);

  if (GetFocused () || IsMouseInside ())
  {
    UpdateBackground ();
    nux::Geometry hl_geo = GetHighlightGeometry ();
    nux::Geometry total_highlight_geo = nux::Geometry (base.x + hl_geo.x - (PADDING)/2 - BLUR_SIZE - 1,
                                                       base.y + hl_geo.y - (PADDING)/2 - BLUR_SIZE - 1,
                                                       hl_geo.width + PADDING + 1 + BLUR_SIZE*2,
                                                       hl_geo.height + PADDING + 1 + BLUR_SIZE*2);

    _hilight_layer->SetGeometry (total_highlight_geo);
    nux::GetPainter ().RenderSinglePaintLayer (gfxContext, total_highlight_geo, _hilight_layer);
  }

  gfxContext.PopClippingRectangle ();
}

void
PlacesTile::DrawContent (nux::GraphicsEngine &GfxContext, bool force_draw)
{
  nux::Geometry base = GetGeometry ();

  GfxContext.PushClippingRectangle (base);

  if (GetFocused () || IsMouseInside ())
  {
    UpdateBackground ();

    nux::Geometry hl_geo = GetHighlightGeometry ();
    nux::Geometry total_highlight_geo = nux::Geometry (base.x + hl_geo.x - (PADDING)/2 - BLUR_SIZE - 1,
                                                       base.y + hl_geo.y - (PADDING)/2 - BLUR_SIZE - 1,
                                                       hl_geo.width + PADDING + 1 + BLUR_SIZE*2,
                                                       hl_geo.height + PADDING + 1 + BLUR_SIZE*2);

    nux::GetPainter ().PushLayer (GfxContext, total_highlight_geo, _hilight_layer);
  }

  if (GetCompositionLayout ())
    GetCompositionLayout ()->ProcessDraw (GfxContext, force_draw);

  if (IsMouseInside () || GetFocused ())
    nux::GetPainter ().PopBackground ();

  GfxContext.PopClippingRectangle();
}

void
PlacesTile::RecvMouseClick (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  if (nux::GetEventButton (button_flags) == 1)
    sigClick.emit (this);
  QueueDraw ();
}

void
PlacesTile::RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  QueueDraw ();
}

void
PlacesTile::RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  QueueDraw ();
}

void
PlacesTile::RecvMouseEnter (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  QueueDraw ();
}

void
PlacesTile::RecvMouseLeave (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  QueueDraw ();
}

void
PlacesTile::OnFocusActivated (nux::Area *area)
{
  sigClick.emit (this);
}