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

% Last Modified by GUIDE v2.5 21-Mar-2012 11:13:01

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

% Choose default command line output for rawviewer
handles.output = hObject;
if ~isfield(handles, 'currentnum')
    handles.currentnum = 0;
    handles.currentbutton = 0;
end
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = rawviewer_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.output;


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
% x = get(hObject, 'UserData');
% delete(x);
set(hObject, 'Visible', 'off');


% --- Executes on button press in refbutton.
function refbutton_Callback(hObject, eventdata, handles)
% hObject    handle to refbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.reference();


% --- Executes on button press in b1.
function b1_Callback(hObject, eventdata, handles)
% hObject    handle to b1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 1);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(1, type);

% --- Executes on button press in b2.
function b2_Callback(hObject, eventdata, handles)
% hObject    handle to b2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 2);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(2, type);


% --- Executes on button press in b3.
function b3_Callback(hObject, eventdata, handles)
% hObject    handle to b3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 3);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(3, type);

% --- Executes on button press in b4.
function b4_Callback(hObject, eventdata, handles)
% hObject    handle to b4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 4);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(4, type);

% --- Executes on button press in b5.
function b5_Callback(hObject, eventdata, handles)
% hObject    handle to b5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 5);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(5, type);

% --- Executes on button press in b6.
function b6_Callback(hObject, eventdata, handles)
% hObject    handle to b6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 6);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(6, type);

% --- Executes on button press in b7.
function b7_Callback(hObject, eventdata, handles)
% hObject    handle to b7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 7);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(7, type);

% --- Executes on button press in b8.
function b8_Callback(hObject, eventdata, handles)
% hObject    handle to b8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 8);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(8, type);

% --- Executes on button press in b9.
function b9_Callback(hObject, eventdata, handles)
% hObject    handle to b9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 9);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(9, type);

% --- Executes on button press in b10.
function b10_Callback(hObject, eventdata, handles)
% hObject    handle to b10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 10);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(10, type);

% --- Executes on button press in b11.
function b11_Callback(hObject, eventdata, handles)
% hObject    handle to b11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 11);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(11, type);

% --- Executes on button press in b12.
function b12_Callback(hObject, eventdata, handles)
% hObject    handle to b12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebutton(hObject, 12);
x = get(handles.figure1, 'UserData');
type = get(hObject, 'String');
x.getdata(12, type);

% --- Executes on button press in refreshbutton.
function refreshbutton_Callback(hObject, eventdata, handles)
% hObject    handle to refreshbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[number, object] = getsetnumobj(0, 0);
if ~isempty(number) && ~isempty(object)
    x = get(handles.figure1, 'UserData');
    x.getdata(number, get(object, 'String'));
end

% This is purely a visual thing. It shows the currently displayed graph.
% BTW: I store the state of the button in the displayed string. It's not
% super kosher, but oh well.
function updatebutton(hObject, number)
[~, lastobj] = getsetnumobj(0, 0);
if isempty(lastobj)
    getsetnumobj(number, hObject);
    cycle(hObject);
elseif lastobj == hObject
    cycle(hObject);
else
    setoff(lastobj);
    getsetnumobj(number, hObject);
    cycle(hObject);
end

function [number, object] = getsetnumobj(num, obj)
persistent lastnumber;
persistent lastobject;
if isempty(num) || isempty(obj)
    lastnumber = num;
    lastobject = obj;
elseif num == 0 && obj == 0
    number = lastnumber;
    object = lastobject;
else
    lastnumber = num;
    lastobject = obj;
end

function setoff(hObject)
set(hObject, 'String', 'Off');
set(hObject, 'BackgroundColor', [0.94 0.94 0.94]);

function cycle(hObject)
switch get(hObject, 'String')
    case 'Off'
        set(hObject, 'String', 'Norm');
        set(hObject, 'BackgroundColor', [0 0.8 0]);
    case 'Norm'
        set(hObject, 'String', 'Raw');
        set(hObject, 'BackgroundColor', [0.4 0 0]);
    case 'Raw'
        set(hObject, 'String', 'Ref');
        set(hObject, 'BackgroundColor', [0 0 0.8]);
    case 'Ref'
        set(hObject, 'String', 'Norm');
        set(hObject, 'BackgroundColor', [0 0.8 0]);
end


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
getsetnumobj([], []);
