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