Annotation of chimera/BookmarksToolbar.mm, revision 1.8

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.8     ! 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.8     ! hyatt      57:   // The buttons will paint themselves. Just call our base class method.
        !            58:   [super drawRect: aRect];
1.1       hyatt      59: }
                     60: 
1.4       hyatt      61: -(void)buildButtonList
                     62: {
                     63:   // Build the buttons, and then lay them all out.
                     64:   nsCOMPtr<nsIDOMNode> child;
                     65:   BookmarksService::gToolbarRoot->GetFirstChild(getter_AddRefs(child));
                     66:   while (child) {
                     67:     nsCOMPtr<nsIDOMElement> childElt(do_QueryInterface(child));
                     68:     if (childElt) {
                     69:       BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
                     70:       [button setElement: childElt];
                     71:       [self addSubview: button];
1.5       hyatt      72:       [mButtons addObject: button];
1.4       hyatt      73:     }
                     74: 
                     75:     nsCOMPtr<nsIDOMNode> temp = child;
                     76:     temp->GetNextSibling(getter_AddRefs(child));
                     77:   }
                     78: 
                     79:   [self reflowButtons];
                     80: }
                     81: 
1.7       hyatt      82: -(void)addButton: (nsIDOMElement*)aElt atIndex: (int)aIndex
                     83: {
                     84:   BookmarksButton* button = [[[BookmarksButton alloc] initWithFrame: NSMakeRect(2, 1, 100, 17)] autorelease];
                     85:   [button setElement: aElt];
                     86:   [self addSubview: button];
                     87:   [mButtons insertObject: button atIndex: aIndex];
                     88:   [self reflowButtonsStartingAtIndex: aIndex];
                     89: }
                     90: 
                     91: -(void)removeButton: (nsIDOMElement*)aElt
                     92: {
                     93:   int count = [mButtons count];
                     94:   for (int i = 0; i < count; i++) {
                     95:     BookmarksButton* button = [mButtons objectAtIndex: i];
                     96:     if ([button element] == aElt) {
                     97:       [mButtons removeObjectAtIndex: i];
                     98:       [button removeFromSuperview];
                     99:       if (count > i)
                    100:         [self reflowButtonsStartingAtIndex: i];
                    101:       break;
                    102:     }
                    103:   }
1.8     ! hyatt     104: 
        !           105:   [self setNeedsDisplay: YES];
1.7       hyatt     106: }
                    107: 
1.4       hyatt     108: -(void)reflowButtons
                    109: {
1.7       hyatt     110:   [self reflowButtonsStartingAtIndex: 0];
                    111: }
                    112: 
                    113: -(void)reflowButtonsStartingAtIndex: (int)aIndex
                    114: {
1.5       hyatt     115:   int count = [mButtons count];
                    116:   int prevX = 2;
1.7       hyatt     117:   if (aIndex > 0) {
                    118:     BookmarksButton* prevButton = [mButtons objectAtIndex: (aIndex-1)];
                    119:     prevX += [prevButton frame].origin.x + [prevButton frame].size.width;
                    120:   }
                    121:   for (int i = aIndex; i < count; i++) {
1.5       hyatt     122:     BookmarksButton* button = [mButtons objectAtIndex: i];
                    123:     [button sizeToFit];
                    124:     int width = [button frame].size.width;
1.7       hyatt     125:     int height = [button frame].size.height;
1.5       hyatt     126:     if (width > 150)
                    127:       width = 150;
1.7       hyatt     128:     if (height < 16)
                    129:       height = 16; // Our folder tiff is only 15 pixels for some reason.
                    130:     [button setFrame: NSMakeRect(prevX, [button frame].origin.y, width, height)];
                    131: 
1.5       hyatt     132:     prevX += [button frame].size.width + 2;
1.8     ! hyatt     133: 
        !           134:     [button setNeedsDisplay: YES];
1.5       hyatt     135:   }
1.4       hyatt     136: }
1.1       hyatt     137: @end

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