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 (15 years, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Personal toolbar now works with folders and responds to add/remove of bookmarks.

//
//  BookmarksButton.mm
//  Chimera
//
//  Created by David Hyatt on Mon Apr 15 2002.
//  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//

#import "BookmarksButton.h"
#include "nsIDOMElement.h"
#include "nsIContent.h"
#include "nsString.h"
#import "BookmarksService.h"

@implementation BookmarksButton

- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code here.
    mElement = nsnull;
    [self setBezelStyle: NSRegularSquareBezelStyle];
    [self setBordered: NO];
    [self setImagePosition: NSImageLeft];
    [self setRefusesFirstResponder: YES];
    [self setFont: [NSFont labelFontOfSize: 11.0]];
  }
  return self;
}

-(IBAction)openBookmark:(id)aSender
{
  printf("Yo?\n");
  
  // Get the href attribute.  This is the URL we want to load.
  nsAutoString href;
  mElement->GetAttribute(NS_LITERAL_STRING("href"), href);
  nsCAutoString cref; cref.AssignWithConversion(href);
  if (cref.IsEmpty())
    return;

  NSString* url = [NSString stringWithCString: cref.get()];

  // Now load the URL in the window.
  [[[self window] windowController] loadURL:[NSURL URLWithString: url]];

  // Focus and activate our content area.
  [[[[[self window] windowController] getMyBrowserView] getBrowserView] setActive: YES];
}

-(void)drawRect:(NSRect)aRect
{
  [super drawRect: aRect];
}

-(void)mouseDown:(NSEvent*)aEvent
{
  if (mIsFolder) {
    nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
    NSMenu* menu = BookmarksService::LocateMenu(content);
    [NSMenu popUpContextMenu: menu withEvent: aEvent forView: self];
  }
  else
    [super mouseDown: aEvent];
}

-(void)setElement: (nsIDOMElement*)aElt
{
  mElement = aElt;
  nsAutoString tag;
  mElement->GetLocalName(tag);
  if (tag.Equals(NS_LITERAL_STRING("folder"))) {
    [self setImage: [NSImage imageNamed: @"folder"]];
    mIsFolder = YES;
  }
  else {
    mIsFolder = NO;
    [self setImage: [NSImage imageNamed: @"smallbookmark"]];
    [self setAction: @selector(openBookmark:)];
    [self setTarget: self];
  }
  
  nsAutoString name;
  mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
  [self setTitle: [NSString stringWithCharacters: name.get() length: nsCRT::strlen(name.get())]];
}

-(nsIDOMElement*)element
{
  return mElement;
}

@end

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