diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 75b7f9d1..430224d8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -32,9 +32,12 @@ if(NOT WIN32) endif() _add_example(drawtext + drawtext/basic.c drawtext/main.c ${_EXAMPLE_RESOURCES_RC} ) +target_include_directories(drawtext + PRIVATE drawtext) add_custom_target(examples DEPENDS diff --git a/examples/drawtext/basic.c b/examples/drawtext/basic.c new file mode 100644 index 00000000..2d3d7f3d --- /dev/null +++ b/examples/drawtext/basic.c @@ -0,0 +1,81 @@ +// 17 january 2017 +#include "drawtext.h" + +static 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." + ""; +static char fontFamily[] = "Palatino"; +// TODO should be const; look at constructor function +static uiDrawFontDescriptor defaultFont = { + .Family = fontFamily, + .Size = 18, + .Weight = uiDrawTextWeightNormal, + .Italic = uiDrawTextItalicNormal, + .Stretch = uiDrawTextStretchNormal, +}; +static uiAttributedString *attrstr; + +#define margins 10 + +static void draw(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); + + // TODO get rid of this later + path = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathAddRectangle(path, -100, -100, + p->AreaWidth * 2, + p->AreaHeight * 2); + uiDrawPathEnd(path); + uiDrawBrush b; + b.Type = uiDrawBrushTypeSolid; + b.R = 0.0; + b.G = 1.0; + b.B = 0.0; + b.A = 1.0; + uiDrawFill(p->Context, path, &b); + uiDrawFreePath(path); + + layout = uiDrawNewTextLayout(attrstr, + &defaultFont, + p->AreaWidth - 2 * margins); + uiDrawText(p->Context, layout, margins, margins); + uiDrawFreeTextLayout(layout); +} + +static struct example basicExample; + +struct example *mkBasicExample(void) +{ + basicExample.name = "Basic Paragraph of Text"; + basicExample.panel = uiControl(uiNewVerticalBox()); + basicExample.draw = draw; + + attrstr = uiNewAttributedString(text); + + return &basicExample; +} + +// TODO on GTK+ an area by itself in a window doesn't get expand properties set properly? diff --git a/examples/drawtext/drawtext.h b/examples/drawtext/drawtext.h new file mode 100644 index 00000000..adbac19b --- /dev/null +++ b/examples/drawtext/drawtext.h @@ -0,0 +1,17 @@ +// 20 january 2017 +#include +#include +#include "../../ui.h" + +struct example { + const char *name; + uiControl *panel; + void (*draw)(uiAreaDrawParams *p); + // TODO mouse and key? +}; + +// main.c +extern void redraw(void); + +// basic.c +extern struct example *mkBasicExample(void); diff --git a/examples/drawtext/main.c b/examples/drawtext/main.c index d01d96c6..ebc9210a 100644 --- a/examples/drawtext/main.c +++ b/examples/drawtext/main.c @@ -1,74 +1,32 @@ // 17 january 2017 -#include -#include -#include "../../ui.h" +#include "drawtext.h" -uiWindow *mainwin; -uiArea *area; -uiAreaHandler handler; +static uiWindow *mainwin; +static uiBox *box; +static uiCombobox *exampleList; +static uiArea *area; +static 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 nExamples 20 +static struct example *examples[nExamples]; +static int curExample = 0; -#define margins 10 +static void onExampleChanged(uiCombobox *c, void *data) +{ + uiControlHide(examples[curExample]->panel); + curExample = uiComboboxSelected(exampleList); + uiControlShow(examples[curExample]->panel); + redraw(); +} + +void redraw(void) +{ + uiAreaQueueRedrawAll(area); +} 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); - - // TODO get rid of this later - path = uiDrawNewPath(uiDrawFillModeWinding); - uiDrawPathAddRectangle(path, -100, -100, - p->AreaWidth * 2, - p->AreaHeight * 2); - uiDrawPathEnd(path); - uiDrawBrush b; - b.Type = uiDrawBrushTypeSolid; - b.R = 0.0; - b.G = 1.0; - b.B = 0.0; - b.A = 1.0; - uiDrawFill(p->Context, path, &b); - uiDrawFreePath(path); - - layout = uiDrawNewTextLayout(attrstr, - &defaultFont, - p->AreaWidth - 2 * margins); - uiDrawText(p->Context, layout, margins, margins); - uiDrawFreeTextLayout(layout); + examples[curExample]->draw(p); } static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e) @@ -109,6 +67,7 @@ int main(void) { uiInitOptions o; const char *err; + int n; handler.Draw = handlerDraw; handler.MouseEvent = handlerMouseEvent; @@ -126,18 +85,34 @@ int main(void) uiOnShouldQuit(shouldQuit, NULL); - attrstr = uiNewAttributedString(text); - mainwin = uiNewWindow("libui Text-Drawing Example", 640, 480, 1); uiWindowOnClosing(mainwin, onClosing, NULL); + box = uiNewVerticalBox(); + uiWindowSetChild(mainwin, uiControl(box)); + + exampleList = uiNewCombobox(); + uiBoxAppend(box, uiControl(exampleList), 0); + area = uiNewArea(&handler); - // TODO on GTK+ this doesn't get expand properties set properly? - uiWindowSetChild(mainwin, uiControl(area)); + uiBoxAppend(box, uiControl(area), 1); + + n = 0; + examples[n] = mkBasicExample(); + uiComboboxAppend(exampleList, examples[n]->name); + uiBoxAppend(box, examples[n]->panel, 0); + n++; + // and set things up for the initial state + uiComboboxSetSelected(exampleList, 0); + uiComboboxOnSelected(exampleList, onExampleChanged, NULL); + // and set up the first one + onExampleChanged(NULL, NULL); uiControlShow(uiControl(mainwin)); uiMain(); - uiFreeAttributedString(attrstr); + + // TODO free examples + uiUninit(); return 0; }