DVX_GUI/dvx/widgets/widgetLabel.c

101 lines
3.3 KiB
C

// widgetLabel.c -- Label widget
//
// Static text display -- the simplest widget. Not focusable, not interactive.
// Supports accelerator keys via '&' prefix: when the user presses Alt+key,
// focus moves to the next focusable widget after this label. This follows
// the Win3.1 convention where labels act as keyboard shortcuts for adjacent
// controls (e.g., a label "&Name:" before a text input).
//
// The text pointer is stored directly (not copied) -- the caller must ensure
// the string remains valid for the widget's lifetime, or use widgetLabelSetText
// to update it. This avoids unnecessary allocations for the common case of
// literal string labels.
//
// Background is transparent by default (bgColor == 0 means use the parent's
// content background color from the color scheme). The +1 in calcMinH adds a
// pixel of breathing room below the text baseline.
#include "widgetInternal.h"
// ============================================================
// wgtLabel
// ============================================================
WidgetT *wgtLabel(WidgetT *parent, const char *text) {
WidgetT *w = widgetAlloc(parent, WidgetLabelE);
if (w) {
w->as.label.text = text;
w->accelKey = accelParse(text);
}
return w;
}
// ============================================================
// widgetLabelGetText
// ============================================================
const char *widgetLabelGetText(const WidgetT *w) {
return w->as.label.text;
}
// ============================================================
// widgetLabelSetText
// ============================================================
void widgetLabelSetText(WidgetT *w, const char *text) {
w->as.label.text = text;
w->accelKey = accelParse(text);
}
// ============================================================
// widgetLabelCalcMinSize
// ============================================================
void widgetLabelCalcMinSize(WidgetT *w, const BitmapFontT *font) {
w->calcMinW = textWidthAccel(font, w->as.label.text);
w->calcMinH = font->charHeight + 1;
}
// ============================================================
// widgetLabelPaint
// ============================================================
void widgetLabelPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
int32_t textW = textWidthAccel(font, w->as.label.text);
int32_t textX = w->x;
int32_t textY = w->y + (w->h - font->charHeight) / 2;
if (w->as.label.textAlign == AlignCenterE) {
textX = w->x + (w->w - textW) / 2;
} else if (w->as.label.textAlign == AlignEndE) {
textX = w->x + w->w - textW;
}
if (!w->enabled) {
drawTextAccelEmbossed(d, ops, font, textX, textY, w->as.label.text, colors);
return;
}
uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg;
uint32_t bg = w->bgColor ? w->bgColor : colors->contentBg;
drawTextAccel(d, ops, font, textX, textY, w->as.label.text, fg, bg, false);
}
// ============================================================
// wgtLabelSetAlign
// ============================================================
void wgtLabelSetAlign(WidgetT *w, WidgetAlignE align) {
if (w && w->type == WidgetLabelE) {
w->as.label.textAlign = align;
}
}