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