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