]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusicon-quartz.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkstatusicon-quartz.c
1 /* gtkstatusicon-quartz.c:
2  *
3  * Copyright (C) 2006 Imendio AB
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * GCC on Mac OS X handles inlined objective C in C-files.
19  *
20  * Authors:
21  *  Mikael Hallendal <micke@imendio.com>
22  */
23
24 #import <Cocoa/Cocoa.h>
25 #include <quartz/gdkquartz.h>
26
27 #define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
28 #define QUARTZ_POOL_RELEASE [pool release]
29
30 @interface GtkQuartzStatusIcon : NSObject 
31 {
32   GtkStatusIcon *status_icon;
33   NSStatusBar   *ns_bar;
34   NSStatusItem  *ns_item;
35   NSImage       *current_image;
36   NSString      *ns_tooltip;
37 }
38 - (id) initWithStatusIcon:(GtkStatusIcon *)status_icon;
39 - (void) ensureItem;
40 - (void) actionCb:(NSObject *)button;
41 - (void) setImage:(GdkPixbuf *)pixbuf;
42 - (void) setVisible:(gboolean)visible;
43 - (void) setToolTip:(const gchar *)tooltip_text;
44 - (float) getWidth;
45 - (float) getHeight;
46 @end
47
48 @implementation GtkQuartzStatusIcon : NSObject
49 - (id) initWithStatusIcon:(GtkStatusIcon *)icon
50 {
51   [super init];
52   status_icon = icon;
53   ns_bar = [NSStatusBar systemStatusBar];
54
55   return self;
56 }
57
58 - (void) ensureItem
59 {
60   if (ns_item != nil)
61     return;
62
63   ns_item = [ns_bar statusItemWithLength:NSVariableStatusItemLength];
64   [ns_item setAction:@selector(actionCb:)];
65   [ns_item setTarget:self];
66   [ns_item retain];
67 }
68
69 - (void) dealloc
70 {
71   g_print ("Deallocating GtkQuartzStatusIcon\n");
72   [current_image release];
73   [ns_item release];
74   [ns_bar release];
75
76   [super dealloc];
77 }
78
79 - (void) actionCb:(NSObject *)button
80
81   NSEvent *event = [NSApp currentEvent];
82   double time = [event timestamp];
83   
84   g_signal_emit (status_icon,
85                  status_icon_signals [POPUP_MENU_SIGNAL], 0,
86                  1,
87                  time * 1000.0);
88 }
89
90 - (void) setImage:(GdkPixbuf *)pixbuf
91 {
92   /* Support NULL */
93   [self ensureItem];
94
95   if (current_image != nil) {
96     [current_image release];
97     current_image = nil;
98   }
99   
100   if (!pixbuf) {
101     [ns_item release];
102     ns_item = nil;
103     return;
104   }
105
106   current_image = gdk_quartz_pixbuf_to_ns_image_libgtk_only (pixbuf);
107   [current_image retain];
108
109   [ns_item setImage:current_image];
110 }
111
112 - (void) setVisible:(gboolean)visible
113 {
114   if (visible) {
115     [self ensureItem];
116     if (ns_item != nil)
117       [ns_item setImage:current_image];
118     if (ns_tooltip != nil)
119       [ns_item setToolTip:ns_tooltip];
120   } else {
121     [ns_item release];
122     ns_item = nil;
123   }
124 }
125
126 - (void) setToolTip:(const gchar *)tooltip_text
127 {
128   [ns_tooltip release];
129   ns_tooltip = [[NSString stringWithUTF8String:tooltip_text] retain];
130   
131   [ns_item setToolTip:ns_tooltip];
132 }
133
134 - (float) getWidth
135 {
136   return [ns_bar thickness];
137 }
138
139 - (float) getHeight
140 {
141   return [ns_bar thickness];
142 }
143 @end
144
145
146