Support precompiled headers
[wxWidgets.git] / src / cocoa / dcclient.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dcclient.mm
3 // Purpose:     wxWindowDC, wxPaintDC, and wxClientDC classes
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/04/01
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14     #include "wx/log.h"
15     #include "wx/window.h"
16     #include "wx/dcclient.h"
17 #endif //WX_PRECOMP
18
19 #import <AppKit/NSView.h>
20 #import <AppKit/NSAffineTransform.h>
21 #import <AppKit/NSColor.h>
22 #import <AppKit/NSGraphicsContext.h>
23 #import <AppKit/NSBezierPath.h>
24 #import <AppKit/NSWindow.h>
25
26 /*
27  * wxWindowDC
28  */
29 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
30
31 wxWindowDC::wxWindowDC(void)
32 :   m_window(NULL)
33 {
34 };
35
36 wxWindowDC::wxWindowDC( wxWindow *window )
37 :   m_window(window)
38 {
39     wxFAIL_MSG("non-client window DC's are not supported");
40 };
41
42 wxWindowDC::~wxWindowDC(void)
43 {
44 };
45
46 void wxWindowDC::Clear()
47 {
48     wxASSERT(m_window);
49
50     NSGraphicsContext *context = [NSGraphicsContext currentContext];
51     [context saveGraphicsState];
52
53     [m_backgroundBrush.GetNSColor() set];
54     [NSBezierPath fillRect:[m_window->GetNSView() bounds]];
55
56     [context restoreGraphicsState];
57 }
58
59 /*
60  * wxClientDC
61  */
62 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
63
64 wxClientDC::wxClientDC(void)
65 {
66 };
67
68 wxClientDC::wxClientDC( wxWindow *window )
69 {
70     m_window = window;
71 };
72
73 wxClientDC::~wxClientDC(void)
74 {
75     CocoaUnwindStackAndLoseFocus();
76 };
77
78 bool wxClientDC::CocoaLockFocus()
79 {
80     wxLogDebug("Locking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
81     if([m_window->GetNSView() lockFocusIfCanDraw])
82     {
83         sm_cocoaDCStack.Insert(this);
84         m_cocoaFlipped = [m_window->GetNSView() isFlipped];
85         m_cocoaHeight = [m_window->GetNSView() bounds].size.height;
86         CocoaApplyTransformations();
87         return true;
88     }
89     wxLogDebug("focus lock failed!");
90     return false;
91 }
92
93 bool wxClientDC::CocoaUnlockFocus()
94 {
95     wxLogDebug("Unlocking focus on wxClientDC=%p, NSView=%p",this, m_window->GetNSView());
96     [[m_window->GetNSView() window] flushWindow];
97     [m_window->GetNSView() unlockFocus];
98     return true;
99 }
100
101 /*
102  * wxPaintDC
103  */
104 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
105
106 wxPaintDC::wxPaintDC(void)
107 {
108 };
109
110 wxPaintDC::wxPaintDC( wxWindow *window )
111 {
112     m_window = window;
113     wxASSERT_MSG([NSView focusView]==window->GetNSView(), "PaintDC's NSView does not have focus.  Please use wxPaintDC only as the first DC created in a paint handler");
114     sm_cocoaDCStack.Insert(this);
115     m_cocoaFlipped = [window->GetNSView() isFlipped];
116     m_cocoaHeight = [window->GetNSView() bounds].size.height;
117     CocoaApplyTransformations();
118 };
119
120 wxPaintDC::~wxPaintDC(void)
121 {
122     CocoaUnwindStackAndLoseFocus();
123 };
124
125 bool wxPaintDC::CocoaLockFocus()
126 {
127     wxFAIL_MSG("wxPaintDC cannot be asked to lock focus!");
128     return false;
129 }
130
131 bool wxPaintDC::CocoaUnlockFocus()
132 {
133     // wxPaintDC focus can never be unlocked.
134     return false;
135 }
136