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"
27 #include "wx/dialog.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
33 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
34 EventRef inEvent
, void* pData
)
36 wxTaskBarIcon
*& pTB
= (wxTaskBarIcon
*&) pData
;
38 const UInt32 eventClass
= GetEventClass(inEvent
);
39 const UInt32 eventKind
= GetEventKind(inEvent
);
41 if (eventClass
== kEventClassCommand
&& eventKind
== kEventCommandProcess
)
43 MenuRef hMenu
= MAC_WXHMENU(pTB
->GetCurrentMenu()->GetHMenu());
44 OSStatus result
= eventNotHandledErr
;
49 err
= GetEventParameter(inEvent
, kEventParamDirectObject
, typeHICommand
,
50 NULL
, sizeof(HICommand
), NULL
, &command
);
51 wxASSERT(err
== noErr
);
53 MenuItemIndex menuItemIndex
;
54 err
= GetIndMenuItemWithCommandID(hMenu
, command
.commandID
, 1, NULL
, &menuItemIndex
);
55 wxASSERT(err
== noErr
);
58 MenuCommand id
= command
.commandID
;
59 wxMenuItem
* item
= NULL
;
60 // for items we don't really control
61 if ( id
== kHICommandPreferences
)
63 id
= wxApp::s_macPreferencesMenuItemId
;
65 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
69 item
= mbar
->FindItem( id
, &menu
) ;
73 GetMenuItemRefCon( hMenu
, menuItemIndex
, (UInt32
*) &item
) ;
77 if (item
->IsCheckable())
79 item
->Check( !item
->IsChecked() ) ;
82 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
88 wxASSERT(eventClass
== kEventClassApplication
&& eventKind
== kEventAppGetDockTileMenu
);
90 //set the internal event
91 pTB
->SetInternalEvent(inEvent
);
93 //process the right click event
94 wxTaskBarIconEvent
evt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
95 pTB
->ProcessEvent(evt
);
97 //set the internal event
98 pTB
->SetInternalEvent(NULL
);
103 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler
);
105 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType
& nType
)
106 : m_nType(nType
), m_pEvent(NULL
), m_pMenu(NULL
)
108 //Register the events that will return the dock menu
109 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
110 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
112 OSStatus err
= InstallApplicationEventHandler(
113 GetwxDockEventHandlerUPP(),
114 GetEventTypeCount(tbEventList
), tbEventList
,
117 wxASSERT(err
== noErr
);
119 wxTaskBarIcon::~wxTaskBarIcon()
121 //TODO:uninstall event handler
124 void wxTaskBarIcon::SetInternalEvent(void* pEvent
)
129 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
135 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
139 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
141 wxASSERT(pTopWindow
);
143 if(pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)))
144 ((wxDialog
*)pTopWindow
)->SetIcon(icon
);
147 wxASSERT(pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)));
148 ((wxFrame
*)pTopWindow
)->SetIcon(icon
);
153 //TODO: Educated guess
156 //create the icon from the bitmap and mask bitmap contained within
157 OSStatus err
= CreateCGImageFromPixMaps(
158 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetHBITMAP())),
159 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetMask()->GetMaskBitmap())),
165 err
= SetApplicationDockTileImage(pImage
);
173 bool wxTaskBarIcon::RemoveIcon()
176 OSStatus err
= RestoreApplicationDockTileImage();
182 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
184 wxASSERT(m_pEvent
!= NULL
);
190 menu
->SetEventHandler(this);
192 //note to self - a MenuRef IS A MenuHandle
193 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
195 //When we call SetEventParameter it will decrement
196 //the reference count of the menu - we need to make
197 //sure it stays around in the wxMenu class here
200 //set the actual dock menu
201 OSStatus err
= SetEventParameter((EventRef
) m_pEvent
, kEventParamMenuRef
,
202 typeMenuRef
, sizeof(MenuRef
),
209 #endif //wxHAS_TASK_BAR_ICON