Added uiFontButtonFont() and uiFontButtonOnChanged(); implemented on OS X.

This commit is contained in:
Pietro Gagliardi 2016-04-20 13:40:54 -04:00
parent 275b80a6d9
commit cfa1b6bf0a
3 changed files with 39 additions and 9 deletions

View File

@ -12,6 +12,7 @@
- (IBAction)fontButtonClicked:(id)sender; - (IBAction)fontButtonClicked:(id)sender;
- (void)activateFontButton; - (void)activateFontButton;
- (void)deactivateFontButton:(BOOL)activatingAnother; - (void)deactivateFontButton:(BOOL)activatingAnother;
- (uiDrawTextFont *)libuiFont;
@end @end
// only one may be active at one time // only one may be active at one time
@ -20,6 +21,8 @@ static fontButton *activeFontButton = nil;
struct uiFontButton { struct uiFontButton {
uiDarwinControl c; uiDarwinControl c;
fontButton *button; fontButton *button;
void (*onChanged)(uiFontButton *, void *);
void *onChangedData;
}; };
uiDarwinDefineControl( uiDarwinDefineControl(
@ -101,6 +104,7 @@ uiDarwinDefineControl(
{ {
NSFontManager *fm; NSFontManager *fm;
NSFont *old; NSFont *old;
uiFontButton *b = self->libui_b;
fm = (NSFontManager *) sender; fm = (NSFontManager *) sender;
old = self->libui_font; old = self->libui_font;
@ -110,6 +114,7 @@ uiDarwinDefineControl(
if (self->libui_font != old) if (self->libui_font != old)
[old release]; [old release];
[self updateFontButtonLabel]; [self updateFontButtonLabel];
(*(b->onChanged))(b, b->onChangedData);
} }
- (NSUInteger)validModesForFontPanel:(NSFontPanel *)panel - (NSUInteger)validModesForFontPanel:(NSFontPanel *)panel
@ -119,6 +124,11 @@ uiDarwinDefineControl(
NSFontPanelCollectionModeMask; NSFontPanelCollectionModeMask;
} }
- (uiDrawTextFont *)libuiFont
{
return mkTextFontFromNSFont(self->libui_font);
}
@end @end
// we do not want font change events to be sent to any controls other than the font buttons // we do not want font change events to be sent to any controls other than the font buttons
@ -142,6 +152,22 @@ BOOL fontButtonOverrideTargetForAction(SEL sel, id from, id to, id *override)
return YES; return YES;
} }
static void defaultOnChanged(uiFontButton *b, void *data)
{
// do nothing
}
uiDrawTextFont *uiFontButtonFont(uiFontButton *b)
{
return [b->button libuiFont];
}
void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data)
{
b->onChanged = f;
b->onChangedData = data;
}
uiFontButton *uiNewFontButton(void) uiFontButton *uiNewFontButton(void)
{ {
uiFontButton *b; uiFontButton *b;
@ -151,6 +177,8 @@ uiFontButton *uiNewFontButton(void)
b->button = [[fontButton alloc] initWithFrame:NSZeroRect libuiFontButton:b]; b->button = [[fontButton alloc] initWithFrame:NSZeroRect libuiFontButton:b];
uiDarwinSetControlFont(b->button, NSRegularControlSize); uiDarwinSetControlFont(b->button, NSRegularControlSize);
uiFontButtonOnChanged(b, defaultOnChanged, NULL);
uiDarwinFinishNewControl(b, uiFontButton); uiDarwinFinishNewControl(b, uiFontButton);
return b; return b;

View File

@ -25,18 +25,10 @@ static double entryDouble(uiEntry *e)
static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp) static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp)
{ {
uiDrawTextFontDescriptor desc;
uiDrawTextFont *font; uiDrawTextFont *font;
uiDrawTextLayout *layout; uiDrawTextLayout *layout;
// TODO font = uiFontButtonFont(textFontButton);
memset(&desc, 0, sizeof (uiDrawTextFontDescriptor));
desc.Family = "Arial";
desc.Size = 36;
desc.Weight = uiDrawTextWeightNormal;
desc.Italic = uiDrawTextItalicNormal;
desc.Stretch = uiDrawTextStretchNormal;
font = uiDrawLoadClosestFont(&desc);
layout = uiDrawNewTextLayout("One two three four", font, -1); layout = uiDrawNewTextLayout("One two three four", font, -1);
uiDrawTextLayoutSetColor(layout, uiDrawTextLayoutSetColor(layout,
@ -72,6 +64,11 @@ static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e)
return 0; return 0;
} }
static void onFontChanged(uiFontButton *b, void *data)
{
uiAreaQueueRedrawAll(textArea);
}
static void onTextApply(uiButton *b, void *data) static void onTextApply(uiButton *b, void *data)
{ {
uiAreaQueueRedrawAll(textArea); uiAreaQueueRedrawAll(textArea);
@ -95,6 +92,7 @@ uiBox *makePage10(void)
uiBoxAppend(hbox, uiControl(textString), 1); uiBoxAppend(hbox, uiControl(textString), 1);
textFontButton = uiNewFontButton(); textFontButton = uiNewFontButton();
uiFontButtonOnChanged(textFontButton, onFontChanged, NULL);
uiBoxAppend(hbox, uiControl(textFontButton), 1); uiBoxAppend(hbox, uiControl(textFontButton), 1);
hbox = newHorizontalBox(); hbox = newHorizontalBox();

4
ui.h
View File

@ -609,6 +609,10 @@ struct uiAreaKeyEvent {
typedef struct uiFontButton uiFontButton; typedef struct uiFontButton uiFontButton;
_UI_EXTERN uintmax_t uiFontButtonType(void); _UI_EXTERN uintmax_t uiFontButtonType(void);
#define uiFontButton(this) ((uiFontButton *) uiIsA((this), uiFontButtonType(), 1)) #define uiFontButton(this) ((uiFontButton *) uiIsA((this), uiFontButtonType(), 1))
// TODO document this returns a new font
_UI_EXTERN uiDrawTextFont *uiFontButtonFont(uiFontButton *b);
// TOOD SetFont, mechanics
_UI_EXTERN void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data);
_UI_EXTERN uiFontButton *uiNewFontButton(void); _UI_EXTERN uiFontButton *uiNewFontButton(void);
#ifdef __cplusplus #ifdef __cplusplus