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