diff --git a/darwin/fontbutton.m b/darwin/fontbutton.m index 13ed889e..8e2af4e3 100644 --- a/darwin/fontbutton.m +++ b/darwin/fontbutton.m @@ -12,6 +12,7 @@ - (IBAction)fontButtonClicked:(id)sender; - (void)activateFontButton; - (void)deactivateFontButton:(BOOL)activatingAnother; +- (uiDrawTextFont *)libuiFont; @end // only one may be active at one time @@ -20,6 +21,8 @@ static fontButton *activeFontButton = nil; struct uiFontButton { uiDarwinControl c; fontButton *button; + void (*onChanged)(uiFontButton *, void *); + void *onChangedData; }; uiDarwinDefineControl( @@ -101,6 +104,7 @@ uiDarwinDefineControl( { NSFontManager *fm; NSFont *old; + uiFontButton *b = self->libui_b; fm = (NSFontManager *) sender; old = self->libui_font; @@ -110,6 +114,7 @@ uiDarwinDefineControl( if (self->libui_font != old) [old release]; [self updateFontButtonLabel]; + (*(b->onChanged))(b, b->onChangedData); } - (NSUInteger)validModesForFontPanel:(NSFontPanel *)panel @@ -119,6 +124,11 @@ uiDarwinDefineControl( NSFontPanelCollectionModeMask; } +- (uiDrawTextFont *)libuiFont +{ + return mkTextFontFromNSFont(self->libui_font); +} + @end // 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; } +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 *b; @@ -151,6 +177,8 @@ uiFontButton *uiNewFontButton(void) b->button = [[fontButton alloc] initWithFrame:NSZeroRect libuiFontButton:b]; uiDarwinSetControlFont(b->button, NSRegularControlSize); + uiFontButtonOnChanged(b, defaultOnChanged, NULL); + uiDarwinFinishNewControl(b, uiFontButton); return b; diff --git a/test/page10.c b/test/page10.c index 3db41a20..4af59cbc 100644 --- a/test/page10.c +++ b/test/page10.c @@ -25,18 +25,10 @@ static double entryDouble(uiEntry *e) static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp) { - uiDrawTextFontDescriptor desc; uiDrawTextFont *font; uiDrawTextLayout *layout; - // TODO - memset(&desc, 0, sizeof (uiDrawTextFontDescriptor)); - desc.Family = "Arial"; - desc.Size = 36; - desc.Weight = uiDrawTextWeightNormal; - desc.Italic = uiDrawTextItalicNormal; - desc.Stretch = uiDrawTextStretchNormal; - font = uiDrawLoadClosestFont(&desc); + font = uiFontButtonFont(textFontButton); layout = uiDrawNewTextLayout("One two three four", font, -1); uiDrawTextLayoutSetColor(layout, @@ -72,6 +64,11 @@ static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e) return 0; } +static void onFontChanged(uiFontButton *b, void *data) +{ + uiAreaQueueRedrawAll(textArea); +} + static void onTextApply(uiButton *b, void *data) { uiAreaQueueRedrawAll(textArea); @@ -95,6 +92,7 @@ uiBox *makePage10(void) uiBoxAppend(hbox, uiControl(textString), 1); textFontButton = uiNewFontButton(); + uiFontButtonOnChanged(textFontButton, onFontChanged, NULL); uiBoxAppend(hbox, uiControl(textFontButton), 1); hbox = newHorizontalBox(); diff --git a/ui.h b/ui.h index b17cd30c..fed3ab0e 100644 --- a/ui.h +++ b/ui.h @@ -609,6 +609,10 @@ struct uiAreaKeyEvent { typedef struct uiFontButton uiFontButton; _UI_EXTERN uintmax_t uiFontButtonType(void); #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); #ifdef __cplusplus