Subversion Repositories Filer-Free

Rev

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

Rev Author Line No. Line
21 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>
35 gdshaw@RISCPKG.ORG 7
#include <cstdio>
21 gdshaw@RISCPKG.ORG 8
 
35 gdshaw@RISCPKG.ORG 9
#include "oslib/osbyte.h"
42 gdshaw@RISCPKG.ORG 10
#include "oslib/osfscontrol.h"
35 gdshaw@RISCPKG.ORG 11
#include "oslib/wimp.h"
12
 
21 gdshaw@RISCPKG.ORG 13
#include "template_loader.h"
25 gdshaw@RISCPKG.ORG 14
#include "layout_method.h"
31 gdshaw@RISCPKG.ORG 15
#include "filer_menu.h"
21 gdshaw@RISCPKG.ORG 16
#include "filer_window.h"
17
#include "filer_application.h"
18
 
35 gdshaw@RISCPKG.ORG 19
namespace {
20
 
21
/** The maximum length of a leafname. */
22
const size_t max_name_length=256;
23
 
24
/** The initial horizontal offset for opening a subdirectory. */
25
const int open_xstart=20;
26
 
27
/** The initial vertical offset for opening a subdirectory. */
28
const int open_ystart=-32;
29
 
30
/** The horizontal offset between subdirectories. */
31
const int open_xstep=20;
32
 
33
/** The vertical offset between subdirectories. */
34
const int open_ystep=0;
35
 
36
/** The number of distinct subdirectory offsets. */
37
const unsigned int open_count=8;
38
 
38 gdshaw@RISCPKG.ORG 39
/** The pathname for the template file. */
40
const char* template_pathname="Resources:$.Resources.Filer.Templates";
41
 
35 gdshaw@RISCPKG.ORG 42
}; /* anonymous namespace */
43
 
21 gdshaw@RISCPKG.ORG 44
filer_window::filer_window(filer_application* app,const char* pathname,
25 gdshaw@RISCPKG.ORG 45
        const os_box& box,const filer_options& options):
21 gdshaw@RISCPKG.ORG 46
        window(app),
47
        _pathname(new char[std::strlen(pathname)+1]),
25 gdshaw@RISCPKG.ORG 48
        _options(options),
49
        _directory(options.sort()),
50
        _xcsize(68),
51
        _ycsize(68),
52
        _xccount(1),
31 gdshaw@RISCPKG.ORG 53
        _yccount(1),
35 gdshaw@RISCPKG.ORG 54
        _temp_selection(directory::npos),
55
        _auto_pos(open_xstart,open_ystart,open_xstep,open_ystep,open_count)
21 gdshaw@RISCPKG.ORG 56
{
57
        // Copy pathname into buffer owned by this object.
58
        std::strcpy(_pathname,pathname);
59
 
60
        // Load directory listing.
61
        reload();
62
 
63
        // Create window from template.
38 gdshaw@RISCPKG.ORG 64
        template_loader loader("directory",template_pathname);
21 gdshaw@RISCPKG.ORG 65
        wimp_window& wblock=loader;
66
        wblock.title_flags|=wimp_ICON_INDIRECTED;
67
        wblock.title_data.indirected_text.text=_pathname;
68
        wblock.title_data.indirected_text.validation="";
69
        wblock.title_data.indirected_text.size=strlen(_pathname);
70
        wblock.visible=box;
71
        wblock.extent.x0=0;
72
        wblock.extent.y0=-(box.y1-box.y0);
73
        wblock.extent.x1=box.x1-box.x0;
74
        wblock.extent.y1=0;
28 gdshaw@RISCPKG.ORG 75
        wblock.flags|=wimp_WINDOW_IGNORE_XEXTENT;
21 gdshaw@RISCPKG.ORG 76
        wblock.icon_count=0;
77
        create_window(wblock);
78
 
79
        // Resize extent to match actual visible area.
80
        wimp_window_state wstate;
81
        get_window_state(wstate);
82
        os_box extent;
83
        extent.x0=0;
84
        extent.y0=-(wstate.visible.y1-wstate.visible.y0);
85
        extent.x1=wstate.visible.x1-wstate.visible.x0;
86
        extent.y1=0;
87
        window::set_extent(extent);
88
 
25 gdshaw@RISCPKG.ORG 89
        // Open window, but keep it hidden from view.
21 gdshaw@RISCPKG.ORG 90
        wimp_window_info winfo;
91
        get_window_info_header(winfo);
92
        wimp_open open;
93
        open.visible.x0=winfo.visible.x0;
94
        open.visible.y0=winfo.visible.y1-(winfo.extent.y1-winfo.extent.y0);
95
        open.visible.x1=winfo.visible.x0+(winfo.extent.x1-winfo.extent.x0);
96
        open.visible.y1=winfo.visible.y1;
97
        open.visible=winfo.visible;
98
        open.xscroll=0;
99
        open.yscroll=0;
25 gdshaw@RISCPKG.ORG 100
        open.next=wimp_HIDDEN;
101
        open_window(open);
102
 
103
        // Reformat content of window.
104
        reformat();
105
 
106
        // Reopen window, now making it visible.
107
        get_window_state(wstate);
108
        open.visible=wstate.visible;
109
        open.xscroll=wstate.xscroll;
110
        open.yscroll=wstate.yscroll;
21 gdshaw@RISCPKG.ORG 111
        open.next=wimp_TOP;
112
        open_window(open);
113
}
114
 
115
filer_window::~filer_window()
116
{
117
        // Delete pathname buffer.
118
        delete[] _pathname;
119
}
120
 
121
void filer_window::handle_redraw_request(wimp_draw& block)
122
{
25 gdshaw@RISCPKG.ORG 123
        // Fetch layout.
124
        layout_method& layout=_options.layout();
125
        int xgap=layout.xgap();
126
        int ygap=layout.ygap();
127
 
21 gdshaw@RISCPKG.ORG 128
        int more=0;
129
        xwimp_redraw_window(&block,&more);
130
        while (more)
131
        {
25 gdshaw@RISCPKG.ORG 132
                // Get clipping coordinates with respect to this window.
133
                int xmin=block.clip.x0-block.box.x0+block.xscroll-xgap;
134
                int ymin=block.clip.y0-block.box.y1+block.yscroll+ygap;
135
                int xmax=block.clip.x1-block.box.x0+block.xscroll-xgap;
136
                int ymax=block.clip.y1-block.box.y1+block.yscroll+ygap;
137
 
138
                // Determine which cells are within clipping box.
139
                int xcmin=(xmin)/(_xcsize+xgap);
140
                int ycmin=(-ymax)/(_ycsize+ygap);
141
                int xcmax=(xmax-1)/(_xcsize+xgap)+1;
142
                int ycmax=(-ymin-1)/(_ycsize+ygap)+1;
143
                if (xcmin<0) xcmin=0;
144
                if (ycmin<0) ycmin=0;
145
                if (xcmax>_xccount) xcmax=_xccount;
146
                if (ycmax>_yccount) ycmax=_yccount;
147
 
148
                // Iterate over cells to be redrawn.
149
                for (int yc=ycmin;yc<ycmax;++yc)
150
                {
151
                        for (int xc=xcmin;xc<xcmax;++xc)
152
                        {
153
                                // Determine whether cell is occupied.
30 gdshaw@RISCPKG.ORG 154
                                unsigned int i=xc+yc*_xccount;
155
                                if (i<_directory.size())
25 gdshaw@RISCPKG.ORG 156
                                {
157
                                        // Redraw cell.
158
                                        os_box box;
159
                                        box.x0=xc*(_xcsize+xgap)+xgap;
160
                                        box.y0=-yc*(_ycsize+ygap)-_ycsize-ygap;
161
                                        box.x1=box.x0+_xcsize;
162
                                        box.y1=box.y0+_ycsize;
163
                                        layout.plot(box,*_directory[i],
164
                                                _directory[i].selected(),_options);
165
                                }
166
                        }
167
                }
168
 
21 gdshaw@RISCPKG.ORG 169
                xwimp_get_rectangle(&block,&more);
170
        }
171
}
172
 
28 gdshaw@RISCPKG.ORG 173
void filer_window::handle_open_request(wimp_open& block)
174
{
175
        int xccount=_xccount;
176
        int yccount=_yccount;
177
        reformat(block.visible.x1-block.visible.x0);
178
        xwimp_open_window(&block);
179
        if (_xccount!=xccount||_yccount!=yccount) force_redraw();
180
}
181
 
29 gdshaw@RISCPKG.ORG 182
void filer_window::handle_close_request(wimp_close& block)
183
{
184
        wimp_pointer pointer;
185
        xwimp_get_pointer_info(&pointer);
186
 
187
        if (pointer.buttons&wimp_CLICK_ADJUST)
188
        {
189
                os_coord offset;
190
                offset.x=0;
191
                offset.y=0;
192
                open_parent(offset);
193
        }
194
 
195
        delete this;
196
}
197
 
30 gdshaw@RISCPKG.ORG 198
void filer_window::handle_mouse_click(wimp_pointer& block)
199
{
200
        // Fetch layout.
201
        layout_method& layout=options().layout();
202
 
35 gdshaw@RISCPKG.ORG 203
        // Read shift state.
204
        int shift_state=0;
205
        xosbyte1(osbyte_VAR_KEYBOARD_STATE,0,0xff,&shift_state);
206
        int shift=(shift_state&0x08)!=0;
207
 
30 gdshaw@RISCPKG.ORG 208
        // Find cell under pointer.
209
        unsigned int index=find_cell(block.pos);
210
 
35 gdshaw@RISCPKG.ORG 211
        bool close=false;
31 gdshaw@RISCPKG.ORG 212
        if (block.buttons==wimp_CLICK_MENU)
30 gdshaw@RISCPKG.ORG 213
        {
31 gdshaw@RISCPKG.ORG 214
                // Any existing menu will be deleted by this action.
215
                handle_menus_deleted();
216
 
217
                // Count number of cells selected.
218
                int count=0;
219
                for (unsigned int i=0,i_end=_directory.size();i!=i_end;++i)
220
                {
221
                        if (_directory[i].selected()) ++count;
222
                }
223
 
224
                // If pointer is over a cell and nothing has yet been selected
225
                // then temporarily select the cell under the pointer.
226
                if (index!=directory::npos&&!_directory[index].selected()&&!count)
227
                {
228
                        _directory[index].selected(true);
229
                        _temp_selection=index;
230
                        force_redraw_cell(index);
231
                }
232
 
233
                // Update then open the menu.
234
                shared_menu().update(this);
235
                shared_menu().show(block);
236
        }
35 gdshaw@RISCPKG.ORG 237
        else if (block.buttons&(wimp_DOUBLE_ADJUST|wimp_DOUBLE_SELECT))
238
        {
239
                // If double click within a cell then run that object.
240
                if (index!=directory::npos)
241
                {
242
                        select_all(0);
243
                        close=(block.buttons&wimp_DOUBLE_ADJUST)!=0;
244
                        if (close) close_window();
245
                        run(*_directory[index],close,shift);
246
                }
247
        }
31 gdshaw@RISCPKG.ORG 248
        else if (block.buttons&(wimp_SINGLE_SELECT|wimp_SINGLE_ADJUST))
249
        {
30 gdshaw@RISCPKG.ORG 250
                // If single select click then first de-select everything.
251
                if (block.buttons&wimp_SINGLE_SELECT) select_all(0);
252
 
253
                // If click within a cell then select that cell.
254
                if (index!=directory::npos)
255
                {
256
                        _directory[index].selected(!_directory[index].selected());
257
                        force_redraw_cell(index);
258
                }
259
        }
35 gdshaw@RISCPKG.ORG 260
 
261
        // If this window has closed as a result of the mouse event
262
        // then delete it.
263
        if (close) delete this;
30 gdshaw@RISCPKG.ORG 264
}
265
 
31 gdshaw@RISCPKG.ORG 266
void filer_window::handle_menus_deleted()
267
{
268
        if (_temp_selection!=directory::npos)
269
        {
270
                _directory[_temp_selection].selected(0);
271
                force_redraw_cell(_temp_selection);
272
                _temp_selection=directory::npos;
273
        }
274
}
275
 
36 gdshaw@RISCPKG.ORG 276
void filer_window::options(const filer_options& options)
277
{
278
        // Keep track of whether window needs to be reformatted, restored
279
        // and redrawn.
280
        int need_reformat=0;
281
        int need_resort=0;
282
        int need_redraw=0;
283
 
284
        // Check whether layout method has changed.
285
        if (&options.layout()!=&_options.layout())
286
        {
287
                need_reformat=1;
288
                need_redraw=1;
289
        }
290
 
291
        // Check whether sorting method has changed.
292
        if (&options.sort()!=&_options.sort())
293
        {
294
                need_resort=1;
295
                need_redraw=1;
296
        }
297
 
298
        // Update options.
299
        _options=options;
300
 
301
        // Reformat, resort and redraw window as required.
302
        if (need_resort) _directory.sort(_options.sort());
303
        if (need_redraw) force_redraw();
304
        if (need_reformat)
305
        {
306
                reformat();
307
                if (need_redraw) force_redraw();
308
        }
309
}
310
 
31 gdshaw@RISCPKG.ORG 311
filer_menu& filer_window::shared_menu() const
312
{
313
        static filer_menu* _shared_menu=0;
314
        if (!_shared_menu)
315
        {
38 gdshaw@RISCPKG.ORG 316
                wimp_open_template(template_pathname);
31 gdshaw@RISCPKG.ORG 317
                _shared_menu=new filer_menu(parent_application());
38 gdshaw@RISCPKG.ORG 318
                wimp_close_template();
31 gdshaw@RISCPKG.ORG 319
        }
320
        return *_shared_menu;
321
}
322
 
21 gdshaw@RISCPKG.ORG 323
void filer_window::reload()
324
{
325
        static int buffer[256];
326
        _directory.load(_pathname,buffer,sizeof(buffer));
327
}
25 gdshaw@RISCPKG.ORG 328
 
329
void filer_window::reformat()
330
{
331
        wimp_window_state wstate;
332
        get_window_state(wstate);
333
        reformat(wstate.visible.x1-wstate.visible.x0);
334
}
335
 
336
void filer_window::reformat(int xsize)
337
{
338
        // Fetch layout.
339
        layout_method& layout=options().layout();
340
        int xgap=layout.xgap();
341
        int ygap=layout.ygap();
342
 
343
        // Calculate cell size.  This must be able to accommodate
344
        // - the minimum cell size for the chosen layout, and
345
        // - the actual size occupied by each file to be listed.
346
        os_coord csize=layout.min_size(_options);
347
        for (unsigned int i=0;i!=_directory.size();++i)
348
        {
349
                os_coord size=layout.size(*_directory[i],_options);
350
                if (size.x>csize.x) csize.x=size.x;
351
                if (size.y>csize.y) csize.y=size.y;
352
        }
353
 
354
        // Apply the calculated cell size.
355
        _xcsize=csize.x;
356
        _ycsize=csize.y;
357
 
358
        // Determine number of rows and columns.
359
        _xccount=xsize/(_xcsize+xgap);
360
        if (_xccount<1) _xccount=1;
361
        _yccount=(_directory.size()+_xccount-1)/_xccount;
362
        if (_yccount<1) _yccount=1;
363
 
364
        // Calculate new extent.
365
        int extent_xsize=_xccount*(_xcsize+xgap)+xgap;
366
        int extent_ysize=_yccount*(_ycsize+ygap)+ygap;
367
 
28 gdshaw@RISCPKG.ORG 368
        // Do not force the window to become narrower than the width requested,
25 gdshaw@RISCPKG.ORG 369
        // unless there are too few directory entries to justify that width.
370
        if (extent_xsize<xsize)
371
        {
372
                extent_xsize=xsize;
373
                if (((extent_xsize+_xcsize-1)/(_xcsize+xgap))>
374
                        static_cast<int>(_directory.size()))
375
                {
376
                        extent_xsize=_directory.size()*(_xcsize+xgap)+xgap;
377
                }
378
        }
379
 
380
        // Apply the calculated extent.
381
        os_box extent;
382
        extent.x0=0;
383
        extent.y0=-extent_ysize;
384
        extent.x1=extent_xsize;
385
        extent.y1=0;
386
        set_extent(extent);
387
}
29 gdshaw@RISCPKG.ORG 388
 
30 gdshaw@RISCPKG.ORG 389
unsigned int filer_window::find_cell(const os_coord& p)
390
{
391
        layout_method& layout=options().layout();
392
        int xgap=layout.xgap();
393
        int ygap=layout.ygap();
394
 
395
        wimp_window_state state;
396
        get_window_state(state);
397
        int x=p.x-state.visible.x0+state.xscroll-xgap;
398
        int y=p.y-state.visible.y1+state.yscroll+ygap;
399
 
400
        int xc=x/(_xcsize+xgap);
401
        int yc=(-y)/(_ycsize+ygap);
402
        unsigned int index=xc+yc*_xccount;
403
 
404
        x-=xc*(_xcsize+xgap);
405
        y+=yc*(_ycsize+ygap);
406
 
407
        int found=(x>=0)&&(y>=-_ycsize)&&(x<_xcsize)&&(y<0)&&
408
                (xc>=0)&&(yc>=0)&&(xc<_xccount)&&(yc<_yccount)&&
409
                (index<_directory.size());
410
 
411
        return (found)?index:directory::npos;
412
}
413
 
414
void filer_window::force_redraw_cell(unsigned int index)
415
{
416
        layout_method& layout=options().layout();
417
        int xgap=layout.xgap();
418
        int ygap=layout.ygap();
419
 
420
        int xc=index%_xccount;
421
        int yc=index/_xccount;
422
 
423
        os_box box;
424
        box.x0=xgap+xc*(_xcsize+xgap);
425
        box.y0=-(yc+1)*(_ycsize+ygap);
426
        box.x1=box.x0+_xcsize;
427
        box.y1=box.y0+_ycsize;
428
        force_redraw(box);
429
}
430
 
34 gdshaw@RISCPKG.ORG 431
void filer_window::refresh()
432
{
433
        reload();
434
        reformat();
435
        force_redraw();
436
}
437
 
30 gdshaw@RISCPKG.ORG 438
void filer_window::select_all(int selected)
439
{
440
        layout_method& layout=options().layout();
441
        int xgap=layout.xgap();
442
        int ygap=layout.ygap();
443
 
444
        int xc_min=_xccount;
445
        int yc_min=_yccount;
446
        int xc_max=0;
447
        int yc_max=0;
448
 
449
        for (unsigned int i=0;i!=_directory.size();++i)
450
        {
451
                if (_directory[i].selected()!=selected)
452
                {
453
                        _directory[i].selected(selected);
454
 
455
                        int xc=i%_xccount;
456
                        int yc=i/_xccount;
457
                        if (xc<xc_min) xc_min=xc;
458
                        if (yc<yc_min) yc_min=yc;
459
                        if (xc+1>xc_max) xc_max=xc+1;
460
                        if (yc+1>yc_max) yc_max=yc+1;
461
                }
462
        }
463
 
464
        if ((xc_max>xc_min)&&(yc_max>yc_min))
465
        {
466
                os_box box;
467
                box.x0=xgap+xc_min*(_xcsize+xgap);
468
                box.y0=-yc_max*(_ycsize+ygap);
469
                box.x1=xc_max*(_xcsize+xgap);
470
                box.y1=-ygap-yc_min*(_ycsize+ygap);
471
                force_redraw(box);
472
        }
31 gdshaw@RISCPKG.ORG 473
 
474
        _temp_selection=directory::npos;
30 gdshaw@RISCPKG.ORG 475
}
476
 
29 gdshaw@RISCPKG.ORG 477
void filer_window::open_parent(const os_coord& offset) const
478
{
479
        // Determine whether there is a parent directory.
480
        char* lastdot=strrchr(_pathname,'.');
481
        if (lastdot&&strcmp(lastdot+1,"$"))
482
        {
483
                // Find top-left corner of this window.
484
                wimp_window_state state;
485
                get_window_state(state);
486
 
487
                // Add offset to give top-left corner of new window.
488
                os_box box;
489
                box.x0=state.visible.x0+offset.x;
490
                box.y1=state.visible.y1+offset.y;
491
                box.x1=box.x0;
492
                box.y0=box.y1;
493
 
494
                // Open new window.
495
                *lastdot=0;
496
                new filer_window((filer_application*)parent_application(),
497
                        _pathname,box,_options);
498
                *lastdot='.';
499
        }
500
}
35 gdshaw@RISCPKG.ORG 501
 
42 gdshaw@RISCPKG.ORG 502
void filer_window::set_work_directory() const
503
{
504
        osfscontrol_dir(_pathname);
505
}
506
 
35 gdshaw@RISCPKG.ORG 507
void filer_window::run(osgbpb_info& info,int close,int shift)
508
{
509
        // Need to at least partially handle shift key here, because
510
        // Filer_Run does not allow coordinates to be specified when
511
        // shift-clicking an application directory.
512
        // (... unless there is an alternative way to invoke Filer_Run?)
513
 
514
        static char buffer[12+max_name_length];
515
        if ((info.obj_type&fileswitch_IS_DIR)&&((info.name[0]!='!')||shift))
516
        {
517
                // Get dimensions of this window.
518
                wimp_window_state state;
519
                get_window_state(state);
520
 
521
                // Choose coordinates for new window.
522
                // If closing this window then open in same position,
523
                // otherwise open below and to the right.
524
                os_coord offset=_auto_pos();
525
                os_box box;
526
                box.x0=state.visible.x0+((close)?0:offset.x);
527
                box.y1=state.visible.y1+((close)?0:offset.y);
528
                box.x1=box.x0;
529
                box.y0=box.y1;
530
 
531
                // Open new window.
532
                if (std::strlen(_pathname)+std::strlen(info.name)+1<max_name_length)
533
                {
534
                        std::sprintf(buffer,"%s.%s",_pathname,info.name);
535
                        window* w=new filer_window(
536
                                (filer_application*)parent_application(),
537
                                buffer,box,_options);
538
                }
539
        }
540
        else
541
        {
542
                // Run object.
543
                if (std::strlen(_pathname)+std::strlen(info.name)+1<max_name_length)
544
                {
545
                        std::sprintf(buffer,"Run %s.%s",_pathname,info.name);
546
                        wimp_start_task(buffer);
547
                }
548
        }
549
}