- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / content_settings / cookie_details_view_controller.mm
1 // Copyright (c) 2010 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/content_settings/cookie_details_view_controller.h"
6
7 #include "base/mac/bundle_locations.h"
8 #import "base/mac/mac_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/content_settings/cookie_tree_node.h"
11 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/resource/resource_bundle.h"
14
15 namespace {
16 static const int kExtraMarginBelowWhenExpirationEditable = 5;
17 }
18
19 #pragma mark View Controller
20
21 @implementation CookieDetailsViewController
22 @dynamic hasExpiration;
23
24 - (id)init {
25   return [super initWithNibName:@"CookieDetailsView"
26                          bundle:base::mac::FrameworkBundle()];
27 }
28
29 - (void)awakeFromNib {
30   DCHECK(objectController_);
31 }
32
33 // Finds and returns the y offset of the lowest-most non-hidden
34 // text field in the view. This is used to shrink the view
35 // appropriately so that it just fits its visible content.
36 - (void)getLowestLabelVerticalPosition:(NSView*)view
37                    lowestLabelPosition:(float&)lowestLabelPosition {
38   if (![view isHidden]) {
39     if ([view isKindOfClass:[NSTextField class]]) {
40       NSRect frame = [view frame];
41       if (frame.origin.y < lowestLabelPosition) {
42         lowestLabelPosition = frame.origin.y;
43       }
44     }
45     for (NSView* subview in [view subviews]) {
46       [self getLowestLabelVerticalPosition:subview
47                        lowestLabelPosition:lowestLabelPosition];
48     }
49   }
50 }
51
52 - (void)setContentObject:(id)content {
53   // Make sure the view is loaded before we set the content object,
54   // otherwise, the KVO notifications to update the content don't
55   // reach the view and all of the detail values are default
56   // strings.
57   NSView* view = [self view];
58
59   [objectController_ setValue:content forKey:@"content"];
60
61   // View needs to be re-tweaked after setting the content object,
62   // since the expiration date may have changed, changing the
63   // size of the expiration popup.
64   [tweaker_ tweakUI:view];
65 }
66
67 - (void)shrinkViewToFit {
68   // Adjust the information pane to be exactly the right size
69   // to hold the visible text information fields.
70   NSView* view = [self view];
71   NSRect frame = [view frame];
72   float lowestLabelPosition = frame.origin.y + frame.size.height;
73   [self getLowestLabelVerticalPosition:view
74                    lowestLabelPosition:lowestLabelPosition];
75   float verticalDelta = lowestLabelPosition - frame.origin.y;
76
77   // Popup menu for the expiration is taller than the plain
78   // text, give it some more room.
79   if ([[[objectController_ content] details] canEditExpiration]) {
80     verticalDelta -= kExtraMarginBelowWhenExpirationEditable;
81   }
82
83   frame.origin.y += verticalDelta;
84   frame.size.height -= verticalDelta;
85   [[self view] setFrame:frame];
86 }
87
88 - (void)configureBindingsForTreeController:(NSTreeController*)treeController {
89   // There seems to be a bug in the binding logic that it's not possible
90   // to bind to the selection of the tree controller, the bind seems to
91   // require an additional path segment in the key, thus the use of
92   // selection.self rather than just selection below.
93   [objectController_ bind:@"contentObject"
94                  toObject:treeController
95               withKeyPath:@"selection.self"
96                   options:nil];
97 }
98
99 - (IBAction)setCookieDoesntHaveExplicitExpiration:(id)sender {
100   [[[objectController_ content] details] setHasExpiration:NO];
101 }
102
103 - (IBAction)setCookieHasExplicitExpiration:(id)sender {
104   [[[objectController_ content] details] setHasExpiration:YES];
105 }
106
107 - (BOOL)hasExpiration {
108   return [[[objectController_ content] details] hasExpiration];
109 }
110
111 @end