]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/taskbar.cpp
2d78d89fdc2716fdb54d69a00d97583f998f2151
[wxWidgets.git] / src / mac / carbon / taskbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: taskbar.cpp
3 // Purpose: wxTaskBarIcon OSX Implementation
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 09/25/2004
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/defs.h"
15
16 #ifdef wxHAS_TASK_BAR_ICON
17
18 #include "wx/mac/private.h"
19
20 #include "wx/taskbar.h"
21 #include "wx/menu.h"
22 #include "wx/icon.h"
23
24 #if 0
25
26 #include "wx/frame.h"
27 #include "wx/dialog.h"
28
29 #endif
30
31 //
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
34 //the front
35 //http://developer.apple.com/documentation/MacOSX/Conceptual/AppleSWDesign/MacOSXEnvironment/chapter_6_section_4.html
36 //TODO:
37 IMPLEMENT_DYNAMIC_CLASS(wxTaskBarIcon, wxEvtHandler)
38
39 pascal OSStatus wxDockEventHandler( EventHandlerCallRef inHandlerCallRef,
40 EventRef inEvent, void* pData)
41 {
42 wxTaskBarIcon*& pTB = (wxTaskBarIcon*&) pData;
43
44 const UInt32 eventClass = GetEventClass(inEvent);
45 const UInt32 eventKind = GetEventKind(inEvent);
46
47 if (eventClass == kEventClassCommand && eventKind == kEventCommandProcess)
48 {
49 //TODO:
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!!
53 //TODO:
54 MenuRef hMenu = MAC_WXHMENU(pTB->GetCurrentMenu()->GetHMenu());
55 OSStatus result = eventNotHandledErr ;
56
57 HICommand command ;
58 OSErr err;
59
60 err = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand,
61 NULL, sizeof(HICommand), NULL, &command);
62 wxASSERT(err == noErr);
63
64 MenuItemIndex menuItemIndex;
65 err = GetIndMenuItemWithCommandID(hMenu, command.commandID, 1, NULL, &menuItemIndex);
66 wxASSERT(err == noErr);
67
68
69 MenuCommand id = command.commandID ;
70 wxMenuItem* item = NULL;
71 // for items we don't really control
72 if ( id == kHICommandPreferences )
73 {
74 id = wxApp::s_macPreferencesMenuItemId ;
75
76 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
77 if ( mbar )
78 {
79 wxMenu* menu = NULL ;
80 item = mbar->FindItem( id , &menu ) ;
81 }
82 }
83 else if (id != 0)
84 GetMenuItemRefCon( hMenu , menuItemIndex , (UInt32*) &item ) ;
85
86 if ( item )
87 {
88 if (item->IsCheckable())
89 {
90 item->Check( !item->IsChecked() ) ;
91 }
92
93 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
94 result = noErr ;
95 }
96 return result ;
97 }
98
99 wxASSERT(eventClass == kEventClassApplication && eventKind == kEventAppGetDockTileMenu);
100
101 //set the internal event
102 pTB->SetInternalEvent(inEvent);
103
104 //process the right click event
105 wxTaskBarIconEvent evt(wxEVT_TASKBAR_RIGHT_UP,NULL);
106 pTB->ProcessEvent(evt);
107
108 //set the internal event
109 pTB->SetInternalEvent(NULL);
110
111 return noErr;
112 }
113
114 DEFINE_ONE_SHOT_HANDLER_GETTER( wxDockEventHandler );
115
116 wxTaskBarIcon::wxTaskBarIcon(const wxTaskBarIconType& nType)
117 : m_nType(nType), m_pEvent(NULL), m_pMenu(NULL), m_iconAdded(false)
118 {
119 //Register the events that will return the dock menu
120 EventTypeSpec tbEventList[] = { { kEventClassCommand, kEventProcessCommand },
121 { kEventClassApplication, kEventAppGetDockTileMenu } };
122
123 OSStatus err = InstallApplicationEventHandler(
124 GetwxDockEventHandlerUPP(),
125 GetEventTypeCount(tbEventList), tbEventList,
126 this, NULL);
127
128 wxASSERT(err == noErr);
129 }
130 wxTaskBarIcon::~wxTaskBarIcon()
131 {
132 //TODO:uninstall event handler
133 }
134
135 void wxTaskBarIcon::SetInternalEvent(void* pEvent)
136 {
137 m_pEvent = pEvent;
138 }
139
140 wxMenu* wxTaskBarIcon::GetCurrentMenu()
141 {
142 return m_pMenu;
143 }
144
145 // Operations:
146 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
147 {
148 #if 0
149 wxASSERT(wxTheApp);
150 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
151
152 wxASSERT(pTopWindow);
153
154 if(pTopWindow->IsKindOf(CLASSINFO(wxDialog)))
155 ((wxDialog*)pTopWindow)->SetIcon(icon);
156 else
157 {
158 wxASSERT(pTopWindow->IsKindOf(CLASSINFO(wxFrame)));
159 ((wxFrame*)pTopWindow)->SetIcon(icon);
160 }
161
162 return true;
163 #else
164 //TODO: (IT WORKS!) Make work without mask - mayby by using a wxDC?
165
166 CGImageRef pImage;
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())),
171 &pImage
172 );
173
174 wxASSERT(err == 0);
175
176 err = SetApplicationDockTileImage(pImage);
177
178 wxASSERT(err == 0);
179
180 if (pImage != NULL)
181 CGImageRelease(pImage);
182
183 return m_iconAdded = err == noErr;
184 #endif
185 }
186
187 bool wxTaskBarIcon::RemoveIcon()
188 {
189 OSStatus err = RestoreApplicationDockTileImage();
190 wxASSERT(err == 0);
191
192 return !(m_iconAdded = !(err == noErr));
193 }
194
195 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
196 {
197 wxASSERT(m_pEvent != NULL);
198
199 if (m_pMenu)
200 delete m_pMenu;
201
202 m_pMenu = menu;
203 menu->SetEventHandler(this);
204
205 //note to self - a MenuRef IS A MenuHandle
206 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
207
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
211 RetainMenu(hMenu);
212
213 //set the actual dock menu
214 OSStatus err = SetEventParameter((EventRef) m_pEvent, kEventParamMenuRef,
215 typeMenuRef, sizeof(MenuRef),
216 &hMenu);
217 wxASSERT(err == 0);
218
219 return err == noErr;
220 }
221
222 #endif //wxHAS_TASK_BAR_ICON