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

« back to all changes in this revision

Viewing changes to src/LauncherIcon.cpp

Import the work done so far with Compiz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Nux/Nux.h"
 
2
#include "Nux/VScrollBar.h"
 
3
#include "Nux/HLayout.h"
 
4
#include "Nux/VLayout.h"
 
5
#include "Nux/MenuPage.h"
 
6
#include "Nux/WindowCompositor.h"
 
7
#include "Nux/BaseWindow.h"
 
8
#include "Nux/MenuPage.h"
 
9
#include "NuxCore/Color.h"
 
10
 
 
11
#include "LauncherIcon.h"
 
12
#include "Launcher.h"
 
13
 
 
14
#define DEFAULT_ICON "empathy"
 
15
 
 
16
LauncherIcon::LauncherIcon(Launcher* IconManager)
 
17
{
 
18
  _folding_angle = 0;
 
19
  m_IconManager = IconManager;
 
20
  m_TooltipText = "blank";
 
21
  _visible = true;
 
22
  _active = false;
 
23
  _running = false;
 
24
  _background_color = nux::Color::White;
 
25
  _mouse_inside = false;
 
26
  _tooltip = new nux::Tooltip ();
 
27
  _icon_type = LAUNCHER_ICON_TYPE_NONE;
 
28
  _sort_priority = 0;
 
29
 
 
30
  MouseEnter.connect (sigc::mem_fun(this, &LauncherIcon::RecvMouseEnter));
 
31
  MouseLeave.connect (sigc::mem_fun(this, &LauncherIcon::RecvMouseLeave));
 
32
}
 
33
 
 
34
LauncherIcon::~LauncherIcon()
 
35
{
 
36
}
 
37
 
 
38
nux::Color LauncherIcon::BackgroundColor ()
 
39
{
 
40
  return _background_color;
 
41
}
 
42
 
 
43
nux::BaseTexture * LauncherIcon::TextureForSize (int size)
 
44
{
 
45
  nux::BaseTexture * result = GetTextureForSize (size);
 
46
  
 
47
  return result;
 
48
}
 
49
 
 
50
nux::Color LauncherIcon::ColorForIcon (GdkPixbuf *pixbuf)
 
51
{
 
52
  unsigned int width = gdk_pixbuf_get_width (pixbuf);
 
53
  unsigned int height = gdk_pixbuf_get_height (pixbuf);
 
54
  unsigned int row_bytes = gdk_pixbuf_get_rowstride (pixbuf);
 
55
  
 
56
  long int rtotal = 0, gtotal = 0, btotal = 0;
 
57
  float total = 0.0f;
 
58
  
 
59
  
 
60
  guchar *img = gdk_pixbuf_get_pixels (pixbuf);
 
61
  
 
62
  for (unsigned int i = 0; i < width; i++)
 
63
  {
 
64
    for (unsigned int j = 0; j < height; j++)
 
65
    {
 
66
      guchar *pixels = img + ( j * row_bytes + i * 4);
 
67
      guchar r = *(pixels + 0);
 
68
      guchar g = *(pixels + 1);
 
69
      guchar b = *(pixels + 2);
 
70
      guchar a = *(pixels + 3);
 
71
      
 
72
      float saturation = (MAX (r, MAX (g, b)) - MIN (r, MIN (g, b))) / 255.0f;
 
73
      float relevance = .1 + .9 * (a / 255.0f) * saturation;
 
74
      
 
75
      rtotal += (guchar) (r * relevance);
 
76
      gtotal += (guchar) (g * relevance);
 
77
      btotal += (guchar) (b * relevance);
 
78
      
 
79
      total += relevance * 255;
 
80
    }
 
81
  }
 
82
  
 
83
  float r, g, b, h, s, v;
 
84
  r = rtotal / total;
 
85
  g = gtotal / total;
 
86
  b = btotal / total;
 
87
  
 
88
  nux::RGBtoHSV (r, g, b, h, s, v);
 
89
  
 
90
  if (s > .15f)
 
91
    s = MAX (s, 0.5f);
 
92
  v = .8f;
 
93
  
 
94
  nux::HSVtoRGB (r, g, b, h, s, v);
 
95
  
 
96
  return nux::Color (r, g, b);
 
97
}
 
98
 
 
99
nux::BaseTexture * LauncherIcon::TextureFromGtkTheme (const char *icon_name, int size)
 
100
{
 
101
  GdkPixbuf *pbuf;
 
102
  GtkIconTheme *theme;
 
103
  GtkIconInfo *info;
 
104
  nux::BaseTexture *result;
 
105
  
 
106
  theme = gtk_icon_theme_get_default ();
 
107
      
 
108
  if (!icon_name)
 
109
    icon_name = g_strdup (DEFAULT_ICON);
 
110
   
 
111
  info = gtk_icon_theme_lookup_icon (theme,
 
112
                                     icon_name,
 
113
                                     size,
 
114
                                     (GtkIconLookupFlags) 0);            
 
115
  if (!info)
 
116
  {
 
117
    info = gtk_icon_theme_lookup_icon (theme,
 
118
                                       DEFAULT_ICON,
 
119
                                       size,
 
120
                                       (GtkIconLookupFlags) 0);
 
121
  }
 
122
        
 
123
  if (gtk_icon_info_get_filename (info) == NULL)
 
124
  {
 
125
    info = gtk_icon_theme_lookup_icon (theme,
 
126
                                       DEFAULT_ICON,
 
127
                                       size,
 
128
                                       (GtkIconLookupFlags) 0);
 
129
  }
 
130
  
 
131
  pbuf = gtk_icon_info_load_icon (info, NULL);
 
132
  result = nux::CreateTextureFromPixbuf (pbuf);
 
133
  
 
134
  _background_color = ColorForIcon (pbuf);
 
135
  
 
136
  g_object_unref (pbuf);
 
137
  
 
138
  return result;
 
139
}
 
140
 
 
141
void LauncherIcon::SetTooltipText(const TCHAR* text)
 
142
{
 
143
    m_TooltipText = text;
 
144
    _tooltip->SetText (m_TooltipText);
 
145
}
 
146
 
 
147
nux::NString LauncherIcon::GetTooltipText()
 
148
{
 
149
    return m_TooltipText;
 
150
}
 
151
 
 
152
void
 
153
LauncherIcon::RecvMouseEnter ()
 
154
{
 
155
  int icon_x = _xform_screen_coord[0].x;
 
156
  int icon_y = _xform_screen_coord[0].y;
 
157
  int icon_w = _xform_screen_coord[2].x - _xform_screen_coord[0].x;
 
158
  int icon_h = _xform_screen_coord[2].y - _xform_screen_coord[0].y;
 
159
 
 
160
  _tooltip->SetBaseX (icon_x + icon_w);
 
161
  _tooltip->SetBaseY (icon_y +
 
162
                      24 + // TODO: HARCODED, replace m_IconManager->GetBaseY ()
 
163
                      (icon_h / 2) -
 
164
                      (_tooltip->GetBaseHeight () / 2));
 
165
  _tooltip->ShowWindow (true);
 
166
}
 
167
 
 
168
void LauncherIcon::RecvMouseLeave ()
 
169
{
 
170
  _tooltip->ShowWindow (false);
 
171
}
 
172
 
 
173
void LauncherIcon::HideTooltip ()
 
174
{
 
175
  _tooltip->ShowWindow (false);
 
176
}
 
177
 
 
178
void
 
179
LauncherIcon::SetVisible (bool visible)
 
180
{
 
181
  if (visible == _visible)
 
182
    return;
 
183
      
 
184
  _visible = visible;
 
185
  
 
186
  if (visible)
 
187
    show.emit (this);
 
188
  else
 
189
    hide.emit (this);
 
190
}
 
191
 
 
192
void
 
193
LauncherIcon::SetActive (bool active)
 
194
{
 
195
  if (active == _active)
 
196
    return;
 
197
    
 
198
  _active = active;
 
199
  needs_redraw.emit (this);
 
200
}
 
201
 
 
202
void LauncherIcon::SetRunning (bool running)
 
203
{
 
204
  if (running == _running)
 
205
    return;
 
206
    
 
207
  _running = running;
 
208
  needs_redraw.emit (this);
 
209
}
 
210
 
 
211
void 
 
212
LauncherIcon::SetIconType (LauncherIconType type)
 
213
{
 
214
  _icon_type = type;
 
215
}
 
216
 
 
217
void 
 
218
LauncherIcon::SetSortPriority (int priority)
 
219
{
 
220
  _sort_priority = priority;
 
221
}
 
222
 
 
223
int 
 
224
LauncherIcon::SortPriority ()
 
225
{
 
226
  return _sort_priority;
 
227
}
 
228
    
 
229
LauncherIconType 
 
230
LauncherIcon::Type ()
 
231
{
 
232
  return _icon_type;
 
233
}
 
234
 
 
235
bool
 
236
LauncherIcon::Visible ()
 
237
{
 
238
  return _visible;
 
239
}
 
240
 
 
241
bool
 
242
LauncherIcon::Active ()
 
243
{
 
244
  return _active;
 
245
}
 
246
 
 
247
bool
 
248
LauncherIcon::Running ()
 
249
{
 
250
  return _running;
 
251
}