~unity-team/unity/trunk

1468.2.1 by Tim Penhey
Add the nux object type macros, and changed the textures in the TextureContainer to be smart pointers.
1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
1334.1.8 by Gord Allott
pre-previews merge
2
/*
3
 * Copyright 2011 Canonical Ltd.
4
 *
5
 * This program is free software: you can redistribute it and/or modify it
6
 * under the terms of the GNU Lesser General Public License version 3, as
7
 * published by the  Free Software Foundation.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranties of
11
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
13
 * License for more details.
14
 *
15
 * You should have received a copy of both the GNU Lesser General Public
16
 * License version 3 along with this program.  If not, see
17
 * <http://www.gnu.org/licenses/>
18
 *
19
 * Authored by: Gordon Allott <gord.allott@canonical.com>
20
 *
21
 */
22
23
#include "ResultRendererTile.h"
1455.3.4 by Tim Penhey
Update call-sites and remove the block timer.
24
1334.1.8 by Gord Allott
pre-previews merge
25
#include <pango/pangocairo.h>
26
1527.3.1 by Tim Penhey
Add count check.
27
#include <NuxCore/Logger.h>
1405.1.8 by Neil Jagdish Patel
Some style fixes, and actually set the default blooming font options
28
#include <UnityCore/GLibWrapper.h>
2950.2.1 by Nick Dedekind
Dash result dnd image now sourced from render icon.
29
#include <NuxGraphics/GdkGraphics.h>
1405.1.8 by Neil Jagdish Patel
Some style fixes, and actually set the default blooming font options
30
2350.2.4 by Gord Allott
shortcuts and unityshell working woo
31
#include "unity-shared/CairoTexture.h"
2350.2.1 by Gord Allott
panel and dash moved to subdirectories
32
#include "unity-shared/DashStyle.h"
33
#include "unity-shared/TextureCache.h"
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
34
#include "unity-shared/RawPixel.h"
3725.7.6 by Marco Trevisan (Treviño)
Unity: Don't use "gtk-xft-dpi" setting for text-scaling, but our own value
35
#include "unity-shared/UnitySettings.h"
1532.3.1 by Gord Allott
quicksave
36
1334.1.8 by Gord Allott
pre-previews merge
37
namespace unity
38
{
2866.1.1 by Tim Penhey
Use the new logging declarations to avoid static initialization of the logger objects.
39
DECLARE_LOGGER(logger, "unity.dash.results");
2938.2.1 by Nick Dedekind
Updated dash animations.
40
1527.3.1 by Tim Penhey
Add count check.
41
namespace
42
{
3725.6.1 by Marco Trevisan (Treviño)
ResultRendererTile: use cairo device scaling for scaling elements
43
const std::string DEFAULT_GICON = ". GThemedIcon text-x-preview";
44
const RawPixel PADDING    =  6_em;
45
const RawPixel SPACING    = 10_em;
46
const int FONT_SIZE       = 10;
47
const int FONT_MULTIPLIER = 1024;
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
48
49
char const REPLACEMENT_CHAR = '?';
50
float const CORNER_HIGHTLIGHT_RADIUS = 2.0f;
2938.2.1 by Nick Dedekind
Updated dash animations.
51
3725.7.6 by Marco Trevisan (Treviño)
Unity: Don't use "gtk-xft-dpi" setting for text-scaling, but our own value
52
void RenderTexture(nux::GraphicsEngine& GfxContext,
2938.2.1 by Nick Dedekind
Updated dash animations.
53
                   int x,
54
                   int y,
55
                   int width,
56
                   int height,
57
                   nux::ObjectPtr<nux::IOpenGLBaseTexture> const& texture,
58
                   nux::TexCoordXForm &texxform,
59
                   const nux::Color &color,
60
                   float saturate
61
)
62
{
63
  if (saturate == 1.0)
64
  {
65
    GfxContext.QRP_1Tex(x,
66
                        y,
67
                        width,
68
                        height,
69
                        texture,
70
                        texxform,
71
                        color);
72
  }
73
  else
74
  {
75
    GfxContext.QRP_TexDesaturate(x,
76
                                 y,
77
                                 width,
78
                                 height,
79
                                 texture,
80
                                 texxform,
81
                                 color,
82
                                 saturate);
83
  }
84
}
85
1527.3.1 by Tim Penhey
Add count check.
86
}
87
1334.1.8 by Gord Allott
pre-previews merge
88
namespace dash
89
{
1527.3.5 by Tim Penhey
Count the containers.
90
1468.2.1 by Tim Penhey
Add the nux object type macros, and changed the textures in the TextureContainer to be smart pointers.
91
NUX_IMPLEMENT_OBJECT_TYPE(ResultRendererTile);
1334.1.8 by Gord Allott
pre-previews merge
92
93
ResultRendererTile::ResultRendererTile(NUX_FILE_LINE_DECL)
94
  : ResultRenderer(NUX_FILE_LINE_PARAM)
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
95
{
96
  UpdateWidthHeight();
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
97
  scale.changed.connect([this] (double) { UpdateWidthHeight(); });
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
98
}
99
100
void ResultRendererTile::UpdateWidthHeight()
101
{
102
  dash::Style const& style = dash::Style::Instance();
103
  RawPixel tile_width  = style.GetTileWidth();
104
  RawPixel tile_height = style.GetTileHeight();
105
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
106
  width  = tile_width.CP(scale());
107
  height = tile_height.CP(scale());
1334.1.8 by Gord Allott
pre-previews merge
108
}
109
1381.1.4 by Gord Allott
ran the formatter on the results code
110
void ResultRendererTile::Render(nux::GraphicsEngine& GfxContext,
111
                                Result& row,
112
                                ResultRendererState state,
2865.1.1 by Andrea Azzarone
Cleanup.
113
                                nux::Geometry const& geometry,
2938.2.1 by Nick Dedekind
Updated dash animations.
114
                                int x_offset, int y_offset,
115
                                nux::Color const& color,
116
                                float saturate)
1334.1.8 by Gord Allott
pre-previews merge
117
{
1482.1.8 by Gord Allott
only pre-load the tiles that are currently visible
118
  TextureContainer* container = row.renderer<TextureContainer*>();
2314.1.1 by Michal Hruby
Finetune priorities when updating dash results, prevents flickering
119
  if (container == nullptr)
1482.1.8 by Gord Allott
only pre-load the tiles that are currently visible
120
    return;
121
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
122
  dash::Style const& style = dash::Style::Instance();
123
  RawPixel const tile_size   = style.GetTileImageSize();
124
  RawPixel const tile_width  = style.GetTileWidth();
125
  RawPixel const tile_height = style.GetTileHeight();
126
  RawPixel const tile_highlight_width  = style.GetTileIconHightlightWidth();
127
  RawPixel const tile_highlight_height = style.GetTileIconHightlightHeight();
128
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
129
  int tile_icon_size = tile_size.CP(scale());
1334.1.13 by Gord Allott
adds tile blur
130
131
  // set up our texture mode
132
  nux::TexCoordXForm texxform;
2254.4.1 by Thomi Richards
ResultRenderTile no longer uses an uninitialised variable to scale icons.
133
2314.1.1 by Michal Hruby
Finetune priorities when updating dash results, prevents flickering
134
  int icon_width, icon_height;
135
  if (container->icon == nullptr)
136
  {
137
    icon_width = icon_height = tile_icon_size;
138
  }
139
  else
140
  {
141
    icon_width = container->icon->GetWidth();
142
    icon_height = container->icon->GetHeight();
143
  }
144
145
  int icon_left_hand_side = geometry.x + (geometry.width - icon_width) / 2;
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
146
  int icon_top_side = geometry.y + PADDING.CP(scale()) + (tile_icon_size - icon_height) / 2;
1532.3.3 by Gord Allott
changed dash icon size from 42 to 64
147
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
148
  // render highlight if its needed
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
149
  if (container->prelight && state != ResultRendererState::RESULT_RENDERER_NORMAL)
1334.1.9 by Gord Allott
added previews
150
  {
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
151
    int highlight_x =  (geometry.x + geometry.width/2) - tile_highlight_width.CP(scale())/2;
152
    int highlight_y =  (geometry.y + PADDING.CP(scale()) + tile_icon_size / 2) - tile_highlight_height.CP(scale())/2;
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
153
2938.2.1 by Nick Dedekind
Updated dash animations.
154
    RenderTexture(GfxContext,
155
                  highlight_x,
156
                  highlight_y,
157
                  container->prelight->GetWidth(),
158
                  container->prelight->GetHeight(),
159
                  container->prelight->GetDeviceTexture(),
160
                  texxform,
161
                  color,
162
                  saturate);
1334.1.9 by Gord Allott
added previews
163
  }
164
1334.1.8 by Gord Allott
pre-previews merge
165
  // draw the icon
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
166
  if (container->icon)
1334.1.8 by Gord Allott
pre-previews merge
167
  {
2938.2.1 by Nick Dedekind
Updated dash animations.
168
    RenderTexture(GfxContext,
169
                  icon_left_hand_side,
170
                  icon_top_side,
171
                  container->icon->GetWidth(),
172
                  container->icon->GetHeight(),
173
                  container->icon->GetDeviceTexture(),
174
                  texxform,
175
                  color,
176
                  saturate);
1334.1.8 by Gord Allott
pre-previews merge
177
  }
178
1532.3.3 by Gord Allott
changed dash icon size from 42 to 64
179
  if (container->text)
180
  {
2938.2.1 by Nick Dedekind
Updated dash animations.
181
    RenderTexture(GfxContext,
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
182
                  geometry.x + PADDING.CP(scale()),
183
                  geometry.y + tile_icon_size + SPACING.CP(scale()),
184
                  tile_width.CP(scale()) - (PADDING.CP(scale()) * 2),
185
                  tile_height.CP(scale()) - tile_icon_size - SPACING.CP(scale()),
2938.2.1 by Nick Dedekind
Updated dash animations.
186
                  container->text->GetDeviceTexture(),
187
                  texxform,
188
                  color,
189
                  saturate);
1532.3.3 by Gord Allott
changed dash icon size from 42 to 64
190
  }
1334.1.8 by Gord Allott
pre-previews merge
191
}
192
1468.2.12 by Tim Penhey
Fix the TextureCache callsites.
193
nux::BaseTexture* ResultRendererTile::DrawHighlight(std::string const& texid, int width, int height)
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
194
{
1455.1.1 by Tim Penhey
Fix the numerous leaks in CreateBlurredTextureCallback
195
  nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, width, height);
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
196
  cairo_surface_set_device_scale(cairo_graphics.GetSurface(), scale(), scale());
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
197
  cairo_t* cr = cairo_graphics.GetInternalContext();
198
199
  cairo_scale(cr, 1.0f, 1.0f);
200
201
  cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.0);
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
202
  cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
203
  cairo_paint(cr);
204
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
205
  // draw the highlight
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
206
  cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
207
  cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 0.2f);
1455.1.1 by Tim Penhey
Fix the numerous leaks in CreateBlurredTextureCallback
208
  cairo_graphics.DrawRoundedRectangle(cr,
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
209
                                      1.0f,
210
                                      0.0f,
211
                                      0.0f,
212
                                      CORNER_HIGHTLIGHT_RADIUS,
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
213
                                      width/scale(),
214
                                      height/scale(),
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
215
                                      false);
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
216
  cairo_fill(cr);
217
1468.2.12 by Tim Penhey
Fix the TextureCache callsites.
218
  return texture_from_cairo_graphics(cairo_graphics);
1334.1.12 by Gord Allott
nicer highlight renderering - a few fixes
219
}
220
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
221
int ResultRendererTile::Padding() const
222
{
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
223
  return PADDING.CP(scale());
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
224
}
225
3008.2.1 by Nick Dedekind
100 scopes work
226
void ResultRendererTile::Preload(Result const& row)
1334.1.8 by Gord Allott
pre-previews merge
227
{
1482.1.8 by Gord Allott
only pre-load the tiles that are currently visible
228
  if (row.renderer<TextureContainer*>() == nullptr)
229
  {
3008.2.1 by Nick Dedekind
100 scopes work
230
    // Shouldn't really do this, but it's safe in this case and quicker than making a copy.
231
    const_cast<Result&>(row).set_renderer(new TextureContainer());
1482.1.8 by Gord Allott
only pre-load the tiles that are currently visible
232
    LoadIcon(row);
233
    LoadText(row);
234
  }
1334.1.8 by Gord Allott
pre-previews merge
235
}
236
3680.8.6 by Brandon Schaefer
* Reload the icons when the scale changes.
237
void ResultRendererTile::ReloadResult(Result const& row)
238
{
239
  Unload(row);
240
241
  if (row.renderer<TextureContainer*>() == nullptr)
242
    const_cast<Result&>(row).set_renderer(new TextureContainer());
243
244
  LoadIcon(row);
245
  LoadText(row);
246
}
247
3008.2.1 by Nick Dedekind
100 scopes work
248
void ResultRendererTile::Unload(Result const& row)
1334.1.8 by Gord Allott
pre-previews merge
249
{
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
250
  TextureContainer *container = row.renderer<TextureContainer*>();
3008.2.1 by Nick Dedekind
100 scopes work
251
  if (container)
252
  {
253
    delete container;
254
    // Shouldn't really do this, but it's safe in this case and quicker than making a copy.
255
    const_cast<Result&>(row).set_renderer<TextureContainer*>(nullptr);
256
  }
1334.1.8 by Gord Allott
pre-previews merge
257
}
258
2950.2.2 by Nick Dedekind
Fixed up some const correctness.
259
nux::NBitmapData* ResultRendererTile::GetDndImage(Result const& row) const
2950.2.1 by Nick Dedekind
Dash result dnd image now sourced from render icon.
260
{
261
  TextureContainer* container = row.renderer<TextureContainer*>();
262
  nux::NBitmapData* bitmap = nullptr;
263
264
  if (container && container->drag_icon && container->drag_icon.IsType(GDK_TYPE_PIXBUF))
265
  {
266
    // Need to ref the drag icon because GdkGraphics will unref it.
267
    nux::GdkGraphics graphics(GDK_PIXBUF(g_object_ref(container->drag_icon)));
268
    bitmap = graphics.GetBitmap();
269
  }
270
  return bitmap ? bitmap : ResultRenderer::GetDndImage(row);
271
}
272
3008.2.1 by Nick Dedekind
100 scopes work
273
void ResultRendererTile::LoadIcon(Result const& row)
1334.1.8 by Gord Allott
pre-previews merge
274
{
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
275
  Style const& style = Style::Instance();
276
  RawPixel const tile_size   = style.GetTileImageSize();
277
  RawPixel const tile_gsize  = style.GetTileGIconSize();
278
  RawPixel const tile_highlight_width  = style.GetTileIconHightlightWidth();
279
  RawPixel const tile_highlight_height = style.GetTileIconHightlightHeight();
280
3725.6.1 by Marco Trevisan (Treviño)
ResultRendererTile: use cairo device scaling for scaling elements
281
  std::string const& icon_hint = row.icon_hint;
282
  std::string const& icon_name = !icon_hint.empty() ? icon_hint : DEFAULT_GICON;
1334.1.8 by Gord Allott
pre-previews merge
283
2680.1.2 by Michal Hruby
Allow specifying both max width and height when calling IconLoader
284
  glib::Object<GIcon> icon(g_icon_new_for_string(icon_name.c_str(), NULL));
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
285
  TextureContainer* container = row.renderer<TextureContainer*>();
286
3008.51.4 by Nick Dedekind
More scope focus navigation
287
  if (container)
288
  {
289
    TextureCache& cache = TextureCache::GetDefault();
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
290
    BaseTexturePtr texture_prelight(cache.FindTexture("resultview_prelight",
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
291
                                                      tile_highlight_width.CP(scale()),
292
                                                      tile_highlight_height.CP(scale()),
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
293
                                                      sigc::mem_fun(this, &ResultRendererTile::DrawHighlight)));
3008.51.4 by Nick Dedekind
More scope focus navigation
294
    container->prelight = texture_prelight;
295
  }
296
1473.1.1 by Gord Allott
removes the useless uri from iconloading
297
  IconLoader::IconLoaderCallback slot = sigc::bind(sigc::mem_fun(this, &ResultRendererTile::IconLoaded), icon_hint, row);
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
298
2680.1.2 by Michal Hruby
Allow specifying both max width and height when calling IconLoader
299
  if (icon.IsType(G_TYPE_ICON))
300
  {
2680.1.4 by Michal Hruby
Merge with Nick's results size branch
301
    bool use_large_icon = icon.IsType(G_TYPE_FILE_ICON) || !icon.IsType(G_TYPE_THEMED_ICON);
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
302
    container->slot_handle = IconLoader::GetDefault().LoadFromGIconString(icon_name, 
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
303
                                                                          tile_size.CP(scale()),
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
304
                                                                          use_large_icon ?
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
305
                                                                          tile_size.CP(scale()) : tile_gsize.CP(scale()), slot);
1572.3.1 by Mikkel Kamstrup Erlandsen
Fix leak of GIcon in ResultRendererTile. It was leaked when loading from a URI or icon name.
306
  }
307
  else
308
  {
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
309
    container->slot_handle = IconLoader::GetDefault().LoadFromIconName(icon_name, -1, tile_gsize.CP(scale()), slot);
1572.3.1 by Mikkel Kamstrup Erlandsen
Fix leak of GIcon in ResultRendererTile. It was leaked when loading from a URI or icon name.
310
  }
1334.1.8 by Gord Allott
pre-previews merge
311
}
312
1468.2.12 by Tim Penhey
Fix the TextureCache callsites.
313
nux::BaseTexture* ResultRendererTile::CreateTextureCallback(std::string const& texid,
314
                                                            int width,
315
                                                            int height,
2364.6.2 by Marco Trevisan (Treviño)
IconLoader: use glib::Object<GdkPixbuf> in callback slot too
316
                                                            glib::Object<GdkPixbuf> const& pixbuf)
1334.1.8 by Gord Allott
pre-previews merge
317
{
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
318
  int pixbuf_width, pixbuf_height;
319
  pixbuf_width = gdk_pixbuf_get_width(pixbuf);
320
  pixbuf_height = gdk_pixbuf_get_height(pixbuf);
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
321
  if (G_UNLIKELY(!pixbuf_height || !pixbuf_width))
322
  {
2254.4.1 by Thomi Richards
ResultRenderTile no longer uses an uninitialised variable to scale icons.
323
    LOG_ERROR(logger) << "Pixbuf: " << texid << " has a zero height/width: "
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
324
                      << width << "," << height;
325
    pixbuf_width = (pixbuf_width) ? pixbuf_width : 1; // no zeros please
326
    pixbuf_height = (pixbuf_height) ? pixbuf_height: 1; // no zeros please
327
  }
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
328
329
  if (pixbuf_width == pixbuf_height)
330
  {
331
    // quick path for square icons
332
    return nux::CreateTexture2DFromPixbuf(pixbuf, true);
333
  }
334
  else
335
  {
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
336
    Style const& style = Style::Instance();
337
    RawPixel const tile_size = style.GetTileImageSize();
338
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
339
    // slow path for non square icons that must be resized to fit in the square
340
    // texture
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
341
    float aspect = static_cast<float>(pixbuf_height) / pixbuf_width; // already sanitized width/height so can not be 0.0
342
    if (aspect < 1.0f)
343
    {
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
344
      pixbuf_width = tile_size.CP(scale());
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
345
      pixbuf_height = pixbuf_width * aspect;
346
347
      if (pixbuf_height > height)
348
      {
349
        // scaled too big, scale down
350
        pixbuf_height = height;
351
        pixbuf_width = pixbuf_height / aspect;
352
      }
353
    }
354
    else
355
    {
356
      pixbuf_height = height;
357
      pixbuf_width = pixbuf_height / aspect;
358
    }
359
2328.4.2 by Michal Hruby
Fix scale inversion when processing non-rect tile textures
360
    if (gdk_pixbuf_get_height(pixbuf) == pixbuf_height)
361
    {
362
      // we changed our mind, fast path is good
363
      return nux::CreateTexture2DFromPixbuf(pixbuf, true);
364
    }
365
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
366
    nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, pixbuf_width, pixbuf_height);
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
367
    cairo_surface_set_device_scale(cairo_graphics.GetSurface(), scale(), scale());
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
368
    cairo_t* cr = cairo_graphics.GetInternalContext();
369
370
    cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
371
    cairo_paint(cr);
372
2328.4.2 by Michal Hruby
Fix scale inversion when processing non-rect tile textures
373
    float scale = float(pixbuf_height) / gdk_pixbuf_get_height(pixbuf);
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
374
2225.1.1 by Gord Allott
Fixes a long standing slight visual bug in the dash, widescreen icons now render correctly
375
    //cairo_translate(cr,
376
    //                static_cast<int>((width - (pixbuf_width * scale)) * 0.5),
377
    //                static_cast<int>((height - (pixbuf_height * scale)) * 0.5));
1623.1.1 by Gord Allott
makes sure icon tiles are aspect ratio correct
378
379
    cairo_scale(cr, scale, scale);
380
381
    cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
382
    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
383
    cairo_paint(cr);
384
385
    return texture_from_cairo_graphics(cairo_graphics);
386
  }
1334.1.8 by Gord Allott
pre-previews merge
387
}
388
1455.3.7 by Tim Penhey
Merge trunk (and design).
389
void ResultRendererTile::IconLoaded(std::string const& texid,
2680.1.2 by Michal Hruby
Allow specifying both max width and height when calling IconLoader
390
                                    int max_width,
391
                                    int max_height,
2364.6.2 by Marco Trevisan (Treviño)
IconLoader: use glib::Object<GdkPixbuf> in callback slot too
392
                                    glib::Object<GdkPixbuf> const& pixbuf,
1455.3.7 by Tim Penhey
Merge trunk (and design).
393
                                    std::string icon_name,
3008.2.1 by Nick Dedekind
100 scopes work
394
                                    Result const& row)
1334.1.8 by Gord Allott
pre-previews merge
395
{
1468.2.5 by Tim Penhey
Making sure that all the TextureContainer textures get assigned with the correct reference count.
396
  TextureContainer *container = row.renderer<TextureContainer*>();
2676.4.1 by Nick Dedekind
Updated dash result highlight focus to 106x106 pixels with 20% white opacity. Increased dash result file image size to 96x96.
397
1468.2.13 by Tim Penhey
Merge trunk.
398
  if (pixbuf && container)
1334.1.8 by Gord Allott
pre-previews merge
399
  {
1468.2.12 by Tim Penhey
Fix the TextureCache callsites.
400
    TextureCache& cache = TextureCache::GetDefault();
2680.1.5 by Michal Hruby
It's better when it compiles
401
    BaseTexturePtr texture(cache.FindTexture(icon_name, max_width, max_height,
1532.3.3 by Gord Allott
changed dash icon size from 42 to 64
402
                           sigc::bind(sigc::mem_fun(this, &ResultRendererTile::CreateTextureCallback), pixbuf)));
1334.1.13 by Gord Allott
adds tile blur
403
1468.2.13 by Tim Penhey
Merge trunk.
404
    container->icon = texture;
2950.2.1 by Nick Dedekind
Dash result dnd image now sourced from render icon.
405
    container->drag_icon = pixbuf;
1334.1.8 by Gord Allott
pre-previews merge
406
1468.2.13 by Tim Penhey
Merge trunk.
407
    NeedsRedraw.emit();
1527.4.2 by Gord Allott
fixes icon renderer so that it returns a null reference when it can't find a file rather than silently quitting, so we can use that codepath instead of stating files all the live long day
408
409
    if (container)
410
      container->slot_handle = 0;
411
  }
1608.1.2 by Gord Allott
add protection in icon loading
412
  else if (container)
1527.4.2 by Gord Allott
fixes icon renderer so that it returns a null reference when it can't find a file rather than silently quitting, so we can use that codepath instead of stating files all the live long day
413
  {
414
    // we need to load a missing icon
415
    IconLoader::IconLoaderCallback slot = sigc::bind(sigc::mem_fun(this, &ResultRendererTile::IconLoaded), icon_name, row);
2680.1.4 by Michal Hruby
Merge with Nick's results size branch
416
    container->slot_handle = IconLoader::GetDefault().LoadFromGIconString(". GThemedIcon text-x-preview", max_width, max_height, slot);
1527.4.2 by Gord Allott
fixes icon renderer so that it returns a null reference when it can't find a file rather than silently quitting, so we can use that codepath instead of stating files all the live long day
417
  }
1334.1.8 by Gord Allott
pre-previews merge
418
}
419
3571.1.2 by Brandon Schaefer
* Make the name more abstract, in case there are other languages we need to blacklist
420
/* Blacklisted unicode ranges:
421
 * Burmese:  U+1000 -> U+109F
422
 * Extended: U+AA60 -> U+AA7B
423
*/
424
bool IsBlacklistedChar(gunichar uni_c)
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
425
{
426
  if ((uni_c >= 0x1000 && uni_c <= 0x109F) ||
427
      (uni_c >= 0xAA60 && uni_c >= 0xAA7B))
428
  {
429
    return true;
430
  }
431
432
  return false;
433
}
434
3571.1.2 by Brandon Schaefer
* Make the name more abstract, in case there are other languages we need to blacklist
435
// FIXME Bug (lp.1239381) in the backend of pango that crashes
436
// when using ellipsize with/height setting in pango
437
std::string ReplaceBlacklistedChars(std::string const& str)
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
438
{
439
  std::string new_string("");
440
3571.1.5 by Brandon Schaefer
* Fix very bad utf8 handling...thnaks bregma!
441
  if (!g_utf8_validate(str.c_str(), -1, NULL))
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
442
    return new_string;
443
444
  gchar const* uni_s = str.c_str();
445
  gunichar uni_c;
3571.1.6 by Brandon Schaefer
* Use *next_char
446
  gchar utf8_buff[6];
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
447
3571.1.6 by Brandon Schaefer
* Use *next_char
448
  int size = g_utf8_strlen(uni_s, -1);
449
  for (int i = 0; i < size; ++i)
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
450
  {
3574.1.1 by Brandon Schaefer
* Get the current char before moving the pointer down to the next char
451
    uni_c = g_utf8_get_char(uni_s);
3571.1.6 by Brandon Schaefer
* Use *next_char
452
    uni_s = g_utf8_next_char(uni_s);
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
453
3571.1.2 by Brandon Schaefer
* Make the name more abstract, in case there are other languages we need to blacklist
454
    if (IsBlacklistedChar(uni_c))
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
455
    {
456
      new_string += REPLACEMENT_CHAR;
457
    }
458
    else
459
    {
3571.1.6 by Brandon Schaefer
* Use *next_char
460
      int end = g_unichar_to_utf8(uni_c, utf8_buff);
461
      utf8_buff[end] = '\0';
462
463
      new_string += utf8_buff;
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
464
    }
465
  }
466
467
  return new_string;
468
}
1334.1.13 by Gord Allott
adds tile blur
469
3008.2.1 by Nick Dedekind
100 scopes work
470
void ResultRendererTile::LoadText(Result const& row)
1334.1.8 by Gord Allott
pre-previews merge
471
{
3680.8.4 by Brandon Schaefer
* Get the icons working per monitor!
472
  Style const& style = Style::Instance();
473
  RawPixel const tile_size   = style.GetTileImageSize();
474
  RawPixel const tile_width  = style.GetTileWidth();
475
  RawPixel const tile_height = style.GetTileHeight();
476
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
477
  nux::CairoGraphics _cairoGraphics(CAIRO_FORMAT_ARGB32,
3725.6.2 by Marco Trevisan (Treviño)
Dash: use nux::Property for scale
478
                                    tile_width.CP(scale()) - (PADDING.CP(scale()) * 2),
479
                                    tile_height.CP(scale()) - tile_size.CP(scale()) - SPACING.CP(scale()));
480
  cairo_surface_set_device_scale(_cairoGraphics.GetSurface(), scale(), scale());
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
481
3725.6.1 by Marco Trevisan (Treviño)
ResultRendererTile: use cairo device scaling for scaling elements
482
  cairo_t* cr = _cairoGraphics.GetInternalContext();
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
483
484
  PangoLayout*          layout     = NULL;
485
  PangoFontDescription* desc       = NULL;
486
  PangoContext*         pango_context   = NULL;
487
  GdkScreen*            screen     = gdk_screen_get_default();    // not ref'ed
488
  glib::String          font;
489
490
  g_object_get(gtk_settings_get_default(), "gtk-font-name", &font, NULL);
491
492
  cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
493
  layout = pango_cairo_create_layout(cr);
494
  desc = pango_font_description_from_string(font.Value());
3725.6.1 by Marco Trevisan (Treviño)
ResultRendererTile: use cairo device scaling for scaling elements
495
  pango_font_description_set_size (desc, FONT_SIZE * FONT_MULTIPLIER);
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
496
497
  pango_layout_set_font_description(layout, desc);
498
  pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
499
500
  pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
501
  pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_START);
3725.6.1 by Marco Trevisan (Treviño)
ResultRendererTile: use cairo device scaling for scaling elements
502
  pango_layout_set_width(layout, (tile_width - (PADDING * 2))* PANGO_SCALE);
1581.1.7 by Gord Allott
remove colours
503
  pango_layout_set_height(layout, -2);
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
504
3571.1.2 by Brandon Schaefer
* Make the name more abstract, in case there are other languages we need to blacklist
505
  // FIXME bug #1239381
3571.1.4 by Brandon Schaefer
* Remove extra string copy
506
  std::string name = ReplaceBlacklistedChars(row.name());
3571.1.1 by Brandon Schaefer
* When dealing with names that are in burmese, we must replace those with '?'.
507
508
  char *escaped_text = g_markup_escape_text(name.c_str(), -1);
1667.4.2 by Gord Allott
fix a gord has heat stroke based leak
509
510
  pango_layout_set_markup(layout, escaped_text, -1);
511
512
  g_free (escaped_text);
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
513
514
  pango_context = pango_layout_get_context(layout);  // is not ref'ed
3725.7.6 by Marco Trevisan (Treviño)
Unity: Don't use "gtk-xft-dpi" setting for text-scaling, but our own value
515
  pango_cairo_context_set_font_options(pango_context, gdk_screen_get_font_options(screen));
516
  pango_cairo_context_set_resolution(pango_context, 96.0 * Settings::Instance().font_scaling());
1457.2.1 by Gord Allott
lots of changes, updated ResultRendererTile to not use a map anywhere, causing IconLoader to support handles in order to disconnect the signals, also removed a warning here and there
517
  pango_layout_context_changed(layout);
518
519
  cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
520
  cairo_paint(cr);
521
522
  cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
523
  cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
524
525
  cairo_move_to(cr, 0.0f, 0.0f);
526
  pango_cairo_show_layout(cr, layout);
527
528
  // clean up
529
  pango_font_description_free(desc);
530
  g_object_unref(layout);
531
532
  TextureContainer *container = row.renderer<TextureContainer*>();
1468.2.13 by Tim Penhey
Merge trunk.
533
  if (container)
534
    container->text = texture_ptr_from_cairo_graphics(_cairoGraphics);
1334.1.8 by Gord Allott
pre-previews merge
535
}
536
537
538
}
539
}