summaryrefslogtreecommitdiff
path: root/plugingui/nativewindow_cocoa.mm
blob: 531c2a80a4b1d47b30db0027b55f2ab155e7d6d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* -*- Mode: ObjC; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            nativewindow_cocoa.mm
 *
 *  Fri Dec  2 20:31:03 CET 2016
 *  Copyright 2016 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

/*

loop:
http://stackoverflow.com/questions/6732400/cocoa-integrate-nsapplication-into-an-existing-c-mainloop

draw pixels:
http://stackoverflow.com/questions/10955913/how-can-i-display-an-array-of-pixels-on-a-nswindow
*/

#include <stdio.h>
#include <unistd.h>

#import <Cocoa/Cocoa.h>

bool running = true;

@interface MyWindow : NSWindow
{
@public
  //  MyView* my_view;
}

- (id) initWithContentRect:(NSRect)contentRect
                 styleMask:(unsigned int)aStyle
                   backing:(NSBackingStoreType)bufferingType
                     defer:(BOOL)flag;
//- (void) setMyView:(MyView*)view;
- (BOOL) windowShouldClose:(id)sender;
- (void) mouseMoved:(NSEvent*)event;
- (void) drawRect:(NSRect)dirtyRect;
@end

@implementation MyWindow

- (id)initWithContentRect:(NSRect)contentRect
                styleMask:(unsigned int)aStyle
                  backing:(NSBackingStoreType)bufferingType
                    defer:(BOOL)flag
{
        NSWindow* result = [super initWithContentRect:contentRect
                                            styleMask:(NSClosableWindowMask |
                                                       NSTitledWindowMask |
                                                       NSResizableWindowMask)
                                              backing:NSBackingStoreBuffered defer:NO];

        [result setAcceptsMouseMovedEvents:YES];
        [result setLevel: CGShieldingWindowLevel() + 1];

        return result;
}

//- (void)setMyView:(MyView*)view
//{
//  //my_view = view;
//      //[self setContentSize:NSMakeSize(view->width, view->height) ];
//}

- (BOOL)windowShouldClose:(id)sender
{
  printf("closing!\n");
  //if (puglview->closeFunc)
  //            puglview->closeFunc(puglview);
  running = false;
  return YES;
}

#define WIDTH 100
#define HEIGHT 100
#define SIZE (WIDTH*HEIGHT)
#define BYTES_PER_PIXEL 2
#define BITS_PER_COMPONENT 5
#define BITS_PER_PIXEL 16

- (void)drawRect:(NSRect)dirtyRect
{
}
- (void) mouseMoved:(NSEvent*)event
{
  /*
        if (puglview->motionFunc) {
                NSPoint loc = [event locationInWindow];
                puglview->mods = getModifiers(puglview, event);
                puglview->motionFunc(puglview, loc.x, puglview->height - loc.y);
        }
  */
  NSPoint loc = [event locationInWindow];
  printf("mouseMove: %f %f\n", loc.x, loc.y);
}

@end




@interface MyView : NSView
{
        int colorBits;
        int depthBits;
@public
  //PuglView* puglview;

  NSTrackingArea* trackingArea;
}

- (id) initWithFrame:(NSRect)frame
           colorBits:(int)numColorBits
           depthBits:(int)numDepthBits;
- (void) reshape;
- (void) drawRect:(NSRect)rect;

@end
@implementation MyView

- (id) initWithFrame:(NSRect)frame
           colorBits:(int)numColorBits
           depthBits:(int)numDepthBits
{
  colorBits = numColorBits;
  depthBits = numDepthBits;

  //NSOpenGLPixelFormatAttribute pixelAttribs[16] = {
  //  NSOpenGLPFADoubleBuffer,
  //  NSOpenGLPFAAccelerated,
  //  NSOpenGLPFAColorSize,
  //  colorBits,
  //  NSOpenGLPFADepthSize,
  //  depthBits,
  //  0
  //};
  //
  //NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc]
  //            initWithAttributes:pixelAttribs];
  //
  //if (pixelFormat) {
  //  self = [super initWithFrame:frame pixelFormat:pixelFormat];
  //  [pixelFormat release];
  //  if (self) {
  //    [[self openGLContext] makeCurrentContext];
  //    [self reshape];
  //  }
  //} else {
  //  self = nil;
  //}

  return self;
}
- (void) reshape
{
  //[[self openGLContext] update];
  //
  //NSRect bounds = [self bounds];
  //int    width  = bounds.size.width;
  //int    height = bounds.size.height;
  //
  //if (puglview) {
  //  /* NOTE: Apparently reshape gets called when the GC gets around to
  //     deleting the view (?), so we must have reset puglview to NULL when
  //     this comes around.
  //  */
  //  if (puglview->reshapeFunc) {
  //    puglview->reshapeFunc(puglview, width, height);
  //  } else {
  //    puglDefaultReshape(puglview, width, height);
  //  }
  //
  //  puglview->width  = width;
  //  puglview->height = height;
  //}
}
- (void) drawRect:(NSRect)rect
{
  printf("drawRect\n");

  // Get current context
  CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

  // Colorspace RGB
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

  // Pixel Matrix allocation
  unsigned short *pixels = calloc(SIZE, sizeof(unsigned short));

  // Random pixels will give you a non-organized RAINBOW
  for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
      pixels[i+ j*HEIGHT] = arc4random() % USHRT_MAX;
    }
  }

  // Provider
  CGDataProviderRef provider = CGDataProviderCreateWithData(nil, pixels, SIZE, nil);

  // CGImage
  CGImageRef image = CGImageCreate(WIDTH,
            HEIGHT,
            BITS_PER_COMPONENT,
            BITS_PER_PIXEL,
            BYTES_PER_PIXEL*WIDTH,
            colorSpace,
            kCGImageAlphaNoneSkipFirst,
            // xRRRRRGGGGGBBBBB - 16-bits, first bit is ignored!
            provider,
            nil, //No decode
            NO,  //No interpolation
            kCGRenderingIntentDefault); // Default rendering

  // Draw
  CGContextDrawImage(context, CGRectMake(0, 0, WIDTH, HEIGHT), image);

  // Once everything is written on screen we can release everything
  CGImageRelease(image);
  CGColorSpaceRelease(colorSpace);
  CGDataProviderRelease(provider);
}
@end

#include "window.h"

namespace GUI
{

NativeWindowCocoa::NativeWindowCocoa(void* native_window, Window& window)
	: buffer(nullptr)
	, window(window)
{
  [NSAutoreleasePool new];
  [NSApplication sharedApplication];
  [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  /*
  id menubar = [[NSMenu new] autorelease];
  id appMenuItem = [[NSMenuItem new] autorelease];
  [menubar addItem:appMenuItem];
  [NSApp setMainMenu:menubar];
  id appMenu = [[NSMenu new] autorelease];
  id appName = [[NSProcessInfo processInfo] processName];
  id quitTitle = [@"Quit " stringByAppendingString:appName];
  id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
    action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
  [appMenu addItem:quitMenuItem];
  [appMenuItem setSubmenu:appMenu];
  */
  id window = [[[MyWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200)
          styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]
  autorelease];
  [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
  //  [window setTitle:appName];
  [window makeKeyAndOrderFront:nil];

  id my_view       = [MyView new];
  [window setContentView:my_view];
  [NSApp activateIgnoringOtherApps:YES];
  [window makeFirstResponder:my_view];

  //  [NSApp run];
}

NativeWindowCocoa::~NativeWindowCocoa()
{
}

void NativeWindowCocoa::setFixedSize(int width, int height)
{
}

void NativeWindowCocoa::resize(int width, int height)
{
}

void NativeWindowCocoa::move(int x, int y)
{
}

void NativeWindowCocoa::show()
{
}

void NativeWindowCocoa::hide()
{
}

void NativeWindowCocoa::handleBuffer()
{
}

void NativeWindowCocoa::redraw()
{
}

void NativeWindowCocoa::setCaption(const std::string &caption)
{
}

void NativeWindowCocoa::grabMouse(bool grab)
{
}

bool NativeWindowCocoa::hasEvent()
{
	return false;
}

std::shared_ptr<Event> NativeWindowCocoa::getNextEvent()
{
  //[window setNeedsDisplay: YES];

  while(running) {
    NSEvent * event;

    do
      {
        event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefa\
ultRunLoopMode dequeue:YES];


        //Convert the cocoa events to something useful here and add them to your own event queue

        [NSApp sendEvent: event];
      }
    while(event != nil);

    //printf("loop\n");
    usleep(10000);
  }

	return nullptr;
}

std::shared_ptr<Event> NativeWindowCocoa::peekNextEvent()
{
	return nullptr;
}

} // GUI::