summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/tab_view_picker_table.mm
blob: 95dec3b4aa86d239f2ede7f57aadbcc802465d9f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 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 "tab_view_picker_table.h"

#include "base/logging.h"

@interface TabViewPickerTable (Private)
// If a heading is shown, the indices between the tab items and the table rows
// are shifted by one. These functions convert between tab indices and table
// indices.
- (NSInteger)tabIndexFromTableIndex:(NSInteger)tableIndex;
- (NSInteger)tableIndexFromTabIndex:(NSInteger)tabIndex;

// Returns if |item| is the item shown as heading. If |heading_| is nil, this
// always returns |NO|.
- (BOOL)isHeadingItem:(id)item;

// Reloads the outline view and sets the selection to the row corresponding to
// the currently selected tab.
- (void)reloadDataWhileKeepingCurrentTabSelected;
@end

@implementation TabViewPickerTable

- (id)initWithFrame:(NSRect)frame {
  if ((self = [super initWithFrame:frame])) {
    [self setDelegate:self];
    [self setDataSource:self];
  }
  return self;
}

- (id)initWithCoder:(NSCoder*)coder {
  if ((self = [super initWithCoder:coder])) {
    [self setDelegate:self];
    [self setDataSource:self];
  }
  return self;
}

- (void)awakeFromNib {
  DCHECK(tabView_);
  DCHECK_EQ([self delegate], self);
  DCHECK_EQ([self dataSource], self);
  DCHECK(![self allowsEmptySelection]);
  DCHECK(![self allowsMultipleSelection]);

  // Suppress the "Selection changed" message that's sent while the table is
  // being built for the first time (this causes a selection change to index 0
  // and back to the prior index).
  id oldTabViewDelegate = [tabView_ delegate];
  [tabView_ setDelegate:nil];

  [self reloadDataWhileKeepingCurrentTabSelected];

  oldTabViewDelegate_ = oldTabViewDelegate;
  [tabView_ setDelegate:self];
}

- (NSString*)heading {
  return heading_.get();
}

- (void)setHeading:(NSString*)str {
  heading_.reset([str copy]);
  [self reloadDataWhileKeepingCurrentTabSelected];
}

- (void)reloadDataWhileKeepingCurrentTabSelected {
  NSInteger index =
      [tabView_ indexOfTabViewItem:[tabView_ selectedTabViewItem]];
  [self reloadData];
  if (heading_)
    [self expandItem:[self outlineView:self child:0 ofItem:nil]];
  NSIndexSet* indexSet =
      [NSIndexSet indexSetWithIndex:[self tableIndexFromTabIndex:index]];
  [self selectRowIndexes:indexSet byExtendingSelection:NO];
}

// NSTabViewDelegate methods.
- (void)         tabView:(NSTabView*)tabView
    didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
  DCHECK_EQ(tabView_, tabView);
  NSInteger index =
      [tabView_ indexOfTabViewItem:[tabView_ selectedTabViewItem]];
  NSIndexSet* indexSet =
      [NSIndexSet indexSetWithIndex:[self tableIndexFromTabIndex:index]];
  [self selectRowIndexes:indexSet byExtendingSelection:NO];
  if ([oldTabViewDelegate_
          respondsToSelector:@selector(tabView:didSelectTabViewItem:)]) {
    [oldTabViewDelegate_ tabView:tabView didSelectTabViewItem:tabViewItem];
  }
}

- (BOOL)            tabView:(NSTabView*)tabView
    shouldSelectTabViewItem:(NSTabViewItem*)tabViewItem {
  if ([oldTabViewDelegate_
          respondsToSelector:@selector(tabView:shouldSelectTabViewItem:)]) {
    return [oldTabViewDelegate_ tabView:tabView
                shouldSelectTabViewItem:tabViewItem];
  }
  return YES;
}

- (void)          tabView:(NSTabView*)tabView
    willSelectTabViewItem:(NSTabViewItem*)tabViewItem {
  if ([oldTabViewDelegate_
          respondsToSelector:@selector(tabView:willSelectTabViewItem:)]) {
    [oldTabViewDelegate_ tabView:tabView willSelectTabViewItem:tabViewItem];
  }
}

- (NSInteger)tabIndexFromTableIndex:(NSInteger)tableIndex {
  if (!heading_)
    return tableIndex;
  DCHECK(tableIndex > 0);
  return tableIndex - 1;
}

- (NSInteger)tableIndexFromTabIndex:(NSInteger)tabIndex {
  DCHECK_GE(tabIndex, 0);
  DCHECK_LT(tabIndex, [tabView_ numberOfTabViewItems]);
  if (!heading_)
    return tabIndex;
  return tabIndex + 1;
}

- (BOOL)isHeadingItem:(id)item {
  return item && item == heading_.get();
}

// NSOutlineViewDataSource methods.
- (NSInteger)  outlineView:(NSOutlineView*)outlineView
    numberOfChildrenOfItem:(id)item {
  if (!item)
    return heading_ ? 1 : [tabView_ numberOfTabViewItems];
  return (item == heading_.get()) ? [tabView_ numberOfTabViewItems] : 0;
}

- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item {
  return [self isHeadingItem:item];
}

- (id)outlineView:(NSOutlineView*)outlineView
            child:(NSInteger)index
           ofItem:(id)item {
  if (!item) {
    return heading_.get() ?
        heading_.get() : static_cast<id>([tabView_ tabViewItemAtIndex:index]);
  }
  return (item == heading_.get()) ? [tabView_ tabViewItemAtIndex:index] : nil;
}

- (id)            outlineView:(NSOutlineView*)outlineView
    objectValueForTableColumn:(NSTableColumn*)tableColumn
                       byItem:(id)item {
  if ([item isKindOfClass:[NSTabViewItem class]])
    return [static_cast<NSTabViewItem*>(item) label];
  if ([self isHeadingItem:item])
    return [item uppercaseString];
  return nil;
}

// NSOutlineViewDelegate methods.
- (void)outlineViewSelectionDidChange:(NSNotification*)notification {
  int row = [self selectedRow];
  [tabView_ selectTabViewItemAtIndex:[self tabIndexFromTableIndex:row]];
}

- (BOOL)outlineView:(NSOutlineView *)sender isGroupItem:(id)item {
  return [self isHeadingItem:item];
}

- (BOOL)outlineView:(NSOutlineView*)outlineView shouldExpandItem:(id)item {
  return [self isHeadingItem:item];
}

- (BOOL)outlineView:(NSOutlineView*)outlineView shouldCollapseItem:(id)item {
  return NO;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
  return ![self isHeadingItem:item];
}

// -outlineView:shouldShowOutlineCellForItem: is 10.6-only.
- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row {
  return NSZeroRect;
}

@end