function varargout = TracePreferenceDialog(varargin)
% TRACEPREFERENCEDIALOG M-file for TracePreferenceDialog.fig
%      TRACEPREFERENCEDIALOG by itself, creates a new TRACEPREFERENCEDIALOG or raises the
%      existing singleton*.
%
%      H = TRACEPREFERENCEDIALOG returns the handle to a new TRACEPREFERENCEDIALOG or the handle to
%      the existing singleton*.
%
%      TRACEPREFERENCEDIALOG('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TRACEPREFERENCEDIALOG.M with the given input arguments.
%
%      TRACEPREFERENCEDIALOG('Property','Value',...) creates a new TRACEPREFERENCEDIALOG or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before TracePreferenceDialog_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to TracePreferenceDialog_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 TracePreferenceDialog

% Last Modified by GUIDE v2.5 07-Aug-2005 18:53:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @TracePreferenceDialog_OpeningFcn, ...
                   'gui_OutputFcn',  @TracePreferenceDialog_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 TracePreferenceDialog is made visible.
function TracePreferenceDialog_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 TracePreferenceDialog (see VARARGIN)


handles.current.stepsize           = varargin{1};
handles.current.max_vertex_count   = varargin{2};
handles.current.max_iter           = varargin{3};
handles.current.interp_cell_number = varargin{4};
handles.current.explore_range      = varargin{5};
handles.current.r_hit              = varargin{6};


handles.stepsize           = handles.current.stepsize           ;
handles.max_vertex_count   = handles.current.max_vertex_count   ;
handles.max_iter           = handles.current.max_iter           ;
handles.interp_cell_number = handles.current.interp_cell_number ;
handles.explore_range      = handles.current.explore_range      ;
handles.r_hit              = handles.current.r_hit      ;


guidata(hObject, handles);

init_dialog(hObject, eventdata, handles, varargin);

% Choose default command line output for TracePreferenceDialog
% handles.output = 'Yes';

% Update handles structure
% guidata(hObject, handles);

% Insert custom Title and Text if specified by the user
% Hint: when choosing keywords, be sure they are not easily confused 
% with existing figure properties.  See the output of set(figure) for
% a list of figure properties.
% if(nargin > 3)
%     for index = 1:2:(nargin-3),
%         if nargin-3==index, break, end
%         switch lower(varargin{index})
%          case 'title'
%           set(hObject, 'Name', varargin{index+1});
%          case 'string'
%           set(handles.text1, 'String', varargin{index+1});
%         end
%     end
% end

% Determine the position of the dialog - centered on the callback figure
% if available, else, centered on the screen
FigPos=get(0,'DefaultFigurePosition');
OldUnits = get(hObject, 'Units');
set(hObject, 'Units', 'pixels');
OldPos = get(hObject,'Position');
FigWidth = OldPos(3);
FigHeight = OldPos(4);
if isempty(gcbf)
    ScreenUnits=get(0,'Units');
    set(0,'Units','pixels');
    ScreenSize=get(0,'ScreenSize');
    set(0,'Units',ScreenUnits);

    FigPos(1)=1/2*(ScreenSize(3)-FigWidth);
    FigPos(2)=2/3*(ScreenSize(4)-FigHeight);
else
    GCBFOldUnits = get(gcbf,'Units');
    set(gcbf,'Units','pixels');
    GCBFPos = get(gcbf,'Position');
    set(gcbf,'Units',GCBFOldUnits);
    FigPos(1:2) = [(GCBFPos(1) + GCBFPos(3) / 2) - FigWidth / 2, ...
                   (GCBFPos(2) + GCBFPos(4) / 2) - FigHeight / 2];
end
FigPos(3:4)=[FigWidth FigHeight];
set(hObject, 'Position', FigPos);
set(hObject, 'Units', OldUnits);



% Make the GUI modal
set(handles.figure1,'WindowStyle','modal')

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

% --- Outputs from this function are returned to the command line.
function varargout = TracePreferenceDialog_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} = handles.current.stepsize;
varargout{2} = handles.current.max_vertex_count;
varargout{3} = handles.current.max_iter;
varargout{4} = handles.current.interp_cell_number;
varargout{5} = handles.current.explore_range;
varargout{6} = handles.current.r_hit;



% The figure can be deleted now
delete(handles.figure1);

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

handles.current.stepsize           = handles.stepsize             ;
handles.current.max_vertex_count   = handles.max_vertex_count     ;
handles.current.max_iter           = handles.max_iter             ;
handles.current.interp_cell_number = handles.interp_cell_number   ;
handles.current.explore_range      = handles.explore_range        ;
handles.current.r_hit              = handles.r_hit        ;

guidata(hObject, handles);

% Use UIRESUME instead of delete because the OutputFcn needs
% to get the updated handles structure.
uiresume(handles.figure1);





function StepSizeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to StepSizeEdit (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 StepSizeEdit as text
%        str2double(get(hObject,'String')) returns contents of StepSizeEdit as a double





stepsize = str2double(get(hObject, 'String'));
if isnan(stepsize)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.StepSizeEdit,        'String',sprintf('%f',    handles.stepsize));
else
    handles.stepsize = stepsize;
    guidata(hObject, handles);
    set(handles.StepSizeEdit,        'String',sprintf('%f',    handles.stepsize));
end;



% --- Executes during object creation, after setting all properties.
function StepSizeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to StepSizeEdit (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


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

uiresume(handles.figure1);



function MaxStepNumberEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxStepNumberEdit (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 MaxStepNumberEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxStepNumberEdit as a double



max_vertex_count = str2double(get(hObject, 'String'));
if isnan(max_vertex_count)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.MaxStepNumberEdit,        'String',sprintf('%d',  round(handles.max_vertex_count)));
else
    handles.max_vertex_count = round(max_vertex_count);
    guidata(hObject, handles);
    set(handles.MaxStepNumberEdit,        'String',sprintf('%d',  round(handles.max_vertex_count)));
end;


% --- Executes during object creation, after setting all properties.
function MaxStepNumberEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxStepNumberEdit (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



function ExploreRangeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ExploreRangeEdit (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 ExploreRangeEdit as text
%        str2double(get(hObject,'String')) returns contents of ExploreRangeEdit as a double


explore_range = str2double(get(hObject, 'String'));
if isnan(explore_range)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.ExploreRangeEdit,    'String',sprintf('%f',    handles.explore_range));
else
    handles.explore_range = explore_range;
    guidata(hObject, handles);
    set(handles.ExploreRangeEdit,    'String',sprintf('%f',    handles.explore_range));
end;





% --- Executes during object creation, after setting all properties.
function ExploreRangeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ExploreRangeEdit (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



function TangentIterEdit_Callback(hObject, eventdata, handles)
% hObject    handle to TangentIterEdit (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 TangentIterEdit as text
%        str2double(get(hObject,'String')) returns contents of TangentIterEdit as a double


max_iter = str2double(get(hObject, 'String'));
if isnan(max_iter)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.TangentIterEdit,     'String',sprintf('%d',round(handles.max_iter)));
else
    handles.max_iter = round(max_iter);
    guidata(hObject, handles);
    set(handles.TangentIterEdit,     'String',sprintf('%d',round(handles.max_iter)));
end;



% --- Executes during object creation, after setting all properties.
function TangentIterEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TangentIterEdit (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

function InterpCellNumberEdit_Callback(hObject, eventdata, handles)

interp_cell_number = str2double(get(hObject, 'String'));
if isnan(interp_cell_number)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.InterpCellNumberEdit,'String',sprintf('%d',round(handles.interp_cell_number)));
else
    handles.interp_cell_number = round(interp_cell_number);
    guidata(hObject, handles);
    set(handles.InterpCellNumberEdit,'String',sprintf('%d',round(handles.interp_cell_number)));
end;




% --- Executes during object creation, after setting all properties.
function InterpCellNumberEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to InterpCellNumberEdit (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




function init_dialog(hObject, eventdata, handles, varargin)

set(handles.StepSizeEdit,        'String',sprintf('%g',      handles.stepsize));
set(handles.MaxStepNumberEdit,   'String',sprintf('%d',round(handles.max_vertex_count)));
set(handles.TangentIterEdit,     'String',sprintf('%d',round(handles.max_iter)));
set(handles.InterpCellNumberEdit,'String',sprintf('%d',round(handles.interp_cell_number)));
set(handles.ExploreRangeEdit,    'String',sprintf('%g',      handles.explore_range));
set(handles.RHitEdit,            'String',sprintf('%g',      handles.r_hit));


function RHitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to RHitEdit (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 RHitEdit as text
%        str2double(get(hObject,'String')) returns contents of RHitEdit as a double




r_hit = str2double(get(hObject, 'String'));
if isnan(r_hit)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
    set(handles.RHitEdit,    'String',sprintf('%g',    handles.r_hit));
else
    handles.r_hit = r_hit;
    guidata(hObject, handles);
    set(handles.RHitEdit,    'String',sprintf('%g',    handles.r_hit));
end;


% --- Executes during object creation, after setting all properties.
function RHitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RHitEdit (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


