cocoa_status_item.m 8.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * MACDRV Cocoa status item class
 *
 * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#import <Cocoa/Cocoa.h>
#include "macdrv_cocoa.h"
#import "cocoa_app.h"
#import "cocoa_event.h"


27
@interface WineStatusItem : NSView
28 29 30
{
    NSStatusItem* item;
    WineEventQueue* queue;
31 32
    NSTrackingArea* trackingArea;
    NSImage* image;
33 34 35 36
}

@property (retain, nonatomic) NSStatusItem* item;
@property (assign, nonatomic) WineEventQueue* queue;
37
@property (retain, nonatomic) NSImage* image;
38 39 40 41 42 43

@end


@implementation WineStatusItem

44
@synthesize item, queue, image;
45 46 47

    - (id) initWithEventQueue:(WineEventQueue*)inQueue
    {
48 49 50 51
        NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
        CGFloat thickness = [statusBar thickness];

        self = [super initWithFrame:NSMakeRect(0, 0, thickness, thickness)];
52 53 54
        if (self)
        {
            item = [[statusBar statusItemWithLength:NSSquareStatusItemLength] retain];
55 56
            // This is a retain cycle which is broken in -removeFromStatusBar.
            [item setView:self];
57 58

            queue = inQueue;
59 60 61 62 63 64

            trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                        options:NSTrackingMouseMoved | NSTrackingActiveAlways | NSTrackingInVisibleRect
                                                          owner:self
                                                       userInfo:nil];
            [self addTrackingArea:trackingArea];
65 66 67 68 69 70 71 72 73 74 75 76
        }
        return self;
    }

    - (void) dealloc
    {
        if (item)
        {
            NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
            [statusBar removeStatusItem:item];
            [item release];
        }
77 78
        [image release];
        [trackingArea release];
79 80 81
        [super dealloc];
    }

82 83 84 85 86 87 88 89 90 91
    - (void) setImage:(NSImage*)inImage
    {
        if (image != inImage)
        {
            [image release];
            image = [inImage retain];
            [self setNeedsDisplay:YES];
        }
    }

92 93 94 95 96 97
    - (void) removeFromStatusBar
    {
        if (item)
        {
            NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
            [statusBar removeStatusItem:item];
98
            [item setView:nil];
99 100 101 102 103 104

            [queue discardEventsPassingTest:^BOOL (macdrv_event* event){
                return ((event->type == STATUS_ITEM_MOUSE_BUTTON && event->status_item_mouse_button.item == (macdrv_status_item)self) ||
                        (event->type == STATUS_ITEM_MOUSE_MOVE && event->status_item_mouse_move.item == (macdrv_status_item)self));
            }];

105 106 107 108
            self.item = nil;
        }
    }

109
    - (void) postMouseButtonEvent:(NSEvent*)nsevent;
110
    {
111
        macdrv_event* event;
112
        NSUInteger typeMask = NSEventMaskFromType([nsevent type]);
113 114 115
        CGPoint point = CGEventGetLocation([nsevent CGEvent]);

        point = cgpoint_win_from_mac(point);
116 117 118 119 120 121

        event = macdrv_create_event(STATUS_ITEM_MOUSE_BUTTON, nil);
        event->status_item_mouse_button.item = (macdrv_status_item)self;
        event->status_item_mouse_button.button = [nsevent buttonNumber];
        event->status_item_mouse_button.down = (typeMask & (NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)) != 0;
        event->status_item_mouse_button.count = [nsevent clickCount];
122 123
        event->status_item_mouse_button.x = floor(point.x);
        event->status_item_mouse_button.y = floor(point.y);
124 125
        [queue postEvent:event];
        macdrv_release_event(event);
126 127
    }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    /*
     * ---------- NSView methods ----------
     */
    - (void) drawRect:(NSRect)rect
    {
        [item drawStatusBarBackgroundInRect:[self bounds] withHighlight:NO];

        if (image)
        {
            NSSize imageSize = [image size];
            NSRect bounds = [self bounds];
            NSPoint imageOrigin = NSMakePoint(NSMidX(bounds) - imageSize.width / 2,
                                              NSMidY(bounds) - imageSize.height / 2);

            imageOrigin = [self convertPointToBase:imageOrigin];
            imageOrigin.x = floor(imageOrigin.x);
            imageOrigin.y = floor(imageOrigin.y);
            imageOrigin = [self convertPointFromBase:imageOrigin];

            [image drawAtPoint:imageOrigin
                      fromRect:NSZeroRect
                     operation:NSCompositeSourceOver
                      fraction:1];
        }
    }


    /*
     * ---------- NSResponder methods ----------
     */
    - (void) mouseDown:(NSEvent*)event
    {
        [self postMouseButtonEvent:event];
    }

    - (void) mouseDragged:(NSEvent*)event
    {
        [self mouseMoved:event];
    }

    - (void) mouseMoved:(NSEvent*)nsevent
    {
        macdrv_event* event;
172 173 174 175
        CGPoint point = CGEventGetLocation([nsevent CGEvent]);

        point = cgpoint_win_from_mac(point);

176 177
        event = macdrv_create_event(STATUS_ITEM_MOUSE_MOVE, nil);
        event->status_item_mouse_move.item = (macdrv_status_item)self;
178 179
        event->status_item_mouse_move.x = floor(point.x);
        event->status_item_mouse_move.y = floor(point.y);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        [queue postEvent:event];
        macdrv_release_event(event);
    }

    - (void) mouseUp:(NSEvent*)event
    {
        [self postMouseButtonEvent:event];
    }

    - (void) otherMouseDown:(NSEvent*)event
    {
        [self postMouseButtonEvent:event];
    }

    - (void) otherMouseDragged:(NSEvent*)event
    {
        [self mouseMoved:event];
    }

    - (void) otherMouseUp:(NSEvent*)event
    {
        [self postMouseButtonEvent:event];
    }

    - (void) rightMouseDown:(NSEvent*)event
    {
        [self postMouseButtonEvent:event];
    }

    - (void) rightMouseDragged:(NSEvent*)event
210
    {
211
        [self mouseMoved:event];
212 213
    }

214
    - (void) rightMouseUp:(NSEvent*)event
215
    {
216
        [self postMouseButtonEvent:event];
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    }

@end


/***********************************************************************
 *              macdrv_create_status_item
 *
 * Creates a new status item in the status bar.
 */
macdrv_status_item macdrv_create_status_item(macdrv_event_queue q)
{
    WineEventQueue* queue = (WineEventQueue*)q;
    __block WineStatusItem* statusItem;

    OnMainThread(^{
        statusItem = [[WineStatusItem alloc] initWithEventQueue:queue];
    });

    return (macdrv_status_item)statusItem;
}

/***********************************************************************
 *              macdrv_destroy_status_item
 *
 * Removes a status item previously returned by
 * macdrv_create_status_item() from the status bar and destroys it.
 */
void macdrv_destroy_status_item(macdrv_status_item s)
{
    WineStatusItem* statusItem = (WineStatusItem*)s;

    OnMainThreadAsync(^{
        [statusItem removeFromStatusBar];
        [statusItem release];
    });
}

/***********************************************************************
 *              macdrv_set_status_item_image
 *
 * Sets the image for a status item.  If cgimage is NULL, clears the
 * image of the status item (leaving it a blank spot on the menu bar).
 */
void macdrv_set_status_item_image(macdrv_status_item s, CGImageRef cgimage)
{
    WineStatusItem* statusItem = (WineStatusItem*)s;

    CGImageRetain(cgimage);

    OnMainThreadAsync(^{
        NSImage* image = nil;
        if (cgimage)
        {
271 272 273 274
            NSSize size;
            CGFloat maxSize = [[NSStatusBar systemStatusBar] thickness];
            BOOL changed = FALSE;

275 276
            image = [[NSImage alloc] initWithCGImage:cgimage size:NSZeroSize];
            CGImageRelease(cgimage);
277 278 279 280 281 282 283 284 285
            size = [image size];
            while (size.width > maxSize || size.height > maxSize)
            {
                size.width /= 2.0;
                size.height /= 2.0;
                changed = TRUE;
            }
            if (changed)
                [image setSize:size];
286
        }
287
        statusItem.image = image;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        [image release];
    });
}

/***********************************************************************
 *              macdrv_set_status_item_tooltip
 *
 * Sets the tooltip string for a status item.  If cftip is NULL, clears
 * the tooltip string for the status item.
 */
void macdrv_set_status_item_tooltip(macdrv_status_item s, CFStringRef cftip)
{
    WineStatusItem* statusItem = (WineStatusItem*)s;
    NSString* tip = (NSString*)cftip;

    if (![tip length]) tip = nil;
    OnMainThreadAsync(^{
305
        [statusItem setToolTip:tip];
306 307
    });
}