- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_notification_container.mm
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 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_container.h"
6
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "chrome/browser/ui/autofill/autofill_dialog_types.h"
10 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
11 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h"
12
13 @implementation AutofillNotificationContainer
14
15 - (id)initWithDelegate:(autofill::AutofillDialogViewDelegate*)delegate {
16   if (self = [super init]) {
17     delegate_ = delegate;
18     [self setView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
19   }
20   return self;
21 }
22
23 // Just here to satisfy the protocol - not actually invoked.
24 - (NSSize)preferredSize {
25   NOTREACHED();
26   return NSZeroSize;
27 }
28
29 - (NSSize)preferredSizeForWidth:(CGFloat)width {
30   NSSize preferredSize = NSMakeSize(width, 0);
31
32   if ([notificationControllers_ count] == 0)
33     return preferredSize;
34
35   // If the first notification doesn't have an arrow, reserve empty space.
36   if (![[notificationControllers_ objectAtIndex:0] hasArrow])
37     preferredSize.height += autofill::kArrowHeight;
38
39   for (AutofillNotificationController* controller in
40        notificationControllers_.get()) {
41     preferredSize.height += [controller preferredSizeForWidth:width].height;
42   }
43
44   return preferredSize;
45 }
46
47 - (void)performLayout {
48   if ([notificationControllers_ count] == 0)
49     return;
50
51   NSRect remaining = [[self view] bounds];
52
53   if (![[notificationControllers_ objectAtIndex:0] hasArrow])
54     remaining.size.height -= autofill::kArrowHeight;
55
56   for (AutofillNotificationController* controller in
57        notificationControllers_.get()) {
58     NSRect viewRect;
59     NSSize size = [controller preferredSizeForWidth:NSWidth(remaining)];
60     NSDivideRect(remaining, &viewRect, &remaining, size.height, NSMaxYEdge);
61     [[controller view ] setFrame:viewRect];
62     [controller performLayout];
63   }
64   DCHECK_EQ(0, NSHeight(remaining));
65 }
66
67 - (void)setNotifications:(const autofill::DialogNotifications&)notifications {
68   notificationControllers_.reset([[NSMutableArray alloc] init]);
69   [[self view] setSubviews:@[]];
70
71   for (size_t i = 0; i < notifications.size(); ++i) {
72     // Create basic notification view.
73     const autofill::DialogNotification& notification = notifications[i];
74     base::scoped_nsobject<AutofillNotificationController>
75         notificationController([[AutofillNotificationController alloc]
76                                     initWithNotification:&notification
77                                                 delegate:delegate_]);
78
79     [notificationControllers_ addObject:notificationController];
80     [[self view] addSubview:[notificationController view]];
81   }
82 }
83
84 - (void)setAnchorView:(NSView*)anchorView {
85   anchorView_ = anchorView;
86 }
87
88 @end