Merge pull request #3093 from eht16/win_bundle_xz
[geany-mirror.git] / src / osx.c
bloba1fcd4f69b965203065d46721a2c28860e23235a
1 /*
2 * osx.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2015 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #ifdef MAC_INTEGRATION
23 #include "osx.h"
25 #include "utils.h"
26 #include "ui_utils.h"
27 #include "main.h"
30 static gboolean app_block_termination_cb(GtkosxApplication *app, gpointer data)
32 return !main_quit();
36 /* For some reason osx doesn't like when the NSApplicationOpenFile handler blocks for
37 * a long time which may be caused by the project_ask_close() below. Finish the
38 * NSApplicationOpenFile handler immediately and perform the potentially blocking
39 * code on idle in this function. */
40 static gboolean open_project_idle(gchar *locale_path)
42 gchar *utf8_path;
44 utf8_path = utils_get_utf8_from_locale(locale_path);
45 if (app->project == NULL ||
46 (g_strcmp0(utf8_path, app->project->file_name) != 0 && project_ask_close()))
47 project_load_file_with_session(locale_path);
48 g_free(utf8_path);
49 g_free(locale_path);
50 return FALSE;
54 static gboolean app_open_file_cb(GtkosxApplication *osx_app, gchar *path, gpointer user_data)
56 gchar opened = FALSE;
57 gchar *locale_path;
59 locale_path = utils_get_locale_from_utf8(path);
61 if (!g_path_is_absolute(locale_path))
63 gchar *cwd = g_get_current_dir();
64 SETPTR(locale_path, g_build_filename(cwd, locale_path, NULL));
65 g_free(cwd);
68 if (g_str_has_suffix(path, ".geany"))
70 g_idle_add((GSourceFunc)open_project_idle, locale_path);
71 opened = TRUE;
73 else
75 opened = document_open_file(locale_path, FALSE, NULL, NULL) != NULL;
76 g_free(locale_path);
79 return opened;
83 static void on_new_window(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
85 utils_start_new_geany_instance(NULL);
89 void osx_ui_init(void)
91 GtkWidget *item, *menu;
92 GtkosxApplication *osx_app = gtkosx_application_get();
94 item = ui_lookup_widget(main_widgets.window, "menubar1");
95 gtk_widget_hide(item);
96 gtkosx_application_set_menu_bar(osx_app, GTK_MENU_SHELL(item));
98 item = ui_lookup_widget(main_widgets.window, "menu_quit1");
99 gtk_widget_hide(item);
101 item = ui_lookup_widget(main_widgets.window, "menu_info1");
102 gtkosx_application_insert_app_menu_item(osx_app, item, 0);
104 item = ui_lookup_widget(main_widgets.window, "menu_help1");
105 gtkosx_application_set_help_menu(osx_app, GTK_MENU_ITEM(item));
107 gtkosx_application_set_use_quartz_accelerators(osx_app, FALSE);
109 g_signal_connect(osx_app, "NSApplicationBlockTermination",
110 G_CALLBACK(app_block_termination_cb), NULL);
111 g_signal_connect(osx_app, "NSApplicationOpenFile",
112 G_CALLBACK(app_open_file_cb), NULL);
114 menu = gtk_menu_new();
115 item = gtk_menu_item_new_with_label("New Window");
116 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(on_new_window), NULL);
117 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
118 gtkosx_application_set_dock_menu(osx_app, GTK_MENU_SHELL(menu));
121 #endif /* MAC_INTEGRATION */