1/* BrowserViewerPref.m 2 * 3 * Copyright (C) 2003-2016 Free Software Foundation, Inc. 4 * 5 * Author: Enrico Sersale <enrico@imago.ro> 6 * Date: August 2001 7 * 8 * This file is part of the GNUstep GWorkspace application 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. 23 */ 24 25#import <Foundation/Foundation.h> 26#import <AppKit/AppKit.h> 27#import <GNUstepBase/GNUstep.h> 28 29#import "BrowserViewerPref.h" 30 31 32#define RESIZER_W 16 33#define RESIZER_Y 48 34 35#define MINIMUM_WIDTH 120 36#define DEFAULT_WIDTH 150 37#define MAXIMUM_WIDTH 362 38 39static NSString *nibName = @"BrowserViewerPref"; 40 41@implementation Resizer 42 43- (void)dealloc 44{ 45 RELEASE (arrow); 46 [super dealloc]; 47} 48 49- (id)initForController:(id)acontroller 50{ 51 self = [super init]; 52 [self setFrame: NSMakeRect(0, 0, RESIZER_W, RESIZER_W)]; 53 controller = acontroller; 54 ASSIGN (arrow, [NSImage imageNamed: @"RightArr.tiff"]); 55 return self; 56} 57 58- (void)mouseDown:(NSEvent *)theEvent 59{ 60 [controller mouseDownOnResizer: theEvent]; 61} 62 63- (void)drawRect:(NSRect)rect 64{ 65 [super drawRect: rect]; 66 [arrow compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver]; 67} 68 69@end 70 71@implementation BrowserViewerPref 72 73- (void)dealloc 74{ 75 RELEASE (colExample); 76 RELEASE (prefbox); 77 [super dealloc]; 78} 79 80- (id)init 81{ 82 self = [super init]; 83 84 if (self) 85 { 86 if ([NSBundle loadNibNamed: nibName owner: self] == NO) 87 { 88 NSLog(@"failed to load %@!", nibName); 89 } 90 else 91 { 92 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 93 NSString *widthStr = [defaults objectForKey: @"browserColsWidth"]; 94 95 RETAIN (prefbox); 96 RELEASE (win); 97 98 if (widthStr) 99 columnsWidth = [widthStr intValue]; 100 else 101 columnsWidth = DEFAULT_WIDTH; 102 103 [colExample setBorderType: NSBezelBorder]; 104 [colExample setHasHorizontalScroller: NO]; 105 [colExample setHasVerticalScroller: YES]; 106 107 resizer = [[Resizer alloc] initForController: self]; 108 [resizer setFrame: NSMakeRect(0, 0, RESIZER_W, RESIZER_W)]; 109 [resizerBox setContentView: resizer]; 110 [self tile]; 111 112 /* Internationalization */ 113 [controlsbox setTitle: NSLocalizedString(@"Column Width", @"")]; 114 [setButt setTitle: NSLocalizedString(@"Use Default Settings", @"")]; 115 } 116 } 117 118 return self; 119} 120 121- (NSView *)prefView 122{ 123 return prefbox; 124} 125 126- (NSString *)prefName 127{ 128 return NSLocalizedString(@"Browser", @""); 129} 130 131- (void)tile 132{ 133 NSRect frameRect; 134 135 frameRect = [colExample frame]; 136 frameRect.size.width = columnsWidth; 137 [colExample setFrame: frameRect]; 138 [resizerBox setFrameOrigin: NSMakePoint(columnsWidth + frameRect.origin.x, RESIZER_Y)]; 139 [controlsbox setNeedsDisplay: YES]; 140} 141 142- (void)mouseDownOnResizer:(NSEvent *)theEvent 143{ 144 NSApplication *app = [NSApplication sharedApplication]; 145 int orx = (int)[controlsbox convertPoint: [theEvent locationInWindow] fromView: nil].x; 146 NSUInteger eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; 147 int newWidth = (int)[colExample bounds].size.width; 148 NSEvent *e; 149 150 [controlsbox lockFocus]; 151 [[NSRunLoop currentRunLoop] limitDateForMode: NSEventTrackingRunLoopMode]; 152 153 e = [app nextEventMatchingMask: eventMask 154 untilDate: [NSDate distantFuture] 155 inMode: NSEventTrackingRunLoopMode 156 dequeue: YES]; 157 158 while ([e type] != NSLeftMouseUp) 159 { 160 int x = (int)[controlsbox convertPoint: [e locationInWindow] fromView: nil].x; 161 int diff = x - orx; 162 163 if ((newWidth + diff < MAXIMUM_WIDTH) && (newWidth + diff > MINIMUM_WIDTH)) 164 { 165 NSRect frameExample; 166 167 frameExample = [colExample frame]; 168 newWidth += diff; 169 [resizerBox setFrameOrigin: NSMakePoint(frameExample.origin.x + newWidth, RESIZER_Y)]; 170 [resizerBox setNeedsDisplay: YES]; 171 172 frameExample.size.width = newWidth; 173 [colExample setFrame: frameExample]; 174 [colExample setNeedsDisplay: YES]; 175 176 [controlsbox setNeedsDisplay: YES]; 177 178 orx = x; 179 } 180 181 e = [app nextEventMatchingMask: eventMask 182 untilDate: [NSDate distantFuture] 183 inMode: NSEventTrackingRunLoopMode 184 dequeue: YES]; 185 } 186 187 [controlsbox unlockFocus]; 188 [self setNewWidth: (int)[colExample bounds].size.width]; 189 [setButt setEnabled: YES]; 190} 191 192- (void)setNewWidth:(int)w 193{ 194 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 195 196 [defaults setObject: [NSString stringWithFormat: @"%i", w] 197 forKey: @"browserColsWidth"]; 198 199 columnsWidth = w; 200 201 [[NSNotificationCenter defaultCenter] 202 postNotificationName: @"GWBrowserColumnWidthChangedNotification" 203 object: [NSNumber numberWithInt: w]]; 204 205 [defaults synchronize]; 206} 207 208- (IBAction)setDefaultWidth:(id)sender 209{ 210 columnsWidth = DEFAULT_WIDTH; 211 [self setNewWidth: columnsWidth]; 212 [self tile]; 213 [setButt setEnabled: NO]; 214} 215 216@end 217