Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / editmenu.c
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 */
22
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>
29
30 #include "editmenu.h"
31
32 typedef struct W_EditMenuItem {
33 W_Class widgetClass;
34 WMView *view;
35
36 struct W_EditMenu *parent;
37
38 char *label;
39 WMPixmap *pixmap; /* pixmap to show at left */
40
41 void *data;
42 WMCallback *destroyData;
43
44 struct W_EditMenu *submenu; /* if it's a cascade, NULL otherwise */
45
46 struct {
47 unsigned isTitle:1;
48 unsigned isHighlighted:1;
49 } flags;
50 } EditMenuItem;
51
52 typedef struct W_EditMenu {
53 W_Class widgetClass;
54 WMView *view;
55
56 struct W_EditMenu *parent;
57
58 WMArray *items; /* EditMenuItem */
59
60 EditMenuItem *selectedItem;
61
62 WMTextField *tfield;
63
64 WMButton *closeB;
65
66 int titleHeight;
67 int itemHeight;
68
69 WEditMenuDelegate *delegate;
70
71 WMTextFieldDelegate *tdelegate;
72
73 /* item dragging */
74 int draggedItem;
75 int dragX, dragY;
76
77 /* only for non-standalone menu */
78 WMSize maxSize;
79 WMSize minSize;
80
81 struct {
82 unsigned standalone:1;
83 unsigned isTitled:1;
84
85 unsigned acceptsDrop:1;
86 unsigned isFactory:1;
87 unsigned isSelectable:1;
88 unsigned isEditable:1;
89
90 unsigned isTornOff:1;
91
92 unsigned isDragging:1;
93 unsigned isEditing:1;
94
95 unsigned wasMapped:1;
96 } flags;
97 } EditMenu;
98
99 /******************** WEditMenuItem ********************/
100
101 static void destroyEditMenuItem(WEditMenuItem * iPtr);
102 static void paintEditMenuItem(WEditMenuItem * iPtr);
103 static void handleItemEvents(XEvent * event, void *data);
104
105 static void handleItemClick(XEvent * event, void *data);
106
107 static W_Class EditMenuItemClass = 0;
108
109 W_Class InitEditMenuItem(WMScreen * scr)
110 {
111 /* register our widget with WINGs and get our widget class ID */
112 if (!EditMenuItemClass) {
113 EditMenuItemClass = W_RegisterUserWidget();
114 }
115
116 return EditMenuItemClass;
117 }
118
119 WEditMenuItem *WCreateEditMenuItem(WMWidget * parent, char *title, Bool isTitle)
120 {
121 WEditMenuItem *iPtr;
122 WMScreen *scr = WMWidgetScreen(parent);
123
124 if (!EditMenuItemClass)
125 InitEditMenuItem(scr);
126
127 iPtr = wmalloc(sizeof(WEditMenuItem));
128
129 memset(iPtr, 0, sizeof(WEditMenuItem));
130
131 iPtr->widgetClass = EditMenuItemClass;
132
133 iPtr->view = W_CreateView(W_VIEW(parent));
134 if (!iPtr->view) {
135 wfree(iPtr);
136 return NULL;
137 }
138 iPtr->view->self = iPtr;
139
140 iPtr->parent = parent;
141
142 iPtr->label = wstrdup(title);
143
144 iPtr->flags.isTitle = isTitle;
145
146 if (isTitle) {
147 WMSetWidgetBackgroundColor(iPtr, WMBlackColor(scr));
148 }
149
150 WMCreateEventHandler(iPtr->view, ExposureMask | StructureNotifyMask, handleItemEvents, iPtr);
151
152 WMCreateEventHandler(iPtr->view, ButtonPressMask | ButtonReleaseMask
153 | ButtonMotionMask, handleItemClick, iPtr);
154
155 return iPtr;
156 }
157
158 char *WGetEditMenuItemTitle(WEditMenuItem * item)
159 {
160 return item->label;
161 }
162
163 void *WGetEditMenuItemData(WEditMenuItem * item)
164 {
165 return item->data;
166 }
167
168 void WSetEditMenuItemData(WEditMenuItem * item, void *data, WMCallback * destroyer)
169 {
170 item->data = data;
171 item->destroyData = destroyer;
172 }
173
174 void WSetEditMenuItemImage(WEditMenuItem * item, WMPixmap * pixmap)
175 {
176 if (item->pixmap)
177 WMReleasePixmap(item->pixmap);
178 item->pixmap = WMRetainPixmap(pixmap);
179 }
180
181 static void paintEditMenuItem(WEditMenuItem * iPtr)
182 {
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);
189
190 if (!iPtr->view->flags.realized)
191 return;
192
193 color = scr->black;
194 if (iPtr->flags.isTitle && !iPtr->flags.isHighlighted) {
195 color = scr->white;
196 }
197
198 XClearWindow(scr->display, win);
199
200 W_DrawRelief(scr, win, 0, 0, w + 1, h, WRRaised);
201
202 WMDrawString(scr, win, color, font, 5, 3, iPtr->label, strlen(iPtr->label));
203
204 if (iPtr->pixmap) {
205 WMSize size = WMGetPixmapSize(iPtr->pixmap);
206
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);
213 }
214 }
215
216 static void highlightItem(WEditMenuItem * iPtr, Bool high)
217 {
218 if (iPtr->flags.isTitle)
219 return;
220
221 iPtr->flags.isHighlighted = high;
222
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)));
230 }
231 }
232 }
233
234 static int getItemTextWidth(WEditMenuItem * iPtr)
235 {
236 WMScreen *scr = WMWidgetScreen(iPtr);
237
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));
242 }
243 }
244
245 static void handleItemEvents(XEvent * event, void *data)
246 {
247 WEditMenuItem *iPtr = (WEditMenuItem *) data;
248
249 switch (event->type) {
250 case Expose:
251 if (event->xexpose.count != 0)
252 break;
253 paintEditMenuItem(iPtr);
254 break;
255
256 case DestroyNotify:
257 destroyEditMenuItem(iPtr);
258 break;
259 }
260 }
261
262 static void destroyEditMenuItem(WEditMenuItem * iPtr)
263 {
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);
270
271 wfree(iPtr);
272 }
273
274 /******************** WEditMenu *******************/
275
276 static void destroyEditMenu(WEditMenu * mPtr);
277
278 static void updateMenuContents(WEditMenu * mPtr);
279
280 static void handleEvents(XEvent * event, void *data);
281
282 static void editItemLabel(WEditMenuItem * item);
283
284 static void stopEditItem(WEditMenu * menu, Bool apply);
285
286 static void deselectItem(WEditMenu * menu);
287
288 static W_Class EditMenuClass = 0;
289
290 W_Class InitEditMenu(WMScreen * scr)
291 {
292 /* register our widget with WINGs and get our widget class ID */
293 if (!EditMenuClass) {
294
295 EditMenuClass = W_RegisterUserWidget();
296 }
297
298 return EditMenuClass;
299 }
300
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;
312
313 #define GSWindowStyleAttr (1<<0)
314 #define GSWindowLevelAttr (1<<1)
315
316 static void writeGNUstepWMAttr(WMScreen * scr, Window window, GNUstepWMAttributes * attr)
317 {
318 unsigned long data[9];
319
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);
333 }
334
335 static void realizeObserver(void *self, WMNotification * not)
336 {
337 WEditMenu *menu = (WEditMenu *) self;
338 GNUstepWMAttributes attribs;
339
340 memset(&attribs, 0, sizeof(GNUstepWMAttributes));
341 attribs.flags = GSWindowStyleAttr | GSWindowLevelAttr;
342 attribs.window_style = WMBorderlessWindowMask;
343 attribs.window_level = WMSubmenuWindowLevel;
344
345 writeGNUstepWMAttr(WMWidgetScreen(menu), menu->view->window, &attribs);
346 }
347
348 static void itemSelectObserver(void *self, WMNotification * notif)
349 {
350 WEditMenu *menu = (WEditMenu *) self;
351 WEditMenu *rmenu;
352
353 rmenu = (WEditMenu *) WMGetNotificationObject(notif);
354 /* check whether rmenu is from the same hierarchy of menu? */
355
356 if (rmenu == menu) {
357 return;
358 }
359
360 if (menu->selectedItem) {
361 if (!menu->selectedItem->submenu)
362 deselectItem(menu);
363 if (menu->flags.isEditing)
364 stopEditItem(menu, False);
365 }
366 }
367
368 static WEditMenu *makeEditMenu(WMScreen * scr, WMWidget * parent, char *title)
369 {
370 WEditMenu *mPtr;
371 WEditMenuItem *item;
372
373 if (!EditMenuClass)
374 InitEditMenu(scr);
375
376 mPtr = wmalloc(sizeof(WEditMenu));
377 memset(mPtr, 0, sizeof(WEditMenu));
378
379 mPtr->widgetClass = EditMenuClass;
380
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;
387 }
388 if (!mPtr->view) {
389 wfree(mPtr);
390 return NULL;
391 }
392 mPtr->view->self = mPtr;
393
394 mPtr->flags.isSelectable = 1;
395 mPtr->flags.isEditable = 1;
396
397 W_SetViewBackgroundColor(mPtr->view, scr->darkGray);
398
399 WMAddNotificationObserver(realizeObserver, mPtr, WMViewRealizedNotification, mPtr->view);
400
401 WMAddNotificationObserver(itemSelectObserver, mPtr, "EditMenuItemSelected", NULL);
402
403 mPtr->items = WMCreateArray(4);
404
405 WMCreateEventHandler(mPtr->view, ExposureMask | StructureNotifyMask, handleEvents, mPtr);
406
407 if (title != NULL) {
408 item = WCreateEditMenuItem(mPtr, title, True);
409
410 WMMapWidget(item);
411 WMAddToArray(mPtr->items, item);
412
413 mPtr->flags.isTitled = 1;
414 }
415
416 mPtr->itemHeight = WMFontHeight(scr->normalFont) + 6;
417 mPtr->titleHeight = WMFontHeight(scr->boldFont) + 8;
418
419 updateMenuContents(mPtr);
420
421 return mPtr;
422 }
423
424 WEditMenu *WCreateEditMenu(WMScreen * scr, char *title)
425 {
426 return makeEditMenu(scr, NULL, title);
427 }
428
429 WEditMenu *WCreateEditMenuPad(WMWidget * parent)
430 {
431 return makeEditMenu(WMWidgetScreen(parent), parent, NULL);
432 }
433
434 void WSetEditMenuDelegate(WEditMenu * mPtr, WEditMenuDelegate * delegate)
435 {
436 mPtr->delegate = delegate;
437 }
438
439 WEditMenuItem *WInsertMenuItemWithTitle(WEditMenu * mPtr, int index, char *title)
440 {
441 WEditMenuItem *item;
442
443 item = WCreateEditMenuItem(mPtr, title, False);
444
445 WMMapWidget(item);
446
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);
455 }
456
457 updateMenuContents(mPtr);
458
459 return item;
460 }
461
462 WEditMenuItem *WGetEditMenuItem(WEditMenu * mPtr, int index)
463 {
464 if (index >= WMGetArrayItemCount(mPtr->items))
465 return NULL;
466
467 return WMGetFromArray(mPtr->items, index + (mPtr->flags.isTitled ? 1 : 0));
468 }
469
470 WEditMenuItem *WAddMenuItemWithTitle(WEditMenu * mPtr, char *title)
471 {
472 return WInsertMenuItemWithTitle(mPtr, WMGetArrayItemCount(mPtr->items), title);
473 }
474
475 void WSetEditMenuTitle(WEditMenu * mPtr, char *title)
476 {
477 WEditMenuItem *item;
478
479 item = WMGetFromArray(mPtr->items, 0);
480
481 wfree(item->label);
482 item->label = wstrdup(title);
483
484 updateMenuContents(mPtr);
485
486 WMRedisplayWidget(item);
487 }
488
489 char *WGetEditMenuTitle(WEditMenu * mPtr)
490 {
491 WEditMenuItem *item;
492
493 item = WMGetFromArray(mPtr->items, 0);
494
495 return item->label;
496 }
497
498 void WSetEditMenuAcceptsDrop(WEditMenu * mPtr, Bool flag)
499 {
500 mPtr->flags.acceptsDrop = flag;
501 }
502
503 void WSetEditMenuSubmenu(WEditMenu * mPtr, WEditMenuItem * item, WEditMenu * submenu)
504 {
505 item->submenu = submenu;
506 submenu->parent = mPtr;
507
508 paintEditMenuItem(item);
509 }
510
511 WEditMenu *WGetEditMenuSubmenu(WEditMenu * mPtr, WEditMenuItem * item)
512 {
513 return item->submenu;
514 }
515
516 void WRemoveEditMenuItem(WEditMenu * mPtr, WEditMenuItem * item)
517 {
518 if (WMRemoveFromArray(mPtr->items, item) != 0) {
519 updateMenuContents(mPtr);
520 }
521 }
522
523 void WSetEditMenuSelectable(WEditMenu * mPtr, Bool flag)
524 {
525 mPtr->flags.isSelectable = flag;
526 }
527
528 void WSetEditMenuEditable(WEditMenu * mPtr, Bool flag)
529 {
530 mPtr->flags.isEditable = flag;
531 }
532
533 void WSetEditMenuIsFactory(WEditMenu * mPtr, Bool flag)
534 {
535 mPtr->flags.isFactory = flag;
536 }
537
538 void WSetEditMenuMinSize(WEditMenu * mPtr, WMSize size)
539 {
540 mPtr->minSize.width = size.width;
541 mPtr->minSize.height = size.height;
542 }
543
544 void WSetEditMenuMaxSize(WEditMenu * mPtr, WMSize size)
545 {
546 mPtr->maxSize.width = size.width;
547 mPtr->maxSize.height = size.height;
548 }
549
550 WMPoint WGetEditMenuLocationForSubmenu(WEditMenu * mPtr, WEditMenu * submenu)
551 {
552 WMArrayIterator iter;
553 WEditMenuItem *item;
554 int y;
555
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);
563
564 return wmkpoint(pt.x + mPtr->view->size.width, pt.y + y);
565 }
566 y += W_VIEW_HEIGHT(item->view);
567 }
568
569 puts("invalid submenu passed to WGetEditMenuLocationForSubmenu()");
570
571 return wmkpoint(0, 0);
572 }
573
574 static void closeMenuAction(WMWidget * w, void *data)
575 {
576 WEditMenu *menu = (WEditMenu *) data;
577
578 WMAddIdleHandler(WMDestroyWidget, menu->closeB);
579 menu->closeB = NULL;
580
581 WEditMenuHide(menu);
582 }
583
584 void WTearOffEditMenu(WEditMenu * menu, WEditMenu * submenu)
585 {
586 WEditMenuItem *item;
587
588 submenu->flags.isTornOff = 1;
589
590 item = (WEditMenuItem *) WMGetFromArray(submenu->items, 0);
591
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);
599
600 if (menu->selectedItem && menu->selectedItem->submenu == submenu)
601 deselectItem(menu);
602 }
603
604 Bool WEditMenuIsTornOff(WEditMenu * mPtr)
605 {
606 return mPtr->flags.isTornOff;
607 }
608
609 void WEditMenuHide(WEditMenu * mPtr)
610 {
611 WEditMenuItem *item;
612 int i = 0;
613
614 if (WMWidgetIsMapped(mPtr)) {
615 WMUnmapWidget(mPtr);
616 mPtr->flags.wasMapped = 1;
617 } else {
618 mPtr->flags.wasMapped = 0;
619 }
620 while ((item = WGetEditMenuItem(mPtr, i++))) {
621 WEditMenu *submenu;
622
623 submenu = WGetEditMenuSubmenu(mPtr, item);
624 if (submenu) {
625 WEditMenuHide(submenu);
626 }
627 }
628 }
629
630 void WEditMenuUnhide(WEditMenu * mPtr)
631 {
632 WEditMenuItem *item;
633 int i = 0;
634
635 if (mPtr->flags.wasMapped) {
636 WMMapWidget(mPtr);
637 }
638 while ((item = WGetEditMenuItem(mPtr, i++))) {
639 WEditMenu *submenu;
640
641 submenu = WGetEditMenuSubmenu(mPtr, item);
642 if (submenu) {
643 WEditMenuUnhide(submenu);
644 }
645 }
646 }
647
648 void WEditMenuShowAt(WEditMenu * menu, int x, int y)
649 {
650 XSizeHints *hints;
651
652 hints = XAllocSizeHints();
653
654 hints->flags = USPosition;
655 hints->x = x;
656 hints->y = y;
657
658 WMMoveWidget(menu, x, y);
659 XSetWMNormalHints(W_VIEW_DISPLAY(menu->view), W_VIEW_DRAWABLE(menu->view), hints);
660 XFree(hints);
661
662 WMMapWidget(menu);
663 }
664
665 static void updateMenuContents(WEditMenu * mPtr)
666 {
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;
674
675 newW = 0;
676 newH = offs;
677
678 i = 0;
679 WM_ITERATE_ARRAY(mPtr->items, item, iter) {
680 w = getItemTextWidth(item);
681
682 newW = WMAX(w, newW);
683
684 WMMoveWidget(item, offs, newH);
685 if (i == 0 && mPtr->flags.isTitled) {
686 newH += mPtr->titleHeight;
687 } else {
688 newH += iheight;
689 }
690 i = 1;
691 }
692
693 newW += iheight + 10;
694 newH--;
695
696 if (mPtr->minSize.width)
697 newW = WMAX(newW, mPtr->minSize.width);
698 if (mPtr->maxSize.width)
699 newW = WMIN(newW, mPtr->maxSize.width);
700
701 if (mPtr->minSize.height)
702 newH = WMAX(newH, mPtr->minSize.height);
703 if (mPtr->maxSize.height)
704 newH = WMIN(newH, mPtr->maxSize.height);
705
706 if (W_VIEW(mPtr)->size.width == newW && mPtr->view->size.height == newH + 1)
707 return;
708
709 W_ResizeView(mPtr->view, newW, newH + 1);
710
711 if (mPtr->closeB)
712 WMMoveWidget(mPtr->closeB, newW - 20, 3);
713
714 newW -= 2 * offs;
715
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);
722 }
723 i = 1;
724 }
725 }
726
727 static void deselectItem(WEditMenu * menu)
728 {
729 WEditMenu *submenu;
730 WEditMenuItem *item = menu->selectedItem;
731
732 highlightItem(item, False);
733
734 if (menu->delegate && menu->delegate->itemDeselected) {
735 (*menu->delegate->itemDeselected) (menu->delegate, menu, item);
736 }
737 submenu = item->submenu;
738
739 if (submenu && !WEditMenuIsTornOff(submenu)) {
740 WEditMenuHide(submenu);
741 }
742
743 menu->selectedItem = NULL;
744 }
745
746 static void selectItem(WEditMenu * menu, WEditMenuItem * item)
747 {
748 if (!menu->flags.isSelectable || menu->selectedItem == item) {
749 return;
750 }
751 if (menu->selectedItem) {
752 deselectItem(menu);
753 }
754
755 if (menu->flags.isEditing) {
756 stopEditItem(menu, False);
757 }
758
759 if (item && !item->flags.isTitle) {
760 highlightItem(item, True);
761
762 if (item->submenu && !W_VIEW_MAPPED(item->submenu->view)) {
763 WMPoint pt;
764
765 pt = WGetEditMenuLocationForSubmenu(menu, item->submenu);
766
767 WEditMenuShowAt(item->submenu, pt.x, pt.y);
768
769 item->submenu->flags.isTornOff = 0;
770 }
771
772 WMPostNotificationName("EditMenuItemSelected", menu, NULL);
773
774 if (menu->delegate && menu->delegate->itemSelected) {
775 (*menu->delegate->itemSelected) (menu->delegate, menu, item);
776 }
777 }
778
779 menu->selectedItem = item;
780 }
781
782 static void paintMenu(WEditMenu * mPtr)
783 {
784 W_View *view = mPtr->view;
785
786 W_DrawRelief(W_VIEW_SCREEN(view), W_VIEW_DRAWABLE(view), 0, 0,
787 W_VIEW_WIDTH(view), W_VIEW_HEIGHT(view), WRSimple);
788 }
789
790 static void handleEvents(XEvent * event, void *data)
791 {
792 WEditMenu *mPtr = (WEditMenu *) data;
793
794 switch (event->type) {
795 case DestroyNotify:
796 destroyEditMenu(mPtr);
797 break;
798
799 case Expose:
800 if (event->xexpose.count == 0)
801 paintMenu(mPtr);
802 break;
803 }
804 }
805
806 /* -------------------------- Menu Label Editing ------------------------ */
807
808 static void stopEditItem(WEditMenu * menu, Bool apply)
809 {
810 char *text;
811
812 if (apply) {
813 text = WMGetTextFieldText(menu->tfield);
814
815 wfree(menu->selectedItem->label);
816 menu->selectedItem->label = wstrdup(text);
817
818 updateMenuContents(menu);
819
820 if (menu->delegate && menu->delegate->itemEdited) {
821 (*menu->delegate->itemEdited) (menu->delegate, menu, menu->selectedItem);
822 }
823
824 }
825 WMUnmapWidget(menu->tfield);
826 menu->flags.isEditing = 0;
827 }
828
829 static void textEndedEditing(struct WMTextFieldDelegate *self, WMNotification * notif)
830 {
831 WEditMenu *menu = (WEditMenu *) self->data;
832 int reason;
833 int i;
834 WEditMenuItem *item;
835
836 if (!menu->flags.isEditing)
837 return;
838
839 reason = (int)(uintptr_t) WMGetNotificationClientData(notif);
840
841 switch (reason) {
842 case WMEscapeTextMovement:
843 stopEditItem(menu, False);
844 break;
845
846 case WMReturnTextMovement:
847 stopEditItem(menu, True);
848 break;
849
850 case WMTabTextMovement:
851 stopEditItem(menu, True);
852
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);
858 }
859 break;
860
861 case WMBacktabTextMovement:
862 stopEditItem(menu, True);
863
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);
869 }
870 break;
871 }
872 }
873
874 static WMTextFieldDelegate textFieldDelegate = {
875 NULL,
876 NULL,
877 NULL,
878 textEndedEditing,
879 NULL,
880 NULL
881 };
882
883 static void editItemLabel(WEditMenuItem * item)
884 {
885 WEditMenu *mPtr = item->parent;
886 WMTextField *tf;
887
888 if (!mPtr->flags.isEditable) {
889 return;
890 }
891
892 if (!mPtr->tfield) {
893 mPtr->tfield = WMCreateTextField(mPtr);
894 WMSetTextFieldBeveled(mPtr->tfield, False);
895 WMRealizeWidget(mPtr->tfield);
896
897 mPtr->tdelegate = wmalloc(sizeof(WMTextFieldDelegate));
898 memcpy(mPtr->tdelegate, &textFieldDelegate, sizeof(WMTextFieldDelegate));
899
900 mPtr->tdelegate->data = mPtr;
901
902 WMSetTextFieldDelegate(mPtr->tfield, mPtr->tdelegate);
903 }
904 tf = mPtr->tfield;
905
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);
912
913 mPtr->flags.isEditing = 1;
914 }
915
916 /* -------------------------------------------------- */
917
918 static void slideWindow(Display * dpy, Window win, int srcX, int srcY, int dstX, int dstY)
919 {
920 double x, y, dx, dy;
921 int i;
922 int iterations;
923
924 iterations = WMIN(25, WMAX(abs(dstX - srcX), abs(dstY - srcY)));
925
926 x = srcX;
927 y = srcY;
928
929 dx = (double)(dstX - srcX) / iterations;
930 dy = (double)(dstY - srcY) / iterations;
931
932 for (i = 0; i <= iterations; i++) {
933 XMoveWindow(dpy, win, x, y);
934 XFlush(dpy);
935
936 wusleep(800);
937
938 x += dx;
939 y += dy;
940 }
941 }
942
943 static int errorHandler(Display * d, XErrorEvent * ev)
944 {
945 /* just ignore */
946 return 0;
947 }
948
949 static WEditMenu *findMenuInWindow(Display * dpy, Window toplevel, int x, int y)
950 {
951 Window foo, bar;
952 Window *children;
953 unsigned nchildren;
954 int i;
955 WEditMenu *menu;
956 WMView *view;
957 int (*oldHandler) (Display *, XErrorEvent *);
958
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;
964 }
965 }
966
967 if (!XQueryTree(dpy, toplevel, &foo, &bar, &children, &nchildren) || children == NULL) {
968 return NULL;
969 }
970
971 oldHandler = XSetErrorHandler(errorHandler);
972
973 /* first window that contains the point is the one */
974 for (i = nchildren - 1; i >= 0; i--) {
975 XWindowAttributes attr;
976
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;
981
982 tmp = children[i];
983
984 menu = findMenuInWindow(dpy, tmp, x - attr.x, y - attr.y);
985 if (menu) {
986 XFree(children);
987 return menu;
988 }
989 }
990 }
991
992 XSetErrorHandler(oldHandler);
993
994 XFree(children);
995 return NULL;
996 }
997
998 static void handleDragOver(WEditMenu * menu, WMView * view, WEditMenuItem * item, int x, int y)
999 {
1000 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1001 Window blaw;
1002 int mx, my;
1003 int offs;
1004
1005 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1006
1007 offs = menu->flags.standalone ? 0 : 1;
1008
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);
1013 }
1014 }
1015
1016 static void handleItemDrop(WEditMenu * menu, WEditMenuItem * item, int x, int y)
1017 {
1018 WMScreen *scr = W_VIEW_SCREEN(menu->view);
1019 Window blaw;
1020 int mx, my;
1021 int index;
1022
1023 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, 0, 0, &mx, &my, &blaw);
1024
1025 index = y - my;
1026 if (menu->flags.isTitled) {
1027 index -= menu->titleHeight;
1028 }
1029 index = (index + menu->itemHeight / 2) / menu->itemHeight;
1030 if (index < 0)
1031 index = 0;
1032
1033 if (menu->flags.isTitled) {
1034 index++;
1035 }
1036
1037 if (index > WMGetArrayItemCount(menu->items)) {
1038 WMAddToArray(menu->items, item);
1039 } else {
1040 WMInsertInArray(menu->items, index, item);
1041 }
1042
1043 W_ReparentView(item->view, menu->view, 0, index * menu->itemHeight);
1044
1045 item->parent = menu;
1046 if (item->submenu) {
1047 item->submenu->parent = menu;
1048 }
1049
1050 updateMenuContents(menu);
1051 }
1052
1053 static void dragMenu(WEditMenu * menu)
1054 {
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;
1061
1062 XGetGeometry(scr->display, W_VIEW_DRAWABLE(menu->view), &blaw, &dx, &dy, &blau, &blau, &blau, &blau);
1063
1064 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(menu->view), scr->rootWin, dx, dy, &dx, &dy, &blaw);
1065
1066 dx = menu->dragX - dx;
1067 dy = menu->dragY - dy;
1068
1069 XGrabPointer(scr->display, scr->rootWin, False,
1070 ButtonReleaseMask | ButtonMotionMask,
1071 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1072
1073 if (menu->parent)
1074 WTearOffEditMenu(menu->parent, menu);
1075
1076 while (!done) {
1077 WMNextEvent(scr->display, &ev);
1078
1079 switch (ev.type) {
1080 case ButtonRelease:
1081 done = True;
1082 break;
1083
1084 case MotionNotify:
1085 while (XCheckMaskEvent(scr->display, ButtonMotionMask, &ev)) ;
1086
1087 WMMoveWidget(menu, ev.xmotion.x_root - dx, ev.xmotion.y_root - dy);
1088 break;
1089
1090 default:
1091 WMHandleEvent(&ev);
1092 break;
1093 }
1094 }
1095
1096 XUngrabPointer(scr->display, CurrentTime);
1097 }
1098
1099 static WEditMenuItem *duplicateItem(WEditMenuItem * item)
1100 {
1101 WEditMenuItem *nitem;
1102
1103 nitem = WCreateEditMenuItem(item->parent, item->label, False);
1104 if (item->pixmap)
1105 nitem->pixmap = WMRetainPixmap(item->pixmap);
1106
1107 return nitem;
1108 }
1109
1110 static WEditMenu *duplicateMenu(WEditMenu * menu)
1111 {
1112 WEditMenu *nmenu;
1113 WEditMenuItem *item;
1114 WMArrayIterator iter;
1115 Bool first = menu->flags.isTitled;
1116
1117 nmenu = WCreateEditMenu(WMWidgetScreen(menu), WGetEditMenuTitle(menu));
1118
1119 memcpy(&nmenu->flags, &menu->flags, sizeof(menu->flags));
1120 nmenu->delegate = menu->delegate;
1121
1122 WM_ITERATE_ARRAY(menu->items, item, iter) {
1123 WEditMenuItem *nitem;
1124
1125 if (first) {
1126 first = False;
1127 continue;
1128 }
1129
1130 nitem = WAddMenuItemWithTitle(nmenu, item->label);
1131 if (item->pixmap)
1132 WSetEditMenuItemImage(nitem, item->pixmap);
1133
1134 if (menu->delegate && menu->delegate->itemCloned) {
1135 (*menu->delegate->itemCloned) (menu->delegate, menu, item, nitem);
1136 }
1137 }
1138
1139 WMRealizeWidget(nmenu);
1140
1141 return nmenu;
1142 }
1143
1144 static void dragItem(WEditMenu * menu, WEditMenuItem * item, Bool copy)
1145 {
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;
1164
1165 if (item->flags.isTitle) {
1166 WMRaiseWidget(menu);
1167
1168 dragMenu(menu);
1169
1170 return;
1171 }
1172
1173 selectItem(menu, NULL);
1174
1175 win = scr->rootWin;
1176
1177 XTranslateCoordinates(dpy, W_VIEW_DRAWABLE(item->view), win, 0, 0, &orix, &oriy, &blaw);
1178
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);
1184
1185 if (menu->flags.isFactory || copy) {
1186 dritem = duplicateItem(item);
1187
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);
1194 }
1195
1196 W_MapView(dview);
1197
1198 dx = menu->dragX - orix;
1199 dy = menu->dragY - oriy;
1200
1201 XGrabPointer(dpy, scr->rootWin, False,
1202 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
1203 GrabModeAsync, GrabModeAsync, None, scr->defaultCursor, CurrentTime);
1204
1205 if (menu->flags.acceptsDrop)
1206 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
1207
1208 while (!done) {
1209 XEvent ev;
1210
1211 WMNextEvent(dpy, &ev);
1212
1213 switch (ev.type) {
1214 case MotionNotify:
1215 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
1216
1217 XQueryPointer(dpy, win, &blaw, &blaw, &blai, &blai, &x, &y, &blau);
1218
1219 dmenu = findMenuInWindow(dpy, win, x, y);
1220
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);
1226 }
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);
1233 }
1234 W_MoveView(dview, x - dx, y - dy);
1235 }
1236
1237 break;
1238
1239 case ButtonRelease:
1240 done = True;
1241 break;
1242
1243 default:
1244 WMHandleEvent(&ev);
1245 break;
1246 }
1247 }
1248 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
1249
1250 XUngrabPointer(dpy, CurrentTime);
1251
1252 if (!enteredMenu) {
1253 Bool rem = True;
1254
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);
1259 }
1260 W_MapView(dview);
1261 }
1262
1263 if (!rem || menu->flags.isFactory || copy) {
1264 slideWindow(dpy, W_VIEW_DRAWABLE(dview), x - dx, y - dy, orix, oriy);
1265
1266 if (!menu->flags.isFactory && !copy) {
1267 WRemoveEditMenuItem(menu, dritem);
1268 handleItemDrop(dmenu ? dmenu : menu, dritem, orix, oriy);
1269 }
1270 } else {
1271 WRemoveEditMenuItem(menu, dritem);
1272 }
1273 } else {
1274 WRemoveEditMenuItem(menu, dritem);
1275
1276 if (menu->delegate && menu->delegate->itemCloned && (menu->flags.isFactory || copy)) {
1277 (*menu->delegate->itemCloned) (menu->delegate, menu, item, dritem);
1278 }
1279
1280 handleItemDrop(dmenu, dritem, x - dy, y - dy);
1281
1282 if (item->submenu && (menu->flags.isFactory || copy)) {
1283 WEditMenu *submenu;
1284
1285 submenu = duplicateMenu(item->submenu);
1286 WSetEditMenuSubmenu(dmenu, dritem, submenu);
1287 }
1288 }
1289
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);
1295 }
1296
1297 static void handleItemClick(XEvent * event, void *data)
1298 {
1299 WEditMenuItem *item = (WEditMenuItem *) data;
1300 WEditMenu *menu = item->parent;
1301
1302 switch (event->type) {
1303 case ButtonPress:
1304 selectItem(menu, item);
1305
1306 if (WMIsDoubleClick(event)) {
1307 editItemLabel(item);
1308 }
1309
1310 menu->flags.isDragging = 1;
1311 menu->dragX = event->xbutton.x_root;
1312 menu->dragY = event->xbutton.y_root;
1313 break;
1314
1315 case ButtonRelease:
1316 menu->flags.isDragging = 0;
1317 break;
1318
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);
1325 }
1326 }
1327 break;
1328 }
1329 }
1330
1331 static void destroyEditMenu(WEditMenu * mPtr)
1332 {
1333 WMRemoveNotificationObserver(mPtr);
1334
1335 WMFreeArray(mPtr->items);
1336
1337 wfree(mPtr->tdelegate);
1338
1339 wfree(mPtr);
1340 }