-
Notifications
You must be signed in to change notification settings - Fork 0
/
WLMainFrameController+TabControl.m
137 lines (116 loc) · 4.43 KB
/
WLMainFrameController+TabControl.m
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
//
// WLMainFrameController+TabControl.m
// Welly
//
// Created by K.O.ed on 10-4-30.
// Copyright 2010 Welly Group. All rights reserved.
//
#import "WLMainFrameController+TabControl.h"
#import "WLTabBarControl.h"
#import "WLTabView.h"
#import "WLConnection.h"
#import "WLSite.h"
#import "WLGlobalConfig.h"
@interface WLMainFrameController ()
- (void)updateEncodingMenu;
- (void)exitFullScreenMode;
@end
@implementation WLMainFrameController (TabControl)
- (void)initializeTabControl {
// tab control style
[_tabBarControl setCanCloseOnlyTab:YES];
NSAssert([_tabBarControl delegate] == self, @"set in .nib");
//show a new-tab button
[_tabBarControl setShowAddTabButton:YES];
[[_tabBarControl addTabButton] setTarget:self];
[[_tabBarControl addTabButton] setAction:@selector(newTab:)];
//_tabView = (WLTabView *)[_tabBarControl tabView];
// open the portal
// the switch
[self tabViewDidChangeNumberOfTabViewItems:_tabView];
[_tabBarControl setMainController:[self retain]];
}
#pragma mark -
#pragma mark Actions
- (IBAction)newTab:(id)sender {
// Draw the portal and entering the portal control mode if needed...
if ([WLGlobalConfig shouldEnableCoverFlow]) {
[_tabView newTabWithCoverFlowPortal];
} else {
[self newConnectionWithSite:[WLSite site]];
// let user input
[_mainWindow makeFirstResponder:_addressBar];
}
}
- (IBAction)selectNextTab:(id)sender {
[_tabBarControl selectNextTabViewItem:sender];
}
- (IBAction)selectPrevTab:(id)sender {
[_tabBarControl selectPreviousTabViewItem:sender];
}
- (IBAction)closeTab:(id)sender {
if ([_tabView numberOfTabViewItems] == 0) return;
// Here, sometimes it may throw a exception...
@try {
[_tabBarControl removeTabViewItem:[_tabView selectedTabViewItem]];
}
@catch (NSException * e) {
}
}
#pragma mark -
#pragma mark TabView delegation
- (BOOL)tabView:(NSTabView *)tabView shouldCloseTabViewItem:(NSTabViewItem *)tabViewItem {
// Restore from full screen firstly
[self exitFullScreenMode];
// TODO: why not put these in WLTabView?
if (![[[tabViewItem identifier] content] isKindOfClass:[WLConnection class]] ||
![[[tabViewItem identifier] content] isConnected])
return YES;
if (![[NSUserDefaults standardUserDefaults] boolForKey:WLConfirmOnCloseEnabledKeyName])
return YES;
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Are you sure you want to close this tab?", @"Sheet Title")
defaultButton:NSLocalizedString(@"Close", @"Default Button")
alternateButton:NSLocalizedString(@"Cancel", @"Cancel Button")
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"The connection is still alive. If you close this tab, the connection will be lost. Do you want to close this tab anyway?", @"Sheet Message")];
if ([alert runModal] == NSAlertDefaultReturn)
return YES;
return NO;
}
- (void)tabView:(NSTabView *)tabView willCloseTabViewItem:(NSTabViewItem *)tabViewItem {
// close the connection
if ([[[tabViewItem identifier] content] isKindOfClass:[WLConnection class]])
[[[tabViewItem identifier] content] close];
}
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
NSAssert(tabView == _tabView, @"tabView");
[_addressBar setStringValue:@""];
if ([[[tabViewItem identifier] content] isKindOfClass:[WLConnection class]]) {
WLConnection *connection = [[tabViewItem identifier] content];
WLSite *site = [connection site];
if (connection && [site address]) {
[_addressBar setStringValue:[site address]];
[connection resetMessageCount];
}
[_mainWindow makeFirstResponder:tabView];
[self updateEncodingMenu];
#define CELLSTATE(x) ((x) ? NSOnState : NSOffState)
[_detectDoubleByteButton setState:CELLSTATE([site shouldDetectDoubleByte])];
[_detectDoubleByteMenuItem setState:CELLSTATE([site shouldDetectDoubleByte])];
[_autoReplyButton setState:CELLSTATE([site shouldAutoReply])];
[_autoReplyMenuItem setState:CELLSTATE([site shouldAutoReply])];
[_mouseButton setState:CELLSTATE([site shouldEnableMouse])];
#undef CELLSTATE
}
}
- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView {
// all tab closed, no didSelectTabViewItem will happen
if ([tabView numberOfTabViewItems] == 0) {
if ([WLGlobalConfig shouldEnableCoverFlow]) {
[_mainWindow makeFirstResponder:tabView];
} else {
[_mainWindow makeFirstResponder:_addressBar];
}
}
}
@end