6
$\begingroup$

I have a PopupMenu in a Manipulate with items in it that are inapplicable in some cases, and rather than removing them, I would like to disable (just) those items that are inappropriate.

For example, I'd like to disable (not remove) everything in list that is, say, less than the item selected in filter:

Manipulate[
 {filter, list},
 {{filter, 1, "Filter:"}, {1, 2, 3, 4, 5, 6}, ControlType -> PopupMenu},
 {{list, 1, "List:"}, {1, 2, 3, 4, 5, 6}, ControlType -> PopupMenu}
 ]

Is there a way to disable specific individual items in a PopupMenu, without resorting to a custom control?

$\endgroup$
  • $\begingroup$ @Kuba: Sorry, pretty basic; added. $\endgroup$ – orome Jun 25 '15 at 13:47
  • $\begingroup$ @Kuba: Better for me is Manipulate. And the above doesn't disable; should it? $\endgroup$ – orome Jun 25 '15 at 23:39
  • 1
    $\begingroup$ @Kuba: Just says "too low". I can still click it, and it still looks enabled. Disabled == does not respond to clicks and shows disabled UI affordance. $\endgroup$ – orome Jun 26 '15 at 14:25
  • $\begingroup$ Understood. But I'm not sure what do you mean you can click it. How would you describe not being able to click? :) $\endgroup$ – Kuba Jun 26 '15 at 14:41
  • $\begingroup$ @Kuba: It should behave just as a disabled menu item in the hosting GUI behaves — e.g., on OS X, the item is grayed out, and the UI provides no affordance for clicking when the mouse is over the item: the cursor does not change, the item is not highlighted, and clicking produces no change at all (nothing happens). It sounds like the answer is actually "no". $\endgroup$ – orome Jun 26 '15 at 14:46
6
$\begingroup$

There's no way to do this that conforms fully to UI guidelines (at least not for OS X). For example, your "disabled" items will still highlight as you hover over them and will still react to selection (thought they will do nothing).

This is as close as you can get:

Manipulate[
 {filter, list}
 ,
 {{filter, 1, "Filter:"}, {1, 2, 3, 4, 5, 6}, ControlType -> PopupMenu} ,
 {list, None} ,
 Grid[{{"List:", 
    PopupMenu[
     Dynamic[list, If[# < filter, , list = #] &], 
     # -> Dynamic[Style[#, If[# < filter, Gray, Black]]] & /@ {1, 2, 3, 4, 5, 6}
    ]
 }}
]]

enter image description here

$\endgroup$
  • $\begingroup$ Last time I delete my answer before fixing it +1 $\endgroup$ – Charlie Joey Hadley Jun 26 '15 at 14:51
  • $\begingroup$ @Kuba - How did you get the picture of the popup menu activated into the answer? I am using the SE Uploader tool and I am not able to do this? $\endgroup$ – Jack LaVigne Aug 23 '15 at 14:21
  • $\begingroup$ @JackLaVigne Sorry for missing your comment! I just come here to check something and saw it. If you are still interested, I did a screen shot, pasted to nb, crop with crop tool, copy as bitmap and ctrl+v here. That is what I usually do. $\endgroup$ – Kuba Aug 12 '16 at 12:47
  • $\begingroup$ @Kuba. Thank you. Sometimes have to wait for good things. 😄 $\endgroup$ – Jack LaVigne Aug 13 '16 at 13:59
3
$\begingroup$

It's a DynamicModule not a Manipulate, but it works (Mma V10.1 on Mac and Windows)

DynamicModule[{filter, list = 1},
 Column[{
   PopupMenu[Dynamic[filter], Range[6]],
   PopupMenu[Dynamic[list], 
    Table[With[{i = i}, (If[i < filter, list = list, list = i]) -> 
       i], {i, 6}]],
   {"filter", Dynamic[filter]},
   {"list", Dynamic[list]}
   }]]

enter image description here

I would advise moving to ActionMenu though, as it is much neater and designed for this type of thing (Mma V10.1 on Mac and Windows):

DynamicModule[{filter, list = 1},
 Column[{
   PopupMenu[Dynamic[filter], Range[6]],
   ActionMenu[Dynamic[list], 
    Table[With[{i = i}, 
      i :> (If[i < filter, list = list, list = i])], {i, 6}]],
   {"filter", Dynamic[filter]},
   {"list", Dynamic[list]}
   }]]

enter image description here

$\endgroup$
  • $\begingroup$ Neither of these work. $\endgroup$ – orome Jun 26 '15 at 16:51
  • $\begingroup$ @raxacoricofallapatorius it works on both my machines (Mma V10.1 on Mac and Windows). I do think that @Kuba's use of the second argument of Dynamic is a more elegant approach for PopupMenu though. $\endgroup$ – Charlie Joey Hadley Jun 27 '15 at 12:10
  • $\begingroup$ All it does is the last of the requirements: "the item is grayed out, and the UI provides no affordance for clicking when the mouse is over the item: the cursor does not change, the item is not highlighted, and clicking produces no change at all (nothing happens)" $\endgroup$ – orome Jun 28 '15 at 14:20
2
$\begingroup$

Expanding on Kuba's answers, if you use Dynamic for both controls I believe you can fully accomplish your goal.

When you set filter it will both gray out the elements less than the filter in list and if the element in list is less than the filter it will reset it to the filter value.

Manipulate[
 {filter, list},

 (* Manipulate Controls *)

 {{filter, 1}, None},
 {{list,   2}, None},

 Grid[{
   {
    "Filter:",
    PopupMenu[
     Dynamic[filter, {(filter = #) &, (If[list < #, list = #]) &}],
     {1, 2, 3, 4, 5, 6}
     ]
    },
   {
    "List:",
    PopupMenu[
     Dynamic[list, 
      If[# < filter, , list = #] &], # -> 
        Dynamic[Style[#, If[# < filter, Gray, Black]]] & /@ {1, 2, 3, 
       4, 5, 6}
     ] (* end of PopupMenu *)
    } (* end of Row in Grid *)
   }] (* end of Grid *)

 ]

I don't know how to place figures in the answer where the popup menu is present (I'll ask Kuba and change the answer if I can learn how to do it). But if you try this I think you will see that it works the way you intended.

$\endgroup$
  • $\begingroup$ As with the other solutions, this misses: "... the UI provides no affordance for clicking when the mouse is over the item: the cursor does not change, the item is not highlighted ..." $\endgroup$ – orome Aug 23 '15 at 14:43
  • $\begingroup$ When the cursor is over a grayed out item clicking has no effect. Changes only occur when a non-gray item is clicked. This is on windows. The color of the item gives one its enabled/disabled state. $\endgroup$ – Jack LaVigne Aug 24 '15 at 2:44
  • $\begingroup$ Yes, but the item highlights when hovered over (at least on OS X). $\endgroup$ – orome Aug 30 '15 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.