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"
32 //TODO: Implement Apple Software Guidelines - show the top window it it's not shown,
33 //and force it to be unminimized - and all unminimized windows should be brought to
37 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
39 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
40 EventRef inEvent
, void* pData
)
42 wxTaskBarIcon
*& pTB
= (wxTaskBarIcon
*&) pData
;
44 const UInt32 eventClass
= GetEventClass(inEvent
);
45 const UInt32 eventKind
= GetEventKind(inEvent
);
47 if (eventClass
== kEventClassCommand
&& eventKind
== kEventCommandProcess
)
50 //TODO: This is a complete copy of
51 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
52 //FIND A WAY TO EXTERN THIS AND USE THAT HERE INSTEAD!!
54 MenuRef hMenu
= MAC_WXHMENU(pTB
->GetCurrentMenu()->GetHMenu());
55 OSStatus result
= eventNotHandledErr
;
60 err
= GetEventParameter(inEvent
, kEventParamDirectObject
, typeHICommand
,
61 NULL
, sizeof(HICommand
), NULL
, &command
);
62 wxASSERT(err
== noErr
);
64 MenuItemIndex menuItemIndex
;
65 err
= GetIndMenuItemWithCommandID(hMenu
, command
.commandID
, 1, NULL
, &menuItemIndex
);
66 wxASSERT(err
== noErr
);
69 MenuCommand id
= command
.commandID
;
70 wxMenuItem
* item
= NULL
;
71 // for items we don't really control
72 if ( id
== kHICommandPreferences
)
74 id
= wxApp::s_macPreferencesMenuItemId
;
76 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
80 item
= mbar
->FindItem( id
, &menu
) ;
84 GetMenuItemRefCon( hMenu
, menuItemIndex
, (UInt32
*) &item
) ;
88 if (item
->IsCheckable())
90 item
->Check( !item
->IsChecked() ) ;
93 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
99 wxASSERT(eventClass
== kEventClassApplication
&& eventKind
== kEventAppGetDockTileMenu
);
101 //set the internal event
102 pTB
->SetInternalEvent(inEvent
);
104 //process the right click event
105 wxTaskBarIconEvent
evt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
106 pTB
->ProcessEvent(evt
);
108 //set the internal event
109 pTB
->SetInternalEvent(NULL
);
114 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler
);
116 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType
& nType
)
117 : m_nType(nType
), m_pEvent(NULL
), m_pEventHandlerRef(NULL
), m_pMenu(NULL
), m_iconAdded(false)
119 //Register the events that will return the dock menu
120 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
121 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
126 InstallApplicationEventHandler(
127 GetwxDockEventHandlerUPP(),
128 GetEventTypeCount(tbEventList
), tbEventList
,
129 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
131 wxASSERT(err
== noErr
);
134 wxTaskBarIcon::~wxTaskBarIcon()
136 RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
139 void wxTaskBarIcon::SetInternalEvent(void* pEvent
)
144 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
150 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
154 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
156 wxASSERT(pTopWindow
);
158 if(pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)))
159 ((wxDialog
*)pTopWindow
)->SetIcon(icon
);
162 wxASSERT(pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)));
163 ((wxFrame
*)pTopWindow
)->SetIcon(icon
);
168 //TODO: (IT WORKS!) Make work without mask - mayby by using a wxDC?
170 wxASSERT(icon
.GetMask() != NULL
);
173 //create the icon from the bitmap and mask bitmap contained within
174 OSStatus err
= CreateCGImageFromPixMaps(
175 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetHBITMAP())),
176 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetMask()->GetMaskBitmap())),
182 err
= SetApplicationDockTileImage(pImage
);
187 CGImageRelease(pImage
);
189 return m_iconAdded
= err
== noErr
;
193 bool wxTaskBarIcon::RemoveIcon()
195 OSStatus err
= RestoreApplicationDockTileImage();
198 return !(m_iconAdded
= !(err
== noErr
));
201 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
203 wxASSERT(m_pEvent
!= NULL
);
209 menu
->SetEventHandler(this);
211 //note to self - a MenuRef IS A MenuHandle
212 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
214 //When we call SetEventParameter it will decrement
215 //the reference count of the menu - we need to make
216 //sure it stays around in the wxMenu class here
219 //set the actual dock menu
220 OSStatus err
= SetEventParameter((EventRef
) m_pEvent
, kEventParamMenuRef
,
221 typeMenuRef
, sizeof(MenuRef
),
228 #endif //wxHAS_TASK_BAR_ICON