function varargout = SurfBCDialogue(varargin)
% SurfBCDialogue is a dialogue for defining boundary conditions along edges
% of the mesh. The user must specify both type (natural or essential) and
% magnitude of normal and tangential components of the boundary condition.
% Look to the title bar of the dialogue to see which edge is currently
% being considered. 

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SurfBCDialogue_OpeningFcn, ...
                   'gui_OutputFcn',  @SurfBCDialogue_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 SurfBCDialogue is made visible.
function SurfBCDialogue_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

edgenum=varargin{1};
set(handles.figure1,'Name',['BCs for edge #',num2str(edgenum)]);

% Choose default settings
handles.tc='n'; % traction tangential
handles.nc='n'; % traction normal
set(handles.normbctype,'Value',1);
set(handles.tangbctype,'Value',1);
set(handles.normbcval,'String',0);
set(handles.tangbcval,'String',0);
set(handles.normbcunits,'String','Force/Area');
set(handles.tangbcunits,'String','Force/Area');

% Choose default command line output for BCDialogue
handles.code = 1;
handles.norm = 0;
handles.tang = 0;

set(handles.figure1,'Color',[.996 .496 .313]);

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = SurfBCDialogue_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.code;
varargout{2} = handles.norm;
varargout{3} = handles.tang;
varargout{4} = handles.figure1;

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


% --- Executes on selection change in tangbctype.
function tangbctype_Callback(hObject, eventdata, handles)
% hObject    handle to tangbctype (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns tangbctype contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tangbctype
val = get(hObject,'Value');

switch val       
    case 1 % natural
        handles.tc='n';
        switch handles.nc
            case 'e'
                handles.code=2;
            case 'n'
                handles.code=1;
        end
        set(handles.tangbcunits,'String','Force/Area');
        
    case 2 % essential
        handles.tc='e';
        switch handles.nc
            case 'e'
                handles.code=4;
            case 'n'
                handles.code=3;
        end
        set(handles.tangbcunits,'String','Length');

end
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function tangbctype_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tangbctype (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 tangbcval_Callback(hObject, eventdata, handles)
% hObject    handle to tangbcval (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 tangbcval as text
%        str2double(get(hObject,'String')) returns contents of tangbcval as a double
handles.tang=str2double(get(hObject,'String'));
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function tangbcval_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tangbcval (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 selection change in normbctype.
function normbctype_Callback(hObject, eventdata, handles)
% hObject    handle to normbctype (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns normbctype contents as cell array
%        contents{get(hObject,'Value')} returns selected item from normbctype
val = get(hObject,'Value');

% (code==1)   %normal: traction; tangential: traction
% (code==2)   %normal: displacement; tangential: traction
% (code==3)   %normal: traction; tangential: displacement
% (code==4)   %normal: displacement; tangential: displacement
switch val
    case 1 % natural
        handles.nc='n';
        switch handles.tc
            case 'e'
                handles.code=3;
            case 'n'
                handles.code=1;
        end
        set(handles.normbcunits,'String','Force/Area');
        
    case 2 % essential
        handles.nc='e';
        switch handles.tc
            case 'e'
                handles.code=4;
            case 'n'
                handles.code=2;
        end
        set(handles.normbcunits,'String','Length');
        
end
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function normbctype_CreateFcn(hObject, eventdata, handles)
% hObject    handle to normbctype (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 normbcval_Callback(hObject, eventdata, handles)
% hObject    handle to normbcval (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 normbcval as text
%        str2double(get(hObject,'String')) returns contents of normbcval as a double
handles.norm=str2double(get(hObject,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function normbcval_CreateFcn(hObject, eventdata, handles)
% hObject    handle to normbcval (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 donebutton.
function donebutton_Callback(hObject, eventdata, handles)
% hObject    handle to donebutton (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);
