This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathcefclient_gtk.cpp
More file actions
281 lines (223 loc) · 7.96 KB
/
cefclient_gtk.cpp
File metadata and controls
281 lines (223 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "cefclient.h"
#include <gtk/gtk.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <sys/stat.h>
#include "cefclient.h"
#include "include/cef_app.h"
#include "include/cef_version.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "client_handler.h"
#include "client_switches.h"
#include "appshell_node_process.h"
static std::string APPICONS[] = {"appshell32.png","appshell48.png","appshell128.png","appshell256.png"};
char szWorkingDir[512]; // The current working directory
std::string szInitialUrl;
std::string szRunningDir;
int add_handler_id;
bool isReallyClosing = false;
// The global ClientHandler reference.
extern CefRefPtr<ClientHandler> g_handler;
// Application startup time
time_t g_appStartupTime;
void destroy(void) {
CefQuitMessageLoop();
}
void TerminationSignalHandler(int signatl) {
destroy();
}
void HandleAdd(GtkContainer *container,
GtkWidget *widget,
gpointer user_data) {
g_signal_handler_disconnect(container, add_handler_id);
if(gtk_widget_get_can_focus(widget)) {
gtk_widget_grab_focus(widget);
}
else {
add_handler_id = g_signal_connect(G_OBJECT(widget), "add",
G_CALLBACK(HandleAdd), NULL);
}
}
static gboolean HandleQuit(int signatl) {
if (!isReallyClosing && g_handler.get() && g_handler->GetBrowserId()) {
CefRefPtr<CommandCallback> callback = new CloseWindowCommandCallback(g_handler->GetBrowser());
g_handler->SendJSCommand(g_handler->GetBrowser(), FILE_CLOSE_WINDOW, callback);
return TRUE;
}
destroy();
}
bool FileExists(std::string path) {
struct stat buf;
return (stat(path.c_str(), &buf) >= 0) && (S_ISREG(buf.st_mode));
}
int GetInitialUrl() {
GtkWidget *dialog;
const char* dialog_title = "Please select the index.html file";
GtkFileChooserAction file_or_directory = GTK_FILE_CHOOSER_ACTION_OPEN ;
dialog = gtk_file_chooser_dialog_new (dialog_title,
NULL,
file_or_directory,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
szInitialUrl = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_widget_destroy (dialog);
return 0;
}
return -1;
}
// Global functions
std::string AppGetWorkingDirectory() {
return szWorkingDir;
}
std::string AppGetRunningDirectory() {
if(szRunningDir.length() > 0)
return szRunningDir;
char buf[512];
int len = readlink("/proc/self/exe", buf, 512);
if(len < 0)
return AppGetWorkingDirectory(); //# Well, can't think of any real-world case where this would be happen
for(; len >= 0; len--){
if(buf[len] == '/'){
buf[len] = '\0';
szRunningDir.append(buf);
return szRunningDir;
}
}
}
CefString AppGetCachePath() {
std::string cachePath = std::string(ClientApp::AppGetSupportDirectory()) + "/cef_data";
return CefString(cachePath);
}
GtkWidget* AddMenuEntry(GtkWidget* menu_widget, const char* text,
GCallback callback) {
GtkWidget* entry = gtk_menu_item_new_with_label(text);
g_signal_connect(entry, "activate", callback, NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu_widget), entry);
return entry;
}
GtkWidget* CreateMenu(GtkWidget* menu_bar, const char* text) {
GtkWidget* menu_widget = gtk_menu_new();
GtkWidget* menu_header = gtk_menu_item_new_with_label(text);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_header), menu_widget);
gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_header);
return menu_widget;
}
// Callback for Debug > Get Source... menu item.
gboolean GetSourceActivated(GtkWidget* widget) {
return FALSE;
}
int main(int argc, char* argv[]) {
CefMainArgs main_args(argc, argv);
g_appStartupTime = time(NULL);
gtk_init(&argc, &argv);
CefRefPtr<ClientApp> app(new ClientApp);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, app.get());
if (exit_code >= 0)
return exit_code;
//Retrieve the current working directory
if (!getcwd(szWorkingDir, sizeof (szWorkingDir)))
return -1;
GtkWidget* window;
// Parse command line arguments.
AppInitCommandLine(argc, argv);
CefSettings settings;
// Populate the settings based on command line arguments.
AppGetSettings(settings, app);
// Check cache_path setting
if (CefString(&settings.cache_path).length() == 0) {
CefString(&settings.cache_path) = AppGetCachePath();
}
CefRefPtr<CefCommandLine> cmdLine = AppGetCommandLine();
if (cmdLine->HasSwitch(cefclient::kStartupPath)) {
szInitialUrl = cmdLine->GetSwitchValue(cefclient::kStartupPath);
} else {
szInitialUrl = AppGetRunningDirectory();
szInitialUrl.append("/dev/src/index.html");
if (!FileExists(szInitialUrl)) {
szInitialUrl = AppGetRunningDirectory();
szInitialUrl.append("/www/index.html");
if (!FileExists(szInitialUrl)) {
if (GetInitialUrl() < 0)
return 0;
}
}
}
// Initialize CEF.
CefInitialize(main_args, settings, app.get());
// Set window icon
std::vector<std::string> icons(APPICONS, APPICONS + sizeof(APPICONS) / sizeof(APPICONS[0]) );
GList *list = NULL;
for(int i = 0; i < icons.size(); ++i) {
GError *error = NULL;
std::string running_dir = AppGetRunningDirectory();
gchar *iconpath = g_strdup_printf("%s/%s", running_dir.c_str(), icons[i].c_str());
GdkPixbuf *icon = gdk_pixbuf_new_from_file(iconpath, &error);
g_free(iconpath);
if (!icon) {
g_printerr("Unable to create GdkPixbuf object. Reason: %s\n", error->message);
g_error_free(error);
continue;
}
list = g_list_append(list, icon);
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
gtk_window_set_icon_list(GTK_WINDOW(window), list);
// Free icon list
g_list_foreach(list, (GFunc) g_object_unref, NULL);
g_list_free(list);
GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
GtkWidget* menuBar = gtk_menu_bar_new();
// GtkWidget* debug_menu = CreateMenu(menuBar, "Tests");
// AddMenuEntry(debug_menu, "Hello World Menu",
// G_CALLBACK(GetSourceActivated));
gtk_box_pack_start(GTK_BOX(vbox), menuBar, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(HandleQuit), NULL);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_widget_destroyed), &window);
add_handler_id = g_signal_connect(G_OBJECT(window), "add",
G_CALLBACK(HandleAdd), NULL);
// g_signal_connect(G_OBJECT(window), "destroy",
// G_CALLBACK(destroy), NULL);
// Create the handler.
g_handler = new ClientHandler();
g_handler->SetMainHwnd(vbox);
// Create the browser view.
CefWindowInfo window_info;
CefBrowserSettings browserSettings;
browserSettings.web_security = STATE_DISABLED;
window_info.SetAsChild(vbox);
CefBrowserHost::CreateBrowser(
window_info,
static_cast<CefRefPtr<CefClient> >(g_handler),
"file://"+szInitialUrl, browserSettings);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(GTK_WIDGET(window));
// Install an signal handler so we clean up after ourselves.
signal(SIGINT, TerminationSignalHandler);
signal(SIGTERM, TerminationSignalHandler);
// Start the node server process
startNodeProcess();
CefRunMessageLoop();
CefShutdown();
return 0;
}
CefString AppGetProductVersionString() {
// TODO
return CefString("");
}
CefString AppGetChromiumVersionString() {
// TODO
return CefString("");
}