1 /*
2 * osx.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2015 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #ifdef MAC_INTEGRATION
22
23 #include "osx.h"
24
25 #include "utils.h"
26 #include "ui_utils.h"
27 #include "main.h"
28
29
app_block_termination_cb(GtkosxApplication * app,gpointer data)30 static gboolean app_block_termination_cb(GtkosxApplication *app, gpointer data)
31 {
32 return !main_quit();
33 }
34
35
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. */
open_project_idle(gchar * locale_path)40 static gboolean open_project_idle(gchar *locale_path)
41 {
42 gchar *utf8_path;
43
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;
51 }
52
53
app_open_file_cb(GtkosxApplication * osx_app,gchar * path,gpointer user_data)54 static gboolean app_open_file_cb(GtkosxApplication *osx_app, gchar *path, gpointer user_data)
55 {
56 gchar opened = FALSE;
57 gchar *locale_path;
58
59 locale_path = utils_get_locale_from_utf8(path);
60
61 if (!g_path_is_absolute(locale_path))
62 {
63 gchar *cwd = g_get_current_dir();
64 SETPTR(locale_path, g_build_filename(cwd, locale_path, NULL));
65 g_free(cwd);
66 }
67
68 if (g_str_has_suffix(path, ".geany"))
69 {
70 g_idle_add((GSourceFunc)open_project_idle, locale_path);
71 opened = TRUE;
72 }
73 else
74 {
75 opened = document_open_file(locale_path, FALSE, NULL, NULL) != NULL;
76 g_free(locale_path);
77 }
78
79 return opened;
80 }
81
82
on_new_window(GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)83 static void on_new_window(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
84 {
85 utils_start_new_geany_instance(NULL);
86 }
87
88
osx_ui_init(void)89 void osx_ui_init(void)
90 {
91 GtkWidget *item, *menu;
92 GtkosxApplication *osx_app = gtkosx_application_get();
93
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));
97
98 item = ui_lookup_widget(main_widgets.window, "menu_quit1");
99 gtk_widget_hide(item);
100
101 item = ui_lookup_widget(main_widgets.window, "menu_info1");
102 gtkosx_application_insert_app_menu_item(osx_app, item, 0);
103
104 item = ui_lookup_widget(main_widgets.window, "menu_help1");
105 gtkosx_application_set_help_menu(osx_app, GTK_MENU_ITEM(item));
106
107 gtkosx_application_set_use_quartz_accelerators(osx_app, FALSE);
108
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);
113
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));
119 }
120
121 #endif /* MAC_INTEGRATION */
122