]> git.saurik.com Git - wxWidgets.git/blame - src/osx/iphone/window.mm
Fix typo that broke compilation and adjust the defines to avoid an empty #if.
[wxWidgets.git] / src / osx / iphone / window.mm
CommitLineData
c16b2153
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/iphone/window.mm
3// Purpose: widgets (non tlw) for iphone
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
14#include "wx/osx/private.h"
15
d3929256
SC
16#ifndef WX_PRECOMP
17 #include "wx/nonownedwnd.h"
18 #include "wx/frame.h"
19 #include "wx/event.h"
20 #include "wx/log.h"
21#endif
22
a7f2b179 23#include <objc/runtime.h>
d3929256
SC
24
25WXWidget wxWidgetImpl::FindFocus()
26{
27 UIView* focusedView = nil;
28 UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
29 if ( keyWindow != nil )
30 {
31 /*
32 NSResponder* responder = [keyWindow firstResponder];
03647350 33 if ( [responder isKindOfClass:[NSTextView class]] &&
d3929256
SC
34 [keyWindow fieldEditor:NO forObject:nil] != nil )
35 {
36 focusedView = [(NSTextView*)responder delegate];
37 }
38 else
39 {
40 if ( [responder isKindOfClass:[NSView class]] )
41 focusedView = (NSView*) responder;
42 }
43 */
44 }
45 return focusedView;
46}
47
48CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
49{
50 int x, y, w, h ;
51
52 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
53 wxRect bounds(x,y,w,h);
54 UIView* sv = (window->GetParent()->GetHandle() );
55
56 return wxToNSRect( sv, bounds );
57}
c16b2153
SC
58
59@interface wxUIView : UIView
60{
c16b2153
SC
61}
62
d3929256
SC
63@end // wxUIView
64
65@interface wxUIView(PossibleMethods)
66- (void)setTitle:(NSString *)title forState:(UIControlState)state;
67
c16b2153
SC
68- (void)drawRect: (CGRect) rect;
69
70- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
71- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
72- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
73- (void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event;
74
c16b2153
SC
75- (BOOL) becomeFirstResponder;
76- (BOOL) resignFirstResponder;
d3929256 77@end
c16b2153
SC
78
79@interface wxUIContentView : wxUIView
80{
81}
82
83@end
84
85@interface wxUIContentViewController : UIViewController
86{
87}
88
89@end
90
91//
92//
93//
94
95void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent )
96{
97 UInt32 modifiers = 0 ;
98 UITouch *touch = [touches anyObject];
99
100 // these parameters are not given for all events
03647350 101 UInt32 button = 0; // no secondary button
c16b2153
SC
102 UInt32 clickCount = [touch tapCount];
103 UInt32 mouseChord = 0; // TODO does this exist for cocoa
104
105 // will be overridden
106 wxevent.m_x = 0;
107 wxevent.m_y = 0;
108 wxevent.m_shiftDown = 0;
109 wxevent.m_controlDown = 0;
110 wxevent.m_altDown = 0;
111 wxevent.m_metaDown = 0;
112 wxevent.m_clickCount = clickCount;
113 wxevent.SetTimestamp( [touch timestamp] ) ;
114/*
115 // a control click is interpreted as a right click
116 bool thisButtonIsFakeRight = false ;
117 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
118 {
119 button = kEventMouseButtonSecondary ;
120 thisButtonIsFakeRight = true ;
121 }
122
123 // otherwise we report double clicks by connecting a left click with a ctrl-left click
124 if ( clickCount > 1 && button != g_lastButton )
125 clickCount = 1 ;
126 // we must make sure that our synthetic 'right' button corresponds in
127 // mouse down, moved and mouse up, and does not deliver a right down and left up
128
129 if ( cEvent.GetKind() == kEventMouseDown )
130 {
131 g_lastButton = button ;
132 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
133 }
134
135 if ( button == 0 )
136 {
137 g_lastButton = 0 ;
138 g_lastButtonWasFakeRight = false ;
139 }
140 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
141 button = g_lastButton ;
142
143 // Adjust the chord mask to remove the primary button and add the
144 // secondary button. It is possible that the secondary button is
145 // already pressed, e.g. on a mouse connected to a laptop, but this
146 // possibility is ignored here:
147 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
148 mouseChord = ((mouseChord & ~1U) | 2U);
149
150 if(mouseChord & 1U)
151 wxevent.m_leftDown = true ;
152 if(mouseChord & 2U)
153 wxevent.m_rightDown = true ;
154 if(mouseChord & 4U)
155 wxevent.m_middleDown = true ;
156
157*/
158 // translate into wx types
159 int eventType = [touch phase];
160 switch (eventType)
161 {
162 case UITouchPhaseBegan :
163 switch ( button )
164 {
165 case 0 :
166 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
167 break ;
168
169 default:
170 break ;
171 }
172 break ;
173
174 case UITouchPhaseEnded :
175 switch ( button )
176 {
177 case 0 :
178 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
179 break ;
180
181 default:
182 break ;
183 }
184 break ;
185
186 case UITouchPhaseMoved :
187 wxevent.SetEventType( wxEVT_MOTION ) ;
188 break;
189 default :
190 break ;
191 }
192}
193
194@implementation wxUIView
195
d3929256 196+ (void)initialize
c16b2153 197{
d3929256 198 static BOOL initialized = NO;
03647350 199 if (!initialized)
c16b2153 200 {
d3929256
SC
201 initialized = YES;
202 wxOSXIPhoneClassAddWXMethods( self );
c16b2153 203 }
c16b2153
SC
204}
205
d3929256 206@end // wxUIView
c16b2153 207
d3929256 208/*
c16b2153
SC
209- (void)drawRect: (CGRect) rect
210{
d3929256
SC
211 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
212 if ( impl )
c16b2153
SC
213 {
214 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
215 CGContextSaveGState( context );
216 // draw background
03647350 217
d3929256 218 CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor());
c16b2153
SC
219 CGContextFillRect(context, rect );
220
d3929256 221 impl->GetWXPeer()->MacSetCGContextRef( context );
c16b2153 222
03647350 223 impl->GetWXPeer()->GetUpdateRegion() =
c16b2153
SC
224 wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ;
225
226 wxPaintEvent event;
227 event.SetTimestamp(0); // todo
d3929256
SC
228 event.SetEventObject(impl->GetWXPeer());
229 impl->GetWXPeer()->HandleWindowEvent(event);
c16b2153
SC
230
231 CGContextRestoreGState( context );
232 }
233
234}
235
03647350 236- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
c16b2153
SC
237{
238 [self handleTouchEvent:touches withEvent:event];
239}
240
03647350 241- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
c16b2153
SC
242{
243 [self handleTouchEvent:touches withEvent:event];
244}
245
03647350 246- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
c16b2153
SC
247{
248 [self handleTouchEvent:touches withEvent:event];
249}
250
03647350
VZ
251-(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event
252{
d3929256 253 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
03647350 254 CGPoint clickLocation;
c16b2153
SC
255 UITouch *touch = [touches anyObject];
256 clickLocation = [touch locationInView:self];
257
258 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
259 SetupMouseEvent( wxevent , touches, event ) ;
260 wxevent.m_x = clickLocation.x;
261 wxevent.m_y = clickLocation.y;
d3929256
SC
262 wxevent.SetEventObject( impl->GetWXPeer() ) ;
263 wxevent.SetId( impl->GetWXPeer()->GetId() ) ;
264 impl->GetWXPeer()->HandleWindowEvent(wxevent);
c16b2153
SC
265}
266
d3929256 267*/
c16b2153 268
03647350 269void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event )
c16b2153 270{
d3929256
SC
271 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
272 if (impl == NULL)
273 return;
03647350 274
d3929256 275 impl->touchEvent(touches, event, self, _cmd);
c16b2153
SC
276}
277
d3929256 278BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd)
c16b2153 279{
d3929256
SC
280 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
281 if (impl == NULL)
282 return NO;
03647350 283
d3929256 284 return impl->becomeFirstResponder(self, _cmd);
c16b2153
SC
285}
286
d3929256 287BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd)
c16b2153 288{
d3929256
SC
289 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
290 if (impl == NULL)
291 return NO;
03647350 292
d3929256 293 return impl->resignFirstResponder(self, _cmd);
c16b2153
SC
294}
295
d3929256 296void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect)
c16b2153 297{
d3929256
SC
298 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
299 if (impl == NULL)
300 return;
03647350 301
d3929256 302 return impl->drawRect(&rect, self, _cmd);
c16b2153
SC
303}
304
305
d3929256
SC
306void wxOSXIPhoneClassAddWXMethods(Class c)
307{
308 class_addMethod(c, @selector(touchesBegan:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
309 class_addMethod(c, @selector(touchesMoved:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
310 class_addMethod(c, @selector(touchesEnded:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
311 class_addMethod(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" );
312 class_addMethod(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" );
313 class_addMethod(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_CGRect={_CGPoint=ff}{_CGSize=ff}}" );
314}
c16b2153
SC
315
316@implementation wxUIContentView
317
318@end
319
320@implementation wxUIContentViewController
321
322- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
323{
65536c92
SC
324 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
325 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
326
327 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
328
c16b2153
SC
329 return YES;
330}
331
332- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
333{
334 CGRect fr = [self.view frame];
65536c92
SC
335 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
336 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
337
338 now->HandleResized(0);
c16b2153
SC
339}
340
341@end
342
343
344IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl )
345
346wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
347 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
348{
349}
350
03647350 351wxWidgetIPhoneImpl::wxWidgetIPhoneImpl()
c16b2153
SC
352{
353}
354
355void wxWidgetIPhoneImpl::Init()
356{
357 m_osxView = NULL;
358}
359
360wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl()
361{
d3929256
SC
362 RemoveAssociations( this );
363
364 if ( !IsRootControl() )
365 {
366 UIView *sv = [m_osxView superview];
367 if ( sv != nil )
368 [m_osxView removeFromSuperview];
369 }
c16b2153
SC
370 [m_osxView release];
371}
03647350
VZ
372
373bool wxWidgetIPhoneImpl::IsVisible() const
c16b2153 374{
8996460c
SC
375 UIView* view = m_osxView;
376 while ( view != nil && [view isHidden] == NO )
377 {
378 view = [view superview];
8996460c
SC
379 }
380 return view == nil;
c16b2153
SC
381}
382
383void wxWidgetIPhoneImpl::SetVisibility( bool visible )
384{
385 [m_osxView setHidden:(visible ? NO:YES)];
386}
387
388void wxWidgetIPhoneImpl::Raise()
389{
390 [[m_osxView superview] bringSubviewToFront:m_osxView];
391}
03647350 392
c16b2153
SC
393void wxWidgetIPhoneImpl::Lower()
394{
395 [[m_osxView superview] sendSubviewToBack:m_osxView];
396}
397
398void wxWidgetIPhoneImpl::ScrollRect( const wxRect *rect, int dx, int dy )
399{
d3929256 400 SetNeedsDisplay() ;
c16b2153
SC
401}
402
403void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height)
404{
405 CGRect r = CGRectMake( x, y, width, height) ;
406 [m_osxView setFrame:r];
407}
408
409void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const
410{
411 CGRect r = [m_osxView frame];
412 x = r.origin.x;
413 y = r.origin.y;
414}
415
416void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const
417{
418 CGRect rect = [m_osxView frame];
419 width = rect.size.width;
420 height = rect.size.height;
421}
422
423void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
424{
425 left = top = 0;
65536c92
SC
426 CGRect rect = [m_osxView bounds];
427 width = rect.size.width;
428 height = rect.size.height;
c16b2153
SC
429}
430
431void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where )
432{
433 if ( where )
434 {
435 CGRect r = CGRectMake( where->x, where->y, where->width, where->height) ;
436 [m_osxView setNeedsDisplayInRect:r];
437 }
438 else
439 [m_osxView setNeedsDisplay];
440}
441
442bool wxWidgetIPhoneImpl::GetNeedsDisplay() const
443{
444 return false;
445// return [m_osxView needsDisplay];
446}
447
448bool wxWidgetIPhoneImpl::CanFocus() const
449{
450 return [m_osxView canBecomeFirstResponder] == YES;
451 // ? return [m_osxView isUserInteractionEnabled] == YES;
452}
453
454bool wxWidgetIPhoneImpl::HasFocus() const
455{
456 return [m_osxView isFirstResponder] == YES;
457}
458
03647350 459bool wxWidgetIPhoneImpl::SetFocus()
c16b2153
SC
460{
461// [m_osxView makeKeyWindow] ;
462// TODO
463 return false;
464}
465
466
467void wxWidgetIPhoneImpl::RemoveFromParent()
468{
469 [m_osxView removeFromSuperview];
470}
471
472void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent )
473{
474 UIView* container = parent->GetWXWidget() ;
475 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
476 [container addSubview:m_osxView];
477}
478
479void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
480{
481 CGPoint p = CGPointMake( pt->x , pt->y );
03647350 482 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
c16b2153
SC
483 pt->x = (int)p.x;
484 pt->y = (int)p.y;
485}
486
487void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col )
488{
489 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
490}
491
d3929256
SC
492void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding)
493{
494 if ( [m_osxView respondsToSelector:@selector(setTitle:forState:) ] )
495 {
496 wxCFStringRef cf( title , encoding );
497 [m_osxView setTitle:cf.AsNSString() forState:UIControlStateNormal ];
498 }
499 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
500 {
501 wxCFStringRef cf( title , encoding );
502 [m_osxView setStringValue:cf.AsNSString()];
503 }
504}
505
506
507void wxWidgetIPhoneImpl::SetCursor( const wxCursor & cursor )
508{
509}
510
511void wxWidgetIPhoneImpl::CaptureMouse()
512{
513}
514
515void wxWidgetIPhoneImpl::ReleaseMouse()
516{
517}
518
519wxInt32 wxWidgetIPhoneImpl::GetValue() const
520{
521}
522
523void wxWidgetIPhoneImpl::SetValue( wxInt32 v )
524{
525}
526
527void wxWidgetIPhoneImpl::SetBitmap( const wxBitmap& bitmap )
528{
529}
530
9e55f38d
SC
531wxBitmap wxWidgetIPhoneImpl::GetBitmap() const
532{
533 wxBitmap bmp;
534 return bmp;
535}
536
537void wxWidgetIPhoneImpl::SetBitmapPosition( wxDirection dir )
538{
539}
540
d3929256
SC
541void wxWidgetIPhoneImpl::SetupTabs( const wxNotebook &notebook )
542{
543}
544
545void wxWidgetIPhoneImpl::GetBestRect( wxRect *r ) const
546{
547 r->x = r->y = r->width = r->height = 0;
548
549 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
550 {
551 CGRect former = [m_osxView frame];
552 [m_osxView sizeToFit];
553 CGRect best = [m_osxView frame];
554 [m_osxView setFrame:former];
555 r->width = best.size.width;
556 r->height = best.size.height;
557 }
558}
559
560bool wxWidgetIPhoneImpl::IsEnabled() const
561{
562}
563
564void wxWidgetIPhoneImpl::Enable( bool enable )
565{
566}
567
568void wxWidgetIPhoneImpl::SetMinimum( wxInt32 v )
569{
570}
571
572void wxWidgetIPhoneImpl::SetMaximum( wxInt32 v )
573{
574}
575
576wxInt32 wxWidgetIPhoneImpl::GetMinimum() const
577{
578}
579
580wxInt32 wxWidgetIPhoneImpl::GetMaximum() const
581{
582}
583
584void wxWidgetIPhoneImpl::PulseGauge()
585{
586}
587
588void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
589{
590}
591
03647350 592void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant )
d3929256
SC
593{
594}
595
596void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
597{
598}
599
600void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
601{
602 WXWidget c = control ? control : (WXWidget) m_osxView;
603 wxWidgetImpl::Associate( c, this ) ;
604
605 if ([c isKindOfClass:[UIControl class] ])
606 {
607 UIControl* cc = (UIControl*) c;
608 /*
609 [cc addTarget:self action:@selector(touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
610 */
611 }
612}
613
614void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
615{
616 wxWindow* thisWindow = GetWXPeer();
617 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
618 {
619 thisWindow->MacInvalidateBorders();
620 }
621
622 if ( receivedFocus )
623 {
9a83f860 624 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
d3929256
SC
625 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
626 thisWindow->HandleWindowEvent(eventFocus);
627
628#if wxUSE_CARET
629 if ( thisWindow->GetCaret() )
630 thisWindow->GetCaret()->OnSetFocus();
631#endif
632
633 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
634 event.SetEventObject(thisWindow);
635 if (otherWindow)
636 event.SetWindow(otherWindow->GetWXPeer());
637 thisWindow->HandleWindowEvent(event) ;
638 }
639 else // !receivedFocuss
640 {
641#if wxUSE_CARET
642 if ( thisWindow->GetCaret() )
643 thisWindow->GetCaret()->OnKillFocus();
644#endif
645
9a83f860 646 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
03647350 647
d3929256
SC
648 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
649 event.SetEventObject(thisWindow);
650 if (otherWindow)
651 event.SetWindow(otherWindow->GetWXPeer());
652 thisWindow->HandleWindowEvent(event) ;
653 }
654}
655
656typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
657typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
658
659bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
660{
661 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
662 // get the current focus before running becomeFirstResponder
03647350 663 UIView* otherView = FindFocus();
d3929256
SC
664 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
665 BOOL r = superimpl(slf, (SEL)_cmd);
666 if ( r )
667 {
668 DoNotifyFocusEvent( true, otherWindow );
669 }
670 return r;
671}
672
673bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
674{
675 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
676 BOOL r = superimpl(slf, (SEL)_cmd);
677 // get the current focus after running resignFirstResponder
03647350 678 UIView* otherView = FindFocus();
d3929256
SC
679 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
680 // NSTextViews have an editor as true responder, therefore the might get the
681 // resign notification if their editor takes over, don't trigger any event hen
682 if ( r && otherWindow != this)
683 {
684 DoNotifyFocusEvent( false, otherWindow );
685 }
686 return r;
687}
688
689void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
690{
691 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
692 CGContextSaveGState( context );
693 // draw background
03647350 694
d3929256
SC
695 CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
696 CGContextFillRect(context, *rect );
697
698 GetWXPeer()->MacSetCGContextRef( context );
699
03647350 700 GetWXPeer()->GetUpdateRegion() =
d3929256
SC
701 wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
702
703 wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
704
705 wxWindow* wxpeer = GetWXPeer();
706 wxpeer->GetUpdateRegion() = updateRgn;
707 wxpeer->MacSetCGContextRef( context );
03647350 708
d3929256 709 bool handled = wxpeer->MacDoRedraw( 0 );
03647350 710
d3929256
SC
711 CGContextRestoreGState( context );
712
713 CGContextSaveGState( context );
714 if ( !handled )
715 {
716 // call super
717 SEL _cmd = @selector(drawRect:);
718 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
8996460c
SC
719 if ( superimpl != wxOSX_drawRect )
720 {
721 superimpl(slf, _cmd, *rect);
722 CGContextRestoreGState( context );
723 CGContextSaveGState( context );
724 }
d3929256
SC
725 }
726 wxpeer->MacPaintChildrenBorders();
727 wxpeer->MacSetCGContextRef( NULL );
728
729 CGContextRestoreGState( context );
730}
731
732void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
733{
03647350 734 CGPoint clickLocation;
d3929256
SC
735 UITouch *touch = [touches anyObject];
736 clickLocation = [touch locationInView:slf];
737 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
738
739 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
740 SetupMouseEvent( wxevent , touches, event ) ;
741 wxevent.m_x = pt.x;
742 wxevent.m_y = pt.y;
743 wxevent.SetEventObject( GetWXPeer() ) ;
744 //?wxevent.SetId( GetWXPeer()->GetId() ) ;
745
746 GetWXPeer()->HandleWindowEvent(wxevent);
747}
748
749void wxWidgetIPhoneImpl::touchUpInsideAction(void* sender, WX_UIEvent evt, WXWidget slf, void* _cmd)
750{
751}
752
c16b2153
SC
753//
754// Factory methods
755//
756
03647350 757wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
d3929256
SC
758 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
759 long WXUNUSED(style), long WXUNUSED(extraStyle))
c16b2153
SC
760{
761 UIView* sv = (wxpeer->GetParent()->GetHandle() );
03647350 762
c16b2153
SC
763 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
764 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
765 wxUIView* v = [[wxUIView alloc] initWithFrame:r];
766 sv.clipsToBounds = YES;
767 sv.contentMode = UIViewContentModeRedraw;
768 sv.clearsContextBeforeDrawing = NO;
c16b2153 769 wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v );
c16b2153
SC
770 return c;
771}
772
03647350 773wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
c16b2153
SC
774{
775 UIWindow* toplevelwindow = now->GetWXWindow();
65536c92
SC
776 CGRect frame = [toplevelwindow bounds];
777 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
c16b2153 778
65536c92
SC
779 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE )
780 {
781 double offset = appframe.origin.y;
782 frame.origin.y += offset;
783 frame.size.height -= offset;
784 }
785
786 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:frame];
c16b2153
SC
787 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
788 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
789 controller.view = contentview;
03647350 790
c16b2153 791 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
d3929256 792 impl->InstallEventHandler();
c16b2153
SC
793 [toplevelwindow addSubview:contentview];
794 return impl;
795}
796