File:  [mozdev] / chimera / BookmarksButton.mm
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Wed Apr 17 23:16:53 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Starting to wire up context menus for bookmarks.

    1: //
    2: //  BookmarksButton.mm
    3: //  Chimera
    4: //
    5: //  Created by David Hyatt on Mon Apr 15 2002.
    6: //  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
    7: //
    8: 
    9: #import "BookmarksButton.h"
   10: #include "nsIDOMElement.h"
   11: #include "nsIContent.h"
   12: #include "nsString.h"
   13: #import "BookmarksService.h"
   14: 
   15: @implementation BookmarksButton
   16: 
   17: - (id)initWithFrame:(NSRect)frame {
   18:   self = [super initWithFrame:frame];
   19:   if (self) {
   20:     // Initialization code here.
   21:     mElement = nsnull;
   22:     [self setBezelStyle: NSRegularSquareBezelStyle];
   23:     [self setBordered: NO];
   24:     [self setImagePosition: NSImageLeft];
   25:     [self setRefusesFirstResponder: YES];
   26:     [self setFont: [NSFont labelFontOfSize: 11.0]];
   27:   }
   28:   return self;
   29: }
   30: 
   31: -(IBAction)openBookmark:(id)aSender
   32: {
   33:   printf("Yo?\n");
   34:   
   35:   // Get the href attribute.  This is the URL we want to load.
   36:   nsAutoString href;
   37:   mElement->GetAttribute(NS_LITERAL_STRING("href"), href);
   38:   nsCAutoString cref; cref.AssignWithConversion(href);
   39:   if (cref.IsEmpty())
   40:     return;
   41: 
   42:   NSString* url = [NSString stringWithCString: cref.get()];
   43: 
   44:   // Now load the URL in the window.
   45:   [[[self window] windowController] loadURL:[NSURL URLWithString: url]];
   46: 
   47:   // Focus and activate our content area.
   48:   [[[[[self window] windowController] getMyBrowserView] getBrowserView] setActive: YES];
   49: }
   50: 
   51: -(void)drawRect:(NSRect)aRect
   52: {
   53:   [super drawRect: aRect];
   54: }
   55: 
   56: -(NSMenu*)menuForEvent:(NSEvent*)aEvent
   57: {
   58:   return [[self superview] menu];
   59: }
   60: 
   61: -(void)mouseDown:(NSEvent*)aEvent
   62: {
   63:   if (mIsFolder) {
   64:     nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
   65:     NSMenu* menu = BookmarksService::LocateMenu(content);
   66:     [NSMenu popUpContextMenu: menu withEvent: aEvent forView: self];
   67:   }
   68:   else
   69:     [super mouseDown: aEvent];
   70: }
   71: 
   72: -(void)setElement: (nsIDOMElement*)aElt
   73: {
   74:   mElement = aElt;
   75:   nsAutoString tag;
   76:   mElement->GetLocalName(tag);
   77:   if (tag.Equals(NS_LITERAL_STRING("folder"))) {
   78:     [self setImage: [NSImage imageNamed: @"folder"]];
   79:     mIsFolder = YES;
   80:   }
   81:   else {
   82:     mIsFolder = NO;
   83:     [self setImage: [NSImage imageNamed: @"smallbookmark"]];
   84:     [self setAction: @selector(openBookmark:)];
   85:     [self setTarget: self];
   86:   }
   87:   
   88:   nsAutoString name;
   89:   mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
   90:   [self setTitle: [NSString stringWithCharacters: name.get() length: nsCRT::strlen(name.get())]];
   91: }
   92: 
   93: -(nsIDOMElement*)element
   94: {
   95:   return mElement;
   96: }
   97: 
   98: @end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>