]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/taskbar.cpp
who knows, maybe this will fix the 10.3 problems
[wxWidgets.git] / src / mac / carbon / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: taskbar.cpp
3 // Purpose: wxTaskBarIcon OSX Implementation
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 09/25/2004
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/defs.h"
15
16 #ifdef wxHAS_TASK_BAR_ICON
17
18 #include "wx/mac/private.h"
19
20 #include "wx/taskbar.h"
21 #include "wx/menu.h"
22 #include "wx/icon.h"
23 #include "wx/dcmemory.h"
24
25 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
26
27 pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef,
28 EventRef inEvent, void* pData)
29 {
30 wxTaskBarIcon*& pTB = (wxTaskBarIcon*&) pData;
31
32 const UInt32 eventClass = GetEventClass(inEvent);
33 const UInt32 eventKind = GetEventKind(inEvent);
34
35 if (eventClass == kEventClassCommand && eventKind == kEventCommandProcess)
36 {
37 //TODO: This is a complete copy of
38 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
39
40 if (! pTB->GetCurrentMenu() )
41 {
42 return eventNotHandledErr;
43 }
44
45 MenuRef hMenu = MAC_WXHMENU(pTB->GetCurrentMenu()->GetHMenu());
46 OSStatus result = eventNotHandledErr ;
47
48 HICommand command ;
49 OSErr err;
50
51 err = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand,
52 NULL, sizeof(HICommand), NULL, &command);
53 wxASSERT(err == noErr);
54
55 MenuItemIndex menuItemIndex;
56 err = GetIndMenuItemWithCommandID(hMenu, command.commandID, 1, NULL, &menuItemIndex);
57 wxASSERT(err == noErr);
58
59
60 MenuCommand id = command.commandID ;
61 wxMenuItem* item = NULL;
62
63 // for items we don't really control
64 if ( id == kHICommandPreferences )
65 {
66 id = wxApp::s_macPreferencesMenuItemId ;
67
68 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
69
70 if ( mbar )
71 {
72 wxMenu* menu = NULL ;
73 item = mbar->FindItem( id , &menu ) ;
74 }
75 }
76 else if (id != 0)
77 GetMenuItemRefCon( hMenu , menuItemIndex , (UInt32*) &item ) ;
78
79 if ( item )
80 {
81 if (item->IsCheckable())
82 {
83 item->Check( !item->IsChecked() ) ;
84 }
85
86 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
87 result = noErr ;
88 }
89
90 return result ;
91 }
92
93 wxASSERT(eventClass == kEventClassApplication && eventKind == kEventAppGetDockTileMenu);
94
95 //process the right click events
96 wxTaskBarIconEvent downevt(wxEVT_TASKBAR_RIGHT_DOWN,NULL);
97 pTB->ProcessEvent(downevt);
98
99 wxTaskBarIconEvent upevt(wxEVT_TASKBAR_RIGHT_UP,NULL);
100 pTB->ProcessEvent(upevt);
101
102 //create popup menu
103 wxMenu* menu = pTB->DoCreatePopupMenu();
104
105 OSStatus err = noErr;
106
107 if(menu)
108 {
109 //note to self - a MenuRef IS A MenuHandle
110 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
111
112 //When we call SetEventParameter it will decrement
113 //the reference count of the menu - we need to make
114 //sure it stays around in the wxMenu class here
115 RetainMenu(hMenu);
116
117 //set the actual dock menu
118 err = SetEventParameter((EventRef) inEvent, kEventParamMenuRef,
119 typeMenuRef, sizeof(MenuRef),
120 &hMenu);
121 wxASSERT(err == 0);
122
123 return err;
124 }
125 else
126 return eventNotHandledErr;
127 }
128
129 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler );
130
131 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType& nType)
132 : m_nType(nType), m_pEventHandlerRef(NULL), m_pMenu(NULL),
133 m_theLastMenu((WXHMENU)GetApplicationDockTileMenu()), m_iconAdded(false)
134 {
135 //Register the events that will return the dock menu
136 EventTypeSpec tbEventList[] = { { kEventClassCommand, kEventProcessCommand },
137 { kEventClassApplication, kEventAppGetDockTileMenu } };
138
139 #ifdef __WXDEBUG__
140 OSStatus err =
141 #endif
142 InstallApplicationEventHandler(
143 GetwxDockEventHandlerUPP(),
144 GetEventTypeCount(tbEventList), tbEventList,
145 this, (&(EventHandlerRef&)m_pEventHandlerRef));
146
147 wxASSERT(err == noErr);
148 }
149
150 wxTaskBarIcon::~wxTaskBarIcon()
151 {
152 //clean up event handler
153 RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
154
155 //restore old icon and menu to the dock
156 RemoveIcon();
157 }
158
159 wxMenu* wxTaskBarIcon::GetCurrentMenu()
160 {
161 return m_pMenu;
162 }
163
164 wxMenu* wxTaskBarIcon::DoCreatePopupMenu()
165 {
166 wxMenu* theNewMenu = CreatePopupMenu();
167
168 if (theNewMenu)
169 {
170 delete m_pMenu;
171 m_pMenu = theNewMenu;
172 m_pMenu->SetEventHandler(this);
173 }
174
175 return m_pMenu;
176 }
177
178 // Operations:
179 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
180 {
181 wxMask* mask = icon.GetMask();
182 if (!mask)
183 {
184 // Make a mask with no transparent pixels
185 wxBitmap bmp(icon.GetWidth(), icon.GetHeight());
186 wxMemoryDC dc;
187 dc.SelectObject(bmp);
188 dc.SetBackground(*wxBLACK_BRUSH);
189 dc.Clear();
190 dc.SelectObject(wxNullBitmap);
191 mask = new wxMask(bmp, *wxWHITE);
192 }
193
194 CGImageRef pImage;
195
196 //create the icon from the bitmap and mask bitmap contained within
197 OSStatus err = CreateCGImageFromPixMaps(
198 GetGWorldPixMap(MAC_WXHBITMAP(icon.GetHBITMAP())),
199 GetGWorldPixMap(MAC_WXHBITMAP(mask->GetMaskBitmap())),
200 &pImage
201 );
202
203 wxASSERT(err == 0);
204
205 err = SetApplicationDockTileImage(pImage);
206
207 wxASSERT(err == 0);
208
209 if (pImage != NULL)
210 CGImageRelease(pImage);
211
212 if (!icon.GetMask())
213 delete mask;
214
215 return m_iconAdded = err == noErr;
216 }
217
218 bool wxTaskBarIcon::RemoveIcon()
219 {
220 if(m_pMenu)
221 {
222 delete m_pMenu;
223 m_pMenu = NULL;
224 }
225
226 //restore old icon to the dock
227 OSStatus err = RestoreApplicationDockTileImage();
228 wxASSERT(err == 0);
229
230 //restore the old menu to the dock
231 SetApplicationDockTileMenu(MAC_WXHMENU(m_theLastMenu));
232
233 return !(m_iconAdded = !(err == noErr));
234 }
235
236 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
237 {
238 wxASSERT(menu != NULL);
239
240 if (m_pMenu)
241 {
242 delete m_pMenu;
243 m_pMenu = NULL;
244 }
245
246 //
247 // NB: Here we have to perform a deep copy of the menu,
248 // copying each and every menu item from menu to m_pMenu.
249 // Other implementations use wxWindow::PopupMenu here,
250 // which idle execution until the user selects something,
251 // but since the mac handles this internally, we can't -
252 // and have no way at all to idle it while the dock menu
253 // is being shown before menu goes out of scope (it may
254 // not be on the heap, and may expire right after this function
255 // is done - we need it to last until the carbon event is triggered -
256 // that's when the user right clicks).
257 //
258 // Also, since there is no equal (assignment) operator
259 // on either wxMenu or wxMenuItem, we have to do all the
260 // dirty work ourselves.
261 //
262
263 //Perform a deep copy of the menu
264 wxMenuItemList& theList = menu->GetMenuItems();
265 wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
266
267 //create the main menu
268 m_pMenu = new wxMenu(menu->GetTitle());
269
270 while(theNode != NULL)
271 {
272 wxMenuItem* theItem = theNode->GetData();
273 m_pMenu->Append(new wxMenuItem( m_pMenu, //parent menu
274 theItem->GetId(), //id
275 theItem->GetText(), //text label
276 theItem->GetHelp(), //status bar help string
277 theItem->GetKind(), //menu flags - checkable, seperator, etc.
278 theItem->GetSubMenu() //submenu
279 ));
280 theNode = theNode->GetNext();
281 }
282
283 m_pMenu->SetEventHandler(this);
284 return true;
285 }
286
287 #endif //wxHAS_TASK_BAR_ICON