0001 function [res, o] = do_save(o, item, flags, filename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if nargin < 2
0020 error('Need item');
0021 end
0022 if nargin < 3
0023 flags = NaN;
0024 end
0025 if nargin < 4
0026 filename = NaN;
0027 end
0028
0029
0030 item_struct = get_item_struct(o, item);
0031
0032
0033 if ~isstruct(flags), flags = []; end
0034 if pr_is_nix(filename), filename = item_struct.file_name; end
0035 if pr_is_nix(filename), filename = item_struct.default_file_name; end
0036
0037 if pr_needs_save(item_struct) || isfield(flags, 'force')
0038
0039 if isfield(flags, 'ui')
0040
0041 if pr_isempty(item_struct)
0042 if isfield(flags, 'warn_empty')
0043 msgbox('Nothing to save', ...
0044 [item_struct.title ' is not set'], 'warn');
0045 end
0046 res = 0;
0047 return
0048 end
0049
0050 if isfield(flags, 'prompt')
0051 prompt = flags.prompt;
0052 else
0053 prompt = item_struct.title;
0054 end
0055 if isfield(flags, 'prompt_prefix')
0056 prompt = [flags.prompt_prefix prompt];
0057 end
0058 if isfield(flags, 'prompt_suffix')
0059 prompt = [prompt flags.prompt_suffix];
0060 end
0061 if isfield(flags, 'ync')
0062 save_yn = questdlg(['Save ' prompt '?'],...
0063 'Save', 'Yes', 'No', 'Cancel', 'Yes');
0064 if strcmp(save_yn, 'Cancel'), res = -1; return, end
0065 if strcmp(save_yn, 'No')
0066 if isfield(flags, 'no_no_save')
0067 o = set_item_param(o, item, 'has_changed', 0);
0068 end
0069 res = 0;
0070 return
0071 end
0072 end
0073 pr = ['Filename to save ' prompt];
0074 [f p] = mars_uifile('put', item_struct.filter_spec, pr, filename);
0075 if all(f==0), res = -1; return, end
0076 filename = fullfile(p, f);
0077 end
0078 savestruct(item_struct.data, filename);
0079 if item_struct.verbose
0080 fprintf('%s saved to %s\n', item_struct.title, filename);
0081 end
0082 item_struct.file_name = filename;
0083 item_struct.has_changed = 0;
0084 if item_struct.leave_as_file
0085
0086 item_struct.data = [];
0087 end
0088 o = set_item_struct(o, item, item_struct);
0089 res = 1;
0090 else
0091 res = 0;
0092 end
0093 return