rosenrot/window.c

166 lines
4.3 KiB
C
Raw Normal View History

2022-05-10 15:35:37 +00:00
#include "window.h"
2022-05-10 21:47:17 +00:00
#include "rose.h"
2022-05-10 15:35:37 +00:00
/* #include "gestures.h" */
#include "config.h"
#include "webview.h"
2022-05-10 17:52:56 +00:00
#define LENGTH(x) (sizeof(x) / sizeof(x[0]))
2022-05-10 15:35:37 +00:00
struct _RoseWindow {
GtkApplicationWindow parent_instance;
2022-05-10 21:47:17 +00:00
guint xid;
2022-05-10 15:35:37 +00:00
GtkWindow *window;
WebKitWebView *webview;
GHashTable *action_groups;
GHashTable *action_labels;
/* RoseGestureController *mouse_gesture_controller; */
};
enum {
PROP_ACTIVE_CHILD
};
G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW)
2022-05-10 17:52:56 +00:00
static void read_clipboard(GObject *object,
GAsyncResult *res,
gpointer webview)
{
GdkDisplay *dpy = gdk_display_get_default();
GdkClipboard *clipboard = gdk_display_get_clipboard(dpy);
webkit_web_view_load_uri(
WEBKIT_WEB_VIEW(webview),
gdk_clipboard_read_text_finish(clipboard, res, NULL)
);
}
2022-05-10 18:28:39 +00:00
static gboolean key_press_callback(RoseWindow *window,
2022-05-10 15:35:37 +00:00
guint keyval,
guint keycode,
GdkModifierType state)
{
2022-05-10 17:52:56 +00:00
for (int i = 0; i < LENGTH(keys); i++) {
2022-05-10 18:28:39 +00:00
if (keys[i].modkey == state
2022-05-10 17:52:56 +00:00
&& keys[i].keycod == keyval) {
switch (keys[i].funcid) {
case goback:
2022-05-10 18:28:39 +00:00
webkit_web_view_go_back(window->webview);
2022-05-10 17:52:56 +00:00
break;
case goforward:
2022-05-10 18:28:39 +00:00
webkit_web_view_go_forward(window->webview);
2022-05-10 17:52:56 +00:00
break;
case copy_url: {
GdkDisplay *dpy = gdk_display_get_default();
gdk_clipboard_set_text(
gdk_display_get_clipboard(dpy),
2022-05-10 18:28:39 +00:00
webkit_web_view_get_uri(window->webview)
2022-05-10 17:52:56 +00:00
);
} break;
case paste_url: {
GdkDisplay *dpy = gdk_display_get_default();
GdkClipboard *clipboard = gdk_display_get_clipboard(dpy);
2022-05-10 18:28:39 +00:00
gdk_clipboard_read_text_async(clipboard, NULL, read_clipboard, window->webview);
2022-05-10 17:52:56 +00:00
} break;
2022-05-10 18:28:39 +00:00
case fullscreen:
if (gtk_window_is_fullscreen(window->window))
gtk_window_unfullscreen(GTK_WINDOW(window->window));
else
gtk_window_fullscreen(GTK_WINDOW(window->window));
break;
2022-05-10 21:47:17 +00:00
case inspector:
puts("wqf");
break;
case search: {
int id = fork();
if (id == 0) {
if (dpy)
close(ConnectionNumber(dpy));
close(spair[0]);
close(spair[1]);
setsid();
char* argument_list[] = {"/bin/sh", "-c", "surf-open", NULL};
execvp("/bin/sh", argument_list);
perror(" failed");
exit(1);
} else {
wait(&id);
webkit_web_view_load_uri(window->webview, getatom(AtomGo));
}
}
2022-05-10 17:52:56 +00:00
}
2022-05-10 15:35:37 +00:00
}
}
2022-05-10 18:28:39 +00:00
2022-05-10 15:35:37 +00:00
return GDK_EVENT_PROPAGATE;
}
static void rose_window_constructed(GObject *object)
{
RoseWindow *window;
GAction *action;
GActionGroup *action_group;
GSimpleActionGroup *simple_action_group;
guint i;
GApplication *app;
GtkEventController *controller;
G_OBJECT_CLASS(rose_window_parent_class)->constructed(object);
window = ROSE_WINDOW(window);
}
static void rose_window_init(RoseWindow *window)
{
window->window = GTK_WINDOW(gtk_window_new());
}
2022-05-10 21:47:17 +00:00
guint rose_window_show(GtkApplication *app, RoseWindow *window)
2022-05-10 15:35:37 +00:00
{
2022-05-10 21:47:17 +00:00
GtkWidget *w = gtk_application_window_new(app);
2022-05-10 18:28:39 +00:00
window->window = GTK_WINDOW(w);
2022-05-10 21:47:17 +00:00
GtkWidget *webview = rose_webview_new();
window->webview = WEBKIT_WEB_VIEW(webview);
if (homepage)
rose_webview_load_url(WEBKIT_WEB_VIEW(webview), homepage);
gtk_window_set_child(GTK_WINDOW(w), GTK_WIDGET(webview));
2022-05-10 15:35:37 +00:00
/* Keyboard shortcuts */
GtkEventController *controller;
controller = gtk_event_controller_key_new();
2022-05-10 18:28:39 +00:00
g_signal_connect_swapped(controller, "key-pressed", G_CALLBACK(key_press_callback), window);
2022-05-10 15:35:37 +00:00
gtk_widget_add_controller(GTK_WIDGET(w), controller);
gtk_widget_show(w);
2022-05-10 21:47:17 +00:00
return gdk_x11_surface_get_xid(gtk_native_get_surface(GTK_NATIVE(w)));
2022-05-10 15:35:37 +00:00
}
void rose_window_set_webview(RoseWindow *window, GtkWidget *webview)
{
window->webview = WEBKIT_WEB_VIEW(webview);
}
static void rose_window_class_init(RoseWindowClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS(class);
GtkApplicationWindowClass *window_class = GTK_APPLICATION_WINDOW_CLASS(class);
object_class->constructed = rose_window_constructed;
/* g_object_class_override_property(object_class, */
/* PROP_ACTIVE_CHILD, */
/* "active-child"); */
}
RoseWindow* rose_window_new(GtkApplication *app)
{
return g_object_new(
GTK_TYPE_APPLICATION_WINDOW,
"application", GTK_APPLICATION(app),
NULL
);
}