function clickedCallback(src, evnt)
% clickedCallback
%   toggle button clicked callback

%   Project: IMPACT
%   Title of Code: clickedCallback
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

 

handles = guidata(src);
hFig = handles.hFig;

% fcn callbacks
set(hFig, 'WindowButtonDownFcn', '');
set(hFig, 'WindowButtonUpFcn', '');
set(hFig, 'WindowButtonMotionFcn', '');

% text
set(handles.Tb.Arrow.hInfo, 'String', {'', ''});

% check which tool is selected
toolbarButton = handles.toolbarButton;
state = get(src, 'State');
if strcmp(state, 'off') % tool is released
    setptr(hFig, 'arrow');
    handles.Tb.selected = '';
    guidata(hFig, handles);
    uiColor = get(0,'defaultUicontrolBackgroundColor');
    tbColor(src, uiColor);
    return;
else
    for m=1:numel(toolbarButton)
        h = toolbarButton(m);
        if h~=src
            if strcmp(get(h, 'State'), 'on')
                set(h, 'State', 'off');
                clickedCallback(h, []);
            end
        end
    end
end
tbColor(src, [255, 175, 106]/255);
set(hFig, 'WindowButtonDownFcn', @buttonDown);

% set point shape
str = lower(get(src, 'TooltipString'));
switch str
    case 'data point'
        setptr(hFig, 'help');
        selected = 'arrow';
        set(hFig, 'WindowButtonMotionFcn', @buttonMotion);
    case 'add pole'
        setptr(hFig, 'addpole');
        selected = 'pole';
    case 'add zero'
        setptr(hFig, 'addzero');
        selected = 'zero';
    case 'add complex pole'
        setptr(hFig, 'addpole');
        selected = 'comPole';
    case 'add complex zero'
        setptr(hFig, 'addzero');
        selected = 'comZero';
    case 'remove pole/zero'
        setptr(hFig, 'eraser');
        selected = 'eraser';
    case 'zoom in'
        setptr(hFig, 'glassplus');
        selected = 'zoomIn';
        set(hFig, 'WindowButtonMotionFcn', @buttonMotion);
        set(hFig, 'WindowButtonUpFcn', @buttonUp);
    case 'zoom out'
        setptr(hFig, 'glassminus');
        selected = 'zoomOut';
        set(hFig, 'WindowButtonUpFcn', @buttonUp);
    case 'drag'
        setptr(hFig, 'hand');
        selected = 'hand';
        set(hFig, 'WindowButtonMotionFcn', @buttonMotion);
        set(hFig, 'WindowButtonUpFcn', @buttonUp);
end

% toolbar struct
handles.Tb.selected = selected;
handles.Tb.hTool = src;
guidata(hFig, handles);


function tbColor(h, color)
% change the toolbar color

cData = get(h, 'CData');

% change color of toolbar icons
for p = 1:3
   mat = cData(:, :, p); 
   [nRows, nCols] = size(mat);
   for m = 1:nRows
      for n = 1:nCols
         if mat(m, n) > 100
             mat(m, n) = color(p)*255;
         end
      end
   end
   cData(:, :, p) = mat;
end

set(h, 'CData', cData);