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