Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved multi-line entry on macOS #440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions darwin/multilineentry.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// see also http://stackoverflow.com/questions/24210153/nstextview-not-properly-resizing-with-auto-layout and http://stackoverflow.com/questions/11237622/using-autolayout-with-expanding-nstextviews
@interface intrinsicSizeTextView : NSTextView {
uiMultilineEntry *libui_e;
NSDictionary *strAttrs;
NSFont *font;
}
- (id)initWithFrame:(NSRect)r e:(uiMultilineEntry *)e;
@end
Expand All @@ -25,8 +27,13 @@ @implementation intrinsicSizeTextView
- (id)initWithFrame:(NSRect)r e:(uiMultilineEntry *)e
{
self = [super initWithFrame:r];
if (self)
if (self) {
self->libui_e = e;
self->font = [NSFont controlContentFontOfSize:0];
self->strAttrs = [NSDictionary
dictionaryWithObjects:@[font, [NSColor controlTextColor]]
forKeys:@[NSFontAttributeName, NSForegroundColorAttributeName]];
}
return self;
}

Expand All @@ -51,6 +58,16 @@ - (void)didChangeText
(*(self->libui_e->onChanged))(self->libui_e, self->libui_e->onChangedData);
}

-(NSDictionary *)stringAttributes
{
return self->strAttrs;
}

-(NSFont *)font
{
return self->font;
}

@end

uiDarwinControlAllDefaultsExceptDestroy(uiMultilineEntry, sv)
Expand Down Expand Up @@ -79,6 +96,9 @@ void uiMultilineEntrySetText(uiMultilineEntry *e, const char *text)
{
[[e->tv textStorage] replaceCharactersInRange:NSMakeRange(0, [[e->tv string] length])
withString:uiprivToNSString(text)];

[[e->tv textStorage] setAttributes:[e->tv stringAttributes] range:NSMakeRange(0, [[e->tv string] length])];

// must be called explicitly according to the documentation of shouldChangeTextInRange:replacementString:
e->changing = YES;
[e->tv didChangeText];
Expand All @@ -90,6 +110,9 @@ void uiMultilineEntryAppend(uiMultilineEntry *e, const char *text)
{
[[e->tv textStorage] replaceCharactersInRange:NSMakeRange([[e->tv string] length], 0)
withString:uiprivToNSString(text)];

[[e->tv textStorage] setAttributes:[e->tv stringAttributes] range:NSMakeRange(0, [[e->tv string] length])];

e->changing = YES;
[e->tv didChangeText];
e->changing = NO;
Expand Down Expand Up @@ -119,7 +142,6 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
static uiMultilineEntry *finishMultilineEntry(BOOL hscroll)
{
uiMultilineEntry *e;
NSFont *font;
uiprivScrollViewCreateParams p;

uiDarwinNewControl(uiMultilineEntry, e);
Expand All @@ -129,8 +151,6 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
// verified against Interface Builder for a sufficiently customized text view

// NSText properties:
// this is what Interface Builder sets the background color to
[e->tv setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
[e->tv setDrawsBackground:YES];
[e->tv setEditable:YES];
[e->tv setSelectable:YES];
Expand Down Expand Up @@ -159,8 +179,6 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
[e->tv setUsesRuler:NO];
[e->tv setUsesInspectorBar:NO];
[e->tv setSelectionGranularity:NSSelectByCharacter];
// there is a dedicated named insertion point color but oh well
[e->tv setInsertionPointColor:[NSColor controlTextColor]];
// typing attributes is nil; keep default (we change it below for fonts though)
[e->tv setSmartInsertDeleteEnabled:NO];
[e->tv setContinuousSpellCheckingEnabled:NO];
Expand Down Expand Up @@ -199,18 +217,11 @@ void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
[[e->tv textContainer] setContainerSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX)];

// don't use uiDarwinSetControlFont() directly; we have to do a little extra work to set the font
font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
[e->tv setTypingAttributes:[NSDictionary
dictionaryWithObject:font
forKey:NSFontAttributeName]];
// e->tv font from Interface Builder is nil, but setFont:nil throws an exception
// let's just set it to the standard control font anyway, just to be safe
[e->tv setFont:font];
[e->tv setTypingAttributes:[e->tv stringAttributes]];
[e->tv setFont:[e->tv font]];

memset(&p, 0, sizeof (uiprivScrollViewCreateParams));
p.DocumentView = e->tv;
// this is what Interface Builder sets it to
p.BackgroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0];
p.DrawsBackground = YES;
p.Bordered = YES;
p.HScroll = hscroll;
Expand Down