Home | History | Annotate | Download | only in status_icons
      1 // Copyright (c) 2011 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/status_icons/status_icon_mac.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/sys_string_conversions.h"
      9 #include "skia/ext/skia_utils_mac.h"
     10 #include "third_party/skia/include/core/SkBitmap.h"
     11 
     12 @interface StatusItemController : NSObject {
     13   StatusIconMac* statusIcon_; // weak
     14 }
     15 - initWithIcon:(StatusIconMac*)icon;
     16 - (void)handleClick:(id)sender;
     17 
     18 @end // @interface StatusItemController
     19 
     20 @implementation StatusItemController
     21 
     22 - (id)initWithIcon:(StatusIconMac*)icon {
     23   statusIcon_ = icon;
     24   return self;
     25 }
     26 
     27 - (void)handleClick:(id)sender {
     28   // Pass along the click notification to our owner.
     29   DCHECK(statusIcon_);
     30   statusIcon_->DispatchClickEvent();
     31 }
     32 
     33 @end
     34 
     35 StatusIconMac::StatusIconMac()
     36     : item_(NULL) {
     37   controller_.reset([[StatusItemController alloc] initWithIcon:this]);
     38 }
     39 
     40 StatusIconMac::~StatusIconMac() {
     41   // Remove the status item from the status bar.
     42   if (item_)
     43     [[NSStatusBar systemStatusBar] removeStatusItem:item_];
     44 }
     45 
     46 NSStatusItem* StatusIconMac::item() {
     47   if (!item_.get()) {
     48     // Create a new status item.
     49     item_.reset([[[NSStatusBar systemStatusBar]
     50                   statusItemWithLength:NSSquareStatusItemLength] retain]);
     51     [item_ setEnabled:YES];
     52     [item_ setTarget:controller_];
     53     [item_ setAction:@selector(handleClick:)];
     54     [item_ setHighlightMode:YES];
     55   }
     56   return item_.get();
     57 }
     58 
     59 void StatusIconMac::SetImage(const SkBitmap& bitmap) {
     60   if (!bitmap.isNull()) {
     61     NSImage* image = gfx::SkBitmapToNSImage(bitmap);
     62     if (image)
     63       [item() setImage:image];
     64   }
     65 }
     66 
     67 void StatusIconMac::SetPressedImage(const SkBitmap& bitmap) {
     68   if (!bitmap.isNull()) {
     69     NSImage* image = gfx::SkBitmapToNSImage(bitmap);
     70     if (image)
     71       [item() setAlternateImage:image];
     72   }
     73 }
     74 
     75 void StatusIconMac::SetToolTip(const string16& tool_tip) {
     76   [item() setToolTip:base::SysUTF16ToNSString(tool_tip)];
     77 }
     78 
     79 void StatusIconMac::DisplayBalloon(const string16& title,
     80                                    const string16& contents) {
     81   // TODO(atwilson): Figure out the right UI to display here when actually
     82   // needed (not yet called).
     83   // http://crbug.com/74970
     84 }
     85 
     86 void StatusIconMac::UpdatePlatformContextMenu(ui::MenuModel* menu) {
     87   // TODO(atwilson): Add support for context menus for Mac when actually needed
     88   // (not yet used by anything) - http://crbug.com/37375.
     89 }
     90