Difference between revisions of "Fix "focus follows mouse" matlab focus bug"

From Applied Optics Wiki
Jump to: navigation, search
(Created page with "If you use a focus policy such as focus follows mouse or focus under mouse and use matlab you will quickly find matlab becomes unusable. In the olden days the destination for a...")
 
Line 27: Line 27:
  
 
   %% fix the annoying focus follows mouse bug  
 
   %% fix the annoying focus follows mouse bug  
   %% (c) GPL SA 2013  
+
   %% (c) GPL Matt Clark 2013  
 
   function setfocus(jAxis, jEventData, hFig)  
 
   function setfocus(jAxis, jEventData, hFig)  
 
   global hasfocus;  
 
   global hasfocus;  

Revision as of 11:13, 24 July 2013

If you use a focus policy such as focus follows mouse or focus under mouse and use matlab you will quickly find matlab becomes unusable.

In the olden days the destination for any graphics operation (eg plot()) was determined by the figure(); command. A few years ago matlab decided this confused windows users and decided as a matter of policy that the focus selected by figure() would be overridden by whatever figure window had been last focussed by the user (normally clicking in the window).

This was a daft idea - it may have saved a few newbies from being confused but the great power of matlab is that it is script driven and following the user focus takes control away from the script.

As a side effect, it made matlab very hard to use if you use a window manager focus policy that does not follow windows conventions, in particular "focus follows mouse", "focus under mouse" or variations on these schemes. These are very common outside of windows and mac environments and extremely useful. Once you have gotten used to them you'll never go back. The problem is that when you use these schemes you cannot help but focus the figure windows as you move the mouse around. This results in your matlab scripts plotting in random figure windows which completely screws up your scripts and figures.

Anyway help is at hand -- here are a few lines of code that will undo this daft design decision and return matlab to a state where figure() determines which window is plotted to - not that last figure the mouse happened to brush over:

(1) Put this function (figure.m) in your path:

 %% fix the annoying focus follows mouse bug 
 %% (c) GPL Matt Clark 2013 
 function h=figure(n); 
   global hasfocus; 
   if(~exist(‘n’)) 
     n=1; 
   end;
   h=builtin(‘figure’,n); 
   jFig = get(h,’JavaFrame’); 
   jAxis = jFig.getAxisComponent; 
   set(jAxis,’FocusGainedCallback’,{@setfocus,h});
   hasfocus=h;

(2) catch the mouse focus events (place this function, setfocus.m, in your path)

 %% fix the annoying focus follows mouse bug 
 %% (c) GPL Matt Clark 2013 
 function setfocus(jAxis, jEventData, hFig) 
 global hasfocus; 
   set(0,’currentfigure’,hasfocus) 
 end

(3) If you are bothered by warning messages spamming up your console put this line in your matlab start up files:

 warning off MATLAB:dispatcher:nameConflict

Works for me on matlab 2007something

It works by storing the last figure set by figure(); and then every time a figure is focussed it resets the 'currentfigure' to the stored value;