implemented text layout functions (Motif)

Sun, 29 Nov 2015 21:43:03 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Nov 2015 21:43:03 +0100
changeset 97
1a786201465f
parent 96
93785a7bda56
child 98
efaae97bd95b

implemented text layout functions (Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/motif/graphics.c file | annotate | diff | comparison | revisions
ui/motif/graphics.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Nov 29 20:03:06 2015 +0100
+++ b/application/main.c	Sun Nov 29 21:43:03 2015 +0100
@@ -47,12 +47,16 @@
 void draw(UiEvent *event, UiGraphics *g, void *data) {
     int width = g->width;
     int height = g->height;
+    printf("rec[%d,%d]\n", width, height);
     ui_graphics_color(g, 64, 64, 64);
     ui_draw_rect(g, 0, 0, width, height, TRUE);
     
     UiTextLayout *text = ui_text(g);
     ui_text_setfont(text, "Monospace", 12);
     ui_text_setstring(text, "Hello World");
+    int w, h;
+    ui_text_getsize(text, &w, &h);
+    printf("ext[%d,%d]\n", w, h);
     
     ui_graphics_color(g, 255, 255, 255);
     ui_draw_text(g, 50, 50, text);
--- a/ui/motif/graphics.c	Sun Nov 29 20:03:06 2015 +0100
+++ b/ui/motif/graphics.c	Sun Nov 29 21:43:03 2015 +0100
@@ -96,6 +96,7 @@
 /* -------------------- text layout functions -------------------- */
 UiTextLayout* ui_text(UiGraphics *g) {
     UiTextLayout *text = malloc(sizeof(UiTextLayout));
+    memset(text, 0, sizeof(UiTextLayout));
     text->text = NULL;
     text->length = 0;
     text->widget = ((UiXlibGraphics*)g)->widget;
@@ -134,25 +135,34 @@
 }
 
 void ui_text_setstring(UiTextLayout *layout, char *str) {
-    layout->text = str;
-    layout->length = strlen(str);
+    ui_text_setstringl(layout, str, strlen(str));
 }
 
 void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
     layout->text = str;
     layout->length = len;
+    layout->changed = 1;
 }
 
 void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
     create_default_fontset(layout);//TODO
+    layout->changed = 1;
 }
 
 void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
-    // TODO
+    if(layout->changed) {
+        XRectangle ext, lext;
+        XmbTextExtents(layout->fontset, layout->text, layout->length, &ext, &lext);
+        layout->width = ext.width;
+        layout->height = ext.height;
+        layout->changed = 0;
+    }
+    *width = layout->width;
+    *height = layout->height;
 }
 
 void ui_text_setwidth(UiTextLayout *layout, int width) {
-    // TODO
+    layout->maxwidth = width;
 }
 
 
@@ -180,13 +190,26 @@
 
 void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
     UiXlibGraphics *gr = (UiXlibGraphics*)g;
+    int width, height;
+    ui_text_getsize(text, &width, &height);
+    if(text->maxwidth > 0) {
+        XRectangle clip;
+        clip.x = x;
+        clip.y = y;
+        clip.width = text->maxwidth;
+        clip.height = height;
+        XSetClipRectangles(gr->display, gr->gc, 0, 0, &clip, 1, Unsorted);
+    }
+    
     XmbDrawString(
             gr->display,
             XtWindow(gr->widget),
             text->fontset,
             gr->gc,
             x,
-            y,
+            y + height,
             text->text,
             text->length);
+    
+    XSetClipMask(gr->display, gr->gc, None);
 }
--- a/ui/motif/graphics.h	Sun Nov 29 20:03:06 2015 +0100
+++ b/ui/motif/graphics.h	Sun Nov 29 21:43:03 2015 +0100
@@ -56,6 +56,10 @@
     size_t   length;
     Widget   widget;
     XFontSet fontset;
+    int      maxwidth;
+    int      width;
+    int      height;
+    int      changed;
 };
 
 

mercurial