function btnpress(figHandle, groupId, buttonId, a4,a5,a6,a7,a8,a9,a10)
%BTNPRESS Button press manager for toolbar button groups.
%	All objects that make up a toolbar button group have a
%	ButtonDownFcn that calls BTNPRESS.  BTNPRESS is
%	responsible for doing the right thing based on the
%	'PressType' property of the button and the 'Exclusive'
%	property of the button group.  BTNPRESS calls BTNDOWN and
%	BTNUP to do the actual work of changing button
%	appearances.
%
%	BTNPRESS may also be used from the command line to
%	simulate a button press with the following calls:
%	BTNPRESS(FIGHANDLE, GROUPID, BUTTONID)
%	BTNPRESS(FIGHANDLE, GROUPID, BUTTONNUM)

%	Steven L. Eddins, 29 June 1994
%	Copyright (c) 1984-94 by The MathWorks, Inc.
%	$Revision: 1.9 $  $Date: 1995/04/17 18:27:30 $

% Get all the information we need about which button was pressed
% and the properties of the button/button group.

if (nargin == 0)
  % BTNPRESS
  % Get button press information from the current object.
  currentObject = gco;
  currentObjectType = get(currentObject, 'Type');
  if (~all(currentObjectType(1:2) == 'ax'))
    buttonNumber = get(currentObject, 'UserData');
    buttonId = get(currentObject, 'Tag');
    axesHandle = get(currentObject, 'Parent');
  else
    % User clicked on the axes.  We've got to do extra work to
    % figure out which button has been pressed.
    currentPoint = get(currentObject, 'CurrentPoint');
    x = currentPoint(1,1);
    y = currentPoint(1,2);
    bevelPatches = findobj(currentObject, 'UserData', 'ULBorder');
    buttonNumber = [];
    for bevelPatch = bevelPatches.'
      xi = get(bevelPatch, 'XData');
      yi = get(bevelPatch, 'YData');
      xmin = min(xi);
      xmax = max(xi);
      ymin = min(yi);
      ymax = max(yi);
      if ((x >= xmin) & (x <= xmax) & (y >= ymin) & (y <= ymax))
	buttonNumber = get(bevelPatch, 'UserData');
	buttonId = get(bevelPatch, 'Tag');
	axesHandle = currentObject;
	break;
      end
    end
    % If we get here and buttonNumber is not defined, then
    % somehow the user clicked on the axes outside all of the
    % buttons.  Don't do anything.
    if (~length(buttonNumber))
      return;
    end
  end    
  
elseif (nargin == 3)
  % BTNPRESS(FIGHANDLE, GROUPID, BUTTONID) or
  % BTNPRESS(FIGHANDLE, GROUPID, BUTTONNUM)
  
  buttonNumber = buttonId;
  axesHandle = findobj(figHandle, 'Type', 'axes', 'Tag', groupId);
  
else
  error('Invalid number of input arguments.');
end

buttons = get(get(axesHandle, 'XLabel'), 'UserData');
if (isstr(buttonNumber))
  % The UserData of border and background patches is a string, not the
  % buttonNumber.  If buttonNumber is a string, then we need to
  % do some work to find the real buttonNumber.
  buttons = get(get(axesHandle, 'XLabel'), 'UserData');
  for k = 1:size(buttons,1)
    temp = buttons(k,:);
    temp(temp==' ') = []; % deblank
    if (strcmp(temp, buttonId))
      buttonNumber = k;
      break;
    end
  end
else
  % Get the buttonID string.
  buttonId = buttons(buttonNumber,:);
  buttonId(buttonId==' ') = [];  % deblank
end
currentFig = get(axesHandle, 'Parent');
groupId = get(axesHandle, 'Tag');
numButtons = prod(size(get(axesHandle, 'UserData')));
exFlag = get(get(axesHandle, 'YLabel'), 'Tag');
callbacks = get(get(axesHandle, 'YLabel'), 'UserData');
types = get(get(axesHandle, 'Title'), 'UserData');
if (isempty(types))
  pressType = 'toggle';
elseif (size(types,1) == 1)
  pressType = types;
else
  pressType = types(buttonNumber,:);
  pressType(pressType==' ') = []; % deblank short-cut
end

% OK, we've got all the state info/properties, now do the right
% thing based on 'Exclusive' and 'PressType' properties.
if (strcmp(exFlag, 'no'))
  if (strcmp(pressType, 'toggle'))
    % Case 1:  nonexclusive group; toggle button
    currentState = btnstate(currentFig, groupId, buttonNumber);
    if (currentState)
      % Button is currently down.
      btnup(currentFig, groupId, buttonNumber);
    else
      % Button is currently up.
      btndown(currentFig, groupId, buttonNumber);
    end
    if (size(callbacks,1) == 1)
      eval(callbacks);
    else
      eval(callbacks(buttonNumber, :));
    end

  else
    % Case 2:  nonexclusive group; flash button

    btndown(currentFig, groupId, buttonNumber);
    drawnow;
    btnup(currentFig, groupId, buttonNumber);
    drawnow;
    if (size(callbacks,1) == 1)
      eval(callbacks);
    else
      eval(callbacks(buttonNumber, :));
    end

  end

else
  % Case 3:  exclusive group
  stateVector = btnstate(currentFig, groupId);
  % Do we need to do anything?
  needDraw = 0;
  needCallback = 0;
  if (~stateVector(buttonNumber))
    needDraw = 1;
    needCallback = 1;
  else
    if (~strcmp(get(gcf,'SelectionType'), 'normal'))
      needCallback = 1;
    end
  end
  
  if (needDraw)
    btndown(currentFig, groupId, buttonNumber);
    % Test buttons other than the current button.  If they're down,
    % raise them.
    for k = [(1:buttonNumber-1) (buttonNumber+1):numButtons]
      if (stateVector(k))
        btnup(currentFig, groupId, k);
      end
    end
  end
  drawnow
  
  if (needCallback)
    if (size(callbacks,1) == 1)
      eval(callbacks);
    else
      eval(callbacks(buttonNumber, :));
    end
  end
end
    
