]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/taskbar.cpp
Fix some of vadim's compilation problems
[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 //
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_pEventHandlerRef(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 #ifdef __WXDEBUG__
124 OSStatus err =
125 #endif
126 InstallApplicationEventHandler(
127 GetwxDockEventHandlerUPP(),
128 GetEventTypeCount(tbEventList), tbEventList,
129 this, (&(EventHandlerRef&)m_pEventHandlerRef));
130
131 wxASSERT(err == noErr);
132 }
133
134 wxTaskBarIcon::~wxTaskBarIcon()
135 {
136 RemoveEventHandler((EventHandlerRef&)m_pEventHandlerRef);
137 }
138
139 void wxTaskBarIcon::SetInternalEvent(void* pEvent)
140 {
141 m_pEvent = pEvent;
142 }
143
144 wxMenu* wxTaskBarIcon::GetCurrentMenu()
145 {
146 return m_pMenu;
147 }
148
149 // Operations:
150 bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
151 {
152 #if 0
153 wxASSERT(wxTheApp);
154 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
155
156 wxASSERT(pTopWindow);
157
158 if(pTopWindow->IsKindOf(CLASSINFO(wxDialog)))
159 ((wxDialog*)pTopWindow)->SetIcon(icon);
160 else
161 {
162 wxASSERT(pTopWindow->IsKindOf(CLASSINFO(wxFrame)));
163 ((wxFrame*)pTopWindow)->SetIcon(icon);
164 }
165
166 return true;
167 #else
168 //TODO: (IT WORKS!) Make work without mask - mayby by using a wxDC?
169
170 wxASSERT(icon.GetMask() != NULL);
171
172 CGImageRef pImage;
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())),
177 &pImage
178 );
179
180 wxASSERT(err == 0);
181
182 err = SetApplicationDockTileImage(pImage);
183
184 wxASSERT(err == 0);
185
186 if (pImage != NULL)
187 CGImageRelease(pImage);
188
189 return m_iconAdded = err == noErr;
190 #endif
191 }
192
193 bool wxTaskBarIcon::RemoveIcon()
194 {
195 OSStatus err = RestoreApplicationDockTileImage();
196 wxASSERT(err == 0);
197
198 return !(m_iconAdded = !(err == noErr));
199 }
200
201 bool wxTaskBarIcon::PopupMenu(wxMenu *menu)
202 {
203 wxASSERT(m_pEvent != NULL);
204
205 if (m_pMenu)
206 delete m_pMenu;
207
208 m_pMenu = menu;
209 menu->SetEventHandler(this);
210
211 //note to self - a MenuRef IS A MenuHandle
212 MenuRef hMenu = MAC_WXHMENU(menu->GetHMenu());
213
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
217 RetainMenu(hMenu);
218
219 //set the actual dock menu
220 OSStatus err = SetEventParameter((EventRef) m_pEvent, kEventParamMenuRef,
221 typeMenuRef, sizeof(MenuRef),
222 &hMenu);
223 wxASSERT(err == 0);
224
225 return err == noErr;
226 }
227
228 #endif //wxHAS_TASK_BAR_ICON