AG_Label * AG_StatusbarAddLabel(AG_Statusbar *sbar, const char *fmt, ...) { AG_Label *lab; va_list ap; AG_ObjectLock(sbar); #ifdef AG_DEBUG if (sbar->nlabels+1 >= AG_STATUSBAR_MAX_LABELS) AG_FatalError("AG_StatusbarAddLabel: Too many labels"); #endif sbar->labels[sbar->nlabels] = Malloc(sizeof(AG_Label)); lab = sbar->labels[sbar->nlabels]; AG_ObjectInit(lab, &agLabelClass); lab->type = AG_LABEL_STATIC; va_start(ap, fmt); Vasprintf(&lab->text, fmt, ap); va_end(ap); AG_ExpandHoriz(lab); AG_ObjectAttach(&sbar->box, lab); lab = sbar->labels[sbar->nlabels++]; AG_ObjectUnlock(sbar); AG_Redraw(sbar); return (lab); }
static void UpdateSV(AG_HSVPal *pal, int ax, int ay) { float s, v; int x = ax - pal->triangle.x; int y = ay - pal->triangle.y; if (x < -y/2) { x = -y/2; } if (x > y/2) { x = y/2; } if (y > pal->triangle.h-1) { y = pal->triangle.h-1; } s = 1.0 - (float)y/(float)pal->triangle.h; v = 1.0 - (float)(x + y/2)/(float)pal->triangle.h; if (s < 0.0F) { s = 0.00001F; } else if (s > 1.0F) { s = 1.0F; } if (v < 0.0F) { v = 0.0001F; } else if (v > 1.0F) { v = 1.0F; } AG_SetFloat(pal, "saturation", s); AG_SetFloat(pal, "value", v); UpdatePixelFromHSVA(pal); AG_PostEvent(NULL, pal, "sv-changed", NULL); pal->flags |= AG_HSVPAL_DIRTY; AG_Redraw(pal); }
/* Set the text from a format string. */ void AG_TextboxPrintf(AG_Textbox *tb, const char *fmt, ...) { AG_Variable *stringb; va_list args; char *text; AG_ObjectLock(tb->ed); stringb = AG_GetVariable(tb->ed, "string", &text); if (fmt != NULL && fmt[0] != '\0') { va_start(args, fmt); Vsnprintf(text, stringb->info.size, fmt, args); va_end(args); tb->ed->pos = AG_LengthUTF8(text); } else { text[0] = '\0'; tb->ed->pos = 0; } AG_TextboxBufferChanged(tb); AG_UnlockVariable(stringb); AG_ObjectUnlock(tb->ed); /* XXX for AG_Numerical, etc. */ AG_Redraw(tb); }
/* Seek one word backwards. */ static int WordBack(AG_Editable *ed, AG_EditableBuffer *buf, AG_KeySym keysym, Uint keymod, Uint32 uch) { int newPos = ed->pos; Uint32 *c; /* XXX: handle other types of spaces */ if (ed->pos > 1 && buf->s[newPos-1] == ' ') { newPos -= 2; } for (c = &buf->s[newPos]; c > &buf->s[0] && *c != ' '; c--, newPos--) ;; if (*c == ' ') newPos++; if (keymod & AG_KEYMOD_SHIFT) { ed->sel += (ed->pos - newPos); } else { ed->sel = 0; } ed->pos = newPos; ed->flags |= AG_EDITABLE_MARKPREF; ed->flags |= AG_EDITABLE_BLINK_ON; ed->xScrollTo = &ed->xCurs; ed->yScrollTo = &ed->yCurs; AG_Redraw(ed); return (0); }
static void Bound(AG_Event *event) { AG_HSVPal *hsv = AG_SELF(); AG_Variable *V = AG_PTR(1); if (AG_VARIABLE_TYPE(V) == AG_VARIABLE_UINT32 && strcmp(V->name, "pixel") == 0) { #if 0 hsv->flags |= AG_HSVPAL_PIXEL; #endif UpdateHSVFromPixel(hsv, *(Uint32 *)V->data.p); } else if (strcmp(V->name, "RGBAv") == 0) { UpdateHSVFromRGBAv(hsv); } else if (strcmp(V->name, "RGBv") == 0) { UpdateHSVFromRGBv(hsv); } else if (strcmp(V->name, "pixel-format") == 0) { if (!(hsv->flags & AG_HSVPAL_FORCE_NOALPHA)) { AG_PixelFormat **pFormat = (AG_PixelFormat **)V->data.p; if ((*pFormat)->Amask != 0) { hsv->flags &= ~(AG_HSVPAL_NOALPHA); } else { hsv->flags |= AG_HSVPAL_NOALPHA; } } } AG_Redraw(hsv); }
/* Move cursor to beginning of line. */ static int CursorHome(AG_Editable *ed, AG_EditableBuffer *buf, AG_KeySym keysym, Uint keymod, Uint32 uch) { int newPos = ed->pos; Uint32 *c; if (ed->flags & AG_EDITABLE_MULTILINE) { if (newPos == 0) { return (0); } for (c = &buf->s[newPos - 1]; c >= &buf->s[0] && newPos >= 0; c--, newPos--) { if (*c == '\n') break; } } else { newPos = 0; } if (keymod & AG_KEYMOD_SHIFT) { ed->sel += (ed->pos - newPos); } else { ed->sel = 0; } ed->pos = newPos; ed->x = 0; ed->flags |= AG_EDITABLE_MARKPREF; AG_Redraw(ed); return (0); }
/* Seek one word forward. */ static int WordForw(AG_Editable *ed, AG_EditableBuffer *buf, AG_KeySym keysym, Uint keymod, Uint32 uch) { int newPos = ed->pos; Uint32 *c; if (newPos == buf->len) { return (0); } if (buf->len > 1 && buf->s[newPos] == ' ') { newPos++; } for (c = &buf->s[newPos]; *c != '\0' && *c != ' '; c++, newPos++) ;; if (keymod & AG_KEYMOD_SHIFT) { ed->sel += (ed->pos - newPos); } else { ed->sel = 0; } ed->pos = newPos; ed->flags |= AG_EDITABLE_MARKPREF; ed->flags |= AG_EDITABLE_BLINK_ON; ed->xScrollTo = &ed->xCurs; ed->yScrollTo = &ed->yCurs; AG_Redraw(ed); return (0); }
static void SetState(AG_Button *bu, AG_Variable *binding, void *p, int v) { switch (AG_VARIABLE_TYPE(binding)) { case AG_VARIABLE_INT: *(int *)p = v; break; case AG_VARIABLE_UINT8: *(Uint8 *)p = v; break; case AG_VARIABLE_UINT16: *(Uint16 *)p = v; break; case AG_VARIABLE_UINT32: *(Uint32 *)p = v; break; case AG_VARIABLE_P_FLAG: AG_SETFLAGS(*(int *)p, (int)binding->info.bitmask, v); break; case AG_VARIABLE_P_FLAG8: AG_SETFLAGS(*(Uint8 *)p, (Uint8)binding->info.bitmask, v); break; case AG_VARIABLE_P_FLAG16: AG_SETFLAGS(*(Uint16 *)p, (Uint16)binding->info.bitmask, v); break; case AG_VARIABLE_P_FLAG32: AG_SETFLAGS(*(Uint32 *)p, (Uint32)binding->info.bitmask, v); break; } AG_Redraw(bu); }
static __inline__ void SetAlpha8(AG_HSVPal *pal, Uint8 a) { AG_Variable *bAlpha; void *pAlpha; bAlpha = AG_GetVariable(pal, "alpha", &pAlpha); switch (AG_VARIABLE_TYPE(bAlpha)) { case AG_VARIABLE_FLOAT: *(float *)pAlpha = (float)(((float)a)/255.0); break; case AG_VARIABLE_DOUBLE: *(double *)pAlpha = (double)(((double)a)/255.0); break; case AG_VARIABLE_INT: *(int *)pAlpha = (int)a; break; case AG_VARIABLE_UINT8: *(Uint8 *)pAlpha = (Uint8)a; break; default: break; } AG_UnlockVariable(bAlpha); AG_Redraw(pal); }
/* Configure alternate font */ void AG_TextboxSetFont(AG_Textbox *tb, AG_Font *font) { AG_ObjectLock(tb); tb->font = font; AG_ObjectUnlock(tb); AG_Redraw(tb); }
void AG_SeparatorSetPadding(AG_Separator *sep, Uint pixels) { AG_ObjectLock(sep); sep->padding = pixels; AG_ObjectUnlock(sep); AG_Redraw(sep); }
void AG_ButtonSetPadding(AG_Button *bu, int lPad, int rPad, int tPad, int bPad) { AG_ObjectLock(bu); if (lPad != -1) { bu->lPad = lPad; } if (rPad != -1) { bu->rPad = rPad; } if (tPad != -1) { bu->tPad = tPad; } if (bPad != -1) { bu->bPad = bPad; } AG_ObjectUnlock(bu); AG_Redraw(bu); }
static void UpdateHSVFromRGBAv(AG_HSVPal *hsv) { AG_Variable *bRGBAv; void *RGBAv; Uint8 r, g, b, a; float h, s, v; if ((bRGBAv = AG_GetVariable(hsv, "RGBAv", &RGBAv)) == NULL) { return; } switch (AG_VARIABLE_TYPE(bRGBAv)) { case AG_VARIABLE_FLOAT: r = (Uint8)(((float *)RGBAv)[0] * 255.0); g = (Uint8)(((float *)RGBAv)[1] * 255.0); b = (Uint8)(((float *)RGBAv)[2] * 255.0); a = (Uint8)(((float *)RGBAv)[3] * 255.0); break; case AG_VARIABLE_DOUBLE: r = (Uint8)(((double *)RGBAv)[0] * 255.0); g = (Uint8)(((double *)RGBAv)[1] * 255.0); b = (Uint8)(((double *)RGBAv)[2] * 255.0); a = (Uint8)(((double *)RGBAv)[3] * 255.0); break; case AG_VARIABLE_INT: r = (Uint8)(((int *)RGBAv)[0]); g = (Uint8)(((int *)RGBAv)[1]); b = (Uint8)(((int *)RGBAv)[2]); a = (Uint8)(((int *)RGBAv)[3]); break; case AG_VARIABLE_UINT8: r = ((Uint8 *)RGBAv)[0]; g = ((Uint8 *)RGBAv)[1]; b = ((Uint8 *)RGBAv)[2]; a = ((Uint8 *)RGBAv)[3]; break; default: r = 0; g = 0; b = 0; a = 0; break; } AG_RGB2HSV(r, g, b, &h, &s, &v); AG_SetFloat(hsv, "hue", h); AG_SetFloat(hsv, "saturation", s); AG_SetFloat(hsv, "value", v); SetAlpha8(hsv, a); AG_UnlockVariable(bRGBAv); hsv->flags |= AG_HSVPAL_DIRTY; AG_Redraw(hsv); }
static void MouseButtonUp(AG_Event *event) { AG_Slider *sl = AG_SELF(); if (sl->ctlPressed) { sl->ctlPressed = 0; sl->xOffs = 0; AG_PostEvent(NULL, sl, "slider-drag-end", NULL); AG_Redraw(sl); } }
static void CopyColor(AG_Event *event) { AG_HSVPal *pal = AG_PTR(1); AG_ObjectLock(pal); AG_MutexLock(&CopyLock); cH = AG_GetFloat(pal, "hue"); cS = AG_GetFloat(pal, "saturation"); cV = AG_GetFloat(pal, "value"); cA = AG_GetFloat(pal, "alpha"); AG_MutexUnlock(&CopyLock); AG_ObjectUnlock(pal); AG_Redraw(pal); }
static void SetComplementaryColor(AG_Event *event) { AG_HSVPal *pal = AG_PTR(1); float hue; AG_ObjectLock(pal); hue = AG_GetFloat(pal, "hue"); AG_SetFloat(pal, "hue", ((int)hue+180) % 359); UpdatePixelFromHSVA(pal); pal->flags |= AG_HSVPAL_DIRTY; AG_PostEvent(NULL, pal, "h-changed", NULL); AG_PostEvent(NULL, pal, "sv-changed", NULL); AG_ObjectUnlock(pal); AG_Redraw(pal); }
/* Set the size of the control in pixels. */ void AG_SliderSetControlSize(AG_Slider *sl, int size) { AG_ObjectLock(sl); sl->wControlPref = size; switch (sl->type) { case AG_SLIDER_HORIZ: sl->wControl = MIN(size, HEIGHT(sl)); break; case AG_SLIDER_VERT: sl->wControl = MIN(size, WIDTH(sl)); break; } AG_ObjectUnlock(sl); AG_Redraw(sl); }
static void UpdateHue(AG_HSVPal *pal, int x, int y) { float h; h = Atan2((float)y, (float)x); if (h < 0) { h += (float)(2*AG_PI); } AG_SetFloat(pal, "hue", h/(2*AG_PI)*360.0); UpdatePixelFromHSVA(pal); AG_PostEvent(NULL, pal, "h-changed", NULL); pal->flags |= AG_HSVPAL_DIRTY; AG_Redraw(pal); }
/* Set the label text (C string). */ void AG_ButtonTextS(AG_Button *bu, const char *label) { AG_ObjectLock(bu); if (bu->surface != -1) { AG_ButtonSurface(bu, NULL); } if (bu->lbl == NULL) { bu->lbl = AG_LabelNewS(bu, 0, label); AG_LabelJustify(bu->lbl, bu->justify); AG_LabelValign(bu->lbl, bu->valign); } else { AG_LabelTextS(bu->lbl, label); } AG_ObjectUnlock(bu); AG_Redraw(bu); }
void AG_ButtonSurfaceNODUP(AG_Button *bu, AG_Surface *su) { AG_ObjectLock(bu); if (bu->lbl != NULL) { AG_ObjectDetach(bu->lbl); AG_ObjectDestroy(bu->lbl); bu->lbl = NULL; } if (bu->surface != -1) { AG_WidgetReplaceSurfaceNODUP(bu, bu->surface, su); } else { bu->surface = AG_WidgetMapSurfaceNODUP(bu, su); } AG_ObjectUnlock(bu); AG_Redraw(bu); }
/* Move cursor right. */ static int CursorRight(AG_Editable *ed, AG_EditableBuffer *buf, AG_KeySym keysym, Uint keymod, Uint32 uch) { if (ed->pos < buf->len) { ed->pos++; if (keymod & AG_KEYMOD_SHIFT) { ed->sel--; } else { ed->sel = 0; } } ed->flags |= AG_EDITABLE_MARKPREF; ed->flags |= AG_EDITABLE_BLINK_ON; ed->xScrollTo = &ed->xCurs; ed->yScrollTo = &ed->yCurs; AG_Redraw(ed); return (0); }
static void UpdateHSVFromPixel(AG_HSVPal *hsv, Uint32 pixel) { Uint8 r, g, b, a; float h, s, v; AG_Variable *bFormat; AG_PixelFormat **pFormat; bFormat = AG_GetVariable(hsv, "pixel-format", &pFormat); AG_GetPixelRGBA(pixel, *pFormat, &r,&g,&b,&a); AG_RGB2HSV(r, g, b, &h,&s,&v); AG_SetFloat(hsv, "hue", h); AG_SetFloat(hsv, "saturation", s); AG_SetFloat(hsv, "value", v); SetAlpha8(hsv, a); AG_UnlockVariable(bFormat); AG_Redraw(hsv); }
static void UpdateAlpha(AG_HSVPal *pal, int x) { AG_Variable *bAlpha; void *pAlpha; bAlpha = AG_GetVariable(pal, "alpha", &pAlpha); switch (AG_VARIABLE_TYPE(bAlpha)) { case AG_VARIABLE_FLOAT: *(float *)pAlpha = ((float)x)/((float)pal->rAlpha.w); if (*(float *)pAlpha > 1.0) { *(float *)pAlpha = 1.0; } else if (*(float *)pAlpha < 0.0) { *(float *)pAlpha = 0.0; } break; case AG_VARIABLE_DOUBLE: *(double *)pAlpha = ((double)x)/((double)pal->rAlpha.w); if (*(double *)pAlpha > 1.0) { *(double *)pAlpha = 1.0; } else if (*(double *)pAlpha < 0.0) { *(double *)pAlpha = 0.0; } break; case AG_VARIABLE_INT: *(int *)pAlpha = x/pal->rAlpha.w; if (*(int *)pAlpha > 255) { *(int *)pAlpha = 255; } else if (*(int *)pAlpha < 0) { *(int *)pAlpha = 0; } break; case AG_VARIABLE_UINT8: *(Uint8 *)pAlpha = (Uint8)(x/pal->rAlpha.w); break; default: break; } AG_UnlockVariable(bAlpha); UpdatePixelFromHSVA(pal); AG_PostEvent(NULL, pal, "a-changed", NULL); AG_Redraw(pal); }
/* Set the label text (format string). */ void AG_BoxSetLabelS(AG_Box *box, const char *s) { AG_ObjectLock(box); if (s != NULL) { if (box->lbl == NULL) { box->lbl = AG_LabelNewS(box, 0, s); AG_SetStyle(box->lbl, "font-size", "80%"); } else { AG_LabelTextS(box->lbl, s); } } else { AG_ObjectDetach(box->lbl); AG_ObjectDestroy(box->lbl); box->lbl = NULL; } AG_Redraw(box); AG_ObjectUnlock(box); }
static void PasteColor(AG_Event *event) { AG_HSVPal *pal = AG_PTR(1); AG_ObjectLock(pal); AG_MutexLock(&CopyLock); AG_SetFloat(pal, "hue", cH); AG_SetFloat(pal, "saturation", cS); AG_SetFloat(pal, "value", cV); AG_SetFloat(pal, "alpha", cA); AG_MutexUnlock(&CopyLock); UpdatePixelFromHSVA(pal); pal->flags |= AG_HSVPAL_DIRTY; AG_PostEvent(NULL, pal, "h-changed", NULL); AG_PostEvent(NULL, pal, "sv-changed", NULL); AG_ObjectUnlock(pal); AG_Redraw(pal); }
static void Decrement(AG_Slider *sl) { AG_Variable *bVal, *bMin, *bMax, *bInc; void *pVal, *pMin, *pMax, *pInc; bVal = AG_GetVariable(sl, "value", &pVal); bMin = AG_GetVariable(sl, "min", &pMin); bMax = AG_GetVariable(sl, "max", &pMax); bInc = AG_GetVariable(sl, "inc", &pInc); switch (AG_VARIABLE_TYPE(bVal)) { case AG_VARIABLE_FLOAT: DECREMENT(float); break; case AG_VARIABLE_DOUBLE: DECREMENT(double); break; #ifdef HAVE_LONG_DOUBLE case AG_VARIABLE_LONG_DOUBLE: DECREMENT(long double); break; #endif case AG_VARIABLE_INT: DECREMENT(int); break; case AG_VARIABLE_UINT: DECREMENT(Uint); break; case AG_VARIABLE_UINT8: DECREMENT(Uint8); break; case AG_VARIABLE_SINT8: DECREMENT(Sint8); break; case AG_VARIABLE_UINT16: DECREMENT(Uint16); break; case AG_VARIABLE_SINT16: DECREMENT(Sint16); break; case AG_VARIABLE_UINT32: DECREMENT(Uint32); break; case AG_VARIABLE_SINT32: DECREMENT(Sint32); break; #ifdef HAVE_64BIT case AG_VARIABLE_UINT64: DECREMENT(Uint64); break; case AG_VARIABLE_SINT64: DECREMENT(Sint64); break; #endif default: break; } AG_PostEvent(NULL, sl, "slider-changed", NULL); AG_UnlockVariable(bVal); AG_UnlockVariable(bMin); AG_UnlockVariable(bMax); AG_UnlockVariable(bInc); AG_Redraw(sl); }
static void MouseButtonDown(AG_Event *event) { AG_HSVPal *pal = AG_SELF(); int btn = AG_INT(1); int x = AG_INT(2); int y = AG_INT(3); float r; if (!AG_WidgetIsFocused(pal)) AG_WidgetFocus(pal); switch (btn) { case AG_MOUSE_LEFT: if (y > pal->rAlpha.y) { UpdateAlpha(pal, x); pal->state = AG_HSVPAL_SEL_A; } else { x -= pal->circle.x; y -= pal->circle.y; r = Hypot((float)x, (float)y); if (r > (float)pal->circle.rin) { UpdateHue(pal, x, y); pal->state = AG_HSVPAL_SEL_H; } else { UpdateSV(pal, AG_INT(2), AG_INT(3)); pal->state = AG_HSVPAL_SEL_SV; } } AG_Redraw(pal); break; case AG_MOUSE_MIDDLE: case AG_MOUSE_RIGHT: OpenMenu(pal, x,y); break; } }
static void MouseButtonDown(AG_Event *event) { AG_Slider *sl = AG_SELF(); int button = AG_INT(1); int x = ((sl->type == AG_SLIDER_HORIZ) ? AG_INT(2) : AG_INT(3)); int pos; if (button != AG_MOUSE_LEFT) { return; } if (GetPosition(sl, &pos) == -1) return; if (!AG_WidgetIsFocused(sl)) { AG_WidgetFocus(sl); } if (x >= pos && x <= (pos + sl->wControl)) { /* * Click on the slider itself. We don't do anything except * saving the cursor position which we will use in future * mousemotion events. */ sl->ctlPressed = 1; sl->xOffs = x - pos; AG_PostEvent(NULL, sl, "slider-drag-begin", NULL); } else { /* * Click outside of control. We seek to the absolute position * described by the cursor. */ sl->ctlPressed = 1; sl->xOffs = sl->wControl/2; SeekToPosition(sl, x - sl->xOffs); AG_PostEvent(NULL, sl, "slider-drag-begin", NULL); } AG_Redraw(sl); }
/* Move cursor one page down in a multi-line string. */ static int PageDown(AG_Editable *ed, AG_EditableBuffer *buf, AG_KeySym keysym, Uint keymod, Uint32 uch) { int prevPos = ed->pos; int prevSel = ed->sel; if (!(ed->flags & AG_EDITABLE_MULTILINE)) return (0); AG_EditableMoveCursor(ed, buf, ed->xCurs, (ed->yCurs - ed->y + ed->yVis)*agTextFontLineSkip + 1); if (keymod & AG_KEYMOD_SHIFT) { ed->sel = prevSel - (ed->pos - prevPos); } else { ed->sel = 0; } ed->xScrollTo = &ed->xCurs; ed->yScrollTo = &ed->yCurs; AG_Redraw(ed); return (0); }
static __inline__ void SeekToPosition(AG_Slider *sl, int x) { AG_Variable *bMin, *bMax, *bVal; void *pMin, *pMax, *pVal; bVal = AG_GetVariable(sl, "value", &pVal); bMin = AG_GetVariable(sl, "min", &pMin); bMax = AG_GetVariable(sl, "max", &pMax); switch (AG_VARIABLE_TYPE(bVal)) { case AG_VARIABLE_FLOAT: SEEK_TO_POSITION(float); break; case AG_VARIABLE_DOUBLE: SEEK_TO_POSITION(double); break; #ifdef HAVE_LONG_DOUBLE case AG_VARIABLE_LONG_DOUBLE: SEEK_TO_POSITION(long double); break; #endif case AG_VARIABLE_INT: SEEK_TO_POSITION(int); break; case AG_VARIABLE_UINT: SEEK_TO_POSITION(Uint); break; case AG_VARIABLE_UINT8: SEEK_TO_POSITION(Uint8); break; case AG_VARIABLE_SINT8: SEEK_TO_POSITION(Sint8); break; case AG_VARIABLE_UINT16: SEEK_TO_POSITION(Uint16); break; case AG_VARIABLE_SINT16: SEEK_TO_POSITION(Sint16); break; case AG_VARIABLE_UINT32: SEEK_TO_POSITION(Uint32); break; case AG_VARIABLE_SINT32: SEEK_TO_POSITION(Sint32); break; #ifdef HAVE_64BIT case AG_VARIABLE_UINT64: SEEK_TO_POSITION(Uint64); break; case AG_VARIABLE_SINT64: SEEK_TO_POSITION(Sint64); break; #endif default: break; } AG_PostEvent(NULL, sl, "slider-changed", NULL); AG_UnlockVariable(bMax); AG_UnlockVariable(bMin); AG_UnlockVariable(bVal); AG_Redraw(sl); }