function varargout = mainMenu(varargin)
% MAINMENU MATLAB code for mainMenu.fig
%      MAINMENU, by itself, creates a new MAINMENU or raises the existing
%      singleton*.
%
%      H = MAINMENU returns the handle to a new MAINMENU or the handle to
%      the existing singleton*.
%
%      MAINMENU('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAINMENU.M with the given input arguments.
%
%      MAINMENU('Property','Value',...) creates a new MAINMENU or raises
%      the existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mainMenu_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mainMenu_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help mainMenu

% Last Modified by GUIDE v2.5 13-Nov-2018 08:24:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mainMenu_OpeningFcn, ...
                   'gui_OutputFcn',  @mainMenu_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before mainMenu is made visible.
function mainMenu_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mainMenu (see VARARGIN)

% Choose default command line output for mainMenu
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

initialize_gui(hObject, handles, false);

% UIWAIT makes mainMenu wait for user response (see UIRESUME)
 uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = mainMenu_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = str2num(get(handles.nMax_edit, 'String'));
varargout{2} = str2double(get(handles.step_edit, 'String'));
varargout{3} = str2double(get(handles.seuil_edit, 'String'));
varargout{4} = handles.optimdata.calculOK;

delete(handles.figure1);


% --- Executes during object creation, after setting all properties.
function nMax_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nMax_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nMax_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nMax_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nMax_edit as text
%        str2double(get(hObject,'String')) returns contents of nMax_edit as a double
nbMax = str2num(get(hObject, 'String'));
if isnan(nbMax)
    set(hObject, 'String', 5000);
    errordlg('Input must be a number','Error');
end

% Save the new nMax_edit value
handles.optimdata.nbMax = nbMax;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function step_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_edit_Callback(hObject, eventdata, handles)
% hObject    handle to step_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_edit as text
%        str2double(get(hObject,'String')) returns contents of step_edit as a double
step = str2double(get(hObject, 'String'));
if isnan(step)
    set(hObject, 'String', 0.0005);
    errordlg('Input must be a number','Error');
end

% Save the new nMax_edit value
handles.optimdata.step = step;
guidata(hObject,handles)

% --- Executes on button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject    handle to calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.optimdata.calculOK = 1;
guidata(hObject,handles);
if isequal(get(handles.figure1, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(handles.figure1);
else
% The GUI is no longer waiting, just close it
delete(handles.figure1);
end
  

% --- Executes on button press in annule.
function annule_Callback(hObject, eventdata, handles)
% hObject    handle to annule (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.optimdata.calculOK = 0;
guidata(hObject,handles);
if isequal(get(handles.figure1, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(handles.figure1);
else
% The GUI is no longer waiting, just close it
delete(handles.figure1);
end

% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the annule flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to annule the data.
 
 

set(handles.nMax_edit, 'String', '5000');
set(handles.step_edit,  'String', '0.0005');
set(handles.seuil_edit, 'String', '0.0000001');
 
% Update handles structure
guidata(handles.figure1, handles);



function seuil_edit_Callback(hObject, eventdata, handles)
% hObject    handle to seuil_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
seuil = str2double(get(hObject, 'String'));
if isnan(seuil)
    set(hObject, 'String', 0.000001);
    errordlg('Input must be a number','Error');
end

% Save the new nMax_edit value
handles.optimdata.seuil = seuil;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of seuil_edit as text
%        str2double(get(hObject,'String')) returns contents of seuil_edit as a double


% --- Executes during object creation, after setting all properties.
function seuil_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to seuil_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
