File:  [mozdev] / chimera / BookmarksButton.mm
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Wed Apr 17 22:57:07 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Personal toolbar now works with folders and responds to add/remove of 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: -(void)mouseDown:(NSEvent*)aEvent
   57: {
   58:   if (mIsFolder) {
   59:     nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
   60:     NSMenu* menu = BookmarksService::LocateMenu(content);
   61:     [NSMenu popUpContextMenu: menu withEvent: aEvent forView: self];
   62:   }
   63:   else
   64:     [super mouseDown: aEvent];
   65: }
   66: 
   67: -(void)setElement: (nsIDOMElement*)aElt
   68: {
   69:   mElement = aElt;
   70:   nsAutoString tag;
   71:   mElement->GetLocalName(tag);
   72:   if (tag.Equals(NS_LITERAL_STRING("folder"))) {
   73:     [self setImage: [NSImage imageNamed: @"folder"]];
   74:     mIsFolder = YES;
   75:   }
   76:   else {
   77:     mIsFolder = NO;
   78:     [self setImage: [NSImage imageNamed: @"smallbookmark"]];
   79:     [self setAction: @selector(openBookmark:)];
   80:     [self setTarget: self];
   81:   }
   82:   
   83:   nsAutoString name;
   84:   mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
   85:   [self setTitle: [NSString stringWithCharacters: name.get() length: nsCRT::strlen(name.get())]];
   86: }
   87: 
   88: -(nsIDOMElement*)element
   89: {
   90:   return mElement;
   91: }
   92: 
   93: @end

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