rri_file_menu.m 4.1 KB

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