]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusicon-quartz.c
tests: Add simple test for image clipboard
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * GCC on Mac OS X handles inlined objective C in C-files.
21  *
22  * Authors:
23  *  Mikael Hallendal <micke@imendio.com>
24  */
25
26 #import <Cocoa/Cocoa.h>
27 #include <quartz/gdkquartz.h>
28
29 #define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
30 #define QUARTZ_POOL_RELEASE [pool release]
31
32 @interface GtkQuartzStatusIcon : NSObject 
33 {
34   GtkStatusIcon *status_icon;
35   NSStatusBar   *ns_bar;
36   NSStatusItem  *ns_item;
37   NSImage       *current_image;
38   NSString      *ns_tooltip;
39 }
40 - (id) initWithStatusIcon:(GtkStatusIcon *)status_icon;
41 - (void) ensureItem;
42 - (void) actionCb:(NSObject *)button;
43 - (void) setImage:(GdkPixbuf *)pixbuf;
44 - (void) setVisible:(gboolean)visible;
45 - (void) setToolTip:(const gchar *)tooltip_text;
46 - (float) getWidth;
47 - (float) getHeight;
48 @end
49
50 @implementation GtkQuartzStatusIcon : NSObject
51 - (id) initWithStatusIcon:(GtkStatusIcon *)icon
52 {
53   [super init];
54   status_icon = icon;
55   ns_bar = [NSStatusBar systemStatusBar];
56
57   return self;
58 }
59
60 - (void) ensureItem
61 {
62   if (ns_item != nil)
63     return;
64
65   ns_item = [ns_bar statusItemWithLength:NSVariableStatusItemLength];
66   [ns_item setAction:@selector(actionCb:)];
67   [ns_item setTarget:self];
68   [ns_item retain];
69 }
70
71 - (void) dealloc
72 {
73   g_print ("Deallocating GtkQuartzStatusIcon\n");
74   [current_image release];
75   [ns_item release];
76   [ns_bar release];
77
78   [super dealloc];
79 }
80
81 - (void) actionCb:(NSObject *)button
82
83   NSEvent *event = [NSApp currentEvent];
84   double time = [event timestamp];
85   
86   g_signal_emit (status_icon,
87                  status_icon_signals [POPUP_MENU_SIGNAL], 0,
88                  1,
89                  time * 1000.0);
90 }
91
92 - (void) setImage:(GdkPixbuf *)pixbuf
93 {
94   /* Support NULL */
95   [self ensureItem];
96
97   if (current_image != nil) {
98     [current_image release];
99     current_image = nil;
100   }
101   
102   if (!pixbuf) {
103     [ns_item release];
104     ns_item = nil;
105     return;
106   }
107
108   current_image = gdk_quartz_pixbuf_to_ns_image_libgtk_only (pixbuf);
109   [current_image retain];
110
111   [ns_item setImage:current_image];
112 }
113
114 - (void) setVisible:(gboolean)visible
115 {
116   if (visible) {
117     [self ensureItem];
118     if (ns_item != nil)
119       [ns_item setImage:current_image];
120     if (ns_tooltip != nil)
121       [ns_item setToolTip:ns_tooltip];
122   } else {
123     [ns_item release];
124     ns_item = nil;
125   }
126 }
127
128 - (void) setToolTip:(const gchar *)tooltip_text
129 {
130   [ns_tooltip release];
131   ns_tooltip = [[NSString stringWithUTF8String:tooltip_text] retain];
132   
133   [ns_item setToolTip:ns_tooltip];
134 }
135
136 - (float) getWidth
137 {
138   return [ns_bar thickness];
139 }
140
141 - (float) getHeight
142 {
143   return [ns_bar thickness];
144 }
145 @end
146
147
148