One of the primary criteria for an acceptable Demonstration is, of course, that multiple copies of the Manipulate Output Cells [MOCs] have to have independent controls and datasets: the operation of controls in one MOC can’t effect those in other ones or the corresponding data. Which is something I’m having some difficulty with as, in part, I don’t fully understand the scope and implications of various programming constructs. Particularly in cases where there are mouse event handlers attached to the displayed expression.
But the simplest example that I think shows the structure required for my purposes is the following code and images for the two MOCs (the "rslt = n2" bit isn't relevant - preliminary testing):
Manipulate[dsply$expr$2a,
Style[Text[Dynamic[mse$psn]], 12], {{n2, 2}, 1, 10, .1},
{mse$psn, ControlType -> None},
TrackedSymbols :> {n2},
Initialization :> (mse$psn = {0, 0};
dsply$expr$2a :=
Module[{rslt}, rslt = n2;
EventHandler[
Dynamic[Plot[Sin[n2 x], {x, 0, 3 \[Pi]},
ImageSize -> {200, 150}]], {"MouseClicked" :> (mse$psn =
MousePosition["EventHandlerAbsolute"])}]])]
As suggested by the above images for the two MOCs, when they are first evaluated and before moving either of the sliders, clicking on the two different plot areas updates only the corresponding display variable (mse$psn), and this can be done multiple times. But as soon as either of the sliders is moved, the other slider is effectively disabled – no change in the corresponding Plot – and clicking on either Plot area updates only the display variable in the second MOC.
I have taken a look at several Demonstrations (here and here), both of which use more or less the same structure – i.e., “EventHandler[Dynamic[Graphics … ]]
” – so I must be missing some aspect or option. And I have tried various variations – DynamicModule, With, passing parameters, etc – but I think that those, absent a better understanding, have been mostly shots-in-the-dark.
Somewhat in passing, while I am impressed with Mathematica in general, I’m also kind of overwhelmed by the frequently cryptic if not inconsistent nature of many of the commands, structures, and underlying processing algorithms. Consequently, while there seems to be quite a bit of detail available on the evaluation of expressions, I was wondering if there’s something available that describes those algorithms, the scope and values of various variables over the course of program execution – what’s happening underneath the hood. I have thought of purchasing the Work Bench program with the idea that it might help in understanding those aspects, but I saw a brief note on the Wolfram WB site suggesting that it is not able to profile or debug various dynamic structures. And Trace might be similarly limited though I haven’t delved much into that possibility.
Thoughts? Suggestions? Pointers
Addendum:
As per Kuba's suggestions below, I've modified the program as follows, and which seems to give the required behaviours:
Manipulate[
DynamicModule[{},
EventHandler[
Dynamic[Plot[Sin[n3 x], {x, 0, 3 \[Pi]},
ImageSize -> {200, 150}]], {"MouseClicked" :> (mse$psn =
MousePosition["EventHandlerAbsolute"])}]],
Style[Text[Dynamic[mse$psn]], 12], {{n3, 2}, 1, 10, .1},
{mse$psn, ControlType -> None},
TrackedSymbols :> {n2},
Initialization :> (mse$psn = {0, 0}
)]
However, I am not at all sure why the first case didn't work while the second one does. Seems that even though the function dsply$expr$2a
is defined in the Initialization section, its call when variables change should have created a new event handler updated with variables local to the instance of the MOC active at the time - obviously not the case but it might be useful, maybe to others as well, to know the differences between the two cases.
dsply$expr$2a
and not scoping it. And inside ismse$psn
which is scoped. At the end you will have shareddsply..
with reference topsn
that was evaluated last. So it will work correctly only in the last manipulate. $\endgroup$dsply$expr$2a
in the first parameter of the Manipulate withDynamicModule[{}, EventHandler[Dynamic[Plot ...]]]
which does work as required. Even if I don't quite understand the why of it. $\endgroup$Manipulate[ {a, expr}, {{a, 1}, ControlType -> None}, Initialization :> ( expr = EventHandler[ Framed@Dynamic@a, {"MouseClicked" :> (Print@1; a++)}])]
$\endgroup$