fix some compile/link errors
[wxWidgets.git] / src / generic / graphicc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-10-03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dc.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/image.h"
22 #include "wx/window.h"
23 #include "wx/dc.h"
24 #include "wx/utils.h"
25 #include "wx/dialog.h"
26 #include "wx/app.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
29 #include "wx/log.h"
30 #include "wx/icon.h"
31 #include "wx/dcprint.h"
32 #include "wx/module.h"
33 #endif
34
35 #ifdef __WXGTK__
36 #include "wx/gtk/win_gtk.h"
37 #endif
38
39 #include "wx/graphics.h"
40
41 #if wxUSE_GRAPHICS_CONTEXT
42
43 #include <vector>
44
45 using namespace std;
46
47 //-----------------------------------------------------------------------------
48 // constants
49 //-----------------------------------------------------------------------------
50
51 const double RAD2DEG = 180.0 / M_PI;
52
53 //-----------------------------------------------------------------------------
54 // Local functions
55 //-----------------------------------------------------------------------------
56
57 static inline double dmin(double a, double b)
58 {
59 return a < b ? a : b;
60 }
61 static inline double dmax(double a, double b)
62 {
63 return a > b ? a : b;
64 }
65
66 static inline double DegToRad(double deg)
67 {
68 return (deg * M_PI) / 180.0;
69 }
70 static inline double RadToDeg(double deg)
71 {
72 return (deg * 180.0) / M_PI;
73 }
74
75 //-----------------------------------------------------------------------------
76 // device context implementation
77 //
78 // more and more of the dc functionality should be implemented by calling
79 // the appropricate wxCairoContext, but we will have to do that step by step
80 // also coordinate conversions should be moved to native matrix ops
81 //-----------------------------------------------------------------------------
82
83 // we always stock two context states, one at entry, to be able to preserve the
84 // state we were called with, the other one after changing to HI Graphics orientation
85 // (this one is used for getting back clippings etc)
86
87 //-----------------------------------------------------------------------------
88 // wxGraphicsPath implementation
89 //-----------------------------------------------------------------------------
90
91 // TODO remove this dependency (gdiplus needs the macros)
92
93 #ifndef max
94 #define max(a,b) (((a) > (b)) ? (a) : (b))
95 #endif
96
97 #ifndef min
98 #define min(a,b) (((a) < (b)) ? (a) : (b))
99 #endif
100
101 #include <cairo.h>
102 #ifdef __WXGTK__
103 #include <gtk/gtk.h>
104 #endif
105
106 class WXDLLIMPEXP_CORE wxCairoPath : public wxGraphicsPath
107 {
108 public :
109 wxCairoPath();
110 wxCairoPath(wxGraphicsRenderer* renderer, cairo_t* pathcontext = NULL);
111 ~wxCairoPath();
112
113 virtual wxGraphicsPath *Clone() const;
114
115 //
116 // These are the path primitives from which everything else can be constructed
117 //
118
119 // begins a new subpath at (x,y)
120 virtual void MoveToPoint( wxDouble x, wxDouble y );
121
122 // adds a straight line from the current point to (x,y)
123 virtual void AddLineToPoint( wxDouble x, wxDouble y );
124
125 // adds a cubic Bezier curve from the current point, using two control points and an end point
126 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
127
128
129 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
130 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
131
132 // gets the last point of the current path, (0,0) if not yet set
133 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ;
134
135 // adds another path
136 virtual void AddPath( const wxGraphicsPath* path );
137
138 // closes the current sub-path
139 virtual void CloseSubpath();
140
141 //
142 // These are convenience functions which - if not available natively will be assembled
143 // using the primitives from above
144 //
145
146 /*
147
148 // appends a rectangle as a new closed subpath
149 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
150 // appends an ellipsis as a new closed subpath fitting the passed rectangle
151 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
152
153 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
154 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
155 */
156
157 // returns the native path
158 virtual void * GetNativePath() const ;
159
160 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
161 virtual void UnGetNativePath(void *p) ;
162
163 // transforms each point of this path by the matrix
164 virtual void Transform( wxGraphicsMatrix* matrix ) ;
165
166 // gets the bounding box enclosing all points (possibly including control points)
167 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) ;
168
169 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) ;
170
171 private :
172 cairo_t* m_pathContext;
173 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoPath)
174 };
175
176 class WXDLLIMPEXP_CORE wxCairoMatrix : public wxGraphicsMatrix
177 {
178 public :
179 wxCairoMatrix() ;
180
181 wxCairoMatrix(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
182 virtual ~wxCairoMatrix() ;
183
184 virtual wxGraphicsMatrix *Clone() const ;
185
186 // concatenates the matrix
187 virtual void Concat( const wxGraphicsMatrix *t );
188
189 // copies the passed in matrix
190 virtual void Copy( const wxGraphicsMatrix *t );
191
192 // sets the matrix to the respective values
193 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
194 wxDouble tx=0.0, wxDouble ty=0.0);
195
196 // makes this the inverse matrix
197 virtual void Invert();
198
199 // returns true if the elements of the transformation matrix are equal ?
200 virtual bool IsEqual( const wxGraphicsMatrix* t) const ;
201
202 // return true if this is the identity matrix
203 virtual bool IsIdentity();
204
205 //
206 // transformation
207 //
208
209 // add the translation to this matrix
210 virtual void Translate( wxDouble dx , wxDouble dy );
211
212 // add the scale to this matrix
213 virtual void Scale( wxDouble xScale , wxDouble yScale );
214
215 // add the rotation to this matrix (radians)
216 virtual void Rotate( wxDouble angle );
217
218 //
219 // apply the transforms
220 //
221
222 // applies that matrix to the point
223 virtual void TransformPoint( wxDouble *x, wxDouble *y );
224
225 // applies the matrix except for translations
226 virtual void TransformDistance( wxDouble *dx, wxDouble *dy );
227
228 // returns the native representation
229 virtual void * GetNativeMatrix() const;
230 private:
231 cairo_matrix_t m_matrix ;
232
233 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoMatrix)
234 } ;
235
236 class WXDLLIMPEXP_CORE wxCairoPen : public wxGraphicsPen
237 {
238 public:
239 wxCairoPen();
240 wxCairoPen( wxGraphicsRenderer* renderer, const wxPen &pen );
241 ~wxCairoPen();
242
243 void Init();
244
245 virtual void Apply( wxGraphicsContext* context );
246 virtual wxDouble GetWidth() { return m_width; }
247
248 private :
249 double m_width;
250
251 double m_red;
252 double m_green;
253 double m_blue;
254 double m_alpha;
255
256 cairo_line_cap_t m_cap;
257 cairo_line_join_t m_join;
258
259 int m_count;
260 const double *m_lengths;
261 double *m_userLengths;
262
263 wxPen m_pen;
264 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoPen)
265 };
266
267 class WXDLLIMPEXP_CORE wxCairoBrush : public wxGraphicsBrush
268 {
269 public:
270 wxCairoBrush();
271 wxCairoBrush( wxGraphicsRenderer* renderer );
272 wxCairoBrush( wxGraphicsRenderer* renderer, const wxBrush &brush );
273 ~wxCairoBrush ();
274
275 virtual void Apply( wxGraphicsContext* context );
276 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
277 const wxColour&c1, const wxColour&c2 );
278 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
279 const wxColour &oColor, const wxColour &cColor );
280
281 protected:
282 virtual void Init();
283
284 private :
285 double m_red;
286 double m_green;
287 double m_blue;
288 double m_alpha;
289
290 cairo_pattern_t* m_brushPattern;
291
292 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoBrush)
293 };
294
295 class wxCairoFont : public wxGraphicsFont
296 {
297 public:
298 wxCairoFont();
299 wxCairoFont( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
300 ~wxCairoFont();
301
302 virtual void Apply( wxGraphicsContext* context );
303 private :
304 wxCharBuffer m_fontName;
305 double m_size;
306 cairo_font_slant_t m_slant;
307 cairo_font_weight_t m_weight;
308 double m_red;
309 double m_green;
310 double m_blue;
311 double m_alpha;
312 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoFont)
313 };
314
315 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
316 {
317 DECLARE_NO_COPY_CLASS(wxCairoContext)
318
319 public:
320 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
321 #ifdef __WXGTK__
322 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
323 #endif
324 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
325 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
326 wxCairoContext();
327 virtual ~wxCairoContext();
328
329 virtual void Clip( const wxRegion &region );
330
331 // clips drawings to the rect
332 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
333
334 // resets the clipping to original extent
335 virtual void ResetClip();
336
337 virtual void * GetNativeContext();
338
339 virtual void StrokePath( const wxGraphicsPath *p );
340 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
341
342 virtual void Translate( wxDouble dx , wxDouble dy );
343 virtual void Scale( wxDouble xScale , wxDouble yScale );
344 virtual void Rotate( wxDouble angle );
345
346 // concatenates this transform with the current transform of this context
347 virtual void ConcatTransform( const wxGraphicsMatrix* matrix );
348
349 // sets the transform of this context
350 virtual void SetTransform( const wxGraphicsMatrix* matrix );
351
352 // gets the matrix of this context
353 virtual void GetTransform( wxGraphicsMatrix* matrix );
354
355 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
356 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
357 virtual void PushState();
358 virtual void PopState();
359
360 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
361 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
362 wxDouble *descent, wxDouble *externalLeading ) const;
363 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
364
365 private:
366 cairo_t* m_context;
367 };
368
369 //-----------------------------------------------------------------------------
370 // wxCairoPen implementation
371 //-----------------------------------------------------------------------------
372
373 IMPLEMENT_DYNAMIC_CLASS(wxCairoPen,wxGraphicsPen)
374
375 wxCairoPen::wxCairoPen() : wxGraphicsPen(NULL)
376 {
377 wxLogDebug(wxT("Illegal Constructor called"));
378 }
379
380 wxCairoPen::~wxCairoPen()
381 {
382 delete[] m_userLengths;
383 }
384
385 void wxCairoPen::Init()
386 {
387 m_lengths = NULL;
388 m_userLengths = NULL;
389 m_width = 0;
390 m_count = 0;
391 }
392
393 wxCairoPen::wxCairoPen( wxGraphicsRenderer* renderer, const wxPen &pen )
394 : wxGraphicsPen(renderer)
395 {
396 Init();
397 m_width = pen.GetWidth();
398 if (m_width <= 0.0)
399 m_width = 0.1;
400
401 m_red = m_pen.GetColour().Red()/255.0;
402 m_green = m_pen.GetColour().Green()/255.0;
403 m_blue = m_pen.GetColour().Blue()/255.0;
404 m_alpha = m_pen.GetColour().Alpha()/255.0;
405
406 switch ( m_pen.GetCap() )
407 {
408 case wxCAP_ROUND :
409 m_cap = CAIRO_LINE_CAP_ROUND;
410 break;
411
412 case wxCAP_PROJECTING :
413 m_cap = CAIRO_LINE_CAP_SQUARE;
414 break;
415
416 case wxCAP_BUTT :
417 m_cap = CAIRO_LINE_CAP_BUTT;
418 break;
419
420 default :
421 m_cap = CAIRO_LINE_CAP_BUTT;
422 break;
423 }
424
425 switch ( m_pen.GetJoin() )
426 {
427 case wxJOIN_BEVEL :
428 m_join = CAIRO_LINE_JOIN_BEVEL;
429 break;
430
431 case wxJOIN_MITER :
432 m_join = CAIRO_LINE_JOIN_MITER;
433 break;
434
435 case wxJOIN_ROUND :
436 m_join = CAIRO_LINE_JOIN_ROUND;
437 break;
438
439 default :
440 m_join = CAIRO_LINE_JOIN_MITER;
441 break;
442 }
443
444 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
445 const double dotted[] =
446 {
447 dashUnit , dashUnit + 2.0
448 };
449 static const double short_dashed[] =
450 {
451 9.0 , 6.0
452 };
453 static const double dashed[] =
454 {
455 19.0 , 9.0
456 };
457 static const double dotted_dashed[] =
458 {
459 9.0 , 6.0 , 3.0 , 3.0
460 };
461
462 switch ( m_pen.GetStyle() )
463 {
464 case wxSOLID :
465 break;
466
467 case wxDOT :
468 m_count = WXSIZEOF(dotted);
469 m_userLengths = new double[ m_count ] ;
470 memcpy( m_userLengths, dotted, sizeof(dotted) );
471 m_lengths = m_userLengths;
472 break;
473
474 case wxLONG_DASH :
475 m_lengths = dotted ;
476 m_count = WXSIZEOF(dashed);
477 break;
478
479 case wxSHORT_DASH :
480 m_lengths = dotted ;
481 m_count = WXSIZEOF(short_dashed);
482 break;
483
484 case wxDOT_DASH :
485 m_lengths = dotted ;
486 m_count = WXSIZEOF(dotted_dashed);
487 break;
488
489 case wxUSER_DASH :
490 {
491 wxDash *wxdashes ;
492 m_count = m_pen.GetDashes( &wxdashes ) ;
493 if ((wxdashes != NULL) && (m_count > 0))
494 {
495 m_userLengths = new double[m_count] ;
496 for ( int i = 0 ; i < m_count ; ++i )
497 {
498 m_userLengths[i] = wxdashes[i] * dashUnit ;
499
500 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
501 m_userLengths[i] = dashUnit + 2.0 ;
502 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
503 m_userLengths[i] = dashUnit ;
504 }
505 }
506 m_lengths = m_userLengths ;
507 }
508 break;
509 case wxSTIPPLE :
510 {
511 /*
512 wxBitmap* bmp = pen.GetStipple();
513 if ( bmp && bmp->Ok() )
514 {
515 wxDELETE( m_penImage );
516 wxDELETE( m_penBrush );
517 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
518 m_penBrush = new TextureBrush(m_penImage);
519 m_pen->SetBrush( m_penBrush );
520 }
521 */
522 }
523 break;
524 default :
525 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
526 {
527 /*
528 wxDELETE( m_penBrush );
529 HatchStyle style = HatchStyleHorizontal;
530 switch( pen.GetStyle() )
531 {
532 case wxBDIAGONAL_HATCH :
533 style = HatchStyleBackwardDiagonal;
534 break ;
535 case wxCROSSDIAG_HATCH :
536 style = HatchStyleDiagonalCross;
537 break ;
538 case wxFDIAGONAL_HATCH :
539 style = HatchStyleForwardDiagonal;
540 break ;
541 case wxCROSS_HATCH :
542 style = HatchStyleCross;
543 break ;
544 case wxHORIZONTAL_HATCH :
545 style = HatchStyleHorizontal;
546 break ;
547 case wxVERTICAL_HATCH :
548 style = HatchStyleVertical;
549 break ;
550
551 }
552 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
553 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
554 m_pen->SetBrush( m_penBrush )
555 */
556 }
557 break;
558 }
559 }
560
561 void wxCairoPen::Apply( wxGraphicsContext* context )
562 {
563 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
564 cairo_set_line_width(ctext,m_width);
565 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
566 cairo_set_line_cap(ctext,m_cap);
567 cairo_set_line_join(ctext,m_join);
568 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
569 }
570
571 //-----------------------------------------------------------------------------
572 // wxCairoBrush implementation
573 //-----------------------------------------------------------------------------
574
575 IMPLEMENT_DYNAMIC_CLASS(wxCairoBrush,wxGraphicsBrush)
576
577 wxCairoBrush::wxCairoBrush() : wxGraphicsBrush( NULL )
578 {
579 wxLogDebug(wxT("Illegal Constructor called"));
580 }
581
582 wxCairoBrush::wxCairoBrush( wxGraphicsRenderer* renderer ) : wxGraphicsBrush( renderer )
583 {
584 Init();
585 }
586
587 wxCairoBrush::wxCairoBrush( wxGraphicsRenderer* renderer, const wxBrush &brush )
588 : wxGraphicsBrush(renderer)
589 {
590 m_red = brush.GetColour().Red()/255.0;
591 m_green = brush.GetColour().Green()/255.0;
592 m_blue = brush.GetColour().Blue()/255.0;
593 m_alpha = brush.GetColour().Alpha()/255.0;
594 /*
595 if ( brush.GetStyle() == wxSOLID)
596 {
597 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
598 brush.GetColour().Green() , brush.GetColour().Blue() ) );
599 }
600 else if ( brush.IsHatch() )
601 {
602 HatchStyle style = HatchStyleHorizontal;
603 switch( brush.GetStyle() )
604 {
605 case wxBDIAGONAL_HATCH :
606 style = HatchStyleBackwardDiagonal;
607 break ;
608 case wxCROSSDIAG_HATCH :
609 style = HatchStyleDiagonalCross;
610 break ;
611 case wxFDIAGONAL_HATCH :
612 style = HatchStyleForwardDiagonal;
613 break ;
614 case wxCROSS_HATCH :
615 style = HatchStyleCross;
616 break ;
617 case wxHORIZONTAL_HATCH :
618 style = HatchStyleHorizontal;
619 break ;
620 case wxVERTICAL_HATCH :
621 style = HatchStyleVertical;
622 break ;
623
624 }
625 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
626 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
627 }
628 else
629 {
630 wxBitmap* bmp = brush.GetStipple();
631 if ( bmp && bmp->Ok() )
632 {
633 wxDELETE( m_brushImage );
634 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
635 m_brush = new TextureBrush(m_brushImage);
636 }
637 }
638 */
639 }
640
641 wxCairoBrush::~wxCairoBrush ()
642 {
643 if (m_brushPattern)
644 cairo_pattern_destroy(m_brushPattern);
645 }
646
647 void wxCairoBrush::Apply( wxGraphicsContext* context )
648 {
649 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
650 if ( m_brushPattern )
651 {
652 cairo_set_source(ctext,m_brushPattern);
653 }
654 else
655 {
656 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
657 }
658 }
659
660 void wxCairoBrush::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
661 const wxColour&c1, const wxColour&c2 )
662 {
663 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
664 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
665 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
666 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
667 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
668 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
669 }
670
671 void wxCairoBrush::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
672 const wxColour &oColor, const wxColour &cColor )
673 {
674 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
675 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
676 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
677 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
678 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
679 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
680 }
681
682 void wxCairoBrush::Init()
683 {
684 m_brushPattern = NULL;
685 }
686
687 //-----------------------------------------------------------------------------
688 // wxCairoFont implementation
689 //-----------------------------------------------------------------------------
690
691 IMPLEMENT_DYNAMIC_CLASS(wxCairoFont,wxGraphicsFont)
692
693 wxCairoFont::wxCairoFont() : wxGraphicsFont(NULL)
694 {
695 wxLogDebug(wxT("Illegal Constructor called"));
696 }
697
698 wxCairoFont::wxCairoFont( wxGraphicsRenderer* renderer, const wxFont &font,
699 const wxColour& col ) : wxGraphicsFont(renderer)
700 {
701 m_red = col.Red()/255.0;
702 m_green = col.Green()/255.0;
703 m_blue = col.Blue()/255.0;
704 m_alpha = col.Alpha()/255.0;
705
706 m_size = font.GetPointSize();
707 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
708 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
709 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
710 }
711
712 wxCairoFont::~wxCairoFont()
713 {
714 }
715
716 void wxCairoFont::Apply( wxGraphicsContext* context )
717 {
718 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
719 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
720 cairo_select_font_face(ctext,m_fontName,m_slant,m_weight);
721 cairo_set_font_size(ctext,m_size);
722 // TODO UNDERLINE
723 // TODO FIX SIZE
724 }
725
726 //-----------------------------------------------------------------------------
727 // wxCairoPath implementation
728 //-----------------------------------------------------------------------------
729
730 IMPLEMENT_DYNAMIC_CLASS(wxCairoPath,wxGraphicsPath)
731
732 wxCairoPath::wxCairoPath() : wxGraphicsPath(NULL)
733 {
734 wxLogDebug(wxT("Illegal Constructor called"));
735 }
736
737 wxCairoPath::wxCairoPath( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
738 : wxGraphicsPath(renderer)
739 {
740 if (pathcontext)
741 {
742 m_pathContext = pathcontext;
743 }
744 else
745 {
746 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
747 m_pathContext = cairo_create(surface);
748 cairo_surface_destroy (surface);
749 }
750 }
751
752 wxCairoPath::~wxCairoPath()
753 {
754 cairo_destroy(m_pathContext);
755 }
756
757 wxGraphicsPath *wxCairoPath::Clone() const
758 {
759 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
760 cairo_t* pathcontext = cairo_create(surface);
761 cairo_surface_destroy (surface);
762
763 cairo_path_t* path = cairo_copy_path(m_pathContext);
764 cairo_append_path(pathcontext, path);
765 cairo_path_destroy(path);
766 return new wxCairoPath( GetRenderer() ,pathcontext);
767 }
768
769
770 void* wxCairoPath::GetNativePath() const
771 {
772 return cairo_copy_path(m_pathContext) ;
773 }
774
775 void wxCairoPath::UnGetNativePath(void *p)
776 {
777 cairo_path_destroy((cairo_path_t*)p);
778 }
779
780 //
781 // The Primitives
782 //
783
784 void wxCairoPath::MoveToPoint( wxDouble x , wxDouble y )
785 {
786 cairo_move_to(m_pathContext,x,y);
787 }
788
789 void wxCairoPath::AddLineToPoint( wxDouble x , wxDouble y )
790 {
791 cairo_line_to(m_pathContext,x,y);
792 }
793
794 void wxCairoPath::AddPath( const wxGraphicsPath* path )
795 {
796 // TODO
797 }
798
799 void wxCairoPath::CloseSubpath()
800 {
801 cairo_close_path(m_pathContext);
802 }
803
804 void wxCairoPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
805 {
806 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
807 }
808
809 // gets the last point of the current path, (0,0) if not yet set
810 void wxCairoPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
811 {
812 double dx,dy;
813 cairo_get_current_point(m_pathContext,&dx,&dy);
814 x = dx;
815 y = dy;
816 }
817
818 void wxCairoPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
819 {
820 // as clockwise means positive in our system (y pointing downwards)
821 // TODO make this interpretation dependent of the
822 // real device trans
823 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
824 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
825 else
826 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
827 }
828
829 // transforms each point of this path by the matrix
830 void wxCairoPath::Transform( wxGraphicsMatrix* matrix )
831 {
832 // as we don't have a true path object, we have to apply the inverse
833 // matrix to the context
834 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
835 cairo_matrix_invert( &m );
836 cairo_transform(m_pathContext,&m);
837 }
838
839 // gets the bounding box enclosing all points (possibly including control points)
840 void wxCairoPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h)
841 {
842 double x1,y1,x2,y2;
843
844 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
845 if ( x2 < x1 )
846 {
847 *x = x2;
848 *w = x1-x2;
849 }
850 else
851 {
852 *x = x1;
853 *w = x2-x1;
854 }
855
856 if( y2 < y1 )
857 {
858 *y = y2;
859 *h = y1-y2;
860 }
861 else
862 {
863 *y = y1;
864 *h = y2-y1;
865 }
866 }
867
868 bool wxCairoPath::Contains( wxDouble x, wxDouble y, int fillStyle )
869 {
870 return cairo_in_stroke( m_pathContext, x, y) != NULL;
871 }
872
873 //-----------------------------------------------------------------------------
874 // wxCairoMatrix implementation
875 //-----------------------------------------------------------------------------
876
877 IMPLEMENT_DYNAMIC_CLASS(wxCairoMatrix,wxGraphicsMatrix)
878
879 wxCairoMatrix::wxCairoMatrix() : wxGraphicsMatrix(NULL)
880 {
881 wxLogDebug(wxT("Illegal Constructor called"));
882 }
883
884 wxCairoMatrix::wxCairoMatrix(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
885 : wxGraphicsMatrix(renderer)
886 {
887 if ( matrix )
888 m_matrix = *matrix;
889 }
890
891 wxCairoMatrix::~wxCairoMatrix()
892 {
893 // nothing to do
894 }
895
896 wxGraphicsMatrix *wxCairoMatrix::Clone() const
897 {
898 return new wxCairoMatrix(GetRenderer(),&m_matrix);
899 }
900
901 // concatenates the matrix
902 void wxCairoMatrix::Concat( const wxGraphicsMatrix *t )
903 {
904 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
905 }
906
907 // copies the passed in matrix
908 void wxCairoMatrix::Copy( const wxGraphicsMatrix *t )
909 {
910 m_matrix = *((cairo_matrix_t*) t->GetNativeMatrix());
911 }
912
913 // sets the matrix to the respective values
914 void wxCairoMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
915 wxDouble tx, wxDouble ty)
916 {
917 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
918 }
919
920 // makes this the inverse matrix
921 void wxCairoMatrix::Invert()
922 {
923 cairo_matrix_invert( &m_matrix );
924 }
925
926 // returns true if the elements of the transformation matrix are equal ?
927 bool wxCairoMatrix::IsEqual( const wxGraphicsMatrix* t) const
928 {
929 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
930 return (
931 m_matrix.xx == tm->xx &&
932 m_matrix.yx == tm->yx &&
933 m_matrix.xy == tm->xy &&
934 m_matrix.yy == tm->yy &&
935 m_matrix.x0 == tm->x0 &&
936 m_matrix.y0 == tm->y0 ) ;
937 }
938
939 // return true if this is the identity matrix
940 bool wxCairoMatrix::IsIdentity()
941 {
942 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
943 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
944 }
945
946 //
947 // transformation
948 //
949
950 // add the translation to this matrix
951 void wxCairoMatrix::Translate( wxDouble dx , wxDouble dy )
952 {
953 cairo_matrix_translate( &m_matrix, dx, dy) ;
954 }
955
956 // add the scale to this matrix
957 void wxCairoMatrix::Scale( wxDouble xScale , wxDouble yScale )
958 {
959 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
960 }
961
962 // add the rotation to this matrix (radians)
963 void wxCairoMatrix::Rotate( wxDouble angle )
964 {
965 cairo_matrix_rotate( &m_matrix, angle) ;
966 }
967
968 //
969 // apply the transforms
970 //
971
972 // applies that matrix to the point
973 void wxCairoMatrix::TransformPoint( wxDouble *x, wxDouble *y )
974 {
975 double lx = *x, ly = *y ;
976 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
977 *x = lx;
978 *y = ly;
979 }
980
981 // applies the matrix except for translations
982 void wxCairoMatrix::TransformDistance( wxDouble *dx, wxDouble *dy )
983 {
984 double lx = *dx, ly = *dy ;
985 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
986 *dx = lx;
987 *dy = ly;
988 }
989
990 // returns the native representation
991 void * wxCairoMatrix::GetNativeMatrix() const
992 {
993 return (void*) &m_matrix;
994 }
995
996 //-----------------------------------------------------------------------------
997 // wxCairoContext implementation
998 //-----------------------------------------------------------------------------
999
1000 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1001 : wxGraphicsContext(renderer)
1002 {
1003 #ifdef __WXGTK__
1004 m_context = gdk_cairo_create( dc.m_window ) ;
1005 #endif
1006 PushState();
1007 PushState();
1008 }
1009
1010 #ifdef __WXGTK__
1011 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1012 : wxGraphicsContext(renderer)
1013 {
1014 m_context = gdk_cairo_create( drawable ) ;
1015 PushState();
1016 PushState();
1017 }
1018 #endif
1019
1020 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1021 : wxGraphicsContext(renderer)
1022 {
1023 m_context = context ;
1024 PushState();
1025 PushState();
1026 }
1027
1028 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1029 : wxGraphicsContext(renderer)
1030 {
1031 #ifdef __WXGTK__
1032 // something along these lines (copied from dcclient)
1033
1034 GtkWidget *widget = window->m_wxwindow;
1035
1036 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1037 // code should still be able to create wxClientDCs for them, so we will
1038 // use the parent window here then.
1039 if ( !widget )
1040 {
1041 window = window->GetParent();
1042 widget = window->m_wxwindow;
1043 }
1044
1045 wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
1046
1047 GtkPizza *pizza = GTK_PIZZA( widget );
1048 GdkDrawable* drawable = pizza->bin_window;
1049 m_context = gdk_cairo_create( drawable ) ;
1050 #endif
1051 PushState();
1052 PushState();
1053 }
1054
1055 wxCairoContext::~wxCairoContext()
1056 {
1057 SetPen(NULL);
1058 SetBrush(NULL);
1059 SetFont(NULL);
1060
1061 if ( m_context )
1062 {
1063 PopState();
1064 PopState();
1065 cairo_destroy(m_context);
1066 }
1067 }
1068
1069
1070 void wxCairoContext::Clip( const wxRegion & WXUNUSED(region) )
1071 {
1072 // TODO
1073 }
1074
1075 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1076 {
1077 // TODO
1078 }
1079
1080 void wxCairoContext::ResetClip()
1081 {
1082 // TODO
1083 }
1084
1085
1086 void wxCairoContext::StrokePath( const wxGraphicsPath *path )
1087 {
1088 if ( m_pen )
1089 {
1090 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1091 cairo_append_path(m_context,cp);
1092 m_pen->Apply(this);
1093 cairo_stroke(m_context);
1094 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1095 }
1096 }
1097
1098 void wxCairoContext::FillPath( const wxGraphicsPath *path , int fillStyle )
1099 {
1100 if ( m_brush )
1101 {
1102 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1103 cairo_append_path(m_context,cp);
1104 m_brush->Apply(this);
1105 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1106 cairo_fill(m_context);
1107 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1108 }
1109 }
1110
1111 void wxCairoContext::Rotate( wxDouble angle )
1112 {
1113 cairo_rotate(m_context,angle);
1114 }
1115
1116 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1117 {
1118 cairo_translate(m_context,dx,dy);
1119 }
1120
1121 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1122 {
1123 cairo_scale(m_context,xScale,yScale);
1124 }
1125
1126 // concatenates this transform with the current transform of this context
1127 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix* matrix )
1128 {
1129 cairo_transform(m_context,(const cairo_matrix_t *) matrix->GetNativeMatrix());
1130 }
1131
1132 // sets the transform of this context
1133 void wxCairoContext::SetTransform( const wxGraphicsMatrix* matrix )
1134 {
1135 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix->GetNativeMatrix());
1136 }
1137
1138 // gets the matrix of this context
1139 void wxCairoContext::GetTransform( wxGraphicsMatrix* matrix )
1140 {
1141 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix->GetNativeMatrix());
1142 }
1143
1144
1145
1146 void wxCairoContext::PushState()
1147 {
1148 cairo_save(m_context);
1149 }
1150
1151 void wxCairoContext::PopState()
1152 {
1153 cairo_restore(m_context);
1154 }
1155
1156 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1157 {
1158 /*
1159 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1160 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1161 delete image ;
1162 */
1163 }
1164
1165 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1166 {
1167 /*
1168 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1169 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1170 delete image ;
1171 */
1172 }
1173
1174
1175 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1176 {
1177 if ( m_font == NULL || str.IsEmpty())
1178 return ;
1179 cairo_move_to(m_context,x,y);
1180 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1181 m_font->Apply(this);
1182 cairo_show_text(m_context,buf);
1183 }
1184
1185 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1186 wxDouble *descent, wxDouble *externalLeading ) const
1187 {
1188 // TODO
1189 }
1190
1191 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1192 {
1193 widths.Empty();
1194 widths.Add(0, text.length());
1195
1196 if (text.empty())
1197 return;
1198
1199 // TODO
1200 }
1201
1202 void * wxCairoContext::GetNativeContext()
1203 {
1204 return m_context;
1205 }
1206
1207 //-----------------------------------------------------------------------------
1208 // wxCairoRenderer declaration
1209 //-----------------------------------------------------------------------------
1210
1211 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1212 {
1213 public :
1214 wxCairoRenderer() {}
1215
1216 virtual ~wxCairoRenderer() {}
1217
1218 // Context
1219
1220 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1221
1222 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1223
1224 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1225
1226 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1227
1228 // Path
1229
1230 virtual wxGraphicsPath * CreatePath();
1231
1232 // Matrix
1233
1234 virtual wxGraphicsMatrix * CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1235 wxDouble tx=0.0, wxDouble ty=0.0);
1236
1237
1238 virtual wxGraphicsPen* CreatePen(const wxPen& pen) ;
1239
1240 virtual wxGraphicsBrush* CreateBrush(const wxBrush& brush ) ;
1241
1242 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1243 virtual wxGraphicsBrush* CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1244 const wxColour&c1, const wxColour&c2) ;
1245
1246 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1247 // with radius r and color cColor
1248 virtual wxGraphicsBrush* CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1249 const wxColour &oColor, const wxColour &cColor) ;
1250
1251 // sets the font
1252 virtual wxGraphicsFont* CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1253
1254 private :
1255 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1256 } ;
1257
1258 //-----------------------------------------------------------------------------
1259 // wxCairoRenderer implementation
1260 //-----------------------------------------------------------------------------
1261
1262 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1263
1264 static wxCairoRenderer gs_cairoGraphicsRenderer;
1265
1266 #ifdef __WXGTK__
1267 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1268 {
1269 return &gs_cairoGraphicsRenderer;
1270 }
1271 #endif
1272
1273 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1274 {
1275 return new wxCairoContext(this,dc);
1276 }
1277
1278 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1279 {
1280 return new wxCairoContext(this,(cairo_t*)context);
1281 }
1282
1283
1284 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1285 {
1286 #ifdef __WXGTK__
1287 return new wxCairoContext(this,(GdkDrawable*)window);
1288 #else
1289 return NULL;
1290 #endif
1291 }
1292
1293 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1294 {
1295 return new wxCairoContext(this, window );
1296 }
1297
1298 // Path
1299
1300 wxGraphicsPath * wxCairoRenderer::CreatePath()
1301 {
1302 return new wxCairoPath( this );
1303 }
1304
1305
1306 // Matrix
1307
1308 wxGraphicsMatrix * wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1309 wxDouble tx, wxDouble ty)
1310
1311 {
1312 wxCairoMatrix* m = new wxCairoMatrix( this );
1313 m->Set( a,b,c,d,tx,ty ) ;
1314 return m;
1315 }
1316
1317 wxGraphicsPen* wxCairoRenderer::CreatePen(const wxPen& pen)
1318 {
1319 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1320 return NULL;
1321 else
1322 return new wxCairoPen( this, pen );
1323 }
1324
1325 wxGraphicsBrush* wxCairoRenderer::CreateBrush(const wxBrush& brush )
1326 {
1327 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1328 return NULL;
1329 else
1330 return new wxCairoBrush( this, brush );
1331 }
1332
1333 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1334 wxGraphicsBrush* wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1335 const wxColour&c1, const wxColour&c2)
1336 {
1337 wxCairoBrush* brush = new wxCairoBrush(this);
1338 brush->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1339 return brush;
1340 }
1341
1342 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1343 // with radius r and color cColor
1344 wxGraphicsBrush* wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1345 const wxColour &oColor, const wxColour &cColor)
1346 {
1347 wxCairoBrush* brush = new wxCairoBrush(this);
1348 brush->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1349 return brush;
1350 }
1351
1352 // sets the font
1353 wxGraphicsFont* wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1354 {
1355 if ( font.Ok() )
1356 return new wxCairoFont( this , font, col );
1357 else
1358 return NULL;
1359 }
1360
1361 #endif // wxUSE_GRAPHICS_CONTEXT