]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dc.mm
no real changes; just reformatting
[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 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
28 wxDC *wxDC::sm_focusedDC = NULL;
29 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
30 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
31 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
32
33 void wxDC::CocoaInitializeTextSystem()
34 {
35 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
36
37 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
38
39 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
40 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
41 // NSTextStorage retains NSLayoutManager, but so do we
42 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
43
44 // NOTE: initWithContainerSize is the designated initializer, but the
45 // Apple CircleView sample gets away with just calling init, which
46 // is all we really need for our purposes.
47 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
48 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
49 // NSLayoutManager retains NSTextContainer, but so do we
50 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
51 }
52
53 void wxDC::CocoaShutdownTextSystem()
54 {
55 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
56 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
57 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
58 }
59
60 wxDC::wxDC(void)
61 {
62 }
63
64 wxDC::~wxDC(void)
65 {
66 }
67
68 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
69 {
70 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
71 [m_textForegroundColour.GetNSColor() set];
72 [bezpath stroke];
73 [m_brush.GetNSColor() set];
74 [bezpath fill];
75 }
76
77 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
78 {
79 NSBezierPath *bezpath = [NSBezierPath bezierPath];
80 [bezpath moveToPoint:NSMakePoint(x1,y1)];
81 [bezpath lineToPoint:NSMakePoint(x2,y2)];
82
83 [m_textForegroundColour.GetNSColor() set];
84 [bezpath stroke];
85 }
86
87 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
88 {
89 wxAutoNSAutoreleasePool pool;
90 // FIXME: Cache this so it can be used for DoDrawText
91 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
92 NSAttributedString *attributedString = [[NSAttributedString alloc]
93 initWithString:[NSString stringWithCString:text.c_str()]];
94 [sm_cocoaNSTextStorage setAttributedString:attributedString];
95 [attributedString release];
96
97 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
98 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
99 if(x)
100 *x=(int)usedRect.size.width;
101 if(y)
102 *y=(int)usedRect.size.height;
103 if(descent)
104 *descent=0;
105 if(externalLeading)
106 *externalLeading=0;
107 }
108
109 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
110 {
111 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
112 NSAttributedString *attributedString = [[NSAttributedString alloc]
113 initWithString:[NSString stringWithCString:text.c_str()]];
114 [sm_cocoaNSTextStorage setAttributedString:attributedString];
115 [attributedString release];
116
117 // Set the color (and later font) attributes
118 NSColor *fgColor = m_textForegroundColour.GetNSColor();
119 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
120 if(!fgColor)
121 fgColor = [NSColor clearColor];
122 if(!bgColor)
123 bgColor = [NSColor clearColor];
124 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
125 fgColor, NSForegroundColorAttributeName,
126 bgColor, NSBackgroundColorAttributeName,
127 nil];
128 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
129 [attrDict release];
130
131 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
132 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
133 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
134 // there is no length or we don't start at zero
135 if(!glyphRange.length)
136 return;
137 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
138
139 NSAffineTransform *transform = [NSAffineTransform transform];
140 [transform translateXBy:x yBy:y];
141
142 NSAffineTransform *flipTransform = [NSAffineTransform transform];
143 /* x' = 1x + 0y + 0
144 y' = 0x + -1y + window's height
145 */
146 NSAffineTransformStruct matrix = {
147 1, 0
148 , 0, -1
149 , 0, usedRect.size.height
150 };
151 [flipTransform setTransformStruct: matrix];
152
153 NSGraphicsContext *context = [NSGraphicsContext currentContext];
154 [context saveGraphicsState];
155 [transform concat];
156 [flipTransform concat];
157 #if 0
158 // Draw+fill a rectangle so we can see where the shit is supposed to be.
159 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
160 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
161 [[NSColor blackColor] set];
162 [bezpath stroke];
163 [[NSColor blueColor] set];
164 [bezpath fill];
165 #endif
166
167 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
168 layoutLocation.x = 0.0;
169 layoutLocation.y *= -1.0;
170 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
171 if(m_backgroundMode==wxSOLID)
172 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
173 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
174
175 [context restoreGraphicsState];
176 }
177
178 // wxDCBase functions
179 int wxDCBase::DeviceToLogicalX(int x) const
180 {
181 return x;
182 }
183
184 int wxDCBase::DeviceToLogicalY(int y) const
185 {
186 return y;
187 }
188
189 int wxDCBase::LogicalToDeviceX(int x) const
190 {
191 return x;
192 }
193
194 int wxDCBase::LogicalToDeviceY(int y) const
195 {
196 return y;
197 }
198
199 ///////////////////////////////////////////////////////////////////////////
200 // cut here, the rest is stubs
201 ///////////////////////////////////////////////////////////////////////////
202
203 //-----------------------------------------------------------------------------
204 // constants
205 //-----------------------------------------------------------------------------
206
207 #define mm2inches 0.0393700787402
208 #define inches2mm 25.4
209 #define mm2twips 56.6929133859
210 #define twips2mm 0.0176388888889
211 #define mm2pt 2.83464566929
212 #define pt2mm 0.352777777778
213
214 //-----------------------------------------------------------------------------
215 // wxDC
216 //-----------------------------------------------------------------------------
217
218 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
219 {
220 };
221
222 void wxDC::DoDrawPoint( int x, int y )
223 {
224 };
225
226 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
227 {
228 };
229
230 void wxDC::DoDrawLines( int, wxPoint *, int, int )
231 {
232 }
233
234 int wxDC::GetDepth() const
235 {
236 return 0;
237 }
238
239 wxSize wxDC::GetPPI() const
240 {
241 return wxSize(0,0);
242 }
243
244 bool wxDC::CanGetTextExtent() const
245 {
246 return false;
247 }
248
249 wxCoord wxDC::GetCharHeight() const
250 {
251 return 0;
252 }
253
254 wxCoord wxDC::GetCharWidth() const
255 {
256 return 0;
257 }
258
259 bool wxDC::CanDrawBitmap() const
260 {
261 return true;
262 }
263
264 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
265 {
266 return false;
267 }
268
269 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
270 {
271 }
272
273 void wxDC::SetPen(const wxPen& pen)
274 {
275 }
276
277 void wxDC::SetBrush(const wxBrush& brush)
278 {
279 m_brush = brush;
280 }
281
282 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
283 {
284 }
285
286 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
287 {
288 }
289
290 void wxDC::DestroyClippingRegion()
291 {
292 }
293
294 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
295 {
296 }
297
298 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
299 {
300 }
301
302 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
303 {
304 }
305
306 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
307 {
308 }
309
310 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
311 {
312 if(!bmp.Ok())
313 return;
314
315 #if 0
316 // Draw a rect so we can see where it's supposed to be
317 wxLogDebug("image at (%d,%d) size %dx%d",x,y,bmp.GetWidth(),bmp.GetHeight());
318 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
319 [[NSColor blackColor] set];
320 [bezpath stroke];
321 [[NSColor blueColor] set];
322 [bezpath fill];
323 #endif // 0
324
325 NSAffineTransform *transform = [NSAffineTransform transform];
326 [transform translateXBy:x yBy:y];
327
328 NSAffineTransform *flipTransform = [NSAffineTransform transform];
329 /* x' = 1x + 0y + 0
330 y' = 0x + -1y + window's height
331 */
332 NSAffineTransformStruct matrix = {
333 1, 0
334 , 0, -1
335 , 0, bmp.GetHeight()
336 };
337 [flipTransform setTransformStruct: matrix];
338
339 NSGraphicsContext *context = [NSGraphicsContext currentContext];
340 [context saveGraphicsState];
341 [transform concat];
342 [flipTransform concat];
343
344 NSImage *nsimage = [[NSImage alloc]
345 initWithSize:NSMakeSize(bmp.GetWidth(), bmp.GetHeight())];
346 [nsimage addRepresentation: const_cast<wxBitmap&>(bmp).GetNSBitmapImageRep()];
347 [nsimage drawAtPoint: NSMakePoint(0,0)
348 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
349 operation: NSCompositeCopy
350 fraction: 1.0];
351
352 [nsimage release];
353 [context restoreGraphicsState];
354 }
355
356 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
357 {
358 return false;
359 }
360
361 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
362 {
363 }
364
365
366 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)
367 {
368 return false;
369 }
370
371 void wxDC::DoGetSize( int* width, int* height ) const
372 {
373 *width = m_maxX-m_minX;
374 *height = m_maxY-m_minY;
375 };
376
377 void wxDC::DoGetSizeMM( int* width, int* height ) const
378 {
379 int w = 0;
380 int h = 0;
381 GetSize( &w, &h );
382 };
383
384 void wxDC::SetTextForeground( const wxColour &col )
385 {
386 if (!Ok()) return;
387 m_textForegroundColour = col;
388 };
389
390 void wxDC::SetTextBackground( const wxColour &col )
391 {
392 if (!Ok()) return;
393 m_textBackgroundColour = col;
394 };
395
396 void wxDC::Clear()
397 {
398 }
399
400 void wxDC::SetBackground(const wxBrush& brush)
401 {
402 m_backgroundBrush = brush;
403 }
404
405 void wxDC::SetPalette(const wxPalette&)
406 {
407 }
408
409 void wxDC::SetLogicalFunction(int)
410 {
411 }
412
413
414 void wxDC::SetMapMode( int mode )
415 {
416 switch (mode)
417 {
418 case wxMM_TWIPS:
419 break;
420 case wxMM_POINTS:
421 break;
422 case wxMM_METRIC:
423 break;
424 case wxMM_LOMETRIC:
425 break;
426 default:
427 case wxMM_TEXT:
428 SetLogicalScale( 1.0, 1.0 );
429 break;
430 };
431 if (mode != wxMM_TEXT)
432 {
433 };
434 };
435
436 void wxDC::SetUserScale( double x, double y )
437 {
438 // allow negative ? -> no
439 m_userScaleX = x;
440 m_userScaleY = y;
441 ComputeScaleAndOrigin();
442 };
443
444 void wxDC::SetLogicalScale( double x, double y )
445 {
446 // allow negative ?
447 m_logicalScaleX = x;
448 m_logicalScaleY = y;
449 ComputeScaleAndOrigin();
450 };
451
452 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
453 {
454 m_logicalOriginX = x * m_signX; // is this still correct ?
455 m_logicalOriginY = y * m_signY;
456 ComputeScaleAndOrigin();
457 };
458
459 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
460 {
461 ComputeScaleAndOrigin();
462 };
463
464 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
465 {
466 m_signX = (xLeftRight ? 1 : -1);
467 m_signY = (yBottomUp ? -1 : 1);
468 ComputeScaleAndOrigin();
469 };
470
471 void wxDC::ComputeScaleAndOrigin(void)
472 {
473 // CMB: copy scale to see if it changes
474 double origScaleX = m_scaleX;
475 double origScaleY = m_scaleY;
476
477 m_scaleX = m_logicalScaleX * m_userScaleX;
478 m_scaleY = m_logicalScaleY * m_userScaleY;
479
480 // CMB: if scale has changed call SetPen to recalulate the line width
481 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
482 {
483 // this is a bit artificial, but we need to force wxDC to think
484 // the pen has changed
485 wxPen* pen = & GetPen();
486 wxPen tempPen;
487 m_pen = tempPen;
488 SetPen(* pen);
489 }
490 };
491