X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Faweather-gui.c;h=88bd9660601fdafde1ab356913187abec249f259;hb=d706e67c559f82a5cd2300b35b23812fd08832c5;hp=dcb2b2cb517afc1dd3aae07a6ec1c91a852bb336;hpb=d066b3335070b50b196a43b54a8753384f9476cb;p=grits diff --git a/src/aweather-gui.c b/src/aweather-gui.c index dcb2b2c..88bd966 100644 --- a/src/aweather-gui.c +++ b/src/aweather-gui.c @@ -25,126 +25,6 @@ #include "aweather-gui.h" #include "aweather-location.h" -/* Needed prototpes */ -gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, AWeatherGui *self); -static void on_gis_refresh(GisWorld *world, gpointer _self); -static void on_gis_site_changed(GisView *view, char *site, gpointer _self); -static void site_setup(AWeatherGui *self); -static void time_setup(AWeatherGui *self); -static void prefs_setup(AWeatherGui *self); - -/**************** - * GObject code * - ****************/ -G_DEFINE_TYPE(AWeatherGui, aweather_gui, GTK_TYPE_WINDOW); -static void aweather_gui_init(AWeatherGui *self) -{ - g_debug("AWeatherGui: init"); - - /* Simple things */ - self->prefs = gis_prefs_new("aweather"); - self->plugins = gis_plugins_new(); - self->world = gis_world_new(); - self->view = gis_view_new(); - - /* Setup window */ - self->builder = gtk_builder_new(); - GError *error = NULL; - if (!gtk_builder_add_from_file(self->builder, DATADIR "/aweather/main.ui", &error)) - g_error("Failed to create gtk builder: %s", error->message); - gtk_widget_reparent(aweather_gui_get_widget(self, "body"), GTK_WIDGET(self)); - - /* GIS things */ - GtkWidget *drawing = aweather_gui_get_widget(self, "drawing"); - self->opengl = gis_opengl_new(self->world, self->view, GTK_DRAWING_AREA(drawing)); - self->opengl->plugins = self->plugins; - //gtk_widget_show_all(GTK_WIDGET(self)); - - /* Plugins */ - GtkTreeIter iter; - self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins")); - for (GList *cur = gis_plugins_available(); cur; cur = cur->next) { - gchar *name = cur->data; - gboolean enabled = gis_prefs_get_boolean_v(self->prefs, cur->data, "enabled"); - gtk_list_store_append(self->gtk_plugins, &iter); - gtk_list_store_set(self->gtk_plugins, &iter, 0, name, 1, enabled, -1); - if (enabled) - aweather_gui_attach_plugin(self, name); - } - - /* Misc, helpers */ - site_setup(self); - time_setup(self); - prefs_setup(self); - - /* Connect signals */ - gtk_builder_connect_signals(self->builder, self); - g_signal_connect(self, "key-press-event", - G_CALLBACK(on_gui_key_press), self); - g_signal_connect_swapped(self->world, "offline", - G_CALLBACK(gtk_toggle_action_set_active), - aweather_gui_get_object(self, "offline")); - - /* deprecated site stuff */ - g_signal_connect(self->view, "site-changed", G_CALLBACK(on_gis_site_changed), self); - g_signal_connect(self->world, "refresh", G_CALLBACK(on_gis_refresh), self); -} -static GObject *aweather_gui_constructor(GType gtype, guint n_properties, - GObjectConstructParam *properties) -{ - g_debug("aweather_gui: constructor"); - GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class); - return parent_class->constructor(gtype, n_properties, properties); -} -static void aweather_gui_dispose(GObject *_self) -{ - g_debug("AWeatherGui: dispose"); - AWeatherGui *self = AWEATHER_GUI(_self); - if (self->builder) { - /* Reparent to avoid double unrefs */ - GtkWidget *body = aweather_gui_get_widget(self, "body"); - GtkWidget *window = aweather_gui_get_widget(self, "main_window"); - gtk_widget_reparent(body, window); - g_object_unref(self->builder); - self->builder = NULL; - } - if (self->world) { - g_object_unref(self->world); - self->world = NULL; - } - if (self->view) { - g_object_unref(self->view); - self->view = NULL; - } - if (self->opengl) { - g_object_unref(self->opengl); - self->opengl = NULL; - } - if (self->plugins) { - gis_plugins_free(self->plugins); - self->plugins = NULL; - } - if (self->prefs) { - g_object_unref(self->prefs); - self->prefs = NULL; - } - G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(_self); -} -static void aweather_gui_finalize(GObject *_self) -{ - g_debug("AWeatherGui: finalize"); - G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(_self); - gtk_main_quit(); - -} -static void aweather_gui_class_init(AWeatherGuiClass *klass) -{ - g_debug("AWeatherGui: class_init"); - GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - gobject_class->constructor = aweather_gui_constructor; - gobject_class->dispose = aweather_gui_dispose; - gobject_class->finalize = aweather_gui_finalize; -} /************* * Callbacks * @@ -158,7 +38,7 @@ gboolean on_gui_key_press(GtkWidget *widget, GdkEventKey *event, AWeatherGui *se else if (event->keyval == GDK_r && event->state & GDK_CONTROL_MASK) gis_world_refresh(self->world); else if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) { - GtkNotebook *tabs = GTK_NOTEBOOK(aweather_gui_get_widget(self, "tabs")); + GtkNotebook *tabs = GTK_NOTEBOOK(aweather_gui_get_widget(self, "main_tabs")); gint num_tabs = gtk_notebook_get_n_pages(tabs); gint cur_tab = gtk_notebook_get_current_page(tabs); if (event->state & GDK_SHIFT_MASK) @@ -189,10 +69,9 @@ void on_refresh(GtkAction *action, AWeatherGui *self) gis_world_refresh(self->world); } - void on_plugin_toggled(GtkCellRendererToggle *cell, gchar *path_str, AWeatherGui *self) { - GtkWidget *tview = aweather_gui_get_widget(self, "plugins_view"); + GtkWidget *tview = aweather_gui_get_widget(self, "prefs_plugins_view"); GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tview)); gboolean state; gchar *name; @@ -254,10 +133,11 @@ static gboolean gtk_tree_model_find_string(GtkTreeModel *model, } return FALSE; } + static void update_time_widget(GisView *view, const char *time, AWeatherGui *self) { g_debug("AWeatherGui: update_time_widget - time=%s", time); - GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "time")); + GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time")); GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview)); GtkTreeIter iter; if (gtk_tree_model_find_string(model, &iter, NULL, 0, time)) { @@ -270,10 +150,11 @@ static void update_time_widget(GisView *view, const char *time, AWeatherGui *sel gtk_tree_path_free(path); } } + static void update_site_widget(GisView *view, char *site, AWeatherGui *self) { g_debug("AWeatherGui: update_site_widget - site=%s", site); - GtkComboBox *combo = GTK_COMBO_BOX(aweather_gui_get_widget(self, "site")); + GtkComboBox *combo = GTK_COMBO_BOX(aweather_gui_get_widget(self, "main_site")); GtkTreeModel *model = GTK_TREE_MODEL(gtk_combo_box_get_model(combo)); GtkTreeIter iter; if (gtk_tree_model_find_string(model, &iter, NULL, 1, site)) { @@ -284,6 +165,7 @@ static void update_site_widget(GisView *view, char *site, AWeatherGui *self) G_CALLBACK(on_site_changed), self); } } + /* Prefs callbacks */ void on_offline(GtkToggleAction *action, AWeatherGui *self) { @@ -292,6 +174,7 @@ void on_offline(GtkToggleAction *action, AWeatherGui *self) gis_prefs_set_boolean(self->prefs, "gis/offline", value); gis_world_set_offline(self->world, value); } + void on_initial_site_changed(GtkComboBox *combo, AWeatherGui *self) { gchar *site; @@ -303,12 +186,14 @@ void on_initial_site_changed(GtkComboBox *combo, AWeatherGui *self) gis_prefs_set_string(self->prefs, "aweather/initial_site", site); g_free(site); } + void on_nexrad_url_changed(GtkEntry *entry, AWeatherGui *self) { const gchar *text = gtk_entry_get_text(entry); g_debug("AWeatherGui: on_nexrad_url_changed - url=%s", text); gis_prefs_set_string(self->prefs, "aweather/nexrad_url", text); } + int on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self) { gint value = gtk_spin_button_get_value_as_int(spinner); @@ -316,7 +201,7 @@ int on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self) gis_prefs_set_integer(self->prefs, "aweather/log_level", value); return TRUE; } -// plugins + /***************** * Setup helpers * @@ -327,9 +212,9 @@ static void prefs_setup(AWeatherGui *self) gchar *nu = gis_prefs_get_string (self->prefs, "aweather/nexrad_url"); gint ll = gis_prefs_get_integer(self->prefs, "aweather/log_level"); gchar *is = gis_prefs_get_string (self->prefs, "aweather/initial_site"); - GtkWidget *nuw = aweather_gui_get_widget(self, "nexrad_url"); - GtkWidget *llw = aweather_gui_get_widget(self, "log_level"); - GtkWidget *isw = aweather_gui_get_widget(self, "initial_site"); + GtkWidget *nuw = aweather_gui_get_widget(self, "prefs_general_url"); + GtkWidget *llw = aweather_gui_get_widget(self, "prefs_general_log"); + GtkWidget *isw = aweather_gui_get_widget(self, "prefs_general_site"); if (nu) gtk_entry_set_text(GTK_ENTRY(nuw), nu), g_free(nu); if (ll) gtk_spin_button_set_value(GTK_SPIN_BUTTON(llw), ll); if (is) { @@ -340,7 +225,7 @@ static void prefs_setup(AWeatherGui *self) } /* Load Plugins */ - GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "plugins_view")); + GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "prefs_plugins_view")); GtkCellRenderer *rend1 = gtk_cell_renderer_text_new(); GtkCellRenderer *rend2 = gtk_cell_renderer_toggle_new(); GtkTreeViewColumn *col1 = gtk_tree_view_column_new_with_attributes( @@ -376,8 +261,8 @@ static void site_setup(AWeatherGui *self) } } - GtkWidget *combo = aweather_gui_get_widget(self, "site"); - GObject *renderer = aweather_gui_get_object(self, "site_rend"); + GtkWidget *combo = aweather_gui_get_widget(self, "main_site"); + GObject *renderer = aweather_gui_get_object(self, "main_site_rend"); gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), GTK_CELL_RENDERER(renderer), combo_sensitive, NULL, NULL); @@ -387,7 +272,7 @@ static void site_setup(AWeatherGui *self) static void time_setup(AWeatherGui *self) { - GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "time")); + GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time")); GtkCellRenderer *rend = gtk_cell_renderer_text_new(); GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes( "Time", rend, "text", 0, NULL); @@ -410,7 +295,7 @@ static void update_times_gtk(AWeatherGui *self, GList *times) GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113 GMatchInfo *info; - GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "time")); + GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self, "main_time")); GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview)); gtk_list_store_clear(lstore); GtkTreeIter iter; @@ -483,6 +368,7 @@ static void on_gis_refresh(GisWorld *world, gpointer _self) update_times(self, self->view, site); } + /*********** * Methods * ***********/ @@ -531,7 +417,7 @@ void aweather_gui_attach_plugin(AWeatherGui *self, const gchar *name) { GisPlugin *plugin = gis_plugins_load(self->plugins, name, self->world, self->view, self->opengl, self->prefs); - GtkWidget *config = aweather_gui_get_widget(self, "tabs"); + GtkWidget *config = aweather_gui_get_widget(self, "main_tabs"); GtkWidget *tab = gtk_label_new(name); GtkWidget *body = gis_plugin_get_config(plugin); gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab); @@ -540,7 +426,7 @@ void aweather_gui_attach_plugin(AWeatherGui *self, const gchar *name) } void aweather_gui_deattach_plugin(AWeatherGui *self, const gchar *name) { - GtkWidget *config = aweather_gui_get_widget(self, "tabs"); + GtkWidget *config = aweather_gui_get_widget(self, "main_tabs"); guint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(config)); for (int i = 0; i < n_pages; i++) { g_debug("testing tab %d", i); @@ -555,3 +441,115 @@ void aweather_gui_deattach_plugin(AWeatherGui *self, const gchar *name) gis_plugins_unload(self->plugins, name); gis_opengl_redraw(self->opengl); } + + +/**************** + * GObject code * + ****************/ +G_DEFINE_TYPE(AWeatherGui, aweather_gui, GTK_TYPE_WINDOW); +static void aweather_gui_init(AWeatherGui *self) +{ + g_debug("AWeatherGui: init"); + + /* Simple things */ + self->prefs = gis_prefs_new("aweather"); + self->plugins = gis_plugins_new(); + self->world = gis_world_new(); + self->view = gis_view_new(); + self->opengl = gis_opengl_new(self->world, self->view, self->plugins); + + /* Setup window */ + self->builder = gtk_builder_new(); + GError *error = NULL; + if (!gtk_builder_add_from_file(self->builder, DATADIR "/aweather/main.ui", &error)) + g_error("Failed to create gtk builder: %s", error->message); + gtk_widget_reparent(aweather_gui_get_widget(self, "main_body"), GTK_WIDGET(self)); + GtkWidget *paned = aweather_gui_get_widget(self, "main_paned"); + gtk_widget_destroy(gtk_paned_get_child1(GTK_PANED(paned))); + gtk_paned_pack1(GTK_PANED(paned), GTK_WIDGET(self->opengl), TRUE, FALSE); + + /* Plugins */ + GtkTreeIter iter; + self->gtk_plugins = GTK_LIST_STORE(aweather_gui_get_object(self, "plugins")); + for (GList *cur = gis_plugins_available(); cur; cur = cur->next) { + gchar *name = cur->data; + gboolean enabled = gis_prefs_get_boolean_v(self->prefs, cur->data, "enabled"); + gtk_list_store_append(self->gtk_plugins, &iter); + gtk_list_store_set(self->gtk_plugins, &iter, 0, name, 1, enabled, -1); + if (enabled) + aweather_gui_attach_plugin(self, name); + } + + /* Misc, helpers */ + site_setup(self); + time_setup(self); + prefs_setup(self); + + /* Connect signals */ + gtk_builder_connect_signals(self->builder, self); + g_signal_connect(self, "key-press-event", + G_CALLBACK(on_gui_key_press), self); + g_signal_connect_swapped(self->world, "offline", + G_CALLBACK(gtk_toggle_action_set_active), + aweather_gui_get_object(self, "offline")); + + /* deprecated site stuff */ + g_signal_connect(self->view, "site-changed", G_CALLBACK(on_gis_site_changed), self); + g_signal_connect(self->world, "refresh", G_CALLBACK(on_gis_refresh), self); +} +static GObject *aweather_gui_constructor(GType gtype, guint n_properties, + GObjectConstructParam *properties) +{ + g_debug("aweather_gui: constructor"); + GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class); + return parent_class->constructor(gtype, n_properties, properties); +} +static void aweather_gui_dispose(GObject *_self) +{ + g_debug("AWeatherGui: dispose"); + AWeatherGui *self = AWEATHER_GUI(_self); + if (self->builder) { + /* Reparent to avoid double unrefs */ + GtkWidget *body = aweather_gui_get_widget(self, "main_body"); + GtkWidget *window = aweather_gui_get_widget(self, "main_window"); + gtk_widget_reparent(body, window); + g_object_unref(self->builder); + self->builder = NULL; + } + if (self->world) { + g_object_unref(self->world); + self->world = NULL; + } + if (self->view) { + g_object_unref(self->view); + self->view = NULL; + } + if (self->opengl) { + g_object_unref(self->opengl); + self->opengl = NULL; + } + if (self->plugins) { + gis_plugins_free(self->plugins); + self->plugins = NULL; + } + if (self->prefs) { + g_object_unref(self->prefs); + self->prefs = NULL; + } + G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(_self); +} +static void aweather_gui_finalize(GObject *_self) +{ + g_debug("AWeatherGui: finalize"); + G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(_self); + gtk_main_quit(); + +} +static void aweather_gui_class_init(AWeatherGuiClass *klass) +{ + g_debug("AWeatherGui: class_init"); + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + gobject_class->constructor = aweather_gui_constructor; + gobject_class->dispose = aweather_gui_dispose; + gobject_class->finalize = aweather_gui_finalize; +}