Writing an event driven UI using Mathematica Manipulate

by Nasser M. Abbasi  6/30/11

First example in report

Manipulate[ 
Row[{

Dynamic[Refresh[
If[Not[event=="reset"],
{
result=dispatch[event]; 
event="reset"
}
];
result,
TrackedSymbols-> {event}]
],

Dynamic[Refresh[event="a_event";"",TrackedSymbols->{a}]],
Dynamic[Refresh[event="b_event";"",TrackedSymbols-> {b}]]
}],

{{a,0,"a"},0,100,1},
{{b,0,"b"},0,30,1},

{{event,"reset"},ControlType->None},

TrackedSymbols->{None},

Initialization:> 
(
dispatch[event_String]:=Module[{},
Text@Row[{"event: ", Style[event,Bold], ". Time is ", Date[]}]
]
)
]

second example in report to solve an ODE

Manipulate[ 

Row[{
Dynamic[Refresh[

If[Not[event=="reset"],
{
result=updatePlot[solution,color]; 
event="reset"
}
];
result,TrackedSymbols-> {event}] ],

Dynamic[Refresh[event="initial_condition";
solution=solveODE[initialCondition];
"",TrackedSymbols->{initialCondition}]],

Dynamic[Refresh[event="color";"",TrackedSymbols->{color}]]
}],

{{initialCondition,0,"y[0]="},0,100,1},
Control[{ {color,Blue,"select color"},{Red->"Red",Blue->"Blue"},ControlType->PopupMenu}],

{{event,"reset"},ControlType->None},
{{solution,0},ControlType->None},
{{result,0},ControlType->None},

TrackedSymbols->{None},

Initialization:> 
(
solveODE[initialCondition_]:=Module[{t,y},
y/.First@NDSolve[{y'[t]==t Cos[t]^2 ,y[0]==initialCondition},y,{t,0,40}]];

updatePlot[solution_,color_]:=Module[{t},
Plot[Evaluate[solution[t]],{t,0,40},PlotRange->{{0,40},{0,100}},
ImageSize->300,PlotStyle->color]
]
)
]

example without event driven

Manipulate[ 

Module[{y,t, solution},

solution = y/.First@NDSolve[{y'[t]==t Cos[t]^2 ,y[0]==initialCondition},y,{t,0,40}];

Plot[Evaluate[solution[t]],{t,0,40},PlotRange->{{0,40},{0,100}},
ImageSize->300,PlotStyle->color]
],

{{initialCondition,0,"y[0]="},0,100,1},

Control[{ {color,Blue,"select color"},{Red->"Red",Blue->"Blue"},ControlType->PopupMenu}]

]