]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/gtkapp.c
New program 'osxlaunch', to use as an OS X bundle launcher.
[PuTTY.git] / unix / gtkapp.c
1 /*
2  * gtkapp.c: a top-level front end to GUI PuTTY and pterm, using
3  * GtkApplication. Suitable for OS X. Currently unfinished.
4  *
5  * (You could run it on ordinary Linux GTK too, in principle, but I
6  * don't think it would be particularly useful to do so, even once
7  * it's fully working.)
8  */
9
10 #include <assert.h>
11 #include <stdlib.h>
12
13 #include <unistd.h>
14
15 #include <gtk/gtk.h>
16
17 #define MAY_REFER_TO_GTK_IN_HEADERS
18
19 #include "putty.h"
20
21 char *x_get_default(const char *key) { return NULL; }
22
23 #if !GTK_CHECK_VERSION(3,0,0)
24 /* This front end only works in GTK 3. If that's not what we've got,
25  * it's easier to just turn this program into a trivial stub by ifdef
26  * in the source than it is to remove it in the makefile edifice. */
27 int main(int argc, char **argv)
28 {
29     fprintf(stderr, "launcher does nothing on non-OSX platforms\n");
30     return 1;
31 }
32 GtkWidget *make_gtk_toplevel_window(void *frontend) { return NULL; }
33 void launch_duplicate_session(Conf *conf) {}
34 void launch_new_session(void) {}
35 void launch_saved_session(const char *str) {}
36 #else /* GTK_CHECK_VERSION(3,0,0) */
37
38 static void startup(GApplication *app, gpointer user_data)
39 {
40     GMenu *menubar, *menu, *section;
41
42     menubar = g_menu_new();
43
44     menu = g_menu_new();
45     g_menu_append_submenu(menubar, "File", G_MENU_MODEL(menu));
46
47     section = g_menu_new();
48     g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
49     g_menu_append(section, "New Window", "app.newwin");
50
51     menu = g_menu_new();
52     g_menu_append_submenu(menubar, "Edit", G_MENU_MODEL(menu));
53
54     section = g_menu_new();
55     g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
56     g_menu_append(section, "Paste", "win.paste");
57
58     gtk_application_set_menubar(GTK_APPLICATION(app),
59                                 G_MENU_MODEL(menubar));
60 }
61
62 static void paste_cb(GSimpleAction *action,
63                      GVariant      *parameter,
64                      gpointer       user_data)
65 {
66     request_paste(user_data);
67 }
68
69 static const GActionEntry win_actions[] = {
70     { "paste", paste_cb },
71 };
72
73 static GtkApplication *app;
74 GtkWidget *make_gtk_toplevel_window(void *frontend)
75 {
76     GtkWidget *win = gtk_application_window_new(app);
77     g_action_map_add_action_entries(G_ACTION_MAP(win),
78                                     win_actions,
79                                     G_N_ELEMENTS(win_actions),
80                                     frontend);
81     return win;
82 }
83
84 extern int cfgbox(Conf *conf);
85
86 void launch_duplicate_session(Conf *conf)
87 {
88     assert(conf_launchable(conf));
89     new_session_window(conf, NULL);
90 }
91
92 void launch_new_session(void)
93 {
94     Conf *conf = conf_new();
95     do_defaults(NULL, conf);
96     if (conf_launchable(conf) || cfgbox(conf)) {
97         new_session_window(conf, NULL);
98     }
99 }
100
101 void launch_saved_session(const char *str)
102 {
103     Conf *conf = conf_new();
104     do_defaults(str, conf);
105     if (conf_launchable(conf) || cfgbox(conf)) {
106         new_session_window(conf, NULL);
107     }
108 }
109
110 void new_app_win(GtkApplication *app)
111 {
112     launch_new_session();
113 }
114
115 static void activate(GApplication *app,
116                      gpointer      user_data)
117 {
118     new_app_win(GTK_APPLICATION(app));
119 }
120
121 static void newwin_cb(GSimpleAction *action,
122                       GVariant      *parameter,
123                       gpointer       user_data)
124 {
125     new_app_win(GTK_APPLICATION(user_data));
126 }
127
128 static void quit_cb(GSimpleAction *action,
129                     GVariant      *parameter,
130                     gpointer       user_data)
131 {
132     g_application_quit(G_APPLICATION(user_data));
133 }
134
135 static const GActionEntry app_actions[] = {
136     { "newwin", newwin_cb },
137     { "quit", quit_cb },
138 };
139
140 int main(int argc, char **argv)
141 {
142     int status;
143
144     {
145         /* Call the function in ux{putty,pterm}.c to do app-type
146          * specific setup */
147         extern void setup(int);
148         setup(FALSE);     /* FALSE means we are not a one-session process */
149     }
150
151     if (argc > 1) {
152         extern char *pty_osx_envrestore_prefix;
153         pty_osx_envrestore_prefix = argv[--argc];
154     }
155
156     {
157         const char *home = getenv("HOME");
158         if (home) {
159             if (chdir(home)) {}
160         }
161     }
162
163     gtkcomm_setup();
164
165     app = gtk_application_new("org.tartarus.projects.putty.macputty",
166                               G_APPLICATION_FLAGS_NONE);
167     g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
168     g_signal_connect(app, "startup", G_CALLBACK(startup), NULL);
169     g_action_map_add_action_entries(G_ACTION_MAP(app),
170                                     app_actions,
171                                     G_N_ELEMENTS(app_actions),
172                                     app);
173
174     status = g_application_run(G_APPLICATION(app), argc, argv);
175     g_object_unref(app);
176
177     return status;
178 }
179
180 #endif /* GTK_CHECK_VERSION(3,0,0) */