File: flash.cpp

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (596 lines) | stat: -rw-r--r-- 17,723 bytes parent folder | download | duplicates (10)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/////////////////////////////////////////////////////////////////////////////
// Name:        flash.cpp
// Purpose:     Sample showing integration of Flash ActiveX control
// Author:      Vadim Zeitlin
// Created:     2009-01-13
// Copyright:   (c) 2009 Vadim Zeitlin
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/*
    Documentation for embedding Flash into anything other than a web browser is
    not easy to find, here is the tech note which provided most of the
    information used here: http://www.adobe.com/go/tn_12059
 */

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef __WXMSW__
    #error "ActiveX controls are MSW-only"
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/cmdline.h"
#include "wx/filename.h"

#ifndef wxHAS_IMAGES_IN_RESOURCES
    #include "../sample.xpm"
#endif

#include "wx/msw/ole/activex.h"

// we currently use VC-specific extensions in this sample, it could be
// rewritten to avoid them if there is real interest in doing it but compiler
// COM support in MSVC makes the code much simpler to understand
#ifndef __VISUALC__
    #error "This sample requires Microsoft Visual C++ compiler COM extensions"
#endif

// import Flash ActiveX control by using its (standard) type library UUID
//
// no_auto_exclude is needed to import IServiceProvider interface defined in
// this type library even though its name conflicts with a standard Windows
// interface with the same name
#import "libid:D27CDB6B-AE6D-11CF-96B8-444553540000" no_auto_exclude

using namespace ShockwaveFlashObjects;

const CLSID CLSID_ShockwaveFlash = __uuidof(ShockwaveFlash);
const IID IID_IShockwaveFlash = __uuidof(IShockwaveFlash);

inline wxString bstr2wx(const _bstr_t& bstr)
{
    return wxString(static_cast<const wchar_t *>(bstr));
}

inline _bstr_t wx2bstr(const wxString& str)
{
    return _bstr_t(str.wc_str());
}

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// taken from type library
namespace
{

const int FLASH_DISPID_ONREADYSTATECHANGE = -609; // DISPID_ONREADYSTATECHANGE
const int FLASH_DISPID_ONPROGRESS = 0x7a6;
const int FLASH_DISPID_FSCOMMAND = 0x96;
const int FLASH_DISPID_FLASHCALL = 0xc5;

enum FlashState
{
    FlashState_Unknown = -1,
    FlashState_Loading,
    FlashState_Uninitialized,
    FlashState_Loaded,
    FlashState_Interactive,
    FlashState_Complete,
    FlashState_Max
};

} // anonymous namespace

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class FlashApp : public wxApp
{
public:
    FlashApp() { }

    virtual bool OnInit();

    virtual void OnInitCmdLine(wxCmdLineParser& parser);
    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);

    virtual bool OnExceptionInMainLoop();

private:
    wxString m_swf;

    wxDECLARE_NO_COPY_CLASS(FlashApp);
};

// Define a new frame type: this is going to be our main frame
class FlashFrame : public wxFrame
{
public:
    // ctor takes ownership of the pointer which must be non-NULL and opens the
    // given SWF file if it's non-empty
    FlashFrame(IShockwaveFlash *flash, const wxString& swf);
    virtual ~FlashFrame();

    void SetMovie(const wxString& movie);

    void Play();
    void Stop();

private:
    enum
    {
        Flash_Play = 100,
        Flash_Get,
        Flash_Set,
        Flash_Call,
        Flash_CallWithArg
    };

    void OnOpen(wxCommandEvent& event);
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

    void OnPlay(wxCommandEvent&) { Play(); }
    void OnStop(wxCommandEvent&) { Stop(); }
    void OnBack(wxCommandEvent& event);
    void OnForward(wxCommandEvent& event);
    void OnInfo(wxCommandEvent& event);
    void OnVarGet(wxCommandEvent& event);
    void OnVarSet(wxCommandEvent& event);
    void OnCall(wxCommandEvent& event);
    void OnCallWithArg(wxCommandEvent& event);

    void OnActiveXEvent(wxActiveXEvent& event);

    // give an error message if hr is not S_OK
    void CheckFlashCall(HRESULT hr, const char *func);

    // return the name of the Flash control state
    wxString GetFlashStateString(int state);

    // call CallFunction() with a single argument of the type specified by
    // argtype or without any arguments if it is empty
    void CallFlashFunc(const wxString& argtype,
                       const wxString& func,
                       const wxString& arg = wxString());


    const IShockwaveFlashPtr m_flash;
    wxLog *m_oldLog;
    wxString m_swf;
    FlashState m_state;

    wxTextCtrl *m_varname,
               *m_varvalue,
               *m_funcname,
               *m_funcarg;

    wxDECLARE_EVENT_TABLE();
    wxDECLARE_NO_COPY_CLASS(FlashFrame);
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

wxBEGIN_EVENT_TABLE(FlashFrame, wxFrame)
    EVT_MENU(wxID_OPEN,  FlashFrame::OnOpen)
    EVT_MENU(wxID_EXIT,  FlashFrame::OnQuit)
    EVT_MENU(wxID_ABOUT, FlashFrame::OnAbout)

    EVT_BUTTON(Flash_Play, FlashFrame::OnPlay)
    EVT_BUTTON(wxID_STOP,  FlashFrame::OnStop)
    EVT_BUTTON(wxID_BACKWARD, FlashFrame::OnBack)
    EVT_BUTTON(wxID_FORWARD,  FlashFrame::OnForward)

    EVT_BUTTON(wxID_INFO, FlashFrame::OnInfo)
    EVT_BUTTON(Flash_Get, FlashFrame::OnVarGet)
    EVT_BUTTON(Flash_Set, FlashFrame::OnVarSet)
    EVT_BUTTON(Flash_Call, FlashFrame::OnCall)
    EVT_BUTTON(Flash_CallWithArg, FlashFrame::OnCallWithArg)

    EVT_ACTIVEX(wxID_ANY, FlashFrame::OnActiveXEvent)
wxEND_EVENT_TABLE()

IMPLEMENT_APP(FlashApp)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

void FlashApp::OnInitCmdLine(wxCmdLineParser& parser)
{
    wxApp::OnInitCmdLine(parser);

    parser.AddParam("SWF file to play",
                    wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
}

bool FlashApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if ( parser.GetParamCount() )
        m_swf = parser.GetParam(0);

    return wxApp::OnCmdLineParsed(parser);
}

bool FlashApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    IShockwaveFlash *flash = NULL;
    HRESULT hr = ::CoCreateInstance
                   (
                    CLSID_ShockwaveFlash,
                    NULL,
                    CLSCTX_INPROC_SERVER,
                    IID_IShockwaveFlash,
                    (void **)&flash
                   );
    if ( FAILED(hr) )
    {
        wxLogSysError(hr, "Failed to create Flash ActiveX control");
        return false;
    }

    new FlashFrame(flash, m_swf);

    return true;
}

bool FlashApp::OnExceptionInMainLoop()
{
    try
    {
        throw;
    }
    catch ( _com_error& ce )
    {
        wxLogMessage("COM exception: %s", ce.ErrorMessage());

        return true;
    }
    catch ( ... )
    {
        throw;
    }
}

// ----------------------------------------------------------------------------
// main frame creation
// ----------------------------------------------------------------------------

// frame constructor
FlashFrame::FlashFrame(IShockwaveFlash *flash, const wxString& swf)
    : wxFrame(NULL, wxID_ANY, "wxWidgets Flash sample"),
      m_flash(flash, false /* take ownership */),
      m_swf(swf),
      m_state(FlashState_Unknown)
{
    // set the frame icon
    SetIcon(wxICON(sample));

    // create the new log target before doing anything with the Flash that
    // could result in log messages
    wxTextCtrl * const log = new wxTextCtrl(this, wxID_ANY, "",
                                            wxDefaultPosition, wxSize(-1, 100),
                                            wxTE_MULTILINE);
    m_oldLog = wxLog::SetActiveTarget(new wxLogTextCtrl(log));

#if wxUSE_MENUS
    // create a menu bar
    wxMenu *fileMenu = new wxMenu;
    fileMenu->Append(wxID_OPEN);
    fileMenu->AppendSeparator();
    fileMenu->Append(wxID_EXIT);

    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(wxID_ABOUT);

    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, "&File");
    menuBar->Append(helpMenu, "&Help");
    SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    CreateStatusBar(2);
    SetStatusText("Welcome to wxWidgets Flash embedding sample");
    SetStatusText("No loaded file", 1);
#endif // wxUSE_STATUSBAR

    wxPanel * const panel = new wxPanel(this);
    wxSizer * const sizerPanel = new wxBoxSizer(wxVERTICAL);
    wxWindow * const activeXParent = new wxWindow(panel, wxID_ANY,
                                                  wxDefaultPosition,
                                                  wxSize(300, 200));
    new wxActiveXContainer(activeXParent, IID_IShockwaveFlash, flash);
    if ( !swf.empty() )
        SetMovie(swf);

    sizerPanel->Add(activeXParent,
                    wxSizerFlags(1).Expand().Border());

    const wxSizerFlags flagsHorz(wxSizerFlags().Centre().HorzBorder());

    wxSizer * const sizerBtns = new wxBoxSizer(wxHORIZONTAL);
    sizerBtns->Add(new wxButton(panel, wxID_BACKWARD), flagsHorz);
    sizerBtns->Add(new wxButton(panel, Flash_Play, "&Play"), flagsHorz);
    sizerBtns->Add(new wxButton(panel, wxID_STOP), flagsHorz);
    sizerBtns->Add(new wxButton(panel, wxID_FORWARD), flagsHorz);
    sizerBtns->AddSpacer(20);
    sizerBtns->Add(new wxButton(panel, wxID_INFO), flagsHorz);
    sizerPanel->Add(sizerBtns, wxSizerFlags().Center().Border());

    wxSizer * const sizerVar = new wxBoxSizer(wxHORIZONTAL);
    sizerVar->Add(new wxStaticText(panel, wxID_ANY, "Variable &name:"),
                  flagsHorz);
    m_varname = new wxTextCtrl(panel, wxID_ANY);
    sizerVar->Add(m_varname, flagsHorz);
    sizerVar->Add(new wxStaticText(panel, wxID_ANY, "&value:"),
                  flagsHorz);
    m_varvalue = new wxTextCtrl(panel, wxID_ANY);
    sizerVar->Add(m_varvalue, flagsHorz);
    sizerVar->AddSpacer(10);
    sizerVar->Add(new wxButton(panel, Flash_Get, "&Get"), flagsHorz);
    sizerVar->Add(new wxButton(panel, Flash_Set, "&Set"), flagsHorz);
    sizerPanel->Add(sizerVar, wxSizerFlags().Center().Border());

    wxSizer * const sizerCall = new wxBoxSizer(wxHORIZONTAL);
    sizerCall->Add(new wxStaticText(panel, wxID_ANY, "&Function name:"),
                   flagsHorz);
    m_funcname = new wxTextCtrl(panel, wxID_ANY);
    sizerCall->Add(m_funcname, flagsHorz);
    sizerCall->Add(new wxButton(panel, Flash_Call, "&Call"), flagsHorz);
    sizerCall->Add(new wxStaticText(panel, wxID_ANY, "&argument:"),
                   flagsHorz);
    m_funcarg = new wxTextCtrl(panel, wxID_ANY);
    sizerCall->Add(m_funcarg, flagsHorz);
    sizerCall->Add(new wxButton(panel, Flash_CallWithArg, "Call &with arg"),
                   flagsHorz);
    sizerPanel->Add(sizerCall, wxSizerFlags().Center().Border());

    panel->SetSizer(sizerPanel);

    wxSizer * const sizerFrame = new wxBoxSizer(wxVERTICAL);
    sizerFrame->Add(panel, wxSizerFlags(2).Expand());
    sizerFrame->Add(log, wxSizerFlags(1).Expand());
    SetSizerAndFit(sizerFrame);

    Show();

    m_flash->PutAllowScriptAccess(L"always");
    wxLogMessage("Script access changed to \"%s\"",
                 bstr2wx(m_flash->GetAllowScriptAccess()));
}

FlashFrame::~FlashFrame()
{
    delete wxLog::SetActiveTarget(m_oldLog);
}

// ----------------------------------------------------------------------------
// Flash API wrappers
// ----------------------------------------------------------------------------

void FlashFrame::CheckFlashCall(HRESULT hr, const char *func)
{
    if ( FAILED(hr) )
    {
        wxLogSysError(hr, "Call to IShockwaveFlash::%s() failed", func);
    }
}

void FlashFrame::CallFlashFunc(const wxString& argtype,
                               const wxString& func,
                               const wxString& arg)
{
    wxString args;
    if ( !argtype.empty() )
    {
        args = wxString::Format("<%s>%s</%s>", argtype, arg, argtype);
    }

    // take care with XML formatting: there should be no spaces in it or the
    // call would fail!
    wxString request = wxString::Format
                       (
                            "<invoke name=\"%s\" returntype=\"xml\">"
                                "<arguments>"
                                "%s"
                                "</arguments>"
                            "</invoke>",
                            func,
                            args
                       );

    wxLogMessage("%s(%s) returned \"%s\"",
                 func, args,
                 bstr2wx(m_flash->CallFunction(wx2bstr(request))));
}

wxString FlashFrame::GetFlashStateString(int state)
{
    static const char *knownStates[] =
    {
        "Loading", "Uninitialized", "Loaded", "Interactive", "Complete",
    };

    if ( state >= 0 && state < WXSIZEOF(knownStates) )
        return knownStates[state];

    return wxString::Format("unknown state (%d)", state);
}

void FlashFrame::SetMovie(const wxString& movie)
{
    // Flash doesn't like relative file names
    wxFileName fn(movie);
    fn.MakeAbsolute();
    const wxString swf = fn.GetFullPath();
    if ( swf == m_swf )
        m_flash->PutMovie(L"");
    else
        m_swf = swf;

    m_flash->PutMovie(m_swf.wc_str());

    SetStatusText("Loaded \"" + m_swf + '"', 1);
}

void FlashFrame::Play()
{
    CheckFlashCall(m_flash->Play(), "Play");
}

void FlashFrame::Stop()
{
    CheckFlashCall(m_flash->Stop(), "Stop");
}

// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------

void FlashFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
{
    wxString swf = wxLoadFileSelector("Flash movie", ".swf", m_swf, this);
    if ( swf.empty() )
        return;

    SetMovie(swf);
}

void FlashFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}

void FlashFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox("Flash ActiveX control embedding sample\n"
                 "\n"
                 "(c) 2009 Vadim Zeitlin",
                 "About " + GetTitle(),
                 wxOK | wxICON_INFORMATION,
                 this);
}

void FlashFrame::OnActiveXEvent(wxActiveXEvent& event)
{
    switch ( event.GetDispatchId() )
    {
        case FLASH_DISPID_ONREADYSTATECHANGE:
            {
                const int state = event[0].GetInteger();
                if ( state != m_state )
                {
                    wxLogMessage("State changed to %s",
                                 GetFlashStateString(state));

                    if ( state >= 0 && state < FlashState_Max )
                        m_state = static_cast<FlashState>(state);
                    else
                        m_state = FlashState_Unknown;
                }
            }
            break;

        case FLASH_DISPID_ONPROGRESS:
            wxLogMessage("Progress: %d%%", event[0].GetInteger());
            break;

        case FLASH_DISPID_FSCOMMAND:
            wxLogMessage("Flash command %s(%s)",
                         event[0].GetString(), event[1].GetString());
            break;

        case FLASH_DISPID_FLASHCALL:
            wxLogMessage("Flash request \"%s\"", event[0].GetString());
            break;

        default:
            wxLogMessage("Unknown event %ld", event.GetDispatchId());
    }

    event.Skip();
}

void FlashFrame::OnBack(wxCommandEvent& WXUNUSED(event))
{
    CheckFlashCall(m_flash->Back(), "Back");
}

void FlashFrame::OnForward(wxCommandEvent& WXUNUSED(event))
{
    CheckFlashCall(m_flash->Forward(), "Forward");
}

void FlashFrame::OnInfo(wxCommandEvent& WXUNUSED(event))
{
    const int state = m_flash->GetReadyState();
    wxString msg = "State: " + GetFlashStateString(state);

    if ( state == FlashState_Complete )
    {
        msg += wxString::Format(", frame: %ld/%ld",
                                m_flash->GetFrameNum() + 1,
                                m_flash->GetTotalFrames());
    }

    if ( m_flash->IsPlaying() )
        msg += ", playing";

    wxLogMessage("%s", msg);
}

void FlashFrame::OnVarGet(wxCommandEvent& WXUNUSED(event))
{
    m_varvalue->SetValue(bstr2wx(
        m_flash->GetVariable(wx2bstr(m_varname->GetValue()))));
}

void FlashFrame::OnVarSet(wxCommandEvent& WXUNUSED(event))
{
    m_flash->SetVariable(wx2bstr(m_varname->GetValue()),
                         wx2bstr(m_varvalue->GetValue()));
}

void FlashFrame::OnCall(wxCommandEvent& WXUNUSED(event))
{
    CallFlashFunc("", m_funcname->GetValue());
}

void FlashFrame::OnCallWithArg(wxCommandEvent& WXUNUSED(event))
{
    CallFlashFunc("string", m_funcname->GetValue(), m_funcarg->GetValue());
}