]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/dc.mm
aui dock renumbering fix
[wxWidgets.git] / src / cocoa / dc.mm
... / ...
CommitLineData
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: wxWidgets licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/dc.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/math.h" //math constants
19#endif //WX_PRECOMP
20
21#include "wx/cocoa/autorelease.h"
22#include "wx/cocoa/string.h"
23
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>
31#import <AppKit/NSTypesetter.h>
32#import <AppKit/NSImage.h>
33
34#include "wx/listimpl.cpp"
35WX_DEFINE_LIST(wxCocoaDCStack);
36
37IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
38WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
39WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
40WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
41wxCocoaDCStack wxDC::sm_cocoaDCStack;
42
43inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
44{
45 [pen.GetNSColor() set];
46 const CGFloat *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
75void wxDC::CocoaInitializeTextSystem()
76{
77 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,wxT("Text system already initalized! BAD PROGRAMMER!"));
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
95void 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
102void wxDC::CocoaUnwindStackAndLoseFocus()
103{
104 wxCocoaDCStack::compatibility_iterator ourNode=sm_cocoaDCStack.Find(this);
105 if(ourNode)
106 {
107 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
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 {
115 wxFAIL_MSG(wxT("Unable to unlock focus on higher-level DC!"));
116 }
117 sm_cocoaDCStack.Erase(node);
118 }
119 wxASSERT(node==ourNode);
120 wxASSERT(ourNode->GetData() == this);
121 ourNode->GetData()->CocoaUnlockFocus();
122 sm_cocoaDCStack.Erase(ourNode);
123 }
124}
125
126bool wxDC::CocoaUnwindStackAndTakeFocus()
127{
128 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
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;
139 sm_cocoaDCStack.Erase(node);
140 }
141 return CocoaLockFocus();
142}
143
144wxDC::wxDC(void)
145{
146 m_cocoaWxToBoundsTransform = nil;
147 m_pen = *wxBLACK_PEN;
148}
149
150wxDC::~wxDC(void)
151{
152 [m_cocoaWxToBoundsTransform release];
153}
154
155bool wxDC::CocoaLockFocus()
156{
157 return false;
158}
159
160bool wxDC::CocoaUnlockFocus()
161{
162 return false;
163}
164
165/*static*/ WX_NSAffineTransform wxDC::CocoaGetWxToBoundsTransform(bool isFlipped, float height)
166{
167 NSAffineTransform *transform = nil;
168 // This transform flips the graphics since wxDC uses top-left origin
169 if(!isFlipped)
170 {
171 // The transform is auto released
172 transform = [NSAffineTransform transform];
173 /* x' = 1x + 0y + 0
174 y' = 0x + -1y + window's height
175 */
176 NSAffineTransformStruct matrix = {
177 1, 0
178 , 0, -1
179 , 0, height
180 };
181 [transform setTransformStruct: matrix];
182 }
183 return transform;
184}
185
186void wxDC::CocoaApplyTransformations()
187{
188 [m_cocoaWxToBoundsTransform concat];
189 // TODO: Apply device/logical/user position/scaling transformations
190}
191
192void 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];
201 [invertTransform release];
202}
203
204bool wxDC::CocoaGetBounds(void *rectData)
205{
206 // We don't know what we are so we can't return anything.
207 return false;
208}
209
210void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
211{
212 wxAutoNSAutoreleasePool pool;
213 if(!CocoaTakeFocus()) return;
214 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
215 CocoaSetPenForNSBezierPath(m_pen,bezpath);
216 [bezpath stroke];
217 [m_brush.GetNSColor() set];
218 [bezpath fill];
219}
220
221void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
222{
223 wxAutoNSAutoreleasePool pool;
224 if(!CocoaTakeFocus()) return;
225 NSBezierPath *bezpath = [NSBezierPath bezierPath];
226 [bezpath moveToPoint:NSMakePoint(x1,y1)];
227 [bezpath lineToPoint:NSMakePoint(x2,y2)];
228
229 CocoaSetPenForNSBezierPath(m_pen,bezpath);
230 [bezpath stroke];
231}
232
233void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, const wxFont *theFont) const
234{
235 wxAutoNSAutoreleasePool pool;
236// FIXME: Cache this so it can be used for DoDrawText
237 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
238 NSAttributedString *attributedString = [[NSAttributedString alloc]
239 initWithString:wxNSStringWithWxString(text.c_str())];
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)
246 *x=(int)usedRect.size.width;
247 if(y)
248 *y=(int)usedRect.size.height;
249 if(descent)
250 *descent=0;
251 if(externalLeading)
252 *externalLeading=0;
253}
254
255void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
256{
257 wxAutoNSAutoreleasePool pool;
258 if(!CocoaTakeFocus()) return;
259 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
260 NSAttributedString *attributedString = [[NSAttributedString alloc]
261 initWithString:wxNSStringWithWxString(text.c_str())];
262 [sm_cocoaNSTextStorage setAttributedString:attributedString];
263 [attributedString release];
264
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
279 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
280 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
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;
285 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
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];
305 #if 0
306 // Draw+fill a rectangle so we can see where the shit is supposed to be.
307 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
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];
313 #endif
314
315 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
316 layoutLocation.x = 0.0;
317 layoutLocation.y *= -1.0;
318
319 // Save the location as is for underlining
320 NSPoint underlineLocation = layoutLocation;
321
322 // Offset the location by the baseline for drawing the glyphs.
323 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
324
325 if(m_backgroundMode==wxSOLID)
326 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
327 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
328
329 int underlineStyle = GetFont().GetUnderlined() ? NSUnderlineStyleSingle : NSUnderlineStyleNone;
330 NSRange lineGlyphRange;
331 NSRect lineRect = [sm_cocoaNSLayoutManager lineFragmentRectForGlyphAtIndex:0 effectiveRange:&lineGlyphRange];
332
333 [sm_cocoaNSLayoutManager underlineGlyphRange:glyphRange underlineType:underlineStyle
334 lineFragmentRect:lineRect lineFragmentGlyphRange:lineGlyphRange
335 containerOrigin:underlineLocation];
336
337 [context restoreGraphicsState];
338}
339
340///////////////////////////////////////////////////////////////////////////
341// cut here, the rest is stubs
342///////////////////////////////////////////////////////////////////////////
343
344//-----------------------------------------------------------------------------
345// wxDC
346//-----------------------------------------------------------------------------
347
348void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
349{
350};
351
352void wxDC::DoDrawPoint( int x, int y )
353{
354};
355
356void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
357{
358};
359
360void wxDC::DoDrawLines( int, wxPoint *, int, int )
361{
362}
363
364int wxDC::GetDepth() const
365{
366 return 0;
367}
368
369wxSize wxDC::GetPPI() const
370{
371 return wxSize(0,0);
372}
373
374bool wxDC::CanGetTextExtent() const
375{
376 return false;
377}
378
379wxCoord wxDC::GetCharHeight() const
380{
381 return 0;
382}
383
384wxCoord wxDC::GetCharWidth() const
385{
386 return 0;
387}
388
389bool wxDC::CanDrawBitmap() const
390{
391 return true;
392}
393
394bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
395{
396 return false;
397}
398
399void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
400{
401}
402
403void wxDC::SetFont(const wxFont& font)
404{
405 m_font = font;
406}
407
408void wxDC::SetPen(const wxPen& pen)
409{
410 m_pen = pen;
411}
412
413void wxDC::SetBrush(const wxBrush& brush)
414{
415 m_brush = brush;
416}
417
418void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
419{
420}
421
422void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
423{
424}
425
426void wxDC::DestroyClippingRegion()
427{
428}
429
430void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
431{
432}
433
434void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
435{
436}
437
438void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
439{
440}
441
442void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
443{
444}
445
446void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
447{
448 wxAutoNSAutoreleasePool pool;
449 if(!CocoaTakeFocus()) return;
450 if(!bmp.Ok())
451 return;
452
453#if 0
454 // Draw a rect so we can see where it's supposed to be
455 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
456 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
457 [[NSColor blackColor] set];
458 [bezpath stroke];
459 [[NSColor blueColor] set];
460 [bezpath fill];
461#endif // 0
462
463 NSAffineTransform *transform = [NSAffineTransform transform];
464 [transform translateXBy:x yBy:y];
465
466 NSAffineTransform *flipTransform = [NSAffineTransform transform];
467 /* x' = 1x + 0y + 0
468 y' = 0x + -1y + window's height
469 */
470 NSAffineTransformStruct matrix = {
471 1, 0
472 , 0, -1
473 , 0, bmp.GetHeight()
474 };
475 [flipTransform setTransformStruct: matrix];
476
477 NSGraphicsContext *context = [NSGraphicsContext currentContext];
478 [context saveGraphicsState];
479 [transform concat];
480 [flipTransform concat];
481
482 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
483
484 [nsimage drawAtPoint: NSMakePoint(0,0)
485 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
486 operation: NSCompositeSourceOver
487 fraction: 1.0];
488
489 [nsimage release];
490 [context restoreGraphicsState];
491}
492
493bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
494{
495 return false;
496}
497
498void wxDC::DoCrossHair(wxCoord x, wxCoord y)
499{
500}
501
502
503bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask , wxCoord xsrcMask, wxCoord ysrcMask)
504{
505 if(!CocoaTakeFocus()) return false;
506 if(!source) return false;
507 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
508 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
509}
510
511bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
512 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
513 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
514{
515 return false;
516}
517
518void wxDC::DoGetSize( int* width, int* height ) const
519{
520 *width = m_maxX-m_minX;
521 *height = m_maxY-m_minY;
522};
523
524void wxDC::DoGetSizeMM( int* width, int* height ) const
525{
526 int w = 0;
527 int h = 0;
528 GetSize( &w, &h );
529};
530
531void wxDC::SetTextForeground( const wxColour &col )
532{
533 if (!Ok()) return;
534 m_textForegroundColour = col;
535};
536
537void wxDC::SetTextBackground( const wxColour &col )
538{
539 if (!Ok()) return;
540 m_textBackgroundColour = col;
541};
542
543void wxDC::Clear()
544{
545 if(!CocoaTakeFocus()) return;
546
547 NSRect boundsRect;
548 if(!CocoaGetBounds(&boundsRect)) return;
549
550 NSGraphicsContext *context = [NSGraphicsContext currentContext];
551 [context saveGraphicsState];
552
553 // Undo all transforms so when we draw our bounds rect we
554 // really overwrite our bounds rect.
555 CocoaUnapplyTransformations();
556
557 [m_backgroundBrush.GetNSColor() set];
558 [NSBezierPath fillRect:boundsRect];
559
560 [context restoreGraphicsState];
561}
562
563void wxDC::SetBackground(const wxBrush& brush)
564{
565 m_backgroundBrush = brush;
566}
567
568void wxDC::SetPalette(const wxPalette&)
569{
570}
571
572void wxDC::SetLogicalFunction(int)
573{
574}
575
576
577void wxDC::SetMapMode( int mode )
578{
579 switch (mode)
580 {
581 case wxMM_TWIPS:
582 break;
583 case wxMM_POINTS:
584 break;
585 case wxMM_METRIC:
586 break;
587 case wxMM_LOMETRIC:
588 break;
589 default:
590 case wxMM_TEXT:
591 SetLogicalScale( 1.0, 1.0 );
592 break;
593 };
594 if (mode != wxMM_TEXT)
595 {
596 };
597};
598
599void wxDC::SetUserScale( double x, double y )
600{
601 // allow negative ? -> no
602 m_userScaleX = x;
603 m_userScaleY = y;
604 ComputeScaleAndOrigin();
605};
606
607void wxDC::SetLogicalScale( double x, double y )
608{
609 // allow negative ?
610 m_logicalScaleX = x;
611 m_logicalScaleY = y;
612 ComputeScaleAndOrigin();
613};
614
615void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
616{
617 m_logicalOriginX = x * m_signX; // is this still correct ?
618 m_logicalOriginY = y * m_signY;
619 ComputeScaleAndOrigin();
620};
621
622void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
623{
624 ComputeScaleAndOrigin();
625};
626
627void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
628{
629 m_signX = (xLeftRight ? 1 : -1);
630 m_signY = (yBottomUp ? -1 : 1);
631 ComputeScaleAndOrigin();
632};
633
634void wxDC::ComputeScaleAndOrigin(void)
635{
636 // CMB: copy scale to see if it changes
637 double origScaleX = m_scaleX;
638 double origScaleY = m_scaleY;
639
640 m_scaleX = m_logicalScaleX * m_userScaleX;
641 m_scaleY = m_logicalScaleY * m_userScaleY;
642
643 // CMB: if scale has changed call SetPen to recalulate the line width
644 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
645 {
646 // this is a bit artificial, but we need to force wxDC to think
647 // the pen has changed
648 const wxPen* pen = & GetPen();
649 wxPen tempPen;
650 m_pen = tempPen;
651 SetPen(* pen);
652 }
653};