1 
2 /*
3  * Osmo - a handy personal organizer
4  *
5  * Copyright (C) 2007 Tomasz Maka <pasp@users.sourceforge.net>
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 
23 #include "calendar_timeline.h"
24 #include "i18n.h"
25 #include "calendar.h"
26 #include "options_prefs.h"
27 #include "stock_icons.h"
28 
29 /*------------------------------------------------------------------------------*/
30 
31 void
insert_timeline_window_close_cb(GtkWidget * widget,GdkEvent * event,gpointer user_data)32 insert_timeline_window_close_cb (GtkWidget *widget, GdkEvent *event, gpointer user_data) {
33 
34     GUI *appGUI = (GUI *)user_data;
35     gtk_widget_destroy (appGUI->cal->insert_timeline_window);
36 }
37 
38 /*------------------------------------------------------------------------------*/
39 
40 void
button_insert_timeline_window_close_cb(GtkWidget * widget,gpointer data)41 button_insert_timeline_window_close_cb (GtkWidget *widget, gpointer data) {
42 
43     insert_timeline_window_close_cb (widget, NULL, data);
44 }
45 
46 /*------------------------------------------------------------------------------*/
47 
48 gint
insert_timeline_key_press_cb(GtkWidget * widget,GdkEventKey * event,gpointer data)49 insert_timeline_key_press_cb (GtkWidget *widget, GdkEventKey *event, gpointer data) {
50 
51     GUI *appGUI = (GUI *)data;
52 
53     if (event->keyval == GDK_KEY_Escape) {
54             insert_timeline_window_close_cb (NULL, NULL, appGUI);
55             return TRUE;
56     }
57 
58     return FALSE;
59 }
60 
61 /*------------------------------------------------------------------------------*/
62 
63 void
insert_timeline_action_cb(GtkButton * button,gpointer user_data)64 insert_timeline_action_cb (GtkButton *button, gpointer user_data) {
65 
66 gint ts, te, m;
67 GtkTextBuffer *text_buffer;
68 GtkTextIter titer;
69 gchar tmpbuf[BUFFER_SIZE];
70 
71     GUI *appGUI = (GUI *)user_data;
72 
73     text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(appGUI->cal->calendar_note_textview));
74     gtk_text_buffer_get_iter_at_offset(GTK_TEXT_BUFFER(text_buffer), &titer, -1);
75 
76     ts = config.timeline_start;
77     te = config.timeline_end + config.timeline_step;
78 
79     do {
80 
81         m = ts / 60;
82         g_snprintf(tmpbuf, BUFFER_SIZE, "%02d:%02d - \n", m, ts-m*60);
83         gtk_text_buffer_insert(text_buffer, &titer, tmpbuf, -1);
84 
85         ts += config.timeline_step;
86 
87     } while (ts < te);
88 
89     insert_timeline_window_close_cb (NULL, NULL, appGUI);
90 
91     enable_disable_note_buttons(appGUI);
92 }
93 
94 /*------------------------------------------------------------------------------*/
95 
96 void
fill_timeline(GUI * appGUI)97 fill_timeline(GUI *appGUI) {
98 
99 gint i;
100 
101     i = config.timeline_start / 60;
102     gtk_spin_button_set_value (GTK_SPIN_BUTTON(appGUI->cal->tl_start_h_spinbutton), i);
103     gtk_spin_button_set_value (GTK_SPIN_BUTTON(appGUI->cal->tl_start_m_spinbutton), config.timeline_start-i*60);
104     i = config.timeline_end / 60;
105     gtk_spin_button_set_value (GTK_SPIN_BUTTON(appGUI->cal->tl_end_h_spinbutton), i);
106     gtk_spin_button_set_value (GTK_SPIN_BUTTON(appGUI->cal->tl_end_m_spinbutton), config.timeline_end-i*60);
107     gtk_spin_button_set_value (GTK_SPIN_BUTTON(appGUI->cal->tl_step_spinbutton), (gdouble)config.timeline_step);
108 }
109 
110 /*------------------------------------------------------------------------------*/
111 
112 void
timeline_changed_cb(GtkSpinButton * spinbutton,gpointer user_data)113 timeline_changed_cb (GtkSpinButton *spinbutton, gpointer user_data) {
114 
115 gint i, j;
116 
117     GUI *appGUI = (GUI *)user_data;
118 
119     i = (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON(appGUI->cal->tl_start_h_spinbutton)) * 60 +
120         (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON(appGUI->cal->tl_start_m_spinbutton));
121     j = (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON(appGUI->cal->tl_end_h_spinbutton)) * 60 +
122         (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON(appGUI->cal->tl_end_m_spinbutton));
123 
124     if (i < j) {
125         config.timeline_start = i;
126         config.timeline_end = j;
127         config.timeline_step = (gint) gtk_spin_button_get_value (GTK_SPIN_BUTTON(appGUI->cal->tl_step_spinbutton));
128     } else {
129         fill_timeline(appGUI);
130     }
131 }
132 
133 /*------------------------------------------------------------------------------*/
134 
135 void
calendar_create_insert_timeline_window(GUI * appGUI)136 calendar_create_insert_timeline_window (GUI *appGUI) {
137 
138 GtkWidget   *vbox1;
139 GtkWidget   *label;
140 GtkWidget   *hseparator;
141 GtkWidget   *hbuttonbox;
142 GtkWidget   *timeline_table;
143 GtkWidget   *frame;
144 GtkAdjustment   *tl_start_h_spinbutton_adj;
145 GtkAdjustment   *tl_start_m_spinbutton_adj;
146 GtkAdjustment   *tl_end_h_spinbutton_adj;
147 GtkAdjustment   *tl_end_m_spinbutton_adj;
148 GtkAdjustment   *tl_step_spinbutton_adj;
149 gint        win_xpos, win_ypos;
150 gchar tmpbuf[BUFFER_SIZE];
151 
152     appGUI->cal->insert_timeline_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
153     gtk_window_set_title (GTK_WINDOW (appGUI->cal->insert_timeline_window), _("Insert timeline"));
154     gtk_window_set_position (GTK_WINDOW (appGUI->cal->insert_timeline_window), GTK_WIN_POS_CENTER_ON_PARENT);
155     gtk_window_set_modal (GTK_WINDOW (appGUI->cal->insert_timeline_window), TRUE);
156     g_signal_connect (G_OBJECT (appGUI->cal->insert_timeline_window), "delete_event",
157                       G_CALLBACK(insert_timeline_window_close_cb), appGUI);
158     g_signal_connect (G_OBJECT (appGUI->cal->insert_timeline_window), "key_press_event",
159                         G_CALLBACK (insert_timeline_key_press_cb), appGUI);
160     gtk_window_set_transient_for(GTK_WINDOW(appGUI->cal->insert_timeline_window), GTK_WINDOW(appGUI->main_window));
161     gtk_container_set_border_width (GTK_CONTAINER (appGUI->cal->insert_timeline_window), 8);
162     gtk_window_set_resizable (GTK_WINDOW (appGUI->cal->insert_timeline_window), FALSE);
163 
164     vbox1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
165     gtk_widget_show (vbox1);
166     gtk_container_add (GTK_CONTAINER (appGUI->cal->insert_timeline_window), vbox1);
167 
168     frame = gtk_frame_new (NULL);
169     gtk_widget_show (frame);
170     gtk_box_pack_start (GTK_BOX (vbox1), frame, FALSE, TRUE, 0);
171     gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
172     g_snprintf(tmpbuf, BUFFER_SIZE, "<b>%s</b>", _("Timeline"));
173     label = gtk_label_new (tmpbuf);
174     gtk_widget_show (label);
175     gtk_frame_set_label_widget (GTK_FRAME (frame), label);
176     gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
177     gtk_widget_set_margin_top (label, 4);
178     gtk_widget_set_margin_bottom(label, 4);
179 
180     timeline_table = gtk_grid_new ();
181     gtk_widget_show (timeline_table);
182     gtk_container_set_border_width (GTK_CONTAINER (timeline_table), 4);
183     gtk_grid_set_column_spacing (GTK_GRID (timeline_table), 8);
184     gtk_grid_set_row_spacing (GTK_GRID (timeline_table), 4);
185     gtk_widget_set_margin_left(timeline_table, 12);
186     gtk_widget_set_margin_bottom(timeline_table, 12);
187     gtk_container_add (GTK_CONTAINER (frame), timeline_table);
188 
189     g_snprintf(tmpbuf, BUFFER_SIZE, "%s:", _("From (hour)"));
190     label = gtk_label_new (tmpbuf);
191     gtk_widget_show (label);
192     gtk_grid_attach (GTK_GRID (timeline_table), label, 0, 0, 1, 1);
193     gtk_widget_set_valign(label, GTK_ALIGN_START);
194     gtk_widget_set_halign(label, GTK_ALIGN_CENTER);
195 
196     tl_start_h_spinbutton_adj = gtk_adjustment_new (1, 0, 24, 1, 10, 0);
197     appGUI->cal->tl_start_h_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (tl_start_h_spinbutton_adj), 1, 0);
198     gtk_widget_show (appGUI->cal->tl_start_h_spinbutton);
199     gtk_grid_attach (GTK_GRID (timeline_table), appGUI->cal->tl_start_h_spinbutton, 1, 0, 1, 1);
200     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->tl_start_h_spinbutton), TRUE);
201 
202     g_snprintf(tmpbuf, BUFFER_SIZE, "%s:", _("Step (minutes)"));
203     label = gtk_label_new (tmpbuf);
204     gtk_widget_show (label);
205     gtk_grid_attach (GTK_GRID (timeline_table), label, 0, 2, 1, 1);
206     gtk_widget_set_valign(label, GTK_ALIGN_START);
207     gtk_widget_set_halign(label, GTK_ALIGN_CENTER);
208 
209     tl_step_spinbutton_adj = gtk_adjustment_new (1, 1, 120, 1, 10, 0);
210     appGUI->cal->tl_step_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (tl_step_spinbutton_adj), 1, 0);
211     gtk_widget_show (appGUI->cal->tl_step_spinbutton);
212     gtk_grid_attach (GTK_GRID (timeline_table), appGUI->cal->tl_step_spinbutton, 1, 2, 1, 1);
213     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->tl_step_spinbutton), TRUE);
214 
215     g_snprintf(tmpbuf, BUFFER_SIZE, "%s:", _("To (hour)"));
216     label = gtk_label_new (tmpbuf);
217     gtk_widget_show (label);
218     gtk_grid_attach (GTK_GRID (timeline_table), label, 0, 1, 1, 1);
219     gtk_widget_set_valign(label, GTK_ALIGN_START);
220     gtk_widget_set_halign(label, GTK_ALIGN_CENTER);
221 
222     tl_end_h_spinbutton_adj = gtk_adjustment_new (1, 1, 23, 1, 10, 0);
223     appGUI->cal->tl_end_h_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (tl_end_h_spinbutton_adj), 1, 0);
224     gtk_widget_show (appGUI->cal->tl_end_h_spinbutton);
225     gtk_grid_attach (GTK_GRID (timeline_table), appGUI->cal->tl_end_h_spinbutton, 1, 1, 1, 1);
226     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->tl_end_h_spinbutton), TRUE);
227 
228     label = gtk_label_new (":");
229     gtk_widget_show (label);
230     gtk_grid_attach (GTK_GRID (timeline_table), label, 2, 0, 1, 1);
231 
232     label = gtk_label_new (":");
233     gtk_widget_show (label);
234     gtk_grid_attach (GTK_GRID (timeline_table), label, 2, 1, 1, 1);
235 
236     tl_start_m_spinbutton_adj = gtk_adjustment_new (0, 0, 59, 1, 10, 0);
237     appGUI->cal->tl_start_m_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (tl_start_m_spinbutton_adj), 1, 0);
238     gtk_widget_show (appGUI->cal->tl_start_m_spinbutton);
239     gtk_grid_attach (GTK_GRID (timeline_table), appGUI->cal->tl_start_m_spinbutton, 3, 0, 1, 1);
240     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->tl_start_m_spinbutton), TRUE);
241 
242     tl_end_m_spinbutton_adj = gtk_adjustment_new (0, 0, 59, 1, 10, 0);
243     appGUI->cal->tl_end_m_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (tl_end_m_spinbutton_adj), 1, 0);
244     gtk_widget_show (appGUI->cal->tl_end_m_spinbutton);
245     gtk_grid_attach (GTK_GRID (timeline_table), appGUI->cal->tl_end_m_spinbutton, 3, 1, 1, 1);
246     gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (appGUI->cal->tl_end_m_spinbutton), TRUE);
247 
248     fill_timeline(appGUI);
249 
250     g_signal_connect (G_OBJECT (appGUI->cal->tl_start_h_spinbutton), "value-changed",
251                       G_CALLBACK (timeline_changed_cb), appGUI);
252     g_signal_connect (G_OBJECT (appGUI->cal->tl_step_spinbutton), "value-changed",
253                       G_CALLBACK (timeline_changed_cb), appGUI);
254     g_signal_connect (G_OBJECT (appGUI->cal->tl_end_h_spinbutton), "value-changed",
255                       G_CALLBACK (timeline_changed_cb), appGUI);
256     g_signal_connect (G_OBJECT (appGUI->cal->tl_start_m_spinbutton), "value-changed",
257                       G_CALLBACK (timeline_changed_cb), appGUI);
258     g_signal_connect (G_OBJECT (appGUI->cal->tl_end_m_spinbutton), "value-changed",
259                       G_CALLBACK (timeline_changed_cb), appGUI);
260 
261     hseparator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
262     gtk_widget_show (hseparator);
263     gtk_box_pack_start (GTK_BOX (vbox1), hseparator, FALSE, TRUE, 4);
264 
265     hbuttonbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
266     gtk_widget_show (hbuttonbox);
267     gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox, FALSE, TRUE, 0);
268     gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_END);
269     gtk_box_set_spacing (GTK_BOX (hbuttonbox), 4);
270 
271     appGUI->cal->cancel_button = gtk_button_new_with_mnemonic (_("_Cancel"));
272     gtk_widget_show (appGUI->cal->cancel_button);
273     g_signal_connect(appGUI->cal->cancel_button, "clicked", G_CALLBACK(button_insert_timeline_window_close_cb), appGUI);
274     gtk_container_add(GTK_CONTAINER(hbuttonbox), appGUI->cal->cancel_button);
275 
276     appGUI->cal->insert_timeline_button = gtk_button_new_with_mnemonic (_("_Paste"));
277     gtk_widget_show (appGUI->cal->insert_timeline_button);
278     g_signal_connect(appGUI->cal->insert_timeline_button, "clicked", G_CALLBACK(insert_timeline_action_cb), appGUI);
279     gtk_container_add(GTK_CONTAINER(hbuttonbox), appGUI->cal->insert_timeline_button);
280 
281     gtk_window_get_position (GTK_WINDOW(appGUI->cal->insert_timeline_window), &win_xpos, &win_ypos);
282     gtk_window_move (GTK_WINDOW (appGUI->cal->insert_timeline_window), win_xpos-5, win_ypos-40);
283     gtk_widget_show(appGUI->cal->insert_timeline_window);
284 
285 }
286 
287 /*------------------------------------------------------------------------------*/
288 
289