Added the beginning of a text-drawing example (since I would need to heavily change the tester to test these things; hopefully in the future the example will be much more sophisticated). Time to fix segfaults!

This commit is contained in:
Pietro Gagliardi 2017-01-17 13:30:00 -05:00
parent 7bda3baee3
commit 907d7c5830
2 changed files with 135 additions and 1 deletions

View File

@ -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)

128
examples/drawtext/main.c Normal file
View File

@ -0,0 +1,128 @@
// 17 january 2017
#include <stdio.h>
#include <string.h>
#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;
}