Convert space plugin to PanelSpace widget.
authorAndriy Grytsenko <andrej@rep.kiev.ua>
Sat, 12 Mar 2016 13:16:18 +0000 (15:16 +0200)
committerAndriy Grytsenko <andrej@rep.kiev.ua>
Sat, 12 Mar 2016 13:16:18 +0000 (15:16 +0200)
This will be required later for drag&drop support.

src/Makefile.am
src/main.c
src/private.h
src/space.c
src/space.h [new file with mode: 0644]

index d898780..f35d447 100644 (file)
@@ -78,6 +78,7 @@ EXTRA_DIST = \
        menu-policy.h \
        icon-grid-old.h \
        gtk-compat.h \
+       space.h \
        private.h
 
 $(top_builddir)/plugins/libbuiltin_plugins.a: $(lxpanel_include_HEADERS) builtin-plugins-hook
index 74b03c1..0baeed5 100644 (file)
@@ -55,6 +55,7 @@
 
 #include "lxpanelctl.h"
 #include "dbg.h"
+#include "space.h"
 
 static gchar *cfgfile = NULL;
 static gchar version[] = VERSION;
@@ -239,10 +240,6 @@ static void init_static_plugins(void)
     REGISTER_STATIC_MODULE(menu);
 #endif
 #endif
-
-#ifdef STATIC_SPACE
-    REGISTER_STATIC_MODULE(space);
-#endif
 }
 
 static void
@@ -480,6 +477,7 @@ int main(int argc, char *argv[], char *env[])
 
     /* prepare modules data */
     lxpanel_prepare_modules();
+    lxpanel_register_plugin_type("space", &_lxpanel_static_plugin_space);
     init_static_plugins();
 
     load_global_config();
index 65a5edd..08ba867 100644 (file)
@@ -205,7 +205,6 @@ static inline char *_user_config_file_name(const char *name1, const char *name2)
 #define STATIC_PAGER
 #define STATIC_TRAY
 #define STATIC_MENU
-#define STATIC_SPACE
 #define STATIC_ICONS
 
 /* Plugins management - new style */
index af94031..b55d7bb 100644 (file)
 
 #include <glib/gi18n.h>
 
-#include "plugin.h"
+#include "space.h"
 
-/* Private context for space plugin. */
-typedef struct {
-    LXPanel *panel; /* The panel and settings are required to apply config */
+struct _PanelSpace
+{
+    GtkEventBox parent;
     config_setting_t *settings;
-    int size;                          /* Size of spacer */
-} SpacePlugin;
+    int size;
+};
 
-static gboolean space_apply_configuration(gpointer user_data);
+struct _PanelSpaceClass
+{
+    GtkEventBoxClass parent_class;
+};
+
+#if GTK_CHECK_VERSION(3, 0, 0)
+static void panel_space_get_preferred_size(GtkWidget *widget,
+                                           gint *minimal_width,
+                                           gint *natural_width)
+{
+    PanelSpace *p = PANEL_SPACE(widget);
+
+    if (minimal_width)
+        *minimal_width = p->size;
+    if (natural_width)
+        *natural_width = p->size;
+}
+#else
+static void panel_space_size_request(GtkWidget *widget,
+                                     GtkRequisition *requisition)
+{
+    PanelSpace *p = PANEL_SPACE(widget);
+
+    requisition->width = requisition->height = p->size;
+}
+#endif
+
+G_DEFINE_TYPE(PanelSpace, panel_space, GTK_TYPE_EVENT_BOX)
+
+static void panel_space_class_init(PanelSpaceClass *klass)
+{
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
+
+#if GTK_CHECK_VERSION(3, 0, 0)
+    widget_class->get_preferred_width = panel_space_get_preferred_size;
+    widget_class->get_preferred_height = panel_space_get_preferred_size;
+#else
+    widget_class->size_request = panel_space_size_request;
+#endif
+}
+
+static void panel_space_init(PanelSpace *self)
+{
+    gtk_widget_set_has_window(GTK_WIDGET(self), FALSE);
+}
 
 /* Plugin constructor. */
-static GtkWidget *space_constructor(LXPanel *panel, config_setting_t *settings)
+GtkWidget *_panel_space_new(LXPanel *panel, config_setting_t *settings)
 {
     /* Allocate plugin context and set into Plugin private data pointer. */
-    SpacePlugin * sp = g_new0(SpacePlugin, 1);
-    GtkWidget * p;
+    PanelSpace * p = g_object_new(PANEL_TYPE_SPACE, NULL);
 
     /* Load parameters from the configuration file. */
-    config_setting_lookup_int(settings, "Size", &sp->size);
+    config_setting_lookup_int(settings, "Size", &p->size);
 
     /* Save construction pointers */
-    sp->panel = panel;
-    sp->settings = settings;
+    p->settings = settings;
 
     /* Default the size parameter. */
-    if (sp->size == 0)
-        sp->size = 2;
-
-    /* Allocate top level widget and set into Plugin widget pointer. */
-    p = gtk_event_box_new();
-    lxpanel_plugin_set_data(p, sp, g_free);
-    gtk_widget_set_has_window(p,FALSE);
+    if (p->size == 0)
+        p->size = 2;
 
-    /* Apply the configuration and show the widget. */
-    space_apply_configuration(p);
-    return p;
+    return GTK_WIDGET(p);
 }
 
 /* Callback when the configuration dialog has recorded a configuration change. */
 static gboolean space_apply_configuration(gpointer user_data)
 {
-    GtkWidget * p = user_data;
-    SpacePlugin * sp = lxpanel_plugin_get_data(p);
+    PanelSpace * p = user_data;
 
     /* Apply settings. */
-    if (panel_get_orientation(sp->panel) == GTK_ORIENTATION_HORIZONTAL)
-        gtk_widget_set_size_request(p, sp->size, 2);
-    else
-        gtk_widget_set_size_request(p, 2, sp->size);
+    gtk_widget_queue_resize(user_data);
     /* Save config values */
-    config_group_set_int(sp->settings, "Size", sp->size);
+    config_group_set_int(p->settings, "Size", p->size);
     return FALSE;
 }
 
+void _panel_space_resize(GtkWidget *spacer, gint size)
+{
+    PanelSpace * p = PANEL_SPACE(spacer);
+
+    p->size = MAX(0, size);
+    space_apply_configuration(p);
+}
+
 /* Callback when the configuration dialog is to be shown. */
 static GtkWidget *space_configure(LXPanel *panel, GtkWidget *instance)
 {
-    SpacePlugin * sp = lxpanel_plugin_get_data(instance);
+    PanelSpace * p = PANEL_SPACE(instance);
     GtkWidget * dlg;
 
     dlg = lxpanel_generic_config_dlg(_("Spacer"), panel,
                                      space_apply_configuration, instance,
-                                     _("Size"), &sp->size, CONF_TYPE_INT, NULL);
+                                     _("Size"), &p->size, CONF_TYPE_INT, NULL);
     gtk_widget_set_size_request(dlg, 200, -1); /* Improve geometry */
     return dlg;
 }
 
 /* Plugin descriptor. */
-LXPanelPluginInit lxpanel_static_plugin_space = {
+LXPanelPluginInit _lxpanel_static_plugin_space = {
     .name = N_("Spacer"),
     .description = N_("Allocate space"),
 
     /* Stretch is available but not default for this plugin. */
     .expand_available = TRUE,
 
-    .new_instance = space_constructor,
+    .new_instance = _panel_space_new,
     .config = space_configure,
 };
diff --git a/src/space.h b/src/space.h
new file mode 100644 (file)
index 0000000..9dc15de
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2014 Andriy Grytsenko <andrej@rep.kiev.ua>
+ *
+ * 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 2 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, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __SPACE_H__
+#define __SPACE_H__ 1
+
+#include "plugin.h"
+
+#define PANEL_TYPE_SPACE               (panel_space_get_type())
+#define PANEL_SPACE(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+                                        PANEL_TYPE_SPACE, PanelSpace))
+#define PANEL_IS_SPACE(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+                                        PANEL_TYPE_SPACE))
+
+extern GType panel_space_get_type   (void) G_GNUC_CONST;
+
+typedef struct _PanelSpace           PanelSpace;
+typedef struct _PanelSpaceClass      PanelSpaceClass;
+
+GtkWidget *_panel_space_new(LXPanel *panel, config_setting_t *settings);
+void _panel_space_resize(GtkWidget *spacer, gint size);
+
+extern LXPanelPluginInit _lxpanel_static_plugin_space;
+
+#endif