% BUILDMAINFIG build the main constructor window
%
% M Hewitson 18-10-08
%
% $Id: buildMainfig.m,v 1.2 2011/05/18 08:25:36 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   = 1000;
  mainfig.Gproperties.Gheight  = 600;
  mainfig.Gproperties.Gborder  = 10;
  mainfig.Gproperties.fontsize = 12;
  
  l = (Screen(3)/2-mainfig.Gproperties.Gwidth/2);
  b = (Screen(4)/2-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', 'SPECWINVIEWERmainfig');
%     set(mainfig.handle, 'Units', 'normalized');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'LTPDA Specwin Viewer',...
      'NumberTitle', 'off',...
      'Visible','off',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', mainfig.Gproperties.Gcol,...
      'Resize', 'on',...
      'Tag', 'SPECWINVIEWERmainfig');
%       'Units','normalized',...
  end
  
  % Set mainfig callbacks
  set(mainfig.handle, 'CloseRequestFcn', {'specwinViewer.cb_mainfigClose', mainfig});  
  
  %------------------------------------------------------------------------
  %- General properties
  %------------------------------------------------------------------------
  hmarg = 0.02;
  vmarg = 0.02;
  commHeight = 0.06;
  % l b w h
  
  % make settings panel
  w = 0.5-2*hmarg;
  h = 1-2*vmarg;
  l = hmarg;
  b = 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 = 0.5-2*hmarg;
  h = 1-2*vmarg;
  l = 0.5+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', 'SPECWINVIEWERparamspanel', ...
    'HighlightColor', [0.5 0.5 0.5]);
  
  
  %--- Settings
  
  % Select window to construct
  % label
  l = hmarg;
  h = commHeight;
  b = 1-h-3*hmarg;
  w = 0.35;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Window 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.5;
  clsh = uicontrol(span, ...
    'Style', 'popupmenu', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', specwin.getTypes, ...
    'Position', [l b w h],...
    'Tag', 'WindowSelect', ...
    'Callback', {'specwinViewer.cb_selectWindow', mainfig});
  
  % Select window size
  % label
  b = b - h - hmarg;
  l = hmarg;
  w = 0.35;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Window size ',...
    '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.5;
  ssh = uicontrol(span, ...
    'Style', 'edit', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', '100', ...
    'Position', [l b w h],...
    'Tag', 'WindowSize');
  
  % Select window psll
  % label
  b = b - h - hmarg;
  l = hmarg;
  w = 0.35;
  sth = uicontrol(span, 'Style','text',...
    'String', 'Window PSLL ',...
    'Units', 'normalized', ...
    'HorizontalAlignment', 'right',...
    'BackgroundColor', [0.9 0.9 0.9], ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h], ...
    'Tag', 'WindowPSLLlabel');
  
  % menu box
  l = l + w + hmarg;
  w = 0.5;
  ssh = uicontrol(span, ...
    'Style', 'edit', ...
    'Units', 'normalized',...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'BackgroundColor', 'w', ...
    'String', '100', ...
    'Position', [l b w h],...
    'Tag', 'WindowPSLL');
  
  % Plot time btn
  w = 0.4;
  l = hmarg;
  b = b - h - hmarg;
  pbh = uicontrol(span,'Style','pushbutton',...
    'String','Plot Time-domain',...
    'Callback', {'specwinViewer.cb_plotTime', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);  
  
  % Plot freq btn
  l = l+w+hmarg;
  pbh = uicontrol(span,'Style','pushbutton',...
    'String','Plot Freq-domain',...
    'Callback', {'specwinViewer.cb_plotFreq', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);  
  
  % info display
  w = 1-2*hmarg;
  l = hmarg;
  h = 0.4;
  b = b - h - hmarg;
  pbh = uicontrol(span,'Style','text',...
    'String',' ',...
    'BackgroundColor', [1 1 1], ...
    'ForegroundColor', [0.2 0.2 1], ...
    'Units', 'normalized', ...
    'Fontsize', 14, ...
    'Tag', 'InfoDisplay', ...
    'Position',[l b w h]);  
  
  % constructor display
  w = 1-2*hmarg;
  l = hmarg;
  h = 0.1;
  b = b - h - hmarg;
  pbh = uicontrol(span,'Style','edit',...
    'String',' ',...
    'BackgroundColor', [1 1 1], ...
    'ForegroundColor', [0 0 0], ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Tag', 'ConstructorDisplay', ...
    'Position',[l b w h]);  
  
  % axes
  M = 0.01;
  ax   = axes('Position', [0.2+M 0.2 0.7 0.7], ...
    'Parent', ppan, ...
    'Tag', 'SpecwinViewerAxes', ...
    'Visible', 'on', ...
    'Box', 'on');
  
  setappdata(mainfig.handle, 'axes', ax);
  
  % plot button
  w = 0.2;
  l = 1-w-vmarg;
  b = hmarg;
  pbh = uicontrol(ppan,'Style','pushbutton',...
    'String','plot',...
    'Callback', {'specwinViewer.cb_plot', mainfig}, ...
    'Units', 'normalized', ...
    'Fontsize', mainfig.Gproperties.fontsize, ...
    'Position',[l b w h]);  
  
  
  % Fire callbacks
  specwinViewer.cb_selectWindow(mainfig);
  

end
