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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
|
/** <title>NSFontPanel</title>
<abstract>System generic panel for selecting and previewing fonts</abstract>
Copyright (C) 1996 Free Software Foundation, Inc.
Author: Fred Kiefer <FredKiefer@gmx.de>
Date: Febuary 2000
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: January 2001 - sizings and resizings
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.
*/
#include "config.h"
#import <Foundation/NSDebug.h>
#import <Foundation/NSValue.h>
#import "AppKit/NSDragging.h"
#import "AppKit/NSFont.h"
#import "AppKit/NSFontPanel.h"
#import "AppKit/NSFontManager.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSSplitView.h"
#import "AppKit/NSScrollView.h"
#import "AppKit/NSBrowser.h"
#import "AppKit/NSBrowserCell.h"
#import "AppKit/NSTextView.h"
#import "AppKit/NSTextField.h"
#import "AppKit/NSTextFieldCell.h"
#import "AppKit/NSColor.h"
#import "AppKit/NSPanel.h"
#import "AppKit/NSButton.h"
#import "AppKit/NSBox.h"
#import "GNUstepGUI/GSCharacterPanel.h"
#import "GSGuiPrivate.h"
#define _SAVE_PANEL_X_PAD 5
#define _SAVE_PANEL_Y_PAD 4
static inline void _setFloatValue (NSTextField *field, float size)
{
/* If casting size to int and then back to float we get no change,
it means it's an integer */
if ((float)((int)size) == size)
{
/* We prefer using this if it's an int, so that it's
displayed like in `8' rather than like in
`8.0000000000'. Yes - when NSCell's formatters are
finished we won't need this. */
[field setIntValue: (int)size];
}
else
{
[field setFloatValue: size];
}
}
static NSText *sizeFieldText = nil;
static float sizes[] = {4.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
14.0, 16.0, 18.0, 24.0, 36.0, 48.0, 64.0};
/* Implemented in NSBrowser */
@interface GSBrowserTitleCell : NSTextFieldCell
{
}
@end
@interface NSFontPanel (Private)
- (NSFont*) _fontForSelection: (NSFont*) fontObject;
-(void) _trySelectSize: (float)size
updateSizeField: (BOOL)updateSizeField;
// Some action methods
- (void) cancel: (id) sender;
- (void) _togglePreview: (id) sender;
- (void) _doPreview;
- (void) ok: (id) sender;
- (void) _getOriginalSize;
- (id)_initWithoutGModel;
- (BOOL) _includeFont: (NSString *)fontName delegate: (id)delegate;
@end
@implementation NSFontPanel
/*
* Class methods
*/
+ (void) initialize
{
if (self == [NSFontPanel class])
{
[self setVersion: 1];
}
}
/** <p>Creates ( if needed ) and returns the shared NSFontPanel.</p>
*/
+ (NSFontPanel*) sharedFontPanel
{
NSFontManager *fm = [NSFontManager sharedFontManager];
return [fm fontPanel: YES];
}
+ (BOOL) sharedFontPanelExists
{
NSFontManager *fm = [NSFontManager sharedFontManager];
return ([fm fontPanel: NO] != nil);
}
/*
* Instance methods
*/
- (id) init
{
// if (![NSBundle loadNibNamed: @"FontPanel" owner: self]);
[self _initWithoutGModel];
ASSIGN(_faceList, [NSArray array]);
_face = -1;
_family = -1;
[self reloadDefaultFontFamilies];
[self _getOriginalSize];
return self;
}
- (void) dealloc
{
RELEASE(_panelFont);
RELEASE(_familyList);
TEST_RELEASE(_faceList);
TEST_RELEASE(_accessoryView);
[super dealloc];
}
/** <p>Returns whether the "set" button is enabled.</p>
<p>See Also: -setEnabled:</p>
*/
- (BOOL) isEnabled
{
NSButton *setButton = [[self contentView] viewWithTag: NSFPSetButton];
return [setButton isEnabled];
}
/**<p>Sets whether the "set" button is enabled.</p>
<p>See Also: -isEnabled</p>
*/
- (void) setEnabled: (BOOL)flag
{
NSButton *setButton = [[self contentView] viewWithTag: NSFPSetButton];
[setButton setEnabled: flag];
}
- (void) reloadDefaultFontFamilies
{
NSFontManager *fm = [NSFontManager sharedFontManager];
id fmDelegate = [fm delegate];
NSBrowser *familyBrowser = [[self contentView] viewWithTag: NSFPFamilyBrowser];
NSArray *fontFamilies = [fm availableFontFamilies];
unsigned int i,j;
NSMutableArray *familyList;
/*
Build an array of all families that have a font that will be included.
*/
familyList = [[NSMutableArray alloc]
initWithCapacity: [fontFamilies count]];
for (i = 0; i < [fontFamilies count]; i++)
{
NSArray *familyMembers;
familyMembers = [fm availableMembersOfFontFamily:
[fontFamilies objectAtIndex: i]];
for (j = 0; j < [familyMembers count]; j++)
{
if ([self _includeFont: [[familyMembers objectAtIndex: j] objectAtIndex: 0]
delegate: fmDelegate])
{
[familyList addObject: [fontFamilies objectAtIndex: i]];
break;
}
}
}
DESTROY(_familyList);
_familyList = familyList;
// Reload the display.
[familyBrowser loadColumnZero];
// Reselect the current font. (Hopefully still there)
[self setPanelFont: [fm selectedFont]
isMultiple: [fm isMultiple]];
}
- (void) setPanelFont: (NSFont *)fontObject
isMultiple: (BOOL)flag
{
NSTextField *previewArea;
previewArea = [[self contentView] viewWithTag: NSFPPreviewField];
ASSIGN(_panelFont, fontObject);
_multiple = flag;
if (fontObject == nil)
{
return;
}
if (flag)
{
// TODO: Unselect all items and show a message
[previewArea setStringValue: _(@"Multiple fonts selected")];
_family = -1;
_face = -1;
}
else
{
NSFontManager *fm = [NSFontManager sharedFontManager];
NSString *family = [fontObject familyName];
NSString *fontName = [fontObject fontName];
float size = [fontObject pointSize];
NSBrowser *familyBrowser = [[self contentView] viewWithTag: NSFPFamilyBrowser];
NSBrowser *faceBrowser = [[self contentView] viewWithTag: NSFPFaceBrowser];
NSString *face = @"";
unsigned int i;
// Store style information for font
_traits = [fm traitsOfFont: fontObject];
_weight = [fm weightOfFont: fontObject];
// Select the row for the font family
for (i = 0; i < [_familyList count]; i++)
{
if ([[_familyList objectAtIndex: i] isEqualToString: family])
break;
}
if (i < [_familyList count])
{
[familyBrowser selectRow: i inColumn: 0];
_family = i;
ASSIGN(_faceList, [fm availableMembersOfFontFamily: family]);
[faceBrowser loadColumnZero];
_face = -1;
}
// Select the row for the font face
for (i = 0; i < [_faceList count]; i++)
{
if ([[[_faceList objectAtIndex: i] objectAtIndex: 0]
isEqualToString: fontName])
break;
}
if (i < [_faceList count])
{
[faceBrowser selectRow: i inColumn: 0];
_face = i;
face = [[_faceList objectAtIndex: i] objectAtIndex: 1];
}
// show point size and select the row if there is one
[self _trySelectSize: size updateSizeField: YES];
// Use in preview
[previewArea setFont: fontObject];
if (_previewString == nil)
{
[previewArea setStringValue: [NSString stringWithFormat: @"%@ %@ %d PT",
family, face, (int)size]];
}
}
}
/**<p>Converts the NSFont <var>fontObject</var></p>
*/
- (NSFont *) panelConvertFont: (NSFont *)fontObject
{
NSFont *newFont;
if (_multiple)
{
//TODO: We go over every item in the panel and check if a
// value is selected. If so we send it on to the manager
// newFont = [fm convertFont: fontObject toHaveTrait: NSItalicFontMask];
NSLog(@"Multiple font conversion not implemented in NSFontPanel");
newFont = [self _fontForSelection: fontObject];
}
else
{
newFont = [self _fontForSelection: fontObject];
}
if (newFont == nil)
{
newFont = fontObject;
}
return newFont;
}
/**<p>Overides the NSPanel/NSWindow method to always returns YES</p>
*/
- (BOOL) worksWhenModal
{
return YES;
}
/** <p>Returns the NSFontPanel's accessory view.</p>
<p>See Also: -setAccessoryView:</p>
*/
- (NSView*) accessoryView
{
return _accessoryView;
}
/** <p>Sets the NSFontPanel's accessory view to <var>aView</var></p>
<p>See Also: -accessoryView</p>
*/
- (void) setAccessoryView: (NSView*)aView
{
NSRect accessoryViewFrame, bottomFrame;
NSRect tmpRect;
NSSize contentSize, contentMinSize;
float addedHeight, accessoryWidth;
if (aView == _accessoryView)
return;
/* The following code is very tricky. Please think and test a lot
before changing it. */
/* Remove old accessory view if any */
if (_accessoryView != nil)
{
/* Remove accessory view */
accessoryViewFrame = [_accessoryView frame];
[_accessoryView removeFromSuperview];
/* Change the min size before doing the resizing otherwise it
could be a problem. */
[self setMinSize: _originalMinSize];
/* Resize the panel to the height without the accessory view.
This must be done with the special care of not resizing
the heights of the other views. */
addedHeight = accessoryViewFrame.size.height + (_SAVE_PANEL_Y_PAD * 2);
contentSize = [[self contentView] frame].size;
contentSize.height -= addedHeight;
// Resize without modifying topView and bottomView height.
[_topView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
[self setContentSize: contentSize];
[_topView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
}
/* Resize the panel to its original size. This resizes freely the
heights of the views. NB: minSize *must* come first */
[self setMinSize: _originalMinSize];
[self setContentSize: _originalSize];
/* Set the new accessory view */
_accessoryView = aView;
/* If there is a new accessory view, plug it in */
if (_accessoryView != nil)
{
/* Make sure the new accessory view behaves - its height must be fixed
* and its position relative to the bottom of the superview must not
* change - so its position rlative to the top must be changable. */
[_accessoryView setAutoresizingMask: NSViewMaxYMargin
| ([_accessoryView autoresizingMask]
& ~(NSViewHeightSizable | NSViewMinYMargin))];
/* Compute size taken by the new accessory view */
accessoryViewFrame = [_accessoryView frame];
addedHeight = accessoryViewFrame.size.height + (_SAVE_PANEL_Y_PAD * 2);
accessoryWidth = accessoryViewFrame.size.width + (_SAVE_PANEL_X_PAD * 2);
/* Resize content size accordingly */
contentSize = _originalSize;
contentSize.height += addedHeight;
if (accessoryWidth > contentSize.width)
{
contentSize.width = accessoryWidth;
}
/* Set new content size without resizing heights of topView, bottomView */
// Our views should resize horizontally if needed, but not vertically
[_topView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
[self setContentSize: contentSize];
// Restore the original autoresizing masks
[_topView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
/* Compute new min size */
contentMinSize = _originalMinSize;
contentMinSize.height += addedHeight;
// width is more delicate
tmpRect = NSMakeRect (0, 0, contentMinSize.width, contentMinSize.height);
tmpRect = [NSWindow contentRectForFrameRect: tmpRect
styleMask: [self styleMask]];
if (accessoryWidth > tmpRect.size.width)
{
contentMinSize.width += accessoryWidth - tmpRect.size.width;
}
// Set new min size
[self setMinSize: contentMinSize];
/*
* Pack the Views
*/
/* BottomView is ready */
bottomFrame = [_bottomView frame];
/* AccessoryView */
accessoryViewFrame.origin.x
= (contentSize.width - accessoryViewFrame.size.width) / 2;
accessoryViewFrame.origin.y = NSMaxY (bottomFrame) + _SAVE_PANEL_Y_PAD;
[_accessoryView setFrameOrigin: accessoryViewFrame.origin];
/* Add the accessory view */
[[self contentView] addSubview: _accessoryView];
}
}
/*
* NSCoding protocol
*/
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[super encodeWithCoder: aCoder];
[aCoder encodeObject: _panelFont];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_multiple];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_preview];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
[super initWithCoder: aDecoder];
_panelFont = RETAIN([aDecoder decodeObject]);
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_multiple];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_preview];
return self;
}
/*
* Overriding fieldEditor:forObject: because we don't want the field
* editor (which is used when you type in the size browser) to use the
* font panel, otherwise typing in the size browser can modify the
* currently selected font in unexpected ways ! */
- (NSText *) fieldEditor: (BOOL)createFlag
forObject: (id)anObject
{
if (([anObject respondsToSelector: @selector(tag)])
&& ([anObject tag] == NSFPSizeField))
{
if ((sizeFieldText == nil) && createFlag)
{
sizeFieldText = [NSText new];
[sizeFieldText setUsesFontPanel: NO];
[sizeFieldText setFieldEditor: YES];
}
return sizeFieldText;
}
return [super fieldEditor: createFlag forObject: anObject];
}
@end
@implementation NSFontPanel (Private)
- (id) _initWithoutGModel
{
NSRect contentRect = {{100, 100}, {320, 300}};
NSRect topAreaRect = {{0, 42}, {320, 258}};
NSRect splitViewRect = {{8, 8}, {304, 243}};
NSRect topSplitRect = {{0, 0}, {304, 45}};
NSRect previewAreaRect = {{0, 1}, {304, 44}};
NSRect bottomSplitRect = {{0, 0}, {304, 190}};
NSRect familyBrowserRect = {{0, 0}, {111, 189}};
NSRect typefaceBrowserRect = {{113, 0}, {111, 189}};
NSRect sizeBrowserRect = {{226, 0}, {78, 143}};
NSRect sizeLabelRect = {{226, 145}, {78, 21}};
NSRect sizeTitleRect = {{226, 168}, {78, 21}};
NSRect bottomAreaRect = {{0, 0}, {320, 42}};
NSRect slashRect = {{0, 40}, {320, 2}};
NSRect revertButtonRect = {{83, 8}, {71, 24}};
NSRect previewButtonRect = {{162, 8}, {71, 24}};
NSRect setButtonRect = {{241, 8}, {71, 24}};
NSRect characterPanelButtonRect = {{8, 8}, {24, 24}};
NSView *v;
NSView *topArea;
NSView *bottomArea;
NSView *topSplit;
NSView *bottomSplit;
NSSplitView *splitView;
NSTextField *previewArea;
NSBrowser *sizeBrowser;
NSBrowser *familyBrowser;
NSBrowser *faceBrowser;
NSTextField *label;
NSTextField *sizeField;
NSButton *revertButton;
NSButton *previewButton;
NSButton *setButton;
NSButton *characterPanelButton;
NSBox *slash;
unsigned int style = NSTitledWindowMask | NSClosableWindowMask
| NSResizableWindowMask | NSUtilityWindowMask;
self = [super initWithContentRect: contentRect
styleMask: style
backing: NSBackingStoreRetained
defer: YES
screen: nil];
if (!self)
{
return nil;
}
[self setTitle: _(@"Font Panel")];
[self setBecomesKeyOnlyIfNeeded: YES];
v = [self contentView];
// preview and selection
topArea = [[NSView alloc] initWithFrame: topAreaRect];
[topArea setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
_topView = topArea;
splitView = [[NSSplitView alloc] initWithFrame: splitViewRect];
[splitView setVertical: NO];
[splitView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
topSplit = [[NSView alloc] initWithFrame: topSplitRect];
[topSplit setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
// Display for the font example
previewArea = [[NSTextField alloc] initWithFrame: previewAreaRect];
[previewArea setBackgroundColor: [NSColor textBackgroundColor]];
[previewArea setDrawsBackground: YES];
[previewArea setEditable: NO];
[previewArea setSelectable: NO];
//[previewArea setUsesFontPanel: NO];
[previewArea setAlignment: NSCenterTextAlignment];
[previewArea setStringValue: _(@"Font preview")];
[previewArea setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
[previewArea setTag: NSFPPreviewField];
[topSplit addSubview: previewArea];
RELEASE(previewArea);
bottomSplit = [[NSView alloc] initWithFrame: bottomSplitRect];
[bottomSplit setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
// Selection of the font family
// We use a browser with one column to get a selection list
familyBrowser = [[NSBrowser alloc] initWithFrame: familyBrowserRect];
[familyBrowser setDelegate: self];
[familyBrowser setMaxVisibleColumns: 1];
[familyBrowser setMinColumnWidth: 0];
[familyBrowser setAllowsMultipleSelection: NO];
[familyBrowser setAllowsEmptySelection: YES];
[familyBrowser setHasHorizontalScroller: NO];
[familyBrowser setTitled: YES];
[familyBrowser setTakesTitleFromPreviousColumn: NO];
[familyBrowser setTarget: self];
[familyBrowser setDoubleAction: @selector(familySelected:)];
[familyBrowser setAction: @selector(_familySelectionChanged:)];
[familyBrowser setAutoresizingMask: (NSViewWidthSizable
| NSViewMaxXMargin
| NSViewHeightSizable)];
[familyBrowser setTag: NSFPFamilyBrowser];
[bottomSplit addSubview: familyBrowser];
RELEASE(familyBrowser);
// selection of type face
// We use a browser with one column to get a selection list
faceBrowser = [[NSBrowser alloc] initWithFrame: typefaceBrowserRect];
[faceBrowser setDelegate: self];
[faceBrowser setMaxVisibleColumns: 1];
[faceBrowser setMinColumnWidth: 0];
[faceBrowser setAllowsMultipleSelection: NO];
[faceBrowser setAllowsEmptySelection: YES];
[faceBrowser setHasHorizontalScroller: NO];
[faceBrowser setTitled: YES];
[faceBrowser setTakesTitleFromPreviousColumn: NO];
[faceBrowser setTarget: self];
[faceBrowser setDoubleAction: @selector(faceSelected:)];
[faceBrowser setAction: @selector(_faceSelectionChanged:)];
[faceBrowser setAutoresizingMask: (NSViewWidthSizable
| NSViewMinXMargin
| NSViewHeightSizable)];
[faceBrowser setTag: NSFPFaceBrowser];
[bottomSplit addSubview: faceBrowser];
RELEASE(faceBrowser);
// label for selection of size
label = [[NSTextField alloc] initWithFrame: sizeTitleRect];
[label setCell: AUTORELEASE([GSBrowserTitleCell new])];
[label setFont: [NSFont boldSystemFontOfSize: 0]];
[label setAlignment: NSCenterTextAlignment];
[label setDrawsBackground: YES];
[label setEditable: NO];
[label setTextColor: [NSColor windowFrameTextColor]];
[label setBackgroundColor: [NSColor controlShadowColor]];
[label setStringValue: _(@"Size")];
[label setAutoresizingMask: NSViewMinXMargin | NSViewMinYMargin];
[label setTag: NSFPSizeTitle];
[bottomSplit addSubview: label];
RELEASE(label);
// this is the size input field
sizeField = [[NSTextField alloc] initWithFrame: sizeLabelRect];
[sizeField setDrawsBackground: YES];
[sizeField setEditable: YES];
[sizeField setAllowsEditingTextAttributes: NO];
[sizeField setAlignment: NSCenterTextAlignment];
[sizeField setAutoresizingMask: NSViewMinXMargin | NSViewMinYMargin];
[sizeField setDelegate: self];
[sizeField setTag: NSFPSizeField];
[bottomSplit addSubview: sizeField];
RELEASE(sizeField);
sizeBrowser = [[NSBrowser alloc] initWithFrame: sizeBrowserRect];
[sizeBrowser setDelegate: self];
[sizeBrowser setMaxVisibleColumns: 1];
[sizeBrowser setAllowsMultipleSelection: NO];
[sizeBrowser setAllowsEmptySelection: YES];
[sizeBrowser setHasHorizontalScroller: NO];
[sizeBrowser setTitled: NO];
[sizeBrowser setTakesTitleFromPreviousColumn: NO];
[sizeBrowser setTarget: self];
[sizeBrowser setDoubleAction: @selector(sizeSelected:)];
[sizeBrowser setAction: @selector(_sizeSelectionChanged:)];
[sizeBrowser setAutoresizingMask: NSViewMinXMargin | NSViewHeightSizable];
[sizeBrowser setTag: NSFPSizeBrowser];
[bottomSplit addSubview: sizeBrowser];
RELEASE(sizeBrowser);
[splitView addSubview: topSplit];
RELEASE(topSplit);
[splitView addSubview: bottomSplit];
RELEASE(bottomSplit);
[splitView setDelegate: self];
[topArea addSubview: splitView];
RELEASE(splitView);
// action buttons
bottomArea = [[NSView alloc] initWithFrame: bottomAreaRect];
_bottomView = bottomArea;
slash = [[NSBox alloc] initWithFrame: slashRect];
[slash setBorderType: NSGrooveBorder];
[slash setTitlePosition: NSNoTitle];
[slash setAutoresizingMask: NSViewWidthSizable];
[bottomArea addSubview: slash];
RELEASE(slash);
// cancel button
revertButton = [[NSButton alloc] initWithFrame: revertButtonRect];
[revertButton setTitle: _(@"Revert")];
[revertButton setAction: @selector(cancel:)];
[revertButton setTarget: self];
[revertButton setTag: NSFPRevertButton];
[revertButton setAutoresizingMask: NSViewMinXMargin];
[bottomArea addSubview: revertButton];
RELEASE(revertButton);
// toggle button for preview
previewButton = [[NSButton alloc] initWithFrame: previewButtonRect];
[previewButton setTitle: _(@"Preview")];
[previewButton setButtonType: NSOnOffButton];
[previewButton setAction: @selector(_togglePreview:)];
[previewButton setTarget: self];
[previewButton setTag: NSFPPreviewButton];
[previewButton setAutoresizingMask: NSViewMinXMargin];
[previewButton setState: NSOnState];
_preview = YES;
[bottomArea addSubview: previewButton];
RELEASE(previewButton);
// button to set the font
setButton = [[NSButton alloc] initWithFrame: setButtonRect];
[setButton setTitle: _(@"Set")];
[setButton setAction: @selector(ok:)];
[setButton setTarget: self];
[setButton setTag: NSFPSetButton];
[setButton setAutoresizingMask: NSViewMinXMargin];
[bottomArea addSubview: setButton];
// make it the default button
[self setDefaultButtonCell: [setButton cell]];
RELEASE(setButton);
// Character Panel button
{
NSString *label;
unichar labelchars[2] = {0x03b1, 0x03b2}; // alpha, beta
label = [[[NSString alloc] initWithCharacters: labelchars
length: 2] autorelease];
characterPanelButton = [[NSButton alloc] initWithFrame: characterPanelButtonRect];
[characterPanelButton setTitle: label];
[characterPanelButton setToolTip: _(@"Character Panel")];
[characterPanelButton setAction: @selector(characterPanel:)];
[characterPanelButton setTarget: self];
[bottomArea addSubview: characterPanelButton];
RELEASE(characterPanelButton);
}
// set up the next key view chain
[familyBrowser setNextKeyView: faceBrowser];
[faceBrowser setNextKeyView: sizeField];
[sizeField setNextKeyView: sizeBrowser];
[sizeBrowser setNextKeyView: revertButton];
[revertButton setNextKeyView: previewButton];
[previewButton setNextKeyView: setButton];
[setButton setNextKeyView: familyBrowser];
[v addSubview: topArea];
RELEASE(topArea);
// Add the accessory view, if there is one
if (_accessoryView != nil)
{
[v addSubview: _accessoryView];
}
[bottomArea setAutoresizingMask: NSViewWidthSizable];
[v addSubview: bottomArea];
RELEASE(bottomArea);
[self setMinSize: [self frame].size];
[self setInitialFirstResponder: setButton];
[self setBecomesKeyOnlyIfNeeded: YES];
return self;
}
- (void) _togglePreview: (id)sender
{
_preview = (sender == nil) ? YES : [sender state];
[self _doPreview];
}
- (void) _doPreview
{
NSFont *font = nil;
NSTextField *previewArea = [[self contentView] viewWithTag: NSFPPreviewField];
if (_preview)
{
font = [self _fontForSelection: _panelFont];
// build up a font and use it in the preview area
if (font != nil)
{
[previewArea setFont: font];
}
}
if (_previewString == nil)
{
NSTextField *sizeField = [[self contentView] viewWithTag: NSFPSizeField];
float size = [sizeField floatValue];
NSString *faceName;
NSString *familyName;
if (size == 0 && font != nil)
{
size = [font pointSize];
}
if (_family == -1)
{
familyName = _(@"NoFamily");
}
else
{
familyName = [_familyList objectAtIndex: _family];
}
if (_face == -1 || ![_faceList count])
{
faceName = _(@"NoFace");
}
else
{
faceName = [[_faceList objectAtIndex: _face] objectAtIndex: 1];
}
[previewArea setStringValue: [NSString stringWithFormat: @"%@ %@ %d PT",
familyName, faceName, (int)size]];
}
}
- (void) ok: (id)sender
{
// The set button has been pushed
NSFontManager *fm = [NSFontManager sharedFontManager];
[fm modifyFontViaPanel: self];
}
- (void) cancel: (id)sender
{
// FIXME/TODO
/*
* The cancel button has been pushed
* we should reset the items in the panel
*/
[self setPanelFont: _panelFont
isMultiple: _multiple];
}
- (void) characterPanel: (id)sender
{
[[NSApplication sharedApplication] orderFrontCharacterPalette: sender];
}
- (NSFont *) _fontForSelection: (NSFont *)fontObject
{
float size;
NSString *fontName;
NSTextField *sizeField = [[self contentView] viewWithTag: NSFPSizeField];
unsigned i = [_faceList count];
size = [sizeField floatValue];
if (size == 0.0)
{
if (fontObject == nil)
{
size = 12.0;
}
else
{
size = [fontObject pointSize];
}
}
if (_face < 0)
{
if (i == 0)
{
return nil; /* Nothing available */
}
// FIXME - just uses first face
fontName = [[_faceList objectAtIndex: 0] objectAtIndex: 0];
}
else
{
/*
i really should be > 0 here, except for the very obscure case where
the delegate has refused all fonts (so that our family and face lists
are completely empty).
*/
if (i)
fontName = [[_faceList objectAtIndex: _face] objectAtIndex: 0];
else
return nil;
}
// FIXME: We should check if the font is correct
return [NSFont fontWithName: fontName size: size];
}
-(void) _trySelectSize: (float)size
updateSizeField: (BOOL)updateSizeField
{
unsigned int i;
NSBrowser *sizeBrowser = [[self contentView] viewWithTag: NSFPSizeBrowser];
NSTextField *sizeField;
if (updateSizeField)
{
/* Make sure our sizeField is updated. */
sizeField = [[self contentView] viewWithTag: NSFPSizeField];
_setFloatValue (sizeField, size);
}
/* Make sure our column is loaded. */
[sizeBrowser loadColumnZero];
for (i = 0; i < sizeof(sizes) / sizeof(float); i++)
{
if (size == sizes[i])
{
/* select the cell */
[sizeBrowser selectRow: i inColumn: 0];
break;
}
}
if (i == sizeof(sizes) / sizeof(float))
{
/* TODO: No matching size found in the list. We should deselect
everything. */
}
}
- (void) controlTextDidChange: (NSNotification *)n
{
NSTextField *sizeField = [[self contentView] viewWithTag: NSFPSizeField];
float size = [sizeField floatValue];
[self _trySelectSize: size updateSizeField: NO];
[self _doPreview];
}
/*
Ask the NSFontManager:s delegate if a font should be included. For speed,
the delegate is an argument; a repeat-caller can then cache it.
*/
- (BOOL) _includeFont: (NSString*)fontName delegate: (id)fmDelegate
{
if (fmDelegate != nil
&& [fmDelegate respondsToSelector: @selector(fontManager:willIncludeFont:)])
{
return [fmDelegate fontManager: [NSFontManager sharedFontManager]
willIncludeFont: fontName];
}
else
return YES;
}
- (void) _getOriginalSize
{
/* Used in setMinSize: */
_originalMinSize = [self minSize];
/* Used in setContentSize: */
_originalSize = [[self contentView] frame].size;
}
@end
@implementation NSFontPanel (NSBrowserDelegate)
static int score_difference(int weight1, int traits1,
int weight2, int traits2)
{
int score, t;
score = (weight1 - weight2);
score = 10 * score * score;
t = traits1 ^ traits2;
if (t & NSFixedPitchFontMask) score += 1000;
if (t & NSCompressedFontMask) score += 150;
if (t & NSPosterFontMask) score += 200;
if (t & NSSmallCapsFontMask) score += 200;
if (t & NSCondensedFontMask) score += 150;
if (t & NSExpandedFontMask) score += 150;
if (t & NSNarrowFontMask) score += 150;
if (t & NSBoldFontMask) score += 20;
if (t & NSItalicFontMask) score += 45;
return score;
}
- (void) _familySelectionChanged: (id)sender
{
NSFontManager *fm = [NSFontManager sharedFontManager];
id fmDelegate = [fm delegate];
NSBrowser *faceBrowser = [[self contentView] viewWithTag: NSFPFaceBrowser];
NSBrowser *familyBrowser = [[self contentView] viewWithTag: NSFPFamilyBrowser];
int row = [familyBrowser selectedRowInColumn: 0];
unsigned int i;
NSArray *entireFaceList;
NSMutableArray *faceList;
entireFaceList = [fm availableMembersOfFontFamily:
[_familyList objectAtIndex: row]];
faceList = [[NSMutableArray alloc] initWithCapacity: [entireFaceList count]];
for (i = 0; i < [entireFaceList count]; i++)
{
id aFace = [entireFaceList objectAtIndex:i];
if ([self _includeFont: [aFace objectAtIndex:0] delegate: fmDelegate])
{
[faceList addObject: aFace];
}
}
DESTROY(_faceList);
_faceList = faceList;
_family = row;
// Select a face with the same properties
for (i = 0; i < [_faceList count]; i++)
{
NSArray *font_info = [_faceList objectAtIndex: i];
if (([[font_info objectAtIndex: 2] intValue] == _weight)
&& ([[font_info objectAtIndex: 3] unsignedIntValue] == _traits))
break;
}
// Find the face that differs the least from what we want
if (i == [_faceList count])
{
int best, best_score, score;
best_score = 1e6;
best = -1;
for (i = 0; i < [_faceList count]; i++)
{
NSArray *font_info = [_faceList objectAtIndex: i];
score = score_difference(_weight, _traits,
[[font_info objectAtIndex: 2] intValue],
[[font_info objectAtIndex: 3] unsignedIntValue]);
if (score < best_score)
{
best = i;
best_score = score;
}
}
if (best != -1)
i = best;
}
if (i == [_faceList count])
i = 0;
_face = i;
[faceBrowser loadColumnZero];
[faceBrowser selectRow: i inColumn: 0];
/* Also make sure the size column has some value */
{
NSTextField *sizeField = [[self contentView] viewWithTag: NSFPSizeField];
float size = [sizeField floatValue];
if (size == 0.0)
{
[self _trySelectSize: 12.0 updateSizeField: YES];
}
}
[self _doPreview];
}
- (void) _faceSelectionChanged: (id)sender
{
NSBrowser *faceBrowser = [[self contentView] viewWithTag: NSFPFaceBrowser];
int row = [faceBrowser selectedRowInColumn: 0];
NSArray *font_info = [_faceList objectAtIndex: row];
_face = row;
_weight = [[font_info objectAtIndex: 2] intValue];
_traits = [[font_info objectAtIndex: 3] unsignedIntValue];
[self _doPreview];
}
- (void) _sizeSelectionChanged: (id)sender
{
NSBrowser *sizeBrowser = [[self contentView] viewWithTag: NSFPSizeBrowser];
int row = [sizeBrowser selectedRowInColumn: 0];
NSTextField *sizeField;
sizeField = [[self contentView] viewWithTag: NSFPSizeField];
_setFloatValue (sizeField, sizes[row]);
[self _doPreview];
}
- (NSInteger) browser: (NSBrowser*)sender numberOfRowsInColumn: (NSInteger)column
{
switch ([sender tag])
{
case NSFPFamilyBrowser:
{
return [_familyList count];
}
case NSFPFaceBrowser:
{
return [_faceList count];
}
case NSFPSizeBrowser:
{
return sizeof (sizes) / sizeof (float);
}
default:
{
return 0;
}
}
}
- (NSString*) browser: (NSBrowser*)sender titleOfColumn: (NSInteger)column
{
switch ([sender tag])
{
case NSFPFamilyBrowser:
{
return _(@"Family");
}
case NSFPFaceBrowser:
{
return _(@"Typeface");
}
default:
{
return @"";
}
}
}
- (void) browser: (NSBrowser *)sender
willDisplayCell: (id)cell
atRow: (NSInteger)row
column: (NSInteger)column
{
NSString *value = nil;
if (row < 0)
return;
switch ([sender tag])
{
case NSFPFamilyBrowser:
{
if ([_familyList count] > (NSUInteger)row)
{
value = [_familyList objectAtIndex: row];
}
break;
}
case NSFPFaceBrowser:
{
if ([_faceList count] > (NSUInteger)row)
{
value = [[_faceList objectAtIndex: row] objectAtIndex: 1];
}
break;
}
case NSFPSizeBrowser:
default:
{
value = [NSString stringWithFormat: @"%d", (int) sizes[row]];
}
}
[cell setStringValue: value];
[cell setLeaf: YES];
}
- (BOOL) browser: (NSBrowser *)sender
isColumnValid: (NSInteger)column;
{
return NO;
}
@end
@implementation NSFontPanel (NSSplitViewDelegate)
- (void) splitView: (NSSplitView *)splitView
constrainMinCoordinate: (CGFloat *)min
maxCoordinate: (CGFloat *)max
ofSubviewAt: (NSInteger)offset
{
*max = *max - 100;
}
@end
|