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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
//
// MControlForm.m
// MySQLGUICommon
//
// Created by Alfredo Kojima on 06/4/30.
// Copyright 2006 MySQL AB. All rights reserved.
//
#import "MControlForm.h"
#define HORIZ_SPACING 4
@implementation MFormCell
- (NSMutableDictionary*)_attributesDict
{
static NSMutableDictionary *attributes= 0;
if (!attributes)
{
NSMutableParagraphStyle *paragraphStyle;
paragraphStyle= [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
//if (_cell.wraps)
// [paragraphStyle setLineBreakMode: NSLineBreakByWordWrapping];
//else
[paragraphStyle setLineBreakMode: NSLineBreakByClipping];
[paragraphStyle setAlignment: NSRightTextAlignment];
attributes= [[NSMutableDictionary alloc] init];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[attributes setObject:[NSFont systemFontOfSize:[NSFont systemFontSize]] forKey:NSFontAttributeName];
}
else
{
NSMutableParagraphStyle *paragraphStyle;
paragraphStyle= [attributes objectForKey: NSParagraphStyleAttributeName];
[paragraphStyle setLineBreakMode: NSLineBreakByClipping];
[paragraphStyle setAlignment: NSRightTextAlignment];
}
return attributes;
}
- (NSMutableDictionary*)_descriptionAttributesDict
{
NSMutableDictionary *attr= [self _attributesDict];
NSMutableParagraphStyle *paragraphStyle;
paragraphStyle= [attr objectForKey: NSParagraphStyleAttributeName];
[paragraphStyle setLineBreakMode: NSLineBreakByWordWrapping];
[paragraphStyle setAlignment: NSLeftTextAlignment];
return attr;
}
- (NSSize)sizeForString:(NSString*)aString withFont:(NSFont*)aFont width:(float)aWidth
{
if (![aString hasSuffix:@"\n"])
aString = [NSString stringWithFormat:@"%@\n", aString];
NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:aString] autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(aWidth,1e7)] autorelease];
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[layoutManager setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
[textStorage addAttribute:NSFontAttributeName value:aFont range:NSMakeRange(0, [textStorage length])];
[textContainer setLineFragmentPadding:0.0];
[layoutManager glyphRangeForTextContainer:textContainer];
return [layoutManager usedRectForTextContainer:textContainer].size;
}
- (float)calcTitleWidth
{
return [[self font] widthOfString:[self stringValue]];
}
- (void)setTitleWidth:(float)width
{
_titleWidth= width;
}
- (float)titleWidth
{
if (_titleWidth == 0)
_titleWidth= [self calcTitleWidth];
return _titleWidth;
}
- (NSSize)cellSizeForWidth:(float)aWidth
{
NSSize size= [super cellSize];
NSSize contentSize= [_contentCell cellSize];
size.height= MAX(size.height, contentSize.height);
size.width+= contentSize.width;
if (_descriptionString)
{
NSSize descrSize;
descrSize= [self sizeForString:_descriptionString
withFont:_descriptionFont?:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]
width:aWidth];
size.width+= descrSize.width;
size.height= MAX(size.height, descrSize.height);
_descriptionSize= descrSize;
}
return size;
}
- (NSSize)cellSize
{
NSSize size= [super cellSize];
NSSize contentSize= [_contentCell cellSize];
size.height= MAX(size.height, contentSize.height);
size.width+= contentSize.width;
if (_descriptionString)
{
size.width+= _descriptionSize.width;
size.height= MAX(size.height, _descriptionSize.height);
}
return size;
}
- (BOOL)trackMouse:(NSEvent *)theEvent
inRect:(NSRect)cellFrame
ofView:(NSView *)controlView
untilMouseUp:(BOOL)untilMouseUp
{
NSPoint location= [controlView convertPoint:[theEvent locationInWindow] fromView:nil];
float x= [self titleWidth];
if (location.x >= x && location.x < x + [_contentCell cellSize].width)
{
cellFrame.origin.x= x;
cellFrame.size= [_contentCell cellSize];
return [_contentCell trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];
}
return NO;
}
- (void)setTitle:(NSString*)title
{
[super setStringValue:title];
}
- (NSString*)title
{
return [super stringValue];
}
- (void)setDescriptionFont:(NSFont*)font
{
if (_descriptionFont != font)
{
[_descriptionFont release];
_descriptionFont= [font retain];
}
}
- (NSFont*)descriptionFont
{
return _descriptionFont;
}
- (void)setDescription:(NSString*)description
{
if (_descriptionString != description)
{
[_descriptionString release];
_descriptionString= [description retain];
}
}
- (NSString*)descrption
{
return _descriptionString;
}
- (void)setContentCell:(NSCell*)cell
{
if (_contentCell != cell)
{
[_contentCell release];
_contentCell= [cell retain];
}
}
- (NSCell*)contentCell
{
return _contentCell;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSMutableDictionary *attrs= [self _attributesDict];
NSRect rect;
NSRect contentRect;
float leftoverWidth;
NSString *title;
if ([self state] || [self isHighlighted])
{
//[[NSColor whiteColor] set];
//NSRectFill(cellFrame);
}
rect= cellFrame;
title= [self stringValue];
if (title && ![title isEqualTo:@""])
{
// draw title
rect.size.width= [self titleWidth];
[attrs setObject:[self font] forKey:NSFontAttributeName];
[[attrs objectForKey:NSParagraphStyleAttributeName] setLineBreakMode:NSLineBreakByClipping];
[title drawInRect:rect withAttributes:attrs];
rect.origin.x+= HORIZ_SPACING + NSWidth(rect);
leftoverWidth= NSWidth(cellFrame) - (NSWidth(rect) + HORIZ_SPACING);
}
else
leftoverWidth= NSWidth(cellFrame);
// draw the action cell
if (_descriptionString)
{
rect.size.width= leftoverWidth / 2;
if (_minActionWidth > 0 && NSWidth(rect) < _minActionWidth)
rect.size.width= _minActionWidth;
leftoverWidth-= NSWidth(rect);
}
else
rect.size.width= leftoverWidth;
contentRect.size= [_contentCell cellSize];
contentRect.size.width= rect.size.width;
contentRect.origin.x= rect.origin.x;
contentRect.origin.y= rect.origin.y= rect.origin.y - (NSHeight(rect) - NSHeight(contentRect)) / 2;
[_contentCell drawWithFrame:contentRect inView:controlView];
// description
if (_descriptionString)
{
rect.origin.y= rect.origin.y - (NSHeight(rect) - _descriptionSize.height) / 2;
rect.origin.x+= HORIZ_SPACING + NSWidth(rect);
rect.size.width= leftoverWidth - HORIZ_SPACING;
attrs= [self _descriptionAttributesDict];
[attrs setObject:_descriptionFont?:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]
forKey:NSFontAttributeName];
[_descriptionString drawInRect:rect withAttributes:attrs];
}
}
- (void)editWithFrame:(NSRect)aRect
inView:(NSView *)controlView
editor:(NSText *)textObj
delegate:(id)anObject
event:(NSEvent *)theEvent
{
NSLog(@"edit %@", NSStringFromRect(aRect));
}
@end
@implementation MControlForm
- (id)initWithFrame:(NSRect)rect
{
self= [super initWithFrame:rect];
if (self)
{
_rows= [[NSMutableArray alloc] init];
_spacing= 4.0;
}
return self;
}
- (void)dealloc
{
[_rows release];
[super dealloc];
}
- (BOOL)isFlipped
{
return YES;
}
- (BOOL)acceptsFirstMouse: (NSEvent *)aEvent
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
NSText *text = [[self window] fieldEditor: YES
forObject: self];
NSLog(@"1st resp");
if ([text superview] != nil)
if ([text resignFirstResponder] == NO)
return NO;
[[_rows lastObject] selectWithFrame: [[_rows lastObject] frame]
inView: self
editor: text
delegate: self
start: 0
length: 1];
return YES;
}
- (int)getRowForPoint:(NSPoint)location
{
unsigned int i, c= [_rows count];
float y;
y= 0;
for (i= 0; i < c; i++)
{
MFormCell *cell= [_rows objectAtIndex:i];
NSSize size= [cell cellSize];
if (location.y <= y + size.height)
return i;
y+= size.height + _spacing;
}
return -1;
}
- (void)mouseDown:(NSEvent*)theEvent
{
NSPoint location= [theEvent locationInWindow];
NSRect cellFrame= [self frame];
int row= -1;
unsigned int i, c= [_rows count];
float y= 0.0;
location= [self convertPoint:location fromView:nil];
for (i= 0; i < c; i++)
{
MFormCell *cell= [_rows objectAtIndex:i];
NSSize size= [cell cellSize];
if (location.y <= y + size.height)
{
cellFrame.origin.y+= y;
cellFrame.size= size;
row= i;
break;
}
y+= size.height + _spacing;
}
if (row < 0)
return;
if (row >= 0)
{
MFormCell *cell= [_rows objectAtIndex:row];
if ([cell isEnabled])
{
NSLog(@"%@ %i %@",cell, row, NSStringFromRect(cellFrame));
[cell trackMouse:theEvent inRect:cellFrame ofView:self untilMouseUp:YES];
}
}
}
- (void)relayout
{
unsigned int i, c= [_rows count];
float width, maxWidth= 0;
for (i= 0; i < c; i++)
{
MFormCell *cell= [_rows objectAtIndex:i];
width= [cell calcTitleWidth];
maxWidth= MAX(width, maxWidth);
}
for (i= 0; i < c; i++)
{
MFormCell *cell= [_rows objectAtIndex:i];
[cell setTitleWidth:maxWidth];
}
}
- (void)drawRect:(NSRect)aRect
{
unsigned int i, c= [_rows count];
NSRect frame= [self frame];
NSRect cellFrame;
[self relayout];
NSDrawWindowBackground(aRect);
[self lockFocus];
cellFrame.origin= NSMakePoint(0,0);
cellFrame= frame;
for (i= 0; i < c; i++)
{
MFormCell *cell= [_rows objectAtIndex:i];
cellFrame.size.height= [cell cellSizeForWidth:NSWidth(frame)].height;
[cell drawWithFrame:cellFrame inView:self];
cellFrame.origin.y+= NSHeight(cellFrame) + _spacing;
}
[self unlockFocus];
}
- (void)addFormRow:(MFormCell*)cell
{
[_rows addObject:cell];
}
- (void)insertFormRow:(MFormCell*)cell atIndex:(int)index
{
//[_rows insertObject:cell atIndex:index];
}
- (MFormCell*)addTextFieldWithTitle:(NSString*)title
description:(NSString*)description
{
NSTextFieldCell *cell= [[NSTextFieldCell alloc] initTextCell:@""];
MFormCell *formCell= [[MFormCell alloc] init];
// [cell setBezeled:YES];
/// [cell setBezelStyle:NSTextFieldSquareBezel];
// [cell setDrawsBackground:YES];
[cell setEditable:YES];
// [cell setEnabled:YES];
[formCell setTitle:title];
[formCell setContentCell:[cell autorelease]];
[formCell setDescription:description];
[self addFormRow:formCell];
return [formCell autorelease];
}
- (MFormCell*)addCheckBoxWithTitle:(NSString*)title
description:(NSString*)description
{
return nil;
}
- (MFormCell*)addPopUpWithTitle:(NSString*)title
items:(NSArray*)items
description:(NSString*)description
{
NSPopUpButtonCell *cell= [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
MFormCell *formCell= [[MFormCell alloc] init];
[cell addItemsWithTitles:items];
[formCell setTitle:title];
[formCell setContentCell:[cell autorelease]];
[formCell setDescription:description];
[self addFormRow:formCell];
return [formCell autorelease];
}
- (MFormCell*)addFilePickerWithTitle:(NSString*)title
directory:(BOOL)flag
description:(NSString*)description
{
return nil;
}
@end
|