From 6544e76d5fa478e984a52139e8410975c2ae82d7 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 17 Aug 2015 15:16:54 -0400 Subject: [PATCH] Implemented the uiSlider and uiProgressBar functions and events. More TODOs. Some build warnings fixed. --- redo/reredo/darwin/alloc.m | 2 +- redo/reredo/darwin/progressbar.m | 6 ++- redo/reredo/darwin/slider.m | 68 +++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/redo/reredo/darwin/alloc.m b/redo/reredo/darwin/alloc.m index f569641a..3421c62e 100644 --- a/redo/reredo/darwin/alloc.m +++ b/redo/reredo/darwin/alloc.m @@ -23,7 +23,7 @@ void initAlloc(void) void uninitAlloc(void) { if ([allocations count] == 0) { - NSInteger i; + NSUInteger i; for (i = 0; i < [delegates count]; i++) [[delegates objectAtIndex:i] release]; diff --git a/redo/reredo/darwin/progressbar.m b/redo/reredo/darwin/progressbar.m index e8f505d6..f55aea38 100644 --- a/redo/reredo/darwin/progressbar.m +++ b/redo/reredo/darwin/progressbar.m @@ -1,6 +1,10 @@ // 14 august 2015 #import "uipriv_darwin.h" +// TODOs: +// - 10.8: left/right side of progress bar clipped +// - 10.8: increasing value animates just like with Aero + struct uiProgressBar { uiDarwinControl c; NSProgressIndicator *pi; @@ -16,7 +20,7 @@ void uiProgressBarSetValue(uiProgressBar *p, int value) { if (value < 0 || value > 100) complain("value %d out of range in progressbarSetValue()", value); - // TODO + [p->pi setDoubleValue:((double) value)]; } uiProgressBar *uiNewProgressBar(void) diff --git a/redo/reredo/darwin/slider.m b/redo/reredo/darwin/slider.m index 29769373..d36c19a7 100644 --- a/redo/reredo/darwin/slider.m +++ b/redo/reredo/darwin/slider.m @@ -1,8 +1,6 @@ // 14 august 2015 #import "uipriv_darwin.h" -// TODO events - struct uiSlider { uiDarwinControl c; NSSlider *slider; @@ -10,20 +8,73 @@ struct uiSlider { void *onChangedData; }; -uiDarwinDefineControl( +@interface sliderDelegateClass : NSObject { + NSMapTable *sliders; +} +- (IBAction)onChanged:(id)sender; +- (void)registerSlider:(uiSlider *)b; +- (void)unregisterSlider:(uiSlider *)b; +@end + +@implementation sliderDelegateClass + +- (id)init +{ + self = [super init]; + if (self) + self->sliders = newMap(); + return self; +} + +- (void)dealloc +{ + if ([self->sliders count] != 0) + complain("attempt to destroy shared slider delegate but sliders are still registered to it"); + [self->sliders release]; + [super dealloc]; +} + +- (IBAction)onChanged:(id)sender +{ + uiSlider *s; + + s = (uiSlider *) mapGet(self->sliders, sender); + (*(s->onChanged))(s, s->onChangedData); +} + +- (void)registerSlider:(uiSlider *)s +{ + mapSet(self->sliders, s->slider, s); + [s->slider setTarget:self]; + [s->slider setAction:@selector(onChanged:)]; +} + +- (void)unregisterSlider:(uiSlider *)s +{ + [s->slider setTarget:nil]; + [self->sliders removeObjectForKey:s->slider]; +} + +@end + +static sliderDelegateClass *sliderDelegate = nil; + +uiDarwinDefineControlWithOnDestroy( uiSlider, // type name uiSliderType, // type function - slider // handle + slider, // handle + [sliderDelegate unregisterSlider:this]; // on destroy ) intmax_t uiSliderValue(uiSlider *s) { - return PUT_CODE_HERE; + // NSInteger is the most similar to intmax_t + return [s->slider integerValue]; } void uiSliderSetValue(uiSlider *s, intmax_t value) { - // TODO + [s->slider setIntegerValue:value]; } void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *, void *), void *data) @@ -55,6 +106,11 @@ uiSlider *uiNewSlider(intmax_t min, intmax_t max) cell = (NSSliderCell *) [s->slider cell]; [cell setSliderType:NSLinearSlider]; + if (sliderDelegate == nil) { + sliderDelegate = [sliderDelegateClass new]; + [delegates addObject:sliderDelegate]; + } + [sliderDelegate registerSlider:s]; uiSliderOnChanged(s, defaultOnChanged, NULL); uiDarwinFinishNewControl(s, uiSlider);