| 1 |
/* viewer-cocoa.m: Code for Cocoa-based viewer |
| 2 |
* |
| 3 |
* Copyright (C) 2025 Khaled Hosny |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
#include "config.h" |
| 21 |
#import <Cocoa/Cocoa.h> |
| 22 |
|
| 23 |
#include <cairo.h> |
| 24 |
#include <cairo-quartz.h> |
| 25 |
#include <gio/gio.h> |
| 26 |
|
| 27 |
#include "viewer-render.h" |
| 28 |
#include "viewer-cocoa.h" |
| 29 |
#include "viewer-cocoa-resources.h" |
| 30 |
|
| 31 |
struct _CocoaViewer |
| 32 |
{ |
| 33 |
NSApplication *app; |
| 34 |
}; |
| 35 |
|
| 36 |
@interface PangoView : NSView |
| 37 |
|
| 38 |
@property (nonatomic, assign, nullable) cairo_surface_t *surface; |
| 39 |
|
| 40 |
@end |
| 41 |
|
| 42 |
@implementation PangoView |
| 43 |
|
| 44 |
- (void)drawRect:(NSRect)dirtyRect |
| 45 |
{ |
| 46 |
if ([self surface]) |
| 47 |
{ |
| 48 |
NSGraphicsContext *context = [NSGraphicsContext currentContext]; |
| 49 |
CGContextRef cgContext = [context CGContext]; |
| 50 |
CGFloat scaleFactor = [[self window] backingScaleFactor]; |
| 51 |
|
| 52 |
CGContextTranslateCTM (cgContext, 0.0, [self bounds].size.height); |
| 53 |
CGContextScaleCTM (cgContext, 1/scaleFactor, -1/scaleFactor); |
| 54 |
|
| 55 |
cairo_surface_t *viewSurface = cairo_quartz_surface_create_for_cg_context (cgContext, |
| 56 |
[self bounds].size.width * scaleFactor, |
| 57 |
[self bounds].size.height * scaleFactor); |
| 58 |
|
| 59 |
cairo_surface_set_device_scale (viewSurface, scaleFactor, scaleFactor); |
| 60 |
cairo_t *cr = cairo_create (viewSurface); |
| 61 |
|
| 62 |
cairo_set_source_surface (cr, [self surface], 0, 0); |
| 63 |
cairo_paint (cr); |
| 64 |
|
| 65 |
cairo_destroy (cr); |
| 66 |
cairo_surface_destroy (viewSurface); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@end |
| 71 |
|
| 72 |
static void |
| 73 |
set_app_icon (NSApplication *app) |
| 74 |
{ |
| 75 |
GBytes *icon_bytes = g_resource_lookup_data (viewer_cocoa_get_resource (), |
| 76 |
"/org/gnome/pango/viewer-cocoa/icon", |
| 77 |
G_RESOURCE_LOOKUP_FLAGS_NONE, NULL); |
| 78 |
if (icon_bytes) |
| 79 |
{ |
| 80 |
gsize data_size; |
| 81 |
gconstpointer data = g_bytes_get_data (icon_bytes, &data_size); |
| 82 |
NSData *iconData = [NSData dataWithBytes:data length:data_size]; |
| 83 |
NSImage *appIcon = [[NSImage alloc] initWithData:iconData]; |
| 84 |
if (appIcon) |
| 85 |
{ |
| 86 |
[app setApplicationIconImage:appIcon]; |
| 87 |
[appIcon release]; |
| 88 |
} |
| 89 |
g_bytes_unref (icon_bytes); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
gpointer |
| 94 |
cocoa_view_create (const PangoViewer *klass G_GNUC_UNUSED) |
| 95 |
{ |
| 96 |
CocoaViewer *viewer = g_slice_new (CocoaViewer); |
| 97 |
|
| 98 |
NSApplication *app = [NSApplication sharedApplication]; |
| 99 |
[app setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 100 |
[app finishLaunching]; |
| 101 |
|
| 102 |
set_app_icon (app); |
| 103 |
|
| 104 |
viewer->app = app; |
| 105 |
|
| 106 |
return viewer; |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
cocoa_view_destroy (gpointer instance) |
| 111 |
{ |
| 112 |
CocoaViewer *viewer = (CocoaViewer *)instance; |
| 113 |
[viewer->app terminate:nil]; |
| 114 |
g_slice_free (CocoaViewer, instance); |
| 115 |
} |
| 116 |
|
| 117 |
gpointer |
| 118 |
cocoa_view_create_surface (gpointer instance G_GNUC_UNUSED, |
| 119 |
int width G_GNUC_UNUSED, |
| 120 |
int height G_GNUC_UNUSED) |
| 121 |
{ |
| 122 |
return cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
cocoa_view_destroy_surface (gpointer instance G_GNUC_UNUSED, |
| 127 |
gpointer surface) |
| 128 |
{ |
| 129 |
cairo_surface_destroy ((cairo_surface_t *)surface); |
| 130 |
} |
| 131 |
|
| 132 |
gpointer |
| 133 |
cocoa_view_create_window (gpointer instance G_GNUC_UNUSED, |
| 134 |
const char *title, |
| 135 |
int width, |
| 136 |
int height) |
| 137 |
{ |
| 138 |
NSRect windowRect = NSMakeRect (0, 0, width, height); |
| 139 |
NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect |
| 140 |
styleMask:(NSWindowStyleMaskTitled | |
| 141 |
NSWindowStyleMaskClosable) |
| 142 |
backing:NSBackingStoreBuffered |
| 143 |
defer:NO]; |
| 144 |
|
| 145 |
[window setTitle:[NSString stringWithUTF8String:title]]; |
| 146 |
[window setContentView:[[PangoView alloc] initWithFrame:windowRect]]; |
| 147 |
[window makeKeyAndOrderFront:nil]; |
| 148 |
[window center]; |
| 149 |
[window setReleasedWhenClosed:NO]; |
| 150 |
|
| 151 |
return window; |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
cocoa_view_destroy_window (gpointer instance G_GNUC_UNUSED, |
| 156 |
gpointer win) |
| 157 |
{ |
| 158 |
NSWindow *window = (NSWindow *)win; |
| 159 |
|
| 160 |
[[window contentView] release]; |
| 161 |
[window close]; |
| 162 |
} |
| 163 |
|
| 164 |
gpointer |
| 165 |
cocoa_view_display (gpointer instance, |
| 166 |
gpointer surface, |
| 167 |
gpointer win, |
| 168 |
int width G_GNUC_UNUSED, |
| 169 |
int height G_GNUC_UNUSED, |
| 170 |
gpointer state) |
| 171 |
{ |
| 172 |
CocoaViewer *viewer = (CocoaViewer *)instance; |
| 173 |
|
| 174 |
NSWindow *window = (NSWindow *)win; |
| 175 |
[window makeKeyAndOrderFront:nil]; |
| 176 |
[window orderFrontRegardless]; |
| 177 |
[window makeMainWindow]; |
| 178 |
[window makeKeyWindow]; |
| 179 |
|
| 180 |
PangoView *view = [window contentView]; |
| 181 |
[view setSurface:(cairo_surface_t *)surface]; |
| 182 |
[view setNeedsDisplay:YES]; |
| 183 |
|
| 184 |
NSApplication *app = viewer->app; |
| 185 |
[app activateIgnoringOtherApps:YES]; |
| 186 |
|
| 187 |
while ([window isVisible]) |
| 188 |
{ |
| 189 |
NSEvent *event = [app nextEventMatchingMask:NSEventMaskAny |
| 190 |
untilDate:[NSDate dateWithTimeIntervalSinceNow:0.1] |
| 191 |
inMode:NSDefaultRunLoopMode |
| 192 |
dequeue:YES]; |
| 193 |
if (event) |
| 194 |
{ |
| 195 |
if ([event type] == NSEventTypeKeyDown) |
| 196 |
{ |
| 197 |
NSString *characters = [event characters]; |
| 198 |
if ([characters length] > 0) |
| 199 |
{ |
| 200 |
switch ([characters characterAtIndex:0]) |
| 201 |
{ |
| 202 |
case 'q': |
| 203 |
case 'w': |
| 204 |
return GINT_TO_POINTER (-1); |
| 205 |
case 'b': |
| 206 |
return GINT_TO_POINTER (GPOINTER_TO_INT (state) + 1); |
| 207 |
default: |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
[app sendEvent:event]; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return GINT_TO_POINTER (-1); |
| 217 |
} |
| 218 |
|
| 219 |
const PangoViewer cocoa_viewer = { |
| 220 |
"Cocoa", |
| 221 |
NULL, |
| 222 |
NULL, |
| 223 |
cocoa_view_create, |
| 224 |
cocoa_view_destroy, |
| 225 |
NULL, |
| 226 |
cocoa_view_create_surface, |
| 227 |
cocoa_view_destroy_surface, |
| 228 |
NULL, |
| 229 |
NULL, |
| 230 |
cocoa_view_create_window, |
| 231 |
cocoa_view_destroy_window, |
| 232 |
cocoa_view_display |
| 233 |
}; |