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
35 //http://developer.apple.com/documentation/MacOSX/Conceptual/AppleSWDesign/MacOSXEnvironment/chapter_6_section_4.html
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_pMenu(NULL
), m_iconAdded(false)
119 //Register the events that will return the dock menu
120 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
121 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
123 OSStatus err
= InstallApplicationEventHandler(
124 GetwxDockEventHandlerUPP(),
125 GetEventTypeCount(tbEventList
), tbEventList
,
128 wxASSERT(err
== noErr
);
130 wxTaskBarIcon::~wxTaskBarIcon()
132 //TODO:uninstall event handler
135 void wxTaskBarIcon::SetInternalEvent(void* pEvent
)
140 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
146 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
150 wxWindow
* pTopWindow
= wxTheApp
->GetTopWindow();
152 wxASSERT(pTopWindow
);
154 if(pTopWindow
->IsKindOf(CLASSINFO(wxDialog
)))
155 ((wxDialog
*)pTopWindow
)->SetIcon(icon
);
158 wxASSERT(pTopWindow
->IsKindOf(CLASSINFO(wxFrame
)));
159 ((wxFrame
*)pTopWindow
)->SetIcon(icon
);
164 //TODO: (IT WORKS!) Make work without mask - mayby by using a wxDC?
167 //create the icon from the bitmap and mask bitmap contained within
168 OSStatus err
= CreateCGImageFromPixMaps(
169 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetHBITMAP())),
170 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetMask()->GetMaskBitmap())),
176 err
= SetApplicationDockTileImage(pImage
);
181 CGImageRelease(pImage
);
183 return m_iconAdded
= err
== noErr
;
187 bool wxTaskBarIcon::RemoveIcon()
189 OSStatus err
= RestoreApplicationDockTileImage();
192 return !(m_iconAdded
= !(err
== noErr
));
195 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
197 wxASSERT(m_pEvent
!= NULL
);
203 menu
->SetEventHandler(this);
205 //note to self - a MenuRef IS A MenuHandle
206 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
208 //When we call SetEventParameter it will decrement
209 //the reference count of the menu - we need to make
210 //sure it stays around in the wxMenu class here
213 //set the actual dock menu
214 OSStatus err
= SetEventParameter((EventRef
) m_pEvent
, kEventParamMenuRef
,
215 typeMenuRef
, sizeof(MenuRef
),
222 #endif //wxHAS_TASK_BAR_ICON