summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/simple_content_exceptions_window_controller.mm
blob: 7beac9caf6bd5a58d6afdc39339b63a939e22a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/simple_content_exceptions_window_controller.h"

#include "app/l10n_util_mac.h"
#include "app/table_model_observer.h"
#include "base/logging.h"
#import "base/mac_util.h"
#import "base/scoped_nsobject.h"
#include "base/sys_string_conversions.h"
#include "grit/generated_resources.h"
#include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"

@interface SimpleContentExceptionsWindowController (Private)
- (id)initWithTableModel:(RemoveRowsTableModel*)model;
@end

namespace  {

const CGFloat kButtonBarHeight = 35.0;

SimpleContentExceptionsWindowController* g_exceptionWindow = nil;

}  // namespace

@implementation SimpleContentExceptionsWindowController

+ (id)controllerWithTableModel:(RemoveRowsTableModel*)model {
  if (!g_exceptionWindow) {
    g_exceptionWindow = [[SimpleContentExceptionsWindowController alloc]
        initWithTableModel:model];
  }
  return g_exceptionWindow;
}

- (id)initWithTableModel:(RemoveRowsTableModel*)model {
  NSString* nibpath = [mac_util::MainAppBundle()
      pathForResource:@"SimpleContentExceptionsWindow"
               ofType:@"nib"];
  if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
    model_.reset(model);

    // TODO(thakis): autoremember window rect.
    // TODO(thakis): sorting support.
  }
  return self;
}

- (void)awakeFromNib {
  DCHECK([self window]);
  DCHECK_EQ(self, [[self window] delegate]);
  DCHECK(tableView_);
  DCHECK(arrayController_);

  CGFloat minWidth = [[removeButton_ superview] bounds].size.width +
                     [[doneButton_ superview] bounds].size.width;
  [[self window] setMinSize:NSMakeSize(minWidth,
                                       [[self window] minSize].height)];
  NSDictionary* columns = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithInt:IDS_EXCEPTIONS_HOSTNAME_HEADER], @"hostname",
      [NSNumber numberWithInt:IDS_EXCEPTIONS_ACTION_HEADER], @"action",
      nil];
  [arrayController_ bindToTableModel:model_.get()
                         withColumns:columns
                    groupTitleColumn:@"hostname"];
}

- (void)setMinWidth:(CGFloat)minWidth {
  NSWindow* window = [self window];
  [window setMinSize:NSMakeSize(minWidth, [window minSize].height)];
  if ([window frame].size.width < minWidth) {
    NSRect frame = [window frame];
    frame.size.width = minWidth;
    [window setFrame:frame display:NO];
  }
}

- (void)windowWillClose:(NSNotification*)notification {
  g_exceptionWindow = nil;
  [self autorelease];
}

// Let esc close the window.
- (void)cancel:(id)sender {
  [self closeSheet:self];
}

- (void)keyDown:(NSEvent*)event {
  NSString* chars = [event charactersIgnoringModifiers];
  if ([chars length] == 1) {
    switch ([chars characterAtIndex:0]) {
      case NSDeleteCharacter:
      case NSDeleteFunctionKey:
        // Delete deletes.
        if ([[tableView_ selectedRowIndexes] count] > 0)
          [arrayController_ remove:event];
        return;
    }
  }
  [super keyDown:event];
}

- (void)attachSheetTo:(NSWindow*)window {
  [NSApp beginSheet:[self window]
     modalForWindow:window
      modalDelegate:self
     didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
}

- (void)sheetDidEnd:(NSWindow*)sheet
         returnCode:(NSInteger)returnCode
        contextInfo:(void*)context {
  [sheet close];
  [sheet orderOut:self];
}

- (IBAction)closeSheet:(id)sender {
  [NSApp endSheet:[self window]];
}


@end