Skip to content

Commit

Permalink
Add Stepper (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack authored May 22, 2024
1 parent 0e4acc7 commit 1db047e
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
>
> #### Changes to original
>
> - 2024-05-22 [#5](https://github.com/roblillack/gocoa/pull/5): Add NSStepper
> - 2024-05-22 [#4](https://github.com/roblillack/gocoa/pull/4): Add text alignment options to textfields
> - 2024-05-22 [#3](https://github.com/roblillack/gocoa/pull/3): Add support for `OnChange` callbacks for textfields
> - 2024-05-22 [#1](https://github.com/roblillack/gocoa/pull/1): Add support for removing controls (based on work by @phaus)
Expand Down
77 changes: 77 additions & 0 deletions stepper.go
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
}
20 changes: 20 additions & 0 deletions stepper.h
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);
61 changes: 61 additions & 0 deletions stepper.m
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];
}
4 changes: 4 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (wnd *Window) AddSlider(slider *Slider) {
C.Window_AddSlider(wnd.winPtr, slider.sliderPtr)
}

func (wnd *Window) AddStepper(stepper *Stepper) {
C.Window_AddStepper(wnd.winPtr, stepper.ptr)
}

func (wnd *Window) AddComboBox(comboBox *ComboBox) {
C.Window_AddComboBox(wnd.winPtr, comboBox.comboBoxPtr)
}
Expand Down
2 changes: 2 additions & 0 deletions window.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "textfield.h"
#include "progressIndicator.h"
#include "slider.h"
#include "stepper.h"

void* Window_New(int goWindowID, int x, int y, int width, int height, const char* title);
void* Centered_Window_New(int goWindowID, int width, int height, const char* title);
Expand All @@ -23,6 +24,7 @@ void Window_AddTextField(void *wndPtr, TextFieldPtr tfPtr);
void Window_AddProgressIndicator(void *wndPtr, ProgressIndicatorPtr progressIndicatorPtr);
void Window_AddImageView(void *wndPtr, ImageViewPtr imageViewPtr);
void Window_AddSlider(void *wndPtr, SliderPtr sliderPtr);
void Window_AddStepper(void *wndPtr, StepperPtr ptr);
void Window_AddComboBox(void *wndPtr, ComboBoxPtr comboBoxPtr);
void Window_Update(void *wndPtr);
void Window_SetTitle(void *wndPtr, const char* title);
Expand Down
7 changes: 7 additions & 0 deletions window.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ void Window_AddSlider(void *wndPtr, SliderPtr sliderPtr)
[[window contentView] addSubview:slider];
}

void Window_AddStepper(void *wndPtr, StepperPtr ptr)
{
NSStepper* stepper = (NSStepper*)ptr;
NSWindow* window = (NSWindow*)wndPtr;
[[window contentView] addSubview:stepper];
}

void Window_AddComboBox(void *wndPtr, ComboBoxPtr comboBoxPtr)
{
NSComboBox* comboBox = (NSComboBox*)comboBoxPtr;
Expand Down

0 comments on commit 1db047e

Please sign in to comment.