]>
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 |
c583fa5a SC |
15 | #include "wx/nonownedwnd.h" |
16 | #include "wx/log.h" | |
05cf95ed | 17 | #endif |
33e90275 SC |
18 | |
19 | #ifdef __WXMAC__ | |
20 | #include "wx/osx/private.h" | |
21 | #endif | |
22 | ||
0c530e5a SC |
23 | #if wxUSE_CARET |
24 | #include "wx/caret.h" | |
25 | #endif | |
26 | ||
dbeddfb9 SC |
27 | NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) |
28 | { | |
29 | int x, y, w, h ; | |
30 | ||
31 | window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ; | |
32 | wxRect bounds(x,y,w,h); | |
33 | NSView* sv = (window->GetParent()->GetHandle() ); | |
34 | ||
35 | return wxToNSRect( sv, bounds ); | |
36 | } | |
33e90275 SC |
37 | |
38 | @interface wxNSView : NSView | |
39 | { | |
b466e85a | 40 | WXCOCOAIMPL_COMMON_MEMBERS |
33e90275 SC |
41 | } |
42 | ||
43 | - (void)drawRect: (NSRect) rect; | |
44 | ||
b466e85a SC |
45 | WXCOCOAIMPL_COMMON_INTERFACE |
46 | ||
524c47aa | 47 | - (BOOL) canBecomeKeyView; |
33e90275 SC |
48 | |
49 | @end // wxNSView | |
50 | ||
ffad7b0d SC |
51 | @interface NSView(PossibleMethods) |
52 | - (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation; | |
53 | - (void)setTitle:(NSString *)aString; | |
54 | - (void)setStringValue:(NSString *)aString; | |
55 | - (void)setIntValue:(int)anInt; | |
56 | - (void)setFloatValue:(float)aFloat; | |
57 | - (void)setDoubleValue:(double)aDouble; | |
58 | ||
19c7ac3d SC |
59 | - (double)minValue; |
60 | - (double)maxValue; | |
ffad7b0d SC |
61 | - (void)setMinValue:(double)aDouble; |
62 | - (void)setMaxValue:(double)aDouble; | |
63 | ||
64 | - (void)sizeToFit; | |
65 | ||
66 | - (BOOL)isEnabled; | |
67 | - (void)setEnabled:(BOOL)flag; | |
68 | ||
69 | - (void)setImage:(NSImage *)image; | |
70 | - (void)setControlSize:(NSControlSize)size; | |
0c530e5a SC |
71 | |
72 | - (id)contentView; | |
ffad7b0d SC |
73 | @end |
74 | ||
19c7ac3d | 75 | long wxOSXTranslateCocoaKey( int unichar ) |
524c47aa | 76 | { |
19c7ac3d | 77 | long retval = unichar; |
524c47aa SC |
78 | switch( unichar ) |
79 | { | |
80 | case NSUpArrowFunctionKey : | |
81 | retval = WXK_UP; | |
82 | break; | |
83 | case NSDownArrowFunctionKey : | |
84 | retval = WXK_DOWN; | |
85 | break; | |
86 | case NSLeftArrowFunctionKey : | |
87 | retval = WXK_LEFT; | |
88 | break; | |
89 | case NSRightArrowFunctionKey : | |
90 | retval = WXK_RIGHT; | |
91 | break; | |
92 | case NSInsertFunctionKey : | |
93 | retval = WXK_INSERT; | |
94 | break; | |
95 | case NSDeleteFunctionKey : | |
96 | retval = WXK_DELETE; | |
97 | break; | |
98 | case NSHomeFunctionKey : | |
99 | retval = WXK_HOME; | |
100 | break; | |
101 | // case NSBeginFunctionKey : | |
102 | // retval = WXK_BEGIN; | |
103 | // break; | |
104 | case NSEndFunctionKey : | |
105 | retval = WXK_END; | |
106 | break; | |
107 | case NSPageUpFunctionKey : | |
108 | retval = WXK_PAGEUP; | |
109 | break; | |
110 | case NSPageDownFunctionKey : | |
111 | retval = WXK_PAGEDOWN; | |
112 | break; | |
113 | case NSHelpFunctionKey : | |
114 | retval = WXK_HELP; | |
115 | break; | |
116 | ||
117 | default : | |
118 | if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey ) | |
119 | retval = WXK_F1 + (unichar - NSF1FunctionKey ); | |
120 | break; | |
121 | } | |
122 | return retval; | |
123 | } | |
124 | ||
125 | void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent ) | |
126 | { | |
127 | UInt32 modifiers = [nsEvent modifierFlags] ; | |
fc39cf72 | 128 | int eventType = [nsEvent type]; |
524c47aa SC |
129 | |
130 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; | |
131 | wxevent.m_controlDown = modifiers & NSControlKeyMask; | |
132 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; | |
133 | wxevent.m_metaDown = modifiers & NSCommandKeyMask; | |
524c47aa | 134 | |
19c7ac3d | 135 | wxevent.m_rawCode = [nsEvent keyCode]; |
524c47aa SC |
136 | wxevent.m_rawFlags = modifiers; |
137 | ||
138 | wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; | |
524c47aa SC |
139 | switch (eventType) |
140 | { | |
141 | case NSKeyDown : | |
142 | wxevent.SetEventType( wxEVT_KEY_DOWN ) ; | |
143 | break; | |
144 | case NSKeyUp : | |
145 | wxevent.SetEventType( wxEVT_KEY_UP ) ; | |
146 | break; | |
147 | case NSFlagsChanged : | |
148 | // setup common code here | |
149 | break; | |
150 | default : | |
151 | break ; | |
152 | } | |
19c7ac3d SC |
153 | |
154 | wxString chars; | |
155 | if ( eventType != NSFlagsChanged ) | |
156 | { | |
157 | NSString* nschars = [nsEvent characters]; | |
158 | if ( nschars ) | |
159 | { | |
160 | wxCFStringRef cfchars((CFStringRef)[nschars retain]); | |
161 | chars = cfchars.AsString(); | |
162 | } | |
163 | } | |
164 | ||
165 | int unichar = chars.Length() > 0 ? chars[0] : 0; | |
166 | long keyval = wxOSXTranslateCocoaKey(unichar) ; | |
167 | if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) ) | |
168 | keyval = wxToupper( keyval ) ; | |
169 | ||
170 | #if wxUSE_UNICODE | |
171 | wxevent.m_uniChar = unichar; | |
172 | #endif | |
173 | wxevent.m_keyCode = keyval; | |
524c47aa SC |
174 | } |
175 | ||
54f11060 SC |
176 | UInt32 g_lastButton = 0 ; |
177 | bool g_lastButtonWasFakeRight = false ; | |
178 | ||
33e90275 SC |
179 | void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) |
180 | { | |
181 | UInt32 modifiers = [nsEvent modifierFlags] ; | |
182 | wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]); | |
183 | ||
184 | // these parameters are not given for all events | |
185 | UInt32 button = [nsEvent buttonNumber]; | |
186 | UInt32 clickCount = [nsEvent clickCount]; | |
33e90275 SC |
187 | |
188 | wxevent.m_x = screenMouseLocation.x; | |
189 | wxevent.m_y = screenMouseLocation.y; | |
190 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; | |
191 | wxevent.m_controlDown = modifiers & NSControlKeyMask; | |
192 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; | |
193 | wxevent.m_metaDown = modifiers & NSCommandKeyMask; | |
194 | wxevent.m_clickCount = clickCount; | |
dbeddfb9 | 195 | wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; |
54f11060 SC |
196 | |
197 | UInt32 mouseChord = 0; | |
198 | int eventType = [nsEvent type]; | |
199 | ||
200 | switch (eventType) | |
201 | { | |
202 | case NSLeftMouseDown : | |
203 | case NSLeftMouseDragged : | |
204 | mouseChord = 1U; | |
205 | break; | |
206 | case NSRightMouseDown : | |
207 | case NSRightMouseDragged : | |
208 | mouseChord = 2U; | |
209 | break; | |
210 | case NSOtherMouseDown : | |
211 | case NSOtherMouseDragged : | |
212 | mouseChord = 4U; | |
213 | break; | |
214 | } | |
215 | ||
33e90275 SC |
216 | // a control click is interpreted as a right click |
217 | bool thisButtonIsFakeRight = false ; | |
54f11060 | 218 | if ( button == 0 && (modifiers & NSControlKeyMask) ) |
33e90275 | 219 | { |
54f11060 | 220 | button = 1 ; |
33e90275 SC |
221 | thisButtonIsFakeRight = true ; |
222 | } | |
223 | ||
224 | // otherwise we report double clicks by connecting a left click with a ctrl-left click | |
225 | if ( clickCount > 1 && button != g_lastButton ) | |
226 | clickCount = 1 ; | |
54f11060 | 227 | |
33e90275 SC |
228 | // we must make sure that our synthetic 'right' button corresponds in |
229 | // mouse down, moved and mouse up, and does not deliver a right down and left up | |
54f11060 | 230 | switch (eventType) |
33e90275 | 231 | { |
54f11060 SC |
232 | case NSLeftMouseDown : |
233 | case NSRightMouseDown : | |
234 | case NSOtherMouseDown : | |
235 | g_lastButton = button ; | |
236 | g_lastButtonWasFakeRight = thisButtonIsFakeRight ; | |
237 | break; | |
238 | } | |
33e90275 SC |
239 | |
240 | if ( button == 0 ) | |
241 | { | |
242 | g_lastButton = 0 ; | |
243 | g_lastButtonWasFakeRight = false ; | |
244 | } | |
54f11060 | 245 | else if ( g_lastButton == 1 && g_lastButtonWasFakeRight ) |
33e90275 SC |
246 | button = g_lastButton ; |
247 | ||
248 | // Adjust the chord mask to remove the primary button and add the | |
249 | // secondary button. It is possible that the secondary button is | |
250 | // already pressed, e.g. on a mouse connected to a laptop, but this | |
251 | // possibility is ignored here: | |
252 | if( thisButtonIsFakeRight && ( mouseChord & 1U ) ) | |
253 | mouseChord = ((mouseChord & ~1U) | 2U); | |
254 | ||
255 | if(mouseChord & 1U) | |
256 | wxevent.m_leftDown = true ; | |
257 | if(mouseChord & 2U) | |
258 | wxevent.m_rightDown = true ; | |
259 | if(mouseChord & 4U) | |
260 | wxevent.m_middleDown = true ; | |
261 | ||
33e90275 | 262 | // translate into wx types |
33e90275 SC |
263 | switch (eventType) |
264 | { | |
265 | case NSLeftMouseDown : | |
266 | case NSRightMouseDown : | |
267 | case NSOtherMouseDown : | |
268 | switch ( button ) | |
269 | { | |
270 | case 0 : | |
271 | wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ; | |
272 | break ; | |
273 | ||
274 | case 1 : | |
275 | wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ; | |
276 | break ; | |
277 | ||
278 | case 2 : | |
279 | wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ; | |
280 | break ; | |
281 | ||
282 | default: | |
283 | break ; | |
284 | } | |
285 | break ; | |
286 | ||
287 | case NSLeftMouseUp : | |
288 | case NSRightMouseUp : | |
289 | case NSOtherMouseUp : | |
290 | switch ( button ) | |
291 | { | |
292 | case 0 : | |
293 | wxevent.SetEventType( wxEVT_LEFT_UP ) ; | |
294 | break ; | |
295 | ||
296 | case 1 : | |
297 | wxevent.SetEventType( wxEVT_RIGHT_UP ) ; | |
298 | break ; | |
299 | ||
300 | case 2 : | |
301 | wxevent.SetEventType( wxEVT_MIDDLE_UP ) ; | |
302 | break ; | |
303 | ||
304 | default: | |
305 | break ; | |
306 | } | |
307 | break ; | |
308 | ||
309 | case NSScrollWheel : | |
310 | { | |
311 | wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ; | |
312 | /* | |
313 | EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ; | |
314 | SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ; | |
315 | */ | |
316 | wxevent.m_wheelRotation = 10; // delta; | |
317 | wxevent.m_wheelDelta = 1; | |
318 | wxevent.m_linesPerAction = 1; | |
319 | if ( 0 /* axis == kEventMouseWheelAxisX*/ ) | |
320 | wxevent.m_wheelAxis = 1; | |
321 | } | |
322 | break ; | |
323 | ||
324 | case NSMouseEntered : | |
325 | case NSMouseExited : | |
326 | case NSLeftMouseDragged : | |
327 | case NSRightMouseDragged : | |
328 | case NSOtherMouseDragged : | |
329 | case NSMouseMoved : | |
330 | wxevent.SetEventType( wxEVT_MOTION ) ; | |
331 | break; | |
332 | default : | |
333 | break ; | |
334 | } | |
335 | } | |
336 | ||
337 | @implementation wxNSView | |
338 | ||
dbeddfb9 SC |
339 | #define OSX_DEBUG_DRAWING 0 |
340 | ||
33e90275 SC |
341 | - (void)drawRect: (NSRect) rect |
342 | { | |
dbeddfb9 | 343 | if ( impl ) |
33e90275 SC |
344 | { |
345 | CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
346 | CGContextSaveGState( context ); | |
dbeddfb9 | 347 | #if OSX_DEBUG_DRAWING |
33e90275 SC |
348 | CGContextBeginPath( context ); |
349 | CGContextMoveToPoint(context, 0, 0); | |
350 | NSRect bounds = [self bounds]; | |
351 | CGContextAddLineToPoint(context, 10, 0); | |
352 | CGContextMoveToPoint(context, 0, 0); | |
353 | CGContextAddLineToPoint(context, 0, 10); | |
354 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
355 | CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10); | |
356 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
357 | CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height); | |
358 | CGContextClosePath( context ); | |
359 | CGContextStrokePath(context); | |
dbeddfb9 | 360 | #endif |
33e90275 SC |
361 | |
362 | if ( [ self isFlipped ] == NO ) | |
363 | { | |
364 | CGContextTranslateCTM( context, 0, [self bounds].size.height ); | |
365 | CGContextScaleCTM( context, 1, -1 ); | |
366 | } | |
dbeddfb9 SC |
367 | |
368 | wxRegion updateRgn; | |
369 | const NSRect *rects; | |
5bf56597 | 370 | NSInteger count; |
dbeddfb9 SC |
371 | |
372 | [self getRectsBeingDrawn:&rects count:&count]; | |
373 | for ( int i = 0 ; i < count ; ++i ) | |
374 | { | |
375 | updateRgn.Union(wxFromNSRect(self, rects[i]) ); | |
376 | } | |
33e90275 | 377 | |
dbeddfb9 SC |
378 | wxWindow* wxpeer = impl->GetWXPeer(); |
379 | wxpeer->GetUpdateRegion() = updateRgn; | |
380 | wxpeer->MacSetCGContextRef( context ); | |
381 | ||
33e90275 SC |
382 | wxPaintEvent event; |
383 | event.SetTimestamp(0); // todo | |
dbeddfb9 SC |
384 | event.SetEventObject(wxpeer); |
385 | wxpeer->HandleWindowEvent(event); | |
386 | ||
33e90275 SC |
387 | CGContextRestoreGState( context ); |
388 | } | |
389 | } | |
390 | ||
b466e85a | 391 | WXCOCOAIMPL_COMMON_IMPLEMENTATION |
33e90275 | 392 | |
524c47aa SC |
393 | - (BOOL) canBecomeKeyView |
394 | { | |
395 | return YES; | |
396 | } | |
33e90275 SC |
397 | |
398 | @end // wxNSView | |
399 | ||
400 | IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl ) | |
401 | ||
402 | wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) : | |
403 | wxWidgetImpl( peer, isRootControl ), m_osxView(w) | |
404 | { | |
405 | } | |
406 | ||
407 | wxWidgetCocoaImpl::wxWidgetCocoaImpl() | |
408 | { | |
409 | } | |
410 | ||
411 | void wxWidgetCocoaImpl::Init() | |
412 | { | |
413 | m_osxView = NULL; | |
414 | } | |
415 | ||
416 | wxWidgetCocoaImpl::~wxWidgetCocoaImpl() | |
417 | { | |
dbeddfb9 SC |
418 | if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] ) |
419 | [m_osxView setImplementation:NULL]; | |
420 | if ( !IsRootControl() ) | |
421 | { | |
422 | NSView *sv = [m_osxView superview]; | |
423 | if ( sv != nil ) | |
424 | [m_osxView removeFromSuperview]; | |
425 | } | |
33e90275 SC |
426 | [m_osxView release]; |
427 | } | |
428 | ||
429 | bool wxWidgetCocoaImpl::IsVisible() const | |
430 | { | |
431 | return [m_osxView isHiddenOrHasHiddenAncestor] == NO; | |
432 | } | |
433 | ||
dbeddfb9 SC |
434 | void wxWidgetCocoaImpl::SetVisibility( bool visible ) |
435 | { | |
436 | [m_osxView setHidden:(visible ? NO:YES)]; | |
437 | } | |
438 | ||
33e90275 SC |
439 | void wxWidgetCocoaImpl::Raise() |
440 | { | |
441 | } | |
442 | ||
443 | void wxWidgetCocoaImpl::Lower() | |
444 | { | |
445 | } | |
446 | ||
447 | void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy ) | |
448 | { | |
54f11060 SC |
449 | #if 1 |
450 | SetNeedsDisplay() ; | |
451 | #else | |
452 | // We should do something like this, but it wasn't working in 10.4. | |
453 | if (GetNeedsDisplay() ) | |
454 | { | |
455 | SetNeedsDisplay() ; | |
456 | } | |
457 | NSRect r = wxToNSRect( [m_osxView superview], *rect ); | |
458 | NSSize offset = NSMakeSize((float)dx, (float)dy); | |
459 | [m_osxView scrollRect:r by:offset]; | |
460 | #endif | |
33e90275 SC |
461 | } |
462 | ||
463 | void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) | |
464 | { | |
0c530e5a SC |
465 | wxWindowMac* parent = GetWXPeer()->GetParent(); |
466 | // under Cocoa we might have a contentView in the wxParent to which we have to | |
467 | // adjust the coordinates | |
468 | if (parent) | |
469 | { | |
470 | wxPoint pt(parent->GetClientAreaOrigin()); | |
471 | x -= pt.x; | |
472 | y -= pt.y; | |
473 | } | |
33e90275 SC |
474 | NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) ); |
475 | [m_osxView setFrame:r]; | |
476 | } | |
477 | ||
478 | void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const | |
479 | { | |
480 | wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] ); | |
481 | x = r.GetLeft(); | |
482 | y = r.GetTop(); | |
483 | } | |
484 | ||
485 | void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const | |
486 | { | |
487 | NSRect rect = [m_osxView frame]; | |
488 | width = rect.size.width; | |
489 | height = rect.size.height; | |
490 | } | |
491 | ||
dbeddfb9 | 492 | void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const |
33e90275 | 493 | { |
0c530e5a SC |
494 | if ( [m_osxView respondsToSelector:@selector(contentView) ] ) |
495 | { | |
496 | NSView* cv = [m_osxView contentView]; | |
497 | ||
498 | NSRect bounds = [m_osxView bounds]; | |
499 | NSRect rect = [cv frame]; | |
500 | ||
501 | int y = rect.origin.y; | |
502 | int x = rect.origin.x; | |
503 | if ( ![ m_osxView isFlipped ] ) | |
504 | y = bounds.size.height - (rect.origin.y + rect.size.height); | |
505 | left = x; | |
506 | top = y; | |
507 | width = rect.size.width; | |
508 | height = rect.size.height; | |
509 | } | |
510 | else | |
511 | { | |
512 | left = top = 0; | |
513 | GetSize( width, height ); | |
514 | } | |
33e90275 SC |
515 | } |
516 | ||
517 | void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where ) | |
518 | { | |
519 | if ( where ) | |
520 | [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )]; | |
521 | else | |
522 | [m_osxView setNeedsDisplay:YES]; | |
523 | } | |
524 | ||
525 | bool wxWidgetCocoaImpl::GetNeedsDisplay() const | |
526 | { | |
527 | return [m_osxView needsDisplay]; | |
528 | } | |
529 | ||
dbeddfb9 | 530 | bool wxWidgetCocoaImpl::CanFocus() const |
33e90275 | 531 | { |
dbeddfb9 | 532 | return [m_osxView canBecomeKeyView] == YES; |
33e90275 SC |
533 | } |
534 | ||
535 | bool wxWidgetCocoaImpl::HasFocus() const | |
536 | { | |
dbeddfb9 | 537 | return ( [[m_osxView window] firstResponder] == m_osxView ); |
33e90275 SC |
538 | } |
539 | ||
540 | bool wxWidgetCocoaImpl::SetFocus() | |
541 | { | |
dbeddfb9 SC |
542 | if ( [m_osxView canBecomeKeyView] == NO ) |
543 | return false; | |
544 | ||
545 | [[m_osxView window] makeFirstResponder: m_osxView] ; | |
546 | return true; | |
33e90275 SC |
547 | } |
548 | ||
549 | ||
550 | void wxWidgetCocoaImpl::RemoveFromParent() | |
551 | { | |
552 | [m_osxView removeFromSuperview]; | |
553 | } | |
554 | ||
555 | void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent ) | |
556 | { | |
557 | NSView* container = parent->GetWXWidget() ; | |
558 | wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ; | |
559 | [container addSubview:m_osxView]; | |
560 | } | |
561 | ||
dbeddfb9 SC |
562 | void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) ) |
563 | { | |
564 | // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()]; | |
565 | } | |
566 | ||
567 | void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding ) | |
568 | { | |
569 | if ( [m_osxView respondsToSelector:@selector(setTitle:) ] ) | |
570 | { | |
571 | wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() ); | |
572 | [m_osxView setTitle:cf.AsNSString()]; | |
573 | } | |
574 | } | |
575 | ||
576 | ||
577 | void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to ) | |
578 | { | |
579 | NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt ); | |
580 | p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; | |
581 | *pt = wxFromNSPoint( to->GetWXWidget(), p ); | |
582 | } | |
583 | ||
584 | wxInt32 wxWidgetCocoaImpl::GetValue() const | |
585 | { | |
586 | return [(NSControl*)m_osxView intValue]; | |
587 | } | |
588 | ||
589 | void wxWidgetCocoaImpl::SetValue( wxInt32 v ) | |
590 | { | |
591 | if ( [m_osxView respondsToSelector:@selector(setIntValue:)] ) | |
592 | { | |
593 | [m_osxView setIntValue:v]; | |
594 | } | |
595 | else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] ) | |
596 | { | |
597 | [m_osxView setFloatValue:(double)v]; | |
598 | } | |
599 | else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] ) | |
600 | { | |
601 | [m_osxView setDoubleValue:(double)v]; | |
602 | } | |
603 | } | |
604 | ||
605 | void wxWidgetCocoaImpl::SetMinimum( wxInt32 v ) | |
606 | { | |
607 | if ( [m_osxView respondsToSelector:@selector(setMinValue:)] ) | |
608 | { | |
609 | [m_osxView setMinValue:(double)v]; | |
610 | } | |
611 | } | |
612 | ||
613 | void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) | |
614 | { | |
615 | if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] ) | |
616 | { | |
617 | [m_osxView setMaxValue:(double)v]; | |
618 | } | |
619 | } | |
620 | ||
19c7ac3d SC |
621 | wxInt32 wxWidgetCocoaImpl::GetMinimum() const |
622 | { | |
623 | if ( [m_osxView respondsToSelector:@selector(getMinValue:)] ) | |
624 | { | |
625 | return [m_osxView minValue]; | |
626 | } | |
627 | return 0; | |
628 | } | |
629 | ||
630 | wxInt32 wxWidgetCocoaImpl::GetMaximum() const | |
631 | { | |
632 | if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] ) | |
633 | { | |
634 | return [m_osxView maxValue]; | |
635 | } | |
636 | return 0; | |
637 | } | |
638 | ||
dbeddfb9 SC |
639 | void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) |
640 | { | |
641 | if ( [m_osxView respondsToSelector:@selector(setImage:)] ) | |
642 | { | |
643 | [m_osxView setImage:bitmap.GetNSImage()]; | |
644 | } | |
645 | } | |
646 | ||
647 | void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook) | |
648 | { | |
649 | // implementation in subclass | |
650 | } | |
651 | ||
652 | void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const | |
653 | { | |
654 | r->x = r->y = r->width = r->height = 0; | |
655 | // if ( [m_osxView isKindOfClass:[NSControl class]] ) | |
656 | if ( [m_osxView respondsToSelector:@selector(sizeToFit)] ) | |
657 | { | |
658 | NSRect former = [m_osxView frame]; | |
659 | [m_osxView sizeToFit]; | |
660 | NSRect best = [m_osxView frame]; | |
661 | [m_osxView setFrame:former]; | |
662 | r->width = best.size.width; | |
663 | r->height = best.size.height; | |
664 | } | |
665 | } | |
666 | ||
667 | bool wxWidgetCocoaImpl::IsEnabled() const | |
668 | { | |
0f9b48d1 SC |
669 | if ( [m_osxView respondsToSelector:@selector(isEnabled) ] ) |
670 | return [m_osxView isEnabled]; | |
671 | return true; | |
dbeddfb9 SC |
672 | } |
673 | ||
674 | void wxWidgetCocoaImpl::Enable( bool enable ) | |
675 | { | |
0f9b48d1 SC |
676 | if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] ) |
677 | [m_osxView setEnabled:enable]; | |
dbeddfb9 SC |
678 | } |
679 | ||
680 | void wxWidgetCocoaImpl::PulseGauge() | |
681 | { | |
682 | } | |
683 | ||
684 | void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view ) | |
685 | { | |
686 | } | |
33e90275 | 687 | |
0f9b48d1 SC |
688 | void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) |
689 | { | |
690 | NSControlSize size = NSRegularControlSize; | |
691 | ||
692 | switch ( variant ) | |
693 | { | |
694 | case wxWINDOW_VARIANT_NORMAL : | |
695 | size = NSRegularControlSize; | |
696 | break ; | |
697 | ||
698 | case wxWINDOW_VARIANT_SMALL : | |
699 | size = NSSmallControlSize; | |
700 | break ; | |
701 | ||
702 | case wxWINDOW_VARIANT_MINI : | |
703 | size = NSMiniControlSize; | |
704 | break ; | |
705 | ||
706 | case wxWINDOW_VARIANT_LARGE : | |
707 | size = NSRegularControlSize; | |
708 | break ; | |
709 | ||
710 | default: | |
711 | wxFAIL_MSG(_T("unexpected window variant")); | |
712 | break ; | |
713 | } | |
714 | if ( [m_osxView respondsToSelector:@selector(setControlSize:)] ) | |
715 | [m_osxView setControlSize:size]; | |
716 | } | |
717 | ||
1e181c7a SC |
718 | void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool) |
719 | { | |
720 | // TODO | |
721 | } | |
722 | ||
c4825ef7 SC |
723 | void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) |
724 | { | |
725 | } | |
726 | ||
ffad7b0d SC |
727 | bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) |
728 | { | |
729 | wxKeyEvent wxevent(wxEVT_KEY_DOWN); | |
730 | SetupKeyEvent( wxevent, event ); | |
19c7ac3d | 731 | |
215bd0e1 | 732 | return GetWXPeer()->OSXHandleKeyEvent(wxevent); |
ffad7b0d SC |
733 | } |
734 | ||
b466e85a | 735 | bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) |
4850cc8b SC |
736 | { |
737 | NSPoint clickLocation; | |
738 | clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; | |
739 | wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); | |
740 | wxMouseEvent wxevent(wxEVT_LEFT_DOWN); | |
741 | SetupMouseEvent( wxevent , event ) ; | |
742 | wxevent.m_x = pt.x; | |
743 | wxevent.m_y = pt.y; | |
b466e85a SC |
744 | |
745 | return GetWXPeer()->HandleWindowEvent(wxevent); | |
4850cc8b SC |
746 | } |
747 | ||
0c530e5a SC |
748 | void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) |
749 | { | |
750 | wxWindow* thisWindow = GetWXPeer(); | |
751 | if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() ) | |
752 | { | |
753 | thisWindow->MacInvalidateBorders(); | |
754 | } | |
755 | ||
756 | if ( receivedFocus ) | |
757 | { | |
758 | wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow)); | |
759 | wxChildFocusEvent eventFocus((wxWindow*)thisWindow); | |
760 | thisWindow->HandleWindowEvent(eventFocus); | |
761 | ||
762 | #if wxUSE_CARET | |
763 | if ( thisWindow->GetCaret() ) | |
764 | thisWindow->GetCaret()->OnSetFocus(); | |
765 | #endif | |
766 | ||
767 | wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId()); | |
768 | event.SetEventObject(thisWindow); | |
769 | // TODO how to find out the targetFocusWindow ? | |
770 | // event.SetWindow(targetFocusWindow); | |
771 | thisWindow->HandleWindowEvent(event) ; | |
772 | } | |
773 | else // !receivedFocuss | |
774 | { | |
775 | #if wxUSE_CARET | |
776 | if ( thisWindow->GetCaret() ) | |
777 | thisWindow->GetCaret()->OnKillFocus(); | |
778 | #endif | |
779 | ||
780 | wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow)); | |
781 | ||
782 | wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); | |
783 | event.SetEventObject(thisWindow); | |
784 | // TODO how to find out the targetFocusWindow ? | |
785 | // event.SetWindow(targetFocusWindow); | |
786 | thisWindow->HandleWindowEvent(event) ; | |
787 | } | |
788 | } | |
789 | ||
54f11060 SC |
790 | void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor) |
791 | { | |
792 | NSPoint location = [NSEvent mouseLocation]; | |
793 | location = [[m_osxView window] convertScreenToBase:location]; | |
794 | NSPoint locationInView = [m_osxView convertPoint:location fromView:nil]; | |
795 | ||
796 | if( NSMouseInRect(locationInView, [m_osxView bounds], YES) ) | |
797 | { | |
798 | [(NSCursor*)cursor.GetHCURSOR() set]; | |
799 | } | |
800 | [[m_osxView window] invalidateCursorRectsForView:m_osxView]; | |
801 | } | |
802 | ||
803 | void wxWidgetCocoaImpl::CaptureMouse() | |
804 | { | |
805 | [[m_osxView window] disableCursorRects]; | |
806 | } | |
807 | ||
808 | void wxWidgetCocoaImpl::ReleaseMouse() | |
809 | { | |
810 | [[m_osxView window] enableCursorRects]; | |
811 | } | |
4850cc8b | 812 | |
33e90275 SC |
813 | // |
814 | // Factory methods | |
815 | // | |
816 | ||
dbeddfb9 SC |
817 | wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, |
818 | long style, long extraStyle) | |
33e90275 SC |
819 | { |
820 | NSView* sv = (wxpeer->GetParent()->GetHandle() ); | |
821 | ||
dbeddfb9 | 822 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
33e90275 SC |
823 | wxNSView* v = [[wxNSView alloc] initWithFrame:r]; |
824 | [sv addSubview:v]; | |
825 | wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); | |
826 | [v setImplementation:c]; | |
827 | return c; | |
828 | } | |
829 | ||
830 | wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) | |
831 | { | |
832 | NSWindow* tlw = now->GetWXWindow(); | |
833 | wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]]; | |
834 | wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true ); | |
835 | [v setImplementation:c]; | |
836 | [tlw setContentView:v]; | |
837 | return c; | |
838 | } |