From 33d101062d370c29c224edf3cb6a6aa21037a0bf Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Wed, 17 Dec 2014 09:24:58 -0200 Subject: [PATCH] time-selector: implemented time selection with spin buttons --- data/Makefile.am | 1 + data/calendar.gresource.xml | 1 + data/ui/edit-dialog.ui | 4 +- data/ui/time-selector.ui | 69 ++++++ src/Makefile.am | 4 +- src/gcal-edit-dialog.c | 56 ++--- src/gcal-time-entry.c | 407 ------------------------------------ src/gcal-time-entry.h | 68 ------ src/gcal-time-selector.c | 211 +++++++++++++++++++ src/gcal-time-selector.h | 68 ++++++ 10 files changed, 382 insertions(+), 507 deletions(-) create mode 100644 data/ui/time-selector.ui delete mode 100644 src/gcal-time-entry.c delete mode 100644 src/gcal-time-entry.h create mode 100644 src/gcal-time-selector.c create mode 100644 src/gcal-time-selector.h diff --git a/data/Makefile.am b/data/Makefile.am index 3cd9e750..3717c191 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -24,6 +24,7 @@ EXTRA_DIST= \ ui/menus.ui \ ui/nav_bar.ui \ ui/new-event.ui \ + ui/time-selector.ui \ ui/window.ui \ theme/gtk-styles.css \ $(desktop_in_files) \ diff --git a/data/calendar.gresource.xml b/data/calendar.gresource.xml index 6a85c7b9..0ce5b824 100644 --- a/data/calendar.gresource.xml +++ b/data/calendar.gresource.xml @@ -4,6 +4,7 @@ ui/edit-dialog.ui ui/menus.ui ui/nav_bar.ui + ui/time-selector.ui ui/window.ui theme/gtk-styles.css diff --git a/data/ui/edit-dialog.ui b/data/ui/edit-dialog.ui index 3710bce7..9544ec75 100644 --- a/data/ui/edit-dialog.ui +++ b/data/ui/edit-dialog.ui @@ -237,7 +237,7 @@ - + True True @@ -257,7 +257,7 @@ - + True True diff --git a/data/ui/time-selector.ui b/data/ui/time-selector.ui new file mode 100644 index 00000000..1e2ab422 --- /dev/null +++ b/data/ui/time-selector.ui @@ -0,0 +1,69 @@ + + + + 23 + 1 + 5 + + + 59 + 1 + 10 + + + True + False + 12 + 6 + + + True + True + 0.5 + number + vertical + hour_adjustment + + + 0 + 0 + + + + + True + True + 0.5 + number + vertical + minute_adjustment + + + 2 + 0 + + + + + True + False + : + + + 1 + 0 + + + + + True + False + center + + + 3 + 0 + + + + diff --git a/src/Makefile.am b/src/Makefile.am index fc42f0dc..ab2bdf22 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -45,8 +45,8 @@ gnome_calendar_SOURCES = \ gcal-event-widget.h \ gcal-edit-dialog.c \ gcal-edit-dialog.h \ - gcal-time-entry.c \ - gcal-time-entry.h \ + gcal-time-selector.c \ + gcal-time-selector.h \ gcal-date-entry.c \ gcal-date-entry.h \ gcal-manager.c \ diff --git a/src/gcal-edit-dialog.c b/src/gcal-edit-dialog.c index e4dc308b..f80c7ecd 100644 --- a/src/gcal-edit-dialog.c +++ b/src/gcal-edit-dialog.c @@ -18,7 +18,7 @@ */ #include "gcal-edit-dialog.h" -#include "gcal-time-entry.h" +#include "gcal-time-selector.h" #include "gcal-date-entry.h" #include "gcal-utils.h" @@ -50,8 +50,8 @@ typedef struct GtkWidget *start_date_entry; GtkWidget *end_date_entry; GtkWidget *all_day_check; - GtkWidget *start_time_entry; - GtkWidget *end_time_entry; + GtkWidget *start_time_selector; + GtkWidget *end_time_selector; GtkWidget *location_entry; GtkWidget *notes_text; @@ -416,7 +416,7 @@ update_time (GtkEntry *entry, if (icaltime_compare (*start_date, *end_date) > -1) { /* change the non-editing entry */ - if (GTK_WIDGET (entry) == priv->start_time_entry) + if (GTK_WIDGET (entry) == priv->start_time_selector) { end_date->hour = start_date->hour + 1; end_date->minute = start_date->minute; @@ -430,22 +430,22 @@ update_time (GtkEntry *entry, } /* update the entries with the new sane values */ - g_signal_handlers_block_by_func (priv->start_time_entry, + g_signal_handlers_block_by_func (priv->start_time_selector, update_time, user_data); - g_signal_handlers_block_by_func (priv->end_time_entry, + g_signal_handlers_block_by_func (priv->end_time_selector, update_time, user_data); /* updates date as well, since hours can change the current date */ - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->start_time_entry), + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->start_time_selector), start_date->hour, start_date->minute); gcal_date_entry_set_date (GCAL_DATE_ENTRY (priv->start_date_entry), start_date->day, start_date->month, start_date->year); - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->end_time_entry), + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->end_time_selector), end_date->hour, end_date->minute); gcal_date_entry_set_date (GCAL_DATE_ENTRY (priv->end_date_entry), @@ -453,10 +453,10 @@ update_time (GtkEntry *entry, end_date->month, end_date->year); - g_signal_handlers_unblock_by_func (priv->start_time_entry, + g_signal_handlers_unblock_by_func (priv->start_time_selector, update_time, user_data); - g_signal_handlers_unblock_by_func (priv->end_time_entry, + g_signal_handlers_unblock_by_func (priv->end_time_selector, update_date, user_data); } @@ -502,9 +502,9 @@ gcal_edit_dialog_class_init (GcalEditDialogClass *klass) gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, sources_button); /* Entries */ gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, summary_entry); - gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, start_time_entry); + gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, start_time_selector); gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, start_date_entry); - gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, end_time_entry); + gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, end_time_selector); gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, end_date_entry); gtk_widget_class_bind_template_child_private (widget_class, GcalEditDialog, location_entry); /* Other */ @@ -573,12 +573,12 @@ gcal_edit_dialog_constructed (GObject* object) /* bind all-day check button & time entries */ g_object_bind_property (priv->all_day_check, "active", - priv->start_time_entry, + priv->start_time_selector, "sensitive", G_BINDING_DEFAULT | G_BINDING_INVERT_BOOLEAN); g_object_bind_property (priv->all_day_check, "active", - priv->end_time_entry, + priv->end_time_selector, "sensitive", G_BINDING_DEFAULT | G_BINDING_INVERT_BOOLEAN); @@ -636,12 +636,12 @@ gcal_edit_dialog_constructed (GObject* object) G_CALLBACK (update_date), object); - g_signal_connect (priv->start_time_entry, + g_signal_connect (priv->start_time_selector, "modified", G_CALLBACK (update_time), object); - g_signal_connect (priv->end_time_entry, + g_signal_connect (priv->end_time_selector, "modified", G_CALLBACK (update_time), object); @@ -696,14 +696,14 @@ gcal_edit_dialog_set_writable (GcalEditDialog *dialog, gtk_editable_set_editable (GTK_EDITABLE (priv->summary_entry), writable); gtk_editable_set_editable (GTK_EDITABLE (priv->start_date_entry), writable); - gtk_editable_set_editable (GTK_EDITABLE (priv->start_time_entry), writable); - gtk_editable_set_editable (GTK_EDITABLE (priv->end_time_entry), writable); - gtk_editable_set_editable (GTK_EDITABLE (priv->end_time_entry), writable); + gtk_editable_set_editable (GTK_EDITABLE (priv->end_date_entry), writable); gtk_editable_set_editable (GTK_EDITABLE (priv->location_entry), writable); gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->notes_text), writable); gtk_widget_set_sensitive (priv->all_day_check, writable); + gtk_widget_set_sensitive (priv->end_time_selector, writable); + gtk_widget_set_sensitive (priv->start_time_selector, writable); gtk_button_set_label (GTK_BUTTON (priv->done_button), writable ? _("Save") : _("Done")); @@ -737,9 +737,9 @@ gcal_edit_dialog_clear_data (GcalEditDialog *dialog) /* date and time */ gtk_entry_set_text (GTK_ENTRY (priv->start_date_entry), ""); - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->start_time_entry), 0, 0); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->start_time_selector), 0, 0); gtk_entry_set_text (GTK_ENTRY (priv->end_date_entry), ""); - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->end_time_entry), 0, 0); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->end_time_selector), 0, 0); /* location */ g_signal_handlers_block_by_func (priv->location_entry, @@ -793,8 +793,8 @@ gcal_edit_dialog_all_day_changed (GtkWidget *widget, if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->all_day_check))) { - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->start_time_entry), 0, 0); - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->end_time_entry), 0, 0); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->start_time_selector), 0, 0); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->end_time_selector), 0, 0); } } @@ -912,7 +912,7 @@ gcal_edit_dialog_set_event_data (GcalEditDialog *dialog, dtstart.value->hour = 0; dtstart.value->minute = 0; } - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->start_time_entry), dtstart.value->hour, dtstart.value->minute); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->start_time_selector), dtstart.value->hour, dtstart.value->minute); /* end date */ e_cal_component_get_dtend (priv->component, &dtend); @@ -923,13 +923,13 @@ gcal_edit_dialog_set_event_data (GcalEditDialog *dialog, all_day = (dtstart.value->is_date == 1 && dtend.value->is_date == 1); if (!all_day) - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->end_time_entry), dtend.value->hour, dtend.value->minute); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->end_time_selector), dtend.value->hour, dtend.value->minute); } else { gcal_date_entry_set_date (GCAL_DATE_ENTRY (priv->end_date_entry), dtstart.value->day, dtstart.value->month, dtstart.value->year); - gcal_time_entry_set_time (GCAL_TIME_ENTRY (priv->end_time_entry), dtstart.value->hour, dtstart.value->minute); + gcal_time_selector_set_time (GCAL_TIME_SELECTOR (priv->end_time_selector), dtstart.value->hour, dtstart.value->minute); all_day = FALSE; } @@ -1079,7 +1079,7 @@ gcal_edit_dialog_get_start_date (GcalEditDialog *dialog) date->year = value3; value1 = value2 = 0; - gcal_time_entry_get_time (GCAL_TIME_ENTRY (priv->start_time_entry), + gcal_time_selector_get_time (GCAL_TIME_SELECTOR (priv->start_time_selector), &value1, &value2); date->hour = value1; @@ -1120,7 +1120,7 @@ gcal_edit_dialog_get_end_date (GcalEditDialog *dialog) date->year = value3; value1 = value2 = 0; - gcal_time_entry_get_time (GCAL_TIME_ENTRY (priv->end_time_entry), + gcal_time_selector_get_time (GCAL_TIME_SELECTOR (priv->end_time_selector), &value1, &value2); date->hour = value1; diff --git a/src/gcal-time-entry.c b/src/gcal-time-entry.c deleted file mode 100644 index 2d0e97b2..00000000 --- a/src/gcal-time-entry.c +++ /dev/null @@ -1,407 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * gcal-time-entry.c - * Copyright (C) 2012 Erick Pérez Castellanos - * - * gnome-calendar 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. - * - * gnome-calendar 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 . - */ - -#include "gcal-time-entry.h" - -#include - -struct _GcalTimeEntryPrivate -{ - gboolean internal_delete; - - gint hours; - gint minutes; -}; - -enum -{ - MODIFIED, - NUM_SIGNALS -}; - -static guint signals[NUM_SIGNALS] = { 0, }; - - -static void gtk_editable_iface_init (GtkEditableInterface *iface); - -static void gcal_time_entry_constructed (GObject *object); - -static gboolean gcal_time_entry_key_press_event (GtkWidget *widget, - GdkEventKey *event); - -static void gcal_time_entry_insert_text (GtkEditable *editable, - const gchar *new_text, - gint new_text_length, - gint *position); - -static void gcal_time_entry_delete_text (GtkEditable *editable, - gint start_pos, - gint end_pos); - -G_DEFINE_TYPE_WITH_CODE (GcalTimeEntry, - gcal_time_entry, - GTK_TYPE_ENTRY, - G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, - gtk_editable_iface_init)); - -static void -gcal_time_entry_class_init (GcalTimeEntryClass *klass) -{ - GtkWidgetClass *widget_class; - GObjectClass *object_class; - - widget_class = GTK_WIDGET_CLASS (klass); - widget_class->key_press_event = gcal_time_entry_key_press_event; - - object_class = G_OBJECT_CLASS (klass); - object_class->constructed = gcal_time_entry_constructed; - - signals[MODIFIED] = g_signal_new ("modified", - GCAL_TYPE_TIME_ENTRY, - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GcalTimeEntryClass, - modified), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, - 0); - - - g_type_class_add_private ((gpointer)klass, sizeof(GcalTimeEntryPrivate)); -} - -static void -gcal_time_entry_init (GcalTimeEntry *self) -{ - GcalTimeEntryPrivate *priv; - gtk_widget_set_has_window (GTK_WIDGET (self), FALSE); - - self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - GCAL_TYPE_TIME_ENTRY, - GcalTimeEntryPrivate); - priv = self->priv; - - priv->internal_delete = FALSE; - priv->hours = 0; - priv->minutes = 0; -} - -static void -gtk_editable_iface_init (GtkEditableInterface *iface) -{ - iface->insert_text = gcal_time_entry_insert_text; - iface->delete_text = gcal_time_entry_delete_text; -} - -static void -gcal_time_entry_constructed (GObject *object) -{ - /* chaining up */ - G_OBJECT_CLASS (gcal_time_entry_parent_class)->constructed (object); - - gtk_entry_set_width_chars (GTK_ENTRY (object), 8); - gtk_entry_set_alignment (GTK_ENTRY (object), 0.5); - - gtk_entry_set_icon_from_icon_name (GTK_ENTRY (object), - GTK_ENTRY_ICON_SECONDARY, - "preferences-system-time-symbolic"); - gtk_entry_set_icon_tooltip_text (GTK_ENTRY (object), - GTK_ENTRY_ICON_SECONDARY, - _("Change the time")); - - /* default text */ - gtk_entry_set_text (GTK_ENTRY (object), "00:00"); -} - -static gboolean -gcal_time_entry_key_press_event (GtkWidget *widget, - GdkEventKey *event) -{ - GcalTimeEntryPrivate *priv; - gint cursor_position; - gchar *new_time; - - priv = GCAL_TIME_ENTRY (widget)->priv; - - if (event->keyval == GDK_KEY_Up) - { - g_object_get (widget, "cursor-position", &cursor_position, NULL); - if (cursor_position == 0 || - cursor_position == 1) - { - priv->hours = (priv->hours + 1) % 24; - } - else if (cursor_position == 3 || - cursor_position == 4) - { - priv->minutes = (priv->minutes + 1) % 60; - } - - new_time = g_strdup_printf ("%.2d:%.2d", priv->hours, priv->minutes); - gtk_entry_set_text (GTK_ENTRY (widget), new_time); - g_free (new_time); - - g_signal_emit_by_name (widget, "move-cursor", 0, cursor_position, FALSE, NULL); - return TRUE; - } - else if (event->keyval == GDK_KEY_Down) - { - g_object_get (widget, "cursor-position", &cursor_position, NULL); - if (cursor_position == 0 || - cursor_position == 1) - { - priv->hours = priv->hours == 0 ? 23 : priv->hours - 1; - } - else if (cursor_position == 3 || - cursor_position == 4) - { - priv->minutes = priv->minutes == 0 ? 59 : priv->minutes - 1; - } - - new_time = g_strdup_printf ("%.2d:%.2d", priv->hours, priv->minutes); - gtk_entry_set_text (GTK_ENTRY (widget), new_time); - g_free (new_time); - - g_signal_emit_by_name (widget, "move-cursor", 0, cursor_position, FALSE, NULL); - return TRUE; - } - - GTK_WIDGET_CLASS (gcal_time_entry_parent_class)->key_press_event (widget, event); - - return FALSE; -} - -static void -gcal_time_entry_insert_text (GtkEditable *editable, - const gchar *new_text, - gint new_text_length, - gint *position) -{ - GcalTimeEntryPrivate *priv; - GtkEditableInterface *parent_editable_iface; - gint i; - gboolean had_colon; - gchar *text_to_insert; - gchar *old_text; - gchar *hours_text; - gint next_pos; - - priv = GCAL_TIME_ENTRY (editable)->priv; - - parent_editable_iface = g_type_interface_peek (gcal_time_entry_parent_class, - GTK_TYPE_EDITABLE); - next_pos = *position + new_text_length; - - if (*position > 4 || - *position == 2) - { - return; - } - - new_text_length = new_text_length + *position > 5 ? 5 - *position : new_text_length; - text_to_insert = g_malloc0 (sizeof(gchar) * new_text_length); - old_text = g_strdup_printf ("%.2d:%.2d", priv->hours, priv->minutes); - - had_colon = FALSE; - for (i = 0; i < new_text_length; i++) - { - if (new_text[i] == 0x3a) - { - had_colon = TRUE; - continue; - } - if (new_text[i] < 0x30 || new_text[i] > 0x39) - text_to_insert[i - (had_colon ? 1 : 0)] = old_text[*position + i]; - else - text_to_insert[i - (had_colon ? 1 : 0)] = new_text[i]; - } - - if (text_to_insert[0] == '\0') - return; - - if (had_colon) - new_text_length--; - - switch (*position) - { - case 0: - switch (new_text_length) - { - case 4: - if (text_to_insert[2] <= '5') - { - old_text[3] = text_to_insert[2]; - } - old_text[4] = text_to_insert[3]; - case 2: - if (text_to_insert[0] == '2') - { - if (text_to_insert[1] <= '3') - old_text[1] = text_to_insert[1]; - else if (old_text[1] > '3') - old_text[1] = '3'; - } - else - { - old_text[1] = text_to_insert[1]; - } - case 1: - if (text_to_insert[0] <= '2') - { - old_text[0] = text_to_insert[0]; - - if (text_to_insert[0] == '2' && - old_text[1] > '3') - { - old_text[1] = '3'; - } - } - break; - default: - return; - } - break; - case 1: - /* this is the only case I will analyze here */ - if (new_text_length == 1) - { - if (old_text[0] == '2') - { - if (text_to_insert[0] < '4') - old_text[1] = text_to_insert[0]; - } - else - { - old_text[1] = text_to_insert[0]; - } - } - break; - case 3: - switch (new_text_length) - { - case 2: - old_text[4] = text_to_insert[1]; - case 1: - if (text_to_insert[0] <= '5') - { - old_text[3] = text_to_insert[0]; - } - break; - default: - return; - } - break; - case 4: - old_text[4] = text_to_insert[0]; - break; - - default: - return; - } - - /* setting hours, minutes */ - hours_text = g_strdup_printf ("%.2s", old_text); - priv->hours = (gint) g_ascii_strtoull (hours_text, NULL, 10); - g_free (hours_text); - priv->minutes = (gint) g_ascii_strtoull (old_text + 3, NULL, 10); - - priv->internal_delete = TRUE; - gtk_editable_delete_text (editable, 0, 5); - priv->internal_delete = FALSE; - *position = 0; - parent_editable_iface->insert_text (editable, - old_text, - 5, - position); - - /* skipping colon */ - if (next_pos == 2) - next_pos = 3; - *position = next_pos; - g_free (old_text); - g_free (text_to_insert); - - /* sending modified signal */ - g_signal_emit (editable, signals[MODIFIED], 0); -} - -static void -gcal_time_entry_delete_text (GtkEditable *editable, - gint start_pos, - gint end_pos) -{ - GcalTimeEntryPrivate *priv; - GtkEditableInterface *parent_editable_iface; - - priv = GCAL_TIME_ENTRY (editable)->priv; - parent_editable_iface = g_type_interface_peek (gcal_time_entry_parent_class, - GTK_TYPE_EDITABLE); - if (! priv->internal_delete) - { - /* FIXME: Handle delete_text */ - /* handle delete */ - } - - parent_editable_iface->delete_text (editable, - start_pos, - end_pos); - -} - -/* Public API */ -GtkWidget* -gcal_time_entry_new (void) -{ - return g_object_new (GCAL_TYPE_TIME_ENTRY, NULL); -} - -void -gcal_time_entry_set_time (GcalTimeEntry *entry, - gint hours, - gint minutes) -{ - GcalTimeEntryPrivate *priv; - gchar *new_time; - - g_return_if_fail (GCAL_IS_TIME_ENTRY (entry)); - priv = entry->priv; - g_warn_if_fail (hours < 24); - g_warn_if_fail (minutes < 60); - - priv->hours = hours < 24 ? hours : 0; - priv->minutes = minutes < 60 ? minutes : 0; - - new_time = g_strdup_printf ("%.2d:%.2d", priv->hours, priv->minutes); - gtk_entry_set_text (GTK_ENTRY (entry), new_time); - g_free (new_time); -} - -void -gcal_time_entry_get_time (GcalTimeEntry *entry, - gint *hours, - gint *minutes) -{ - GcalTimeEntryPrivate *priv; - - g_return_if_fail (GCAL_IS_TIME_ENTRY (entry)); - priv = entry->priv; - - *hours = priv->hours; - *minutes = priv->minutes; -} diff --git a/src/gcal-time-entry.h b/src/gcal-time-entry.h deleted file mode 100644 index a895a798..00000000 --- a/src/gcal-time-entry.h +++ /dev/null @@ -1,68 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* - * gcal-time-entry.h - * Copyright (C) 2012 Erick Pérez Castellanos - * - * gnome-calendar 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. - * - * gnome-calendar 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 . - */ - -#ifndef __GCAL_TIME_ENTRY_H__ -#define __GCAL_TIME_ENTRY_H__ - -#include - -G_BEGIN_DECLS - -#define GCAL_TYPE_TIME_ENTRY (gcal_time_entry_get_type ()) -#define GCAL_TIME_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCAL_TYPE_TIME_ENTRY, GcalTimeEntry)) -#define GCAL_TIME_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCAL_TYPE_TIME_ENTRY, GcalTimeEntryClass)) -#define GCAL_IS_TIME_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCAL_TYPE_TIME_ENTRY)) -#define GCAL_IS_TIME_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCAL_TYPE_TIME_ENTRY)) -#define GCAL_TIME_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCAL_TYPE_TIME_ENTRY, GcalTimeEntryClass)) - -typedef struct _GcalTimeEntry GcalTimeEntry; -typedef struct _GcalTimeEntryClass GcalTimeEntryClass; -typedef struct _GcalTimeEntryPrivate GcalTimeEntryPrivate; - -struct _GcalTimeEntry -{ - GtkEntry parent; - /* add your public declarations here */ - - GcalTimeEntryPrivate *priv; -}; - -struct _GcalTimeEntryClass -{ - GtkEntryClass parent_class; - - /* signals */ - void (*modified) (GcalTimeEntry *entry); -}; - -GType gcal_time_entry_get_type (void); - -GtkWidget* gcal_time_entry_new (void); - -void gcal_time_entry_set_time (GcalTimeEntry *entry, - gint hours, - gint minutes); - -void gcal_time_entry_get_time (GcalTimeEntry *entry, - gint *hours, - gint *minutes); - -G_END_DECLS - -#endif /* __GCAL_TIME_ENTRY_H__ */ diff --git a/src/gcal-time-selector.c b/src/gcal-time-selector.c new file mode 100644 index 00000000..14850b01 --- /dev/null +++ b/src/gcal-time-selector.c @@ -0,0 +1,211 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * gcal-time-selector.c + * Copyright (C) 2012 Erick Pérez Castellanos + * 2014 Georges Basile Stavracas Neto + * + * gnome-calendar 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. + * + * gnome-calendar 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 . + */ + +#include "gcal-time-selector.h" + +#include + +struct _GcalTimeSelectorPrivate +{ + GtkWidget *time_label; + GtkWidget *popover; + GtkWidget *hour_spin; + GtkWidget *minute_spin; + GtkWidget *period_check; + + gboolean format_24h; +}; + +enum +{ + MODIFIED, + NUM_SIGNALS +}; + +static guint signals[NUM_SIGNALS] = { 0, }; + +static gboolean on_output (GtkSpinButton *button, + gpointer user_data); + +static void gcal_time_selector_constructed (GObject *object); + +G_DEFINE_TYPE_WITH_PRIVATE (GcalTimeSelector, gcal_time_selector, GTK_TYPE_TOGGLE_BUTTON); + +static gboolean +on_output (GtkSpinButton *button, + gpointer user_data) +{ + gchar *text; + gint value; + + value = (gint) gtk_adjustment_get_value (gtk_spin_button_get_adjustment (button)); + text = g_strdup_printf ("%02d", value); + gtk_entry_set_text (GTK_ENTRY (button), text); + + g_free (text); + + return TRUE; +} + +static void +gcal_time_selector_class_init (GcalTimeSelectorClass *klass) +{ + GtkWidgetClass *widget_class; + GObjectClass *object_class; + + widget_class = GTK_WIDGET_CLASS (klass); + + object_class = G_OBJECT_CLASS (klass); + object_class->constructed = gcal_time_selector_constructed; + + signals[MODIFIED] = g_signal_new ("modified", + GCAL_TYPE_TIME_SELECTOR, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GcalTimeSelectorClass, + modified), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, + 0); + +} + +static void +gcal_time_selector_init (GcalTimeSelector *self) +{ + GcalTimeSelectorPrivate *priv; + + priv = gcal_time_selector_get_instance_private (self); + + priv->time_label = NULL; + priv->popover = NULL; + priv->hour_spin = NULL; + priv->minute_spin = NULL; +} + +static void +gcal_time_selector_constructed (GObject *object) +{ + GcalTimeSelectorPrivate *priv; + GtkWidget *grid; + GtkBuilder *builder; + + GSettings *settings; + gchar *clock_format; + + priv = gcal_time_selector_get_instance_private (GCAL_TIME_SELECTOR (object)); + + /* chaining up */ + G_OBJECT_CLASS (gcal_time_selector_parent_class)->constructed (object); + + gtk_widget_set_hexpand (GTK_WIDGET (object), TRUE); + + /* 24h setting */ + settings = g_settings_new ("org.gnome.desktop.interface"); + clock_format = g_settings_get_string (settings, "clock-format"); + priv->format_24h = (g_strcmp0 (clock_format, "24h") == 0); + + g_free (clock_format); + g_object_unref (settings); + + /* time label */ + priv->time_label = gtk_label_new (NULL); + gtk_label_set_label (GTK_LABEL (priv->time_label), "00:00"); + gtk_widget_show (priv->time_label); + + gtk_container_add (GTK_CONTAINER (object), priv->time_label); + + /* popover */ + builder = gtk_builder_new (); + gtk_builder_add_from_resource (builder, "/org/gnome/calendar/time-selector.ui", NULL); + + grid = (GtkWidget*) gtk_builder_get_object (builder, "grid"); + priv->popover = gtk_popover_new (GTK_WIDGET (object)); + priv->hour_spin = (GtkWidget*) gtk_builder_get_object (builder, "hour_spin"); + priv->minute_spin = (GtkWidget*) gtk_builder_get_object (builder, "minute_spin"); + + g_object_unref (builder); + + gtk_container_add (GTK_CONTAINER (priv->popover), grid); + g_object_bind_property (priv->popover, "visible", object, "active", G_BINDING_BIDIRECTIONAL); + + /* signals */ + g_signal_connect (priv->hour_spin, "output", G_CALLBACK (on_output), object); + g_signal_connect (priv->minute_spin, "output", G_CALLBACK (on_output), object); +} + +/* Public API */ +GtkWidget* +gcal_time_selector_new (void) +{ + return g_object_new (GCAL_TYPE_TIME_SELECTOR, NULL); +} + +void +gcal_time_selector_set_time (GcalTimeSelector *selector, + gint hours, + gint minutes) +{ + GcalTimeSelectorPrivate *priv; + GtkAdjustment *hour_adj; + GtkAdjustment *minute_adj; + gchar *new_time; + + g_return_if_fail (GCAL_IS_TIME_SELECTOR (selector)); + priv = gcal_time_selector_get_instance_private (selector); + hour_adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->hour_spin)); + minute_adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->minute_spin)); + g_warn_if_fail (hours < 24); + g_warn_if_fail (minutes < 60); + + gtk_adjustment_set_value (hour_adj, hours < 24 ? hours : 0); + gtk_adjustment_set_value (minute_adj, minutes < 60 ? minutes : 0); + + if (priv->format_24h) + { + new_time = g_strdup_printf ("%.2d:%.2d", hours, minutes); + } + else + { + new_time = g_strdup_printf ("%.2d:%.2d", hours, minutes); + } + + gtk_label_set_label (GTK_LABEL (priv->time_label), new_time); + + g_free (new_time); +} + +void +gcal_time_selector_get_time (GcalTimeSelector *selector, + gint *hours, + gint *minutes) +{ + GcalTimeSelectorPrivate *priv; + GtkAdjustment *hour_adj; + GtkAdjustment *minute_adj; + + g_return_if_fail (GCAL_IS_TIME_SELECTOR (selector)); + priv = gcal_time_selector_get_instance_private (selector); + hour_adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->hour_spin)); + minute_adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->minute_spin)); + + *hours = (gint) gtk_adjustment_get_value (hour_adj); + *minutes = (gint) gtk_adjustment_get_value (minute_adj); +} diff --git a/src/gcal-time-selector.h b/src/gcal-time-selector.h new file mode 100644 index 00000000..1aeb453c --- /dev/null +++ b/src/gcal-time-selector.h @@ -0,0 +1,68 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * gcal-time-selector.h + * Copyright (C) 2012 Erick Pérez Castellanos + * + * gnome-calendar 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. + * + * gnome-calendar 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 . + */ + +#ifndef __GCAL_TIME_SELECTOR_H__ +#define __GCAL_TIME_SELECTOR_H__ + +#include + +G_BEGIN_DECLS + +#define GCAL_TYPE_TIME_SELECTOR (gcal_time_selector_get_type ()) +#define GCAL_TIME_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GCAL_TYPE_TIME_SELECTOR, GcalTimeSelector)) +#define GCAL_TIME_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GCAL_TYPE_TIME_SELECTOR, GcalTimeSelectorClass)) +#define GCAL_IS_TIME_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCAL_TYPE_TIME_SELECTOR)) +#define GCAL_IS_TIME_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GCAL_TYPE_TIME_SELECTOR)) +#define GCAL_TIME_SELECTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GCAL_TYPE_TIME_SELECTOR, GcalTimeSelectorClass)) + +typedef struct _GcalTimeSelector GcalTimeSelector; +typedef struct _GcalTimeSelectorClass GcalTimeSelectorClass; +typedef struct _GcalTimeSelectorPrivate GcalTimeSelectorPrivate; + +struct _GcalTimeSelector +{ + GtkToggleButton parent; + /* add your public declarations here */ + + GcalTimeSelectorPrivate *priv; +}; + +struct _GcalTimeSelectorClass +{ + GtkToggleButtonClass parent_class; + + /* signals */ + void (*modified) (GcalTimeSelector *selector); +}; + +GType gcal_time_selector_get_type (void); + +GtkWidget* gcal_time_selector_new (void); + +void gcal_time_selector_set_time (GcalTimeSelector *selector, + gint hours, + gint minutes); + +void gcal_time_selector_get_time (GcalTimeSelector *selector, + gint *hours, + gint *minutes); + +G_END_DECLS + +#endif /* __GCAL_TIME_SELECTOR_H__ */