From 1fae3eea02de828f281156f899a0f89528004d40 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 10 Mar 2018 21:57:45 -0500 Subject: [PATCH] And wrote a new, simpler drawtext example. Now to debug run-time issues in the new attributed string code! First up: some infinite loop. --- examples/CMakeLists.txt | 6 +- examples/drawtext/main.c | 158 +++++++++++++++++++++++++++++++++++++++ ui_attrstr.h | 1 + 3 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 examples/drawtext/main.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b820df8b..cdedb039 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -31,12 +31,10 @@ if(NOT WIN32) target_link_libraries(cpp-multithread pthread) endif() -_add_example(opentype - opentype/main.c +_add_example(drawtext + drawtext/main.c ${_EXAMPLE_RESOURCES_RC} ) -target_include_directories(opentype - PRIVATE opentype) add_custom_target(examples DEPENDS diff --git a/examples/drawtext/main.c b/examples/drawtext/main.c new file mode 100644 index 00000000..13c7c659 --- /dev/null +++ b/examples/drawtext/main.c @@ -0,0 +1,158 @@ +// 10 march 2018 +#include +#include +#include "../../ui.h" + +uiWindow *mainwin; +uiArea *area; +uiAreaHandler handler; +uiFontButton *fontButton; + +uiAttributedString *attrstr; + +#define margins 20 + +static void appendWithAttribute(const char *what, uiAttribute *attr) +{ + size_t start, end; + + start = uiAttributedStringLen(attrstr); + end = start + strlen(what); + uiAttributedStringAppendUnattributed(attrstr, what); + uiAttributedStringSetAttribute(attrstr, attr, start, end); +} + +static void makeAttributedString(void) +{ + uiAttribute *attr; + + attrstr = uiNewAttributedString( + "Drawing strings with libui is done with the uiAttributedString and uiDrawTextLayout obects.\n" + "uiAttributedString lets you have a variety of attributes: "); + + attr = uiNewFamilyAttribute("Courier New"); + appendWithAttribute("font family", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewSizeAttribute(18); + appendWithAttribute("font size", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewWeightAttribute(uiTextWeightBold); + appendWithAttribute("font weight", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewItalicAttribute(uiTextItalicItalic); + appendWithAttribute("font italicness", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewStretchAttribute(uiTextStretchCondensed); + appendWithAttribute("font stretch", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewColorAttribute(0.75, 0.25, 0.5, 0.75); + appendWithAttribute("text color", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); + + attr = uiNewBackgroundAttribute(0.5, 0.5, 0.25, 0.5); + appendWithAttribute("text background color", attr); + uiAttributedStringAppendUnattributed(attrstr, ", "); +} + +static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) +{ + uiDrawTextLayout *textLayout; + uiFontDescriptor fontdesc; + uiDrawTextLayoutParams params; + + params.String = attrstr; + uiFontButtonFont(fontButton, &fontdesc); + params.Width = p->AreaWidth - (2 * margins); + params.Align = uiDrawTextAlignLeft; + textLayout = uiDrawNewTextLayout(¶ms); + // TODO clip to margins + uiDrawText(p->Context, textLayout, margins, margins); + uiDrawFreeTextLayout(textLayout); +} + +static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e) +{ + // do nothing +} + +static void handlerMouseCrossed(uiAreaHandler *ah, uiArea *a, int left) +{ + // do nothing +} + +static void handlerDragBroken(uiAreaHandler *ah, uiArea *a) +{ + // do nothing +} + +static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e) +{ + // reject all keys + return 0; +} + +static int onClosing(uiWindow *w, void *data) +{ + uiControlDestroy(uiControl(mainwin)); + uiQuit(); + return 0; +} + +static int shouldQuit(void *data) +{ + uiControlDestroy(uiControl(mainwin)); + return 1; +} + +int main(void) +{ + uiInitOptions o; + const char *err; + uiBox *hbox, *vbox; + + handler.Draw = handlerDraw; + handler.MouseEvent = handlerMouseEvent; + handler.MouseCrossed = handlerMouseCrossed; + handler.DragBroken = handlerDragBroken; + handler.KeyEvent = handlerKeyEvent; + + memset(&o, 0, sizeof (uiInitOptions)); + err = uiInit(&o); + if (err != NULL) { + fprintf(stderr, "error initializing ui: %s\n", err); + uiFreeInitError(err); + return 1; + } + + uiOnShouldQuit(shouldQuit, NULL); + + makeAttributedString(); + + mainwin = uiNewWindow("libui Histogram Example", 640, 480, 1); + uiWindowSetMargined(mainwin, 1); + uiWindowOnClosing(mainwin, onClosing, NULL); + + hbox = uiNewHorizontalBox(); + uiBoxSetPadded(hbox, 1); + uiWindowSetChild(mainwin, uiControl(hbox)); + + vbox = uiNewVerticalBox(); + uiBoxSetPadded(vbox, 1); + uiBoxAppend(hbox, uiControl(vbox), 0); + + fontButton = uiNewFontButton(); + uiBoxAppend(vbox, uiControl(fontButton), 0); + + area = uiNewArea(&handler); + uiBoxAppend(hbox, uiControl(area), 1); + + uiControlShow(uiControl(mainwin)); + uiMain(); + uiUninit(); + return 0; +} diff --git a/ui_attrstr.h b/ui_attrstr.h index 6dd738ee..66b0fb4b 100644 --- a/ui_attrstr.h +++ b/ui_attrstr.h @@ -433,6 +433,7 @@ typedef struct uiDrawTextLayout uiDrawTextLayout; // uiDrawTextAlign specifies the alignment of lines of text in a // uiDrawTextLayout. +// TODO should this really have Draw in the name? _UI_ENUM(uiDrawTextAlign) { uiDrawTextAlignLeft, uiDrawTextAlignCenter,