180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTypes.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if defined(SK_BUILD_FOR_MAC)
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <AGL/agl.h>
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <Carbon/Carbon.h>
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCGUtils.h"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkWindow.h"
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkOSMenu.h"
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTime.h"
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkGraphics.h"
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <new.h>
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void (*gPrevNewHandler)();
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruextern "C" {
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void sk_new_handler()
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (SkGraphics::SetFontCacheUsed(0))
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (gPrevNewHandler)
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            gPrevNewHandler();
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        else
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            sk_throw();
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkOSWindow* gCurrOSWin;
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic EventTargetRef gEventTarget;
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic EventQueueRef gCurrEventQ;
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic OSStatus MyDrawEventHandler(EventHandlerCallRef myHandler,
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                   EventRef event, void *userData) {
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // NOTE: GState is save/restored by the HIView system doing the callback,
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // so the draw handler doesn't need to do it
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    OSStatus status = noErr;
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    CGContextRef context;
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIRect        bounds;
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // Get the CGContextRef
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    status = GetEventParameter (event, kEventParamCGContextRef,
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                typeCGContextRef, NULL,
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                sizeof (CGContextRef),
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                NULL,
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                &context);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (status != noErr) {
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkDebugf("Got error %d getting the context!\n", status);
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return status;
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // Get the bounding rectangle
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewGetBounds ((HIViewRef) userData, &bounds);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    gCurrOSWin->doPaint(context);
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return status;
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_MacEventClass            FOUR_CHAR_CODE('SKec')
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_MacEventKind                FOUR_CHAR_CODE('SKek')
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_MacEventParamName        FOUR_CHAR_CODE('SKev')
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_MacEventSinkIDParamName    FOUR_CHAR_CODE('SKes')
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void set_bindingside(HISideBinding* side, HIViewRef parent, HIBindingKind kind) {
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    side->toView = parent;
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    side->kind = kind;
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    side->offset = 0;
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void set_axisscale(HIAxisScale* axis, HIViewRef parent) {
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    axis->toView = parent;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    axis->kind = kHILayoutScaleAbsolute;
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    axis->ratio = 1;
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void set_axisposition(HIAxisPosition* pos, HIViewRef parent, HIPositionKind kind) {
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pos->toView = parent;
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pos->kind = kind;
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pos->offset = 0;
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkOSWindow::SkOSWindow(void* hWnd) : fHWND(hWnd), fAGLCtx(NULL)
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    OSStatus    result;
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    WindowRef   wr = (WindowRef)hWnd;
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewRef imageView, parent;
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewRef rootView = HIViewGetRoot(wr);
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewFindByID(rootView, kHIViewWindowContentID, &parent);
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    result = HIImageViewCreate(NULL, &imageView);
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(result == noErr);
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    result = HIViewAddSubview(parent, imageView);
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(result == noErr);
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fHVIEW = imageView;
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewSetVisible(imageView, true);
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewPlaceInSuperviewAt(imageView, 0, 0);
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (true) {
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        HILayoutInfo layout;
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        layout.version = kHILayoutInfoVersionZero;
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_bindingside(&layout.binding.left, parent, kHILayoutBindLeft);
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_bindingside(&layout.binding.top, parent, kHILayoutBindTop);
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_bindingside(&layout.binding.right, parent, kHILayoutBindRight);
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_bindingside(&layout.binding.bottom, parent, kHILayoutBindBottom);
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_axisscale(&layout.scale.x, parent);
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_axisscale(&layout.scale.y, parent);
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_axisposition(&layout.position.x, parent, kHILayoutPositionLeft);
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        set_axisposition(&layout.position.y, rootView, kHILayoutPositionTop);
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        HIViewSetLayoutInfo(imageView, &layout);
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIImageViewSetOpaque(imageView, true);
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIImageViewSetScaleToFit(imageView, false);
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const EventTypeSpec  gTypes[] = {
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassKeyboard,  kEventRawKeyDown            },
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassKeyboard,  kEventRawKeyUp              },
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassMouse,        kEventMouseDown                },
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassMouse,        kEventMouseDragged            },
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassMouse,        kEventMouseMoved            },
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassMouse,        kEventMouseUp                },
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent   },
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { kEventClassWindow,    kEventWindowBoundsChanged    },
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//        { kEventClassWindow,    kEventWindowDrawContent        },
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacEventClass,        SK_MacEventKind                }
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    EventHandlerUPP handlerUPP = NewEventHandlerUPP(SkOSWindow::EventHandler);
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int                count = SK_ARRAY_COUNT(gTypes);
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    result = InstallEventHandler(GetWindowEventTarget(wr), handlerUPP,
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                        count, gTypes, this, nil);
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(result == noErr);
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    gCurrOSWin = this;
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    gCurrEventQ = GetCurrentEventQueue();
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    gEventTarget = GetWindowEventTarget(wr);
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static bool gOnce = true;
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (gOnce) {
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gOnce = false;
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gPrevNewHandler = set_new_handler(sk_new_handler);
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::doPaint(void* ctx)
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if 0
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->update(NULL);
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkBitmap& bm = this->getBitmap();
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    CGImageRef img = SkCreateCGImageRef(bm);
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (img) {
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextRef cg = reinterpret_cast<CGContextRef>(ctx);
17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextSaveGState(cg);
17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextTranslateCTM(cg, 0, r.size.height);
17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextScaleCTM(cg, 1, -1);
17780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextDrawImage(cg, r, img);
17980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGContextRestoreGState(cg);
18180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        CGImageRelease(img);
18380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
18480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
18580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
18680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::updateSize()
18880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
18980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Rect    r;
19080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GetWindowBounds((WindowRef)fHWND, kWindowContentRgn, &r);
19280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->resize(r.right - r.left, r.bottom - r.top);
19380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if 0
19580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIRect    frame;
19680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewRef imageView = (HIViewRef)getHVIEW();
19780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewRef parent = HIViewGetSuperview(imageView);
19880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    HIViewGetBounds(imageView, &frame);
20080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDebugf("------ %d bounds %g %g %g %g\n", r.right - r.left,
20180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru             frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
20280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
20380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
20480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
20580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::onHandleInval(const SkIRect& r)
20680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
20780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    (new SkEvent("inval-imageview", this->getSinkID()))->post();
20880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
20980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkOSWindow::onEvent(const SkEvent& evt) {
21180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (evt.isType("inval-imageview")) {
21280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->update(NULL);
21380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkEvent query("ignore-window-bitmap");
21580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (!this->doQuery(&query) || !query.getFast32()) {
21680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            const SkBitmap& bm = this->getBitmap();
21780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            CGImageRef img = SkCreateCGImageRef(bm);
21980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            HIImageViewSetImage((HIViewRef)getHVIEW(), img);
22080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            CGImageRelease(img);
22180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
22280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
22380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
22480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return INHERITED::onEvent(evt);
22580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
22680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
22780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::onSetTitle(const char title[])
22880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
22980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    CFStringRef str = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
23080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SetWindowTitleWithCFString((WindowRef)fHWND, str);
23180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    CFRelease(str);
23280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
23380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::onAddMenu(const SkOSMenu* sk_menu)
23580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
23680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
23780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void getparam(EventRef inEvent, OSType name, OSType type, UInt32 size, void* data)
23980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
24080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    EventParamType  actualType;
24180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UInt32            actualSize;
24280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    OSStatus        status;
24380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
24480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    status = GetEventParameter(inEvent, name, type, &actualType, size, &actualSize, data);
24580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
24680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(actualType == type);
24780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(actualSize == size);
24880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
24980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
25080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruenum {
25180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacReturnKey        = 36,
25280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacDeleteKey        = 51,
25380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacEndKey        = 119,
25480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacLeftKey        = 123,
25580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacRightKey        = 124,
25680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacDownKey        = 125,
25780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_MacUpKey            = 126,
25880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
25980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac0Key          = 0x52,
26080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac1Key          = 0x53,
26180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac2Key          = 0x54,
26280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac3Key          = 0x55,
26380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac4Key          = 0x56,
26480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac5Key          = 0x57,
26580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac6Key          = 0x58,
26680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac7Key          = 0x59,
26780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac8Key          = 0x5b,
26880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_Mac9Key          = 0x5c
26980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
27080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
27180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkKey raw2key(UInt32 raw)
27280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
27380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const struct {
27480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        UInt32  fRaw;
27580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkKey   fKey;
27680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } gKeys[] = {
27780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacUpKey,        kUp_SkKey        },
27880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacDownKey,    kDown_SkKey        },
27980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacLeftKey,    kLeft_SkKey        },
28080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacRightKey,   kRight_SkKey    },
28180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacReturnKey,  kOK_SkKey        },
28280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacDeleteKey,  kBack_SkKey        },
28380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_MacEndKey,        kEnd_SkKey        },
28480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac0Key,       k0_SkKey        },
28580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac1Key,       k1_SkKey        },
28680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac2Key,       k2_SkKey        },
28780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac3Key,       k3_SkKey        },
28880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac4Key,       k4_SkKey        },
28980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac5Key,       k5_SkKey        },
29080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac6Key,       k6_SkKey        },
29180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac7Key,       k7_SkKey        },
29280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac8Key,       k8_SkKey        },
29380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        { SK_Mac9Key,       k9_SkKey        }
29480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
29580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
29680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++)
29780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (gKeys[i].fRaw == raw)
29880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return gKeys[i].fKey;
29980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return kNONE_SkKey;
30080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
30180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
30280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void post_skmacevent()
30380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
30480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    EventRef    ref;
30580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    OSStatus    status = CreateEvent(nil, SK_MacEventClass, SK_MacEventKind, 0, 0, &ref);
30680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
30780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
30880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if 0
30980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    status = SetEventParameter(ref, SK_MacEventParamName, SK_MacEventParamName, sizeof(evt), &evt);
31080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
31180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    status = SetEventParameter(ref, SK_MacEventSinkIDParamName, SK_MacEventSinkIDParamName, sizeof(sinkID), &sinkID);
31280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
31380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
31480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
31580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    EventTargetRef target = gEventTarget;
31680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SetEventParameter(ref, kEventParamPostTarget, typeEventTargetRef, sizeof(target), &target);
31780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
31880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
31980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    status = PostEventToQueue(gCurrEventQ, ref, kEventPriorityStandard);
32080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(status == noErr);
32180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
32280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ReleaseEvent(ref);
32380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
32480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
32580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupascal OSStatus SkOSWindow::EventHandler( EventHandlerCallRef inHandler, EventRef inEvent, void* userData )
32680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
32780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkOSWindow* win = (SkOSWindow*)userData;
32880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    OSStatus    result = eventNotHandledErr;
32980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UInt32        wClass = GetEventClass(inEvent);
33080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    UInt32        wKind = GetEventKind(inEvent);
33180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
33280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    gCurrOSWin = win;    // will need to be in TLS. Set this so PostEvent will work
33380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
33480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    switch (wClass) {
33580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case kEventClassMouse: {
33680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            Point   pt;
33780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            getparam(inEvent, kEventParamMouseLocation, typeQDPoint, sizeof(pt), &pt);
33880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SetPortWindowPort((WindowRef)win->getHWND());
33980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            GlobalToLocal(&pt);
34080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
34180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            switch (wKind) {
34280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventMouseDown:
34380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    if (win->handleClick(pt.h, pt.v, Click::kDown_State)) {
34480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                        result = noErr;
34580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    }
34680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
34780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventMouseMoved:
34880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    // fall through
34980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventMouseDragged:
35080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    (void)win->handleClick(pt.h, pt.v, Click::kMoved_State);
35180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                  //  result = noErr;
35280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
35380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventMouseUp:
35480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    (void)win->handleClick(pt.h, pt.v, Click::kUp_State);
35580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                  //  result = noErr;
35680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
35780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                default:
35880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
35980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
36080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
36180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
36280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case kEventClassKeyboard:
36380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (wKind == kEventRawKeyDown) {
36480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                UInt32  raw;
36580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                getparam(inEvent, kEventParamKeyCode, typeUInt32, sizeof(raw), &raw);
36680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkKey key = raw2key(raw);
36780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                if (key != kNONE_SkKey)
36880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    (void)win->handleKey(key);
36980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            } else if (wKind == kEventRawKeyUp) {
37080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                UInt32 raw;
37180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                getparam(inEvent, kEventParamKeyCode, typeUInt32, sizeof(raw), &raw);
37280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkKey key = raw2key(raw);
37380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                if (key != kNONE_SkKey)
37480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    (void)win->handleKeyUp(key);
37580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
37680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
37780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case kEventClassTextInput:
37880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (wKind == kEventTextInputUnicodeForKeyEvent) {
37980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                UInt16  uni;
38080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                getparam(inEvent, kEventParamTextInputSendText, typeUnicodeText, sizeof(uni), &uni);
38180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                win->handleChar(uni);
38280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
38380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
38480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case kEventClassWindow:
38580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            switch (wKind) {
38680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventWindowBoundsChanged:
38780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    win->updateSize();
38880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
38980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                case kEventWindowDrawContent: {
39080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    CGContextRef cg;
39180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    result = GetEventParameter(inEvent,
39280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               kEventParamCGContextRef,
39380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               typeCGContextRef,
39480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               NULL,
39580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               sizeof (CGContextRef),
39680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               NULL,
39780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                               &cg);
39880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    if (result != 0) {
39980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                        cg = NULL;
40080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    }
40180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    win->doPaint(cg);
40280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
40380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
40480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                default:
40580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    break;
40680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
40780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
40880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case SK_MacEventClass: {
40980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkASSERT(wKind == SK_MacEventKind);
41080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (SkEvent::ProcessEvent()) {
41180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    post_skmacevent();
41280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
41380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #if 0
41480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkEvent*        evt;
41580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkEventSinkID    sinkID;
41680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            getparam(inEvent, SK_MacEventParamName, SK_MacEventParamName, sizeof(evt), &evt);
41780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            getparam(inEvent, SK_MacEventSinkIDParamName, SK_MacEventSinkIDParamName, sizeof(sinkID), &sinkID);
41880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #endif
41980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            result = noErr;
42080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
42180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
42280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        default:
42380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
42480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
42580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (result == eventNotHandledErr) {
42680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        result = CallNextEventHandler(inHandler, inEvent);
42780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
42880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return result;
42980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
43080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
43180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru///////////////////////////////////////////////////////////////////////////////////////
43280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
43380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkEvent::SignalNonEmptyQueue()
43480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
43580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    post_skmacevent();
43680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    SkDebugf("signal nonempty\n");
43780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
43880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
43980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic TMTask    gTMTaskRec;
44080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic TMTask*    gTMTaskPtr;
44180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
44280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic void sk_timer_proc(TMTask* rec)
44380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
44480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEvent::ServiceQueueTimer();
44580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//    SkDebugf("timer task fired\n");
44680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
44780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
44880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkEvent::SignalQueueTimer(SkMSec delay)
44980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
45080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (gTMTaskPtr)
45180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
45280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        RemoveTimeTask((QElem*)gTMTaskPtr);
45380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        DisposeTimerUPP(gTMTaskPtr->tmAddr);
45480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gTMTaskPtr = nil;
45580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
45680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (delay)
45780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
45880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gTMTaskPtr = &gTMTaskRec;
45980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        memset(gTMTaskPtr, 0, sizeof(gTMTaskRec));
46080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gTMTaskPtr->tmAddr = NewTimerUPP(sk_timer_proc);
46180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        OSErr err = InstallTimeTask((QElem*)gTMTaskPtr);
46280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//        SkDebugf("installtimetask of %d returned %d\n", delay, err);
46380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        PrimeTimeTask((QElem*)gTMTaskPtr, delay);
46480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
46580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
46680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
46780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define USE_MSAA 0
46880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
46980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruAGLContext create_gl(WindowRef wref)
47080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
47180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GLint major, minor;
47280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    AGLContext ctx;
47380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
47480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglGetVersion(&major, &minor);
47580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDebugf("---- agl version %d %d\n", major, minor);
47680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
47780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const GLint pixelAttrs[] = {
47880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_RGBA,
47980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_STENCIL_SIZE, 8,
48080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if USE_MSAA
48180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_SAMPLE_BUFFERS_ARB, 1,
48280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_MULTISAMPLE,
48380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_SAMPLES_ARB, 8,
48480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
48580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_ACCELERATED,
48680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_DOUBLEBUFFER,
48780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_NONE
48880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
48980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    AGLPixelFormat format = aglChoosePixelFormat(NULL, 0, pixelAttrs);
49080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    //AGLPixelFormat format = aglCreatePixelFormat(pixelAttrs);
49180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDebugf("----- agl format %p\n", format);
49280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ctx = aglCreateContext(format, NULL);
49380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDebugf("----- agl context %p\n", ctx);
49480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglDestroyPixelFormat(format);
49580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
49680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const GLint interval = 1;
49780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetInteger(ctx, AGL_SWAP_INTERVAL, &interval);
49880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetCurrentContext(ctx);
49980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return ctx;
50080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
50180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
50280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkOSWindow::attach(SkBackEndTypes /* attachType */)
50380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
50480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == fAGLCtx) {
50580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fAGLCtx = create_gl((WindowRef)fHWND);
50680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (NULL == fAGLCtx) {
50780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return false;
50880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
50980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
51080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
51180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GLboolean success = true;
51280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
51380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int width, height;
51480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
51580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    success = aglSetWindowRef((AGLContext)fAGLCtx, (WindowRef)fHWND);
51680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    width = this->width();
51780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    height = this->height();
51880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
51980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GLenum err = aglGetError();
52080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (err) {
52180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkDebugf("---- aglSetWindowRef %d %d %s [%d %d]\n", success, err,
52280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                 aglErrorString(err), width, height);
52380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
52480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
52580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (success) {
52680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        glViewport(0, 0, width, height);
52780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        glClearColor(0, 0, 0, 0);
52880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        glClearStencil(0);
52980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
53080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
53180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return success;
53280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
53380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
53480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::detach() {
53580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetWindowRef((AGLContext)fAGLCtx, NULL);
53680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
53780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
53880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkOSWindow::present() {
53980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSwapBuffers((AGLContext)fAGLCtx);
54080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
54180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
54280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
543