Fixes for garglk_set_zcolors()
authorMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Sat, 29 Jan 2011 22:47:05 +0000 (23:47 +0100)
committerMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Sat, 29 Jan 2011 22:47:05 +0000 (23:47 +0100)
libchimara/garglk.c
libchimara/strio.c
libchimara/style.c

index 6e472f0a256d18f79a11196baaa796f8e1278b53..aacd1d54a72b9d0dcc18278fde5738f1171b4987 100644 (file)
@@ -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", &current_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", &current_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;
        }
 }
index bc645b9a6c6bfb926d08dbffaa276f10cbbadc11..125c3c82b0d23c49445f278c284e209bb55f0c2f 100644 (file)
@@ -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);
index 557f3dcaa5170f3b467c034d87f45e6b7cdc0502..5bbbb5be22ec3385da336297a6a9cfe21a12e2cf 100644 (file)
@@ -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;