[gnome-flashback] desktop: add GfMonitorView



commit a07a5e194c46d6f6b751a1a4993fab0664e67534
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Thu Oct 31 01:16:21 2019 +0200

    desktop: add GfMonitorView

 gnome-flashback/libdesktop/Makefile.am       |   2 +
 gnome-flashback/libdesktop/gf-icon-view.c    | 107 +++++++++++++++
 gnome-flashback/libdesktop/gf-monitor-view.c | 194 +++++++++++++++++++++++++++
 gnome-flashback/libdesktop/gf-monitor-view.h |  34 +++++
 4 files changed, 337 insertions(+)
---
diff --git a/gnome-flashback/libdesktop/Makefile.am b/gnome-flashback/libdesktop/Makefile.am
index 3a69aa9..d69ba66 100644
--- a/gnome-flashback/libdesktop/Makefile.am
+++ b/gnome-flashback/libdesktop/Makefile.am
@@ -24,6 +24,8 @@ libdesktop_la_SOURCES = \
        gf-desktop.h \
        gf-icon-view.c \
        gf-icon-view.h \
+       gf-monitor-view.c \
+       gf-monitor-view.h \
        $(NULL)
 
 libdesktop_la_LDFLAGS = \
diff --git a/gnome-flashback/libdesktop/gf-icon-view.c b/gnome-flashback/libdesktop/gf-icon-view.c
index eb137bb..c6705f1 100644
--- a/gnome-flashback/libdesktop/gf-icon-view.c
+++ b/gnome-flashback/libdesktop/gf-icon-view.c
@@ -18,6 +18,8 @@
 #include "config.h"
 #include "gf-icon-view.h"
 
+#include "gf-monitor-view.h"
+
 struct _GfIconView
 {
   GtkEventBox  parent;
@@ -27,6 +29,88 @@ struct _GfIconView
 
 G_DEFINE_TYPE (GfIconView, gf_icon_view, GTK_TYPE_EVENT_BOX)
 
+static GtkWidget *
+find_monitor_view_by_monitor (GfIconView *self,
+                              GdkMonitor *monitor)
+{
+  GfMonitorView *view;
+  GList *children;
+  GList *l;
+
+  children = gtk_container_get_children (GTK_CONTAINER (self->fixed));
+
+  for (l = children; l != NULL; l = l->next)
+    {
+      view = GF_MONITOR_VIEW (l->data);
+
+      if (gf_monitor_view_get_monitor (view) == monitor)
+        break;
+
+      view = NULL;
+    }
+
+  g_list_free (children);
+
+  return GTK_WIDGET (view);
+}
+
+static void
+workarea_cb (GdkMonitor *monitor,
+             GParamSpec *pspec,
+             GfIconView *self)
+{
+  GtkWidget *view;
+  GdkRectangle workarea;
+
+  view = find_monitor_view_by_monitor (self, monitor);
+  if (view == NULL)
+    return;
+
+  gdk_monitor_get_workarea (monitor, &workarea);
+
+  gtk_fixed_move (GTK_FIXED (self->fixed), view, workarea.x, workarea.y);
+}
+
+static void
+create_monitor_view (GfIconView *self,
+                     GdkMonitor *monitor)
+{
+  GdkRectangle workarea;
+  GtkWidget *view;
+
+  gdk_monitor_get_workarea (monitor, &workarea);
+
+  view = gf_monitor_view_new (monitor);
+  gtk_fixed_put (GTK_FIXED (self->fixed), view, workarea.x, workarea.y);
+  gtk_widget_show (view);
+
+  g_signal_connect_object (monitor, "notify::workarea",
+                           G_CALLBACK (workarea_cb),
+                           self, 0);
+}
+
+static void
+monitor_added_cb (GdkDisplay *display,
+                  GdkMonitor *monitor,
+                  GfIconView *self)
+{
+  create_monitor_view (self, monitor);
+}
+
+static void
+monitor_removed_cb (GdkDisplay *display,
+                    GdkMonitor *monitor,
+                    GfIconView *self)
+{
+  GtkWidget *view;
+
+  view = find_monitor_view_by_monitor (self, monitor);
+  if (view == NULL)
+    return;
+
+  gtk_widget_destroy (view);
+}
+
 static void
 gf_icon_view_class_init (GfIconViewClass *self_class)
 {
@@ -35,9 +119,32 @@ gf_icon_view_class_init (GfIconViewClass *self_class)
 static void
 gf_icon_view_init (GfIconView *self)
 {
+  GdkDisplay *display;
+  int n_monitors;
+  int i;
+
   self->fixed = gtk_fixed_new ();
   gtk_container_add (GTK_CONTAINER (self), self->fixed);
   gtk_widget_show (self->fixed);
+
+  display = gdk_display_get_default ();
+  n_monitors = gdk_display_get_n_monitors (display);
+
+  g_signal_connect_object (display, "monitor-added",
+                           G_CALLBACK (monitor_added_cb),
+                           self, 0);
+
+  g_signal_connect_object (display, "monitor-removed",
+                           G_CALLBACK (monitor_removed_cb),
+                           self, 0);
+
+  for (i = 0; i < n_monitors; i++)
+    {
+      GdkMonitor *monitor;
+
+      monitor = gdk_display_get_monitor (display, i);
+      create_monitor_view (self, monitor);
+    }
 }
 
 GtkWidget *
diff --git a/gnome-flashback/libdesktop/gf-monitor-view.c b/gnome-flashback/libdesktop/gf-monitor-view.c
new file mode 100644
index 0000000..b1ceb9e
--- /dev/null
+++ b/gnome-flashback/libdesktop/gf-monitor-view.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2019 Alberts Muktupāvels
+ *
+ * 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.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "gf-monitor-view.h"
+
+struct _GfMonitorView
+{
+  GtkFixed    parent;
+
+  GdkMonitor *monitor;
+
+  int         width;
+  int         height;
+};
+
+enum
+{
+  PROP_0,
+
+  PROP_MONITOR,
+
+  LAST_PROP
+};
+
+static GParamSpec *view_properties[LAST_PROP] = { NULL };
+
+G_DEFINE_TYPE (GfMonitorView, gf_monitor_view, GTK_TYPE_FIXED)
+
+static void
+workarea_cb (GdkMonitor    *monitor,
+             GParamSpec    *pspec,
+             GfMonitorView *self)
+{
+  GdkRectangle workarea;
+
+  gdk_monitor_get_workarea (monitor, &workarea);
+
+  self->width = workarea.width;
+  self->height = workarea.height;
+
+  gtk_widget_queue_resize (GTK_WIDGET (self));
+}
+
+static void
+gf_monitor_view_constructed (GObject *object)
+{
+  GfMonitorView *self;
+
+  self = GF_MONITOR_VIEW (object);
+
+  G_OBJECT_CLASS (gf_monitor_view_parent_class)->constructed (object);
+
+  g_signal_connect_object (self->monitor, "notify::workarea",
+                           G_CALLBACK (workarea_cb),
+                           self, 0);
+
+  workarea_cb (self->monitor, NULL, self);
+}
+
+static void
+gf_monitor_view_dispose (GObject *object)
+{
+  GfMonitorView *self;
+
+  self = GF_MONITOR_VIEW (object);
+
+  g_clear_object (&self->monitor);
+
+  G_OBJECT_CLASS (gf_monitor_view_parent_class)->dispose (object);
+}
+
+static void
+gf_monitor_view_set_property (GObject      *object,
+                              guint         property_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  GfMonitorView *self;
+
+  self = GF_MONITOR_VIEW (object);
+
+  switch (property_id)
+    {
+      case PROP_MONITOR:
+        g_assert (self->monitor == NULL);
+        self->monitor = g_value_dup_object (value);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+        break;
+    }
+}
+
+static void
+gf_monitor_view_get_preferred_height (GtkWidget *widget,
+                                      gint      *minimum_height,
+                                      gint      *natural_height)
+{
+  GfMonitorView *self;
+
+  self = GF_MONITOR_VIEW (widget);
+
+  *minimum_height = self->height;
+  *natural_height = self->height;
+}
+
+static void
+gf_monitor_view_get_preferred_width (GtkWidget *widget,
+                                     gint      *minimum_width,
+                                     gint      *natural_width)
+{
+  GfMonitorView *self;
+
+  self = GF_MONITOR_VIEW (widget);
+
+  *minimum_width = self->width;
+  *natural_width = self->width;
+}
+
+static GtkSizeRequestMode
+gf_monitor_view_get_request_mode (GtkWidget *widget)
+{
+  return GTK_SIZE_REQUEST_CONSTANT_SIZE;
+}
+
+static void
+install_properties (GObjectClass *object_class)
+{
+  view_properties[PROP_MONITOR] =
+    g_param_spec_object ("monitor",
+                         "monitor",
+                         "monitor",
+                         GDK_TYPE_MONITOR,
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_WRITABLE |
+                         G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, LAST_PROP, view_properties);
+}
+
+static void
+gf_monitor_view_class_init (GfMonitorViewClass *self_class)
+{
+  GObjectClass *object_class;
+  GtkWidgetClass *widget_class;
+
+  object_class = G_OBJECT_CLASS (self_class);
+  widget_class = GTK_WIDGET_CLASS (self_class);
+
+  object_class->constructed = gf_monitor_view_constructed;
+  object_class->dispose = gf_monitor_view_dispose;
+  object_class->set_property = gf_monitor_view_set_property;
+
+  widget_class->get_preferred_height = gf_monitor_view_get_preferred_height;
+  widget_class->get_preferred_width = gf_monitor_view_get_preferred_width;
+  widget_class->get_request_mode = gf_monitor_view_get_request_mode;
+
+  install_properties (object_class);
+}
+
+static void
+gf_monitor_view_init (GfMonitorView *self)
+{
+}
+
+GtkWidget *
+gf_monitor_view_new (GdkMonitor *monitor)
+{
+  return g_object_new (GF_TYPE_MONITOR_VIEW,
+                       "monitor", monitor,
+                       NULL);
+}
+
+GdkMonitor *
+gf_monitor_view_get_monitor (GfMonitorView *self)
+{
+  return self->monitor;
+}
diff --git a/gnome-flashback/libdesktop/gf-monitor-view.h b/gnome-flashback/libdesktop/gf-monitor-view.h
new file mode 100644
index 0000000..52794ef
--- /dev/null
+++ b/gnome-flashback/libdesktop/gf-monitor-view.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2019 Alberts Muktupāvels
+ *
+ * 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.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GF_MONITOR_VIEW_H
+#define GF_MONITOR_VIEW_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GF_TYPE_MONITOR_VIEW (gf_monitor_view_get_type ())
+G_DECLARE_FINAL_TYPE (GfMonitorView, gf_monitor_view, GF, MONITOR_VIEW, GtkFixed)
+
+GtkWidget  *gf_monitor_view_new         (GdkMonitor    *monitor);
+
+GdkMonitor *gf_monitor_view_get_monitor (GfMonitorView *self);
+
+G_END_DECLS
+
+#endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]