function sbinit
%SBINIT Initialize signal browser.
 
%   Copyright 1988-2012 The MathWorks, Inc.

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

    figname = getString(message('signal:sptoolgui:SignalBrowser'));

    % ====================================================================
    % 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 persistent 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.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'')',...
            'WindowStyle','normal',...
            '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={ getString(message('signal:sptoolgui:File'))                              ' '                     'filemenu'
     ['>' getString(message('signal:sptoolgui:PrintPreview2')) '...']                'printcomp(''prev'')'   'printprev'
     ['>' getString(message('signal:sptoolgui:Print2')) '...']                        'printcomp(''prnt'')'   'prnt'
     '>---'                              ' '                     ' '
     ['>' getString(message('signal:sptoolgui:Close2')) '^w']                         'close'                 'closemenu'};

    mc = ruler('makemenu',mc);  % add Marker menu

    menu_handles = makemenu(fig, char(mc(:,1)), ...
                            char(mc(:,2)), char(mc(:,3)));
                        
    matlab.ui.internal.createWinMenu(fig);
        
    % Help menu.
    mh=sigbrowser_helpmenu;
    menu_handles = [menu_handles; makemenu(fig, char(mh(:,1)), ...
                            char(mh(:,2)), char(mh(:,3)))];

    set(menu_handles,'HandleVisibility','callback')

    % ====================================================================
    % Create context menu for lines in main axes:  TPK 5/31/99
    ud.contextMenu.u = uicontextmenu('Parent',fig,'Tag','sbContextMenu');
%    ud.contextMenu.title = uimenu(ud.contextMenu.u,...
%              'callback','sbswitch(''sigbrowse'',''setLabel'')');
%    ud.contextMenu.Fs = uimenu(ud.contextMenu.u,...
%              'label',[getString(message('signal:sptoolgui:SamplingFrequency')) '...'],...
%              'callback','sbswitch(''sigbrowse'',''setFs'')');
%    ud.contextMenu.lineprop = uimenu(ud.contextMenu.u,...
%              'label',getString(message('signal:sptoolgui:LineProperties')),...
%              'Separator','on',...
%              'callback','');
%    ud.contextMenu.arraysigs = uimenu(ud.contextMenu.u,...
%              'label',getString(message('signal:sptoolgui:VisibleColumns')),'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',getString(message('signal:sptoolgui:RealPart')),'Visible','off','Separator','on',...
              'Callback','sbswitch(''sbcmplx'',1)','Tag','sbReal');
    ud.contextMenu.imag = uimenu(ud.contextMenu.u,...
              'Label',getString(message('signal:sptoolgui:ImaginaryPart')),'Visible','off',...
              'Callback','sbswitch(''sbcmplx'',2)','Tag','sbImag');
    ud.contextMenu.mag = uimenu(ud.contextMenu.u,...
              'Label',getString(message('signal:sptoolgui:Magnitude')),'Visible','off',...
              'Callback','sbswitch(''sbcmplx'',3)','Tag','sbMag');
    ud.contextMenu.angle = uimenu(ud.contextMenu.u,...
              'Label',getString(message('signal:sptoolgui:Angle')),'Visible','off',...
              'Callback','sbswitch(''sbcmplx'',4)','Tag','sbAngle');
    

    % ====================================================================
    % 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'')' ,...
                     'Tag', 'sbPrint', ...
                     'TooltipString',getString(message('signal:sptoolgui:Print')));
    ud.toolbar.printpreview = ...
        uipushtool('CData',bmp.printpreview,...
                     'Parent',ut,...
                     'ClickedCallback','printcomp(''prev'')' ,...
                     'Tag', 'sbPrintPreview', ...
                     'TooltipString',[getString(message('signal:sptoolgui:PrintPreview')) '...']);
    ud.toolbar.playsound = ...
        uipushtool('CData',bmp.playsound,...
                     'Parent',ut,...
                     'Separator','on',...
                     'ClickedCallback','sigbrowse(''play'')' ,...
                     'Tag', 'sbPlaySound', ...
                     'TooltipString',getString(message('signal:sptoolgui:PlaySelectedSignal')));
    ud.toolbar.arraysigs = ...
        uipushtool('CData',bmp.arraysigs,...
                     'Parent',ut,...
                     'Enable','off',...
                     'Separator','on',...
                     'ClickedCallback','sbswitch(''sigbrowse'',''array'')',...
                     'Tag', 'sbArraySigs', ...
                     'TooltipString',[getString(message('signal:sptoolgui:ArraySignals')) '...']);
    %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'')',...
                     'Tag', 'sbComplex', ...
                     'TooltipString',...
                        getString(message('signal:sptoolgui:ComplexSignalsCurrentDisplayReal')));
    ud.toolbar.mousezoom = ...
        uitoggletool('CData',bmp.mousezoom,...
                     'Parent',ut,...
                     'Separator','on',...
                     'ClickedCallback','sbswitch(''sbzoom'',''mousezoom'')',...
                     'Tag', 'sbMouseZoom', ...
                     'TooltipString',getString(message('signal:sptoolgui:MouseZoom')));
    ud.toolbar.fullview = ...
        uipushtool('CData',bmp.fullview,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sbzoom'',''zoomout'')',...
                     'Tag', 'sbFullView', ...
                     'TooltipString',getString(message('signal:sptoolgui:FullViewzoomsOutBothAxes'))); 
    ud.toolbar.zoominy = ...
        uipushtool('CData',bmp.zoominy,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sbzoom'',''zoominy'')',...
                     'Tag', 'sbZoomInY', ...
                     'TooltipString',getString(message('signal:sptoolgui:ZoomInYverticalAxis')));
    ud.toolbar.zoomouty = ...
        uipushtool('CData',bmp.zoomouty,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sbzoom'',''zoomouty'')',...
                     'Tag', 'sbZoomOutY', ...
                     'TooltipString',getString(message('signal:sptoolgui:ZoomOutYverticalAxis')));
    ud.toolbar.zoominx = ...
        uipushtool('CData',bmp.zoominx,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sbzoom'',''zoominx'')',...
                     'Tag', 'sbZoomInX', ...
                     'TooltipString',getString(message('signal:sptoolgui:ZoomInXhorizontalAxis')));
    ud.toolbar.zoomoutx = ...
        uipushtool('CData',bmp.zoomoutx,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sbzoom'',''zoomoutx'')',...
                     'Tag', 'sbZoomOutX', ...
                     'TooltipString',getString(message('signal:sptoolgui:ZoomOutXhorizontalAxis')));
    ud.toolbar.select = ...
        uipushtool('CData',bmp.select,...
                     'Parent',ut,...
                     'Separator','on',...
                     'ClickedCallback','sbswitch(''sptlegend'',''popup'')' ,...
                     'Tag', 'sbSelect', ...
                     'TooltipString',[getString(message('signal:sptoolgui:SelectSignal')) '...']);
    ud.toolbar.lineprop = ...
        uipushtool('CData',bmp.lineprop,...
                     'Parent',ut,...
                     'ClickedCallback','sbswitch(''sptlegend'',''button'')' ,...
                     'Tag', 'sbLineProp', ...
                     'TooltipString',[getString(message('signal:sptoolgui:LineProperties')) '...']);
                  
    ud.toolbar = ruler('toolbarbuttons',ud.toolbar,ut,bmp);
                                    
    ud.toolbar.whatsthis = ...
        uitoggletool('CData',bmp.whatsthis,...
                     'Parent',ut,...
                     'Separator','on',...
                     'ClickedCallback','sbswitch(''sigbrowse'',''help'')' ,...
                     'Tag', 'sbWhatsThis', ...
                     'TooltipString',getString(message('signal:sptoolgui:WhatsThisHelp')));

    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      = getString(message('signal:sptoolgui:SignalBrowser'));
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
