[bijiben/wip/sadiq/rewrite: 7/11] Add window class



commit 9a3ee2a1a88397c472d5757378d5e2c80269d570
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Tue Feb 27 07:51:23 2018 +0530

    Add window class

 src/bjb-window.c |  149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bjb-window.h |   34 ++++++++++++
 2 files changed, 183 insertions(+), 0 deletions(-)
---
diff --git a/src/bjb-window.c b/src/bjb-window.c
new file mode 100644
index 0000000..c77e284
--- /dev/null
+++ b/src/bjb-window.c
@@ -0,0 +1,149 @@
+/* bjb-window.c
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "bjb-window"
+
+#include "bjb-trace.h"
+#include "bjb-app.h"
+
+#include "bjb-window.h"
+
+
+struct _BjbWindow
+{
+  GtkApplicationWindow parent_instance;
+
+  GtkWidget *menu_button;
+
+  gint       width;
+  gint       height;
+  gint       pos_x;
+  gint       pos_y;
+  gboolean   is_maximized;
+};
+
+G_DEFINE_TYPE (BjbWindow, bjb_window, GTK_TYPE_APPLICATION_WINDOW)
+
+
+static void
+bjb_window_load_geometry (BjbWindow *self)
+{
+  GSettings *settings = bjb_application_get_default_settings ();
+
+  BJB_ENTRY;
+
+  g_assert (BJB_IS_WINDOW (self));
+
+  self->is_maximized = g_settings_get_boolean (settings, "window-maximized");
+  g_settings_get (settings, "window-size", "(ii)", &self->width, &self->height);
+  g_settings_get (settings, "window-position", "(ii)", &self->pos_x, &self->pos_y);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_window_save_geometry (BjbWindow *self)
+{
+  GSettings *settings = bjb_application_get_default_settings ();
+
+  BJB_ENTRY;
+
+  g_assert (BJB_IS_WINDOW (self));
+
+  g_settings_set_boolean (settings, "window-maximized", self->is_maximized);
+  g_settings_set (settings, "window-size", "(ii)", self->width, self->height);
+  g_settings_set (settings, "window-position", "(ii)", self->pos_x, self->pos_y);
+
+  BJB_EXIT;
+}
+
+static gboolean
+bjb_window_configure_event (BjbWindow *self)
+{
+  g_assert (BJB_IS_WINDOW (self));
+
+  self->is_maximized = gtk_window_is_maximized (GTK_WINDOW (self));
+  if (!self->is_maximized)
+    {
+      gtk_window_get_size (GTK_WINDOW (self), &self->width, &self->height);
+      gtk_window_get_position (GTK_WINDOW (self), &self->pos_x, &self->pos_y);
+    }
+
+  return FALSE;
+}
+
+static void
+bjb_window_destroy (BjbWindow *self)
+{
+  g_assert (BJB_IS_WINDOW (self));
+
+  bjb_window_save_geometry (self);
+}
+
+static void
+bjb_window_constructed (GObject *object)
+{
+  BjbWindow *self = BJB_WINDOW (object);
+  GtkWindow *window = GTK_WINDOW (self);
+
+  bjb_window_load_geometry (self);
+  gtk_window_set_default_size (window, self->width, self->height);
+
+  if (self->is_maximized)
+    gtk_window_maximize (window);
+  else if (self->pos_x >= 0)
+    gtk_window_move (window, self->pos_x, self->pos_y);
+
+  G_OBJECT_CLASS (bjb_window_parent_class)->constructed (object);
+}
+
+static void
+bjb_window_class_init (BjbWindowClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->constructed = bjb_window_constructed;
+
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                               "/org/gnome/bijiben/"
+                                               "ui/bjb-window.ui");
+
+  gtk_widget_class_bind_template_child (widget_class, BjbWindow, menu_button);
+
+  gtk_widget_class_bind_template_callback (widget_class, bjb_window_destroy);
+  gtk_widget_class_bind_template_callback (widget_class, bjb_window_configure_event);
+}
+
+static void
+bjb_window_init (BjbWindow *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+BjbWindow *
+bjb_window_new (BjbApplication *application)
+{
+  g_assert (GTK_IS_APPLICATION (application));
+
+  return g_object_new (BJB_TYPE_WINDOW,
+                       "application", application,
+                       NULL);
+}
diff --git a/src/bjb-window.h b/src/bjb-window.h
new file mode 100644
index 0000000..2d73ea2
--- /dev/null
+++ b/src/bjb-window.h
@@ -0,0 +1,34 @@
+/* bjb-window.h
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+#include "bjb-app.h"
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_WINDOW (bjb_window_get_type ())
+
+G_DECLARE_FINAL_TYPE (BjbWindow, bjb_window, BJB, WINDOW, GtkApplicationWindow)
+
+BjbWindow *bjb_window_new (BjbApplication *application);
+
+G_END_DECLS


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