File:  [mozdev] / chimera / BookmarksToolbar.mm
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Wed Apr 17 22:57:07 2002 UTC (15 years, 6 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Personal toolbar now works with folders and responds to add/remove of bookmarks.

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

#import "BookmarksButton.h"
#import "BookmarksToolbar.h"
#import "BookmarksService.h"
#include "nsIDOMElement.h"

@implementation BookmarksToolbar

- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    mBookmarks = nsnull;
    mButtons = [[NSMutableArray alloc] init];
  }
  return self;
}

-(void)initializeToolbar
{
  // Initialization code here.
  mBookmarks = new BookmarksService(self);
  mBookmarks->AddObserver();
  mBookmarks->EnsureToolbarRoot();
  [self buildButtonList];
}

-(void) dealloc
{
  [mButtons autorelease];
  mBookmarks->RemoveObserver();
  delete mBookmarks;
  [super dealloc];
}

- (void)drawRect:(NSRect)aRect {
  // Fill the background with our background color.
 // [[NSColor colorWithCalibratedWhite: 0.98 alpha: 1.0] set];
 // NSRectFill(aRect);

  //printf("The rect is: %f %f %f %f\n", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
  
  if (aRect.origin.y + aRect.size.height ==
      [self bounds].size.height) {
    // The personal toolbar is 21 pixels tall.  The bottom two pixels
    // are a separator.
    [[NSColor colorWithCalibratedWhite: 0.90 alpha: 1.0] set];
    NSRectFill(NSMakeRect(aRect.origin.x, aRect.origin.y, aRect.size.width, 2));
  }

  // The buttons will paint themselves.
}

-(void)buildButtonList
{
  // Build the buttons, and then lay them all out.
  nsCOMPtr<nsIDOMNode> child;
  BookmarksService::gToolbarRoot->GetFirstChild(getter_AddRefs(child));
  while (child) {
    nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
    if (childElt) {
      BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
      [button setElement: childElt];
      [self addSubview: button];
      [mButtons addObject: button];
    }

    nsCOMPtr<nsIDOMNode> temp = child;
    temp->GetNextSibling(getter_AddRefs(child));
  }

  [self reflowButtons];
}

-(void)addButton: (nsIDOMElement*)aElt atIndex: (int)aIndex
{
  BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
  [button setElement: aElt];
  [self addSubview: button];
  [mButtons insertObject: button atIndex: aIndex];
  [self reflowButtonsStartingAtIndex: aIndex];
}

-(void)removeButton: (nsIDOMElement*)aElt
{
  int count = [mButtons count];
  for (int i = 0; i < count; i++) {
    BookmarksButton* button = [mButtons objectAtIndex: i];
    if ([button element] == aElt) {
      [mButtons removeObjectAtIndex: i];
      [button removeFromSuperview];
      if (count > i)
        [self reflowButtonsStartingAtIndex: i];
      break;
    }
  }
}

-(void)reflowButtons
{
  [self reflowButtonsStartingAtIndex: 0];
}

-(void)reflowButtonsStartingAtIndex: (int)aIndex
{
  int count = [mButtons count];
  int prevX = 2;
  if (aIndex > 0) {
    BookmarksButton* prevButton = [mButtons objectAtIndex: (aIndex-1)];
    prevX += [prevButton frame].origin.x + [prevButton frame].size.width;
  }
  for (int i = aIndex; i < count; i++) {
    BookmarksButton* button = [mButtons objectAtIndex: i];
    [button sizeToFit];
    int width = [button frame].size.width;
    int height = [button frame].size.height;
    if (width > 150)
      width = 150;
    if (height < 16)
      height = 16; // Our folder tiff is only 15 pixels for some reason.
    [button setFrame: NSMakeRect(prevX, [button frame].origin.y, width, height)];

    prevX += [button frame].size.width + 2;
  }
}
@end

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