photos-zoom-controls.c 8.88 KB
Newer Older
Debarshi Ray's avatar
Debarshi Ray committed
1 2 3 4
/*
 * Photos - access, organize and share your photos on GNOME
 * Copyright © 2017 Red Hat, Inc.
 *
5 6 7 8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Debarshi Ray's avatar
Debarshi Ray committed
9 10 11 12 13 14 15
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Debarshi Ray's avatar
Debarshi Ray committed
17 18 19 20 21 22 23 24 25
 */


#include "config.h"

#include <string.h>

#include <gio/gio.h>

26
#include "photos-utils.h"
Debarshi Ray's avatar
Debarshi Ray committed
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
#include "photos-zoom-controls.h"


struct _PhotosZoomControls
{
  GtkBin parent_instance;
  GAction *zoom_best_fit_action;
  GAction *zoom_out_action;
  GtkWidget *revealer;
  GtkWidget *zoom_in_button;
  GtkWidget *zoom_out_button;
  GtkWidget *zoom_toggle_button;
  gboolean hover;
  guint notify_enabled_id;
};

enum
{
  PROP_0,
  PROP_HOVER
};


G_DEFINE_TYPE (PhotosZoomControls, photos_zoom_controls, GTK_TYPE_BIN);


static void
photos_zoom_controls_update_buttons (PhotosZoomControls *self)
{
  GtkWidget *image;
  gboolean zoom_best_fit_enabled;
  gboolean zoom_out_enabled;
  const gchar *icon_name;

  zoom_best_fit_enabled = g_action_get_enabled (self->zoom_best_fit_action);
  zoom_out_enabled = g_action_get_enabled (self->zoom_out_action);
  g_return_if_fail (zoom_best_fit_enabled == zoom_out_enabled);

  gtk_revealer_set_reveal_child (GTK_REVEALER (self->revealer), zoom_out_enabled);

  gtk_actionable_set_action_name (GTK_ACTIONABLE (self->zoom_toggle_button), NULL);
  gtk_actionable_set_action_target_value (GTK_ACTIONABLE (self->zoom_toggle_button), NULL);

  if (zoom_out_enabled)
    {
      gtk_actionable_set_action_name (GTK_ACTIONABLE (self->zoom_toggle_button), "app.zoom-best-fit");
    }
  else
    {
76 77 78 79
      GVariant *target_value = NULL;

      target_value = photos_utils_create_zoom_target_value (1.0, PHOTOS_ZOOM_EVENT_MOUSE_CLICK);
      gtk_actionable_set_action_target_value (GTK_ACTIONABLE (self->zoom_toggle_button), target_value);
Debarshi Ray's avatar
Debarshi Ray committed
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
      gtk_actionable_set_action_name (GTK_ACTIONABLE (self->zoom_toggle_button), "app.zoom-in");
    }

  icon_name = zoom_out_enabled ? "zoom-fit-best-symbolic" : "zoom-in-symbolic";
  image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_INVALID);
  gtk_image_set_pixel_size (GTK_IMAGE (image), 16);
  gtk_button_set_image (GTK_BUTTON (self->zoom_toggle_button), image);
}


static gboolean
photos_zoom_controls_notify_idle (gpointer user_data)
{
  PhotosZoomControls *self = PHOTOS_ZOOM_CONTROLS (user_data);

  self->notify_enabled_id = 0;
  photos_zoom_controls_update_buttons (self);
  return G_SOURCE_REMOVE;
}


static void
photos_zoom_controls_notify_enabled (PhotosZoomControls *self)
{
  if (self->notify_enabled_id != 0)
    return;

  self->notify_enabled_id = g_idle_add (photos_zoom_controls_notify_idle, self);
}


static void
photos_zoom_controls_set_hover (PhotosZoomControls *self, gboolean hover)
{
  if (self->hover == hover)
    return;

  self->hover = hover;
  g_object_notify (G_OBJECT (self), "hover");
}


static gboolean
photos_zoom_controls_enter_notify (GtkWidget *widget, GdkEventCrossing *event)
{
  PhotosZoomControls *self = PHOTOS_ZOOM_CONTROLS (widget);

  if (event->detail != GDK_NOTIFY_INFERIOR)
    photos_zoom_controls_set_hover (self, TRUE);

  return GDK_EVENT_PROPAGATE;
}


static gboolean
photos_zoom_controls_leave_notify (GtkWidget *widget, GdkEventCrossing *event)
{
  PhotosZoomControls *self = PHOTOS_ZOOM_CONTROLS (widget);

  if (event->detail != GDK_NOTIFY_INFERIOR)
    photos_zoom_controls_set_hover (self, FALSE);

  return GDK_EVENT_PROPAGATE;
}


static void
photos_zoom_controls_realize (GtkWidget *widget)
{
  GdkVisual *visual;
150
  g_autoptr (GdkWindow) window = NULL;
Debarshi Ray's avatar
Debarshi Ray committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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
  GdkWindow *parent_window;
  GdkWindowAttr attributes;
  GtkAllocation allocation;
  gint attributes_mask;

  gtk_widget_set_realized (widget, TRUE);

  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_BUTTON_PRESS_MASK
                            | GDK_BUTTON_RELEASE_MASK
                            | GDK_ENTER_NOTIFY_MASK
                            | GDK_LEAVE_NOTIFY_MASK);

  gtk_widget_get_allocation (widget, &allocation);
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;

  attributes.wclass = GDK_INPUT_OUTPUT;

  visual = gtk_widget_get_visual (widget);
  attributes.visual = visual;

  attributes.window_type = GDK_WINDOW_CHILD;

  attributes_mask = GDK_WA_VISUAL | GDK_WA_X | GDK_WA_Y;

  parent_window = gtk_widget_get_parent_window (widget);
  window = gdk_window_new (parent_window, &attributes, attributes_mask);
  gtk_widget_set_window (widget, g_object_ref (window));
  gtk_widget_register_window (widget, window);
}


static void
photos_zoom_controls_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
  GdkWindow *window;

  GTK_WIDGET_CLASS (photos_zoom_controls_parent_class)->size_allocate (widget, allocation);

  if (!gtk_widget_get_realized (widget))
    return;

  window = gtk_widget_get_window (widget);
  gdk_window_move_resize (window, allocation->x, allocation->y, allocation->width, allocation->height);
}


static void
photos_zoom_controls_dispose (GObject *object)
{
  PhotosZoomControls *self = PHOTOS_ZOOM_CONTROLS (object);

  if (self->notify_enabled_id != 0)
    {
      g_source_remove (self->notify_enabled_id);
      self->notify_enabled_id = 0;
    }

  G_OBJECT_CLASS (photos_zoom_controls_parent_class)->dispose (object);
}


static void
photos_zoom_controls_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  PhotosZoomControls *self = PHOTOS_ZOOM_CONTROLS (object);

  switch (prop_id)
    {
    case PROP_HOVER:
      g_value_set_boolean (value, self->hover);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


static void
photos_zoom_controls_init (PhotosZoomControls *self)
{
  GApplication *app;

  app = g_application_get_default ();

  gtk_widget_init_template (GTK_WIDGET (self));
  gtk_widget_set_has_window (GTK_WIDGET (self), TRUE);

  self->zoom_best_fit_action = g_action_map_lookup_action (G_ACTION_MAP (app), "zoom-best-fit");
  g_signal_connect_swapped (self->zoom_best_fit_action,
                            "notify::enabled",
                            G_CALLBACK (photos_zoom_controls_notify_enabled),
                            self);

  self->zoom_out_action = g_action_map_lookup_action (G_ACTION_MAP (app), "zoom-out");
  g_signal_connect_swapped (self->zoom_out_action,
                            "notify::enabled",
                            G_CALLBACK (photos_zoom_controls_notify_enabled),
                            self);

  photos_zoom_controls_update_buttons (self);
}


static void
photos_zoom_controls_class_init (PhotosZoomControlsClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->dispose = photos_zoom_controls_dispose;
  object_class->get_property = photos_zoom_controls_get_property;

  widget_class->enter_notify_event = photos_zoom_controls_enter_notify;
  widget_class->leave_notify_event = photos_zoom_controls_leave_notify;
  widget_class->realize = photos_zoom_controls_realize;
  widget_class->size_allocate = photos_zoom_controls_size_allocate;

  g_object_class_install_property (object_class,
                                   PROP_HOVER,
                                   g_param_spec_boolean ("hover",
                                                         "Hover",
                                                         "Whether the widget is hovered",
                                                         FALSE,
                                                         G_PARAM_EXPLICIT_NOTIFY |
                                                         G_PARAM_READABLE |
                                                         G_PARAM_STATIC_STRINGS));

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Photos/zoom-controls.ui");
  gtk_widget_class_bind_template_child (widget_class, PhotosZoomControls, revealer);
  gtk_widget_class_bind_template_child (widget_class, PhotosZoomControls, zoom_in_button);
  gtk_widget_class_bind_template_child (widget_class, PhotosZoomControls, zoom_out_button);
  gtk_widget_class_bind_template_child (widget_class, PhotosZoomControls, zoom_toggle_button);
}


GtkWidget *
photos_zoom_controls_new (void)
{
  return g_object_new (PHOTOS_TYPE_ZOOM_CONTROLS, NULL);
}


gboolean
photos_zoom_controls_get_hover (PhotosZoomControls *self)
{
  g_return_val_if_fail (PHOTOS_IS_ZOOM_CONTROLS (self), FALSE);
  return self->hover;
}