summaryrefslogtreecommitdiff
path: root/old/test/gnash.cc
blob: 034990ad04a594ce5771bd8d06816cb51b5cc007 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
################################################################ begin ###
//
//   Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#ifdef HAVE_CONFIG_H
#include "gnashconfig.h"
#endif

#include <fltk/Item.h>
#include <fltk/Window.h>

#ifdef HAVE_X11
#include <fltk/x11.h>
#endif

#include <fltk/events.h>
#include <fltk/run.h>
#include <fltk/Cursor.h>
#include <fltk/layout.h>
#include <fltk/MenuBar.h>
#include <fltk/ItemGroup.h>
#include <fltk/file_chooser.h>

#include "fltksup.h"
#include "gnash.h"
#include "gui.h"
#include "VM.h"

#include "render_handler.h"

using namespace std;
using namespace fltk;

namespace gnash
{


FltkGui::FltkGui(unsigned long xid, float scale, bool loop, unsigned int depth)
  : Window(0, 0),
    Gui(xid, scale, loop, depth),
    _menu_height(_xid ? 0 : 20)
{
}

FltkGui::~FltkGui()
{
    delete _popup_menu;
}


void
FltkGui::renderBuffer()
{
    // FLTK has a nice mechanism where you can set damage() to whatever you want
    // so in draw() you can check what exactly you want to redraw. But
    // unfortunately it doesn't seem to remember what bits you turn on. So I'll
    // just do it the old-fashioned way.

    static bool firstRun = true;

    if (firstRun) {
      using namespace geometry;
      Range2d<int> bounds(0, 0, _width, _height);
      _glue->render(bounds);

      return;
    }

    if (! _drawbounds_vec.size() ) {
      return; // XXX what about Cairo?
    }

    for (unsigned bno=0; bno < _drawbounds_vec.size(); bno++) {
       geometry::Range2d<int>& bounds = _drawbounds_vec[bno];

       assert ( bounds.isFinite() );

       _glue->render(bounds);
    }
}

int
FltkGui::handle(int event)
{
    switch (event) {
      case TIMEOUT:
        advance_movie(this);
        repeat_timeout(_interval);
        return true;
      case PUSH:
        Window::handle(event);
        notify_mouse_clicked(true, 1);
        return true;
      case RELEASE:
        Window::handle(event);
        notify_mouse_clicked(false, 1);
        return true;
      case MOVE:
      {
        if (!_xid && event_y() < static_cast<int>(_menu_height)) {
          return Window::handle(event);
        }
        notify_mouse_moved(event_x(), event_y()-_menu_height);
        return Window::handle(event);;
      }
      case SHORTCUT:
      case KEY:
        handleKey(event_key());
        return true;
      default:
        return Window::handle(event);
    }
}

void
FltkGui::handleKey(unsigned key)
{
    // TODO: there are more keys
    struct {
      unsigned              fltkKey;
      gnash::key::code      gnashKey;
    } table[] = {
      { BackSpaceKey,       gnash::key::BACKSPACE },
      { TabKey,             gnash::key::TAB },
      { ClearKey,           gnash::key::CLEAR },
      { ReturnKey,          gnash::key::ENTER },
      { CapsLockKey,        gnash::key::CAPSLOCK },
      { EscapeKey,          gnash::key::ESCAPE },
      { SpaceKey,           gnash::key::SPACE },
      { PageDownKey,        gnash::key::PGDN },
      { PageUpKey,          gnash::key::PGUP },
      { HomeKey,            gnash::key::HOME },
      { EndKey,             gnash::key::END },
      { LeftKey,            gnash::key::LEFT },
      { UpKey,              gnash::key::UP },
      { RightKey,           gnash::key::RIGHT },
      { DownKey,            gnash::key::DOWN },
      { InsertKey,          gnash::key::INSERT },
      { DeleteKey,          gnash::key::DELETEKEY },
      { HelpKey,            gnash::key::HELP },
      { NumLockKey,         gnash::key::NUM_LOCK },
      { SubtractKey,        gnash::key::MINUS },
      { DivideKey,          gnash::key::SLASH },
      { 0,                  gnash::key::INVALID }
#if 0
            // These appear to be unavailable in fltk
            { bracketleft, gnash::key::LEFT_BRACKET },
            { backslash, gnash::key::BACKSLASH },
            { bracketright, gnash::key::RIGHT_BRACKET },
            { quotedbl, gnash::key::QUOTE },
            { VoidSymbol, gnash::key::INVALID }
            { SemicolonKey, gnash::key::SEMICOLON },
            { equalKey, gnash::key::EQUALS },
#endif
    };

    int modifier = gnash::key::GNASH_MOD_NONE;

    unsigned long state = event_state();

    if (state & SHIFT) {
        modifier = modifier | gnash::key::GNASH_MOD_SHIFT;
    }
    if (state & CTRL) {
        modifier = modifier | gnash::key::GNASH_MOD_CONTROL;
    }
    if (state & ALT) {
        modifier = modifier | gnash::key::GNASH_MOD_ALT;
    }

    for (int i = 0; table[i].fltkKey; i++) {
        if (key == table[i].fltkKey) {
            notify_key_event((gnash::key::code)table[i].gnashKey, modifier,
                             true);
            break;
        }
    }
}

bool
FltkGui::run()
{
    fltk::run();

    return true;
}

bool
FltkGui::init(int /* argc */, char *** /*argv */)
{

    return true;
}

void
FltkGui::setInterval(unsigned int time)
{
    _interval = time / 1000.0;
    add_timeout (_interval);
}

void
FltkGui::create()
{
#ifdef HAVE_X11
    if (_xid) {
      // Make FLTK render into an X window indicated by the XID.
      CreatedWindow::set_xid(this, _xid);
      return;
    }
#endif
    Window::create();
}

bool
FltkGui::createWindow(const char* title, int width, int height)
{
    resize(width, _menu_height + height);


    label(title);
    begin();

    if (!_xid) {
      MenuBar* menubar = new MenuBar(0, 0, width, _menu_height);
      menubar->begin();
      addMenuItems();
      menubar->end();
    }
#ifdef RENDERER_AGG
    _glue = new FltkAggGlue(0, _menu_height, width, height);
#elif defined(RENDERER_CAIRO)
#error FLTK/Cairo is currently broken. Please try again soon...
    FltkCairoGlue _glue;
#elif defined(RENDERER_OPENGL)
#error FLTK/OpenGL is currently broken. Please try again soon...
    FltkCairoGlue _glue;
#endif
    createMenu();
    end();

    _renderer = _glue->createRenderHandler();
    if ( ! _renderer ) return false;
    set_render_handler(_renderer);

    _glue->initBuffer(width, height);

    // The minimum size of the window is 1x1 pixels.
    size_range (1, 1);

    show();

    return true;
}


static void fltk_menu_open_file(Widget*, void*)
{
    const char *newfile = fltk::file_chooser("Open File", "*.swf", NULL);
    if (!newfile) {
      return;
    }

    // menu_open_file()..
}

static void fltk_menu_save_file_as(Widget*, void*)
{
    const char* savefile = file_chooser("Save File as", NULL, NULL);
    if (!savefile) {
      return;
    }

    // menu_save_file();
}

static void fltk_menu_fullscreen(Widget*, void* ptr)
{
//    GNASH_REPORT_FUNCTION;

    static bool fullscreen = false;
    static Rectangle oldBounds;

    fullscreen = !fullscreen;

    FltkGui* gui = static_cast<FltkGui*>(ptr);
    if (fullscreen) {
      oldBounds.set(gui->x(), gui->y(), gui->w(), gui->h());
      gui->fullscreen();
    } else {
      gui->fullscreen_off(oldBounds.x(), oldBounds.y(), oldBounds.w(),
oldBounds.h());
    }
}


static void
fltk_menu_quit(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_quit();
}

static void
fltk_menu_play(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_play();
}

static void
fltk_menu_pause(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_pause();
}

static void
fltk_menu_stop(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_stop();
}

static void
fltk_menu_restart(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_restart();
}

static void
fltk_menu_step_forward(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_step_forward();
}


static void
fltk_menu_step_backward(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_step_backward();
}

static void
fltk_menu_jump_forward(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_jump_forward();
}

static void
fltk_menu_jump_backward(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_jump_backward();
}

static void
fltk_menu_toggle_sound(Widget*, void* ptr)
{
    FltkGui* gui = static_cast<FltkGui*>(ptr);
    gui->menu_toggle_sound();
}

void
FltkGui::addMenuItems()
{
    ItemGroup* file = new ItemGroup("File");


    file->begin();
    new Item("Open",                    0, fltk_menu_open_file);
    new Item("Save as",                 0, fltk_menu_save_file_as);
    new Item("Quit",                    0, fltk_menu_quit, this);
    file->end();

    ItemGroup* edit = new ItemGroup("Edit");
    edit->begin();
    new Item("Preferences");
    edit->end();

    ItemGroup* view = new ItemGroup("View");
    view->begin();
    new Item("Double size");
    new Item("Fullscreen",              0, fltk_menu_fullscreen, this);
    view->end();

    ItemGroup* movie_ctrl = new ItemGroup("Movie control");
    movie_ctrl->begin();
    new Item("Play Movie",              0, fltk_menu_play, this);
    new Item("Pause Movie",             0, fltk_menu_pause, this);
    new Item("Stop Movie",              0, fltk_menu_stop, this);
    new Item("Restart Movie",           0, fltk_menu_restart, this);
    new Item("Step Forward Frame",      0, fltk_menu_step_forward, this);
    new Item("Step Backward Frame",     0, fltk_menu_step_backward, this);
    new Item("Jump Forward 10 Frames",  0, fltk_menu_jump_forward, this);
    new Item("Jump Backward 10 Frames", 0, fltk_menu_jump_backward, this);
    new Item("Toggle Sound",            0, fltk_menu_toggle_sound, this);
    movie_ctrl->end();

    ItemGroup* help = new ItemGroup("Help");
    help->begin();
    new Item("About");
    help->end();
}



bool
FltkGui::createMenu()
{
    _popup_menu = new PopupMenu(0, 0, w(), h());
    _popup_menu->type(PopupMenu::POPUP3);

    _popup_menu->begin();

    addMenuItems();

    _popup_menu->end();

    return true;
}

void
FltkGui::layout()
{
    if (!VM::isInitialized()) {
      // No movie yet; don't bother resizing anything.
      return;
    }

    // Let FLTK update the window borders, etc.
    Window::layout();

    if ((layout_damage() & LAYOUT_WH)) {
      _glue->resize(w(), h() - _menu_height);
      resize_view(w(), h() - _menu_height);
    }
}

void
FltkGui::setCursor(gnash_cursor_type newcursor)
{
    fltk::Cursor* cursortype;

    switch(newcursor) {
      case gnash::CURSOR_HAND:
        cursortype = fltk::CURSOR_HAND;
        break;
      case gnash::CURSOR_INPUT:
        cursortype = fltk::CURSOR_INSERT;
        break;
      default:
        cursortype = fltk::CURSOR_DEFAULT;
    }

    cursor(cursortype);
}

void
FltkGui::setInvalidatedRegions(const InvalidatedRanges& ranges)
{
    // forward to renderer
    //
    // Why? Why have the region been invalidated ??
    // Was the renderer offscreen buffer also invalidated
    // (need to rerender)?
    // Was only the 'onscreen' buffer be invalidated (no need to rerender,
    // just to blit) ??
    //
    // To be safe just assume this 'invalidated' region is actually
    // the offscreen buffer, for safety, but we need to clarify this.
    //
    _renderer->set_invalidated_regions(ranges);

    _drawbounds_vec.clear();

    for (size_t rno=0; rno<ranges.size(); rno++) {

      geometry::Range2d<int> bounds = Intersection(
      _renderer->world_to_pixel(ranges.getRange(rno)),
      _validbounds);

      // it may happen that a particular range is out of the screen, which
      // will lead to bounds==null.
      if (bounds.isNull()) continue;

      assert(bounds.isFinite());

      _drawbounds_vec.push_back(bounds);

    }
}

// end of namespace
}