From a19a45147ae940f4b30d66fcb3ddbbcba57ac8ba Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sat, 17 Nov 2012 22:21:09 +0100 Subject: [PATCH] Allow to compile against gtk3. Fixed not setting of completion colors. Removed disabling of scrollbars. This can be done for gtk2 via ~/.gtkrc-2.0: style "vimp-no-scrollbars" { GtkScrollbar::slider-width=0 GtkScrollbar::trough-border=0 } widget "vimp*" style "vimp-no-scrollbars" and for gtk3 via XDG_CONFIG_HOME/gtk-3.0/gtk.css #vimp GtkScrollbar { -GtkRange-slider-width: 0; -GtkRange-trough-border: 0; } --- config.mk | 20 +++++++++++- doc/config | 2 +- src/completion.c | 14 ++++----- src/keybind.c | 82 ++++++++++++++++++------------------------------ src/keybind.h | 10 +----- src/main.c | 43 ++++++++++--------------- src/main.h | 28 ++++++++++++++--- src/setting.c | 34 ++++++++++---------- 8 files changed, 115 insertions(+), 118 deletions(-) diff --git a/config.mk b/config.mk index 75175a1..8285b79 100644 --- a/config.mk +++ b/config.mk @@ -7,7 +7,21 @@ BINDIR ?= $(PREFIX)/bin/ MANDIR ?= $(PREFIX)/share/man/ #----------------compile options--------------------- -LIBS = gtk+-2.0 webkit-1.0 libsoup-2.4 +LIBS = libsoup-2.4 + +GTK3LIBS=gtk+-3.0 webkitgtk-3.0 +GTK2LIBS=gtk+-2.0 webkit-1.0 + +ifeq (${GTK}, 3) +ifeq ($(shell pkg-config --exists $(GTK3LIBS) && echo 1), 1) #has gtk3 libs +LIBS += $(GTK3LIBS) +USEGTK3 = 1 +else +LIBS += $(GTK2LIBS) +endif +else +LIBS += $(GTK2LIBS) +endif CFLAGS += $(shell pkg-config --cflags $(LIBS)) CFLAGS += -Wall @@ -26,6 +40,10 @@ CPPFLAGS += -DFEATURE_COOKIE CPPFLAGS += -DPROJECT=\"$(PROJECT)\" CPPFLAGS += -DVERSION=\"${VERSION}\" +ifeq ($(USEGTK3), 1) +CPPFLAGS += -DHAS_GTK3 +endif + #----------------developer options------------------- DFLAGS += $(CFLAGS) diff --git a/doc/config b/doc/config index dbd6123..a7d2fcf 100644 --- a/doc/config +++ b/doc/config @@ -1,6 +1,6 @@ # default config nmap gf source -nmap : input +nmap input nmap o inputopen nmap O inputopencurrent nmap d quit diff --git a/src/completion.c b/src/completion.c index bd3f2a8..eb03efe 100644 --- a/src/completion.c +++ b/src/completion.c @@ -28,7 +28,7 @@ typedef struct { static GList* completion_init_completion(GList* target, GList* source, const gchar* prefix); static GList* completion_update(GList* completion, GList* active, gboolean back); static void completion_show(gboolean back); -static void completion_set_color(Completion* completion, const GdkColor* fg, const GdkColor* bg, PangoFontDescription* font); +static void completion_set_color(Completion* completion, const VpColor* fg, const VpColor* bg, PangoFontDescription* font); static void completion_set_entry_text(Completion* completion); static Completion* completion_get_new(const gchar* label, const gchar* prefix); @@ -48,7 +48,7 @@ gboolean completion_complete(gboolean back) } /* create new completion */ -#if _HAS_GTK3 +#ifdef HAS_GTK3 vp.gui.compbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gtk_box_set_homogeneous(GTK_BOX(vp.gui.compbox), TRUE); #else @@ -243,11 +243,11 @@ static void completion_show(gboolean back) } } -static void completion_set_color(Completion* completion, const GdkColor* fg, const GdkColor* bg, PangoFontDescription* font) +static void completion_set_color(Completion* completion, const VpColor* fg, const VpColor* bg, PangoFontDescription* font) { - gtk_widget_modify_fg(completion->label, GTK_STATE_NORMAL, fg); - gtk_widget_modify_bg(completion->event, GTK_STATE_NORMAL, bg); - gtk_widget_modify_font(completion->label, font); + VP_WIDGET_OVERRIDE_COLOR(completion->label, GTK_STATE_NORMAL, fg); + VP_WIDGET_OVERRIDE_BACKGROUND(completion->event, GTK_STATE_NORMAL, bg); + VP_WIDGET_OVERRIDE_FONT(completion->label, font); } static void completion_set_entry_text(Completion* completion) @@ -278,7 +278,7 @@ static Completion* completion_get_new(const gchar* label, const gchar* prefix) c->event = gtk_event_box_new(); c->prefix = g_strdup(prefix); -#if _HAS_GTK3 +#ifdef HAS_GTK3 GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); gtk_box_set_homogeneous(GTK_BOX(hbox), TRUE); #else diff --git a/src/keybind.c b/src/keybind.c index 4eaad39..fc87bd9 100644 --- a/src/keybind.c +++ b/src/keybind.c @@ -113,7 +113,6 @@ static GSList* keybind_find(int mode, guint modkey, guint modmask, guint keyval) GSList* link; for (link = keys; link != NULL; link = link->next) { Keybind* keybind = (Keybind*)link->data; - if (keybind->keyval == keyval && keybind->modmask == modmask && keybind->modkey == modkey @@ -150,12 +149,6 @@ static void keybind_str_to_keybind(gchar* str, Keybind* keybind) keybind->modkey = str[0]; keybind->keyval = str[1]; } else { - /* special handling for shift tab */ - /* TODO find a better solution for such cases */ - if (g_ascii_strcasecmp(str, "") == 0) { - keybind->keyval = GDK_ISO_Left_Tab; - return; - } /* keybind has keys like "" or */ if (str[0] == '<') { /* no modkey set */ @@ -175,6 +168,12 @@ static void keybind_str_to_keybind(gchar* str, Keybind* keybind) } g_strfreev(string); } + + /* post process the keybinding */ + /* special handling for shift tab */ + if (keybind->keyval == GDK_Tab && keybind->modmask == GDK_SHIFT_MASK) { + keybind->keyval = GDK_ISO_Left_Tab; + } } static guint keybind_str_to_modmask(const gchar* str) @@ -200,59 +199,40 @@ static guint keybind_str_to_value(const gchar* str) static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* event) { - GdkModifierType irrelevant; - guint keyval; - static GdkKeymap *keymap; - - keymap = gdk_keymap_get_default(); - - /* Get a mask of modifiers that shouldn't be considered for this event. - * E.g.: It shouldn't matter whether ';' is shifted or not. */ - gdk_keymap_translate_keyboard_state(keymap, event->hardware_keycode, - event->state, event->group, &keyval, NULL, NULL, &irrelevant); + static GdkModifierType modifiers; + modifiers = gtk_accelerator_get_default_mod_mask(); + guint keyval = event->keyval; + guint state = (event->state & modifiers); /* check for escape or modkeys or counts */ - if ((CLEAN(event->state) & ~irrelevant) == 0) { - if (IS_ESCAPE(event)) { - completion_clean(); - /* switch to normal mode and clear the input box */ - Arg a = {VP_MODE_NORMAL, ""}; - vp_set_mode(&a); + if (keyval == GDK_Escape && state == 0) { + completion_clean(); + /* switch to normal mode and clear the input box */ + Arg a = {VP_MODE_NORMAL, ""}; + vp_set_mode(&a); + + return TRUE; + } + /* allow mode keys and counts only in normal mode */ + if (VP_MODE_NORMAL == vp.state.mode) { + if (vp.state.modkey == 0 && ((keyval >= GDK_1 && keyval <= GDK_9) + || (keyval == GDK_0 && vp.state.count))) { + /* append the new entered count to previous one */ + vp.state.count = (vp.state.count ? vp.state.count * 10 : 0) + (keyval - GDK_0); + vp_update_statusbar(); return TRUE; } - /* allow mode keys and counts only in normal mode */ - if (VP_MODE_NORMAL == vp.state.mode) { - if (vp.state.modkey == 0 && ((event->keyval >= GDK_1 && event->keyval <= GDK_9) - || (event->keyval == GDK_0 && vp.state.count))) { - /* append the new entered count to previous one */ - vp.state.count = (vp.state.count ? vp.state.count * 10 : 0) + (event->keyval - GDK_0); - vp_update_statusbar(); - - return TRUE; - } - if (strchr(modkeys->str, event->keyval) && vp.state.modkey != event->keyval) { - vp.state.modkey = (gchar)event->keyval; - vp_update_statusbar(); - - return TRUE; - } - } - } + if (strchr(modkeys->str, keyval) && vp.state.modkey != keyval) { + vp.state.modkey = (gchar)keyval; + vp_update_statusbar(); -#if 0 - /* TODO should we use a command for that too? */ - if (CLEAN_MODE(vp.state.mode) == VP_MODE_COMMAND - && (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) - ) { - completion_complete(event->keyval == GDK_ISO_Left_Tab); - return TRUE; + return TRUE; + } } -#endif /* check for keybinding */ - GSList* link = keybind_find(CLEAN_MODE(vp.state.mode), vp.state.modkey, - (CLEAN(event->state) & ~irrelevant), keyval); + GSList* link = keybind_find(CLEAN_MODE(vp.state.mode), vp.state.modkey, state, keyval); if (link) { Keybind* keybind = (Keybind*)link->data; diff --git a/src/keybind.h b/src/keybind.h index 06e8048..ef9db09 100644 --- a/src/keybind.h +++ b/src/keybind.h @@ -21,15 +21,7 @@ #define KEYBIND_H #include - -/* the CLEAN_MOD_*_MASK defines have all the bits set that will be stripped from the modifier bit field */ -#define CLEAN_MOD_NUMLOCK_MASK (GDK_MOD2_MASK) -#define CLEAN_MOD_BUTTON_MASK (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK|GDK_BUTTON4_MASK|GDK_BUTTON5_MASK) - -/* remove unused bits, numlock symbol and buttons from keymask */ -#define CLEAN(mask) (mask & (GDK_MODIFIER_MASK) & ~(CLEAN_MOD_NUMLOCK_MASK) & ~(CLEAN_MOD_BUTTON_MASK)) -#define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval)) -#define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || (s == GDK_CONTROL_MASK && k == GDK_c)) +#include typedef struct { int mode; /* mode maks for allowed browser modes */ diff --git a/src/main.c b/src/main.c index 897aa41..d5a061c 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,6 @@ VpCore vp; /* callbacks */ static void vp_webview_load_status_cb(WebKitWebView* view, GParamSpec* pspec, gpointer user_data); static void vp_destroy_window_cb(GtkWidget* widget, GtkWidget* window, gpointer user_data); -static gboolean vp_frame_scrollbar_policy_changed_cb(void); static void vp_inputbox_activate_cb(GtkEntry* entry, gpointer user_data); static gboolean vp_inputbox_keyrelease_cb(GtkEntry* entry, GdkEventKey* event); static void vp_scroll_cb(GtkAdjustment* adjustment, gpointer data); @@ -88,11 +87,6 @@ static void vp_destroy_window_cb(GtkWidget* widget, GtkWidget* window, gpointer command_close(0); } -static gboolean vp_frame_scrollbar_policy_changed_cb(void) -{ - return TRUE; -} - static void vp_inputbox_activate_cb(GtkEntry *entry, gpointer user_data) { gboolean success = FALSE; @@ -128,9 +122,9 @@ static gboolean vp_inputbox_keyrelease_cb(GtkEntry* entry, GdkEventKey* event) } static void vp_scroll_cb(GtkAdjustment* adjustment, gpointer data) -{ +{ vp_update_statusbar(); -} +} #ifdef FEATURE_COOKIE static void vp_new_request_cb(SoupSession* session, SoupMessage *message, gpointer data) @@ -427,11 +421,6 @@ static void vp_init_gui(void) { Gui* gui = &vp.gui; - gui->sb_h = GTK_SCROLLBAR(gtk_hscrollbar_new(NULL)); - gui->sb_v = GTK_SCROLLBAR(gtk_vscrollbar_new(NULL)); - gui->adjust_h = gtk_range_get_adjustment(GTK_RANGE(gui->sb_h)); - gui->adjust_v = gtk_range_get_adjustment(GTK_RANGE(gui->sb_v)); - GdkGeometry hints = {10, 10}; gui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_wmclass(GTK_WINDOW(gui->window), PROJECT, PROJECT); @@ -439,6 +428,7 @@ static void vp_init_gui(void) gtk_window_set_title(GTK_WINDOW(gui->window), PROJECT); gtk_window_set_geometry_hints(GTK_WINDOW(gui->window), NULL, &hints, GDK_HINT_MIN_SIZE); gtk_window_set_icon(GTK_WINDOW(gui->window), NULL); + gtk_widget_set_name(GTK_WIDGET(gui->window), PROJECT); /* Create a browser instance */ gui->webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); @@ -450,21 +440,22 @@ static void vp_init_gui(void) #endif /* Create a scrollable area */ - gui->viewport = gtk_scrolled_window_new(gui->adjust_h, gui->adjust_v); - gtk_scrolled_window_set_policy( - GTK_SCROLLED_WINDOW(gui->viewport), - GTK_POLICY_NEVER, GTK_POLICY_NEVER - ); - - gui->box = GTK_BOX(gtk_vbox_new(FALSE, 0)); + gui->viewport = gtk_scrolled_window_new(NULL, NULL); + gui->adjust_h = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(gui->viewport)); + gui->adjust_v = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(gui->viewport)); /* Prepare the imputbox */ gui->inputbox = gtk_entry_new(); gtk_entry_set_inner_border(GTK_ENTRY(gui->inputbox), NULL); g_object_set(gtk_widget_get_settings(gui->inputbox), "gtk-entry-select-on-focus", FALSE, NULL); - /* Prepare the statusbar */ +#ifdef HAS_GTK3 + gui->box = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)); + gui->statusbar.box = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0)); +#else + gui->box = GTK_BOX(gtk_vbox_new(FALSE, 0)); gui->statusbar.box = GTK_BOX(gtk_hbox_new(FALSE, 0)); +#endif gui->statusbar.left = gtk_label_new(NULL); gui->statusbar.right = gtk_label_new(NULL); @@ -508,22 +499,20 @@ static void vp_init_files(void) g_free(path); } -void vp_set_widget_font(GtkWidget* widget, const GdkColor* fg, const GdkColor* bg, PangoFontDescription* font) +void vp_set_widget_font(GtkWidget* widget, const VpColor* fg, const VpColor* bg, PangoFontDescription* font) { - gtk_widget_modify_font(widget, font); - gtk_widget_modify_text(widget, GTK_STATE_NORMAL, fg); - gtk_widget_modify_base(widget, GTK_STATE_NORMAL, bg); + VP_WIDGET_OVERRIDE_FONT(widget, font); + VP_WIDGET_OVERRIDE_TEXT(widget, GTK_STATE_NORMAL, fg); + VP_WIDGET_OVERRIDE_BASE(widget, GTK_STATE_NORMAL, bg); } static void vp_setup_signals(void) { Gui* gui = &vp.gui; - WebKitWebFrame *frame = webkit_web_view_get_main_frame(gui->webview); /* Set up callbacks so that if either the main window or the browser * instance is closed, the program will exit */ g_signal_connect(gui->window, "destroy", G_CALLBACK(vp_destroy_window_cb), NULL); - g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed", G_CALLBACK(vp_frame_scrollbar_policy_changed_cb), NULL); g_signal_connect(G_OBJECT(gui->webview), "notify::load-status", G_CALLBACK(vp_webview_load_status_cb), NULL); g_object_connect( diff --git a/src/main.h b/src/main.h index 26eacce..8bd5093 100644 --- a/src/main.h +++ b/src/main.h @@ -47,6 +47,24 @@ #define CLEAN_MODE(mode) ((mode) & ~(VP_MODE_COMPLETE)) #define CLEAR_INPUT() (vp_echo(VP_MSG_NORMAL, "")) +#ifdef HAS_GTK3 +#define VpColor GdkRGBA +#define VP_COLOR_PARSE(color, string) (gdk_rgba_parse(color, string)) +#define VP_WIDGET_OVERRIDE_BACKGROUND gtk_widget_override_background_color +#define VP_WIDGET_OVERRIDE_BASE gtk_widget_override_background_color +#define VP_WIDGET_OVERRIDE_COLOR gtk_widget_override_color +#define VP_WIDGET_OVERRIDE_TEXT gtk_widget_override_color +#define VP_WIDGET_OVERRIDE_FONT gtk_widget_override_font +#else +#define VpColor GdkColor +#define VP_COLOR_PARSE(color, string) (gdk_color_parse(string, color)) +#define VP_WIDGET_OVERRIDE_BACKGROUND gtk_widget_modify_bg +#define VP_WIDGET_OVERRIDE_BASE gtk_widget_modify_base +#define VP_WIDGET_OVERRIDE_COLOR gtk_widget_modify_fg +#define VP_WIDGET_OVERRIDE_TEXT gtk_widget_modify_text +#define VP_WIDGET_OVERRIDE_FONT gtk_widget_modify_font +#endif + /* enums */ typedef enum _vp_mode { VP_MODE_NORMAL = 1<<0, @@ -180,11 +198,11 @@ typedef struct { } Completions; typedef struct { - GdkColor input_fg[VP_MSG_LAST]; - GdkColor input_bg[VP_MSG_LAST]; + VpColor input_fg[VP_MSG_LAST]; + VpColor input_bg[VP_MSG_LAST]; PangoFontDescription* input_font[VP_MSG_LAST]; - GdkColor comp_fg[VP_COMP_LAST]; - GdkColor comp_bg[VP_COMP_LAST]; + VpColor comp_fg[VP_COMP_LAST]; + VpColor comp_bg[VP_COMP_LAST]; PangoFontDescription* comp_font[VP_COMP_LAST]; } Style; @@ -216,7 +234,7 @@ void vp_update_statusbar(void); void vp_update_urlbar(const gchar* uri); void vp_echo(const MessageType type, gboolean hide, const char *error, ...); gboolean vp_set_mode(const Arg* arg); -void vp_set_widget_font(GtkWidget* widget, const GdkColor* fg, const GdkColor* bg, PangoFontDescription* font); +void vp_set_widget_font(GtkWidget* widget, const VpColor* fg, const VpColor* bg, PangoFontDescription* font); gboolean vp_load_uri(const Arg* arg); void vp_clean_up(void); diff --git a/src/setting.c b/src/setting.c index a8a3de5..22d96fe 100644 --- a/src/setting.c +++ b/src/setting.c @@ -197,24 +197,24 @@ static gboolean setting_scrollstep(const Setting* s) static gboolean setting_status_color_bg(const Setting* s) { - GdkColor color; + VpColor color; - gdk_color_parse(s->arg.s, &color); - gtk_widget_modify_bg(vp.gui.eventbox, GTK_STATE_NORMAL, &color); - gtk_widget_modify_bg(GTK_WIDGET(vp.gui.statusbar.left), GTK_STATE_NORMAL, &color); - gtk_widget_modify_bg(GTK_WIDGET(vp.gui.statusbar.right), GTK_STATE_NORMAL, &color); + VP_COLOR_PARSE(&color, s->arg.s); + VP_WIDGET_OVERRIDE_BACKGROUND(vp.gui.eventbox, GTK_STATE_NORMAL, &color); + VP_WIDGET_OVERRIDE_BACKGROUND(GTK_WIDGET(vp.gui.statusbar.left), GTK_STATE_NORMAL, &color); + VP_WIDGET_OVERRIDE_BACKGROUND(GTK_WIDGET(vp.gui.statusbar.right), GTK_STATE_NORMAL, &color); return TRUE; } static gboolean setting_status_color_fg(const Setting* s) { - GdkColor color; + VpColor color; - gdk_color_parse(s->arg.s, &color); - gtk_widget_modify_fg(vp.gui.eventbox, GTK_STATE_NORMAL, &color); - gtk_widget_modify_fg(GTK_WIDGET(vp.gui.statusbar.left), GTK_STATE_NORMAL, &color); - gtk_widget_modify_fg(GTK_WIDGET(vp.gui.statusbar.right), GTK_STATE_NORMAL, &color); + VP_COLOR_PARSE(&color, s->arg.s); + VP_WIDGET_OVERRIDE_COLOR(vp.gui.eventbox, GTK_STATE_NORMAL, &color); + VP_WIDGET_OVERRIDE_COLOR(GTK_WIDGET(vp.gui.statusbar.left), GTK_STATE_NORMAL, &color); + VP_WIDGET_OVERRIDE_COLOR(GTK_WIDGET(vp.gui.statusbar.right), GTK_STATE_NORMAL, &color); return TRUE; } @@ -224,9 +224,9 @@ static gboolean setting_status_font(const Setting* s) PangoFontDescription* font; font = pango_font_description_from_string(s->arg.s); - gtk_widget_modify_font(vp.gui.eventbox, font); - gtk_widget_modify_font(GTK_WIDGET(vp.gui.statusbar.left), font); - gtk_widget_modify_font(GTK_WIDGET(vp.gui.statusbar.right), font); + VP_WIDGET_OVERRIDE_FONT(vp.gui.eventbox, font); + VP_WIDGET_OVERRIDE_FONT(GTK_WIDGET(vp.gui.statusbar.left), font); + VP_WIDGET_OVERRIDE_FONT(GTK_WIDGET(vp.gui.statusbar.right), font); pango_font_description_free(font); @@ -239,9 +239,9 @@ static gboolean setting_input_style(const Setting* s) MessageType type = g_str_has_suffix(s->name, "normal") ? VP_MSG_NORMAL : VP_MSG_ERROR; if (g_str_has_prefix(s->name, "input-bg")) { - gdk_color_parse(s->arg.s, &style->input_bg[type]); + VP_COLOR_PARSE(&style->input_bg[type], s->arg.s); } else if (g_str_has_prefix(s->name, "input-fg")) { - gdk_color_parse(s->name, &style->input_fg[type]); + VP_COLOR_PARSE(&style->input_fg[type], s->arg.s); } else if (g_str_has_prefix(s->arg.s, "input-font")) { if (style->input_font[type]) { pango_font_description_free(style->input_font[type]); @@ -258,9 +258,9 @@ static gboolean setting_completion_style(const Setting* s) CompletionStyle type = g_str_has_suffix(s->name, "normal") ? VP_COMP_NORMAL : VP_COMP_ACTIVE; if (g_str_has_prefix(s->name, "completion-bg")) { - gdk_color_parse(s->arg.s, &style->comp_bg[type]); + VP_COLOR_PARSE(&style->comp_bg[type], s->arg.s); } else if (g_str_has_prefix(s->name, "completion-fg")) { - gdk_color_parse(s->name, &style->comp_fg[type]); + VP_COLOR_PARSE(&style->comp_fg[type], s->arg.s); } else if (g_str_has_prefix(s->arg.s, "completion-font")) { if (style->comp_font[type]) { pango_font_description_free(style->comp_font[type]); -- 2.20.1