Subversion Repositories Filer-Free

Rev

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