From a038923060f4183ff6d62faa94d2c56e49f52831 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 15 May 2016 21:02:35 -0400 Subject: [PATCH] Added a color well to the histogram example. --- darwin/colorbutton.m | 2 ++ examples/histogram/main.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/darwin/colorbutton.m b/darwin/colorbutton.m index 47f8511e..af6d1faa 100644 --- a/darwin/colorbutton.m +++ b/darwin/colorbutton.m @@ -1,6 +1,8 @@ // 15 may 2016 #import "uipriv_darwin.h" +// TODO no intrinsic height? + @interface colorButton : NSColorWell { uiColorButton *libui_b; BOOL libui_changing; diff --git a/examples/histogram/main.c b/examples/histogram/main.c index 669a9033..f2b0e793 100644 --- a/examples/histogram/main.c +++ b/examples/histogram/main.c @@ -9,6 +9,7 @@ uiWindow *mainwin; uiArea *histogram; uiAreaHandler handler; uiSpinbox *datapoints[10]; +uiColorButton *colorButton; int currentPoint = -1; // some metrics @@ -94,6 +95,7 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) uiDrawStrokeParams sp; uiDrawMatrix m; double graphWidth, graphHeight; + double graphR, graphG, graphB, graphA; // fill the area with white setSolidBrush(&brush, colorWhite, 1.0); @@ -134,15 +136,23 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) uiDrawMatrixTranslate(&m, xoffLeft, yoffTop); uiDrawTransform(p->Context, &m); + // now get the color for the graph itself and set up the brush + uiColorButtonColor(colorButton, &graphR, &graphG, &graphB, &graphA); + brush.Type = uiDrawBrushTypeSolid; + brush.R = graphR; + brush.G = graphG; + brush.B = graphB; + // we set brush->A below to different values for the fill and stroke + // now create the fill for the graph below the graph line path = constructGraph(graphWidth, graphHeight, 1); - setSolidBrush(&brush, colorDodgerBlue, 0.5); + brush.A = graphA / 2; uiDrawFill(p->Context, path, &brush); uiDrawFreePath(path); // now draw the histogram line path = constructGraph(graphWidth, graphHeight, 0); - setSolidBrush(&brush, colorDodgerBlue, 1.0); + brush.A = graphA; uiDrawStroke(p->Context, path, &brush, &sp); uiDrawFreePath(path); @@ -216,6 +226,11 @@ static void onDatapointChanged(uiSpinbox *s, void *data) uiAreaQueueRedrawAll(histogram); } +static void onColorChanged(uiColorButton *b, void *data) +{ + uiAreaQueueRedrawAll(histogram); +} + static int onClosing(uiWindow *w, void *data) { uiControlDestroy(uiControl(mainwin)); @@ -235,6 +250,7 @@ int main(void) const char *err; uiBox *hbox, *vbox; int i; + uiDrawBrush brush; handler.Draw = handlerDraw; handler.MouseEvent = handlerMouseEvent; @@ -272,6 +288,17 @@ int main(void) uiBoxAppend(vbox, uiControl(datapoints[i]), 0); } + colorButton = uiNewColorButton(); + // TODO inline these + setSolidBrush(&brush, colorDodgerBlue, 1.0); + uiColorButtonSetColor(colorButton, + brush.R, + brush.G, + brush.B, + brush.A); + uiColorButtonOnChanged(colorButton, onColorChanged, NULL); + uiBoxAppend(vbox, uiControl(colorButton), 0); + histogram = uiNewArea(&handler); uiBoxAppend(hbox, uiControl(histogram), 1);