[gnome-control-center] Add time editor widget



commit 63243d886aa5ee09d3eb8efb4306640cb31d1869
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Thu May 7 20:37:24 2020 +0530

    Add time editor widget

 panels/common/cc-time-editor.c     | 368 ++++++++++++++++++++++
 panels/common/cc-time-editor.h     |  45 +++
 panels/common/cc-time-editor.ui    | 193 ++++++++++++
 panels/common/cc-time-entry.c      | 611 +++++++++++++++++++++++++++++++++++++
 panels/common/cc-time-entry.h      |  48 +++
 panels/common/common.gresource.xml |   1 +
 panels/common/meson.build          |   3 +
 7 files changed, 1269 insertions(+)
---
diff --git a/panels/common/cc-time-editor.c b/panels/common/cc-time-editor.c
new file mode 100644
index 000000000..684a9cd12
--- /dev/null
+++ b/panels/common/cc-time-editor.c
@@ -0,0 +1,368 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
+/* cc-time-editor.c
+ *
+ * Copyright 2020 Purism SPC
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s):
+ *   Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#undef G_LOG_DOMAIN
+#define G_LOG_DOMAIN "cc-time-editor"
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#define GNOME_DESKTOP_USE_UNSTABLE_API
+#include <libgnome-desktop/gnome-wall-clock.h>
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+
+#include "cc-time-entry.h"
+#include "cc-time-editor.h"
+
+
+#define TIMEOUT_INITIAL  500
+#define TIMEOUT_REPEAT    50
+
+#define FILECHOOSER_SCHEMA "org.gtk.Settings.FileChooser"
+#define CLOCK_SCHEMA       "org.gnome.desktop.interface"
+#define CLOCK_FORMAT_KEY   "clock-format"
+#define SECONDS_PER_MINUTE  (60)
+#define SECONDS_PER_HOUR    (60 * 60)
+#define SECONDS_PER_DAY     (60 * 60 * 24)
+
+
+struct _CcTimeEditor
+{
+  GtkBin     parent_instance;
+
+  GtkButton *am_pm_button;
+  GtkStack  *am_pm_stack;
+  GtkLabel  *am_label;
+  GtkLabel  *pm_label;
+  GtkButton *hour_up_button;
+  GtkButton *hour_down_button;
+  GtkButton *minute_up_button;
+  GtkButton *minute_down_button;
+  CcTimeEntry *time_entry;
+
+  GtkButton *clicked_button; /* The button currently being clicked */
+  GSettings *clock_settings;
+  GSettings *filechooser_settings;
+
+  guint      timer_id;
+};
+
+G_DEFINE_TYPE (CcTimeEditor, cc_time_editor, GTK_TYPE_BIN)
+
+
+enum {
+  TIME_CHANGED,
+  N_SIGNALS
+};
+
+static guint signals[N_SIGNALS];
+
+static void
+time_editor_clock_changed_cb (CcTimeEditor *self)
+{
+  GDesktopClockFormat value;
+  gboolean is_am_pm;
+
+  g_assert (CC_IS_TIME_EDITOR (self));
+
+  value = g_settings_get_enum (self->clock_settings, CLOCK_FORMAT_KEY);
+
+  is_am_pm = value == G_DESKTOP_CLOCK_FORMAT_12H;
+  cc_time_entry_set_am_pm (self->time_entry, is_am_pm);
+  gtk_widget_set_visible (GTK_WIDGET (self->am_pm_button), is_am_pm);
+
+  if (is_am_pm)
+    {
+      if (cc_time_entry_get_is_am (self->time_entry))
+        gtk_stack_set_visible_child (self->am_pm_stack, GTK_WIDGET (self->am_label));
+      else
+        gtk_stack_set_visible_child (self->am_pm_stack, GTK_WIDGET (self->pm_label));
+    }
+}
+
+static void
+time_editor_time_changed_cb (CcTimeEditor *self)
+{
+  g_assert (CC_IS_TIME_EDITOR (self));
+
+  g_signal_emit (self, signals[TIME_CHANGED], 0);
+}
+
+static void
+editor_change_time_clicked_cb (CcTimeEditor *self,
+                               GtkButton    *button)
+{
+  g_assert (CC_IS_TIME_EDITOR (self));
+
+  if (button == NULL)
+    return;
+
+  if (button == self->hour_up_button)
+    {
+      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 0);
+      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_UP);
+    }
+  else if (button == self->hour_down_button)
+    {
+      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 0);
+      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_DOWN);
+    }
+  else if (button == self->minute_up_button)
+    {
+      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 3);
+      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_UP);
+    }
+  else if (button == self->minute_down_button)
+    {
+      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 3);
+      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_DOWN);
+    }
+}
+
+static gboolean
+editor_change_time_repeat (CcTimeEditor *self)
+{
+  if (self->clicked_button == NULL)
+    {
+      self->timer_id = 0;
+
+      return G_SOURCE_REMOVE;
+    }
+
+  editor_change_time_clicked_cb (self, self->clicked_button);
+
+  return G_SOURCE_CONTINUE;
+}
+
+static gboolean
+editor_change_time_cb (CcTimeEditor *self)
+{
+  g_assert (CC_IS_TIME_EDITOR (self));
+  g_clear_handle_id (&self->timer_id, g_source_remove);
+
+  editor_change_time_clicked_cb (self, self->clicked_button);
+  self->timer_id = g_timeout_add (TIMEOUT_REPEAT,
+                                  (GSourceFunc)editor_change_time_repeat,
+                                  self);
+  return G_SOURCE_REMOVE;
+}
+
+static gboolean
+editor_change_time_pressed_cb (CcTimeEditor *self,
+                               GdkEvent     *event,
+                               GtkButton    *button)
+{
+  g_assert (CC_IS_TIME_EDITOR (self));
+
+  self->clicked_button = button;
+  /* Keep changing time until the press is released */
+  self->timer_id = g_timeout_add (TIMEOUT_INITIAL,
+                                  (GSourceFunc)editor_change_time_cb,
+                                  self);
+  editor_change_time_clicked_cb (self, button);
+  return FALSE;
+}
+
+static gboolean
+editor_change_time_released_cb (CcTimeEditor *self)
+{
+  self->clicked_button = NULL;
+  g_clear_handle_id (&self->timer_id, g_source_remove);
+
+  return FALSE;
+}
+
+static void
+editor_am_pm_button_clicked_cb (CcTimeEditor *self)
+{
+  gboolean is_am;
+
+  g_assert (CC_IS_TIME_EDITOR (self));
+  g_assert (cc_time_entry_get_am_pm (self->time_entry));
+
+  is_am = cc_time_entry_get_is_am (self->time_entry);
+  /* Toggle AM PM */
+  cc_time_entry_set_is_am (self->time_entry, !is_am);
+  time_editor_clock_changed_cb (self);
+}
+
+static void
+editor_am_pm_stack_changed_cb (CcTimeEditor *self)
+{
+  AtkObject *accessible;
+  GtkWidget *label;
+  const gchar *text;
+
+  g_assert (CC_IS_TIME_EDITOR (self));
+
+  accessible = gtk_widget_get_accessible (GTK_WIDGET (self->am_pm_button));
+  if (accessible == NULL)
+    return;
+
+  label = gtk_stack_get_visible_child (self->am_pm_stack);
+  text = gtk_label_get_text (GTK_LABEL (label));
+  atk_object_set_name (accessible, text);
+}
+
+static void
+cc_time_editor_constructed (GObject *object)
+{
+  CcTimeEditor *self = (CcTimeEditor *)object;
+  GDateTime *date;
+  char *label;
+
+  G_OBJECT_CLASS (cc_time_editor_parent_class)->constructed (object);
+
+  /* Set localized identifier for AM */
+  date = g_date_time_new_utc (1, 1, 1, 0, 0, 0);
+  label = g_date_time_format (date, "%p");
+  gtk_label_set_label (self->am_label, label);
+  g_date_time_unref (date);
+  g_free (label);
+
+  /* Set localized identifier for PM */
+  date = g_date_time_new_utc (1, 1, 1, 12, 0, 0);
+  label = g_date_time_format (date, "%p");
+  gtk_label_set_label (self->pm_label, label);
+  g_date_time_unref (date);
+  g_free (label);
+}
+
+static void
+cc_time_editor_finalize (GObject *object)
+{
+  CcTimeEditor *self = (CcTimeEditor *)object;
+
+  g_clear_handle_id (&self->timer_id, g_source_remove);
+  g_clear_object (&self->clock_settings);
+  g_clear_object (&self->filechooser_settings);
+
+  G_OBJECT_CLASS (cc_time_editor_parent_class)->finalize (object);
+}
+
+static void
+cc_time_editor_class_init (CcTimeEditorClass *klass)
+{
+  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->constructed = cc_time_editor_constructed;
+  object_class->finalize = cc_time_editor_finalize;
+
+  signals[TIME_CHANGED] =
+    g_signal_new ("time-changed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                               "/org/gnome/control-center/"
+                                               "common/cc-time-editor.ui");
+
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_pm_button);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_pm_stack);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_label);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, pm_label);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, hour_up_button);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, hour_down_button);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, minute_up_button);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, minute_down_button);
+  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, time_entry);
+
+  gtk_widget_class_bind_template_callback (widget_class, editor_change_time_pressed_cb);
+  gtk_widget_class_bind_template_callback (widget_class, editor_change_time_released_cb);
+  gtk_widget_class_bind_template_callback (widget_class, editor_am_pm_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, editor_am_pm_stack_changed_cb);
+
+  g_type_ensure (CC_TYPE_TIME_ENTRY);
+}
+
+static void
+cc_time_editor_init (CcTimeEditor *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  self->clock_settings = g_settings_new (CLOCK_SCHEMA);
+  self->filechooser_settings = g_settings_new (FILECHOOSER_SCHEMA);
+
+  g_signal_connect_object (self->clock_settings, "changed::" CLOCK_FORMAT_KEY,
+                           G_CALLBACK (time_editor_clock_changed_cb), self,
+                           G_CONNECT_SWAPPED);
+  g_signal_connect_swapped (self->time_entry, "time-changed",
+                            G_CALLBACK (time_editor_time_changed_cb), self);
+  time_editor_clock_changed_cb (self);
+}
+
+CcTimeEditor *
+cc_time_editor_new (void)
+{
+  return g_object_new (CC_TYPE_TIME_EDITOR, NULL);
+}
+
+void
+cc_time_editor_set_time (CcTimeEditor *self,
+                         guint         hour,
+                         guint         minute)
+{
+  g_return_if_fail (CC_IS_TIME_EDITOR (self));
+
+  cc_time_entry_set_time (self->time_entry, hour, minute);
+}
+
+guint
+cc_time_editor_get_hour (CcTimeEditor *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), 0);
+
+  return cc_time_entry_get_hour (self->time_entry);
+}
+
+guint
+cc_time_editor_get_minute (CcTimeEditor *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), 0);
+
+  return cc_time_entry_get_minute (self->time_entry);
+}
+
+gboolean
+cc_time_editor_get_am_pm (CcTimeEditor *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), TRUE);
+
+  return TRUE;
+}
+
+void
+cc_time_editor_set_am_pm (CcTimeEditor *self,
+                          gboolean      is_am_pm)
+{
+  g_return_if_fail (CC_IS_TIME_EDITOR (self));
+
+  cc_time_entry_set_am_pm (self->time_entry, is_am_pm);
+}
diff --git a/panels/common/cc-time-editor.h b/panels/common/cc-time-editor.h
new file mode 100644
index 000000000..48c4534e2
--- /dev/null
+++ b/panels/common/cc-time-editor.h
@@ -0,0 +1,45 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
+/* cc-time-editor.h
+ *
+ * Copyright 2020 Purism SPC
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s):
+ *   Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define CC_TYPE_TIME_EDITOR (cc_time_editor_get_type ())
+
+G_DECLARE_FINAL_TYPE (CcTimeEditor, cc_time_editor, CC, TIME_EDITOR, GtkBin)
+
+CcTimeEditor *cc_time_editor_new        (void);
+void          cc_time_editor_set_time   (CcTimeEditor *self,
+                                         guint         hour,
+                                         guint         minute);
+guint         cc_time_editor_get_hour   (CcTimeEditor *self);
+guint         cc_time_editor_get_minute (CcTimeEditor *self);
+gboolean      cc_time_editor_get_am_pm  (CcTimeEditor *self);
+void          cc_time_editor_set_am_pm  (CcTimeEditor *self,
+                                         gboolean      is_am_pm);
+
+G_END_DECLS
diff --git a/panels/common/cc-time-editor.ui b/panels/common/cc-time-editor.ui
new file mode 100644
index 000000000..1732fcd6f
--- /dev/null
+++ b/panels/common/cc-time-editor.ui
@@ -0,0 +1,193 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="CcTimeEditor" parent="GtkBin">
+    <property name="visible">True</property>
+    <child>
+      <object class="GtkGrid">
+        <property name="visible">True</property>
+        <property name="row-spacing">6</property>
+        <property name="column-spacing">6</property>
+
+        <!-- Increment Hour Button -->
+        <child>
+          <object class="GtkButton" id="hour_up_button">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="valign">center</property>
+            <property name="halign">center</property>
+            <signal name="button-press-event" handler="editor_change_time_pressed_cb" swapped="yes"/>
+            <signal name="button-release-event" handler="editor_change_time_released_cb" swapped="yes"/>
+            <style>
+              <class name="titlebutton"/>
+              <class name="circular"/>
+              <class name="flat"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">go-up-symbolic</property>
+              </object>
+            </child>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-name" translatable="yes">Increment Hour</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+
+        <!-- Increment Minute Button -->
+        <child>
+          <object class="GtkButton" id="minute_up_button">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="valign">center</property>
+            <property name="halign">center</property>
+            <signal name="button-press-event" handler="editor_change_time_pressed_cb" swapped="yes"/>
+            <signal name="button-release-event" handler="editor_change_time_released_cb" swapped="yes"/>
+            <style>
+              <class name="titlebutton"/>
+              <class name="circular"/>
+              <class name="flat"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">go-up-symbolic</property>
+              </object>
+            </child>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-name" translatable="yes">Increment Minute</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">1</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+
+        <child>
+          <object class="CcTimeEntry" id="time_entry">
+            <property name="visible">True</property>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-description" translatable="yes">Time</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">1</property>
+            <property name="width">2</property>
+          </packing>
+        </child>
+
+        <!-- Decrement Hour Button -->
+        <child>
+          <object class="GtkButton" id="hour_down_button">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="valign">center</property>
+            <property name="halign">center</property>
+            <signal name="button-press-event" handler="editor_change_time_pressed_cb" swapped="yes"/>
+            <signal name="button-release-event" handler="editor_change_time_released_cb" swapped="yes"/>
+            <style>
+              <class name="titlebutton"/>
+              <class name="circular"/>
+              <class name="flat"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">go-down-symbolic</property>
+              </object>
+            </child>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-name" translatable="yes">Decrement Hour</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">2</property>
+          </packing>
+        </child>
+
+        <!-- Decrement Minute Button -->
+        <child>
+          <object class="GtkButton" id="minute_down_button">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="valign">center</property>
+            <property name="halign">center</property>
+            <signal name="button-press-event" handler="editor_change_time_pressed_cb" swapped="yes"/>
+            <signal name="button-release-event" handler="editor_change_time_released_cb" swapped="yes"/>
+            <style>
+              <class name="titlebutton"/>
+              <class name="circular"/>
+              <class name="flat"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">go-down-symbolic</property>
+              </object>
+            </child>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-name" translatable="yes">Decrement Minute</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">1</property>
+            <property name="top-attach">2</property>
+          </packing>
+        </child>
+
+        <!-- AM/PM Button -->
+        <child>
+          <object class="GtkButton" id="am_pm_button">
+            <property name="visible">True</property>
+            <property name="valign">center</property>
+            <signal name="clicked" handler="editor_am_pm_button_clicked_cb" swapped="yes"/>
+            <child>
+              <object class="GtkStack" id="am_pm_stack">
+                <property name="visible">True</property>
+                <signal name="notify::visible-child" handler="editor_am_pm_stack_changed_cb" swapped="yes"/>
+                <child>
+                  <object class="GtkLabel" id="am_label">
+                    <property name="visible">True</property>
+                    <attributes>
+                      <attribute name="scale" value="1.4"/>
+                    </attributes>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="pm_label">
+                    <property name="visible">True</property>
+                    <attributes>
+                      <attribute name="scale" value="1.4"/>
+                    </attributes>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left-attach">2</property>
+            <property name="top-attach">1</property>
+          </packing>
+        </child>
+
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/panels/common/cc-time-entry.c b/panels/common/cc-time-entry.c
new file mode 100644
index 000000000..5a5942513
--- /dev/null
+++ b/panels/common/cc-time-entry.c
@@ -0,0 +1,611 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
+/* cc-time-entry.c
+ *
+ * Copyright 2020 Purism SPC
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s):
+ *   Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#undef G_LOG_DOMAIN
+#define G_LOG_DOMAIN "cc-time-entry"
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+
+#include "cc-time-entry.h"
+
+#define SEPARATOR_INDEX      2
+#define END_INDEX            4
+#define EMIT_CHANGED_TIMEOUT 100
+
+
+struct _CcTimeEntry
+{
+  GtkEntry   parent_instance;
+
+  guint      insert_text_id;
+  guint      time_changed_id;
+  int        hour; /* Range: 0-23 in 24H and 1-12 in 12H with is_am set/unset */
+  int        minute;
+  gboolean   is_am_pm;
+  gboolean   is_am; /* AM if TRUE. PM if FALSE. valid iff is_am_pm set */
+};
+
+G_DEFINE_TYPE (CcTimeEntry, cc_time_entry, GTK_TYPE_ENTRY)
+
+enum {
+  CHANGE_VALUE,
+  TIME_CHANGED,
+  N_SIGNALS
+};
+
+static guint signals[N_SIGNALS];
+
+static gboolean
+emit_time_changed (CcTimeEntry *self)
+{
+  self->time_changed_id = 0;
+
+  g_signal_emit (self, signals[TIME_CHANGED], 0);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+time_entry_fill_time (CcTimeEntry *self)
+{
+  g_autofree gchar *str = NULL;
+
+  g_assert (CC_IS_TIME_ENTRY (self));
+
+  str = g_strdup_printf ("%02d:%02d", self->hour, self->minute);
+
+  g_signal_handler_block (self, self->insert_text_id);
+  gtk_entry_set_text (GTK_ENTRY (self), str);
+  g_signal_handler_unblock (self, self->insert_text_id);
+}
+
+static void
+cursor_position_changed_cb (CcTimeEntry *self)
+{
+  int current_pos;
+
+  g_assert (CC_IS_TIME_ENTRY (self));
+
+  current_pos = gtk_editable_get_position (GTK_EDITABLE (self));
+
+  g_signal_handlers_block_by_func (self, cursor_position_changed_cb, self);
+
+  /* If cursor is on ‘:’ move to the next field */
+  if (current_pos == SEPARATOR_INDEX)
+    gtk_editable_set_position (GTK_EDITABLE (self), current_pos + 1);
+
+  /* If cursor is after the last digit and without selection, move to last digit */
+  if (current_pos > END_INDEX &&
+      !gtk_editable_get_selection_bounds (GTK_EDITABLE (self), NULL, NULL))
+    gtk_editable_set_position (GTK_EDITABLE (self), END_INDEX);
+
+  g_signal_handlers_unblock_by_func (self, cursor_position_changed_cb, self);
+}
+
+static void
+entry_selection_changed_cb (CcTimeEntry *self)
+{
+  GtkEditable *editable;
+
+  g_assert (CC_IS_TIME_ENTRY (self));
+
+  editable = GTK_EDITABLE (self);
+
+  g_signal_handlers_block_by_func (self, cursor_position_changed_cb, self);
+
+  /* If cursor is after the last digit and without selection, move to last digit */
+  if (gtk_editable_get_position (editable) > END_INDEX &&
+      !gtk_editable_get_selection_bounds (editable, NULL, NULL))
+    gtk_editable_set_position (editable, END_INDEX);
+
+  g_signal_handlers_unblock_by_func (self, cursor_position_changed_cb, self);
+}
+
+static void
+editable_insert_text_cb (CcTimeEntry *self,
+                         char        *new_text,
+                         gint         new_text_length,
+                         gint        *position)
+{
+  g_assert (CC_IS_TIME_ENTRY (self));
+
+  if (new_text_length == -1)
+    new_text_length = strlen (new_text);
+
+  if (new_text_length == 5)
+    {
+      guint16 text_length;
+
+      text_length = gtk_entry_get_text_length (GTK_ENTRY (self));
+
+      /* Return if the text matches XX:XX template (where X is a number) */
+      if (text_length == 0 &&
+          strstr (new_text, "0123456789:") == new_text + new_text_length &&
+          strchr (new_text, ':') == strrchr (new_text, ':'))
+        return;
+    }
+
+  /* Insert text if single digit number */
+  if (new_text_length == 1 &&
+      strspn (new_text, "0123456789"))
+    {
+      int pos, number;
+
+      pos = *position;
+      number = *new_text - '0';
+
+      if (pos == 0)
+        self->hour = self->hour % 10 + number * 10;
+      else if (pos == 1)
+        self->hour = self->hour / 10 * 10 + number;
+      else if (pos == 3)
+        self->minute = self->minute % 10 + number * 10;
+      else if (pos == 4)
+        self->minute = self->minute / 10 * 10 + number;
+
+      if (self->is_am_pm)
+        self->hour = CLAMP (self->hour, 1, 12);
+      else
+        self->hour = CLAMP (self->hour, 0, 23);
+
+      self->minute = CLAMP (self->minute, 0, 59);
+
+      g_signal_stop_emission_by_name (self, "insert-text");
+      time_entry_fill_time (self);
+      *position = pos + 1;
+
+      g_clear_handle_id (&self->time_changed_id, g_source_remove);
+      self->time_changed_id = g_timeout_add (EMIT_CHANGED_TIMEOUT,
+                                             (GSourceFunc)emit_time_changed, self);
+      return;
+    }
+
+  /* Warn otherwise */
+  g_signal_stop_emission_by_name (self, "insert-text");
+  gtk_widget_error_bell (GTK_WIDGET (self));
+}
+
+
+static void
+entry_select_all (CcTimeEntry *self)
+{
+  gtk_editable_select_region (GTK_EDITABLE (self), 0, -1);
+}
+
+static void
+entry_populate_popup_cb (CcTimeEntry *self,
+                         GtkWidget   *widget)
+{
+  GList *children;
+
+  if (!GTK_IS_CONTAINER (widget))
+    return;
+
+  children = gtk_container_get_children (GTK_CONTAINER (widget));
+
+  if (GTK_IS_MENU (widget))
+    {
+      GtkWidget *menu_item;
+
+      for (GList *child = children; child; child = child->next)
+        gtk_container_remove (GTK_CONTAINER (widget), child->data);
+
+      menu_item = gtk_menu_item_new_with_mnemonic (_("_Copy"));
+      gtk_widget_set_sensitive (menu_item, gtk_editable_get_selection_bounds (GTK_EDITABLE (self), NULL, 
NULL));
+      g_signal_connect_swapped (menu_item, "activate", G_CALLBACK (gtk_editable_copy_clipboard), self);
+      gtk_widget_show (menu_item);
+      gtk_menu_shell_append (GTK_MENU_SHELL (widget), menu_item);
+
+      menu_item = gtk_menu_item_new_with_mnemonic (_("Select _All"));
+      gtk_widget_set_sensitive (menu_item, gtk_entry_get_text_length (GTK_ENTRY (self)) > 0);
+      g_signal_connect_swapped (menu_item, "activate", G_CALLBACK (entry_select_all), self);
+      gtk_widget_show (menu_item);
+      gtk_menu_shell_append (GTK_MENU_SHELL (widget), menu_item);
+    }
+}
+
+static void
+time_entry_change_value_cb (CcTimeEntry   *self,
+                            GtkScrollType  type)
+{
+  int position;
+  g_assert (CC_IS_TIME_ENTRY (self));
+
+  position = gtk_editable_get_position (GTK_EDITABLE (self));
+
+  if (position > SEPARATOR_INDEX)
+    {
+      if (type == GTK_SCROLL_STEP_UP)
+        self->minute++;
+      else
+        self->minute--;
+
+      if (self->minute >= 60)
+        self->minute = 0;
+      else if (self->minute <= -1)
+        self->minute = 59;
+    }
+  else
+    {
+      if (type == GTK_SCROLL_STEP_UP)
+        self->hour++;
+      else
+        self->hour--;
+
+      if (self->is_am_pm)
+        {
+          if (self->hour > 12)
+            self->hour = 1;
+          else if (self->hour < 1)
+            self->hour = 12;
+        }
+      else
+        {
+          if (self->hour >= 24)
+            self->hour = 0;
+          else if (self->hour <= -1)
+            self->hour = 23;
+        }
+    }
+
+  time_entry_fill_time (self);
+  gtk_editable_set_position (GTK_EDITABLE (self), position);
+
+  g_clear_handle_id (&self->time_changed_id, g_source_remove);
+  self->time_changed_id = g_timeout_add (EMIT_CHANGED_TIMEOUT,
+                                         (GSourceFunc)emit_time_changed, self);
+}
+
+static void
+cc_entry_move_cursor (GtkEntry        *entry,
+                      GtkMovementStep  step,
+                      gint             count,
+                      gboolean         extend_selection)
+{
+  int current_pos;
+
+  current_pos = gtk_editable_get_position (GTK_EDITABLE (entry));
+
+  /* If cursor is on ‘:’ move backward/forward depending on the current movement */
+  if ((step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
+       step == GTK_MOVEMENT_VISUAL_POSITIONS) &&
+      current_pos + count == SEPARATOR_INDEX)
+    count > 0 ? count++ : count--;
+
+  GTK_ENTRY_CLASS (cc_time_entry_parent_class)->move_cursor (entry, step, count, extend_selection);
+}
+
+static void
+cc_time_entry_error_bell (GtkEntry *entry)
+{
+  gtk_widget_error_bell (GTK_WIDGET (entry));
+}
+
+static void
+cc_time_entry_delete_from_cursor (GtkEntry      *entry,
+                                  GtkDeleteType  type,
+                                  gint            count)
+{
+  gtk_widget_error_bell (GTK_WIDGET (entry));
+}
+
+static gboolean
+cc_time_entry_drag_motion (GtkWidget      *widget,
+                           GdkDragContext *context,
+                           gint            x,
+                           gint            y,
+                           guint           time)
+{
+  return TRUE;
+}
+
+static gboolean
+cc_time_entry_key_press (GtkWidget   *widget,
+                         GdkEventKey *event)
+{
+  CcTimeEntry *self = (CcTimeEntry *)widget;
+
+  /* Allow entering numbers */
+  if (!(event->state & GDK_SHIFT_MASK) &&
+      ((event->keyval >= GDK_KEY_KP_0 &&
+        event->keyval <= GDK_KEY_KP_9) ||
+       (event->keyval >= GDK_KEY_0 &&
+        event->keyval <= GDK_KEY_9)))
+    return GTK_WIDGET_CLASS (cc_time_entry_parent_class)->key_press_event (widget, event);
+
+  /* Allow navigation keys */
+  if ((event->keyval >= GDK_KEY_Left &&
+       event->keyval <= GDK_KEY_Down) ||
+      (event->keyval >= GDK_KEY_KP_Left &&
+       event->keyval <= GDK_KEY_KP_Down) ||
+      event->keyval == GDK_KEY_Home ||
+      event->keyval == GDK_KEY_End ||
+      event->keyval == GDK_KEY_Menu)
+    return GTK_WIDGET_CLASS (cc_time_entry_parent_class)->key_press_event (widget, event);
+
+  if (event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK))
+    return GTK_WIDGET_CLASS (cc_time_entry_parent_class)->key_press_event (widget, event);
+
+  if (event->keyval == GDK_KEY_Tab)
+    {
+      /* If focus is on Hour field skip to minute field */
+      if (gtk_editable_get_position (GTK_EDITABLE (self)) <= 1)
+        {
+          gtk_editable_set_position (GTK_EDITABLE (self), SEPARATOR_INDEX + 1);
+
+          return GDK_EVENT_STOP;
+        }
+
+      return GTK_WIDGET_CLASS (cc_time_entry_parent_class)->key_press_event (widget, event);
+    }
+
+  /* Shift-Tab */
+  if (event->keyval == GDK_KEY_ISO_Left_Tab)
+    {
+      /* If focus is on Minute field skip back to Hour field */
+      if (gtk_editable_get_position (GTK_EDITABLE (self)) >= 2)
+        {
+          gtk_editable_set_position (GTK_EDITABLE (self), 0);
+
+          return GDK_EVENT_STOP;
+        }
+
+      return GTK_WIDGET_CLASS (cc_time_entry_parent_class)->key_press_event (widget, event);
+    }
+
+  return GDK_EVENT_STOP;
+}
+
+static void
+cc_time_entry_constructed (GObject *object)
+{
+  PangoAttrList *list;
+  PangoAttribute *attribute;
+
+  G_OBJECT_CLASS (cc_time_entry_parent_class)->constructed (object);
+
+  g_object_set (object,
+                "input-purpose", GTK_INPUT_PURPOSE_DIGITS,
+                "input-hints", GTK_INPUT_HINT_NO_EMOJI,
+                "overwrite-mode", TRUE,
+                "xalign", 0.5,
+                "max-length", 5,
+                NULL);
+
+  time_entry_fill_time (CC_TIME_ENTRY (object));
+
+  list = pango_attr_list_new ();
+
+  attribute = pango_attr_size_new (PANGO_SCALE * 32);
+  pango_attr_list_insert (list, attribute);
+
+  attribute = pango_attr_weight_new (PANGO_WEIGHT_LIGHT);
+  pango_attr_list_insert (list, attribute);
+
+  /* Use tabular(monospace) letters */
+  attribute = pango_attr_font_features_new ("tnum");
+  pango_attr_list_insert (list, attribute);
+
+  attribute = pango_attr_letter_spacing_new (PANGO_SCALE * 12);
+  attribute->start_index = SEPARATOR_INDEX;
+  attribute->end_index = SEPARATOR_INDEX + 1;
+  pango_attr_list_insert (list, attribute);
+
+  /* Raise ‘:’ a bit so that they are well aligned with the text */
+  attribute = pango_attr_rise_new (PANGO_SCALE * 4);
+  attribute->start_index = SEPARATOR_INDEX;
+  attribute->end_index = SEPARATOR_INDEX + 1;
+  pango_attr_list_insert (list, attribute);
+
+  gtk_entry_set_attributes (GTK_ENTRY (object), list);
+}
+
+static void
+cc_time_entry_class_init (CcTimeEntryClass *klass)
+{
+  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GtkEntryClass  *entry_class  = GTK_ENTRY_CLASS (klass);
+  GtkBindingSet  *binding_set;
+
+  object_class->constructed = cc_time_entry_constructed;
+
+  widget_class->drag_motion = cc_time_entry_drag_motion;
+  widget_class->key_press_event = cc_time_entry_key_press;
+
+  entry_class->delete_from_cursor = cc_time_entry_delete_from_cursor;
+  entry_class->move_cursor = cc_entry_move_cursor;
+  entry_class->toggle_overwrite = cc_time_entry_error_bell;
+  entry_class->backspace = cc_time_entry_error_bell;
+  entry_class->cut_clipboard = cc_time_entry_error_bell;
+  entry_class->paste_clipboard = cc_time_entry_error_bell;
+
+  signals[CHANGE_VALUE] =
+    g_signal_new ("change-value",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_ACTION,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 1,
+                  GTK_TYPE_SCROLL_TYPE);
+
+  signals[TIME_CHANGED] =
+    g_signal_new ("time-changed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+
+  binding_set = gtk_binding_set_by_class (klass);
+
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
+                                "change-value", 1,
+                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
+                                "change-value", 1,
+                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
+
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
+                                "change-value", 1,
+                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
+                                "change-value", 1,
+                                GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
+}
+
+static void
+cc_time_entry_init (CcTimeEntry *self)
+{
+  g_signal_connect_after (self, "notify::cursor-position",
+                          G_CALLBACK (cursor_position_changed_cb), NULL);
+  g_signal_connect_after (self, "notify::selection-bound",
+                          G_CALLBACK (entry_selection_changed_cb), NULL);
+  self->insert_text_id = g_signal_connect (self, "insert-text",
+                                           G_CALLBACK (editable_insert_text_cb), NULL);
+  g_signal_connect_after (self, "populate-popup",
+                          G_CALLBACK (entry_populate_popup_cb), NULL);
+  g_signal_connect (self, "change-value",
+                    G_CALLBACK (time_entry_change_value_cb), NULL);
+}
+
+GtkWidget *
+cc_time_entry_new (void)
+{
+  return g_object_new (CC_TYPE_TIME_ENTRY, NULL);
+}
+
+void
+cc_time_entry_set_time (CcTimeEntry *self,
+                        guint        hour,
+                        guint        minute)
+{
+  gboolean is_am_pm;
+
+  g_return_if_fail (CC_IS_TIME_ENTRY (self));
+
+  if (cc_time_entry_get_hour (self) == hour &&
+      cc_time_entry_get_minute (self) == minute)
+    return;
+
+  is_am_pm = cc_time_entry_get_am_pm (self);
+  cc_time_entry_set_am_pm (self, FALSE);
+
+  self->hour = CLAMP (hour, 0, 23);
+  self->minute = CLAMP (minute, 0, 59);
+
+  cc_time_entry_set_am_pm (self, is_am_pm);
+  time_entry_fill_time (self);
+}
+
+guint
+cc_time_entry_get_hour (CcTimeEntry *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_ENTRY (self), 0);
+
+  if (!self->is_am_pm)
+    return self->hour;
+
+  if (self->is_am && self->hour == 12)
+    return 0;
+  else if (self->is_am || self->hour == 12)
+    return self->hour;
+  else
+    return self->hour + 12;
+}
+
+guint
+cc_time_entry_get_minute (CcTimeEntry *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_ENTRY (self), 0);
+
+  return self->minute;
+}
+
+gboolean
+cc_time_entry_get_is_am (CcTimeEntry *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_ENTRY (self), FALSE);
+
+  if (self->is_am_pm)
+    return self->is_am;
+
+  return self->hour < 12;
+}
+
+void
+cc_time_entry_set_is_am (CcTimeEntry *self,
+                         gboolean     is_am)
+{
+  g_return_if_fail (CC_IS_TIME_ENTRY (self));
+
+  self->is_am = !!is_am;
+  g_signal_emit (self, signals[TIME_CHANGED], 0);
+}
+
+gboolean
+cc_time_entry_get_am_pm (CcTimeEntry *self)
+{
+  g_return_val_if_fail (CC_IS_TIME_ENTRY (self), FALSE);
+
+  return self->is_am_pm;
+}
+
+void
+cc_time_entry_set_am_pm (CcTimeEntry *self,
+                         gboolean     is_am_pm)
+{
+  g_return_if_fail (CC_IS_TIME_ENTRY (self));
+
+  if (self->is_am_pm == !!is_am_pm)
+    return;
+
+  if (self->hour < 12)
+    self->is_am = TRUE;
+  else
+    self->is_am = FALSE;
+
+  if (is_am_pm)
+    {
+      if (self->hour == 0)
+        self->hour = 12;
+      else if (self->hour > 12)
+        self->hour = self->hour - 12;
+    }
+  else
+    {
+      if (self->hour == 12 && self->is_am)
+        self->hour = 0;
+      else if (!self->is_am)
+        self->hour = self->hour + 12;
+    }
+
+  self->is_am_pm = !!is_am_pm;
+  time_entry_fill_time (self);
+}
diff --git a/panels/common/cc-time-entry.h b/panels/common/cc-time-entry.h
new file mode 100644
index 000000000..f3ddb8890
--- /dev/null
+++ b/panels/common/cc-time-entry.h
@@ -0,0 +1,48 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
+/* cc-time-entry.h
+ *
+ * Copyright 2020 Purism SPC
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s):
+ *   Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define CC_TYPE_TIME_ENTRY (cc_time_entry_get_type ())
+
+G_DECLARE_FINAL_TYPE (CcTimeEntry, cc_time_entry, CC, TIME_ENTRY, GtkEntry)
+
+GtkWidget *cc_time_entry_new        (void);
+void       cc_time_entry_set_time   (CcTimeEntry *self,
+                                     guint        hour,
+                                     guint        minute);
+guint      cc_time_entry_get_minute (CcTimeEntry *self);
+guint      cc_time_entry_get_hour   (CcTimeEntry *self);
+gboolean   cc_time_entry_get_is_am  (CcTimeEntry *self);
+void       cc_time_entry_set_is_am  (CcTimeEntry *self,
+                                     gboolean     is_am);
+gboolean   cc_time_entry_get_am_pm  (CcTimeEntry *self);
+void       cc_time_entry_set_am_pm  (CcTimeEntry *self,
+                                     gboolean     is_am_pm);
+
+G_END_DECLS
diff --git a/panels/common/common.gresource.xml b/panels/common/common.gresource.xml
index fb421b48f..9620dc110 100644
--- a/panels/common/common.gresource.xml
+++ b/panels/common/common.gresource.xml
@@ -3,6 +3,7 @@
   <gresource prefix="/org/gnome/control-center/common">
     <file preprocess="xml-stripblanks">cc-language-chooser.ui</file>
     <file preprocess="xml-stripblanks">cc-list-row.ui</file>
+    <file preprocess="xml-stripblanks">cc-time-editor.ui</file>
     <file preprocess="xml-stripblanks">cc-permission-infobar.ui</file>
   </gresource>
 </gresources>
diff --git a/panels/common/meson.build b/panels/common/meson.build
index 32f18dfac..85fabf21e 100644
--- a/panels/common/meson.build
+++ b/panels/common/meson.build
@@ -26,6 +26,7 @@ common_sources += gnome.mkenums(
 
 sources = files(
   'cc-hostname-entry.c',
+  'cc-time-entry.c',
   'cc-os-release.c',
   'hostname-helper.c',
   'list-box-helper.c',
@@ -46,6 +47,7 @@ sources = common_sources + files(
   'cc-common-language.c',
   'cc-language-chooser.c',
   'cc-list-row.c',
+  'cc-time-editor.c',
   'cc-permission-infobar.c',
   'cc-util.c'
 )
@@ -53,6 +55,7 @@ sources = common_sources + files(
 resource_data = files(
   'cc-language-chooser.ui',
   'cc-list-row.ui',
+  'cc-time-editor.ui',
   'cc-permission-infobar.ui',
 )
 



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