File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.19: download - view: text, annotated - select for diffs - revision graph
Thu Feb 7 07:18:26 2002 UTC (17 years, 10 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Make the sidebar unload its web page when the drawer is closed.

    1: /*
    2:  *  BrowserWindowController.mm
    3:  */
    4: 
    5: #import "BrowserWindowController.h"
    6: #import "MyBrowserView.h"
    7: 
    8: static NSString *BrowserToolbarIdentifier	= @"Browser Window Toolbar";
    9: static NSString *BackToolbarItemIdentifier	= @"Back Toolbar Item";
   10: static NSString *ForwardToolbarItemIdentifier	= @"Forward Toolbar Item";
   11: static NSString *ReloadToolbarItemIdentifier	= @"Reload Toolbar Item";
   12: static NSString *StopToolbarItemIdentifier	= @"Stop Toolbar Item";
   13: static NSString *HomeToolbarItemIdentifier	= @"Home Toolbar Item";
   14: static NSString *LocationToolbarItemIdentifier	= @"Location Toolbar Item";
   15: static NSString *SidebarToolbarItemIdentifier	= @"Sidebar Toolbar Item";
   16: static NSString *PrintToolbarItemIdentifier	= @"Print Toolbar Item";
   17: 
   18: @interface BrowserWindowController(Private)
   19: - (void)setupToolbar;
   20: @end
   21: 
   22: @implementation BrowserWindowController
   23: 
   24: - (id)init 
   25: {
   26:     mInitialized = NO;
   27:     return [super init];
   28: }
   29: 
   30: - (void)windowWillClose:(NSNotification *)notification
   31: {
   32:     [self autorelease];
   33: }
   34: 
   35: - (void)dealloc
   36: {
   37:     [mBrowserView windowClosed];
   38:     [super dealloc];
   39: }
   40: 
   41: - (void)windowDidLoad
   42: {
   43:     mInitialized = YES;
   44:     [self setupToolbar];
   45:     
   46:     if (mURL) {
   47:       [self loadURL: mURL];
   48:       [mURL release];
   49:     }
   50:     
   51:     [mSidebarDrawer setDelegate: self];
   52: }
   53: 
   54: - (void)drawerDidOpen:(NSNotification *)aNotification
   55: {
   56:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
   57: }
   58: 
   59: - (void)drawerDidClose:(NSNotification *)aNotification
   60: {
   61:     // Unload the Gecko web page in "My Panels" to save memory.
   62:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
   63: }
   64: 
   65: - (void)setupToolbar
   66: {
   67:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
   68:     
   69:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
   70:     [toolbar setAllowsUserCustomization:YES];
   71:     [toolbar setAutosavesConfiguration:YES];
   72:     [toolbar setDelegate:self];
   73:     [[self window] setToolbar:toolbar];
   74: }
   75: 
   76: 
   77: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
   78: {
   79:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
   80:                                         ForwardToolbarItemIdentifier,
   81:                                         ReloadToolbarItemIdentifier,
   82:                                         StopToolbarItemIdentifier,
   83:                                         HomeToolbarItemIdentifier,
   84:                                         LocationToolbarItemIdentifier,
   85:                                         SidebarToolbarItemIdentifier,
   86:                                         PrintToolbarItemIdentifier,
   87:                                         NSToolbarCustomizeToolbarItemIdentifier,
   88:                                         NSToolbarFlexibleSpaceItemIdentifier,
   89:                                         NSToolbarSpaceItemIdentifier,
   90:                                         NSToolbarSeparatorItemIdentifier,
   91:                                         nil];
   92: }
   93: 
   94: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
   95: {
   96:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
   97:                                         ForwardToolbarItemIdentifier,
   98:                                         ReloadToolbarItemIdentifier,
   99:                                         StopToolbarItemIdentifier,
  100:                                         HomeToolbarItemIdentifier,
  101:                                         LocationToolbarItemIdentifier,
  102:                                         SidebarToolbarItemIdentifier,
  103:                                         nil];
  104: }
  105: 
  106: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  107:       itemForItemIdentifier:(NSString *)itemIdent
  108:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  109: {
  110:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  111:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  112:         [toolbarItem setLabel:@"Back"];
  113:         [toolbarItem setPaletteLabel:@"Go Back"];
  114:         [toolbarItem setToolTip:@"Go back one page"];
  115:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  116:         [toolbarItem setTarget:self];
  117:         [toolbarItem setAction:@selector(back:)];
  118:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  119:         [toolbarItem setLabel:@"Forward"];
  120:         [toolbarItem setPaletteLabel:@"Go Forward"];
  121:         [toolbarItem setToolTip:@"Go forward one page"];
  122:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  123:         [toolbarItem setTarget:self];
  124:         [toolbarItem setAction:@selector(forward:)];
  125:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  126:         [toolbarItem setLabel:@"Reload"];
  127:         [toolbarItem setPaletteLabel:@"Reload Page"];
  128:         [toolbarItem setToolTip:@"Reload current page"];
  129:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  130:         [toolbarItem setTarget:self];
  131:         [toolbarItem setAction:@selector(reload:)];
  132:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  133:         [toolbarItem setLabel:@"Stop"];
  134:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  135:         [toolbarItem setToolTip:@"Stop loading this page"];
  136:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  137:         [toolbarItem setTarget:self];
  138:         [toolbarItem setAction:@selector(stop:)];
  139:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  140:         [toolbarItem setLabel:@"Home"];
  141:         [toolbarItem setPaletteLabel:@"Go Home"];
  142:         [toolbarItem setToolTip:@"Go to home page"];
  143:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  144:         [toolbarItem setTarget:self];
  145:         [toolbarItem setAction:@selector(home:)];
  146:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  147:         [toolbarItem setLabel:@"Sidebar"];
  148:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  149:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  150:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  151:         [toolbarItem setTarget:self];
  152:         [toolbarItem setAction:@selector(toggleSidebar:)];
  153:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  154:         
  155:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  156:         
  157:         [toolbarItem setLabel:@"Location"];
  158:         [toolbarItem setPaletteLabel:@"Location"];
  159:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  160:         [toolbarItem setView:mLocationToolbarView];
  161:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  162:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  163:         
  164:         [menuFormRep setTarget:self];
  165:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  166:         [menuFormRep setTitle:[toolbarItem label]];
  167:         
  168:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  169:         mLocationToolbarItem = toolbarItem;
  170: 
  171:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  172:         [toolbarItem setLabel:@"Print"];
  173:         [toolbarItem setPaletteLabel:@"Print"];
  174:         [toolbarItem setToolTip:@"Print this page"];
  175:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  176:         [toolbarItem setTarget:self];
  177:         [toolbarItem setAction:@selector(printDocument)];
  178:     } else {
  179:         toolbarItem = nil;
  180:     }
  181:         
  182:     return toolbarItem;
  183: }
  184: 
  185: // This method handles the enabling/disabling of the toolbar buttons.
  186: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  187: {
  188:   // Check the action and see if it matches.
  189:   SEL action = [theItem action];
  190:   if (action == @selector(back:))
  191:     return [[mBrowserView getBrowserView] canGoBack];
  192:   else if (action == @selector(forward:))
  193:     return [[mBrowserView getBrowserView] canGoForward];
  194:   else if (action == @selector(reload:))
  195:     return [mBrowserView isBusy] == NO;
  196:   else if (action == @selector(stop:))
  197:     return [mBrowserView isBusy];
  198:   else
  199:     return YES;
  200: }
  201:    
  202: - (void)updateToolbarItems
  203: {
  204:   [[[self window] toolbar] validateVisibleItems];
  205: }
  206: 
  207: - (void)performAppropriateLocationAction
  208: {
  209:     if ( [[[self window] toolbar] isVisible] ) {
  210:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  211:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  212:             [self focusURLBar];
  213:         } else {
  214:             [self beginLocationSheet];
  215:         }
  216:     } else {
  217:         [self beginLocationSheet];
  218:     }
  219: }
  220: 
  221: - (void)focusURLBar
  222: {
  223:     [mURLBar selectText: self];
  224: }
  225: 
  226: - (void)beginLocationSheet
  227: {
  228:     [NSApp beginSheet:	mLocationSheetWindow
  229:        modalForWindow:	[self window]
  230:         modalDelegate:	nil
  231:        didEndSelector:	nil
  232:           contextInfo:	nil];
  233: }
  234: 
  235: - (IBAction)endLocationSheet:(id)sender
  236: {
  237:     [mLocationSheetWindow orderOut:self];
  238:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  239:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  240: }
  241: 
  242: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  243: {
  244:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  245: }
  246: 
  247: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  248: {
  249:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  250: }
  251: 
  252: - (void)printDocument
  253: {
  254:     [[mBrowserView getBrowserView] printDocument];
  255: }
  256: 
  257: - (void)printPreview
  258: {
  259:     [[mBrowserView getBrowserView] printPreview];
  260: }
  261: 
  262: - (void)findInPage
  263: {
  264:     [[mBrowserView getBrowserView] findInPage];
  265: }
  266: 
  267: - (void)findAgain
  268: {
  269:     [[mBrowserView getBrowserView] findAgain];
  270: }
  271: 
  272: - (IBAction)back:(id)aSender
  273: {
  274:   [[mBrowserView getBrowserView] goBack];
  275: }
  276: 
  277: - (IBAction)forward:(id)aSender
  278: {
  279:   [[mBrowserView getBrowserView] goForward];
  280: }
  281: 
  282: - (IBAction)reload:(id)aSender
  283: {
  284:   [[mBrowserView getBrowserView] reload: 0];
  285: }
  286: 
  287: - (IBAction)stop:(id)aSender
  288: {
  289:   [[mBrowserView getBrowserView] stop: 0];
  290: }
  291: 
  292: - (IBAction)home:(id)aSender
  293: {
  294:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  295: }
  296: 
  297: - (IBAction)toggleSidebar:(id)aSender
  298: {
  299:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) {
  300:         [mSidebarDrawer open];
  301:     } else {
  302:         [mSidebarDrawer close];
  303:     }
  304: }
  305: 
  306: -(void)loadURL:(NSURL*)aURL
  307: {
  308:     if (mInitialized)
  309:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  310:     else {
  311:         mURL = aURL;
  312:         [mURL retain];
  313:     }
  314: }
  315: 
  316: - (void)updateLocationFields:(NSString *)locationString
  317: {
  318: /* //commenting this out because it doesn't work right yet.
  319:     if ( [locationString length] > 30 ) {
  320:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  321:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  322:     } else {
  323:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  324:             [NSString stringWithFormat:@"Location: %@", locationString]];
  325:     }
  326: */
  327: 
  328:     [mURLBar setStringValue:locationString];
  329:     [mLocationSheetURLField setStringValue:locationString];
  330: 
  331:     [[self window] update];
  332:     [[self window] display];
  333: }
  334: @end
  335: 

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