summaryrefslogtreecommitdiff
path: root/cocoa/gui.m
blob: 80c09d123a9c942950c2611b3caf578d480761fa (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
/*
 * Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import <Cocoa/Cocoa.h>

#import "cocoa/gui.h"
#import "cocoa/plotter.h"
#import "cocoa/BrowserView.h"
#import "cocoa/BrowserViewController.h"
#import "cocoa/BrowserWindowController.h"
#import "cocoa/FormSelectMenu.h"

#import "desktop/gui.h"
#import "desktop/netsurf.h"
#import "desktop/browser.h"
#import "desktop/options.h"
#import "desktop/textinput.h"
#import "desktop/selection.h"
#import "desktop/401login.h"
#import "utils/utils.h"
#import "image/ico.h"
#import "content/fetchers/resource.h"

char *default_stylesheet_url = (char *)"resource:default.css";
char *adblock_stylesheet_url = (char *)"resource:adblock.css";
char *quirks_stylesheet_url = (char *)"resource:quirks.css";

NSString * const kCookiesFileOption = @"CookiesFile";
NSString * const kURLsFileOption = @"URLsFile";
NSString * const kHotlistFileOption = @"Hotlist";
NSString * const kHomepageURLOption = @"HomepageURL";
NSString * const kOptionsFileOption = @"ClassicOptionsFile";
NSString * const kAlwaysCancelDownload = @"AlwaysCancelDownload";
NSString * const kAlwaysCloseMultipleTabs = @"AlwaysCloseMultipleTabs";

#define UNIMPL() NSLog( @"Function '%s' unimplemented", __func__ )

char* gui_get_resource_url(const char *filename)
{
	NSString *path = [[NSBundle mainBundle] pathForResource: [NSString stringWithUTF8String: filename] ofType: @""];
	if (path == nil) return NULL;
	return strdup( [[[NSURL fileURLWithPath: path] absoluteString] UTF8String] );
}

void gui_multitask(void)
{
	// nothing to do
}

void gui_poll(bool active)
{
	cocoa_autorelease();
	
	NSEvent *event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: active ? nil : [NSDate distantFuture]
										   inMode: NSDefaultRunLoopMode dequeue: YES];
	
	if (nil != event) {
		[NSApp sendEvent: event];
		[NSApp updateWindows];	
	}
}

void gui_quit(void)
{
	// nothing to do
}

struct browser_window;

struct gui_window *gui_create_browser_window(struct browser_window *bw,
											 struct browser_window *clone, bool new_tab)
{
	BrowserWindowController *window = nil;

	if (clone != NULL) {
		bw->scale = clone->scale;
		window = [(BrowserViewController *)(clone->window) windowController];
	} else {
		bw->scale = (float) option_scale / 100;	
	}

	BrowserViewController *result = [[BrowserViewController alloc] initWithBrowser: bw];

	if (bw->parent == NULL) {
		if (!new_tab || nil == window) {
			window = [[[BrowserWindowController alloc] init] autorelease];
			[[window window] makeKeyAndOrderFront: nil];
		}
		[window addTab: result];
	} else {
		BrowserViewController *parent = (BrowserViewController *)bw->parent->window;
		NSCParameterAssert( parent != nil );
		[[parent browserView] addSubview: [result view]];
	}
	
	return (struct gui_window *)result;
}

struct browser_window *gui_window_get_browser_window(struct gui_window *g)
{
	return [(BrowserViewController *)g browser];
}

void gui_window_destroy(struct gui_window *g)
{
	BrowserViewController *vc = (BrowserViewController *)g;

	if ([vc browser]->parent != NULL) [[vc view] removeFromSuperview];
	[vc release];
}

void gui_window_set_title(struct gui_window *g, const char *title)
{
	[(BrowserViewController *)g setTitle: [NSString stringWithUTF8String: title]];
}

void gui_window_redraw_window(struct gui_window *g)
{
	[[(BrowserViewController *)g browserView] setNeedsDisplay: YES];
}

void gui_window_update_box(struct gui_window *g, const struct rect *rect)
{
	const NSRect nsrect = cocoa_scaled_rect_wh( [(BrowserViewController *)g browser]->scale, 
											 rect->x0, rect->y0, 
											 rect->x1 - rect->x0, rect->y1 - rect->y0 );
	[[(BrowserViewController *)g browserView] setNeedsDisplayInRect: nsrect];
}

bool gui_window_get_scroll(struct gui_window *g, int *sx, int *sy)
{
	NSCParameterAssert( g != NULL && sx != NULL && sy != NULL );
	
	NSRect visible = [[(BrowserViewController *)g browserView] visibleRect];
	*sx = cocoa_pt_to_px( NSMinX( visible ) );
	*sy = cocoa_pt_to_px( NSMinY( visible ) );
	return true;
}

void gui_window_set_scroll(struct gui_window *g, int sx, int sy)
{
	[[(BrowserViewController *)g browserView] scrollPoint: cocoa_point( sx, sy )];
}

void gui_window_scroll_visible(struct gui_window *g, int x0, int y0,
							   int x1, int y1)
{
	gui_window_set_scroll( g, x0, y0 );
}

void gui_window_position_frame(struct gui_window *g, int x0, int y0,
							   int x1, int y1)
{
	const NSRect rect = cocoa_rect( x0, y0, x1, y1 );
	[[(BrowserViewController *)g view] setFrame: rect];
}

void gui_window_get_dimensions(struct gui_window *g, int *width, int *height,
							   bool scaled)
{
	NSCParameterAssert( width != NULL && height != NULL );
	
	NSRect frame = [[[(BrowserViewController *)g browserView] superview] frame];
	if (scaled) {
		const CGFloat scale = [(BrowserViewController *)g browser]->scale;
		frame.size.width /= scale;
		frame.size.height /= scale;
	}
	*width = cocoa_pt_to_px( NSWidth( frame ) );
	*height = cocoa_pt_to_px( NSHeight( frame ) );
}

void gui_window_update_extent(struct gui_window *g)
{
	BrowserViewController * const window = (BrowserViewController *)g;

	struct browser_window *browser = [window browser];
	int width = content_get_width( browser->current_content );
	int height = content_get_height( browser->current_content );
	
	[[window browserView] setMinimumSize: cocoa_scaled_size( browser->scale, width, height )];
}

void gui_window_set_status(struct gui_window *g, const char *text)
{
	[(BrowserViewController *)g setStatus: [NSString stringWithUTF8String: text]];
}

void gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape)
{
	switch (shape) {
		case GUI_POINTER_DEFAULT:
		case GUI_POINTER_WAIT:
		case GUI_POINTER_PROGRESS:
			[[NSCursor arrowCursor] set];
			break;
			
		case GUI_POINTER_CROSS:
			[[NSCursor crosshairCursor] set];
			break;
			
		case GUI_POINTER_POINT:
		case GUI_POINTER_MENU:
			[[NSCursor pointingHandCursor] set];
			break;
			
		case GUI_POINTER_CARET:
			[[NSCursor IBeamCursor] set];
			break;
			
		case GUI_POINTER_MOVE:
			[[NSCursor closedHandCursor] set];
			break;

		default:
			NSLog( @"Other cursor %d requested", shape );
			[[NSCursor arrowCursor] set];
			break;
	}
}

void gui_window_hide_pointer(struct gui_window *g)
{
}

void gui_window_set_url(struct gui_window *g, const char *url)
{
	[(BrowserViewController *)g setUrl: [NSString stringWithUTF8String: url]];
}

void gui_window_start_throbber(struct gui_window *g)
{
	[(BrowserViewController *)g setIsProcessing: YES];
	[(BrowserViewController *)g updateBackForward];
}

void gui_window_stop_throbber(struct gui_window *g)
{
	[(BrowserViewController *)g setIsProcessing: NO];
	[(BrowserViewController *)g updateBackForward];
}

void gui_window_set_icon(struct gui_window *g, hlcache_handle *icon)
{
	NSBitmapImageRep *bmp = icon != NULL ? (NSBitmapImageRep *)content_get_bitmap( icon ) : NULL;

	NSImage *image = nil;
	if (bmp != nil) {
		image = [[NSImage alloc] initWithSize: NSMakeSize( 32, 32 )];
		[image addRepresentation: bmp];
	} else {
		image = [[NSImage imageNamed: @"NetSurf"] copy];
	}
	[image setFlipped: YES];

	[(BrowserViewController *)g setFavicon: image];
	[image release];
}

void gui_window_set_search_ico(hlcache_handle *ico)
{
	UNIMPL();
}

void gui_window_place_caret(struct gui_window *g, int x, int y, int height)
{
	[[(BrowserViewController *)g browserView] addCaretAt: cocoa_point( x, y ) 
												  height: cocoa_px_to_pt( height )];
}

void gui_window_remove_caret(struct gui_window *g)
{
	[[(BrowserViewController *)g browserView] removeCaret];
}

void gui_window_new_content(struct gui_window *g)
{
	[(BrowserViewController *)g contentUpdated];
}

bool gui_window_scroll_start(struct gui_window *g)
{
	return true;
}

bool gui_window_box_scroll_start(struct gui_window *g,
								 int x0, int y0, int x1, int y1)
{
	return true;
}

bool gui_window_frame_resize_start(struct gui_window *g)
{
	return true;
}

void gui_window_save_link(struct gui_window *g, const char *url, 
						  const char *title)
{
	UNIMPL();
}

void gui_drag_save_object(gui_save_type type, hlcache_handle *c,
						  struct gui_window *g)
{
}

void gui_drag_save_selection(struct selection *s, struct gui_window *g)
{
}


void gui_create_form_select_menu(struct browser_window *bw,
								 struct form_control *control)
{
	FormSelectMenu  *menu = [[FormSelectMenu alloc] initWithControl: control forWindow: bw];
	[menu runInView: [(BrowserViewController *)bw->window browserView]];
	[menu release];
}

void gui_launch_url(const char *url)
{
	[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: [NSString stringWithUTF8String: url]]];
}

struct ssl_cert_info;

void gui_cert_verify(const char *url, const struct ssl_cert_info *certs, 
					 unsigned long num, nserror (*cb)(bool proceed, void *pw),
					 void *cbpw)
{
	cb( false, cbpw );
}


void gui_401login_open(const char *url, const char *realm,
					   nserror (*cb)(bool proceed, void *pw), void *cbpw)
{
	cb( false, cbpw );
}