Files
wmaker/WINGs/wpopupbutton.c
T

697 lines
18 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
#include "WINGsP.h"
typedef struct W_PopUpButton {
2009-08-20 00:59:40 +02:00
W_Class widgetClass;
WMView *view;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
void *clientData;
WMAction *action;
1998-09-29 22:36:29 +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
WMArray *items;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
short selectedItemIndex;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
short highlightedItem;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
WMView *menuView; /* override redirect popup menu */
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMHandlerID timer; /* for autoscroll */
1999-01-06 15:22:33 +00:00
2009-08-20 00:59:40 +02:00
/**/ int scrollStartY; /* for autoscroll */
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
struct {
unsigned int pullsDown:1;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
unsigned int configured:1;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
unsigned int insideMenu:1;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
unsigned int enabled:1;
1999-01-06 15:22:33 +00:00
2009-08-20 00:59:40 +02:00
} flags;
1998-09-29 22:36:29 +00:00
} PopUpButton;
#define MENU_BLINK_DELAY 60000
#define MENU_BLINK_COUNT 2
1999-03-30 22:14:17 +00:00
#define SCROLL_DELAY 10
1999-01-06 15:22:33 +00:00
1998-09-29 22:36:29 +00:00
#define DEFAULT_WIDTH 60
#define DEFAULT_HEIGHT 20
#define DEFAULT_CAPTION ""
2009-08-20 00:59:40 +02:00
static void destroyPopUpButton(PopUpButton * bPtr);
static void paintPopUpButton(PopUpButton * bPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data);
static void handleActionEvents(XEvent * event, void *data);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static void resizeMenu(PopUpButton * bPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMPopUpButton *WMCreatePopUpButton(WMWidget * parent)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
PopUpButton *bPtr;
W_Screen *scr = W_VIEW(parent)->screen;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr = wmalloc(sizeof(PopUpButton));
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
bPtr->widgetClass = WC_PopUpButton;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr->view = W_CreateView(W_VIEW(parent));
if (!bPtr->view) {
wfree(bPtr);
return NULL;
}
bPtr->view->self = bPtr;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
WMCreateEventHandler(bPtr->view, ExposureMask | StructureNotifyMask
| ClientMessageMask, handleEvents, bPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
W_ResizeView(bPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
bPtr->caption = wstrdup(DEFAULT_CAPTION);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMCreateEventHandler(bPtr->view, ButtonPressMask | ButtonReleaseMask, handleActionEvents, bPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
bPtr->flags.enabled = 1;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
bPtr->items = WMCreateArrayWithDestructor(4, (WMFreeDataProc *) WMDestroyMenuItem);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr->selectedItemIndex = -1;
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
bPtr->menuView = W_CreateUnmanagedTopView(scr);
2000-01-05 22:02:22 +00:00
2009-08-20 00:59:40 +02:00
W_ResizeView(bPtr->menuView, bPtr->view->size.width, 1);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMCreateEventHandler(bPtr->menuView, ButtonPressMask | ButtonReleaseMask
| EnterWindowMask | LeaveWindowMask | ButtonMotionMask
| ExposureMask, handleActionEvents, bPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
return bPtr;
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetPopUpButtonAction(WMPopUpButton * bPtr, WMAction * action, void *clientData)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
CHECK_CLASS(bPtr, WC_PopUpButton);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr->action = action;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr->clientData = clientData;
1998-09-29 22:36:29 +00:00
}
WMMenuItem *WMAddPopUpButtonItem(WMPopUpButton * bPtr, const char *title)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMMenuItem *item;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
CHECK_CLASS(bPtr, WC_PopUpButton);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
item = WMCreateMenuItem();
WMSetMenuItemTitle(item, title);
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
WMAddToArray(bPtr->items, item);
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->menuView && bPtr->menuView->flags.realized)
resizeMenu(bPtr);
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
return item;
1998-09-29 22:36:29 +00:00
}
WMMenuItem *WMInsertPopUpButtonItem(WMPopUpButton * bPtr, int index, const char *title)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMMenuItem *item;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
CHECK_CLASS(bPtr, WC_PopUpButton);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
item = WMCreateMenuItem();
WMSetMenuItemTitle(item, title);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMInsertInArray(bPtr->items, index, item);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
/* if there is an selected item, update it's index to match the new
* position */
if (index < bPtr->selectedItemIndex)
bPtr->selectedItemIndex++;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->menuView && bPtr->menuView->flags.realized)
resizeMenu(bPtr);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
return item;
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMRemovePopUpButtonItem(WMPopUpButton * bPtr, int index)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
CHECK_CLASS(bPtr, WC_PopUpButton);
wassertr(index >= 0 && index < WMGetArrayItemCount(bPtr->items));
WMDeleteFromArray(bPtr->items, index);
if (bPtr->selectedItemIndex >= 0 && !bPtr->flags.pullsDown) {
if (index < bPtr->selectedItemIndex)
bPtr->selectedItemIndex--;
else if (index == bPtr->selectedItemIndex) {
/* reselect first item if the removed item is the
* selected one */
bPtr->selectedItemIndex = 0;
if (bPtr->view->flags.mapped)
paintPopUpButton(bPtr);
}
}
if (bPtr->menuView && bPtr->menuView->flags.realized)
resizeMenu(bPtr);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetPopUpButtonEnabled(WMPopUpButton * bPtr, Bool flag)
1999-01-06 15:22:33 +00:00
{
2009-08-20 00:59:40 +02:00
bPtr->flags.enabled = ((flag == 0) ? 0 : 1);
if (bPtr->view->flags.mapped)
paintPopUpButton(bPtr);
1999-01-06 15:22:33 +00:00
}
2009-08-20 00:59:40 +02:00
Bool WMGetPopUpButtonEnabled(WMPopUpButton * bPtr)
{
2009-08-20 00:59:40 +02:00
return bPtr->flags.enabled;
}
2009-08-20 00:59:40 +02:00
void WMSetPopUpButtonSelectedItem(WMPopUpButton * bPtr, int index)
{
2009-08-20 00:59:40 +02:00
wassertr(index < WMGetArrayItemCount(bPtr->items));
2009-08-20 00:59:40 +02:00
/* if (index >= WMGetArrayCount(bPtr->items))
index = -1; */
2009-08-20 00:59:40 +02:00
bPtr->selectedItemIndex = index;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->view->flags.mapped)
paintPopUpButton(bPtr);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
int WMGetPopUpButtonSelectedItem(WMPopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (!bPtr->flags.pullsDown && bPtr->selectedItemIndex < 0)
return -1;
else
return bPtr->selectedItemIndex;
1998-09-29 22:36:29 +00:00
}
void WMSetPopUpButtonText(WMPopUpButton * bPtr, const char *text)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (bPtr->caption)
wfree(bPtr->caption);
if (text)
bPtr->caption = wstrdup(text);
else
bPtr->caption = NULL;
if (bPtr->view->flags.realized) {
if (bPtr->flags.pullsDown || bPtr->selectedItemIndex < 0) {
paintPopUpButton(bPtr);
}
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void WMSetPopUpButtonItemEnabled(WMPopUpButton * bPtr, int index, Bool flag)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMSetMenuItemEnabled(WMGetFromArray(bPtr->items, index), (flag ? 1 : 0));
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
Bool WMGetPopUpButtonItemEnabled(WMPopUpButton * bPtr, int index)
{
2009-08-20 00:59:40 +02:00
return WMGetMenuItemEnabled(WMGetFromArray(bPtr->items, index));
}
2009-08-20 00:59:40 +02:00
void WMSetPopUpButtonPullsDown(WMPopUpButton * bPtr, Bool flag)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
bPtr->flags.pullsDown = ((flag == 0) ? 0 : 1);
if (flag) {
bPtr->selectedItemIndex = -1;
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->view->flags.mapped)
paintPopUpButton(bPtr);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
int WMGetPopUpButtonNumberOfItems(WMPopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
return WMGetArrayItemCount(bPtr->items);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
char *WMGetPopUpButtonItem(WMPopUpButton * bPtr, int index)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (index >= WMGetArrayItemCount(bPtr->items) || index < 0)
return NULL;
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
return WMGetMenuItemTitle(WMGetFromArray(bPtr->items, index));
1999-10-20 03:25:06 +00:00
}
2009-08-20 00:59:40 +02:00
WMMenuItem *WMGetPopUpButtonMenuItem(WMPopUpButton * bPtr, int index)
{
return WMGetFromArray(bPtr->items, index);
}
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
static void paintPopUpButton(PopUpButton * bPtr)
1999-10-20 03:25:06 +00:00
{
2009-08-20 00:59:40 +02:00
W_Screen *scr = bPtr->view->screen;
char *caption;
Pixmap pixmap;
if (bPtr->flags.pullsDown) {
caption = bPtr->caption;
} else {
if (bPtr->selectedItemIndex < 0) {
/* if no item selected, show the caption */
caption = bPtr->caption;
} else {
caption = WMGetPopUpButtonItem(bPtr, bPtr->selectedItemIndex);
}
}
pixmap = XCreatePixmap(scr->display, bPtr->view->window,
bPtr->view->size.width, bPtr->view->size.height, scr->depth);
XFillRectangle(scr->display, pixmap, WMColorGC(scr->gray), 0, 0,
bPtr->view->size.width, bPtr->view->size.height);
W_DrawRelief(scr, pixmap, 0, 0, bPtr->view->size.width, bPtr->view->size.height, WRRaised);
if (caption) {
W_PaintText(bPtr->view, pixmap, scr->normalFont, 6,
(bPtr->view->size.height - WMFontHeight(scr->normalFont)) / 2,
bPtr->view->size.width, WALeft,
bPtr->flags.enabled ? scr->black : scr->darkGray, False, caption, strlen(caption));
}
if (bPtr->flags.pullsDown) {
XCopyArea(scr->display, scr->pullDownIndicator->pixmap,
pixmap, scr->copyGC, 0, 0, scr->pullDownIndicator->width,
scr->pullDownIndicator->height,
bPtr->view->size.width - scr->pullDownIndicator->width - 4,
(bPtr->view->size.height - scr->pullDownIndicator->height) / 2);
} else {
int x, y;
x = bPtr->view->size.width - scr->popUpIndicator->width - 4;
y = (bPtr->view->size.height - scr->popUpIndicator->height) / 2;
XSetClipOrigin(scr->display, scr->clipGC, x, y);
XSetClipMask(scr->display, scr->clipGC, scr->popUpIndicator->mask);
XCopyArea(scr->display, scr->popUpIndicator->pixmap, pixmap,
scr->clipGC, 0, 0, scr->popUpIndicator->width, scr->popUpIndicator->height, x, y);
}
XCopyArea(scr->display, pixmap, bPtr->view->window, scr->copyGC, 0, 0,
bPtr->view->size.width, bPtr->view->size.height, 0, 0);
XFreePixmap(scr->display, pixmap);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data)
{
PopUpButton *bPtr = (PopUpButton *) data;
CHECK_CLASS(data, WC_PopUpButton);
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;
paintPopUpButton(bPtr);
break;
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
case DestroyNotify:
destroyPopUpButton(bPtr);
break;
}
}
static void paintMenuEntry(PopUpButton * bPtr, int index, int highlight)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
W_Screen *scr = bPtr->view->screen;
int yo;
2011-03-24 16:07:20 +01:00
int width, itemHeight, itemCount;
2009-08-20 00:59:40 +02:00
char *title;
itemCount = WMGetArrayItemCount(bPtr->items);
if (index < 0 || index >= itemCount)
return;
itemHeight = bPtr->view->size.height;
width = bPtr->view->size.width;
yo = (itemHeight - WMFontHeight(scr->normalFont)) / 2;
if (!highlight) {
XClearArea(scr->display, bPtr->menuView->window, 0, index * itemHeight, width, itemHeight, False);
return;
}
XFillRectangle(scr->display, bPtr->menuView->window, WMColorGC(scr->white),
1, index * itemHeight + 1, width - 3, itemHeight - 3);
title = WMGetPopUpButtonItem(bPtr, index);
W_DrawRelief(scr, bPtr->menuView->window, 0, index * itemHeight, width, itemHeight, WRRaised);
W_PaintText(bPtr->menuView, bPtr->menuView->window, scr->normalFont, 6,
index * itemHeight + yo, width, WALeft, scr->black, False, title, strlen(title));
if (!bPtr->flags.pullsDown && index == bPtr->selectedItemIndex) {
XCopyArea(scr->display, scr->popUpIndicator->pixmap,
bPtr->menuView->window, scr->copyGC, 0, 0,
scr->popUpIndicator->width, scr->popUpIndicator->height,
width - scr->popUpIndicator->width - 4,
index * itemHeight + (itemHeight - scr->popUpIndicator->height) / 2);
}
1998-09-29 22:36:29 +00:00
}
static Pixmap makeMenuPixmap(PopUpButton * bPtr)
2009-08-20 00:59:40 +02:00
{
Pixmap pixmap;
W_Screen *scr = bPtr->view->screen;
WMMenuItem *item;
WMArrayIterator iter;
int yo, i;
int width, height, itemHeight;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
itemHeight = bPtr->view->size.height;
width = bPtr->view->size.width;
height = itemHeight * WMGetArrayItemCount(bPtr->items);
yo = (itemHeight - WMFontHeight(scr->normalFont)) / 2;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
pixmap = XCreatePixmap(scr->display, bPtr->view->window, width, height, scr->depth);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
XFillRectangle(scr->display, pixmap, WMColorGC(scr->gray), 0, 0, width, height);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
i = 0;
WM_ITERATE_ARRAY(bPtr->items, item, iter) {
WMColor *color;
char *text;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
text = WMGetMenuItemTitle(item);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
W_DrawRelief(scr, pixmap, 0, i * itemHeight, width, itemHeight, WRRaised);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (!WMGetMenuItemEnabled(item))
color = scr->darkGray;
else
color = scr->black;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
W_PaintText(bPtr->menuView, pixmap, scr->normalFont, 6,
i * itemHeight + yo, width, WALeft, color, False, text, strlen(text));
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (!bPtr->flags.pullsDown && i == bPtr->selectedItemIndex) {
XCopyArea(scr->display, scr->popUpIndicator->pixmap, pixmap,
scr->copyGC, 0, 0, scr->popUpIndicator->width,
scr->popUpIndicator->height,
width - scr->popUpIndicator->width - 4,
i * itemHeight + (itemHeight - scr->popUpIndicator->height) / 2);
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
i++;
}
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
return pixmap;
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void resizeMenu(PopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
int height;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
height = WMGetArrayItemCount(bPtr->items) * bPtr->view->size.height;
if (height > 0)
W_ResizeView(bPtr->menuView, bPtr->view->size.width, height);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void popUpMenu(PopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
W_Screen *scr = bPtr->view->screen;
Window dummyW;
int x, y;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (!bPtr->flags.enabled)
return;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (!bPtr->menuView->flags.realized) {
W_RealizeView(bPtr->menuView);
resizeMenu(bPtr);
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (WMGetArrayItemCount(bPtr->items) < 1)
return;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
XTranslateCoordinates(scr->display, bPtr->view->window, scr->rootWin, 0, 0, &x, &y, &dummyW);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->flags.pullsDown) {
y += bPtr->view->size.height;
} else {
y -= bPtr->view->size.height * bPtr->selectedItemIndex;
}
W_MoveView(bPtr->menuView, x, y);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
XSetWindowBackgroundPixmap(scr->display, bPtr->menuView->window, makeMenuPixmap(bPtr));
XClearWindow(scr->display, bPtr->menuView->window);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (W_VIEW_WIDTH(bPtr->menuView) != W_VIEW_WIDTH(bPtr->view))
resizeMenu(bPtr);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
W_MapView(bPtr->menuView);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
bPtr->highlightedItem = 0;
if (!bPtr->flags.pullsDown && bPtr->selectedItemIndex < 0)
paintMenuEntry(bPtr, bPtr->selectedItemIndex, True);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void popDownMenu(PopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
W_UnmapView(bPtr->menuView);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void autoScroll(void *data)
1999-01-06 15:22:33 +00:00
{
2009-08-20 00:59:40 +02:00
PopUpButton *bPtr = (PopUpButton *) data;
int scrHeight = WMWidgetScreen(bPtr)->rootView->size.height;
int repeat = 0;
int dy = 0;
if (bPtr->scrollStartY >= scrHeight - 1
&& bPtr->menuView->pos.y + bPtr->menuView->size.height >= scrHeight - 1) {
repeat = 1;
if (bPtr->menuView->pos.y + bPtr->menuView->size.height - 5 <= scrHeight - 1) {
dy = scrHeight - 1 - (bPtr->menuView->pos.y + bPtr->menuView->size.height);
} else
dy = -5;
} else if (bPtr->scrollStartY <= 1 && bPtr->menuView->pos.y < 1) {
repeat = 1;
if (bPtr->menuView->pos.y + 5 > 1)
dy = 1 - bPtr->menuView->pos.y;
else
dy = 5;
}
if (repeat) {
int oldItem;
W_MoveView(bPtr->menuView, bPtr->menuView->pos.x, bPtr->menuView->pos.y + dy);
oldItem = bPtr->highlightedItem;
bPtr->highlightedItem = (bPtr->scrollStartY - bPtr->menuView->pos.y)
/ bPtr->view->size.height;
if (oldItem != bPtr->highlightedItem) {
WMMenuItem *item;
paintMenuEntry(bPtr, oldItem, False);
if (bPtr->highlightedItem >= 0 && bPtr->highlightedItem < WMGetArrayItemCount(bPtr->items)) {
item = WMGetPopUpButtonMenuItem(bPtr, bPtr->highlightedItem);
paintMenuEntry(bPtr, bPtr->highlightedItem, WMGetMenuItemEnabled(item));
} else {
bPtr->highlightedItem = -1;
}
}
bPtr->timer = WMAddTimerHandler(SCROLL_DELAY, autoScroll, bPtr);
} else {
bPtr->timer = NULL;
}
1999-01-06 15:22:33 +00:00
}
2009-08-20 00:59:40 +02:00
static void wheelScrollUp(PopUpButton * bPtr)
2000-04-13 21:24:28 +00:00
{
2009-08-20 00:59:40 +02:00
int testIndex = bPtr->selectedItemIndex - 1;
while (testIndex >= 0 && !WMGetPopUpButtonItemEnabled(bPtr, testIndex))
testIndex--;
if (testIndex != -1) {
WMSetPopUpButtonSelectedItem(bPtr, testIndex);
if (bPtr->action)
(*bPtr->action) (bPtr, bPtr->clientData);
}
2000-04-13 21:24:28 +00:00
}
2009-08-20 00:59:40 +02:00
static void wheelScrollDown(PopUpButton * bPtr)
2000-04-13 21:24:28 +00:00
{
2009-08-20 00:59:40 +02:00
int itemCount = WMGetArrayItemCount(bPtr->items);
int testIndex = bPtr->selectedItemIndex + 1;
while (testIndex < itemCount && !WMGetPopUpButtonItemEnabled(bPtr, testIndex))
testIndex++;
if (testIndex != itemCount) {
WMSetPopUpButtonSelectedItem(bPtr, testIndex);
if (bPtr->action)
(*bPtr->action) (bPtr, bPtr->clientData);
}
2000-04-13 21:24:28 +00:00
}
2009-08-20 00:59:40 +02:00
static void handleActionEvents(XEvent * event, void *data)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
PopUpButton *bPtr = (PopUpButton *) data;
int oldItem;
int scrHeight = WMWidgetScreen(bPtr)->rootView->size.height;
CHECK_CLASS(data, WC_PopUpButton);
if (WMGetArrayItemCount(bPtr->items) < 1)
return;
switch (event->type) {
/* called for menuView */
case Expose:
paintMenuEntry(bPtr, bPtr->highlightedItem, True);
break;
case LeaveNotify:
bPtr->flags.insideMenu = 0;
if (bPtr->menuView->flags.mapped)
paintMenuEntry(bPtr, bPtr->highlightedItem, False);
bPtr->highlightedItem = -1;
break;
case EnterNotify:
bPtr->flags.insideMenu = 1;
break;
case MotionNotify:
if (bPtr->flags.insideMenu) {
oldItem = bPtr->highlightedItem;
bPtr->highlightedItem = event->xmotion.y / bPtr->view->size.height;
if (oldItem != bPtr->highlightedItem) {
WMMenuItem *item;
paintMenuEntry(bPtr, oldItem, False);
if (bPtr->highlightedItem >= 0 &&
bPtr->highlightedItem < WMGetArrayItemCount(bPtr->items)) {
item = WMGetPopUpButtonMenuItem(bPtr, bPtr->highlightedItem);
paintMenuEntry(bPtr, bPtr->highlightedItem, WMGetMenuItemEnabled(item));
} else {
bPtr->highlightedItem = -1;
}
}
if (event->xmotion.y_root >= scrHeight - 1 || event->xmotion.y_root <= 1) {
bPtr->scrollStartY = event->xmotion.y_root;
if (!bPtr->timer)
autoScroll(bPtr);
} else if (bPtr->timer) {
WMDeleteTimerHandler(bPtr->timer);
bPtr->timer = NULL;
}
}
break;
/* called for bPtr->view */
case ButtonPress:
if (!bPtr->flags.enabled)
break;
if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
if (!bPtr->menuView->flags.mapped && !bPtr->flags.pullsDown) {
wheelScrollUp(bPtr);
}
break;
} else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
if (!bPtr->menuView->flags.mapped && !bPtr->flags.pullsDown) {
wheelScrollDown(bPtr);
}
break;
}
popUpMenu(bPtr);
if (!bPtr->flags.pullsDown) {
bPtr->highlightedItem = bPtr->selectedItemIndex;
bPtr->flags.insideMenu = 1;
} else {
bPtr->highlightedItem = -1;
bPtr->flags.insideMenu = 0;
}
XGrabPointer(bPtr->view->screen->display, bPtr->menuView->window,
False, ButtonReleaseMask | ButtonMotionMask | EnterWindowMask
| LeaveWindowMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
break;
case ButtonRelease:
if (event->xbutton.button == WINGsConfiguration.mouseWheelUp ||
event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
break;
}
XUngrabPointer(bPtr->view->screen->display, event->xbutton.time);
if (!bPtr->flags.pullsDown)
popDownMenu(bPtr);
if (bPtr->timer) {
WMDeleteTimerHandler(bPtr->timer);
bPtr->timer = NULL;
}
if (bPtr->flags.insideMenu && bPtr->highlightedItem >= 0) {
WMMenuItem *item;
item = WMGetPopUpButtonMenuItem(bPtr, bPtr->highlightedItem);
if (WMGetMenuItemEnabled(item)) {
int i;
WMSetPopUpButtonSelectedItem(bPtr, bPtr->highlightedItem);
if (bPtr->flags.pullsDown) {
for (i = 0; i < MENU_BLINK_COUNT; i++) {
paintMenuEntry(bPtr, bPtr->highlightedItem, False);
XSync(bPtr->view->screen->display, 0);
wusleep(MENU_BLINK_DELAY);
paintMenuEntry(bPtr, bPtr->highlightedItem, True);
XSync(bPtr->view->screen->display, 0);
wusleep(MENU_BLINK_DELAY);
}
}
paintMenuEntry(bPtr, bPtr->highlightedItem, False);
popDownMenu(bPtr);
if (bPtr->action)
(*bPtr->action) (bPtr, bPtr->clientData);
}
}
if (bPtr->menuView->flags.mapped)
popDownMenu(bPtr);
break;
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void destroyPopUpButton(PopUpButton * bPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (bPtr->timer) {
WMDeleteTimerHandler(bPtr->timer);
}
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
WMFreeArray(bPtr->items);
1999-10-20 03:25:06 +00:00
2009-08-20 00:59:40 +02:00
if (bPtr->caption)
wfree(bPtr->caption);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
/* have to destroy explicitly because the popup is a toplevel */
W_DestroyView(bPtr->menuView);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
wfree(bPtr);
1998-09-29 22:36:29 +00:00
}