ColorMenu.st
changeset 3292 07786876f1fa
parent 3283 a0388721f027
child 3293 1ae2428ad1e5
--- a/ColorMenu.st	Fri Nov 30 08:17:16 2007 +0100
+++ b/ColorMenu.st	Tue Dec 04 09:15:55 2007 +0100
@@ -69,106 +69,111 @@
 
 !ColorMenu class methodsFor:'adding & removing'!
 
-addUserDefinedColor:aColor named:aName
-    |menu item label submenu|
+addUserDefinedColors:aColors labels:labelsOrNil title:aTitle
+    |size submenu item label fgColor bgColor labels defLabel|
+
+    submenu := self userDefinedSubmenu.
+    size    := aColors size.
+
+    size == 0 ifTrue:[  "/ separator
+        submenu addItem:(MenuItem label:'-').
+        ^ self
+    ].
+
+    labels   := labelsOrNil ? #().
+    bgColor  := aColors first.
+    defLabel := '        '.
+
+    bgColor brightness < 0.5 ifTrue:[ fgColor := Color white ]
+                            ifFalse:[ fgColor := Color black ].
+
+    aTitle isEmptyOrNil ifTrue:[ label := labels at:1 ifAbsent:defLabel ]
+                       ifFalse:[ label := aTitle ].
 
-"
-ColorMenuSpec := nil.
+    label := Text string:label foregroundColor:fgColor backgroundColor:bgColor.
+    item  := MenuItem labeled:label.
+    submenu addItem:item.
+
+    size == 1 ifTrue:[
+        item argument:bgColor.
+    ] ifFalse:[
+        item submenu:(submenu := Menu new).
 
-self addUserDefinedColor:Color red lightened named:'UBS   red'.
-self addUserDefinedColor:Color lightBlue lightened named:'UBS   blue'.
+        aColors keysAndValuesDo:[:idx :aBgColor|
+            aBgColor brightness < 0.5 ifTrue:[ fgColor := Color white ]
+                                     ifFalse:[ fgColor := Color black ].
+
+            label := labels at:idx ifAbsent:defLabel.
+            label := Text string:label foregroundColor:fgColor backgroundColor:aBgColor.
+            item  := MenuItem labeled:label.
+            item argument:bgColor.
+            
+            submenu addItem:item.
+        ].
+    ].
 
 "
-    menu := self colorMenu.
-    item := menu detectItem:[:el| (el nameKey == #userDefined and:[el submenu notNil]) ]
-                     ifNone:nil.
+    ColorMenu  removeAllUserDefinedColors.
 
-    item isNil ifTrue:[
-        item := MenuItem labeled:(self userDefinedLabel).
-        item nameKey:#userDefined.
-        item submenu:(submenu := Menu new).
-        menu addItem:item beforeIndex:1.
+    #(
+        #(  Black       16r000000 )
+        #(  Red         16rff0000 )
+        nil
+        #(  Rust        16rff6600 16rff8533 16rffa366 16rffc299 16rffe0cc )
+        #(  Tangerine   16rff9933 16rffad5c 16rffc285 16rffd6ad 16rffebd6 )
+        #(  Sunflower   16rffcc00 16rfed633 16rfee066 16rffeb99 16rfff5cc )  
+        #(  Mango       16rffcc99 16rffd6ad 16rffe0c2 16rffebd6 16rfff5eb )
+        #(  Buttercup   16rffff66 16rffff85 16rffffa3 16rffffc2 16rffffe0 )
+        #(  Lemon       16rffffcc 16rffffd6 16rffffe0 16rffffeb 16rfffff5 )
 
-        item := MenuItem new.
-        item nameKey:#userDefined.
-        menu addItem:item beforeIndex:1.
-    ] ifFalse:[
-        submenu := item submenu.
-        item := submenu detectItem:[:el| el argument = aColor ] ifNone:nil.
-        item notNil ifTrue:[ ^ self ].  "/ already exists
+    ) do:[:aDef| |colors title labels percentage|
+        aDef isEmptyOrNil ifTrue:[
+            colors := labels := title := nil.
+        ] ifFalse:[
+            title      := aDef first.
+            colors     := OrderedCollection new.
+            labels     := OrderedCollection new.
+            percentage := 100.
+
+            aDef from:2 do:[:rgb|
+                colors add:(Color rgbValue:rgb).
+                labels add:('%1    %2 %%' bindWith:title with:percentage).
+                percentage := percentage - 20.
+            ].
+        ].
+        ColorMenu addUserDefinedColors:colors labels:labels title:title.
     ].
-    label := ' %1 ' bindWith:(aName ? '      ').
-    label := Text string:label emphasis:#backgroundColor->aColor.
-
-    item := MenuItem labeled:label.
-    item argument:aColor.
-
-    submenu addItem:item.
+"
 !
 
 removeAllUserDefinedColors
-    |menu item|
-
-"
-self addUserDefinedColor:Color lightBlue lightened named:'UBS   lightBlue'.
-self addUserDefinedColor:Color red lightened named:'UBS   red'.
-self removeAllUserDefinedColors.
-"
-    menu := self colorMenu.
-
-    [   item := menu detectItem:[:el| el nameKey == #userDefined ] ifNone:nil.
-        item notNil
-    ] whileTrue:[
-        menu removeItem:item.
-    ].
-!
-
-removeUserDefinedColor:aColor
-    |menu menuitem submenu subitem|
-
-"
-ColorMenuSpec := nil.
-
-self removeUserDefinedColor:Color lightBlue lightened
-self removeUserDefinedColor:Color red lightened
-self addUserDefinedColor:Color lightBlue lightened named:'UBS   lightBlue'
-self addUserDefinedColor:Color red lightened named:'UBS   red'
-
-"
-    menu := self colorMenu.
-    menuitem := menu detectItem:[:el| (el nameKey == #userDefined and:[el submenu notNil]) ]
-                         ifNone:nil.
-
-    menuitem isNil ifTrue:[ ^ self ].
-
-    submenu := menuitem submenu.
-    subitem := submenu detectItem:[:el| el argument = aColor] ifNone:nil.
-    subitem notNil ifTrue:[
-        submenu removeItem:subitem.
-    ].
-    submenu items size > 0 ifTrue:[^ self].
-
-    [menuitem notNil] whileTrue:[
-        "/ remove submenu and separators
-        menu removeItem:menuitem.
-        menuitem := menu detectItem:[:el| el nameKey == #userDefined ] ifNone:nil.
-    ].
-!
-
-userDefinedLabel
-    "returns the user defined label used by the ColorMenu"
-
-    ^ XPToolbarIconLibrary changesBrowser24x24Icon
+    ColorMenuSpec := nil.
 ! !
 
 !ColorMenu class methodsFor:'menu spec'!
 
+colorMenu:labelAreColored value:aValue
+    "returns a color menu
+    "
+    |menu|
+
+    menu := Menu new.
+
+    self colorMenu itemsDo:[:el|
+        menu addItem:(self resolveMenuItem:el value:aValue labelAreColored:labelAreColored).
+    ].
+    ^ menu
+"
+(ColorMenu colorMenu:true  value:nil) startUp
+(ColorMenu colorMenu:false value:#aSelector:) startUp
+"
+! !
+
+!ColorMenu class methodsFor:'private'!
+
 colorMenu
     |menuItem subItem baseColor color label getColSel submenu colorId|
-"
-ColorMenuSpec := nil.
-self colorMenu
-"
+
     ColorMenuSpec notNil ifTrue:[ ^ ColorMenuSpec ].
 
     ColorMenuSpec := Menu new.
@@ -183,6 +188,7 @@
 
             menuItem label:(Text string:'  ' emphasis:(#backgroundColor->baseColor)).
             menuItem submenu:(submenu := Menu new).
+            menuItem isButton:true.
 
             aSlice last do:[:el|         
                 color := label := nil.
@@ -201,7 +207,7 @@
                     label isNil ifTrue:[
                         label := getColSel, ' ', el printString.
                     ].
-                    subItem := MenuItem label:label.
+                    subItem := MenuItem label:'Color ', label.
                     subItem argument:color.
                     submenu addItem:subItem.
                 ].
@@ -211,49 +217,6 @@
     ^ ColorMenuSpec
 !
 
-colorMenu:labelAreColored value:aValue
-    "returns a color menu
-    "
-    |style menu menuItem subItem|
-
-    menu := Menu new.
-
-    labelAreColored == false ifTrue:[ style := #backgroundColor ]
-                            ifFalse:[ style := #color ].
-
-    self colorMenu itemsDo:[:anItem| |submenu|
-        menuItem := MenuItem label:(anItem rawLabel ? '').
-        menu addItem:menuItem.
-
-        anItem hasSubmenu ifTrue:[
-            menuItem isButton:true.
-            menuItem submenu:(submenu := Menu new).
-            anItem submenu itemsDo:[:el| | label color emphasis|
-            label := el rawLabel ? '    '.
-            color := el argument.
-
-            label isText ifFalse:[
-                emphasis := style->color.
-
-                style == #backgroundColor ifTrue:[
-                    color brightness < 0.5 ifTrue:[
-                        emphasis := Array with:emphasis with:(#color->Color white) 
-                    ]
-                ].
-                label := Text string:('Color ', label) emphasis:emphasis.
-            ].
-            subItem := MenuItem label:label value:aValue.
-            subItem argument:color.
-            submenu addItem:subItem.
-        ]].
-    ].
-    ^ menu
-"
-(ColorMenu colorMenu:true  value:nil) startUp
-(ColorMenu colorMenu:false value:#aSelector:) startUp
-"
-!
-
 colorMenuSpec
     "color definitions used to build a color menu
     "
@@ -261,7 +224,6 @@
 ColorMenuSpec := nil.
 "
   ^ #(
-nil                         "/ separator
         #(  gray
             gray:
             #(  white
@@ -301,6 +263,62 @@
         )
 
     )
+!
+
+resolveMenuItem:aMenuItem value:aValue labelAreColored:labelAreColored
+    |menuItem label color fgColor submenu|
+
+    label    := aMenuItem rawLabel ? ''.
+    menuItem := MenuItem label:label.
+    menuItem isButton:(aMenuItem isButton).
+
+    aMenuItem hasSubmenu ifFalse:[
+        color := aMenuItem argument.
+        menuItem argument:color.
+
+        (color isColor and:[label isText not]) ifTrue:[
+            labelAreColored ifTrue:[
+                label := Text string:label color:color.
+            ] ifFalse:[
+                color brightness < 0.5 ifTrue:[ fgColor := Color white ]
+                                      ifFalse:[ fgColor := Color black ].
+
+                label := Text string:label foregroundColor:fgColor backgroundColor:color.
+            ].
+            menuItem label:label.
+            menuItem value:aValue.
+        ].
+        ^ menuItem
+    ].
+    submenu := Menu new.
+    aMenuItem submenu itemsDo:[:el|
+        submenu addItem:(self resolveMenuItem:el value:aValue labelAreColored:labelAreColored).
+    ].
+    menuItem submenu:submenu.
+    ^ menuItem
+!
+
+userDefinedSubmenu
+    "answer the menu entry under which the userdefined color entries are placed"
+
+    |menu submenu item icon|
+
+    menu := self colorMenu.
+    item := menu detectItem:[:el| (el nameKey == #userDefined and:[el submenu notNil]) ]
+                     ifNone:nil.
+
+    item isNil ifTrue:[
+        icon := XPToolbarIconLibrary changesBrowser24x24Icon.
+        item := MenuItem labeled:icon.
+        item nameKey:#userDefined.
+        menu addItem:item beforeIndex:1.
+    ].
+
+    (submenu := item submenu) isNil ifTrue:[
+        submenu := Menu new.
+        item submenu:submenu.
+    ].
+    ^ submenu
 ! !
 
 !ColorMenu methodsFor:'accepting'!
@@ -430,6 +448,7 @@
                 nameKey:#def;
                 isButton: true). 
 
+    menu addItem:(MenuItem label:'') beforeIndex:1.
     item := MenuItem labeled:(Text string:'   ' emphasis:(#backgroundColor->DefaultViewBackgroundColor)).
     item value:#selection.
     menu addItem:item beforeIndex:1.
@@ -458,5 +477,5 @@
 !ColorMenu class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/ColorMenu.st,v 1.37 2007-11-28 12:38:23 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/ColorMenu.st,v 1.38 2007-12-04 08:15:55 ca Exp $'
 ! !