Home > vbmeg > functions > common > utility > dir_dialog_dir > dir_dialog_callback.m

dir_dialog_callback

PURPOSE ^

Callback functions for dir_dialog

SYNOPSIS ^

function varargout = dir_dialog_callback(varargin)

DESCRIPTION ^

 Callback functions for dir_dialog

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = dir_dialog_callback(varargin)
0002 % Callback functions for dir_dialog
0003 %
0004 % Copyright (C) 2011, ATR All Rights Reserved.
0005 % License : New BSD License(see VBMEG_LICENSE.txt)
0006 if ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
0007     try
0008         if nargout == 0
0009             feval(varargin{:}) % FEVAL switchyard
0010         else
0011             % There are output value.
0012             [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
0013         end
0014     catch
0015         disp(lasterr);
0016     end
0017 end
0018 
0019 % --------------------------------------------------------------------
0020 function varargout = txtInfo_Callback(h, eventdata, handles, varargin)
0021     dir_ = get(handles.txtInfo, 'String');
0022     err = false;
0023     if isdir([handles.mypath, filesep, dir_])
0024         % search selected directory
0025         dir_ = [handles.mypath, filesep, dir_];
0026     elseif isdir([pwd, filesep, dir_])
0027         % search MATLAB current directory
0028         err = true;
0029     elseif isdir(dir_)
0030         % absolute path
0031     else
0032         err = true;
0033     end
0034     if err
0035         set(handles.txtInfo, 'String', handles.mypath);
0036         return;
0037     else
0038         % get absolute path
0039         current_dir = pwd;
0040         cd(dir_);
0041         dir_ = pwd;
0042         cd(current_dir); 
0043         if strcmp(dir_(length(dir_)), filesep)
0044             dir_ = dir_(1:length(dir_)-1);
0045         end
0046         % set selected directory
0047         handles.mypath = dir_;
0048         handles = update_listbox(handles);
0049 
0050         % set drive(Windows)
0051         if ispc
0052             ix = strfind(dir_, ':');
0053             if ~isempty(ix)
0054                 drive = dir_(1:ix);
0055                 drive_list = get(handles.drive_popup, 'String');
0056                 ix_list = strmatch(drive, drive_list);
0057                 if ~isempty(ix_list)
0058                     set(handles.drive_popup, 'Value', ix_list);
0059                 end
0060             end
0061         end
0062         % save data
0063         guidata(handles.browser, handles);
0064     end
0065 
0066 % --------------------------------------------------------------------
0067 function varargout = List_Callback(h, eventdata, handles, varargin)
0068 
0069     index_selected = get(handles.List,'Value');
0070     path_name = handles.file_path{index_selected};
0071     new_path  = [handles.mypath filesep path_name];
0072 
0073     if strcmp(path_name, '.')
0074         return;
0075     elseif strcmp(path_name, '..')
0076         % get absolute path
0077         tmp = pwd;
0078         cd(new_path);
0079         new_path = pwd;
0080         cd(tmp);
0081         if strcmp(new_path(length(new_path)), filesep)
0082             new_path = new_path(1:length(new_path)-1);
0083         end
0084         handles.mypath = new_path;
0085     elseif IsdirOK(new_path) == true
0086         % mypath 1level forward
0087         handles.mypath = new_path;
0088     else
0089         % something error occured.
0090         return;
0091     end
0092 
0093     set(handles.txtInfo, 'String', handles.mypath);
0094     handles = update_listbox(handles);
0095 
0096     % save data
0097     guidata(handles.browser, handles);
0098 
0099 % --------------------------------------------------------------------
0100 function varargout = btnApply_Callback(h, eventdata, handles, varargin)
0101     handles.mypath = get(handles.txtInfo, 'String');
0102     guidata(handles.browser, handles);
0103     
0104     % This causes dropping out of waitfor in init()
0105     set(handles.browser, 'Visible', 'off'); 
0106 
0107 % --------------------------------------------------------------------
0108 function varargout = btnCancel_Callback(h, eventdata, handles, varargin)
0109     handles.mypath = [];
0110     guidata(handles.browser, handles);
0111 
0112     % This causes dropping out of waitfor in init()
0113     set(handles.browser, 'Visible', 'off');
0114 
0115 % --------------------------------------------------------------------
0116 function varargout = mkdir_button_Callback(hObject, eventdata, handles, varargin)
0117     dir_name = inputdlg('', 'input directory name');
0118     if ~isempty(dir_name)
0119         new_dir = fullfile(char(handles.mypath), char(dir_name));
0120         mkdir(new_dir);
0121         handles.mypath = new_dir;
0122         handles = update_listbox(handles);
0123         % save data
0124         guidata(handles.browser, handles);
0125     end
0126 
0127 % --------------------------------------------------------------------
0128 function varargout = drive_popup_Callback(h, eventdata, handles, varargin)
0129 
0130     val = get(handles.drive_popup, 'Value');
0131     select_list = get(handles.drive_popup, 'String');
0132     handles.mypath = select_list{val};
0133     set(handles.txtInfo, 'String', handles.mypath);
0134     handles = update_listbox(handles);
0135 
0136     % save data
0137     guidata(handles.browser, handles);
0138 
0139 % --------------------------------------------------------------------
0140 function varargout = close_function(h, eventdata, handles, varargin)
0141     btnCancel_Callback(h, eventdata, handles, varargin);
0142 
0143 % --------------------------------------------------------------------
0144 function handles = update_listbox(handles)
0145 
0146     if IsdirOK(handles.mypath) == false
0147         return;
0148     end
0149     
0150     items   = dir([handles.mypath, filesep]);
0151     n_items = max(size(items));
0152 
0153     if strcmp(handles.mypath, handles.START_DIR)
0154         root = true;
0155     else
0156         root = false;
0157     end
0158 
0159     handles.file_path = [];
0160     k=0;
0161 
0162     % set directory names to file_path{}
0163     for i = 1 : n_items
0164         if items(i).isdir
0165             dir_name = items(i).name;
0166             if root & (handles.move_mode == false) &...
0167                (strcmp(dir_name, '.') | strcmp(dir_name, '..'))
0168                 % do not add '.' and '..'
0169                 continue;
0170             else
0171                 k = k+1;
0172                 handles.file_path{k} = dir_name;
0173             end
0174         end
0175     end
0176 
0177     set(handles.List, 'String', handles.file_path, 'Value', 1);
0178     set(handles.txtInfo, 'String', handles.mypath);
0179 
0180 % --------------------------------------------------------------------
0181 function set_drive_info(handles, current_dir)
0182 
0183     drive_name = drive_search;
0184     num_drive  = length(drive_name);
0185     current_drive = 1;
0186 
0187     for k=1:num_drive
0188         if strncmp( upper(current_dir),...
0189            drive_name{k}, ...
0190            length(drive_name{k}) )
0191            current_drive = k;
0192            break;
0193         end
0194     end
0195     
0196     set(handles.drive_popup, 'String', drive_name);
0197     set(handles.drive_popup, 'Value', current_drive);
0198  
0199 % --------------------------------------------------------------------
0200 function [drive_name] = drive_search
0201 
0202     if ispc
0203         % Search drive (from C to Z)
0204         cnt = 0;
0205         for drive = [real('C'):real('Z')]
0206             str = sprintf('%c:', drive);
0207             if length( dir(str) )
0208                 % drive found
0209                 cnt = cnt+1;
0210                 drive_name{cnt} = str;
0211             end
0212         end
0213     end
0214 
0215 % --------------------------------------------------------------------
0216 function [result] = IsdirOK(path_name)
0217     items   = dir([path_name, filesep]);
0218     n_items = max(size(items));
0219     n_dir   = 0;
0220     result = false;
0221 
0222     try
0223         for j = 1 : n_items
0224             if items(j).isdir
0225                 n_dir = n_dir+1;
0226             end
0227         end
0228     catch
0229         % directory is not accessable.
0230         result = false;
0231         return;
0232     end
0233 
0234     if n_dir < 1
0235         % directory is probably abnormal.
0236         result = false;
0237     elseif n_dir == 2
0238         % only '.' and '..'
0239         result = true;
0240     else
0241         result = true;
0242     end
0243

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005