Subversion Repositories Filer-Free

Rev

Rev 34 | Rev 38 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 gdshaw@RISCPKG.ORG 1
// This file is part of the free Filer module for RISC OS.
2
// Copyright  2007 Graham Shaw.
3
// Redistribution and modification are permitted under the terms of the
4
// GNU General Public License (version 2 or any later version).
5
 
6
#include <cstring>
7
#include <cstdio>
8
 
9
#include "oslib/fileraction.h"
10
#include "oslib/osfscontrol.h"
11
 
12
#include "filer_options.h"
13
#include "filer_window.h"
14
#include "filer_menu.h"
15
 
16
namespace {
17
 
18
/** The maximum length of a leafname. */
19
const size_t max_name_length=256;
20
 
21
}; /* anonymous namespace */
22
 
23
filer_menu::filer_menu(application* app):
24
        menu(app,"Filer",9),
25
        _owner(0),
26
        _object(0),
27
        _display_menu(app,"Display",8),
28
        _rename_menu(app,"Rename",1),
29
        _access_menu(app,"Access",5),
30
        _find_menu(app,"Find",1),
31
        _filetype_menu(app,"Set type",1),
32
        _selection_menu(app,"File",11),
33
        _options_menu(app,"Options",6),
34
        _rename_leafname(0),
35
        _find_pattern(0),
36
        _filetype_field(0),
37
        _selection_text(new char[12+max_name_length]),
38
        _layout_count(3),
39
        _layout_methods(new layout_method*[_layout_count]),
40
        _sort_count(4),
41
        _sort_methods(new sort_method*[_sort_count])
42
{
43
        _layout_methods[0]=&filer_options::layout_large();
44
        _layout_methods[1]=&filer_options::layout_small();
45
        _layout_methods[2]=&filer_options::layout_full();
46
 
47
        _sort_methods[0]=&filer_options::sort_by_name();
48
        _sort_methods[1]=&filer_options::sort_by_type();
49
        _sort_methods[2]=&filer_options::sort_by_size();
50
        _sort_methods[3]=&filer_options::sort_by_date();
51
 
52
        _display_menu[0].text("Large icons");
53
        _display_menu[1].text("Small icons");
54
        _display_menu[2].text("Full info");
55
        _display_menu[3].text("Thumbnails");
56
        _display_menu[4].text("Sort by name");
57
        _display_menu[5].text("Sort by type");
58
        _display_menu[6].text("Sort by size");
59
        _display_menu[7].text("Sort by date");
60
        _display_menu[3].separator(1);
61
 
62
        _rename_leafname=new char[256];
63
        _rename_leafname[0]=0;
64
        _rename_menu[0].text(_rename_leafname,256);
65
        _rename_menu[0].writable(1);
66
 
67
        _access_menu[0].text("Protected");
68
        _access_menu[1].text("Unprotected");
69
        _access_menu[2].text("Public");
70
        _access_menu[3].text("Private");
71
        _access_menu[4].text("Access details");
72
        _access_menu[1].separator(1);
73
        _access_menu[3].separator(1);
74
 
75
        _find_pattern=new char[256];
76
        _find_pattern[0]=0;
77
        _find_menu[0].text(_find_pattern,256);
78
        _find_menu[0].writable(1);
79
 
80
        _filetype_field=new char[16];
81
        _filetype_field[0]=0;
82
        _filetype_menu[0].text(_filetype_field,16);
83
        _filetype_menu[0].writable(1);
84
 
85
        _selection_menu[0].text("Copy");
86
        _selection_menu[1].text("Rename");
87
        _selection_menu[2].text("Delete");
88
        _selection_menu[3].text("Access");
89
        _selection_menu[4].text("Count");
90
        _selection_menu[5].text("Help");
91
        _selection_menu[6].text("Info");
92
        _selection_menu[7].text("Find");
93
        _selection_menu[8].text("Set type");
94
        _selection_menu[9].text("Stamp");
95
        _selection_menu[10].text("Share");
96
        _selection_menu[1].submenu(_rename_menu);
97
        _selection_menu[3].submenu(_access_menu);
98
        _selection_menu[7].submenu(_find_menu);
99
        _selection_menu[8].submenu(_filetype_menu);
100
 
101
        _options_menu[0].text("Confirm all");
102
        _options_menu[1].text("Confirm deletes");
103
        _options_menu[2].text("Verbose");
104
        _options_menu[3].text("Force");
105
        _options_menu[4].text("Newer");
106
        _options_menu[5].text("Faster");
107
        _options_menu[1].separator(1);
108
 
109
        *_selection_text=0;
110
        (*this)[0].text("Display");
111
        (*this)[1].text(_selection_text);
112
        (*this)[2].text("Select all");
113
        (*this)[3].text("Clear selection");
114
        (*this)[4].text("Options");
115
        (*this)[5].text("New directory");
116
        (*this)[6].text("Set work directory");
117
        (*this)[7].text("Open parent");
118
        (*this)[8].text("Refresh");
119
        (*this)[0].submenu(_display_menu);
120
        (*this)[1].submenu(_selection_menu);
121
        (*this)[4].submenu(_options_menu);
122
}
123
 
124
filer_menu::~filer_menu()
125
{
126
        delete[] _rename_leafname;
127
        delete[] _find_pattern;
128
        delete[] _filetype_field;
129
        delete[] _selection_text;
130
        delete[] _layout_methods;
131
        delete[] _sort_methods;
132
}
133
 
134
void filer_menu::handle_menu_selection(wimp_selection& block)
135
{
136
        // Ensure that handler can never be executed when the menu
137
        // has no owner.
138
        if (!_owner) return;
139
 
140
        // Get mouse button state for later use.
141
        // (Do this at an early stage in case it changes.)
142
        wimp_pointer info;
143
        xwimp_get_pointer_info(&info);
144
 
36 gdshaw@RISCPKG.ORG 145
        // Get copy of filer options.
146
        // (This needs to be a copy so that it can be modified,
147
        // and so that the filer window can see what has changed.)
148
        filer_options options(_owner->options());
149
 
32 gdshaw@RISCPKG.ORG 150
        switch (block.items[0])
151
        {
36 gdshaw@RISCPKG.ORG 152
        case 0: /* display */
153
                switch (block.items[1])
154
                {
155
                case 0:
156
                case 1:
157
                case 2:
158
                        options.layout(*_layout_methods[block.items[1]]);
159
                        _owner->options(options);
160
                        update_layout();
161
                        break;
162
                case 4:
163
                case 5:
164
                case 6:
165
                case 7:
166
                        options.sort(*_sort_methods[block.items[1]-4]);
167
                        _owner->options(options);
168
                        update_sort();
169
                        break;
170
                }
171
                break;
33 gdshaw@RISCPKG.ORG 172
        case 2: /* select all */
32 gdshaw@RISCPKG.ORG 173
                _owner->select_all(1);
174
                break;
33 gdshaw@RISCPKG.ORG 175
        case 3: /* clear selection */
32 gdshaw@RISCPKG.ORG 176
                _owner->select_all(0);
177
                break;
33 gdshaw@RISCPKG.ORG 178
        case 7: /* open parent */
179
                {
180
                        os_coord offset;
181
                        offset.x=-20;
182
                        offset.y=32;
183
                        _owner->open_parent(offset);
184
                }
185
                break;
34 gdshaw@RISCPKG.ORG 186
        case 8: /* refresh */
187
                _owner->refresh();
188
                break;
32 gdshaw@RISCPKG.ORG 189
        }
190
 
31 gdshaw@RISCPKG.ORG 191
        if (info.buttons&1)
192
        {
193
                // If selection made with adjust click then re-open menu.
194
                menu::show(info);
195
        }
196
        else
197
        {
198
                // Otherwise, allow menu to close but inform filer window.
199
                if (_owner) _owner->handle_menus_deleted();
200
        }
201
}
202
 
203
void filer_menu::handle_menus_deleted()
204
{
205
        // If menu is closed, other than because of a selection, then
206
        // inform filer window.
207
        if (_owner) _owner->handle_menus_deleted();
208
}
209
 
210
void filer_menu::show(wimp_pointer& block)
211
{
212
        menu::show(block);
213
}
214
 
215
void filer_menu::update_layout()
216
{
217
        // Iterate over the list of layout methods.
218
        // Tick the one which matches the window, untick the others.
219
        layout_method& method=_owner->options().layout();
220
        for (unsigned int i=0;i!=_layout_count;++i)
221
                _display_menu[i].tick(_layout_methods[i]==&method);
222
}
223
 
224
void filer_menu::update_sort()
225
{
226
        sort_method& method=_owner->options().sort();
227
        for (unsigned int i=0;i!=_sort_count;++i)
228
                _display_menu[4+i].tick(_sort_methods[i]==&method);
229
}
230
 
231
void filer_menu::update_selection()
232
{
233
        // Fetch selection and temporary selection.
234
        directory& objects=_owner->selection();
235
        unsigned int temp_selection=_owner->temp_selection();
236
 
237
        // Iterate over selection, counting objects and distinct filetypes
238
        // (in the latter case, distinguishing only between 0, 1 and some).
239
        // If there is only one selected object then record its identity.
240
        // Similarly if there is only one distinct filetype.
241
        osgbpb_info* object=0;
242
        unsigned int count=0;
243
        bits filetype_last=(bits)-1;
244
        int filetype_count=0;
245
        for (int i=0,i_end=objects.size();i!=i_end;++i)
246
        {
247
                if (objects[i].selected())
248
                {
249
                        // Count selected objects.
250
                        osgbpb_info& info=*objects[i];
251
                        switch (info.obj_type)
252
                        {
253
                        case fileswitch_IS_FILE:
254
                        case fileswitch_IS_DIR:
255
                        case fileswitch_IS_IMAGE:
256
                                object=&info;
257
                                ++count;
258
                                break;
259
                        }
260
 
261
                        // Count distinct filetypes.
262
                        // In fact what is recorded is the number of times the filetype
263
                        // changes whilst iterating through the selection, which is
264
                        // useful enough but easier to compute.
265
                        bits filetype=(bits)-1;
266
                        xosfscontrol_info_to_file_type(info.name,info.load_addr,
267
                                info.exec_addr,info.size,info.attr,info.obj_type,&filetype);
268
                        if (filetype!=filetype_last)
269
                        {
270
                                filetype_last=filetype;
271
                                ++filetype_count;
272
                        }
273
                }
274
        }
275
 
276
        // Determine how many objects were counted because they are part of
277
        // a temporary selection.  (This will be either 0 or 1.)
278
        unsigned int temp_count=0;
279
        if ((temp_selection!=directory::npos)&&
280
                (objects[temp_selection].selected())) ++temp_count;
281
 
282
        // Update text of menu entry leading to selection submenu.
283
        if (count==0)
284
        {
285
                // No objects selected.
286
                _object=0;
287
                std::sprintf(_selection_text,"File ''");
288
        }
289
        else if (count==1)
290
        {
291
                // One object selected.
292
                // Text depends on whether it is a file, directory, image file,
293
                // or application directory.
294
                _object=object;
295
                const char* fmt="";
296
                switch (object->obj_type)
297
                {
298
                case fileswitch_IS_FILE:
299
                        fmt="File '%.*s'";
300
                        break;
301
                case fileswitch_IS_IMAGE:
302
                        if (object->name[0]=='!') fmt="App. '%.*s'";
303
                        else fmt="Image '%.*s'";
304
                        break;
305
                case fileswitch_IS_DIR:
306
                        if (object->name[0]=='!') fmt="App. '%.*s'";
307
                        else fmt="Dir. '%.*s'";
308
                        break;
309
                }
310
                std::sprintf(_selection_text,fmt,max_name_length,object->name);
311
        }
312
        else
313
        {
314
                // More than one object selected.
315
                _object=0;
316
                std::sprintf(_selection_text,"Selection");
317
        }
318
 
319
        // Disable selection submenu if nothing selected.
320
        // Disable select all if everything already selected.
321
        // Disable clear selection if nothing selected.
322
        // (Disregard any temporary selection.)
323
        (*this)[1].disabled(count==0);
324
        (*this)[2].disabled(count-temp_count==objects.size());
325
        (*this)[3].disabled(count-temp_count==0);
326
 
327
        // Disable copy and rename options unless exactly one object selected.
328
        _selection_menu[0].disabled(count!=1);
329
        _selection_menu[1].disabled(count!=1);
330
 
331
        // Disable help option unless exactly one application selected.
332
        _selection_menu[5].disabled(!(count==1&&
333
                (object->obj_type&fileswitch_IS_DIR)&&object->name[0]=='!'));
334
 
335
        // Disable info option unless exactly one object selected.
336
        _selection_menu[6].disabled(count!=1);
337
 
338
        // Populate filetype field.
339
        if ((filetype_count==1)&&(filetype_last<0x1000))
340
        {
341
                union
342
                {
343
                        bits words[2];
344
                        char chars[9];
345
                };
346
                xosfscontrol_read_file_type(filetype_last,&words[0],&words[1]);
347
                int i=8;
348
                while (i&&chars[i-1]==' ') --i;
349
                chars[i]=0;
350
                std::strcpy(_filetype_field,chars);
351
        }
352
        else _filetype_field[0]=0;
353
}
354
 
355
void filer_menu::update_pathname()
356
{
357
        // Disable open parent option if already at root.
358
        char* pathname=(char*)_owner->pathname();
359
        char* lastdot=strrchr(pathname,'.');
360
        int isroot=!(lastdot&&strcmp(lastdot+1,"$"));
361
        (*this)[7].disabled(isroot);
362
}
363
 
364
void filer_menu::update(filer_window* owner)
365
{
366
        _owner=owner;
367
        if (_owner)
368
        {
369
                update_layout();
370
                update_sort();
371
                update_selection();
372
                update_pathname();
373
        }
374
}