From 352f99218b90bc9a3e5386afc3f12c36c3ebec60 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 26 Dec 2008 08:25:16 +0000 Subject: [PATCH] Implemented glk_window_get_size() Fixed dependencies in the makefile git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@18 ddfedd41-794f-dd11-ae45-00112f111e67 --- src/Makefile.am | 13 ++-------- src/chimara-glk.c | 2 +- src/gridtest.c | 19 +++++++++++++-- src/window.c | 60 ++++++++++++++++++++++++++++++++++++++--------- src/window.h | 11 +++++++-- 5 files changed, 78 insertions(+), 27 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 488abd5..4da430e 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,10 +14,10 @@ AM_CFLAGS =\ -Wall\ -g -bin_PROGRAMS = test-chimara +noinst_PROGRAMS = test-chimara test_chimara_SOURCES = main.c callbacks.c callbacks.h error.c error.h -test_chimara_LDADD = $(TEST_LIBS) libchimara.la +test_chimara_LDADD = $(TEST_LIBS) -lchimara lib_LTLIBRARIES = libchimara.la @@ -40,17 +40,8 @@ libchimara_includedir = $(includedir)/chimara/chimara libchimara_include_HEADERS = chimara-glk.h pkglib_LTLIBRARIES = first.la model.la gridtest.la - -first_la_SOURCES = first.c -first_la_LIBADD = libchimara.la first_la_LDFLAGS = -module -avoid-version - -model_la_SOURCES = model.c -model_la_LIBADD = libchimara.la model_la_LDFLAGS = -module -avoid-version - -gridtest_la_SOURCES = gridtest.c -gridtest_la_LIBADD = libchimara.la gridtest_la_LDFLAGS = -module -avoid-version CLEANFILES = chimara.ui diff --git a/src/chimara-glk.c b/src/chimara-glk.c index 4484e81..3580623 100644 --- a/src/chimara-glk.c +++ b/src/chimara-glk.c @@ -130,7 +130,7 @@ chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition) /* For now, just pass the size request on to the root Glk window */ if(priv->root_window) { GtkWidget *child = ((winid_t)(priv->root_window->data))->frame; - if(GTK_WIDGET_VISIBLE(child)) + if(GTK_WIDGET_VISIBLE(child)) gtk_widget_size_request(child, requisition); } else { requisition->width = CHIMARA_GLK_MIN_WIDTH; diff --git a/src/gridtest.c b/src/gridtest.c index 1f81736..252d5d0 100644 --- a/src/gridtest.c +++ b/src/gridtest.c @@ -1,12 +1,27 @@ +#include "stdio.h" #include "glk.h" void glk_main(void) { - winid_t mainwin = glk_window_open(0, 0, 0, wintype_TextGrid, 0); + event_t ev; + winid_t mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0); if(!mainwin) return; glk_set_window(mainwin); glk_put_string("Philip en Marijn zijn vet goed.\n"); - glk_put_string("A veeeeeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing\n"); + glk_put_string("A veeeeeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing.\n"); + + guint32 width, height; + glk_window_get_size(mainwin, &width, &height); + fprintf(stderr, "\nWidth: %d\nHeight: %d\n", width, height); + + glk_request_char_event(mainwin); + while(1) { + glk_select(&ev); + if(ev.type == evtype_CharInput) { + glk_window_get_size(mainwin, &width, &height); + fprintf(stderr, "\nWidth: %d\nHeight: %d\n", width, height); + } + } } \ No newline at end of file diff --git a/src/window.c b/src/window.c index 5b6b621..1fed03d 100644 --- a/src/window.c +++ b/src/window.c @@ -195,6 +195,9 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, win->widget = label; win->frame = label; + /* A blank window has no size */ + win->unit_width = 0; + win->unit_height = 0; /* You can print to a blank window's stream, but it does nothing */ win->window_stream = window_stream_new(win); win->echo_stream = NULL; @@ -215,6 +218,12 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, win->widget = textview; win->frame = scrolledwindow; + /* Determine the size of a "0" character in pixels" */ + PangoLayout *zero = gtk_widget_create_pango_layout(textview, "0"); + pango_layout_get_pixel_size( zero, &(win->unit_width), &(win->unit_height) ); + g_object_unref(zero); + + /* Set the other parameters */ win->window_stream = window_stream_new(win); win->echo_stream = NULL; win->input_request_type = INPUT_REQUEST_NONE; @@ -457,17 +466,46 @@ glk_window_get_size(winid_t win, glui32 *widthptr, glui32 *heightptr) { g_return_if_fail(win != NULL); - /* TODO: Write this function */ - /* For a text buffer window: Return the number of rows and columns which - would be available _if_ the window was filled with "0" (zero) characters in - the "normal" font. */ - if(widthptr != NULL) { - *widthptr = 0; - } - - if(heightptr != NULL) { - *heightptr = 0; - } + switch(win->type) + { + case wintype_Blank: + if(widthptr != NULL) + *widthptr = 0; + if(heightptr != NULL) + *heightptr = 0; + break; + + case wintype_TextBuffer: + /* TODO: Glk wants to be able to get its windows' sizes as soon as they are created, but GTK doesn't decide on their sizes until they are drawn. The drawing happens somewhere in an idle function. A good method would be to make an educated guess of the window's size using the ChimaraGlk widget's size. */ + gdk_threads_enter(); + /*if(win->widget->allocation.width == 1 && win->widget->allocation.height == 1) + { + g_warning("glk_window_get_size: The Glk program requested the size of a window before it was allocated screen space by GTK. The window size is just an educated guess."); + guess the size from the parent window; + break; + } */ + + /* Instead, we wait for GTK to draw the widget. This is probably very slow and should be fixed. */ + while(win->widget->allocation.width == 1 && win->widget->allocation.height == 1) + { + /* Release the GDK lock momentarily */ + gdk_threads_leave(); + gdk_threads_enter(); + while(gtk_events_pending()) + gtk_main_iteration(); + } + + if(widthptr != NULL) + *widthptr = (glui32)(win->widget->allocation.width / win->unit_width); + if(heightptr != NULL) + *heightptr = (glui32)(win->widget->allocation.height / win->unit_height); + gdk_threads_leave(); + + break; + + default: + g_warning("glk_window_get_size: Unsupported window type"); + } } /** diff --git a/src/window.h b/src/window.h index 728a381..6f83744 100644 --- a/src/window.h +++ b/src/window.h @@ -25,8 +25,15 @@ struct glk_window_struct GNode *window_node; /* Window parameters */ glui32 type; - GtkWidget *widget; /* actual widget that does stuff */ - GtkWidget *frame; /* container child */ + /* "widget" is the actual widget with the window's functionality */ + GtkWidget *widget; + /* "frame" is the widget that is the child of the ChimaraGlk container, such + as a scroll window. It may be the same as "widget". */ + GtkWidget *frame; + /* Width and height of the window's size units, in pixels */ + int unit_width; + int unit_height; + /* Streams associated with the window */ strid_t window_stream; strid_t echo_stream; /* Input request stuff */ -- 2.30.2