% BUILDMAINFIG build the main constructor window
%
% M Hewitson 18-10-08
%
% $Id: buildMainfig.m,v 1.2 2008/12/02 08:59:14 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.3;
  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', 'CONSTRUCTORmainfig');
    set(mainfig.handle, 'Units', 'normalized');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'LTPDA Constructor Helper',...
      'NumberTitle', 'off',...
      'Visible','off',...
      'Units','normalized',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', mainfig.Gproperties.Gcol,...
      'Resize', 'on',...
      'Tag', 'CONSTRUCTORmainfig');
  end
  
  % Set mainfig callbacks
  set(mainfig.handle, 'CloseRequestFcn', {'constructor.cb_mainfigClose', mainfig});  
  
  %------------------------------------------------------------------------
  %- General properties
  %------------------------------------------------------------------------
  hmarg = 0.01;
  vmarg = 0.02;
  commHeight = 0.5;
  % l b w h
  
  % make settings panel
  w = 1-2*hmarg;
  h = 0.2-2*vmarg;
  l = hmarg;
  b = 1-h-vmarg;
  span = uipanel(mainfig.handle,...
    'Title', 'Settings ', ...
    '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]);
  
      
  % make build panel
  w = 1-2*hmarg;
  h = 0.8-2*vmarg;
  l = hmarg;
  b = vmarg;
  ppan = uipanel(mainfig.handle,...
    'Title', 'Build ', ...
    '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]);
  
  
  %--- Settings
  
  % Select class to construct
  % label
  l = hmarg;
  h = commHeight;
  b = 0.5-(h/2);
  w = 0.15;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Select class ',...
    '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;
  clsh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', utils.helper.ltpda_userclasses, ...
    'Position', [l b w h],...
    'Tag', 'ClassSelect', ...
    'Callback', {'constructor.cb_selectClass', mainfig});
  
  % Select class to construct
  % label
  l = l + w + hmarg;
  w = 0.15;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Select set ',...
    '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.35;
  ssh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', {' '}, ...
    'Position', [l b w h],...
    'Tag', 'SetSelect', ...
    'Callback', {'constructor.cb_selectSet', mainfig});
  
  
  
  %------------- Build panel
  
  % display plist
  l = hmarg;
  w = 1-2*hmarg;
  b = 0.2;
  h = 0.8-vmarg;
  sth = uicontrol(ppan, 'Style','edit',...
    'String', '',...
    'Units', 'normalized', ...
    'BackgroundColor', [1 1 1], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Tag', 'ConstructorDisplay', ...
    'HorizontalAlignment', 'left', ...
    'Max', 100, ...
    'Position',[l b w h]);
  
  % Variable name field
  l = hmarg;
  b = vmarg;
  w = 0.15;
  h = 0.1;
  sth = uicontrol(ppan,'Style','text',...
    'String','Variable ',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'left', ...
    'BackgroundColor', [200 220 240]/255, ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);
  
  l = l + w + hmarg;
  
  eth = uicontrol(ppan,'Style','edit',...
    'String','obj',...
    'Units', 'normalized', ...
    'BackgroundColor', 'w', ...
    'HorizontalAlignment', 'center', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Tag', 'VariableTxt', ...
    'Position',[l b w h]);
  
  %  Build btn
  l = l + w + hmarg;
  pbh = uicontrol(ppan,'Style','pushbutton','String','Build',...
    'Units', 'normalized', ...
    'Position',[l b w h],...
    'Tag', 'BuildBtn');
  set(pbh, 'Callback', {'constructor.buildObj', mainfig});
  
  
  % Fire callbacks
  
  constructor.cb_selectClass(clsh, mainfig);
  constructor.cb_selectSet(ssh, mainfig);
  
  
  
end
