]>
Commit | Line | Data |
---|---|---|
93cfbc21 RN |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
4 | * | |
5 | * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | #include "vikdatetime_edit_dialog.h" | |
23 | ||
24 | // Show leading zeros | |
25 | static gboolean on_output ( GtkSpinButton *spin, gpointer data ) | |
26 | { | |
27 | GtkAdjustment *adjustment = gtk_spin_button_get_adjustment ( spin ); | |
28 | gint value = (gint)gtk_adjustment_get_value ( adjustment ); | |
29 | gchar *text = g_strdup_printf ( "%02d", value ); | |
30 | gtk_entry_set_text ( GTK_ENTRY (spin), text ); | |
31 | g_free ( text ); | |
32 | ||
33 | return TRUE; | |
34 | } | |
35 | ||
36 | /** | |
37 | * vik_datetime_edit_dialog: | |
38 | * @parent: The parent window | |
39 | * @title: The title to use for the dialog | |
40 | * @initial_time: The inital date/time to be shown | |
41 | * @tz: The #GTimeZone this dialog will operate in | |
42 | * | |
43 | * Returns: A time selected by the user via this dialog | |
44 | * Even though a time of zero is notionally valid - consider it unlikely to be actually wanted! | |
45 | * Thus if the time is zero then the dialog was cancelled or somehow an invalid date was encountered. | |
46 | */ | |
47 | time_t vik_datetime_edit_dialog ( GtkWindow *parent, const gchar *title, time_t initial_time, GTimeZone *tz ) | |
48 | { | |
49 | g_return_val_if_fail ( tz, 0 ); | |
50 | ||
51 | GtkWidget *dialog = gtk_dialog_new_with_buttons ( title, | |
52 | parent, | |
53 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | |
54 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | |
55 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, | |
56 | NULL ); | |
57 | ||
58 | gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT ); | |
59 | GtkWidget *response_w = NULL; | |
60 | #if GTK_CHECK_VERSION (2, 20, 0) | |
61 | response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT ); | |
62 | #endif | |
63 | ||
64 | GtkWidget *label; | |
65 | GtkWidget *cal = gtk_calendar_new (); | |
66 | ||
67 | // Set according to the given date/time + timezone for display | |
68 | GDateTime *gdt_in = g_date_time_new_from_unix_utc ( (gint64)initial_time ); | |
69 | GDateTime *gdt_tz = g_date_time_to_timezone ( gdt_in, tz ); | |
70 | g_date_time_unref ( gdt_in ); | |
71 | ||
72 | gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(gdt_tz)-1, g_date_time_get_year (gdt_tz) ); | |
73 | gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(gdt_tz) ); | |
74 | ||
75 | GtkWidget *hbox_time = gtk_hbox_new ( FALSE, 1 ); | |
76 | ||
77 | label = gtk_label_new ( g_date_time_get_timezone_abbreviation(gdt_tz) ); | |
78 | gtk_box_pack_start ( GTK_BOX(hbox_time), label, FALSE, FALSE, 5 ); | |
79 | ||
80 | GtkWidget *sb_hours = gtk_spin_button_new_with_range ( 0.0, 23.0, 1.0 ); | |
81 | gtk_box_pack_start ( GTK_BOX(hbox_time), sb_hours, FALSE, FALSE, 0 ); | |
82 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_hours), g_date_time_get_hour(gdt_tz) ); | |
83 | g_signal_connect ( sb_hours, "output", G_CALLBACK(on_output), NULL ); | |
84 | ||
85 | label = gtk_label_new ( ":" ); | |
86 | gtk_box_pack_start ( GTK_BOX(hbox_time), label, FALSE, FALSE, 0 ); | |
87 | ||
88 | GtkWidget *sb_minutes = gtk_spin_button_new_with_range ( 0.0, 59.0, 1.0 ); | |
89 | gtk_box_pack_start ( GTK_BOX(hbox_time), sb_minutes, FALSE, FALSE, 0); | |
90 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_minutes), g_date_time_get_minute(gdt_tz) ); | |
91 | g_signal_connect ( sb_minutes, "output", G_CALLBACK(on_output), NULL ); | |
92 | ||
93 | label = gtk_label_new ( ":" ); | |
94 | gtk_box_pack_start(GTK_BOX(hbox_time), label, FALSE, FALSE, 0); | |
95 | ||
96 | GtkWidget *sb_seconds = gtk_spin_button_new_with_range ( 0.0, 59.0, 1.0 ); | |
97 | gtk_box_pack_start ( GTK_BOX(hbox_time), sb_seconds, FALSE, FALSE, 0 ); | |
98 | gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_seconds), g_date_time_get_second(gdt_tz) ); | |
99 | g_signal_connect ( sb_seconds, "output", G_CALLBACK(on_output), NULL ); | |
100 | ||
101 | gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0 ); | |
102 | gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox_time, FALSE, FALSE, 5 ); | |
103 | ||
104 | if ( response_w ) | |
105 | gtk_widget_grab_focus ( response_w ); | |
106 | ||
107 | g_date_time_unref ( gdt_tz ); | |
108 | ||
109 | gtk_widget_show_all ( dialog ); | |
110 | if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) { | |
111 | gtk_widget_destroy ( dialog ); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | // Read values | |
116 | guint year = 0; | |
117 | guint month = 0; | |
118 | guint day = 0; | |
119 | guint hours = 0; | |
120 | guint minutes = 0; | |
121 | guint seconds = 0; | |
122 | ||
123 | gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day ); | |
124 | hours = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_hours) ); | |
125 | minutes = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_minutes) ); | |
126 | seconds = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_seconds) ); | |
127 | ||
128 | gtk_widget_destroy(dialog); | |
129 | ||
130 | time_t ans = initial_time; | |
131 | GDateTime *gdt_ans = g_date_time_new ( tz, year, month+1, day, hours, minutes, (gdouble)seconds ); | |
132 | if ( gdt_ans ) { | |
133 | ans = g_date_time_to_unix ( gdt_ans ); | |
134 | g_date_time_unref ( gdt_ans ); | |
135 | } | |
136 | ||
137 | return ans; | |
138 | } |