Commit 5417efcf authored by Sebastian Ramacher's avatar Sebastian Ramacher

Use GiraraTemplate for CSS

Signed-off-by: default avatarSebastian Ramacher <sebastian@ramacher.at>
parent d2136fb8
/* No resize grip on GtkWindows */
#@SESSION@ GtkWindow {
#@session@ GtkWindow {
-GtkWindow-resize-grip-height: 0;
-GtkWindow-resize-grip-width: 0;
}
/* Hide scrollbars from GtkScrolledWindow */
#@SESSION@ GtkScrolledWindow GtkScrollbar:insensitive {
#@session@ GtkScrolledWindow GtkScrollbar:insensitive {
-GtkRange-slider-width: 0;
-GtkRange-trough-border: 0;
}
#@SESSION@ * {
color: @DEFAULT-FG@;
background-color: @DEFAULT-BG@;
#@session@ * {
color: @default-fg@;
background-color: @default-bg@;
}
/* Inputbar */
#@SESSION@ GtkEntry.inputbar {
background-color: @INPUTBAR-BG@;
#@session@ GtkEntry.inputbar {
background-color: @inputbar-bg@;
}
#@SESSION@ .inputbar {
color: @INPUTBAR-FG@;
background-color: @INPUTBAR-BG@;
#@session@ .inputbar {
color: @inputbar-fg@;
background-color: @inputbar-bg@;
}
/* Completion entries and groups */
#@SESSION@ .completion, .completion-box {
color: @COMPLETION-FG@;
background-color: @COMPLETION-BG@;
#@session@ .completion, .completion-box {
color: @completion-fg@;
background-color: @completion-bg@;
}
#@SESSION@ .completion {
#@session@ .completion {
padding: 1px;
}
#@SESSION@ .completion:selected {
color: @COMPLETION-HIGHLIGHT-FG@;
background-color: @COMPLETION-HIGHLIGHT-BG@;
#@session@ .completion:selected {
color: @completion-highlight-fg@;
background-color: @completion-highlight-bg@;
}
#@SESSION@ .completion-group {
color: @COMPLETION-GROUP-FG@;
background-color: @COMPLETION-GROUP-BG@;
#@session@ .completion-group {
color: @completion-group-fg@;
background-color: @completion-group-bg@;
padding: 2px 4px;
}
/* Statusbar */
#@SESSION@ .statusbar {
color: @STATUSBAR-FG@;
background-color: @STATUSBAR-BG@;
#@session@ .statusbar {
color: @statusbar-fg@;
background-color: @statusbar-bg@;
}
/* Tabbar */
#@SESSION@ .tab {
color: @TABBAR-FG@;
background-color: @TABBAR-BG@;
#@session@ .tab {
color: @tabbar-fg@;
background-color: @tabbar-bg@;
}
#@SESSION@ GtkLabel.tab {
#@session@ GtkLabel.tab {
padding: 4px;
}
#@SESSION@ .tab:selected {
color: @TABBAR-FOCUS-FG@;
background-color: @TABBAR-FOCUS-BG@;
#@session@ .tab:selected {
color: @tabbar-focus-fg@;
background-color: @tabbar-focus-bg@;
}
/* Notification area */
#@SESSION@ .notification {
color: @NOTIFICATION-FG@;
background-color: @NOTIFICATION-BG@;
#@session@ .notification {
color: @notification-fg@;
background-color: @notification-bg@;
}
#@SESSION@ .notification-error {
color: @NOTIFICATION-ERROR-FG@;
background-color: @NOTIFICATION-ERROR-BG@;
#@session@ .notification-error {
color: @notification-error-fg@;
background-color: @notification-error-bg@;
}
#@SESSION@ .notification-warning {
color: @NOTIFICATION-WARNING-FG@;
background-color: @NOTIFICATION-WARNING-BG@;
#@session@ .notification-warning {
color: @notification-warning-fg@;
background-color: @notification-warning-bg@;
}
/* Padding of the bottom box */
#@SESSION@ #bottom_box {
#@session@ #bottom_box {
border-style: none;
margin: 0px;
padding: @BOTTOMBOX-PADDING1@px @BOTTOMBOX-PADDING2@px @BOTTOMBOX-PADDING3@px
@BOTTOMBOX-PADDING4@px;
padding: @bottombox-padding1@px @bottombox-padding2@px @bottombox-padding3@px
@bottombox-padding4@px;
}
......@@ -46,20 +46,59 @@ ensure_gettext_initialized(void)
}
}
static void
init_template_engine(GiraraTemplate* csstemplate)
{
static const char* variable_names[] = {
"session",
"font",
"default-fg",
"default-bg",
"inputbar-fg",
"inputbar-bg",
"statusbar-fg",
"statusbar-bg",
"completion-fg",
"completion-bg",
"completion-group-fg",
"completion-group-bg",
"completion-highlight-fg",
"completion-highlight-bg",
"notification-error-fg",
"notification-error-bg",
"notification-warning-fg",
"notification-warning-bg",
"notification-fg",
"notification-bg",
"tabbar-fg",
"tabbar-bg",
"tabbar-focus-fg",
"tabbar-focus-bg",
"bottombox-padding1",
"bottombox-padding2",
"bottombox-padding3",
"bottombox-padding4"
};
for (size_t idx = 0; idx < LENGTH(variable_names); ++idx) {
girara_template_add_variable(csstemplate, variable_names[idx]);
}
}
static char*
load_css(girara_session_t* session, const char* session_name)
{
char* css_data = g_strdup(CSS_TEMPLATE);
if (css_data == NULL) {
return NULL;
}
GiraraTemplate* csstemplate = session->private_data->csstemplate;
girara_template_set_variable_value(csstemplate, "session", session_name);
/*
char* tmp = girara_replace_substring(css_data, "@SESSION@", session_name);
g_free(css_data);
if (tmp == NULL) {
return NULL;
}
css_data = tmp;
css_data = tmp; */
/* parse color values */
typedef struct color_setting_mapping_s {
......@@ -92,7 +131,7 @@ load_css(girara_session_t* session, const char* session_name)
{"tabbar-focus-bg", &(session->style.tabbar_focus_background)},
};
for (size_t i = 0; i < LENGTH(color_setting_mappings) && css_data != NULL; i++) {
for (size_t i = 0; i < LENGTH(color_setting_mappings); i++) {
char* tmp_value = NULL;
girara_setting_get(session, color_setting_mappings[i].identifier, &tmp_value);
if (tmp_value != NULL) {
......@@ -101,21 +140,9 @@ load_css(girara_session_t* session, const char* session_name)
}
char* color = gdk_rgba_to_string(color_setting_mappings[i].color);
char* identifier = g_ascii_strup(color_setting_mappings[i].identifier, -1);
char* css_identifier = g_strdup_printf("@%s@", identifier);
tmp = girara_replace_substring(css_data, css_identifier, color);
g_free(css_data);
g_free(css_identifier);
g_free(identifier);
girara_template_set_variable_value(csstemplate,
color_setting_mappings[i].identifier, color);
g_free(color);
css_data = tmp;
}
if (css_data == NULL) {
return NULL;
}
/* we want inputbar_entry the same height as notification_text and statusbar,
......@@ -134,27 +161,19 @@ load_css(girara_session_t* session, const char* session_name)
} padding_mapping_t;
const padding_mapping_t padding_mapping[] = {
{"@BOTTOMBOX-PADDING1@", g_strdup_printf("%d", ypadding - ypadding/2)},
{"@BOTTOMBOX-PADDING2@", g_strdup_printf("%d", xpadding/2)},
{"@BOTTOMBOX-PADDING3@", g_strdup_printf("%d", ypadding/2)},
{"@BOTTOMBOX-PADDING4@", g_strdup_printf("%d", xpadding - xpadding/2)},
{"bottombox-padding1", g_strdup_printf("%d", ypadding - ypadding/2)},
{"bottombox-padding2", g_strdup_printf("%d", xpadding/2)},
{"bottombox-padding3", g_strdup_printf("%d", ypadding/2)},
{"bottombox-padding4", g_strdup_printf("%d", xpadding - xpadding/2)},
};
for (size_t i = 0; i < LENGTH(padding_mapping) && css_data != NULL; ++i) {
tmp = girara_replace_substring(css_data, padding_mapping[i].identifier, padding_mapping[i].value);
g_free(css_data);
css_data = tmp;
}
for (size_t i = 0; i < LENGTH(padding_mapping); ++i) {
girara_template_set_variable_value(csstemplate,
padding_mapping[i].identifier, padding_mapping[i].value);
g_free(padding_mapping[i].value);
}
if (css_data == NULL) {
return NULL;
}
return css_data;
return girara_template_evaluate(csstemplate);
}
girara_session_t*
......@@ -188,6 +207,7 @@ girara_session_create()
/* CSS style provider */
session->private_data->csstemplate = girara_template_new(CSS_TEMPLATE);
session->private_data->gtk.cssprovider = gtk_css_provider_new();
init_template_engine(session->private_data->csstemplate);
/* init modes */
session->modes.identifiers = girara_list_new2(
......
......@@ -14,6 +14,7 @@ G_DEFINE_TYPE(GiraraTemplate, girara_template, G_TYPE_OBJECT)
typedef struct private_s {
char* base;
GRegex* variable_regex;
GRegex* variable_check_regex;
girara_list_t* variables_in_base;
girara_list_t* variables;
bool valid;
......@@ -175,12 +176,21 @@ girara_template_init(GiraraTemplate* history)
g_error_free(error);
}
private_t* priv = GET_PRIVATE(history);
priv->base = g_strdup("");
priv->variable_regex = regex;
priv->variables_in_base = girara_list_new2(g_free);
priv->variables = girara_list_new2(free_variable);
priv->valid = true;
GRegex* check_regex = g_regex_new("^[A-Za-z0-9][A-Za-z0-9_-]*$",
G_REGEX_OPTIMIZE, 0, &error);
if (check_regex == NULL) {
girara_error("Failed to create regex: %s", error->message);
g_regex_unref(regex);
g_error_free(error);
}
private_t* priv = GET_PRIVATE(history);
priv->base = g_strdup("");
priv->variable_regex = regex;
priv->variable_check_regex = check_regex;
priv->variables_in_base = girara_list_new2(g_free);
priv->variables = girara_list_new2(free_variable);
priv->valid = true;
}
/* GObject dispose */
......@@ -190,7 +200,10 @@ dispose(GObject* object)
private_t* priv = GET_PRIVATE(object);
g_regex_unref(priv->variable_regex);
g_regex_unref(priv->variable_check_regex);
priv->variable_regex = NULL;
priv->variable_check_regex = NULL;
G_OBJECT_CLASS(girara_template_parent_class)->dispose(object);
}
......@@ -348,30 +361,37 @@ girara_template_referenced_variables(GiraraTemplate* object)
return priv->variables_in_base;
}
void
bool
girara_template_add_variable(GiraraTemplate* object, const char* name)
{
g_return_if_fail(GIRARA_IS_TEMPLATE(object));
g_return_if_fail(name != NULL);
g_return_val_if_fail(GIRARA_IS_TEMPLATE(object), false);
g_return_val_if_fail(name != NULL, false);
private_t* priv = GET_PRIVATE(object);
if (g_regex_match(priv->variable_check_regex, name, 0, NULL) == FALSE) {
girara_debug("'%s' is not a valid variable name.", name);
return false;
}
variable_t* variable = girara_list_find(priv->variables, compare_variable_name,
name);
if (variable != NULL) {
girara_error("Variable '%s' already exists.", name);
return;
girara_debug("Variable '%s' already exists.", name);
return false;
}
variable = new_variable(name);
if (variable == NULL) {
girara_error("Could not create new variable.");
return;
girara_debug("Could not create new variable.");
return false;
}
girara_list_append(priv->variables, variable);
g_signal_emit(object, signals[VARIABLE_CHANGED], 0, name);
g_signal_emit(object, signals[TEMPLATE_CHANGED], 0);
return true;
}
void
......
......@@ -41,17 +41,59 @@ GType girara_template_get_type(void);
/**
* Create new template object.
*
* @param template a string that is used as template
* @param base a string that is used as template
* @returns a templot object
*/
GiraraTemplate* girara_template_new(const char* base);
/**
* Set the base string of the template.
*
* @param object GiraraTemplate object
* @param base a string that is used as template
*/
void girara_template_set_base(GiraraTemplate* object, const char* base);
/**
* Get the base string of the template.
*
* @param object GiraraTemplate object
* @returns string that is used as template
*/
const char* girara_template_get_base(GiraraTemplate* object);
/**
* Get list of variable names referenced in the template.
*
* @param object GiraraTemplate object
* @returns list of variables names referenced in the template
*/
girara_list_t* girara_template_referenced_variables(GiraraTemplate* object);
void girara_template_add_variable(GiraraTemplate* object, const char* name);
/**
* Register a variable.
*
* @param object GiraraTemplate object
* @param name name of the variable
* @returns true if the variable was added, false otherwise
*/
bool girara_template_add_variable(GiraraTemplate* object, const char* name);
/**
* Set value of a variable.
*
* @param object GiraraTemplate object
* @param name name of the variable
* @param value value of the variable
*/
void girara_template_set_variable_value(GiraraTemplate* object, const char* name, const char* value);
/**
* Replace all variables with their values in the template.
*
* @param object GiraraTemplate object
* @returns evaluated template, needes to be deallocated with g_free
*/
char* girara_template_evaluate(GiraraTemplate* object);
#endif
......@@ -72,7 +72,15 @@ START_TEST(test_variable_add) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
ck_assert(girara_template_add_variable(obj, "name"));
g_object_unref(obj);
} END_TEST
START_TEST(test_variable_add_invalid) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
ck_assert(!girara_template_add_variable(obj, "na|me"));
g_object_unref(obj);
} END_TEST
......@@ -80,7 +88,7 @@ START_TEST(test_variable_set) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
ck_assert(girara_template_add_variable(obj, "name"));
girara_template_set_variable_value(obj, "name", "value");
g_object_unref(obj);
} END_TEST
......@@ -89,7 +97,7 @@ START_TEST(test_full_1) {
GiraraTemplate* obj = girara_template_new("name = @name@");
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
ck_assert(girara_template_add_variable(obj, "name"));
girara_template_set_variable_value(obj, "name", "value");
char* result = girara_template_evaluate(obj);
......@@ -141,6 +149,7 @@ Suite* suite_template()
tcase = tcase_create("variables");
tcase_add_checked_fixture(tcase, setup, NULL);
tcase_add_test(tcase, test_variable_add);
tcase_add_test(tcase, test_variable_add_invalid);
tcase_add_test(tcase, test_variable_set);
suite_add_tcase(suite, tcase);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment