From 423719b21fa21f38739742cee111f32c72c4d8c9 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 14 Apr 2016 12:54:37 -0400 Subject: [PATCH] Started adding uiFontButton. Duplicated Page 9 to Page 10 to test it. --- test/GNUfiles.mk | 1 + test/page10.c | 204 +++++++++++++++++++++++++++++++++++++++++++++++ test/test.h | 3 + ui.h | 5 ++ 4 files changed, 213 insertions(+) create mode 100644 test/page10.c diff --git a/test/GNUfiles.mk b/test/GNUfiles.mk index 4de5e9b7..2b9501b9 100644 --- a/test/GNUfiles.mk +++ b/test/GNUfiles.mk @@ -16,6 +16,7 @@ CFILES += \ test/page7c.c \ test/page8.c \ test/page9.c \ + test/page10.c \ test/spaced.c HFILES += \ diff --git a/test/page10.c b/test/page10.c new file mode 100644 index 00000000..39cb0f3c --- /dev/null +++ b/test/page10.c @@ -0,0 +1,204 @@ +// 22 december 2015 +#include "test.h" + +// TODO draw a rectangle pointing out where (10,10) is both to test initial colors and to figure out what the *real* ascent is + +static uiEntry *textString; +static uiFontButton *textFontButton; +static uiEntry *textWidth; +static uiButton *textApply; +static uiCheckbox *addLeading; +static uiArea *textArea; +static uiAreaHandler textAreaHandler; + +static double entryDouble(uiEntry *e) +{ + char *s; + double d; + + s = uiEntryText(e); + d = atof(s); + uiFreeText(s); + return d; +} + +// TODO this should be altered not to restore all state on exit so default text attributes can be checked +static void drawGuides(uiDrawContext *c, uiDrawTextFontMetrics *m) +{ + uiDrawPath *p; + uiDrawBrush b; + uiDrawStrokeParams sp; + + memset(&b, 0, sizeof (uiDrawBrush)); + b.Type = uiDrawBrushTypeSolid; + memset(&sp, 0, sizeof (uiDrawStrokeParams)); + sp.Cap = uiDrawLineCapFlat; + sp.Join = uiDrawLineJoinMiter; + sp.MiterLimit = uiDrawDefaultMiterLimit; + sp.Thickness = 2; + + uiDrawSave(c); + + p = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathNewFigure(p, 8, 10); + uiDrawPathLineTo(p, 8, 10 + m->Ascent); + uiDrawPathEnd(p); + b.R = 0.94; + b.G = 0.5; + b.B = 0.5; + b.A = 1.0; + uiDrawStroke(c, p, &b, &sp); + uiDrawFreePath(p); + + p = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathNewFigure(p, 8, 10 + m->Ascent); + uiDrawPathLineTo(p, 8, 10 + m->Ascent + m->Descent); + uiDrawPathEnd(p); + b.R = 0.12; + b.G = 0.56; + b.B = 1.0; + b.A = 1.0; + uiDrawStroke(c, p, &b, &sp); + uiDrawFreePath(p); + + p = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathAddRectangle(p, 0, 0, 10, 10); + uiDrawPathEnd(p); + uiDrawClip(c, p); + b.R = 0.85; + b.G = 0.65; + b.B = 0.13; + b.A = 1.0; + uiDrawStroke(c, p, &b, &sp); + uiDrawFreePath(p); + + uiDrawRestore(c); +} + +static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp) +{ +#if 0 + uiDrawTextFontDescriptor desc; + uiDrawTextFont *font; + char *s; + char *family; // make compiler happy + uiDrawTextLayout *layout; + uiDrawTextFontMetrics metrics; + double ypos; + double width; + double height; + + memset(&desc, 0, sizeof (uiDrawTextFontDescriptor)); + family = uiEntryText(textFont); + desc.Family = family; + desc.Size = entryDouble(textSize); + desc.Weight = uiComboboxSelected(textWeight); + desc.Italic = uiComboboxSelected(textItalic); + desc.SmallCaps = uiCheckboxChecked(textSmallCaps); + desc.Stretch = uiComboboxSelected(textStretch); + desc.Gravity = uiComboboxSelected(textGravity); + font = uiDrawLoadClosestFont(&desc); + uiFreeText(family); + uiDrawTextFontGetMetrics(font, &metrics); + + width = entryDouble(textWidth); + + drawGuides(dp->Context, &metrics); + + s = uiEntryText(textString); + layout = uiDrawNewTextLayout(s, font, width); + uiFreeText(s); + ypos = 10; + uiDrawText(dp->Context, 10, ypos, layout); + // TODO make these optional? + uiDrawTextLayoutExtents(layout, &width, &height); + uiDrawFreeTextLayout(layout); + + layout = uiDrawNewTextLayout("This is a second line", font, -1); + if (/*TODO reuse width*/entryDouble(textWidth) < 0) { + double ad; + + ad = metrics.Ascent + metrics.Descent; + printf("ad:%g extent:%g\n", ad, height); + } + ypos += height; + if (uiCheckboxChecked(addLeading)) + ypos += metrics.Leading; + uiDrawText(dp->Context, 10, ypos, layout); + uiDrawFreeTextLayout(layout); + + uiDrawFreeTextFont(font); +#endif +} + +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) +{ + // do nothing + return 0; +} + +static void onTextApply(uiButton *b, void *data) +{ + uiAreaQueueRedrawAll(textArea); +} + +uiBox *makePage10(void) +{ + uiBox *page10; + uiBox *vbox; + uiBox *hbox; + + page10 = newVerticalBox(); + vbox = page10; + + hbox = newHorizontalBox(); + uiBoxAppend(vbox, uiControl(hbox), 0); + + textString = uiNewEntry(); + // TODO make it placeholder + uiEntrySetText(textString, "Enter text here"); + uiBoxAppend(hbox, uiControl(textString), 1); + + textFontButton = uiNewFontButton(); + uiBoxAppend(hbox, uiControl(textFontButton), 1); + + hbox = newHorizontalBox(); + uiBoxAppend(vbox, uiControl(hbox), 0); + + textApply = uiNewButton("Apply"); + uiButtonOnClicked(textApply, onTextApply, NULL); + uiBoxAppend(hbox, uiControl(textApply), 1); + + textWidth = uiNewEntry(); + uiEntrySetText(textWidth, "-1"); + uiBoxAppend(hbox, uiControl(textWidth), 1); + + addLeading = uiNewCheckbox("Add Leading"); + uiCheckboxSetChecked(addLeading, 1); + uiBoxAppend(hbox, uiControl(addLeading), 0); + + textAreaHandler.Draw = handlerDraw; + textAreaHandler.MouseEvent = handlerMouseEvent; + textAreaHandler.MouseCrossed = handlerMouseCrossed; + textAreaHandler.DragBroken = handlerDragBroken; + textAreaHandler.KeyEvent = handlerKeyEvent; + textArea = uiNewArea(&textAreaHandler); + uiBoxAppend(vbox, uiControl(textArea), 1); + + return page10; +} diff --git a/test/test.h b/test/test.h index 2d47c2b8..89d41695 100644 --- a/test/test.h +++ b/test/test.h @@ -71,3 +71,6 @@ extern uiBox *makePage8(void); // page9.c extern uiBox *makePage9(void); + +// page10.c +extern uiBox *makePage10(void); diff --git a/ui.h b/ui.h index 0deb134c..bcd2375f 100644 --- a/ui.h +++ b/ui.h @@ -614,6 +614,11 @@ struct uiAreaKeyEvent { int Up; }; +typedef struct uiFontButton uiFontButton; +_UI_EXTERN uintmax_t uiFontButtonType(void); +#define uiFontButton(this) ((uiFontButton *) uiIsA((this), uiFontButtonType(), 1)) +_UI_EXTERN uiFontButton *uiNewFontButton(void); + #ifdef __cplusplus } #endif