~canonical-dx-team/unity/unity.fix-ql-losing-focus

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
/*
 * 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>
 *              Neil Jagdish Patel <neil.patel@canonical.com>
 *
 */

#include "PlacesSettings.h"
#include "ubus-server.h"
#include "UBusMessages.h"

#include "PlacesSimpleTile.h"

PlacesSimpleTile::PlacesSimpleTile (const char *icon_name, const char *label, int icon_size, bool defer_icon_loading)
: PlacesTile (NUX_TRACKER_LOCATION),
  _label (NULL),
  _icon (NULL),
  _uri (NULL)
{
  nux::VLayout *layout = new nux::VLayout ("", NUX_TRACKER_LOCATION);

  _label = g_strdup (label);
  _icon = g_strdup (icon_name);

  _icontex = new IconTexture (_icon, icon_size, defer_icon_loading);
  _icontex->SetMinMaxSize (PlacesSettings::GetDefault ()->GetDefaultTileWidth (), icon_size);
  _icontex->SinkReference ();
  AddChild (_icontex);

  _cairotext = new nux::StaticCairoText (_label);
  _cairotext->SinkReference ();
  _cairotext->SetFont ("Ubuntu normal 9");
  _cairotext->SetTextEllipsize (nux::StaticCairoText::NUX_ELLIPSIZE_START);
  _cairotext->SetTextAlignment (nux::StaticCairoText::NUX_ALIGN_CENTRE);
  _cairotext->SetMaximumWidth (140);

  layout->AddLayout (new nux::SpaceLayout (0, 0, 12, 12));
  layout->AddView (_icontex, 0, nux::eCenter, nux::eFull);
  layout->AddLayout (new nux::SpaceLayout (0, 0, 12, 12));
  layout->AddView (_cairotext, 0, nux::eCenter, nux::eFull);

  SetMinMaxSize (160, 128);

  SetLayout (layout);

  OnMouseClick.connect (sigc::mem_fun (this, &PlacesSimpleTile::Clicked));
}


PlacesSimpleTile::~PlacesSimpleTile ()
{
  _icontex->UnReference ();
  _cairotext->UnReference ();

  g_free (_label);
  g_free (_icon);
  g_free (_uri);
}

nux::Geometry
PlacesSimpleTile::GetHighlightGeometry ()
{
  nux::Geometry base = GetGeometry ();
  int width = 0, height = 0;

  _icontex->GetTextureSize (&width, &height);
  
  _highlight_geometry.x = (base.width - width) / 2;
  _highlight_geometry.y = 12;
  _highlight_geometry.width = width;
  _highlight_geometry.height = height;

  return _highlight_geometry;
}

const char *
PlacesSimpleTile::GetLabel ()
{
  return _label;
}

const char *
PlacesSimpleTile::GetIcon ()
{
  return _icon;
}

const char *
PlacesSimpleTile::GetURI ()
{
  return _uri;
}

void
PlacesSimpleTile::SetURI (const char *uri)
{
  if (_uri)
    g_free (_uri);
  
  _uri = NULL;

  if (uri)
    _uri = g_strdup (uri);
}

const gchar*
PlacesSimpleTile::GetName ()
{
	return "PlacesTile";
}

const gchar *
PlacesSimpleTile::GetChildsName ()
{
  return "PlacesTileContents";
}

void
PlacesSimpleTile::AddProperties (GVariantBuilder *builder)
{
  nux::Geometry geo = GetGeometry ();

  g_variant_builder_add (builder, "{sv}", "x", g_variant_new_int32 (geo.x));
  g_variant_builder_add (builder, "{sv}", "y", g_variant_new_int32 (geo.y));
  g_variant_builder_add (builder, "{sv}", "width", g_variant_new_int32 (geo.width));
  g_variant_builder_add (builder, "{sv}", "height", g_variant_new_int32 (geo.height));
}

void
PlacesSimpleTile::Clicked (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  if (_uri)
  {
    ubus_server_send_message (ubus_server_get_default (),
                              UBUS_PLACE_TILE_ACTIVATE_REQUEST,
                              g_variant_new_string (_uri));
  }
}

void
PlacesSimpleTile::LoadIcon ()
{
  _icontex->LoadIcon ();

  QueueDraw ();
}