]> git.saurik.com Git - wxWidgets.git/blob - src/common/dcbase.cpp
5e11a81ee5761ae9b1ba9fc7eb019258ac29830f
[wxWidgets.git] / src / common / dcbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcbase.cpp
3 // Purpose: generic methods of the wxDC Class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 05/25/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dc.h"
28 #include "wx/dcclient.h"
29 #include "wx/dcmemory.h"
30 #include "wx/dcscreen.h"
31 #include "wx/dcprint.h"
32 #include "wx/prntbase.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/math.h"
36 #endif
37
38 #ifdef __WXMSW__
39 #include "wx/msw/dcclient.h"
40 #include "wx/msw/dcmemory.h"
41 #include "wx/msw/dcscreen.h"
42 #endif
43
44 #ifdef __WXGTK__
45 #include "wx/gtk/dcclient.h"
46 #include "wx/gtk/dcmemory.h"
47 #include "wx/gtk/dcscreen.h"
48 #endif
49
50 #ifdef __WXMAC__
51 #include "wx/mac/dcclient.h"
52 #include "wx/mac/dcmemory.h"
53 #include "wx/mac/dcscreen.h"
54 #endif
55
56 #ifdef __WXX11__
57 #include "wx/x11/dcclient.h"
58 #include "wx/x11/dcmemory.h"
59 #include "wx/x11/dcscreen.h"
60 #endif
61
62 //----------------------------------------------------------------------------
63 // wxDCFactory
64 //----------------------------------------------------------------------------
65
66 wxDCFactory *wxDCFactory::m_factory = NULL;
67
68 void wxDCFactory::SetDCFactory( wxDCFactory *factory )
69 {
70 if (wxDCFactory::m_factory)
71 delete wxDCFactory::m_factory;
72
73 wxDCFactory::m_factory = factory;
74 }
75
76 wxDCFactory *wxDCFactory::Get()
77 {
78 if (!wxDCFactory::m_factory)
79 wxDCFactory::m_factory = new wxNativeDCFactory;
80
81 return wxDCFactory::m_factory;
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxNativeDCFactory
86 //-----------------------------------------------------------------------------
87
88 wxDCImpl* wxNativeDCFactory::CreateWindowDC( wxWindowDC *owner )
89 {
90 return new wxWindowDCImpl( owner );
91 }
92
93 wxDCImpl* wxNativeDCFactory::CreateWindowDC( wxWindowDC *owner, wxWindow *window )
94 {
95 return new wxWindowDCImpl( owner, window );
96 }
97
98 wxDCImpl* wxNativeDCFactory::CreateClientDC( wxClientDC *owner )
99 {
100 return new wxClientDCImpl( owner );
101 }
102
103 wxDCImpl* wxNativeDCFactory::CreateClientDC( wxClientDC *owner, wxWindow *window )
104 {
105 return new wxClientDCImpl( owner, window );
106 }
107
108 wxDCImpl* wxNativeDCFactory::CreatePaintDC( wxPaintDC *owner )
109 {
110 return new wxPaintDCImpl( owner );
111 }
112
113 wxDCImpl* wxNativeDCFactory::CreatePaintDC( wxPaintDC *owner, wxWindow *window )
114 {
115 return new wxPaintDCImpl( owner, window );
116 }
117
118 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner )
119 {
120 return new wxMemoryDCImpl( owner );
121 }
122
123 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner, wxBitmap &bitmap )
124 {
125 return new wxMemoryDCImpl( owner, bitmap );
126 }
127
128 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner, wxDC *dc )
129 {
130 return new wxMemoryDCImpl( owner, dc );
131 }
132
133 wxDCImpl* wxNativeDCFactory::CreateScreenDC( wxScreenDC *owner )
134 {
135 return new wxScreenDCImpl( owner );
136 }
137
138 #if wxUSE_PRINTING_ARCHITECTURE
139 wxDCImpl *wxNativeDCFactory::CreatePrinterDC( wxPrinterDC *owner, const wxPrintData &data )
140 {
141 wxPrintFactory *factory = wxPrintFactory::GetFactory();
142 return factory->CreatePrinterDCImpl( owner, data );
143 }
144 #endif
145
146 //-----------------------------------------------------------------------------
147 // wxWindowDC
148 //-----------------------------------------------------------------------------
149
150 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC, wxDC)
151
152 wxWindowDC::wxWindowDC(wxWindow *win)
153 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win))
154 {
155 }
156
157 //-----------------------------------------------------------------------------
158 // wxClientDC
159 //-----------------------------------------------------------------------------
160
161 IMPLEMENT_ABSTRACT_CLASS(wxClientDC, wxWindowDC)
162
163 wxClientDC::wxClientDC(wxWindow *win)
164 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win))
165 {
166 }
167
168 //-----------------------------------------------------------------------------
169 // wxMemoryDC
170 //-----------------------------------------------------------------------------
171
172 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
173
174 wxMemoryDC::wxMemoryDC()
175 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
176 {
177 }
178
179 wxMemoryDC::wxMemoryDC(wxBitmap& bitmap)
180 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap))
181 {
182 }
183
184 wxMemoryDC::wxMemoryDC(wxDC *dc)
185 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc))
186 {
187 }
188
189 void wxMemoryDC::SelectObject(wxBitmap& bmp)
190 {
191 // make sure that the given wxBitmap is not sharing its data with other
192 // wxBitmap instances as its contents will be modified by any drawing
193 // operation done on this DC
194 if (bmp.IsOk())
195 bmp.UnShare();
196
197 GetImpl()->DoSelect(bmp);
198 }
199
200 void wxMemoryDC::SelectObjectAsSource(const wxBitmap& bmp)
201 {
202 GetImpl()->DoSelect(bmp);
203 }
204
205 const wxBitmap& wxMemoryDC::GetSelectedBitmap() const
206 {
207 return GetImpl()->GetSelectedBitmap();
208 }
209
210 wxBitmap& wxMemoryDC::GetSelectedBitmap()
211 {
212 return GetImpl()->GetSelectedBitmap();
213 }
214
215
216 //-----------------------------------------------------------------------------
217 // wxPaintDC
218 //-----------------------------------------------------------------------------
219
220 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC, wxClientDC)
221
222 wxPaintDC::wxPaintDC(wxWindow *win)
223 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win))
224 {
225 }
226
227 //-----------------------------------------------------------------------------
228 // wxScreenDC
229 //-----------------------------------------------------------------------------
230
231 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC, wxWindowDC)
232
233 wxScreenDC::wxScreenDC()
234 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
235 {
236 }
237
238 //-----------------------------------------------------------------------------
239 // wxPrinterDC
240 //-----------------------------------------------------------------------------
241
242 #if wxUSE_PRINTING_ARCHITECTURE
243
244 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC, wxDC)
245
246 wxPrinterDC::wxPrinterDC()
247 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
248 {
249 }
250
251 wxPrinterDC::wxPrinterDC(const wxPrintData& data)
252 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data))
253 {
254 }
255
256 wxRect wxPrinterDC::GetPaperRect()
257 {
258 return GetImpl()->GetPaperRect();
259 }
260
261 int wxPrinterDC::GetResolution()
262 {
263 return GetImpl()->GetResolution();
264 }
265
266 #endif // wxUSE_PRINTING_ARCHITECTURE
267
268 //-----------------------------------------------------------------------------
269 // wxDCImpl
270 //-----------------------------------------------------------------------------
271
272 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl, wxObject)
273
274 wxDCImpl::wxDCImpl( wxDC *owner )
275 : m_window(NULL)
276 , m_colour(wxColourDisplay())
277 , m_ok(true)
278 , m_clipping(false)
279 , m_isInteractive(0)
280 , m_isBBoxValid(false)
281 , m_logicalOriginX(0), m_logicalOriginY(0)
282 , m_deviceOriginX(0), m_deviceOriginY(0)
283 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
284 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
285 , m_userScaleX(1.0), m_userScaleY(1.0)
286 , m_scaleX(1.0), m_scaleY(1.0)
287 , m_signX(1), m_signY(1)
288 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
289 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
290 , m_logicalFunction(wxCOPY)
291 , m_backgroundMode(wxTRANSPARENT)
292 , m_mappingMode(wxMM_TEXT)
293 , m_pen()
294 , m_brush()
295 , m_backgroundBrush(*wxTRANSPARENT_BRUSH)
296 , m_textForegroundColour(*wxBLACK)
297 , m_textBackgroundColour(*wxWHITE)
298 , m_font()
299 #if wxUSE_PALETTE
300 , m_palette()
301 , m_hasCustomPalette(false)
302 #endif // wxUSE_PALETTE
303 {
304 m_owner = owner;
305
306 m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
307 (double)wxGetDisplaySizeMM().GetWidth();
308 m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
309 (double)wxGetDisplaySizeMM().GetHeight();
310
311 ResetBoundingBox();
312 ResetClipping();
313 }
314
315 wxDCImpl::~wxDCImpl()
316 {
317 }
318
319 // ----------------------------------------------------------------------------
320 // coordinate conversions and transforms
321 // ----------------------------------------------------------------------------
322
323 wxCoord wxDCImpl::DeviceToLogicalX(wxCoord x) const
324 {
325 return wxRound((double)(x - m_deviceOriginX - m_deviceLocalOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
326 }
327
328 wxCoord wxDCImpl::DeviceToLogicalY(wxCoord y) const
329 {
330 return wxRound((double)(y - m_deviceOriginY - m_deviceLocalOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
331 }
332
333 wxCoord wxDCImpl::DeviceToLogicalXRel(wxCoord x) const
334 {
335 return wxRound((double)(x) / m_scaleX);
336 }
337
338 wxCoord wxDCImpl::DeviceToLogicalYRel(wxCoord y) const
339 {
340 return wxRound((double)(y) / m_scaleY);
341 }
342
343 wxCoord wxDCImpl::LogicalToDeviceX(wxCoord x) const
344 {
345 return wxRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX * m_signY + m_deviceLocalOriginX;
346 }
347
348 wxCoord wxDCImpl::LogicalToDeviceY(wxCoord y) const
349 {
350 return wxRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY * m_signY + m_deviceLocalOriginY;
351 }
352
353 wxCoord wxDCImpl::LogicalToDeviceXRel(wxCoord x) const
354 {
355 return wxRound((double)(x) * m_scaleX);
356 }
357
358 wxCoord wxDCImpl::LogicalToDeviceYRel(wxCoord y) const
359 {
360 return wxRound((double)(y) * m_scaleY);
361 }
362
363 void wxDCImpl::ComputeScaleAndOrigin()
364 {
365 m_scaleX = m_logicalScaleX * m_userScaleX;
366 m_scaleY = m_logicalScaleY * m_userScaleY;
367 }
368
369 void wxDCImpl::SetMapMode( int mode )
370 {
371 switch (mode)
372 {
373 case wxMM_TWIPS:
374 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
375 break;
376 case wxMM_POINTS:
377 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
378 break;
379 case wxMM_METRIC:
380 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
381 break;
382 case wxMM_LOMETRIC:
383 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
384 break;
385 default:
386 case wxMM_TEXT:
387 SetLogicalScale( 1.0, 1.0 );
388 break;
389 }
390 m_mappingMode = mode;
391 }
392
393 void wxDCImpl::SetUserScale( double x, double y )
394 {
395 // allow negative ? -> no
396 m_userScaleX = x;
397 m_userScaleY = y;
398 ComputeScaleAndOrigin();
399 }
400
401 void wxDCImpl::SetLogicalScale( double x, double y )
402 {
403 // allow negative ?
404 m_logicalScaleX = x;
405 m_logicalScaleY = y;
406 ComputeScaleAndOrigin();
407 }
408
409 void wxDCImpl::SetLogicalOrigin( wxCoord x, wxCoord y )
410 {
411 m_logicalOriginX = x * m_signX;
412 m_logicalOriginY = y * m_signY;
413 ComputeScaleAndOrigin();
414 }
415
416 void wxDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
417 {
418 m_deviceOriginX = x;
419 m_deviceOriginY = y;
420 ComputeScaleAndOrigin();
421 }
422
423 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x, wxCoord y )
424 {
425 m_deviceLocalOriginX = x;
426 m_deviceLocalOriginY = y;
427 ComputeScaleAndOrigin();
428 }
429
430 void wxDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
431 {
432 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
433 // wxWidgets 2.9: no longer override it
434 m_signX = (xLeftRight ? 1 : -1);
435 m_signY = (yBottomUp ? -1 : 1);
436 ComputeScaleAndOrigin();
437 }
438
439
440 // Each element of the widths array will be the width of the string up to and
441 // including the corresponding character in text. This is the generic
442 // implementation, the port-specific classes should do this with native APIs
443 // if available and if faster. Note: pango_layout_index_to_pos is much slower
444 // than calling GetTextExtent!!
445
446 #define FWC_SIZE 256
447
448 class FontWidthCache
449 {
450 public:
451 FontWidthCache() : m_scaleX(1), m_widths(NULL) { }
452 ~FontWidthCache() { delete []m_widths; }
453
454 void Reset()
455 {
456 if (!m_widths)
457 m_widths = new int[FWC_SIZE];
458
459 memset(m_widths, 0, sizeof(int)*FWC_SIZE);
460 }
461
462 wxFont m_font;
463 double m_scaleX;
464 int *m_widths;
465 };
466
467 static FontWidthCache s_fontWidthCache;
468
469 bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
470 {
471 int totalWidth = 0;
472
473 const size_t len = text.length();
474 widths.Empty();
475 widths.Add(0, len);
476
477 // reset the cache if font or horizontal scale have changed
478 if ( !s_fontWidthCache.m_widths ||
479 !wxIsSameDouble(s_fontWidthCache.m_scaleX, m_scaleX) ||
480 (s_fontWidthCache.m_font != GetFont()) )
481 {
482 s_fontWidthCache.Reset();
483 s_fontWidthCache.m_font = GetFont();
484 s_fontWidthCache.m_scaleX = m_scaleX;
485 }
486
487 // Calculate the position of each character based on the widths of
488 // the previous characters
489 int w, h;
490 for ( size_t i = 0; i < len; i++ )
491 {
492 const wxChar c = text[i];
493 unsigned int c_int = (unsigned int)c;
494
495 if ((c_int < FWC_SIZE) && (s_fontWidthCache.m_widths[c_int] != 0))
496 {
497 w = s_fontWidthCache.m_widths[c_int];
498 }
499 else
500 {
501 DoGetTextExtent(c, &w, &h);
502 if (c_int < FWC_SIZE)
503 s_fontWidthCache.m_widths[c_int] = w;
504 }
505
506 totalWidth += w;
507 widths[i] = totalWidth;
508 }
509
510 return true;
511 }
512
513 void wxDCImpl::GetMultiLineTextExtent(const wxString& text,
514 wxCoord *x,
515 wxCoord *y,
516 wxCoord *h,
517 const wxFont *font) const
518 {
519 wxCoord widthTextMax = 0, widthLine,
520 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
521
522 wxString curLine;
523 for ( wxString::const_iterator pc = text.begin(); ; ++pc )
524 {
525 if ( pc == text.end() || *pc == _T('\n') )
526 {
527 if ( curLine.empty() )
528 {
529 // we can't use GetTextExtent - it will return 0 for both width
530 // and height and an empty line should count in height
531 // calculation
532
533 // assume that this line has the same height as the previous
534 // one
535 if ( !heightLineDefault )
536 heightLineDefault = heightLine;
537
538 if ( !heightLineDefault )
539 {
540 // but we don't know it yet - choose something reasonable
541 DoGetTextExtent(_T("W"), NULL, &heightLineDefault,
542 NULL, NULL, font);
543 }
544
545 heightTextTotal += heightLineDefault;
546 }
547 else
548 {
549 DoGetTextExtent(curLine, &widthLine, &heightLine,
550 NULL, NULL, font);
551 if ( widthLine > widthTextMax )
552 widthTextMax = widthLine;
553 heightTextTotal += heightLine;
554 }
555
556 if ( pc == text.end() )
557 {
558 break;
559 }
560 else // '\n'
561 {
562 curLine.clear();
563 }
564 }
565 else
566 {
567 curLine += *pc;
568 }
569 }
570
571 if ( x )
572 *x = widthTextMax;
573 if ( y )
574 *y = heightTextTotal;
575 if ( h )
576 *h = heightLine;
577 }
578
579 void wxDCImpl::DoDrawCheckMark(wxCoord x1, wxCoord y1,
580 wxCoord width, wxCoord height)
581 {
582 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
583
584 wxCoord x2 = x1 + width,
585 y2 = y1 + height;
586
587 // the pen width is calibrated to give 3 for width == height == 10
588 wxDCPenChanger pen( *m_owner, wxPen(GetTextForeground(), (width + height + 1)/7));
589
590 // we're drawing a scaled version of wx/generic/tick.xpm here
591 wxCoord x3 = x1 + (4*width) / 10, // x of the tick bottom
592 y3 = y1 + height / 2; // y of the left tick branch
593 DoDrawLine(x1, y3, x3, y2);
594 DoDrawLine(x3, y2, x2, y1);
595
596 CalcBoundingBox(x1, y1);
597 CalcBoundingBox(x2, y2);
598 }
599
600 bool
601 wxDCImpl::DoStretchBlit(wxCoord xdest, wxCoord ydest,
602 wxCoord dstWidth, wxCoord dstHeight,
603 wxDC *source,
604 wxCoord xsrc, wxCoord ysrc,
605 wxCoord srcWidth, wxCoord srcHeight,
606 int rop,
607 bool useMask,
608 wxCoord xsrcMask,
609 wxCoord ysrcMask)
610 {
611 wxCHECK_MSG( srcWidth && srcHeight && dstWidth && dstHeight, false,
612 _T("invalid blit size") );
613
614 // emulate the stretching by modifying the DC scale
615 double xscale = (double)srcWidth/dstWidth,
616 yscale = (double)srcHeight/dstHeight;
617
618 double xscaleOld, yscaleOld;
619 GetUserScale(&xscaleOld, &yscaleOld);
620 SetUserScale(xscaleOld/xscale, yscaleOld/yscale);
621
622 bool rc = DoBlit(wxCoord(xdest*xscale), wxCoord(ydest*yscale),
623 wxCoord(dstWidth*xscale), wxCoord(dstHeight*yscale),
624 source,
625 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
626
627 SetUserScale(xscaleOld, yscaleOld);
628
629 return rc;
630 }
631
632 void wxDCImpl::DrawLines(const wxPointList *list, wxCoord xoffset, wxCoord yoffset)
633 {
634 int n = list->GetCount();
635 wxPoint *points = new wxPoint[n];
636
637 int i = 0;
638 for ( wxPointList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
639 {
640 wxPoint *point = node->GetData();
641 points[i].x = point->x;
642 points[i].y = point->y;
643 }
644
645 DoDrawLines(n, points, xoffset, yoffset);
646
647 delete [] points;
648 }
649
650 void wxDCImpl::DrawPolygon(const wxPointList *list,
651 wxCoord xoffset, wxCoord yoffset,
652 int fillStyle)
653 {
654 int n = list->GetCount();
655 wxPoint *points = new wxPoint[n];
656
657 int i = 0;
658 for ( wxPointList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
659 {
660 wxPoint *point = node->GetData();
661 points[i].x = point->x;
662 points[i].y = point->y;
663 }
664
665 DoDrawPolygon(n, points, xoffset, yoffset, fillStyle);
666
667 delete [] points;
668 }
669
670 void
671 wxDCImpl::DoDrawPolyPolygon(int n,
672 int count[],
673 wxPoint points[],
674 wxCoord xoffset, wxCoord yoffset,
675 int fillStyle)
676 {
677 if ( n == 1 )
678 {
679 DoDrawPolygon(count[0], points, xoffset, yoffset, fillStyle);
680 return;
681 }
682
683 int i, j, lastOfs;
684 wxPoint* pts;
685 wxPen pen;
686
687 for (i = j = lastOfs = 0; i < n; i++)
688 {
689 lastOfs = j;
690 j += count[i];
691 }
692 pts = new wxPoint[j+n-1];
693 for (i = 0; i < j; i++)
694 pts[i] = points[i];
695 for (i = 2; i <= n; i++)
696 {
697 lastOfs -= count[n-i];
698 pts[j++] = pts[lastOfs];
699 }
700
701 pen = GetPen();
702 SetPen(wxPen(*wxBLACK, 0, wxTRANSPARENT));
703 DoDrawPolygon(j, pts, xoffset, yoffset, fillStyle);
704 SetPen(pen);
705 for (i = j = 0; i < n; i++)
706 {
707 DoDrawLines(count[i], pts+j, xoffset, yoffset);
708 j += count[i];
709 }
710 delete[] pts;
711 }
712
713 #if wxUSE_SPLINES
714
715 void wxDCImpl::DoDrawSpline(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord x3, wxCoord y3)
716 {
717 wxPointList point_list;
718
719 wxPoint *point1 = new wxPoint;
720 point1->x = x1; point1->y = y1;
721 point_list.Append( point1 );
722
723 wxPoint *point2 = new wxPoint;
724 point2->x = x2; point2->y = y2;
725 point_list.Append( point2 );
726
727 wxPoint *point3 = new wxPoint;
728 point3->x = x3; point3->y = y3;
729 point_list.Append( point3 );
730
731 DoDrawSpline(&point_list);
732
733 for( wxPointList::compatibility_iterator node = point_list.GetFirst(); node; node = node->GetNext() )
734 {
735 wxPoint *p = node->GetData();
736 delete p;
737 }
738 }
739
740 void wxDCImpl::DoDrawSpline(int n, wxPoint points[])
741 {
742 wxPointList list;
743 for (int i =0; i < n; i++)
744 list.Append( &points[i] );
745
746 DoDrawSpline(&list);
747 }
748
749 // ----------------------------------- spline code ----------------------------------------
750
751 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
752 double a3, double b3, double a4, double b4);
753 void wx_clear_stack();
754 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
755 double *y3, double *x4, double *y4);
756 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
757 double x4, double y4);
758 static bool wx_spline_add_point(double x, double y);
759 static void wx_spline_draw_point_array(wxDC *dc);
760
761 wxPointList wx_spline_point_list;
762
763 #define half(z1, z2) ((z1+z2)/2.0)
764 #define THRESHOLD 5
765
766 /* iterative version */
767
768 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
769 double b4)
770 {
771 register double xmid, ymid;
772 double x1, y1, x2, y2, x3, y3, x4, y4;
773
774 wx_clear_stack();
775 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
776
777 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
778 xmid = (double)half(x2, x3);
779 ymid = (double)half(y2, y3);
780 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
781 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
782 wx_spline_add_point( x1, y1 );
783 wx_spline_add_point( xmid, ymid );
784 } else {
785 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
786 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
787 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
788 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
789 }
790 }
791 }
792
793 /* utilities used by spline drawing routines */
794
795 typedef struct wx_spline_stack_struct {
796 double x1, y1, x2, y2, x3, y3, x4, y4;
797 } Stack;
798
799 #define SPLINE_STACK_DEPTH 20
800 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
801 static Stack *wx_stack_top;
802 static int wx_stack_count;
803
804 void wx_clear_stack()
805 {
806 wx_stack_top = wx_spline_stack;
807 wx_stack_count = 0;
808 }
809
810 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
811 {
812 wx_stack_top->x1 = x1;
813 wx_stack_top->y1 = y1;
814 wx_stack_top->x2 = x2;
815 wx_stack_top->y2 = y2;
816 wx_stack_top->x3 = x3;
817 wx_stack_top->y3 = y3;
818 wx_stack_top->x4 = x4;
819 wx_stack_top->y4 = y4;
820 wx_stack_top++;
821 wx_stack_count++;
822 }
823
824 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
825 double *x3, double *y3, double *x4, double *y4)
826 {
827 if (wx_stack_count == 0)
828 return (0);
829 wx_stack_top--;
830 wx_stack_count--;
831 *x1 = wx_stack_top->x1;
832 *y1 = wx_stack_top->y1;
833 *x2 = wx_stack_top->x2;
834 *y2 = wx_stack_top->y2;
835 *x3 = wx_stack_top->x3;
836 *y3 = wx_stack_top->y3;
837 *x4 = wx_stack_top->x4;
838 *y4 = wx_stack_top->y4;
839 return (1);
840 }
841
842 static bool wx_spline_add_point(double x, double y)
843 {
844 wxPoint *point = new wxPoint( wxRound(x), wxRound(y) );
845 wx_spline_point_list.Append(point );
846 return true;
847 }
848
849 static void wx_spline_draw_point_array(wxDC *dc)
850 {
851 dc->DrawLines(&wx_spline_point_list, 0, 0 );
852 wxPointList::compatibility_iterator node = wx_spline_point_list.GetFirst();
853 while (node)
854 {
855 wxPoint *point = node->GetData();
856 delete point;
857 wx_spline_point_list.Erase(node);
858 node = wx_spline_point_list.GetFirst();
859 }
860 }
861
862 void wxDCImpl::DoDrawSpline( const wxPointList *points )
863 {
864 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
865
866 wxPoint *p;
867 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
868 double x1, y1, x2, y2;
869
870 wxPointList::compatibility_iterator node = points->GetFirst();
871 if (!node)
872 // empty list
873 return;
874
875 p = (wxPoint *)node->GetData();
876
877 x1 = p->x;
878 y1 = p->y;
879
880 node = node->GetNext();
881 p = node->GetData();
882
883 x2 = p->x;
884 y2 = p->y;
885 cx1 = (double)((x1 + x2) / 2);
886 cy1 = (double)((y1 + y2) / 2);
887 cx2 = (double)((cx1 + x2) / 2);
888 cy2 = (double)((cy1 + y2) / 2);
889
890 wx_spline_add_point(x1, y1);
891
892 while ((node = node->GetNext())
893 #if !wxUSE_STL
894 != NULL
895 #endif // !wxUSE_STL
896 )
897 {
898 p = node->GetData();
899 x1 = x2;
900 y1 = y2;
901 x2 = p->x;
902 y2 = p->y;
903 cx4 = (double)(x1 + x2) / 2;
904 cy4 = (double)(y1 + y2) / 2;
905 cx3 = (double)(x1 + cx4) / 2;
906 cy3 = (double)(y1 + cy4) / 2;
907
908 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
909
910 cx1 = cx4;
911 cy1 = cy4;
912 cx2 = (double)(cx1 + x2) / 2;
913 cy2 = (double)(cy1 + y2) / 2;
914 }
915
916 wx_spline_add_point( cx1, cy1 );
917 wx_spline_add_point( x2, y2 );
918
919 wx_spline_draw_point_array( m_owner );
920 }
921
922 #endif // wxUSE_SPLINES
923
924
925
926 void wxDCImpl::DoGradientFillLinear(const wxRect& rect,
927 const wxColour& initialColour,
928 const wxColour& destColour,
929 wxDirection nDirection)
930 {
931 // save old pen
932 wxPen oldPen = m_pen;
933 wxBrush oldBrush = m_brush;
934
935 wxUint8 nR1 = initialColour.Red();
936 wxUint8 nG1 = initialColour.Green();
937 wxUint8 nB1 = initialColour.Blue();
938 wxUint8 nR2 = destColour.Red();
939 wxUint8 nG2 = destColour.Green();
940 wxUint8 nB2 = destColour.Blue();
941 wxUint8 nR, nG, nB;
942
943 if ( nDirection == wxEAST || nDirection == wxWEST )
944 {
945 wxInt32 x = rect.GetWidth();
946 wxInt32 w = x; // width of area to shade
947 wxInt32 xDelta = w/256; // height of one shade bend
948 if (xDelta < 1)
949 xDelta = 1;
950
951 while (x >= xDelta)
952 {
953 x -= xDelta;
954 if (nR1 > nR2)
955 nR = nR1 - (nR1-nR2)*(w-x)/w;
956 else
957 nR = nR1 + (nR2-nR1)*(w-x)/w;
958
959 if (nG1 > nG2)
960 nG = nG1 - (nG1-nG2)*(w-x)/w;
961 else
962 nG = nG1 + (nG2-nG1)*(w-x)/w;
963
964 if (nB1 > nB2)
965 nB = nB1 - (nB1-nB2)*(w-x)/w;
966 else
967 nB = nB1 + (nB2-nB1)*(w-x)/w;
968
969 wxColour colour(nR,nG,nB);
970 SetPen(wxPen(colour, 1, wxSOLID));
971 SetBrush(wxBrush(colour));
972 if(nDirection == wxEAST)
973 DoDrawRectangle(rect.GetRight()-x-xDelta+1, rect.GetTop(),
974 xDelta, rect.GetHeight());
975 else //nDirection == wxWEST
976 DoDrawRectangle(rect.GetLeft()+x, rect.GetTop(),
977 xDelta, rect.GetHeight());
978 }
979 }
980 else // nDirection == wxNORTH || nDirection == wxSOUTH
981 {
982 wxInt32 y = rect.GetHeight();
983 wxInt32 w = y; // height of area to shade
984 wxInt32 yDelta = w/255; // height of one shade bend
985 if (yDelta < 1)
986 yDelta = 1;
987
988 while (y > 0)
989 {
990 y -= yDelta;
991 if (nR1 > nR2)
992 nR = nR1 - (nR1-nR2)*(w-y)/w;
993 else
994 nR = nR1 + (nR2-nR1)*(w-y)/w;
995
996 if (nG1 > nG2)
997 nG = nG1 - (nG1-nG2)*(w-y)/w;
998 else
999 nG = nG1 + (nG2-nG1)*(w-y)/w;
1000
1001 if (nB1 > nB2)
1002 nB = nB1 - (nB1-nB2)*(w-y)/w;
1003 else
1004 nB = nB1 + (nB2-nB1)*(w-y)/w;
1005
1006 wxColour colour(nR,nG,nB);
1007 SetPen(wxPen(colour, 1, wxSOLID));
1008 SetBrush(wxBrush(colour));
1009 if(nDirection == wxNORTH)
1010 DoDrawRectangle(rect.GetLeft(), rect.GetTop()+y,
1011 rect.GetWidth(), yDelta);
1012 else //nDirection == wxSOUTH
1013 DoDrawRectangle(rect.GetLeft(), rect.GetBottom()-y-yDelta+1,
1014 rect.GetWidth(), yDelta);
1015 }
1016 }
1017
1018 SetPen(oldPen);
1019 SetBrush(oldBrush);
1020 }
1021
1022 void wxDCImpl::DoGradientFillConcentric(const wxRect& rect,
1023 const wxColour& initialColour,
1024 const wxColour& destColour,
1025 const wxPoint& circleCenter)
1026 {
1027 //save the old pen color
1028 wxColour oldPenColour = m_pen.GetColour();
1029
1030 wxUint8 nR1 = destColour.Red();
1031 wxUint8 nG1 = destColour.Green();
1032 wxUint8 nB1 = destColour.Blue();
1033 wxUint8 nR2 = initialColour.Red();
1034 wxUint8 nG2 = initialColour.Green();
1035 wxUint8 nB2 = initialColour.Blue();
1036 wxUint8 nR, nG, nB;
1037
1038
1039 //Radius
1040 wxInt32 cx = rect.GetWidth() / 2;
1041 wxInt32 cy = rect.GetHeight() / 2;
1042 wxInt32 nRadius;
1043 if (cx < cy)
1044 nRadius = cx;
1045 else
1046 nRadius = cy;
1047
1048 //Offset of circle
1049 wxInt32 nCircleOffX = circleCenter.x - (rect.GetWidth() / 2);
1050 wxInt32 nCircleOffY = circleCenter.y - (rect.GetHeight() / 2);
1051
1052 for ( wxInt32 x = 0; x < rect.GetWidth(); x++ )
1053 {
1054 for ( wxInt32 y = 0; y < rect.GetHeight(); y++ )
1055 {
1056 //get color difference
1057 wxInt32 nGradient = ((nRadius -
1058 (wxInt32)sqrt(
1059 pow((double)(x - cx - nCircleOffX), 2) +
1060 pow((double)(y - cy - nCircleOffY), 2)
1061 )) * 100) / nRadius;
1062
1063 //normalize Gradient
1064 if (nGradient < 0 )
1065 nGradient = 0;
1066
1067 //get dest colors
1068 nR = (wxUint8)(nR1 + ((nR2 - nR1) * nGradient / 100));
1069 nG = (wxUint8)(nG1 + ((nG2 - nG1) * nGradient / 100));
1070 nB = (wxUint8)(nB1 + ((nB2 - nB1) * nGradient / 100));
1071
1072 //set the pixel
1073 m_pen.SetColour(wxColour(nR,nG,nB));
1074 DoDrawPoint(x + rect.GetLeft(), y + rect.GetTop());
1075 }
1076 }
1077 //return old pen color
1078 m_pen.SetColour(oldPenColour);
1079 }
1080
1081 //-----------------------------------------------------------------------------
1082 // wxDC
1083 //-----------------------------------------------------------------------------
1084
1085 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
1086
1087 void wxDC::DrawLabel(const wxString& text,
1088 const wxBitmap& bitmap,
1089 const wxRect& rect,
1090 int alignment,
1091 int indexAccel,
1092 wxRect *rectBounding)
1093 {
1094 // find the text position
1095 wxCoord widthText, heightText, heightLine;
1096 GetMultiLineTextExtent(text, &widthText, &heightText, &heightLine);
1097
1098 wxCoord width, height;
1099 if ( bitmap.Ok() )
1100 {
1101 width = widthText + bitmap.GetWidth();
1102 height = bitmap.GetHeight();
1103 }
1104 else // no bitmap
1105 {
1106 width = widthText;
1107 height = heightText;
1108 }
1109
1110 wxCoord x, y;
1111 if ( alignment & wxALIGN_RIGHT )
1112 {
1113 x = rect.GetRight() - width;
1114 }
1115 else if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1116 {
1117 x = (rect.GetLeft() + rect.GetRight() + 1 - width) / 2;
1118 }
1119 else // alignment & wxALIGN_LEFT
1120 {
1121 x = rect.GetLeft();
1122 }
1123
1124 if ( alignment & wxALIGN_BOTTOM )
1125 {
1126 y = rect.GetBottom() - height;
1127 }
1128 else if ( alignment & wxALIGN_CENTRE_VERTICAL )
1129 {
1130 y = (rect.GetTop() + rect.GetBottom() + 1 - height) / 2;
1131 }
1132 else // alignment & wxALIGN_TOP
1133 {
1134 y = rect.GetTop();
1135 }
1136
1137 // draw the bitmap first
1138 wxCoord x0 = x,
1139 y0 = y,
1140 width0 = width;
1141 if ( bitmap.Ok() )
1142 {
1143 DrawBitmap(bitmap, x, y, true /* use mask */);
1144
1145 wxCoord offset = bitmap.GetWidth() + 4;
1146 x += offset;
1147 width -= offset;
1148
1149 y += (height - heightText) / 2;
1150 }
1151
1152 // we will draw the underscore under the accel char later
1153 wxCoord startUnderscore = 0,
1154 endUnderscore = 0,
1155 yUnderscore = 0;
1156
1157 // split the string into lines and draw each of them separately
1158 wxString curLine;
1159 for ( wxString::const_iterator pc = text.begin(); ; ++pc )
1160 {
1161 if ( *pc == _T('\n') || pc == text.end() )
1162 {
1163 int xRealStart = x; // init it here to avoid compielr warnings
1164
1165 if ( !curLine.empty() )
1166 {
1167 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1168 // wxALIGN_LEFT is 0
1169 if ( alignment & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL) )
1170 {
1171 wxCoord widthLine;
1172 GetTextExtent(curLine, &widthLine, NULL);
1173
1174 if ( alignment & wxALIGN_RIGHT )
1175 {
1176 xRealStart += width - widthLine;
1177 }
1178 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1179 {
1180 xRealStart += (width - widthLine) / 2;
1181 }
1182 }
1183 //else: left aligned, nothing to do
1184
1185 DrawText(curLine, xRealStart, y);
1186 }
1187
1188 y += heightLine;
1189
1190 // do we have underscore in this line? we can check yUnderscore
1191 // because it is set below to just y + heightLine if we do
1192 if ( y == yUnderscore )
1193 {
1194 // adjust the horz positions to account for the shift
1195 startUnderscore += xRealStart;
1196 endUnderscore += xRealStart;
1197 }
1198
1199 if ( pc == text.end() )
1200 break;
1201
1202 curLine.clear();
1203 }
1204 else // not end of line
1205 {
1206 if ( pc - text.begin() == indexAccel )
1207 {
1208 // remeber to draw underscore here
1209 GetTextExtent(curLine, &startUnderscore, NULL);
1210 curLine += *pc;
1211 GetTextExtent(curLine, &endUnderscore, NULL);
1212
1213 yUnderscore = y + heightLine;
1214 }
1215 else
1216 {
1217 curLine += *pc;
1218 }
1219 }
1220 }
1221
1222 // draw the underscore if found
1223 if ( startUnderscore != endUnderscore )
1224 {
1225 // it should be of the same colour as text
1226 SetPen(wxPen(GetTextForeground(), 0, wxSOLID));
1227
1228 yUnderscore--;
1229
1230 DrawLine(startUnderscore, yUnderscore, endUnderscore, yUnderscore);
1231 }
1232
1233 // return bounding rect if requested
1234 if ( rectBounding )
1235 {
1236 *rectBounding = wxRect(x, y - heightText, widthText, heightText);
1237 }
1238
1239 CalcBoundingBox(x0, y0);
1240 CalcBoundingBox(x0 + width0, y0 + height);
1241 }
1242
1243 #if WXWIN_COMPATIBILITY_2_8
1244 // for compatibility with the old code when wxCoord was long everywhere
1245 void wxDC::GetTextExtent(const wxString& string,
1246 long *x, long *y,
1247 long *descent,
1248 long *externalLeading,
1249 const wxFont *theFont) const
1250 {
1251 wxCoord x2, y2, descent2, externalLeading2;
1252 m_pimpl->DoGetTextExtent(string, &x2, &y2,
1253 &descent2, &externalLeading2,
1254 theFont);
1255 if ( x )
1256 *x = x2;
1257 if ( y )
1258 *y = y2;
1259 if ( descent )
1260 *descent = descent2;
1261 if ( externalLeading )
1262 *externalLeading = externalLeading2;
1263 }
1264
1265 void wxDC::GetLogicalOrigin(long *x, long *y) const
1266 {
1267 wxCoord x2, y2;
1268 m_pimpl->DoGetLogicalOrigin(&x2, &y2);
1269 if ( x )
1270 *x = x2;
1271 if ( y )
1272 *y = y2;
1273 }
1274
1275 void wxDC::GetDeviceOrigin(long *x, long *y) const
1276 {
1277 wxCoord x2, y2;
1278 m_pimpl->DoGetDeviceOrigin(&x2, &y2);
1279 if ( x )
1280 *x = x2;
1281 if ( y )
1282 *y = y2;
1283 }
1284
1285 void wxDC::GetClippingBox(long *x, long *y, long *w, long *h) const
1286 {
1287 wxCoord xx,yy,ww,hh;
1288 m_pimpl->DoGetClippingBox(&xx, &yy, &ww, &hh);
1289 if (x) *x = xx;
1290 if (y) *y = yy;
1291 if (w) *w = ww;
1292 if (h) *h = hh;
1293 }
1294
1295 #endif // WXWIN_COMPATIBILITY_2_8