1/* 2 Project: Graphos 3 GRTextEditorView.m 4 5 Copyright (C) 2000-2017 GNUstep Application Project 6 7 Author: Enrico Sersale (original GDraw implementation) 8 Author: Ing. Riccardo Mottola 9 10 This application is free software; you can redistribute it and/or 11 modify it under the terms of the GNU General Public 12 License as published by the Free Software Foundation; either 13 version 2 of the License, or (at your option) any later version. 14 15 This application 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 GNU 18 Library General Public License for more details. 19 20 You should have received a copy of the GNU General Public 21 License along with this library; if not, write to the Free 22 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 23 */ 24 25 26 27#import "GRTextEditorView.h" 28 29@implementation GRTextEditorView 30 31- (id)initWithFrame:(NSRect)frameRect 32 withString:(NSString *)string 33 attributes:(NSDictionary *)attributes 34{ 35 self = [super initWithFrame: frameRect]; 36 if(self) 37 { 38 NSString *firstStr; 39 NSFont *f; 40 NSParagraphStyle *pstyle; 41 42 controlsView = [[NSView alloc] initWithFrame: NSMakeRect(0, 270, 500, 40)]; 43 [controlsView setAutoresizingMask: ~NSViewMaxYMargin & ~NSViewHeightSizable]; 44 45 fontField = [[NSTextField alloc] initWithFrame: NSMakeRect(10, 10, 190, 26)]; 46 [fontField setEditable:NO]; 47 [controlsView addSubview: fontField]; 48 49 chooseFontButton = [[NSButton alloc] initWithFrame: NSMakeRect(210, 10, 28, 20)]; 50 [chooseFontButton setTitle:@"..."]; 51 [chooseFontButton setTarget: self]; 52 [chooseFontButton setAction: @selector(chooseFont:)]; 53 [controlsView addSubview: chooseFontButton]; 54 55 leftButt = [[NSButton alloc] initWithFrame: NSMakeRect(255, 10, 20, 20)]; 56 [leftButt setButtonType: NSOnOffButton]; 57 [leftButt setImage: [NSImage imageNamed:@"txtAlignLeft.tiff"]]; 58 [leftButt setImagePosition: NSImageOnly]; 59 [leftButt setTarget: self]; 60 [leftButt setAction: @selector(changeTextAlignment:)]; 61 [controlsView addSubview: leftButt]; 62 63 centerButt = [[NSButton alloc] initWithFrame: NSMakeRect(285, 10, 20, 20)]; 64 [centerButt setButtonType: NSOnOffButton]; 65 [centerButt setImage: [NSImage imageNamed:@"txtAlignCenter.tiff"]]; 66 [centerButt setImagePosition: NSImageOnly]; 67 [centerButt setTarget: self]; 68 [centerButt setAction: @selector(changeTextAlignment:)]; 69 [controlsView addSubview: centerButt]; 70 71 rightButt = [[NSButton alloc] initWithFrame: NSMakeRect(315, 10, 20, 20)]; 72 [rightButt setButtonType: NSOnOffButton]; 73 [rightButt setImage: [NSImage imageNamed:@"txtAlignRight.tiff"]]; 74 [rightButt setImagePosition: NSImageOnly]; 75 [rightButt setTarget: self]; 76 [rightButt setAction: @selector(changeTextAlignment:)]; 77 [controlsView addSubview: rightButt]; 78 79 cancelButt = [[NSButton alloc] initWithFrame: NSMakeRect(350, 5, 80, 30)]; 80 [cancelButt setButtonType: NSMomentaryLight]; 81#if defined(__APPLE__) 82 [cancelButt setBezelStyle:NSRoundedBezelStyle]; 83#endif 84 [cancelButt setTitle: @"Cancel"]; 85 [cancelButt setTarget: self]; 86 [cancelButt setAction: @selector(okCancelPressed:)]; 87 [controlsView addSubview: cancelButt]; 88 89 okButt = [[NSButton alloc] initWithFrame: NSMakeRect(430, 5, 60, 30)]; 90 [okButt setButtonType: NSMomentaryLight]; 91#if defined(__APPLE__) 92 [okButt setBezelStyle:NSRoundedBezelStyle]; 93#endif 94 [okButt setTitle: @"Ok"]; 95 [okButt setTarget: self]; 96 [okButt setAction: @selector(okCancelPressed:)]; 97 [controlsView addSubview: okButt]; 98 99 if(attributes) 100 { 101 firstStr = [NSString stringWithString: string]; 102 f = [attributes objectForKey: NSFontAttributeName]; 103 fontSize = (int)[f pointSize]; 104 font = f; 105 pstyle = [attributes objectForKey: NSParagraphStyleAttributeName]; 106 parSpace = [pstyle paragraphSpacing]; 107 textAlignment = [pstyle alignment]; 108 if(textAlignment == NSLeftTextAlignment) 109 [leftButt setState: NSOnState]; 110 if(textAlignment == NSCenterTextAlignment) 111 [centerButt setState: NSOnState]; 112 if(textAlignment == NSRightTextAlignment) 113 [rightButt setState: NSOnState]; 114 115 } 116 else 117 { 118 firstStr = @"New Text"; 119 textAlignment = NSLeftTextAlignment; 120 [leftButt setState: NSOnState]; 121 fontSize = 12; 122 font = [NSFont systemFontOfSize:fontSize]; 123 pstyle = [NSMutableParagraphStyle defaultParagraphStyle]; 124 parSpace = [pstyle paragraphSpacing]; 125 } 126 [self addSubview: controlsView]; 127 128 scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 500, 260)]; 129 [scrollView setHasHorizontalScroller:NO]; 130 [scrollView setHasVerticalScroller:YES]; 131 [scrollView setAutoresizingMask: NSViewHeightSizable]; 132 133 theText = [[NSText alloc] initWithFrame: NSMakeRect(20, 0, 480, 250)]; 134 [theText setAlignment: textAlignment]; 135 [theText setString: firstStr]; 136 [self updateFontPreview:fontField :font]; 137 [scrollView setDocumentView: theText]; 138 [theText setFont: font]; 139 [theText setNeedsDisplay: YES]; 140 141 [self addSubview: scrollView]; 142 } 143 return self; 144} 145 146- (void) dealloc 147{ 148 [leftButt release]; 149 [centerButt release]; 150 [rightButt release]; 151 [cancelButt release]; 152 [okButt release]; 153 [chooseFontButton release]; 154 [fontField release]; 155 [controlsView release]; 156 [theText release]; 157 [scrollView release]; 158 [super dealloc]; 159} 160 161/* we accept to be the first responder 162 we want to be the first responder to handle changeFont from the FontPanel 163*/ 164- (BOOL)acceptsFirstResponder 165{ 166 return YES; 167} 168 169- (void)setFirstResponder 170{ 171 [[self window] makeFirstResponder:self]; 172} 173 174 175- (void)changeTextAlignment:(id)sender 176{ 177 NSButton *b = (NSButton *)sender; 178 if(b == leftButt) { 179 textAlignment = NSLeftTextAlignment; 180 [leftButt setState: NSOnState]; 181 [centerButt setState: NSOffState]; 182 [rightButt setState: NSOffState]; 183 } else if(b == centerButt) { 184 textAlignment = NSCenterTextAlignment; 185 [leftButt setState: NSOffState]; 186 [centerButt setState: NSOnState]; 187 [rightButt setState: NSOffState]; 188 } else if(b == rightButt) { 189 textAlignment = NSRightTextAlignment; 190 [leftButt setState: NSOffState]; 191 [centerButt setState: NSOffState]; 192 [rightButt setState: NSOnState]; 193 } 194 195 [theText setAlignment: textAlignment]; 196 [theText setNeedsDisplay: YES]; 197} 198 199- (IBAction) chooseFont:(id)sender 200{ 201 NSFontManager *fontMgr; 202 203 fontMgr = [NSFontManager sharedFontManager]; 204 205 [fontMgr setSelectedFont: font isMultiple:NO]; 206 [fontMgr setAction:@selector(changeFontAction:)]; 207 [fontMgr setDelegate:self]; 208 [fontMgr orderFrontFontPanel: self]; 209} 210 211- (void) changeFontAction:(id)sender 212{ 213 NSFont *newFont; 214 215 newFont = [sender convertFont: [fontField font]]; 216 217 if (newFont != nil) 218 { 219 [self updateFontPreview:fontField :newFont]; 220 font = newFont; 221 [font pointSize]; 222 [theText setFont: font]; 223 [theText setNeedsDisplay: YES]; 224 } 225} 226 227- (void) updateFontPreview:(NSTextField *)previewField :(NSFont *)aFont 228{ 229 NSString *fontName; 230 231 fontName = [aFont displayName]; 232 if (fontName) 233 { 234 [fontField setFont:[NSFont fontWithName: fontName size:12.0]]; 235 [previewField setStringValue: fontName]; 236 font = aFont; 237 } 238 else 239 { 240 [fontField setFont:[NSFont systemFontOfSize: -1]]; 241 [fontField setStringValue: @"(unset)"]; 242 } 243 244} 245 246 247- (void)okCancelPressed:(id)sender 248{ 249 if(sender == okButt) 250 result = NSAlertDefaultReturn; 251 else 252 result = NSAlertAlternateReturn; 253 [[self window] orderOut: self]; 254 [[NSApplication sharedApplication] stopModal]; 255} 256 257- (NSString *)textString 258{ 259 return [theText string]; 260} 261 262- (NSDictionary *)textAttributes 263{ 264 NSDictionary *dict; 265 NSMutableParagraphStyle *style; 266 267 style = [[NSMutableParagraphStyle alloc] init]; 268 [style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]]; 269 [style setAlignment: textAlignment]; 270 [style setParagraphSpacing: parSpace]; 271 dict = [NSDictionary dictionaryWithObjectsAndKeys: 272 [theText font], NSFontAttributeName, 273 style, NSParagraphStyleAttributeName, nil]; 274 [style release]; 275 return dict; 276} 277 278- (int)result 279{ 280 return result; 281} 282 283@end 284 285