Finished implementing garglk_set_zcolors()
authorMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Sat, 29 Jan 2011 17:36:08 +0000 (18:36 +0100)
committerMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Sat, 29 Jan 2011 17:36:08 +0000 (18:36 +0100)
libchimara/garglk.c
libchimara/strio.c
libchimara/style.c
tests/styletest.c

index 02f032c38de3da4f0b6eb247dad308216f817e87..6e472f0a256d18f79a11196baaa796f8e1278b53 100644 (file)
@@ -4,6 +4,7 @@
 #include "stream.h"
 #include "fileref.h"
 #include "style.h"
+#include "garglk.h"
 
 extern GPrivate *glk_data_key;
 
@@ -164,7 +165,48 @@ void
 garglk_set_zcolors_stream(strid_t str, glui32 fg, glui32 bg)
 {
        VALID_STREAM(str, return);
-       WARNING(_("Not implemented"));
+       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);
+
+       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)
+       );
+
+       if(fg == zcolor_Default) {
+               window->zcolor = NULL;
+       } else {
+               GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
+               if(tag == NULL) {
+                       tag = gtk_text_buffer_create_tag(
+                               buffer,
+                               name,
+                               "foreground-gdk", &fore,
+                               "foreground-set", TRUE,
+                               "background-gdk", &back,
+                               "background-set", TRUE,
+                               NULL
+                       );
+               }
+
+               window->zcolor = tag;
+       }
 }
 
 /**
@@ -185,29 +227,8 @@ garglk_set_zcolors(glui32 fg, glui32 bg)
 {
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
        g_return_if_fail(glk_data->current_stream != NULL);
-       g_return_if_fail(glk_data->current_stream->window != NULL);
-
-       winid_t window = glk_data->current_stream->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);
-
-       gchar *id = g_strdup_printf("%d", ++window->last_zcolor_id);
-       printf("id = %s\nfg = %08X\nbg = %08X\n\n", id, fg, bg);
-       
-       GtkTextTag *tag = gtk_text_buffer_create_tag(
-               buffer,
-               g_strdup_printf("%d", ++window->last_zcolor_id),
-               "foreground-gdk", &fore,
-               "foreground-set", TRUE,
-               "background-gdk", &back,
-               "background-set", TRUE,
-               NULL
-       );
 
-       window->zcolor = tag;
+       garglk_set_zcolors_stream(glk_data->current_stream, fg, bg);
 }
 
 static void
index 6d54e825c50bcbd1ccf6c7fb9361a1495c6af7c8..bc645b9a6c6bfb926d08dbffaa276f10cbbadc11 100644 (file)
@@ -45,7 +45,8 @@ flush_window_buffer(winid_t win)
        case wintype_TextBuffer:
        {
                GtkTextIter start, end;
-               gtk_text_buffer_get_end_iter(buffer, &start);
+               gtk_text_buffer_get_end_iter(buffer, &end);
+               gint start_offset;
 
                GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
 
@@ -53,8 +54,9 @@ flush_window_buffer(winid_t win)
                GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
                GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
 
-               gtk_text_buffer_insert(buffer, &start, win->buffer->str, -1);
-               gtk_text_buffer_get_end_iter(buffer, &end);
+               start_offset = gtk_text_iter_get_offset(&end);
+               gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
+               gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
 
                // Default style
                gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
@@ -92,10 +94,12 @@ flush_window_buffer(winid_t win)
                GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
                
                /* Get cursor position */
-               GtkTextIter start;
-               gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
+               GtkTextIter start, insert;
+               gint start_offset;
+
+               gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
                /* Spaces available on this line */
-               gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
+               gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
                
                GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
                GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
@@ -107,52 +111,70 @@ flush_window_buffer(winid_t win)
                {
                        GtkTextIter end = start;
                        gtk_text_iter_forward_to_line_end(&end);
-                       gtk_text_buffer_delete(buffer, &start, &end);
+                       gtk_text_buffer_delete(buffer, &insert, &end);
 
-                       gtk_text_buffer_insert(buffer, &start, win->buffer->str + (length - chars_left), available_space);
+                       start_offset = gtk_text_iter_get_offset(&insert);
+                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
+                       gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
 
                        // Default style
-                       gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
+                       gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
 
                        // Player's style overrides
-                       gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
+                       gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
 
                        // GLK Program's style overrides
-                       gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
+                       gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
 
                        // Link style overrides
                        if(win->window_stream->hyperlink_mode) {
                                GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
-                               gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
+                               gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
+                               gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
                        }
 
                        // GLK Program's style overrides using garglk_set_zcolors()
+                       /*
                        if(win->zcolor != NULL)
-                               gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
+                               gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
+                               */
+
 
                        chars_left -= available_space;
-                       gtk_text_iter_forward_line(&start);
+                       gtk_text_iter_forward_line(&insert);
                        available_space = win->width;
                }
-               if(!gtk_text_iter_is_end(&start))
+               if(!gtk_text_iter_is_end(&insert))
                {
-                       GtkTextIter end = start;
+                       GtkTextIter end = insert;
                        gtk_text_iter_forward_chars(&end, chars_left);
-                       gtk_text_buffer_delete(buffer, &start, &end);
+                       gtk_text_buffer_delete(buffer, &insert, &end);
+
+                       start_offset = gtk_text_iter_get_offset(&insert);
+                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
+                       gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
 
-                       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
-                       GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
-                       GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-                       GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
+                       // Default style
+                       gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
 
+                       // Player's style overrides
+                       gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
+
+                       // GLK Program's style overrides
+                       gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
+
+                       // Link style overrides
                        if(win->window_stream->hyperlink_mode) {
-                               GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
                                GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
-                       } else {
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, NULL);
+                               gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
+                               gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
                        }
+
+                       // 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 48883a94a0ba4d3e6d55c707a897f5181b308b19..557f3dcaa5170f3b467c034d87f45e6b7cdc0502 100644 (file)
@@ -657,19 +657,6 @@ style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
        return TRUE;
 }
 
-/* Internal function: parses a glk color to a #hex-value */
-static void
-glkcolor_to_hex(glui32 val, gchar *buffer)
-{
-       g_return_if_fail(buffer != NULL);
-
-       sprintf(buffer, "#%02X%02X%02X",
-               ((val & 0xff0000) >> 16),
-               ((val & 0x00ff00) >> 8),
-               (val & 0x0000ff)
-       );
-}
-
 /* Internal function: parses a glk color to a GdkColor */
 void
 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
@@ -699,7 +686,7 @@ apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype,
        gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
 
        int i = 0;
-       gchar color[20];
+       GdkColor color;
        switch(hint) {
        case stylehint_Indentation:
                g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
@@ -770,22 +757,22 @@ apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype,
                break;
 
        case stylehint_TextColor:
-               glkcolor_to_hex(val, color);
+               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", &color, "foreground-set", TRUE, NULL);
                else
-                       g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
+                       g_object_set(tag_object, "background", &color, "background-set", TRUE, NULL);
 
                break;
 
        case stylehint_BackColor:
-               glkcolor_to_hex(val, color);
+               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", &color, "background-set", TRUE, NULL);
                else
-                       g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
+                       g_object_set(tag_object, "foreground", &color, "background-set", TRUE, NULL);
 
                break;
 
index 8ed77372268c74d3662adc8a5222636a2942a70c..4e9a503f294660c9b5caa2cbcb1a738e274a3555 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <libchimara/glk.h>
+#include <libchimara/garglk.h>
 
 void print_help();
 void do_style_test();
@@ -111,7 +112,12 @@ do_style_test() {
        glk_set_style(style_User2);
        glk_put_string("User2\n");
 
+       glk_set_style(style_Header);
+       garglk_set_zcolors(0xFF0000, 0x00FFFF);
+       glk_put_string("Zcolor test\n");
+
        glk_set_style(style_Normal);
+       garglk_set_zcolors(0x000000, 0xFFFFFF);
 }
 
 void