]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dc.mm
removed the fudge from GetViewRect(), it isn't needed
[wxWidgets.git] / src / cocoa / dc.mm
CommitLineData
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
DE
8// Copyright: (c) 2003 David Elliott
9// Licence: wxWindows license
a24aff65
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/dc.h"
16#endif //WX_PRECOMP
a24aff65 17
7fc77f30
DE
18#include "wx/cocoa/autorelease.h"
19
891d0563
DE
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>
1fd17880 28#import <AppKit/NSImage.h>
891d0563 29
fe8f7943
DE
30#include <wx/listimpl.cpp>
31WX_DEFINE_LIST(wxCocoaDCStack);
32
a24aff65 33IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
891d0563
DE
34WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
35WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
36WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
fe8f7943 37wxCocoaDCStack wxDC::sm_cocoaDCStack;
891d0563 38
5eb5a0ac
DE
39inline 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
891d0563
DE
71void 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
91void 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
fe8f7943
DE
98void 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
122bool 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
891d0563
DE
140wxDC::wxDC(void)
141{
fe8f7943
DE
142 m_cocoaFlipped = false;
143 m_cocoaHeight = 0.0;
5eb5a0ac 144 m_pen = *wxBLACK_PEN;
891d0563
DE
145}
146
147wxDC::~wxDC(void)
148{
149}
150
fe8f7943
DE
151bool wxDC::CocoaLockFocus()
152{
153 return false;
154}
155
156bool wxDC::CocoaUnlockFocus()
157{
158 return false;
159}
160
161void 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
891d0563
DE
183void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
184{
fe8f7943 185 if(!CocoaTakeFocus()) return;
891d0563 186 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
5eb5a0ac 187 CocoaSetPenForNSBezierPath(m_pen,bezpath);
891d0563 188 [bezpath stroke];
c3b0c2c3
DE
189 [m_brush.GetNSColor() set];
190 [bezpath fill];
891d0563
DE
191}
192
193void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
194{
fe8f7943 195 if(!CocoaTakeFocus()) return;
891d0563
DE
196 NSBezierPath *bezpath = [NSBezierPath bezierPath];
197 [bezpath moveToPoint:NSMakePoint(x1,y1)];
198 [bezpath lineToPoint:NSMakePoint(x2,y2)];
c3b0c2c3 199
5eb5a0ac 200 CocoaSetPenForNSBezierPath(m_pen,bezpath);
891d0563
DE
201 [bezpath stroke];
202}
203
204void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
205{
7fc77f30 206 wxAutoNSAutoreleasePool pool;
891d0563
DE
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)
dc483f61 217 *x=(int)usedRect.size.width;
891d0563 218 if(y)
dc483f61 219 *y=(int)usedRect.size.height;
891d0563
DE
220 if(descent)
221 *descent=0;
222 if(externalLeading)
223 *externalLeading=0;
224}
225
226void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
227{
fe8f7943 228 if(!CocoaTakeFocus()) return;
891d0563
DE
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
c0440c78
DE
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
891d0563
DE
249 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
250 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
13fc3db4
DE
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");
891d0563
DE
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];
c3b0c2c3 275 #if 0
891d0563
DE
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];
c3b0c2c3 283 #endif
891d0563
DE
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];
c0440c78
DE
289 if(m_backgroundMode==wxSOLID)
290 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
891d0563
DE
291 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
292
293 [context restoreGraphicsState];
294}
295
2894667f
DE
296// wxDCBase functions
297int wxDCBase::DeviceToLogicalX(int x) const
298{
299 return x;
300}
301
302int wxDCBase::DeviceToLogicalY(int y) const
303{
304 return y;
305}
306
276a1256
DE
307int wxDCBase::DeviceToLogicalXRel(int x) const
308{
309 return x;
310}
311
312int wxDCBase::DeviceToLogicalYRel(int y) const
313{
314 return y;
315}
316
2894667f
DE
317int wxDCBase::LogicalToDeviceX(int x) const
318{
319 return x;
320}
321
322int wxDCBase::LogicalToDeviceY(int y) const
323{
324 return y;
325}
326
276a1256
DE
327int wxDCBase::LogicalToDeviceXRel(int x) const
328{
329 return x;
330}
331
332int wxDCBase::LogicalToDeviceYRel(int y) const
333{
334 return y;
335}
336
891d0563
DE
337///////////////////////////////////////////////////////////////////////////
338// cut here, the rest is stubs
339///////////////////////////////////////////////////////////////////////////
a24aff65
DE
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
a24aff65
DE
356void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
357{
358};
359
360void wxDC::DoDrawPoint( int x, int y )
361{
362};
363
364void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
365{
366};
367
368void wxDC::DoDrawLines( int, wxPoint *, int, int )
369{
370}
371
a24aff65
DE
372int wxDC::GetDepth() const
373{
374 return 0;
375}
376
377wxSize wxDC::GetPPI() const
378{
379 return wxSize(0,0);
380}
381
382bool wxDC::CanGetTextExtent() const
383{
384 return false;
385}
386
a24aff65
DE
387wxCoord wxDC::GetCharHeight() const
388{
389 return 0;
390}
391
392wxCoord wxDC::GetCharWidth() const
393{
394 return 0;
395}
396
397bool wxDC::CanDrawBitmap() const
398{
1fd17880 399 return true;
a24aff65
DE
400}
401
402bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
403{
404 return false;
405}
406
407void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
408{
409}
410
411void wxDC::SetPen(const wxPen& pen)
412{
5eb5a0ac 413 m_pen = pen;
a24aff65
DE
414}
415
416void wxDC::SetBrush(const wxBrush& brush)
417{
c3b0c2c3 418 m_brush = brush;
a24aff65
DE
419}
420
421void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
422{
423}
424
425void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
426{
427}
428
429void wxDC::DestroyClippingRegion()
430{
431}
432
433void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
434{
435}
436
437void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
438{
439}
440
441void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
442{
443}
444
a24aff65
DE
445void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
446{
447}
448
449void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
450{
fe8f7943 451 if(!CocoaTakeFocus()) return;
1fd17880
DE
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];
a24aff65
DE
494}
495
496bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
497{
498 return false;
499}
500
501void wxDC::DoCrossHair(wxCoord x, wxCoord y)
502{
503}
504
a24aff65
DE
505
506bool 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
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
514bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
515 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
516 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
a24aff65
DE
517{
518 return false;
519}
520
521void wxDC::DoGetSize( int* width, int* height ) const
522{
523 *width = m_maxX-m_minX;
524 *height = m_maxY-m_minY;
525};
526
527void wxDC::DoGetSizeMM( int* width, int* height ) const
528{
529 int w = 0;
530 int h = 0;
531 GetSize( &w, &h );
532};
533
534void wxDC::SetTextForeground( const wxColour &col )
535{
536 if (!Ok()) return;
537 m_textForegroundColour = col;
538};
539
540void wxDC::SetTextBackground( const wxColour &col )
541{
542 if (!Ok()) return;
543 m_textBackgroundColour = col;
544};
545
546void wxDC::Clear()
547{
548}
549
7bc429ef 550void wxDC::SetBackground(const wxBrush& brush)
a24aff65 551{
7bc429ef 552 m_backgroundBrush = brush;
a24aff65
DE
553}
554
555void wxDC::SetPalette(const wxPalette&)
556{
557}
558
559void wxDC::SetLogicalFunction(int)
560{
561}
562
563
564void 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
586void wxDC::SetUserScale( double x, double y )
587{
588 // allow negative ? -> no
589 m_userScaleX = x;
590 m_userScaleY = y;
591 ComputeScaleAndOrigin();
592};
593
594void wxDC::SetLogicalScale( double x, double y )
595{
596 // allow negative ?
597 m_logicalScaleX = x;
598 m_logicalScaleY = y;
599 ComputeScaleAndOrigin();
600};
601
602void 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
609void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
610{
611 ComputeScaleAndOrigin();
612};
613
614void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
615{
616 m_signX = (xLeftRight ? 1 : -1);
617 m_signY = (yBottomUp ? -1 : 1);
618 ComputeScaleAndOrigin();
619};
620
621void 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