Home | History | Annotate | Download | only in native_viewport
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "mojo/services/native_viewport/native_viewport.h"
      6 
      7 #import <AppKit/NSApplication.h>
      8 #import <AppKit/NSView.h>
      9 #import <AppKit/NSWindow.h>
     10 
     11 #include "base/bind.h"
     12 #include "ui/gfx/rect.h"
     13 
     14 namespace mojo {
     15 namespace services {
     16 
     17 class NativeViewportMac : public NativeViewport {
     18  public:
     19   NativeViewportMac(NativeViewportDelegate* delegate)
     20       : delegate_(delegate),
     21         window_(nil) {
     22   }
     23 
     24   virtual ~NativeViewportMac() {
     25     [window_ orderOut:nil];
     26     [window_ close];
     27   }
     28 
     29  private:
     30   // Overridden from NativeViewport:
     31   virtual void Init(const gfx::Rect& bounds) OVERRIDE {
     32     [NSApplication sharedApplication];
     33 
     34     rect_ = bounds;
     35     window_ = [[NSWindow alloc]
     36                   initWithContentRect:NSRectFromCGRect(rect_.ToCGRect())
     37                             styleMask:NSTitledWindowMask
     38                               backing:NSBackingStoreBuffered
     39                                 defer:NO];
     40     delegate_->OnAcceleratedWidgetAvailable([window_ contentView]);
     41     delegate_->OnBoundsChanged(rect_);
     42   }
     43 
     44   virtual void Show() OVERRIDE {
     45     [window_ orderFront:nil];
     46   }
     47 
     48   virtual void Hide() OVERRIDE {
     49     [window_ orderOut:nil];
     50   }
     51 
     52   virtual void Close() OVERRIDE {
     53     // TODO(beng): perform this in response to NSWindow destruction.
     54     delegate_->OnDestroyed();
     55   }
     56 
     57   virtual gfx::Size GetSize() OVERRIDE {
     58     return rect_.size();
     59   }
     60 
     61   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
     62     NOTIMPLEMENTED();
     63   }
     64 
     65   virtual void SetCapture() OVERRIDE {
     66     NOTIMPLEMENTED();
     67   }
     68 
     69   virtual void ReleaseCapture() OVERRIDE {
     70     NOTIMPLEMENTED();
     71   }
     72 
     73   NativeViewportDelegate* delegate_;
     74   NSWindow* window_;
     75   gfx::Rect rect_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(NativeViewportMac);
     78 };
     79 
     80 // static
     81 scoped_ptr<NativeViewport> NativeViewport::Create(
     82     shell::Context* context,
     83     NativeViewportDelegate* delegate) {
     84   return scoped_ptr<NativeViewport>(new NativeViewportMac(delegate)).Pass();
     85 }
     86 
     87 }  // namespace services
     88 }  // namespace mojo
     89