Imported Upstream version 1.4
[debian/gsimplecal.git] / src / MainWindow.cpp
1 #include <gtk/gtk.h>
2 #include <gdk/gdkkeysyms.h>
3
4 #include "MainWindow.hpp"
5 #include "Config.hpp"
6 #include "Calendar.hpp"
7 #include "Timezones.hpp"
8
9
10 bool closeCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
11                    GdkModifierType mod, gpointer user_data)
12 {
13     if (user_data) {
14         ((MainWindow*)user_data)->close();
15     }
16     return true;
17 }
18 bool nextYearCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
19                       GdkModifierType mod, gpointer user_data)
20 {
21     if (user_data) {
22         ((MainWindow*)user_data)->nextYear();
23     }
24     return true;
25 }
26 bool prevYearCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
27                       GdkModifierType mod, gpointer user_data)
28 {
29     if (user_data) {
30         ((MainWindow*)user_data)->prevYear();
31     }
32     return true;
33 }
34 bool nextMonthCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
35                        GdkModifierType mod, gpointer user_data)
36 {
37     if (user_data) {
38         ((MainWindow*)user_data)->nextMonth();
39     }
40     return true;
41 }
42 bool prevMonthCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
43                        GdkModifierType mod, gpointer user_data)
44 {
45     if (user_data) {
46         ((MainWindow*)user_data)->prevMonth();
47     }
48     return true;
49 }
50 bool goTodayCallback(GtkAccelGroup *group, GObject *obj, guint keyval,
51                      GdkModifierType mod, gpointer user_data)
52 {
53     if (user_data) {
54         ((MainWindow*)user_data)->goToday();
55     }
56     return true;
57 }
58
59
60 MainWindow::MainWindow()
61 {
62     Config* config = Config::getInstance();
63     gint xpos, ypos;
64     widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
65
66     gtk_window_set_title(GTK_WINDOW(widget), "gsimplecal");
67     gtk_window_set_decorated(GTK_WINDOW(widget), config->mainwindow_decorated);
68     gtk_window_set_position(GTK_WINDOW(widget), config->mainwindow_position);
69     gtk_window_get_position(GTK_WINDOW(widget), &xpos, &ypos);
70     gtk_window_move(GTK_WINDOW(widget),  config->mainwindow_xoffset + xpos,  config->mainwindow_yoffset + ypos);
71     gtk_window_set_resizable(GTK_WINDOW(widget), config->mainwindow_resizable);
72     gtk_window_set_keep_above(GTK_WINDOW(widget),
73                               config->mainwindow_keep_above);
74     gtk_window_set_skip_taskbar_hint(GTK_WINDOW(widget),
75                                      config->mainwindow_skip_taskbar);
76     if (config->mainwindow_sticky) {
77         gtk_window_stick(GTK_WINDOW(widget));
78     }
79
80     // Create box for child items
81     children_box = gtk_vbox_new(false, 10);
82
83     calendar = NULL;
84     if (config->show_calendar) {
85         calendar = new Calendar();
86         calendar->addToBox(children_box);
87     }
88     timezones = NULL;
89     if (config->show_timezones) {
90         timezones = new Timezones();
91         timezones->updateTime();
92         timezones->addToBox(children_box);
93     }
94
95     gtk_container_add(GTK_CONTAINER(widget), children_box);
96     gtk_widget_show(children_box);
97     gtk_widget_show(widget);
98
99     // Connect keyboard accelerators
100     GtkAccelGroup *accelerators = gtk_accel_group_new();
101     GClosure *closure;
102
103     Shortcut keys[] = {{GDK_Escape, 0, closeCallback},
104                        {GDK_q, GDK_CONTROL_MASK, closeCallback},
105                        {GDK_w, GDK_CONTROL_MASK, closeCallback},
106                        {GDK_j, GDK_SHIFT_MASK, nextYearCallback},
107                        {GDK_k, GDK_SHIFT_MASK, prevYearCallback},
108                        {GDK_j, 0, nextMonthCallback},
109                        {GDK_k, 0, prevMonthCallback},
110                        {GDK_g, 0, goTodayCallback},
111                        {GDK_Home, 0, goTodayCallback}};
112     for (int key = 0; key < 9; key++) {
113         closure = g_cclosure_new(G_CALLBACK(keys[key].func), (gpointer)this, NULL);
114         gtk_accel_group_connect(accelerators, keys[key].key,
115                                 (GdkModifierType)keys[key].modifier,
116                                 (GtkAccelFlags)NULL, closure);
117         g_closure_unref(closure);
118     }
119
120     gtk_window_add_accel_group(GTK_WINDOW(widget), accelerators);
121 }
122
123 MainWindow::~MainWindow()
124 {
125     if (calendar) {
126         delete calendar;
127     }
128     if (timezones) {
129         delete timezones;
130     }
131     gtk_widget_destroy(children_box);
132     gtk_widget_destroy(widget);
133 }
134
135 GtkWindow* MainWindow::getWindow()
136 {
137     return GTK_WINDOW(widget);
138 }
139
140 void MainWindow::updateTime()
141 {
142     if (timezones) {
143         timezones->updateTime();
144     }
145 }
146
147 void MainWindow::close()
148 {
149     gtk_signal_emit_by_name(GTK_OBJECT(widget), "destroy");
150 }
151
152 void MainWindow::goToday()
153 {
154     calendar->goToday();
155 }
156 void MainWindow::nextMonth()
157 {
158     calendar->nextMonth();
159 }
160 void MainWindow::prevMonth()
161 {
162     calendar->prevMonth();
163 }
164
165 void MainWindow::nextYear()
166 {
167     calendar->nextYear();
168 }
169 void MainWindow::prevYear()
170 {
171     calendar->prevYear();
172 }