From a706d1eab45506c099bd2b93d301969754ccebf9 Mon Sep 17 00:00:00 2001
From: Philip Chimento <philip.chimento@gmail.com>
Date: Sat, 12 Jul 2008 23:43:30 +0000
Subject: [PATCH] Een heleboel, relatief nutteloze dingen, zoals blank windows,
 en filerefs die nog niets doen. Ik heb de volgende functies geimplementeerd.
 De unicode functies heb ik nog niet getest.

glk_gestalt()
glk_gestalt_ext()
glk_window_iterate()
glk_window_get_rock()
glk_stream_iterate()
glk_stream_get_rock()
glk_fileref_iterate()
glk_fileref_get_rock()
glk_char_to_lower()
glk_char_to_upper()
glk_buffer_to_lower_case_uni()
glk_buffer_to_upper_case_uni()
glk_buffer_to_title_case_uni()



git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@3 ddfedd41-794f-dd11-ae45-00112f111e67
---
 src/Makefile |   9 +++++
 src/glk.h    |   8 ++--
 src/model.c  |  37 +++---------------
 src/stream.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++-----
 src/window.c |  76 +++++++++++++++++++++++++++++++++++-
 src/window.h |   1 +
 6 files changed, 193 insertions(+), 45 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index f9ea8a6..1413a5f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -5,6 +5,9 @@ SOURCES = main.c \
 		  stream.c stream.h \
 		  window.c window.h \
 		  glk.c glk.h \
+		  gestalt.c \
+		  fileref.c fileref.h \
+		  case.c \
 		  model.c
 OBJECTS = main.o \
 		  callbacks.o \
@@ -12,6 +15,9 @@ OBJECTS = main.o \
 		  stream.o \
 		  window.o \
 		  glk.o \
+		  gestalt.o \
+		  fileref.o \
+		  case.o \
 		  model.o
 
 CFLAGS = -g -Wall -O0 -export-dynamic `pkg-config --cflags ${PKG_CONFIG}`
@@ -24,3 +30,6 @@ gargoyle-gtk: ${OBJECTS}
 
 gargoyle-gtk.ui: gargoyle-gtk.glade
 	gtk-builder-convert gargoyle-gtk.glade gargoyle-gtk.ui
+	
+clean:
+	${RM} -f ${OBJECTS} gargoyle-gtk gargoyle-gtk.ui
diff --git a/src/glk.h b/src/glk.h
index 4daa971..9fc7206 100644
--- a/src/glk.h
+++ b/src/glk.h
@@ -23,10 +23,10 @@ typedef gint32 glsi32;
 
 /* These are the compile-time conditionals that reveal various Glk optional
     modules. */
-/* #define GLK_MODULE_UNICODE */
-/* #define GLK_MODULE_IMAGE */
-/* #define GLK_MODULE_SOUND */
-/* #define GLK_MODULE_HYPERLINKS */
+#define GLK_MODULE_UNICODE
+#define GLK_MODULE_IMAGE
+#define GLK_MODULE_SOUND
+#define GLK_MODULE_HYPERLINKS
 
 /* These types are opaque object identifiers. They're pointers to opaque
     C structures, which are defined differently by each library. */
diff --git a/src/model.c b/src/model.c
index d3edd5c..10bafb7 100644
--- a/src/model.c
+++ b/src/model.c
@@ -1,38 +1,7 @@
 #include "glk.h"
 
-/* model.c: Model program for Glk API, version 0.5.
-    Designed by Andrew Plotkin <erkyrath@eblong.com>
-    http://www.eblong.com/zarf/glk/index.html
-    This program is in the public domain.
-*/
-
-/* This is a simple model of a text adventure which uses the Glk API.
-    It shows how to input a line of text, display results, maintain a
-    status window, write to a transcript file, and so on. */
-
-/* This is the cleanest possible form of a Glk program. It includes only
-    "glk.h", and doesn't call any functions outside Glk at all. We even
-    define our own str_eq() and str_len(), rather than relying on the
-    standard libraries. */
-
-/* We also define our own TRUE and FALSE and NULL. */
-#ifndef TRUE
-#define TRUE 1
-#endif
-#ifndef FALSE
-#define FALSE 0
-#endif
-#ifndef NULL
-#define NULL 0
-#endif
-
 static winid_t mainwin = NULL;
 
-/* Forward declarations */
-void glk_main(void);
-
-/* The glk_main() function is called by the Glk system; it's the main entry
-    point for your program. */
 void glk_main(void)
 {
     /* Open the main window. */
@@ -46,7 +15,13 @@ void glk_main(void)
     /* Set the current output stream to print to it. */
     glk_set_window(mainwin);
     
+    unsigned char buffer[255];
+    int i;
+    for(i = 0; i < 255; i++)
+    	buffer[i] = glk_char_to_upper(i + 1);
+    
     glk_put_string("Philip en Marijn zijn vet goed.\n");
+    glk_put_string(buffer);
 
 	/* Bye bye */
 	glk_exit();
diff --git a/src/stream.c b/src/stream.c
index e32fb6f..020443b 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -1,10 +1,59 @@
 #include "stream.h"
+#include <string.h>
 
 /* Global current stream */
 static strid_t current_stream = NULL;
 /* List of streams currently in existence */
 static GList *stream_list = NULL;
 
+/**
+ * glk_stream_iterate:
+ * @str: A stream, or #NULL.
+ * @rockptr: Return location for the next window's rock, or #NULL.
+ *
+ * Iterates over the list of streams; if @str is #NULL, it returns the first
+ * stream, otherwise the next stream after @str. If there are no more, it
+ * returns #NULL. The stream's rock is stored in @rockptr. If you don't want
+ * the rocks to be returned, you may set @rockptr to #NULL.
+ *
+ * The order in which streams are returned is arbitrary. The order may change
+ * every time you create or destroy a stream, invalidating the iteration.
+ *
+ * Returns: the next stream, or #NULL if there are no more.
+ */
+strid_t
+glk_stream_iterate(strid_t str, glui32 *rockptr)
+{
+	GList *retnode;
+	
+	if(str == NULL)
+		retnode = stream_list;
+	else
+		retnode = str->stream_list->next;
+	strid_t retval = retnode? (strid_t)retnode->data : NULL;
+		
+	/* Store the stream's rock in rockptr */
+	if(retval && rockptr)
+		*rockptr = glk_stream_get_rock(retval);
+		
+	return retval;
+}
+
+/**
+ * glk_stream_get_rock:
+ * @str: A stream.
+ * 
+ * Returns the stream @str's rock value.
+ *
+ * Returns: A rock value.
+ */
+glui32
+glk_stream_get_rock(strid_t str)
+{
+	g_return_val_if_fail(str != NULL, 0);
+	return str->rock;
+}
+
 /* Internal function: create a window stream to go with window. */
 strid_t
 window_stream_new(winid_t window)
@@ -40,6 +89,21 @@ glk_stream_set_current(strid_t str)
 	current_stream = str;
 }
 
+/* Internal function: change illegal (control) characters in a string to a
+placeholder character. Must free returned string afterwards. */
+static gchar *
+remove_latin1_control_characters(gchar *s)
+{
+	gchar *retval = g_strdup(s);
+	unsigned char *ptr;
+	for(ptr = (unsigned char *)retval; *ptr != '\0'; ptr++)
+		if( (*ptr < 32 && *ptr != 10) || (*ptr >= 127 && *ptr <= 159) )
+			*ptr = '?';
+			/* Our placeholder character is '?'; other options are possible,
+			like printing "0x7F" or something */
+	return retval;
+}
+
 /**
  * glk_put_string:
  * @s: A null-terminated string in Latin-1 encoding.
@@ -49,14 +113,20 @@ glk_stream_set_current(strid_t str)
 void
 glk_put_string(char *s)
 {
+	/* Illegal to print to the current stream if it is NULL */
+	g_return_if_fail(current_stream != NULL);
+	
 	GError *error = NULL;
-	gchar *utf8;
+	gchar *canonical, *utf8;
 
 	switch(current_stream->stream_type)
 	{
 		case STREAM_TYPE_WINDOW:
-			utf8 = g_convert(s, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
-
+			canonical = remove_latin1_control_characters(s);
+			utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, 
+			                 &error);
+			g_free(canonical);
+			
 			if(utf8 == NULL)
 			{
 				g_warning("glk_put_string: "
@@ -66,14 +136,33 @@ glk_put_string(char *s)
 				return;
 			}
 
-			GtkTextBuffer *buffer = gtk_text_view_get_buffer( 
-				GTK_TEXT_VIEW(current_stream->window->widget) );
-
-			GtkTextIter iter;
-			gtk_text_buffer_get_end_iter(buffer, &iter);
+			/* Each window type has a different way of printing to it */
+			switch(current_stream->window->window_type)
+			{
+				/* Printing to a these windows' streams does nothing */
+				case wintype_Blank:
+				case wintype_Pair:
+				case wintype_Graphics:
+					current_stream->write_count += strlen(s);
+					break;
+				/* Text buffer window */	
+				case wintype_TextBuffer:
+				{
+					GtkTextBuffer *buffer = gtk_text_view_get_buffer( 
+						GTK_TEXT_VIEW(current_stream->window->widget) );
 
-			gtk_text_buffer_insert(buffer, &iter, utf8, -1);
+					GtkTextIter iter;
+					gtk_text_buffer_get_end_iter(buffer, &iter);
 
+					gtk_text_buffer_insert(buffer, &iter, utf8, -1);
+				}
+					current_stream->write_count += strlen(s);
+					break;
+				default:
+					g_warning("glk_put_string: "
+						"Writing to this kind of window unsupported.");
+			}
+			
 			g_free(utf8);
 			break;
 		default:
diff --git a/src/window.c b/src/window.c
index cfa066c..be7c20d 100644
--- a/src/window.c
+++ b/src/window.c
@@ -3,6 +3,68 @@
 /* Global tree of all windows */
 static GNode *root_window = NULL;
 
+/**
+ * glk_window_iterate:
+ * @win: A window, or #NULL.
+ * @rockptr: Return location for the next window's rock, or #NULL.
+ *
+ * Iterates over the list of windows; if @win is #NULL, it returns the first
+ * window, otherwise the next window after @win. If there are no more, it
+ * returns #NULL. The window's rock is stored in @rockptr. If you don't want
+ * the rocks to be returned, you may set @rockptr to #NULL.
+ *
+ * The order in which windows are returned is arbitrary. The root window is
+ * not necessarily first, nor is it necessarily last. The order may change
+ * every time you create or destroy a window, invalidating the iteration.
+ *
+ * Returns: the next window, or #NULL if there are no more.
+ */
+winid_t
+glk_window_iterate(winid_t win, glui32 *rockptr)
+{
+	GNode *retnode;
+	
+	if(win == NULL)
+		retnode = root_window;
+	else
+	{
+		GNode *node = win->window_node;
+		if( G_NODE_IS_LEAF(node) )
+		{
+			while(node && node->next == NULL)
+				node = node->parent;
+			if(node)
+				retnode = node->next;
+			else
+				retnode = NULL;
+		}
+		else
+			retnode = g_node_first_child(node);
+	}
+	winid_t retval = retnode? (winid_t)retnode->data : NULL;
+		
+	/* Store the window's rock in rockptr */
+	if(retval && rockptr)
+		*rockptr = glk_window_get_rock(retval);
+		
+	return retval;
+}
+
+/**
+ * glk_window_get_rock:
+ * @win: A window.
+ * 
+ * Returns the window @win's rock value. Pair windows always have rock 0.
+ *
+ * Returns: A rock value.
+ */
+glui32
+glk_window_get_rock(winid_t win)
+{
+	g_return_val_if_fail(win != NULL, 0);
+	return win->rock;
+}
+
 /**
  * glk_window_open:
  * @split: The window to split to create the new window. Must be 0 if there
@@ -59,9 +121,21 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
 
 	switch(wintype)
 	{
+		case wintype_Blank:
+		{
+			/* A blank window will be a label without any text */
+			GtkWidget *window = gtk_label_new("");
+			gtk_box_pack_end(vbox, window, TRUE, TRUE, 0);
+			gtk_widget_show(window);
+			
+			new_window->widget = window;
+			/* You can print to a blank window's stream, but it does nothing */
+			new_window->window_stream = window_stream_new(new_window);
+			new_window->echo_stream = NULL;
+		}
+			break;	
 		case wintype_TextBuffer:
 		{
-			/* We need to put these declarations inside their own scope */
 			GtkWidget *scroll_window = gtk_scrolled_window_new(NULL, NULL);
 			GtkWidget *window = gtk_text_view_new();
 			gtk_container_add( GTK_CONTAINER(scroll_window), window );
diff --git a/src/window.h b/src/window.h
index 02afe78..954415e 100644
--- a/src/window.h
+++ b/src/window.h
@@ -31,6 +31,7 @@ struct glk_window_struct
 	gchar *line_input_buffer;
 	glui32 *line_input_buffer_unicode;
 	glui32 line_input_buffer_max_len;
+	gboolean mouse_input_requested;
 };
 
 #endif
-- 
2.30.2