642b0d89d47fc284474ad4fabcfcc70ef9ca7d9a
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_popup_view_bridge.mm
1 // Copyright (c) 2012 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 #import <Cocoa/Cocoa.h>
6
7 #include "chrome/browser/ui/cocoa/autofill/autofill_popup_view_bridge.h"
8
9 #include "base/logging.h"
10 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
11 #import "chrome/browser/ui/cocoa/autofill/autofill_popup_view_cocoa.h"
12 #include "ui/base/cocoa/window_size_constants.h"
13 #include "ui/gfx/rect.h"
14
15 namespace autofill {
16
17 AutofillPopupViewBridge::AutofillPopupViewBridge(
18     AutofillPopupController* controller)
19     : controller_(controller) {
20   window_ =
21       [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
22                                   styleMask:NSBorderlessWindowMask
23                                     backing:NSBackingStoreBuffered
24                                       defer:YES];
25   // Telling Cocoa that the window is opaque enables some drawing optimizations.
26   [window_ setOpaque:YES];
27
28   view_ = [[[AutofillPopupViewCocoa alloc]
29              initWithController:controller_
30                           frame:NSZeroRect] autorelease];
31   [window_ setContentView:view_];
32 }
33
34 AutofillPopupViewBridge::~AutofillPopupViewBridge() {
35   [view_ controllerDestroyed];
36
37   // Remove the child window before closing, otherwise it can mess up
38   // display ordering.
39   [[window_ parentWindow] removeChildWindow:window_];
40
41   [window_ close];
42 }
43
44 void AutofillPopupViewBridge::Hide() {
45   delete this;
46 }
47
48 void AutofillPopupViewBridge::Show() {
49   UpdateBoundsAndRedrawPopup();
50   [[controller_->container_view() window] addChildWindow:window_
51                                                  ordered:NSWindowAbove];
52 }
53
54 void AutofillPopupViewBridge::InvalidateRow(size_t row) {
55   NSRect dirty_rect =
56       NSRectFromCGRect(controller_->GetRowBounds(row).ToCGRect());
57   [view_ setNeedsDisplayInRect:dirty_rect];
58 }
59
60 void AutofillPopupViewBridge::UpdateBoundsAndRedrawPopup() {
61   NSRect frame = NSRectFromCGRect(controller_->popup_bounds().ToCGRect());
62
63   // Flip coordinates back into Cocoa-land.  The controller's platform-neutral
64   // coordinate space places the origin at the top-left of the first screen,
65   // whereas Cocoa's coordinate space expects the origin to be at the
66   // bottom-left of this same screen.
67   NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
68   frame.origin.y = NSMaxY([screen frame]) - NSMaxY(frame);
69
70   // TODO(isherman): The view should support scrolling if the popup gets too
71   // big to fit on the screen.
72   [window_ setFrame:frame display:YES];
73 }
74
75 AutofillPopupView* AutofillPopupView::Create(
76     AutofillPopupController* controller) {
77   return new AutofillPopupViewBridge(controller);
78 }
79
80 }  // namespace autofill