4
$\begingroup$

I have a three buttons next to each other that set the text justification to left, center or right. I would like the button to have Appearance->"Pressed" when that button agrees with the current justification. I can't seem to find a way to this. Here's the code...

Grid[{Button[
     Grid[{{"---"}, {"--"}, {"---"}, {"-"}}, 
      BaseStyle -> {9, "Label", Bold}, 
      Alignment -> {"Columns" -> {{#}}}, 
      Spacings -> {"Columns" -> {{0}}, 
        "Rows" -> {{-.8}}}], {SelectionMove[InputNotebook[], All, 
       Cell], SetOptions[NotebookSelection[InputNotebook[]], 
       TextAlignment -> #]}, Appearance->"Button"] & /@ {Left, 
    Center, Right}
  }, Spacings -> -1]

At first I thought this problem could be solved easily using a SetterBar[]. But I think SetterBar[] isn't really made to execute actions, whereas Button[] and ButtonBar[] are. I couldn't figure out a way to make it work, that's why I used a grid of buttons.

$\endgroup$
3
$\begingroup$

I'd say that is exactly the job for RadioButtonBar, you can set actions in the second argument of Dynamic, but it may be less convenient to make it look like button.

So if you want to something happen dynamically when certain conditions are met you can straightforwardly say it:

Dynamic[If[certainCondition, this, somethingelse]]

which in our case is e.g.:

Appearance ->  Dynamic @ If[
   SameQ[Left, CurrentValue[NotebookSelection[], TextAlignment]], 
   "Pressed", "Button"]

And the full code (with Evaluator->None since this action can be done by the FrontEnd alone):

Grid[{
  Button[
    Grid[{{"---"}, {"--"}, {"---"}, {"-"}}, 
      BaseStyle -> {9, "Label", Bold}, 
      Alignment -> {"Columns" -> {{#}}}, 
      Spacings -> {
        "Columns" -> {{0}}, 
        "Rows" -> {{-.8}}}
    ], 
    SelectionMove[InputNotebook[], All, Cell];  
    SetOptions[NotebookSelection[InputNotebook[]], TextAlignment -> #], 
    Appearance -> Dynamic@If[
        SameQ[#, CurrentValue[NotebookSelection[], TextAlignment]], 
        "Pressed", 
        "Button"
    ],
    Evaluator -> None
  ] & /@ {Left, Center, Right}}, 
  Spacings -> -1
]

gif

| improve this answer | |
$\endgroup$
  • $\begingroup$ when I run your code the alignment shifts but the "left aligned button" stays highlighted $\endgroup$ – Sascha Dec 16 '16 at 8:41
  • $\begingroup$ @Sascha... it's because you have to click on a different cell other than the one containing the button. Not sure why it does that, but works fine for any other cell. I noticed the same thing. $\endgroup$ – Michael McCain Dec 16 '16 at 8:49
  • $\begingroup$ @MichaelMcCain You are right, when I select a different cell (than the control cell itself) it works. $\endgroup$ – Sascha Dec 16 '16 at 8:51
  • $\begingroup$ @Sascha and Michael what may trick you is that you can change alignment of the cell that is above the cursor which is between cells but the selection won't reflect that untill you really move the cursor to that cell. $\endgroup$ – Kuba Dec 16 '16 at 8:55
  • $\begingroup$ @Kuba you are right, one has to specifically select the cell as shown in the .gif I added to your answer. $\endgroup$ – Sascha Dec 16 '16 at 8:57

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.