doc: Add info about user icons directory to manpage.
[wmaker-crm.git] / WPrefs.app / editmenu.c
blob7a9b29b98b68c9eaaf9331efc2c66738928e4f05
1 /* editmenu.c - editable menus
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <WINGs/WINGsP.h>
23 #include <WINGs/WUtil.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <assert.h>
27 #include <ctype.h>
29 #include "editmenu.h"
31 typedef struct W_EditMenuItem {
32 W_Class widgetClass;
33 WMView *view;
35 struct W_EditMenu *parent;
37 char *label;
38 WMPixmap *pixmap; /* pixmap to show at left */
40 void *data;
41 WMCallback *destroyData;
43 struct W_EditMenu *submenu; /* if it's a cascade, NULL otherwise */
45 struct {
46 unsigned isTitle:1;
47 unsigned isHighlighted:1;
48 } flags;
49 } EditMenuItem;
51 typedef struct W_EditMenu {
52 W_Class widgetClass;
53 WMView *view;
55 struct W_EditMenu *parent;
57 WMArray *items; /* EditMenuItem */
59 EditMenuItem *selectedItem;
61 WMTextField *tfield;
63 WMButton *closeB;
65 int titleHeight;
66 int itemHeight;
68 WEditMenuDelegate *delegate;
70 WMTextFieldDelegate *tdelegate;
72 /* item dragging */
73 int draggedItem;
74 int dragX, dragY;
76 /* only for non-standalone menu */
77 WMSize maxSize;
78 WMSize minSize;
80 struct {
81 unsigned standalone:1;
82 unsigned isTitled:1;
84 unsigned acceptsDrop:1;
85 unsigned isFactory:1;
86 unsigned isSelectable:1;
87 unsigned isEditable:1;
89 unsigned isTornOff:1;
91 unsigned isDragging:1;
92 unsigned isEditing:1;
94 unsigned wasMapped:1;
95 } flags;
96 } EditMenu;
98 /******************** WEditMenuItem ********************/
100 static void destroyEditMenuItem(WEditMenuItem * iPtr);
101 static void paintEditMenuItem(WEditMenuItem * iPtr);
102 static void handleItemEvents(XEvent * event, void *data);
104 static void handleItemClick(XEvent * event, void *data);
106 static W_Class EditMenuItemClass = 0;
108 W_Class InitEditMenuItem(void)
110 /* register our widget with WINGs and get our widget class ID */
111 if (!EditMenuItemClass) {
112 EditMenuItemClass = W_RegisterUserWidget();
115 return EditMenuItemClass;
118 WEditMenuItem *WCreateEditMenuItem(WMWidget * parent, const char *title, Bool isTitle)
120 WEditMenuItem *iPtr;
121 WMScreen *scr = WMWidgetScreen(parent);
123 if (!EditMenuItemClass)
124 InitEditMenuItem();
126 iPtr = wmalloc(sizeof(WEditMenuItem));
128 iPtr->widgetClass = EditMenuItemClass;
130 iPtr->view = W_CreateView(W_VIEW(parent));
131 if (!iPtr->view) {
132 wfree(iPtr);
133 return NULL;
135 iPtr->view->self = iPtr;
137 iPtr->parent = parent;
139 iPtr->label = wstrdup(title);
141 iPtr->flags.isTitle = isTitle;
143 if (isTitle) {
144 WMSetWidgetBackgroundColor(iPtr, WMBlackColor(scr));
147 WMCreateEventHandler(iPtr->view, ExposureMask | StructureNotifyMask, handleItemEvents, iPtr);
149 WMCreateEventHandler(iPtr->view, ButtonPressMask | ButtonReleaseMask
150 | ButtonMotionMask, handleItemClick, iPtr);
152 return iPtr;
155 char *WGetEditMenuItemTitle(WEditMenuItem * item)
157 return item->label;
160 void *WGetEditMenuItemData(WEditMenuItem * item)
162 return item->data;
165 void WSetEditMenuItemData(WEditMenuItem * item, void *data, WMCallback * destroyer)
167 item->data = data;
168 item->destroyData = destroyer;
171 void WSetEditMenuItemImage(WEditMenuItem * item, WMPixmap * pixmap)
173 if (item->pixmap)
174 WMReleasePixmap(item->pixmap);
175 item->pixmap = WMRetainPixmap(pixmap);
178 static void paintEditMenuItem(WEditMenuItem * iPtr)
180 WMScreen *scr = WMWidgetScreen(iPtr);
181 WMColor *color;
182 Window win = W_VIEW(iPtr)->window;
183 int w = W_VIEW(iPtr)->size.width;
184 int h = W_VIEW(iPtr)->size.height;
185 WMFont *font = (iPtr->flags.isTitle ? scr->boldFont : scr->normalFont);
187 if (!iPtr->view->flags.realized)
188 return;
190 color = scr->black;
191 if (iPtr->flags.isTitle && !iPtr->flags.isHighlighted) {
192 color = scr->white;
195 XClearWindow(scr->display, win);
197 W_DrawRelief(scr, win, 0, 0, w + 1, h, WRRaised);
199 WMDrawString(scr, win, color, font, 5, 3, iPtr->label, strlen(iPtr->label));
201 if (iPtr->pixmap) {
202 WMSize size = WMGetPixmapSize(iPtr->pixmap);
204 WMDrawPixmap(iPtr->pixmap, win, w - size.width - 5, (h - size.height) / 2);
205 } else if (iPtr->submenu) {
206 /* draw the cascade indicator */
207 XDrawLine(scr->display, win, WMColorGC(scr->darkGray), w - 11, 6, w - 6, h / 2 - 1);
208 XDrawLine(scr->display, win, WMColorGC(scr->white), w - 11, h - 8, w - 6, h / 2 - 1);
209 XDrawLine(scr->display, win, WMColorGC(scr->black), w - 12, 6, w - 12, h - 8);
213 static void highlightItem(WEditMenuItem * iPtr, Bool high)
215 if (iPtr->flags.isTitle)
216 return;
218 iPtr->flags.isHighlighted = high;
220 if (high) {
221 WMSetWidgetBackgroundColor(iPtr, WMWhiteColor(WMWidgetScreen(iPtr)));
222 } else {
223 if (!iPtr->flags.isTitle) {
224 WMSetWidgetBackgroundColor(iPtr, WMGrayColor(WMWidgetScreen(iPtr)));
225 } else {
226 WMSetWidgetBackgroundColor(iPtr, WMBlackColor(WMWidgetScreen(iPtr)));
231 static int getItemTextWidth(WEditMenuItem * iPtr)
233 WMScreen *scr = WMWidgetScreen(iPtr);
235 if (iPtr->flags.isTitle) {
236 return WMWidthOfString(scr->boldFont, iPtr->label, strlen(iPtr->label));
237 } else {
238 return WMWidthOfString(scr->normalFont, iPtr->label, strlen(iPtr->label));
242 static void handleItemEvents(XEvent * event, void *data)
244 WEditMenuItem *iPtr = (WEditMenuItem *) data;
246 switch (event->type) {
247 case Expose:
248 if (event->xexpose.count != 0)
249 break;
250 paintEditMenuItem(iPtr);
251 break;
253 case DestroyNotify:
254 destroyEditMenuItem(iPtr);
255 break;
259 static void destroyEditMenuItem(WEditMenuItem * iPtr)
261 if (iPtr->label)
262 wfree(iPtr->label);
263 if (iPtr->data && iPtr->destroyData)
264 (*iPtr->destroyData) (iPtr->data);
265 if (iPtr->submenu)
266 WMDestroyWidget(iPtr->submenu);
268 wfree(iPtr);
271 /******************** WEditMenu *******************/
273 static void destroyEditMenu(WEditMenu * mPtr);
275 static void updateMenuContents(WEditMenu * mPtr);
277 static void handleEvents(XEvent * event, void *data);
279 static void editItemLabel(WEditMenuItem * item);
281 static void stopEditItem(WEditMenu * menu, Bool apply);
283 static void deselectItem(WEditMenu * menu);
285 static W_Class EditMenuClass = 0;
287 W_Class InitEditMenu(void)
289 /* register our widget with WINGs and get our widget class ID */
290 if (!EditMenuClass) {
292 EditMenuClass = W_RegisterUserWidget();
295 return EditMenuClass;
298 typedef struct {
299 int flags;
300 int window_style;
301 int window_level;
302 int reserved;
303 Pixmap miniaturize_pixmap; /* pixmap for miniaturize button */
304 Pixmap close_pixmap; /* pixmap for close button */
305 Pixmap miniaturize_mask; /* miniaturize pixmap mask */
306 Pixmap close_mask; /* close pixmap mask */
307 int extra_flags;
308 } GNUstepWMAttributes;
310 #define GSWindowStyleAttr (1<<0)
311 #define GSWindowLevelAttr (1<<1)
313 static void writeGNUstepWMAttr(WMScreen * scr, Window window, GNUstepWMAttributes * attr)
315 unsigned long data[9];
317 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
318 data[0] = attr->flags;
319 data[1] = attr->window_style;
320 data[2] = attr->window_level;
321 data[3] = 0; /* reserved */
322 /* The X protocol says XIDs are 32bit */
323 data[4] = attr->miniaturize_pixmap;
324 data[5] = attr->close_pixmap;
325 data[6] = attr->miniaturize_mask;
326 data[7] = attr->close_mask;
327 data[8] = attr->extra_flags;
328 XChangeProperty(scr->display, window, scr->attribsAtom, scr->attribsAtom,
329 32, PropModeReplace, (unsigned char *)data, 9);
332 static void realizeObserver(void *self, WMNotification * not)
334 WEditMenu *menu = (WEditMenu *) self;
335 GNUstepWMAttributes attribs;
337 /* Parameter not used, but tell the compiler that it is ok */
338 (void) not;
340 memset(&attribs, 0, sizeof(GNUstepWMAttributes));
341 attribs.flags = GSWindowStyleAttr | GSWindowLevelAttr;
342 attribs.window_style = WMBorderlessWindowMask;
343 attribs.window_level = WMSubmenuWindowLevel;
345 writeGNUstepWMAttr(WMWidgetScreen(menu), menu->view->window, &attribs);
348 static void itemSelectObserver(void *self, WMNotification * notif)
350 WEditMenu *menu = (WEditMenu *) self;
351 WEditMenu *rmenu;
353 rmenu = (WEditMenu *) WMGetNotificationObject(notif);
354 /* check whether rmenu is from the same hierarchy of menu? */
356 if (rmenu == menu) {
357 return;
360 if (menu->selectedItem) {
361 if (!menu->selectedItem->submenu)
362 deselectItem(menu);
363 if (menu->flags.isEditing)
364 stopEditItem(menu, False);
368 static WEditMenu *makeEditMenu(WMScreen * scr, WMWidget * parent, const char *title)
370 WEditMenu *mPtr;
371 WEditMenuItem *item;
373 if (!EditMenuClass)
374 InitEditMenu();
376 mPtr = wmalloc(sizeof(WEditMenu));
378 mPtr->widgetClass = EditMenuClass;
380 if (parent) {
381 mPtr->view = W_CreateView(W_VIEW(parent));
382 mPtr->flags.standalone = 0;
383 } else {
384 mPtr->view = W_CreateTopView(scr);
385 mPtr->flags.standalone = 1;
387 if (!mPtr->view) {
388 wfree(mPtr);
389 return NULL;
391 mPtr->view->self = mPtr;
393 mPtr->flags.isSelectable = 1;
394 mPtr->flags.isEditable = 1;
396 W_SetViewBackgroundColor(mPtr->view, scr->darkGray);
398 WMAddNotificationObserver(realizeObserver, mPtr, WMViewRealizedNotification, mPtr->view);
400 WMAddNotificationObserver(itemSelectObserver, mPtr, "EditMenuItemSelected", NULL);
402 mPtr->items = WMCreateArray(4);
404 WMCreateEventHandler(mPtr->view, ExposureMask | StructureNotifyMask, handleEvents, mPtr);
406 if (title != NULL) {
407 item = WCreateEditMenuItem(mPtr, title, True);
409 WMMapWidget(item);
410 WMAddToArray(mPtr->items, item);
412 mPtr->flags.isTitled = 1;
415 mPtr->itemHeight = WMFontHeight(scr->normalFont) + 6;
416 mPtr->titleHeight = WMFontHeight(scr->boldFont) + 8;
418 updateMenuContents(mPtr);
420 return mPtr;
423 WEditMenu *WCreateEditMenu(WMScreen * scr, const char *title)
425 return makeEditMenu(scr, NULL, title);
428 WEditMenu *WCreateEditMenuPad(WMWidget * parent)
430 return makeEditMenu(WMWidgetScreen(parent), parent, NULL);
433 void WSetEditMenuDelegate(WEditMenu * mPtr, WEditMenuDelegate * delegate)
435 mPtr->delegate = delegate;
438 WEditMenuItem *WInsertMenuItemWithTitle(WEditMenu * mPtr, int index, const char *title)
440 WEditMenuItem *item;
442 item = WCreateEditMenuItem(mPtr, title, False);
444 WMMapWidget(item);
446 if (index >= WMGetArrayItemCount(mPtr->items)) {
447 WMAddToArray(mPtr->items, item);
448 } else {
449 if (index < 0)
450 index = 0;
451 if (mPtr->flags.isTitled)
452 index++;
453 WMInsertInArray(mPtr->items, index, item);
456 updateMenuContents(mPtr);
458 return item;
461 WEditMenuItem *WGetEditMenuItem(WEditMenu * mPtr, int index)
463 if (index >= WMGetArrayItemCount(mPtr->items))
464 return NULL;
466 return WMGetFromArray(mPtr->items, index + (mPtr->flags.isTitled ? 1 : 0));
469 WEditMenuItem *WAddMenuItemWithTitle(WEditMenu * mPtr, const char *title)
471 return WInsertMenuItemWithTitle(mPtr, WMGetArrayItemCount(mPtr->items), title);
474 void WSetEditMenuTitle(WEditMenu * mPtr, const char *title)
476 WEditMenuItem *item;
478 item = WMGetFromArray(mPtr->items, 0);
480 wfree(item->label);
481 item->label = wstrdup(title);
483 updateMenuContents(mPtr);
485 WMRedisplayWidget(item);
488 char *WGetEditMenuTitle(WEditMenu * mPtr)
490 WEditMenuItem *item;
492 item = WMGetFromArray(mPtr->items, 0);
494 return item->label;
497 void WSetEditMenuAcceptsDrop(WEditMenu * mPtr, Bool flag)
499 mPtr->flags.acceptsDrop = flag;
502 void WSetEditMenuSubmenu(WEditMenu * mPtr, WEditMenuItem * item, WEditMenu * submenu)
504 item->submenu = submenu;
505 submenu->parent = mPtr;
507 paintEditMenuItem(item);
510 WEditMenu *WGetEditMenuSubmenu(WEditMenuItem *item)
512 return item->submenu;
515 void WRemoveEditMenuItem(WEditMenu * mPtr, WEditMenuItem * item)
517 if (WMRemoveFromArray(mPtr->items, item) != 0) {
518 updateMenuContents(mPtr);
522 void WSetEditMenuSelectable(WEditMenu * mPtr, Bool flag)
524 mPtr->flags.isSelectable = flag;
527 void WSetEditMenuEditable(WEditMenu * mPtr, Bool flag)
529 mPtr->flags.isEditable = flag;
532 void WSetEditMenuIsFactory(WEditMenu * mPtr, Bool flag)
534 mPtr->flags.isFactory = flag;
537 void WSetEditMenuMinSize(WEditMenu * mPtr, WMSize size)
539 mPtr->minSize.width = size.width;
540 mPtr->minSize.height = size.height;
543 void WSetEditMenuMaxSize(WEditMenu * mPtr, WMSize size)
545 mPtr->maxSize.width = size.width;
546 mPtr->maxSize.height = size.height;
549 WMPoint WGetEditMenuLocationForSubmenu(WEditMenu * mPtr, WEditMenu * submenu)
551 WMArrayIterator iter;
552 WEditMenuItem *item;
553 int y;
555 if (mPtr->flags.isTitled)
556 y = -mPtr->titleHeight;
557 else
558 y = 0;
559 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
560 if (item->submenu == submenu) {
561 WMPoint pt = WMGetViewScreenPosition(mPtr->view);
563 return wmkpoint(pt.x + mPtr->view->size.width, pt.y + y);
565 y += W_VIEW_HEIGHT(item->view);
568 puts("invalid submenu passed to WGetEditMenuLocationForSubmenu()");
570 return wmkpoint(0, 0);
573 static void closeMenuAction(WMWidget * w, void *data)
575 WEditMenu *menu = (WEditMenu *) data;
577 /* Parameter not used, but tell the compiler that it is ok */
578 (void) w;
580 WMAddIdleHandler(WMDestroyWidget, menu->closeB);
581 menu->closeB = NULL;
583 WEditMenuHide(menu);
586 void WTearOffEditMenu(WEditMenu * menu, WEditMenu * submenu)
588 WEditMenuItem *item;
590 submenu->flags.isTornOff = 1;
592 item = (WEditMenuItem *) WMGetFromArray(submenu->items, 0);
594 submenu->closeB = WMCreateCommandButton(item);
595 WMResizeWidget(submenu->closeB, 15, 15);
596 WMMoveWidget(submenu->closeB, W_VIEW(submenu)->size.width - 20, 3);
597 WMRealizeWidget(submenu->closeB);
598 WMSetButtonText(submenu->closeB, "X");
599 WMSetButtonAction(submenu->closeB, closeMenuAction, submenu);
600 WMMapWidget(submenu->closeB);
602 if (menu->selectedItem && menu->selectedItem->submenu == submenu)
603 deselectItem(menu);
606 Bool WEditMenuIsTornOff(WEditMenu * mPtr)
608 return mPtr->flags.isTornOff;
611 void WEditMenuHide(WEditMenu * mPtr)
613 WEditMenuItem *item;
614 int i = 0;
616 if (WMWidgetIsMapped(mPtr)) {
617 WMUnmapWidget(mPtr);
618 mPtr->flags.wasMapped = 1;
619 } else {
620 mPtr->flags.wasMapped = 0;
622 while ((item = WGetEditMenuItem(mPtr, i++))) {
623 WEditMenu *submenu;
625 submenu = WGetEditMenuSubmenu(item);
626 if (submenu) {
627 WEditMenuHide(submenu);
632 void WEditMenuUnhide(WEditMenu * mPtr)
634 WEditMenuItem *item;
635 int i = 0;
637 if (mPtr->flags.wasMapped) {
638 WMMapWidget(mPtr);
640 while ((item = WGetEditMenuItem(mPtr, i++))) {
641 WEditMenu *submenu;
643 submenu = WGetEditMenuSubmenu(item);
644 if (submenu) {
645 WEditMenuUnhide(submenu);
650 void WEditMenuShowAt(WEditMenu * menu, int x, int y)
652 XSizeHints *hints;
654 hints = XAllocSizeHints();
656 hints->flags = USPosition;
657 hints->x = x;
658 hints->y = y;
660 WMMoveWidget(menu, x, y);
661 XSetWMNormalHints(W_VIEW_DISPLAY(menu->view), W_VIEW_DRAWABLE(menu->view), hints);
662 XFree(hints);
664 WMMapWidget(menu);
667 static void updateMenuContents(WEditMenu * mPtr)
669 int newW, newH;
670 int w;
671 int i;
672 int iheight = mPtr->itemHeight;
673 int offs = 1;
674 WMArrayIterator iter;
675 WEditMenuItem *item;
677 newW = 0;
678 newH = offs;
680 i = 0;
681 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
682 w = getItemTextWidth(item);
684 newW = WMAX(w, newW);
686 WMMoveWidget(item, offs, newH);
687 if (i == 0 && mPtr->flags.isTitled) {
688 newH += mPtr->titleHeight;
689 } else {
690 newH += iheight;
692 i = 1;
695 newW += iheight + 10;
696 newH--;
698 if (mPtr->minSize.width)
699 newW = WMAX(newW, mPtr->minSize.width);
700 if (mPtr->maxSize.width)
701 newW = WMIN(newW, mPtr->maxSize.width);
703 if (mPtr->minSize.height)
704 newH = WMAX(newH, mPtr->minSize.height);
705 if (mPtr->maxSize.height)
706 newH = WMIN(newH, mPtr->maxSize.height);
708 if (W_VIEW(mPtr)->size.width == newW && mPtr->view->size.height == newH + 1)
709 return;
711 W_ResizeView(mPtr->view, newW, newH + 1);
713 if (mPtr->closeB)
714 WMMoveWidget(mPtr->closeB, newW - 20, 3);
716 newW -= 2 * offs;
718 i = 0;
719 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
720 if (i == 0 && mPtr->flags.isTitled) {
721 WMResizeWidget(item, newW, mPtr->titleHeight);
722 } else {
723 WMResizeWidget(item, newW, iheight);
725 i = 1;
729 static void deselectItem(WEditMenu * menu)
731 WEditMenu *submenu;
732 WEditMenuItem *item = menu->selectedItem;
734 highlightItem(item, False);
736 if (menu->delegate && menu->delegate->itemDeselected) {
737 (*menu->delegate->itemDeselected) (menu->delegate, menu, item);
739 submenu = item->submenu;
741 if (submenu && !WEditMenuIsTornOff(submenu)) {
742 WEditMenuHide(submenu);
745 menu->selectedItem = NULL;
748 static void selectItem(WEditMenu * menu, WEditMenuItem * item)
750 if (!menu->flags.isSelectable || menu->selectedItem == item) {
751 return;
753 if (menu->selectedItem) {
754 deselectItem(menu);
757 if (menu->flags.isEditing) {
758 stopEditItem(menu, False);
761 if (item && !item->flags.isTitle) {
762 highlightItem(item, True);
764 if (item->submenu && !W_VIEW_MAPPED(item->submenu->view)) {
765 WMPoint pt;
767 pt = WGetEditMenuLocationForSubmenu(menu, item->submenu);
769 WEditMenuShowAt(item->submenu, pt.x, pt.y);
771 item->submenu->flags.isTornOff = 0;
774 WMPostNotificationName("EditMenuItemSelected", menu, NULL);
776 if (menu->delegate && menu->delegate->itemSelected) {
777 (*menu->delegate->itemSelected) (menu->delegate, menu, item);
781 menu->selectedItem = item;
784 static void paintMenu(WEditMenu * mPtr)
786 W_View *view = mPtr->view;
788 W_DrawRelief(W_VIEW_SCREEN(view), W_VIEW_DRAWABLE(view), 0, 0,
789 W_VIEW_WIDTH(view), W_VIEW_HEIGHT(view), WRSimple);
792 static void handleEvents(XEvent * event, void *data)
794 WEditMenu *mPtr = (WEditMenu *) data;
796 switch (event->type) {
797 case DestroyNotify:
798 destroyEditMenu(mPtr);
799 break;
801 case Expose:
802 if (event->xexpose.count == 0)
803 paintMenu(mPtr);
804 break;
808 /* -------------------------- Menu Label Editing ------------------------ */
810 static void stopEditItem(WEditMenu * menu, Bool apply)
812 if (apply) {
813 wfree(menu->selectedItem->label);
814 menu->selectedItem->label = WMGetTextFieldText(menu->tfield);
816 updateMenuContents(menu);
818 if (menu->delegate && menu->delegate->itemEdited) {
819 (*menu->delegate->itemEdited) (menu->delegate, menu, menu->selectedItem);
823 WMUnmapWidget(menu->tfield);
824 menu->flags.isEditing = 0;
827 static void textEndedEditing(struct WMTextFieldDelegate *self, WMNotification * notif)
829 WEditMenu *menu = (WEditMenu *) self->data;
830 uintptr_t reason;
831 int i;
832 WEditMenuItem *item;
834 if (!menu->flags.isEditing)
835 return;
837 reason = (uintptr_t)WMGetNotificationClientData(notif);
839 switch (reason) {
840 case WMEscapeTextMovement:
841 stopEditItem(menu, False);
842 break;
844 case WMReturnTextMovement:
845 stopEditItem(menu, True);
846 break;
848 case WMTabTextMovement:
849 stopEditItem(menu, True);
851 i = WMGetFirstInArray(menu->items, menu->selectedItem);
852 item = WMGetFromArray(menu->items, i + 1);
853 if (item != NULL) {
854 selectItem(menu, item);
855 editItemLabel(item);
857 break;
859 case WMBacktabTextMovement:
860 stopEditItem(menu, True);
862 i = WMGetFirstInArray(menu->items, menu->selectedItem);
863 item = WMGetFromArray(menu->items, i - 1);
864 if (item != NULL) {
865 selectItem(menu, item);
866 editItemLabel(item);
868 break;
872 static WMTextFieldDelegate textFieldDelegate = {
873 NULL,
874 NULL,
875 NULL,
876 textEndedEditing,
877 NULL,
878 NULL
881 static void editItemLabel(WEditMenuItem * item)
883 WEditMenu *mPtr = item->parent;
884 WMTextField *tf;
886 if (!mPtr->flags.isEditable) {
887 return;
890 if (!mPtr->tfield) {
891 mPtr->tfield = WMCreateTextField(mPtr);
892 WMSetTextFieldBeveled(mPtr->tfield, False);
893 WMRealizeWidget(mPtr->tfield);
895 mPtr->tdelegate = wmalloc(sizeof(WMTextFieldDelegate));
896 memcpy(mPtr->tdelegate, &textFieldDelegate, sizeof(WMTextFieldDelegate));
898 mPtr->tdelegate->data = mPtr;
900 WMSetTextFieldDelegate(mPtr->tfield, mPtr->tdelegate);
902 tf = mPtr->tfield;
904 WMSetTextFieldText(tf, item->label);
905 WMSelectTextFieldRange(tf, wmkrange(0, strlen(item->label)));
906 WMResizeWidget(tf, mPtr->view->size.width, mPtr->itemHeight);
907 WMMoveWidget(tf, 0, item->view->pos.y);
908 WMMapWidget(tf);
909 WMSetFocusToWidget(tf);
911 mPtr->flags.isEditing = 1;
914 /* -------------------------------------------------- */
916 static void slideWindow(Display * dpy, Window win, int srcX, int srcY, int dstX, int dstY)
918 double x, y, dx, dy;
919 int i;
920 int iterations;
922 iterations = WMIN(25, WMAX(abs(dstX - srcX), abs(dstY - srcY)));
924 x = srcX;
925 y = srcY;
927 dx = (double)(dstX - srcX) / iterations;
928 dy = (double)(dstY - srcY) / iterations;
930 for (i = 0; i <= iterations; i++) {
931 XMoveWindow(dpy, win, x, y);
932 XFlush(dpy);
934 wusleep(800);
936 x += dx;
937 y += dy;
941 static int errorHandler(Display * d, XErrorEvent * ev)
943 /* Parameter not used, but tell the compiler that it is ok */
944 (void) d;
945 (void) ev;
947 /* just ignore */
948 return 0;
951 static WEditMenu *findMenuInWindow(Display * dpy, Window toplevel, int x, int y)
953 Window foo, bar;
954 Window *children;
955 unsigned nchildren;
956 int i;
957 WEditMenu *menu;
958 WMView *view;
959 int (*oldHandler) (Display *, XErrorEvent *);
961 view = W_GetViewForXWindow(dpy, toplevel);
962 if (view && view->self && WMWidgetClass(view->self) == EditMenuClass) {
963 menu = (WEditMenu *) view->self;
964 if (menu->flags.acceptsDrop) {
965 return menu;
969 if (!XQueryTree(dpy, toplevel, &foo, &bar, &children, &nchildren) || children == NULL) {
970 return NULL;
973 oldHandler = XSetErrorHandler(errorHandler);
975 /* first window that contains the point is the one */
976 for (i = nchildren - 1; i >= 0; i--) {
977 XWindowAttributes attr;
979 if (XGetWindowAttributes(dpy, children[i], &attr)
980 && attr.map_state == IsViewable
981 && x >= attr.x && y >= attr.y && x < attr.x + attr.width && y < attr.y + attr.height) {
982 Window tmp;
984 tmp = children[i];
986 menu = findMenuInWindow(dpy, tmp, x - attr.x, y - attr.y);
987 if (menu) {
988 XFree(children);
989 return menu;
994 XSetErrorHandler(oldHandler);
996 XFree(children);
997 return NULL;
1000 static void handleDragOver(WEditMenu *menu, WMView *view, WEditMenuItem *item, int y)
1002 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1003 Window blaw;
1004 int mx, my;
1005 int offs;
1007 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1009 offs = menu->flags.standalone ? 0 : 1;
1011 W_MoveView(view, mx + offs, y);
1012 if (view->size.width != menu->view->size.width) {
1013 W_ResizeView(view, menu->view->size.width - 2 * offs, menu->itemHeight);
1014 W_ResizeView(item->view, menu->view->size.width - 2 * offs, menu->itemHeight);
1018 static void handleItemDrop(WEditMenu *menu, WEditMenuItem *item, int y)
1020 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1021 Window blaw;
1022 int mx, my;
1023 int index;
1025 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1027 index = y - my;
1028 if (menu->flags.isTitled) {
1029 index -= menu->titleHeight;
1031 index = (index + menu->itemHeight / 2) / menu->itemHeight;
1032 if (index < 0)
1033 index = 0;
1035 if (menu->flags.isTitled) {
1036 index++;
1039 if (index > WMGetArrayItemCount(menu->items)) {
1040 WMAddToArray(menu->items, item);
1041 } else {
1042 WMInsertInArray(menu->items, index, item);
1045 W_ReparentView(item->view, menu->view, 0, index * menu->itemHeight);
1047 item->parent = menu;
1048 if (item->submenu) {
1049 item->submenu->parent = menu;
1052 updateMenuContents(menu);
1055 static void dragMenu(WEditMenu * menu)
1057 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1058 XEvent ev;
1059 Bool done = False;
1060 int dx, dy;
1061 unsigned blau;
1062 Window blaw;
1064 XGetGeometry(scr->display, W_VIEW_DRAWABLE(menu->view), &blaw, &dx, &dy, &blau, &blau, &blau, &blau);
1066 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, dx, dy, &dx, &dy, &blaw);
1068 dx = menu->dragX - dx;
1069 dy = menu->dragY - dy;
1071 XGrabPointer(scr->display, scr->rootWin, False,
1072 ButtonReleaseMask | ButtonMotionMask,
1073 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1075 if (menu->parent)
1076 WTearOffEditMenu(menu->parent, menu);
1078 while (!done) {
1079 WMNextEvent(scr->display, &ev);
1081 switch (ev.type) {
1082 case ButtonRelease:
1083 done = True;
1084 break;
1086 case MotionNotify:
1087 while (XCheckMaskEvent(scr->display, ButtonMotionMask, &ev)) ;
1089 WMMoveWidget(menu, ev.xmotion.x_root - dx, ev.xmotion.y_root - dy);
1090 break;
1092 default:
1093 WMHandleEvent(&ev);
1094 break;
1098 XUngrabPointer(scr->display, CurrentTime);
1101 static WEditMenuItem *duplicateItem(WEditMenuItem * item)
1103 WEditMenuItem *nitem;
1105 nitem = WCreateEditMenuItem(item->parent, item->label, False);
1106 if (item->pixmap)
1107 nitem->pixmap = WMRetainPixmap(item->pixmap);
1109 return nitem;
1112 static WEditMenu *duplicateMenu(WEditMenu * menu)
1114 WEditMenu *nmenu;
1115 WEditMenuItem *item;
1116 WMArrayIterator iter;
1117 Bool first = menu->flags.isTitled;
1119 nmenu = WCreateEditMenu(WMWidgetScreen(menu), WGetEditMenuTitle(menu));
1121 memcpy(&nmenu->flags, &menu->flags, sizeof(menu->flags));
1122 nmenu->delegate = menu->delegate;
1124 WM_ITERATE_ARRAY(menu->items, item, iter) {
1125 WEditMenuItem *nitem;
1127 if (first) {
1128 first = False;
1129 continue;
1132 nitem = WAddMenuItemWithTitle(nmenu, item->label);
1133 if (item->pixmap)
1134 WSetEditMenuItemImage(nitem, item->pixmap);
1136 if (menu->delegate && menu->delegate->itemCloned) {
1137 (*menu->delegate->itemCloned) (menu->delegate, menu, item, nitem);
1141 WMRealizeWidget(nmenu);
1143 return nmenu;
1146 static void dragItem(WEditMenu * menu, WEditMenuItem * item, Bool copy)
1148 static XColor black = { 0, 0, 0, 0, DoRed | DoGreen | DoBlue, 0 };
1149 static XColor green = { 0x0045b045, 0x4500, 0xb000, 0x4500, DoRed | DoGreen | DoBlue, 0 };
1150 static XColor back = { 0, 0xffff, 0xffff, 0xffff, DoRed | DoGreen | DoBlue, 0 };
1151 Display *dpy = W_VIEW_DISPLAY(menu->view);
1152 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1153 int x, y;
1154 int dx, dy;
1155 Bool done = False;
1156 Window blaw;
1157 int blai;
1158 unsigned blau;
1159 Window win;
1160 WMView *dview;
1161 int orix, oriy;
1162 Bool enteredMenu = False;
1163 WMSize oldSize = item->view->size;
1164 WEditMenuItem *dritem = item;
1165 WEditMenu *dmenu = NULL;
1167 if (item->flags.isTitle) {
1168 WMRaiseWidget(menu);
1170 dragMenu(menu);
1172 return;
1175 selectItem(menu, NULL);
1177 win = scr->rootWin;
1179 XTranslateCoordinates(dpy, W_VIEW_DRAWABLE(item->view), win, 0, 0, &orix, &oriy, &blaw);
1181 dview = W_CreateUnmanagedTopView(scr);
1182 W_SetViewBackgroundColor(dview, scr->black);
1183 W_ResizeView(dview, W_VIEW_WIDTH(item->view), W_VIEW_HEIGHT(item->view));
1184 W_MoveView(dview, orix, oriy);
1185 W_RealizeView(dview);
1187 if (menu->flags.isFactory || copy) {
1188 dritem = duplicateItem(item);
1190 W_ReparentView(dritem->view, dview, 0, 0);
1191 WMResizeWidget(dritem, oldSize.width, oldSize.height);
1192 WMRealizeWidget(dritem);
1193 WMMapWidget(dritem);
1194 } else {
1195 W_ReparentView(item->view, dview, 0, 0);
1198 W_MapView(dview);
1200 dx = menu->dragX - orix;
1201 dy = menu->dragY - oriy;
1203 XGrabPointer(dpy, scr->rootWin, False,
1204 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
1205 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1207 if (menu->flags.acceptsDrop)
1208 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
1210 while (!done) {
1211 XEvent ev;
1213 WMNextEvent(dpy, &ev);
1215 switch (ev.type) {
1216 case MotionNotify:
1217 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
1219 XQueryPointer(dpy, win, &blaw, &blaw, &blai, &blai, &x, &y, &blau);
1221 dmenu = findMenuInWindow(dpy, win, x, y);
1223 if (dmenu) {
1224 handleDragOver(dmenu, dview, dritem, y - dy);
1225 if (!enteredMenu) {
1226 enteredMenu = True;
1227 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
1229 } else {
1230 if (enteredMenu) {
1231 W_ResizeView(dview, oldSize.width, oldSize.height);
1232 W_ResizeView(dritem->view, oldSize.width, oldSize.height);
1233 enteredMenu = False;
1234 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
1236 W_MoveView(dview, x - dx, y - dy);
1239 break;
1241 case ButtonRelease:
1242 done = True;
1243 break;
1245 default:
1246 WMHandleEvent(&ev);
1247 break;
1250 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
1252 XUngrabPointer(dpy, CurrentTime);
1254 if (!enteredMenu) {
1255 Bool rem = True;
1257 if (!menu->flags.isFactory && !copy) {
1258 W_UnmapView(dview);
1259 if (menu->delegate && menu->delegate->shouldRemoveItem) {
1260 rem = (*menu->delegate->shouldRemoveItem) (menu->delegate, menu, item);
1262 W_MapView(dview);
1265 if (!rem || menu->flags.isFactory || copy) {
1266 slideWindow(dpy, W_VIEW_DRAWABLE(dview), x - dx, y - dy, orix, oriy);
1268 if (!menu->flags.isFactory && !copy) {
1269 WRemoveEditMenuItem(menu, dritem);
1270 handleItemDrop(dmenu ? dmenu : menu, dritem, oriy);
1272 } else {
1273 WRemoveEditMenuItem(menu, dritem);
1275 } else {
1276 WRemoveEditMenuItem(menu, dritem);
1278 if (menu->delegate && menu->delegate->itemCloned && (menu->flags.isFactory || copy)) {
1279 (*menu->delegate->itemCloned) (menu->delegate, menu, item, dritem);
1282 handleItemDrop(dmenu, dritem, y - dy);
1284 if (item->submenu && (menu->flags.isFactory || copy)) {
1285 WEditMenu *submenu;
1287 submenu = duplicateMenu(item->submenu);
1288 WSetEditMenuSubmenu(dmenu, dritem, submenu);
1292 /* can't destroy now because we're being called from
1293 * the event handler of the item. and destroying now,
1294 * would mean destroying the item too in some cases.
1296 WMAddIdleHandler((WMCallback *) W_DestroyView, dview);
1299 static void handleItemClick(XEvent * event, void *data)
1301 WEditMenuItem *item = (WEditMenuItem *) data;
1302 WEditMenu *menu = item->parent;
1304 switch (event->type) {
1305 case ButtonPress:
1306 selectItem(menu, item);
1308 if (WMIsDoubleClick(event)) {
1309 editItemLabel(item);
1312 menu->flags.isDragging = 1;
1313 menu->dragX = event->xbutton.x_root;
1314 menu->dragY = event->xbutton.y_root;
1315 break;
1317 case ButtonRelease:
1318 menu->flags.isDragging = 0;
1319 break;
1321 case MotionNotify:
1322 if (menu->flags.isDragging) {
1323 if (abs(event->xbutton.x_root - menu->dragX) > 5
1324 || abs(event->xbutton.y_root - menu->dragY) > 5) {
1325 menu->flags.isDragging = 0;
1326 dragItem(menu, item, event->xbutton.state & ControlMask);
1329 break;
1333 static void destroyEditMenu(WEditMenu * mPtr)
1335 WMRemoveNotificationObserver(mPtr);
1337 WMFreeArray(mPtr->items);
1339 wfree(mPtr->tdelegate);
1341 wfree(mPtr);