########################################################################
##
## Copyright (C) 2020 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave 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.
##
## Octave 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 Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} xtickangle (@var{angle})
## @deftypefnx {} {@var{angle} =} xtickangle ()
## @deftypefnx {} {} xtickangle (@qcode{"manual"})
## @deftypefnx {} {} xtickangle (@qcode{"auto"})
## @deftypefnx {} {@var{mode} =} xtickangle (@qcode{"mode"})
## @deftypefnx {} {[@dots{}] =} xtickangle (@var{hax}, @dots{})
## Query or set the rotation angle of the tick labels on the x-axis of the current axis.
##
## When called without an argument, return the rotation angle in degrees
## of the tick labels as specified in the @qcode{"XTickLabelRotation"} axes property.
## This angle can be changed by calling @code{xtickangle} with a scalar value
## in degrees.
##
## When called with argument @qcode{"mode"}, @code{xtickangle} returns the
## current value of the axes property @qcode{"XTickLabelRotationMode"}.  This
## property can be changed by calling @code{xtickangle} with either @qcode{"auto"}
## (algorithm determines tick labels angle) or @qcode{"manual"} (tick labels angle
## remain fixed).  Note: Specifying @code{xtickangle} values will also set the
## @qcode{"XTickLabelRotationMode"} property to @qcode{"manual"}.
##
## If the first argument @var{hax} is an axes handle, then operate on
## this axis rather than the current axes returned by @code{gca}.
##
## Requesting a return value when calling @code{xtickangle} to set a property
## value will result in an error.
##
## @seealso{ytickangle, ztickangle, get, set}
## @end deftypefn

function retval = xtickangle (varargin)

  if (nargin > 2 || nargout > 1)
    print_usage ();
  endif
  
  hax = val = [];
  get_rotate_angle = false;
  if (nargin == 2)
    hax = varargin{1};
    val = varargin{2};
    if (! isaxes (hax))
      error ("xtickangle: HAX must be a handle to an axes object");
    endif
  elseif (nargin == 1)
    if (isaxes (varargin{1}))
      hax = varargin{1};
      get_rotate_angle = true;
    else
      val = varargin{1};
      hax = gca ();
    end
  else
    hax = gca ();
    get_rotate_angle = true;
  endif
  
  if (get_rotate_angle)
    retval = get(hax, "XTickLabelRotation");
  elseif (isscalar (val) && isnumeric (val) && isfinite (val))
    if (nargout > 0)
      error ("xtickangle: function called with too many outputs");
    endif
    set (hax, "XTickLabelRotation", val);
    set (hax, "XTickLabelRotationMode", "manual");
  elseif (ischar (val))
    if (any (strcmpi (val, {"auto", "manual"})))
      if (nargout > 0)
        error ("xtickangle: function called with too many outputs");
      endif
      set (hax, "XTickLabelRotationMode", val)
    elseif (strcmpi (val, "mode"))
      retval = get(hax, "XTickLabelRotationMode");
    else
      error ("xtickangle: ANGLE must be a scalar value in degrees");
    endif
  else
    error ("xtickangle: ANGLE must be a scalar value in degrees");
  endif

endfunction

%!test
%! hf = figure ("visible", "off");
%! hax = axes (hf);
%! unwind_protect
%!   assert (xtickangle ("mode"), "auto");
%!   xtickangle (45);
%!   assert (xtickangle (), 45);
%!   xtickangle (hax, 90);
%!   a1 = xtickangle ();
%!   a2 = xtickangle (hax);
%!   assert (a1, a2);
%!   assert (a1, 90);
%!   assert (xtickangle ("mode"), "manual");
%!   xtickangle ("auto")
%!   assert (xtickangle ("mode"), "auto");
%!   xtickangle ("manual")
%!   assert (xtickangle ("mode"), "manual");
%!   xtickangle (hax, "auto")
%!   assert (xtickangle (hax, "mode"), "auto");
%!   xtickangle (hax, "manual")
%!   assert (xtickangle (hax, "mode"), "manual");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

## Test input validation
%!error <Invalid call> xtickangle (1, 2, 3)
%!error <function called with too many outputs> [a, b] = xtickangle ()
%!error <function called with too many outputs> [a, b] = xtickangle (1, 2, 3)
%!error <function called with too many outputs> a = xtickangle (45)
%!error <ANGLE must be a scalar value in degrees> xtickangle (Inf)
%!error <ANGLE must be a scalar value in degrees> xtickangle (NaN)
%!error <ANGLE must be a scalar value in degrees> xtickangle (eye (2))
%!error <ANGLE must be a scalar value in degrees> xtickangle ({90})
%!error <ANGLE must be a scalar value in degrees> xtickangle (struct ("angle", 5))
%!error <ANGLE must be a scalar value in degrees> xtickangle ("octave")
%!error <function called with too many outputs> m = xtickangle ("auto")
%!error <function called with too many outputs> m = xtickangle ("manual")
%!error <HAX must be a handle to an axes object> xtickangle (0, 45)
%!error <HAX must be a handle to an axes object> xtickangle (0, "auto")
%!error <HAX must be a handle to an axes object> xtickangle (0, "manual")
%!error <HAX must be a handle to an axes object> xtickangle (0, "mode")
%!error <HAX must be a handle to an axes object> xtickangle (0, "invalid")
