5a45cf8a1d176abe5ea1cb4967e48b25bab23d12
[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
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_set_resizable(GTK_WINDOW(widget), config->mainwindow_resizable);
70     gtk_window_set_keep_above(GTK_WINDOW(widget),
71                               config->mainwindow_keep_above);
72     gtk_window_set_skip_taskbar_hint(GTK_WINDOW(widget),
73                                      config->mainwindow_skip_taskbar);
74     if (config->mainwindow_sticky) {
75         gtk_window_stick(GTK_WINDOW(widget));
76     }
77
78     // Create box for child items
79     children_box = gtk_vbox_new(false, 10);
80
81     calendar = NULL;
82     if (config->show_calendar) {
83         calendar = new Calendar();
84         calendar->addToBox(children_box);
85     }
86     timezones = NULL;
87     if (config->show_timezones) {
88         timezones = new Timezones();
89         timezones->updateTime();
90         timezones->addToBox(children_box);
91     }
92
93     gtk_container_add(GTK_CONTAINER(widget), children_box);
94     gtk_widget_show(children_box);
95     gtk_widget_show(widget);
96
97     // Connect keyboard accelerators
98     GtkAccelGroup *accelerators = gtk_accel_group_new();
99     GClosure *closure;
100
101     Shortcut keys[] = {{GDK_Escape, 0, closeCallback},
102                        {GDK_q, GDK_CONTROL_MASK, closeCallback},
103                        {GDK_w, GDK_CONTROL_MASK, closeCallback},
104                        {GDK_j, GDK_SHIFT_MASK, nextYearCallback},
105                        {GDK_k, GDK_SHIFT_MASK, prevYearCallback},
106                        {GDK_j, 0, nextMonthCallback},
107                        {GDK_k, 0, prevMonthCallback},
108                        {GDK_g, 0, goTodayCallback},
109                        {GDK_Home, 0, goTodayCallback}};
110     for (int key = 0; key < 9; key++) {
111         closure = g_cclosure_new(G_CALLBACK(keys[key].func), (gpointer)this, NULL);
112         gtk_accel_group_connect(accelerators, keys[key].key,
113                                 (GdkModifierType)keys[key].modifier,
114                                 (GtkAccelFlags)NULL, closure);
115         g_closure_unref(closure);
116     }
117
118     gtk_window_add_accel_group(GTK_WINDOW(widget), accelerators);
119 }
120
121 MainWindow::~MainWindow()
122 {
123     if (calendar) {
124         delete calendar;
125     }
126     if (timezones) {
127         delete timezones;
128     }
129     gtk_widget_destroy(children_box);
130     gtk_widget_destroy(widget);
131 }
132
133 GtkWindow* MainWindow::getWindow()
134 {
135     return GTK_WINDOW(widget);
136 }
137
138 void MainWindow::updateTime()
139 {
140     if (timezones) {
141         timezones->updateTime();
142     }
143 }
144
145 void MainWindow::close()
146 {
147     gtk_signal_emit_by_name(GTK_OBJECT(widget), "destroy");
148 }
149
150 void MainWindow::goToday()
151 {
152     calendar->goToday();
153 }
154 void MainWindow::nextMonth()
155 {
156     calendar->nextMonth();
157 }
158 void MainWindow::prevMonth()
159 {
160     calendar->prevMonth();
161 }
162
163 void MainWindow::nextYear()
164 {
165     calendar->nextYear();
166 }
167 void MainWindow::prevYear()
168 {
169     calendar->prevYear();
170 }