diff --git a/TimerApplication.c b/TimerApplication.c index a4f1372..68e29dd 100644 --- a/TimerApplication.c +++ b/TimerApplication.c @@ -53,12 +53,7 @@ static void timer_application_activate(GApplication *app) { gtk_widget_destroy(msgDiag); } TimerMainWindow *win = timer_main_window_new(TIMER_APPLICATION(app)); - gtk_window_present(GTK_WINDOW(win)); -#ifdef IS_APPLE - if (timer_main_window_is_always_on_top(win)) { - gtk_window_set_keep_above(GTK_WINDOW(win), TRUE); - } -#endif + timer_main_window_show_for_mode(win); } TimerApplication *timer_application_new() { diff --git a/TimerMainWindow.c b/TimerMainWindow.c index 646385b..0ef12cb 100644 --- a/TimerMainWindow.c +++ b/TimerMainWindow.c @@ -35,11 +35,6 @@ struct _TimerMainWindow { /* time in seconds */ gint64 currentTime; GDateTime *startTime; - - int widthBuff; - int heightBuff; - int xBuff; - int yBuff; }; G_DEFINE_TYPE(TimerMainWindow, timer_main_window, GTK_TYPE_APPLICATION_WINDOW); @@ -131,6 +126,7 @@ static void timer_main_window_interpret_settings(TimerMainWindow *self) { self, "Settings", "Always on Top", FALSE)); gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(self->nameBox)); + gtk_combo_box_text_remove_all(timer_mini_window_get_name_box(self->miniWindow)); gsize len; GError *err = NULL; char **data = g_key_file_get_string_list(self->keyFile, "Settings", "Tasks", @@ -142,6 +138,8 @@ static void timer_main_window_interpret_settings(TimerMainWindow *self) { for (i = 0; i < len; ++i) { gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(self->nameBox), data[i]); + gtk_combo_box_text_append_text(timer_mini_window_get_name_box(self->miniWindow), + data[i]); g_free(data[i]); } g_free(data); @@ -154,6 +152,8 @@ static void timer_main_window_interpret_settings(TimerMainWindow *self) { self, "Cache", "Current Name", ""); gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(self->nameBox))), taskName); + gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN( + timer_mini_window_get_name_box(self->miniWindow)))), taskName); g_free(taskName); int x = g_key_file_get_integer(self->keyFile, "Cache", "x", NULL); int y = g_key_file_get_integer(self->keyFile, "Cache", "y", NULL); @@ -253,8 +253,8 @@ static void start_stop_button_callback(GtkButton *btn, TimerMainWindow *win) { gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Stop"); gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Stop"); gtk_widget_set_sensitive(win->resetButton, TRUE); - gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_reset_button(win->miniWindow)), TRUE); gtk_widget_set_sensitive(win->saveButton, TRUE); + gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), TRUE); win->startTime = g_date_time_new_now_local(); timer_main_window_update_time(win); } @@ -269,8 +269,8 @@ static void reset_button_callback(GtkButton *btn, TimerMainWindow *win) { gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start"); gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start"); gtk_widget_set_sensitive(win->resetButton, FALSE); - gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_reset_button(win->miniWindow)), FALSE); gtk_widget_set_sensitive(win->saveButton, FALSE); + gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), FALSE); g_date_time_unref(win->startTime); win->startTime = NULL; } @@ -279,8 +279,12 @@ static void save_button_callback(GtkButton *btn, TimerMainWindow *win) { if (timer_clock_is_running(win->timerClock)) { timer_clock_stop(win->timerClock); } - const char *text = - gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox)); + const char *text; + if (win->miniWindowMode) { + text = gtk_combo_box_text_get_active_text(timer_mini_window_get_name_box(win->miniWindow)); + } else { + text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox)); + } timer_task_tree_add_task(TIMER_TASK_TREE(win->taskTree), win->startTime, strcmp(text, "") == 0 ? "Untitled" : text, win->currentTime); @@ -289,8 +293,10 @@ static void save_button_callback(GtkButton *btn, TimerMainWindow *win) { win->startTime = NULL; timer_main_window_update_time(win); gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start"); + gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start"); gtk_widget_set_sensitive(win->resetButton, FALSE); gtk_widget_set_sensitive(win->saveButton, FALSE); + gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), FALSE); } static void timer_button_callback(GtkButton *btn, TimerMainWindow *win) { @@ -298,6 +304,7 @@ static void timer_button_callback(GtkButton *btn, TimerMainWindow *win) { timer_clock_stop(win->timerClock); timer_main_window_update_time(win); gtk_button_set_label(GTK_BUTTON(win->startStopButton), "Start"); + gtk_button_set_label(timer_mini_window_get_start_stop_button(win->miniWindow), "Start"); } gsize optLen; const char **names = @@ -323,6 +330,7 @@ static void timer_button_callback(GtkButton *btn, TimerMainWindow *win) { timer_main_window_update_time(win); gtk_widget_set_sensitive(win->resetButton, TRUE); gtk_widget_set_sensitive(win->saveButton, TRUE); + gtk_widget_set_sensitive(GTK_WIDGET(timer_mini_window_get_save_button(win->miniWindow)), TRUE); } gtk_widget_destroy(GTK_WIDGET(diag)); } @@ -357,17 +365,21 @@ static void timer_main_window_get_defualt_label_color(TimerMainWindow *self) { (int)round(color.blue * 255)); } -void timer_main_window_save_mini_window_pos(TimerMainWindow *self, int x, int y) { +static void timer_main_window_save_mini_window_specs(TimerMainWindow *self, int x, int y, int w, int h) { g_key_file_set_integer(self->keyFile, "Cache", "smallX", x); g_key_file_set_integer(self->keyFile, "Cache", "smallY", y); + g_key_file_set_integer(self->keyFile, "Cache", "smallW", w); + g_key_file_set_integer(self->keyFile, "Cache", "smallH", h); } -void timer_main_window_read_mini_window_pos(TimerMainWindow *self, int *x, int *y) { +static void timer_main_window_read_mini_window_specs(TimerMainWindow *self, int *x, int *y, int *w, int *h) { *x = g_key_file_get_integer(self->keyFile, "Cache", "smallX", NULL); *y = g_key_file_get_integer(self->keyFile, "Cache", "smallY", NULL); + *w = g_key_file_get_integer(self->keyFile, "Cache", "smallW", NULL); + *h = g_key_file_get_integer(self->keyFile, "Cache", "smallH", NULL); } -static gboolean window_configure_callback(TimerMainWindow *win, GdkEventConfigure event, gpointer ptr) { +static gboolean window_configure_callback(TimerMainWindow *win, GdkEvent *event, gpointer ptr) { int x, y, w, h; gtk_window_get_size(GTK_WINDOW(win), &w, &h); gtk_window_get_position(GTK_WINDOW(win), &x, &y); @@ -408,53 +420,83 @@ static gboolean window_delete_event(TimerMainWindow *win, GdkEvent *evt, gpointe return FALSE; } -static gboolean mini_window_configure_callback(TimerMiniWindow *win, GdkEventConfigure event, TimerMainWindow *main) { +static gboolean mini_window_configure_callback(TimerMiniWindow *win, GdkEvent *event, TimerMainWindow *main) { if (main->miniWindowMode) { - int x, y; + int x, y, w, h; gtk_window_get_position(GTK_WINDOW(win), &x, &y); - timer_main_window_save_mini_window_pos(main, x, y); + gtk_window_get_size(GTK_WINDOW(win), &w, &h); + timer_main_window_save_mini_window_specs(main, x, y, w, h); } return FALSE; } static gboolean mini_window_delete_event(TimerMiniWindow *win, GdkEvent *evt, TimerMainWindow *main) { + if (main->startTime != NULL) { + 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?"); + gtk_window_set_position(GTK_WINDOW(diag), GTK_WIN_POS_MOUSE); + int resp = gtk_dialog_run(GTK_DIALOG(diag)); + gtk_widget_destroy(diag); + if (resp != GTK_RESPONSE_NO) { + main->miniWindowMode = FALSE; + gtk_widget_destroy(GTK_WIDGET(main)); + } + } main->miniWindowMode = FALSE; - gtk_widget_hide(GTK_WIDGET(win)); - gtk_widget_show_all(GTK_WIDGET(main)); - return FALSE; + gtk_widget_destroy(GTK_WIDGET(main)); + return TRUE; } static void mini_window_expand_callback(GtkButton *btn, TimerMainWindow *win) { win->miniWindowMode = FALSE; gtk_widget_hide(GTK_WIDGET(win->miniWindow)); + const char *text = gtk_combo_box_text_get_active_text(timer_mini_window_get_name_box(win->miniWindow)); + gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(win->nameBox))), text); + if (timer_main_window_is_always_on_top(win)) { + gtk_window_set_keep_above(GTK_WINDOW(win), TRUE); + } gtk_widget_show_all(GTK_WIDGET(win)); + g_key_file_set_boolean(win->keyFile, "Cache", "isMini", FALSE); } static void init_mini_window(TimerMainWindow *self) { - self->miniWindowFirstOpen = TRUE; self->miniWindowMode = FALSE; + self->miniWindowFirstOpen = TRUE; self->miniWindow = timer_mini_window_new(); g_signal_connect(self->miniWindow, "configure-event", G_CALLBACK(mini_window_configure_callback), self); g_signal_connect(self->miniWindow, "delete-event", G_CALLBACK(mini_window_delete_event), self); g_signal_connect(timer_mini_window_get_start_stop_button(self->miniWindow), "clicked", G_CALLBACK(start_stop_button_callback), self); - g_signal_connect(timer_mini_window_get_reset_button(self->miniWindow), - "clicked", G_CALLBACK(reset_button_callback), self); + g_signal_connect(timer_mini_window_get_save_button(self->miniWindow), + "clicked", G_CALLBACK(save_button_callback), self); g_signal_connect(timer_mini_window_get_expand_button(self->miniWindow), "clicked", G_CALLBACK(mini_window_expand_callback), self); + g_signal_connect(timer_mini_window_get_timer_button(self->miniWindow), + "clicked", G_CALLBACK(timer_button_callback), self); } static void main_window_collapse_callback(GtkButton *btn, TimerMainWindow *win) { gtk_widget_hide(GTK_WIDGET(win)); + const char *text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(win->nameBox)); + gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(timer_mini_window_get_name_box(win->miniWindow)))), text); + gtk_window_set_keep_above(GTK_WINDOW(win->miniWindow), TRUE); gtk_widget_show_all(GTK_WIDGET(win->miniWindow)); - win->miniWindowMode = TRUE; if (win->miniWindowFirstOpen) { - gtk_window_set_keep_above(GTK_WINDOW(win->miniWindow), TRUE); - int x, y; - timer_main_window_read_mini_window_pos(win, &x, &y); + int x, y, w, h; + timer_main_window_read_mini_window_specs(win, &x, &y, &w, &h); gtk_window_move(GTK_WINDOW(win->miniWindow), x, y); + gtk_window_resize(GTK_WINDOW(win->miniWindow), w, h); win->miniWindowFirstOpen = FALSE; } + win->miniWindowMode = TRUE; + g_key_file_set_boolean(win->keyFile, "Cache", "isMini", TRUE); +} + +void timer_main_window_show_for_mode(TimerMainWindow *self) { + if (g_key_file_get_boolean(self->keyFile, "Cache", "isMini", NULL)) { + main_window_collapse_callback(NULL, self); + } else { + mini_window_expand_callback(NULL, self); + } } static void timer_main_window_finalize(GObject *self) { diff --git a/TimerMainWindow.h b/TimerMainWindow.h index a72e71a..4aba52d 100644 --- a/TimerMainWindow.h +++ b/TimerMainWindow.h @@ -17,8 +17,7 @@ TimerDataPoint *timer_main_window_get_task_data(TimerMainWindow *self, gsize *le TimerDataPoint *timer_main_window_get_day_data(TimerMainWindow *self, gsize *len); gboolean timer_main_window_is_always_on_top(TimerMainWindow *self); GDateTime *timer_main_window_get_last_task_end(TimerMainWindow *self); -void timer_main_window_save_mini_window_pos(TimerMainWindow *self, int x, int y); -void timer_main_window_read_mini_window_pos(TimerMainWindow *self, int *x, int *y); +void timer_main_window_show_for_mode(TimerMainWindow *self); G_END_DECLS diff --git a/TimerMiniWindow.c b/TimerMiniWindow.c index d29982c..5a0ddbf 100644 --- a/TimerMiniWindow.c +++ b/TimerMiniWindow.c @@ -4,9 +4,11 @@ struct _TimerMiniWindow { GtkWindow parent; GtkWidget *startStopButton; - GtkWidget *resetButton; + GtkWidget *saveButton; GtkWidget *expandButton; GtkWidget *timerLabel; + GtkWidget *timerButton; + GtkWidget *nameBox; }; G_DEFINE_TYPE(TimerMiniWindow, timer_mini_window, GTK_TYPE_WINDOW); @@ -19,29 +21,41 @@ GtkLabel *timer_mini_window_get_timer_label(TimerMiniWindow *self) { return GTK_LABEL(self->timerLabel); } +GtkButton *timer_mini_window_get_timer_button(TimerMiniWindow *self) { + return GTK_BUTTON(self->timerButton); +} + GtkButton *timer_mini_window_get_start_stop_button(TimerMiniWindow *self) { return GTK_BUTTON(self->startStopButton); } -GtkButton *timer_mini_window_get_reset_button(TimerMiniWindow *self) { - return GTK_BUTTON(self->resetButton); +GtkButton *timer_mini_window_get_save_button(TimerMiniWindow *self) { + return GTK_BUTTON(self->saveButton); } GtkButton *timer_mini_window_get_expand_button(TimerMiniWindow *self) { return GTK_BUTTON(self->expandButton); } +GtkComboBoxText *timer_mini_window_get_name_box(TimerMiniWindow *self) { + return GTK_COMBO_BOX_TEXT(self->nameBox); +} + void timer_mini_window_class_init(TimerMiniWindowClass *class) { gtk_widget_class_set_template_from_resource( GTK_WIDGET_CLASS(class), "/zander/practicetimer/ui/mini-window.glade"); gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), TimerMiniWindow, startStopButton); gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), - TimerMiniWindow, resetButton); + TimerMiniWindow, saveButton); gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), TimerMiniWindow, expandButton); gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), TimerMiniWindow, timerLabel); + gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), + TimerMiniWindow, nameBox); + gtk_widget_class_bind_template_child_internal(GTK_WIDGET_CLASS(class), + TimerMiniWindow, timerButton); } void timer_mini_window_init(TimerMiniWindow *self) { diff --git a/TimerMiniWindow.h b/TimerMiniWindow.h index f4b27a1..3fdb9d4 100644 --- a/TimerMiniWindow.h +++ b/TimerMiniWindow.h @@ -10,9 +10,11 @@ G_DECLARE_FINAL_TYPE(TimerMiniWindow, timer_mini_window, TIMER, MINI_WINDOW, Gtk TimerMiniWindow *timer_mini_window_new(); GtkLabel *timer_mini_window_get_timer_label(TimerMiniWindow *self); +GtkButton *timer_mini_window_get_timer_button(TimerMiniWindow *self); GtkButton *timer_mini_window_get_start_stop_button(TimerMiniWindow *self); -GtkButton *timer_mini_window_get_reset_button(TimerMiniWindow *self); +GtkButton *timer_mini_window_get_save_button(TimerMiniWindow *self); GtkButton *timer_mini_window_get_expand_button(TimerMiniWindow *self); +GtkComboBoxText *timer_mini_window_get_name_box(TimerMiniWindow *self); G_END_DECLS diff --git a/ui/mini-window.glade b/ui/mini-window.glade index 81ab497..701a5a3 100644 --- a/ui/mini-window.glade +++ b/ui/mini-window.glade @@ -4,7 +4,6 @@