Skip to content
Snippets Groups Projects
panel.c 5.65 KiB
Newer Older
Guido Gunther's avatar
Guido Gunther committed
/*
Guido Gunther's avatar
Guido Gunther committed
 * Copyright (C) 2018 Purism SPC
Guido Gunther's avatar
Guido Gunther committed
 * SPDX-License-Identifier: GPL-3.0+
Guido Gunther's avatar
Guido Gunther committed
 * Author: Guido Günther <agx@sigxcpu.org>
Guido Gunther's avatar
Guido Gunther committed
 *
Guido Gunther's avatar
Guido Gunther committed
 * Somewhat based on maynard's panel which is
Guido Gunther's avatar
Guido Gunther committed
 * Copyright (C) 2014 Collabora Ltd. *
 * Author: Jonny Lamb <jonny.lamb@collabora.co.uk>
 */

Guido Gunther's avatar
Guido Gunther committed
#define G_LOG_DOMAIN "phosh-panel"

Guido Gunther's avatar
Guido Gunther committed
#include "config.h"

#include "panel.h"
#include "wwaninfo.h"
#include "batteryinfo.h"
Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-wall-clock.h>

#define _(String) gettext (String)
Guido Gunther's avatar
Guido Gunther committed
enum {
  FAVORITES_ACTIVATED,
Guido Gunther's avatar
Guido Gunther committed
  SETTINGS_ACTIVATED,
Guido Gunther's avatar
Guido Gunther committed
  N_SIGNALS
};
static guint signals[N_SIGNALS] = { 0 };

typedef struct {
Guido Gunther's avatar
Guido Gunther committed
  GtkWidget *btn_favorites;
  GtkWidget *btn_settings;
  GtkWidget *wwaninfo;
  GtkWidget *batteryinfo;
  gint height;
Guido Gunther's avatar
Guido Gunther committed

  GnomeWallClock *wall_clock;
} PhoshPanelPrivate;

typedef struct _PhoshPanel
{
  GtkWindow parent;
} PhoshPanel;
Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
G_DEFINE_TYPE_WITH_PRIVATE (PhoshPanel, phosh_panel, GTK_TYPE_WINDOW)
Guido Gunther's avatar
Guido Gunther committed


static void
Guido Gunther's avatar
Guido Gunther committed
favorites_clicked_cb (PhoshPanel *self, GtkButton *btn)
Guido Gunther's avatar
Guido Gunther committed
{
Guido Gunther's avatar
Guido Gunther committed
  g_return_if_fail (PHOSH_IS_PANEL (self));
  g_return_if_fail (GTK_IS_BUTTON (btn));
  g_signal_emit(self, signals[FAVORITES_ACTIVATED], 0);
Guido Gunther's avatar
Guido Gunther committed
static void
settings_clicked_cb (PhoshPanel *self, GtkButton *btn)
{
  g_return_if_fail (PHOSH_IS_PANEL (self));
  g_return_if_fail (GTK_IS_BUTTON (btn));
  g_signal_emit(self, signals[SETTINGS_ACTIVATED], 0);
}


Guido Gunther's avatar
Guido Gunther committed
static void
wall_clock_notify_cb (GnomeWallClock *wall_clock,
    GParamSpec *pspec,
    PhoshPanel *self)
{
  PhoshPanelPrivate *priv = phosh_panel_get_instance_private (self);
  const gchar *str;
Guido Gunther's avatar
Guido Gunther committed

  str = gnome_wall_clock_get_clock(wall_clock);
Guido Gunther's avatar
Guido Gunther committed
  gtk_button_set_label (GTK_BUTTON (priv->btn_settings), str);
static void
size_allocated_cb (PhoshPanel *self, gpointer unused)
{
  gint width;
  PhoshPanelPrivate *priv = phosh_panel_get_instance_private (self);

  gtk_window_get_size (GTK_WINDOW (self), &width, &priv->height);
}

Guido Gunther's avatar
Guido Gunther committed
static void
phosh_panel_constructed (GObject *object)
{
  PhoshPanel *self = PHOSH_PANEL (object);
Guido Gunther's avatar
Guido Gunther committed
  PhoshPanelPrivate *priv = phosh_panel_get_instance_private (self);
Guido Gunther's avatar
Guido Gunther committed

  G_OBJECT_CLASS (phosh_panel_parent_class)->constructed (object);

  gtk_button_set_label (GTK_BUTTON (priv->btn_favorites), _("Librem5 dev board"));
  priv->wall_clock = gnome_wall_clock_new ();

Guido Gunther's avatar
Guido Gunther committed
  g_signal_connect (priv->wall_clock,
                    "notify::clock",
                    G_CALLBACK (wall_clock_notify_cb),
                    self);
Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
  g_signal_connect_object (priv->btn_favorites,
                           "clicked",
                           G_CALLBACK (favorites_clicked_cb),
                           self,
                           G_CONNECT_SWAPPED);

  g_signal_connect_object (priv->btn_settings,
Guido Gunther's avatar
Guido Gunther committed
                           "clicked",
Guido Gunther's avatar
Guido Gunther committed
                           G_CALLBACK (settings_clicked_cb),
Guido Gunther's avatar
Guido Gunther committed
                           self,
                           G_CONNECT_SWAPPED);
  g_signal_connect (self,
                    "size-allocate",
                    G_CALLBACK (size_allocated_cb),
                    NULL);
Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
  /* window properties */
  gtk_window_set_title (GTK_WINDOW (self), "phosh panel");
  gtk_window_set_decorated (GTK_WINDOW (self), FALSE);
  gtk_widget_realize(GTK_WIDGET (self));

  gtk_style_context_add_class (
      gtk_widget_get_style_context (GTK_WIDGET (self)),
      "phosh-panel");

  /* Button properites */
Guido Gunther's avatar
Guido Gunther committed
  gtk_style_context_remove_class (gtk_widget_get_style_context (priv->btn_favorites),
                                  "button");
Guido Gunther's avatar
Guido Gunther committed
  gtk_style_context_remove_class (gtk_widget_get_style_context (priv->btn_favorites),
                                  "image-button");
Guido Gunther's avatar
Guido Gunther committed
  gtk_style_context_remove_class (gtk_widget_get_style_context (priv->btn_settings),
                                  "button");
Guido Gunther's avatar
Guido Gunther committed
  gtk_style_context_remove_class (gtk_widget_get_style_context (priv->btn_settings),
                                  "image-button");
Guido Gunther's avatar
Guido Gunther committed
  wall_clock_notify_cb (priv->wall_clock, NULL, self);
}


static void
phosh_panel_dispose (GObject *object)
{
  PhoshPanel *self = PHOSH_PANEL (object);
  PhoshPanelPrivate *priv = phosh_panel_get_instance_private (self);

  g_clear_object (&priv->wall_clock);
Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
  G_OBJECT_CLASS (phosh_panel_parent_class)->dispose (object);
Guido Gunther's avatar
Guido Gunther committed
}


static void
phosh_panel_class_init (PhoshPanelClass *klass)
{
  GObjectClass *object_class = (GObjectClass *)klass;
Guido Gunther's avatar
Guido Gunther committed
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
Guido Gunther's avatar
Guido Gunther committed

  object_class->constructed = phosh_panel_constructed;
Guido Gunther's avatar
Guido Gunther committed
  object_class->dispose = phosh_panel_dispose;
Guido Gunther's avatar
Guido Gunther committed

  signals[FAVORITES_ACTIVATED] = g_signal_new ("favorites-activated",
Guido Gunther's avatar
Guido Gunther committed
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
      NULL, G_TYPE_NONE, 0);

Guido Gunther's avatar
Guido Gunther committed
  signals[SETTINGS_ACTIVATED] = g_signal_new ("settings-activated",
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
      NULL, G_TYPE_NONE, 0);

Guido Gunther's avatar
Guido Gunther committed
  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/sm/puri/phosh/ui/top-panel.ui");
Guido Gunther's avatar
Guido Gunther committed
  gtk_widget_class_bind_template_child_private (widget_class, PhoshPanel, btn_favorites);
  gtk_widget_class_bind_template_child_private (widget_class, PhoshPanel, btn_settings);
  PHOSH_TYPE_WWAN_INFO; /* make sure the type is known */
  gtk_widget_class_bind_template_child_private (widget_class, PhoshPanel, wwaninfo);
  PHOSH_TYPE_BATTERY_INFO; /* make sure the type is known */
  gtk_widget_class_bind_template_child_private (widget_class, PhoshPanel, batteryinfo);
Guido Gunther's avatar
Guido Gunther committed
}


static void
phosh_panel_init (PhoshPanel *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
Guido Gunther's avatar
Guido Gunther committed
}

Guido Gunther's avatar
Guido Gunther committed

Guido Gunther's avatar
Guido Gunther committed
GtkWidget *
phosh_panel_new (void)
{
  return g_object_new (PHOSH_PANEL_TYPE,
      NULL);
}

gint
phosh_panel_get_height (PhoshPanel *self)
{
  PhoshPanelPrivate *priv = phosh_panel_get_instance_private (self);

  return priv->height;
}