TimerMainWindow.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #include "TimerMainWindow.h"
  2. #include "TimerClock.h"
  3. #include "TimerEditWindow.h"
  4. #include "TimerSettingsWindow.h"
  5. #include "TimerMiniWindow.h"
  6. #include <math.h>
  7. struct _TimerMainWindow {
  8. GtkApplicationWindow parent;
  9. GtkWidget *timerButton;
  10. GtkWidget *timerLabel;
  11. GtkWidget *nameBox;
  12. GtkWidget *startStopButton;
  13. GtkWidget *resetButton;
  14. GtkWidget *saveButton;
  15. GtkWidget *optionsButton;
  16. GtkWidget *taskTreeBox;
  17. GtkWidget *taskTree;
  18. GtkWidget *shrinkButton;
  19. TimerClock *timerClock;
  20. TimerClock *updateClock;
  21. GKeyFile *keyFile;
  22. TimerMiniWindow *miniWindow;
  23. int miniWindowFirstOpen;
  24. int miniWindowMode;
  25. char *labelColorString;
  26. GDateTime *lastUpdateTime;
  27. /* time in seconds */
  28. gint64 currentTime;
  29. GDateTime *startTime;
  30. };
  31. G_DEFINE_TYPE(TimerMainWindow, timer_main_window, GTK_TYPE_APPLICATION_WINDOW);
  32. static gboolean timer_main_window_get_boolean_with_default(
  33. TimerMainWindow *self, const char *group, const char *key, gboolean def) {
  34. GError *err = NULL;
  35. gboolean b = g_key_file_get_boolean(self->keyFile, group, key, &err);
  36. if (err != NULL) {
  37. g_error_free(err);
  38. return def;
  39. }
  40. return b;
  41. }
  42. static char *timer_main_window_get_string_with_default(TimerMainWindow *self,
  43. const char *group,
  44. const char *key,
  45. const char *def) {
  46. GError *err = NULL;
  47. char *s = g_key_file_get_string(self->keyFile, group, key, &err);
  48. if (err != NULL) {
  49. g_error_free(err);
  50. return g_strdup(def);
  51. }
  52. return s;
  53. }
  54. static void timer_main_window_make_data_file(TimerMainWindow *self) {
  55. char *path = timer_main_window_get_string_with_default(self, "Settings",
  56. "Data Path", timer_application_get_default_data_file(TIMER_APPLICATION(gtk_window_get_application(GTK_WINDOW(self)))));
  57. GFile *file = g_file_new_for_path(path);
  58. GFile *parent = g_file_get_parent(file);
  59. GError *err = NULL;
  60. g_file_make_directory_with_parents(parent, NULL, &err);
  61. if (err != NULL) {
  62. if (err->code != G_IO_ERROR_EXISTS) {
  63. GtkWidget *msgDiag = gtk_message_dialog_new(
  64. NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
  65. "Could not create data file: %s", err->message);
  66. gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE);
  67. gtk_dialog_run(GTK_DIALOG(msgDiag));
  68. gtk_widget_destroy(msgDiag);
  69. }
  70. g_clear_error(&err);
  71. }
  72. GFileOutputStream *stream =
  73. g_file_create(file, G_FILE_CREATE_NONE, NULL, &err);
  74. if (err != NULL) {
  75. if (err->code != G_IO_ERROR_EXISTS) {
  76. GtkWidget *msgDiag = gtk_message_dialog_new(
  77. NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
  78. "Could not create data file: %s", err->message);
  79. gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE);
  80. gtk_dialog_run(GTK_DIALOG(msgDiag));
  81. gtk_widget_destroy(msgDiag);
  82. }
  83. g_error_free(err);
  84. } else {
  85. g_output_stream_write(G_OUTPUT_STREAM(stream), "[]", 2, NULL, &err);
  86. if (err != NULL) {
  87. GtkWidget *msgDiag = gtk_message_dialog_new(
  88. NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
  89. "An internal error occured: %s", err->message);
  90. gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE);
  91. gtk_dialog_run(GTK_DIALOG(msgDiag));
  92. gtk_widget_destroy(msgDiag);
  93. g_error_free(err);
  94. }
  95. g_object_unref(stream);
  96. }
  97. timer_task_tree_set_data_path(TIMER_TASK_TREE(self->taskTree), path);
  98. g_free(path);
  99. g_object_unref(parent);
  100. g_object_unref(file);
  101. }
  102. gboolean timer_main_window_is_always_on_top(TimerMainWindow *self) {
  103. return timer_main_window_get_boolean_with_default(self, "Settings", "Always on Top", FALSE);
  104. }
  105. GDateTime *timer_main_window_get_last_task_end(TimerMainWindow *self) {
  106. return timer_task_tree_get_last_task_end(TIMER_TASK_TREE(self->taskTree));
  107. }
  108. static void timer_main_window_interpret_settings(TimerMainWindow *self) {
  109. gtk_window_set_keep_above(GTK_WINDOW(self),
  110. timer_main_window_get_boolean_with_default(
  111. self, "Settings", "Always on Top", FALSE));
  112. gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(self->nameBox));
  113. gtk_combo_box_text_remove_all(timer_mini_window_get_name_box(self->miniWindow));
  114. gsize len;
  115. GError *err = NULL;
  116. char **data = g_key_file_get_string_list(self->keyFile, "Settings", "Tasks",
  117. &len, &err);
  118. if (err == NULL) {
  119. timer_task_tree_set_task_names(TIMER_TASK_TREE(self->taskTree),
  120. (const char **)data, len);
  121. gsize i;
  122. for (i = 0; i < len; ++i) {
  123. gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(self->nameBox),
  124. data[i]);
  125. gtk_combo_box_text_append_text(timer_mini_window_get_name_box(self->miniWindow),
  126. data[i]);
  127. g_free(data[i]);
  128. }
  129. g_free(data);
  130. } else {
  131. timer_task_tree_set_task_names(TIMER_TASK_TREE(self->taskTree), NULL,
  132. 0);
  133. g_error_free(err);
  134. }
  135. char *taskName = timer_main_window_get_string_with_default(
  136. self, "Cache", "Current Name", "");
  137. gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(self->nameBox))),
  138. taskName);
  139. gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(
  140. timer_mini_window_get_name_box(self->miniWindow)))), taskName);
  141. g_free(taskName);
  142. int x = g_key_file_get_integer(self->keyFile, "Cache", "x", NULL);
  143. int y = g_key_file_get_integer(self->keyFile, "Cache", "y", NULL);
  144. int w = g_key_file_get_integer(self->keyFile, "Cache", "width", NULL);
  145. int h = g_key_file_get_integer(self->keyFile, "Cache", "height", NULL);
  146. gtk_window_move(GTK_WINDOW(self), x, y);
  147. gtk_window_resize(GTK_WINDOW(self), w, h);
  148. timer_main_window_make_data_file(self);
  149. }
  150. TimerMainWindow *timer_main_window_new(TimerApplication *app) {
  151. TimerMainWindow *win = TIMER_MAIN_WINDOW(
  152. g_object_new(TIMER_TYPE_MAIN_WINDOW, "application", app, NULL));
  153. g_key_file_load_from_file(win->keyFile,
  154. timer_application_get_config_file(app),
  155. G_KEY_FILE_NONE, NULL);
  156. timer_main_window_interpret_settings(win);
  157. timer_task_tree_expand_today(TIMER_TASK_TREE(win->taskTree));
  158. win->lastUpdateTime = g_date_time_new_now_local();
  159. timer_clock_start(win->updateClock);
  160. return win;
  161. }
  162. char *timer_main_window_get_task_csv(TimerMainWindow *self) {
  163. return timer_task_tree_get_csv(TIMER_TASK_TREE(self->taskTree));
  164. }
  165. TimerDataPoint *timer_main_window_get_day_data(TimerMainWindow *self,
  166. gsize *len) {
  167. return timer_task_tree_get_day_data(TIMER_TASK_TREE(self->taskTree), len);
  168. }
  169. TimerDataPoint *timer_main_window_get_task_data(TimerMainWindow *self,
  170. gsize *len) {
  171. return timer_task_tree_get_task_data(TIMER_TASK_TREE(self->taskTree), len);
  172. }
  173. static gboolean timer_main_window_update_time(TimerMainWindow *self) {
  174. int hour = floor(self->currentTime / 3600.0f);
  175. int minute = floor(self->currentTime / 60.0f) - (hour * 60);
  176. int second = self->currentTime - (hour * 3600) - (minute * 60);
  177. char *time = g_strdup_printf(
  178. "<span foreground='%s'>%02d:%02d:%02d</span>",
  179. timer_clock_is_running(self->timerClock) ? "red" : self->labelColorString,
  180. hour, minute, second);
  181. gtk_label_set_markup(GTK_LABEL(self->timerLabel), time);
  182. gtk_label_set_markup(timer_mini_window_get_timer_label(self->miniWindow), time);
  183. g_free(time);
  184. return FALSE;
  185. }
  186. static void timer_clock_callback(TimerMainWindow *win) {
  187. ++win->currentTime;
  188. g_main_context_invoke(NULL, G_SOURCE_FUNC(timer_main_window_update_time), win);
  189. }
  190. static void options_button_callback(GtkWidget *btn, TimerMainWindow *win) {
  191. TimerSettingsWindow *diag = timer_settings_window_new(
  192. TIMER_APPLICATION(gtk_window_get_application(GTK_WINDOW(win))),
  193. win->keyFile, GTK_WINDOW(win));
  194. int resp = gtk_dialog_run(GTK_DIALOG(diag));
  195. if (resp == GTK_RESPONSE_APPLY) {
  196. GKeyFile *sKeyFile = timer_settings_window_get_key_file(diag);
  197. gsize len;
  198. char *data = g_key_file_to_data(sKeyFile, &len, NULL);
  199. g_key_file_load_from_data(win->keyFile, data, len, G_KEY_FILE_NONE,
  200. NULL);
  201. g_free(data);
  202. GError *err = NULL;
  203. g_key_file_save_to_file(
  204. win->keyFile,
  205. timer_application_get_config_file(
  206. TIMER_APPLICATION(gtk_window_get_application(GTK_WINDOW(win)))),
  207. &err);
  208. if (err != NULL) {
  209. GtkWidget *msgDiag = gtk_message_dialog_new(
  210. GTK_WINDOW(win), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
  211. GTK_BUTTONS_OK, "Could not save settings: %s", err->message);
  212. gtk_window_set_position(GTK_WINDOW(msgDiag), GTK_WIN_POS_MOUSE);
  213. gtk_dialog_run(GTK_DIALOG(msgDiag));
  214. g_error_free(err);
  215. gtk_widget_destroy(msgDiag);
  216. }
  217. timer_main_window_interpret_settings(win);
  218. }
  219. gtk_widget_destroy(GTK_WIDGET(diag));
  220. }
  221. static void start_stop_button_callback(GtkButton *btn, TimerMainWindow *win) {
  222. if (timer_clock_is_running(win->timerClock)) {
  223. timer_clock_stop(win->timerClock);
  224. gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start");
  225. gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start");
  226. timer_main_window_update_time(win);
  227. } else {
  228. timer_clock_start(win->timerClock);
  229. gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Stop");
  230. gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Stop");
  231. gtk_widget_set_sensitive(win->resetButton, TRUE);
  232. gtk_widget_set_sensitive(win->saveButton, TRUE);
  233. gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), TRUE);
  234. win->startTime = g_date_time_new_now_local();
  235. timer_main_window_update_time(win);
  236. }
  237. }
  238. static void reset_button_callback(GtkButton *btn, TimerMainWindow *win) {
  239. if (timer_clock_is_running(win->timerClock)) {
  240. timer_clock_stop(win->timerClock);
  241. }
  242. win->currentTime = 0;
  243. timer_main_window_update_time(win);
  244. gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start");
  245. gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start");
  246. gtk_widget_set_sensitive(win->resetButton, FALSE);
  247. gtk_widget_set_sensitive(win->saveButton, FALSE);
  248. gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), FALSE);
  249. g_date_time_unref(win->startTime);
  250. win->startTime = NULL;
  251. }
  252. static void save_button_callback(GtkButton *btn, TimerMainWindow *win) {
  253. if (timer_clock_is_running(win->timerClock)) {
  254. timer_clock_stop(win->timerClock);
  255. }
  256. const char *text;
  257. if (win->miniWindowMode) {
  258. text = gtk_combo_box_text_get_active_text(timer_mini_window_get_name_box(win->miniWindow));
  259. } else {
  260. text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox));
  261. }
  262. timer_task_tree_add_task(TIMER_TASK_TREE(win->taskTree), win->startTime,
  263. strcmp(text, "") == 0 ? "Untitled" : text,
  264. win->currentTime);
  265. win->currentTime = 0;
  266. g_date_time_unref(win->startTime);
  267. win->startTime = NULL;
  268. timer_main_window_update_time(win);
  269. gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start");
  270. gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start");
  271. gtk_widget_set_sensitive(win->resetButton, FALSE);
  272. gtk_widget_set_sensitive(win->saveButton, FALSE);
  273. gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), FALSE);
  274. }
  275. static void timer_button_callback(GtkButton *btn, TimerMainWindow *win) {
  276. if (timer_clock_is_running(win->timerClock)) {
  277. timer_clock_stop(win->timerClock);
  278. timer_main_window_update_time(win);
  279. gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start");
  280. gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start");
  281. }
  282. gsize optLen;
  283. const char **names =
  284. timer_task_tree_get_task_names(TIMER_TASK_TREE(win->taskTree), &optLen);
  285. GDateTime *start = win->startTime == NULL
  286. ? g_date_time_new_now_local()
  287. : g_date_time_to_local(win->startTime);
  288. TimerEditWindow *diag = timer_edit_window_new(
  289. gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox)),
  290. start, win->currentTime, names, optLen, TRUE, timer_main_window_get_last_task_end(win));
  291. g_date_time_unref(start);
  292. int resp = gtk_dialog_run(GTK_DIALOG(diag));
  293. if (resp == GTK_RESPONSE_APPLY) {
  294. if (win->startTime) {
  295. g_date_time_unref(win->startTime);
  296. }
  297. win->startTime = timer_edit_window_get_start(diag);
  298. win->currentTime = timer_edit_window_get_length(diag);
  299. char *name = timer_edit_window_get_name(diag);
  300. gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(win->nameBox))),
  301. name);
  302. g_free(name);
  303. timer_main_window_update_time(win);
  304. gtk_widget_set_sensitive(win->resetButton, TRUE);
  305. gtk_widget_set_sensitive(win->saveButton, TRUE);
  306. gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), TRUE);
  307. }
  308. gtk_widget_destroy(GTK_WIDGET(diag));
  309. }
  310. static gboolean do_update_header_dates(TimerMainWindow *win) {
  311. timer_task_tree_update_header_dates(TIMER_TASK_TREE(win->taskTree));
  312. return FALSE;
  313. }
  314. static gboolean compare_date(GDateTime *dt1, GDateTime *dt2) {
  315. int y1, m1, d1, y2, m2, d2;
  316. g_date_time_get_ymd(dt1, &y1, &m1, &d1);
  317. g_date_time_get_ymd(dt2, &y2, &m2, &d2);
  318. return y1 == y2 && m1 == m2 && d1 == d2;
  319. }
  320. static void update_clock_callback(TimerMainWindow *win) {
  321. GDateTime *now = g_date_time_new_now_local();
  322. if (!compare_date(win->lastUpdateTime, now)) {
  323. g_main_context_invoke(NULL, G_SOURCE_FUNC(do_update_header_dates), win);
  324. g_date_time_unref(win->lastUpdateTime);
  325. win->lastUpdateTime = now;
  326. }
  327. }
  328. static void timer_main_window_get_defualt_label_color(TimerMainWindow *self) {
  329. GtkStyleContext *ctx = gtk_widget_get_style_context(self->timerLabel);
  330. GdkRGBA color;
  331. gtk_style_context_get_color(ctx, GTK_STATE_FLAG_NORMAL, &color);
  332. self->labelColorString = g_strdup_printf(
  333. "#%x%x%x", (int)round(color.red * 255), (int)round(color.green * 255),
  334. (int)round(color.blue * 255));
  335. }
  336. static void timer_main_window_save_mini_window_specs(TimerMainWindow *self, int x, int y, int w, int h) {
  337. g_key_file_set_integer(self->keyFile, "Cache", "smallX", x);
  338. g_key_file_set_integer(self->keyFile, "Cache", "smallY", y);
  339. g_key_file_set_integer(self->keyFile, "Cache", "smallW", w);
  340. g_key_file_set_integer(self->keyFile, "Cache", "smallH", h);
  341. }
  342. static void timer_main_window_read_mini_window_specs(TimerMainWindow *self, int *x, int *y, int *w, int *h) {
  343. *x = g_key_file_get_integer(self->keyFile, "Cache", "smallX", NULL);
  344. *y = g_key_file_get_integer(self->keyFile, "Cache", "smallY", NULL);
  345. *w = g_key_file_get_integer(self->keyFile, "Cache", "smallW", NULL);
  346. *h = g_key_file_get_integer(self->keyFile, "Cache", "smallH", NULL);
  347. }
  348. static gboolean window_configure_callback(TimerMainWindow *win, GdkEvent *event, gpointer ptr) {
  349. int x, y, w, h;
  350. gtk_window_get_size(GTK_WINDOW(win), &w, &h);
  351. gtk_window_get_position(GTK_WINDOW(win), &x, &y);
  352. g_key_file_set_integer(win->keyFile, "Cache", "x", x);
  353. g_key_file_set_integer(win->keyFile, "Cache", "y", y);
  354. g_key_file_set_integer(win->keyFile, "Cache", "width", w);
  355. g_key_file_set_integer(win->keyFile, "Cache", "height", h);
  356. return FALSE;
  357. }
  358. static void window_destroy_callback(TimerMainWindow *win, gpointer ptr) {
  359. g_key_file_set_string(win->keyFile, "Cache", "Current Name",
  360. gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(win->nameBox)))));
  361. GError *err = NULL;
  362. g_key_file_save_to_file(win->keyFile,
  363. timer_application_get_config_file(TIMER_APPLICATION(
  364. gtk_window_get_application(GTK_WINDOW(win)))),
  365. &err);
  366. if (err != NULL) {
  367. GtkWidget *diag = gtk_message_dialog_new(
  368. NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
  369. "Could not save window state: %s", err->message);
  370. gtk_window_set_position(GTK_WINDOW(diag), GTK_WIN_POS_MOUSE);
  371. gtk_dialog_run(GTK_DIALOG(diag));
  372. gtk_widget_destroy(diag);
  373. g_error_free(err);
  374. }
  375. }
  376. static gboolean window_delete_event(TimerMainWindow *win, GdkEvent *evt, gpointer ptr) {
  377. if (win->startTime != NULL) {
  378. GtkWidget *diag = gtk_message_dialog_new(GTK_WINDOW(win), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are you sure you would like to exit?");
  379. gtk_window_set_position(GTK_WINDOW(diag), GTK_WIN_POS_MOUSE);
  380. int resp = gtk_dialog_run(GTK_DIALOG(diag));
  381. gtk_widget_destroy(diag);
  382. return resp == GTK_RESPONSE_NO;
  383. }
  384. return FALSE;
  385. }
  386. static gboolean mini_window_configure_callback(TimerMiniWindow *win, GdkEvent *event, TimerMainWindow *main) {
  387. if (main->miniWindowMode) {
  388. int x, y, w, h;
  389. gtk_window_get_position(GTK_WINDOW(win), &x, &y);
  390. gtk_window_get_size(GTK_WINDOW(win), &w, &h);
  391. timer_main_window_save_mini_window_specs(main, x, y, w, h);
  392. }
  393. return FALSE;
  394. }
  395. static gboolean mini_window_delete_event(TimerMiniWindow *win, GdkEvent *evt, TimerMainWindow *main) {
  396. if (main->startTime != NULL) {
  397. GtkWidget *diag = gtk_message_dialog_new(GTK_WINDOW(win), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are you sure you would like to exit?");
  398. gtk_window_set_position(GTK_WINDOW(diag), GTK_WIN_POS_MOUSE);
  399. int resp = gtk_dialog_run(GTK_DIALOG(diag));
  400. gtk_widget_destroy(diag);
  401. if (resp != GTK_RESPONSE_NO) {
  402. main->miniWindowMode = FALSE;
  403. gtk_widget_destroy(GTK_WIDGET(main));
  404. return TRUE;
  405. }
  406. }
  407. main->miniWindowMode = FALSE;
  408. gtk_widget_destroy(GTK_WIDGET(main));
  409. return TRUE;
  410. }
  411. static void mini_window_expand_callback(GtkButton *btn, TimerMainWindow *win) {
  412. win->miniWindowMode = FALSE;
  413. gtk_widget_hide(GTK_WIDGET(win->miniWindow));
  414. const char *text = gtk_combo_box_text_get_active_text(timer_mini_window_get_name_box(win->miniWindow));
  415. gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(win->nameBox))), text);
  416. if (timer_main_window_is_always_on_top(win)) {
  417. gtk_window_set_keep_above(GTK_WINDOW(win), TRUE);
  418. }
  419. gtk_widget_show_all(GTK_WIDGET(win));
  420. g_key_file_set_boolean(win->keyFile, "Cache", "isMini", FALSE);
  421. }
  422. static void init_mini_window(TimerMainWindow *self) {
  423. self->miniWindowMode = FALSE;
  424. self->miniWindowFirstOpen = TRUE;
  425. self->miniWindow = timer_mini_window_new();
  426. g_signal_connect(self->miniWindow, "configure-event", G_CALLBACK(mini_window_configure_callback), self);
  427. g_signal_connect(self->miniWindow, "delete-event", G_CALLBACK(mini_window_delete_event), self);
  428. g_signal_connect(timer_mini_window_get_start_stop_button(self->miniWindow),
  429. "clicked", G_CALLBACK(start_stop_button_callback), self);
  430. g_signal_connect(timer_mini_window_get_save_button(self->miniWindow),
  431. "clicked", G_CALLBACK(save_button_callback), self);
  432. g_signal_connect(timer_mini_window_get_expand_button(self->miniWindow),
  433. "clicked", G_CALLBACK(mini_window_expand_callback), self);
  434. g_signal_connect(timer_mini_window_get_timer_button(self->miniWindow),
  435. "clicked", G_CALLBACK(timer_button_callback), self);
  436. }
  437. static void main_window_collapse_callback(GtkButton *btn, TimerMainWindow *win) {
  438. gtk_widget_hide(GTK_WIDGET(win));
  439. const char *text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox));
  440. gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(timer_mini_window_get_name_box(win->miniWindow)))), text);
  441. gtk_window_set_keep_above(GTK_WINDOW(win->miniWindow), TRUE);
  442. gtk_widget_show_all(GTK_WIDGET(win->miniWindow));
  443. if (win->miniWindowFirstOpen) {
  444. int x, y, w, h;
  445. timer_main_window_read_mini_window_specs(win, &x, &y, &w, &h);
  446. gtk_window_move(GTK_WINDOW(win->miniWindow), x, y);
  447. gtk_window_resize(GTK_WINDOW(win->miniWindow), w, h);
  448. win->miniWindowFirstOpen = FALSE;
  449. }
  450. win->miniWindowMode = TRUE;
  451. g_key_file_set_boolean(win->keyFile, "Cache", "isMini", TRUE);
  452. }
  453. void timer_main_window_show_for_mode(TimerMainWindow *self) {
  454. if (g_key_file_get_boolean(self->keyFile, "Cache", "isMini", NULL)) {
  455. main_window_collapse_callback(NULL, self);
  456. } else {
  457. mini_window_expand_callback(NULL, self);
  458. }
  459. }
  460. static void timer_main_window_finalize(GObject *self) {
  461. g_free(TIMER_MAIN_WINDOW(self)->labelColorString);
  462. g_object_unref(TIMER_MAIN_WINDOW(self)->timerClock);
  463. g_object_unref(TIMER_MAIN_WINDOW(self)->updateClock);
  464. gtk_widget_destroy(GTK_WIDGET(TIMER_MAIN_WINDOW(self)->miniWindow));
  465. if (TIMER_MAIN_WINDOW(self)->lastUpdateTime != NULL) {
  466. g_date_time_unref(TIMER_MAIN_WINDOW(self)->lastUpdateTime);
  467. }
  468. if (TIMER_MAIN_WINDOW(self)->startTime != NULL) {
  469. g_date_time_unref(TIMER_MAIN_WINDOW(self)->startTime);
  470. }
  471. G_OBJECT_CLASS(timer_main_window_parent_class)->finalize(self);
  472. }
  473. static void timer_main_window_class_init(TimerMainWindowClass *class) {
  474. G_OBJECT_CLASS(class)->finalize = timer_main_window_finalize;
  475. gtk_widget_class_set_template_from_resource(
  476. GTK_WIDGET_CLASS(class), "/zander/practicetimer/ui/main-window.glade");
  477. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  478. TimerMainWindow, timerButton);
  479. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  480. TimerMainWindow, timerLabel);
  481. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  482. TimerMainWindow, nameBox);
  483. gtk_widget_class_bind_template_child_internal(
  484. GTK_WIDGET_CLASS(class), TimerMainWindow, startStopButton);
  485. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  486. TimerMainWindow, resetButton);
  487. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  488. TimerMainWindow, saveButton);
  489. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  490. TimerMainWindow, shrinkButton);
  491. gtk_widget_class_bind_template_child_internal(
  492. GTK_WIDGET_CLASS(class), TimerMainWindow, optionsButton);
  493. gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class),
  494. TimerMainWindow, taskTreeBox);
  495. }
  496. static void timer_main_window_init(TimerMainWindow *self) {
  497. gtk_widget_init_template(GTK_WIDGET(self));
  498. self->taskTree = timer_task_tree_new();
  499. gtk_container_add(GTK_CONTAINER(self->taskTreeBox), self->taskTree);
  500. self->keyFile = g_key_file_new();
  501. g_signal_connect(self->optionsButton, "clicked",
  502. G_CALLBACK(options_button_callback), self);
  503. self->timerClock =
  504. timer_clock_new(1000, (TimerClockAction)timer_clock_callback, self);
  505. self->updateClock =
  506. timer_clock_new(60000 /* one minute */, (TimerClockAction) update_clock_callback, self);
  507. g_signal_connect(self->startStopButton, "clicked",
  508. G_CALLBACK(start_stop_button_callback), self);
  509. g_signal_connect(self->resetButton, "clicked",
  510. G_CALLBACK(reset_button_callback), self);
  511. g_signal_connect(self->saveButton, "clicked",
  512. G_CALLBACK(save_button_callback), self);
  513. g_signal_connect(self->timerButton, "clicked",
  514. G_CALLBACK(timer_button_callback), self);
  515. g_signal_connect(self->shrinkButton, "clicked",
  516. G_CALLBACK(main_window_collapse_callback), self);
  517. g_signal_connect(self, "destroy", G_CALLBACK(window_destroy_callback), NULL);
  518. g_signal_connect(self, "configure-event", G_CALLBACK(window_configure_callback), NULL);
  519. g_signal_connect(self, "delete-event", G_CALLBACK(window_delete_event), NULL);
  520. timer_main_window_get_defualt_label_color(self);
  521. init_mini_window(self);
  522. }