source: trunk/macosx/URLSheetWindowController.m @ 11873

Last change on this file since 11873 was 11873, checked in by livings124, 11 years ago

fix a warning

File size: 4.2 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2011 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "URLSheetWindowController.h"
26#import "Controller.h"
27
28@interface URLSheetWindowController (Private)
29
30- (void) updateOpenButtonForURL: (NSString *) string;
31
32@end
33
34@implementation URLSheetWindowController
35
36NSString * urlString = nil;
37
38- (id) initWithController: (Controller *) controller
39{
40    if ((self = [self initWithWindowNibName: @"URLSheetWindow"]))
41    {
42        fController = controller;
43    }
44    return self;
45}
46
47- (void) awakeFromNib
48{
49    [fLabelField setStringValue: NSLocalizedString(@"Internet address of torrent file:", "URL sheet label")];
50   
51    if (urlString)
52    {
53        [fTextField setStringValue: urlString];
54        [fTextField selectText: self];
55       
56        [self updateOpenButtonForURL: urlString];
57    }
58   
59    [fOpenButton setTitle: NSLocalizedString(@"Open", "URL sheet button")];
60    [fCancelButton setTitle: NSLocalizedString(@"Cancel", "URL sheet button")];
61   
62    [fOpenButton sizeToFit];
63    [fCancelButton sizeToFit];
64   
65    //size the two buttons the same
66    NSRect openFrame = [fOpenButton frame];
67    openFrame.size.width += 10.0;
68    NSRect cancelFrame = [fCancelButton frame];
69    cancelFrame.size.width += 10.0;
70   
71    if (NSWidth(openFrame) > NSWidth(cancelFrame))
72        cancelFrame.size.width = NSWidth(openFrame);
73    else
74        openFrame.size.width = NSWidth(cancelFrame);
75   
76    openFrame.origin.x = NSWidth([[self window] frame]) - NSWidth(openFrame) - 20.0 + 6.0; //I don't know why the extra 6.0 is needed
77    [fOpenButton setFrame: openFrame];
78   
79    cancelFrame.origin.x = NSMinX(openFrame) - NSWidth(cancelFrame);
80    [fCancelButton setFrame: cancelFrame];
81}
82
83- (IBAction) beginSheetForWindow: (NSWindow *) window
84{
85    [NSApp beginSheet: [self window] modalForWindow: window modalDelegate: self
86        didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: nil];
87}
88
89- (void) openURLEndSheet: (id) sender
90{
91    [[self window] orderOut: sender];
92    [NSApp endSheet: [self window] returnCode: 1];
93}
94
95- (void) openURLCancelEndSheet: (id) sender
96{
97    [[self window] orderOut: sender];
98    [NSApp endSheet: [self window] returnCode: 0];
99}
100
101- (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
102{
103    [urlString release];
104    urlString = [[fTextField stringValue] retain];
105    [fController urlSheetDidEnd: self url: urlString returnCode: returnCode];
106}
107
108- (void) controlTextDidChange: (NSNotification *) notification
109{
110    [self updateOpenButtonForURL: [fTextField stringValue]];
111}
112
113@end
114
115@implementation URLSheetWindowController (Private)
116
117- (void) updateOpenButtonForURL: (NSString *) string
118{
119    BOOL enable = YES;
120    if ([string isEqualToString: @""])
121        enable = NO;
122    else
123    {
124        NSRange prefixRange = [string rangeOfString: @"://"];
125        if (prefixRange.location != NSNotFound && [string length] == NSMaxRange(prefixRange))
126            enable = NO;
127    }
128   
129    [fOpenButton setEnabled: enable];
130}
131
132@end
Note: See TracBrowser for help on using the repository browser.