Menu.st
changeset 4095 563b7c522f45
parent 4086 de7c2d6148e1
child 4310 a618a8ac992a
equal deleted inserted replaced
4094:d648fee7bdb4 4095:563b7c522f45
    47 
    47 
    48 !
    48 !
    49 
    49 
    50 documentation
    50 documentation
    51 "
    51 "
    52     not yet finished Menu class - this will eventually replace
    52     Menu (aka MenuSpec, that's what it is!!).
    53     most of the MenuView and PopUpMenu stuff.
    53 
    54     (and hopefully be ST-80 compatible ...)
    54     similar to UISpecifications, this describes the look, feel and behavior of
    55 
    55     popUpMenus, toolbarMenus and window menus.
    56     For now, only a subset of the full protocol is implemented.
    56 
    57 
    57     Instances of me are usually created from menuSpec methods, which return a literal
       
    58     array description of me.
       
    59     Instances of me are processed by MenuBuilders, which create a real MenuView/MenuPanel
       
    60     from that into a window. 
       
    61 
       
    62     Should be relatively VisualWorks compatible (clean room development, by emulating the
       
    63     protocol needed by some public domain VW applications)
       
    64     
    58     [author:]
    65     [author:]
    59 	Claus Gittinger
    66         Claus Gittinger
    60 
    67 
    61     [see also:]
    68     [see also:]
    62 	MenuItem
    69         MenuItem
    63 	PopUpMenu
    70         PopUpMenu
    64 "
    71 "
    65 ! !
    72 ! !
    66 
    73 
    67 !Menu class methodsFor:'instance creation'!
    74 !Menu class methodsFor:'instance creation'!
    68 
    75 
   268 groupSizes:something
   275 groupSizes:something
   269     groupSizes := something.
   276     groupSizes := something.
   270 !
   277 !
   271 
   278 
   272 itemAtValue:aValue
   279 itemAtValue:aValue
   273     "gets the item which has aValue assigned as value
   280     "gets the item which has aValue assigned as value"
   274     "
   281 
   275     ^ self menuAndSubmenusDetectItem:[:anItem | anItem value == aValue ].
   282     ^ self menuAndSubmenusDetectItem:[:anItem | anItem value == aValue ].
   276 
   283 
   277     "Created: / 14-03-2017 / 16:12:37 / cg"
   284     "Created: / 14-03-2017 / 16:12:37 / cg"
       
   285     "Modified (comment): / 24-05-2018 / 16:11:15 / Claus Gittinger"
   278 !
   286 !
   279 
   287 
   280 items
   288 items
   281     ^ items
   289     ^ items
   282 !
   290 !
   284 items:aCollectionOfMenuItems
   292 items:aCollectionOfMenuItems
   285     items := aCollectionOfMenuItems
   293     items := aCollectionOfMenuItems
   286 !
   294 !
   287 
   295 
   288 labelAt:anIndex
   296 labelAt:anIndex
   289     "gets the label of the menu item at the given index or nil
   297     "gets the label of the menu item at the given index or nil"
   290     "
   298     
   291     |item|
   299     |item|
   292 
   300 
   293     (item := self menuItemAt:anIndex) notNil ifTrue:[
   301     (item := self menuItemAt:anIndex) notNil ifTrue:[
   294         ^ item label
   302         ^ item label
   295     ].
   303     ].
   296     ^ nil
   304     ^ nil
   297 
   305 
   298     "Modified: / 2.2.1998 / 13:28:32 / cg"
   306     "Modified: / 02-02-1998 / 13:28:32 / cg"
       
   307     "Modified (comment): / 24-05-2018 / 16:11:26 / Claus Gittinger"
   299 !
   308 !
   300 
   309 
   301 labelAtValue:aValue
   310 labelAtValue:aValue
   302     "gets the label of the menu item assigned to value
   311     "gets the label of the menu item assigned to value"
   303     "
   312     
   304     |item|
   313     |item|
   305 
   314 
   306     item := self menuAndSubmenusDetectItem:[:anItem | anItem value == aValue ].
   315     item := self menuAndSubmenusDetectItem:[:anItem | anItem value == aValue ].
   307 
   316 
   308     item notNil ifTrue:[
   317     item notNil ifTrue:[
   309         ^ item label
   318         ^ item label
   310     ].
   319     ].
   311     ^ nil
   320     ^ nil
   312 
   321 
   313     "Modified: / 2.2.1998 / 13:28:28 / cg"
   322     "Modified: / 02-02-1998 / 13:28:28 / cg"
       
   323     "Modified (comment): / 24-05-2018 / 16:11:30 / Claus Gittinger"
   314 !
   324 !
   315 
   325 
   316 labels
   326 labels
   317     "return a collection of labels from my items"
   327     "return a collection of labels from my items"
   318 
   328 
   459             groupSizes := sizes copyButLast:1
   469             groupSizes := sizes copyButLast:1
   460         ]
   470         ]
   461     ].
   471     ].
   462 
   472 
   463     values notNil ifTrue:[
   473     values notNil ifTrue:[
   464         items with:values do:[:anItem :aValue |anItem value:aValue]
   474         items with:values do:[:anItem :eachValue |anItem value:eachValue]
   465     ].
   475     ].
   466 
   476 
   467     "Modified: / 19.4.1998 / 11:47:34 / cg"
   477     "Modified: / 19-04-1998 / 11:47:34 / cg"
       
   478     "Modified (format): / 24-05-2018 / 15:18:25 / Claus Gittinger"
   468 !
   479 !
   469 
   480 
   470 menuPerformer:something
   481 menuPerformer:something
   471     "set the receiver of the menu messages"
   482     "set the receiver of the menu messages"
   472 
   483 
   861         |enabledInItem enabled|
   872         |enabledInItem enabled|
   862 
   873 
   863         enabledInItem := each enabled.
   874         enabledInItem := each enabled.
   864         enabledInItem notNil ifTrue:[
   875         enabledInItem notNil ifTrue:[
   865             enabledInItem isSymbol ifTrue:[
   876             enabledInItem isSymbol ifTrue:[
       
   877                 anApplicationOrNil isNil ifTrue:[
       
   878                     Smalltalk isSmalltalkDevelopmentSystem ifTrue:[ 
       
   879                         self halt:('no application to ask %1 (enabled)' bindWith:enabledInItem)
       
   880                     ].
       
   881                 ].
   866                 enabled := anApplicationOrNil isNil or:[ (anApplicationOrNil perform:enabledInItem) ].
   882                 enabled := anApplicationOrNil isNil or:[ (anApplicationOrNil perform:enabledInItem) ].
   867             ] ifFalse:[
   883             ] ifFalse:[
   868                 enabled := enabledInItem
   884                 enabled := enabledInItem
   869             ].
   885             ].
   870             enabled value ifFalse:[
   886             enabled value ifFalse:[
   889                       ).
   905                       ).
   890 
   906 
   891     ^ PopUpMenu forMenu:menuView
   907     ^ PopUpMenu forMenu:menuView
   892 
   908 
   893     "Modified: / 30-06-2011 / 10:35:57 / cg"
   909     "Modified: / 30-06-2011 / 10:35:57 / cg"
       
   910     "Modified: / 24-05-2018 / 15:26:17 / Claus Gittinger"
   894 !
   911 !
   895 
   912 
   896 fromLiteralArrayEncoding:aLiteralEncodedArray
   913 fromLiteralArrayEncoding:aLiteralEncodedArray
   897     "read my contents from a aLiteralEncodedArray"
   914     "read my contents from a aLiteralEncodedArray"
   898 
   915 
  1041 ! !
  1058 ! !
  1042 
  1059 
  1043 !Menu methodsFor:'enumerating'!
  1060 !Menu methodsFor:'enumerating'!
  1044 
  1061 
  1045 allItemsDetect:aOneArgBlock ifNone:exceptionalValue
  1062 allItemsDetect:aOneArgBlock ifNone:exceptionalValue
  1046     "find an element amongst each item and submenu items"
  1063     "recursively find an element amongst each item and submenu items"
  1047 
  1064 
  1048     self itemsDo:[:anItem|
  1065     self itemsDo:[:anItem|
  1049         |sub subItem|
  1066         |sub subItem|
  1050 
  1067 
  1051         (aOneArgBlock value:anItem) ifTrue:[^ anItem].
  1068         (aOneArgBlock value:anItem) ifTrue:[^ anItem].
  1053             subItem := sub allItemsDetect:aOneArgBlock ifNone:nil.
  1070             subItem := sub allItemsDetect:aOneArgBlock ifNone:nil.
  1054             subItem notNil ifTrue:[^ subItem].
  1071             subItem notNil ifTrue:[^ subItem].
  1055         ]
  1072         ]
  1056     ].
  1073     ].
  1057     ^ exceptionalValue value
  1074     ^ exceptionalValue value
       
  1075 
       
  1076     "Modified (comment): / 24-05-2018 / 16:10:36 / Claus Gittinger"
  1058 !
  1077 !
  1059 
  1078 
  1060 allItemsDo:aOneArgBlock
  1079 allItemsDo:aOneArgBlock
  1061     "evaluate block on each item and submenu items
  1080     "recursively evaluate block on each item and submenu items"
  1062     "
  1081 
  1063     self itemsDo:[:anItem|
  1082     self itemsDo:[:anItem|
  1064         |sub|
  1083         |sub|
  1065 
  1084 
  1066         aOneArgBlock value:anItem.
  1085         aOneArgBlock value:anItem.
  1067         (sub := anItem submenu) notNil ifTrue:[
  1086         (sub := anItem submenu) notNil ifTrue:[
  1068             sub allItemsDo:aOneArgBlock
  1087             sub allItemsDo:aOneArgBlock
  1069         ]
  1088         ]
  1070     ]
  1089     ]
  1071 
  1090 
  1072     "Modified: / 19.6.1998 / 00:34:53 / cg"
  1091     "Modified: / 19-06-1998 / 00:34:53 / cg"
       
  1092     "Modified (comment): / 24-05-2018 / 16:10:41 / Claus Gittinger"
  1073 !
  1093 !
  1074 
  1094 
  1075 detectItem:aBlock
  1095 detectItem:aBlock
  1076     "evaluate the argument, aBlock for each item in the menu until the
  1096     "evaluate the argument, aBlock for each item in the menu until the
  1077      block returns true; in this case return the item which caused the
  1097      block returns true; in this case return the item which caused the
  1078      true evaluation.
  1098      true evaluation.
  1079      If none of the evaluations returns true, return the result of the
  1099      If none of the evaluations returns true, return the result of the
  1080      evaluation of the exceptionBlock
  1100      evaluation of the exceptionBlock"
  1081     "
  1101 
  1082     ^ self detectItem:aBlock ifNone:[self errorNotFound]
  1102     ^ self detectItem:aBlock ifNone:[self errorNotFound]
  1083 
  1103 
       
  1104     "Modified (comment): / 24-05-2018 / 16:10:46 / Claus Gittinger"
  1084 !
  1105 !
  1085 
  1106 
  1086 detectItem:aBlock ifNone:exceptionValue
  1107 detectItem:aBlock ifNone:exceptionValue
  1087     "evaluate the argument, aBlock for each item in the menu until the
  1108     "evaluate the argument, aBlock for each item in the menu until the
  1088      block returns true; in this case return the item which caused the
  1109      block returns true; in this case return the item which caused the
  1089      true evaluation.
  1110      true evaluation.
  1090      If none of the evaluations returns true, return the value from exceptionValue
  1111      If none of the evaluations returns true, return the value from exceptionValue"
  1091     "
  1112 
  1092     items notNil ifTrue:[
  1113     items notNil ifTrue:[
  1093         ^ items detect:aBlock ifNone:exceptionValue
  1114         ^ items detect:aBlock ifNone:exceptionValue
  1094     ].
  1115     ].
  1095     ^ exceptionValue value
  1116     ^ exceptionValue value
  1096 
  1117 
  1097     "Modified (comment): / 14-03-2017 / 16:09:51 / cg"
  1118     "Modified (comment): / 14-03-2017 / 16:09:51 / cg"
       
  1119     "Modified (comment): / 24-05-2018 / 16:10:51 / Claus Gittinger"
       
  1120 !
       
  1121 
       
  1122 do:aOneArgBlock
       
  1123     "recursively evaluate block on each item and submenu items"
       
  1124 
       
  1125     self allItemsDo:aOneArgBlock
       
  1126 
       
  1127     "Created: / 24-05-2018 / 16:10:25 / Claus Gittinger"
  1098 !
  1128 !
  1099 
  1129 
  1100 itemsDo:aOneArgBlock
  1130 itemsDo:aOneArgBlock
  1101     "evaluate the block for each item in the current menu
  1131     "evaluate the block for each item in the current menu"
  1102     "
  1132 
  1103     items notNil ifTrue:[items do:aOneArgBlock]
  1133     items notNil ifTrue:[items do:aOneArgBlock]
       
  1134 
       
  1135     "Modified (comment): / 24-05-2018 / 16:10:55 / Claus Gittinger"
  1104 !
  1136 !
  1105 
  1137 
  1106 menuAndSubmenusDetectItem:aOneArgBlock
  1138 menuAndSubmenusDetectItem:aOneArgBlock
  1107     "evaluate the block for each item in the current menu and all
  1139     "evaluate the block for each item in the current menu and all
  1108      submenus. In case that the block returns a non nil argument,
  1140      submenus. In case that the block returns a non nil argument,
  1109      the item will be returned
  1141      the item will be returned"
  1110     "
  1142 
  1111     |item|
  1143     |item|
  1112 
  1144 
  1113     items notNil ifTrue:[
  1145     items notNil ifTrue:[
  1114         items do:[:anItem|
  1146         items do:[:anItem|
  1115             |sub|
  1147             |sub|
  1125             ]
  1157             ]
  1126         ]
  1158         ]
  1127     ].
  1159     ].
  1128     ^ nil
  1160     ^ nil
  1129 
  1161 
  1130     "Modified: / 19.6.1998 / 00:35:00 / cg"
  1162     "Modified: / 19-06-1998 / 00:35:00 / cg"
       
  1163     "Modified (comment): / 24-05-2018 / 16:10:59 / Claus Gittinger"
  1131 ! !
  1164 ! !
  1132 
  1165 
  1133 !Menu methodsFor:'kludged fixes'!
  1166 !Menu methodsFor:'kludged fixes'!
  1134 
  1167 
  1135 destroy
  1168 destroy