22

Background: In the Mathematica tutorial 'Dynamic Interactivity' I read that there is a way of developing GUIs in Mathematica that do not use Manipulate. I want to investigate this further.

Question: What is the equivalent of Manipulate[i,{i,1,5,1}] in 'lower-level' Mathematica functions?

István Zachar
  • 47,032
  • 20
  • 143
  • 291
nilo de roock
  • 9,657
  • 3
  • 35
  • 77

2 Answers2

28

Start from

{Slider[Dynamic[x], {1, 5, 1}], Dynamic[x]}

enter image description here

Next localize control variable:

DynamicModule[{x}, {Slider[Dynamic[x], {1, 5, 1}], Dynamic[x]}]

enter image description here

And add some interface elements:

Panel@DynamicModule[{x},  Column[{Slider[Dynamic[x], 
{1, 5, 1}], Panel[Dynamic[x], ImageSize -> 200]}]]

enter image description here

Add even more

Panel@DynamicModule[{x},   Column[{Row[{"x", Spacer[10], 
Animator[Dynamic[x], {1, 5, 1}, AnimationRunning -> False, 
ImageSize -> Small]}], Panel[Dynamic[x], ImageSize -> 235]}]]

enter image description here

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
10

I realize this isn't what you meant by "lower level" but nevertheless viewing the Cell Expression for various kinds of output can be interesting and informative. Based on observing that expression it is possible to craft this:

DynamicBox[
  Manipulate`ManipulateBoxes[1, StandardForm, 
   "Variables" :> {a$$ = 0},
   "ControllerVariables" :> {Hold[a$$, 0]}, 
   "OtherVariables" :> {}, 
   "Body" :> Plot[Sin[x (1 + a$$ x)], {x, 0, 6}], 
   "Specifications" :> {{a$$, 0, 2}},
   "Options" :> {}, 
   "DefaultOptions" :> {}
  ]
] // ToExpression

Mathematica graphics

Of course direct use like this isn't advisable because I have stripped out all localization etc. for this example that is normally generated automatically.


As a further point of curiosity, if you want to see the very low level Box expressions, leave off DynamicBox and ToExpression from the example above to reveal this:

PaneBox[PanelBox[
  DynamicWrapperBox[
   GridBox[{{ItemBox[
       ItemBox[StyleBox[
         GridBox[{{TagBox["a", HoldForm], 
            TagBox[PaneBox[
              DynamicModuleBox[{Typeset`open$$ = False, 
                Typeset`paused$$ = 0, Typeset`rate$$ = Automatic, 
                Typeset`dir$$ = Forward}, 
               StyleBox[
                DynamicBox[
                 FEPrivate`FrontEndResource["FEExpressions", 
                   "Manipulator04"][Dynamic[a$$], 
                  Dynamic[a$$], {0, 2}, Medium, Small, Automatic, 
                  True, Automatic, False, True, True, All, False, {}, 
                  Dynamic[Typeset`open$$], Dynamic[Typeset`paused$$], 
                  Dynamic[Typeset`rate$$], Dynamic[Typeset`dir$$]]], 
                DynamicUpdating -> True], 
               DynamicModuleValues -> Automatic], ImageMargins -> 0, 
              BaselinePosition -> Baseline], 
             Manipulate`InterpretManipulator[
              Dynamic[a$$], {0, 2}, {ContinuousAction -> True, 
               AutoAction -> False}]]}}, AutoDelete -> False, 
          GridBoxItemSize -> {"Columns" -> {{Automatic}}, 
            "Rows" -> {{Automatic}}}, 
          ColumnAlignments -> {Right, Left}], "ManipulateLabel", 
         StripOnInput -> False], Alignment -> {Automatic, Inherited}, 
        StripOnInput -> False], Background -> None, 
       StripOnInput -> False]}, {ItemBox[
       TagBox[StyleBox[PaneBox[DynamicBox[ToBoxes[Refresh[\!\(\*
TagBox[
RowBox[{"Plot", "[", 
RowBox[{
RowBox[{"Sin", "[", 
RowBox[{"x", " ", 
RowBox[{"(", 
RowBox[{"1", "+", 
RowBox[{"a$$", " ", "x"}]}], ")"}]}], "]"}], ",", 
RowBox[{"{", 
RowBox[{"x", ",", "0", ",", "6"}], "}"}]}], "]"}],
Identity,
Editable->True,
Selectable->True]\), TrackedSymbols -> Full], StandardForm], 
           SynchronousUpdating -> Automatic], ImageMargins -> 10], 
         Deployed -> False, ScriptLevel -> 0, 
         GraphicsBoxOptions -> {PreserveImageOptions -> True}, 
         Graphics3DBoxOptions -> {PreserveImageOptions -> True}, 
         StripOnInput -> False], Identity, Selectable -> False, 
        Editable -> False], Alignment -> {Left, Center}, 
       Background -> GrayLevel[1], Frame -> 1, 
       FrameStyle -> GrayLevel[0, 0.2], ItemSize -> Automatic, 
       StripOnInput -> False]}}, AutoDelete -> False, 
    GridBoxItemSize -> {"Columns" -> {{Automatic}}, 
      "Rows" -> {{Automatic}}}, ColumnAlignments -> Left, 
    GridFrame -> False, GridFrameMargins -> 1, RowSpacings -> 2, 
    RowAlignments -> Top], 
   If[CurrentValue["SelectionOver"], 
    Manipulate`Dump`ReadControllerState[(Manipulate`Dump`updateOneVar[\
#1, CurrentValue["PreviousFormatTime"], 
         CurrentValue[
          "CurrentFormatTime"]] &) /@ {Manipulate`Dump`controllerLink[
        a$$, "X1", If["DefaultAbsolute", True, "JB1"], False, {0, 2}, 
        1.]}, CurrentValue[{"ControllerData", {"Gamepad", "Joystick", 
        "Multi-Axis Controller"}}], {}]]], DefaultBaseStyle -> {}, 
  FrameMargins -> {{5, 5}, {5, 5}}], ImageMargins -> 0, 
 BaselinePosition -> Automatic]

I am rather glad we don't have to do "low level" FrontEnd programming very often!

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371