PullDownMenu.st
changeset 133 e58c7c979f33
parent 131 208fa92f434d
child 146 7726707ac980
equal deleted inserted replaced
132:596439fe4efd 133:e58c7c979f33
    13 SimpleView subclass:#PullDownMenu
    13 SimpleView subclass:#PullDownMenu
    14        instanceVariableNames:'receiver menus titles selectors activeMenuNumber
    14        instanceVariableNames:'receiver menus titles selectors activeMenuNumber
    15 			      showSeparatingLines topMargin
    15 			      showSeparatingLines topMargin
    16 			      fgColor bgColor activeFgColor activeBgColor
    16 			      fgColor bgColor activeFgColor activeBgColor
    17 			      onLevel offLevel edgeStyle
    17 			      onLevel offLevel edgeStyle
    18 			      keepMenu toggleKeep'
    18 			      keepMenu toggleKeep raiseTopWhenActivated'
    19        classVariableNames:'DefaultFont
    19        classVariableNames:'DefaultFont
    20 			   DefaultViewBackground 
    20 			   DefaultViewBackground 
    21 			   DefaultForegroundColor 
    21 			   DefaultForegroundColor 
    22 			   DefaultBackgroundColor
    22 			   DefaultBackgroundColor
    23 			   DefaultHilightForegroundColor 
    23 			   DefaultHilightForegroundColor 
    32 
    32 
    33 PullDownMenu comment:'
    33 PullDownMenu comment:'
    34 COPYRIGHT (c) 1989 by Claus Gittinger
    34 COPYRIGHT (c) 1989 by Claus Gittinger
    35 	     All Rights Reserved
    35 	     All Rights Reserved
    36 
    36 
    37 $Header: /cvs/stx/stx/libwidg/PullDownMenu.st,v 1.20 1995-06-27 02:23:48 claus Exp $
    37 $Header: /cvs/stx/stx/libwidg/PullDownMenu.st,v 1.21 1995-07-23 03:02:18 claus Exp $
    38 '!
    38 '!
    39 
    39 
    40 !PullDownMenu class methodsFor:'documentation'!
    40 !PullDownMenu class methodsFor:'documentation'!
    41 
    41 
    42 copyright
    42 copyright
    53 "
    53 "
    54 !
    54 !
    55 
    55 
    56 version
    56 version
    57 "
    57 "
    58 $Header: /cvs/stx/stx/libwidg/PullDownMenu.st,v 1.20 1995-06-27 02:23:48 claus Exp $
    58 $Header: /cvs/stx/stx/libwidg/PullDownMenu.st,v 1.21 1995-07-23 03:02:18 claus Exp $
    59 "
    59 "
    60 !
    60 !
    61 
    61 
    62 documentation
    62 documentation
    63 "
    63 "
   144       pullDownMenuFont                        font to use for the menu bar
   144       pullDownMenuFont                        font to use for the menu bar
   145 					      default: menuFont
   145 					      default: menuFont
   146 
   146 
   147       pullDownMenuShowSeparatingLines         if true, lines are drawn between items.
   147       pullDownMenuShowSeparatingLines         if true, lines are drawn between items.
   148 					      default: false
   148 					      default: false
       
   149 
       
   150       pullDownMenuRaiseTop                    if true, topview is raised whenever an entry
       
   151 					      is activated.
       
   152 					      default: true
   149 "
   153 "
   150 !
   154 !
   151 
   155 
   152 examples 
   156 examples 
   153 "
   157 "
   340 !PullDownMenu class methodsFor:'defaults'!
   344 !PullDownMenu class methodsFor:'defaults'!
   341 
   345 
   342 updateStyleCache
   346 updateStyleCache
   343     "extract values from the styleSheet and cache them in class variables"
   347     "extract values from the styleSheet and cache them in class variables"
   344 
   348 
   345     DefaultViewBackground := StyleSheet colorAt:'pullDownMenuViewBackground'.
   349     |styleSheet|
       
   350 
       
   351     styleSheet := StyleSheet.
       
   352 
       
   353     DefaultViewBackground := styleSheet colorAt:'pullDownMenuViewBackground'.
   346     DefaultViewBackground isNil ifTrue:[
   354     DefaultViewBackground isNil ifTrue:[
   347 	DefaultViewBackground := StyleSheet colorAt:'menuViewBackground'.
   355 	DefaultViewBackground := styleSheet colorAt:'menuViewBackground'.
   348     ].
   356     ].
   349     DefaultForegroundColor := StyleSheet colorAt:'pullDownMenuForegroundColor'.
   357     DefaultForegroundColor := styleSheet colorAt:'pullDownMenuForegroundColor'.
   350     DefaultForegroundColor isNil ifTrue:[
   358     DefaultForegroundColor isNil ifTrue:[
   351 	DefaultForegroundColor := StyleSheet colorAt:'menuForegroundColor'.
   359 	DefaultForegroundColor := styleSheet colorAt:'menuForegroundColor'.
   352     ].
   360     ].
   353     DefaultBackgroundColor := StyleSheet colorAt:'pullDownMenuBackgroundColor'.
   361     DefaultBackgroundColor := styleSheet colorAt:'pullDownMenuBackgroundColor'.
   354     DefaultBackgroundColor isNil ifTrue:[
   362     DefaultBackgroundColor isNil ifTrue:[
   355 	DefaultViewBackground notNil ifTrue:[
   363 	DefaultViewBackground notNil ifTrue:[
   356 	    DefaultBackgroundColor := DefaultViewBackground
   364 	    DefaultBackgroundColor := DefaultViewBackground
   357 	] ifFalse:[
   365 	] ifFalse:[
   358 	    DefaultBackgroundColor := StyleSheet colorAt:'menuBackgroundColor'.
   366 	    DefaultBackgroundColor := styleSheet colorAt:'menuBackgroundColor'.
   359 	]
   367 	]
   360     ].
   368     ].
   361     DefaultHilightForegroundColor := StyleSheet colorAt:'pullDownMenuHilightForegroundColor'.
   369     DefaultHilightForegroundColor := styleSheet colorAt:'pullDownMenuHilightForegroundColor'.
   362     DefaultHilightForegroundColor isNil ifTrue:[
   370     DefaultHilightForegroundColor isNil ifTrue:[
   363 	DefaultHilightForegroundColor := StyleSheet colorAt:'menuHilightForegroundColor'.
   371 	DefaultHilightForegroundColor := styleSheet colorAt:'menuHilightForegroundColor'.
   364     ].
   372     ].
   365     DefaultHilightBackgroundColor := StyleSheet colorAt:'pullDownMenuHilightBackgroundColor'.
   373     DefaultHilightBackgroundColor := styleSheet colorAt:'pullDownMenuHilightBackgroundColor'.
   366     DefaultHilightBackgroundColor isNil ifTrue:[
   374     DefaultHilightBackgroundColor isNil ifTrue:[
   367 	DefaultHilightBackgroundColor := StyleSheet colorAt:'menuHilightBackgroundColor'.
   375 	DefaultHilightBackgroundColor := styleSheet colorAt:'menuHilightBackgroundColor'.
   368     ].
   376     ].
   369     DefaultHilightLevel := StyleSheet at:'pullDownMenuHilightLevel'.
   377     DefaultHilightLevel := styleSheet at:'pullDownMenuHilightLevel'.
   370     DefaultHilightLevel isNil ifTrue:[
   378     DefaultHilightLevel isNil ifTrue:[
   371 	DefaultHilightLevel := StyleSheet at:'menuHilightLevel' default:0.
   379 	DefaultHilightLevel := styleSheet at:'menuHilightLevel' default:0.
   372     ].
   380     ].
   373     DefaultEdgeStyle := StyleSheet at:'pullDownMenuEdgeStyle'.
   381     DefaultEdgeStyle := styleSheet at:'pullDownMenuEdgeStyle'.
   374     DefaultKeepMenu := StyleSheet at:'pullDownMenuKeepMenu' default:false.
   382     DefaultKeepMenu := styleSheet at:'pullDownMenuKeepMenu' default:false.
   375     DefaultToggleKeep := StyleSheet at:'pullDownMenuToggleKeep' default:false.
   383     DefaultToggleKeep := styleSheet at:'pullDownMenuToggleKeep' default:false.
   376     DefaultLevel := StyleSheet at:'pullDownMenuLevel' default:1.
   384     DefaultLevel := styleSheet at:'pullDownMenuLevel' default:1.
   377     DefaultFont := StyleSheet fontAt:'pullDownMenuFont'.
   385     DefaultFont := styleSheet fontAt:'pullDownMenuFont'.
   378     DefaultFont isNil ifTrue:[
   386     DefaultFont isNil ifTrue:[
   379 	DefaultFont := StyleSheet fontAt:'menuFont'.
   387 	DefaultFont := styleSheet fontAt:'menuFont'.
   380     ].
   388     ].
   381     DefaultSeparatingLines := StyleSheet at:'pullDownMenuSeparatingLines' default:false.
   389     DefaultSeparatingLines := styleSheet at:'pullDownMenuSeparatingLines' default:false.
   382 
   390 
   383     "
   391     "
   384      PullDownMenu updateStyleCache
   392      PullDownMenu updateStyleCache
   385     "
   393     "
   386 ! !
   394 ! !
   475 
   483 
   476     bgColor := bgColor on:device.
   484     bgColor := bgColor on:device.
   477     fgColor := fgColor on:device.
   485     fgColor := fgColor on:device.
   478     activeBgColor := activeBgColor on:device.
   486     activeBgColor := activeBgColor on:device.
   479     activeFgColor := activeFgColor on:device.
   487     activeFgColor := activeFgColor on:device.
       
   488 
       
   489     raiseTopWhenActivated := styleSheet at:'pullDownMenuRaiseTop' default:true.
       
   490 !
       
   491 
       
   492 initCursor
       
   493     "set up a hand cursor"
       
   494 
       
   495     cursor := Cursor hand
   480 !
   496 !
   481 
   497 
   482 recreate
   498 recreate
   483     "if the image was saved with an active menu, hide it"
   499     "if the image was saved with an active menu, hide it"
   484 
   500 
   509 	].
   525 	].
   510 	menus := nil
   526 	menus := nil
   511     ].
   527     ].
   512     activeMenuNumber := nil.
   528     activeMenuNumber := nil.
   513     super destroy.
   529     super destroy.
       
   530 !
       
   531 
       
   532 superView:aView
       
   533     "when my superView changes, all of my menus must change as well"
       
   534 
       
   535     super superView:aView.
       
   536     menus notNil ifTrue:[
       
   537 	menus do:[:aMenu |
       
   538 	    aMenu notNil ifTrue:[
       
   539 		aMenu superView:aView
       
   540 	    ]
       
   541 	]
       
   542     ]
   514 ! !
   543 ! !
   515 
   544 
   516 !PullDownMenu methodsFor:'accessing-look'!
   545 !PullDownMenu methodsFor:'accessing-look'!
   517 
   546 
   518 showSeparatingLines:aBoolean
   547 showSeparatingLines:aBoolean
   746 	idx ~~ 0 ifTrue:[^ idx].
   775 	idx ~~ 0 ifTrue:[^ idx].
   747     ].
   776     ].
   748     ^ titles indexOf:stringOrNumber
   777     ^ titles indexOf:stringOrNumber
   749 !
   778 !
   750 
   779 
       
   780 someMenuItemLabeled:aLabel
       
   781     "find a menu item.
       
   782      Currently, in ST/X, instances of MenuItem are only created as dummy"
       
   783 
       
   784     |idx|
       
   785 
       
   786     idx := self indexOf:aLabel.
       
   787     idx ~~ 0 ifTrue:[
       
   788 	^ MenuItem new menu:self index:idx
       
   789     ].
       
   790     menus notNil ifTrue:[
       
   791 	menus do:[:aMenu |
       
   792 	    |item|
       
   793 
       
   794 	    aMenu notNil ifTrue:[
       
   795 		(item := aMenu someMenuItemLabeled:aLabel) notNil ifTrue:[
       
   796 		    ^ item
       
   797 		]
       
   798 	    ]
       
   799 	]
       
   800     ].
       
   801     ^ nil
       
   802 !
       
   803 
   751 setMenuOrigins
   804 setMenuOrigins
   752     "adjust origins of menus when font changes"
   805     "adjust origins of menus when font changes"
   753 
   806 
   754     (font device == device) ifTrue:[
   807     (font device == device) ifTrue:[
   755 	menus keysAndValuesDo:[:index :aMenu |
   808 	menus keysAndValuesDo:[:index :aMenu |
   831 
   884 
   832     activeMenuNumber notNil ifTrue:[self hideActiveMenuRelease:false].
   885     activeMenuNumber notNil ifTrue:[self hideActiveMenuRelease:false].
   833     activeMenuNumber := aNumber.
   886     activeMenuNumber := aNumber.
   834     subMenu := menus at:aNumber.
   887     subMenu := menus at:aNumber.
   835 
   888 
       
   889     raiseTopWhenActivated ifTrue:[
       
   890 	self topView raise.
       
   891     ].
       
   892 
   836     (activeMenuNumber notNil 
   893     (activeMenuNumber notNil 
   837     and:[
   894     and:[
   838 	 subMenu notNil
   895 	 subMenu notNil
   839 	 or:[selectors notNil and:[(selectors at:activeMenuNumber) notNil]]]) ifTrue:[
   896 	 or:[selectors notNil and:[(selectors at:activeMenuNumber) notNil]]]) ifTrue:[
   840 	    self highlightActiveTitle.
   897 	    self highlightActiveTitle.
   869 
   926 
   870 redraw
   927 redraw
   871     |x     "{ Class: SmallInteger }"
   928     |x     "{ Class: SmallInteger }"
   872      y     "{ Class: SmallInteger }"
   929      y     "{ Class: SmallInteger }"
   873      index "{ Class: SmallInteger }" 
   930      index "{ Class: SmallInteger }" 
   874      wSpace|
   931      wSpace clr|
   875 
   932 
   876     shown ifFalse: [ ^ self ].
   933     shown ifFalse: [ ^ self ].
   877     titles isNil ifTrue:[^ self].
   934     titles isNil ifTrue:[^ self].
   878 
   935 
   879     wSpace := (font widthOf:' ').
   936     wSpace := (font widthOf:' ').
   892 	showSeparatingLines ifTrue:[
   949 	showSeparatingLines ifTrue:[
   893 	    self is3D ifTrue:[
   950 	    self is3D ifTrue:[
   894 		self paint:shadowColor.
   951 		self paint:shadowColor.
   895 		self displayLineFromX:x y:0 toX:x y:y.
   952 		self displayLineFromX:x y:0 toX:x y:y.
   896 		x := x + 1.
   953 		x := x + 1.
   897 		self paint:lightColor.
   954 		clr := lightColor.
   898 	    ] ifFalse:[
   955 	    ] ifFalse:[
   899 		self paint:fgColor.
   956 		clr := fgColor.
   900 	    ].
   957 	    ].
       
   958 	    self paint:clr.
   901 	    self displayLineFromX:x y:0 toX:x y:y.
   959 	    self displayLineFromX:x y:0 toX:x y:y.
   902 	    x := x + 1
   960 	    x := x + 1
   903 	].
   961 	].
   904 	index := index + 1
   962 	index := index + 1
   905     ]
   963     ]
   906 !
   964 !
   907 
   965 
   908 drawTitle:stringOrImage x:x0 selected:selected
   966 drawTitle:stringOrImage x:x0 selected:selected
   909     |y w x wSpace|
   967     |y w x wSpace fg bg|
       
   968 
       
   969     selected ifTrue:[
       
   970 	fg := activeFgColor.
       
   971 	bg := activeBgColor
       
   972     ] ifFalse:[
       
   973 	fg := fgColor.
       
   974 	bg := bgColor
       
   975     ].
   910 
   976 
   911     wSpace := font widthOf:' '.
   977     wSpace := font widthOf:' '.
   912     x := x0.
   978     x := x0.
   913     stringOrImage isString ifTrue:[
   979     stringOrImage isString ifTrue:[
   914 	y := ((height - (font height)) // 2) + (font ascent) "+ topMargin".
   980 	y := ((height - (font height)) // 2) + (font ascent) "+ topMargin".
   917 	y := ((height - stringOrImage height) // 2) max:0.
   983 	y := ((height - stringOrImage height) // 2) max:0.
   918 	w := stringOrImage width
   984 	w := stringOrImage width
   919     ].
   985     ].
   920     w := w + (wSpace * 2).
   986     w := w + (wSpace * 2).
   921 
   987 
   922     selected ifTrue:[
   988     self paint:bg.
   923 	self paint:activeBgColor
       
   924     ] ifFalse:[
       
   925 	self paint:bgColor
       
   926     ].
       
   927     self fillRectangleX:x y:0 width:w height:height.
   989     self fillRectangleX:x y:0 width:w height:height.
   928 
   990 
   929     self is3D ifTrue:[
   991     self is3D ifTrue:[
   930 	self drawEdgesForX:x y:0
   992 	self drawEdgesForX:x y:0
   931 		     width:w
   993 		     width:w
   932 		    height:height
   994 		    height:height
   933 		     level:(selected ifTrue:[onLevel] ifFalse:[offLevel])
   995 		     level:(selected ifTrue:[onLevel] ifFalse:[offLevel])
   934     ].
   996     ].
   935     selected ifTrue:[
   997     self paint:fg.
   936 	self paint:activeFgColor
       
   937     ] ifFalse:[
       
   938 	self paint:fgColor
       
   939     ].
       
   940     x := x + wSpace.
   998     x := x + wSpace.
   941     stringOrImage isString ifTrue:[
   999     stringOrImage isString ifTrue:[
   942 	self displayString:stringOrImage x:x y:y
  1000 	self displayString:stringOrImage x:x y:y
   943     ] ifFalse:[
  1001     ] ifFalse:[
   944 	self displayForm:stringOrImage x:x y:y
  1002 	self displayForm:stringOrImage x:x y:y
   945     ]
  1003     ]
   946 !
  1004 !
   947 
  1005 
   948 highlightActiveTitle
  1006 drawActiveTitleSelected:selected
   949     |x|
  1007     |x|
   950     activeMenuNumber notNil ifTrue:[
  1008     activeMenuNumber notNil ifTrue:[
   951 	x := self titleLenUpTo:activeMenuNumber.
  1009 	x := self titleLenUpTo:activeMenuNumber.
   952 	self drawTitle:(titles at:activeMenuNumber) x:x selected:true
  1010 	self drawTitle:(titles at:activeMenuNumber) x:x selected:selected 
   953     ]
  1011     ]
   954 !
  1012 !
   955 
  1013 
       
  1014 highlightActiveTitle
       
  1015     self drawActiveTitleSelected:true 
       
  1016 !
       
  1017 
   956 unHighlightActiveTitle
  1018 unHighlightActiveTitle
   957     |x|
  1019     self drawActiveTitleSelected:false 
   958     activeMenuNumber notNil ifTrue:[
       
   959 	x := self titleLenUpTo:activeMenuNumber.
       
   960 	self drawTitle:(titles at:activeMenuNumber) x:x selected:false
       
   961     ]
       
   962 ! !
  1020 ! !
   963 
  1021 
   964 !PullDownMenu methodsFor:'submenu notifications'!
  1022 !PullDownMenu methodsFor:'submenu notifications'!
   965 
  1023 
   966 showActive
  1024 showActive
   996 keyPress:key x:x y:y
  1054 keyPress:key x:x y:y
   997     |index m sel|
  1055     |index m sel|
   998 
  1056 
   999     "
  1057     "
  1000      handle CursorLeft/Right for non-mouse operation
  1058      handle CursorLeft/Right for non-mouse operation
  1001      (once it has the explicit focus)
  1059      (for example, if it has the explicit focus)
       
  1060      These will pull the previous/next menu
  1002     "
  1061     "
  1003     ((key == #CursorRight) or:[key == #CursorLeft]) ifTrue:[
  1062     ((key == #CursorRight) or:[key == #CursorLeft]) ifTrue:[
  1004 	activeMenuNumber isNil ifTrue:[
  1063 	activeMenuNumber isNil ifTrue:[
  1005 	    index := (key == #CursorRight) ifTrue:[1] ifFalse:[menus size].
  1064 	    index := (key == #CursorRight) ifTrue:[1] ifFalse:[menus size].
  1006 	] ifFalse:[
  1065 	] ifFalse:[
  1019     ].
  1078     ].
  1020 
  1079 
  1021     activeMenuNumber isNil ifTrue:[^self].
  1080     activeMenuNumber isNil ifTrue:[^self].
  1022 
  1081 
  1023     "
  1082     "
  1024      pass it on to the active menu or perform the items action
  1083      Return, space or the (virtual) MenuSelect key trigger
       
  1084      a menu entry (for non-submenu entries).
       
  1085      Otherwise, if we have a submenu open,
       
  1086      pass the key on to it ...
  1025     "
  1087     "
  1026     m := menus at:activeMenuNumber.
  1088     m := menus at:activeMenuNumber.
  1027     m isNil ifTrue:[
  1089     m isNil ifTrue:[
  1028 	key == #Return ifTrue:[
  1090 	(key == #Return 
       
  1091 	or:[key == #MenuSelect
       
  1092 	or:[key == Character space]]) ifTrue:[
  1029 	    sel := selectors at:activeMenuNumber.
  1093 	    sel := selectors at:activeMenuNumber.
  1030 	    sel notNil ifTrue:[
  1094 	    sel notNil ifTrue:[
  1031 		receiver perform:sel
  1095 		receiver perform:sel
  1032 	    ]
  1096 	    ]
  1033 	].
  1097 	].