From a5aa3e9262e858debf7e08a1c1bf22e68c15fa5b Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 16 Dec 2021 23:06:13 -0300 Subject: [PATCH] Migrating from GtkApplication to GtkOSXApplication. --- configure.ac | 14 +++++++++++++- src/objects/application/application.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------ src/objects/application/plugins.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/objects/application/private.h | 26 ++++++++++++++++++++++++++ src/objects/window/window.c | 4 +++- 5 files changed, 179 insertions(+), 50 deletions(-) create mode 100644 src/objects/application/plugins.c diff --git a/configure.ac b/configure.ac index 4ae42fe..7acbd8c 100644 --- a/configure.ac +++ b/configure.ac @@ -234,7 +234,19 @@ dnl --------------------------------------------------------------------------- GLIB_GSETTINGS -PKG_CHECK_MODULES( [GTK], [gtk+-3.0 glib-2.0 gmodule-2.0], AC_DEFINE(HAVE_GTK), AC_MSG_ERROR([GTK not present.])) +case "$host" in + *-mingw32|*-pc-msys) + PKG_CHECK_MODULES( [GTK], [gtk+-3.0 glib-2.0 gmodule-2.0], AC_DEFINE(HAVE_GTK), AC_MSG_ERROR([GTK not present.])) + ;; + + *-apple-darwin*) + PKG_CHECK_MODULES( [GTK], [gtk+-3.0 glib-2.0 gmodule-2.0 gtk-mac-integration-gtk3], AC_DEFINE(HAVE_GTK), AC_MSG_ERROR([GTK not present.])) + ;; + + *) + PKG_CHECK_MODULES( [GTK], [gtk+-3.0 glib-2.0 gmodule-2.0], AC_DEFINE(HAVE_GTK), AC_MSG_ERROR([GTK not present.])) + +esac AC_SUBST(GTK_LIBS) AC_SUBST(GTK_CFLAGS) diff --git a/src/objects/application/application.c b/src/objects/application/application.c index 47802da..98ba738 100644 --- a/src/objects/application/application.c +++ b/src/objects/application/application.c @@ -34,26 +34,15 @@ enum { static GParamSpec * props[NUM_PROPERTIES]; -struct _pw3270ApplicationClass { - GtkApplicationClass parent_class; -}; - -struct _pw3270Application { - GtkApplication parent; - - GSettings * settings; - GList * keypads; - gchar * logfile; - GSList * plugins; ///< @brief Handlers of the loaded plugins. - PW3270_UI_STYLE ui_style; - -}; - static void startup(GApplication * application); static void activate(GApplication * application); static void finalize(GObject *object); -G_DEFINE_TYPE(pw3270Application, pw3270Application, GTK_TYPE_APPLICATION); +#ifdef __APPLE__ + G_DEFINE_TYPE(pw3270Application, pw3270Application, GTKOSX_TYPE_APPLICATION); +#else + G_DEFINE_TYPE(pw3270Application, pw3270Application, GTK_TYPE_APPLICATION); +#endif // __APPLE__ static void get_property(GObject *object, guint prop_id, GValue *value, GParamSpec G_GNUC_UNUSED(*pspec)) { @@ -270,41 +259,62 @@ static void pw3270Application_init(pw3270Application *app) { g_application_add_main_option_entries(G_APPLICATION(app), cmd_options); -#ifdef _WIN32 + app->settings = pw3270_application_settings_new(); + +#if defined(_WIN32) + // + // Setup windows UI + // app->ui_style = PW3270_UI_STYLE_CLASSICAL; + { + // https://stackoverflow.com/questions/37035936/how-to-get-native-windows-decorations-on-gtk3-on-windows-7-and-msys2 + int gtk_csd = g_settings_get_int(app->settings,"gtk-csd"); + if(gtk_csd != -1) { + g_autofree gchar * env = g_strdup_printf("GTK_CSD=%d",gtk_csd); + putenv(env); + } + } + + if(app->settings) { + g_object_ref_sink(G_OBJECT(app->settings)); + g_settings_bind(app->settings, "ui-style", app, "ui-style", G_SETTINGS_BIND_DEFAULT); + } + + { + lib3270_autoptr(char) plugin_path = lib3270_build_data_filename("plugins",NULL); + pw3270_load_plugins_from_path(app, plugin_path); + } + +#elif defined(__APPLE__) + // + // Setup Apple UI + // + { + lib3270_autoptr(char) plugin_path = lib3270_build_data_filename("plugins",NULL); + pw3270_load_plugins_from_path(app, plugin_path); + } + #else + // + // Setup linux UI + // app->ui_style = PW3270_UI_STYLE_AUTOMATIC; -#endif // _WIN32 - - // Get settings app->settings = pw3270_application_settings_new(); - - // Bind properties if(app->settings) { - g_object_ref_sink(G_OBJECT(app->settings)); + g_settings_bind(app->settings, "ui-style", app, "ui-style", G_SETTINGS_BIND_DEFAULT); + } + + pw3270_load_plugins_from_path(app, G_STRINGIFY(LIBDIR) G_DIR_SEPARATOR_S G_STRINGIFY(PRODUCT_NAME) "-plugins"); -#ifdef _WIN32 - { - // https://stackoverflow.com/questions/37035936/how-to-get-native-windows-decorations-on-gtk3-on-windows-7-and-msys2 - int gtk_csd = g_settings_get_int(app->settings,"gtk-csd"); - if(gtk_csd != -1) { - g_autofree gchar * env = g_strdup_printf("GTK_CSD=%d",gtk_csd); - putenv(env); - } - } #endif // _WIN32 - g_settings_bind(app->settings, "ui-style", app, "ui-style", G_SETTINGS_BIND_DEFAULT); - } + /* + + FIX-ME: Move to other place. // Get plugins. { -#ifdef _WIN32 - lib3270_autoptr(char) path = lib3270_build_data_filename("plugins",NULL); -#else - const gchar * path = G_STRINGIFY(LIBDIR) G_DIR_SEPARATOR_S G_STRINGIFY(PRODUCT_NAME) "-plugins"; -#endif // _WIN32 if(g_file_test(path,G_FILE_TEST_IS_DIR)) { @@ -353,17 +363,19 @@ static void pw3270Application_init(pw3270Application *app) { } - // Initialize plugins - { - GSList * item; - void (*call)(GtkApplication *application); - for(item = app->plugins; item; item = g_slist_next(item)) { - if(g_module_symbol((GModule *) item->data, "pw3270_plugin_set_application", (gpointer *) &call)) { - call(GTK_APPLICATION(app)); - } - } + } + */ + + // Initialize plugins + { + GSList * item; + void (*call)(GtkApplication *application); + for(item = app->plugins; item; item = g_slist_next(item)) { + if(g_module_symbol((GModule *) item->data, "pw3270_plugin_set_application", (gpointer *) &call)) { + call(GTK_APPLICATION(app)); + } } } @@ -537,6 +549,7 @@ void activate(GApplication *application) { void pw3270_application_set_ui_style(GApplication *app, PW3270_UI_STYLE type) { +#ifndef __APPLE__ g_return_if_fail(PW3270_IS_APPLICATION(app)); pw3270Application * application = PW3270_APPLICATION(app); @@ -547,12 +560,18 @@ void pw3270_application_set_ui_style(GApplication *app, PW3270_UI_STYLE type) { application->ui_style = type; g_object_notify_by_pspec(G_OBJECT(app), props[PROP_UI_STYLE]); +#endif // !__APPLE + } PW3270_UI_STYLE pw3270_application_get_ui_style(GApplication *app) { +#ifdef __APPLE__ + return PW3270_UI_STYLE_GNOME; +#else g_return_val_if_fail(PW3270_IS_APPLICATION(app),PW3270_UI_STYLE_CLASSICAL); return PW3270_APPLICATION(app)->ui_style; +#endif // __APPLE__ } diff --git a/src/objects/application/plugins.c b/src/objects/application/plugins.c new file mode 100644 index 0000000..36199c0 --- /dev/null +++ b/src/objects/application/plugins.c @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ + +/* + * Copyright (C) 2008 Banco do Brasil S.A. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#include "private.h" + +void pw3270_load_plugins_from_path(pw3270Application *app, const char *path) { + + if(g_file_test(path,G_FILE_TEST_IS_DIR)) { + + g_message("Loading plugins from %s",path); + + GError * err = NULL; + GDir * dir = g_dir_open(path,0,&err); + + if(dir) { + + const gchar *name; + while((name = g_dir_read_name(dir)) != NULL) { + + g_autofree gchar *filename = g_build_filename(path,name,NULL); + + if(g_str_has_suffix(filename,G_MODULE_SUFFIX)) { + + g_message("Loading %s",filename); + + GModule *handle = g_module_open(filename,G_MODULE_BIND_LOCAL); + + if(handle) { + + app->plugins = g_slist_append(app->plugins,handle); + + } else { + + g_warning("Can't load %s: %s",filename,g_module_error()); + + } + + } + + } + + g_dir_close(dir); + } + + if(err) { + + g_warning("Can't load plugins from %s: %s",path,err->message); + g_error_free(err); + + } + + } + +} diff --git a/src/objects/application/private.h b/src/objects/application/private.h index 8b6c506..3f85e65 100644 --- a/src/objects/application/private.h +++ b/src/objects/application/private.h @@ -46,10 +46,36 @@ #include #include +#ifdef __APPLE__ + #include +#endif // __APPLE__ + +typedef struct _pw3270Application { +#ifdef __APPLE__ + GtkosxApplication parent; +#else + GtkApplication parent; + PW3270_UI_STYLE ui_style; +#endif // __APPLE__ + + GSettings * settings; + GList * keypads; + gchar * logfile; + GSList * plugins; ///< @brief Handlers of the loaded plugins. + +} pw3270Application; + +struct _pw3270ApplicationClass { + GtkApplicationClass parent_class; +}; + + G_GNUC_INTERNAL void pw3270_application_open(GApplication * application, GFile **files, gint n_files, const gchar *hint); G_GNUC_INTERNAL GtkWidget * pw3270_terminal_new(const gchar *session_file); +G_GNUC_INTERNAL void pw3270_load_plugins_from_path(pw3270Application *app, const char *path); + // Actions G_GNUC_INTERNAL GAction * pw3270_about_action_new(); G_GNUC_INTERNAL GAction * pw3270_preferences_action_new(); diff --git a/src/objects/window/window.c b/src/objects/window/window.c index 86ec3ad..b3503c4 100644 --- a/src/objects/window/window.c +++ b/src/objects/window/window.c @@ -531,7 +531,9 @@ static void pw3270ApplicationWindow_init(pw3270ApplicationWindow *widget) { // // Bind properties // -#ifndef __APPLE__ +#ifdef __APPLE__ + gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(widget),TRUE); +#else g_action_map_add_action( G_ACTION_MAP(widget), G_ACTION(g_property_action_new("menubar", widget, "show-menubar")) -- libgit2 0.21.2