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