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 MenuRef hMenu
= MAC_WXHMENU(pTB
->GetCurrentMenu()->GetHMenu());
41 OSStatus result
= eventNotHandledErr
;
46 err
= GetEventParameter(inEvent
, kEventParamDirectObject
, typeHICommand
,
47 NULL
, sizeof(HICommand
), NULL
, &command
);
48 wxASSERT(err
== noErr
);
50 MenuItemIndex menuItemIndex
;
51 err
= GetIndMenuItemWithCommandID(hMenu
, command
.commandID
, 1, NULL
, &menuItemIndex
);
52 wxASSERT(err
== noErr
);
55 MenuCommand id
= command
.commandID
;
56 wxMenuItem
* item
= NULL
;
58 // for items we don't really control
59 if ( id
== kHICommandPreferences
)
61 id
= wxApp::s_macPreferencesMenuItemId
;
63 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
68 item
= mbar
->FindItem( id
, &menu
) ;
72 GetMenuItemRefCon( hMenu
, menuItemIndex
, (UInt32
*) &item
) ;
76 if (item
->IsCheckable())
78 item
->Check( !item
->IsChecked() ) ;
81 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
88 wxASSERT(eventClass
== kEventClassApplication
&& eventKind
== kEventAppGetDockTileMenu
);
90 //process the right click events
91 wxTaskBarIconEvent
downevt(wxEVT_TASKBAR_RIGHT_DOWN
,NULL
);
92 pTB
->ProcessEvent(downevt
);
94 wxTaskBarIconEvent
upevt(wxEVT_TASKBAR_RIGHT_UP
,NULL
);
95 pTB
->ProcessEvent(upevt
);
98 wxMenu
* menu
= pTB
->DoCreatePopupMenu();
100 OSStatus err
= noErr
;
104 //note to self - a MenuRef IS A MenuHandle
105 MenuRef hMenu
= MAC_WXHMENU(menu
->GetHMenu());
107 //When we call SetEventParameter it will decrement
108 //the reference count of the menu - we need to make
109 //sure it stays around in the wxMenu class here
112 //set the actual dock menu
113 err
= SetEventParameter((EventRef
) inEvent
, kEventParamMenuRef
,
114 typeMenuRef
, sizeof(MenuRef
),
121 return eventNotHandledErr
;
124 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler
);
126 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType
& nType
)
127 : m_nType(nType
), m_pEventHandlerRef(NULL
), m_pMenu(NULL
),
128 m_theLastMenu((WXHMENU
)GetApplicationDockTileMenu()), m_iconAdded(false)
130 //Register the events that will return the dock menu
131 EventTypeSpec tbEventList
[] = { { kEventClassCommand
, kEventProcessCommand
},
132 { kEventClassApplication
, kEventAppGetDockTileMenu
} };
137 InstallApplicationEventHandler(
138 GetwxDockEventHandlerUPP(),
139 GetEventTypeCount(tbEventList
), tbEventList
,
140 this, (&(EventHandlerRef
&)m_pEventHandlerRef
));
142 wxASSERT(err
== noErr
);
145 wxTaskBarIcon::~wxTaskBarIcon()
147 //clean up event handler
148 RemoveEventHandler((EventHandlerRef
&)m_pEventHandlerRef
);
150 //restore old icon and menu to the dock
154 wxMenu
* wxTaskBarIcon::GetCurrentMenu()
159 wxMenu
* wxTaskBarIcon::DoCreatePopupMenu()
161 wxMenu
* theNewMenu
= CreatePopupMenu();
166 m_pMenu
= theNewMenu
;
167 m_pMenu
->SetEventHandler(this);
174 bool wxTaskBarIcon::SetIcon(const wxIcon
& icon
, const wxString
& tooltip
)
176 wxMask
* mask
= icon
.GetMask();
179 // Make a mask with no transparent pixels
180 wxBitmap
bmp(icon
.GetWidth(), icon
.GetHeight());
182 dc
.SelectObject(bmp
);
183 dc
.SetBackground(*wxBLACK_BRUSH
);
185 dc
.SelectObject(wxNullBitmap
);
186 mask
= new wxMask(bmp
, *wxWHITE
);
191 //create the icon from the bitmap and mask bitmap contained within
192 OSStatus err
= CreateCGImageFromPixMaps(
193 GetGWorldPixMap(MAC_WXHBITMAP(icon
.GetHBITMAP())),
194 GetGWorldPixMap(MAC_WXHBITMAP(mask
->GetMaskBitmap())),
200 err
= SetApplicationDockTileImage(pImage
);
205 CGImageRelease(pImage
);
210 return m_iconAdded
= err
== noErr
;
213 bool wxTaskBarIcon::RemoveIcon()
221 //restore old icon to the dock
222 OSStatus err
= RestoreApplicationDockTileImage();
225 //restore the old menu to the dock
226 SetApplicationDockTileMenu(MAC_WXHMENU(m_theLastMenu
));
228 return !(m_iconAdded
= !(err
== noErr
));
231 bool wxTaskBarIcon::PopupMenu(wxMenu
*menu
)
233 wxASSERT(menu
!= NULL
);
242 // NB: Here we have to perform a deep copy of the menu,
243 // copying each and every menu item from menu to m_pMenu.
244 // Other implementations use wxWindow::PopupMenu here,
245 // which idle execution until the user selects something,
246 // but since the mac handles this internally, we can't -
247 // and have no way at all to idle it while the dock menu
248 // is being shown before menu goes out of scope (it may
249 // not be on the heap, and may expire right after this function
250 // is done - we need it to last until the carbon event is triggered -
251 // that's when the user right clicks).
253 // Also, since there is no equal (assignment) operator
254 // on either wxMenu or wxMenuItem, we have to do all the
255 // dirty work ourselves.
258 //Perform a deep copy of the menu
259 wxMenuItemList
& theList
= menu
->GetMenuItems();
260 wxMenuItemList::compatibility_iterator theNode
= theList
.GetFirst();
262 //create the main menu
263 m_pMenu
= new wxMenu(menu
->GetTitle());
265 while(theNode
!= NULL
)
267 wxMenuItem
* theItem
= theNode
->GetData();
268 m_pMenu
->Append(new wxMenuItem( m_pMenu
, //parent menu
269 theItem
->GetId(), //id
270 theItem
->GetText(), //text label
271 theItem
->GetHelp(), //status bar help string
272 theItem
->GetKind(), //menu flags - checkable, seperator, etc.
273 theItem
->GetSubMenu() //submenu
275 theNode
= theNode
->GetNext();
278 m_pMenu
->SetEventHandler(this);
282 #endif //wxHAS_TASK_BAR_ICON