aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gs/examples/file_dialog.erl
blob: 1dffc5b009b71ab9e7eef6a9a8145a1e7e75613c (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
%% ------------------------------------------------------------
%% File Selection Dialog
%% ------------------------------------------------------------

-module(file_dialog).
-compile([{nowarn_deprecated_function,{gs,config,2}},
          {nowarn_deprecated_function,{gs,create,4}},
          {nowarn_deprecated_function,{gs,label,2}},
          {nowarn_deprecated_function,{gs,read,2}},
          {nowarn_deprecated_function,{gs,start,0}}]).

-export([start/0,start/1,start/2,fs_init/3]).

-include_lib("kernel/include/file.hrl").


%% ----- File Selection ----
start() ->
    {ok,Dir}=file:get_cwd(),
    start(Dir,[]).

start(Dir) ->
    start(Dir,[]).

start(Dir,File) ->
    Dir0 = case lists:last(Dir) of
	$/ -> Dir;
	_  -> lists:append(Dir,"/")
    end,
    Pid=spawn(file_dialog,fs_init,[Dir0,File,self()]),	
    receive
	{file_dialog,Pid,Result} -> Result
    end.


%% ------------------------------------------------------------
fs_init(Dir,File,Owner) ->
    S=gs:start(),
    gs:create(window,win,S,[{width,250},{height,265},{title,"File Dialog"},
			    {configure,true}]),
    gs:create(label,label,win,[{y,0},{width,250},{label, {text,Dir}}]),
    gs:label(win,[{width,50},{y,30},{x,5},{label, {text,"File:"}}]),
    gs:create(entry,entry,win,[{y,30},{width,190},{x,55},
			{keypress,true},{focus,true}]),
    gs:create(listbox,lb,win,[{x,5},{y,60},{width,160},{height,199},
			   {vscroll,right},{click,true},{doubleclick,true}]),
    gs:create(button,ok,win,[{label, {text,"OK"}},{width,40},{x,185},{y,170}]),
    gs:create(button,cancel,win,[{label, {text,"Cancel"}},{x,175},{y,220},{width,65}]),
    Items=refresh(Dir),
    %% --- select File if it's given ---
    case index_member(File,Items) of
	{ok,Index} ->
	    gs:config(lb,{selection,clear}),
	    gs:config(lb,{selection,Index});
	_ -> true
    end,
    gs:config(win,{map,true}),
    fs_loop(Dir,Owner).

fs_loop(Dir,Owner) ->
    receive
	{gs,ok,click,_,_} ->
	    entered_name(Dir,Owner);
	{gs,cancel,click,_,_} ->
	    Owner ! {file_dialog,self(),cancel};
	{gs,entry,keypress,_,['Return'|_]} ->
	    entered_name(Dir,Owner);
	{gs,entry,keypress,_,[_Keysym|_]} ->
	    fs_loop(Dir,Owner);
	{gs,lb,click,_,_} ->
	    clicked(Dir,Owner);
	{gs,lb,doubleclick,_,_} ->
	    double_clicked(Dir,Owner);
	{gs,win,configure,_,[250,265|_]} -> % already got that size
	    fs_loop(Dir,Owner);
	{gs,win,configure,_,_} ->
	    gs:config(win,[{geometry,{250,265}}]),
	    fs_loop(Dir,Owner);
	stop ->
	    exit(normal);
	{gs,_,destroy,_,_} -> 
	    Owner ! {file_dialog,self(),cancel};
	X ->
	    io:format("file_dialog: got other: ~w.~n",[X]),
	    fs_loop(Dir,Owner)
    end.



refresh(Dir) ->
    gs:config(lb,clear),
    gs:config(label,{label, {text,Dir}}),
    gs:config(entry,{text,""}),
    Items=["../"|get_files(Dir)],
    gs:config(lb,[{items,Items}]),
    Items.

    
entered_name(Dir,Owner) ->
    File=gs:read(entry,text),
    case check_file(Dir,File) of
	{file,Dir2,File2} ->
	    Owner ! {file_dialog,self(),{ok,Dir2,File2}};
	{dir,Dir2} ->
	    refresh(Dir2),
	    fs_loop(Dir2,Owner);
	{error,no_file} ->
	    double_clicked(Dir,Owner);
	_ ->
	    fs_loop(Dir,Owner)
    end.


clicked(Dir,Owner) ->
    [Idx|_]=gs:read(lb,selection),
    File=gs:read(lb,{get,Idx}),
    case lists:last(File) of
	$/ -> %it's a dir
	    true;
	_ -> % it's a file
	    gs:config(entry,{text,File})
    end,
    fs_loop(Dir,Owner).


double_clicked(Dir,Owner) ->
    case gs:read(lb,selection) of
	[0] -> % up one dir
	    NewDir=up_one_dir(Dir),
	    refresh(NewDir),
	    fs_loop(NewDir,Owner);
	[] ->
	    fs_loop(Dir,Owner);
	[Idx] ->
	    File=gs:read(lb,{get,Idx}),
	    case lists:last(File) of
		$/ -> % down a dir
		    NewDir=lists:append(Dir,File),
		    refresh(NewDir),
		    fs_loop(NewDir,Owner);
		_ -> % done
		    Owner!{file_dialog,self(),{ok,Dir,File}}
	    end
    end.


%% checks if a file exists
%% returns {file,Dir,File}
%%         {dir,Dir}
%%    or   {error,What}
check_file(Dir,File) ->
    case catch lists:last(File) of
	$/ -> % File is a Dir
	    NewDir = case File of
			 [$/|_] -> %absolute path
			     File;
			 _ -> %relative path
			     lists:append(Dir,File)
	    end,
	    case file:list_dir(NewDir) of
		{ok,_} -> {dir,NewDir};
		_      -> {error,bad_dir}
	    end;
	{'EXIT',_Why} -> {error,no_file};
	_ ->
	    Words=string:tokens(File,[$/,$\\]),
	    NewFile=lists:last(Words),
	    NewDir = case File of
		[$/|_] -> %absolute path
		    up_one_dir(File);
		_ -> %relative path
		    case up_one_dir(File) of
			[$/]        -> Dir;
			[$/|SubDir] -> lists:flatten([Dir,SubDir,$/])
		    end
	    end,
	    case file:read_file_info(lists:append(NewDir,NewFile)) of
		{ok,_} -> 
		    {file,NewDir,NewFile};
		_ -> 
		    {error,bad_file}
	    end
    end.

	        
get_files(Dir) -> 
    {ok,Files} = file:list_dir(Dir),
    add_slash(Dir,lists:sort(Files)).
 
add_slash(_,[]) -> [];
add_slash(Dir,[H|T]) ->
    case file:read_file_info(lists:append(Dir,[$/|H])) of
	{ok,FI} when FI#file_info.type==directory ->
            [lists:append(H,"/")|add_slash(Dir,T)];
        _ ->
	    [H|add_slash(Dir,T)]
    end.


%filter([H|T]) ->
%    case lists:last(H) of
%	$/ -> [H|filter(T)];
%	_ ->
%	    Len =length(H),
%	    if Len>4 ->
%		    case lists:nthtail(Len-4,H) of
%			".erl" -> [H|filter(T)];
%			_ -> filter(T)
%		    end;
%		true -> filter(T)
%	    end
%    end;
%filter([]) ->    
%    [].
	
    
%% like member but also returns index
index_member(Item,List) ->
    i_m(0,Item,List).

i_m(N,Item,[Item|_List]) ->
    {ok,N};
i_m(N,Item,[_|List]) ->
    i_m(N+1,Item,List);
i_m(_N,_Item,[]) ->
    false.

up_one_dir(Dir) ->
    L =string:tokens(Dir,[$/,$\\]),
    lists:flatten(rem_last(L)).

rem_last([_Last]) ->
    [$/];
rem_last([Head|Tail]) ->
    [$/,Head|rem_last(Tail)];
rem_last([]) ->
    [$/].
		  
%% ----------------------------------------
%% done