File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.22: download - view: text, annotated - select for diffs - revision graph
Thu Mar 7 18:35:48 2002 UTC (17 years, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Adding the click listener that will handle modifier clicks on links.

    1: /*
    2:  *  MyBrowserView.mm
    3:  */
    4: 
    5: #import "MyBrowserView.h"
    6: #import "BrowserWindowController.h"
    7: 
    8: #include "nsCOMPtr.h"
    9: #include "nsIServiceManager.h"
   10: #include "nsIIOService.h"
   11: #include "ContentClickListener.h"
   12: #include "nsIDOMWindow.h"
   13: #include "nsIChromeEventHandler.h"
   14: #include "nsPIDOMWindow.h"
   15: #include "nsIDOMEventReceiver.h"
   16: 
   17: #define DOCUMENT_DONE_STRING @"Document: Done"
   18: #define LOADING_STRING @"Loading..."
   19: 
   20: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   21: 
   22: @implementation MyBrowserView
   23: 
   24: -(void)windowClosed
   25: {
   26:     // Break the cycle.
   27:     [browserView setContainer: nil];
   28:     [browserView removeListener: self];
   29:     [browserView destroyWebBrowser];
   30: }
   31: 
   32: -(void)dealloc
   33: {
   34:     printf("My browser view died.\n");
   35:     
   36:     [[NSNotificationCenter defaultCenter] removeObserver: self];
   37:     
   38: 	[defaultStatus release];
   39:     [loadingStatus release];
   40:     
   41: 	[super dealloc];
   42: }
   43: 
   44: - (IBAction)load:(id)sender
   45: {
   46:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
   47: }
   48: 
   49: -(void)disconnectView
   50: {
   51:     urlbar = nil;
   52:     status = nil;
   53:     progress = nil;
   54:     progressSuper = nil;
   55:     mIsPrimary = NO;
   56:     [[NSNotificationCenter defaultCenter] removeObserver:self];
   57:     
   58:     [browserView setActive: NO];
   59: }
   60: 
   61: - (id)initWithFrame:(NSRect)frameRect
   62: {
   63:     [super initWithFrame: frameRect];
   64:      browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
   65:     [self addSubview:browserView];
   66:     [browserView setContainer:self];
   67:     [browserView addListener:self];
   68:     mIsBusy = NO;
   69:     mListenersAttached = NO;
   70:     return self;
   71: }
   72: 
   73: -(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
   74:     progress: (id)aProgress windowController: aWindowController
   75: {
   76:   mTab = tab;
   77:   urlbar = aUrlbar;
   78:   status = aStatus;
   79:   progress = aProgress;
   80:   progressSuper = [aProgress superview];
   81:   mWindowController = aWindowController;
   82:   
   83:   if (!mIsBusy)
   84:       [progress removeFromSuperview];
   85:   
   86:   defaultStatus = NULL;
   87:   loadingStatus = DOCUMENT_DONE_STRING;
   88:   [status setStringValue:loadingStatus];
   89:   
   90:   mIsPrimary = YES;
   91:  
   92:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   93:   if (!ioService)
   94:     return;
   95:   PRBool offline = PR_FALSE;
   96:   ioService->GetOffline(&offline);
   97:   mOffline = offline;
   98:   
   99:   if (mWindowController) // Only register if we're the content area.
  100:     [[NSNotificationCenter defaultCenter] addObserver:self
  101:         selector:@selector(offlineModeChanged:)
  102:         name:@"offlineModeChanged"
  103:         object:nil];
  104:         
  105:   // Update the URL bar.
  106:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  107:   
  108:   if (mWindowController && !mListenersAttached) {
  109:     mListenersAttached = YES;
  110:     
  111:     // We need to hook up our click and context menu listeners.
  112:     ContentClickListener* clickListener = new ContentClickListener(mWindowController);
  113:     if (!clickListener)
  114:       return;
  115:     
  116:     nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[self getBrowserView] getContentWindow]);
  117:     nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(contentWindow));
  118:     nsCOMPtr<nsIChromeEventHandler> chromeHandler;
  119:     piWindow->GetChromeEventHandler(getter_AddRefs(chromeHandler));
  120:     nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(chromeHandler));
  121:     rec->AddEventListenerByIID(clickListener, NS_GET_IID(nsIDOMMouseListener));
  122:   }
  123: }
  124: 
  125: -(NSString*)getCurrentURLSpec
  126: {
  127:   return [browserView getCurrentURLSpec];
  128: }
  129: 
  130: - (void)awakeFromNib 
  131: {
  132: }
  133: 
  134: - (void)setFrame:(NSRect)frameRect
  135: {
  136:   [super setFrame:frameRect];
  137:   NSRect bounds = [self bounds];
  138:   [browserView setFrame:bounds];
  139: }
  140: 
  141: -(BOOL)isBusy
  142: {
  143:   return mIsBusy;
  144: }
  145: 
  146: - (void)onLoadingStarted 
  147: {
  148:   if (defaultStatus) {
  149:     [defaultStatus release];
  150:     defaultStatus = NULL;
  151:   }
  152: 
  153:   [progressSuper addSubview:progress];
  154:   [progress setIndeterminate:YES];
  155:   [progress startAnimation:self];
  156: 
  157:   loadingStatus = LOADING_STRING;
  158:   [status setStringValue:loadingStatus];
  159: 
  160:   mIsBusy = YES;
  161:   [mTab setLabel: LOADING_STRING];
  162:   
  163:   if (mWindowController)
  164:     [mWindowController updateToolbarItems];
  165: }
  166: 
  167: - (void)onLoadingCompleted:(BOOL)succeeded
  168: {
  169:   [progress setIndeterminate:YES];
  170:   [progress stopAnimation:self];
  171:   [progress removeFromSuperview];
  172: 
  173:   loadingStatus = DOCUMENT_DONE_STRING;
  174:   if (defaultStatus) {
  175:     [status setStringValue:defaultStatus];
  176:   }
  177:   else {
  178:     [status setStringValue:loadingStatus];
  179:   }
  180: 
  181:   mIsBusy = NO;
  182:   
  183:   if (mWindowController)
  184:     [mWindowController updateToolbarItems];
  185: }
  186: 
  187: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  188: {
  189:   if (maxBytes > 0) {
  190:     BOOL isIndeterminate = [progress isIndeterminate];
  191:     if (isIndeterminate) {
  192:       [progress setIndeterminate:NO];
  193:     }
  194:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  195:     [progress setDoubleValue:val];
  196:   }
  197: }
  198: 
  199: - (void)onLocationChange:(NSURL*)url 
  200: {
  201:   NSString* spec = [url absoluteString];
  202:   [mWindowController updateLocationFields:spec];
  203: }
  204: 
  205: - (void)onStatusChange:(NSString*)aStatusString
  206: {
  207:   [status setStringValue: aStatusString];
  208: }
  209: 
  210: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  211: {
  212:   if (type == NSStatusTypeScriptDefault) {
  213:     if (defaultStatus) {
  214:       [defaultStatus release];
  215:     }
  216:     defaultStatus = statusString;
  217:     if (defaultStatus) {
  218:       [defaultStatus retain];
  219:     }
  220:   }
  221:   else if (!statusString) {
  222:     if (defaultStatus) {
  223:       [status setStringValue:defaultStatus];
  224:     }
  225:     else {
  226:       [status setStringValue:loadingStatus];
  227:     }      
  228:   }
  229:   else {
  230:     [status setStringValue:statusString];
  231:   }
  232: }
  233: 
  234: - (NSString *)title 
  235: {
  236:   return [[mWindowController window] title];
  237: }
  238: 
  239: - (void)setTitle:(NSString *)title
  240: {
  241:     if (!mWindowController)
  242:         return; // Sidebar panels can't affect the window title.
  243:     
  244:     // We must be the primary content area.
  245:     if (mIsPrimary) {
  246:         if (mOffline) {
  247:             NSString* newTitle;
  248:             if (title && ![title isEqualToString:@""])
  249:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  250:             else
  251:                 newTitle = @"Untitled [Working Offline]";
  252:             [[mWindowController window] setTitle: newTitle];
  253:         }
  254:         else {
  255:             if (title && ![title isEqualToString:@""])
  256:                 [[mWindowController window] setTitle:title];
  257:             else
  258:                 [[mWindowController window] setTitle:@"Untitled"];
  259:         }
  260:     }
  261:     
  262:     // Always set the tab.
  263:     if (title && ![title isEqualToString:@""])
  264:         [mTab setLabel:title];
  265:     else
  266:         [mTab setLabel:@"Untitled"];
  267: }
  268: 
  269: - (void)offlineModeChanged: (NSNotification*)aNotification
  270: {
  271:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  272:     if (!ioService)
  273:         return;
  274:     PRBool offline = PR_FALSE;
  275:     ioService->GetOffline(&offline);
  276:     mOffline = offline;
  277:     
  278:     if (mOffline) {
  279:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  280:         [[mWindowController window] setTitle: newTitle];
  281:     }
  282:     else {
  283:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  284:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  285:     }
  286: }
  287: 
  288: - (void)sizeBrowserTo:(NSSize)dimensions
  289: {
  290:   NSRect bounds = [self bounds];
  291:   float dx = dimensions.width - bounds.size.width;
  292:   float dy = dimensions.height - bounds.size.height;
  293: 
  294:   NSRect frame = [[self window] frame];
  295:   frame.size.width += dx;
  296:   frame.size.height += dy;
  297: 
  298:   [[self window] setFrame:frame display:YES];
  299: }
  300: 
  301: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  302: {
  303:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  304:   [controller enterModalSession];
  305:   return [[controller getMyBrowserView] getBrowserView];
  306: }
  307: 
  308: - (NSBrowserView*)getBrowserView
  309: {
  310:   return browserView;
  311: }
  312: 
  313: @end

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