dzl-suggestion-row.c 10.2 KB
Newer Older
Christian Hergert's avatar
Christian Hergert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* dzl-suggestion-row.c
 *
 * Copyright (C) 2017 Christian Hergert <chergert@redhat.com>
 *
 * This file is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define G_LOG_DOMAIN "dzl-suggestion-row"

21 22
#include "config.h"

23 24
#include "suggestions/dzl-suggestion-row.h"
#include "util/dzl-macros.h"
Christian Hergert's avatar
Christian Hergert committed
25 26 27 28 29

typedef struct
{
  DzlSuggestion *suggestion;

30 31
  GtkOrientation orientation;

32 33
  gulong         notify_icon_handler;

Christian Hergert's avatar
Christian Hergert committed
34 35 36 37
  GtkImage      *image;
  GtkLabel      *title;
  GtkLabel      *separator;
  GtkLabel      *subtitle;
38
  GtkGrid       *grid;
Christian Hergert's avatar
Christian Hergert committed
39 40 41 42 43
} DzlSuggestionRowPrivate;

enum {
  PROP_0,
  PROP_SUGGESTION,
44
  PROP_ORIENTATION,
Christian Hergert's avatar
Christian Hergert committed
45 46 47
  N_PROPS
};

48 49 50 51
G_DEFINE_TYPE_EXTENDED (DzlSuggestionRow, dzl_suggestion_row, DZL_TYPE_LIST_BOX_ROW, 0,
                        G_ADD_PRIVATE (DzlSuggestionRow)
                        G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))

Christian Hergert's avatar
Christian Hergert committed
52 53 54 55 56 57 58 59 60 61 62 63 64

static GParamSpec *properties [N_PROPS];

static void
dzl_suggestion_row_disconnect (DzlSuggestionRow *self)
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);

  g_return_if_fail (DZL_IS_SUGGESTION_ROW (self));

  if (priv->suggestion == NULL)
    return;

65 66
  dzl_clear_signal_handler (priv->suggestion, &priv->notify_icon_handler);

Christian Hergert's avatar
Christian Hergert committed
67 68 69 70 71 72
  g_object_set (priv->image, "icon-name", NULL, NULL);
  gtk_label_set_label (priv->title, NULL);
  gtk_label_set_label (priv->subtitle, NULL);
}

static void
73 74 75
on_notify_icon_cb (DzlSuggestionRow *self,
                   GParamSpec       *pspec,
                   DzlSuggestion    *suggestion)
Christian Hergert's avatar
Christian Hergert committed
76 77
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);
78
  cairo_surface_t *surface;
Christian Hergert's avatar
Christian Hergert committed
79

80 81
  g_assert (DZL_IS_SUGGESTION_ROW (self));
  g_assert (DZL_IS_SUGGESTION (suggestion));
Christian Hergert's avatar
Christian Hergert committed
82

83
  if ((surface = dzl_suggestion_get_icon_surface (suggestion, GTK_WIDGET (priv->image))))
84 85 86 87 88 89
    {
      gtk_image_set_from_surface (priv->image, surface);
      cairo_surface_destroy (surface);
    }
  else
    {
90
      g_autoptr(GIcon) icon = dzl_suggestion_get_icon (suggestion);
91 92
      gtk_image_set_from_gicon (priv->image, icon, GTK_ICON_SIZE_MENU);
    }
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
static void
dzl_suggestion_set_orientation (DzlSuggestionRowPrivate *priv)
{
  const gchar *subtitle;

  subtitle = dzl_suggestion_get_subtitle (priv->suggestion);

  gtk_widget_set_visible (GTK_WIDGET (priv->separator),
                          priv->orientation != GTK_ORIENTATION_VERTICAL);

  g_object_ref (priv->image);
  g_object_ref (priv->title);
  g_object_ref (priv->subtitle);

  gtk_container_remove (GTK_CONTAINER (priv->grid), GTK_WIDGET (priv->image));
  gtk_container_remove (GTK_CONTAINER (priv->grid), GTK_WIDGET (priv->title));
  gtk_container_remove (GTK_CONTAINER (priv->grid), GTK_WIDGET (priv->subtitle));

  if (priv->orientation == GTK_ORIENTATION_VERTICAL)
    {
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->image), 0, 0, 1, 1);
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->title), 1, 0, 1, 1);
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->subtitle), 1, 1, 1, 1);

      gtk_widget_set_visible (GTK_WIDGET (priv->separator), FALSE);
    }
  else
    {
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->image), 0, 0, 1, 2);
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->title), 1, 0, 1, 1);
      gtk_grid_attach (priv->grid, GTK_WIDGET (priv->subtitle), 3, 0, 1, 1);

      gtk_widget_set_visible (GTK_WIDGET (priv->separator), !!subtitle);
    }

  g_object_unref (priv->subtitle);
  g_object_unref (priv->title);
  g_object_unref (priv->image);
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static void
dzl_suggestion_row_connect (DzlSuggestionRow *self)
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);
  const gchar *subtitle;

  g_return_if_fail (DZL_IS_SUGGESTION_ROW (self));
  g_return_if_fail (priv->suggestion != NULL);

  priv->notify_icon_handler =
    g_signal_connect_object (priv->suggestion,
                             "notify::icon",
                             G_CALLBACK (on_notify_icon_cb),
                             self,
                             G_CONNECT_SWAPPED);

  on_notify_icon_cb (self, NULL, priv->suggestion);
152

Christian Hergert's avatar
Christian Hergert committed
153 154 155 156
  gtk_label_set_label (priv->title, dzl_suggestion_get_title (priv->suggestion));

  subtitle = dzl_suggestion_get_subtitle (priv->suggestion);
  gtk_label_set_label (priv->subtitle, subtitle);
157

158
  dzl_suggestion_set_orientation (priv);
Christian Hergert's avatar
Christian Hergert committed
159 160 161
}

static void
162
dzl_suggestion_row_dispose (GObject *object)
Christian Hergert's avatar
Christian Hergert committed
163 164 165 166
{
  DzlSuggestionRow *self = (DzlSuggestionRow *)object;
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);

167 168 169 170 171
  if (priv->suggestion != NULL)
    {
      dzl_suggestion_row_disconnect (self);
      g_clear_object (&priv->suggestion);
    }
Christian Hergert's avatar
Christian Hergert committed
172

173
  G_OBJECT_CLASS (dzl_suggestion_row_parent_class)->dispose (object);
Christian Hergert's avatar
Christian Hergert committed
174 175 176 177 178 179 180 181 182
}

static void
dzl_suggestion_row_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  DzlSuggestionRow *self = DZL_SUGGESTION_ROW (object);
183
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);
Christian Hergert's avatar
Christian Hergert committed
184 185 186 187 188 189 190

  switch (prop_id)
    {
    case PROP_SUGGESTION:
      g_value_set_object (value, dzl_suggestion_row_get_suggestion (self));
      break;

191 192 193
    case PROP_ORIENTATION:
      g_value_set_enum (value, priv->orientation);
      break;
194

Christian Hergert's avatar
Christian Hergert committed
195 196 197 198 199 200 201 202 203 204 205 206
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
dzl_suggestion_row_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  DzlSuggestionRow *self = DZL_SUGGESTION_ROW (object);
207
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);
Christian Hergert's avatar
Christian Hergert committed
208 209 210 211 212 213 214

  switch (prop_id)
    {
    case PROP_SUGGESTION:
      dzl_suggestion_row_set_suggestion (self, g_value_get_object (value));
      break;

215
    case PROP_ORIENTATION:
216 217 218
      if (priv->orientation != g_value_get_enum (value))
        {
          priv->orientation = g_value_get_enum (value);
219
          dzl_suggestion_set_orientation (priv);
220
        }
221 222
      break;

Christian Hergert's avatar
Christian Hergert committed
223 224 225 226 227 228 229 230 231 232 233
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
dzl_suggestion_row_class_init (DzlSuggestionRowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

234
  object_class->dispose = dzl_suggestion_row_dispose;
Christian Hergert's avatar
Christian Hergert committed
235 236 237 238 239 240 241 242 243 244
  object_class->get_property = dzl_suggestion_row_get_property;
  object_class->set_property = dzl_suggestion_row_set_property;

  properties [PROP_SUGGESTION] =
    g_param_spec_object ("suggestion",
                         "Suggestion",
                         "The suggestion to display",
                         DZL_TYPE_SUGGESTION,
                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));

245 246 247 248 249 250 251
  properties [PROP_ORIENTATION] =
    g_param_spec_enum ("orientation",
                       "Orientation",
                       "Orientation",
                       GTK_TYPE_ORIENTATION,
                       GTK_ORIENTATION_VERTICAL,
                       (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252

253
   g_object_class_install_properties (object_class, N_PROPS, properties);
Christian Hergert's avatar
Christian Hergert committed
254 255 256 257 258 259

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/dazzle/ui/dzl-suggestion-row.ui");
  gtk_widget_class_bind_template_child_private (widget_class, DzlSuggestionRow, image);
  gtk_widget_class_bind_template_child_private (widget_class, DzlSuggestionRow, title);
  gtk_widget_class_bind_template_child_private (widget_class, DzlSuggestionRow, subtitle);
  gtk_widget_class_bind_template_child_private (widget_class, DzlSuggestionRow, separator);
260
  gtk_widget_class_bind_template_child_private (widget_class, DzlSuggestionRow, grid);
Christian Hergert's avatar
Christian Hergert committed
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
}

static void
dzl_suggestion_row_init (DzlSuggestionRow *self)
{
  GtkStyleContext *context;

  gtk_widget_init_template (GTK_WIDGET (self));

  context = gtk_widget_get_style_context (GTK_WIDGET (self));
  gtk_style_context_add_class (context, "suggestion");
}

/**
 * dzl_suggestion_row_get_suggestion:
 * @self: a #DzlSuggestionRow
 *
 * Gets the suggestion to be displayed.
 *
 * Returns: (transfer none): An #DzlSuggestion
 */
DzlSuggestion *
dzl_suggestion_row_get_suggestion (DzlSuggestionRow *self)
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);

  g_return_val_if_fail (DZL_IS_SUGGESTION_ROW (self), NULL);

  return priv->suggestion;
}

void
dzl_suggestion_row_set_suggestion (DzlSuggestionRow *self,
                                   DzlSuggestion    *suggestion)
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);

  g_return_if_fail (DZL_IS_SUGGESTION_ROW (self));
  g_return_if_fail (!suggestion || DZL_IS_SUGGESTION (suggestion));

  if (priv->suggestion != suggestion)
    {
      if (priv->suggestion != NULL)
        {
          dzl_suggestion_row_disconnect (self);
          g_clear_object (&priv->suggestion);
        }

      if (suggestion != NULL)
        {
          priv->suggestion = g_object_ref (suggestion);
          dzl_suggestion_row_connect (self);
        }

      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SUGGESTION]);
    }
}
318 319 320 321 322 323 324 325 326 327 328 329 330

void
_dzl_suggestion_row_set_ellipsize (DzlSuggestionRow   *self,
                                   PangoEllipsizeMode  title,
                                   PangoEllipsizeMode  subtitle)
{
  DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);

  g_assert (DZL_IS_SUGGESTION_ROW (self));

  gtk_label_set_ellipsize (priv->title, title);
  gtk_label_set_ellipsize (priv->subtitle, subtitle);
}
331 332 333 334 335 336

GtkWidget *
dzl_suggestion_row_new (void)
{
  return g_object_new (DZL_TYPE_SUGGESTION_ROW, NULL);
}