~ken-vandine/unity/make-quicklists-work-again

« back to all changes in this revision

Viewing changes to src/LauncherIcon.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-11-11 18:51:08 UTC
  • mfrom: (572.1.58 unity-3.0)
  • Revision ID: neil.patel@canonical.com-20101111185108-71923a90txzvxbit
[merge] Unity 3

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: Jason Smith <jason.smith@canonical.com>
 
17
 */
 
18
 
 
19
#include <sys/time.h>
 
20
 
 
21
#include "Nux/Nux.h"
 
22
#include "Nux/VScrollBar.h"
 
23
#include "Nux/HLayout.h"
 
24
#include "Nux/VLayout.h"
 
25
#include "Nux/MenuPage.h"
 
26
#include "Nux/WindowCompositor.h"
 
27
#include "Nux/BaseWindow.h"
 
28
#include "Nux/MenuPage.h"
 
29
#include "NuxCore/Color.h"
 
30
 
 
31
#include "LauncherIcon.h"
 
32
#include "Launcher.h"
 
33
 
 
34
#define DEFAULT_ICON "empathy"
 
35
 
 
36
LauncherIcon::LauncherIcon(Launcher* IconManager)
 
37
{
 
38
  _folding_angle = 0;
 
39
  m_IconManager = IconManager;
 
40
  m_TooltipText = "blank";
 
41
 
 
42
  _show_time.tv_sec = 0;
 
43
  _hide_time.tv_sec = 0;
 
44
  _running_time.tv_sec = 0;
 
45
  _urgent_time.tv_sec = 0;
 
46
 
 
47
  _show_time.tv_usec = 0;
 
48
  _hide_time.tv_usec = 0;
 
49
  _running_time.tv_usec = 0;
 
50
  _urgent_time.tv_usec = 0;
 
51
 
 
52
  _active  = false;
 
53
  _running = false;
 
54
  _visible = false;
 
55
  _urgent  = false;
 
56
  
 
57
  _related_windows = 0;
 
58
 
 
59
  _background_color = nux::Color::White;
 
60
  _mouse_inside = false;
 
61
  _tooltip = new nux::Tooltip ();
 
62
  _icon_type = LAUNCHER_ICON_TYPE_NONE;
 
63
  _sort_priority = 0;
 
64
 
 
65
  MouseEnter.connect (sigc::mem_fun(this, &LauncherIcon::RecvMouseEnter));
 
66
  MouseLeave.connect (sigc::mem_fun(this, &LauncherIcon::RecvMouseLeave));
 
67
}
 
68
 
 
69
LauncherIcon::~LauncherIcon()
 
70
{
 
71
}
 
72
 
 
73
nux::Color LauncherIcon::BackgroundColor ()
 
74
{
 
75
  return _background_color;
 
76
}
 
77
 
 
78
nux::BaseTexture * LauncherIcon::TextureForSize (int size)
 
79
{
 
80
  nux::BaseTexture * result = GetTextureForSize (size);
 
81
  return result;
 
82
}
 
83
 
 
84
nux::Color LauncherIcon::ColorForIcon (GdkPixbuf *pixbuf)
 
85
{
 
86
  unsigned int width = gdk_pixbuf_get_width (pixbuf);
 
87
  unsigned int height = gdk_pixbuf_get_height (pixbuf);
 
88
  unsigned int row_bytes = gdk_pixbuf_get_rowstride (pixbuf);
 
89
  
 
90
  long int rtotal = 0, gtotal = 0, btotal = 0;
 
91
  float total = 0.0f;
 
92
  
 
93
  
 
94
  guchar *img = gdk_pixbuf_get_pixels (pixbuf);
 
95
  
 
96
  for (unsigned int i = 0; i < width; i++)
 
97
  {
 
98
    for (unsigned int j = 0; j < height; j++)
 
99
    {
 
100
      guchar *pixels = img + ( j * row_bytes + i * 4);
 
101
      guchar r = *(pixels + 0);
 
102
      guchar g = *(pixels + 1);
 
103
      guchar b = *(pixels + 2);
 
104
      guchar a = *(pixels + 3);
 
105
      
 
106
      float saturation = (MAX (r, MAX (g, b)) - MIN (r, MIN (g, b))) / 255.0f;
 
107
      float relevance = .1 + .9 * (a / 255.0f) * saturation;
 
108
      
 
109
      rtotal += (guchar) (r * relevance);
 
110
      gtotal += (guchar) (g * relevance);
 
111
      btotal += (guchar) (b * relevance);
 
112
      
 
113
      total += relevance * 255;
 
114
    }
 
115
  }
 
116
  
 
117
  float r, g, b, h, s, v;
 
118
  r = rtotal / total;
 
119
  g = gtotal / total;
 
120
  b = btotal / total;
 
121
  
 
122
  nux::RGBtoHSV (r, g, b, h, s, v);
 
123
  
 
124
  if (s > .15f)
 
125
    s = 0.4f;
 
126
  v = .85f;
 
127
  
 
128
  nux::HSVtoRGB (r, g, b, h, s, v);
 
129
  
 
130
  return nux::Color (r, g, b);
 
131
}
 
132
 
 
133
nux::BaseTexture * LauncherIcon::TextureFromGtkTheme (const char *icon_name, int size)
 
134
{
 
135
  GdkPixbuf *pbuf;
 
136
  GtkIconTheme *theme;
 
137
  GtkIconInfo *info;
 
138
  nux::BaseTexture *result;
 
139
  
 
140
  theme = gtk_icon_theme_get_default ();
 
141
      
 
142
  if (!icon_name)
 
143
    icon_name = g_strdup (DEFAULT_ICON);
 
144
   
 
145
  info = gtk_icon_theme_lookup_icon (theme,
 
146
                                     icon_name,
 
147
                                     size,
 
148
                                     (GtkIconLookupFlags) 0);            
 
149
  if (!info)
 
150
  {
 
151
    info = gtk_icon_theme_lookup_icon (theme,
 
152
                                       DEFAULT_ICON,
 
153
                                       size,
 
154
                                       (GtkIconLookupFlags) 0);
 
155
  }
 
156
        
 
157
  if (gtk_icon_info_get_filename (info) == NULL)
 
158
  {
 
159
    info = gtk_icon_theme_lookup_icon (theme,
 
160
                                       DEFAULT_ICON,
 
161
                                       size,
 
162
                                       (GtkIconLookupFlags) 0);
 
163
  }
 
164
  
 
165
  pbuf = gtk_icon_info_load_icon (info, NULL);
 
166
  result = nux::CreateTextureFromPixbuf (pbuf);
 
167
  
 
168
  _background_color = ColorForIcon (pbuf);
 
169
  
 
170
  g_object_unref (pbuf);
 
171
  
 
172
  return result;
 
173
}
 
174
 
 
175
void LauncherIcon::SetTooltipText(const TCHAR* text)
 
176
{
 
177
    m_TooltipText = text;
 
178
    _tooltip->SetText (m_TooltipText);
 
179
}
 
180
 
 
181
nux::NString LauncherIcon::GetTooltipText()
 
182
{
 
183
    return m_TooltipText;
 
184
}
 
185
 
 
186
void
 
187
LauncherIcon::RecvMouseEnter ()
 
188
{
 
189
  int icon_x = _xform_screen_coord[0].x;
 
190
  int icon_y = _xform_screen_coord[0].y;
 
191
  int icon_w = _xform_screen_coord[2].x - _xform_screen_coord[0].x;
 
192
  int icon_h = _xform_screen_coord[2].y - _xform_screen_coord[0].y;
 
193
 
 
194
  _tooltip->SetBaseX (icon_x + icon_w - 10);
 
195
  _tooltip->SetBaseY (icon_y +
 
196
                      23 + // TODO: HARCODED, replace m_IconManager->GetBaseY ()
 
197
                      (icon_h / 2) -
 
198
                      (_tooltip->GetBaseHeight () / 2));
 
199
  _tooltip->ShowWindow (true);
 
200
}
 
201
 
 
202
void LauncherIcon::RecvMouseLeave ()
 
203
{
 
204
  _tooltip->ShowWindow (false);
 
205
}
 
206
 
 
207
void LauncherIcon::HideTooltip ()
 
208
{
 
209
  _tooltip->ShowWindow (false);
 
210
}
 
211
 
 
212
struct timeval LauncherIcon::ShowTime ()
 
213
{
 
214
  return _show_time;
 
215
}
 
216
 
 
217
struct timeval LauncherIcon::HideTime ()
 
218
{
 
219
  return _hide_time;
 
220
}
 
221
 
 
222
struct timeval LauncherIcon::RunningTime ()
 
223
{
 
224
  return _running_time;
 
225
}
 
226
 
 
227
struct timeval LauncherIcon::UrgentTime ()
 
228
{
 
229
  return _urgent_time;
 
230
}
 
231
 
 
232
void
 
233
LauncherIcon::SetVisible (bool visible)
 
234
{
 
235
  if (visible == _visible)
 
236
    return;
 
237
      
 
238
  _visible = visible;
 
239
  
 
240
  needs_redraw.emit (this);
 
241
 
 
242
  if (visible)
 
243
  {
 
244
    gettimeofday (&_show_time, NULL);
 
245
    show.emit (this);
 
246
  }
 
247
  else
 
248
  {
 
249
    gettimeofday (&_hide_time, NULL);
 
250
    hide.emit (this);
 
251
  }
 
252
}
 
253
 
 
254
void
 
255
LauncherIcon::SetActive (bool active)
 
256
{
 
257
  if (active == _active)
 
258
    return;
 
259
    
 
260
  _active = active;
 
261
  needs_redraw.emit (this);
 
262
}
 
263
 
 
264
void LauncherIcon::SetRunning (bool running)
 
265
{
 
266
  if (running == _running)
 
267
    return;
 
268
    
 
269
  _running = running;
 
270
  gettimeofday (&_running_time, NULL);
 
271
  needs_redraw.emit (this);
 
272
}
 
273
 
 
274
void LauncherIcon::SetUrgent (bool urgent)
 
275
{
 
276
  if (urgent == _urgent)
 
277
    return;
 
278
  
 
279
  _urgent = urgent;
 
280
  
 
281
  if (urgent)
 
282
      gettimeofday (&_urgent_time, NULL);
 
283
  
 
284
  needs_redraw.emit (this);
 
285
}
 
286
 
 
287
void LauncherIcon::SetRelatedWindows (int windows)
 
288
{
 
289
  if (_related_windows == windows)
 
290
    return;
 
291
    
 
292
  _related_windows = windows;
 
293
  needs_redraw.emit (this);
 
294
}
 
295
 
 
296
void LauncherIcon::Remove ()
 
297
{
 
298
  SetVisible (false);
 
299
  remove.emit (this);
 
300
}
 
301
 
 
302
void 
 
303
LauncherIcon::SetIconType (LauncherIconType type)
 
304
{
 
305
  _icon_type = type;
 
306
}
 
307
 
 
308
void 
 
309
LauncherIcon::SetSortPriority (int priority)
 
310
{
 
311
  _sort_priority = priority;
 
312
}
 
313
 
 
314
int 
 
315
LauncherIcon::SortPriority ()
 
316
{
 
317
  return _sort_priority;
 
318
}
 
319
    
 
320
LauncherIconType 
 
321
LauncherIcon::Type ()
 
322
{
 
323
  return _icon_type;
 
324
}
 
325
 
 
326
bool
 
327
LauncherIcon::Visible ()
 
328
{
 
329
  return _visible;
 
330
}
 
331
 
 
332
bool
 
333
LauncherIcon::Active ()
 
334
{
 
335
  return _active;
 
336
}
 
337
 
 
338
bool
 
339
LauncherIcon::Running ()
 
340
{
 
341
  return _running;
 
342
}
 
343
 
 
344
bool
 
345
LauncherIcon::Urgent ()
 
346
{
 
347
  return _urgent;
 
348
}
 
349
 
 
350
int
 
351
LauncherIcon::RelatedWindows ()
 
352
{
 
353
  return _related_windows;
 
354
}
 
355
 
 
356
std::list<DbusmenuClient *> LauncherIcon::Menus ()
 
357
{
 
358
  return GetMenus ();
 
359
}
 
360
 
 
361
std::list<DbusmenuClient *> LauncherIcon::GetMenus ()
 
362
{
 
363
  std::list<DbusmenuClient *> result;
 
364
  return result;
 
365
}