Implemented wxWindowCocoa::Cocoa_drawRect
[wxWidgets.git] / src / cocoa / NSView.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/NSView.mm
3 // Purpose:     wxCocoaNSView
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/02/15
7 // RCS-ID:      $Id: 
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22     #include "wx/window.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/cocoa/NSView.h"
26
27 #import <Appkit/NSView.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30
31 // ----------------------------------------------------------------------------
32 // globals
33 // ----------------------------------------------------------------------------
34 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
35
36 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
37 {
38     sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
39     [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
40     [cocoaNSView setPostsFrameChangedNotifications: YES];
41 }
42
43 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
44 {
45     sm_cocoaHash.erase(cocoaNSView);
46     [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
47 }
48
49 // ============================================================================
50 // @class wxPoserNSView
51 // ============================================================================
52 @interface wxPoserNSView : NSView
53 {
54 }
55
56 - (void)drawRect: (NSRect)rect;
57 @end // wxPoserNSView
58
59 WX_IMPLEMENT_POSER(wxPoserNSView);
60 @implementation wxPoserNSView : NSView
61
62 - (void)drawRect: (NSRect)rect
63 {
64     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
65     if( !win || !win->Cocoa_drawRect(rect) )
66         [super drawRect:rect];
67 }
68
69 @end // implementation wxPoserNSView
70
71 @interface wxNSViewNotificationObserver : NSObject
72 {
73 }
74
75 // FIXME: Initializing like this is a really bad idea.  If for some reason
76 // we ever require posing as an NSObject we won't be able to since an instance
77 // will have already been created here.  Of course, catching messages for
78 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
79 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
80
81 - (void)notificationFrameChanged: (NSNotification *)notification;
82 @end // interface wxNSViewNotificationObserver
83
84 @implementation wxNSViewNotificationObserver : NSObject
85
86 - (void)notificationFrameChanged: (NSNotification *)notification;
87 {
88     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
89     wxCHECK_RET(win,"notificationFrameChanged received but no wxWindow exists");
90     win->Cocoa_FrameChanged();
91 }
92
93 @end // implementation wxNSViewNotificationObserver
94