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.
Add CustomButton which does not handle mouse clicks for you
- Loading branch information
1 parent
0512f30
commit 0fbdd13
Showing
3 changed files
with
90 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
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) | ||
} |
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,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); |
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,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]; | ||
} |