source: trunk/macosx/URLSheetWindowController.m @ 11872

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

fix a crash with the URL window, and memory leak with the URL window, and update the "Open" button of the URL window when shown

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- (BOOL) 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   
78    [fOpenButton setFrame: openFrame];
79   
80    cancelFrame.origin.x = NSMinX(openFrame) - NSWidth(cancelFrame);
81    [fCancelButton setFrame: cancelFrame];
82}
83
84- (IBAction) beginSheetForWindow: (NSWindow *) window
85{
86    [NSApp beginSheet: [self window] modalForWindow: window modalDelegate: self
87        didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: nil];
88}
89
90- (void) openURLEndSheet: (id) sender
91{
92    [[self window] orderOut: sender];
93    [NSApp endSheet: [self window] returnCode: 1];
94}
95
96- (void) openURLCancelEndSheet: (id) sender
97{
98    [[self window] orderOut: sender];
99    [NSApp endSheet: [self window] returnCode: 0];
100}
101
102- (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
103{
104    [urlString release];
105    urlString = [[fTextField stringValue] retain];
106    [fController urlSheetDidEnd: self url: urlString returnCode: returnCode];
107}
108
109- (void) controlTextDidChange: (NSNotification *) notification
110{
111    [self updateOpenButtonForURL: [fTextField stringValue]];
112}
113
114@end
115
116@implementation URLSheetWindowController (Private)
117
118- (BOOL) updateOpenButtonForURL: (NSString *) string
119{
120    BOOL enable = YES;
121    if ([string isEqualToString: @""])
122        enable = NO;
123    else
124    {
125        NSRange prefixRange = [string rangeOfString: @"://"];
126        if (prefixRange.location != NSNotFound && [string length] == NSMaxRange(prefixRange))
127            enable = NO;
128    }
129   
130    [fOpenButton setEnabled: enable];
131}
132
133@end
Note: See TracBrowser for help on using the repository browser.