NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/setdir.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function theResult = setdir(theCommand, theTitle)
2
3 % setdir -- Dialog for setting directory.
4 %  setdir (no argument) displays a dialog for
5 %   selecting the directory to be made current,
6 %   starting from the present working directory.
7 %   Existing directories are prefixed by '>' in
8 %   in the file-list.  Navigation is accomplished
9 %   through single-clicks on the controls.
10 %  setdir([], 'theTitle') sets the dialog name
11 %   to theTitle.
12 %
13 %   "Cancel" -- No directory change.
14 %   "Select ..." -- Select the named directory.
15 %   "New" -- Create a new directory.
16  
17 % Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO.
18 %  All Rights Reserved.
19 %   Disclosure without explicit written consent from the
20 %    copyright owner does not constitute publication.
21  
22 % Version of 30-Dec-1999 10:35:32.
23 % Updated    01-Jan-2000 08:34:38.
24
25 persistent result
26
27 if nargin < 1, theCommand = ''; end
28 if nargin < 2, theTitle = ''; end
29
30 if isempty(theCommand), theCommand = 'create'; end
31
32 switch lower(theCommand)
33 case 'closerequestfcn'
34         result = [];
35         theDialog = gcbf;
36         if isempty(theDialog), theDialog = gcf; end
37         delete(theDialog)
38         return
39 case 'callback'
40         theDialog = gcbf;
41         if isempty(theDialog), theDialog = gcf; end
42         switch lower(get(gcbo, 'Tag'))
43         case 'up'
44                 s = get(gcbo, 'String');
45                 v = get(gcbo, 'Value');
46                 for i = v+1:length(s)
47                         cd ..
48                 end
49                 feval(mfilename, 'update')
50         case 'down'
51                 s = get(gcbo, 'String');
52                 v = get(gcbo, 'Value');
53                 x = s{v};
54                 if x(1) == '>'
55                         cd(x(2:end))
56                         feval(mfilename, 'update')
57                 end
58         case 'cancel'
59                 result = [];
60                 delete(theDialog)
61         case 'new'
62                 try
63                         set(theDialog, 'WindowStyle', 'normal')
64                         s.NewFolderName = 'untitled folder';
65                         isModal = ~any(findstr(computer, 'pcwin'));
66                         s = guido(s, 'New Folder', isModal);
67                         set(theDialog, 'WindowStyle', 'modal')
68                         if ~isempty(s)
69                                 try
70                                         mkdir(s.NewFolderName)
71                                         cd(s.NewFolderName)
72                                 catch
73                                         disp(lasterr)
74                                 end
75                         end
76                 catch
77                 end
78                 feval(mfilename, 'update')
79                 return
80         case 'okay'
81                 f = findobj(theDialog, 'Tag', 'up');
82                 s = get(f, 'String');
83                 result = pwd;
84                 delete(theDialog)
85         otherwise
86         end
87         return
88 case 'keypressfcn'
89         theDialog = gcbf;
90         if isempty(theDialog), theDialog = gcf; end
91         theKey = lower(get(theDialog, 'CurrentCharacter'));
92         if lower(theKey) >= 'a' & lower(theKey) < 'z'
93                 h = findobj(theDialog, 'Type', 'uicontrol', 'Tag', 'down');
94                 s = get(h, 'String');
95                 theIndex = 0;
96                 for i = 1:length(s)
97                         theName = lower(s{i});
98                         if theName(1) == '>', theName(1) = ''; end
99                         if theName(1) > theKey
100                                 break
101                         elseif theName(1) < theKey
102                                 theIndex = i;
103                         else
104                                 theIndex = i;
105                                 break
106                         end
107                 end
108                 if any(theIndex)
109                         set(h, 'Value', theIndex, 'ListBoxTop', theIndex)
110                 end
111         end
112         return
113 case 'update'
114         d = dir;
115         s = [];
116         theIndex = [];
117         for i = 1:length(d)
118                 theName = d(i).name;
119                 if d(i).isdir
120                         theName = ['>' theName];
121                         if isempty(theIndex)
122                                 theIndex = i;
123                         end
124                 end
125                 s{end+1} = theName;
126         end
127         if isempty(s), s = {'(folder is empty)'}; end
128         if isempty(theIndex), theIndex = 1; end
129         p = pwd;
130         if p(1) ~= filesep, p = [filesep p]; end
131         if p(end) ~= filesep, p(end+1) = filesep; end
132         index = find(p == filesep);
133         t = [];
134         for i = 1:length(index)-1
135                 t{i} = p(index(i)+1:index(i+1)-1);
136         end
137         theDialog = gcbf;
138         if isempty(theDialog), theDialog = gcf; end
139         h = findobj(theDialog, 'Type', 'uicontrol', 'Tag', 'up');
140         set(h, 'String', t , 'Value', length(t))
141         h = findobj(theDialog, 'Type', 'uicontrol', 'Tag', 'down');
142         set(h, 'String', s, 'Value', theIndex)
143         h = findobj(theDialog, 'Type', 'uicontrol', 'Tag', 'okay');
144         set(h, 'String', ['Select ' t{end}])
145         return
146 case 'create'
147         theOldPWD = pwd;
148         if isempty(theTitle), theTitle = 'Set Directory'; end
149         theFigure = figure( ...
150                                         'Name', theTitle, ...
151                                         'NumberTitle', 'off', ...
152                                         'WindowStyle', 'modal', ...
153                                         'CloseRequestFcn', 'setdir CloseRequestFcn', ...
154                                         'KeyPressFcn', 'setdir KeyPressFcn' ...
155                                         );
156         width = 300;
157         height = 200;
158         pos = get(theFigure, 'Position');
159         pos(1) = pos(1) + 100;
160         pos(2) = pos(2)+pos(4)-height - 100;
161         pos(3) = width;
162         pos(4) = height;
163         set(theFigure, 'Position', pos)
164         h = [];
165         h(end+1) = uicontrol(theFigure, ...
166                                         'Style', 'popupmenu', ...
167                                         'String', {'-'}, ...
168                                         'Value', 1, ...
169                                         'Tag', 'up');
170         h(end+1) = uicontrol(theFigure, ...
171                                         'Style', 'listbox', ...
172                                         'FontSize', 12', ...
173                                         'FontWeight', 'bold', ...
174                                         'String', {'-'}, ...'
175                                         'Value', 1, ...
176                                         'Tag', 'down');
177         h(end+1) = uicontrol(theFigure, ...
178                                         'Style', 'pushbutton', ...
179                                         'String', 'Cancel', ...
180                                         'BackgroundColor', [10 5 5]/10, ...
181                                         'Tag', 'cancel');
182         h(end+1) = uicontrol(theFigure, ...
183                                         'Style', 'pushbutton', ...
184                                         'String', 'Okay', ...
185                                         'BackgroundColor', [5 10 5]/10, ...
186                                         'Tag', 'okay');
187         h(end+1) = uicontrol(theFigure, ...
188                                         'Style', 'pushbutton', ...
189                                         'String', 'New', ...
190                                         'BackgroundColor', [5 5 10]/10, ...
191                                         'Tag', 'new');
192
193
194         theFontName = get(0, 'DefaultUIControlFontName');
195         theFontSize = 12;
196         theFontWeight = 'bold';
197
198         set(h, ...
199                         'FontName', theFontName, ...
200                         'FontSize', theFontSize, ...
201                         'FontWeight', theFontWeight, ...
202                         'Callback', [mfilename ' Callback'])
203                        
204         x = inf;
205        
206         theLayout = [
207                                         x x 1 1 1 1 1 x x
208                                         2 2 2 2 2 2 2 2 2
209                                         2 2 2 2 2 2 2 2 2
210                                         2 2 2 2 2 2 2 2 2
211                                         2 2 2 2 2 2 2 2 2
212                                         2 2 2 2 2 2 2 2 2
213                                         3 3 4 4 4 4 4 5 5
214                                 ];
215                                
216         uilayout(h, theLayout)
217        
218         feval(mfilename, 'update')
219        
220         waitfor(theFigure)
221        
222         if isempty(result), cd(theOldPWD); end
223         if nargout > 0
224                 theResult = result;
225         else
226                 disp(pwd)
227         end
228         return
229 otherwise
230         disp(theCommand)
231         return
232 end
233
234
235 % ---------- uilayout ---------- %
236
237
238 function theResult = uilayout(theControls, theLayout, thePosition)
239
240 % uilayout -- Layout for ui controls.
241 %  uilayout(theControls, theLayout) positions theControls
242 %   according to theLayout, an array whose entries, taken
243 %   in sorted order, define the rectangular extents occupied
244 %   by each control.  TheLayout defaults to a simple vertical
245 %   arrangement of theControls.  A one-percent margin is
246 %   imposed between controls.  To define a layout region
247 %   containing no control, use Inf.
248 %  uilayout(..., thePosition) confines the controls to the
249 %   given normalized position of the figure.  This syntax
250 %   is useful for embedding controls within a frame.
251 %  uilayout (no argument) demonstrates itself.
252  
253 % Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
254 %  All Rights Reserved.
255 %   Disclosure without explicit written consent from the
256 %    copyright owner does not constitute publication.
257  
258 % Version of 18-Apr-1997 08:07:54.
259
260 if nargin < 1, theControls = 'demo'; help(mfilename), end
261
262 if strcmp(theControls, 'demo')
263    theLayout = [1 2;
264                 3 4;
265                 5 Inf;
266                 5 6;
267                 5 Inf;
268                 7 8;
269                 9 10;
270                 11 12;
271                 13 14];
272    [m, n] = size(theLayout);
273    thePos = get(0, 'DefaultUIControlPosition');
274    theSize = [n+2 m+2] .* thePos(3:4);
275    theFigure = figure('Name', 'UILayout', ...
276                       'NumberTitle', 'off', ...
277                       'Resize', 'off', ...
278                       'Units', 'pixels');
279    thePos = get(theFigure, 'Position');
280    theTop = thePos(2) + thePos(4);
281    thePos = thePos .* [1 1 0 0] + [0 0 theSize];
282    thePos(2) = theTop - (thePos(2) + thePos(4));
283    set(theFigure, 'Position', thePos);
284    theFrame = uicontrol('Style', 'frame', ...
285                         'Units', 'normalized', ...
286                         'Position', [0 0 1 1], ...
287                         'BackgroundColor', [0.5 1 1]);
288    theStyles = {'checkbox'; 'text'; ...
289                 'edit'; 'text'; ...
290                 'listbox'; 'text'; ...
291                 'popupmenu'; 'text'; ...
292                 'pushbutton'; 'text'; ...
293                 'radiobutton'; 'text'; ...
294                 'text'; 'text'};
295    theStrings = {'Anchovies?', '<-- CheckBox --', ...
296                  'Hello World!', '<-- Edit --', ...
297                  {'Now', 'Is', 'The' 'Time' 'For' 'All' 'Good', ...
298                   'Men', 'To', 'Come' 'To' 'The' 'Aid' 'Of', ...
299                   'Their' 'Country'}, ...
300                  '<-- ListBox --', ...
301                  {'Cheetah', 'Leopard', 'Lion', 'Tiger', 'Wildcat'}, ...
302                  '<-- PopupMenu --', ...
303                  'Okay', '<-- PushButton --', ...
304                  'Cream?', '<-- RadioButton --', ...
305                  'UILayout', '<-- Text --'};
306    theControls = zeros(size(theStyles));
307    for i = 1:length(theStyles)
308       theControls(i) = uicontrol('Style', theStyles{i}, ...
309                                  'String', theStrings{i}, ...
310                                  'Callback', ...
311                                  'disp(int2str(get(gcbo, ''Value'')))');
312    end
313    set(theControls(1:2:length(theControls)), 'BackGroundColor', [1 1 0.5])
314    set(theControls(2:2:length(theControls)), 'BackGroundColor', [0.5 1 1])
315    thePosition = [1 1 98 98] ./ 100;
316    uilayout(theControls, theLayout, thePosition)
317    set(theFrame, 'UserData', theControls)
318    theStyles, theLayout, thePosition
319    if nargout > 0, theResult = theFrame; end
320    return
321 end
322
323 if nargin < 2, theLayout = (1:length(theControls)).'; end
324 if nargin < 3, thePosition = [0 0 1 1]; end
325
326 a = theLayout(:);
327 a = a(finite(a));
328 a = sort(a);
329 a(diff(a) == 0) = [];
330
331 b = zeros(size(theLayout));
332
333 for k = 1:length(a)
334    b(theLayout == a(k)) = k;
335 end
336
337 [m, n] = size(theLayout);
338
339 set(theControls, 'Units', 'Normalized')
340 theMargin = [1 1 -2 -2] ./ 100;
341 for k = 1:min(length(theControls), length(a))
342    [i, j] = find(b == k);
343    xmin = (min(j) - 1) ./ n;
344    xmax = max(j) ./ n;
345    ymin = 1 - max(i) ./ m;
346    ymax = 1 - (min(i) - 1) ./ m;
347    thePos = [xmin ymin (xmax-xmin) (ymax-ymin)] + theMargin;
348 if (1)
349    thePos = thePos .* thePosition([3 4 3 4]);
350    thePos(1:2) = thePos(1:2) + thePosition(1:2);
351 end
352    set(theControls(k), 'Position', thePos);
353 end
354
355 if nargout > 0, theResult = theControls; end
356
Note: See TracBrowser for help on using the browser.