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