function setup_main

  global vars handles

handles.mainwindow = ...
    figure('Unit','pixels','Position',[20 50 800 600]);
clf reset
blackBackground(gcf)
set(gcf,'Units','normalized')
set(gcf,'CloseRequestFcn','close_all');
set(handles.mainwindow,'Name','HHsim Hodgkin-Huxley Simulator', ...
    'MenuBar','none','NumberTitle','off');
handles.axmain = axes('Position',[0 0.6 1 0.4]);
axis off
set(gcf,'PaperPositionMode','auto');
set(gcf,'InvertHardcopy','on');

s = version('-release');  % should be something like '2015a'
if length(s) ~= 5   % failsafe check
    vars.matlab_release = 2015;
else
    vars.matlab_release = str2num(s(1:4));
end

if vars.matlab_release >= 2015
    drawattr = 'SortMethod';
    drawvalue = 'childorder';
else
    drawattr = 'DrawMode';
    drawvalue = 'fast';
end

handles.mainplot = axes('Position',[0.08 0.53 0.9 0.45]);
set(handles.mainplot,drawattr,drawvalue, 'XTickLabelMode', 'auto', ...
    'YTickLabelMode','manual', 'YTick', -100:20:60, ...
    'YTickLabel', -100:20:60, 'ButtonDownFcn', 'set_cursor_vis(''off'')');
make_splot(gca,500,0,[-100 60],{'r' vars.stimcolor})
ylabel('Membrane Voltage (mV)')
grid on, box on
userdata = get(handles.mainplot, 'UserData');
set(userdata.lines(1), 'ButtonDownFcn', 'line_click(1)');
set(userdata.lines(2), 'ButtonDownFcn', 'line_click(2)');

handles.varplot = axes('Position',[0.08 0.3 0.9 0.18]);
set(handles.varplot,drawattr,drawvalue, 'ButtonDownFcn', 'set_cursor_vis(''off'')');
set(handles.varplot,'YTickLabelMode', 'manual', ...
    'Ytick', [0 1], 'YTickLabel', ['Low ';'High']);
make_splot(gca,500,0,[-0.1 1.1],{'y' 'g' 'c'})
xlabel('Time (msec)')
grid on, box on
userdata = get(handles.varplot, 'UserData');
set(userdata.lines(1), 'ButtonDownFcn', 'line_click(3)');
set(userdata.lines(2), 'ButtonDownFcn', 'line_click(4)');
set(userdata.lines(3), 'ButtonDownFcn', 'line_click(5)');

linkaxes([handles.mainplot handles.varplot], 'x');
varlist = {'m','h','n','I_Na (uA)','I_K (uA)', 'g_Na (uS)', 'g_K (uS)', 'I_leak (uA)', ...
    'p (user chan)', 'q (user chan)', 'I_user (uA)', 'g_user (uS)', 'blank'};

handles.modebutton = ...
  uicontrol('Style','Popup','Position',[641 1 140 22], ...
    'Value',1,'String',{'Voltage Recording','Voltage Clamp'}, ...
    'FontSize',9, 'Tooltip','Toggle Voltage Clamp mode', ...
    'CallBack','callbacks(23)');

%%%% Var Buttons

handles.v1button = ...
  uicontrol('Style','PopUp','String',varlist,'Value',1, ...
	'Units','pixel','Position',[110 45 100 21], ...
	'Tooltips','Select plot variable', ...
        'ForegroundColor', 'k', ...
	'BackgroundColor','y','CallBack','change_varplotlines');
handles.v2button = ...
  uicontrol('Style','PopUp','String',varlist,'Value',2, ...
	'Units','pixel','Position',[220 45 100 21], ...
	'Tooltips','Select plot variable', ...
        'ForegroundColor', 'k', ...
	'BackgroundColor','g','CallBack','change_varplotlines');
handles.v3button = ...
  uicontrol('Style','PopUp','String',varlist,'Value',3, ...
	'Units','pixel','Position',[330 45 100 21], ...
	'Tooltips','Select plot variable', ...
        'ForegroundColor', 'k', ...
	'BackgroundColor','c','CallBack','change_varplotlines');

handles.v1toptext = uicontrol('Style','text','String', '1','Units','pixel', ...
      'ForegroundColor', 'y', 'Position', [120 68 80 17], ...
      'BackgroundColor','black');
handles.v1bottomtext = uicontrol('Style','text','String', '0','Units','pixel', ...
      'ForegroundColor', 'y', 'Position', [120 25 80 17], ...
      'BackgroundColor','black');
handles.v2toptext = uicontrol('Style','text','String', '1','Units','pixel', ...
      'ForegroundColor', 'g', 'Position', [230 68 80 17], ... 
      'BackgroundColor','black');
handles.v2bottomtext = uicontrol('Style','text','String', '0','Units','pixel', ...
      'ForegroundColor', 'g', 'Position', [230 25 80 17], ...
      'BackgroundColor','black');
handles.v3toptext = uicontrol('Style','text','String', '1','Units','pixel', ...
      'ForegroundColor', 'c', 'Position', [340 68 80 17], ...
      'BackgroundColor','black');
handles.v3bottomtext = uicontrol('Style','text','String', '0','Units','pixel', ...
      'ForegroundColor', 'c', 'Position', [340 25 80 17], ... 
      'BackgroundColor','black');
handles.highlabeltext = uicontrol('style','text','string','High value:', ...
	  'ForegroundColor', 'white', 'BackgroundColor','black', ...
          'Position', [10 68 80 17]);
handles.lowlabeltext = uicontrol('style','text','string','Low value:', ...
	  'ForegroundColor', 'white', 'BackgroundColor','black', ...
          'Position', [10 25 80 17]);

handles.v1axislabel = ...
    uicontrol('Style','Text','Position',[10 210 40 15], ...
    'BackgroundColor','k','ForegroundColor','y', ...
    'HorizontalAlignment','right');
handles.v2axislabel = ...
    uicontrol('Style','Text','Position',[10 190 40 15], ...
    'BackgroundColor','k','ForegroundColor','g', ...
    'HorizontalAlignment','right');
handles.v3axislabel = ...
    uicontrol('Style','Text','Position',[10 170 40 15], ...
    'BackgroundColor','k','ForegroundColor','c', ...
    'HorizontalAlignment','right');

vars.varplotdata_size = 500;
vars.varplotdata = NaN * ones(length(varlist),vars.varplotdata_size);
vars.Vtot_hist = NaN * ones(1,vars.varplotdata_size);
vars.Itot_hist = NaN * ones(1,vars.varplotdata_size);
vars.time_hist = zeros(1,vars.varplotdata_size);

vars.cursorx=1;
store_state;

xwidth = 200 * vars.plot_rate * vars.deltaT_plot * 1e3;

handles.slider = ...
   uicontrol('Style','Slider','Value', xwidth/2, ...
	    'Visible', 'on', ...
            'Min',100 * vars.plot_rate * vars.deltaT_max * 1e3, ...
            'Max',100 * vars.plot_rate * vars.deltaT_max * 1e3 + 1, ...
            'SliderStep',[0.01 1], ...
	    'Position', [42 90 630 15], 'CallBack', 'moveslider'); 
    
slider_set;

handles.cursor = rectangle('Curvature', [1 1], 'visible', 'off', ...
    'HitTest','off');

set_xaxis_limits( xwidth/2 );

figure(handles.mainwindow)
set(gcf,'BackingStore','off')
set(gcf,'Units','pixels');
bx = 5; toprowy = 478;
bw = 80; bh = 22;
handles.membutton = ...
  uicontrol('Style','CheckBox','Position',[bx toprowy bw bh], ...
    'Value',0,'String','Membrane','FontSize',9, ...
    'Tooltip','Toggle membrane window', ...
    'CallBack','callbacks(1)');
bx=bx+bw+3;

handles.chanbutton = ...
  uicontrol('Style','CheckBox','Position',[bx toprowy bw bh], ...
    'Value',0,'String','Channels','FontSize',9, ...
    'Tooltip','Toggle channels window', ...
    'CallBack','callbacks(2)');
bx=bx+bw+3;

handles.stimbutton = ...
  uicontrol('Style','CheckBox','Position',[bx toprowy bw bh], ...
    'Value',0,'String','Stimuli','FontSize',9, ...
    'HorizontalAlignment','left', ...
    'Tooltip','Toggle stimulus window', ...
    'CallBack','callbacks(3)');

handles.vc_button = ...
  uicontrol('Style','CheckBox','Position',[bx toprowy bw bh], ...
    'Value',1,'String','VCstim','FontSize',9, ...
    'HorizontalAlignment','left', ...
    'Tooltip','Toggle voltage clamp details window', ...
    'CallBack','callbacks(24)','visible','off');
bx=bx+bw+3;

handles.drugbutton = ...
  uicontrol('Style','CheckBox','Position',[bx toprowy bw bh], ...
    'Value',0,'String','Drugs','FontSize',9, ...
    'HorizontalAlignment','left', ...
    'Tooltip','Toggle drugs window', ...
    'CallBack','callbacks(4)');
bx=bx+bw+3;

handles.stim1button = ...
  uicontrol('Style','PushButton','Position',[5 1 45 20], ...
	'String','Stim1','FontSize',9,'BackgroundColor',vars.stimcolor, ...
	'Tooltip','Stimulus pattern 1', ...
	'CallBack','callbacks(5)');
handles.stim2button = ...
  uicontrol('Style','PushButton','Position',[50 1 45 20], ...
	'String','Stim2','FontSize',9,'BackgroundColor',vars.stimcolor, ...
	'Tooltip','Stimulus pattern 2', ...
	'CallBack','callbacks(6)');

handles.runbutton = ...
  uicontrol('Style','PushButton','Position',[190 1 45 20], ...
	'String','Run','FontSize',9,'BackgroundColor',[0.4 1 0.4], ...
	'Tooltip','Run simulation', ...
	'CallBack','run_system(2)');

handles.nudgebutton = ...
  uicontrol('Style','PushButton','Position',[235 1 45 20], ...
	'String','Nudge','FontSize',9,'BackgroundColor',[0.8 0.8 0.4], ...
	'Tooltip','Run for a few steps', ...
	'CallBack','run_system(1)');

handles.stopbutton = ...
  uicontrol('Style','PushButton','Position',[280 1 45 20], ...
	'String','Stop','FontSize',9,'BackgroundColor',[1 0.4 0.4], ...
	'Tooltip','Stop simulation', ...
	'CallBack','run_system(3)');

handles.printbutton = ...
  uicontrol('Style','PushButton','Position',[350 1 45 20], ...
  'String','Print','FontSize',9, ...
  'Tooltip','Print to file', ...
  'Callback','print_data;');

handles.exportbutton = ...
  uicontrol('Style','PushButton','Position',[400 1 50 20], ...
  'String','Export','FontSize',9, ...
  'Tooltip','Export data to text file', ...
  'Callback','export_data;');

handles.clearbutton = ...
  uicontrol('Style','PushButton','Position',[455 1 50 20], ...
	'String','Clear','FontSize',9, ...
	'Tooltip','Clear plot', ...
	'CallBack','callbacks(7)');

handles.zoominbutton = ...
  uicontrol('Style','PushButton','Position',[585 1 55 20], ...
	'String','Zoom in','FontSize',9, ...
	'CallBack','resize_plots(-1)', 'visible', 'off');

handles.zoomoutbutton = ...
  uicontrol('Style','PushButton','Position',[640 1 55 20], ...
	'String','Zoom out','FontSize',9, ...
	'CallBack','resize_plots(1)', 'visible', 'off');

%%%%%%%%% radio buttons
handles.radio_frame = ...
    uicontrol('style', 'frame', 'position', [431 0 185 25], ...
    'backgroundcolor', [.6 .3 .3]);

handles.zoombutton = ...
    uicontrol('Style', 'togglebutton', 'Position', [497 3 55 20], ...
    'String', 'Zoom', 'FontSize', 9, ...
    'BackgroundColor', [0.94 0.94 0.94], ...
    'CallBack', 'radiobuttons(1)', 'visible', 'on', 'ForegroundColor', 'k', ...
    'value', 0);

handles.panbutton = ...
    uicontrol('Style', 'togglebutton', 'Position', [555 3 55 20], ...
    'String', 'Pan', 'FontSize', 9, ...
    'BackgroundColor', [0.94 0.94 0.94], ...
    'CallBack', 'radiobuttons(2)', 'visible', 'on', 'ForegroundColor', 'k', ...
    'value',  0);

handles.cursorbutton = ...
    uicontrol('Style', 'togglebutton', 'Position', [437 3 55 20], ...
    'String', 'Cursor', 'FontSize', 9, ...
    'BackgroundColor', [0.94 0.94 0.94], ...
    'CallBack', 'radiobuttons(3)', 'visible', 'on', 'ForegroundColor', 'r', ...
    'value',  1);
    

handles.helpbutton = ...
  uicontrol('Style','PushButton','Position',[675 toprowy 22 22], ...
        'String','?','FontSize',11,'FontWeight','b', ... 
	'Tooltip','Browse user guide', ...
        'CallBack','help_hhsim');

        

%%%%%%%%%%%%%%%% Cursor

cursor_frame_color = [0.3 0.1 0.1];

handles.recall_frame = ...
    uicontrol('style', 'frame', 'position', [550 0 62 30], ...
    'BackgroundColor', cursor_frame_color);

handles.cursor_frame = uicontrol('Style','frame', ...
    'Position',[450 25 360 60], ...
    'BackgroundColor',cursor_frame_color);

handles.cursor_instruction = uicontrol('Style','text', ...
    'Position',[451 30 340 50], ...
    'BackgroundColor',get(handles.cursor_frame,'BackgroundColor'), ...
    'ForegroundColor','w', ...
    'String','Click on any of the lines above to set the cursor and measure the value at that point.');

handles.recallbutton = ...
  uicontrol('Style','PushButton','Position',[556 2 50 20], ...
	'String','Recall','FontSize',9, ...
	'Tooltip','Recall saved state', ...
	'CallBack','callbacks(8)');

handles.cursor_text = uicontrol('Style','text','String','','Units','pixel', ...
    'ForegroundColor','black','BackgroundColor','k', 'visible', 'off', ...
    'FontSize',10,'Position',[490 55 90 20]);

handles.cursor_time_text = ...
    uicontrol('Style','text','String','','Units','pixel', ...
    'ForegroundColor','w','BackgroundColor','k', 'visible', 'off', ...
    'FontSize',10,'Position',[595 55 85 20]);

handles.cursor_label_text = ...
    uicontrol('Style','text','String','','Units','pixel', ...
    'BackgroundColor',cursor_frame_color, ...
    'FontSize',10,'Position',[460 30 150 20]);

handles.changelinebutton = ...
  uicontrol('Style','PushButton','Position',[455 55 32 20], ...
  'String','var','FontSize',9, ...
  'CallBack','move_cursor(0)', 'tooltip', 'move cursor to a different line');

handles.storebutton = ...
  uicontrol('Style','PushButton','Position',[730 30 40 20], ...
  'String','Save','FontSize',9, ...
  'CallBack','store_state', 'tooltip', 'Save state for later recall');

handles.cursor_off = ...
    uicontrol('Style', 'PushButton', 'Position', [782 65 15 15], ...
    'String', 'X', 'FontSiZe', 8, ...
    'tooltip', 'remove cursor', 'CallBack', 'cursor_off', 'visible', 'off');

handles.farleftbutton = ...
  uicontrol('Style','PushButton','Position',[595 30 25 20], ...
  'String','<<','FontSize',9, ...
  'CallBack','move_cursor(-2)', 'tooltip', 'move to next maximum or minimum');

handles.leftbutton = ...
  uicontrol('Style','PushButton','Position',[625 30 20 20], ...
    'String','<','FontSize',9, ...
    'CallBack','move_cursor(-1)', 'tooltip', 'move right one step');

handles.rightbutton = ...
  uicontrol('Style','PushButton','Position',[650 30 20 20], ...
    'String','>','FontSize',9, ...
    'CallBack','move_cursor(1)', 'tooltip', 'move left on step');

handles.farrightbutton = ...
  uicontrol('Style','PushButton','Position',[675 30 25 20], ...
    'String','>>','FontSize',9, ...
    'CallBack','move_cursor(2)', 'tooltip', 'move to next maximum or minimum');

handles.vc_delbutton = ...
  uicontrol('Style','PushButton','Position',[730 30 30 20], ...
  'String','Del','FontSize',9, 'Visible', 'off', ...
  'CallBack','vc_delcurve', 'tooltip', 'Delete this line');

set_cursor_vis('off');

handles.vc_mainplot = axes('Position',[0.09 0.53 0.9 0.45]);
set(handles.vc_mainplot,drawattr,drawvalue, 'XTickLabelMode', 'auto', ...
    'ButtonDownFcn', 'set_cursor_vis(''off'')','visible','off');
ylabel('Current (nA)')
grid on, box on

handles.vc_vplot = axes('Position',[0.09 0.3 0.9 0.2]);
set(handles.vc_vplot,drawattr,drawvalue, ...
    'YTickLabelMode','manual', ...
    'YTickLabel',[-90:30:60], 'YTick',[-90:30:60],'YLim',[-90 60], ...
    'ButtonDownFcn', 'set_cursor_vis(''off'')', ...
    'visible','off');
xlabel('Time (msec)');
ylabel('Voltage (mV)');
grid on, box on


handles.vc_slider = ...
   uicontrol('Style','Slider','Value', xwidth/2, ...
	    'Visible', 'off', ...
            'Min',100 * vars.plot_rate * vars.deltaT_max * 1e3, ...
            'Max',100 * vars.plot_rate * vars.deltaT_max * 1e3 + 1, ...
            'SliderStep',[0.01 1], ...
	    'Position', [42 90 630 15], 'CallBack', 'vc_moveslider'); 

pos = get(handles.vc_slider, 'position');

handles.vc_helpnote1 = ...
  uicontrol('Style','text','String','', ...
            'FontSize',12,'ForegroundColor','m','BackgroundColor','black', ...
            'visible','off',...
            'HorizontalAlignment','left','Position',[36 60 415 20]);

handles.vc_helpnote2 = ...
  uicontrol('Style','text','String','   Press "Run" to plot.', ...
            'FontSize',12,'ForegroundColor','white', ...
            'BackgroundColor','black' ,'visible','off',...
            'HorizontalAlignment','left','Position',[40 40 250 20]);

% put these after the help notes so they come out on top
handles.zoom_instruction = uicontrol('Style','text', ...
    'Position',[620 45 180 65], ...
    'BackgroundColor',0.15*[1 1 1], ...
    'ForegroundColor','r', ...
    'String','Left-click to zoom in, right-click for more options. Click and drag for zooming box', ...
    'visible', 'off');

handles.pan_instruction = uicontrol('Style','text', ...
    'Position',[620 45 180 65], ...
    'BackgroundColor',0.15*[1 1 1], ...
    'ForegroundColor','r', ...
    'String',{'','Click and drag to pan'}, ...
    'visible', 'off');

handles.vc_zoom_instruction = uicontrol('Style','text', ...
    'Position',[620 45 180 65], ...
    'BackgroundColor',0.15*[1 1 1], ...
    'ForegroundColor','r', ...
    'String','Left-click to zoom in, right-click for more options. Click and drag for zooming box', ...
    'visible', 'off');

handles.vc_pan_instruction = uicontrol('Style','text', ...
    'Position',[620 45 180 65], ...
    'BackgroundColor',0.15*[1 1 1], ...
    'ForegroundColor','r', ...
    'String',{'','Click and drag to pan'}, ...
    'visible', 'off');

vc_varselect(3,1);
handles.vc_vertslider = ...
   uicontrol('Style','Slider','Value', 50, ...
	    'Visible', 'off', ...
            'Min',0 , ...
            'Max',100 , ...
            'SliderStep',[0.01 .5], ...
	    'Position', [2 349 18 167], 'CallBack', 'vc_movevertslider'); 

handles.yzoominbutton=uicontrol('Style','PushButton','String','+',...
  'Position',[2 450 18 18],'CallBack','callbacks(31)','visible','off');

handles.yzoomoutbutton=uicontrol('Style','PushButton','String','-',...
  'Position',[2 265 18 18],'CallBack','callbacks(32)','visible','off');
vc_vertslider_pos;

vc_clear;


%used to set symbols in translate buttons for scrolling without a scrollbar
updown = 'udrl';
updown(1) = uint8(173);
updown(2) = uint8(175);
updown(3) = uint8(174);
updown(4) = uint8(172);

handles.transupbutton = ...
    uicontrol('Style','PushButton','fontname', 'symbol', 'String',updown(1),...
  'Position',[2 449 18 18],'CallBack','callbacks(34)','visible','off');

handles.transdownbutton = ...
    uicontrol('Style','PushButton','fontname', 'symbol', 'String',updown(2),...
  'Position',[2 266 18 18],'CallBack','callbacks(35)','visible','off');
change_varplotlines  % to set up initial axis labels
linkaxes([handles.vc_mainplot handles.vc_vplot], 'x');
set(handles.mainwindow,'ResizeFcn','resize_main');
vars.move_slider = 0;
resize_main
