14

Could anyone point out why this is not working in Mathematica 8:

DynamicModule[{x = Pink},
 Row[
  {Style["Hello", x],
   Mouseover[
    x = Green; "World",
    x = Blue; "World"]}]]

What I expect is to see the color of "Hello" change when I mouse over "World". What I am getting is a pink "Hello" that never changes color.

gdelfino
  • 11,053
  • 6
  • 44
  • 48
  • 1
    I believe the reason this doesn't work is because `MouseOver` evaluates the `x = ...` expressions only once. I don't know enough as to how you can get the behavior you want though. – Mike Bailey Nov 29 '11 at 22:49
  • 1
    Wow, I got really good answers from all of you. I up voted you all. Selecting THE answer is really hard. Sjoerd and Mike explained very well why my code fails to work which is what I asked. And Heike, Mr.Wizard and Arnoud proposed how to solve the problem which is what I really wanted to ask. THANK YOU ALL! – gdelfino Nov 30 '11 at 17:49

5 Answers5

10

I think I have waited long enough to be fair. Here is my proposal:

DynamicModule[{x = Pink},
 Row[{
   Dynamic@Style["Hello", If[MouseAnnotation[] === 1, x = Green; Blue, x]],
   Annotation["World", 1, "Mouse"]
 }]
]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • I would've answered sooner but I had to leave almost immediately after posting my answer. This is the most elegant solution though. – Mike Bailey Nov 30 '11 at 15:12
  • @Mike yours is the answer I would accept were this question mine. It is extremely concise and totally clear. This "answer" doesn't address the OP's question at all; it's quite possible he knew of a method like this before he asked, and was only interested in the behavior of `Mouseover`. – Mr.Wizard Nov 30 '11 at 15:17
9

If you look at the FullForm of the result, you'll see that it only contains the last part of each compound instruction set. Apparently Mouseover evaluates its arguments and only stores the results.

enter image description here

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
9

Try using EventHandler with "MouseEntered" and "MouseExited":

DynamicModule[{c = Pink}, Row[{
 Style["Hello", FontColor -> Dynamic[c]], 
 EventHandler[
  "World", {
   "MouseEntered" :> (c = Blue), 
   "MouseExited" :> (c = Green)
}]}]]
Arnoud Buzing
  • 15,383
  • 3
  • 20
  • 50
  • Interesting. Is there a reason you don't implement the example with this method? – Mr.Wizard Nov 30 '11 at 01:53
  • @Mr.Wizard -- You mean why isn't Mouseover behaving like EventHandler above? I'd have to check. – Arnoud Buzing Nov 30 '11 at 01:59
  • No, I think Mike's answer shows the behavior of Mouseover quite well. I just meant is there a reason you did not use this method to write code that produces the result that mine does, which is what I believe the OP was trying to achieve? – Mr.Wizard Nov 30 '11 at 02:01
  • I thought it was more illustrative to `EventHandler`'s behavior. But I changed it to act like the original example. – Arnoud Buzing Nov 30 '11 at 15:36
  • Please do as you think best, but that seems more clear to me. However, this doesn't appear to be working; is this v8 only? – Mr.Wizard Nov 30 '11 at 15:39
  • 1
    @Mr.Wizard -- Yes, this is new in V8 (but now that I look at it, apparently not documented under EventHandler). – Arnoud Buzing Nov 30 '11 at 19:46
7

A quick check shows that Mouseover evaluates all the expressions inside of it when you first launch it:

Mouseover[Print["One"]; 1, Print["Two"]; 2]

The idiomatic way of actually making the Mouseover modify the values of x is to use MouseAnnotation. Mr. Wizard's answer describes how to achieve this.

Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
7

As an alternative you could do something like

DynamicModule[{col = Pink}, 
 Row[{Style["Hello ", FontColor -> Dynamic[col]], 
   Dynamic@If[CurrentValue["MouseOver"],
     col = Green; "World", 
     col = col /. Green -> Blue; "World"]}]
] 
Heike
  • 24,102
  • 2
  • 31
  • 45
  • @Mr.Wizard: Yes I know. I realized this later but I hoped nobody would notice until I got a chance to fix it (which I have now). – Heike Nov 30 '11 at 09:11