## Copyright (C) 2015 Pantxo Diribarne
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{retval} =} add_annotation_menu (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Pantxo Diribarne <pantxo@dibona>
## Created: 2015-02-17

function add_annotation_menu ()
  hmain = uimenu ("label", "Annotations");
  annotations = {"line", "arrow", "doublearrow", ...
                 "textarrow", "textbox", "rectangle", ...
                 "ellipse"};
  for ii = 1:numel (annotations)
    annot = annotations{ii};
    fcn = @() uiannot (annot);
    uimenu (hmain, "label", annot, "callback", fcn);
  endfor
  uimenu (hmain, "label", "Clear all", "separator", "on", ...
          "callback", @delete_annotations);
endfunction

function delete_annotations ()
  hax = findall (gcbf (), "-depth", 1, "tag", "scribeoverlay");
  h = get (hax, "children");
  delete (h);
endfunction
      
function uiannot (typ)
  f = gcf;
  printf ("Press left mouse button and drag to draw the %s\n", typ)
  set (f, 'WindowButtonDownFcn', {@onButtonDown, typ});
end

function onButtonDown (f, evt, typ)
  ha = annotation (typ);
  ax = get (ha, "parent");
  set (ax, "userdata", []);
  oldax = get (f, "currentaxes");
  set (f, "currentaxes", ax);
  set (ax, "handlevisibility", "on");

  
  if (strcmp (get (f, 'SelectionType'), 'normal'))
    set (f, 'WindowButtonMotionFcn', {@onButtonMotion, ax, ha});
    set (f, 'WindowButtonUpFcn', {@onButtonUp, oldax, ha, typ});
  endif

end

function onButtonMotion (f, evt, ax, ha)
  if (isempty (get (ax, "userdata")))
    p = get (ax, 'CurrentPoint');
    p = p(1,1:2);
    set (ax, "userdata", p)
  else
    p = get (ax, "userdata");
  endif
  ## persistent p = [];
  ## if (isempty (p))
  ##   p = get (ax, 'CurrentPoint')(1,1:2);
  ## endif
  p2 = get (ax, 'CurrentPoint');
  p2 = p2(1,1:2);
  dp = p2 - p;
  pos = [p(1), p(2), dp(1), dp(2)];
  set (ha, "position", pos);
  drawnow;

end

function onButtonUp (f, evt, oldax, ha, typ)
  set (f, 'WindowButtonMotionFcn', []);
  set (f, 'WindowButtonDownFcn', []);
  set (f, 'WindowButtonUpFcn', []);
  if (! isempty (oldax))
    set (f, "currentaxes", oldax);
  endif
  set (get (ha, "parent"), "handlevisibility", "off");
  if (any (strcmp (typ, {"textarrow", "textbox"})))
    str = inputdlg ("Text :"){1};
    set (ha, "string", str);
  endif
  
end
