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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 #include <WINGs/WINGsP.h>
24 #include <WINGs/WUtil.h>
32 typedef struct W_EditMenuItem
{
36 struct W_EditMenu
*parent
;
39 WMPixmap
*pixmap
; /* pixmap to show at left */
42 WMCallback
*destroyData
;
44 struct W_EditMenu
*submenu
; /* if it's a cascade, NULL otherwise */
48 unsigned isHighlighted
:1;
52 typedef struct W_EditMenu
{
56 struct W_EditMenu
*parent
;
58 WMArray
*items
; /* EditMenuItem */
60 EditMenuItem
*selectedItem
;
69 WEditMenuDelegate
*delegate
;
71 WMTextFieldDelegate
*tdelegate
;
77 /* only for non-standalone menu */
82 unsigned standalone
:1;
85 unsigned acceptsDrop
:1;
87 unsigned isSelectable
:1;
88 unsigned isEditable
:1;
92 unsigned isDragging
:1;
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
)
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
));
138 iPtr
->view
->self
= iPtr
;
140 iPtr
->parent
= parent
;
142 iPtr
->label
= wstrdup(title
);
144 iPtr
->flags
.isTitle
= isTitle
;
147 WMSetWidgetBackgroundColor(iPtr
, WMBlackColor(scr
));
150 WMCreateEventHandler(iPtr
->view
, ExposureMask
| StructureNotifyMask
, handleItemEvents
, iPtr
);
152 WMCreateEventHandler(iPtr
->view
, ButtonPressMask
| ButtonReleaseMask
153 | ButtonMotionMask
, handleItemClick
, iPtr
);
158 char *WGetEditMenuItemTitle(WEditMenuItem
* item
)
163 void *WGetEditMenuItemData(WEditMenuItem
* item
)
168 void WSetEditMenuItemData(WEditMenuItem
* item
, void *data
, WMCallback
* destroyer
)
171 item
->destroyData
= destroyer
;
174 void WSetEditMenuItemImage(WEditMenuItem
* item
, WMPixmap
* pixmap
)
177 WMReleasePixmap(item
->pixmap
);
178 item
->pixmap
= WMRetainPixmap(pixmap
);
181 static void paintEditMenuItem(WEditMenuItem
* iPtr
)
183 WMScreen
*scr
= WMWidgetScreen(iPtr
);
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
)
194 if (iPtr
->flags
.isTitle
&& !iPtr
->flags
.isHighlighted
) {
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
));
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
)
221 iPtr
->flags
.isHighlighted
= high
;
224 WMSetWidgetBackgroundColor(iPtr
, WMWhiteColor(WMWidgetScreen(iPtr
)));
226 if (!iPtr
->flags
.isTitle
) {
227 WMSetWidgetBackgroundColor(iPtr
, WMGrayColor(WMWidgetScreen(iPtr
)));
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
));
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
) {
251 if (event
->xexpose
.count
!= 0)
253 paintEditMenuItem(iPtr
);
257 destroyEditMenuItem(iPtr
);
262 static void destroyEditMenuItem(WEditMenuItem
* iPtr
)
266 if (iPtr
->data
&& iPtr
->destroyData
)
267 (*iPtr
->destroyData
) (iPtr
->data
);
269 WMDestroyWidget(iPtr
->submenu
);
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
;
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 */
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
;
353 rmenu
= (WEditMenu
*) WMGetNotificationObject(notif
);
354 /* check whether rmenu is from the same hierarchy of menu? */
360 if (menu
->selectedItem
) {
361 if (!menu
->selectedItem
->submenu
)
363 if (menu
->flags
.isEditing
)
364 stopEditItem(menu
, False
);
368 static WEditMenu
*makeEditMenu(WMScreen
* scr
, WMWidget
* parent
, char *title
)
376 mPtr
= wmalloc(sizeof(WEditMenu
));
377 memset(mPtr
, 0, sizeof(WEditMenu
));
379 mPtr
->widgetClass
= EditMenuClass
;
382 mPtr
->view
= W_CreateView(W_VIEW(parent
));
383 mPtr
->flags
.standalone
= 0;
385 mPtr
->view
= W_CreateTopView(scr
);
386 mPtr
->flags
.standalone
= 1;
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
);
408 item
= WCreateEditMenuItem(mPtr
, title
, True
);
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
);
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
)
443 item
= WCreateEditMenuItem(mPtr
, title
, False
);
447 if (index
>= WMGetArrayItemCount(mPtr
->items
)) {
448 WMAddToArray(mPtr
->items
, item
);
452 if (mPtr
->flags
.isTitled
)
454 WMInsertInArray(mPtr
->items
, index
, item
);
457 updateMenuContents(mPtr
);
462 WEditMenuItem
*WGetEditMenuItem(WEditMenu
* mPtr
, int index
)
464 if (index
>= WMGetArrayItemCount(mPtr
->items
))
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
)
479 item
= WMGetFromArray(mPtr
->items
, 0);
482 item
->label
= wstrdup(title
);
484 updateMenuContents(mPtr
);
486 WMRedisplayWidget(item
);
489 char *WGetEditMenuTitle(WEditMenu
* mPtr
)
493 item
= WMGetFromArray(mPtr
->items
, 0);
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
;
556 if (mPtr
->flags
.isTitled
)
557 y
= -mPtr
->titleHeight
;
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
);
584 void WTearOffEditMenu(WEditMenu
* menu
, WEditMenu
* submenu
)
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
)
604 Bool
WEditMenuIsTornOff(WEditMenu
* mPtr
)
606 return mPtr
->flags
.isTornOff
;
609 void WEditMenuHide(WEditMenu
* mPtr
)
614 if (WMWidgetIsMapped(mPtr
)) {
616 mPtr
->flags
.wasMapped
= 1;
618 mPtr
->flags
.wasMapped
= 0;
620 while ((item
= WGetEditMenuItem(mPtr
, i
++))) {
623 submenu
= WGetEditMenuSubmenu(mPtr
, item
);
625 WEditMenuHide(submenu
);
630 void WEditMenuUnhide(WEditMenu
* mPtr
)
635 if (mPtr
->flags
.wasMapped
) {
638 while ((item
= WGetEditMenuItem(mPtr
, i
++))) {
641 submenu
= WGetEditMenuSubmenu(mPtr
, item
);
643 WEditMenuUnhide(submenu
);
648 void WEditMenuShowAt(WEditMenu
* menu
, int x
, int y
)
652 hints
= XAllocSizeHints();
654 hints
->flags
= USPosition
;
658 WMMoveWidget(menu
, x
, y
);
659 XSetWMNormalHints(W_VIEW_DISPLAY(menu
->view
), W_VIEW_DRAWABLE(menu
->view
), hints
);
665 static void updateMenuContents(WEditMenu
* mPtr
)
670 int iheight
= mPtr
->itemHeight
;
672 WMArrayIterator iter
;
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
;
693 newW
+= iheight
+ 10;
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)
709 W_ResizeView(mPtr
->view
, newW
, newH
+ 1);
712 WMMoveWidget(mPtr
->closeB
, newW
- 20, 3);
717 WM_ITERATE_ARRAY(mPtr
->items
, item
, iter
) {
718 if (i
== 0 && mPtr
->flags
.isTitled
) {
719 WMResizeWidget(item
, newW
, mPtr
->titleHeight
);
721 WMResizeWidget(item
, newW
, iheight
);
727 static void deselectItem(WEditMenu
* menu
)
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
) {
751 if (menu
->selectedItem
) {
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
)) {
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
) {
796 destroyEditMenu(mPtr
);
800 if (event
->xexpose
.count
== 0)
806 /* -------------------------- Menu Label Editing ------------------------ */
808 static void stopEditItem(WEditMenu
* menu
, Bool 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
;
836 if (!menu
->flags
.isEditing
)
839 reason
= (uintptr_t)WMGetNotificationClientData(notif
);
842 case WMEscapeTextMovement
:
843 stopEditItem(menu
, False
);
846 case WMReturnTextMovement
:
847 stopEditItem(menu
, True
);
850 case WMTabTextMovement
:
851 stopEditItem(menu
, True
);
853 i
= WMGetFirstInArray(menu
->items
, menu
->selectedItem
);
854 item
= WMGetFromArray(menu
->items
, i
+ 1);
856 selectItem(menu
, item
);
861 case WMBacktabTextMovement
:
862 stopEditItem(menu
, True
);
864 i
= WMGetFirstInArray(menu
->items
, menu
->selectedItem
);
865 item
= WMGetFromArray(menu
->items
, i
- 1);
867 selectItem(menu
, item
);
874 static WMTextFieldDelegate textFieldDelegate
= {
883 static void editItemLabel(WEditMenuItem
* item
)
885 WEditMenu
*mPtr
= item
->parent
;
888 if (!mPtr
->flags
.isEditable
) {
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
);
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
);
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
)
924 iterations
= WMIN(25, WMAX(abs(dstX
- srcX
), abs(dstY
- 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
);
943 static int errorHandler(Display
* d
, XErrorEvent
* ev
)
949 static WEditMenu
*findMenuInWindow(Display
* dpy
, Window toplevel
, int x
, int y
)
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
) {
967 if (!XQueryTree(dpy
, toplevel
, &foo
, &bar
, &children
, &nchildren
) || children
== 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
) {
984 menu
= findMenuInWindow(dpy
, tmp
, x
- attr
.x
, y
- attr
.y
);
992 XSetErrorHandler(oldHandler
);
998 static void handleDragOver(WEditMenu
* menu
, WMView
* view
, WEditMenuItem
* item
, int x
, int y
)
1000 WMScreen
*scr
= W_VIEW_SCREEN(menu
->view
);
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
);
1023 XTranslateCoordinates(scr
->display
, W_VIEW_DRAWABLE(menu
->view
), scr
->rootWin
, 0, 0, &mx
, &my
, &blaw
);
1026 if (menu
->flags
.isTitled
) {
1027 index
-= menu
->titleHeight
;
1029 index
= (index
+ menu
->itemHeight
/ 2) / menu
->itemHeight
;
1033 if (menu
->flags
.isTitled
) {
1037 if (index
> WMGetArrayItemCount(menu
->items
)) {
1038 WMAddToArray(menu
->items
, item
);
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
);
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
);
1074 WTearOffEditMenu(menu
->parent
, menu
);
1077 WMNextEvent(scr
->display
, &ev
);
1085 while (XCheckMaskEvent(scr
->display
, ButtonMotionMask
, &ev
)) ;
1087 WMMoveWidget(menu
, ev
.xmotion
.x_root
- dx
, ev
.xmotion
.y_root
- dy
);
1096 XUngrabPointer(scr
->display
, CurrentTime
);
1099 static WEditMenuItem
*duplicateItem(WEditMenuItem
* item
)
1101 WEditMenuItem
*nitem
;
1103 nitem
= WCreateEditMenuItem(item
->parent
, item
->label
, False
);
1105 nitem
->pixmap
= WMRetainPixmap(item
->pixmap
);
1110 static WEditMenu
*duplicateMenu(WEditMenu
* menu
)
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
;
1130 nitem
= WAddMenuItemWithTitle(nmenu
, item
->label
);
1132 WSetEditMenuItemImage(nitem
, item
->pixmap
);
1134 if (menu
->delegate
&& menu
->delegate
->itemCloned
) {
1135 (*menu
->delegate
->itemCloned
) (menu
->delegate
, menu
, item
, nitem
);
1139 WMRealizeWidget(nmenu
);
1144 static void dragItem(WEditMenu
* menu
, WEditMenuItem
* item
, Bool copy
)
1146 static XColor black
= { 0, 0, 0, 0, DoRed
| DoGreen
| DoBlue
, 0 };
1147 static XColor green
= { 0x0045b045, 0x4500, 0xb000, 0x4500, DoRed
| DoGreen
| DoBlue
, 0 };
1148 static XColor back
= { 0, 0xffff, 0xffff, 0xffff, DoRed
| DoGreen
| DoBlue
, 0 };
1149 Display
*dpy
= W_VIEW_DISPLAY(menu
->view
);
1150 WMScreen
*scr
= W_VIEW_SCREEN(menu
->view
);
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
);
1173 selectItem(menu
, NULL
);
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
);
1193 W_ReparentView(item
->view
, dview
, 0, 0);
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
);
1211 WMNextEvent(dpy
, &ev
);
1215 while (XCheckMaskEvent(dpy
, ButtonMotionMask
, &ev
)) ;
1217 XQueryPointer(dpy
, win
, &blaw
, &blaw
, &blai
, &blai
, &x
, &y
, &blau
);
1219 dmenu
= findMenuInWindow(dpy
, win
, x
, y
);
1222 handleDragOver(dmenu
, dview
, dritem
, x
- dx
, y
- dy
);
1225 XRecolorCursor(dpy
, scr
->defaultCursor
, &green
, &back
);
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
);
1248 XRecolorCursor(dpy
, scr
->defaultCursor
, &black
, &back
);
1250 XUngrabPointer(dpy
, CurrentTime
);
1255 if (!menu
->flags
.isFactory
&& !copy
) {
1257 if (menu
->delegate
&& menu
->delegate
->shouldRemoveItem
) {
1258 rem
= (*menu
->delegate
->shouldRemoveItem
) (menu
->delegate
, menu
, item
);
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
);
1271 WRemoveEditMenuItem(menu
, dritem
);
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
)) {
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.
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
) {
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
;
1316 menu
->flags
.isDragging
= 0;
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
);
1331 static void destroyEditMenu(WEditMenu
* mPtr
)
1333 WMRemoveNotificationObserver(mPtr
);
1335 WMFreeArray(mPtr
->items
);
1337 wfree(mPtr
->tdelegate
);