forked from mojbro/gocoa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e4acc7
commit 1db047e
Showing
7 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gocoa | ||
|
||
// #cgo CFLAGS: -x objective-c | ||
// #cgo LDFLAGS: -framework Cocoa | ||
// #import "stepper.h" | ||
import "C" | ||
|
||
type Stepper struct { | ||
ptr C.StepperPtr | ||
id int | ||
cb func(value float64) | ||
} | ||
|
||
var steppers []*Stepper | ||
|
||
//export onStepperValueChanged | ||
func onStepperValueChanged(id C.int) { | ||
stepper := getStepper(id) | ||
if stepper != nil && stepper.cb != nil { | ||
stepper.cb(stepper.Value()) | ||
} | ||
} | ||
|
||
func getStepper(id C.int) *Stepper { | ||
stepperID := int(id) | ||
if stepperID < len(steppers) { | ||
return steppers[stepperID] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewStepper(x int, y int, width int, height int) *Stepper { | ||
stepperID := len(steppers) | ||
stepperPtr := C.Stepper_New(C.int(stepperID), C.int(x), C.int(y), C.int(width), C.int(height)) | ||
|
||
stepper := &Stepper{ | ||
ptr: stepperPtr, | ||
id: stepperID, | ||
} | ||
steppers = append(steppers, stepper) | ||
return stepper | ||
} | ||
|
||
func (stepper *Stepper) SetMaxValue(val float64) { | ||
C.Stepper_SetMaxValue(stepper.ptr, C.double(val)) | ||
} | ||
|
||
func (stepper *Stepper) SetMinValue(val float64) { | ||
C.Stepper_SetMinValue(stepper.ptr, C.double(val)) | ||
} | ||
|
||
func (stepper *Stepper) SetIncrement(val float64) { | ||
C.Stepper_SetIncrement(stepper.ptr, C.double(val)) | ||
} | ||
|
||
func (stepper *Stepper) SetValue(val float64) { | ||
C.Stepper_SetValue(stepper.ptr, C.double(val)) | ||
} | ||
|
||
func (stepper *Stepper) SetValueWraps(val bool) { | ||
C.Stepper_SetValueWraps(stepper.ptr, C.bool(val)) | ||
} | ||
|
||
func (stepper *Stepper) Value() float64 { | ||
return float64(C.Stepper_Value(stepper.ptr)) | ||
} | ||
|
||
func (stepper *Stepper) OnStepperValueChanged(fn func(value float64)) { | ||
stepper.cb = fn | ||
} | ||
|
||
// Remove removes a Slider from the parent view again. | ||
func (stepper *Stepper) Remove() { | ||
C.Stepper_Remove(stepper.ptr) | ||
steppers[stepper.id] = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#import <Cocoa/Cocoa.h> | ||
|
||
@interface StepperHandler : NSObject | ||
|
||
@property(assign) int goStepperId; | ||
- (void)stepperValueChanged:(id)sender; | ||
|
||
@end | ||
|
||
typedef void *StepperPtr; | ||
|
||
StepperPtr Stepper_New(int goStepperId, int x, int y, int w, int h); | ||
void Stepper_SetMaxValue(StepperPtr StepperPtr, double val); | ||
void Stepper_SetMinValue(StepperPtr StepperPtr, double val); | ||
void Stepper_SetIncrement(StepperPtr StepperPtr, double val); | ||
void Stepper_SetValue(StepperPtr StepperPtr, double val); | ||
double Stepper_Value(StepperPtr StepperPtr); | ||
void Stepper_Remove(StepperPtr StepperPtr); | ||
bool Stepper_ValueWraps(StepperPtr StepperPtr); | ||
void Stepper_SetValueWraps(StepperPtr StepperPtr, bool wraps); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#import "stepper.h" | ||
#include "_cgo_export.h" | ||
|
||
@implementation StepperHandler | ||
- (void)stepperValueChanged:(id)sender { | ||
onStepperValueChanged([self goStepperId]); | ||
} | ||
@end | ||
|
||
StepperPtr Stepper_New(int goStepperId, int x, int y, int w, int h) { | ||
id nsStepper = | ||
[[[NSStepper alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease]; | ||
|
||
StepperHandler *handler = [[StepperHandler alloc] init]; | ||
[handler setGoStepperId:goStepperId]; | ||
[handler autorelease]; | ||
[nsStepper setTarget:handler]; | ||
[nsStepper setAction:@selector(stepperValueChanged:)]; | ||
|
||
return (StepperPtr)nsStepper; | ||
} | ||
|
||
void Stepper_SetMaxValue(StepperPtr StepperPtr, double val) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper setMaxValue:val]; | ||
} | ||
|
||
void Stepper_SetMinValue(StepperPtr StepperPtr, double val) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper setMinValue:val]; | ||
} | ||
|
||
void Stepper_SetIncrement(StepperPtr StepperPtr, double val) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper setIncrement:val]; | ||
} | ||
|
||
void Stepper_SetValue(StepperPtr StepperPtr, double val) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper setDoubleValue:val]; | ||
} | ||
|
||
bool Stepper_ValueWraps(StepperPtr StepperPtr) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
return stepper.valueWraps; | ||
} | ||
|
||
void Stepper_SetValueWraps(StepperPtr StepperPtr, bool wraps) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper setValueWraps:wraps]; | ||
} | ||
|
||
double Stepper_Value(StepperPtr StepperPtr) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
return stepper.doubleValue; | ||
} | ||
|
||
void Stepper_Remove(StepperPtr StepperPtr) { | ||
NSStepper *stepper = (NSStepper *)StepperPtr; | ||
[stepper removeFromSuperview]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters