2016-04-14 12:27:15 -05:00
|
|
|
// 14 april 2016
|
|
|
|
#import "uipriv_darwin.h"
|
2018-03-04 18:51:45 -06:00
|
|
|
#import "attrstr.h"
|
2016-04-14 12:27:15 -05:00
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
@interface uiprivFontButton : NSButton {
|
2016-04-14 12:27:15 -05:00
|
|
|
uiFontButton *libui_b;
|
|
|
|
NSFont *libui_font;
|
|
|
|
}
|
|
|
|
- (id)initWithFrame:(NSRect)frame libuiFontButton:(uiFontButton *)b;
|
|
|
|
- (void)updateFontButtonLabel;
|
|
|
|
- (IBAction)fontButtonClicked:(id)sender;
|
|
|
|
- (void)activateFontButton;
|
|
|
|
- (void)deactivateFontButton:(BOOL)activatingAnother;
|
2016-05-02 09:32:39 -05:00
|
|
|
- (void)deactivateOnClose:(NSNotification *)note;
|
2018-03-08 22:04:45 -06:00
|
|
|
- (void)getfontdesc:(uiFontDescriptor *)uidesc;
|
2016-04-14 12:27:15 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
// only one may be active at one time
|
2018-03-04 18:51:45 -06:00
|
|
|
static uiprivFontButton *activeFontButton = nil;
|
2016-04-14 12:27:15 -05:00
|
|
|
|
|
|
|
struct uiFontButton {
|
|
|
|
uiDarwinControl c;
|
2018-03-04 18:51:45 -06:00
|
|
|
uiprivFontButton *button;
|
2016-04-20 12:40:54 -05:00
|
|
|
void (*onChanged)(uiFontButton *, void *);
|
|
|
|
void *onChangedData;
|
2016-04-14 12:27:15 -05:00
|
|
|
};
|
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
@implementation uiprivFontButton
|
2016-04-14 12:27:15 -05:00
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)frame libuiFontButton:(uiFontButton *)b
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self) {
|
|
|
|
self->libui_b = b;
|
|
|
|
|
|
|
|
// imitate a NSColorWell in appearance
|
|
|
|
[self setButtonType:NSPushOnPushOffButton];
|
|
|
|
[self setBordered:YES];
|
|
|
|
[self setBezelStyle:NSShadowlessSquareBezelStyle];
|
|
|
|
|
|
|
|
// default font values according to the CTFontDescriptor reference
|
|
|
|
// this is autoreleased (thanks swillits in irc.freenode.net/#macdev)
|
|
|
|
self->libui_font = [[NSFont fontWithName:@"Helvetica" size:12.0] retain];
|
|
|
|
[self updateFontButtonLabel];
|
|
|
|
|
|
|
|
// for when clicked
|
|
|
|
[self setTarget:self];
|
|
|
|
[self setAction:@selector(fontButtonClicked:)];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-05-02 09:32:39 -05:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// clean up notifications
|
|
|
|
if (activeFontButton == self)
|
|
|
|
[self deactivateFontButton:NO];
|
|
|
|
[self->libui_font release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2016-04-14 12:27:15 -05:00
|
|
|
- (void)updateFontButtonLabel
|
|
|
|
{
|
|
|
|
NSString *title;
|
|
|
|
|
|
|
|
title = [NSString stringWithFormat:@"%@ %g",
|
|
|
|
[self->libui_font displayName],
|
|
|
|
[self->libui_font pointSize]];
|
|
|
|
[self setTitle:title];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)fontButtonClicked:(id)sender
|
|
|
|
{
|
|
|
|
if ([self state] == NSOnState)
|
|
|
|
[self activateFontButton];
|
|
|
|
else
|
|
|
|
[self deactivateFontButton:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)activateFontButton
|
|
|
|
{
|
|
|
|
NSFontManager *sfm;
|
|
|
|
|
|
|
|
sfm = [NSFontManager sharedFontManager];
|
|
|
|
if (activeFontButton != nil)
|
|
|
|
[activeFontButton deactivateFontButton:YES];
|
|
|
|
[sfm setTarget:self];
|
|
|
|
[sfm setSelectedFont:self->libui_font isMultiple:NO];
|
|
|
|
[sfm orderFrontFontPanel:self];
|
|
|
|
activeFontButton = self;
|
2016-05-02 09:32:39 -05:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(deactivateOnClose:)
|
|
|
|
name:NSWindowWillCloseNotification
|
|
|
|
object:[NSFontPanel sharedFontPanel]];
|
2016-04-14 12:27:15 -05:00
|
|
|
[self setState:NSOnState];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)deactivateFontButton:(BOOL)activatingAnother
|
|
|
|
{
|
|
|
|
NSFontManager *sfm;
|
|
|
|
|
|
|
|
sfm = [NSFontManager sharedFontManager];
|
|
|
|
[sfm setTarget:nil];
|
|
|
|
if (!activatingAnother)
|
|
|
|
[[NSFontPanel sharedFontPanel] orderOut:self];
|
|
|
|
activeFontButton = nil;
|
2016-05-02 09:32:39 -05:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:NSWindowWillCloseNotification
|
|
|
|
object:[NSFontPanel sharedFontPanel]];
|
2016-04-14 12:27:15 -05:00
|
|
|
[self setState:NSOffState];
|
|
|
|
}
|
|
|
|
|
2016-05-02 09:32:39 -05:00
|
|
|
- (void)deactivateOnClose:(NSNotification *)note
|
|
|
|
{
|
|
|
|
[self deactivateFontButton:NO];
|
|
|
|
}
|
|
|
|
|
2016-04-14 12:27:15 -05:00
|
|
|
- (void)changeFont:(id)sender
|
|
|
|
{
|
|
|
|
NSFontManager *fm;
|
|
|
|
NSFont *old;
|
2016-04-20 12:40:54 -05:00
|
|
|
uiFontButton *b = self->libui_b;
|
2016-04-14 12:27:15 -05:00
|
|
|
|
|
|
|
fm = (NSFontManager *) sender;
|
|
|
|
old = self->libui_font;
|
|
|
|
self->libui_font = [sender convertFont:self->libui_font];
|
2016-05-14 20:29:44 -05:00
|
|
|
// do this even if it returns the same; we don't own anything that isn't from a new or alloc/init
|
|
|
|
[self->libui_font retain];
|
|
|
|
// do this second just in case
|
|
|
|
[old release];
|
2016-04-14 12:27:15 -05:00
|
|
|
[self updateFontButtonLabel];
|
2016-04-20 12:40:54 -05:00
|
|
|
(*(b->onChanged))(b, b->onChangedData);
|
2016-04-14 12:27:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger)validModesForFontPanel:(NSFontPanel *)panel
|
|
|
|
{
|
|
|
|
return NSFontPanelFaceModeMask |
|
|
|
|
NSFontPanelSizeModeMask |
|
|
|
|
NSFontPanelCollectionModeMask;
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
- (void)getfontdesc:(uiFontDescriptor *)uidesc
|
2016-04-20 12:40:54 -05:00
|
|
|
{
|
2017-02-11 00:13:07 -06:00
|
|
|
CTFontRef ctfont;
|
|
|
|
CTFontDescriptorRef ctdesc;
|
|
|
|
|
|
|
|
ctfont = (CTFontRef) (self->libui_font);
|
|
|
|
ctdesc = CTFontCopyFontDescriptor(ctfont);
|
2018-03-08 22:04:45 -06:00
|
|
|
uiprivFontDescriptorFromCTFontDescriptor(ctdesc, uidesc);
|
2017-02-11 00:13:07 -06:00
|
|
|
CFRelease(ctdesc);
|
|
|
|
uidesc->Size = CTFontGetSize(ctfont);
|
2016-04-20 12:40:54 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 12:27:15 -05:00
|
|
|
@end
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaults(uiFontButton, button)
|
|
|
|
|
2016-04-14 13:01:33 -05:00
|
|
|
// we do not want font change events to be sent to any controls other than the font buttons
|
|
|
|
// see main.m for more details
|
2018-03-04 18:51:45 -06:00
|
|
|
BOOL uiprivFontButtonInhibitSendAction(SEL sel, id from, id to)
|
2016-04-14 13:01:33 -05:00
|
|
|
{
|
|
|
|
if (sel != @selector(changeFont:))
|
|
|
|
return NO;
|
2018-03-04 18:51:45 -06:00
|
|
|
return ![to isKindOfClass:[uiprivFontButton class]];
|
2016-04-14 13:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// we do not want NSFontPanelValidation messages to be sent to any controls other than the font buttons when a font button is active
|
|
|
|
// see main.m for more details
|
2018-03-04 18:51:45 -06:00
|
|
|
BOOL uiprivFontButtonOverrideTargetForAction(SEL sel, id from, id to, id *override)
|
2016-04-14 13:01:33 -05:00
|
|
|
{
|
|
|
|
if (activeFontButton == nil)
|
|
|
|
return NO;
|
|
|
|
if (sel != @selector(validModesForFontPanel:))
|
|
|
|
return NO;
|
|
|
|
*override = activeFontButton;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2016-05-15 18:56:01 -05:00
|
|
|
// we also don't want the panel to be usable when there's a dialog running; see stddialogs.m for more details on that
|
|
|
|
// unfortunately the panel seems to ignore -setWorksWhenModal: so we'll have to do things ourselves
|
2018-03-04 18:51:45 -06:00
|
|
|
@interface uiprivNonModalFontPanel : NSFontPanel
|
2016-05-15 18:56:01 -05:00
|
|
|
@end
|
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
@implementation uiprivNonModalFontPanel
|
2016-05-15 18:56:01 -05:00
|
|
|
|
|
|
|
- (BOOL)worksWhenModal
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
void uiprivSetupFontPanel(void)
|
2016-05-15 18:56:01 -05:00
|
|
|
{
|
2018-03-04 18:51:45 -06:00
|
|
|
[NSFontManager setFontPanelFactory:[uiprivNonModalFontPanel class]];
|
2016-05-15 18:56:01 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:40:54 -05:00
|
|
|
static void defaultOnChanged(uiFontButton *b, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
void uiFontButtonFont(uiFontButton *b, uiFontDescriptor *desc)
|
2016-04-20 12:40:54 -05:00
|
|
|
{
|
2017-02-11 00:13:07 -06:00
|
|
|
[b->button getfontdesc:desc];
|
2016-04-20 12:40:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data)
|
|
|
|
{
|
|
|
|
b->onChanged = f;
|
|
|
|
b->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
2016-04-14 12:27:15 -05:00
|
|
|
uiFontButton *uiNewFontButton(void)
|
|
|
|
{
|
|
|
|
uiFontButton *b;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiFontButton, b);
|
2016-04-14 12:27:15 -05:00
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
b->button = [[uiprivFontButton alloc] initWithFrame:NSZeroRect libuiFontButton:b];
|
2016-04-14 12:27:15 -05:00
|
|
|
uiDarwinSetControlFont(b->button, NSRegularControlSize);
|
|
|
|
|
2016-04-20 12:40:54 -05:00
|
|
|
uiFontButtonOnChanged(b, defaultOnChanged, NULL);
|
|
|
|
|
2016-04-14 12:27:15 -05:00
|
|
|
return b;
|
|
|
|
}
|
2018-03-17 22:55:33 -05:00
|
|
|
|
|
|
|
void uiFreeFontButtonFont(uiFontDescriptor *desc)
|
|
|
|
{
|
|
|
|
// TODO ensure this is synchronized with fontmatch.m
|
|
|
|
uiFreeText((char *) (desc->Family));
|
|
|
|
}
|