Implements gui style settings
authorRobert Timm <mail@rtti.de>
Mon, 17 Oct 2016 15:35:59 +0000 (22:35 +0700)
committerRobert Timm <mail@rtti.de>
Tue, 18 Oct 2016 18:18:22 +0000 (01:18 +0700)
README.md
src/config.def.h
src/main.c
src/main.h
src/setting.c

index b46bc2f..e22640d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -102,7 +102,7 @@ project directory.
   - [ ] try to use the webkit2 feature of running multiple pages with related
         view instance `webkit_web_view_new_with_related_view`
   - [ ] allow setting of different scopes, global and instance (new feature)
-  - [ ] remove the settings related to the gui like `status-color-bg` this was
+  - [x] remove the settings related to the gui like `status-color-bg` this was
         only a hack and is not recommended for new gtk3 applications. the
         color and font settings should be setup by css instead.
   - [ ] webkit2 does not provide the view mode `source` so maybe this is going
index cc3b915..8d0f7f2 100644 (file)
@@ -26,6 +26,8 @@
 #define FEATURE_TITLE_PROGRESS
 /* show page title in url completions */
 #define FEATURE_TITLE_IN_COMPLETION
+/* support gui style settings compatible with vimb2 */
+#define FEATURE_GUI_STYLE_VIMB2_COMPAT
 
 #ifdef FEATURE_WGET_PROGRESS_BAR
 /* chars to use for the progressbar */
 
 /* number of chars to be shown in statusbar for ambiguous commands */
 #define SHOWCMD_LEN                 10
-/* css applied to the gui elements of the borwser window */
-#define GUI_STYLE                   "\
-#statusbar{color:#fff;background-color:#000;font:monospace bold 10;} \
-#statusbar.secure{background-color:#95e454;color:#000;} \
-#statusbar.insecure{background-color:#f77;color:#000;} \
-#input{background-color:#fff;color:#000;font:monospace 10;} \
-#input.error{background-color:#f77;font-weight:bold;} \
-#completion{color:#fff;background-color:#656565;font:monospace 10;} \
-#completion:hover{background-color:#777;} \
-#completion:selected{color:#f6f3e8;background-color:#888;}"
+/* css applied to the gui elements regardless of user's settings */
+#define GUI_STYLE_CSS_BASE          "#input text{background-color:inherit;color:inherit;caret-color:@color;font:inherit;}"
 
 /* default font size for fonts in webview */
 #define SETTING_DEFAULT_FONT_SIZE   10
index 77d7af7..dcbed9c 100644 (file)
@@ -1312,7 +1312,96 @@ static void vimb_setup(void)
 
     /* Prepare the style provider to be used for the clients and completion. */
     vb.style_provider = gtk_css_provider_get_default();
-    gtk_css_provider_load_from_data(vb.style_provider, GUI_STYLE, -1, NULL);
+}
+
+/**
+ * Update the gui style settings for client c, given a style setting name and a
+ * style setting value to be updated. The complete style sheet document will be
+ * regenerated and re-fed into gtk css provider.
+ */
+void vb_gui_style_update(Client *c, const char *setting_name_new, const char *setting_value_new)
+{
+    g_assert(c);
+    g_assert(setting_name_new);
+    g_assert(setting_value_new);
+
+    /* The css style sheet document being composed in this function */
+    GString *style_sheet = g_string_new(GUI_STYLE_CSS_BASE);
+
+    /* Mapping from vimb config setting name to css style sheet string */
+    static const char *setting_style_map[][2] = {
+#ifdef FEATURE_GUI_STYLE_VIMB2_COMPAT
+        {"completion-bg-active",        " #completion:selected{background-color:%s;}"},
+        {"completion-bg-normal",        " #completion{background-color:%s;}"},
+        {"completion-fg-active",        " #completion:selected{color:%s;}"},
+        {"completion-fg-normal",        " #completion{color:%s;}"},
+        {"completion-font",             " #completion{font:%s;}"},
+        {"input-bg-error",              " #input.error{background-color:%s;}"},
+        {"input-bg-normal",             " #input{background-color:%s;}"},
+        {"input-fg-error",              " #input.error{color:%s;}"},
+        {"input-fg-normal",             " #input{color:%s;}"},
+        {"input-font-error",            " #input.error{font:%s;}"},
+        {"input-font-normal",           " #input{font:%s;}"},
+        {"status-color-bg",             " #statusbar{background-color:%s;}"},
+        {"status-color-fg",             " #statusbar{color:%s;}"},
+        {"status-font",                 " #statusbar{font:%s;}"},
+        {"status-ssl-color-bg",         " #statusbar.secure{background-color:%s;}"},
+        {"status-ssl-color-fg",         " #statusbar.secure{color:%s;}"},
+        {"status-ssl-font",             " #statusbar.secure{font:%s;}"},
+        {"status-sslinvalid-color-bg",  " #statusbar.unsecure{background-color:%s;}"},
+        {"status-sslinvalid-color-fg",  " #statusbar.unsecure{color:%s;}"},
+        {"status-sslinvalid-font",      " #statusbar.unsecure{font:%s;}"},
+#else /* vimb3 gui style settings */
+        {"completion-css",              " #completion{%s;}"},
+        {"completion-hover-css",        " #completion:hover{%s;}"},
+        {"completion-selected-css",     " #completion:selected{%s;}"},
+        {"input-css",                   " #input{%s;}"},
+        {"input-error-css",             " #input.error{%s;}"},
+        {"status-css",                  " #statusbar{%s;}"},
+        {"status-ssl-css",              " #statusbar.secure{%s;}"},
+        {"status-sslinvalid-css",       " #statusbar.unsecure{%s;}"},
+#endif /* FEATURE_GUI_STYLE_VIMB2_COMPAT */
+
+        {0, 0},
+    };
+
+    /* For each supported style setting name */
+    for (size_t i = 0; setting_style_map[i][0]; i++) {
+        const char *setting_name = setting_style_map[i][0];
+        const char *style_string = setting_style_map[i][1];
+
+        /* If the current style setting name is the one to be updated,
+         * append the given value with appropriate css wrapping to the
+         * style sheet document. */
+        if (strcmp(setting_name, setting_name_new) == 0) {
+            if (strlen(setting_value_new)) {
+                g_string_append_printf(style_sheet, style_string, setting_value_new);
+            }
+        }
+        /* If the current style setting name is NOT the one being updated,
+         * append the css string based on the current config setting. */
+        else {
+            Setting* setting_value = (Setting*)g_hash_table_lookup(c->config.settings, setting_name);
+
+            /* If the current style setting name is not available via settings
+             * yet - this happens during setting_init() - cleanup and return.
+             * We are going to be called again. With the last setting_add(),
+             * all style setting names are available. */
+            if(!setting_value) {
+                goto cleanup;
+            }
+
+            if (strlen(setting_value->value.s)) {
+                g_string_append_printf(style_sheet, style_string, setting_value->value.s);
+            }
+        }
+    }
+
+    /* Feed style sheet document to gtk */
+    gtk_css_provider_load_from_data(vb.style_provider, style_sheet->str, -1, NULL);
+
+cleanup:
+    g_string_free(style_sheet, TRUE);
 }
 
 /**
index a4c7d7f..9b93c6a 100644 (file)
@@ -275,5 +275,6 @@ void vb_quit(Client *c, gboolean force);
 void vb_register_add(Client *c, char buf, const char *value);
 const char *vb_register_get(Client *c, char buf);
 void vb_statusbar_update(Client *c);
+void vb_gui_style_update(Client *c, const char *name, const char *value);
 
 #endif /* end of include guard: _MAIN_H */
index d8b0de0..6ebb350 100644 (file)
@@ -51,6 +51,7 @@ static void setting_free(Setting *s);
 static int cookie_accept(Client *c, const char *name, DataType type, void *value, void *data);
 static int default_zoom(Client *c, const char *name, DataType type, void *value, void *data);
 static int fullscreen(Client *c, const char *name, DataType type, void *value, void *data);
+static int gui_style(Client *c, const char *name, DataType type, void *value, void *data);
 static int input_autohide(Client *c, const char *name, DataType type, void *value, void *data);
 static int internal(Client *c, const char *name, DataType type, void *value, void *data);
 static int headers(Client *c, const char *name, DataType type, void *value, void *data);
@@ -142,6 +143,40 @@ void setting_init(Client *c)
     setting_add(c, "default-zoom", TYPE_INTEGER, &i, default_zoom, 0, NULL);
     setting_add(c, "download-path", TYPE_CHAR, &"~", NULL, 0, NULL);
 
+#ifdef FEATURE_GUI_STYLE_VIMB2_COMPAT
+    /* gui style settings vimb2 compatibility */
+    setting_add(c, "completion-bg-active", TYPE_CHAR, &"#888", gui_style, 0, NULL);
+    setting_add(c, "completion-bg-normal", TYPE_CHAR, &"#656565", gui_style, 0, NULL);
+    setting_add(c, "completion-fg-active", TYPE_CHAR, &"#f6f3e8", gui_style, 0, NULL);
+    setting_add(c, "completion-fg-normal", TYPE_CHAR, &"#fff", gui_style, 0, NULL);
+    setting_add(c, "completion-font", TYPE_CHAR, &"monospace 10", gui_style, 0, NULL);
+    setting_add(c, "input-bg-error", TYPE_CHAR, &"#f77", gui_style, 0, NULL);
+    setting_add(c, "input-bg-normal", TYPE_CHAR, &"#fff", gui_style, 0, NULL);
+    setting_add(c, "input-fg-error", TYPE_CHAR, &"#000", gui_style, 0, NULL);
+    setting_add(c, "input-fg-normal", TYPE_CHAR, &"#000", gui_style, 0, NULL);
+    setting_add(c, "input-font-error", TYPE_CHAR, &"monospace bold 10", gui_style, 0, NULL);
+    setting_add(c, "input-font-normal", TYPE_CHAR, &"monospace 10", gui_style, 0, NULL);
+    setting_add(c, "status-color-bg", TYPE_CHAR, &"#000", gui_style, 0, NULL);
+    setting_add(c, "status-color-fg", TYPE_CHAR, &"#fff", gui_style, 0, NULL);
+    setting_add(c, "status-font", TYPE_CHAR, &"monospace bold 10", gui_style, 0, NULL);
+    setting_add(c, "status-ssl-color-bg", TYPE_CHAR, &"#95e454", gui_style, 0, NULL);
+    setting_add(c, "status-ssl-color-fg", TYPE_CHAR, &"#000", gui_style, 0, NULL);
+    setting_add(c, "status-ssl-font", TYPE_CHAR, &"", gui_style, 0, NULL);
+    setting_add(c, "status-sslinvalid-color-bg", TYPE_CHAR, &"#f77", gui_style, 0, NULL);
+    setting_add(c, "status-sslinvalid-color-fg", TYPE_CHAR, &"#000", gui_style, 0, NULL);
+    setting_add(c, "status-sslinvalid-font", TYPE_CHAR, &"", gui_style, 0, NULL);
+#else
+    /* gui style settings vimb3 */
+    setting_add(c, "completion-css", TYPE_CHAR, &"color:#fff;background-color:#656565;font:monospace 10;", gui_style, 0, NULL);
+    setting_add(c, "completion-hover-css", TYPE_CHAR, &"background-color:#777;", gui_style, 0, NULL);
+    setting_add(c, "completion-selected-css", TYPE_CHAR, &"color:#f6f3e8;background-color:#888;", gui_style, 0, NULL);
+    setting_add(c, "input-css", TYPE_CHAR, &"background-color:#fff;color:#000;font:monospace 10;", gui_style, 0, NULL);
+    setting_add(c, "input-error-css", TYPE_CHAR, &"background-color:#f77;font-weight:bold;", gui_style, 0, NULL);
+    setting_add(c, "status-css", TYPE_CHAR, &"color:#fff;background-color:#000;font:monospace bold 10;", gui_style, 0, NULL);
+    setting_add(c, "status-ssl-css", TYPE_CHAR, &"background-color:#95e454;color:#000;", gui_style, 0, NULL);
+    setting_add(c, "status-sslinvalid-css", TYPE_CHAR, &"background-color:#f77;color:#000;", gui_style, 0, NULL);
+#endif /* FEATURE_GUI_STYLE_VIMB2_COMPAT */
+
     /* initialize the shortcuts and set the default shortcuts */
     shortcut_init(c);
     shortcut_add(c, "dl", "https://duckduckgo.com/html/?q=$0");
@@ -613,6 +648,13 @@ static int statusbar(Client *c, const char *name, DataType type, void *value, vo
     return CMD_SUCCESS;
 }
 
+static int gui_style(Client *c, const char *name, DataType type, void *value, void *data)
+{
+    vb_gui_style_update(c, name, (const char*)value);
+
+    return CMD_SUCCESS;
+}
+
 static int tls_policy(Client *c, const char *name, DataType type, void *value, void *data)
 {
     gboolean strict = *((gboolean*)value);