Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / editmenu.c
blobf5b84aac995ef785a5a17381becc19eb5f5b8fbb
1 /* editmenu.c - editable menus
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
21 */
23 #include <WINGs/WINGsP.h>
24 #include <WINGs/WUtil.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "editmenu.h"
32 typedef struct W_EditMenuItem {
33 W_Class widgetClass;
34 WMView *view;
36 struct W_EditMenu *parent;
38 char *label;
39 WMPixmap *pixmap; /* pixmap to show at left */
41 void *data;
42 WMCallback *destroyData;
44 struct W_EditMenu *submenu; /* if it's a cascade, NULL otherwise */
46 struct {
47 unsigned isTitle:1;
48 unsigned isHighlighted:1;
49 } flags;
50 } EditMenuItem;
52 typedef struct W_EditMenu {
53 W_Class widgetClass;
54 WMView *view;
56 struct W_EditMenu *parent;
58 WMArray *items; /* EditMenuItem */
60 EditMenuItem *selectedItem;
62 WMTextField *tfield;
64 WMButton *closeB;
66 int titleHeight;
67 int itemHeight;
69 WEditMenuDelegate *delegate;
71 WMTextFieldDelegate *tdelegate;
73 /* item dragging */
74 int draggedItem;
75 int dragX, dragY;
77 /* only for non-standalone menu */
78 WMSize maxSize;
79 WMSize minSize;
81 struct {
82 unsigned standalone:1;
83 unsigned isTitled:1;
85 unsigned acceptsDrop:1;
86 unsigned isFactory:1;
87 unsigned isSelectable:1;
88 unsigned isEditable:1;
90 unsigned isTornOff:1;
92 unsigned isDragging:1;
93 unsigned isEditing:1;
95 unsigned wasMapped:1;
96 } flags;
97 } EditMenu;
99 /******************** WEditMenuItem ********************/
101 static void destroyEditMenuItem(WEditMenuItem * iPtr);
102 static void paintEditMenuItem(WEditMenuItem * iPtr);
103 static void handleItemEvents(XEvent * event, void *data);
105 static void handleItemClick(XEvent * event, void *data);
107 static W_Class EditMenuItemClass = 0;
109 W_Class InitEditMenuItem(WMScreen * scr)
111 /* register our widget with WINGs and get our widget class ID */
112 if (!EditMenuItemClass) {
113 EditMenuItemClass = W_RegisterUserWidget();
116 return EditMenuItemClass;
119 WEditMenuItem *WCreateEditMenuItem(WMWidget * parent, char *title, Bool isTitle)
121 WEditMenuItem *iPtr;
122 WMScreen *scr = WMWidgetScreen(parent);
124 if (!EditMenuItemClass)
125 InitEditMenuItem(scr);
127 iPtr = wmalloc(sizeof(WEditMenuItem));
129 memset(iPtr, 0, sizeof(WEditMenuItem));
131 iPtr->widgetClass = EditMenuItemClass;
133 iPtr->view = W_CreateView(W_VIEW(parent));
134 if (!iPtr->view) {
135 wfree(iPtr);
136 return NULL;
138 iPtr->view->self = iPtr;
140 iPtr->parent = parent;
142 iPtr->label = wstrdup(title);
144 iPtr->flags.isTitle = isTitle;
146 if (isTitle) {
147 WMSetWidgetBackgroundColor(iPtr, WMBlackColor(scr));
150 WMCreateEventHandler(iPtr->view, ExposureMask | StructureNotifyMask, handleItemEvents, iPtr);
152 WMCreateEventHandler(iPtr->view, ButtonPressMask | ButtonReleaseMask
153 | ButtonMotionMask, handleItemClick, iPtr);
155 return iPtr;
158 char *WGetEditMenuItemTitle(WEditMenuItem * item)
160 return item->label;
163 void *WGetEditMenuItemData(WEditMenuItem * item)
165 return item->data;
168 void WSetEditMenuItemData(WEditMenuItem * item, void *data, WMCallback * destroyer)
170 item->data = data;
171 item->destroyData = destroyer;
174 void WSetEditMenuItemImage(WEditMenuItem * item, WMPixmap * pixmap)
176 if (item->pixmap)
177 WMReleasePixmap(item->pixmap);
178 item->pixmap = WMRetainPixmap(pixmap);
181 static void paintEditMenuItem(WEditMenuItem * iPtr)
183 WMScreen *scr = WMWidgetScreen(iPtr);
184 WMColor *color;
185 Window win = W_VIEW(iPtr)->window;
186 int w = W_VIEW(iPtr)->size.width;
187 int h = W_VIEW(iPtr)->size.height;
188 WMFont *font = (iPtr->flags.isTitle ? scr->boldFont : scr->normalFont);
190 if (!iPtr->view->flags.realized)
191 return;
193 color = scr->black;
194 if (iPtr->flags.isTitle && !iPtr->flags.isHighlighted) {
195 color = scr->white;
198 XClearWindow(scr->display, win);
200 W_DrawRelief(scr, win, 0, 0, w + 1, h, WRRaised);
202 WMDrawString(scr, win, color, font, 5, 3, iPtr->label, strlen(iPtr->label));
204 if (iPtr->pixmap) {
205 WMSize size = WMGetPixmapSize(iPtr->pixmap);
207 WMDrawPixmap(iPtr->pixmap, win, w - size.width - 5, (h - size.height) / 2);
208 } else if (iPtr->submenu) {
209 /* draw the cascade indicator */
210 XDrawLine(scr->display, win, WMColorGC(scr->darkGray), w - 11, 6, w - 6, h / 2 - 1);
211 XDrawLine(scr->display, win, WMColorGC(scr->white), w - 11, h - 8, w - 6, h / 2 - 1);
212 XDrawLine(scr->display, win, WMColorGC(scr->black), w - 12, 6, w - 12, h - 8);
216 static void highlightItem(WEditMenuItem * iPtr, Bool high)
218 if (iPtr->flags.isTitle)
219 return;
221 iPtr->flags.isHighlighted = high;
223 if (high) {
224 WMSetWidgetBackgroundColor(iPtr, WMWhiteColor(WMWidgetScreen(iPtr)));
225 } else {
226 if (!iPtr->flags.isTitle) {
227 WMSetWidgetBackgroundColor(iPtr, WMGrayColor(WMWidgetScreen(iPtr)));
228 } else {
229 WMSetWidgetBackgroundColor(iPtr, WMBlackColor(WMWidgetScreen(iPtr)));
234 static int getItemTextWidth(WEditMenuItem * iPtr)
236 WMScreen *scr = WMWidgetScreen(iPtr);
238 if (iPtr->flags.isTitle) {
239 return WMWidthOfString(scr->boldFont, iPtr->label, strlen(iPtr->label));
240 } else {
241 return WMWidthOfString(scr->normalFont, iPtr->label, strlen(iPtr->label));
245 static void handleItemEvents(XEvent * event, void *data)
247 WEditMenuItem *iPtr = (WEditMenuItem *) data;
249 switch (event->type) {
250 case Expose:
251 if (event->xexpose.count != 0)
252 break;
253 paintEditMenuItem(iPtr);
254 break;
256 case DestroyNotify:
257 destroyEditMenuItem(iPtr);
258 break;
262 static void destroyEditMenuItem(WEditMenuItem * iPtr)
264 if (iPtr->label)
265 wfree(iPtr->label);
266 if (iPtr->data && iPtr->destroyData)
267 (*iPtr->destroyData) (iPtr->data);
268 if (iPtr->submenu)
269 WMDestroyWidget(iPtr->submenu);
271 wfree(iPtr);
274 /******************** WEditMenu *******************/
276 static void destroyEditMenu(WEditMenu * mPtr);
278 static void updateMenuContents(WEditMenu * mPtr);
280 static void handleEvents(XEvent * event, void *data);
282 static void editItemLabel(WEditMenuItem * item);
284 static void stopEditItem(WEditMenu * menu, Bool apply);
286 static void deselectItem(WEditMenu * menu);
288 static W_Class EditMenuClass = 0;
290 W_Class InitEditMenu(WMScreen * scr)
292 /* register our widget with WINGs and get our widget class ID */
293 if (!EditMenuClass) {
295 EditMenuClass = W_RegisterUserWidget();
298 return EditMenuClass;
301 typedef struct {
302 int flags;
303 int window_style;
304 int window_level;
305 int reserved;
306 Pixmap miniaturize_pixmap; /* pixmap for miniaturize button */
307 Pixmap close_pixmap; /* pixmap for close button */
308 Pixmap miniaturize_mask; /* miniaturize pixmap mask */
309 Pixmap close_mask; /* close pixmap mask */
310 int extra_flags;
311 } GNUstepWMAttributes;
313 #define GSWindowStyleAttr (1<<0)
314 #define GSWindowLevelAttr (1<<1)
316 static void writeGNUstepWMAttr(WMScreen * scr, Window window, GNUstepWMAttributes * attr)
318 unsigned long data[9];
320 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
321 data[0] = attr->flags;
322 data[1] = attr->window_style;
323 data[2] = attr->window_level;
324 data[3] = 0; /* reserved */
325 /* The X protocol says XIDs are 32bit */
326 data[4] = attr->miniaturize_pixmap;
327 data[5] = attr->close_pixmap;
328 data[6] = attr->miniaturize_mask;
329 data[7] = attr->close_mask;
330 data[8] = attr->extra_flags;
331 XChangeProperty(scr->display, window, scr->attribsAtom, scr->attribsAtom,
332 32, PropModeReplace, (unsigned char *)data, 9);
335 static void realizeObserver(void *self, WMNotification * not)
337 WEditMenu *menu = (WEditMenu *) self;
338 GNUstepWMAttributes attribs;
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, char *title)
370 WEditMenu *mPtr;
371 WEditMenuItem *item;
373 if (!EditMenuClass)
374 InitEditMenu(scr);
376 mPtr = wmalloc(sizeof(WEditMenu));
377 memset(mPtr, 0, sizeof(WEditMenu));
379 mPtr->widgetClass = EditMenuClass;
381 if (parent) {
382 mPtr->view = W_CreateView(W_VIEW(parent));
383 mPtr->flags.standalone = 0;
384 } else {
385 mPtr->view = W_CreateTopView(scr);
386 mPtr->flags.standalone = 1;
388 if (!mPtr->view) {
389 wfree(mPtr);
390 return NULL;
392 mPtr->view->self = mPtr;
394 mPtr->flags.isSelectable = 1;
395 mPtr->flags.isEditable = 1;
397 W_SetViewBackgroundColor(mPtr->view, scr->darkGray);
399 WMAddNotificationObserver(realizeObserver, mPtr, WMViewRealizedNotification, mPtr->view);
401 WMAddNotificationObserver(itemSelectObserver, mPtr, "EditMenuItemSelected", NULL);
403 mPtr->items = WMCreateArray(4);
405 WMCreateEventHandler(mPtr->view, ExposureMask | StructureNotifyMask, handleEvents, mPtr);
407 if (title != NULL) {
408 item = WCreateEditMenuItem(mPtr, title, True);
410 WMMapWidget(item);
411 WMAddToArray(mPtr->items, item);
413 mPtr->flags.isTitled = 1;
416 mPtr->itemHeight = WMFontHeight(scr->normalFont) + 6;
417 mPtr->titleHeight = WMFontHeight(scr->boldFont) + 8;
419 updateMenuContents(mPtr);
421 return mPtr;
424 WEditMenu *WCreateEditMenu(WMScreen * scr, char *title)
426 return makeEditMenu(scr, NULL, title);
429 WEditMenu *WCreateEditMenuPad(WMWidget * parent)
431 return makeEditMenu(WMWidgetScreen(parent), parent, NULL);
434 void WSetEditMenuDelegate(WEditMenu * mPtr, WEditMenuDelegate * delegate)
436 mPtr->delegate = delegate;
439 WEditMenuItem *WInsertMenuItemWithTitle(WEditMenu * mPtr, int index, char *title)
441 WEditMenuItem *item;
443 item = WCreateEditMenuItem(mPtr, title, False);
445 WMMapWidget(item);
447 if (index >= WMGetArrayItemCount(mPtr->items)) {
448 WMAddToArray(mPtr->items, item);
449 } else {
450 if (index < 0)
451 index = 0;
452 if (mPtr->flags.isTitled)
453 index++;
454 WMInsertInArray(mPtr->items, index, item);
457 updateMenuContents(mPtr);
459 return item;
462 WEditMenuItem *WGetEditMenuItem(WEditMenu * mPtr, int index)
464 if (index >= WMGetArrayItemCount(mPtr->items))
465 return NULL;
467 return WMGetFromArray(mPtr->items, index + (mPtr->flags.isTitled ? 1 : 0));
470 WEditMenuItem *WAddMenuItemWithTitle(WEditMenu * mPtr, char *title)
472 return WInsertMenuItemWithTitle(mPtr, WMGetArrayItemCount(mPtr->items), title);
475 void WSetEditMenuTitle(WEditMenu * mPtr, char *title)
477 WEditMenuItem *item;
479 item = WMGetFromArray(mPtr->items, 0);
481 wfree(item->label);
482 item->label = wstrdup(title);
484 updateMenuContents(mPtr);
486 WMRedisplayWidget(item);
489 char *WGetEditMenuTitle(WEditMenu * mPtr)
491 WEditMenuItem *item;
493 item = WMGetFromArray(mPtr->items, 0);
495 return item->label;
498 void WSetEditMenuAcceptsDrop(WEditMenu * mPtr, Bool flag)
500 mPtr->flags.acceptsDrop = flag;
503 void WSetEditMenuSubmenu(WEditMenu * mPtr, WEditMenuItem * item, WEditMenu * submenu)
505 item->submenu = submenu;
506 submenu->parent = mPtr;
508 paintEditMenuItem(item);
511 WEditMenu *WGetEditMenuSubmenu(WEditMenu * mPtr, WEditMenuItem * item)
513 return item->submenu;
516 void WRemoveEditMenuItem(WEditMenu * mPtr, WEditMenuItem * item)
518 if (WMRemoveFromArray(mPtr->items, item) != 0) {
519 updateMenuContents(mPtr);
523 void WSetEditMenuSelectable(WEditMenu * mPtr, Bool flag)
525 mPtr->flags.isSelectable = flag;
528 void WSetEditMenuEditable(WEditMenu * mPtr, Bool flag)
530 mPtr->flags.isEditable = flag;
533 void WSetEditMenuIsFactory(WEditMenu * mPtr, Bool flag)
535 mPtr->flags.isFactory = flag;
538 void WSetEditMenuMinSize(WEditMenu * mPtr, WMSize size)
540 mPtr->minSize.width = size.width;
541 mPtr->minSize.height = size.height;
544 void WSetEditMenuMaxSize(WEditMenu * mPtr, WMSize size)
546 mPtr->maxSize.width = size.width;
547 mPtr->maxSize.height = size.height;
550 WMPoint WGetEditMenuLocationForSubmenu(WEditMenu * mPtr, WEditMenu * submenu)
552 WMArrayIterator iter;
553 WEditMenuItem *item;
554 int y;
556 if (mPtr->flags.isTitled)
557 y = -mPtr->titleHeight;
558 else
559 y = 0;
560 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
561 if (item->submenu == submenu) {
562 WMPoint pt = WMGetViewScreenPosition(mPtr->view);
564 return wmkpoint(pt.x + mPtr->view->size.width, pt.y + y);
566 y += W_VIEW_HEIGHT(item->view);
569 puts("invalid submenu passed to WGetEditMenuLocationForSubmenu()");
571 return wmkpoint(0, 0);
574 static void closeMenuAction(WMWidget * w, void *data)
576 WEditMenu *menu = (WEditMenu *) data;
578 WMAddIdleHandler(WMDestroyWidget, menu->closeB);
579 menu->closeB = NULL;
581 WEditMenuHide(menu);
584 void WTearOffEditMenu(WEditMenu * menu, WEditMenu * submenu)
586 WEditMenuItem *item;
588 submenu->flags.isTornOff = 1;
590 item = (WEditMenuItem *) WMGetFromArray(submenu->items, 0);
592 submenu->closeB = WMCreateCommandButton(item);
593 WMResizeWidget(submenu->closeB, 15, 15);
594 WMMoveWidget(submenu->closeB, W_VIEW(submenu)->size.width - 20, 3);
595 WMRealizeWidget(submenu->closeB);
596 WMSetButtonText(submenu->closeB, "X");
597 WMSetButtonAction(submenu->closeB, closeMenuAction, submenu);
598 WMMapWidget(submenu->closeB);
600 if (menu->selectedItem && menu->selectedItem->submenu == submenu)
601 deselectItem(menu);
604 Bool WEditMenuIsTornOff(WEditMenu * mPtr)
606 return mPtr->flags.isTornOff;
609 void WEditMenuHide(WEditMenu * mPtr)
611 WEditMenuItem *item;
612 int i = 0;
614 if (WMWidgetIsMapped(mPtr)) {
615 WMUnmapWidget(mPtr);
616 mPtr->flags.wasMapped = 1;
617 } else {
618 mPtr->flags.wasMapped = 0;
620 while ((item = WGetEditMenuItem(mPtr, i++))) {
621 WEditMenu *submenu;
623 submenu = WGetEditMenuSubmenu(mPtr, item);
624 if (submenu) {
625 WEditMenuHide(submenu);
630 void WEditMenuUnhide(WEditMenu * mPtr)
632 WEditMenuItem *item;
633 int i = 0;
635 if (mPtr->flags.wasMapped) {
636 WMMapWidget(mPtr);
638 while ((item = WGetEditMenuItem(mPtr, i++))) {
639 WEditMenu *submenu;
641 submenu = WGetEditMenuSubmenu(mPtr, item);
642 if (submenu) {
643 WEditMenuUnhide(submenu);
648 void WEditMenuShowAt(WEditMenu * menu, int x, int y)
650 XSizeHints *hints;
652 hints = XAllocSizeHints();
654 hints->flags = USPosition;
655 hints->x = x;
656 hints->y = y;
658 WMMoveWidget(menu, x, y);
659 XSetWMNormalHints(W_VIEW_DISPLAY(menu->view), W_VIEW_DRAWABLE(menu->view), hints);
660 XFree(hints);
662 WMMapWidget(menu);
665 static void updateMenuContents(WEditMenu * mPtr)
667 int newW, newH;
668 int w;
669 int i;
670 int iheight = mPtr->itemHeight;
671 int offs = 1;
672 WMArrayIterator iter;
673 WEditMenuItem *item;
675 newW = 0;
676 newH = offs;
678 i = 0;
679 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
680 w = getItemTextWidth(item);
682 newW = WMAX(w, newW);
684 WMMoveWidget(item, offs, newH);
685 if (i == 0 && mPtr->flags.isTitled) {
686 newH += mPtr->titleHeight;
687 } else {
688 newH += iheight;
690 i = 1;
693 newW += iheight + 10;
694 newH--;
696 if (mPtr->minSize.width)
697 newW = WMAX(newW, mPtr->minSize.width);
698 if (mPtr->maxSize.width)
699 newW = WMIN(newW, mPtr->maxSize.width);
701 if (mPtr->minSize.height)
702 newH = WMAX(newH, mPtr->minSize.height);
703 if (mPtr->maxSize.height)
704 newH = WMIN(newH, mPtr->maxSize.height);
706 if (W_VIEW(mPtr)->size.width == newW && mPtr->view->size.height == newH + 1)
707 return;
709 W_ResizeView(mPtr->view, newW, newH + 1);
711 if (mPtr->closeB)
712 WMMoveWidget(mPtr->closeB, newW - 20, 3);
714 newW -= 2 * offs;
716 i = 0;
717 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
718 if (i == 0 && mPtr->flags.isTitled) {
719 WMResizeWidget(item, newW, mPtr->titleHeight);
720 } else {
721 WMResizeWidget(item, newW, iheight);
723 i = 1;
727 static void deselectItem(WEditMenu * menu)
729 WEditMenu *submenu;
730 WEditMenuItem *item = menu->selectedItem;
732 highlightItem(item, False);
734 if (menu->delegate && menu->delegate->itemDeselected) {
735 (*menu->delegate->itemDeselected) (menu->delegate, menu, item);
737 submenu = item->submenu;
739 if (submenu && !WEditMenuIsTornOff(submenu)) {
740 WEditMenuHide(submenu);
743 menu->selectedItem = NULL;
746 static void selectItem(WEditMenu * menu, WEditMenuItem * item)
748 if (!menu->flags.isSelectable || menu->selectedItem == item) {
749 return;
751 if (menu->selectedItem) {
752 deselectItem(menu);
755 if (menu->flags.isEditing) {
756 stopEditItem(menu, False);
759 if (item && !item->flags.isTitle) {
760 highlightItem(item, True);
762 if (item->submenu && !W_VIEW_MAPPED(item->submenu->view)) {
763 WMPoint pt;
765 pt = WGetEditMenuLocationForSubmenu(menu, item->submenu);
767 WEditMenuShowAt(item->submenu, pt.x, pt.y);
769 item->submenu->flags.isTornOff = 0;
772 WMPostNotificationName("EditMenuItemSelected", menu, NULL);
774 if (menu->delegate && menu->delegate->itemSelected) {
775 (*menu->delegate->itemSelected) (menu->delegate, menu, item);
779 menu->selectedItem = item;
782 static void paintMenu(WEditMenu * mPtr)
784 W_View *view = mPtr->view;
786 W_DrawRelief(W_VIEW_SCREEN(view), W_VIEW_DRAWABLE(view), 0, 0,
787 W_VIEW_WIDTH(view), W_VIEW_HEIGHT(view), WRSimple);
790 static void handleEvents(XEvent * event, void *data)
792 WEditMenu *mPtr = (WEditMenu *) data;
794 switch (event->type) {
795 case DestroyNotify:
796 destroyEditMenu(mPtr);
797 break;
799 case Expose:
800 if (event->xexpose.count == 0)
801 paintMenu(mPtr);
802 break;
806 /* -------------------------- Menu Label Editing ------------------------ */
808 static void stopEditItem(WEditMenu * menu, Bool apply)
810 char *text;
812 if (apply) {
813 text = WMGetTextFieldText(menu->tfield);
815 wfree(menu->selectedItem->label);
816 menu->selectedItem->label = wstrdup(text);
818 updateMenuContents(menu);
820 if (menu->delegate && menu->delegate->itemEdited) {
821 (*menu->delegate->itemEdited) (menu->delegate, menu, menu->selectedItem);
825 WMUnmapWidget(menu->tfield);
826 menu->flags.isEditing = 0;
829 static void textEndedEditing(struct WMTextFieldDelegate *self, WMNotification * notif)
831 WEditMenu *menu = (WEditMenu *) self->data;
832 int reason;
833 int i;
834 WEditMenuItem *item;
836 if (!menu->flags.isEditing)
837 return;
839 reason = (int)(uintptr_t) WMGetNotificationClientData(notif);
841 switch (reason) {
842 case WMEscapeTextMovement:
843 stopEditItem(menu, False);
844 break;
846 case WMReturnTextMovement:
847 stopEditItem(menu, True);
848 break;
850 case WMTabTextMovement:
851 stopEditItem(menu, True);
853 i = WMGetFirstInArray(menu->items, menu->selectedItem);
854 item = WMGetFromArray(menu->items, i + 1);
855 if (item != NULL) {
856 selectItem(menu, item);
857 editItemLabel(item);
859 break;
861 case WMBacktabTextMovement:
862 stopEditItem(menu, True);
864 i = WMGetFirstInArray(menu->items, menu->selectedItem);
865 item = WMGetFromArray(menu->items, i - 1);
866 if (item != NULL) {
867 selectItem(menu, item);
868 editItemLabel(item);
870 break;
874 static WMTextFieldDelegate textFieldDelegate = {
875 NULL,
876 NULL,
877 NULL,
878 textEndedEditing,
879 NULL,
880 NULL
881 };
883 static void editItemLabel(WEditMenuItem * item)
885 WEditMenu *mPtr = item->parent;
886 WMTextField *tf;
888 if (!mPtr->flags.isEditable) {
889 return;
892 if (!mPtr->tfield) {
893 mPtr->tfield = WMCreateTextField(mPtr);
894 WMSetTextFieldBeveled(mPtr->tfield, False);
895 WMRealizeWidget(mPtr->tfield);
897 mPtr->tdelegate = wmalloc(sizeof(WMTextFieldDelegate));
898 memcpy(mPtr->tdelegate, &textFieldDelegate, sizeof(WMTextFieldDelegate));
900 mPtr->tdelegate->data = mPtr;
902 WMSetTextFieldDelegate(mPtr->tfield, mPtr->tdelegate);
904 tf = mPtr->tfield;
906 WMSetTextFieldText(tf, item->label);
907 WMSelectTextFieldRange(tf, wmkrange(0, strlen(item->label)));
908 WMResizeWidget(tf, mPtr->view->size.width, mPtr->itemHeight);
909 WMMoveWidget(tf, 0, item->view->pos.y);
910 WMMapWidget(tf);
911 WMSetFocusToWidget(tf);
913 mPtr->flags.isEditing = 1;
916 /* -------------------------------------------------- */
918 static void slideWindow(Display * dpy, Window win, int srcX, int srcY, int dstX, int dstY)
920 double x, y, dx, dy;
921 int i;
922 int iterations;
924 iterations = WMIN(25, WMAX(abs(dstX - srcX), abs(dstY - srcY)));
926 x = srcX;
927 y = srcY;
929 dx = (double)(dstX - srcX) / iterations;
930 dy = (double)(dstY - srcY) / iterations;
932 for (i = 0; i <= iterations; i++) {
933 XMoveWindow(dpy, win, x, y);
934 XFlush(dpy);
936 wusleep(800);
938 x += dx;
939 y += dy;
943 static int errorHandler(Display * d, XErrorEvent * ev)
945 /* just ignore */
946 return 0;
949 static WEditMenu *findMenuInWindow(Display * dpy, Window toplevel, int x, int y)
951 Window foo, bar;
952 Window *children;
953 unsigned nchildren;
954 int i;
955 WEditMenu *menu;
956 WMView *view;
957 int (*oldHandler) (Display *, XErrorEvent *);
959 view = W_GetViewForXWindow(dpy, toplevel);
960 if (view && view->self && WMWidgetClass(view->self) == EditMenuClass) {
961 menu = (WEditMenu *) view->self;
962 if (menu->flags.acceptsDrop) {
963 return menu;
967 if (!XQueryTree(dpy, toplevel, &foo, &bar, &children, &nchildren) || children == NULL) {
968 return NULL;
971 oldHandler = XSetErrorHandler(errorHandler);
973 /* first window that contains the point is the one */
974 for (i = nchildren - 1; i >= 0; i--) {
975 XWindowAttributes attr;
977 if (XGetWindowAttributes(dpy, children[i], &attr)
978 && attr.map_state == IsViewable
979 && x >= attr.x && y >= attr.y && x < attr.x + attr.width && y < attr.y + attr.height) {
980 Window tmp;
982 tmp = children[i];
984 menu = findMenuInWindow(dpy, tmp, x - attr.x, y - attr.y);
985 if (menu) {
986 XFree(children);
987 return menu;
992 XSetErrorHandler(oldHandler);
994 XFree(children);
995 return NULL;
998 static void handleDragOver(WEditMenu * menu, WMView * view, WEditMenuItem * item, int x, int y)
1000 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1001 Window blaw;
1002 int mx, my;
1003 int offs;
1005 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1007 offs = menu->flags.standalone ? 0 : 1;
1009 W_MoveView(view, mx + offs, y);
1010 if (view->size.width != menu->view->size.width) {
1011 W_ResizeView(view, menu->view->size.width - 2 * offs, menu->itemHeight);
1012 W_ResizeView(item->view, menu->view->size.width - 2 * offs, menu->itemHeight);
1016 static void handleItemDrop(WEditMenu * menu, WEditMenuItem * item, int x, int y)
1018 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1019 Window blaw;
1020 int mx, my;
1021 int index;
1023 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1025 index = y - my;
1026 if (menu->flags.isTitled) {
1027 index -= menu->titleHeight;
1029 index = (index + menu->itemHeight / 2) / menu->itemHeight;
1030 if (index < 0)
1031 index = 0;
1033 if (menu->flags.isTitled) {
1034 index++;
1037 if (index > WMGetArrayItemCount(menu->items)) {
1038 WMAddToArray(menu->items, item);
1039 } else {
1040 WMInsertInArray(menu->items, index, item);
1043 W_ReparentView(item->view, menu->view, 0, index * menu->itemHeight);
1045 item->parent = menu;
1046 if (item->submenu) {
1047 item->submenu->parent = menu;
1050 updateMenuContents(menu);
1053 static void dragMenu(WEditMenu * menu)
1055 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1056 XEvent ev;
1057 Bool done = False;
1058 int dx, dy;
1059 unsigned blau;
1060 Window blaw;
1062 XGetGeometry(scr->display, W_VIEW_DRAWABLE(menu->view), &blaw, &dx, &dy, &blau, &blau, &blau, &blau);
1064 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, dx, dy, &dx, &dy, &blaw);
1066 dx = menu->dragX - dx;
1067 dy = menu->dragY - dy;
1069 XGrabPointer(scr->display, scr->rootWin, False,
1070 ButtonReleaseMask | ButtonMotionMask,
1071 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1073 if (menu->parent)
1074 WTearOffEditMenu(menu->parent, menu);
1076 while (!done) {
1077 WMNextEvent(scr->display, &ev);
1079 switch (ev.type) {
1080 case ButtonRelease:
1081 done = True;
1082 break;
1084 case MotionNotify:
1085 while (XCheckMaskEvent(scr->display, ButtonMotionMask, &ev)) ;
1087 WMMoveWidget(menu, ev.xmotion.x_root - dx, ev.xmotion.y_root - dy);
1088 break;
1090 default:
1091 WMHandleEvent(&ev);
1092 break;
1096 XUngrabPointer(scr->display, CurrentTime);
1099 static WEditMenuItem *duplicateItem(WEditMenuItem * item)
1101 WEditMenuItem *nitem;
1103 nitem = WCreateEditMenuItem(item->parent, item->label, False);
1104 if (item->pixmap)
1105 nitem->pixmap = WMRetainPixmap(item->pixmap);
1107 return nitem;
1110 static WEditMenu *duplicateMenu(WEditMenu * menu)
1112 WEditMenu *nmenu;
1113 WEditMenuItem *item;
1114 WMArrayIterator iter;
1115 Bool first = menu->flags.isTitled;
1117 nmenu = WCreateEditMenu(WMWidgetScreen(menu), WGetEditMenuTitle(menu));
1119 memcpy(&nmenu->flags, &menu->flags, sizeof(menu->flags));
1120 nmenu->delegate = menu->delegate;
1122 WM_ITERATE_ARRAY(menu->items, item, iter) {
1123 WEditMenuItem *nitem;
1125 if (first) {
1126 first = False;
1127 continue;
1130 nitem = WAddMenuItemWithTitle(nmenu, item->label);
1131 if (item->pixmap)
1132 WSetEditMenuItemImage(nitem, item->pixmap);
1134 if (menu->delegate && menu->delegate->itemCloned) {
1135 (*menu->delegate->itemCloned) (menu->delegate, menu, item, nitem);
1139 WMRealizeWidget(nmenu);
1141 return nmenu;
1144 static void dragItem(WEditMenu * menu, WEditMenuItem * item, Bool copy)
1146 static XColor black = { 0, 0, 0, 0, DoRed | DoGreen | DoBlue };
1147 static XColor green = { 0x0045b045, 0x4500, 0xb000, 0x4500, DoRed | DoGreen | DoBlue };
1148 static XColor back = { 0, 0xffff, 0xffff, 0xffff, DoRed | DoGreen | DoBlue };
1149 Display *dpy = W_VIEW_DISPLAY(menu->view);
1150 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1151 int x, y;
1152 int dx, dy;
1153 Bool done = False;
1154 Window blaw;
1155 int blai;
1156 unsigned blau;
1157 Window win;
1158 WMView *dview;
1159 int orix, oriy;
1160 Bool enteredMenu = False;
1161 WMSize oldSize = item->view->size;
1162 WEditMenuItem *dritem = item;
1163 WEditMenu *dmenu = NULL;
1165 if (item->flags.isTitle) {
1166 WMRaiseWidget(menu);
1168 dragMenu(menu);
1170 return;
1173 selectItem(menu, NULL);
1175 win = scr->rootWin;
1177 XTranslateCoordinates(dpy, W_VIEW_DRAWABLE(item->view), win, 0, 0, &orix, &oriy, &blaw);
1179 dview = W_CreateUnmanagedTopView(scr);
1180 W_SetViewBackgroundColor(dview, scr->black);
1181 W_ResizeView(dview, W_VIEW_WIDTH(item->view), W_VIEW_HEIGHT(item->view));
1182 W_MoveView(dview, orix, oriy);
1183 W_RealizeView(dview);
1185 if (menu->flags.isFactory || copy) {
1186 dritem = duplicateItem(item);
1188 W_ReparentView(dritem->view, dview, 0, 0);
1189 WMResizeWidget(dritem, oldSize.width, oldSize.height);
1190 WMRealizeWidget(dritem);
1191 WMMapWidget(dritem);
1192 } else {
1193 W_ReparentView(item->view, dview, 0, 0);
1196 W_MapView(dview);
1198 dx = menu->dragX - orix;
1199 dy = menu->dragY - oriy;
1201 XGrabPointer(dpy, scr->rootWin, False,
1202 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
1203 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1205 if (menu->flags.acceptsDrop)
1206 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
1208 while (!done) {
1209 XEvent ev;
1211 WMNextEvent(dpy, &ev);
1213 switch (ev.type) {
1214 case MotionNotify:
1215 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
1217 XQueryPointer(dpy, win, &blaw, &blaw, &blai, &blai, &x, &y, &blau);
1219 dmenu = findMenuInWindow(dpy, win, x, y);
1221 if (dmenu) {
1222 handleDragOver(dmenu, dview, dritem, x - dx, y - dy);
1223 if (!enteredMenu) {
1224 enteredMenu = True;
1225 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
1227 } else {
1228 if (enteredMenu) {
1229 W_ResizeView(dview, oldSize.width, oldSize.height);
1230 W_ResizeView(dritem->view, oldSize.width, oldSize.height);
1231 enteredMenu = False;
1232 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
1234 W_MoveView(dview, x - dx, y - dy);
1237 break;
1239 case ButtonRelease:
1240 done = True;
1241 break;
1243 default:
1244 WMHandleEvent(&ev);
1245 break;
1248 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
1250 XUngrabPointer(dpy, CurrentTime);
1252 if (!enteredMenu) {
1253 Bool rem = True;
1255 if (!menu->flags.isFactory && !copy) {
1256 W_UnmapView(dview);
1257 if (menu->delegate && menu->delegate->shouldRemoveItem) {
1258 rem = (*menu->delegate->shouldRemoveItem) (menu->delegate, menu, item);
1260 W_MapView(dview);
1263 if (!rem || menu->flags.isFactory || copy) {
1264 slideWindow(dpy, W_VIEW_DRAWABLE(dview), x - dx, y - dy, orix, oriy);
1266 if (!menu->flags.isFactory && !copy) {
1267 WRemoveEditMenuItem(menu, dritem);
1268 handleItemDrop(dmenu ? dmenu : menu, dritem, orix, oriy);
1270 } else {
1271 WRemoveEditMenuItem(menu, dritem);
1273 } else {
1274 WRemoveEditMenuItem(menu, dritem);
1276 if (menu->delegate && menu->delegate->itemCloned && (menu->flags.isFactory || copy)) {
1277 (*menu->delegate->itemCloned) (menu->delegate, menu, item, dritem);
1280 handleItemDrop(dmenu, dritem, x - dy, y - dy);
1282 if (item->submenu && (menu->flags.isFactory || copy)) {
1283 WEditMenu *submenu;
1285 submenu = duplicateMenu(item->submenu);
1286 WSetEditMenuSubmenu(dmenu, dritem, submenu);
1290 /* can't destroy now because we're being called from
1291 * the event handler of the item. and destroying now,
1292 * would mean destroying the item too in some cases.
1293 */
1294 WMAddIdleHandler((WMCallback *) W_DestroyView, dview);
1297 static void handleItemClick(XEvent * event, void *data)
1299 WEditMenuItem *item = (WEditMenuItem *) data;
1300 WEditMenu *menu = item->parent;
1302 switch (event->type) {
1303 case ButtonPress:
1304 selectItem(menu, item);
1306 if (WMIsDoubleClick(event)) {
1307 editItemLabel(item);
1310 menu->flags.isDragging = 1;
1311 menu->dragX = event->xbutton.x_root;
1312 menu->dragY = event->xbutton.y_root;
1313 break;
1315 case ButtonRelease:
1316 menu->flags.isDragging = 0;
1317 break;
1319 case MotionNotify:
1320 if (menu->flags.isDragging) {
1321 if (abs(event->xbutton.x_root - menu->dragX) > 5
1322 || abs(event->xbutton.y_root - menu->dragY) > 5) {
1323 menu->flags.isDragging = 0;
1324 dragItem(menu, item, event->xbutton.state & ControlMask);
1327 break;
1331 static void destroyEditMenu(WEditMenu * mPtr)
1333 WMRemoveNotificationObserver(mPtr);
1335 WMFreeArray(mPtr->items);
1337 wfree(mPtr->tdelegate);
1339 wfree(mPtr);