Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Locators and Dynamic

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89536] Re: [mg89397] Locators and Dynamic
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Thu, 12 Jun 2008 02:58:16 -0400 (EDT)
  • Reply-to: jfultz at wolfram.com

On Sun, 8 Jun 2008 02:32:50 -0400 (EDT), Ekta Obheroi wrote:
> Hi everyone,
>
> I have a question regarding locators being used with Dynamic. I have the
> following scenario:
>
> height=833;
> width = 1440;
> (*Create a notebook randomly on the screen*)
> nb = NotebookCreate[WindowSize -> {240, 240},
> WindowMargins -> {{RandomInteger[{0, width}], Automatic}, {Automatic,
> RandomInteger[{0, height}]}}]
>
> (*Get the x and y coordinates of the position of the notebooks*)
> getCoords[nb_] := {#1, height - #2} & @@  Flatten[WindowMargins /.
> Options[nb]][[{1, -1}]]
>
> What I would like to do now is, if I move the newly created notebook
> anywhere on the screen, I would like to have x and y coordinates print to
> the screen dynamically.
> I tried the following:
> DynamicModule[{},
> Dynamic[ getCoords[nb]]
> ]
> When I run this the first time, it works. However, when I move the
> notebook (nb) it does not update the value on the screen.
>
> Please let me know how this can be accomplished.
>
> Thanks very much

Hmm...this ought to work.  But, fortunately, you can make it work with code that 
is more compact and arguably easier to understand.  Use CurrentValue instead of 
Options.  Redefine getCoords as...

getCoords[nb_] := {#1, height - #2} & @@ 
  Flatten[CurrentValue[nb, WindowMargins]][[{1, -1}]]

Also, your code won't always do nice things because the parts of WindowMargins 
which you're plucking can sometimes be Automatic rather than explicit numerical 
values.  But perhaps you already knew that.

Sincerely,
 
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.




  • Prev by Date: Help with Alignment for PasteButtons
  • Next by Date: Re: ConditionNumber
  • Previous by thread: Locators and Dynamic
  • Next by thread: ListAnimate or movie with No AppearanceElements