Skip to content

Commit 101ca94

Browse files
committed
imageview: Allow adding image.Images
1 parent 0fbdd13 commit 101ca94

File tree

3 files changed

+87
-30
lines changed

3 files changed

+87
-30
lines changed

imageview.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ package gocoa
33
// #cgo CFLAGS: -x objective-c
44
// #cgo LDFLAGS: -framework Cocoa
55
// #import "imageview.h"
6+
// #include <stdlib.h>
67
import "C"
7-
import "fmt"
8+
import (
9+
"fmt"
10+
"image"
11+
)
812

913
// Represents an ImageView control that can display images.
1014
type ImageView struct {
@@ -45,17 +49,42 @@ const (
4549

4650
var imageViews []*ImageView
4751

48-
func NewImageView(x int, y int, width int, height int, url string) *ImageView {
52+
func NewImageViewWithContentsOfURL(x int, y int, width int, height int, url string) *ImageView {
53+
imageViewID := len(imageViews)
54+
imageViewPtr := C.ImageView_NewWithContentsOfURL(C.int(imageViewID), C.int(x), C.int(y), C.int(width), C.int(height), C.CString(url))
55+
56+
img := &ImageView{
57+
imageViewPtr: imageViewPtr,
58+
}
59+
imageViews = append(imageViews, img)
60+
return img
61+
}
62+
63+
func NewImageView(x int, y int, width int, height int) *ImageView {
4964
imageViewID := len(imageViews)
50-
imageViewPtr := C.ImageView_New(C.int(imageViewID), C.int(x), C.int(y), C.int(width), C.int(height), C.CString(url))
65+
imageViewPtr := C.ImageView_New(C.int(imageViewID), C.int(x), C.int(y), C.int(width), C.int(height))
5166

5267
img := &ImageView{
5368
imageViewPtr: imageViewPtr,
5469
}
5570
imageViews = append(imageViews, img)
71+
72+
return img
73+
}
74+
75+
func NewImageViewWithImage(x int, y int, width int, height int, image *image.RGBA) *ImageView {
76+
img := NewImageView(x, y, width, height)
77+
img.SetImage(image)
5678
return img
5779
}
5880

81+
func (imageView *ImageView) SetImage(img *image.RGBA) {
82+
bytes := C.CBytes(img.Pix)
83+
nsImage := C.Image_NewWithRGBA(C.int(img.Bounds().Dx()), C.int(img.Bounds().Dy()), (*C.uchar)(bytes))
84+
C.ImageView_SetImage(imageView.imageViewPtr, nsImage)
85+
C.free(bytes)
86+
}
87+
5988
func (imageView *ImageView) SetEditable(editable bool) {
6089
if editable {
6190
C.ImageView_SetEditable(imageView.imageViewPtr, 1)

imageview.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
#import "image.h"
12
#import <Cocoa/Cocoa.h>
23

3-
// typedef void (*callback)(void);
4+
typedef void *ImageViewPtr;
45

5-
typedef void* ImageViewPtr;
6-
7-
ImageViewPtr ImageView_New(int goImageViewID, int x, int y, int w, int h, const char* url);
6+
ImageViewPtr ImageView_New(int goImageViewID, int x, int y, int w, int h);
7+
ImageViewPtr ImageView_NewWithContentsOfURL(int goImageViewID, int x, int y,
8+
int w, int h, const char *url);
89
void ImageView_SetAnimates(ImageViewPtr imageViewPtr, int animates);
9-
void ImageView_SetContentTintColor(ImageViewPtr imageViewPtr, int r, int g, int b, int a);
10+
void ImageView_SetContentTintColor(ImageViewPtr imageViewPtr, int r, int g,
11+
int b, int a);
1012
void ImageView_SetEditable(ImageViewPtr imageViewPtr, int editable);
1113
void ImageView_SetFrameStyle(ImageViewPtr imageViewPtr, int frameStyle);
1214
void ImageView_SetImageAlignment(ImageViewPtr imageViewPtr, int imageAlignment);
1315
void ImageView_SetImageScaling(ImageViewPtr imageViewPtr, int imageScaling);
1416
void ImageView_Remove(ImageViewPtr imageViewPtr);
17+
18+
void ImageView_SetImage(ImageViewPtr imageViewPtr, ImagePtr imagePtr);

imageview.m

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,70 @@
11
#import "imageview.h"
22
#include "_cgo_export.h"
3+
#import "image.h"
34

4-
ButtonPtr ImageView_New(int goButtonID, int x, int y, int w, int h, const char* url) {
5-
NSImage *theImage = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithUTF8String:url]]];
5+
ImageViewPtr ImageView_New(int goButtonID, int x, int y, int w, int h) {
6+
id nsImageView =
7+
[[[NSImageView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
68

7-
id nsImageView = [[[NSImageView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
8-
[nsImageView setImage:theImage];
9+
return (ImageViewPtr)nsImageView;
10+
}
11+
12+
ImageViewPtr ImageView_NewWithContentsOfURL(int goButtonID, int x, int y, int w,
13+
int h, const char *url) {
14+
NSImage *theImage = [[NSImage alloc]
15+
initWithContentsOfURL:[NSURL
16+
URLWithString:[NSString
17+
stringWithUTF8String:url]]];
18+
19+
id nsImageView =
20+
[[[NSImageView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease];
21+
[nsImageView setImage:theImage];
922

10-
return (ImageViewPtr)nsImageView;
23+
return (ImageViewPtr)nsImageView;
1124
}
1225

1326
void ImageView_SetFrameStyle(ImageViewPtr imageViewPtr, int frameStyle) {
14-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
15-
[nsImageView setImageFrameStyle:frameStyle];
27+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
28+
[nsImageView setImageFrameStyle:frameStyle];
1629
}
1730

18-
void ImageView_SetImageAlignment(ImageViewPtr imageViewPtr, int imageAlignment) {
19-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
20-
[nsImageView setImageAlignment:imageAlignment];
31+
void ImageView_SetImageAlignment(ImageViewPtr imageViewPtr,
32+
int imageAlignment) {
33+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
34+
[nsImageView setImageAlignment:imageAlignment];
2135
}
2236

2337
void ImageView_SetImageScaling(ImageViewPtr imageViewPtr, int imageScaling) {
24-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
25-
[nsImageView setImageScaling:imageScaling];
38+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
39+
[nsImageView setImageScaling:imageScaling];
2640
}
2741

2842
void ImageView_SetAnimates(ImageViewPtr imageViewPtr, int animates) {
29-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
30-
[nsImageView setAnimates:animates];
43+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
44+
[nsImageView setAnimates:animates];
3145
}
3246

33-
void ImageView_SetContentTintColor(ImageViewPtr imageViewPtr, int r, int g, int b, int a) {
34-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
35-
[nsImageView setContentTintColor:[NSColor colorWithCalibratedRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f]];
47+
void ImageView_SetContentTintColor(ImageViewPtr imageViewPtr, int r, int g,
48+
int b, int a) {
49+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
50+
[nsImageView setContentTintColor:[NSColor colorWithCalibratedRed:r / 255.f
51+
green:g / 255.f
52+
blue:b / 255.f
53+
alpha:a / 255.f]];
3654
}
3755

3856
void ImageView_SetEditable(ImageViewPtr imageViewPtr, int editable) {
39-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
40-
[nsImageView setEditable:editable];
57+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
58+
[nsImageView setEditable:editable];
4159
}
4260

4361
void ImageView_Remove(ImageViewPtr imageViewPtr) {
44-
NSImageView* nsImageView = (NSImageView*)imageViewPtr;
45-
[nsImageView removeFromSuperview];
46-
}
62+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
63+
[nsImageView removeFromSuperview];
64+
}
65+
66+
void ImageView_SetImage(ImageViewPtr imageViewPtr, ImagePtr imagePtr) {
67+
NSImage *theImage = (NSImage *)imagePtr;
68+
NSImageView *nsImageView = (NSImageView *)imageViewPtr;
69+
[nsImageView setImage:theImage];
70+
}

0 commit comments

Comments
 (0)