forked from rsms/kod
/
KStatusBarController.mm
165 lines (128 loc) · 4.29 KB
/
KStatusBarController.mm
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
// Copyright (c) 2010-2011, Rasmus Andersson. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
#import "KStatusBarController.h"
#import "KSplitView.h"
#import "KDocument.h"
#import "HEventEmitter.h"
#import "common.h"
NSString * const KStatusBarDidChangeHiddenStateNotification =
@"KStatusBarDidChangeHiddenStateNotification";
@implementation KStatusBarController
- (void)dealloc {
[currentContents_ release];
[super dealloc];
}
- (void)_updateCursorPosition {
NSString *label;
if (!currentContents_) {
label = @"0, 0";
} else {
NSRange selection = [currentContents_.textView selectedRange];
// line
NSString *line;
NSUInteger lineno =
[currentContents_ lineNumberForLocation:selection.location];
NSRange lineRange = [currentContents_ rangeOfLineAtLineNumber:lineno];
if (selection.length > 0 &&
(lineRange.location + lineRange.length)
< (selection.location + selection.length) ) {
// find end lineno
NSUInteger endLineno = [currentContents_ lineNumberForLocation:
(selection.location + selection.length - 1)];
if (endLineno != lineno) {
line = [NSString stringWithFormat:@"%lu:%lu", lineno, endLineno];
} else {
line = [NSString stringWithFormat:@"%lu", lineno];
}
} else {
line = [NSString stringWithFormat:@"%lu", lineno];
}
// column
NSString *column;
NSUInteger colno = selection.location - lineRange.location;
if (selection.length == 0) {
column = [NSString stringWithFormat:@"%lu", colno];
} else {
column = [NSString stringWithFormat:@"%lu:%lu", colno, selection.length];
}
#if 0 // include character offset
// offset
NSString *offset;
if (selection.length == 0) {
offset = [NSString stringWithFormat:@"%lu", selection.location];
} else {
offset = [NSString stringWithFormat:@"%lu-%lu", selection.location,
selection.location + selection.length];
}
label = [NSString stringWithFormat:@"%@, %@, %@", line, column, offset];
#else
label = [NSString stringWithFormat:@"%@, %@", line, column];
#endif
}
[self statusBarView].cursorPositionTextField.stringValue = label;
}
- (void)updateWithContents:(KDocument*)contents {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
id oldContents = currentContents_;
if (!h_atomic_cas(¤tContents_, oldContents, contents))
return;
if (oldContents) {
[self stopObservingObject:currentContents_.textView];
[oldContents release];
}
if (currentContents_) {
[currentContents_ retain];
[self observe:NSTextViewDidChangeSelectionNotification
source:currentContents_.textView
handler:@selector(contentsTextViewDidChangeSelection:)];
}
[self _updateCursorPosition];
}
- (void)contentsTextViewDidChangeSelection:(NSNotification*)notification {
if (!self.isHidden)
[self _updateCursorPosition];
}
- (void)_updateToggleSplitViewButton {
[toggleSplitViewButton_ setState:splitView_.isCollapsed];
}
- (void)awakeFromNib {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(splitViewDidResize:)
name:NSSplitViewDidResizeSubviewsNotification
object:splitView_];
[self statusBarView].splitView = splitView_;
[self _updateToggleSplitViewButton];
}
- (KStatusBarView*)statusBarView {
return (KStatusBarView*)super.view;
}
- (void)splitViewDidResize:(NSNotification*)notification {
if (!self.isHidden) {
[self.statusBarView splitViewDidResize];
[self _updateToggleSplitViewButton];
}
}
- (IBAction)toggleSplitView:(id)sender {
[splitView_ toggleCollapse:sender];
}
- (BOOL)isHidden {
return self.statusBarView.isHidden;
}
- (void)setIsHidden:(BOOL)hidden {
if (self.isHidden != hidden) {
[self.statusBarView setHidden:hidden];
[self post:KStatusBarDidChangeHiddenStateNotification];
// trigger an update since updates are suspended while hidden
if (!hidden) {
[self _updateCursorPosition];
[self _updateToggleSplitViewButton];
}
[self.statusBarView splitViewDidResize];
}
}
- (IBAction)toggleStatusBarVisibility:(id)sender {
self.isHidden = !self.isHidden;
}
@end