Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 335 lines (290 sloc) 9.174 kb
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
//
// BrowserView.m
// Vienna
//
// Created by Steve on 8/26/05.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "BrowserView.h"
#import "Preferences.h"
#import "Constants.h"
#import <PSMTabBarControl/PSMTabBarControl.h>

@interface NSTabView (BrowserViewAdditions)
-(NSTabViewItem *)tabViewItemWithIdentifier:(id)identifier;
@end

@implementation NSTabView (BrowserViewAdditions)

/* tabViewItemWithIdentifier
* Returns the tab view item that matches the specified identifier.
*/
-(NSTabViewItem *)tabViewItemWithIdentifier:(id)identifier
{
NSInteger i = [self indexOfTabViewItemWithIdentifier:identifier];
return (i != NSNotFound ? [self tabViewItemAtIndex:i] : nil);
}
@end

@implementation BrowserView

-(void)awakeFromNib
{
[[tabView tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Articles", nil)];

//Metal is the default
//[tabBarControl setStyleNamed:@"Metal"];

[tabBarControl setHideForSingleTab:YES];
[tabBarControl setUseOverflowMenu:YES];
[tabBarControl setAllowsBackgroundTabClosing:YES];
[tabBarControl setAutomaticallyAnimates:NO];
[tabBarControl setSizeCellsToFit:YES];
[tabBarControl setCellMinWidth:60];
[tabBarControl setCellMaxWidth:350];

[tabBarControl setShowAddTabButton:YES];
[[tabBarControl addTabButton] setTarget:[NSApp delegate]];
[[tabBarControl addTabButton] setAction:@selector(newTab:)];
}

/* stringForToolTip
* Returns the tooltip for the tab specified by the userData object. This is the tab's full title which
* may have been truncated for display.
//XXX Not being used...
*/
-(NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)userData
{
return [[tabView tabViewItemWithIdentifier:(NSView *)userData] label];
}

/* setPrimaryTabItemView
* Sets the primary tab view. This is the view that is always displayed and
* occupies the first tab position.
*/
-(void)setPrimaryTabItemView:(NSView<BaseView> *)newPrimaryTabItemView
{
[newPrimaryTabItemView retain];

NSTabViewItem * item;
if (primaryTabItemView == nil)
{
// This should only be called on launch
item = [tabView tabViewItemAtIndex:0];
}
else
{
item = [tabView tabViewItemWithIdentifier:primaryTabItemView];
}

[item setIdentifier:newPrimaryTabItemView];
[item setView:newPrimaryTabItemView];

[primaryTabItemView release];
primaryTabItemView = newPrimaryTabItemView;

[primaryTabItemView setNeedsDisplay:YES];
[self setActiveTabToPrimaryTab];
}

/* activeTabItemView
* Returns the view associated with the active tab.
*/
-(NSView<BaseView> *)activeTabItemView
{
return [[tabView selectedTabViewItem] identifier];
}

/* setActiveTabToPrimaryTab
* Make the primary tab the active tab.
*/
-(void)setActiveTabToPrimaryTab
{
[self showTabItemView:primaryTabItemView];
}

/* primaryTabItemView
* Return the primary tab view.
*/
-(NSView<BaseView> *)primaryTabItemView
{
return primaryTabItemView;
}

/* createNewTabWithView
* Create a new tab with the specified view. If makeKey is YES then the new tab is
* made active, otherwise the current tab stays active.
*/
-(void)createNewTabWithView:(NSView<BaseView> *)newTabView makeKey:(BOOL)keyIt
{
NSTabViewItem *tabViewItem = [[NSTabViewItem alloc] initWithIdentifier:newTabView];
[tabViewItem setView:newTabView];
[tabView addTabViewItem:tabViewItem];
[tabViewItem release];

if (keyIt) [self showTabItemView:newTabView];
}

/* setTabTitle
* Sets the title of the specified tab then redraws the tab bar.
*/
-(void)setTabItemViewTitle:(NSView *)inTabView title:(NSString *)newTitle
{
[[tabView tabViewItemWithIdentifier:inTabView] setLabel:newTitle];
}

/* tabTitle
* Returns the title of the specified tab. May be an empty string.
*/
-(NSString *)tabItemViewTitle:(NSView *)tabItemView
{
return [[tabView tabViewItemWithIdentifier:tabItemView] label];
}

/* closeAllTabs
* Close all tabs.
*/
-(void)closeAllTabs
{
int count = [tabView numberOfTabViewItems];
int i;
for ((i = (count - 1)); i >= 0; i--) {
NSTabViewItem * item = [tabView tabViewItemAtIndex:i];
if ([item identifier] != primaryTabItemView)
{
[tabView removeTabViewItem:item];
}
}
}

/* closeTab
* Close the specified tab unless it is the primary tab, in which case
* we do nothing.
*/
-(void)closeTabItemView:(NSView *)tabItemView
{
if (tabItemView != primaryTabItemView) {
NSTabViewItem *tabViewItem = [tabView tabViewItemWithIdentifier:tabItemView];
int oldIndex = [tabView indexOfTabViewItem:tabViewItem];

if ([tabView numberOfTabViewItems] > (oldIndex + 1)) {
[tabView selectTabViewItemAtIndex:(oldIndex + 1)];
}

[tabView removeTabViewItem:tabViewItem];
}
}

- (BOOL)tabView:(NSTabView *)inTabView shouldCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
if ([tabViewItem identifier] == primaryTabItemView)
{
return NO;
}
else
{
int oldIndex = [tabView indexOfTabViewItem:tabViewItem];
if ([tabView numberOfTabViewItems] > (oldIndex + 1)) {
[tabView selectTabViewItemAtIndex:(oldIndex + 1)];
}

return YES;
}
}

/* countOfTabs
* Returns the total number of tabs.
*/
-(int)countOfTabs
{
return [tabView numberOfTabViewItems];
}

/* showTabVew
* Makes the specified tab active if not already and post a notification.
*/
-(void)showTabItemView:(NSView *)theTabView
{
if ([tabView tabViewItemWithIdentifier:theTabView]) {
[tabView selectTabViewItemWithIdentifier:theTabView];
}
}

/* showPreviousTab
* Switch to the previous tab in the view order. Wrap round to the end
* if we're at the beginning.
*/
-(void)showPreviousTab
{
if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0)
[tabView selectLastTabViewItem:self];
else
[tabView selectPreviousTabViewItem:self];
}

/* showNextTab
* Switch to the next tab in the tab order. Wrap round to the beginning
* if we're at the end.
*/
-(void)showNextTab
{
if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == ([tabView numberOfTabViewItems] - 1))
[tabView selectFirstTabViewItem:self];
else
[tabView selectNextTabViewItem:self];
}

/* didSelectTabViewItem
* Called when the tab is changed.
*/
-(void)tabView:(NSTabView *)inTabView didSelectTabViewItem:(NSTabViewItem *)inTabViewItem
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MA_Notify_TabChanged" object:[inTabViewItem identifier]];
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MA_Notify_TabCountChanged" object:nil];
}

/* disableTabCloseForTabViewItem
* Returns whether the tab close should be disabled for the specified item. We disable the close button
* for the primary item.
*/
-(BOOL)tabView:(NSTabView *)aTabView disableTabCloseForTabViewItem:(NSTabViewItem *)tabViewItem
{
return ([tabViewItem identifier] == primaryTabItemView);
}

/* tabView:shouldDragTabViewItem:fromTabBar:
* Should a tab view item be allowed to be dragged?
*/
- (BOOL)tabView:(NSTabView *)aTabView shouldDragTabViewItem:(NSTabViewItem *)tabViewItem fromTabBar:(PSMTabBarControl *)tabBarControl
{
return YES;
}

/* tabView:shouldDropTabViewItem:inTabBar:
* Should a tab view item drop be accepted?
*/
- (BOOL)tabView:(NSTabView *)aTabView shouldDropTabViewItem:(NSTabViewItem *)tabViewItem inTabBar:(PSMTabBarControl *)tabBarControl
{
return YES;
}

/* tabView:didDropTabViewItem:inTabBar:
* A drag & drop operation of a tab view item was completed.
*/
- (void)tabView:(NSTabView*)aTabView didDropTabViewItem:(NSTabViewItem *)tabViewItem inTabBar:(PSMTabBarControl *)tabBarControl
{
}

/* tabView:shouldAllowTabViewItem:toLeaveTabBar:
* Should a tab view item be allowed to leave the tab bar?
*/
- (BOOL)tabView:(NSTabView *)aTabView shouldAllowTabViewItem:(NSTabViewItem *)tabViewItem toLeaveTabBar:(PSMTabBarControl *)tabBarControl;
{
return NO;
}

#pragma mark -
/* saveOpenTabs
* Persist the URLs of each open tab to the preferences so they can be
* restored when we reload.
*/
-(void)saveOpenTabs
{
NSMutableArray *tabLinks = [[NSMutableArray alloc] initWithCapacity:[self countOfTabs]];

for (NSTabViewItem * tabViewItem in [tabView tabViewItems])
{
NSView<BaseView> * theView = [tabViewItem identifier];
NSString * tabLink = [theView viewLink];
if (tabLink != nil)
[tabLinks addObject:tabLink];
}

[[Preferences standardPreferences] setObject:tabLinks forKey:MAPref_TabList];
[tabLinks release];

[[Preferences standardPreferences] savePreferences];
}

/* dealloc
* Clean up behind ourselves.
*/
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];

[super dealloc];
}
@end
Something went wrong with that request. Please try again.