1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon OSX Implementation
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #ifdef wxHAS_TASK_BAR_ICON
18 #include "wx/mac/private.h"
20 #include "wx/taskbar.h"
23 #include "wx/dcmemory.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
27 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
28 EventRef inEvent
, void* pData
)
30 wxTaskBarIcon
*& pTB
= (wxTaskBarIcon
*&) pData
;
32 const UInt32 eventClass
= GetEventClass(inEvent
);
33 const UInt32 eventKind
= GetEventKind(inEvent
);
35 if (eventClass
== kEventClassCommand
&& eventKind
== kEventCommandProcess
)
37 //TODO: This is a complete copy of
38 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
40 if (! pTB
->GetCurrentMenu() )
42 return eventNotHandledErr
;
45 MenuRef hMenu
= MAC_WXHMENU(pTB
->GetCurrentMenu()->GetHMenu());
46 OSStatus result
= eventNotHandledErr
;
51 err
= GetEventParameter(inEvent
, kEventParamDirectObject
, typeHICommand
,
52 NULL
, sizeof(HICommand
), NULL
, &command
);
53 wxASSERT(err
== noErr
);
55 MenuItemIndex menuItemIndex
;
56 err
= GetIndMenuItemWithCommandID(hMenu
, command
.commandID
, 1, NULL
, &menuItemIndex
);
57 wxASSERT(err
== noErr
);
60 MenuCommand id
= command
.commandID
;
61 wxMenuItem
* item
= NULL
;
63 // for items we don't really control
64 if ( id
== kHICommandPreferences
)
66 id
= wxApp::s_macPreferencesMenuItemId
;
68 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
73 item
= mbar
->FindItem( id
, &menu
) ;
77 GetMenuItemRefCon( hMenu
, menuItemIndex
, (UInt32
*) &item
) ;
81 if (item
->IsCheckable())
83 item
->Check( !item
->IsChecked() ) ;
86 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
93 wxASSERT(eventClass
== kEventClassApplication
&& eventKind
== kEventAppGetDockTileMenu
);
95 //process the right click events
96 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
,NULL
);
97 pTB
->ProcessEvent(downevt
);
99 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
100 pTB
->ProcessEvent(upevt
);
103 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
105 OSStatus err
= noErr
;
109 //note to self - a MenuRef IS A MenuHandle
110 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
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
117 //set the actual dock menu
118 err
= SetEventParameter((EventRef
) inEvent
, kEventParamMenuRef
,
119 typeMenuRef
, sizeof(MenuRef
),
126 return eventNotHandledErr
;
129 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler
);
131 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType
& nType
)
132 : m_nType(nType
), m_pEventHandlerRef(NULL
), m_pMenu(NULL
),
133 m_theLastMenu((WXHMENU
)GetApplicationDockTileMenu()), m_iconAdded(false)
135 //Register the events that will return the dock menu
136 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
137 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
142 InstallApplicationEventHandler(
143 GetwxDockEventHandlerUPP(),
144 GetEventTypeCount(tbEventList
), tbEventList
,
145 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
147 wxASSERT(err
== noErr
);
150 wxTaskBarIcon::~wxTaskBarIcon()
152 //clean up event handler
153 RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
155 //restore old icon and menu to the dock
159 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
164 wxMenu
* wxTaskBarIcon::DoCreatePopupMenu()
166 wxMenu
* theNewMenu
= CreatePopupMenu();
171 m_pMenu
= theNewMenu
;
172 m_pMenu
->SetEventHandler(this);
179 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
181 wxMask
* mask
= icon
.GetMask();
184 // Make a mask with no transparent pixels
185 wxBitmap
bmp(icon
.GetWidth(), icon
.GetHeight());
187 dc
.SelectObject(bmp
);
188 dc
.SetBackground(*wxBLACK_BRUSH
);
190 dc
.SelectObject(wxNullBitmap
);
191 mask
= new wxMask(bmp
, *wxWHITE
);
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())),
205 err
= SetApplicationDockTileImage(pImage
);
210 CGImageRelease(pImage
);
215 return m_iconAdded
= err
== noErr
;
218 bool wxTaskBarIcon::RemoveIcon()
226 //restore old icon to the dock
227 OSStatus err
= RestoreApplicationDockTileImage();
230 //restore the old menu to the dock
231 SetApplicationDockTileMenu(MAC_WXHMENU(m_theLastMenu
));
233 return !(m_iconAdded
= !(err
== noErr
));
236 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
238 wxASSERT(menu
!= NULL
);
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).
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.
263 //Perform a deep copy of the menu
264 wxMenuItemList
& theList
= menu
->GetMenuItems();
265 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
267 //create the main menu
268 m_pMenu
= new wxMenu(menu
->GetTitle());
270 while(theNode
!= NULL
)
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
280 theNode
= theNode
->GetNext();
283 m_pMenu
->SetEventHandler(this);
287 #endif //wxHAS_TASK_BAR_ICON