function print(ViewerObj,Device)
%PRINT  Print LTI Viewer

%   Author(s): A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.4 $  $Date: 2000/06/14 20:03:14 $

switch Device
case 'printer'
   localPrintToPaper(ViewerObj);
case 'figure'
   localPrintToFigure(ViewerObj);
end


%%%%%%%%%%%%%%%%%%%%%
% localPrintToPaper %
%%%%%%%%%%%%%%%%%%%%%
function localPrintToPaper(ViewerObj)
%---Print to paper
   ch = get(ViewerObj.Handle,'Children');

   %---New figure (we'll show the user this figure while the real one gets manipulated for output)
    newfig = figure(...
       'Visible','off',...
       'Name',get(ViewerObj.Handle,'Name'),...
       'Menu','none',...
       'HandleVisibility','callback',...
       'IntegerHandle','off',...
       'NumberTitle','off',...
       'Pointer','watch',...
       'Units',get(ViewerObj.Handle,'Units'));

   %---Copy UIControls
    Menus = findall(ch,'flat','Type','uimenu');
    for n=1:length(Menus)
       uimenu(newfig,...
          'Label',   get(Menus(n),'Label'),...
          'Position',get(Menus(n),'Position'),...
          'Visible', get(Menus(n),'Visible'));
    end
    sf=copyobj(ViewerObj.StatusFrame,newfig);
    st=copyobj(ViewerObj.StatusText,newfig);
    uistack(sf,'bottom')

   %---Match figure position
    set(newfig,'Position',get(ViewerObj.Handle,'Position'));

   %---Copy axes to new figure
    LTIdisplayAxes = findobj(ch,'flat','Tag','LTIdisplayAxes');
    B = copyobj(ViewerObj.BackgroundAxes,newfig);
    h = copyobj(LTIdisplayAxes,newfig);
    kids = get([h(:);B(:)],{'children'});
    kids = cat(1,kids{:});

   %---Clear all callbacks/uicontextmenus/tags/userdata associated with new copies
    set([B(:);h(:);kids(:)],'DeleteFcn','','ButtonDownFcn','','UIContextMenu',[],'UserData',[],'Tag','');

   %---Enable visibility
    set(newfig,'Visible','on');

   %---Hide status bar and redraw figure
    set([ViewerObj.Handle ViewerObj.StatusFrame ViewerObj.StatusText],'Visible','off');
    resize(ViewerObj);

   %---Disable warnings during print
    WarnMode = warning('off');
    set(0,'CurrentFigure',ViewerObj.Handle);
    drawnow;

    printdlg(ViewerObj.Handle);
    warning(WarnMode);

   %---Show status bar and redraw figure
    set([ViewerObj.Handle ViewerObj.StatusFrame ViewerObj.StatusText],'Visible','on','Units','pixels');
    delete(newfig);
    resize(ViewerObj);


%%%%%%%%%%%%%%%%%%%%%%
% localPrintToFigure %
%%%%%%%%%%%%%%%%%%%%%%
function localPrintToFigure(ViewerObj)
%---Print to figure

 %---Hide the status bar and redraw (via resize)
  set([ViewerObj.StatusFrame ViewerObj.StatusText],'Visible','off');
  resize(ViewerObj);

 %---New figure
  newfig = figure('Visible','off','Name','LTI Viewer Responses','Units',get(ViewerObj.Handle,'Units'));

 %---Match figure size (but not necessarily location)
  p1 = get(ViewerObj.Handle,'Position');
  p2 = get(newfig,'Position');
  set(newfig,'Position',[p2(1) p2(2)-(p1(4)-p2(4)) p1(3) p1(4)]);

 %---Copy axes to new figure
  LTIdisplayAxes = findobj(get(ViewerObj.Handle,'Children'),'flat','Tag','LTIdisplayAxes');
  B = copyobj(ViewerObj.BackgroundAxes,newfig);
  h = copyobj(LTIdisplayAxes,newfig);
  kids = get([h(:);B(:)],{'children'});
  kids = cat(1,kids{:});

 %---Clear all callbacks/uicontextmenus/tags/userdata associated with new copies
  set([B(:);h(:);kids(:)],'DeleteFcn','','ButtonDownFcn','','UIContextMenu',[],'UserData',[],'Tag','');

 %---Make sure all axes have well-behaved units
  set([B(:);h(:)],'Units','norm');
  set(findall(B(:),'Type','text'),'Units','data');

 %---Show the status bar and redraw (via resize)
  set([ViewerObj.StatusFrame ViewerObj.StatusText],'Visible','on');
  resize(ViewerObj);

 %---Enable visibility
  set(newfig,'Visible','on');
