Annotation of chimera/BookmarksButton.mm, revision 1.9

1.8       macserv     1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3: * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4: *
                      5: * The contents of this file are subject to the Netscape Public License
                      6: * Version 1.1 (the "License"); you may not use this file except in
                      7: * compliance with the License. You may obtain a copy of the License at
                      8: * http://www.mozilla.org/NPL/
                      9: *
                     10: * Software distributed under the License is distributed on an "AS IS" basis,
                     11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12: * for the specific language governing rights and limitations under the
                     13: * License.
                     14: *
                     15: * The Original Code is mozilla.org code.
                     16: *
                     17: * The Initial Developer of the Original Code is
                     18: * Netscape Communications Corporation.
                     19: * Portions created by the Initial Developer are Copyright (C) 2002
                     20: * the Initial Developer. All Rights Reserved.
                     21: *
                     22: * Contributor(s):
                     23: *
                     24: * Alternatively, the contents of this file may be used under the terms of
                     25: * either the GNU General Public License Version 2 or later (the "GPL"), or
                     26: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27: * in which case the provisions of the GPL or the LGPL are applicable instead
                     28: * of those above. If you wish to allow use of your version of this file only
                     29: * under the terms of either the GPL or the LGPL, and not to allow others to
                     30: * use your version of this file under the terms of the NPL, indicate your
                     31: * decision by deleting the provisions above and replace them with the notice
                     32: * and other provisions required by the GPL or the LGPL. If you do not delete
                     33: * the provisions above, a recipient may use your version of this file under
                     34: * the terms of any one of the NPL, the GPL or the LGPL.
                     35: *
                     36: * ***** END LICENSE BLOCK ***** */
1.1       hyatt      37: 
                     38: #import "BookmarksButton.h"
1.2       hyatt      39: #include "nsIDOMElement.h"
1.6       hyatt      40: #include "nsIContent.h"
1.2       hyatt      41: #include "nsString.h"
1.6       hyatt      42: #import "BookmarksService.h"
1.1       hyatt      43: 
                     44: @implementation BookmarksButton
                     45: 
                     46: - (id)initWithFrame:(NSRect)frame {
1.8       macserv    47:     if ( (self = [super initWithFrame:frame]) ) {
                     48:         mElement = nsnull;
                     49:         [self setBezelStyle: NSRegularSquareBezelStyle];
                     50:         [self setBordered: NO];
                     51:         [self setImagePosition: NSImageLeft];
                     52:         [self setRefusesFirstResponder: YES];
                     53:         [self setFont: [NSFont labelFontOfSize: 11.0]];
                     54:     }
1.2       hyatt      55:   return self;
                     56: }
1.1       hyatt      57: 
1.3       hyatt      58: -(IBAction)openBookmark:(id)aSender
                     59: {
1.9     ! hyatt      60:   // See if we're a group.
        !            61:   nsAutoString group;
        !            62:   mElement->GetAttribute(NS_LITERAL_STRING("group"), group);
        !            63:   if (!group.IsEmpty()) {
        !            64:     BookmarksService::OpenBookmarkGroup([[[self window] windowController] getTabBrowser], mElement);
        !            65:     return;
        !            66:   }
1.3       hyatt      67:   
                     68:   // Get the href attribute.  This is the URL we want to load.
                     69:   nsAutoString href;
                     70:   mElement->GetAttribute(NS_LITERAL_STRING("href"), href);
                     71:   nsCAutoString cref; cref.AssignWithConversion(href);
                     72:   if (cref.IsEmpty())
                     73:     return;
                     74: 
                     75:   NSString* url = [NSString stringWithCString: cref.get()];
                     76: 
                     77:   // Now load the URL in the window.
                     78:   [[[self window] windowController] loadURL:[NSURL URLWithString: url]];
                     79: 
                     80:   // Focus and activate our content area.
                     81:   [[[[[self window] windowController] getMyBrowserView] getBrowserView] setActive: YES];
                     82: }
                     83: 
1.5       hyatt      84: -(void)drawRect:(NSRect)aRect
1.4       hyatt      85: {
1.5       hyatt      86:   [super drawRect: aRect];
1.4       hyatt      87: }
                     88: 
1.7       hyatt      89: -(NSMenu*)menuForEvent:(NSEvent*)aEvent
                     90: {
                     91:   return [[self superview] menu];
                     92: }
                     93: 
1.6       hyatt      94: -(void)mouseDown:(NSEvent*)aEvent
                     95: {
                     96:   if (mIsFolder) {
                     97:     nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
                     98:     NSMenu* menu = BookmarksService::LocateMenu(content);
                     99:     [NSMenu popUpContextMenu: menu withEvent: aEvent forView: self];
                    100:   }
                    101:   else
                    102:     [super mouseDown: aEvent];
                    103: }
                    104: 
1.2       hyatt     105: -(void)setElement: (nsIDOMElement*)aElt
                    106: {
                    107:   mElement = aElt;
                    108:   nsAutoString tag;
                    109:   mElement->GetLocalName(tag);
1.9     ! hyatt     110: 
        !           111:   nsAutoString group;
        !           112:   mElement->GetAttribute(NS_LITERAL_STRING("group"), group);
        !           113: 
        !           114:   if (!group.IsEmpty()) {
        !           115:     mIsFolder = NO;
        !           116:     [self setImage: [NSImage imageNamed: @"smallbookmark"]];
        !           117:     [self setAction: @selector(openBookmark:)];
        !           118:     [self setTarget: self];
        !           119:   }
        !           120:   else if (tag.Equals(NS_LITERAL_STRING("folder"))) {
1.2       hyatt     121:     [self setImage: [NSImage imageNamed: @"folder"]];
1.6       hyatt     122:     mIsFolder = YES;
                    123:   }
1.3       hyatt     124:   else {
1.6       hyatt     125:     mIsFolder = NO;
1.2       hyatt     126:     [self setImage: [NSImage imageNamed: @"smallbookmark"]];
1.3       hyatt     127:     [self setAction: @selector(openBookmark:)];
                    128:     [self setTarget: self];
                    129:   }
                    130:   
1.2       hyatt     131:   nsAutoString name;
                    132:   mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
                    133:   [self setTitle: [NSString stringWithCharacters: name.get() length: nsCRT::strlen(name.get())]];
1.6       hyatt     134: }
                    135: 
                    136: -(nsIDOMElement*)element
                    137: {
                    138:   return mElement;
1.1       hyatt     139: }
                    140: 
                    141: @end

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