function sldvdemo_extender_cb(blk,callback)
%

%   Copyright 2009-2010 The MathWorks, Inc.

    switch callback
        case 'init'
            init_cb(blk)
        case 'redraw'
            exttype_cb(blk)
        otherwise
            disp('Extender: callback not implemented')
    end
   

function exttype_cb(blk)
switch get_param(blk,'ext_type')
    case 'Finite'
        set_param([blk '/Infinite'],'Value','false')
        set_param(blk,'MaskVisibilities',{'on';'on';'on'});
        set_param(blk,'MaskEnables',{'on';'on';'on'});
        hold = str2num(get_param(blk, 'out_hold'));   %#ok<*ST2NM>
        draw_icon(blk, hold, false);
    case 'Infinite'
        set_param([blk '/Infinite'],'Value','true')
        set_param(blk,'MaskVisibilities',{'on';'on';'off'});
        set_param(blk,'MaskEnables',{'on';'on';'off'});
        draw_icon(blk, 1, true);
end

    
function init_cb(blk)
    switch get_param(blk, 'reset')
        case 'on'
            if strcmp(get_param([blk '/Reset'], 'BlockType'), 'Constant')
                replace([blk '/Reset'], 'built-in/Inport');
            end
        case 'off'
             % Check for inport
            if strcmp(get_param([blk '/Reset'],'BlockType'),'Inport')
                replace([blk '/Reset'],'built-in/Constant')
                set_param([blk '/Reset'],'Value','false')
            end
    end
    exttype_cb(blk)
     
% Local replace function
function replace(oldblock,newblock)
pos = get_param(oldblock,'Position');
orient = get_param(oldblock,'Orientation');
delete_block(oldblock);
add_block(newblock,oldblock,'Position',pos,...
    'Orientation',orient);

function draw_icon(blk, hold, inf)
  iconStr = 'text(0.5,0.0,''Extender'',''horizontalAlignment'',''center'',''verticalAlignment'',''bottom'',''texmode'',''on'');';

  left_justified = sprintf('''horizontalAlignment'',''left'',''verticalAlignment'',''bottom'',''texmode'',''on'');');
  
  pstable1 = 0.4; %Extension period starts here.
  if(inf)
    pstable2 = 0.75;
  else
    pstable2 = 0.65;
  end
  
  str = sprintf('<%d>',hold);
  
  sigheight = 0.15;
  y1 = 0.7;
  y1h = y1 + sigheight;
  y2 = 0.30;
  y2h = y2 + sigheight;
  
  iconStr = [iconStr, sprintf('\n plot([0.15 0.2 0.2 %d %d 0.75],[ 0.7 0.7 %d %d 0.7 0.7]);', pstable1,pstable1,y1h,y1h)];
  
  if(~inf)
      iconStr = [iconStr, sprintf('\n color(''orange'');')];
      iconStr = [iconStr, sprintf('\n text(%d, %d,''%s'',', pstable1, y2h, str), left_justified];
  end
  
  iconStr = [iconStr, sprintf('\n color(''blue'');')];
  iconStr = [iconStr, sprintf('\n plot([0.15 0.2 0.2 %d], [%d, %d %d %d]);', pstable2,y2,y2,y2h,y2h)];
  if(~inf)
      iconStr = [iconStr, sprintf('\n plot([%d %d 0.75],[%d %d %d]);',pstable2,pstable2,y2h,y2,y2)];
  end
  
  
  set_param(blk, 'MaskDisplay', sprintf(iconStr));        
