From daf0bacb2f258c3f77a5f8567b0713cb9635fdbb Mon Sep 17 00:00:00 2001 From: Marijn van Vliet Date: Sat, 29 Jan 2011 23:47:05 +0100 Subject: [PATCH] Fixes for garglk_set_zcolors() --- libchimara/garglk.c | 107 +++++++++++++++++++++++++++++++++++--------- libchimara/strio.c | 13 +++--- libchimara/style.c | 8 ++-- 3 files changed, 96 insertions(+), 32 deletions(-) diff --git a/libchimara/garglk.c b/libchimara/garglk.c index 6e472f0..aacd1d5 100644 --- a/libchimara/garglk.c +++ b/libchimara/garglk.c @@ -167,44 +167,111 @@ garglk_set_zcolors_stream(strid_t str, glui32 fg, glui32 bg) VALID_STREAM(str, return); g_return_if_fail(str->window != NULL); - if (fg == zcolor_Transparent && fg == zcolor_Cursor) { - WARNING(_("zcolor_Transparent and zcolor_Cursor not implemented")); - return; - } - winid_t window = str->window; GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(window->widget) ); GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer); GdkColor fore, back; - glkcolor_to_gdkcolor(fg, &fore); - glkcolor_to_gdkcolor(bg, &back); + GdkColor *fore_pointer = NULL; + GdkColor *back_pointer = NULL; + gchar *fore_name; + gchar *back_name; + + switch(fg) { + case zcolor_Transparent: + case zcolor_Cursor: + WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented")); + // Fallthrough to default + case zcolor_Default: + fore_name = "default"; + break; + case zcolor_Current: + { + if(window->zcolor) { + // Get the current foreground color + GdkColor *current_color; + g_object_get(window->zcolor, "foreground-gdk", ¤t_color, NULL); + fore_name = g_strdup_printf("%02X%02X%02X", current_color->red, current_color->green, current_color->blue); + + // Copy the color and use it + fore.red = current_color->red; + fore.green = current_color->green; + fore.blue = current_color->blue; + fore_pointer = &fore; + } else { + fore_name = "default"; + } + break; + } + default: + glkcolor_to_gdkcolor(fg, &fore); + fore_pointer = &fore; + fore_name = g_strdup_printf("%02X%02X%02X", + ((fg & 0xff0000) >> 16), + ((fg & 0x00ff00) >> 8), + (fg & 0x0000ff) + ); + } - char *name = g_strdup_printf("zcolor:#%02X%02X%02X/#%02X%02X%02x", - ((fg & 0xff0000) >> 16), - ((fg & 0x00ff00) >> 8), - (fg & 0x0000ff), - ((bg & 0xff0000) >> 16), - ((bg & 0x00ff00) >> 8), - (bg & 0x0000ff) - ); + switch(bg) { + case zcolor_Transparent: + case zcolor_Cursor: + WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented")); + // Fallthrough to default + case zcolor_Default: + back_name = "default"; + break; + case zcolor_Current: + { + if(window->zcolor) { + // Get the current background color + GdkColor *current_color; + g_object_get(window->zcolor, "background-gdk", ¤t_color, NULL); + back_name = g_strdup_printf("%02X%02X%02X", current_color->red, current_color->green, current_color->blue); - if(fg == zcolor_Default) { + // Copy the color and use it + back.red = current_color->red; + back.green = current_color->green; + back.blue = current_color->blue; + back_pointer = &back; + } else { + back_name = "default"; + } + break; + } + default: + glkcolor_to_gdkcolor(bg, &back); + back_pointer = &back; + back_name = g_strdup_printf("%02X%02X%02X", + ((bg & 0xff0000) >> 16), + ((bg & 0x00ff00) >> 8), + (bg & 0x0000ff) + ); + } + + char *name = g_strdup_printf("zcolor:#%s/#%s", fore_name, back_name); + + if(fore_pointer == NULL && back_pointer == NULL) { + // NULL value means to ignore the zcolor property altogether window->zcolor = NULL; } else { + // See if we have used this color combination before GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name); + if(tag == NULL) { + // Create a new texttag with the specified colors tag = gtk_text_buffer_create_tag( buffer, name, - "foreground-gdk", &fore, - "foreground-set", TRUE, - "background-gdk", &back, - "background-set", TRUE, + "foreground-gdk", fore_pointer, + "foreground-set", fore_pointer != NULL, + "background-gdk", back_pointer, + "background-set", back_pointer != NULL, NULL ); } + // From now on, text will be drawn in the specified colors window->zcolor = tag; } } diff --git a/libchimara/strio.c b/libchimara/strio.c index bc645b9..125c3c8 100644 --- a/libchimara/strio.c +++ b/libchimara/strio.c @@ -76,8 +76,10 @@ flush_window_buffer(winid_t win) } // GLK Program's style overrides using garglk_set_zcolors() - if(win->zcolor != NULL) + if(win->zcolor != NULL) { gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end); + } + ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK)); g_assert(glk); @@ -107,9 +109,9 @@ flush_window_buffer(winid_t win) GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style); GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink"); - while(chars_left > available_space && !gtk_text_iter_is_end(&start)) + while(chars_left > available_space && !gtk_text_iter_is_end(&insert)) { - GtkTextIter end = start; + GtkTextIter end = insert; gtk_text_iter_forward_to_line_end(&end); gtk_text_buffer_delete(buffer, &insert, &end); @@ -134,11 +136,8 @@ flush_window_buffer(winid_t win) } // GLK Program's style overrides using garglk_set_zcolors() - /* if(win->zcolor != NULL) gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert); - */ - chars_left -= available_space; gtk_text_iter_forward_line(&insert); @@ -171,10 +170,8 @@ flush_window_buffer(winid_t win) } // GLK Program's style overrides using garglk_set_zcolors() - /* if(win->zcolor != NULL) gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert); - */ } gtk_text_buffer_move_mark(buffer, cursor, &start); diff --git a/libchimara/style.c b/libchimara/style.c index 557f3dc..5bbbb5b 100644 --- a/libchimara/style.c +++ b/libchimara/style.c @@ -760,9 +760,9 @@ apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype, glkcolor_to_gdkcolor(val, &color); if(!reverse_color) - g_object_set(tag_object, "foreground", &color, "foreground-set", TRUE, NULL); + g_object_set(tag_object, "foreground-gdk", &color, "foreground-set", TRUE, NULL); else - g_object_set(tag_object, "background", &color, "background-set", TRUE, NULL); + g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL); break; @@ -770,9 +770,9 @@ apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype, glkcolor_to_gdkcolor(val, &color); if(!reverse_color) - g_object_set(tag_object, "background", &color, "background-set", TRUE, NULL); + g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL); else - g_object_set(tag_object, "foreground", &color, "background-set", TRUE, NULL); + g_object_set(tag_object, "foreground-gdk", &color, "background-set", TRUE, NULL); break; -- 2.30.2