plugins: plugin loader redesign
[geany-mirror.git] / src / osx.c
blob2bfca577204b33e5068f83fbaa696e7d2271a634
1 /*
2 * osx.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2015 Jiri Techet <techet(at)gmail(dot)com>
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 static void app_active_cb(GtkosxApplication* app, G_GNUC_UNUSED gpointer user_data)
91 GeanyDocument *doc = document_get_current();
92 if (doc)
93 document_check_disk_status(doc, TRUE);
97 void osx_ui_init(void)
99 GtkWidget *item, *menu;
100 GtkosxApplication *osx_app = gtkosx_application_get();
102 item = ui_lookup_widget(main_widgets.window, "menubar1");
103 gtk_widget_hide(item);
104 gtkosx_application_set_menu_bar(osx_app, GTK_MENU_SHELL(item));
106 item = ui_lookup_widget(main_widgets.window, "menu_quit1");
107 gtk_widget_hide(item);
109 item = ui_lookup_widget(main_widgets.window, "menu_info1");
110 gtkosx_application_insert_app_menu_item(osx_app, item, 0);
112 item = ui_lookup_widget(main_widgets.window, "menu_help1");
113 gtkosx_application_set_help_menu(osx_app, GTK_MENU_ITEM(item));
115 gtkosx_application_set_use_quartz_accelerators(osx_app, FALSE);
117 g_signal_connect(osx_app, "NSApplicationBlockTermination",
118 G_CALLBACK(app_block_termination_cb), NULL);
119 g_signal_connect(osx_app, "NSApplicationOpenFile",
120 G_CALLBACK(app_open_file_cb), NULL);
121 g_signal_connect(osx_app, "NSApplicationDidBecomeActive",
122 G_CALLBACK(app_active_cb), NULL);
124 menu = gtk_menu_new();
125 item = gtk_menu_item_new_with_label("New Window");
126 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(on_new_window), NULL);
127 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
128 gtkosx_application_set_dock_menu(osx_app, GTK_MENU_SHELL(menu));
131 #endif /* MAC_INTEGRATION */