function mainswitch(action)
% MAINSWITCH   Opens a window introducing the demonstration programs
% for Theory and Design for Mechanical Measurements

if nargin==0		% start-up
	h0 = figure('Color',[0.07 0.07 0.16], ...
		'Name','Welcome','NumberTitle','off', ...
		'Units','pixels','Position',[100 35 512 384], ...
		'Tag','MainSwitch');
	currdir=pwd;
	homedir=pathto('mainswitch.m');
	cd(homedir)
	d=dir('fontpref.mat');
	if (length(d))
		load fontpref
		set(h0,'DefaultAxesFontName',axfont.FontName, ...
			'DefaultAxesFontUnits',axfont.FontUnits, ...
			'DefaultAxesFontSize',axfont.FontSize, ...
			'DefaultAxesFontWeight',axfont.FontWeight, ...
			'DefaultAxesFontAngle',axfont.FontAngle, ...
			'DefaultUicontrolFontName',uifont.FontName, ...
			'DefaultUicontrolFontUnits',uifont.FontUnits, ...
			'DefaultUicontrolFontSize',uifont.FontSize, ...
			'DefaultUicontrolFontWeight',uifont.FontWeight, ...
			'DefaultUicontrolFontAngle',uifont.FontAngle)
	end
	cd(currdir)
	h1 = uicontrol('Parent',h0, ...
		'Units','pixels', ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'FontName','Times', ...
		'FontSize',15, ...
		'ForegroundColor',[1 1 1], ...
		'Position',[117 300 280 75], ...
		'String',{'DEMONSTRATION PROGRAMS';'Theory and Design for';
	    'Mechanical Measurements, 3rd ed.'}, ...
		'Style','text', ...
		'Tag','titlebox');
	ext=get(h1,'Extent');
	newsize=min(floor(15*[280 65]./ext(3:4)));
	set(h1,'FontSize',newsize);
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.6 1 1], ...
		'ForegroundColor',[0 0 0], ...
		'Position',[4 290 508 5], ...
		'Style','frame', ...
		'Tag','BarFrame');
	h1 = axes('Parent',h0, ...
		'Units','pixels', ...
		'Box','on', ...
		'Color',[0.07 0.07 0.16], ...
		'Position',[35 299 64 80], ...
		'Tag','leftax', ...
		'XColor',[0.6 1 1], ...
		'XLimMode','manual', ...
		'XTickMode','manual', ...
		'YColor',[0.6 1 1], ...
		'YLim',[-1.8 1.3], ...
		'YLimMode','manual', ...
		'YTickMode','manual');
   title('')
	x=[0:0.05:1];
	y1=sin(2*pi*x);
	y2=cos(2*pi*x)-1.1+1.3*x;
	y3=-1.2*cos(2*pi*(0.8*x+0.1))-0.7+1.1*x;
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'Tag','l1', ...
		'XData',x, ...
		'YData',y1);
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'LineStyle','-.', ...
		'Tag','l2', ...
		'XData',x, ...
		'YData',y2);
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'LineStyle','--', ...
		'Tag','l3', ...
		'XData',x, ...
		'YData',y3);
	h1 = axes('Parent',h0, ...
		'Units','pixels', ...
		'Box','on', ...
		'Color',[0.07 0.07 0.16], ...
		'Position',[415 299 64 80], ...
		'Tag','rightax', ...
		'XColor',[0.6 1 1], ...
		'XLimMode','manual', ...
		'XTickMode','manual', ...
		'YColor',[0.6 1 1], ...
		'YLim',[-1.8 1.3], ...
		'YLimMode','manual', ...
		'YTickMode','manual');
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'Tag','r1', ...
		'XData',x, ...
		'YData',y1(21:-1:1));
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'LineStyle','-.', ...
		'Tag','r2', ...
		'XData',x, ...
		'YData',y2(21:-1:1));
	h2 = line('Parent',h1, ...
		'Color',[0.6 1 1], ...
		'LineStyle','--', ...
		'Tag','r3', ...
		'XData',x, ...
		'YData',y3(21:-1:1));
	str={['FourCoef demonstrates Fourier Sine and Cosine ', ...
      'partial sums for periodic functions including ', ...
      'Square Wave (Ex. 2.3), and Rectified Sine (Ex. 2.5).']};
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[82 247 370 36], ...
		'Style','text', ...
		'FontSize',35, ...
		'String',str, ...
		'Tag','FourCoefText');
   ext=get(h1,'Extent');
   newsize=floor(360/ext(3)*35*2);
   set(h1,'FontSize',newsize)
   outstr=textwrap(h1,str);
   set(h1,'String',outstr)
%   defsize=newsize*72/get(0,'ScreenPixelsPerInch');
   set(h0,'DefaultUicontrolFontSize',newsize)
	h1 = uicontrol('Parent',h0, ...
		'Callback','fourcoef', ...
		'Position',[5 253 60 24], ...
		'String','FourCoef', ...
		'Tag','FourCoefButton');
	h1 = uicontrol('Parent',h0, ...
		'Callback','funspect', ...
		'Position',[448 215 60 24], ...
		'String','FunSpect', ...
		'Tag','FunSpectButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[62 209 370 36], ...
      'Style','text', ...
		'Tag','FunSpectText');
	str={['FunSpect shows the spectrum of samples taken from ', ...
		'periodic functions (Ex. 2.6).  Signal ', ...
		'bandwidth and sampling are limited.']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'Callback','dataspect', ...
		'Position',[5 177 60 24], ...
		'String','DataSpect', ...
		'Tag','DataSpectButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[82 171 370 36], ...
		'Style','text', ...
		'Tag','DataSpectText');
	str={['DataSpect imports real samples from a data file ', ...
		'and displays the spectrum.  Some example files are provided.']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'Callback','firstord', ...
		'Position',[448 139 60 24], ...
		'String','FirstOrd', ...
		'Tag','FirstOrdButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[62 133 370 36], ...
		'Style','text', ...
		'Tag','FirstOrdText');
	str={['FirstOrd shows the time response of a first-order ', ...
		'system to forcing functions ', ...
		'including thermal step (Ex. 3.3), and harmonic (Ex. 3.6).']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'Callback','secondord', ...
		'Position',[5 101 60 24], ...
		'String','SecondOrd', ...
		'Tag','SecondOrdButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[82 95 370 36], ...
		'Style','text', ...
		'Tag','SecondOrdText');
	str={['SecondOrd shows the time response of a second-order ', ...
		'system to forcing functions ', ...
		'including mechanical step (Ex. 3.8), and harmonic (Ex. 3.10).']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'Callback','multisys', ...
		'Position',[446 63 64 24], ...
		'String','MultiSys', ...
		'Tag','MultiSysButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[62 57 370 36], ...
		'Style','text', ...
		'Tag','MultiSysText');
   str={['MultiSys shows the time response and frequency ' ...
         'response of multiple dynamic systems linked together ', ... 
         'in series.']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'Callback','sampling', ...
		'Position',[5 25 60 24], ...
		'String','Sampling', ...
		'Tag','SamplingButton');
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[82 19 370 36], ...
		'Style','text', ...
		'Tag','SamplingText');
	str={['Sampling shows the spectrum of samples taken from a ', ...
	      'data function.  Signal bandwidth and sampling ', ...
	      'are unlimited.']};
	outstr=textwrap(h1,str);
	set(h1,'String',outstr)
	h1 = uicontrol('Parent',h0, ...
		'BackgroundColor',[0.07 0.07 0.16], ...
		'ForegroundColor',[1 1 1], ...
		'HorizontalAlignment','left', ...
		'Position',[82 1 400 19], ...
		'Style','text', ...
		'FontAngle','italic', ...
		'String',['In each function, select "Help" from the ', ...
			'function menu for complete discussion.'], ...
		'HorizontalAlignment','left', ...
		'Tag','HintText');
	b = uimenu('Parent',h0,'Label','MainSwitch','Tag','uimenu1');
	c1 = uimenu('Parent',b,'Label','Help','Enable','on','Tag','helpmenu', ...
		'Callback','mainswitch help');
	c2 = uimenu('Parent',b,'Label','Fonts','Enable','on', ...
		'Tag','fontsmenu','Callback','mainswitch fonts');
	c3 = uimenu('Parent',b,'Label','Visible','Enable','on', ...
		'Callback',['set(gcf,''Handlevisibility'',''on'');', ...
			'set(get(gcbo,''UserData''),''Enable'',''on'');', ...
			'set(gcbo,''Enable'',''off'')']);
	c4 = uimenu('Parent',b,'Label','Callback','Enable','off', ...
		'Callback',['set(gcf,''Handlevisibility'',''callback'');', ...
			'set(get(gcbo,''UserData''),''Enable'',''on'');', ...
			'set(gcbo,''Enable'',''off'')'],'UserData',c3);
	set(c3,'UserData',c4)
	set(h0,'HandleVisibility','callback')
	p=path;
	if ~size(findstr(p,homedir))
		path(path,homedir)
		disp([homedir,'  added to search path'])
	end

elseif (nargin==1 & isstr(action))		% callbacks
	switch (action)
	case 'fonts'
		disp('Select Axes Font')
		fieldnoms={'FontName','FontUnits','FontSize','FontWeight','FontAngle'};
		th=get(findobj('Parent',gcf,'Tag','leftax'),'Title');
		axfont=cell2struct(get(th,fieldnoms),fieldnoms,2);
		newvals=uisetfont(th,'Select Axes Font');
		if isstruct(newvals) 
			axfont=newvals; 
		elseif ishandle(newvals) & newvals>0
			axfont=cell2struct(get(newvals,fieldnoms),fieldnoms,2);
      end
%      axfont.FontSize=axfont.FontSize*72/get(0,'ScreenPixelsPerInch');
		disp('Select Uicontrol Font')
		bh=findobj('Parent',gcf,'Tag','BarFrame');
		uifont=cell2struct(get(bh,fieldnoms),fieldnoms,2);
		newvals=uisetfont(bh,'Select Uicontrol Font');
		if isstruct(newvals) 
			uifont=newvals; 
		elseif ishandle(newvals) & newvals>0
			uifont=cell2struct(get(newvals,fieldnoms),fieldnoms,2);
		end
%      uifont.FontSize=uifont.FontSize*72/get(0,'ScreenPixelsPerInch');
		disp('Font settings take full effect on next GUI start')
		currdir=pwd;
		homedir=pathto('mainswitch.m');
		cd(homedir)
		save fontpref axfont uifont
		cd(currdir)
	    a=figure('Units','normalized','Position',[0.2 0.2 0.75 0.65], ...
	      'Numbertitle','off','Name','Font Prefs','Tag','FontPrefsBR549');
		set(0,'ShowHiddenHandles','on')
		wh=findobj('Tag','FontPrefsBR549');
		s=length(wh);
		if (s>1)
			set(a,'Name',['Font Prefs #',num2str(s)]);
		end
		set(0,'ShowHiddenHandles','off')
		set(a,'DefaultAxesFontName',axfont.FontName, ...
			'DefaultAxesFontUnits',axfont.FontUnits, ...
			'DefaultAxesFontSize',axfont.FontSize, ...
			'DefaultAxesFontWeight',axfont.FontWeight, ...
			'DefaultAxesFontAngle',axfont.FontAngle, ...
			'DefaultUicontrolFontName',uifont.FontName, ...
			'DefaultUicontrolFontUnits',uifont.FontUnits, ...
			'DefaultUicontrolFontSize',uifont.FontSize, ...
			'DefaultUicontrolFontWeight',uifont.FontWeight, ...
			'DefaultUicontrolFontAngle',uifont.FontAngle)
		color=get(a,'Color');
		b=axes('Parent',a,'Units','normalized','XGrid','on','YGrid','on', ...
			'Position',[0.33 0.12 0.6 0.80]);
		w=2*pi*[0:100]/100;
		plot(cos(w),sin(w),'g'),	hold on
		w=2*pi*[63:87]/100;
		plot(cos(w)/1.4,sin(w)/1.4,'b')
		plot([-0.5 0.5],[0.5 0.5],'rx','MarkerSize',10)
		axstr=[axfont.FontName,' ',num2str(axfont.FontSize),' ', ...
			axfont.FontUnits];
		title(axstr),	xlabel('xlabel'),	ylabel('ylabel')
		c=uicontrol('Parent',a,'Style','text','Units','normalized', ...
		   'Position',[0.015 0.94 0.195 0.05],'BackgroundColor',color, ...
		   'String','Plain Text');
		c=uicontrol('Parent',a,'Style','popupmenu','Units','normalized', ...
		   'Position',[0.015 0.88 0.195 0.05],'BackgroundColor',color, ...
		   'String',['Popup Menu|',uifont.FontName,'|', ...
		   num2str(uifont.FontSize),'|',uifont.FontUnits],'Value',1);
		c=uicontrol('Parent',a,'Style','edit','Units','normalized', ...
		   'Position',[0.015 0.707 0.15 0.06],'String','edit text');
		c=uicontrol('Parent',a,'Style','text','Units','normalized', ...
		   'Position',[0.015 0.23 0.21 0.16],'BackgroundColor',color, ...
		   'String',{uifont.FontName;num2str(uifont.FontSize);
		   		uifont.FontUnits});
		rb=uicontrol('Parent',a,'Style','pushbutton','Units','normalized', ...
		   'Position',[0.015 0.015 0.15 0.06], ...
		   'String','Button');
	case 'help'
		helpwin('aboutmainswitch')
	otherwise
		disp(['callback ',action,' unknown'])
	end
else
	disp('argument error')
end

function dest=pathto(fname)
r=which(fname);
[tok1,r]=strtok(r,filesep);
dest=tok1;
[tok2,r]=strtok(r,filesep);
while ~isempty(r)
	tok1=tok2;
	dest=[dest,filesep,tok1];
	[tok2,r]=strtok(r,filesep);
end
