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"
25 //TODO: Implement Apple Software Guidelines - show the top window it it's not shown,
26 //and force it to be unminimized - and all unminimized windows should be brought to
30 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon
, wxEvtHandler
)
32 pascal OSStatus
wxDockEventHandler( EventHandlerCallRef inHandlerCallRef
,
33 EventRef inEvent
, void* pData
)
35 wxTaskBarIcon
*& pTB
= (wxTaskBarIcon
*&) pData
;
37 const UInt32 eventClass
= GetEventClass(inEvent
);
38 const UInt32 eventKind
= GetEventKind(inEvent
);
40 if (eventClass
== kEventClassCommand
&& eventKind
== kEventCommandProcess
)
43 //TODO: This is a complete copy of
44 //static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
45 //FIND A WAY TO EXTERN THIS AND USE THAT HERE INSTEAD!!
47 MenuRef hMenu
= MAC_WXHMENU(pTB
->GetCurrentMenu()->GetHMenu());
48 OSStatus result
= eventNotHandledErr
;
53 err
= GetEventParameter(inEvent
, kEventParamDirectObject
, typeHICommand
,
54 NULL
, sizeof(HICommand
), NULL
, &command
);
55 wxASSERT(err
== noErr
);
57 MenuItemIndex menuItemIndex
;
58 err
= GetIndMenuItemWithCommandID(hMenu
, command
.commandID
, 1, NULL
, &menuItemIndex
);
59 wxASSERT(err
== noErr
);
62 MenuCommand id
= command
.commandID
;
63 wxMenuItem
* item
= NULL
;
64 // for items we don't really control
65 if ( id
== kHICommandPreferences
)
67 id
= wxApp::s_macPreferencesMenuItemId
;
69 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 ) ;
92 wxASSERT(eventClass
== kEventClassApplication
&& eventKind
== kEventAppGetDockTileMenu
);
94 //process the right click event
95 wxTaskBarIconEvent
evt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
96 pTB
->ProcessEvent(evt
);
99 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
101 OSStatus err
= noErr
;
105 //note to self - a MenuRef IS A MenuHandle
106 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
108 //When we call SetEventParameter it will decrement
109 //the reference count of the menu - we need to make
110 //sure it stays around in the wxMenu class here
113 //set the actual dock menu
114 err
= SetEventParameter((EventRef
) inEvent
, kEventParamMenuRef
,
115 typeMenuRef
, sizeof(MenuRef
),
123 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler
);
125 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType
& nType
)
126 : m_nType(nType
), m_pEventHandlerRef(NULL
), m_pMenu(NULL
), m_iconAdded(false)
128 //Register the events that will return the dock menu
129 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
130 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
135 InstallApplicationEventHandler(
136 GetwxDockEventHandlerUPP(),
137 GetEventTypeCount(tbEventList
), tbEventList
,
138 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
140 wxASSERT(err
== noErr
);
143 wxTaskBarIcon::~wxTaskBarIcon()
145 RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
148 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
153 wxMenu
* wxTaskBarIcon::DoCreatePopupMenu()
158 m_pMenu
= CreatePopupMenu();
161 m_pMenu
->SetEventHandler(this);
167 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
169 //TODO: (IT WORKS!) Make work without mask - mayby by using a wxDC?
171 wxASSERT(icon
.GetMask() != NULL
);
174 //create the icon from the bitmap and mask bitmap contained within
175 OSStatus err
= CreateCGImageFromPixMaps(
176 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetHBITMAP())),
177 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetMask()->GetMaskBitmap())),
183 err
= SetApplicationDockTileImage(pImage
);
188 CGImageRelease(pImage
);
190 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
)
209 m_pMenu
->SetEventHandler(this);
211 return SetApplicationDockTileMenu(MAC_WXHMENU(menu
->GetHMenu()));
214 #endif //wxHAS_TASK_BAR_ICON