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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
|
/** <title>GSInfoPanel.m</title>
<abstract>Standard GNUstep info panel</abstract>
Copyright (C) 2000 Free Software Foundation, Inc.
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: January 2000
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import <Foundation/NSBundle.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSString.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSProcessInfo.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSButton.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSFont.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSImageView.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSTextField.h"
#import "GNUstepGUI/GSInfoPanel.h"
#import "GNUstepGUI/GSTheme.h"
#import "GSGuiPrivate.h"
static id
value_from_info_plist_for_key (NSString *key)
{
static NSDictionary *d = nil;
/* We use this additional BOOL so that if loading Info-gnustep.plist
fails once, we do not try again. */
static BOOL load_failed = NO;
if ((d == nil) && (load_failed == NO))
{
d = [[NSBundle mainBundle] localizedInfoDictionary];
if (d == nil)
load_failed = YES;
}
if (d)
return [d objectForKey: key];
return nil;
}
static BOOL
nil_or_not_of_class (id object, Class class)
{
return ((object == nil) || ([object isKindOfClass: class] == NO));
}
static NSTextField *
new_label (NSString *value)
{
NSTextField *t;
t = AUTORELEASE([NSTextField new]);
[t setStringValue: value];
[t setDrawsBackground: NO];
[t setEditable: NO];
[t setSelectable: NO];
[t setBezeled: NO];
[t setBordered: NO];
[t setAlignment: NSLeftTextAlignment];
return t;
}
/*
* An object that displays a list of left-aligned strings (used for the authors)
*/
@interface _GSLabelListView: NSView
{
}
/* After initialization, its size is the size it needs, just move it
where we want it to show */
- (id) initWithStringArray: (NSArray *)array
font: (NSFont *)font;
@end
@implementation _GSLabelListView
- (id) initWithStringArray: (NSArray *)array
font: (NSFont *)font
{
self = [super init];
if (self != nil)
{
unsigned int count;
NSTextField *field;
float height = 2;
float width = 0;
NSRect r;
count = [array count];
/*
* We go through the array in reverse order, adding items from
* the bottom of the view working upwards. This means that the
* order of strings in the array will appear orderd from top to
* bottom in the view.
*/
while (count-- > 0)
{
id item = [array objectAtIndex: count];
if ([item isKindOfClass: [NSString class]] == NO)
continue;
field = new_label (item);
[field setFont: font];
[field sizeToFit];
[field setAutoresizingMask: NSViewNotSizable];
r = [field frame];
r.origin.x = 0;
r.origin.y = height;
if (r.size.width > width)
width = r.size.width;
height += r.size.height + 2;
[field setFrame: r];
[self setFrameSize: NSMakeSize (width, height)];
[self addSubview: field];
}
[self setFrameSize: NSMakeSize (width, height - 2)];
}
return self;
}
@end
@implementation GSInfoPanel: NSPanel
+ (void) initialize
{
if (self == [GSInfoPanel class])
{
[self setVersion: 1];
}
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[super dealloc];
}
/* When the current theme changes, we need to update the info panel to match.
*/
- (void) _themeDidActivate: (NSNotification*)n
{
NSView *c = [self contentView];
NSEnumerator *e = [[c subviews] objectEnumerator];
NSView *v;
NSButton *b;
while ((v = [e nextObject]) != nil)
{
if ([v isKindOfClass: [NSButton class]]
&& [(b = (NSButton*)v) target] == [GSTheme class])
{
NSString *s;
NSRect f;
s = [NSString stringWithFormat: @"%@: %@",
_(@"Current theme"), [[GSTheme theme] name]];
[b setTitle: s];
[b sizeToFit];
f = [b frame];
f.origin.x = ([c frame].size.width - f.size.width) / 2;
[b setFrame: f];
[c setNeedsDisplay: YES];
}
}
}
- (id) initWithDictionary: (NSDictionary *)dictionary;
{
/* Info to show */
NSString *name = nil;
NSString *description = nil;
NSImage *icon = nil;
NSString *release = nil;
NSString *fullVersionID = nil;
NSArray *authors = nil;
NSString *url = nil;
NSString *copyright = nil;
NSString *copyrightDescription = nil;
NSString *theme = nil;
/* GUI Objects used to show the Info */
NSButton *iconButton;
NSTextField *nameLabel;
NSTextField *descriptionLabel = nil;
NSTextField *versionLabel;
_GSLabelListView *authorsList;
NSTextField *authorTitleLabel;
NSTextField *urlLabel = nil;
NSTextField *copyrightLabel;
NSTextField *copyrightDescriptionLabel = nil;
NSButton *themeLabel = nil;
NSFont *smallFont;
/* Minimum size we use for the panel */
float width = 241;
float height = 107;
/* Used for computations */
float tmp_A;
float tmp_a;
float tmp_b;
float tmp_c;
NSRect f;
NSView *cv;
/*
* Gets what we need to show
*/
/* Application Name */
if (dictionary)
name = [dictionary objectForKey: @"ApplicationName"];
if (nil_or_not_of_class (name, [NSString class]))
{
name = value_from_info_plist_for_key (@"ApplicationName");
if (nil_or_not_of_class (name, [NSString class]))
{
name = value_from_info_plist_for_key (@"NSHumanReadableShortName");
if (nil_or_not_of_class (name, [NSString class]))
{
name = value_from_info_plist_for_key (@"CFBundleName");
if (nil_or_not_of_class (name, [NSString class]))
{
name = [[NSProcessInfo processInfo] processName];
}
}
}
}
/* Application Description */
if (dictionary)
description = [dictionary objectForKey: @"ApplicationDescription"];
if (nil_or_not_of_class (description, [NSString class]))
{
description = value_from_info_plist_for_key
(@"ApplicationDescription");
if ([description isKindOfClass: [NSString class]] == NO)
description = nil;
}
/* NB: description might be nil */
/* Application Icon */
if (dictionary)
icon = [dictionary objectForKey: @"ApplicationIcon"];
if (nil_or_not_of_class (icon, [NSImage class]))
{
icon = [NSImage imageNamed:
value_from_info_plist_for_key (@"ApplicationIcon")];
if (nil_or_not_of_class (icon, [NSImage class]))
{
icon = [NSImage imageNamed: @"NSApplicationIcon"];
if (nil_or_not_of_class (icon, [NSImage class]))
icon = [NSApp applicationIconImage];
}
}
/* Release */
if (dictionary)
release = [dictionary objectForKey: @"ApplicationRelease"];
if (nil_or_not_of_class (release, [NSString class]))
{
if (dictionary)
release = [dictionary objectForKey: @"ApplicationVersion"];
if (nil_or_not_of_class (release, [NSString class]))
{
release = value_from_info_plist_for_key (@"ApplicationRelease");
if (nil_or_not_of_class (release, [NSString class]))
{
release = value_from_info_plist_for_key (@"NSAppVersion");
if (nil_or_not_of_class (release, [NSString class]))
{
release = value_from_info_plist_for_key (@"CFBundleVersion");
if (nil_or_not_of_class (release, [NSString class]))
{
release = @"Unknown";
}
}
}
}
}
/* FullVersionID */
if (dictionary)
fullVersionID = [dictionary objectForKey: @"FullVersionID"];
if (nil_or_not_of_class (fullVersionID, [NSString class]))
{
if (dictionary)
fullVersionID = [dictionary objectForKey: @"Version"];
if (nil_or_not_of_class (fullVersionID, [NSString class]))
{
fullVersionID = value_from_info_plist_for_key (@"NSBuildVersion");
if ([fullVersionID isKindOfClass: [NSString class]] == NO)
fullVersionID = nil;
}
}
/* NB: fullVersionID can be nil! */
/* Now we prepare the complete release string */
release = [_(@"Release: ") stringByAppendingString: release];
if (fullVersionID)
{
release = [release stringByAppendingString: @" ("];
release = [release stringByAppendingString: fullVersionID];
release = [release stringByAppendingString: @")"];
}
/* Authors */
if (dictionary)
authors = [dictionary objectForKey: @"Authors"];
if (nil_or_not_of_class (authors, [NSArray class]))
{
if ([authors isKindOfClass: [NSString class]])
{
authors = [NSArray arrayWithObject: authors];
}
else
{
authors = value_from_info_plist_for_key (@"Authors");
if (nil_or_not_of_class (authors, [NSArray class]))
{
if ([authors isKindOfClass: [NSString class]])
{
authors = [NSArray arrayWithObject: authors];
}
else
{
authors = [NSArray arrayWithObject: @"Unknown"];
}
}
}
}
/* URL */
if (dictionary)
url = [dictionary objectForKey: @"URL"];
if (nil_or_not_of_class (url, [NSString class]))
{
url = value_from_info_plist_for_key (@"URL");
}
// URL can be nil
/* Copyright */
if (dictionary)
copyright = [dictionary objectForKey: @"Copyright"];
if (nil_or_not_of_class (copyright, [NSString class]))
{
copyright = value_from_info_plist_for_key (@"Copyright");
if (nil_or_not_of_class (copyright, [NSString class]))
{
copyright = value_from_info_plist_for_key
(@"NSHumanReadableCopyright");
if (nil_or_not_of_class (copyright, [NSString class]))
copyright = _(@"Copyright Information Not Available");
}
}
/* Copyright Description */
if (dictionary)
copyrightDescription = [dictionary objectForKey: @"CopyrightDescription"];
if (nil_or_not_of_class (copyrightDescription, [NSString class]))
{
copyrightDescription = value_from_info_plist_for_key
(@"CopyrightDescription");
if ([copyrightDescription isKindOfClass: [NSString class]] == NO)
copyrightDescription = nil;
}
/* NB: copyrightDescription can be nil */
/*
* Create GUI Objects
*/
f = NSMakeRect(0, 0, 48, 48);
iconButton = AUTORELEASE([[NSButton alloc] initWithFrame: f]);
[[iconButton cell] setImageScaling: NSImageScaleProportionallyUpOrDown];
[iconButton setImage: icon];
[iconButton setBordered: NO];
[iconButton setImagePosition: NSImageOnly];
/* Clicking on the iconButton starts the GSMemoryPanel. */
[iconButton setEnabled: YES];
[iconButton setTarget: NSApp];
[iconButton setAction: @selector(orderFrontSharedMemoryPanel:)];
nameLabel = new_label (name);
[nameLabel setFont: [NSFont boldSystemFontOfSize: 32]];
[nameLabel sizeToFit];
if (description)
{
descriptionLabel = new_label (description);
[descriptionLabel setFont: [NSFont boldSystemFontOfSize: 14]];
[descriptionLabel sizeToFit];
}
smallFont = [NSFont systemFontOfSize: 12];
versionLabel = new_label (release);
[versionLabel setFont: smallFont];
[versionLabel sizeToFit];
if ([authors count] == 0)
{
authorTitleLabel = new_label (@"");
}
else if ([authors count] == 1)
{
authorTitleLabel = new_label (_(@"Author: "));
}
else
{
authorTitleLabel = new_label (_(@"Authors: "));
}
[authorTitleLabel setFont: smallFont];
[authorTitleLabel sizeToFit];
authorsList = AUTORELEASE([[_GSLabelListView alloc]
initWithStringArray: authors font: smallFont]);
if (url)
{
urlLabel = new_label (url);
[urlLabel setFont: smallFont];
[urlLabel sizeToFit];
}
copyrightLabel = new_label (copyright);
[copyrightLabel setFont: smallFont];
[copyrightLabel sizeToFit];
if (copyrightDescription)
{
copyrightDescriptionLabel = new_label (copyrightDescription);
[copyrightDescriptionLabel setFont: smallFont];
[copyrightDescriptionLabel sizeToFit];
}
theme = [NSString stringWithFormat: @"%@: %@",
_(@"Current theme"), [[GSTheme theme] name]];
themeLabel = AUTORELEASE([NSButton new]);
[themeLabel setTitle: theme];
[themeLabel setBordered: NO];
[themeLabel setAlignment: NSLeftTextAlignment];
[themeLabel setFont: smallFont];
[themeLabel setButtonType: NSMomentaryLightButton];
[themeLabel setFocusRingType: NSFocusRingTypeNone];
[themeLabel sizeToFit];
/*
* Compute width and height of the panel
*/
/** width **/
tmp_A = f.size.width;
/* distance between icon and title */
tmp_A += 10;
/* compute the maximum of the following three sizes */
tmp_a = [nameLabel frame].size.width;
if (description)
{
tmp_b = [descriptionLabel frame].size.width;
if (tmp_a < tmp_b)
tmp_a = tmp_b;
}
tmp_b = [versionLabel frame].size.width;
if (tmp_a < tmp_b)
tmp_a = tmp_b;
/* Add in to tmp_A */
tmp_A += tmp_a;
/* Update width */
if (width < tmp_A)
width = tmp_A;
tmp_A = [authorTitleLabel frame].size.width;
tmp_A += [authorsList frame].size.width;
if (width < tmp_A)
width = tmp_A;
/* FIXME depending on where we put url */
if (url)
{
tmp_A = [urlLabel frame].size.width;
if (tmp_A > width)
width = tmp_A;
}
tmp_A = [copyrightLabel frame].size.width;
if (tmp_A > width)
width = tmp_A;
if (copyrightDescription)
{
tmp_A = [copyrightDescriptionLabel frame].size.width;
if (tmp_A > width)
width = tmp_A;
}
tmp_A = [themeLabel frame].size.width;
if (tmp_A > width)
width = tmp_A;
/* height */
/* Warning: we implicitly assume icon height is approx of the
standard height of 48. The code tries to be nice so that 50 or
47 should more or less work -- but beware that 200 or 20 will
*not* work. */
tmp_A = f.size.height;
if (description)
tmp_A += 10;
else
tmp_A += 5;
tmp_A += [versionLabel frame].size.height;
tmp_A += 20;
tmp_A += [authorsList frame].size.height;
if (url)
{
tmp_A += [urlLabel frame].size.height + 2;
}
tmp_A += 25;
tmp_A += [copyrightLabel frame].size.height;
if (copyrightDescription)
{
tmp_A += 2;
tmp_A += [copyrightDescriptionLabel frame].size.height;
}
tmp_A += 5;
tmp_A += [themeLabel frame].size.height;
if (tmp_A > height)
height = tmp_A;
/* Add border to both width and height */
width += 32;
height += 36;
/*
* TODO: Adjust/check obtained width and height ?
* (NB: If they are adjusted, code putting views in the panel
* has to be adjusted too!)
*/
/*
* Creates the panel with the right width and height
*/
self = [super initWithContentRect: NSMakeRect (100, 100, width, height)
styleMask: (NSTitledWindowMask | NSClosableWindowMask)
backing: NSBackingStoreRetained defer: YES];
if (!self)
return nil;
/*
* Add objects to the panel in their position
*/
cv = [self contentView];
{
NSImageView* backgroundImage = [[NSImageView alloc]
initWithFrame:
NSMakeRect(0, 0, width, height)];
//[backgroundImage setImageAlignment: NSImageAlignCenter];
//[backgroundImage setImageScaling: NSScaleProportionally];
[backgroundImage setImage: [NSImage imageNamed: @"LogoGNUstep"]];
[backgroundImage setEditable: NO];
[cv addSubview: backgroundImage];
RELEASE(backgroundImage);
}
f = [iconButton frame];
f.origin.x = 16;
f.origin.y = height - 18 - f.size.height;
tmp_a = f.origin.x + f.size.width + 10;
tmp_b = f.origin.y;
tmp_c = f.size.height;
[cv addSubview: iconButton];
[iconButton setFrame: f];
f = [nameLabel frame];
f.origin.x = tmp_a;
/* NB: We rely upon the fact that the text in a text field is
vertically centered in its frame */
if (description)
f.origin.y = tmp_b + 10;
else
f.origin.y = tmp_b;
f.size.height = tmp_c;
[cv addSubview: nameLabel];
[nameLabel setFrame: f];
if (description)
{
f = [descriptionLabel frame];
f.origin.x = tmp_a;
f.origin.y = tmp_b - 5;
[cv addSubview: descriptionLabel];
[descriptionLabel setFrame: f];
}
f = [versionLabel frame];
f.origin.x = width - 16 - f.size.width;
if (description)
f.origin.y = tmp_b - 10 - f.size.height;
else
f.origin.y = tmp_b - 5 - f.size.height;
tmp_b = f.origin.y;
[cv addSubview: versionLabel];
[versionLabel setFrame: f];
tmp_b -= 20;
tmp_a = [authorTitleLabel frame].size.width;
tmp_a += [authorsList frame].size.width;
tmp_a = (width - tmp_a) / 2;
f = [authorTitleLabel frame];
f.origin.x = tmp_a;
tmp_c = tmp_a + f.size.width;
f.origin.y = tmp_b - f.size.height;
[cv addSubview: authorTitleLabel];
[authorTitleLabel setFrame: f];
f = [authorsList frame];
f.origin.x = tmp_c;
f.origin.y = tmp_b - f.size.height;
tmp_b = f.origin.y;
[cv addSubview: authorsList];
[authorsList setFrame: f];
if (url)
{
/* FIXME position of this thing */
f = [urlLabel frame];
f.origin.x = tmp_a;
f.origin.y = tmp_b - 2 - f.size.height;
tmp_b = f.origin.y;
[cv addSubview: urlLabel];
[urlLabel setFrame: f];
}
f = [copyrightLabel frame];
f.origin.x = (width - f.size.width) / 2;
f.origin.y = tmp_b - 25 - f.size.height;
tmp_b = f.origin.y;
[cv addSubview: copyrightLabel];
[copyrightLabel setFrame: f];
if (copyrightDescription)
{
f = [copyrightDescriptionLabel frame];
f.origin.x = (width - f.size.width) / 2;
f.origin.y = tmp_b - 2 - f.size.height;
tmp_b = f.origin.y;
[cv addSubview: copyrightDescriptionLabel];
[copyrightDescriptionLabel setFrame: f];
}
f = [themeLabel frame];
f.origin.x = (width - f.size.width) / 2;
f.origin.y = tmp_b - 5 - f.size.height;
[cv addSubview: themeLabel];
[themeLabel setFrame: f];
[themeLabel setTarget: [GSTheme class]];
[themeLabel setAction: @selector(orderFrontSharedThemePanel:)];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_themeDidActivate:)
name: GSThemeDidActivateNotification
object: nil];
[self center];
return self;
}
- (void) copy: (id)sender
{
NSArray *types = [NSArray arrayWithObject: NSStringPboardType];
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSMutableString *text = [[NSMutableString alloc] init];
NSView *cv = [self contentView];
NSEnumerator *enumerator = [[cv subviews] objectEnumerator];
NSView *subview;
// Loop over all the text subviews and collect the information
while ((subview = [enumerator nextObject]) != nil)
{
if ([subview isKindOfClass: [NSTextField class]])
{
[text appendString: [(NSTextField*)subview stringValue]];
[text appendString: @"\n"];
}
}
[pboard declareTypes: types owner: self];
[pboard setString: text
forType: NSStringPboardType];
RELEASE(text);
}
- (void) keyDown: (NSEvent*)theEvent
{
NSString *characters = [theEvent characters];
unichar character = 0;
if ([characters length] > 0)
{
character = [characters characterAtIndex: 0];
}
// FIXME: Hard coded
if (character == 'c' && ([theEvent modifierFlags] & NSCommandKeyMask))
{
[self copy: nil];
return;
}
[super keyDown: theEvent];
}
@end
|