7 typedef struct W_Button
{
30 float periodicInterval
;
32 WMHandlerID
*timer
; /* for continuous mode */
36 WMImagePosition imagePosition
:4;
37 WMAlignment alignment
:2;
39 unsigned int selected
:1;
41 unsigned int enabled
:1;
43 unsigned int bordered
:1;
45 unsigned int springLoaded
:1;
47 unsigned int pushIn
:1; /* change relief while pushed */
49 unsigned int pushLight
:1; /* highlight while pushed */
51 unsigned int pushChange
:1; /* change caption while pushed */
53 unsigned int stateLight
:1; /* state indicated by highlight */
55 unsigned int stateChange
:1; /* state indicated by caption change */
57 unsigned int statePush
:1; /* state indicated by relief */
59 unsigned int continuous
:1; /* continually perform action */
61 unsigned int prevSelected
:1;
63 unsigned int pushed
:1;
65 unsigned int wasPushed
:1;
67 unsigned int redrawPending
:1;
69 unsigned int addedObserver
:1;
75 #define DEFAULT_BUTTON_WIDTH 60
76 #define DEFAULT_BUTTON_HEIGHT 24
77 #define DEFAULT_BUTTON_ALIGNMENT WACenter
78 #define DEFAULT_BUTTON_IS_BORDERED True
81 #define DEFAULT_RADIO_WIDTH 100
82 #define DEFAULT_RADIO_HEIGHT 20
83 #define DEFAULT_RADIO_ALIGNMENT WALeft
84 #define DEFAULT_RADIO_IMAGE_POSITION WIPLeft
85 #define DEFAULT_RADIO_TEXT "Radio"
88 #define DEFAULT_SWITCH_WIDTH 100
89 #define DEFAULT_SWITCH_HEIGHT 20
90 #define DEFAULT_SWITCH_ALIGNMENT WALeft
91 #define DEFAULT_SWITCH_IMAGE_POSITION WIPLeft
92 #define DEFAULT_SWITCH_TEXT "Switch"
95 static void destroyButton(Button
*bPtr
);
96 static void paintButton(Button
*bPtr
);
98 static void handleEvents(XEvent
*event
, void *data
);
99 static void handleActionEvents(XEvent
*event
, void *data
);
102 W_ViewProcedureTable _ButtonViewProcedures
= {
109 static char *WMPushedRadioNotification
="WMPushedRadioNotification";
112 #define NFONT(b) (b)->view->screen->normalFont
116 WMCreateCustomButton(WMWidget
*parent
, int behaviourMask
)
120 bPtr
= wmalloc(sizeof(Button
));
121 memset(bPtr
, 0, sizeof(Button
));
123 bPtr
->widgetClass
= WC_Button
;
125 bPtr
->view
= W_CreateView(W_VIEW(parent
));
130 bPtr
->view
->self
= bPtr
;
132 bPtr
->flags
.type
= 0;
134 bPtr
->flags
.springLoaded
= (behaviourMask
& WBBSpringLoadedMask
)!=0;
135 bPtr
->flags
.pushIn
= (behaviourMask
& WBBPushInMask
)!=0;
136 bPtr
->flags
.pushChange
= (behaviourMask
& WBBPushChangeMask
)!=0;
137 bPtr
->flags
.pushLight
= (behaviourMask
& WBBPushLightMask
)!=0;
138 bPtr
->flags
.stateLight
= (behaviourMask
& WBBStateLightMask
)!=0;
139 bPtr
->flags
.stateChange
= (behaviourMask
& WBBStateChangeMask
)!=0;
140 bPtr
->flags
.statePush
= (behaviourMask
& WBBStatePushMask
)!=0;
142 W_ResizeView(bPtr
->view
, DEFAULT_BUTTON_WIDTH
, DEFAULT_BUTTON_HEIGHT
);
143 bPtr
->flags
.alignment
= DEFAULT_BUTTON_ALIGNMENT
;
144 bPtr
->flags
.bordered
= DEFAULT_BUTTON_IS_BORDERED
;
146 bPtr
->flags
.enabled
= 1;
149 WMCreateEventHandler(bPtr
->view
, ExposureMask
|StructureNotifyMask
,
152 WMCreateEventHandler(bPtr
->view
, ButtonPressMask
|ButtonReleaseMask
153 |EnterWindowMask
|LeaveWindowMask
,
154 handleActionEvents
, bPtr
);
156 W_ResizeView(bPtr
->view
, DEFAULT_BUTTON_WIDTH
, DEFAULT_BUTTON_HEIGHT
);
157 bPtr
->flags
.alignment
= DEFAULT_BUTTON_ALIGNMENT
;
158 bPtr
->flags
.bordered
= DEFAULT_BUTTON_IS_BORDERED
;
166 WMCreateButton(WMWidget
*parent
, WMButtonType type
)
168 W_Screen
*scrPtr
= W_VIEW(parent
)->screen
;
172 case WBTMomentaryPush
:
173 bPtr
= WMCreateCustomButton(parent
, WBBSpringLoadedMask
174 |WBBPushInMask
|WBBPushLightMask
);
177 case WBTMomentaryChange
:
178 bPtr
= WMCreateCustomButton(parent
, WBBSpringLoadedMask
182 case WBTPushOnPushOff
:
183 bPtr
= WMCreateCustomButton(parent
, WBBPushInMask
|WBBStatePushMask
188 bPtr
= WMCreateCustomButton(parent
, WBBPushInMask
|WBBStateChangeMask
193 bPtr
= WMCreateCustomButton(parent
, WBBStateLightMask
);
197 bPtr
= WMCreateCustomButton(parent
, WBBStateChangeMask
);
198 bPtr
->flags
.bordered
= 0;
199 bPtr
->image
= WMRetainPixmap(scrPtr
->checkButtonImageOff
);
200 bPtr
->altImage
= WMRetainPixmap(scrPtr
->checkButtonImageOn
);
204 bPtr
= WMCreateCustomButton(parent
, WBBStateChangeMask
);
205 bPtr
->flags
.bordered
= 0;
206 bPtr
->image
= WMRetainPixmap(scrPtr
->radioButtonImageOff
);
207 bPtr
->altImage
= WMRetainPixmap(scrPtr
->radioButtonImageOn
);
211 case WBTMomentaryLight
:
212 bPtr
= WMCreateCustomButton(parent
, WBBSpringLoadedMask
214 bPtr
->flags
.bordered
= 1;
218 bPtr
->flags
.type
= type
;
220 if (type
==WBTRadio
) {
221 W_ResizeView(bPtr
->view
, DEFAULT_RADIO_WIDTH
, DEFAULT_RADIO_HEIGHT
);
222 WMSetButtonText(bPtr
, DEFAULT_RADIO_TEXT
);
223 bPtr
->flags
.alignment
= DEFAULT_RADIO_ALIGNMENT
;
224 bPtr
->flags
.imagePosition
= DEFAULT_RADIO_IMAGE_POSITION
;
225 } else if (type
==WBTSwitch
) {
226 W_ResizeView(bPtr
->view
, DEFAULT_SWITCH_WIDTH
, DEFAULT_SWITCH_HEIGHT
);
227 WMSetButtonText(bPtr
, DEFAULT_SWITCH_TEXT
);
228 bPtr
->flags
.alignment
= DEFAULT_SWITCH_ALIGNMENT
;
229 bPtr
->flags
.imagePosition
= DEFAULT_SWITCH_IMAGE_POSITION
;
237 updateDisabledMask(WMButton
*bPtr
)
239 WMScreen
*scr
= WMWidgetScreen(bPtr
);
240 Display
*dpy
= scr
->display
;
245 bPtr
->dimage
->mask
= XCreatePixmap(dpy
, scr
->stipple
,
247 bPtr
->dimage
->height
, 1);
249 XSetForeground(dpy
, scr
->monoGC
, 0);
250 XFillRectangle(dpy
, bPtr
->dimage
->mask
, scr
->monoGC
, 0, 0,
251 bPtr
->dimage
->width
, bPtr
->dimage
->height
);
255 gcv
.stipple
= scr
->stipple
;
256 gcv
.fill_style
= FillStippled
;
257 gcv
.clip_mask
= bPtr
->image
->mask
;
258 gcv
.clip_x_origin
= 0;
259 gcv
.clip_y_origin
= 0;
261 XChangeGC(dpy
, scr
->monoGC
, GCForeground
|GCBackground
|GCStipple
262 |GCFillStyle
|GCClipMask
|GCClipXOrigin
|GCClipYOrigin
, &gcv
);
264 XFillRectangle(dpy
, bPtr
->dimage
->mask
, scr
->monoGC
, 0, 0,
265 bPtr
->dimage
->width
, bPtr
->dimage
->height
);
267 gcv
.fill_style
= FillSolid
;
268 gcv
.clip_mask
= None
;
269 XChangeGC(dpy
, scr
->monoGC
, GCFillStyle
|GCClipMask
, &gcv
);
275 WMSetButtonImage(WMButton
*bPtr
, WMPixmap
*image
)
277 if (bPtr
->image
!=NULL
)
278 WMReleasePixmap(bPtr
->image
);
279 bPtr
->image
= WMRetainPixmap(image
);
282 bPtr
->dimage
->pixmap
= None
;
283 WMReleasePixmap(bPtr
->dimage
);
288 bPtr
->dimage
= WMCreatePixmapFromXPixmaps(WMWidgetScreen(bPtr
),
290 image
->width
, image
->height
,
292 updateDisabledMask(bPtr
);
295 if (bPtr
->view
->flags
.realized
) {
302 WMSetButtonAltImage(WMButton
*bPtr
, WMPixmap
*image
)
304 if (bPtr
->altImage
!=NULL
)
305 WMReleasePixmap(bPtr
->altImage
);
306 bPtr
->altImage
= WMRetainPixmap(image
);
309 if (bPtr
->view
->flags
.realized
) {
316 WMSetButtonImagePosition(WMButton
*bPtr
, WMImagePosition position
)
318 bPtr
->flags
.imagePosition
= position
;
321 if (bPtr
->view
->flags
.realized
) {
330 WMSetButtonTextAlignment(WMButton
*bPtr
, WMAlignment alignment
)
332 bPtr
->flags
.alignment
= alignment
;
335 if (bPtr
->view
->flags
.realized
) {
341 WMSetButtonText(WMButton
*bPtr
, char *text
)
347 bPtr
->caption
= wstrdup(text
);
349 bPtr
->caption
= NULL
;
353 if (bPtr
->view
->flags
.realized
) {
360 WMSetButtonAltText(WMButton
*bPtr
, char *text
)
362 if (bPtr
->altCaption
)
363 free(bPtr
->altCaption
);
366 bPtr
->altCaption
= wstrdup(text
);
368 bPtr
->altCaption
= NULL
;
371 if (bPtr
->view
->flags
.realized
) {
378 WMSetButtonSelected(WMButton
*bPtr
, int isSelected
)
380 bPtr
->flags
.selected
= isSelected
;
382 if (bPtr
->view
->flags
.realized
) {
389 WMGetButtonSelected(WMButton
*bPtr
)
391 CHECK_CLASS(bPtr
, WC_Button
);
393 return bPtr
->flags
.selected
;
398 WMSetButtonBordered(WMButton
*bPtr
, int isBordered
)
400 bPtr
->flags
.bordered
= isBordered
;
402 if (bPtr
->view
->flags
.realized
) {
409 WMSetButtonFont(WMButton
*bPtr
, WMFont
*font
)
412 WMReleaseFont(bPtr
->font
);
414 bPtr
->font
= WMRetainFont(font
);
419 WMSetButtonEnabled(WMButton
*bPtr
, Bool flag
)
421 bPtr
->flags
.enabled
= flag
;
423 if (bPtr
->view
->flags
.mapped
) {
430 WMSetButtonTag(WMButton
*bPtr
, int tag
)
438 WMPerformButtonClick(WMButton
*bPtr
)
440 CHECK_CLASS(bPtr
, WC_Button
);
442 if (!bPtr
->flags
.enabled
)
445 bPtr
->flags
.pushed
= 1;
446 bPtr
->flags
.selected
= 1;
448 if (bPtr
->view
->flags
.mapped
) {
450 XFlush(WMScreenDisplay(WMWidgetScreen(bPtr
)));
454 if (bPtr
->groupIndex
>0) {
455 WMPostNotificationName(WMPushedRadioNotification
, bPtr
, NULL
);
459 (*bPtr
->action
)(bPtr
, bPtr
->clientData
);
461 bPtr
->flags
.pushed
= 0;
463 if (bPtr
->view
->flags
.mapped
)
470 WMSetButtonAction(WMButton
*bPtr
, WMAction
*action
, void *clientData
)
472 CHECK_CLASS(bPtr
, WC_Button
);
474 bPtr
->action
= action
;
476 bPtr
->clientData
= clientData
;
483 radioPushObserver(void *observerData
, WMNotification
*notification
)
485 WMButton
*bPtr
= (WMButton
*)observerData
;
486 WMButton
*pushedButton
= (WMButton
*)WMGetNotificationObject(notification
);
488 if (bPtr
!=pushedButton
&& pushedButton
->groupIndex
== bPtr
->groupIndex
489 && bPtr
->groupIndex
!=0) {
490 if (bPtr
->flags
.selected
) {
491 bPtr
->flags
.selected
= 0;
500 WMGroupButtons(WMButton
*bPtr
, WMButton
*newMember
)
502 static int tagIndex
= 0;
504 CHECK_CLASS(bPtr
, WC_Button
);
505 CHECK_CLASS(newMember
, WC_Button
);
507 if (!bPtr
->flags
.addedObserver
) {
508 WMAddNotificationObserver(radioPushObserver
, bPtr
,
509 WMPushedRadioNotification
, NULL
);
510 bPtr
->flags
.addedObserver
= 1;
512 if (!newMember
->flags
.addedObserver
) {
513 WMAddNotificationObserver(radioPushObserver
, newMember
,
514 WMPushedRadioNotification
, NULL
);
515 newMember
->flags
.addedObserver
= 1;
518 if (bPtr
->groupIndex
==0) {
519 bPtr
->groupIndex
= ++tagIndex
;
521 newMember
->groupIndex
= bPtr
->groupIndex
;
526 WMSetButtonContinuous(WMButton
*bPtr
, Bool flag
)
528 bPtr
->flags
.continuous
= flag
;
530 WMDeleteTimerHandler(bPtr
->timer
);
537 WMSetButtonPeriodicDelay(WMButton
*bPtr
, float delay
, float interval
)
539 bPtr
->periodicInterval
= interval
;
540 bPtr
->periodicDelay
= delay
;
546 paintButton(Button
*bPtr
)
548 W_Screen
*scrPtr
= bPtr
->view
->screen
;
557 caption
= bPtr
->caption
;
558 if (bPtr
->flags
.enabled
|| !bPtr
->dimage
)
561 image
= bPtr
->dimage
;
563 if (bPtr
->flags
.bordered
)
568 if (bPtr
->flags
.selected
) {
569 if (bPtr
->flags
.stateLight
)
570 gc
= WMColorGC(scrPtr
->white
);
572 if (bPtr
->flags
.stateChange
) {
573 if (bPtr
->altCaption
) {
574 caption
= bPtr
->altCaption
;
577 image
= bPtr
->altImage
;
580 if (bPtr
->flags
.statePush
&& bPtr
->flags
.bordered
) {
586 if (bPtr
->flags
.pushed
) {
587 if (bPtr
->flags
.pushIn
) {
591 if (bPtr
->flags
.pushLight
)
592 gc
= WMColorGC(scrPtr
->white
);
594 if (bPtr
->flags
.pushChange
) {
595 if (bPtr
->altCaption
) {
596 caption
= bPtr
->altCaption
;
599 image
= bPtr
->altImage
;
604 if (bPtr
->flags
.enabled
)
605 textGC
= WMColorGC(scrPtr
->black
);
607 textGC
= WMColorGC(scrPtr
->darkGray
);
609 W_PaintTextAndImage(bPtr
->view
, True
, textGC
,
610 (bPtr
->font
!=NULL
? bPtr
->font
: scrPtr
->normalFont
),
611 relief
, caption
, bPtr
->flags
.alignment
, image
,
612 bPtr
->flags
.imagePosition
, gc
, offset
);
618 handleEvents(XEvent
*event
, void *data
)
620 Button
*bPtr
= (Button
*)data
;
622 CHECK_CLASS(data
, WC_Button
);
625 switch (event
->type
) {
627 if (event
->xexpose
.count
!=0)
640 autoRepeat(void *data
)
642 Button
*bPtr
= (Button
*)data
;
644 if (bPtr
->action
&& bPtr
->flags
.pushed
)
645 (*bPtr
->action
)(bPtr
, bPtr
->clientData
);
647 bPtr
->timer
= WMAddTimerHandler((int)(bPtr
->periodicInterval
*1000),
653 handleActionEvents(XEvent
*event
, void *data
)
655 Button
*bPtr
= (Button
*)data
;
656 int doclick
= 0, dopaint
=0;
658 CHECK_CLASS(data
, WC_Button
);
660 if (!bPtr
->flags
.enabled
)
663 switch (event
->type
) {
665 if (bPtr
->groupIndex
== 0) {
666 bPtr
->flags
.pushed
= bPtr
->flags
.wasPushed
;
667 if (bPtr
->flags
.pushed
) {
668 bPtr
->flags
.selected
= !bPtr
->flags
.prevSelected
;
675 if (bPtr
->groupIndex
== 0) {
676 bPtr
->flags
.wasPushed
= bPtr
->flags
.pushed
;
677 if (bPtr
->flags
.pushed
) {
678 bPtr
->flags
.selected
= bPtr
->flags
.prevSelected
;
681 bPtr
->flags
.pushed
= 0;
686 if (event
->xbutton
.button
== Button1
) {
687 if (bPtr
->groupIndex
>0) {
688 if (!bPtr
->flags
.selected
)
690 bPtr
->flags
.pushed
= 1;
691 bPtr
->flags
.selected
= 1;
695 bPtr
->flags
.wasPushed
= 0;
696 bPtr
->flags
.pushed
= 1;
697 bPtr
->flags
.prevSelected
= bPtr
->flags
.selected
;
698 bPtr
->flags
.selected
= !bPtr
->flags
.selected
;
701 if (bPtr
->flags
.continuous
&& !bPtr
->timer
) {
702 bPtr
->timer
= WMAddTimerHandler((int)(bPtr
->periodicDelay
*1000),
709 if (event
->xbutton
.button
== Button1
) {
710 if (bPtr
->flags
.pushed
) {
711 if (bPtr
->groupIndex
==0)
714 if (bPtr
->flags
.springLoaded
) {
715 bPtr
->flags
.selected
= bPtr
->flags
.prevSelected
;
718 bPtr
->flags
.pushed
= 0;
721 WMDeleteTimerHandler(bPtr
->timer
);
731 if (bPtr
->flags
.selected
&& bPtr
->groupIndex
>0) {
732 WMPostNotificationName(WMPushedRadioNotification
, bPtr
, NULL
);
736 (*bPtr
->action
)(bPtr
, bPtr
->clientData
);
743 destroyButton(Button
*bPtr
)
745 if (bPtr
->flags
.addedObserver
) {
746 WMRemoveNotificationObserver(bPtr
);
750 WMDeleteTimerHandler(bPtr
->timer
);
753 WMReleaseFont(bPtr
->font
);
758 if (bPtr
->altCaption
)
759 free(bPtr
->altCaption
);
762 WMReleasePixmap(bPtr
->image
);
766 bPtr
->dimage
->pixmap
= None
;
768 WMReleasePixmap(bPtr
->dimage
);
771 WMReleasePixmap(bPtr
->altImage
);