Annotation of chimera/BookmarksToolbar.mm, revision 1.12

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.10      hyatt      54:     NSRectFill(NSMakeRect(aRect.origin.x, [self bounds].size.height-2, aRect.size.width, [self bounds].size.height));
1.1       hyatt      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.11      hyatt     115:   float oldHeight = [self frame].size.height;
                    116:   float computedHeight = 18;
1.5       hyatt     117:   int count = [mButtons count];
1.10      hyatt     118:   float currY = 1.0;
1.9       pinkerto  119:   float prevX = 2.0;
1.7       hyatt     120:   if (aIndex > 0) {
                    121:     BookmarksButton* prevButton = [mButtons objectAtIndex: (aIndex-1)];
                    122:     prevX += [prevButton frame].origin.x + [prevButton frame].size.width;
1.10      hyatt     123:     currY = [prevButton frame].origin.y;
1.7       hyatt     124:   }
                    125:   for (int i = aIndex; i < count; i++) {
1.5       hyatt     126:     BookmarksButton* button = [mButtons objectAtIndex: i];
                    127:     [button sizeToFit];
1.9       pinkerto  128:     float width = [button frame].size.width;
                    129:     float height = [button frame].size.height;
1.5       hyatt     130:     if (width > 150)
                    131:       width = 150;
1.7       hyatt     132:     if (height < 16)
                    133:       height = 16; // Our folder tiff is only 15 pixels for some reason.
1.10      hyatt     134:     [button setFrame: NSMakeRect(prevX, currY, width, height)];
1.7       hyatt     135: 
1.5       hyatt     136:     prevX += [button frame].size.width + 2;
1.8       hyatt     137: 
1.10      hyatt     138:     if ([self bounds].size.width < prevX) {
                    139:       // The previous button didn't fit.  We need to make a new row.
                    140:       currY += 18;
1.11      hyatt     141:       computedHeight += 18;
                    142:       if (computedHeight > oldHeight) {
                    143:         [self setFrame: NSMakeRect([self frame].origin.x, [self frame].origin.y+(oldHeight-computedHeight),
                    144:                                    [self frame].size.width, computedHeight)];
                    145:         [self setNeedsDisplay: YES];
                    146:       }
                    147:       
1.10      hyatt     148:       prevX = 2;
                    149:       [button setFrame: NSMakeRect(prevX, currY, width, height)];
1.11      hyatt     150:       prevX += [button frame].size.width + 2;
1.10      hyatt     151:     }
                    152:     
1.8       hyatt     153:     [button setNeedsDisplay: YES];
1.5       hyatt     154:   }
1.10      hyatt     155: 
1.11      hyatt     156:   float currentHeight = [self frame].size.height;
                    157:   if (computedHeight != currentHeight) {
                    158:     [self setFrame: NSMakeRect([self frame].origin.x, [self frame].origin.y + (currentHeight - computedHeight),
                    159:                                [self frame].size.width, computedHeight)];
                    160:     [self setNeedsDisplay: YES];
                    161:   }
                    162:   
                    163:   float sizeChange = computedHeight - oldHeight;
1.10      hyatt     164:   if (sizeChange != 0) {
                    165:     // We need to adjust the content area.
                    166:     NSView* view = [[[self window] windowController] getTabBrowser];
                    167:     [view setFrame: NSMakeRect([view frame].origin.x, [view frame].origin.y,
                    168:                                [view frame].size.width, [view frame].size.height - sizeChange)];
                    169:   }
                    170: }
                    171: 
                    172: -(BOOL)isFlipped
                    173: {
                    174:   return YES; // Use flipped coords, so we can layout out from top row to bottom row.
1.4       hyatt     175: }
1.10      hyatt     176: 
                    177: -(void)setFrame:(NSRect)aRect
                    178: {
                    179:   NSRect oldFrame = [self frame];
                    180:   [super setFrame:aRect];
                    181: 
1.11      hyatt     182:   if (oldFrame.size.width == aRect.size.width)
                    183:     return;
                    184: 
                    185:   int count = [mButtons count];
                    186:   if (count <= 2)
                    187:     return; // We have too few buttons to care.
1.10      hyatt     188:   
1.11      hyatt     189:   // Do some optimizations when we have only one row.
                    190:   if (aRect.size.height < 25) // We have only one row.
                    191:   {
                    192:     if (oldFrame.size.width < aRect.size.width)
                    193:       // We got bigger.  If we already only have one row, just bail.
                    194:       //       This will optimize for a common resizing case.
                    195:       return;
                    196:     else {
                    197:       // We got smaller.  Just go to the last button and see if it is outside
                    198:       // our bounds.
                    199:       BookmarksButton* button = [mButtons objectAtIndex:(count-1)];
                    200:       if ([button frame].origin.x + [button frame].size.width >
                    201:           [self bounds].size.width - 2) {
                    202:         // The button doesn't fit any more.  Reflow starting at this index.
                    203:         [self reflowButtonsStartingAtIndex:(count-1)];
                    204:       }
                    205:     }
                    206:   }
                    207:   else {
                    208:     // See if we got bigger or smaller.  We could gain or lose a row.
                    209:     [self reflowButtons];
                    210:   }
1.10      hyatt     211: }
                    212: 
1.1       hyatt     213: @end

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