Normally, LocatorPane detects any single click as in the following example:

    DynamicModule[{pt = {1, 1}/2}, 
     {LocatorPane[Dynamic[pt], Graphics[{Gray, Disk[]}]], Dynamic[pt]}]

My goal is to redefine the behaviour of LocatorPane to detect only double clicks (i.e., the value of pt is returned only when the user double-clicks in the LocatorPane)

I tried the following definition, but with no success!

doubleclickPane[pt_, back_, opts : OptionsPattern[]] := 
  DynamicModule[{ptt = pt},
    Deploy @ EventHandler[
      LocatorPane[Dynamic@ptt, back, ,Evaluate[FilterRules[{opts}, Options[LocatorPane]]]],
      {"MouseDown" :> (
        ptt = MousePosition["Graphics"]; 
        If[Mod[CurrentValue["MouseClickCount"], 2] == 0, pt = MousePosition["Graphics"]]

Your code seems to intend to do something a little different than what I understood from the question - so I hope this is right. :)

Fixing the code require two changes,

  1. remove Deploy -- it makes the EventHandler inoperative; and
  2. put Dynamic around the argument pt -- one of the important attributes of Dynamic is that it is HoldFirst, which allows the symbol to be passed to a control instead of merely its value (otherwise the symbol could not be updated).

Code -- Edit: Updated to handle options

doubleclickPane[Dynamic[pt_], back_, opts : OptionsPattern[LocatorPane]] := 
 DynamicModule[{ptt = pt}, 
  EventHandler[LocatorPane[Dynamic@ptt, back, opts],
   {"MouseDown" :> (ptt = MousePosition["Graphics"];
      If[Mod[CurrentValue["MouseClickCount"], 2] == 0, 
       pt = MousePosition["Graphics"]])}]]

In this form a single click moves the Locator (i.e. updates the local variable ptt). A double-click changes the argument pt. Since ptt appears outside the If statement, I assumed that was the desired behavior.

Example: I double-clicked near {0.5, 0.5} and then single-clicked near {-0.35, 0.2}.

myPt = {0, 0};
 Graphics[Circle[], PlotRange -> 1, Frame -> True, GridLines -> Automatic]]

Mathematica graphics

Mathematica graphics

Follow-up: If I had passed myPt instead of Dynamic[myPt] to doubleClickPane (and there was no Dynamic in the definition), only the value {0, 0} would be passed. The control doubleClickPane would not be able to change the variable myPt, because that symbol never got passed to the code. As it is, pt is the symbol myPt in the function call and the important piece of code

pt = MousePosition["Graphics"]

is really

myPt = MousePosition["Graphics"]]

Without Dynamic, this line becomes the invalid statement

{0, 0} = MousePosition["Graphics"]]
  • $\begingroup$ Your code works very good!I can not understand why you use Dynamic[pt_] instead of pt_ in your definition of doubleclickPane! Also why Deploy@ is nouse here? $\endgroup$
    – kornaros
    Aug 10 '13 at 23:01
  • $\begingroup$ @kornaros I updated the answer to address your questions. $\endgroup$
    – Michael E2
    Aug 10 '13 at 23:17
  • $\begingroup$ I tried your code without Dynamic over pt_ and worked. Maybe I am wrong...Anyway thank you for your code&comments. $\endgroup$
    – kornaros
    Aug 10 '13 at 23:47
  • $\begingroup$ I tried some LocatorPane's options like Appearance ->"String" in doubleclickPane but without any success! Any ideas? $\endgroup$
    – kornaros
    Aug 11 '13 at 0:11
  • $\begingroup$ @kornaros For the options, I simplified the code and it worked (see above). I don't know what you mean when you say it "worked" without Dynamic. I removed Dynamic from both Dynamic[pt_] and Dynamic[myPt], and myPt was always {0, 0}. It never changed. In other words, double-clicks had no effect. $\endgroup$
    – Michael E2
    Aug 11 '13 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.