]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/taskbar.cpp
fixed fatal bug when moving the control after itself in tab order
[wxWidgets.git] / src / mac / carbon / taskbar.cpp
CommitLineData
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
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"
af7e08a4 23#include "wx/dcmemory.h"
607667fd 24
607667fd
RN
25IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
26
e768548a
RD
27pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef,
28 EventRef inEvent, void* pData)
607667fd
RN
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 {
af7e08a4
RN
37 //TODO: This is a complete copy of
38 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
890976b8
RD
39
40 if (! pTB->GetCurrentMenu() )
41 {
42 return eventNotHandledErr;
43 }
44
af7e08a4
RN
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,
607667fd 52 NULL, sizeof(HICommand), NULL, &command);
af7e08a4 53 wxASSERT(err == noErr);
607667fd 54
af7e08a4
RN
55 MenuItemIndex menuItemIndex;
56 err = GetIndMenuItemWithCommandID(hMenu, command.commandID, 1, NULL, &menuItemIndex);
57 wxASSERT(err == noErr);
607667fd
RN
58
59
af7e08a4
RN
60 MenuCommand id = command.commandID ;
61 wxMenuItem* item = NULL;
607667fd 62
af7e08a4
RN
63 // for items we don't really control
64 if ( id == kHICommandPreferences )
607667fd 65 {
af7e08a4
RN
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 }
607667fd 75 }
af7e08a4
RN
76 else if (id != 0)
77 GetMenuItemRefCon( hMenu , menuItemIndex , (UInt32*) &item ) ;
607667fd 78
af7e08a4 79 if ( item )
e768548a 80 {
af7e08a4
RN
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 ;
e768548a 88 }
af7e08a4
RN
89
90 return result ;
607667fd
RN
91 }
92
93 wxASSERT(eventClass == kEventClassApplication && eventKind == kEventAppGetDockTileMenu);
af7e08a4
RN
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
a45beea1
RN
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);
607667fd 116
a45beea1
RN
117 //set the actual dock menu
118 err = SetEventParameter((EventRef) inEvent, kEventParamMenuRef,
e768548a
RD
119 typeMenuRef, sizeof(MenuRef),
120 &hMenu);
a45beea1 121 wxASSERT(err == 0);
a45beea1 122
af7e08a4
RN
123 return err;
124 }
125 else
126 return eventNotHandledErr;
607667fd
RN
127}
128
129DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler );
130
131wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType& nType)
af7e08a4
RN
132 : m_nType(nType), m_pEventHandlerRef(NULL), m_pMenu(NULL),
133 m_theLastMenu((WXHMENU)GetApplicationDockTileMenu()), m_iconAdded(false)
607667fd
RN
134{
135 //Register the events that will return the dock menu
e768548a 136 EventTypeSpec tbEventList[] = { { kEventClassCommand, kEventProcessCommand },
607667fd 137 { kEventClassApplication, kEventAppGetDockTileMenu } };
e768548a 138
cc3388ae 139#ifdef __WXDEBUG__
e768548a 140 OSStatus err =
cc3388ae 141#endif
e768548a 142 InstallApplicationEventHandler(
607667fd
RN
143 GetwxDockEventHandlerUPP(),
144 GetEventTypeCount(tbEventList), tbEventList,
e768548a
RD
145 this, (&(EventHandlerRef&)m_pEventHandlerRef));
146
147 wxASSERT(err == noErr);
607667fd 148}
cc3388ae 149
607667fd
RN
150wxTaskBarIcon::~wxTaskBarIcon()
151{
af7e08a4 152 //clean up event handler
cc3388ae 153 RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
af7e08a4
RN
154
155 //restore old icon and menu to the dock
156 RemoveIcon();
607667fd
RN
157}
158
a45beea1 159wxMenu* wxTaskBarIcon::GetCurrentMenu()
607667fd 160{
a45beea1 161 return m_pMenu;
607667fd
RN
162}
163
a45beea1 164wxMenu* wxTaskBarIcon::DoCreatePopupMenu()
af7e08a4
RN
165{
166 wxMenu* theNewMenu = CreatePopupMenu();
a45beea1 167
af7e08a4
RN
168 if (theNewMenu)
169 {
170 delete m_pMenu;
171 m_pMenu = theNewMenu;
a45beea1 172 m_pMenu->SetEventHandler(this);
af7e08a4 173 }
a45beea1 174
607667fd
RN
175 return m_pMenu;
176}
177
178// Operations:
179bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
180{
e768548a
RD
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;
607667fd 195
e768548a
RD
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);
cc3388ae 204
e768548a
RD
205 err = SetApplicationDockTileImage(pImage);
206
207 wxASSERT(err == 0);
739e35e4
RN
208
209 if (pImage != NULL)
210 CGImageRelease(pImage);
607667fd 211
e768548a
RD
212 if (!icon.GetMask())
213 delete mask;
214
215 return m_iconAdded = err == noErr;
607667fd 216}
e768548a 217
607667fd
RN
218bool wxTaskBarIcon::RemoveIcon()
219{
af7e08a4
RN
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));
607667fd 232
e768548a 233 return !(m_iconAdded = !(err == noErr));
607667fd 234}
e768548a 235
607667fd
RN
236bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
237{
af7e08a4
RN
238 wxASSERT(menu != NULL);
239
607667fd 240 if (m_pMenu)
af7e08a4 241 {
607667fd 242 delete m_pMenu;
af7e08a4
RN
243 m_pMenu = NULL;
244 }
a45beea1 245
af7e08a4
RN
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();
a45beea1 266
af7e08a4
RN
267 //create the main menu
268 m_pMenu = new wxMenu(menu->GetTitle());
a45beea1 269
af7e08a4
RN
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;
607667fd
RN
285}
286
f2641bc2 287#endif //wxHAS_TASK_BAR_ICON