]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/taskbar.cpp
pragma and prec-header patch applied
[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 #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"
21 #include "wx/dcmemory.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
24
25 pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef,
26 EventRef inEvent, void* pData)
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 {
35 //TODO: This is a complete copy of
36 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
37
38 if (! pTB->GetCurrentMenu() )
39 {
40 return eventNotHandledErr;
41 }
42
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,
50 NULL, sizeof(HICommand), NULL, &command);
51 wxASSERT(err == noErr);
52
53 MenuItemIndex menuItemIndex;
54 err = GetIndMenuItemWithCommandID(hMenu, command.commandID, 1, NULL, &menuItemIndex);
55 wxASSERT(err == noErr);
56
57
58 MenuCommand id = command.commandID ;
59 wxMenuItem* item = NULL;
60
61 // for items we don't really control
62 if ( id == kHICommandPreferences )
63 {
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 }
73 }
74 else if (id != 0)
75 GetMenuItemRefCon( hMenu , menuItemIndex , (UInt32*) &item ) ;
76
77 if ( item )
78 {
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 ;
86 }
87
88 return result ;
89 }
90
91 wxASSERT(eventClass == kEventClassApplication && eventKind == kEventAppGetDockTileMenu);
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
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);
114
115 //set the actual dock menu
116 err = SetEventParameter((EventRef) inEvent, kEventParamMenuRef,
117 typeMenuRef, sizeof(MenuRef),
118 &hMenu);
119 wxASSERT(err == 0);
120
121 return err;
122 }
123 else
124 return eventNotHandledErr;
125 }
126
127 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler );
128
129 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType& nType)
130 : m_nType(nType), m_pEventHandlerRef(NULL), m_pMenu(NULL),
131 m_theLastMenu((WXHMENU)GetApplicationDockTileMenu()), m_iconAdded(false)
132 {
133 //Register the events that will return the dock menu
134 EventTypeSpec tbEventList[] = { { kEventClassCommand, kEventProcessCommand },
135 { kEventClassApplication, kEventAppGetDockTileMenu } };
136
137 #ifdef __WXDEBUG__
138 OSStatus err =
139 #endif
140 InstallApplicationEventHandler(
141 GetwxDockEventHandlerUPP(),
142 GetEventTypeCount(tbEventList), tbEventList,
143 this, (&(EventHandlerRef&)m_pEventHandlerRef));
144
145 wxASSERT(err == noErr);
146 }
147
148 wxTaskBarIcon::~wxTaskBarIcon()
149 {
150 //clean up event handler
151 RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
152
153 //restore old icon and menu to the dock
154 RemoveIcon();
155 }
156
157 wxMenu* wxTaskBarIcon::GetCurrentMenu()
158 {
159 return m_pMenu;
160 }
161
162 wxMenu* wxTaskBarIcon::DoCreatePopupMenu()
163 {
164 wxMenu* theNewMenu = CreatePopupMenu();
165
166 if (theNewMenu)
167 {
168 delete m_pMenu;
169 m_pMenu = theNewMenu;
170 m_pMenu->SetEventHandler(this);
171 }
172
173 return m_pMenu;
174 }
175
176 // Operations:
177 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
178 {
179 wxMask* mask = icon.GetMask();
180 if (!mask)
181 {
182 // Make a mask with no transparent pixels
183 wxBitmap bmp(icon.GetWidth(), icon.GetHeight());
184 wxMemoryDC dc;
185 dc.SelectObject(bmp);
186 dc.SetBackground(*wxBLACK_BRUSH);
187 dc.Clear();
188 dc.SelectObject(wxNullBitmap);
189 mask = new wxMask(bmp, *wxWHITE);
190 }
191
192 CGImageRef pImage;
193
194 //create the icon from the bitmap and mask bitmap contained within
195 OSStatus err = CreateCGImageFromPixMaps(
196 GetGWorldPixMap(MAC_WXHBITMAP(icon.GetHBITMAP())),
197 GetGWorldPixMap(MAC_WXHBITMAP(mask->GetMaskBitmap())),
198 &pImage
199 );
200
201 wxASSERT(err == 0);
202
203 err = SetApplicationDockTileImage(pImage);
204
205 wxASSERT(err == 0);
206
207 if (pImage != NULL)
208 CGImageRelease(pImage);
209
210 if (!icon.GetMask())
211 delete mask;
212
213 return m_iconAdded = err == noErr;
214 }
215
216 bool wxTaskBarIcon::RemoveIcon()
217 {
218 if(m_pMenu)
219 {
220 delete m_pMenu;
221 m_pMenu = NULL;
222 }
223
224 //restore old icon to the dock
225 OSStatus err = RestoreApplicationDockTileImage();
226 wxASSERT(err == 0);
227
228 //restore the old menu to the dock
229 SetApplicationDockTileMenu(MAC_WXHMENU(m_theLastMenu));
230
231 return !(m_iconAdded = !(err == noErr));
232 }
233
234 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
235 {
236 wxASSERT(menu != NULL);
237
238 if (m_pMenu)
239 {
240 delete m_pMenu;
241 m_pMenu = NULL;
242 }
243
244 //
245 // NB: Here we have to perform a deep copy of the menu,
246 // copying each and every menu item from menu to m_pMenu.
247 // Other implementations use wxWindow::PopupMenu here,
248 // which idle execution until the user selects something,
249 // but since the mac handles this internally, we can't -
250 // and have no way at all to idle it while the dock menu
251 // is being shown before menu goes out of scope (it may
252 // not be on the heap, and may expire right after this function
253 // is done - we need it to last until the carbon event is triggered -
254 // that's when the user right clicks).
255 //
256 // Also, since there is no equal (assignment) operator
257 // on either wxMenu or wxMenuItem, we have to do all the
258 // dirty work ourselves.
259 //
260
261 //Perform a deep copy of the menu
262 wxMenuItemList& theList = menu->GetMenuItems();
263 wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
264
265 //create the main menu
266 m_pMenu = new wxMenu(menu->GetTitle());
267
268 while(theNode != NULL)
269 {
270 wxMenuItem* theItem = theNode->GetData();
271 m_pMenu->Append(new wxMenuItem( m_pMenu, //parent menu
272 theItem->GetId(), //id
273 theItem->GetText(), //text label
274 theItem->GetHelp(), //status bar help string
275 theItem->GetKind(), //menu flags - checkable, seperator, etc.
276 theItem->GetSubMenu() //submenu
277 ));
278 theNode = theNode->GetNext();
279 }
280
281 m_pMenu->SetEventHandler(this);
282 return true;
283 }
284
285 #endif //wxHAS_TASK_BAR_ICON