]>
Commit | Line | Data |
---|---|---|
a24aff65 | 1 | ///////////////////////////////////////////////////////////////////////////// |
891d0563 DE |
2 | // Name: src/cocoa/dc.mm |
3 | // Purpose: wxDC | |
4 | // Author: David Elliott | |
a24aff65 | 5 | // Modified by: |
891d0563 | 6 | // Created: 2003/04/01 |
a24aff65 | 7 | // RCS-ID: $Id$ |
891d0563 | 8 | // Copyright: (c) 2003 David Elliott |
da80ae71 | 9 | // Licence: wxWidgets licence |
a24aff65 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
da80ae71 WS |
13 | |
14 | #include "wx/dc.h" | |
15 | ||
449c5673 DE |
16 | #ifndef WX_PRECOMP |
17 | #include "wx/log.h" | |
18680f86 | 18 | #include "wx/math.h" //math constants |
449c5673 | 19 | #endif //WX_PRECOMP |
a24aff65 | 20 | |
7fc77f30 | 21 | #include "wx/cocoa/autorelease.h" |
b0c0a393 | 22 | #include "wx/cocoa/string.h" |
7fc77f30 | 23 | |
891d0563 DE |
24 | #import <AppKit/NSBezierPath.h> |
25 | #import <AppKit/NSTextStorage.h> | |
26 | #import <AppKit/NSLayoutManager.h> | |
27 | #import <AppKit/NSTextContainer.h> | |
28 | #import <AppKit/NSGraphicsContext.h> | |
29 | #import <AppKit/NSAffineTransform.h> | |
30 | #import <AppKit/NSColor.h> | |
f910a887 | 31 | #import <AppKit/NSTypesetter.h> |
1fd17880 | 32 | #import <AppKit/NSImage.h> |
891d0563 | 33 | |
d8418952 | 34 | #include "wx/listimpl.cpp" |
fe8f7943 DE |
35 | WX_DEFINE_LIST(wxCocoaDCStack); |
36 | ||
a24aff65 | 37 | IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject) |
891d0563 DE |
38 | WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil; |
39 | WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil; | |
40 | WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil; | |
fe8f7943 | 41 | wxCocoaDCStack wxDC::sm_cocoaDCStack; |
891d0563 | 42 | |
5eb5a0ac DE |
43 | inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath) |
44 | { | |
45 | [pen.GetNSColor() set]; | |
46 | const float *pattern; | |
47 | [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0]; | |
48 | [bezpath setLineWidth:pen.GetWidth()]; | |
49 | switch(pen.GetJoin()) | |
50 | { | |
51 | case wxJOIN_BEVEL: | |
52 | [bezpath setLineJoinStyle:NSBevelLineJoinStyle]; | |
53 | break; | |
54 | case wxJOIN_ROUND: | |
55 | [bezpath setLineJoinStyle:NSRoundLineJoinStyle]; | |
56 | break; | |
57 | case wxJOIN_MITER: | |
58 | [bezpath setLineJoinStyle:NSMiterLineJoinStyle]; | |
59 | break; | |
60 | } | |
61 | switch(pen.GetCap()) | |
62 | { | |
63 | case wxCAP_ROUND: | |
64 | [bezpath setLineCapStyle:NSRoundLineCapStyle]; | |
65 | break; | |
66 | case wxCAP_PROJECTING: | |
67 | [bezpath setLineCapStyle:NSSquareLineCapStyle]; | |
68 | break; | |
69 | case wxCAP_BUTT: | |
70 | [bezpath setLineCapStyle:NSButtLineCapStyle]; | |
71 | break; | |
72 | } | |
73 | } | |
74 | ||
891d0563 DE |
75 | void wxDC::CocoaInitializeTextSystem() |
76 | { | |
2b030203 | 77 | wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,wxT("Text system already initalized! BAD PROGRAMMER!")); |
891d0563 DE |
78 | |
79 | sm_cocoaNSTextStorage = [[NSTextStorage alloc] init]; | |
80 | ||
81 | sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init]; | |
82 | [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager]; | |
83 | // NSTextStorage retains NSLayoutManager, but so do we | |
84 | // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain]; | |
85 | ||
86 | // NOTE: initWithContainerSize is the designated initializer, but the | |
87 | // Apple CircleView sample gets away with just calling init, which | |
88 | // is all we really need for our purposes. | |
89 | sm_cocoaNSTextContainer = [[NSTextContainer alloc] init]; | |
90 | [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer]; | |
91 | // NSLayoutManager retains NSTextContainer, but so do we | |
92 | // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain]; | |
93 | } | |
94 | ||
95 | void wxDC::CocoaShutdownTextSystem() | |
96 | { | |
97 | [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil; | |
98 | [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil; | |
99 | [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil; | |
100 | } | |
101 | ||
fe8f7943 DE |
102 | void wxDC::CocoaUnwindStackAndLoseFocus() |
103 | { | |
7ce8248b | 104 | wxCocoaDCStack::compatibility_iterator ourNode=sm_cocoaDCStack.Find(this); |
fe8f7943 DE |
105 | if(ourNode) |
106 | { | |
7ce8248b | 107 | wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst(); |
fe8f7943 DE |
108 | for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst()) |
109 | { | |
110 | wxDC *dc = node->GetData(); | |
111 | wxASSERT(dc); | |
112 | wxASSERT(dc!=this); | |
113 | if(!dc->CocoaUnlockFocus()) | |
114 | { | |
2b030203 | 115 | wxFAIL_MSG(wxT("Unable to unlock focus on higher-level DC!")); |
fe8f7943 | 116 | } |
7ce8248b | 117 | sm_cocoaDCStack.Erase(node); |
fe8f7943 DE |
118 | } |
119 | wxASSERT(node==ourNode); | |
120 | wxASSERT(ourNode->GetData() == this); | |
121 | ourNode->GetData()->CocoaUnlockFocus(); | |
7ce8248b | 122 | sm_cocoaDCStack.Erase(ourNode); |
fe8f7943 DE |
123 | } |
124 | } | |
125 | ||
126 | bool wxDC::CocoaUnwindStackAndTakeFocus() | |
127 | { | |
7ce8248b | 128 | wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst(); |
fe8f7943 DE |
129 | for(;node;node = sm_cocoaDCStack.GetFirst()) |
130 | { | |
131 | wxDC *dc = node->GetData(); | |
132 | wxASSERT(dc); | |
133 | // If we're on the stack, then it's unwound enough and we have focus | |
134 | if(dc==this) | |
135 | return true; | |
136 | // If unable to unlockFocus (e.g. wxPaintDC) stop here | |
137 | if(!dc->CocoaUnlockFocus()) | |
138 | break; | |
7ce8248b | 139 | sm_cocoaDCStack.Erase(node); |
fe8f7943 DE |
140 | } |
141 | return CocoaLockFocus(); | |
142 | } | |
143 | ||
891d0563 DE |
144 | wxDC::wxDC(void) |
145 | { | |
db8512fe | 146 | m_cocoaWxToBoundsTransform = nil; |
5eb5a0ac | 147 | m_pen = *wxBLACK_PEN; |
891d0563 DE |
148 | } |
149 | ||
150 | wxDC::~wxDC(void) | |
151 | { | |
1a1e9ff1 | 152 | [m_cocoaWxToBoundsTransform release]; |
891d0563 DE |
153 | } |
154 | ||
fe8f7943 DE |
155 | bool wxDC::CocoaLockFocus() |
156 | { | |
157 | return false; | |
158 | } | |
159 | ||
160 | bool wxDC::CocoaUnlockFocus() | |
161 | { | |
162 | return false; | |
163 | } | |
164 | ||
4db3c8ac | 165 | /*static*/ WX_NSAffineTransform wxDC::CocoaGetWxToBoundsTransform(bool isFlipped, float height) |
fe8f7943 | 166 | { |
4db3c8ac | 167 | NSAffineTransform *transform = nil; |
fe8f7943 | 168 | // This transform flips the graphics since wxDC uses top-left origin |
4db3c8ac | 169 | if(!isFlipped) |
fe8f7943 DE |
170 | { |
171 | // The transform is auto released | |
4db3c8ac | 172 | transform = [NSAffineTransform transform]; |
fe8f7943 DE |
173 | /* x' = 1x + 0y + 0 |
174 | y' = 0x + -1y + window's height | |
175 | */ | |
176 | NSAffineTransformStruct matrix = { | |
177 | 1, 0 | |
178 | , 0, -1 | |
4db3c8ac | 179 | , 0, height |
fe8f7943 DE |
180 | }; |
181 | [transform setTransformStruct: matrix]; | |
fe8f7943 | 182 | } |
4db3c8ac DE |
183 | return transform; |
184 | } | |
185 | ||
186 | void wxDC::CocoaApplyTransformations() | |
187 | { | |
188 | [m_cocoaWxToBoundsTransform concat]; | |
189 | // TODO: Apply device/logical/user position/scaling transformations | |
fe8f7943 DE |
190 | } |
191 | ||
3e21fc05 DE |
192 | void wxDC::CocoaUnapplyTransformations() |
193 | { | |
194 | // NOTE: You *must* call this with focus held. | |
195 | // Undo all transforms so we're back in true Cocoa coords with | |
196 | // no scaling or flipping. | |
197 | NSAffineTransform *invertTransform; | |
198 | invertTransform = [m_cocoaWxToBoundsTransform copy]; | |
199 | [invertTransform invert]; | |
200 | [invertTransform concat]; | |
f48ab29b | 201 | [invertTransform release]; |
3e21fc05 DE |
202 | } |
203 | ||
204 | bool wxDC::CocoaGetBounds(void *rectData) | |
205 | { | |
206 | // We don't know what we are so we can't return anything. | |
207 | return false; | |
208 | } | |
209 | ||
891d0563 DE |
210 | void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
211 | { | |
1b1f8b2d | 212 | wxAutoNSAutoreleasePool pool; |
fe8f7943 | 213 | if(!CocoaTakeFocus()) return; |
891d0563 | 214 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)]; |
5eb5a0ac | 215 | CocoaSetPenForNSBezierPath(m_pen,bezpath); |
891d0563 | 216 | [bezpath stroke]; |
c3b0c2c3 DE |
217 | [m_brush.GetNSColor() set]; |
218 | [bezpath fill]; | |
891d0563 DE |
219 | } |
220 | ||
221 | void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2) | |
222 | { | |
1b1f8b2d | 223 | wxAutoNSAutoreleasePool pool; |
fe8f7943 | 224 | if(!CocoaTakeFocus()) return; |
891d0563 DE |
225 | NSBezierPath *bezpath = [NSBezierPath bezierPath]; |
226 | [bezpath moveToPoint:NSMakePoint(x1,y1)]; | |
227 | [bezpath lineToPoint:NSMakePoint(x2,y2)]; | |
c3b0c2c3 | 228 | |
5eb5a0ac | 229 | CocoaSetPenForNSBezierPath(m_pen,bezpath); |
891d0563 DE |
230 | [bezpath stroke]; |
231 | } | |
232 | ||
c94f845b | 233 | void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, const wxFont *theFont) const |
891d0563 | 234 | { |
7fc77f30 | 235 | wxAutoNSAutoreleasePool pool; |
891d0563 | 236 | // FIXME: Cache this so it can be used for DoDrawText |
2b030203 | 237 | wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!")); |
891d0563 | 238 | NSAttributedString *attributedString = [[NSAttributedString alloc] |
b0c0a393 | 239 | initWithString:wxNSStringWithWxString(text.c_str())]; |
891d0563 DE |
240 | [sm_cocoaNSTextStorage setAttributedString:attributedString]; |
241 | [attributedString release]; | |
242 | ||
243 | NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer]; | |
244 | NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer]; | |
245 | if(x) | |
dc483f61 | 246 | *x=(int)usedRect.size.width; |
891d0563 | 247 | if(y) |
dc483f61 | 248 | *y=(int)usedRect.size.height; |
891d0563 DE |
249 | if(descent) |
250 | *descent=0; | |
251 | if(externalLeading) | |
252 | *externalLeading=0; | |
253 | } | |
254 | ||
255 | void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y) | |
256 | { | |
1b1f8b2d | 257 | wxAutoNSAutoreleasePool pool; |
fe8f7943 | 258 | if(!CocoaTakeFocus()) return; |
2b030203 | 259 | wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!")); |
891d0563 | 260 | NSAttributedString *attributedString = [[NSAttributedString alloc] |
b0c0a393 | 261 | initWithString:wxNSStringWithWxString(text.c_str())]; |
891d0563 DE |
262 | [sm_cocoaNSTextStorage setAttributedString:attributedString]; |
263 | [attributedString release]; | |
264 | ||
c0440c78 DE |
265 | // Set the color (and later font) attributes |
266 | NSColor *fgColor = m_textForegroundColour.GetNSColor(); | |
267 | NSColor *bgColor = m_textBackgroundColour.GetNSColor(); | |
268 | if(!fgColor) | |
269 | fgColor = [NSColor clearColor]; | |
270 | if(!bgColor) | |
271 | bgColor = [NSColor clearColor]; | |
272 | NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys: | |
273 | fgColor, NSForegroundColorAttributeName, | |
274 | bgColor, NSBackgroundColorAttributeName, | |
275 | nil]; | |
276 | [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])]; | |
277 | [attrDict release]; | |
278 | ||
891d0563 DE |
279 | NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer]; |
280 | NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer]; | |
13fc3db4 DE |
281 | // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if |
282 | // there is no length or we don't start at zero | |
283 | if(!glyphRange.length) | |
284 | return; | |
2b030203 | 285 | wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero")); |
891d0563 DE |
286 | |
287 | NSAffineTransform *transform = [NSAffineTransform transform]; | |
288 | [transform translateXBy:x yBy:y]; | |
289 | ||
290 | NSAffineTransform *flipTransform = [NSAffineTransform transform]; | |
291 | /* x' = 1x + 0y + 0 | |
292 | y' = 0x + -1y + window's height | |
293 | */ | |
294 | NSAffineTransformStruct matrix = { | |
295 | 1, 0 | |
296 | , 0, -1 | |
297 | , 0, usedRect.size.height | |
298 | }; | |
299 | [flipTransform setTransformStruct: matrix]; | |
300 | ||
301 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
302 | [context saveGraphicsState]; | |
303 | [transform concat]; | |
304 | [flipTransform concat]; | |
c3b0c2c3 | 305 | #if 0 |
891d0563 | 306 | // Draw+fill a rectangle so we can see where the shit is supposed to be. |
48580976 | 307 | wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height); |
891d0563 DE |
308 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)]; |
309 | [[NSColor blackColor] set]; | |
310 | [bezpath stroke]; | |
311 | [[NSColor blueColor] set]; | |
312 | [bezpath fill]; | |
c3b0c2c3 | 313 | #endif |
891d0563 DE |
314 | |
315 | NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0]; | |
316 | layoutLocation.x = 0.0; | |
317 | layoutLocation.y *= -1.0; | |
318 | layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0]; | |
c0440c78 DE |
319 | if(m_backgroundMode==wxSOLID) |
320 | [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint]; | |
891d0563 DE |
321 | [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation]; |
322 | ||
323 | [context restoreGraphicsState]; | |
324 | } | |
325 | ||
2894667f DE |
326 | // wxDCBase functions |
327 | int wxDCBase::DeviceToLogicalX(int x) const | |
328 | { | |
329 | return x; | |
330 | } | |
331 | ||
332 | int wxDCBase::DeviceToLogicalY(int y) const | |
333 | { | |
334 | return y; | |
335 | } | |
336 | ||
276a1256 DE |
337 | int wxDCBase::DeviceToLogicalXRel(int x) const |
338 | { | |
339 | return x; | |
340 | } | |
341 | ||
342 | int wxDCBase::DeviceToLogicalYRel(int y) const | |
343 | { | |
344 | return y; | |
345 | } | |
346 | ||
2894667f DE |
347 | int wxDCBase::LogicalToDeviceX(int x) const |
348 | { | |
349 | return x; | |
350 | } | |
351 | ||
352 | int wxDCBase::LogicalToDeviceY(int y) const | |
353 | { | |
354 | return y; | |
355 | } | |
356 | ||
276a1256 DE |
357 | int wxDCBase::LogicalToDeviceXRel(int x) const |
358 | { | |
359 | return x; | |
360 | } | |
361 | ||
362 | int wxDCBase::LogicalToDeviceYRel(int y) const | |
363 | { | |
364 | return y; | |
365 | } | |
366 | ||
891d0563 DE |
367 | /////////////////////////////////////////////////////////////////////////// |
368 | // cut here, the rest is stubs | |
369 | /////////////////////////////////////////////////////////////////////////// | |
a24aff65 | 370 | |
a24aff65 DE |
371 | //----------------------------------------------------------------------------- |
372 | // wxDC | |
373 | //----------------------------------------------------------------------------- | |
374 | ||
a24aff65 DE |
375 | void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) ) |
376 | { | |
377 | }; | |
378 | ||
da80ae71 WS |
379 | void wxDC::DoDrawPoint( int x, int y ) |
380 | { | |
a24aff65 DE |
381 | }; |
382 | ||
383 | void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int) | |
384 | { | |
385 | }; | |
386 | ||
387 | void wxDC::DoDrawLines( int, wxPoint *, int, int ) | |
388 | { | |
389 | } | |
390 | ||
a24aff65 DE |
391 | int wxDC::GetDepth() const |
392 | { | |
393 | return 0; | |
394 | } | |
395 | ||
396 | wxSize wxDC::GetPPI() const | |
397 | { | |
398 | return wxSize(0,0); | |
399 | } | |
400 | ||
401 | bool wxDC::CanGetTextExtent() const | |
402 | { | |
403 | return false; | |
404 | } | |
405 | ||
a24aff65 DE |
406 | wxCoord wxDC::GetCharHeight() const |
407 | { | |
408 | return 0; | |
409 | } | |
410 | ||
411 | wxCoord wxDC::GetCharWidth() const | |
412 | { | |
413 | return 0; | |
414 | } | |
415 | ||
416 | bool wxDC::CanDrawBitmap() const | |
417 | { | |
1fd17880 | 418 | return true; |
a24aff65 DE |
419 | } |
420 | ||
421 | bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const | |
422 | { | |
423 | return false; | |
424 | } | |
425 | ||
426 | void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc) | |
427 | { | |
428 | } | |
da80ae71 | 429 | |
a24aff65 DE |
430 | void wxDC::SetPen(const wxPen& pen) |
431 | { | |
5eb5a0ac | 432 | m_pen = pen; |
a24aff65 DE |
433 | } |
434 | ||
435 | void wxDC::SetBrush(const wxBrush& brush) | |
436 | { | |
c3b0c2c3 | 437 | m_brush = brush; |
a24aff65 DE |
438 | } |
439 | ||
440 | void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region) | |
441 | { | |
442 | } | |
443 | ||
444 | void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height) | |
445 | { | |
446 | } | |
447 | ||
448 | void wxDC::DestroyClippingRegion() | |
449 | { | |
450 | } | |
451 | ||
452 | void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius) | |
453 | { | |
454 | } | |
455 | ||
456 | void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle) | |
457 | { | |
458 | } | |
459 | ||
460 | void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea) | |
461 | { | |
462 | } | |
463 | ||
a24aff65 DE |
464 | void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
465 | { | |
466 | } | |
467 | ||
468 | void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask) | |
469 | { | |
1b1f8b2d | 470 | wxAutoNSAutoreleasePool pool; |
fe8f7943 | 471 | if(!CocoaTakeFocus()) return; |
1fd17880 DE |
472 | if(!bmp.Ok()) |
473 | return; | |
474 | ||
475 | #if 0 | |
476 | // Draw a rect so we can see where it's supposed to be | |
48580976 | 477 | wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight()); |
1fd17880 DE |
478 | NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())]; |
479 | [[NSColor blackColor] set]; | |
480 | [bezpath stroke]; | |
481 | [[NSColor blueColor] set]; | |
482 | [bezpath fill]; | |
483 | #endif // 0 | |
484 | ||
485 | NSAffineTransform *transform = [NSAffineTransform transform]; | |
486 | [transform translateXBy:x yBy:y]; | |
487 | ||
488 | NSAffineTransform *flipTransform = [NSAffineTransform transform]; | |
489 | /* x' = 1x + 0y + 0 | |
490 | y' = 0x + -1y + window's height | |
491 | */ | |
492 | NSAffineTransformStruct matrix = { | |
493 | 1, 0 | |
494 | , 0, -1 | |
495 | , 0, bmp.GetHeight() | |
496 | }; | |
497 | [flipTransform setTransformStruct: matrix]; | |
498 | ||
499 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
500 | [context saveGraphicsState]; | |
501 | [transform concat]; | |
502 | [flipTransform concat]; | |
503 | ||
a00daa65 DE |
504 | NSImage *nsimage = [bmp.GetNSImage(useMask) retain]; |
505 | ||
1fd17880 DE |
506 | [nsimage drawAtPoint: NSMakePoint(0,0) |
507 | fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight()) | |
a00daa65 | 508 | operation: NSCompositeSourceOver |
1fd17880 | 509 | fraction: 1.0]; |
da80ae71 | 510 | |
1fd17880 DE |
511 | [nsimage release]; |
512 | [context restoreGraphicsState]; | |
a24aff65 DE |
513 | } |
514 | ||
515 | bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style) | |
516 | { | |
517 | return false; | |
518 | } | |
519 | ||
520 | void wxDC::DoCrossHair(wxCoord x, wxCoord y) | |
521 | { | |
522 | } | |
523 | ||
a24aff65 DE |
524 | |
525 | bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask , wxCoord xsrcMask, wxCoord ysrcMask) | |
2c23fe91 DE |
526 | { |
527 | if(!CocoaTakeFocus()) return false; | |
528 | if(!source) return false; | |
529 | return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height, | |
530 | xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask); | |
531 | } | |
532 | ||
533 | bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest, | |
534 | wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc, | |
535 | int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask) | |
a24aff65 DE |
536 | { |
537 | return false; | |
538 | } | |
539 | ||
540 | void wxDC::DoGetSize( int* width, int* height ) const | |
541 | { | |
542 | *width = m_maxX-m_minX; | |
543 | *height = m_maxY-m_minY; | |
544 | }; | |
545 | ||
546 | void wxDC::DoGetSizeMM( int* width, int* height ) const | |
547 | { | |
548 | int w = 0; | |
549 | int h = 0; | |
550 | GetSize( &w, &h ); | |
551 | }; | |
552 | ||
553 | void wxDC::SetTextForeground( const wxColour &col ) | |
554 | { | |
555 | if (!Ok()) return; | |
556 | m_textForegroundColour = col; | |
557 | }; | |
558 | ||
559 | void wxDC::SetTextBackground( const wxColour &col ) | |
560 | { | |
561 | if (!Ok()) return; | |
562 | m_textBackgroundColour = col; | |
563 | }; | |
564 | ||
565 | void wxDC::Clear() | |
566 | { | |
3e21fc05 DE |
567 | if(!CocoaTakeFocus()) return; |
568 | ||
569 | NSRect boundsRect; | |
570 | if(!CocoaGetBounds(&boundsRect)) return; | |
571 | ||
572 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
573 | [context saveGraphicsState]; | |
574 | ||
575 | // Undo all transforms so when we draw our bounds rect we | |
576 | // really overwrite our bounds rect. | |
577 | CocoaUnapplyTransformations(); | |
578 | ||
579 | [m_backgroundBrush.GetNSColor() set]; | |
580 | [NSBezierPath fillRect:boundsRect]; | |
581 | ||
582 | [context restoreGraphicsState]; | |
a24aff65 DE |
583 | } |
584 | ||
7bc429ef | 585 | void wxDC::SetBackground(const wxBrush& brush) |
a24aff65 | 586 | { |
7bc429ef | 587 | m_backgroundBrush = brush; |
a24aff65 DE |
588 | } |
589 | ||
590 | void wxDC::SetPalette(const wxPalette&) | |
591 | { | |
592 | } | |
593 | ||
594 | void wxDC::SetLogicalFunction(int) | |
595 | { | |
596 | } | |
597 | ||
598 | ||
599 | void wxDC::SetMapMode( int mode ) | |
600 | { | |
da80ae71 | 601 | switch (mode) |
a24aff65 DE |
602 | { |
603 | case wxMM_TWIPS: | |
604 | break; | |
605 | case wxMM_POINTS: | |
606 | break; | |
607 | case wxMM_METRIC: | |
608 | break; | |
609 | case wxMM_LOMETRIC: | |
610 | break; | |
611 | default: | |
612 | case wxMM_TEXT: | |
613 | SetLogicalScale( 1.0, 1.0 ); | |
614 | break; | |
615 | }; | |
616 | if (mode != wxMM_TEXT) | |
617 | { | |
618 | }; | |
619 | }; | |
620 | ||
621 | void wxDC::SetUserScale( double x, double y ) | |
622 | { | |
623 | // allow negative ? -> no | |
624 | m_userScaleX = x; | |
625 | m_userScaleY = y; | |
626 | ComputeScaleAndOrigin(); | |
627 | }; | |
628 | ||
629 | void wxDC::SetLogicalScale( double x, double y ) | |
630 | { | |
631 | // allow negative ? | |
632 | m_logicalScaleX = x; | |
633 | m_logicalScaleY = y; | |
634 | ComputeScaleAndOrigin(); | |
635 | }; | |
636 | ||
637 | void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y ) | |
638 | { | |
639 | m_logicalOriginX = x * m_signX; // is this still correct ? | |
640 | m_logicalOriginY = y * m_signY; | |
641 | ComputeScaleAndOrigin(); | |
642 | }; | |
643 | ||
644 | void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y ) | |
645 | { | |
646 | ComputeScaleAndOrigin(); | |
647 | }; | |
648 | ||
649 | void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
650 | { | |
651 | m_signX = (xLeftRight ? 1 : -1); | |
652 | m_signY = (yBottomUp ? -1 : 1); | |
653 | ComputeScaleAndOrigin(); | |
654 | }; | |
655 | ||
656 | void wxDC::ComputeScaleAndOrigin(void) | |
657 | { | |
658 | // CMB: copy scale to see if it changes | |
659 | double origScaleX = m_scaleX; | |
660 | double origScaleY = m_scaleY; | |
661 | ||
662 | m_scaleX = m_logicalScaleX * m_userScaleX; | |
663 | m_scaleY = m_logicalScaleY * m_userScaleY; | |
664 | ||
da80ae71 | 665 | // CMB: if scale has changed call SetPen to recalulate the line width |
a24aff65 DE |
666 | if (m_scaleX != origScaleX || m_scaleY != origScaleY) |
667 | { | |
668 | // this is a bit artificial, but we need to force wxDC to think | |
669 | // the pen has changed | |
10c5f652 | 670 | const wxPen* pen = & GetPen(); |
a24aff65 DE |
671 | wxPen tempPen; |
672 | m_pen = tempPen; | |
673 | SetPen(* pen); | |
674 | } | |
675 | }; |