| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/cocoa/dcclient.mm |
| 3 | // Purpose: wxWindowDCImpl, wxPaintDCImpl, and wxClientDCImpl 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: wxWidgets licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #include "wx/wxprec.h" |
| 13 | #ifndef WX_PRECOMP |
| 14 | #include "wx/log.h" |
| 15 | #include "wx/window.h" |
| 16 | #endif //WX_PRECOMP |
| 17 | |
| 18 | #include "wx/cocoa/dcclient.h" |
| 19 | |
| 20 | #import <AppKit/NSView.h> |
| 21 | #import <AppKit/NSAffineTransform.h> |
| 22 | #import <AppKit/NSColor.h> |
| 23 | #import <AppKit/NSGraphicsContext.h> |
| 24 | #import <AppKit/NSBezierPath.h> |
| 25 | #import <AppKit/NSWindow.h> |
| 26 | |
| 27 | /* |
| 28 | * wxWindowDCImpl |
| 29 | */ |
| 30 | IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxCocoaDCImpl) |
| 31 | |
| 32 | wxWindowDCImpl::wxWindowDCImpl(wxDC *owner) |
| 33 | : wxCocoaDCImpl(owner) |
| 34 | , m_window(NULL) |
| 35 | , m_lockedNSView(NULL) |
| 36 | { |
| 37 | }; |
| 38 | |
| 39 | wxWindowDCImpl::wxWindowDCImpl(wxDC *owner, wxWindow *window) |
| 40 | : wxCocoaDCImpl(owner) |
| 41 | , m_window(window) |
| 42 | , m_lockedNSView(NULL) |
| 43 | { |
| 44 | wxLogDebug(wxT("non-client window DC's are not supported, oh well")); |
| 45 | }; |
| 46 | |
| 47 | wxWindowDCImpl::~wxWindowDCImpl(void) |
| 48 | { |
| 49 | CocoaUnwindStackAndLoseFocus(); |
| 50 | }; |
| 51 | |
| 52 | bool wxWindowDCImpl::CocoaLockFocusOnNSView(WX_NSView nsview) |
| 53 | { |
| 54 | if([nsview lockFocusIfCanDraw]) |
| 55 | { |
| 56 | sm_cocoaDCStack.Insert(this); |
| 57 | CocoaApplyTransformations(); |
| 58 | m_lockedNSView = nsview; |
| 59 | return true; |
| 60 | } |
| 61 | wxLogDebug(wxT("focus lock failed!")); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | bool wxWindowDCImpl::CocoaUnlockFocusOnNSView() |
| 66 | { |
| 67 | [[m_lockedNSView window] flushWindow]; |
| 68 | [m_lockedNSView unlockFocus]; |
| 69 | m_lockedNSView = NULL; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool wxWindowDCImpl::CocoaLockFocus() |
| 74 | { |
| 75 | wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView()); |
| 76 | NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_window->GetNonClientNSView() isFlipped], [m_window->GetNonClientNSView() bounds].size.height); |
| 77 | [newTransform retain]; |
| 78 | [m_cocoaWxToBoundsTransform release]; |
| 79 | m_cocoaWxToBoundsTransform = newTransform; |
| 80 | return CocoaLockFocusOnNSView(m_window->GetNonClientNSView()); |
| 81 | } |
| 82 | |
| 83 | bool wxWindowDCImpl::CocoaUnlockFocus() |
| 84 | { |
| 85 | wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxWindowDCImpl=%p, NSView=%p"),this, m_window->GetNonClientNSView()); |
| 86 | return CocoaUnlockFocusOnNSView(); |
| 87 | } |
| 88 | |
| 89 | bool wxWindowDCImpl::CocoaGetBounds(void *rectData) |
| 90 | { |
| 91 | if(!rectData) |
| 92 | return false; |
| 93 | if(!m_lockedNSView) |
| 94 | return false; |
| 95 | NSRect *pRect = (NSRect*)rectData; |
| 96 | *pRect = [m_lockedNSView bounds]; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * wxClientDCImpl |
| 102 | */ |
| 103 | IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl) |
| 104 | |
| 105 | wxClientDCImpl::wxClientDCImpl(wxDC *owner) |
| 106 | : wxWindowDCImpl(owner) |
| 107 | { |
| 108 | }; |
| 109 | |
| 110 | wxClientDCImpl::wxClientDCImpl(wxDC *owner, wxWindow *window) |
| 111 | : wxWindowDCImpl(owner) |
| 112 | { |
| 113 | m_window = window; |
| 114 | }; |
| 115 | |
| 116 | wxClientDCImpl::~wxClientDCImpl(void) |
| 117 | { |
| 118 | CocoaUnwindStackAndLoseFocus(); |
| 119 | }; |
| 120 | |
| 121 | bool wxClientDCImpl::CocoaLockFocus() |
| 122 | { |
| 123 | wxLogTrace(wxTRACE_COCOA,wxT("Locking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView()); |
| 124 | NSAffineTransform *newTransform = m_window->CocoaGetWxToBoundsTransform(); |
| 125 | [newTransform retain]; |
| 126 | [m_cocoaWxToBoundsTransform release]; |
| 127 | m_cocoaWxToBoundsTransform = newTransform; |
| 128 | return CocoaLockFocusOnNSView(m_window->GetNSView()); |
| 129 | } |
| 130 | |
| 131 | bool wxClientDCImpl::CocoaUnlockFocus() |
| 132 | { |
| 133 | wxLogTrace(wxTRACE_COCOA,wxT("Unlocking focus on wxClientDCImpl=%p, NSView=%p"),this, m_window->GetNSView()); |
| 134 | return CocoaUnlockFocusOnNSView(); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * wxPaintDCImpl |
| 139 | */ |
| 140 | IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl) |
| 141 | |
| 142 | wxPaintDCImpl::wxPaintDCImpl(wxDC *owner) |
| 143 | : wxWindowDCImpl(owner) |
| 144 | { |
| 145 | }; |
| 146 | |
| 147 | wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow *window) |
| 148 | : wxWindowDCImpl(owner) |
| 149 | { |
| 150 | m_window = window; |
| 151 | wxASSERT_MSG([NSView focusView]==window->GetNSView(), wxT("PaintDC's NSView does not have focus. Please use wxPaintDCImpl only as the first DC created in a paint handler")); |
| 152 | sm_cocoaDCStack.Insert(this); |
| 153 | m_lockedNSView = window->GetNSView(); |
| 154 | NSAffineTransform *newTransform = window->CocoaGetWxToBoundsTransform(); |
| 155 | [newTransform retain]; |
| 156 | [m_cocoaWxToBoundsTransform release]; |
| 157 | m_cocoaWxToBoundsTransform = newTransform; |
| 158 | CocoaApplyTransformations(); |
| 159 | }; |
| 160 | |
| 161 | wxPaintDCImpl::~wxPaintDCImpl(void) |
| 162 | { |
| 163 | CocoaUnwindStackAndLoseFocus(); |
| 164 | }; |
| 165 | |
| 166 | bool wxPaintDCImpl::CocoaLockFocus() |
| 167 | { |
| 168 | wxFAIL_MSG(wxT("wxPaintDCImpl cannot be asked to lock focus!")); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | bool wxPaintDCImpl::CocoaUnlockFocus() |
| 173 | { |
| 174 | // wxPaintDCImpl focus can never be unlocked. |
| 175 | return false; |
| 176 | } |
| 177 | |