2011-01-19 23:19:43 +03:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-05-26 01:46:27 +04:00
|
|
|
#import "utils/nsoption.h"
|
2012-10-11 15:48:15 +04:00
|
|
|
#import "utils/corestrings.h"
|
2011-05-12 21:31:55 +04:00
|
|
|
#import "utils/filename.h"
|
2014-05-27 02:43:36 +04:00
|
|
|
#import "utils/file.h"
|
2013-02-22 18:44:13 +04:00
|
|
|
#import "utils/messages.h"
|
2011-05-12 21:31:55 +04:00
|
|
|
#import "utils/url.h"
|
2016-05-02 15:12:43 +03:00
|
|
|
#import "desktop/browser_history.h"
|
|
|
|
#import "desktop/browser.h"
|
|
|
|
#import "desktop/textinput.h"
|
2014-02-03 22:12:44 +04:00
|
|
|
#import "content/hlcache.h"
|
2011-05-12 21:31:55 +04:00
|
|
|
|
2016-05-02 15:12:43 +03:00
|
|
|
#import "cocoa/gui.h"
|
|
|
|
#import "cocoa/BrowserViewController.h"
|
|
|
|
#import "cocoa/BrowserView.h"
|
|
|
|
#import "cocoa/BrowserWindowController.h"
|
|
|
|
#import "cocoa/fetch.h"
|
|
|
|
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
|
|
|
|
@implementation BrowserViewController
|
|
|
|
|
|
|
|
@synthesize browser;
|
|
|
|
@synthesize url;
|
|
|
|
@synthesize browserView;
|
|
|
|
@synthesize windowController;
|
|
|
|
@synthesize title;
|
|
|
|
@synthesize status;
|
2011-01-20 16:22:39 +03:00
|
|
|
@synthesize isProcessing;
|
2011-01-25 20:16:17 +03:00
|
|
|
@synthesize favicon;
|
2011-01-26 16:35:07 +03:00
|
|
|
@synthesize canGoBack;
|
|
|
|
@synthesize canGoForward;
|
2011-01-19 23:19:43 +03:00
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (void) dealloc
|
2011-01-20 14:38:10 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
[self setUrl: nil];
|
|
|
|
[self setBrowserView: nil];
|
|
|
|
[self setWindowController: nil];
|
|
|
|
[self setTitle: nil];
|
|
|
|
[self setStatus: nil];
|
|
|
|
[self setFavicon: nil];
|
|
|
|
|
|
|
|
[super dealloc];
|
2011-01-20 14:38:10 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- initWithBrowser: (struct browser_window *) bw
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
if ((self = [super initWithNibName: @"Browser" bundle: nil]) == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser = bw;
|
|
|
|
|
|
|
|
return self;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) navigate: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
nsurl *urlns;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
error = nsurl_create([url UTF8String], &urlns);
|
|
|
|
if (error != NSERROR_OK) {
|
2016-05-02 12:24:49 +03:00
|
|
|
cocoa_warning(messages_get_errorcode(error), 0);
|
2016-02-09 03:34:47 +03:00
|
|
|
} else {
|
|
|
|
browser_window_navigate(browser,
|
|
|
|
urlns,
|
|
|
|
NULL,
|
|
|
|
BW_NAVIGATE_HISTORY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
nsurl_unref(urlns);
|
|
|
|
}
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (void) awakeFromNib
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
[browserView setBrowser: browser];
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (IBAction) zoomIn: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
browser_window_set_scale(browser,
|
|
|
|
browser_window_get_scale(browser) * 1.1,
|
|
|
|
true);
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) zoomOut: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
browser_window_set_scale(browser,
|
|
|
|
browser_window_get_scale(browser) * 0.9,
|
|
|
|
true);
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) zoomOriginal: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
browser_window_set_scale(browser,
|
|
|
|
(float)nsoption_int(scale) / 100.0,
|
|
|
|
true);
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) backForwardSelected: (id) sender
|
2011-01-24 12:12:22 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
if ([sender selectedSegment] == 0) {
|
|
|
|
[self goBack: sender];
|
|
|
|
} else {
|
|
|
|
[self goForward: sender];
|
|
|
|
}
|
2011-01-24 12:12:22 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) goBack: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
if (browser && browser_window_history_back_available( browser )) {
|
|
|
|
browser_window_history_back(browser, false);
|
|
|
|
[self updateBackForward];
|
|
|
|
}
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) goForward: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
if (browser && browser_window_history_forward_available( browser )) {
|
|
|
|
browser_window_history_forward(browser, false);
|
|
|
|
[self updateBackForward];
|
|
|
|
}
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) goHome: (id) sender
|
2011-01-31 21:48:09 +03:00
|
|
|
{
|
2013-02-18 16:24:49 +04:00
|
|
|
nsurl *urlns;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
error = nsurl_create(nsoption_charp(homepage_url), &urlns);
|
|
|
|
if (error == NSERROR_OK) {
|
|
|
|
error = browser_window_navigate(browser,
|
|
|
|
urlns,
|
|
|
|
NULL,
|
2016-02-09 03:34:47 +03:00
|
|
|
BW_NAVIGATE_HISTORY,
|
2013-02-18 16:24:49 +04:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
nsurl_unref(urlns);
|
|
|
|
}
|
|
|
|
if (error != NSERROR_OK) {
|
2016-05-02 12:24:49 +03:00
|
|
|
cocoa_warning(messages_get_errorcode(error), 0);
|
2013-02-18 16:24:49 +04:00
|
|
|
}
|
2011-01-31 21:48:09 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) reloadPage: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
browser_window_reload( browser, true );
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) stopLoading: (id) sender
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
browser_window_stop( browser );
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
- (IBAction) viewSource: (id) sender
|
2011-05-12 21:31:55 +04:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
struct hlcache_handle *content;
|
|
|
|
size_t size;
|
|
|
|
const char *source;
|
|
|
|
char *path = NULL;
|
|
|
|
|
|
|
|
if (browser == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
content = browser_window_get_content(browser);
|
|
|
|
if (content == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
source = content_get_source_data(content, &size);
|
|
|
|
if (source == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to load local files directly. */
|
|
|
|
netsurf_nsurl_to_path(hlcache_handle_get_url(content), &path);
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
/* We cannot release the requested filename until after it
|
|
|
|
* has finished being used. As we can't easily find out when
|
|
|
|
* this is, we simply don't bother releasing it and simply
|
|
|
|
* allow it to be re-used next time NetSurf is started. The
|
|
|
|
* memory overhead from doing this is under 1 byte per
|
|
|
|
* filename. */
|
|
|
|
const char *filename = filename_request();
|
|
|
|
const char *extension = "txt";
|
|
|
|
fprintf(stderr, "filename '%p'\n", filename);
|
|
|
|
if (filename == NULL)
|
|
|
|
return;
|
|
|
|
lwc_string *str = content_get_mime_type(content);
|
|
|
|
if (str) {
|
|
|
|
NSString *mime = [NSString stringWithUTF8String:lwc_string_data(str)];
|
|
|
|
NSString *uti = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (CFStringRef)mime, NULL);
|
|
|
|
NSString *ext = (NSString *)UTTypeCopyPreferredTagWithClass((CFStringRef)uti, kUTTagClassFilenameExtension);
|
|
|
|
extension = [ext UTF8String];
|
|
|
|
lwc_string_unref(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURL *dataUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%s.%s", filename, extension]
|
|
|
|
relativeToURL:[NSURL fileURLWithPath:@TEMP_FILENAME_PREFIX]];
|
|
|
|
|
|
|
|
|
|
|
|
NSData *data = [NSData dataWithBytes:source length:size];
|
|
|
|
[data writeToURL:dataUrl atomically:NO];
|
|
|
|
path = [[dataUrl path] UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
NSString * p = [NSString stringWithUTF8String: path];
|
|
|
|
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
|
|
|
|
[ws openFile:p withApplication:@"Xcode"];
|
|
|
|
}
|
2011-05-12 21:31:55 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
|
|
|
|
static inline bool
|
|
|
|
compare_float( float a, float b )
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
const float epsilon = 0.00001;
|
|
|
|
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fabs( (a - b) / b ) <= epsilon;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (BOOL) validateUserInterfaceItem: (id) item
|
2011-01-19 23:19:43 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
SEL action = [item action];
|
|
|
|
|
|
|
|
if (action == @selector(copy:)) {
|
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_COPY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector(cut:)) {
|
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_CUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector(paste:)) {
|
|
|
|
return browser_window_get_editor_flags( browser ) & BW_EDITOR_CAN_PASTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( stopLoading: )) {
|
|
|
|
return browser_window_stop_available( browser );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( zoomOriginal: )) {
|
|
|
|
return !compare_float( browser_window_get_scale(browser), (float)nsoption_int(scale) / 100.0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( goBack: )) {
|
|
|
|
return canGoBack;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == @selector( goForward: )) {
|
|
|
|
return canGoForward;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
2011-01-19 23:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (void) updateBackForward
|
2011-01-26 16:35:07 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
[browserView updateHistory];
|
|
|
|
[self setCanGoBack: browser != NULL && browser_window_history_back_available( browser )];
|
|
|
|
[self setCanGoForward: browser != NULL && browser_window_history_forward_available( browser )];
|
2011-01-26 16:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (void) contentUpdated
|
2011-01-26 16:35:07 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
[browserView updateHistory];
|
2011-01-26 16:35:07 +03:00
|
|
|
}
|
|
|
|
|
2011-02-28 18:24:38 +03:00
|
|
|
struct history_add_menu_item_data {
|
2016-02-09 03:34:47 +03:00
|
|
|
NSInteger index;
|
|
|
|
NSMenu *menu;
|
|
|
|
id target;
|
2011-02-28 18:24:38 +03:00
|
|
|
};
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
static bool
|
|
|
|
history_add_menu_item_cb(const struct browser_window *bw,
|
|
|
|
int x0, int y0, int x1, int y1,
|
|
|
|
const struct history_entry *page,
|
|
|
|
void *user_data )
|
2011-02-28 18:24:38 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
struct history_add_menu_item_data *data = user_data;
|
|
|
|
|
|
|
|
NSMenuItem *item = nil;
|
|
|
|
if (data->index < [data->menu numberOfItems]) {
|
|
|
|
item = [data->menu itemAtIndex: data->index];
|
|
|
|
} else {
|
|
|
|
item = [[NSMenuItem alloc] initWithTitle: @""
|
|
|
|
action: @selector( historyItemSelected: )
|
|
|
|
keyEquivalent: @""];
|
|
|
|
[data->menu addItem: item];
|
|
|
|
[item release];
|
|
|
|
}
|
|
|
|
++data->index;
|
|
|
|
|
|
|
|
[item setTarget: data->target];
|
|
|
|
[item setTitle: [NSString stringWithUTF8String: browser_window_history_entry_get_title( page )]];
|
|
|
|
[item setRepresentedObject: [NSValue valueWithPointer: page]];
|
|
|
|
|
|
|
|
return true;
|
2011-02-28 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (IBAction) historyItemSelected: (id) sender
|
2011-02-28 18:24:38 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
struct history_entry *entry = [[sender representedObject] pointerValue];
|
|
|
|
browser_window_history_go( browser, entry, false );
|
|
|
|
[self updateBackForward];
|
2011-02-28 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (void) buildBackMenu: (NSMenu *)menu
|
2011-02-28 18:24:38 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
struct history_add_menu_item_data data = {
|
|
|
|
.index = 0,
|
|
|
|
.menu = menu,
|
|
|
|
.target = self
|
|
|
|
};
|
|
|
|
browser_window_history_enumerate_back(browser,
|
|
|
|
history_add_menu_item_cb,
|
|
|
|
&data);
|
|
|
|
while (data.index < [menu numberOfItems]) {
|
|
|
|
[menu removeItemAtIndex: data.index];
|
|
|
|
}
|
2011-02-28 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 03:34:47 +03:00
|
|
|
- (void) buildForwardMenu: (NSMenu *)menu
|
2011-02-28 18:24:38 +03:00
|
|
|
{
|
2016-02-09 03:34:47 +03:00
|
|
|
struct history_add_menu_item_data data = {
|
|
|
|
.index = 0,
|
|
|
|
.menu = menu,
|
|
|
|
.target = self
|
|
|
|
};
|
|
|
|
browser_window_history_enumerate_forward(browser,
|
|
|
|
history_add_menu_item_cb,
|
|
|
|
&data);
|
|
|
|
while (data.index < [menu numberOfItems]) {
|
|
|
|
[menu removeItemAtIndex: data.index];
|
|
|
|
}
|
2011-02-28 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
@end
|