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