1

I have created an action menu that allows you to change the style of a cell. It works. However, I would like it to dynamically update the action menu "name". I can't get the name to refresh. Anyone have any ideas? Here is my code.

ActionMenu[
 Dynamic[CurrentValue[Cell, "CellStyleName"]]
 ,
 Evaluate[
    # :> FrontEndTokenExecute[InputNotebook[], "Style", #] & /@ {
       "Subtitle", "Section", "Text", "Input", "Subsection", 
       "DisplayFormula", "DisplayFormulaNumbered"
    }
 ], 
 Appearance -> "PopupMenu"
]
Kuba
  • 136,707
  • 13
  • 279
  • 740
B flat
  • 5,523
  • 2
  • 14
  • 36
  • It's not only about refreshing, it's a problem of getting CurrentValue[Cell, "CellStyleName"] from the Cell you really want it. E.g. when you ust this to change the style of the cell that ActionMenu is in, it will update. – Kuba Oct 08 '15 at 09:24
  • I see. To further clarify... I am attempting to replicate the functionality built into Mathematica. When one clicks on the format menu and then hovers over Style, you can see that the correct style is checked for the current cell in the notebook. – B flat Oct 08 '15 at 09:28
  • I think I see what you are saying. – B flat Oct 08 '15 at 09:32
  • 1
    I wondered how formatting toolbar menu is done and I've not found any sources in Installation directory. Maybe it's not easy/possible to this in pure Mathematica, at least in efficient way. You can always put UpdateInterval and check SelectedCells[] styles but this isn't neat for me. – Kuba Oct 08 '15 at 09:38
  • Yes. I placed the button in my docked cell. The name is now updated to "Docked Cell" and does not change no matter what the current cell style is at the position of my curser in the notebook. I can't seem to figure out a way to point to the current cell. – B flat Oct 08 '15 at 09:39
  • 1
    I see. I'll mess around with this some more and see if I can get anything to work. It seems I am looking for a function that replicated InputNotebook[] but at the cell level. Something like InputCell[] but this doesn't exist. Thank you for edit. – B flat Oct 08 '15 at 09:41

1 Answers1

1

After a couple hours of tinkering around... I think I got it. Here it is...

currentStyle = 
  DynamicModule[{label = "         "}, 
   Dynamic[With[{sel = Cells[NotebookSelection[InputNotebook[]]]}, 
     If[sel =!= {}, 
      label = CurrentValue[sel[[1]], "CellStyleName"]]];
    label, UpdateInterval -> .5]];



ActionMenu[currentStyle, 
 Evaluate[# :> 
     FrontEndTokenExecute[InputNotebook[], 
      "Style", #] & /@ {"Subtitle", "Section", "Text", "Input", 
    "Subsection", "DisplayFormula", "DisplayFormulaNumbered"}], 
 Appearance -> "PopupMenu"]

Thank you for your help Kuba. I would not have known where to start.

B flat
  • 5,523
  • 2
  • 14
  • 36