% BUILDMAINFIG build the main modelViewer window
%
% M Hewitson 18-10-08
%
% $Id: buildMainfig.m,v 1.2 2008/11/30 16:16: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', 'MODELVIEWERmainfig');
    set(mainfig.handle, 'Units', 'normalized');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'LTPDA Model Viewer',...
      'NumberTitle', 'off',...
      'Visible','off',...
      'Units','normalized',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', mainfig.Gproperties.Gcol,...
      'Resize', 'on',...
      'Tag', 'MODELVIEWERmainfig');
  end
  
  % Set mainfig callbacks
  set(mainfig.handle, 'CloseRequestFcn', {'modelViewer.cb_mainfigClose', mainfig});  
  
  %------------------------------------------------------------------------
  %- General properties
  %------------------------------------------------------------------------
  hmarg = 0.01;
  vmarg = 0.02;
  commHeight = 0.2;
  % l b w h
  
  % make settings panel
  w = 1-2*hmarg;
  h = 0.25-2*vmarg;
  l = hmarg;
  b = 1-h-vmarg;
  span = uipanel(mainfig.handle,...
    'Title', 'Select model ', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'Tag', 'SettingsPanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
%     'BackgroundColor', [1 1 1], ...
      
  % make parameter panel
  w = 1-2*hmarg;
  h = 0.55-2*vmarg;
  l = hmarg;
  b = 0.2+vmarg;
  ppan = uipanel(mainfig.handle,...
    'Title', 'Parameters ', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', [200 220 240]/255, ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'Tag', 'MODELVIEWERparamspanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  % make parameter panel
  w = 1-2*hmarg;
  h = 0.2-2*vmarg;
  l = hmarg;
  b = vmarg;
  cpan = uipanel(mainfig.handle,...
    'Title', 'Controls ', ...
    'FontSize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', [200 220 240]/255, ...
    'Position', [l b w h], ...
    'BorderType', 'line', ...
    'BorderWidth', 1, ...
    'Tag', 'MODELVIEWERcontrolpanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  %------------------------------------------------------------------------
  % Model type
  
  % label
  l = hmarg;
  h = commHeight;
  b = 1-h-3*hmarg;
  w = 0.15;
  sth = uicontrol(span, 'Style','text',...
    'String', 'model type ',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % menu box
  l = l + w + hmarg;
  w = 0.2;
  mtsh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', {'ao', 'ssm'}, ...
    'Position', [l b w h],...
    'Tag', 'ModelTypeSelect', ...
    'Callback', {'modelViewer.cb_selectModelType', mainfig});
  
  
  % Model 
  
  % label
  l = l + w + hmarg;
  h = commHeight;
  w = 0.15;
  sth = uicontrol(span, 'Style','text',...
    'String', 'model ',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % menu box
  l = l + w + hmarg;
  w = 0.4;
  clsh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', ' ', ...
    'Position', [l b w h],...
    'Tag', 'ModelSelect', ...
    'Callback', {'modelViewer.cb_selectModel', mainfig});


  % Description field  
  l = hmarg;
  h = 2.5*commHeight;
  w = 1-2*hmarg;
  b = 4*vmarg;
  sth = uicontrol(span, 'Style','text',...
    'String', '  ',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'left',...
    'BackgroundColor', [1 1 1], ...
    'Max', 1000, ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Enable', 'on', ...
    'Tag', 'MODELVIEWERmodeldescription', ...
    'Position',[l b w h]);
  
  % Fire callbacks
  modelViewer.cb_selectModelType(mtsh, mainfig);
  modelViewer.cb_selectModel(clsh, mainfig);
  
  % Build button
  h = 1.5*commHeight;
  w = 0.2;
  l = 1-w-hmarg;
  b = 4*vmarg;
  pbh = uicontrol(cpan,'Style','pushbutton',...
    'String','build',...
    'Callback', {'modelViewer.cb_buildModel', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  % Type button
  h = 1.5*commHeight;
  w = 0.2;
  l = l-w-vmarg;
  b = 4*vmarg;
  pbh = uicontrol(cpan,'Style','pushbutton',...
    'String','type',...
    'Callback', {'modelViewer.cb_typeModel', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  
  
end
