Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/cocoa/base_view.h"
      6 
      7 NSString* kViewDidBecomeFirstResponder =
      8     @"Chromium.kViewDidBecomeFirstResponder";
      9 NSString* kSelectionDirection = @"Chromium.kSelectionDirection";
     10 
     11 @implementation BaseView
     12 
     13 - (id)initWithFrame:(NSRect)frame {
     14   self = [super initWithFrame:frame];
     15   if (self) {
     16     trackingArea_ =
     17         [[NSTrackingArea alloc] initWithRect:frame
     18                                      options:NSTrackingMouseMoved |
     19                                              NSTrackingMouseEnteredAndExited |
     20                                              NSTrackingActiveInActiveApp |
     21                                              NSTrackingInVisibleRect
     22                                        owner:self
     23                                     userInfo:nil];
     24     [self addTrackingArea:trackingArea_];
     25   }
     26   return self;
     27 }
     28 
     29 - (void)dealloc {
     30   [self removeTrackingArea:trackingArea_];
     31   [trackingArea_ release];
     32 
     33   [super dealloc];
     34 }
     35 
     36 - (void)mouseEvent:(NSEvent *)theEvent {
     37   // This method left intentionally blank.
     38 }
     39 
     40 - (void)keyEvent:(NSEvent *)theEvent {
     41   // This method left intentionally blank.
     42 }
     43 
     44 - (void)mouseDown:(NSEvent *)theEvent {
     45   dragging_ = YES;
     46   [self mouseEvent:theEvent];
     47 }
     48 
     49 - (void)rightMouseDown:(NSEvent *)theEvent {
     50   [self mouseEvent:theEvent];
     51 }
     52 
     53 - (void)otherMouseDown:(NSEvent *)theEvent {
     54   [self mouseEvent:theEvent];
     55 }
     56 
     57 - (void)mouseUp:(NSEvent *)theEvent {
     58   [self mouseEvent:theEvent];
     59 
     60   dragging_ = NO;
     61   if (pendingExitEvent_.get()) {
     62     NSEvent* exitEvent =
     63         [NSEvent enterExitEventWithType:NSMouseExited
     64                                location:[theEvent locationInWindow]
     65                           modifierFlags:[theEvent modifierFlags]
     66                               timestamp:[theEvent timestamp]
     67                            windowNumber:[theEvent windowNumber]
     68                                 context:[theEvent context]
     69                             eventNumber:[pendingExitEvent_.get() eventNumber]
     70                          trackingNumber:[pendingExitEvent_.get() trackingNumber]
     71                                userData:[pendingExitEvent_.get() userData]];
     72     [self mouseEvent:exitEvent];
     73     pendingExitEvent_.reset();
     74   }
     75 }
     76 
     77 - (void)rightMouseUp:(NSEvent *)theEvent {
     78   [self mouseEvent:theEvent];
     79 }
     80 
     81 - (void)otherMouseUp:(NSEvent *)theEvent {
     82   [self mouseEvent:theEvent];
     83 }
     84 
     85 - (void)mouseMoved:(NSEvent *)theEvent {
     86   [self mouseEvent:theEvent];
     87 }
     88 
     89 - (void)mouseDragged:(NSEvent *)theEvent {
     90   [self mouseEvent:theEvent];
     91 }
     92 
     93 - (void)rightMouseDragged:(NSEvent *)theEvent {
     94   [self mouseEvent:theEvent];
     95 }
     96 
     97 - (void)otherMouseDragged:(NSEvent *)theEvent {
     98   [self mouseEvent:theEvent];
     99 }
    100 
    101 - (void)mouseEntered:(NSEvent *)theEvent {
    102   if (pendingExitEvent_.get()) {
    103     pendingExitEvent_.reset();
    104     return;
    105   }
    106 
    107   [self mouseEvent:theEvent];
    108 }
    109 
    110 - (void)mouseExited:(NSEvent *)theEvent {
    111   // The tracking area will send an exit event even during a drag, which isn't
    112   // how the event flow for drags should work. This stores the exit event, and
    113   // sends it when the drag completes instead.
    114   if (dragging_) {
    115     pendingExitEvent_.reset([theEvent retain]);
    116     return;
    117   }
    118 
    119   [self mouseEvent:theEvent];
    120 }
    121 
    122 - (void)keyDown:(NSEvent *)theEvent {
    123   [self keyEvent:theEvent];
    124 }
    125 
    126 - (void)keyUp:(NSEvent *)theEvent {
    127   [self keyEvent:theEvent];
    128 }
    129 
    130 - (void)flagsChanged:(NSEvent *)theEvent {
    131   [self keyEvent:theEvent];
    132 }
    133 
    134 - (gfx::Rect)flipNSRectToRect:(NSRect)rect {
    135   gfx::Rect new_rect(NSRectToCGRect(rect));
    136   new_rect.set_y([self bounds].size.height - new_rect.y() - new_rect.height());
    137   return new_rect;
    138 }
    139 
    140 - (NSRect)flipRectToNSRect:(gfx::Rect)rect {
    141   NSRect new_rect(NSRectFromCGRect(rect.ToCGRect()));
    142   new_rect.origin.y =
    143       [self bounds].size.height - new_rect.origin.y - new_rect.size.height;
    144   return new_rect;
    145 }
    146 
    147 @end
    148