diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3a9ec4c9..75b7f9d1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -31,8 +31,14 @@ if(NOT WIN32) target_link_libraries(cpp-multithread pthread) endif() +_add_example(drawtext + drawtext/main.c + ${_EXAMPLE_RESOURCES_RC} +) + add_custom_target(examples DEPENDS controlgallery histogram - cpp-multithread) + cpp-multithread + drawtext) diff --git a/examples/drawtext/main.c b/examples/drawtext/main.c new file mode 100644 index 00000000..90c401ee --- /dev/null +++ b/examples/drawtext/main.c @@ -0,0 +1,128 @@ +// 17 january 2017 +#include +#include +#include "../../ui.h" + +uiWindow *mainwin; +uiArea *area; +uiAreaHandler handler; + +const char *text = + "It is with a kind of fear that I begin to write the history of my life. " + "I have, as it were, a superstitious hesitation in lifting the veil that " + "clings about my childhood like a golden mist. The task of writing an " + "autobiography is a difficult one. When I try to classify my earliest " + "impressions, I find that fact and fancy look alike across the years that " + "link the past with the present. The woman paints the child's experiences " + "in her own fantasy. A few impressions stand out vividly from the first " + "years of my life; but \"the shadows of the prison-house are on the rest.\" " + "Besides, many of the joys and sorrows of childhood have lost their " + "poignancy; and many incidents of vital importance in my early education " + "have been forgotten in the excitement of great discoveries. In order, " + "therefore, not to be tedious I shall try to present in a series of " + "sketches only the episodes that seem to me to be the most interesting " + "and important." + ""; +char fontFamily[] = "Palatino"; +// TODO should be const; look at constructor function +uiDrawFontDescriptor defaultFont = { + .Family = fontFamily, + .Size = 18, + .Weight = uiDrawTextWeightNormal, + .Italic = uiDrawTextItalicNormal, + .Stretch = uiDrawTextStretchNormal, +}; +uiAttributedString *attrstr; + +#define margins 5 + +static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) +{ + uiDrawPath *path; + uiDrawTextLayout *layout; + + path = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathAddRectangle(path, margins, margins, + p->AreaWidth - 2 * margins, + p->AreaHeight - 2 * margins); + uiDrawPathEnd(path); + uiDrawClip(p->Context, path); + uiDrawFreePath(path); + + layout = uiDrawNewTextLayout(attrstr, + &defaultFont, + p->AreaWidth - 2 * margins); + uiDrawText(p->Context, layout, margins, margins); + uiDrawFreeTextLayout(layout); +} + +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; + + 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); + + attrstr = uiNewAttributedString(text); + + mainwin = uiNewWindow("libui Histogram Example", 640, 480, 1); + uiWindowSetMargined(mainwin, 1); + uiWindowOnClosing(mainwin, onClosing, NULL); + + area = uiNewArea(&handler); + uiWindowSetChild(mainwin, uiControl(area)); + + uiControlShow(uiControl(mainwin)); + uiMain(); + uiFreeAttributedString(attrstr); + uiUninit(); + return 0; +}