function sbinit
%SBINIT Initialize signal browser.
 
%   Copyright 1988-2004 The MathWorks, Inc.
% $Revision: 1.15.4.2 $

    save_shh = get(0,'showhiddenhandles');
    set(0,'showhiddenhandles','on')

    figname = prepender('Signal Browser');

    % ====================================================================
    % set defaults and initialize userdata structure

    rulerPrefs = sptool('getprefs','ruler');
    colorPrefs = sptool('getprefs','color');
    sigbrowsePrefs = sptool('getprefs','sigbrowse');

    ud.prefs.tool.ruler = sigbrowsePrefs.rulerEnable;    % rulers enabled
    ud.prefs.tool.panner = sigbrowsePrefs.pannerEnable;  % panner enabled
    ud.prefs.tool.zoompersist = sigbrowsePrefs.zoomFlag; % is zoom mode persistant or
       % does it go away when you zoom once?

    ud.prefs.colororder = colorPrefs.colorOrder;
    ud.prefs.linestyleorder = colorPrefs.linestyleOrder;

    ud.prefs.minsize = [150 150 60]; 
      % minsize(1)   - minimum width of main axes in pixels
      % minsize(2)   - minimum height of main axes in pixels
      % minsize(3)   - minimum height of workspace listbox in pixels

    ud.prefs.xaxis.label = sigbrowsePrefs.xlabel;
    ud.prefs.xaxis.grid = 1;
    ud.prefs.yaxis.label = sigbrowsePrefs.ylabel;
    ud.prefs.yaxis.grid = 1;
    
    ud.prefs.title.mode = 'auto';  % can be 'auto' or 'manual'
    ud.prefs.title.manualstring = '';  % title string in case mode is manual
   
    markerStr = { '+' 'o' '*' '.' 'x' ...
         'square' 'diamond' 'v' '^' '>' '<' 'pentagram' 'hexagram' }';
    typeStr = {'vertical' 'horizontal' 'track' 'slope'}';
    
    ud.prefs.ruler.color = rulerPrefs.rulerColor;
    ud.prefs.ruler.marker = markerStr{rulerPrefs.rulerMarker};
    ud.prefs.ruler.markersize = rulerPrefs.markerSize;
    ud.prefs.ruler.type = typeStr{rulerPrefs.initialType};

    ud.prefs.panner.erasemode = 'background';
    ud.prefs.panner.dynamicdrag = 1;

    ud.prefs.hidedialogs = 1;  % hide or destroy flag; 1 == hide
                               %  0 = destroy.

    ud.prefs.rulerPrefsPanelName = 'sigbrowse'; %ruler needs to know this so
       % it can update the SPTOOL preferences when it is turned on/off
    ud.sz = sptsizes;

    ud.sigs = [];
    
    ud.linecache.h = [];  % no line objects defined yet
    if ud.prefs.tool.panner
        ud.linecache.ph = [];  
    end

    ud.lines = [];
    ud.SPToolIndices = [];
    ud.focusIndex = [];
    ud.colorCount = 0;  % number of colors allocated thus far
    ud.colororder = num2cell(evalin('base',ud.prefs.colororder),2);
    ud.linestyleorder = num2cell(evalin('base',ud.prefs.linestyleorder),2);

    ud.pointer = 0;  % == -1 watch, 0 arrow/drag indicators, 1 zoom,
                     %     2 help
    ud.t0 = 0;
    
    ud.tabfig = [];  % handle to settings dialog box figure

    ud.limits.xlim = [0 1];
    ud.limits.ylim = [0 1];

    ud.left_width = 0;  % used by ruler.m for positioning

    ud.justzoom = [ 0 0 ] ;

    sz = ud.sz;

    screensize = get(0,'screensize');
    fp = get(0,'defaultfigureposition');
    fw = fp(3)+sz.rw; % figure width
    fw = min(fw,screensize(3)-30);
    fh = ruler('minWidth',sz);
    fp = [fp(1)-(fw-fp(3))/2 fp(2)+fp(4)-fh fw fh];
    
    % CREATE FIGURE
    fig = figure('createfcn','',...
            'closerequestfcn','sbswitch(''sigbrowse'',''SPTclose'')',...
            'dockcontrols','off',...
            'tag','sigbrowse',...
            'numbertitle','off',...
            'integerhandle','off',...
            'handlevisibility','callback',...
            'userdata',ud,...
            'units','pixels',...
            'position',fp,...
            'menubar','none',...
            'name',figname,...
            'inverthardcopy','off',...
            'paperpositionmode','auto',...
            'visible','off',...
            'resizefcn','sbswitch(''resizedispatch'')%');
    figure(fig)
    uibgcolor = get(0,'defaultuicontrolbackgroundcolor');
    uifgcolor = get(0,'defaultuicontrolforegroundcolor');

    % ====================================================================
    % MENUs
    %  create cell array with {menu label, callback, tag}

 %  MENU LABEL                          CALLBACK                    TAG
mc={ 'File'                              ' '                     'filemenu'
     '>Pa&ge Setup...'                   'printcomp(''pgepos'')' 'pagepos'
     '>Print Pre&view...'                'printcomp(''prev'')'   'printprev'
     '>&Print...'                        'printcomp(''prnt'')'   'prnt'
     '>---'                              ' '                     ' '
     '>&Close^w'                         'close'                 'closemenu'
     '&Window'                            winmenu('callback')    'winmenu'};

	% Build the cell array string for the Help menu.
    mh=sigbrowser_helpmenu;
	mc = [mc; mh];
 
    mc = ruler('makemenu',mc);  % add Marker menu

    menu_handles = makemenu(fig, char(mc(:,1)), ...
                            char(mc(:,2)), char(mc(:,3)));
    winmenu(fig)
        
    set(menu_handles,'handlevisibility','callback')

    % ====================================================================
    % Create context menu for lines in main axes:  TPK 5/31/99
    ud.contextMenu.u = uicontextmenu('parent',fig);
%    ud.contextMenu.title = uimenu(ud.contextMenu.u,...
%              'callback','sbswitch(''sigbrowse'',''setLabel'')');
%    ud.contextMenu.Fs = uimenu(ud.contextMenu.u,...
%              'label','Sampling Frequency...',...
%              'callback','sbswitch(''sigbrowse'',''setFs'')');
%    ud.contextMenu.lineprop = uimenu(ud.contextMenu.u,...
%              'label','Line Properties...',...
%              'separator','on',...
%              'callback','');
%    ud.contextMenu.arraysigs = uimenu(ud.contextMenu.u,...
%              'label','Visible Columns...','visible','off',...
%              'callback','');
 % list of context menus for all the visible signals:
    ud.contextMenu.pickMenu = uimenu(ud.contextMenu.u);  
    ud.contextMenu.changeName = uimenu(ud.contextMenu.pickMenu);
    ud.contextMenu.Fs = uimenu(ud.contextMenu.pickMenu);
    ud.contextMenu.lineprop = uimenu(ud.contextMenu.pickMenu);
    ud.contextMenu.array = uimenu(ud.contextMenu.pickMenu);
 
    ud.contextMenu.real = uimenu(ud.contextMenu.u,...
              'label','Real Part','visible','off','separator','on',...
              'callback','sbswitch(''sbcmplx'',1)');
    ud.contextMenu.imag = uimenu(ud.contextMenu.u,...
              'label','Imaginary Part','visible','off',...
              'callback','sbswitch(''sbcmplx'',2)');
    ud.contextMenu.mag = uimenu(ud.contextMenu.u,...
              'label','Magnitude','visible','off',...
              'callback','sbswitch(''sbcmplx'',3)');
    ud.contextMenu.angle = uimenu(ud.contextMenu.u,...
              'label','Angle','visible','off',...
              'callback','sbswitch(''sbcmplx'',4)');
    

    % ====================================================================
    % Create Main axes
    mainaxes = axes('units','pixels',...
         'box','on',...
         'handlevisibility','callback', ...
         'tag','mainaxes',...
         'buttondownfcn','sigbrowse(''mainaxes_down'')',...
         'uicontextmenu',ud.contextMenu.u);
    % create a copy that will be underneath the main axes, and
    % will be used as a border during panning operations to prevent
    % background erasemode from clobbering the main axes plot box.
    temp = copyobj(mainaxes,fig);
    mainaxes_border = mainaxes;
    mainaxes = temp;

    set(mainaxes_border,'xtick',[],'ytick',[],'visible','off',...
          'tag','mainaxes_border')

    set(get(mainaxes,'title'),'FontAngle',  get(mainaxes, 'FontAngle'), ...
        'FontName',   get(mainaxes, 'FontName'), ...
        'FontSize',   get(mainaxes, 'FontSize'), ...
        'FontWeight', get(mainaxes, 'FontWeight'), ...
        'color',get(mainaxes,'xcolor'),...
        'tag','mainaxestitle',...
        'interpreter','none')
    set(get(mainaxes,'xlabel'),'string',ud.prefs.xaxis.label,...
         'tag','mainaxesxlabel')
    set(get(mainaxes,'ylabel'),'string',ud.prefs.yaxis.label,...
         'tag','mainaxesylabel')

    ud.mainaxes = mainaxes;
    ud.mainaxes_border = mainaxes_border;

    set(fig,'userdata',ud)
    
    % ====================================================================
    % now add toolbar for signal browser
        
    % NEW toolbar, using uitoolbar functionality, available as of 5.3:
    ud = get(fig,'userdata');
    ut=uitoolbar(fig);
    load siggui_icons
    ud.toolbar.print = ...
        uipushtool('cdata',bmp.print,...
                     'parent',ut,...
                     'clickedcallback','printcomp(''prnt'')' ,...
                     'tooltipstring',xlate('Print...'));
    ud.toolbar.printpreview = ...
        uipushtool('cdata',bmp.printpreview,...
                     'parent',ut,...
                     'clickedcallback','printcomp(''prev'')' ,...
                     'tooltipstring',xlate('Print Preview...'));
    ud.toolbar.playsound = ...
        uipushtool('cdata',bmp.playsound,...
                     'parent',ut,...
                     'separator','on',...
                     'clickedcallback','sigbrowse(''play'')' ,...
                     'tooltipstring',xlate('Play Selected Signal'));
    ud.toolbar.arraysigs = ...
        uipushtool('cdata',bmp.arraysigs,...
                     'parent',ut,...
                     'enable','off',...
                     'separator','on',...
                     'clickedcallback','sbswitch(''sigbrowse'',''array'')',...
                     'tooltipstring',xlate('Array Signals...'));
    %NOTE: ud.toolbar.complex's userdata contains the
    % state of the complex display as an integer: 
    %    1 = real, 2 = imag, 3 = mag, 4 = angle
    ud.toolbar.complex = ...
        uipushtool('cdata',bmp.complex,...
                     'parent',ut,...
                     'userdata',1,...
                     'enable','off',...
                     'clickedcallback','sbswitch(''sbcmplx'')',...
                     'tooltipstring',...
                        sprintf('Complex signals...\nCurrent display: real'));
    ud.toolbar.mousezoom = ...
        uitoggletool('cdata',bmp.mousezoom,...
                     'parent',ut,...
                     'separator','on',...
                     'clickedcallback','sbswitch(''sbzoom'',''mousezoom'')',...
                     'tooltipstring',xlate('Mouse Zoom'));
    ud.toolbar.fullview = ...
        uipushtool('cdata',bmp.fullview,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sbzoom'',''zoomout'')',...
                     'tooltipstring',xlate('Full View (zooms out both axes)')); 
    ud.toolbar.zoominy = ...
        uipushtool('cdata',bmp.zoominy,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sbzoom'',''zoominy'')',...
                     'tooltipstring',xlate('Zoom In - Y (vertical axis)'));
    ud.toolbar.zoomouty = ...
        uipushtool('cdata',bmp.zoomouty,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sbzoom'',''zoomouty'')',...
                     'tooltipstring',xlate('Zoom Out - Y (vertical axis)'));
    ud.toolbar.zoominx = ...
        uipushtool('cdata',bmp.zoominx,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sbzoom'',''zoominx'')',...
                     'tooltipstring',xlate('Zoom In - X (horizontal axis)'));
    ud.toolbar.zoomoutx = ...
        uipushtool('cdata',bmp.zoomoutx,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sbzoom'',''zoomoutx'')',...
                     'tooltipstring',xlate('Zoom Out - X (horizontal axis)'));
    ud.toolbar.select = ...
        uipushtool('cdata',bmp.select,...
                     'parent',ut,...
                     'separator','on',...
                     'clickedcallback','sbswitch(''sptlegend'',''popup'')' ,...
                     'tooltipstring',xlate('Select signal...'));
    ud.toolbar.lineprop = ...
        uipushtool('cdata',bmp.lineprop,...
                     'parent',ut,...
                     'clickedcallback','sbswitch(''sptlegend'',''button'')' ,...
                     'tooltipstring',xlate('Line Properties...'));
                  
    ud.toolbar = ruler('toolbarbuttons',ud.toolbar,ut,bmp);
                                    
    ud.toolbar.whatsthis = ...
        uitoggletool('cdata',bmp.whatsthis,...
                     'parent',ut,...
                     'separator','on',...
                     'clickedcallback','sbswitch(''sigbrowse'',''help'')' ,...
                     'tooltipstring',xlate('"What''s This?" Help'));

    ud.focusColumn = 0;
    set(fig,'userdata',ud)
%Old ud.toolbar fields:     toolbar
%                         zoomgroup
%                         helpgroup
%                          minWidth
%                        left_width
%                       right_width

    % create legend - changes userdata
    sptlegend(fig,'sigbrowse(''changefocus'')','sigbrowse')

    % ====================================================================
    % position objects:
    sbresize(1,fig)

    set(fig,'resizefcn',appstr(get(fig,'resizefcn'),'sbswitch(''sbresize'')'))
    set(fig,'windowbuttonmotionfcn','sbswitch(''sbmotion'')')

    if ud.prefs.tool.ruler
       ruler
    end

    if ud.prefs.tool.panner
       panner
    end

    set(0,'showhiddenhandles',save_shh)
    % set(fig,'visible','on')  - do this later (in calling function)

%------------------------------------------------------------------------------
function mh = sigbrowser_helpmenu
% Set up a string cell array that can be passed to makemenu to create the Help
% menu for the Signal Browser.

% Define specifics for the Help menu in Signal Browser.
toolname      = 'Signal Browser';
toolhelp_cb   = 'sigbrowse(''help'',''topics'')';
toolhelp_tag  = 'helptopicsmenu';
whatsthis_cb  = 'sigbrowse(''help'',''whatsthis'')';
whatsthis_tag = 'whatsthismenu';

% Add other Help menu choices that are common to all SPTool clients.
mh=sptool_helpmenu(toolname,toolhelp_cb,toolhelp_tag,whatsthis_cb,whatsthis_tag);

% [EOF] sbinit.m