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