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