Skip to content

Commit

Permalink
Add CustomButton which does not handle mouse clicks for you
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed May 27, 2024
1 parent 0512f30 commit 0fbdd13
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
52 changes: 52 additions & 0 deletions custombutton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gocoa

// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #import "window.h"
// #import "custombutton.h"
// #include <stdlib.h>
import "C"
import "image"

// Button represents a button control that can trigger actions.
type CustomButton struct {
ptr C.ButtonPtr
callback func(x, y int, secondary bool)
}

var customButtons []*CustomButton

//export onCustomButtonClicked
func onCustomButtonClicked(id C.int, x C.int, y C.int, secondary C.bool) {
buttonID := int(id)
if buttonID < len(buttons) && customButtons[buttonID].callback != nil {
customButtons[buttonID].callback(int(x), int(y), bool(secondary))
}
}

func NewCustomButton(x int, y int, width int, height int) *CustomButton {
buttonID := len(customButtons)
ptr := C.CustomButton_New(C.int(buttonID), C.int(x), C.int(y), C.int(width), C.int(height))
btn := &CustomButton{ptr: ptr}
customButtons = append(customButtons, btn)
return btn
}

func (c *CustomButton) OnClick(fn func(x, y int, secondary bool)) {
c.callback = fn
}

func (c *CustomButton) Remove() {
C.Button_Remove(c.ptr)
}

func (c *CustomButton) SetImage(img *image.RGBA) {
bytes := C.CBytes(img.Pix)
nsImage := C.Image_NewWithRGBA(C.int(img.Bounds().Dx()), C.int(img.Bounds().Dy()), (*C.uchar)(bytes))
C.Button_SetImage(c.ptr, nsImage)
C.free(bytes)
}

func (w *Window) AddCustomButton(c *CustomButton) {
C.Window_AddButton(w.winPtr, c.ptr)
}
11 changes: 11 additions & 0 deletions custombutton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "button.h"
#import "image.h"

@interface CustomButton : NSButton {
}
@property(assign) int goButtonID;
- (void)mouseDown:(NSEvent *)theEvent;
@end

ButtonPtr CustomButton_New(int goButtonID, int x, int y, int w, int h);
void Window_AddCustomButton(void *wndPtr, ButtonPtr btnPtr);
27 changes: 27 additions & 0 deletions custombutton.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "_cgo_export.h"
#import "button.h"

@implementation CustomButton
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint p = [self convertPoint:theEvent.locationInWindow fromView:nil];
onCustomButtonClicked([self goButtonID], p.x, p.y,
theEvent.buttonNumber != 1);
}
@end

ButtonPtr CustomButton_New(int goButtonID, int x, int y, int w, int h) {
CustomButton *nsButton =
[[[CustomButton alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];

[nsButton setGoButtonID:goButtonID];
[nsButton setButtonType:NSButtonTypeMomentaryLight];
[nsButton setBezelStyle:NSBezelStyleRounded];

return (ButtonPtr)nsButton;
}

void Window_AddCustomButton(void *wndPtr, ButtonPtr btnPtr) {
CustomButton *button = (CustomButton *)btnPtr;
NSWindow *window = (NSWindow *)wndPtr;
[[window contentView] addSubview:button];
}

0 comments on commit 0fbdd13

Please sign in to comment.