-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIPSWebContainerView.m
More file actions
executable file
·391 lines (310 loc) · 10 KB
/
Copy pathIPSWebContainerView.m
File metadata and controls
executable file
·391 lines (310 loc) · 10 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright (c) 2007, Marketcircle Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import "IPSWebContainerView.h"
#import "IPSWebAddressBarView.h"
#import "IPSWebToolbarView.h"
#import "IPSStatusBarView.h"
#import "IPSWebAddressField.h"
#import "IPSCategories.h"
#import "IPSScroller.h"
@interface IPSWebContainerView (Private)
- (float)scaleFactor;
- (void)setScaleFactor:(float)aFactor;
- (void)scaleToFit;
- (void)scaleToNatural;
@end
@implementation IPSWebContainerView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
isLoading = NO;
count = 1;
scaleFactor = 1.0;
shouldScaleToFit = NO;
}
return self;
}
- (void)awakeFromNib;
{
[webView setMaintainsBackForwardList:YES];
// -----------------------------------------------------------------------------
// Default Page
//[self loadPageWithAddress:@"http://sylvain.local:8099/content/home.html"];
[self toggleHideLocationBar:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressEstimateChanged:) name:WebViewProgressEstimateChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressFinished:) name:WebViewProgressFinishedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressStarted:) name:WebViewProgressStartedNotification object:nil];
[[self window] performSelector:@selector(makeFirstResponder:) withObject:[self window] afterDelay:0];
}
- (void)replaceScrollersInView:(NSScrollView *)scrollView {
// FIXME: this plain don't work.... :P
}
#pragma mark -
#pragma mark Accessors
- (BOOL)isLoading {
return isLoading;
}
- (void)setIsLoading:(BOOL)inIsLoading {
isLoading = inIsLoading;
}
- (BOOL)shouldScaleToFit {
return shouldScaleToFit;
}
- (void)setShouldScaleToFit:(BOOL)aBool {
if (shouldScaleToFit != aBool) {
shouldScaleToFit = aBool;
[self doScale];
}
}
#pragma mark -
#pragma mark Scaling Machinery
- (float)scaleFactor {
return scaleFactor;
}
- (void)setScaleFactor:(float)aFactor {
if (scaleFactor != aFactor) {
NSClipView *clipView;
if ((clipView = [webView firstDescendentClipView])) {
[[clipView window] disableFlushWindow];
[[clipView window] setAutodisplay:NO];
// scale back to natural size...
NSSize newUnitSize;
newUnitSize = NSMakeSize(1.0 / scaleFactor, 1.0 / scaleFactor);
[clipView scaleUnitSquareToSize:newUnitSize];
scaleFactor = aFactor;
// now scale to the target size...
newUnitSize = NSMakeSize(scaleFactor, scaleFactor);
[clipView scaleUnitSquareToSize:newUnitSize];
[[clipView window] setAutodisplay:YES];
[[clipView window] displayIfNeeded];
[[clipView window] enableFlushWindow];
[[clipView window] flushWindowIfNeeded];
}
}
}
- (void)doScale {
if ([self shouldScaleToFit])
[self scaleToFit];
else
[self scaleToNatural];
}
- (void)scaleToFit {
NSClipView *clipView = [webView firstDescendentClipView];
if (clipView == nil) return;
[self replaceScrollersInView:[clipView enclosingScrollView]];
NSView *documentView = [clipView documentView];
if (documentView == nil) return;
float wClip = NSWidth([clipView frame]);
float wDoc = NSWidth([documentView frame]);
if (wClip == 0 || wDoc == 0) return;
float newScaleFactor = wClip / wDoc;
[self setScaleFactor:newScaleFactor];
}
- (void)scaleToNatural {
if (scaleFactor != 1.0) {
[self setScaleFactor: 1.0];
}
}
#pragma mark -
#pragma mark Action Methods
- (IBAction)stop:(id)sender; {
[webView stopLoading:sender];
}
- (IBAction)reload:(id)sender;
{
if (isLoading) {
[webView stopLoading:sender];
}
else {
[webView reload:sender];
}
}
- (IBAction)toggleScaleToFit:(id)sender {
[self setShouldScaleToFit:![self shouldScaleToFit]];
}
- (IBAction)openLocation:(id)sender {
if (isLocationBarHidden) {
[self toggleHideLocationBar:sender];
}
[[self window] makeFirstResponder:[addressBar addressField]];
}
- (IBAction)toggleHideLocationBar:(id)sender;
{
if (isLocationBarHidden)
{
isLocationBarHidden = NO;
NSRect wf = [webView frame];
[webView setFrame:NSMakeRect(wf.origin.x,wf.origin.y,wf.size.width,wf.size.height - [addressBar frame].size.height)];
[addressBar setHidden:NO];
[self setNeedsDisplay:YES];
}
else
{
isLocationBarHidden = YES;
NSRect wf = [webView frame];
[webView setFrame:NSMakeRect(wf.origin.x,wf.origin.y,wf.size.width,wf.size.height + [addressBar frame].size.height)];
[addressBar setHidden:YES];
[self setNeedsDisplay:YES];
}
}
- (IBAction)addressFieldDidAct:(id)sender;
{
NSEvent *event = [NSApp currentEvent];
WebFrame *mainFrame = [webView mainFrame];
WebDataSource *src = [mainFrame provisionalDataSource];
if (!src)
src = [mainFrame dataSource];
NSString *currentUrlStr = [[[src request] URL] absoluteString];
if ([currentUrlStr isEqualToString:[sender stringValue]])
{
// ignore the request, since it can't be enter/return if it's not a key event
if ([event type] != NSKeyDown)
return;
BOOL shouldReload = NO;
NSString *eventCharacters;
unsigned int characterIndex, characterCount;
// strings are equal, so unless they hit return, we won't reload the page
eventCharacters = [event characters];
characterCount = [eventCharacters length];
for (characterIndex = 0; characterIndex < characterCount; characterIndex++) {
unichar key;
key = [eventCharacters characterAtIndex:characterIndex];
//NSLog(@"Got key %c", key);
switch (key) {
case NSInsertFunctionKey:
case NSInsertLineFunctionKey:
case '\003': // enter
case '\r': // return
shouldReload = YES;
break;
}
}
if (!shouldReload)
return;
}
NSString *urlStr = [sender stringValue];
// check if it needs a prefix
NSRange range = [urlStr rangeOfString:@"://" options:(0 & (~NSAnchoredSearch))];
if (!range.length)
{
urlStr = [NSString stringWithFormat:@"http://%@", urlStr];
}
//NSLog(@"loading url %@", urlStr);
[self loadPageWithAddress:urlStr];
}
- (void)loadPageWithAddress:(NSString *)addr;
{
[addressField removeFirstResponder];
WebFrame *mainFrame = [webView mainFrame];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]];
[mainFrame loadRequest:request];
}
#pragma mark Adrdress Field Delegate
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification {
//nothing for now
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
[[self window] performSelector:@selector(makeFirstResponder:) withObject:[self window] afterDelay:0];
}
#pragma mark Notifications
- (void)progressEstimateChanged:(NSNotification *)notif {
//NSLog(@"progress changed");
if ([webView estimatedProgress] != 1.0) {
[addressField setProgress:[webView estimatedProgress]];
}
}
- (void)progressFinished:(NSNotification *)notif {
//NSLog(@"progress finished");
isLoading = NO;
[self performSelector:@selector(setProgressToDone) withObject:nil afterDelay:0];
}
- (void)setProgressToDone {
//NSLog(@" set progress finished");
if (isLoading == NO) {
[addressField setProgress:1.0];
}
[self performSelector:@selector(finishedLoading) withObject:nil afterDelay:0.15];
}
- (void)progressStarted:(NSNotification *)notif {
//NSLog(@"progress started");
[statusBar startProgress];
[reloadButton setImage:[NSImage imageNamed:@"IPSWebAddressBarStopButton"]];
isLoading = YES;
}
- (void)finishedLoading {
//NSLog(@"finishing Loading");
[addressField setProgress:0.0];
if (isLoading == NO) {
[statusBar endProgress];
[reloadButton setImage:[NSImage imageNamed:@"IPSWebAddressBarReloadButton"]];
}
}
#pragma mark FrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame])
{
NSString *urlStr = [[[[frame dataSource] request] URL] absoluteString];
[addressBar setUrlString:urlStr];
}
}
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame])
{
NSString *urlStr = [[[[frame provisionalDataSource] request] URL] absoluteString];
// special case, clicking on screensplitr link should popup a safari
if ([urlStr caseInsensitiveCompare:@"http://www.plutinosoft.com/screensplitr"] == NSOrderedSame) {
[sender stopLoading:nil];
NSWorkspace* ws = [NSWorkspace sharedWorkspace];
NSURL* url = [NSURL URLWithString:urlStr];
[ws openURL:url];
return;
}
[addressBar setTitle:@"Loading..."];
[addressBar setUrlString:urlStr];
[toolbar validateBackForwardButtons];
}
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame])
{
[addressBar setTitle:title];
}
}
- (void)webView:(WebView *)sender resource:(id)identifier didReceiveContentLength: (unsigned)length fromDataSource:(WebDataSource *)dataSource {
[self doScale];
}
- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource {
[self doScale];
}
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
SEL action = [anItem action];
if (action == @selector(toggleHideLocationBar:))
{
if (isLocationBarHidden)
[anItem setTitle:@"Show Location Bar"];
else
[anItem setTitle:@"Hide Location Bar"];
} else if (action == @selector(toggleScaleToFit:))
{
[anItem setState:[self shouldScaleToFit] ? 1 : 0];
}
return YES;
}
@end