# Making button appearance change to “Pressed” depending on current state in notebook

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.

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
]


• when I run your code the alignment shifts but the "left aligned button" stays highlighted – Sascha Dec 16 '16 at 8:41
• @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. – Michael McCain Dec 16 '16 at 8:49
• @MichaelMcCain You are right, when I select a different cell (than the control cell itself) it works. – Sascha Dec 16 '16 at 8:51
• @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. – Kuba Dec 16 '16 at 8:55
• @Kuba you are right, one has to specifically select the cell as shown in the .gif I added to your answer. – Sascha Dec 16 '16 at 8:57