]>
Commit | Line | Data |
---|---|---|
33e90275 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/cocoa/window.mm | |
3 | // Purpose: widgets (non tlw) for cocoa | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2008-06-20 | |
7 | // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
05cf95ed | 14 | #ifndef WX_PRECOMP |
f487501a | 15 | #include "wx/dcclient.h" |
c583fa5a SC |
16 | #include "wx/nonownedwnd.h" |
17 | #include "wx/log.h" | |
05cf95ed | 18 | #endif |
33e90275 SC |
19 | |
20 | #ifdef __WXMAC__ | |
4dd9fdf8 | 21 | #include "wx/osx/private.h" |
33e90275 SC |
22 | #endif |
23 | ||
0c530e5a SC |
24 | #if wxUSE_CARET |
25 | #include "wx/caret.h" | |
26 | #endif | |
27 | ||
4dd9fdf8 SC |
28 | #if wxUSE_DRAG_AND_DROP |
29 | #include "wx/dnd.h" | |
30 | #endif | |
31 | ||
32 | #include <objc/objc-runtime.h> | |
33 | ||
dbeddfb9 SC |
34 | NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) |
35 | { | |
36 | int x, y, w, h ; | |
37 | ||
38 | window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ; | |
39 | wxRect bounds(x,y,w,h); | |
40 | NSView* sv = (window->GetParent()->GetHandle() ); | |
41 | ||
42 | return wxToNSRect( sv, bounds ); | |
43 | } | |
33e90275 SC |
44 | |
45 | @interface wxNSView : NSView | |
46 | { | |
33e90275 SC |
47 | } |
48 | ||
524c47aa | 49 | - (BOOL) canBecomeKeyView; |
33e90275 SC |
50 | |
51 | @end // wxNSView | |
52 | ||
ffad7b0d | 53 | @interface NSView(PossibleMethods) |
ffad7b0d SC |
54 | - (void)setTitle:(NSString *)aString; |
55 | - (void)setStringValue:(NSString *)aString; | |
56 | - (void)setIntValue:(int)anInt; | |
57 | - (void)setFloatValue:(float)aFloat; | |
58 | - (void)setDoubleValue:(double)aDouble; | |
59 | ||
19c7ac3d SC |
60 | - (double)minValue; |
61 | - (double)maxValue; | |
ffad7b0d SC |
62 | - (void)setMinValue:(double)aDouble; |
63 | - (void)setMaxValue:(double)aDouble; | |
64 | ||
65 | - (void)sizeToFit; | |
66 | ||
67 | - (BOOL)isEnabled; | |
68 | - (void)setEnabled:(BOOL)flag; | |
69 | ||
70 | - (void)setImage:(NSImage *)image; | |
71 | - (void)setControlSize:(NSControlSize)size; | |
0c530e5a SC |
72 | |
73 | - (id)contentView; | |
4dd9fdf8 SC |
74 | |
75 | - (void)setTarget:(id)anObject; | |
76 | - (void)setAction:(SEL)aSelector; | |
77 | - (void)setDoubleAction:(SEL)aSelector; | |
ffad7b0d SC |
78 | @end |
79 | ||
19c7ac3d | 80 | long wxOSXTranslateCocoaKey( int unichar ) |
524c47aa | 81 | { |
19c7ac3d | 82 | long retval = unichar; |
524c47aa SC |
83 | switch( unichar ) |
84 | { | |
85 | case NSUpArrowFunctionKey : | |
86 | retval = WXK_UP; | |
87 | break; | |
88 | case NSDownArrowFunctionKey : | |
89 | retval = WXK_DOWN; | |
90 | break; | |
91 | case NSLeftArrowFunctionKey : | |
92 | retval = WXK_LEFT; | |
93 | break; | |
94 | case NSRightArrowFunctionKey : | |
95 | retval = WXK_RIGHT; | |
96 | break; | |
97 | case NSInsertFunctionKey : | |
98 | retval = WXK_INSERT; | |
99 | break; | |
100 | case NSDeleteFunctionKey : | |
101 | retval = WXK_DELETE; | |
102 | break; | |
103 | case NSHomeFunctionKey : | |
104 | retval = WXK_HOME; | |
105 | break; | |
106 | // case NSBeginFunctionKey : | |
107 | // retval = WXK_BEGIN; | |
108 | // break; | |
109 | case NSEndFunctionKey : | |
110 | retval = WXK_END; | |
111 | break; | |
112 | case NSPageUpFunctionKey : | |
113 | retval = WXK_PAGEUP; | |
114 | break; | |
115 | case NSPageDownFunctionKey : | |
116 | retval = WXK_PAGEDOWN; | |
117 | break; | |
118 | case NSHelpFunctionKey : | |
119 | retval = WXK_HELP; | |
120 | break; | |
121 | ||
122 | default : | |
123 | if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey ) | |
124 | retval = WXK_F1 + (unichar - NSF1FunctionKey ); | |
125 | break; | |
126 | } | |
127 | return retval; | |
128 | } | |
129 | ||
130 | void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent ) | |
131 | { | |
132 | UInt32 modifiers = [nsEvent modifierFlags] ; | |
fc39cf72 | 133 | int eventType = [nsEvent type]; |
524c47aa SC |
134 | |
135 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; | |
136 | wxevent.m_controlDown = modifiers & NSControlKeyMask; | |
137 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; | |
138 | wxevent.m_metaDown = modifiers & NSCommandKeyMask; | |
524c47aa | 139 | |
19c7ac3d | 140 | wxevent.m_rawCode = [nsEvent keyCode]; |
524c47aa SC |
141 | wxevent.m_rawFlags = modifiers; |
142 | ||
143 | wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; | |
524c47aa SC |
144 | switch (eventType) |
145 | { | |
146 | case NSKeyDown : | |
147 | wxevent.SetEventType( wxEVT_KEY_DOWN ) ; | |
148 | break; | |
149 | case NSKeyUp : | |
150 | wxevent.SetEventType( wxEVT_KEY_UP ) ; | |
151 | break; | |
152 | case NSFlagsChanged : | |
153 | // setup common code here | |
154 | break; | |
155 | default : | |
156 | break ; | |
157 | } | |
19c7ac3d SC |
158 | |
159 | wxString chars; | |
160 | if ( eventType != NSFlagsChanged ) | |
161 | { | |
162 | NSString* nschars = [nsEvent characters]; | |
163 | if ( nschars ) | |
164 | { | |
165 | wxCFStringRef cfchars((CFStringRef)[nschars retain]); | |
166 | chars = cfchars.AsString(); | |
167 | } | |
168 | } | |
169 | ||
170 | int unichar = chars.Length() > 0 ? chars[0] : 0; | |
171 | long keyval = wxOSXTranslateCocoaKey(unichar) ; | |
172 | if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) ) | |
173 | keyval = wxToupper( keyval ) ; | |
174 | ||
175 | #if wxUSE_UNICODE | |
176 | wxevent.m_uniChar = unichar; | |
177 | #endif | |
178 | wxevent.m_keyCode = keyval; | |
524c47aa SC |
179 | } |
180 | ||
54f11060 SC |
181 | UInt32 g_lastButton = 0 ; |
182 | bool g_lastButtonWasFakeRight = false ; | |
183 | ||
33e90275 SC |
184 | void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) |
185 | { | |
4800ca79 | 186 | int eventType = [nsEvent type]; |
33e90275 SC |
187 | UInt32 modifiers = [nsEvent modifierFlags] ; |
188 | wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]); | |
189 | ||
190 | // these parameters are not given for all events | |
191 | UInt32 button = [nsEvent buttonNumber]; | |
4800ca79 SC |
192 | UInt32 clickCount = 0; |
193 | if ( eventType != NSScrollWheel ) | |
194 | [nsEvent clickCount]; | |
33e90275 SC |
195 | |
196 | wxevent.m_x = screenMouseLocation.x; | |
197 | wxevent.m_y = screenMouseLocation.y; | |
198 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; | |
199 | wxevent.m_controlDown = modifiers & NSControlKeyMask; | |
200 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; | |
201 | wxevent.m_metaDown = modifiers & NSCommandKeyMask; | |
202 | wxevent.m_clickCount = clickCount; | |
dbeddfb9 | 203 | wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; |
54f11060 SC |
204 | |
205 | UInt32 mouseChord = 0; | |
54f11060 SC |
206 | |
207 | switch (eventType) | |
208 | { | |
209 | case NSLeftMouseDown : | |
210 | case NSLeftMouseDragged : | |
211 | mouseChord = 1U; | |
212 | break; | |
213 | case NSRightMouseDown : | |
214 | case NSRightMouseDragged : | |
215 | mouseChord = 2U; | |
216 | break; | |
217 | case NSOtherMouseDown : | |
218 | case NSOtherMouseDragged : | |
219 | mouseChord = 4U; | |
220 | break; | |
221 | } | |
222 | ||
33e90275 SC |
223 | // a control click is interpreted as a right click |
224 | bool thisButtonIsFakeRight = false ; | |
54f11060 | 225 | if ( button == 0 && (modifiers & NSControlKeyMask) ) |
33e90275 | 226 | { |
54f11060 | 227 | button = 1 ; |
33e90275 SC |
228 | thisButtonIsFakeRight = true ; |
229 | } | |
230 | ||
231 | // otherwise we report double clicks by connecting a left click with a ctrl-left click | |
232 | if ( clickCount > 1 && button != g_lastButton ) | |
233 | clickCount = 1 ; | |
54f11060 | 234 | |
33e90275 SC |
235 | // we must make sure that our synthetic 'right' button corresponds in |
236 | // mouse down, moved and mouse up, and does not deliver a right down and left up | |
54f11060 | 237 | switch (eventType) |
33e90275 | 238 | { |
54f11060 SC |
239 | case NSLeftMouseDown : |
240 | case NSRightMouseDown : | |
241 | case NSOtherMouseDown : | |
242 | g_lastButton = button ; | |
243 | g_lastButtonWasFakeRight = thisButtonIsFakeRight ; | |
244 | break; | |
245 | } | |
33e90275 SC |
246 | |
247 | if ( button == 0 ) | |
248 | { | |
249 | g_lastButton = 0 ; | |
250 | g_lastButtonWasFakeRight = false ; | |
251 | } | |
54f11060 | 252 | else if ( g_lastButton == 1 && g_lastButtonWasFakeRight ) |
33e90275 SC |
253 | button = g_lastButton ; |
254 | ||
255 | // Adjust the chord mask to remove the primary button and add the | |
256 | // secondary button. It is possible that the secondary button is | |
257 | // already pressed, e.g. on a mouse connected to a laptop, but this | |
258 | // possibility is ignored here: | |
259 | if( thisButtonIsFakeRight && ( mouseChord & 1U ) ) | |
260 | mouseChord = ((mouseChord & ~1U) | 2U); | |
261 | ||
262 | if(mouseChord & 1U) | |
263 | wxevent.m_leftDown = true ; | |
264 | if(mouseChord & 2U) | |
265 | wxevent.m_rightDown = true ; | |
266 | if(mouseChord & 4U) | |
267 | wxevent.m_middleDown = true ; | |
268 | ||
33e90275 | 269 | // translate into wx types |
33e90275 SC |
270 | switch (eventType) |
271 | { | |
272 | case NSLeftMouseDown : | |
273 | case NSRightMouseDown : | |
274 | case NSOtherMouseDown : | |
275 | switch ( button ) | |
276 | { | |
277 | case 0 : | |
278 | wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ; | |
279 | break ; | |
280 | ||
281 | case 1 : | |
282 | wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ; | |
283 | break ; | |
284 | ||
285 | case 2 : | |
286 | wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ; | |
287 | break ; | |
288 | ||
289 | default: | |
290 | break ; | |
291 | } | |
292 | break ; | |
293 | ||
294 | case NSLeftMouseUp : | |
295 | case NSRightMouseUp : | |
296 | case NSOtherMouseUp : | |
297 | switch ( button ) | |
298 | { | |
299 | case 0 : | |
300 | wxevent.SetEventType( wxEVT_LEFT_UP ) ; | |
301 | break ; | |
302 | ||
303 | case 1 : | |
304 | wxevent.SetEventType( wxEVT_RIGHT_UP ) ; | |
305 | break ; | |
306 | ||
307 | case 2 : | |
308 | wxevent.SetEventType( wxEVT_MIDDLE_UP ) ; | |
309 | break ; | |
310 | ||
311 | default: | |
312 | break ; | |
313 | } | |
314 | break ; | |
315 | ||
316 | case NSScrollWheel : | |
317 | { | |
318 | wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ; | |
33e90275 SC |
319 | wxevent.m_wheelDelta = 1; |
320 | wxevent.m_linesPerAction = 1; | |
4800ca79 SC |
321 | if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) ) |
322 | { | |
33e90275 | 323 | wxevent.m_wheelAxis = 1; |
4800ca79 SC |
324 | wxevent.m_wheelRotation = [nsEvent deltaX] > 0.0 ? 1 : -1; |
325 | } | |
326 | else | |
327 | { | |
328 | wxevent.m_wheelRotation = [nsEvent deltaY] > 0.0 ? 1 : -1; | |
329 | } | |
33e90275 SC |
330 | } |
331 | break ; | |
332 | ||
333 | case NSMouseEntered : | |
334 | case NSMouseExited : | |
335 | case NSLeftMouseDragged : | |
336 | case NSRightMouseDragged : | |
337 | case NSOtherMouseDragged : | |
338 | case NSMouseMoved : | |
339 | wxevent.SetEventType( wxEVT_MOTION ) ; | |
340 | break; | |
341 | default : | |
342 | break ; | |
343 | } | |
344 | } | |
345 | ||
346 | @implementation wxNSView | |
347 | ||
4dd9fdf8 SC |
348 | + (void)initialize |
349 | { | |
350 | static BOOL initialized = NO; | |
351 | if (!initialized) | |
352 | { | |
353 | initialized = YES; | |
354 | wxOSXCocoaClassAddWXMethods( self ); | |
355 | } | |
356 | } | |
357 | ||
358 | - (BOOL) canBecomeKeyView | |
359 | { | |
360 | return YES; | |
361 | } | |
362 | ||
363 | @end // wxNSView | |
364 | ||
365 | // | |
366 | // event handlers | |
367 | // | |
368 | ||
369 | #if wxUSE_DRAG_AND_DROP | |
370 | ||
371 | // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html | |
372 | // for details on the NSPasteboard -> PasteboardRef conversion | |
373 | ||
374 | NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender ) | |
375 | { | |
376 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
377 | if (impl == NULL) | |
378 | return NSDragOperationNone; | |
379 | ||
380 | return impl->draggingEntered(sender, self, _cmd); | |
381 | } | |
382 | ||
383 | void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender ) | |
384 | { | |
385 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
386 | if (impl == NULL) | |
387 | return ; | |
388 | ||
389 | return impl->draggingExited(sender, self, _cmd); | |
390 | } | |
391 | ||
392 | NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender ) | |
393 | { | |
394 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
395 | if (impl == NULL) | |
396 | return NSDragOperationNone; | |
397 | ||
398 | return impl->draggingUpdated(sender, self, _cmd); | |
399 | } | |
400 | ||
401 | BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender ) | |
402 | { | |
403 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
404 | if (impl == NULL) | |
405 | return NSDragOperationNone; | |
406 | ||
407 | return impl->performDragOperation(sender, self, _cmd) ? YES:NO ; | |
408 | } | |
409 | ||
410 | void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) | |
411 | { | |
412 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
413 | if (impl == NULL) | |
414 | return; | |
415 | ||
416 | impl->mouseEvent(event, self, _cmd); | |
417 | } | |
418 | ||
419 | void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event) | |
420 | { | |
421 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
422 | if (impl == NULL) | |
423 | return; | |
424 | ||
425 | impl->keyEvent(event, self, _cmd); | |
426 | } | |
427 | ||
428 | BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) | |
429 | { | |
430 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
431 | if (impl == NULL) | |
432 | return NO; | |
433 | ||
434 | return impl->performKeyEquivalent(event, self, _cmd); | |
435 | } | |
436 | ||
437 | BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd) | |
438 | { | |
439 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
440 | if (impl == NULL) | |
441 | return NO; | |
442 | ||
443 | return impl->becomeFirstResponder(self, _cmd); | |
444 | } | |
445 | ||
446 | BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd) | |
447 | { | |
448 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
449 | if (impl == NULL) | |
450 | return NO; | |
451 | ||
452 | return impl->resignFirstResponder(self, _cmd); | |
453 | } | |
454 | ||
455 | void wxOSX_resetCursorRects(NSView* self, SEL _cmd) | |
456 | { | |
457 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
458 | if (impl == NULL) | |
459 | return; | |
460 | ||
461 | impl->resetCursorRects(self, _cmd); | |
462 | } | |
463 | ||
464 | BOOL wxOSX_isFlipped(NSView* self, SEL _cmd) | |
465 | { | |
466 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
467 | if (impl == NULL) | |
468 | return NO; | |
469 | ||
470 | return impl->isFlipped(self, _cmd) ? YES:NO; | |
471 | } | |
472 | ||
473 | void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect) | |
474 | { | |
475 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
476 | if (impl == NULL) | |
477 | return; | |
478 | ||
479 | return impl->drawRect(&rect, self, _cmd); | |
480 | } | |
481 | ||
482 | void wxOSX_clickedAction(NSView* self, SEL _cmd, id sender) | |
483 | { | |
484 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
485 | if (impl == NULL) | |
486 | return; | |
487 | ||
488 | impl->clickedAction(self, _cmd, sender); | |
489 | } | |
490 | ||
491 | void wxOSX_doubleClickedAction(NSView* self, SEL _cmd, id sender) | |
492 | { | |
493 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
494 | if (impl == NULL) | |
495 | return; | |
496 | ||
497 | impl->doubleClickedAction(self, _cmd, sender); | |
498 | } | |
dbeddfb9 | 499 | |
4dd9fdf8 | 500 | unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd) |
33e90275 | 501 | { |
4dd9fdf8 SC |
502 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; |
503 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
504 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
505 | ||
506 | wxWindow* wxpeer = GetWXPeer(); | |
507 | if ( wxpeer == NULL ) | |
508 | return NSDragOperationNone; | |
509 | ||
510 | wxDropTarget* target = wxpeer->GetDropTarget(); | |
511 | if ( target == NULL ) | |
512 | return NSDragOperationNone; | |
513 | ||
514 | wxDragResult result = wxDragNone; | |
515 | wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); | |
516 | ||
517 | if ( sourceDragMask & NSDragOperationLink ) | |
518 | result = wxDragLink; | |
519 | else if ( sourceDragMask & NSDragOperationCopy ) | |
520 | result = wxDragCopy; | |
521 | else if ( sourceDragMask & NSDragOperationMove ) | |
522 | result = wxDragMove; | |
523 | ||
524 | PasteboardRef pboardRef; | |
525 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); | |
526 | target->SetCurrentDragPasteboard(pboardRef); | |
527 | result = target->OnEnter(pt.x, pt.y, result); | |
528 | CFRelease(pboardRef); | |
529 | ||
530 | NSDragOperation nsresult = NSDragOperationNone; | |
531 | switch (result ) | |
33e90275 | 532 | { |
4dd9fdf8 SC |
533 | case wxDragLink: |
534 | nsresult = NSDragOperationLink; | |
535 | case wxDragMove: | |
536 | nsresult = NSDragOperationMove; | |
537 | case wxDragCopy: | |
538 | nsresult = NSDragOperationCopy; | |
539 | default : | |
540 | break; | |
541 | } | |
542 | return nsresult; | |
543 | } | |
33e90275 | 544 | |
4dd9fdf8 SC |
545 | void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd) |
546 | { | |
547 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
548 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
549 | ||
550 | wxWindow* wxpeer = GetWXPeer(); | |
551 | if ( wxpeer == NULL ) | |
552 | return; | |
553 | ||
554 | wxDropTarget* target = wxpeer->GetDropTarget(); | |
555 | if ( target == NULL ) | |
556 | return; | |
557 | ||
558 | PasteboardRef pboardRef; | |
559 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); | |
560 | target->SetCurrentDragPasteboard(pboardRef); | |
561 | target->OnLeave(); | |
562 | CFRelease(pboardRef); | |
563 | } | |
564 | ||
565 | unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd) | |
566 | { | |
567 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
568 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
569 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
dbeddfb9 | 570 | |
4dd9fdf8 SC |
571 | wxWindow* wxpeer = GetWXPeer(); |
572 | if ( wxpeer == NULL ) | |
573 | return NSDragOperationNone; | |
574 | ||
575 | wxDropTarget* target = wxpeer->GetDropTarget(); | |
576 | if ( target == NULL ) | |
577 | return NSDragOperationNone; | |
578 | ||
579 | wxDragResult result = wxDragNone; | |
580 | wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); | |
581 | ||
582 | if ( sourceDragMask & NSDragOperationLink ) | |
583 | result = wxDragLink; | |
584 | else if ( sourceDragMask & NSDragOperationCopy ) | |
585 | result = wxDragCopy; | |
586 | else if ( sourceDragMask & NSDragOperationMove ) | |
587 | result = wxDragMove; | |
588 | ||
589 | PasteboardRef pboardRef; | |
590 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); | |
591 | target->SetCurrentDragPasteboard(pboardRef); | |
592 | result = target->OnDragOver(pt.x, pt.y, result); | |
593 | CFRelease(pboardRef); | |
594 | ||
595 | NSDragOperation nsresult = NSDragOperationNone; | |
596 | switch (result ) | |
597 | { | |
598 | case wxDragLink: | |
599 | nsresult = NSDragOperationLink; | |
600 | case wxDragMove: | |
601 | nsresult = NSDragOperationMove; | |
602 | case wxDragCopy: | |
603 | nsresult = NSDragOperationCopy; | |
604 | default : | |
605 | break; | |
606 | } | |
607 | return nsresult; | |
608 | } | |
609 | ||
610 | bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd) | |
611 | { | |
612 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
dbeddfb9 | 613 | |
4dd9fdf8 SC |
614 | NSPasteboard *pboard = [sender draggingPasteboard]; |
615 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
616 | ||
617 | wxWindow* wxpeer = GetWXPeer(); | |
618 | wxDropTarget* target = wxpeer->GetDropTarget(); | |
619 | wxDragResult result = wxDragNone; | |
620 | wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] ); | |
621 | ||
622 | if ( sourceDragMask & NSDragOperationLink ) | |
623 | result = wxDragLink; | |
624 | else if ( sourceDragMask & NSDragOperationCopy ) | |
625 | result = wxDragCopy; | |
626 | else if ( sourceDragMask & NSDragOperationMove ) | |
627 | result = wxDragMove; | |
628 | ||
629 | PasteboardRef pboardRef; | |
630 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); | |
631 | target->SetCurrentDragPasteboard(pboardRef); | |
632 | result = target->OnData(pt.x, pt.y, result); | |
633 | CFRelease(pboardRef); | |
634 | ||
635 | return result != wxDragNone; | |
636 | } | |
637 | ||
638 | #endif | |
639 | ||
640 | typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); | |
641 | typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); | |
642 | typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd); | |
643 | typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd); | |
644 | typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect); | |
645 | ||
646 | void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
647 | { | |
648 | if ( !DoHandleMouseEvent(event) ) | |
649 | { | |
650 | wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
651 | superimpl(slf, (SEL)_cmd, event); | |
652 | } | |
653 | } | |
654 | ||
655 | void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
656 | { | |
657 | if ( !DoHandleKeyEvent(event) ) | |
658 | { | |
659 | wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
660 | superimpl(slf, (SEL)_cmd, event); | |
661 | } | |
662 | } | |
663 | ||
664 | bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
665 | { | |
666 | if ( !DoHandleKeyEvent(event) ) | |
667 | { | |
668 | wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
669 | return superimpl(slf, (SEL)_cmd, event); | |
670 | } | |
671 | ||
672 | return YES; | |
673 | } | |
674 | ||
675 | bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) | |
676 | { | |
677 | wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
678 | BOOL r = superimpl(slf, (SEL)_cmd); | |
679 | if ( r ) | |
680 | DoNotifyFocusEvent( true ); | |
681 | return r; | |
682 | } | |
683 | ||
684 | bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd) | |
685 | { | |
686 | wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
687 | BOOL r = superimpl(slf, (SEL)_cmd); | |
688 | if ( r ) | |
689 | DoNotifyFocusEvent( false ); | |
690 | return r; | |
691 | } | |
692 | ||
693 | void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd) | |
694 | { | |
695 | wxWindow* wxpeer = GetWXPeer(); | |
696 | if ( wxpeer ) | |
697 | { | |
698 | NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR(); | |
699 | if (cursor == NULL) | |
dbeddfb9 | 700 | { |
4dd9fdf8 SC |
701 | wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; |
702 | superimpl(slf, (SEL)_cmd); | |
dbeddfb9 | 703 | } |
4dd9fdf8 SC |
704 | else |
705 | [slf addCursorRect: [slf bounds] | |
706 | cursor: cursor]; | |
707 | } | |
708 | } | |
709 | ||
710 | bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd) | |
711 | { | |
712 | return m_isFlipped; | |
713 | } | |
33e90275 | 714 | |
4dd9fdf8 SC |
715 | |
716 | #define OSX_DEBUG_DRAWING 0 | |
717 | ||
718 | void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd) | |
719 | { | |
720 | CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
721 | CGContextSaveGState( context ); | |
722 | ||
723 | #if OSX_DEBUG_DRAWING | |
724 | CGContextBeginPath( context ); | |
725 | CGContextMoveToPoint(context, 0, 0); | |
726 | NSRect bounds = [self bounds]; | |
727 | CGContextAddLineToPoint(context, 10, 0); | |
728 | CGContextMoveToPoint(context, 0, 0); | |
729 | CGContextAddLineToPoint(context, 0, 10); | |
730 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
731 | CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10); | |
732 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
733 | CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height); | |
734 | CGContextClosePath( context ); | |
735 | CGContextStrokePath(context); | |
736 | #endif | |
737 | ||
738 | if ( !m_isFlipped ) | |
739 | { | |
740 | CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height ); | |
741 | CGContextScaleCTM( context, 1, -1 ); | |
742 | } | |
743 | ||
744 | wxRegion updateRgn; | |
745 | const NSRect *rects; | |
746 | NSInteger count; | |
747 | ||
748 | [slf getRectsBeingDrawn:&rects count:&count]; | |
749 | for ( int i = 0 ; i < count ; ++i ) | |
750 | { | |
751 | updateRgn.Union(wxFromNSRect(slf, rects[i]) ); | |
752 | } | |
753 | ||
754 | wxWindow* wxpeer = GetWXPeer(); | |
755 | wxpeer->GetUpdateRegion() = updateRgn; | |
756 | wxpeer->MacSetCGContextRef( context ); | |
757 | ||
09489833 | 758 | bool handled = wxpeer->MacDoRedraw( 0 ); |
4dd9fdf8 SC |
759 | |
760 | CGContextRestoreGState( context ); | |
09489833 SC |
761 | |
762 | CGContextSaveGState( context ); | |
4dd9fdf8 SC |
763 | if ( !handled ) |
764 | { | |
765 | // call super | |
766 | SEL _cmd = @selector(drawRect:); | |
767 | wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd]; | |
768 | superimpl(slf, _cmd, *(NSRect*)rect); | |
09489833 SC |
769 | CGContextRestoreGState( context ); |
770 | CGContextSaveGState( context ); | |
33e90275 | 771 | } |
09489833 SC |
772 | wxpeer->MacPaintChildrenBorders(); |
773 | CGContextRestoreGState( context ); | |
33e90275 SC |
774 | } |
775 | ||
4dd9fdf8 SC |
776 | void wxWidgetCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender) |
777 | { | |
778 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
779 | if ( wxpeer ) | |
780 | wxpeer->OSXHandleClicked(0); | |
781 | } | |
33e90275 | 782 | |
4dd9fdf8 | 783 | void wxWidgetCocoaImpl::doubleClickedAction( WXWidget slf, void *_cmd, void *sender) |
524c47aa | 784 | { |
524c47aa | 785 | } |
33e90275 | 786 | |
4dd9fdf8 SC |
787 | // |
788 | ||
789 | #if OBJC_API_VERSION >= 2 | |
790 | ||
791 | #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ | |
792 | class_addMethod(c, s, i, t ); | |
793 | ||
794 | #else | |
795 | ||
796 | #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ | |
797 | { s, t, i }, | |
798 | ||
799 | #endif | |
800 | ||
801 | void wxOSXCocoaClassAddWXMethods(Class c) | |
802 | { | |
803 | ||
804 | #if OBJC_API_VERSION < 2 | |
805 | static objc_method wxmethods[] = | |
806 | { | |
807 | #endif | |
808 | ||
809 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
810 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
811 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
812 | ||
813 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
814 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
815 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
816 | ||
817 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
818 | ||
819 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
820 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
821 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
822 | ||
823 | wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
824 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
825 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
826 | ||
827 | wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" ) | |
828 | wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" ) | |
829 | wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" ) | |
830 | ||
831 | wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" ) | |
832 | ||
833 | ||
834 | wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" ) | |
835 | wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" ) | |
836 | wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" ) | |
837 | ||
838 | wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" ) | |
839 | wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" ) | |
840 | ||
841 | wxOSX_CLASS_ADD_METHOD(c, @selector(clickedAction:), (IMP) wxOSX_clickedAction, "v@:@" ) | |
842 | wxOSX_CLASS_ADD_METHOD(c, @selector(doubleClickedAction:), (IMP) wxOSX_doubleClickedAction, "v@:@" ) | |
843 | ||
844 | #if wxUSE_DRAG_AND_DROP | |
845 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" ) | |
846 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" ) | |
847 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" ) | |
848 | wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" ) | |
849 | #endif | |
850 | ||
851 | #if OBJC_API_VERSION < 2 | |
852 | } ; | |
853 | static int method_count = WXSIZEOF( wxmethods ); | |
854 | static objc_method_list *wxmethodlist = NULL; | |
855 | if ( wxmethodlist == NULL ) | |
856 | { | |
857 | wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) ); | |
858 | memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) ); | |
859 | wxmethodlist->method_count = method_count; | |
860 | wxmethodlist->obsolete = 0; | |
861 | } | |
862 | class_addMethods( c, wxmethodlist ); | |
863 | #endif | |
864 | } | |
865 | ||
866 | // | |
867 | // C++ implementation class | |
868 | // | |
33e90275 SC |
869 | |
870 | IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl ) | |
871 | ||
872 | wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) : | |
4dd9fdf8 | 873 | wxWidgetImpl( peer, isRootControl ) |
33e90275 | 874 | { |
4dd9fdf8 SC |
875 | Init(); |
876 | m_osxView = w; | |
33e90275 SC |
877 | } |
878 | ||
879 | wxWidgetCocoaImpl::wxWidgetCocoaImpl() | |
880 | { | |
4dd9fdf8 | 881 | Init(); |
33e90275 SC |
882 | } |
883 | ||
884 | void wxWidgetCocoaImpl::Init() | |
885 | { | |
886 | m_osxView = NULL; | |
4dd9fdf8 | 887 | m_isFlipped = true; |
33e90275 SC |
888 | } |
889 | ||
890 | wxWidgetCocoaImpl::~wxWidgetCocoaImpl() | |
891 | { | |
4dd9fdf8 SC |
892 | RemoveAssociations( this ); |
893 | ||
dbeddfb9 SC |
894 | if ( !IsRootControl() ) |
895 | { | |
896 | NSView *sv = [m_osxView superview]; | |
897 | if ( sv != nil ) | |
898 | [m_osxView removeFromSuperview]; | |
899 | } | |
33e90275 SC |
900 | [m_osxView release]; |
901 | } | |
902 | ||
903 | bool wxWidgetCocoaImpl::IsVisible() const | |
904 | { | |
905 | return [m_osxView isHiddenOrHasHiddenAncestor] == NO; | |
906 | } | |
907 | ||
dbeddfb9 SC |
908 | void wxWidgetCocoaImpl::SetVisibility( bool visible ) |
909 | { | |
910 | [m_osxView setHidden:(visible ? NO:YES)]; | |
911 | } | |
912 | ||
33e90275 SC |
913 | void wxWidgetCocoaImpl::Raise() |
914 | { | |
915 | } | |
916 | ||
917 | void wxWidgetCocoaImpl::Lower() | |
918 | { | |
919 | } | |
920 | ||
921 | void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy ) | |
922 | { | |
54f11060 SC |
923 | #if 1 |
924 | SetNeedsDisplay() ; | |
925 | #else | |
926 | // We should do something like this, but it wasn't working in 10.4. | |
927 | if (GetNeedsDisplay() ) | |
928 | { | |
929 | SetNeedsDisplay() ; | |
930 | } | |
931 | NSRect r = wxToNSRect( [m_osxView superview], *rect ); | |
932 | NSSize offset = NSMakeSize((float)dx, (float)dy); | |
933 | [m_osxView scrollRect:r by:offset]; | |
934 | #endif | |
33e90275 SC |
935 | } |
936 | ||
937 | void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) | |
938 | { | |
0c530e5a SC |
939 | wxWindowMac* parent = GetWXPeer()->GetParent(); |
940 | // under Cocoa we might have a contentView in the wxParent to which we have to | |
941 | // adjust the coordinates | |
942 | if (parent) | |
943 | { | |
944 | wxPoint pt(parent->GetClientAreaOrigin()); | |
945 | x -= pt.x; | |
946 | y -= pt.y; | |
947 | } | |
33e90275 SC |
948 | NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) ); |
949 | [m_osxView setFrame:r]; | |
950 | } | |
951 | ||
952 | void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const | |
953 | { | |
954 | wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] ); | |
955 | x = r.GetLeft(); | |
956 | y = r.GetTop(); | |
957 | } | |
958 | ||
959 | void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const | |
960 | { | |
961 | NSRect rect = [m_osxView frame]; | |
962 | width = rect.size.width; | |
963 | height = rect.size.height; | |
964 | } | |
965 | ||
dbeddfb9 | 966 | void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const |
33e90275 | 967 | { |
0c530e5a SC |
968 | if ( [m_osxView respondsToSelector:@selector(contentView) ] ) |
969 | { | |
970 | NSView* cv = [m_osxView contentView]; | |
971 | ||
972 | NSRect bounds = [m_osxView bounds]; | |
973 | NSRect rect = [cv frame]; | |
974 | ||
975 | int y = rect.origin.y; | |
976 | int x = rect.origin.x; | |
977 | if ( ![ m_osxView isFlipped ] ) | |
978 | y = bounds.size.height - (rect.origin.y + rect.size.height); | |
979 | left = x; | |
980 | top = y; | |
981 | width = rect.size.width; | |
982 | height = rect.size.height; | |
983 | } | |
984 | else | |
985 | { | |
986 | left = top = 0; | |
987 | GetSize( width, height ); | |
988 | } | |
33e90275 SC |
989 | } |
990 | ||
991 | void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where ) | |
992 | { | |
993 | if ( where ) | |
994 | [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )]; | |
995 | else | |
996 | [m_osxView setNeedsDisplay:YES]; | |
997 | } | |
998 | ||
999 | bool wxWidgetCocoaImpl::GetNeedsDisplay() const | |
1000 | { | |
1001 | return [m_osxView needsDisplay]; | |
1002 | } | |
1003 | ||
dbeddfb9 | 1004 | bool wxWidgetCocoaImpl::CanFocus() const |
33e90275 | 1005 | { |
dbeddfb9 | 1006 | return [m_osxView canBecomeKeyView] == YES; |
33e90275 SC |
1007 | } |
1008 | ||
1009 | bool wxWidgetCocoaImpl::HasFocus() const | |
1010 | { | |
dbeddfb9 | 1011 | return ( [[m_osxView window] firstResponder] == m_osxView ); |
33e90275 SC |
1012 | } |
1013 | ||
1014 | bool wxWidgetCocoaImpl::SetFocus() | |
1015 | { | |
dbeddfb9 SC |
1016 | if ( [m_osxView canBecomeKeyView] == NO ) |
1017 | return false; | |
1018 | ||
1019 | [[m_osxView window] makeFirstResponder: m_osxView] ; | |
1020 | return true; | |
33e90275 SC |
1021 | } |
1022 | ||
1023 | ||
1024 | void wxWidgetCocoaImpl::RemoveFromParent() | |
1025 | { | |
1026 | [m_osxView removeFromSuperview]; | |
1027 | } | |
1028 | ||
1029 | void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent ) | |
1030 | { | |
1031 | NSView* container = parent->GetWXWidget() ; | |
1032 | wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ; | |
1033 | [container addSubview:m_osxView]; | |
1034 | } | |
1035 | ||
dbeddfb9 SC |
1036 | void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) ) |
1037 | { | |
1038 | // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()]; | |
1039 | } | |
1040 | ||
1041 | void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding ) | |
1042 | { | |
1043 | if ( [m_osxView respondsToSelector:@selector(setTitle:) ] ) | |
1044 | { | |
1045 | wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() ); | |
1046 | [m_osxView setTitle:cf.AsNSString()]; | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | ||
1051 | void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to ) | |
1052 | { | |
1053 | NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt ); | |
1054 | p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; | |
1055 | *pt = wxFromNSPoint( to->GetWXWidget(), p ); | |
1056 | } | |
1057 | ||
1058 | wxInt32 wxWidgetCocoaImpl::GetValue() const | |
1059 | { | |
1060 | return [(NSControl*)m_osxView intValue]; | |
1061 | } | |
1062 | ||
1063 | void wxWidgetCocoaImpl::SetValue( wxInt32 v ) | |
1064 | { | |
1065 | if ( [m_osxView respondsToSelector:@selector(setIntValue:)] ) | |
1066 | { | |
1067 | [m_osxView setIntValue:v]; | |
1068 | } | |
1069 | else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] ) | |
1070 | { | |
1071 | [m_osxView setFloatValue:(double)v]; | |
1072 | } | |
1073 | else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] ) | |
1074 | { | |
1075 | [m_osxView setDoubleValue:(double)v]; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | void wxWidgetCocoaImpl::SetMinimum( wxInt32 v ) | |
1080 | { | |
1081 | if ( [m_osxView respondsToSelector:@selector(setMinValue:)] ) | |
1082 | { | |
1083 | [m_osxView setMinValue:(double)v]; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) | |
1088 | { | |
1089 | if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] ) | |
1090 | { | |
1091 | [m_osxView setMaxValue:(double)v]; | |
1092 | } | |
1093 | } | |
1094 | ||
19c7ac3d SC |
1095 | wxInt32 wxWidgetCocoaImpl::GetMinimum() const |
1096 | { | |
1097 | if ( [m_osxView respondsToSelector:@selector(getMinValue:)] ) | |
1098 | { | |
1099 | return [m_osxView minValue]; | |
1100 | } | |
1101 | return 0; | |
1102 | } | |
1103 | ||
1104 | wxInt32 wxWidgetCocoaImpl::GetMaximum() const | |
1105 | { | |
1106 | if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] ) | |
1107 | { | |
1108 | return [m_osxView maxValue]; | |
1109 | } | |
1110 | return 0; | |
1111 | } | |
1112 | ||
dbeddfb9 SC |
1113 | void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) |
1114 | { | |
1115 | if ( [m_osxView respondsToSelector:@selector(setImage:)] ) | |
1116 | { | |
1117 | [m_osxView setImage:bitmap.GetNSImage()]; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook) | |
1122 | { | |
1123 | // implementation in subclass | |
1124 | } | |
1125 | ||
1126 | void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const | |
1127 | { | |
1128 | r->x = r->y = r->width = r->height = 0; | |
1129 | // if ( [m_osxView isKindOfClass:[NSControl class]] ) | |
1130 | if ( [m_osxView respondsToSelector:@selector(sizeToFit)] ) | |
1131 | { | |
1132 | NSRect former = [m_osxView frame]; | |
1133 | [m_osxView sizeToFit]; | |
1134 | NSRect best = [m_osxView frame]; | |
1135 | [m_osxView setFrame:former]; | |
1136 | r->width = best.size.width; | |
1137 | r->height = best.size.height; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | bool wxWidgetCocoaImpl::IsEnabled() const | |
1142 | { | |
0f9b48d1 SC |
1143 | if ( [m_osxView respondsToSelector:@selector(isEnabled) ] ) |
1144 | return [m_osxView isEnabled]; | |
1145 | return true; | |
dbeddfb9 SC |
1146 | } |
1147 | ||
1148 | void wxWidgetCocoaImpl::Enable( bool enable ) | |
1149 | { | |
0f9b48d1 SC |
1150 | if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] ) |
1151 | [m_osxView setEnabled:enable]; | |
dbeddfb9 SC |
1152 | } |
1153 | ||
1154 | void wxWidgetCocoaImpl::PulseGauge() | |
1155 | { | |
1156 | } | |
1157 | ||
1158 | void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view ) | |
1159 | { | |
1160 | } | |
33e90275 | 1161 | |
0f9b48d1 SC |
1162 | void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) |
1163 | { | |
1164 | NSControlSize size = NSRegularControlSize; | |
1165 | ||
1166 | switch ( variant ) | |
1167 | { | |
1168 | case wxWINDOW_VARIANT_NORMAL : | |
1169 | size = NSRegularControlSize; | |
1170 | break ; | |
1171 | ||
1172 | case wxWINDOW_VARIANT_SMALL : | |
1173 | size = NSSmallControlSize; | |
1174 | break ; | |
1175 | ||
1176 | case wxWINDOW_VARIANT_MINI : | |
1177 | size = NSMiniControlSize; | |
1178 | break ; | |
1179 | ||
1180 | case wxWINDOW_VARIANT_LARGE : | |
1181 | size = NSRegularControlSize; | |
1182 | break ; | |
1183 | ||
1184 | default: | |
1185 | wxFAIL_MSG(_T("unexpected window variant")); | |
1186 | break ; | |
1187 | } | |
1188 | if ( [m_osxView respondsToSelector:@selector(setControlSize:)] ) | |
1189 | [m_osxView setControlSize:size]; | |
1190 | } | |
1191 | ||
1e181c7a SC |
1192 | void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool) |
1193 | { | |
1194 | // TODO | |
1195 | } | |
1196 | ||
c4825ef7 SC |
1197 | void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) |
1198 | { | |
4dd9fdf8 SC |
1199 | WXWidget c = control ? control : (WXWidget) m_osxView; |
1200 | wxWidgetImpl::Associate( c, this ) ; | |
1201 | if ([c respondsToSelector:@selector(setAction:)]) | |
1202 | { | |
1203 | [c setTarget: c]; | |
1204 | [c setAction: @selector(clickedAction:)]; | |
1205 | if ([c respondsToSelector:@selector(setDoubleAction:)]) | |
1206 | { | |
1207 | [c setDoubleAction: @selector(doubleClickedAction:)]; | |
1208 | } | |
1209 | ||
1210 | } | |
c4825ef7 SC |
1211 | } |
1212 | ||
ffad7b0d SC |
1213 | bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) |
1214 | { | |
1215 | wxKeyEvent wxevent(wxEVT_KEY_DOWN); | |
1216 | SetupKeyEvent( wxevent, event ); | |
19c7ac3d | 1217 | |
215bd0e1 | 1218 | return GetWXPeer()->OSXHandleKeyEvent(wxevent); |
ffad7b0d SC |
1219 | } |
1220 | ||
b466e85a | 1221 | bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) |
4850cc8b SC |
1222 | { |
1223 | NSPoint clickLocation; | |
1224 | clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; | |
1225 | wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); | |
1226 | wxMouseEvent wxevent(wxEVT_LEFT_DOWN); | |
1227 | SetupMouseEvent( wxevent , event ) ; | |
1228 | wxevent.m_x = pt.x; | |
1229 | wxevent.m_y = pt.y; | |
b466e85a SC |
1230 | |
1231 | return GetWXPeer()->HandleWindowEvent(wxevent); | |
4850cc8b SC |
1232 | } |
1233 | ||
0c530e5a SC |
1234 | void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) |
1235 | { | |
1236 | wxWindow* thisWindow = GetWXPeer(); | |
1237 | if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() ) | |
1238 | { | |
1239 | thisWindow->MacInvalidateBorders(); | |
1240 | } | |
1241 | ||
1242 | if ( receivedFocus ) | |
1243 | { | |
1244 | wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow)); | |
1245 | wxChildFocusEvent eventFocus((wxWindow*)thisWindow); | |
1246 | thisWindow->HandleWindowEvent(eventFocus); | |
1247 | ||
1248 | #if wxUSE_CARET | |
1249 | if ( thisWindow->GetCaret() ) | |
1250 | thisWindow->GetCaret()->OnSetFocus(); | |
1251 | #endif | |
1252 | ||
1253 | wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId()); | |
1254 | event.SetEventObject(thisWindow); | |
1255 | // TODO how to find out the targetFocusWindow ? | |
1256 | // event.SetWindow(targetFocusWindow); | |
1257 | thisWindow->HandleWindowEvent(event) ; | |
1258 | } | |
1259 | else // !receivedFocuss | |
1260 | { | |
1261 | #if wxUSE_CARET | |
1262 | if ( thisWindow->GetCaret() ) | |
1263 | thisWindow->GetCaret()->OnKillFocus(); | |
1264 | #endif | |
1265 | ||
1266 | wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow)); | |
1267 | ||
1268 | wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); | |
1269 | event.SetEventObject(thisWindow); | |
1270 | // TODO how to find out the targetFocusWindow ? | |
1271 | // event.SetWindow(targetFocusWindow); | |
1272 | thisWindow->HandleWindowEvent(event) ; | |
1273 | } | |
1274 | } | |
1275 | ||
54f11060 SC |
1276 | void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor) |
1277 | { | |
1278 | NSPoint location = [NSEvent mouseLocation]; | |
1279 | location = [[m_osxView window] convertScreenToBase:location]; | |
1280 | NSPoint locationInView = [m_osxView convertPoint:location fromView:nil]; | |
1281 | ||
1282 | if( NSMouseInRect(locationInView, [m_osxView bounds], YES) ) | |
1283 | { | |
1284 | [(NSCursor*)cursor.GetHCURSOR() set]; | |
1285 | } | |
1286 | [[m_osxView window] invalidateCursorRectsForView:m_osxView]; | |
1287 | } | |
1288 | ||
1289 | void wxWidgetCocoaImpl::CaptureMouse() | |
1290 | { | |
1291 | [[m_osxView window] disableCursorRects]; | |
1292 | } | |
1293 | ||
1294 | void wxWidgetCocoaImpl::ReleaseMouse() | |
1295 | { | |
1296 | [[m_osxView window] enableCursorRects]; | |
1297 | } | |
4850cc8b | 1298 | |
4dd9fdf8 SC |
1299 | void wxWidgetCocoaImpl::SetFlipped(bool flipped) |
1300 | { | |
1301 | m_isFlipped = flipped; | |
1302 | } | |
1303 | ||
33e90275 SC |
1304 | // |
1305 | // Factory methods | |
1306 | // | |
1307 | ||
dbeddfb9 SC |
1308 | wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, |
1309 | long style, long extraStyle) | |
33e90275 | 1310 | { |
dbeddfb9 | 1311 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
33e90275 | 1312 | wxNSView* v = [[wxNSView alloc] initWithFrame:r]; |
4dd9fdf8 SC |
1313 | |
1314 | // temporary hook for dnd | |
1315 | [v registerForDraggedTypes:[NSArray arrayWithObjects: | |
1316 | NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; | |
1317 | ||
33e90275 | 1318 | wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); |
33e90275 SC |
1319 | return c; |
1320 | } | |
1321 | ||
1322 | wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) | |
1323 | { | |
1324 | NSWindow* tlw = now->GetWXWindow(); | |
1325 | wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]]; | |
1326 | wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true ); | |
33e90275 SC |
1327 | [tlw setContentView:v]; |
1328 | return c; | |
1329 | } |