Annotation of chimera/BookmarksToolbar.mm, revision 1.7

1.1       hyatt       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: 
1.2       hyatt       9: #import "BookmarksButton.h"
1.1       hyatt      10: #import "BookmarksToolbar.h"
1.3       hyatt      11: #import "BookmarksService.h"
1.4       hyatt      12: #include "nsIDOMElement.h"
1.1       hyatt      13: 
                     14: @implementation BookmarksToolbar
                     15: 
                     16: - (id)initWithFrame:(NSRect)frame {
                     17:   self = [super initWithFrame:frame];
                     18:   if (self) {
1.3       hyatt      19:     mBookmarks = nsnull;
1.5       hyatt      20:     mButtons = [[NSMutableArray alloc] init];
1.1       hyatt      21:   }
                     22:   return self;
1.3       hyatt      23: }
                     24: 
                     25: -(void)initializeToolbar
                     26: {
                     27:   // Initialization code here.
                     28:   mBookmarks = new BookmarksService(self);
                     29:   mBookmarks->AddObserver();
1.4       hyatt      30:   mBookmarks->EnsureToolbarRoot();
                     31:   [self buildButtonList];
1.3       hyatt      32: }
                     33: 
                     34: -(void) dealloc
                     35: {
1.5       hyatt      36:   [mButtons autorelease];
1.3       hyatt      37:   mBookmarks->RemoveObserver();
                     38:   delete mBookmarks;
                     39:   [super dealloc];
1.1       hyatt      40: }
                     41: 
                     42: - (void)drawRect:(NSRect)aRect {
                     43:   // Fill the background with our background color.
1.6       hyatt      44:  // [[NSColor colorWithCalibratedWhite: 0.98 alpha: 1.0] set];
                     45:  // NSRectFill(aRect);
1.1       hyatt      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.
1.5       hyatt      53:     [[NSColor colorWithCalibratedWhite: 0.90 alpha: 1.0] set];
1.1       hyatt      54:     NSRectFill(NSMakeRect(aRect.origin.x, aRect.origin.y, aRect.size.width, 2));
                     55:   }
                     56: 
1.2       hyatt      57:   // The buttons will paint themselves.
1.1       hyatt      58: }
                     59: 
1.4       hyatt      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];
1.5       hyatt      71:       [mButtons addObject: button];
1.4       hyatt      72:     }
                     73: 
                     74:     nsCOMPtr<nsIDOMNode> temp = child;
                     75:     temp->GetNextSibling(getter_AddRefs(child));
                     76:   }
                     77: 
                     78:   [self reflowButtons];
                     79: }
                     80: 
1.7     ! hyatt      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: 
1.4       hyatt     105: -(void)reflowButtons
                    106: {
1.7     ! hyatt     107:   [self reflowButtonsStartingAtIndex: 0];
        !           108: }
        !           109: 
        !           110: -(void)reflowButtonsStartingAtIndex: (int)aIndex
        !           111: {
1.5       hyatt     112:   int count = [mButtons count];
                    113:   int prevX = 2;
1.7     ! hyatt     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++) {
1.5       hyatt     119:     BookmarksButton* button = [mButtons objectAtIndex: i];
                    120:     [button sizeToFit];
                    121:     int width = [button frame].size.width;
1.7     ! hyatt     122:     int height = [button frame].size.height;
1.5       hyatt     123:     if (width > 150)
                    124:       width = 150;
1.7     ! hyatt     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: 
1.5       hyatt     129:     prevX += [button frame].size.width + 2;
                    130:   }
1.4       hyatt     131: }
1.1       hyatt     132: @end

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