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 (18 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: //  BookmarksToolbar.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: #import "BookmarksToolbar.h"
   11: #import "BookmarksService.h"
   12: #include "nsIDOMElement.h"
   13: 
   14: @implementation BookmarksToolbar
   15: 
   16: - (id)initWithFrame:(NSRect)frame {
   17:   self = [super initWithFrame:frame];
   18:   if (self) {
   19:     mBookmarks = nsnull;
   20:     mButtons = [[NSMutableArray alloc] init];
   21:   }
   22:   return self;
   23: }
   24: 
   25: -(void)initializeToolbar
   26: {
   27:   // Initialization code here.
   28:   mBookmarks = new BookmarksService(self);
   29:   mBookmarks->AddObserver();
   30:   mBookmarks->EnsureToolbarRoot();
   31:   [self buildButtonList];
   32: }
   33: 
   34: -(void) dealloc
   35: {
   36:   [mButtons autorelease];
   37:   mBookmarks->RemoveObserver();
   38:   delete mBookmarks;
   39:   [super dealloc];
   40: }
   41: 
   42: - (void)drawRect:(NSRect)aRect {
   43:   // Fill the background with our background color.
   44:  // [[NSColor colorWithCalibratedWhite: 0.98 alpha: 1.0] set];
   45:  // NSRectFill(aRect);
   46: 
   47:   //printf("The rect is: %f %f %f %f\n", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
   48:   
   49:   if (aRect.origin.y + aRect.size.height ==
   50:       [self bounds].size.height) {
   51:     // The personal toolbar is 21 pixels tall.  The bottom two pixels
   52:     // are a separator.
   53:     [[NSColor colorWithCalibratedWhite: 0.90 alpha: 1.0] set];
   54:     NSRectFill(NSMakeRect(aRect.origin.x, aRect.origin.y, aRect.size.width, 2));
   55:   }
   56: 
   57:   // The buttons will paint themselves.
   58: }
   59: 
   60: -(void)buildButtonList
   61: {
   62:   // Build the buttons, and then lay them all out.
   63:   nsCOMPtr<nsIDOMNode> child;
   64:   BookmarksService::gToolbarRoot->GetFirstChild(getter_AddRefs(child));
   65:   while (child) {
   66:     nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
   67:     if (childElt) {
   68:       BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
   69:       [button setElement: childElt];
   70:       [self addSubview: button];
   71:       [mButtons addObject: button];
   72:     }
   73: 
   74:     nsCOMPtr<nsIDOMNode> temp = child;
   75:     temp->GetNextSibling(getter_AddRefs(child));
   76:   }
   77: 
   78:   [self reflowButtons];
   79: }
   80: 
   81: -(void)addButton: (nsIDOMElement*)aElt atIndex: (int)aIndex
   82: {
   83:   BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
   84:   [button setElement: aElt];
   85:   [self addSubview: button];
   86:   [mButtons insertObject: button atIndex: aIndex];
   87:   [self reflowButtonsStartingAtIndex: aIndex];
   88: }
   89: 
   90: -(void)removeButton: (nsIDOMElement*)aElt
   91: {
   92:   int count = [mButtons count];
   93:   for (int i = 0; i < count; i++) {
   94:     BookmarksButton* button = [mButtons objectAtIndex: i];
   95:     if ([button element] == aElt) {
   96:       [mButtons removeObjectAtIndex: i];
   97:       [button removeFromSuperview];
   98:       if (count > i)
   99:         [self reflowButtonsStartingAtIndex: i];
  100:       break;
  101:     }
  102:   }
  103: }
  104: 
  105: -(void)reflowButtons
  106: {
  107:   [self reflowButtonsStartingAtIndex: 0];
  108: }
  109: 
  110: -(void)reflowButtonsStartingAtIndex: (int)aIndex
  111: {
  112:   int count = [mButtons count];
  113:   int prevX = 2;
  114:   if (aIndex > 0) {
  115:     BookmarksButton* prevButton = [mButtons objectAtIndex: (aIndex-1)];
  116:     prevX += [prevButton frame].origin.x + [prevButton frame].size.width;
  117:   }
  118:   for (int i = aIndex; i < count; i++) {
  119:     BookmarksButton* button = [mButtons objectAtIndex: i];
  120:     [button sizeToFit];
  121:     int width = [button frame].size.width;
  122:     int height = [button frame].size.height;
  123:     if (width > 150)
  124:       width = 150;
  125:     if (height < 16)
  126:       height = 16; // Our folder tiff is only 15 pixels for some reason.
  127:     [button setFrame: NSMakeRect(prevX, [button frame].origin.y, width, height)];
  128: 
  129:     prevX += [button frame].size.width + 2;
  130:   }
  131: }
  132: @end

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