% BUILDMAINFIG build the main launchBay window
%
% M Hewitson 30-11-08
%
% $Id: buildMainfig.m,v 1.5 2009/02/02 18:15:24 hewitson Exp $
%
function mainfig = buildMainfig(mainfig, varargin)
  %   %% Check if I exist already
  %   id = findobj('Tag', 'SIGBUILDERmainfig');
  %   if ~isempty(id)
  %     figure(id)
  %     return
  %   end
  
  %% Some initial setup
  
  Screen = get(0,'screensize');
  mainfig.Gproperties.Gcol     = [255 255 255]/255;
  mainfig.Gproperties.Screen   = Screen;
  mainfig.Gproperties.Gwidth   = 0.3;
  mainfig.Gproperties.Gheight  = 0.5;
  mainfig.Gproperties.Gborder  = 10;
  mainfig.Gproperties.fontsize = 12;
  
  l = (0.5-mainfig.Gproperties.Gwidth/2);
  b = (0.5-mainfig.Gproperties.Gheight/2);
  w = mainfig.Gproperties.Gwidth;
  h = mainfig.Gproperties.Gheight;
  mainfig.Gproperties.Gposition = [l b w h];
  
  if ~isempty(varargin) && ishandle(varargin{1})
    mainfig.handle = varargin{1};
    set(mainfig.handle, 'Tag', 'LAUNCHBAYmainfig');
    set(mainfig.handle, 'Units', 'normalized');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'LTPDA Launch Bay',...
      'NumberTitle', 'off',...
      'Visible','off',...
      'Units','normalized',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', mainfig.Gproperties.Gcol,...
      'Resize', 'off',...
      'Tag', 'LAUNCHBAYmainfig');
  end
  
  % Set mainfig callbacks
  set(mainfig.handle, 'CloseRequestFcn', {'launchBay.cb_mainfigClose', mainfig});
  
  % Add  Logo
  M = 0.0;
  ax   = axes('Position', [M M 1-2*M 1-2*M], 'Parent', mainfig.handle);
  logo = imread('LPFlogo.gif');
  image(logo, 'Parent', ax)
  set(ax,'Visible', 'on', 'Box', 'off', 'Xtick', [], 'Ytick', []);
  axis(ax, 'off');
  
  % create a grid of launch buttons
  launchers = {'modelViewer', 'modelViewer.png'; ...
               'sigBuilder', 'sigBuilder.png'; ...
               'repogui', 'repogui.png';
               'constructor', 'constructor.png'; 
               'specwinViewer', 'specwinViewer.png';
               'LTPDAprefs', 'ltpdaprefs.png';
               'pzmodel_helper', 'pzmodeldesigner.png';
               'ltpdv', 'ltpdv.png';
               'ltpdaquicklook', 'quicklook.png'};
  
  
  % Place buttons
  vmarg = 0.02;
  h = 0.2;
  w = 0.2;
  l = vmarg;
  b = 1-h-vmarg;
  for kk = 1:size(launchers,1)
    
    pbh = uicontrol(mainfig.handle,'Style','pushbutton',...
      'String','',...
      'Callback', launchers{kk,1}, ...
      'Units', 'normalized', ...
      'Fontsize', mainfig.Gproperties.fontsize, ...
      'Position',[l b w h]);
    
    img = imread(launchers{kk,2});
    set(pbh, 'cdata', img);
    
    l = l + w + vmarg;
    if l+w+vmarg > 1
      l = vmarg;
      b = b - vmarg - h;
    end
        
  end
  
  
  
end
