function varargout = advexpfigui(varargin)
% ADVEXPFIGUI Raises a dialog, detects Matlab figures and enables to select
% them for export to various output formats.
%
% Primoz Cermelj, 07.10.2004
% (c) Primoz Cermelj, primoz.cermelj@email.si
%
% $Revision: 2.2.0 $ $Date: 27.02.2007$
%--------------------------------------------------------------------------
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @advexpfigui_OpeningFcn, ...
'gui_OutputFcn', @advexpfigui_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 advexpfigui is made visible.
function advexpfigui_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 advexpfigui (see VARARGIN)
% Choose default command line output for advexpfigui
handles.output = hObject;
% Some global variables
handles.figs = [];
handles.expFormat = 1; % 1 = eps, 2 = eps & pdf 3 = emf 4 = jpg 5 = pdf via eps 6 = jpg via eps
handles.iniFile = 'advexpfigui.ini';
handles.versionNumber = '2.1.2';
handles.releaseDate = '22.08.2007';
handles.authorName = 'Primoz Cermelj';
handles.authorEmail = 'primoz.cermelj@email.si';
handles.hColor = [1 1 0.502]; % highlight color
handles.h_default = 9;
handles.w_default = 15;
% Ini-file reading and setting the variables
handles.iniFile = which('advexpfigui');
handles.iniFile = fileparts(handles.iniFile);
handles.iniFile = fullfile(handles.iniFile,'advexpfigui.ini');
if ~exist(handles.iniFile)
inifile(handles.iniFile,'new');
end
readSett = inifile(handles.iniFile,'read',...
{'','','w','s','15';...
'','','h','s','9';...
'','','exportdir','s',cd;...
'','','filename','s','test';...
'','','expformat','i',1;...
'','','pos','i',[10 10];...
'','','gspath','s',cd;...
'','','preservesize','i',1;...
'custom','','h_default','i',handles.h_default;...
'custom','','w_default','i',handles.w_default;...
'','','uioff','i',0;...
'','','colored','i',0;...
'','','renderer','i',1});
set(handles.edW,'String',readSett{1});
set(handles.edH,'String',readSett{2});
set(handles.edExportDir,'String',readSett{3});
set(handles.edFileName,'String',readSett{4});
handles.expFormat = readSett{5};
pos = get(handles.mainFigure,'Position');
pos(1) = readSett{6}(1);
pos(2) = readSett{6}(2);
set(handles.mainFigure,'Position',pos);
set(handles.edGS,'String',readSett{7});
if readSett{8}; set(handles.chkPreserveSize,'Value',1); end;
handles.h_default = readSett{9};
handles.w_default = readSett{10};
if readSett{11}; set(handles.chkUIOff,'Value',1); end;
if readSett{12}; set(handles.chkColoredEps,'Value',1); end;
rendNum = readSett{13};
rendNum = ~(rendNum>0 & rendNum < 5) + (rendNum>0 & rendNum < 5)*rendNum;
set(handles.popRenderer,'Value',rendNum);
% Some additional GUI settings
set(handles.edFileName,'BackGroundColor',handles.hColor);
%
c_refresh_list(hObject,handles);
switch handles.expFormat
case 1, set(handles.rdEps,'Value',1);
case 2, set(handles.rdEpsAndPdf,'Value',1);
case 3, set(handles.rdEmf,'Value',1);
case 4, set(handles.rdJpg,'Value',1);
case 5, set(handles.rdPdfviaeps,'Value',1);
otherwise set(handles.rdJpgviaeps,'Value',1);
end
set(handles.mainFigure,'Visible','on');
iconFile = fullfile(fileparts(which('advexpfigui')),'advexpfigui.ico');
changeicon(handles.mainFigure,iconFile,0);
c_refresh_gui(hObject,handles);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes advexpfigui wait for user response (see UIRESUME)
% uiwait(handles.mainFigure);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function varargout = advexpfigui_OutputFcn(hObject, eventdata, handles)
% Outputs from this function are returned to the command line.
varargout{1} = handles.output;
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function c_refresh_gui(hObject,handles)
% Refreshes the GUI and set/change...
if get(handles.chkPreserveSize,'Value');
set(handles.edW,'Enable','off');
set(handles.edH,'Enable','off');
set(handles.edW,'BackGroundColor','white');
set(handles.edH,'BackGroundColor','white');
else
set(handles.edW,'Enable','on');
set(handles.edH,'Enable','on');
set(handles.edW,'BackGroundColor',handles.hColor);
set(handles.edH,'BackGroundColor',handles.hColor);
end
if get(handles.rdEps,'Value') || get(handles.rdEpsAndPdf,'Value') || get(handles.rdPdfviaeps,'Value') ||...
get(handles.rdJpgviaeps,'Value')
set(handles.chkColoredEps,'Enable','on');
else
set(handles.chkColoredEps,'Enable','off');
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function c_rd_Callback(hObject, eventdata, handles)
% When radio buttons are clicked
c_refresh_gui(hObject,handles)
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function c_refresh_list(hObject,handles)
% Detects all the figures, returns their handles, and displays them
% in the list
handles.figs = findobj(0,'Type','figure');
handles.figs = sort(handles.figs);
handles.figs(find(handles.figs==handles.mainFigure)) = []; % remove this figure
str = [];
for ii=1:length(handles.figs)
str{ii,1} = [num2str(handles.figs(ii)) ': ' get(handles.figs(ii),'Name')];
end
if find(length(handles.figs))
set(handles.btnExport,'Enable','on');
else
set(handles.btnExport,'Enable','off');
end
value = get(handles.listFigures,'Value');
set(handles.listFigures,'String',str);
if isempty(value)
set(handles.listFigures,'Value',1);
elseif value > length(handles.figs)
if length(handles.figs) < 1
set(handles.listFigures,'Value',1);
else
set(handles.listFigures,'Value',length(handles.figs));
end
end
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function btnExport_Callback(hObject, eventdata, handles)
c_refresh_list(hObject,handles);
handles = guidata(hObject);
figNum = get(handles.listFigures,'Value');
if figNum <= length(handles.figs)
try
w = []; h = [];
if ~get(handles.chkPreserveSize,'Value')
w = str2num(get(handles.edW,'String'));
h = str2num(get(handles.edH,'String'));
end
if ~get(handles.chkUIOff,'Value')
uiOn = 'on';
else
uiOn = 'off';
end
if get(handles.chkColoredEps,'Value')
coloredEps = 1;
formatAdd = 'c2';
else
coloredEps = 0;
formatAdd = '2';
end
fName = fullfile(get(handles.edExportDir,'String'),get(handles.edFileName,'String'));
ind = strfind(fName,'.');
if isempty(ind)
fName = fName;
else
fName = fName(1:ind(end)-1);
end
%
fh = handles.figs(figNum);
renderer = get(handles.popRenderer,'String');
num = get(handles.popRenderer,'Value');
if num > 1
renderer = renderer{num};
else
renderer = [];
end
gsFile = get(handles.edGS,'String');
if get(handles.rdEps,'Value')
fName = [fName '.eps'];
if c_canconvert(fName)
[result,msg] = advexpfig(fh,fName,['-deps' formatAdd],'w',w,'h',h,'ui',uiOn,'renderer',renderer);
end
elseif get(handles.rdEpsAndPdf,'Value')
fName = [fName '.eps'];
if c_canconvert(fName, gsFile)
[result,msg] = advexpfig(fh,fName,['-deps' formatAdd],'w',w,'h',h,'ui',uiOn,'renderer',renderer);
[result,msg] = eps2xxx(fName,{'pdf'},'fullGsPath', gsFile);
end
elseif get(handles.rdEmf,'Value')
fName = [fName '.emf'];
if c_canconvert(fName)
[result,msg] = advexpfig(fh,fName,'-dmeta','w',w,'h',h,'ui',uiOn,'renderer',renderer);
end
elseif get(handles.rdJpg,'Value')
fName = [fName '.jpg'];
if c_canconvert(fName)
[result,msg] = advexpfig(fh,fName,'-djpeg','w',w,'h',h,'ui',uiOn,'renderer',renderer);
end
elseif get(handles.rdPdfviaeps,'Value')
fName = [fName '.pdf'];
tempFName = [fName '~'];
if c_canconvert(fName, gsFile)
[result,msg] = advexpfig(fh,fName,'pdf<-eps','w',w,'h',h,'ui',uiOn,'renderer',renderer,'colored',coloredEps);
end
else % jpg via eps
fName = [fName '.jpg'];
tempFName = [fName '~'];
if c_canconvert(fName, gsFile)
[result,msg] = advexpfig(fh,fName,'jpg<-eps','w',w,'h',h,'ui',uiOn,'renderer',renderer,'colored',coloredEps,'gs',get(handles.edGS,'String'));
end
end
if result < 0
warndlg(msg,'Error');
elseif result == 0
warndlg(msg,'Warning');
end
%
catch
errordlg(['Error occured: ' lasterr],'Export error');
end
end
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function res = c_canconvert(fName, gs)
% Checks if file exists and raises a confirmation dialog on what to do next
res = 0;
if ~(nargin < 2 || isempty(gs))
% Check if gs exists
if ~exist(gs,'file')
errordlg(['Ghost script executable ' gs ' could not be found!'],'Error','Modal');
return
end
end
if exist(fName,'file')
button = questdlg(['File ' fName ' already exists! Overwrite?'],'Warning','Yes','No','Yes');
if strcmp(button,'Yes')
res = 1;
end
else
res = 1;
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function listFigures_Callback(hObject, eventdata, handles)
% Executes on selection change in listFigures.
c_refresh_list(hObject,handles);
handles = guidata(hObject);
figNum = get(handles.listFigures,'Value');
if figNum <= length(handles.figs)
figure(handles.figs(figNum));
figure(handles.mainFigure);
end
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function listFigures_KeyPressFcn(hObject, eventdata, handles)
% Executes on a key press over the figures' list
% If del is pressed, delete the selected item and close the corresponding
% figure
handles = guidata(hObject);
if strcmpi(get(handles.mainFigure, 'CurrentKey'), 'delete')
figNum = get(hObject, 'Value');
if ~isempty(figNum)
close(handles.figs(figNum));
end
end
c_refresh_list(hObject, handles);
handles = guidata(hObject);
figure(handles.mainFigure);
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function btnReset_Callback(hObject, eventdata, handles)
set(handles.edW,'String',num2str(handles.w_default));
set(handles.edH,'String',num2str(handles.h_default));
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function btnExportDir_Callback(hObject, eventdata, handles)
path = uigetdir(get(handles.edExportDir,'String'),'Select path of the export dir');
if ~isempty(path) & path~=0
set(handles.edExportDir,'String',path);
end
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function mainFigure_CloseRequestFcn(hObject, eventdata, handles)
if get(handles.rdEps,'Value')
expFormat = 1;
elseif get(handles.rdEpsAndPdf,'Value')
expFormat = 2;
elseif get(handles.rdEmf,'Value')
expFormat = 3;
elseif get(handles.rdJpg,'Value')
expFormat = 4;
elseif get(handles.rdPdfviaeps,'Value')
expFormat = 5;
else
expFormat = 6;
end
pos = get(handles.mainFigure,'Position');
inifile(handles.iniFile,'write',...
{'','','w',get(handles.edW,'String');...
'','','h',get(handles.edH,'String');...
'','','exportdir',get(handles.edExportDir,'String');...
'','','filename',get(handles.edFileName,'String');...
'','','expformat',expFormat;...
'','','pos',[pos(1),pos(2)];...
'','','gspath',get(handles.edGS,'String');...
'','','preservesize',get(handles.chkPreserveSize,'Value');...
'','','uioff',get(handles.chkUIOff,'Value');...
'','','colored',get(handles.chkColoredEps,'Value');...
'','','renderer',get(handles.popRenderer,'Value')});
delete(hObject);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function btnAbout_Callback(hObject, eventdata, handles)
% Shows the about box
w = 320;
h = 150;
pos = get(handles.mainFigure,'Position');
aboutDlg = figure('Position',[pos(1)+(pos(3)-w)/2 pos(2)+(pos(4)-h)/2 w h],...
'Units','pixels',...
'MenuBar','none',...
'Color',get(0,'defaultUicontrolBackgroundColor'),...
'Name','About',...
'Resize','off',...
'NumberTitle','off',...
'WindowStyle','modal',...
'Visible','off');
try
im = get(handles.mainFigure,'UserData');
[h,w,p] = size(im);
imagePanel = axes('Box','off',...
'Visible','off',...
'Units','pixels',...
'Position',[5 35 w h]);
image(im,'Parent',imagePanel);
set(imagePanel,'xtick',[]);
set(imagePanel,'ytick',[]);
catch
end
text(110,90,'AdvExpFigUI','FontSize',20,'FontWeight','bold','Units','pixels');
text(110,58,['Version ' handles.versionNumber ' (' handles.releaseDate ')'],'Units','pixels','FontSize',8);
text(110,46,handles.authorName,'Units','pixels','FontSize',8);
text(110,8,handles.authorEmail,'Units','pixels','FontSize',8);
okBtn = uicontrol('Style','pushbutton',...
'String','OK',...
'Callback','delete(gcf)',...
'Units','pixels',...
'Position',[125 5 70 20]);
set(aboutDlg,'Visible','on');
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function btnGS_Callback(hObject, eventdata, handles)
origDir = cd;
if exist(get(handles.edGS,'String')) == 7
cd(get(handles.edGS,'String'));
end
[fileName,path] = uigetfile('*.*','Select path for GS executable',get(handles.edGS,'String'));
if ~isempty(fileName) & fileName~=0
set(handles.edGS,'String',fullfile(path,fileName));
end
cd(origDir);
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% --- Executes on mouse motion over figure - except title and menu.
function mainFigure_WindowButtonMotionFcn(hObject, eventdata, handles)
c_refresh_list(hObject,handles);
handles = guidata(hObject);
guidata(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function chkPreserveSize_Callback(hObject, eventdata, handles)
c_refresh_gui(hObject,handles);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function listFigures_CreateFcn(hObject, eventdata, handles)
% hObject handle to listFigures (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function edExportDir_CreateFcn(hObject, eventdata, handles)
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes during object creation, after setting all properties.
function edGS_CreateFcn(hObject, eventdata, handles)
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function edW_Callback(hObject, eventdata, handles)
function edH_Callback(hObject, eventdata, handles)
function edExportDir_Callback(hObject, eventdata, handles)
function edFileName_Callback(hObject, eventdata, handles)
function edGS_Callback(hObject, eventdata, handles)
function chkUIOff_Callback(hObject, eventdata, handles)
function chkColoredEps_Callback(hObject, eventdata, handles)
% --- Executes on selection change in popRenderer.
function popRenderer_Callback(hObject, eventdata, handles)
% hObject handle to popRenderer (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 popRenderer contents as cell array
% contents{get(hObject,'Value')} returns selected item from popRenderer
% --- Executes during object creation, after setting all properties.
function popRenderer_CreateFcn(hObject, eventdata, handles)
% hObject handle to popRenderer (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