MenuPanel.st
changeset 2249 cb1073891ea8
parent 2238 68ecae98bcd2
child 2251 30490c3f2173
--- a/MenuPanel.st	Fri Oct 04 11:46:38 2002 +0200
+++ b/MenuPanel.st	Sat Oct 05 18:25:28 2002 +0200
@@ -2791,6 +2791,7 @@
 "/            selection isNil ifTrue:[
 "/                self accept:nil keepValue:true
 "/            ]
+        ] ifTrue:[
         ]
     ].
 !
@@ -2924,10 +2925,7 @@
                     ^ self pointerEntersItem:nil
                 ].
                 (superMenu notNil and:[self selectionFollowsMouse]) ifTrue:[
-                    self select:sel openMenu:true.
-"/                    self openMenuOnSelect:true.
-"/                    self selection:sel.
-"/                    self openMenuOnSelect:nil.
+                    self selection:sel openMenu:true.
                 ] ifFalse:[
                     self pointerEntersItem:sel
                 ].
@@ -2943,10 +2941,7 @@
             sel   := menu itemAtPoint:translatedPoint.
 
             (sel notNil and:[sel canSelect]) ifTrue:[
-                menu select:sel openMenu:true.
-"/                self openMenuOnSelect:true.
-"/                self selection:sel.
-"/                self openMenuOnSelect:nil.
+                menu selection:sel openMenu:true.
             ]
         ].
         ^ self
@@ -2963,10 +2958,7 @@
 
     menu == self ifTrue:[
         (sel := self itemAtPoint:motionPoint) notNil ifTrue:[
-            self select:sel openMenu:true.
-"/            self openMenuOnSelect:true.
-"/            self selection:sel.
-"/            self openMenuOnSelect:nil.
+            self selection:sel openMenu:true.
         ].
     ] ifFalse:[
         translatedPoint := self translateMenuPoint:motionPoint toMenu:menu.
@@ -2975,10 +2967,7 @@
         (sel isNil and:[menu isPopUpView not]) ifTrue:[
             sel := menu selection.
         ].
-        menu select:sel openMenu:true.
-"/        menu openMenuOnSelect:true.
-"/        menu selection:sel.
-"/        menu openMenuOnSelect:nil.
+        menu selection:sel openMenu:true.
     ]
 
     "Created: / 13.11.2001 / 20:21:24 / cg"
@@ -3001,11 +2990,7 @@
             ]
         ]
     ].
-
-    self openMenuOnSelect:true.
-    self selection:item.
-    self openMenuOnSelect:nil.
-
+    self selection:item openMenu:true.
     item isNil ifTrue:[^ self].
     item hasDelayedMenu ifTrue:[^ self].
 
@@ -3068,9 +3053,14 @@
 
             (el := items at:idx ifAbsent:nil) notNil ifTrue:[
                 el canSelect ifTrue:[
-                    self   selection:el.
-                    self openDelayed:nil. "/ do not open delayed
-                  ^ self
+                    el hasDelayedMenu ifTrue:[
+                        "/ do not open menu
+                        self selection:el openMenu:false
+                    ] ifFalse:[
+                        "/ open comes from style-sheet
+                        self selection:el.
+                    ].
+                    ^ self
                 ].
             ]
         ].
@@ -3116,7 +3106,7 @@
             superMenu handleCursorKey:aKey
         ] ifFalse:[
             (item := items findFirst:[:el| el canSelect]) notNil ifTrue:[
-                self selection:item
+                self selectionIndex:item
             ]
         ].
         ^ self
@@ -3180,7 +3170,14 @@
 
     (item := selection) isNil ifTrue:[
         superMenu notNil ifTrue:[
-            superMenu selection toggleSubmenuVisibility
+            item := superMenu selection.
+
+            item value notNil ifTrue:[
+                "/ is a delayed menu
+                self accept:item
+            ] ifFalse:[
+                item toggleSubmenuVisibility
+            ]
         ] ifFalse:[
             self accept
         ].
@@ -3188,12 +3185,14 @@
     ].
     selection hasSubmenu ifTrue:[
         selection hasDelayedMenu ifFalse:[
-            ^ selection toggleSubmenuVisibility
+            selection toggleSubmenuVisibility.
+          ^ self
         ].
         subm := selection currentSubmenu.
 
         (subm notNil and:[subm shown]) ifTrue:[
-            ^ selection toggleSubmenuVisibility
+            selection toggleSubmenuVisibility.
+          ^ self
         ].
         self openDelayed:nil
     ].
@@ -3772,16 +3771,14 @@
     |item|
 
     anItemIndiceList size == 0 ifTrue:[
-	^ self
+        ^ self
     ].
     item := self itemAt:(anItemIndiceList removeFirst).
 
     (item notNil and:[item enabled]) ifTrue:[
-	InitialSelectionQuerySignal answer:anItemIndiceList do:[
-	    self openMenuOnSelect:true.
-	    self selection:item.
-	    self openMenuOnSelect:nil.
-	]
+        InitialSelectionQuerySignal answer:anItemIndiceList do:[
+            self selection:item openMenu:true.
+        ]
     ].
 !
 
@@ -4581,30 +4578,6 @@
     ^ items size == 0 and:[subViews size ~~ 0]
 !
 
-openMenuOnSelect
-    "returns true if the item should open its menu if selected
-    "
-    |flag|
-
-    flag := self menuAdornmentAt:#openMenuOnSelect.
-    flag ifNotNil:[^ flag].
-
-  ^ styleSheet at:#'menu.openOnSelect' default:false
-!
-
-openMenuOnSelect:something
-    ^ self menuAdornmentAt:#openMenuOnSelect put:something
-!
-
-select:newSel openMenu:openMenu
-    |old|
-
-    old := self menuAdornmentAt:#openMenuOnSelect.
-    self menuAdornmentAt:#openMenuOnSelect put:openMenu.
-    self selection:newSel.
-    self menuAdornmentAt:#openMenuOnSelect put:old.
-!
-
 type
     ^ nil.
 
@@ -4653,46 +4626,88 @@
 
 selection:anItemOrNil
     "change selection to an item or nil
-    "
-    |item newSel hlp visItem|
-
-    (newSel := anItemOrNil) isNumber ifTrue:[
-	newSel := self itemAt:anItemOrNil
-    ].
-    visItem := newSel.
-
-    (newSel notNil and:[newSel canSelect]) ifFalse:[
-	newSel := nil
-    ].
-
-    selection == newSel ifTrue:[
-	^ self
+     if the item has a submenu the first item might be selected (style-sheet)
+    "
+    |openMenu openOnSelect submenu item|
+
+    selection == anItemOrNil ifTrue:[^ self].
+
+    (anItemOrNil isNil or:[anItemOrNil hasSubmenu not]) ifTrue:[
+        self selection:anItemOrNil openMenu:false.
+      ^ self
+    ].
+
+    openMenu     := self isPopUpView not.
+    openOnSelect := styleSheet at:#'menu.openOnSelect' default:false.
+
+    openMenu ifFalse:[
+        openMenu := openOnSelect.
+    ].
+    self selection:anItemOrNil openMenu:openMenu.
+
+    openOnSelect ifFalse:[
+        "/ select first item in submenu
+
+        submenu := anItemOrNil currentSubmenu.
+
+        submenu ifNotNil:[
+            item := submenu itemAt:1.
+            ((item := submenu itemAt:1) notNil and:[item hasSubmenu not]) ifTrue:[
+                submenu selection:item openMenu:false
+            ]
+        ].
+    ].
+!
+
+selection:anItemOrNil openMenu:openMenu
+    "change selection to an item or nil
+    "
+    |item newSel hlp|
+
+    anItemOrNil == selection ifTrue:[
+        ^ self
     ].
     self openDelayed:nil.
 
-    (item := selection) notNil ifTrue:[
-	selection := nil.
-	item selected:false.
-    ].
-    visItem notNil ifTrue:[self makeItemVisible:visItem].
-
-    newSel isNil ifTrue:[^ self].
+    newSel := nil.
+
+    anItemOrNil ifNotNil:[
+        self makeItemVisible:anItemOrNil.
+        anItemOrNil canSelect ifTrue:[
+            newSel := anItemOrNil
+        ] ifFalse:[
+            selection ifNil:[^ self].
+        ].
+    ].
+    selection ifNotNil:[
+        "/ clear current selection
+        item := selection.
+        selection := nil.
+        item isSelected:false.
+    ].
+    newSel ifNil:[^ self].
 
     newSel == enteredItem ifTrue:[
-	enteredItem := nil
+        enteredItem := nil
     ] ifFalse:[
-	self pointerEntersItem:nil
+        self pointerEntersItem:nil
     ].
     selection := newSel.
 
     ActiveHelp isActive ifTrue:[
-	hlp := ActiveHelp currentHelpListener.
-	hlp initiateHelpFor:self at:nil now:true.
-    ].
-    shown "realized" ifTrue:[self rearrangeItems].
-    selection selected:true.
-
-    "Modified: / 13.11.2001 / 20:17:47 / cg"
+        hlp := ActiveHelp currentHelpListener.
+        hlp initiateHelpFor:self at:nil now:true.
+    ].
+    shown ifTrue:[
+        self rearrangeItems.
+
+        openMenu ifFalse:[
+            selection invalidate.
+        ]
+    ].
+    openMenu ifTrue:[
+        selection isSelected:true.
+    ].
 !
 
 selectionIndex
@@ -5636,7 +5651,7 @@
      is fully visible by shifting it into the visible screen area if
      nescessary.
     "
-    |p o device isVertical topMenu windGrp prefExtent item
+    |p o device isVertical topMenu windGrp prefExtent
      devBot   "{ Class:SmallInteger }"
      devRight "{ Class:SmallInteger }"
      width    "{ Class:SmallInteger }"
@@ -5646,14 +5661,14 @@
     |
 
     (subMenu notNil and:[subMenu shown not and:[self isSelected and:[menuPanel realized]]]) ifFalse:[
-	^ self
+        ^ self
     ].
 
     topMenu := menuPanel topMenu.
     (subMenu device notNil and:[topMenu device ~~ subMenu device]) ifTrue:[
-	subMenu releaseDeviceResources.
-	subMenu setDevice:topMenu device id:nil gcId:nil.
-	subMenu recreate.
+        subMenu releaseDeviceResources.
+        subMenu setDevice:topMenu device id:nil gcId:nil.
+        subMenu recreate.
     ].
 
     windGrp := topMenu windowGroup.
@@ -5663,18 +5678,18 @@
     subMenu cursor:Cursor hand.
 
     windGrp notNil ifTrue:[
-	subMenu windowGroup:windGrp.
-	windGrp addTopView:subMenu.
+        subMenu windowGroup:windGrp.
+        windGrp addTopView:subMenu.
     ].
 
     "Q&D kludge - test whether the layout is nil;
-		  if true recompute the layouts
+                  if true recompute the layouts
     "
     layout ifNil:[menuPanel rearrangeItems].
 
     " Q&D kludge - if any visibility attributes are blocks;
       TODO: only invoke mustRearrange if any are blocks
-	    (since I react correctly on valueHolder changes)
+            (since I react correctly on valueHolder changes)
     "
     subMenu rearrangeItemsIfItemVisibilityChanged.
     subMenu fixSize.
@@ -5692,16 +5707,16 @@
     p := isVertical ifTrue:[layout topRight - 2] ifFalse:[layout bottomLeft].
 
     menuPanel isPopUpView ifTrue:[
-	o := menuPanel origin + p
+        o := menuPanel origin + p
     ] ifFalse:[
-	o := menuPanel translatePoint:p to:nil.   "/ translate to root window
+        o := menuPanel translatePoint:p to:nil.   "/ translate to root window
     ].
     left := o x.
     top  := o y.
 
     top + height > devBot ifTrue:[
-	top := isVertical ifTrue:[devBot - height]
-			 ifFalse:[top - layout height - height + 2]
+        top := isVertical ifTrue:[devBot - height]
+                         ifFalse:[top - layout height - height + 2]
     ].
     top := top max:0.
 
@@ -5715,25 +5730,17 @@
 "/    ].
         
     left + width > devRight ifTrue:[
-	left := isVertical ifTrue:[left - layout width - width + 2]
-			  ifFalse:[devRight - width]
+        left := isVertical ifTrue:[left - layout width - width + 2]
+                          ifFalse:[devRight - width]
     ].
     left := left max:0.
 
     subMenu origin:(left@top).
 
     subMenu realized ifFalse:[
-	subMenu realize. 
+        subMenu realize. 
     ] ifTrue:[
-	topMenu device mapWindow:(subMenu id).
-    ].
-
-    menuPanel openMenuOnSelect ifFalse:[
-	(    menuPanel superMenu notNil                                         "/ not the top menu
-	 or:[((item := subMenu itemAt:1) notNil and:[item hasSubmenu not])]     "/ top menu but no submenu item
-	) ifTrue:[
-	    subMenu selectionIndex:1
-	]
+        topMenu device mapWindow:(subMenu id).
     ].
 !
 
@@ -7088,33 +7095,28 @@
     ^ menuPanel notNil and:[menuPanel selection == self]
 !
 
-selected:isSelected
+isSelected:isSelected
     "change selection to a state. Dependant on the state open or hide an existing
      submenu and perform a redraw
     "
-    menuPanel isNil ifTrue:[
-	^ self hideSubmenu
-    ].
-
-    isSelected ifFalse:[
-	self invalidate.
-      ^ self hideSubmenu.
-    ].
+    (isSelected and:[menuPanel notNil]) ifFalse:[
+        self invalidate.
+        self hideSubmenu.
+      ^ self
+    ].
+
     menuPanel realized ifFalse:[ ^ self ].
 
     (indication isNil or:[isButton not]) ifTrue:[
-	self invalidate
+        self invalidate
     ].
     self hasSubmenu ifFalse:[ ^ self].
 
-    (menuPanel isPopUpView not or:[menuPanel openMenuOnSelect]) ifFalse:[
-	^ self
-    ].
     self hasDelayedMenu ifTrue:[
-	menuPanel openDelayed:self
+        menuPanel openDelayed:self
     ] ifFalse:[
-	subMenu := self setupSubmenu.
-	subMenu ifNotNil:[ self openSubmenu ].
+        subMenu := self setupSubmenu.
+        subMenu ifNotNil:[ self openSubmenu ].
     ].
 ! !
 
@@ -7289,7 +7291,7 @@
 !MenuPanel class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/MenuPanel.st,v 1.333 2002-10-02 07:27:50 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/MenuPanel.st,v 1.334 2002-10-05 16:25:28 ca Exp $'
 ! !
 
 MenuPanel initialize!