-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrri_file_menu.m
executable file
·179 lines (138 loc) · 4.06 KB
/
rri_file_menu.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
% Imbed a file menu to any figure. If file menu exist, it will append
% to the existing file menu. This file menu includes: Copy to clipboard,
% print, save, close etc.
%
% Usage: rri_file_menu(fig);
%
% rri_file_menu(fig,0) means no 'Close' menu.
%
% - Jimmy Shen ([email protected])
%
%--------------------------------------------------------------------
function rri_file_menu(action, varargin)
if isnumeric(action)
fig = action;
action = 'init';
end
% clear the message line,
%
h = findobj(gcf,'Tag','MessageLine');
set(h,'String','');
if ~strcmp(action, 'init')
set(gcbf, 'InvertHardcopy','off');
% set(gcbf, 'PaperPositionMode','auto');
end
switch action
case {'init'}
if nargin > 1
init(fig, 1); % no 'close' menu
else
init(fig, 0);
end
case {'print_fig'}
printdlg(gcbf);
case {'copy_fig'}
copy_fig;
case {'export_fig'}
export_fig;
end
return % rri_file_menu
%------------------------------------------------
%
% Create (or append) File menu
%
function init(fig, no_close)
% search for file menu
%
h_file = [];
menuitems = findobj(fig, 'type', 'uimenu');
for i=1:length(menuitems)
filelabel = get(menuitems(i),'label');
if strcmpi(strrep(filelabel, '&', ''), 'file')
h_file = menuitems(i);
break;
end
end
set(fig, 'menubar', 'none');
if isempty(h_file)
if isempty(menuitems)
h_file = uimenu('parent', fig, 'label', 'File');
else
h_file = uimenu('parent', fig, 'label', 'Copy Figure');
end
h1 = uimenu('parent', h_file, ...
'callback','rri_file_menu(''copy_fig'');', ...
'label','Copy to Clipboard');
else
h1 = uimenu('parent', h_file, ...
'callback','rri_file_menu(''copy_fig'');', ...
'separator','on', ...
'label','Copy to Clipboard');
end
h2 = uimenu(h_file, ...
'callback','pagesetupdlg(gcbf);', ...
'label','Page Setup...');
h2 = uimenu(h_file, ...
'callback','printpreview(gcbf);', ...
'label','Print Preview...');
h2 = uimenu('parent', h_file, ...
'callback','printdlg(gcbf);', ...
'label','Print Figure ...');
h2 = uimenu('parent', h_file, ...
'callback','rri_file_menu(''export_fig'');', ...
'label','Save Figure ...');
arch = computer;
if ~strcmpi(arch(1:2),'PC')
set(h1, 'enable', 'off');
end
if ~no_close
h1 = uimenu('parent', h_file, ...
'callback','close(gcbf);', ...
'separator','on', ...
'label','Close');
end
return; % init
%------------------------------------------------
%
% Copy to clipboard
%
function copy_fig
arch = computer;
if(~strcmpi(arch(1:2),'PC'))
error('copy to clipboard can only be used under MS Windows');
return;
end
print -noui -dbitmap;
return % copy_fig
%------------------------------------------------
%
% Save as an image file
%
function export_fig
curr = pwd;
if isempty(curr)
curr = filesep;
end
[selected_file, selected_path] = rri_select_file(curr,'Save As');
if isempty(selected_file) | isempty(selected_path)
return;
end
filename = [selected_path selected_file];
if(exist(filename,'file')==2) % file exist
dlg_title = 'Confirm File Overwrite';
msg = ['File ',filename,' exist. Are you sure you want to overwrite it?'];
response = questdlg(msg,dlg_title,'Yes','No','Yes');
if(strcmp(response,'No'))
return;
end
end
old_pointer = get(gcbf,'pointer');
set(gcbf,'pointer','watch');
try
saveas(gcbf,filename);
catch
msg = 'ERROR: Cannot save file';
set(findobj(gcf,'Tag','MessageLine'),'String',msg);
end
set(gcbf,'pointer',old_pointer);
return; % export_fig