Files
wmaker/WINGs/wlabel.c
T

215 lines
4.0 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
#include "WINGsP.h"
typedef struct W_Label {
2009-08-20 00:59:40 +02:00
W_Class widgetClass;
W_View *view;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
char *caption;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMColor *textColor;
WMFont *font; /* if NULL, use default */
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
W_Pixmap *image;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
struct {
WMReliefType relief:3;
WMImagePosition imagePosition:4;
WMAlignment alignment:2;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
unsigned int noWrap:1;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
unsigned int redrawPending:1;
} flags;
1998-09-29 22:36:29 +00:00
} Label;
#define DEFAULT_WIDTH 60
#define DEFAULT_HEIGHT 14
#define DEFAULT_ALIGNMENT WALeft
#define DEFAULT_RELIEF WRFlat
#define DEFAULT_IMAGE_POSITION WIPNoImage
2009-08-20 00:59:40 +02:00
static void destroyLabel(Label * lPtr);
static void paintLabel(Label * lPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMLabel *WMCreateLabel(WMWidget * parent)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
Label *lPtr;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
lPtr = wmalloc(sizeof(Label));
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
lPtr->widgetClass = WC_Label;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
lPtr->view = W_CreateView(W_VIEW(parent));
if (!lPtr->view) {
wfree(lPtr);
return NULL;
}
lPtr->view->self = lPtr;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
WMCreateEventHandler(lPtr->view, ExposureMask | StructureNotifyMask, handleEvents, lPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
lPtr->flags.alignment = DEFAULT_ALIGNMENT;
lPtr->flags.relief = DEFAULT_RELIEF;
lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
lPtr->flags.noWrap = 1;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
return lPtr;
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelImage(WMLabel * lPtr, WMPixmap * image)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (lPtr->image != NULL)
WMReleasePixmap(lPtr->image);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (image)
lPtr->image = WMRetainPixmap(image);
else
lPtr->image = NULL;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
WMPixmap *WMGetLabelImage(WMLabel * lPtr)
1999-01-06 15:22:33 +00:00
{
2009-08-20 00:59:40 +02:00
return lPtr->image;
1999-01-06 15:22:33 +00:00
}
2009-08-20 00:59:40 +02:00
char *WMGetLabelText(WMLabel * lPtr)
2000-07-10 22:37:39 +00:00
{
2009-08-20 00:59:40 +02:00
return lPtr->caption;
2000-07-10 22:37:39 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelImagePosition(WMLabel * lPtr, WMImagePosition position)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
lPtr->flags.imagePosition = position;
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelTextAlignment(WMLabel * lPtr, WMAlignment alignment)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
lPtr->flags.alignment = alignment;
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelRelief(WMLabel * lPtr, WMReliefType relief)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
lPtr->flags.relief = relief;
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
void WMSetLabelText(WMLabel * lPtr, const char *text)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (lPtr->caption)
wfree(lPtr->caption);
if (text != NULL) {
lPtr->caption = wstrdup(text);
} else {
lPtr->caption = NULL;
}
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
WMFont *WMGetLabelFont(WMLabel * lPtr)
{
2009-08-20 00:59:40 +02:00
return lPtr->font;
}
2009-08-20 00:59:40 +02:00
void WMSetLabelFont(WMLabel * lPtr, WMFont * font)
2004-10-12 21:28:27 +00:00
{
2009-08-20 00:59:40 +02:00
if (lPtr->font != NULL)
WMReleaseFont(lPtr->font);
if (font)
lPtr->font = WMRetainFont(font);
else
lPtr->font = NULL;
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelTextColor(WMLabel * lPtr, WMColor * color)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (lPtr->textColor)
WMReleaseColor(lPtr->textColor);
lPtr->textColor = WMRetainColor(color);
2000-04-18 03:31:36 +00:00
2009-08-20 00:59:40 +02:00
if (lPtr->view->flags.realized) {
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetLabelWraps(WMLabel * lPtr, Bool flag)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
flag = ((flag == 0) ? 0 : 1);
if (lPtr->flags.noWrap != !flag) {
lPtr->flags.noWrap = !flag;
if (lPtr->view->flags.realized)
paintLabel(lPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void paintLabel(Label * lPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
W_Screen *scrPtr = lPtr->view->screen;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap,
lPtr->textColor ? lPtr->textColor : scrPtr->black,
(lPtr->font != NULL ? lPtr->font : scrPtr->normalFont),
lPtr->flags.relief, lPtr->caption,
lPtr->flags.alignment, lPtr->image, lPtr->flags.imagePosition, NULL, 0);
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
Label *lPtr = (Label *) data;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
CHECK_CLASS(data, WC_Label);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
switch (event->type) {
case Expose:
if (event->xexpose.count != 0)
break;
paintLabel(lPtr);
break;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
case DestroyNotify:
destroyLabel(lPtr);
break;
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void destroyLabel(Label * lPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (lPtr->textColor)
WMReleaseColor(lPtr->textColor);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (lPtr->caption)
wfree(lPtr->caption);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (lPtr->font)
WMReleaseFont(lPtr->font);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (lPtr->image)
WMReleasePixmap(lPtr->image);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
wfree(lPtr);
1998-09-29 22:36:29 +00:00
}