Added a color well to the histogram example.

This commit is contained in:
Pietro Gagliardi 2016-05-15 21:02:35 -04:00
parent bf411e787e
commit a038923060
2 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,8 @@
// 15 may 2016
#import "uipriv_darwin.h"
// TODO no intrinsic height?
@interface colorButton : NSColorWell {
uiColorButton *libui_b;
BOOL libui_changing;

View File

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