-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicklistView.cpp
1358 lines (1152 loc) · 35.4 KB
/
QuicklistView.cpp
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- 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 <NuxGraphics/CairoGraphics.h>
#include <UnityCore/Variant.h>
#include "unity-shared/CairoTexture.h"
#include "QuicklistView.h"
#include "QuicklistMenuItem.h"
#include "QuicklistMenuItemLabel.h"
#include "QuicklistMenuItemSeparator.h"
#include "QuicklistMenuItemCheckmark.h"
#include "QuicklistMenuItemRadio.h"
#include "unity-shared/Introspectable.h"
#include "unity-shared/UnitySettings.h"
#include "unity-shared/ubus-server.h"
#include "unity-shared/UBusMessages.h"
#include "unity-shared/DashStyle.h"
namespace unity
{
namespace
{
const int ANCHOR_WIDTH = 10.0f;
}
NUX_IMPLEMENT_OBJECT_TYPE(QuicklistView);
QuicklistView::QuicklistView()
: _anchorX(0)
, _anchorY(0)
, _labelText("QuicklistView 1234567890")
, _top_size(4)
, _mouse_down(false)
, _enable_quicklist_for_testing(false)
, _anchor_width(10)
, _anchor_height(18)
, _corner_radius(4)
, _padding(13)
, _left_padding_correction(-1)
, _offset_correction(-1)
, _cairo_text_has_changed(true)
, _current_item_index(-1)
{
SetGeometry(nux::Geometry(0, 0, 1, 1));
_left_space = new nux::SpaceLayout(_padding +
_anchor_width +
_corner_radius +
_left_padding_correction,
_padding +
_anchor_width +
_corner_radius +
_left_padding_correction,
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 = new nux::VLayout(TEXT(""), NUX_TRACKER_LOCATION);
_vlayout->AddLayout(_top_space, 0);
_item_layout = new nux::VLayout(TEXT(""), NUX_TRACKER_LOCATION);
_vlayout->AddLayout(_item_layout, 0);
_vlayout->AddLayout(_bottom_space, 0);
_vlayout->SetMinimumWidth(140);
_hlayout = new nux::HLayout(TEXT(""), NUX_TRACKER_LOCATION);
_hlayout->AddLayout(_left_space, 0);
_hlayout->AddLayout(_vlayout, 0, nux::eCenter, nux::eFull);
_hlayout->AddLayout(_right_space, 0);
SetWindowSizeMatchLayout(true);
SetLayout(_hlayout);
mouse_down_outside_pointer_grab_area.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseDownOutsideOfQuicklist));
mouse_down.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseDown));
mouse_up.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseUp));
mouse_click.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseClick));
mouse_move.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseMove));
mouse_drag.connect(sigc::mem_fun(this, &QuicklistView::RecvMouseDrag));
key_down.connect(sigc::mem_fun(this, &QuicklistView::RecvKeyPressed));
begin_key_focus.connect(sigc::mem_fun(this, &QuicklistView::RecvStartFocus));
end_key_focus.connect(sigc::mem_fun(this, &QuicklistView::RecvEndFocus));
SetAcceptKeyNavFocus(true);
}
void
QuicklistView::RecvStartFocus()
{
PushToFront();
}
void
QuicklistView::RecvEndFocus()
{
}
void
QuicklistView::SelectItem(int index)
{
CancelItemsPrelightStatus();
int target_item = -1;
if (IsMenuItemSelectable(index))
{
QuicklistMenuItem* menu_item = GetNthItems(index);
if (menu_item)
{
target_item = index;
menu_item->Select();
}
}
if (_current_item_index != target_item)
{
_current_item_index = target_item;
selection_change.emit();
QueueDraw();
}
}
bool
QuicklistView::IsMenuItemSelectable(int index)
{
QuicklistMenuItem* menu_item = nullptr;
if (index < 0)
return false;
menu_item = GetNthItems(index);
if (!menu_item)
return false;
return menu_item->GetSelectable();
}
void
QuicklistView::RecvKeyPressed(unsigned long eventType,
unsigned long key_sym,
unsigned long key_state,
const char* character,
unsigned short keyCount)
{
switch (key_sym)
{
// home or page up (highlight the first menu-hitem)
case NUX_VK_PAGE_UP:
case NUX_VK_HOME:
{
int num_items = GetNumItems();
int target_index = -1;
do
{
++target_index;
}
while (!IsMenuItemSelectable(target_index) && target_index < num_items);
if (target_index < num_items)
SelectItem(target_index);
break;
}
// end or page down (highlight the last menu-hitem)
case NUX_VK_PAGE_DOWN:
case NUX_VK_END:
{
int target_index = GetNumItems();
do
{
--target_index;
}
while (!IsMenuItemSelectable(target_index) && target_index >= 0);
if (target_index >= 0)
SelectItem(target_index);
break;
}
// up (highlight previous menu-item)
case NUX_VK_UP:
case NUX_KP_UP:
{
int target_index = _current_item_index;
bool loop_back = false;
if (target_index <= 0)
target_index = GetNumItems();
do
{
--target_index;
// If the first item is not selectable, we must loop from the last one
if (!loop_back && target_index == 0 && !IsMenuItemSelectable(target_index))
{
loop_back = true;
target_index = GetNumItems() - 1;
}
}
while (!IsMenuItemSelectable(target_index) && target_index >= 0);
if (target_index >= 0)
SelectItem(target_index);
break;
}
// down (highlight next menu-item)
case NUX_VK_DOWN:
case NUX_KP_DOWN:
{
int target_index = _current_item_index;
int num_items = GetNumItems();
bool loop_back = false;
if (target_index >= num_items - 1)
target_index = -1;
do
{
++target_index;
// If the last item is not selectable, we must loop from the first one
if (!loop_back && target_index == num_items - 1 && !IsMenuItemSelectable(target_index))
{
loop_back = true;
target_index = 0;
}
}
while (!IsMenuItemSelectable(target_index) && target_index < num_items);
if (target_index < num_items)
SelectItem(target_index);
break;
}
// left (close quicklist, go back to laucher key-nav)
case NUX_VK_LEFT:
case NUX_KP_LEFT:
Hide();
// inform Launcher we switch back to Launcher key-nav
ubus_server_send_message(ubus_server_get_default(),
UBUS_QUICKLIST_END_KEY_NAV,
NULL);
break;
// esc (close quicklist, exit key-nav)
case NUX_VK_ESCAPE:
Hide();
// inform UnityScreen we leave key-nav completely
ubus_server_send_message(ubus_server_get_default(),
UBUS_LAUNCHER_END_KEY_NAV,
NULL);
break;
// <SPACE>, <RETURN> (activate selected menu-item)
case NUX_VK_SPACE:
case NUX_VK_ENTER:
case NUX_KP_ENTER:
if (IsMenuItemSelectable(_current_item_index))
{
ActivateItem(GetNthItems(_current_item_index));
Hide();
}
break;
default:
break;
}
}
QuicklistView::~QuicklistView()
{
for (auto item : _item_list)
{
// Remove from introspection
RemoveChild(item);
item->UnReference();
}
_item_list.clear();
}
void
QuicklistView::EnableQuicklistForTesting(bool enable_testing)
{
_enable_quicklist_for_testing = enable_testing;
}
void QuicklistView::ShowQuicklistWithTipAt(int anchor_tip_x, int anchor_tip_y)
{
_anchorX = anchor_tip_x;
_anchorY = anchor_tip_y;
if (!_enable_quicklist_for_testing)
{
if (!_item_list.empty())
{
int offscreen_size = GetBaseY() +
GetBaseHeight() -
nux::GetWindowThread()->GetGraphicsDisplay().GetWindowHeight();
if (offscreen_size > 0)
_top_size = offscreen_size;
else
_top_size = 4;
int x = _anchorX - _padding;
int y = _anchorY - _anchor_height / 2 - _top_size - _corner_radius - _padding;
SetBaseX(x);
SetBaseY(y);
}
else
{
_top_size = 0;
int x = _anchorX - _padding;
int y = _anchorY - _anchor_height / 2 - _top_size - _corner_radius - _padding;
SetBaseX(x);
SetBaseY(y);
}
}
Show();
}
void QuicklistView::ShowWindow(bool b, bool start_modal)
{
unity::CairoBaseWindow::ShowWindow(b, start_modal);
}
void QuicklistView::Show()
{
if (!IsVisible())
{
// FIXME: ShowWindow shouldn't need to be called first
ShowWindow(true);
PushToFront();
//EnableInputWindow (true, "quicklist", false, true);
//SetInputFocus ();
GrabPointer();
GrabKeyboard();
QueueDraw();
_compute_blur_bkg = true;
}
}
void QuicklistView::Hide()
{
if (IsVisible() && !_enable_quicklist_for_testing)
{
CancelItemsPrelightStatus();
CaptureMouseDownAnyWhereElse(false);
UnGrabPointer();
UnGrabKeyboard();
//EnableInputWindow (false);
ShowWindow(false);
if (_current_item_index != -1)
{
selection_change.emit();
_current_item_index = -1;
}
}
}
void QuicklistView::Draw(nux::GraphicsEngine& gfxContext, bool forceDraw)
{
CairoBaseWindow::Draw(gfxContext, forceDraw);
nux::Geometry base(GetGeometry());
base.x = 0;
base.y = 0;
gfxContext.PushClippingRectangle(base);
for (auto item : _item_list)
{
if (item->GetVisible())
item->ProcessDraw(gfxContext, forceDraw);
}
gfxContext.PopClippingRectangle();
}
void QuicklistView::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
{}
void QuicklistView::PreLayoutManagement()
{
int MaxItemWidth = 0;
int TotalItemHeight = 0;
for (auto item : _item_list)
{
// Make sure item is in layout if it should be
if (!item->GetVisible())
{
_item_layout->RemoveChildObject(item);
continue;
}
else if (!item->GetParentObject())
{
_item_layout->AddView(item, 1, nux::eCenter, nux::eFull);
}
nux::Size const& text_extents = item->GetTextExtents();
MaxItemWidth = std::max(MaxItemWidth, text_extents.width);
TotalItemHeight += text_extents.height;
}
if (TotalItemHeight < _anchor_height)
{
int b = (_anchor_height - TotalItemHeight) / 2 + _padding + _corner_radius;
int t = b + _offset_correction;
_top_space->SetMinimumHeight(t);
_top_space->SetMaximumHeight(t);
_bottom_space->SetMinimumHeight(b);
_bottom_space->SetMaximumHeight(b);
}
else
{
int b = _padding + _corner_radius;
int t = b + _offset_correction;
_top_space->SetMinimumHeight(t);
_top_space->SetMaximumHeight(t);
_bottom_space->SetMinimumHeight(b);
_bottom_space->SetMaximumHeight(b);
}
_item_layout->SetMinimumWidth(MaxItemWidth);
CairoBaseWindow::PreLayoutManagement();
}
long QuicklistView::PostLayoutManagement(long LayoutResult)
{
long result = CairoBaseWindow::PostLayoutManagement(LayoutResult);
UpdateTexture();
int x = _padding + _anchor_width + _corner_radius + _offset_correction;
int y = _top_space->GetMinimumHeight();
for (auto item : _item_list)
{
if (!item->GetVisible())
continue;
item->SetBaseX(x);
item->SetBaseY(y);
y += item->GetBaseHeight();
}
// We must correct the width of line separators. The rendering of the separator can be smaller than the width of the
// quicklist. The reason for that is, the quicklist width is determined by the largest entry it contains. That size is
// only after MaxItemWidth is computed in QuicklistView::PreLayoutManagement.
// The setting of the separator width is done here after the Layout cycle for this widget is over. The width of the separator
// has bee set correctly during the layout cycle, but the cairo rendering still need to be adjusted.
unsigned separator_width = _item_layout->GetBaseWidth();
for (auto item : _item_list)
{
if (item->GetVisible() && item->GetCairoSurfaceWidth() != separator_width)
{
// Compute textures of the item.
item->UpdateTexture();
}
}
return result;
}
void QuicklistView::RecvCairoTextChanged(QuicklistMenuItem* cairo_text)
{
_cairo_text_has_changed = true;
}
void QuicklistView::RecvCairoTextColorChanged(QuicklistMenuItem* cairo_text)
{
NeedRedraw();
}
void QuicklistView::RecvItemMouseClick(QuicklistMenuItem* item, int x, int y)
{
_mouse_down = false;
if (IsVisible() && item->GetEnabled())
{
// Check if the mouse was released over an item and emit the signal
CheckAndEmitItemSignal(x + item->GetBaseX(), y + item->GetBaseY());
Hide();
}
}
void QuicklistView::CheckAndEmitItemSignal(int x, int y)
{
nux::Geometry geo;
for (auto item : _item_list)
{
if (!item->GetVisible())
continue;
geo = item->GetGeometry();
geo.width = _item_layout->GetBaseWidth();
if (geo.IsPointInside(x, y))
{
// An action is performed: send the signal back to the application
ActivateItem(item);
}
}
}
void QuicklistView::ActivateItem(QuicklistMenuItem* item)
{
if (!item)
return;
item->Activate();
}
void QuicklistView::RecvItemMouseRelease(QuicklistMenuItem* item, int x, int y)
{
_mouse_down = false;
if (IsVisible() && item->GetEnabled())
{
// Check if the mouse was released over an item and emit the signal
CheckAndEmitItemSignal(x + item->GetBaseX(), y + item->GetBaseY());
Hide();
}
}
void QuicklistView::CancelItemsPrelightStatus()
{
for (auto item : _item_list)
{
item->Select(false);
}
}
void QuicklistView::RecvItemMouseDrag(QuicklistMenuItem* item, int x, int y)
{
nux::Geometry geo;
for (auto it : _item_list)
{
int item_index = GetItemIndex(it);
if (!IsMenuItemSelectable(item_index))
continue;
geo = it->GetGeometry();
geo.width = _item_layout->GetBaseWidth();
if (geo.IsPointInside(x + item->GetBaseX(), y + item->GetBaseY()))
{
SelectItem(item_index);
}
}
}
void QuicklistView::RecvItemMouseEnter(QuicklistMenuItem* item)
{
int item_index = GetItemIndex(item);
SelectItem(item_index);
}
void QuicklistView::RecvItemMouseLeave(QuicklistMenuItem* item)
{
int item_index = GetItemIndex(item);
if (item_index < 0 || item_index == _current_item_index)
SelectItem(-1);
}
void QuicklistView::RecvMouseDown(int x, int y, unsigned long button_flags, unsigned long key_flags)
{
// if (IsVisible ())
// {
// CaptureMouseDownAnyWhereElse (false);
// UnGrabPointer ();
// EnableInputWindow (false);
// ShowWindow (false);
// }
}
void QuicklistView::RecvMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags)
{
// Check if the mouse was released over an item and emit the signal
CheckAndEmitItemSignal(x, y);
}
void QuicklistView::RecvMouseClick(int x, int y, unsigned long button_flags, unsigned long key_flags)
{
if (IsVisible())
{
Hide();
}
}
void QuicklistView::RecvMouseMove(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
{
}
void QuicklistView::RecvMouseDrag(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
{
}
void QuicklistView::RecvMouseDownOutsideOfQuicklist(int x, int y, unsigned long button_flags, unsigned long key_flags)
{
Hide();
}
void QuicklistView::RemoveAllMenuItem()
{
for (auto item : _item_list)
{
// Remove from introspection
RemoveChild(item);
item->UnReference();
}
_item_list.clear();
_item_layout->Clear();
_cairo_text_has_changed = true;
nux::GetWindowThread()->QueueObjectLayout(this);
}
void QuicklistView::AddMenuItem(QuicklistMenuItem* item)
{
if (item == 0)
return;
item->sigTextChanged.connect(sigc::mem_fun(this, &QuicklistView::RecvCairoTextChanged));
item->sigColorChanged.connect(sigc::mem_fun(this, &QuicklistView::RecvCairoTextColorChanged));
item->sigMouseClick.connect(sigc::mem_fun(this, &QuicklistView::RecvItemMouseClick));
item->sigMouseReleased.connect(sigc::mem_fun(this, &QuicklistView::RecvItemMouseRelease));
item->sigMouseEnter.connect(sigc::mem_fun(this, &QuicklistView::RecvItemMouseEnter));
item->sigMouseLeave.connect(sigc::mem_fun(this, &QuicklistView::RecvItemMouseLeave));
item->sigMouseDrag.connect(sigc::mem_fun(this, &QuicklistView::RecvItemMouseDrag));
_item_list.push_back(item);
item->Reference();
// Add to introspection
AddChild(item);
_cairo_text_has_changed = true;
nux::GetWindowThread()->QueueObjectLayout(this);
NeedRedraw();
}
void QuicklistView::RenderQuicklistView()
{
}
int QuicklistView::GetNumItems()
{
return _item_list.size();
}
QuicklistMenuItem* QuicklistView::GetNthItems(int index)
{
if (index < (int)_item_list.size())
{
int i = 0;
for (auto item : _item_list)
{
if (i++ == index)
return item;
}
}
return nullptr;
}
int QuicklistView::GetItemIndex(QuicklistMenuItem* item)
{
int index = -1;
for (auto it : _item_list)
{
++index;
if (it == item)
return index;
}
return index;
}
QuicklistMenuItemType QuicklistView::GetNthType(int index)
{
QuicklistMenuItem* item = GetNthItems(index);
if (item)
return item->GetItemType();
return QuicklistMenuItemType::UNKNOWN;
}
std::list<QuicklistMenuItem*> QuicklistView::GetChildren()
{
return _item_list;
}
void QuicklistView::SelectFirstItem()
{
SelectItem(0);
}
void ql_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)
{
cairo_surface_t* dots_surf = NULL;
cairo_t* dots_cr = NULL;
cairo_pattern_t* dots_pattern = NULL;
cairo_pattern_t* hl_pattern = NULL;
// create context for dot-pattern
dots_surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 4, 4);
dots_cr = cairo_create(dots_surf);
// 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_preserve(cr);
// create pattern in dot-context
cairo_set_operator(dots_cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(dots_cr);
cairo_scale(dots_cr, 1.0f, 1.0f);
cairo_set_operator(dots_cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(dots_cr,
rgba_dot[0],
rgba_dot[1],
rgba_dot[2],
rgba_dot[3]);
cairo_rectangle(dots_cr, 0.0f, 0.0f, 1.0f, 1.0f);
cairo_fill(dots_cr);
cairo_rectangle(dots_cr, 2.0f, 2.0f, 1.0f, 1.0f);
cairo_fill(dots_cr);
dots_pattern = cairo_pattern_create_for_surface(dots_surf);
// fill path of normal context with dot-pattern
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source(cr, dots_pattern);
cairo_pattern_set_extend(dots_pattern, CAIRO_EXTEND_REPEAT);
cairo_fill_preserve(cr);
cairo_pattern_destroy(dots_pattern);
cairo_surface_destroy(dots_surf);
cairo_destroy(dots_cr);
// draw highlight
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
hl_pattern = cairo_pattern_create_radial(hl_x,
hl_y,
0.0f,
hl_x,
hl_y,
hl_size);
cairo_pattern_add_color_stop_rgba(hl_pattern,
0.0f,
rgba_hl[0],
rgba_hl[1],
rgba_hl[2],
rgba_hl[3]);
cairo_pattern_add_color_stop_rgba(hl_pattern, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
cairo_set_source(cr, hl_pattern);
cairo_fill(cr);
cairo_pattern_destroy(hl_pattern);
}
void ql_setup(cairo_surface_t** surf,
cairo_t** cr,
gboolean outline,
gint width,
gint height,
gboolean negative)
{
// // create context
// if (outline)
// *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
// else
// *surf = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
// *cr = cairo_create (*surf);
// 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 ql_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;
}
//gint dynamic_size = height - 2*radius - 2*padding - anchor_height;
//gint upper_dynamic_size = upper_size;
//gint lower_dynamic_size = dynamic_size - upper_dynamic_size;
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 ql_compute_mask(cairo_t* cr)
{