+/**
+ * garglk_set_zcolors_stream:
+ * @str: a stream.
+ * @fg: a 24-bit foreground color.
+ * @bg: a 24-bit background color.
+ *
+ * This function changes the foreground color of @str to @fg and the background
+ * color to @bg. @fg and @bg are encoded the same way as described in
+ * %stylehint_TextColor. See garglk_set_zcolors() for more information.
+ */
+void
+garglk_set_zcolors_stream(strid_t str, glui32 fg, glui32 bg)
+{
+#ifdef DEBUG_STYLES
+ g_printf("garglk_set_zcolors_stream(str->rock=%d, fg=%08X, bg=%08X)\n", str->rock, fg, bg);
+#endif
+
+ VALID_STREAM(str, return);
+ g_return_if_fail(str->window != NULL);
+
+ winid_t window = str->window;
+
+ gdk_threads_enter();
+
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(window->widget) );
+ GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+ GdkColor fore, 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 = g_strdup("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 = gdkcolor_to_hex(current_color);
+
+ // 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 = g_strdup("default");
+ }
+ break;
+ }
+ default:
+ glkcolor_to_gdkcolor(fg, &fore);
+ fore_pointer = &fore;
+ fore_name = glkcolor_to_hex(fg);
+ }
+
+ switch(bg) {
+ case zcolor_Transparent:
+ case zcolor_Cursor:
+ WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
+ // Fallthrough to default
+ case zcolor_Default:
+ back_name = g_strdup("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 = gdkcolor_to_hex(current_color);
+
+ // 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 = g_strdup("default");
+ }
+ break;
+ }
+ default:
+ glkcolor_to_gdkcolor(bg, &back);
+ back_pointer = &back;
+ back_name = glkcolor_to_hex(bg);
+ }
+
+ if(fore_pointer == NULL && back_pointer == NULL) {
+ // NULL value means to ignore the zcolor property altogether
+ window->zcolor = NULL;
+ } else {
+ char *name = g_strdup_printf("zcolor:#%s/#%s", fore_name, back_name);
+ g_free(fore_name);
+ g_free(back_name);
+
+ // 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_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;
+
+ // Update the reversed version if necessary
+ if(str->window->zcolor_reversed) {
+ gint reversed = GPOINTER_TO_INT( g_object_get_data( G_OBJECT(str->window->zcolor_reversed), "reverse-color" ) );
+
+ gdk_threads_leave();
+ garglk_set_reversevideo_stream(str, reversed != 0);
+ gdk_threads_enter();
+ }
+
+ }
+
+ gdk_threads_leave();
+}
+