~canonical-dx-team/unity/unity.fix-ql-losing-focus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2010 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Jay Taoko <jay.taoko@canonical.com>
 * Authored by: Mirco Müller <mirco.mueller@canonical.com
 */

#include "Nux/Nux.h"
#include "Nux/VLayout.h"
#include "Nux/HLayout.h"
#include "Nux/WindowThread.h"
#include "Nux/WindowCompositor.h"
#include "Nux/BaseWindow.h"
#include "Nux/Button.h"
#include "NuxGraphics/GraphicsEngine.h"
#include "Nux/TextureArea.h"
#include "NuxImage/CairoGraphics.h"

#include "QuicklistMenuItem.h"

#include "Tooltip.h"

namespace nux
{
  NUX_IMPLEMENT_OBJECT_TYPE (Tooltip);
  
  Tooltip::Tooltip ()
  {
    _name = g_strdup ("Tooltip");
    _texture_bg = 0;
    _texture_mask = 0;
    _texture_outline = 0;
    _cairo_text_has_changed = true;

    _anchorX   = 0;
    _anchorY   = 0;
    _labelText = TEXT ("Unity");

    _anchor_width   = 10;
    _anchor_height  = 18;
    _corner_radius  = 4;
    _padding        = 15;
    _top_size       = 4;

    _hlayout         = new nux::HLayout (TEXT(""), NUX_TRACKER_LOCATION);
    _vlayout         = new nux::VLayout (TEXT(""), NUX_TRACKER_LOCATION);

    _left_space = new nux::SpaceLayout (_padding + _anchor_width + _corner_radius, _padding + _anchor_width + _corner_radius, 1, 1000);
    _right_space = new nux::SpaceLayout (_padding + _corner_radius, _padding + _corner_radius, 1, 1000);

    _top_space = new nux::SpaceLayout (1, 1000, _padding + _corner_radius, _padding + _corner_radius);
    _bottom_space = new nux::SpaceLayout (1, 1000, _padding + _corner_radius, _padding + _corner_radius);

    _vlayout->AddLayout(_top_space, 0);

    _tooltip_text = new nux::StaticCairoText (_labelText.GetTCharPtr (), NUX_TRACKER_LOCATION);
    _tooltip_text->sigTextChanged.connect (sigc::mem_fun (this, &Tooltip::RecvCairoTextChanged));
    _tooltip_text->sigFontChanged.connect (sigc::mem_fun (this, &Tooltip::RecvCairoTextChanged));
    _tooltip_text->Reference();
    
    _vlayout->AddView(_tooltip_text, 1, eCenter, eFull);

    _vlayout->AddLayout(_bottom_space, 0);
 
    _hlayout->AddLayout(_left_space, 0);
    _hlayout->AddLayout(_vlayout, 1, eCenter, eFull);
    _hlayout->AddLayout(_right_space, 0);

    SetWindowSizeMatchLayout (true);
    SetLayout (_hlayout);
  
  }

  Tooltip::~Tooltip ()
  {
    if (_name)
      g_free (_name);
    
    if (_texture_bg)
      _texture_bg->UnReference ();

    _tooltip_text->UnReference();
  }

  long Tooltip::ProcessEvent (IEvent& ievent, long TraverseInfo, long ProcessEventInfo)
  {
    long ret = TraverseInfo;

    _tooltip_text->ProcessEvent(ievent, ret, ProcessEventInfo);
    
    return ret;
  }
  
  void Tooltip::ShowTooltipWithTipAt (int anchor_tip_x, int anchor_tip_y)
  {
    int window_width;
    int window_height;

    window_width = nux::GetWindow ().GetWindowWidth ();
    window_height = nux::GetWindow ().GetWindowHeight ();

    _anchorX = anchor_tip_x;
    _anchorY = anchor_tip_y;

    int x = _anchorX - _padding;
    int y = anchor_tip_y - _anchor_height/2 - _top_size - _corner_radius - _padding;

    SetBaseX (x);
    SetBaseY (y);

    ShowWindow (true);
  }

  void Tooltip::Draw (GraphicsEngine& gfxContext, bool forceDraw)
  {
    Geometry base = GetGeometry();

    // the elements position inside the window are referenced to top-left window
    // corner. So bring base to (0, 0).
    base.SetX (0);
    base.SetY (0);
    gfxContext.PushClippingRectangle (base);

    GetGraphicsEngine().GetRenderStates().SetBlend (false);

    TexCoordXForm texxform_bg;
    texxform_bg.SetWrap(TEXWRAP_CLAMP, TEXWRAP_CLAMP);
    texxform_bg.SetTexCoordType (TexCoordXForm::OFFSET_COORD);

    TexCoordXForm texxform_mask;
    texxform_mask.SetWrap(TEXWRAP_CLAMP, TEXWRAP_CLAMP);
    texxform_mask.SetTexCoordType (TexCoordXForm::OFFSET_COORD);


    gfxContext.QRP_2TexMod (base.x,
      base.y,
      base.width,
      base.height,
      _texture_bg->GetDeviceTexture(),
      texxform_bg,
      Color(1.0f, 1.0f, 1.0f, 1.0f),
      _texture_mask->GetDeviceTexture(),
      texxform_mask,
      Color(1.0f, 1.0f, 1.0f, 1.0f));


    TexCoordXForm texxform;
    texxform.SetWrap(TEXWRAP_CLAMP, TEXWRAP_CLAMP);
    texxform.SetTexCoordType (TexCoordXForm::OFFSET_COORD);

    GetGraphicsEngine ().GetRenderStates ().SetBlend (true);
    GetGraphicsEngine ().GetRenderStates ().SetPremultipliedBlend (nux::SRC_OVER);
    gfxContext.QRP_1Tex (base.x,
      base.y,
      base.width,
      base.height,
      _texture_outline->GetDeviceTexture(),
      texxform,
      Color(1.0f, 1.0f, 1.0f, 1.0f));

    GetGraphicsEngine().GetRenderStates().SetBlend (false);

    _tooltip_text->ProcessDraw(gfxContext, forceDraw);
    
    gfxContext.PopClippingRectangle ();
  }

  void Tooltip::DrawContent (GraphicsEngine& GfxContext, bool force_draw)
  {

  }

  void Tooltip::PreLayoutManagement ()
  {
    int MaxItemWidth = 0;
    int TotalItemHeight = 0;
    int  textWidth  = 0;
    int  textHeight = 0;
    
    _tooltip_text->GetTextExtents(textWidth, textHeight);
    
    if (textWidth > MaxItemWidth)
      MaxItemWidth = textWidth;
    TotalItemHeight += textHeight;


    if(TotalItemHeight < _anchor_height)
    {
      _top_space->SetMinMaxSize(1, (_anchor_height - TotalItemHeight)/2 +1 + _padding + _corner_radius);
      _bottom_space->SetMinMaxSize(1, (_anchor_height - TotalItemHeight)/2 +1 + _padding + _corner_radius);
    }

    BaseWindow::PreLayoutManagement ();
  }

  long Tooltip::PostLayoutManagement (long LayoutResult)
  {
    long result = BaseWindow::PostLayoutManagement (LayoutResult);
    UpdateTexture ();

    int x = _padding + _anchor_width + _corner_radius;
    int y = _padding + _corner_radius;

    _tooltip_text->SetBaseX (x);
    _tooltip_text->SetBaseY (y);

    return result;
  }

  void Tooltip::RecvCairoTextChanged (StaticCairoText* cairo_text)
  {
    _cairo_text_has_changed = true;
  }

/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

  static inline void _blurinner (guchar* pixel,
    gint   *zR,
    gint   *zG,
    gint   *zB,
    gint   *zA,
    gint    alpha,
    gint    aprec,
    gint    zprec)
  {
    gint R;
    gint G;
    gint B;
    guchar A;

    R = *pixel;
    G = *(pixel + 1);
    B = *(pixel + 2);
    A = *(pixel + 3);

    *zR += (alpha * ((R << zprec) - *zR)) >> aprec;
    *zG += (alpha * ((G << zprec) - *zG)) >> aprec;
    *zB += (alpha * ((B << zprec) - *zB)) >> aprec;
    *zA += (alpha * ((A << zprec) - *zA)) >> aprec;

    *pixel       = *zR >> zprec;
    *(pixel + 1) = *zG >> zprec;
    *(pixel + 2) = *zB >> zprec;
    *(pixel + 3) = *zA >> zprec;
  }

  static inline void _blurrow (guchar* pixels,
    gint    width,
    gint    height,
    gint    channels,
    gint    line,
    gint    alpha,
    gint    aprec,
    gint    zprec)
  {
    gint    zR;
    gint    zG;
    gint    zB;
    gint    zA;
    gint    index;
    guchar* scanline;

    scanline = &(pixels[line * width * channels]);

    zR = *scanline << zprec;
    zG = *(scanline + 1) << zprec;
    zB = *(scanline + 2) << zprec;
    zA = *(scanline + 3) << zprec;

    for (index = 0; index < width; index ++)
      _blurinner (&scanline[index * channels], &zR, &zG, &zB, &zA, alpha, aprec,
      zprec);

    for (index = width - 2; index >= 0; index--)
      _blurinner (&scanline[index * channels], &zR, &zG, &zB, &zA, alpha, aprec,
      zprec);
  }

  static inline void _blurcol (guchar* pixels,
    gint    width,
    gint    height,
    gint    channels,
    gint    x,
    gint    alpha,
    gint    aprec,
    gint    zprec)
  {
    gint zR;
    gint zG;
    gint zB;
    gint zA;
    gint index;
    guchar* ptr;

    ptr = pixels;

    ptr += x * channels;

    zR = *((guchar*) ptr    ) << zprec;
    zG = *((guchar*) ptr + 1) << zprec;
    zB = *((guchar*) ptr + 2) << zprec;
    zA = *((guchar*) ptr + 3) << zprec;

    for (index = width; index < (height - 1) * width; index += width)
      _blurinner ((guchar*) &ptr[index * channels], &zR, &zG, &zB, &zA, alpha,
      aprec, zprec);

    for (index = (height - 2) * width; index >= 0; index -= width)
      _blurinner ((guchar*) &ptr[index * channels], &zR, &zG, &zB, &zA, alpha,
      aprec, zprec);
  }

  //
  // pixels   image-data
  // width    image-width
  // height   image-height
  // channels image-channels
  //
  // in-place blur of image 'img' with kernel of approximate radius 'radius'
  //
  // blurs with two sided exponential impulse response
  //
  // aprec = precision of alpha parameter in fixed-point format 0.aprec
  //
  // zprec = precision of state parameters zR,zG,zB and zA in fp format 8.zprec
  //
  void _expblur (guchar* pixels,
    gint    width,
    gint    height,
    gint    channels,
    gint    radius,
    gint    aprec,
    gint    zprec)
  {
    gint alpha;
    gint row = 0;
    gint col = 0;

    if (radius < 1)
      return;

    // calculate the alpha such that 90% of 
    // the kernel is within the radius.
    // (Kernel extends to infinity)
    alpha = (gint) ((1 << aprec) * (1.0f - expf (-2.3f / (radius + 1.f))));

    for (; row < height; row++)
      _blurrow (pixels, width, height, channels, row, alpha, aprec, zprec);

    for(; col < width; col++)
      _blurcol (pixels, width, height, channels, col, alpha, aprec, zprec);

    return;
  }

/**
 * ctk_surface_blur:
 * @surface: pointer to a cairo image-surface
 * @radius: unsigned integer acting as the blur-radius to apply
 *
 * Applies an exponential blur on the passed surface executed on the CPU. Not as
 * nice as a real gaussian blur, but much faster.
 **/
void ctk_surface_blur (cairo_surface_t* surface,
                  guint            radius)
{
  guchar*        pixels;
  guint          width;
  guint          height;
  cairo_format_t format;

  // before we mess with the surface execute any pending drawing
  cairo_surface_flush (surface);

  pixels = cairo_image_surface_get_data (surface);
  width  = cairo_image_surface_get_width (surface);
  height = cairo_image_surface_get_height (surface);
  format = cairo_image_surface_get_format (surface);

  switch (format)
  {
    case CAIRO_FORMAT_ARGB32:
      _expblur (pixels, width, height, 4, radius, 16, 7);
    break;

    case CAIRO_FORMAT_RGB24:
      _expblur (pixels, width, height, 3, radius, 16, 7);
    break;

    case CAIRO_FORMAT_A8:
      _expblur (pixels, width, height, 1, radius, 16, 7);
    break;

    default :
      // do nothing
    break;
  }

  // inform cairo we altered the surfaces contents
  cairo_surface_mark_dirty (surface);	
}

    
  void tint_dot_hl (cairo_t* cr,
    gint    width,
    gint    height,
    gfloat  hl_x,
    gfloat  hl_y,
    gfloat  hl_size,
    gfloat* rgba_tint,
    gfloat* rgba_hl,
    gfloat* rgba_dot)
  {
    // clear normal context
    cairo_scale (cr, 1.0f, 1.0f);
    cairo_set_source_rgba (cr, 0.0f, 0.0f, 0.0f, 0.0f);
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
    cairo_paint (cr);

    // prepare drawing for normal context
    cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

    // create path in normal context
    cairo_rectangle (cr, 0.0f, 0.0f, (gdouble) width, (gdouble) height);  

    // fill path of normal context with tint
    cairo_set_source_rgba (cr,
      rgba_tint[0],
      rgba_tint[1],
      rgba_tint[2],
      rgba_tint[3]);
    cairo_fill (cr);
  }

  void _setup (cairo_surface_t** surf,
    cairo_t**         cr,
    gboolean          outline,
    gint              width,
    gint              height,
    gboolean          negative)
  {
    // clear context
    cairo_scale (*cr, 1.0f, 1.0f);
    if (outline)
    {
      cairo_set_source_rgba (*cr, 0.0f, 0.0f, 0.0f, 0.0f);
      cairo_set_operator (*cr, CAIRO_OPERATOR_CLEAR);
    }  
    else
    {
      cairo_set_operator (*cr, CAIRO_OPERATOR_OVER);
      if (negative)
        cairo_set_source_rgba (*cr, 0.0f, 0.0f, 0.0f, 0.0f);
      else
        cairo_set_source_rgba (*cr, 1.0f, 1.0f, 1.0f, 1.0f);
    }
    cairo_paint (*cr);
  }

  void _compute_full_mask_path (cairo_t* cr,
    gfloat   anchor_width,
    gfloat   anchor_height,
    gint     width,
    gint     height,
    gint     upper_size,
    gfloat   radius,
    guint    pad)
  {
    //     0  1        2  3
    //     +--+--------+--+
    //     |              |
    //     + 14           + 4
    //     |              |
    //     |              |
    //     |              |
    //     + 13           |
    //    /               |
    //   /                |
    //  + 12              |
    //   \                |
    //    \               |
    //  11 +              |
    //     |              |
    //     |              |
    //     |              |
    //  10 +              + 5
    //     |              |
    //     +--+--------+--+ 6
    //     9  8        7


    gfloat padding  = pad;
    int ZEROPOINT5 = 0.0f;

    gfloat HeightToAnchor = 0.0f;
    HeightToAnchor = ((gfloat) height - 2.0f * radius - anchor_height -2*padding) / 2.0f;
    if (HeightToAnchor < 0.0f)
    {
      g_warning ("Anchor-height and corner-radius a higher than whole texture!");
      return;
    }

    if(upper_size >= 0)
    {
      if(upper_size > height - 2.0f * radius - anchor_height -2 * padding)
      {
        //g_warning ("[_compute_full_mask_path] incorrect upper_size value");
        HeightToAnchor = 0;
      }
      else
      {
        HeightToAnchor = height - 2.0f * radius - anchor_height -2 * padding - upper_size;
      }
    }
    else
    {
      HeightToAnchor = (height - 2.0f * radius - anchor_height -2*padding) / 2.0f;
    }

    cairo_translate (cr, -0.5f, -0.5f);

    // create path
    cairo_move_to (cr, padding + anchor_width + radius + ZEROPOINT5, padding + ZEROPOINT5); // Point 1
    cairo_line_to (cr, width - padding - radius, padding + ZEROPOINT5);   // Point 2
    cairo_arc (cr,
      width  - padding - radius + ZEROPOINT5,
      padding + radius + ZEROPOINT5,
      radius,
      -90.0f * G_PI / 180.0f,
      0.0f * G_PI / 180.0f);   // Point 4
    cairo_line_to (cr,
      (gdouble) width - padding + ZEROPOINT5,
      (gdouble) height - radius - padding + ZEROPOINT5); // Point 5
    cairo_arc (cr,
      (gdouble) width - padding - radius + ZEROPOINT5,
      (gdouble) height - padding - radius + ZEROPOINT5,
      radius,
      0.0f * G_PI / 180.0f,
      90.0f * G_PI / 180.0f);  // Point 7
    cairo_line_to (cr,
      anchor_width + padding + radius + ZEROPOINT5,
      (gdouble) height - padding + ZEROPOINT5); // Point 8

    cairo_arc (cr,
      anchor_width + padding + radius + ZEROPOINT5,
      (gdouble) height - padding - radius,
      radius,
      90.0f * G_PI / 180.0f,
      180.0f * G_PI / 180.0f); // Point 10

    cairo_line_to (cr,
      padding + anchor_width + ZEROPOINT5,
      (gdouble) height - padding - radius - HeightToAnchor + ZEROPOINT5 );  // Point 11
    cairo_line_to (cr,
      padding + ZEROPOINT5,
      (gdouble) height - padding - radius - HeightToAnchor - anchor_height / 2.0f + ZEROPOINT5); // Point 12
    cairo_line_to (cr,
      padding + anchor_width + ZEROPOINT5,
      (gdouble) height - padding - radius - HeightToAnchor - anchor_height + ZEROPOINT5);  // Point 13

    cairo_line_to (cr, padding + anchor_width + ZEROPOINT5, padding + radius  + ZEROPOINT5);  // Point 14
    cairo_arc (cr,
      padding + anchor_width + radius + ZEROPOINT5,
      padding + radius + ZEROPOINT5,
      radius,
      180.0f * G_PI / 180.0f,
      270.0f * G_PI / 180.0f);

    cairo_close_path (cr);
  }

  void compute_mask (cairo_t* cr)
  {
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
    cairo_fill_preserve (cr);
  }

  void compute_outline (cairo_t* cr,
    gfloat   line_width,
    gfloat*  rgba_line)
  {
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgba (cr,
      rgba_line[0],
      rgba_line[1],
      rgba_line[2],
      rgba_line[3]);
    cairo_set_line_width (cr, line_width);
    cairo_stroke (cr);
  }

  void _draw (cairo_t* cr,
    gboolean outline,
    gfloat   line_width,
    gfloat*  rgba,
    gboolean negative,
    gboolean stroke)
  {
    // prepare drawing
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);

    // actually draw the mask
    if (outline)
    {
      cairo_set_line_width (cr, line_width);
      cairo_set_source_rgba (cr, rgba[0], rgba[1], rgba[2], rgba[3]);
    }
    else
    {
      if (negative)
        cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 1.0f);
      else
        cairo_set_source_rgba (cr, 0.0f, 0.0f, 0.0f, 0.0f);
    }

    // stroke or fill?
    if (stroke)
      cairo_stroke_preserve (cr);
    else
      cairo_fill_preserve (cr);
  }

  void _finalize (cairo_t** cr,
    gboolean  outline,
    gfloat    line_width,
    gfloat*   rgba,
    gboolean  negative,
    gboolean  stroke)
  {
    // prepare drawing
    cairo_set_operator (*cr, CAIRO_OPERATOR_SOURCE);

    // actually draw the mask
    if (outline)
    {
      cairo_set_line_width (*cr, line_width);
      cairo_set_source_rgba (*cr, rgba[0], rgba[1], rgba[2], rgba[3]);
    }
    else
    {
      if (negative)
        cairo_set_source_rgba (*cr, 1.0f, 1.0f, 1.0f, 1.0f);
      else
        cairo_set_source_rgba (*cr, 0.0f, 0.0f, 0.0f, 0.0f);
    }

    // stroke or fill?
    if (stroke)
      cairo_stroke (*cr);
    else
      cairo_fill (*cr);
  }

  void
    compute_full_outline_shadow (
    cairo_t* cr,
    cairo_surface_t* surf,
    gint    width,
    gint    height,
    gfloat  anchor_width,
    gfloat  anchor_height,
    gint    upper_size,
    gfloat  corner_radius,
    guint   blur_coeff,
    gfloat* rgba_shadow,
    gfloat  line_width,
    gint    padding_size,
    gfloat* rgba_line)
  {
    _setup (&surf, &cr, TRUE, width, height, FALSE);
    _compute_full_mask_path (cr,
      anchor_width,
      anchor_height,
      width,
      height,
      upper_size,
      corner_radius,
      padding_size);

    _draw (cr, TRUE, line_width, rgba_shadow, FALSE, FALSE);
    ctk_surface_blur (surf, blur_coeff);
    compute_mask (cr);
  }

  void compute_full_mask (
    cairo_t* cr,
    cairo_surface_t* surf,
    gint     width,
    gint     height,
    gfloat   radius,
    guint    shadow_radius,
    gfloat   anchor_width,
    gfloat   anchor_height,
    gint     upper_size,
    gboolean negative,
    gboolean outline,
    gfloat   line_width,
    gint     padding_size,
    gfloat*  rgba)
  {
    _setup (&surf, &cr, outline, width, height, negative);
    _compute_full_mask_path (cr,
      anchor_width,
      anchor_height,
      width,
      height,
      upper_size,
      radius,
      padding_size);
    _finalize (&cr, outline, line_width, rgba, negative, outline);
  }

  void Tooltip::UpdateTexture ()
  {
    if (_cairo_text_has_changed == false)
      return;

    int height = GetBaseHeight ();
    int size_above_anchor = -1; // equal to sise below
    
    _top_size = 0;
    size_above_anchor = -1;
    int x = _anchorX - _padding;
    int y = _anchorY - height/2;

    SetBaseX (x);
    SetBaseY (y);

    float blur_coef         = 6.0f;

    CairoGraphics* cairo_bg       = new CairoGraphics (CAIRO_FORMAT_ARGB32, GetBaseWidth (), GetBaseHeight ());
    CairoGraphics* cairo_mask     = new CairoGraphics (CAIRO_FORMAT_ARGB32, GetBaseWidth (), GetBaseHeight ());
    CairoGraphics* cairo_outline  = new CairoGraphics (CAIRO_FORMAT_ARGB32, GetBaseWidth (), GetBaseHeight ());

    cairo_t *cr_bg      = cairo_bg->GetContext ();
    cairo_t *cr_mask    = cairo_mask->GetContext ();
    cairo_t *cr_outline = cairo_outline->GetContext ();

    float   tint_color[4]    = {0.074f, 0.074f, 0.074f, 0.80f};
    float   hl_color[4]      = {1.0f, 1.0f, 1.0f, 0.15f};
    float   dot_color[4]     = {1.0f, 1.0f, 1.0f, 0.20f};
    float   shadow_color[4]  = {0.0f, 0.0f, 0.0f, 1.00f};
    float   outline_color[4] = {1.0f, 1.0f, 1.0f, 0.75f};
    float   mask_color[4]    = {1.0f, 1.0f, 1.0f, 1.00f};

    tint_dot_hl (cr_bg,
      GetBaseWidth (),
      GetBaseHeight (),
      GetBaseWidth () / 2.0f,
      0,
      Max<float>(GetBaseWidth () / 1.3f, GetBaseHeight () / 1.3f),
      tint_color,
      hl_color,
      dot_color);

    compute_full_outline_shadow
      (
      cr_outline,
      cairo_outline->GetSurface(),
      GetBaseWidth (),
      GetBaseHeight (),
      _anchor_width,
      _anchor_height,
      -1,
      _corner_radius,
      blur_coef,
      shadow_color,
      1.0f,
      _padding,
      outline_color);

    compute_full_mask (
      cr_mask,
      cairo_mask->GetSurface(),
      GetBaseWidth (),
      GetBaseHeight(),
      _corner_radius, // radius,
      16,             // shadow_radius,
      _anchor_width,  // anchor_width,
      _anchor_height, // anchor_height,
      -1,             // upper_size,
      true,           // negative,
      false,          // outline,
      1.0,            // line_width,
      _padding,       // padding_size,
      mask_color);

    cairo_destroy (cr_bg);
    cairo_destroy (cr_outline);
    cairo_destroy (cr_mask);

    NBitmapData* bitmap = cairo_bg->GetBitmap();

    if (_texture_bg)
      _texture_bg->UnReference ();
    _texture_bg = GetThreadGLDeviceFactory()->CreateSystemCapableTexture ();
    _texture_bg->Update(bitmap);
    delete bitmap;

    bitmap = cairo_mask->GetBitmap();
    if (_texture_mask)
      _texture_mask->UnReference ();
    _texture_mask = GetThreadGLDeviceFactory()->CreateSystemCapableTexture ();
    _texture_mask->Update(bitmap);
    delete bitmap;

    bitmap = cairo_outline->GetBitmap();
    if (_texture_outline)
      _texture_outline->UnReference ();
    _texture_outline = GetThreadGLDeviceFactory()->CreateSystemCapableTexture ();
    _texture_outline->Update(bitmap);
    delete bitmap;

    delete cairo_bg;
    delete cairo_mask;
    delete cairo_outline;
    _cairo_text_has_changed = false;
  }

  void Tooltip::PositionChildLayout (float offsetX,
                                float offsetY)
  {
  }

  void Tooltip::LayoutWindowElements ()
  {
  }

  void Tooltip::NotifyConfigurationChange (int width,
                                      int height)
  {
  }

  void Tooltip::SetText (NString text)
  {
    if (_labelText == text)
      return;

    _labelText = text;
    _tooltip_text->SetText (_labelText);
    QueueRelayout ();
  }

  // Introspection

  const gchar* Tooltip::GetName ()
  {
    return g_strdup (_name);
  }

  void Tooltip::AddProperties (GVariantBuilder *builder)
  {
    g_variant_builder_add (builder, "{sv}", "text", g_variant_new_string (_labelText.GetTCharPtr ()));
    g_variant_builder_add (builder, "{sv}", "x", g_variant_new_int32  (GetBaseX ()));
    g_variant_builder_add (builder, "{sv}", "y", g_variant_new_int32  (GetBaseY ()));
    g_variant_builder_add (builder, "{sv}", "width", g_variant_new_int32 (GetBaseWidth ()));
    g_variant_builder_add (builder, "{sv}", "height", g_variant_new_int32 (GetBaseHeight ()));
    g_variant_builder_add (builder, "{sv}", "active", g_variant_new_boolean (IsVisible ()));
  }

} // namespace nux