function [colParaNo, choice] = exportParameterChoice( header )
%
%function [colParaNo, choice] = exportParameterChoice( header )
%
% Display the parameters in the header
% Build a GUI form to choose among the header
%
% Input
% -----
% header .......... Cell array of parameter of the labview type file
%
% Output
% ------
% colParaNo .......... Column/line of the chosen parameter
% choice ............. 1: OK ; 0 : no choice
%
% $Id$

% Initialisation
% --------------
choice    = 0;
colParaNo = [];
nHeader           = length( header );
indSelectedHeader = ones(nHeader,1);

% Current and minimum FontSize
% ----------------------------
fontsize    = 11;
minFontsize = 5;

% Change default units to centimeters
% -----------------------------------
oldUnits = get(0, 'units' );
set(0, 'units', 'centimeters');

% Get the screen dimension in centimeters
% ---------------------------------------
screenSize =  get(0, 'screensize' );

% Pushbutton dimension in cm
% ---------------------------
pbHeight     = 0.8;
pbWidth      = 2.5;
nbPushbutton = 2;

% Set the left and bottom limit of the GUI
% ----------------------------------------
XBorder   = .5;
YBorder   = 1;

% Compute the size of the GUI depending on the number of UIcontrol
% If it does not fit into the PC screen, change the FontSize
% -------------------------------------------------------------------
figWidth = 2*screenSize(3);
while figWidth > screenSize(3) && fontsize > minFontsize

  % Determine the max size of the UIcontrol
  % ----------------------------------------
  hf = figure('Visible', 'off', 'Position', screenSize);
  for ipar = nHeader : -1 : 1

    h = uicontrol( 'Parent', hf, 'Style', 'checkbox', 'String', header(ipar),...
     'Value', 1, 'Fontsize', fontsize, 'Units', 'centimeters', 'visible', 'off');

    % Get the dimension of the string in the Uicontrol
    % ------------------------------------------------
    extent = get(h, 'extent');
    cbWidth(ipar)  = extent(3);
    cbHeight(ipar) = extent(4);

    delete( h )
  end
  delete( hf )

  % Checkboxes dimension - + 0.5 : size of the checkbox
  % ---------------------------------------------------
  cbWidth     = max(cbWidth) + .5;
  cbHeight    = max(cbHeight);
  cbYinterval = .1;

  % Compute figure height using the number of UIcontrol +
  % the height of some pushbutton + the border height
  % -----------------------------------------------------
  nbCol     = 0;
  figHeight = 2*screenSize(4);
  while figHeight > screenSize(4)
    nbCol     = nbCol + 1;
    nbLine    = floor( nHeader / nbCol );
    figHeight = (cbYinterval + pbHeight) + ...
                 cbHeight*nbLine + cbYinterval*(nbLine+1) + YBorder*2;
  end

  % Compute figure height using the number of UIcontrol +
  % the height of some pushbutton
  % ---------------------------------------------------
  figHeight = (cbYinterval + pbHeight) + ...
               cbHeight*nbLine + cbYinterval*(nbLine+1);

  % Compute figure width using the number of UIcontrol +
  % border width
  % ---------------------------------------------------
  figWidth = cbWidth * nbCol + 2*XBorder;
  
  fontsize = fontsize -1;
  
end

% Compute figure width using the number of UIcontrol
% --------------------------------------------------
figWidth = cbWidth * nbCol;
figWidth = max( figWidth, nbPushbutton*pbWidth);

% Center the GUI
% --------------
XBorder = (screenSize(3) - figWidth)/2;
YBorder = (screenSize(4) - figHeight)/2;

% header Uicontrols in a new figure
% ---------------------------------
hParamFig = figure(...
  'Name', 'Choose', ...
  'NumberTitle', 'off', ...
  'Resize', 'off', ...
  'Menubar','none', 'Toolbar', 'none', ...
  'Tag', 'TAG_LBV_CHOIX', ...
  'Visible','on',...
  'HandleVisibility', 'callback',...
  'Units', 'centimeters',...
  'Position',[XBorder, YBorder, figWidth, figHeight], ...
  'Color', get(0, 'DefaultUIControlBackgroundColor'));
 % 'WindowStyle', 'modal', ...

% Iterate from each element
% -------------------------
ipar   = 1;
x_left = .05;
for i = 1 : nbCol
  
  % Set the upper UIcontrol first
  % -----------------------------
  y_bottom = (pbHeight + cbYinterval) + ...
              cbHeight*(nbLine-1) + cbYinterval*(nbLine);
  for j = 1 : nbLine

    if ipar > nHeader
      break;
    end

    % display dynamically uicontrol
    % -----------------------------
    ui(ipar) = uicontrol(...
      'Parent', hParamFig, ...
      'Style', 'checkbox', ...
      'String', header(ipar), ...
      'Fontsize', fontsize, ...
      'Value', indSelectedHeader(ipar), ...
      'Tag', ['TAG_CHECK_BOX_' num2str(ipar)], ...
      'HandleVisibility', 'on',...
      'Units', 'centimeters', ...
      'Position', [x_left y_bottom cbWidth cbHeight ]);

    y_bottom = y_bottom - cbHeight - cbYinterval;
    ipar = ipar + 1;

  end
  x_left = x_left + cbWidth;
end

% CONTINUE PUSH BUTTON
% --------------------
uicontrol( ...
  'Parent', hParamFig, ...
  'Style','pushbutton',...
  'Fontsize', fontsize,...
  'String','Continue',...
  'Interruptible','off',...
  'BusyAction','cancel',...
  'Tag','PUSH_BUTTON',...
  'Units', 'centimeters', ...
  'Position',[.05, cbYinterval, pbWidth, pbHeight],...
  'Callback', @continueCallback);

% CANCEL PUSH BUTTON
% ------------------
uicontrol( ...
  'Parent', hParamFig, ...
  'Style','pushbutton',...
  'Fontsize', fontsize,...
  'String','Cancel',...
  'Interruptible','off',...
  'BusyAction','cancel',...
  'Tag','PUSH_BUTTON',...
  'Units', 'centimeters', ...
  'Position',[.05+pbWidth, cbYinterval, pbWidth, pbHeight],...
  'Callback', @cancelCallback);

set(0, 'units', oldUnits);

% stop execution until push-button Continue was press
% and hParamFig is deleted
% ---------------------------------------------------
uiwait(hParamFig);

%% Nested callback

  % -----------------------------------------------------------------------
  % Continue action, get uicontrol fields and populate tsg structure
  % -----------------------------------------------------------------------
  function continueCallback(obj, event)

    % Everything's ok
    % ---------------
    choice = 1;

    % Get the values of the checkboxes
    % --------------------------------
    for i = 1 : nHeader
      indSelectedHeader(i) = get( ui(i), 'value');
    end

    % Return the column/line number of the parameter
    % -----------------------------------------
    colParaNo = find( indSelectedHeader == 1 );

    % close windows (replace call to uiresume(hParamFig))
    % --------------------------------------------------
    close(hParamFig);

    % flushes the event queue and updates the figure window
    % -----------------------------------------------------
    drawnow;

  end

  % -----------------------------------------------------------------------
  % Cancel button, no action
  % -----------------------------------------------------------------------
  function cancelCallback(obj, event)
    
    % return choice code (no change)
    % -----------------------------
    choice = 0;

    % close windows
    % -------------
    close(hParamFig);
    
    % flushes the event queue and updates the figure window
    % -----------------------------------------------------
    drawnow;
    
  end
end
