Commit 9f12d6bf3f0b9ca3f3dc469e841e0abf60aa9691

Thomas de Grivel 2023-11-18T13:35:42

shutdown cocoa app when window closes

diff --git a/libc3/window/cairo/c3_window_cairo_demo.c b/libc3/window/cairo/c3_window_cairo_demo.c
index a23e502..94ecb7a 100644
--- a/libc3/window/cairo/c3_window_cairo_demo.c
+++ b/libc3/window/cairo/c3_window_cairo_demo.c
@@ -25,10 +25,7 @@ bool c3_window_cairo_demo_button (s_window_cairo *window, u8 button,
 {
   assert(window);
   (void) window;
-  (void) button;
-  (void) x;
-  (void) y;
-  printf("on_button: %lu (%ld, %ld)\n", (uw) button, x, y);
+  printf("c3_window_cairo_demo_button: %lu (%ld, %ld)\n", (uw) button, x, y);
   return true;
 }
 
diff --git a/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.h b/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.h
index 990cf1b..2de29f2 100644
--- a/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.h
+++ b/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.h
@@ -14,7 +14,8 @@
 #import "../window_cairo.h"
 #import "window_cairo_quartz_view.h"
 
-@interface WindowCairoQuartzAppDelegate : NSObject <NSApplicationDelegate>
+@interface WindowCairoQuartzAppDelegate : NSObject <NSApplicationDelegate,
+  NSWindowDelegate>
 
 @property (strong, nonatomic) NSView *view;
 @property (strong, nonatomic) NSWindow *window;
diff --git a/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.m b/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.m
index 8f7b423..56f1bf0 100644
--- a/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.m
+++ b/libc3/window/cairo/quartz/window_cairo_quartz_app_delegate.m
@@ -34,6 +34,7 @@
                                        NSWindowStyleMaskResizable)
                               backing:NSBackingStoreBuffered
                                 defer:NO];
+  self.window.delegate = self;
   [self.window makeKeyAndOrderFront:nil];
   [self.window
       setTitle:[NSString
@@ -65,4 +66,8 @@
   [self.view setNeedsDisplay:YES];
 }
 
+- (void)windowWillClose:(NSNotification *)notification {
+  [NSApp stop:nil];
+}
+
 @end
diff --git a/libc3/window/cairo/quartz/window_cairo_quartz_view.m b/libc3/window/cairo/quartz/window_cairo_quartz_view.m
index 000416f..c5cb5fe 100644
--- a/libc3/window/cairo/quartz/window_cairo_quartz_view.m
+++ b/libc3/window/cairo/quartz/window_cairo_quartz_view.m
@@ -31,17 +31,6 @@
     return YES;
 }
 
-- (void)keyDown:(NSEvent *)event {
-  NSString *characters = [event characters];
-  unichar character = [characters characterAtIndex:0];
-  u32 keysym = quartz_to_xkbcommon(character);
-  if (! self.window_cairo->key(self.window_cairo, keysym)) {
-    [self.window close];
-    [self.window release];
-    [[NSApplication sharedApplication] stop:nil];
-  }
-}
-
 - (void) drawRect:(NSRect)dirtyRect {
     [super drawRect:dirtyRect];
     struct CGContext *cg_context = [[NSGraphicsContext currentContext]
@@ -63,5 +52,34 @@
     cairo_surface_destroy(surface);
 }
 
+- (void)keyDown:(NSEvent *)event {
+  NSString *characters = [event characters];
+  unichar character = [characters characterAtIndex:0];
+  u32 keysym = quartz_to_xkbcommon(character);
+  if (! self.window_cairo->key(self.window_cairo, keysym)) {
+    [self.window close];
+    [self.window release];
+    [[NSApplication sharedApplication] stop:nil];
+  }
+}
+
+- (void)mouseDown:(NSEvent *)event {
+  NSPoint p = [event locationInWindow];
+  if (! self.window_cairo->button(self.window_cairo, 0, p.x, p.y)) {
+    [self.window close];
+    [self.window release];
+    [[NSApplication sharedApplication] stop:nil];
+  }
+}
+
+- (void)rightMouseDown:(NSEvent *)event {
+  NSPoint p = [event locationInWindow];
+  if (! self.window_cairo->button(self.window_cairo, 2, p.x, p.y)) {
+    [self.window close];
+    [self.window release];
+    [[NSApplication sharedApplication] stop:nil];
+  }
+}
+
 @end