From 67c9970e544f987fc275c7a5eac67a9123e94868 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 25 Jan 2011 17:16:17 +0000 Subject: Implemented favicons. Doesn't work for .ICO files svn path=/trunk/netsurf/; revision=11485 --- cocoa/BrowserViewController.h | 2 ++ cocoa/BrowserViewController.m | 1 + cocoa/BrowserWindowController.h | 2 ++ cocoa/BrowserWindowController.m | 2 ++ cocoa/URLFieldCell.h | 2 ++ cocoa/URLFieldCell.m | 51 +++++++++++++++++++++++++++++++++++------ cocoa/gui.m | 15 +++++++++++- cocoa/res/BrowserWindow.xib | 17 +++++++++++++- 8 files changed, 83 insertions(+), 9 deletions(-) (limited to 'cocoa') diff --git a/cocoa/BrowserViewController.h b/cocoa/BrowserViewController.h index fc83054a4..020e49e25 100644 --- a/cocoa/BrowserViewController.h +++ b/cocoa/BrowserViewController.h @@ -31,6 +31,7 @@ struct browser_window; NSString *title; NSString *status; BOOL isProcessing; + NSImage *favicon; } @property (readwrite, assign, nonatomic) struct browser_window *browser; @@ -40,6 +41,7 @@ struct browser_window; @property (readwrite, copy, nonatomic) NSString *title; @property (readwrite, copy, nonatomic) NSString *status; @property (readwrite, assign, nonatomic) BOOL isProcessing; +@property (readwrite, copy, nonatomic) NSImage *favicon; - initWithBrowser: (struct browser_window *) bw; diff --git a/cocoa/BrowserViewController.m b/cocoa/BrowserViewController.m index 0d08676be..3d35f8f0a 100644 --- a/cocoa/BrowserViewController.m +++ b/cocoa/BrowserViewController.m @@ -37,6 +37,7 @@ @synthesize title; @synthesize status; @synthesize isProcessing; +@synthesize favicon; - (void) dealloc; { diff --git a/cocoa/BrowserWindowController.h b/cocoa/BrowserWindowController.h index 25482bbfa..333a50429 100644 --- a/cocoa/BrowserWindowController.h +++ b/cocoa/BrowserWindowController.h @@ -26,6 +26,7 @@ PSMTabBarControl *tabBar; NSTabView *tabView; URLFieldCell *urlField; + NSObjectController *activeBrowserController; BrowserViewController *activeBrowser; } @@ -33,6 +34,7 @@ @property (readwrite, retain, nonatomic) IBOutlet PSMTabBarControl *tabBar; @property (readwrite, retain, nonatomic) IBOutlet NSTabView *tabView; @property (readwrite, retain, nonatomic) IBOutlet URLFieldCell *urlField; +@property (readwrite, retain, nonatomic) IBOutlet NSObjectController *activeBrowserController; @property (readwrite, assign, nonatomic) BrowserViewController *activeBrowser; diff --git a/cocoa/BrowserWindowController.m b/cocoa/BrowserWindowController.m index ceb3f9f28..019807513 100644 --- a/cocoa/BrowserWindowController.m +++ b/cocoa/BrowserWindowController.m @@ -32,6 +32,7 @@ @synthesize urlField; @synthesize activeBrowser; +@synthesize activeBrowserController; - (id) init; { @@ -62,6 +63,7 @@ [[self window] setAcceptsMouseMovedEvents: YES]; [urlField setRefreshAction: @selector(reloadPage:)]; + [urlField bind: @"favicon" toObject: activeBrowserController withKeyPath: @"selection.favicon" options: nil]; } - (void) addTab: (BrowserViewController *)browser; diff --git a/cocoa/URLFieldCell.h b/cocoa/URLFieldCell.h index bb0d6572b..38a75a139 100644 --- a/cocoa/URLFieldCell.h +++ b/cocoa/URLFieldCell.h @@ -21,9 +21,11 @@ @interface URLFieldCell : NSTextFieldCell { NSButtonCell *refreshCell; + NSImage *favicon; } @property (readwrite, assign, nonatomic) SEL refreshAction; @property (readwrite, assign, nonatomic) id refreshTarget; +@property (readwrite, retain, nonatomic) NSImage *favicon; @end diff --git a/cocoa/URLFieldCell.m b/cocoa/URLFieldCell.m index cb9565aa1..5e5dfdb6b 100644 --- a/cocoa/URLFieldCell.m +++ b/cocoa/URLFieldCell.m @@ -24,35 +24,52 @@ @property (readonly, retain, nonatomic) NSButtonCell *refreshCell; - (NSRect) buttonFrame: (NSRect) cellFrame; +- (NSRect) urlFrame: (NSRect) cellFrame; +- (NSRect) iconFrame: (NSRect) cellFrame; @end @implementation URLFieldCell +@synthesize favicon; + +- (void) setFavicon: (NSImage *)newIcon; +{ + if (favicon != newIcon) { + [favicon release]; + favicon = [newIcon retain]; + [[self controlView] setNeedsDisplay: YES]; + } +} + #define BUTTON_SIZE 32 #define PADDING 2 - (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView; { - [super drawInteriorWithFrame: cellFrame inView: controlView]; - const NSRect buttonRect = [self buttonFrame: cellFrame]; - [[self refreshCell] drawInteriorWithFrame: buttonRect inView: controlView]; + [favicon drawInRect: [self iconFrame: cellFrame] fromRect: NSZeroRect + operation: NSCompositeSourceOver fraction: 1.0]; + + [super drawInteriorWithFrame: [self urlFrame: cellFrame] inView: controlView]; + + [[self refreshCell] drawInteriorWithFrame: [self buttonFrame: cellFrame] + inView: controlView]; } - (void) selectWithFrame: (NSRect)aRect inView: (NSView *)controlView editor: (NSText *)textObj delegate: (id)anObject start: (NSInteger)selStart length: (NSInteger)selLength; { - aRect.size.width -= BUTTON_SIZE + PADDING; - [super selectWithFrame: aRect inView: controlView editor: textObj + const NSRect textFrame = [self urlFrame: aRect]; + [super selectWithFrame: textFrame inView: controlView editor: textObj delegate: anObject start: selStart length: selLength]; } - (void) editWithFrame: (NSRect)aRect inView: (NSView *)controlView editor: (NSText *)textObj delegate: (id)anObject event: (NSEvent *)theEvent; { - aRect.size.width -= BUTTON_SIZE + PADDING; - [super editWithFrame: aRect inView: controlView editor: textObj + const NSRect textFrame = [self urlFrame: aRect]; + [super editWithFrame: textFrame inView: controlView editor: textObj delegate: anObject event: theEvent]; } @@ -84,6 +101,26 @@ return buttonRect; } +- (NSRect) urlFrame: (NSRect) cellFrame; +{ + NSRect textFrame = cellFrame; + textFrame.origin.x += cellFrame.size.height; + textFrame.size.width -= cellFrame.size.height + BUTTON_SIZE + PADDING; + return textFrame; +} + +- (NSRect) iconFrame: (NSRect)cellFrame; +{ + NSRect iconFrame = { + .origin = { + .x = cellFrame.origin.x + PADDING, + .y = cellFrame.origin.y, + }, + .size = NSMakeSize( NSHeight( cellFrame ), NSHeight( cellFrame ) ) + }; + return NSInsetRect( iconFrame, 2 * PADDING, 2 * PADDING ); +} + - (NSButtonCell *) refreshCell; { if (nil == refreshCell) { diff --git a/cocoa/gui.m b/cocoa/gui.m index 03a9b1015..83c0cbd65 100644 --- a/cocoa/gui.m +++ b/cocoa/gui.m @@ -32,6 +32,8 @@ #import "desktop/401login.h" #import "utils/utils.h" +#import "image/ico.h" + char *default_stylesheet_url; char *adblock_stylesheet_url; char *quirks_stylesheet_url; @@ -236,7 +238,18 @@ void gui_window_stop_throbber(struct gui_window *g) void gui_window_set_icon(struct gui_window *g, hlcache_handle *icon) { - // ignore + 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 = [[NSApp applicationIconImage] retain]; + } + + [(BrowserViewController *)g setFavicon: image]; + [image release]; } void gui_window_set_search_ico(hlcache_handle *ico) diff --git a/cocoa/res/BrowserWindow.xib b/cocoa/res/BrowserWindow.xib index f474162ee..2af21b77b 100644 --- a/cocoa/res/BrowserWindow.xib +++ b/cocoa/res/BrowserWindow.xib @@ -638,6 +638,14 @@ 79 + + + activeBrowserController + + + + 80 + @@ -931,7 +939,7 @@ - 79 + 80 @@ -1069,12 +1077,14 @@ YES YES + activeBrowserController tabBar tabView urlField YES + NSObjectController PSMTabBarControl NSTabView URLFieldCell @@ -1084,12 +1094,17 @@ YES YES + activeBrowserController tabBar tabView urlField YES + + activeBrowserController + NSObjectController + tabBar PSMTabBarControl -- cgit v1.2.3