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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/ui/graphics.h"
#include "gate/results.h"
#if defined(GATE_UI_GTK)
#include "gate/ui/gateui_gtk.h"
#include "gate/debugging.h"
gate_result_t gate_ui_graphics_create_virtual(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_uint32_t width, gate_uint32_t height)
{
return gate_ui_graphics_create_image(graph, host, width, height, 32);
}
static cairo_format_t gate_ui_graphics_resolve_format(gate_uint32_t depth)
{
if ((depth == 0) || (depth > 24))
{
return CAIRO_FORMAT_ARGB32;
}
else if (depth > 16)
{
return CAIRO_FORMAT_RGB24;
}
else if (depth > 8)
{
return CAIRO_FORMAT_RGB16_565;
}
else if (depth > 2)
{
return CAIRO_FORMAT_A8;
}
else
{
return CAIRO_FORMAT_A1;
}
}
gate_result_t gate_ui_graphics_create_image(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_uint32_t width, gate_uint32_t height, gate_uint32_t depth)
{
gate_result_t ret = GATE_RESULT_FAILED;
cairo_t* cr = NULL;
cairo_surface_t* surface = NULL;
cairo_format_t format;
do
{
gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
format = gate_ui_graphics_resolve_format(depth);
surface = cairo_image_surface_create(format, width, height);
if (surface == NULL)
{
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
cr = cairo_create(surface);
if (cr == NULL)
{
cairo_surface_destroy(surface);
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
graph->host = host;
graph->width = width;
graph->height = height;
graph->resources[0] = cr;
graph->resources[1] = surface;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_graphics_create_image_from(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_rasterimage_t const* rasterimage)
{
gate_result_t ret = gate_ui_graphics_create_image(graph, host, rasterimage->width, rasterimage->height, 32);
if (GATE_SUCCEEDED(ret))
{
gate_ui_point_t pnt = { 0, 0 };
gate_ui_size_t sz = { rasterimage->width, rasterimage->height };
ret = gate_ui_graphics_draw_image(graph, rasterimage, &pnt, &sz, &pnt);
if (GATE_FAILED(ret))
{
gate_ui_graphics_destroy(graph);
gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
}
}
return ret;
}
gate_result_t gate_ui_graphics_create_ctrl(gate_ui_graphics_t* graph, gate_ui_ctrl_t* ctrl)
{
gate_result_t ret = GATE_RESULT_FAILED;
cairo_surface_t* surface = NULL;
cairo_t* cr = NULL;
do
{
gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(ctrl);
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
GdkWindow* window = NULL;
int width, height;
if (!host || !widget)
{
ret = GATE_RESULT_NOTAVAILABLE;
break;
}
window = gtk_widget_get_window(widget);
width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height(widget);
surface = gdk_window_create_similar_surface(window, CAIRO_CONTENT_COLOR, width, height);
if (!surface)
{
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
cr = cairo_create(surface);
if (!cr)
{
ret = GATE_RESULT_FAILED;
break;
}
gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
graph->host = host;
graph->width = width;
graph->height = height;
graph->resources[0] = cr;
graph->resources[1] = surface;
cr = NULL;
surface = NULL;
ret = GATE_RESULT_OK;
} while (0);
if (cr)
{
cairo_destroy(cr);
}
if (surface)
{
cairo_surface_destroy(surface);
}
return ret;
}
gate_result_t gate_ui_graphics_create_native(gate_ui_graphics_t* graph, gate_ui_host_t* host, void* graphics, void* param, gate_int32_t width, gate_int32_t height)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
graph->host = host;
graph->resources[0] = cairo_reference((cairo_t*)graphics); /**< cairo_t */
graph->resources[1] = param; /**< surface */
graph->width = width;
graph->height = height;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_graphics_destroy(gate_ui_graphics_t* graph)
{
cairo_t* cr = graph->resources[0];
cairo_surface_t* surface = graph->resources[1];
if (surface)
{
cairo_surface_destroy(surface);
}
if (cr)
{
cairo_destroy(cr);
}
return GATE_RESULT_OK;
}
gate_int32_t gate_ui_graphics_width(gate_ui_graphics_t* graph)
{
return graph->width;
}
gate_int32_t gate_ui_graphics_height(gate_ui_graphics_t* graph)
{
return graph->height;
}
gate_result_t gate_ui_graphics_set_pixel(gate_ui_graphics_t* graph, gate_ui_point_t pos, gate_ui_color_t col)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
cairo_set_source_rgb(cr, col.r, col.g, col.b);
cairo_rectangle(cr, (double)pos.x, (double)pos.y, 1.0, 1.0);
cairo_fill(cr);
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_get_pixel(gate_ui_graphics_t* graph, gate_ui_point_t pos, gate_ui_color_t* col)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_graphics_draw(gate_ui_graphics_t* graph, gate_ui_graphics_t* srcgraph,
gate_ui_point_t const* dst, gate_ui_size_t const* size, gate_ui_point_t const* src)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
cairo_surface_t* src_surface = srcgraph->resources[1];
if (src_surface == NULL)
{
return GATE_RESULT_INVALIDINPUT;
}
cairo_set_source_surface(cr, src_surface, dst ? dst->x : 0, dst ? dst->y : 0);
/* TODO: dst/src */
cairo_paint(cr);
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_draw_ex(gate_ui_graphics_t* graph, gate_ui_graphics_t* src_graph,
gate_ui_position_t const* dest_rect, gate_ui_position_t const* src_rect)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
cairo_surface_t* src_surface = src_graph->resources[1];
if (src_surface == NULL)
{
return GATE_RESULT_INVALIDINPUT;
}
cairo_set_source_surface(cr, src_surface, 0, 0);
/* TODO: dst/src */
cairo_paint(cr);
return GATE_RESULT_OK;
}
static GdkPixbuf* gate_ui_gtk_raster_to_pixbuf(gate_rasterimage_t const* srcimage,
gate_int32_t src_x, gate_int32_t src_y, gate_int32_t width, gate_int32_t height)
{
GdkPixbuf* buf;
gate_int32_t x, y;
if (src_x + width > (gate_int32_t)srcimage->width)
{
width = (gate_int32_t)srcimage->width - src_x;
}
if (src_y + height > (gate_int32_t)srcimage->height)
{
height = (gate_int32_t)srcimage->height - src_y;
}
buf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, (int)width, (int)height);
if (buf != NULL)
{
guchar* pixels = gdk_pixbuf_get_pixels(buf);
for (y = 0; y < height; ++y)
{
guchar* dst_pixels = pixels + (y * width) * 4;
gate_color_t const* src_pixels = (((gate_color_t*)gate_rasterimage_get_line_ptr(srcimage, (src_y + y))) + src_x);
for (x = 0; x < width; ++x)
{
dst_pixels[0] = src_pixels->r;
dst_pixels[1] = src_pixels->g;
dst_pixels[2] = src_pixels->b;
dst_pixels[3] = src_pixels->a;
dst_pixels += 4;
src_pixels++;
}
}
}
return buf;
}
gate_result_t gate_ui_graphics_draw_image(gate_ui_graphics_t* graph, gate_rasterimage_t const* srcimage,
gate_ui_point_t const* dst, gate_ui_size_t const* size, gate_ui_point_t const* srcpos)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
int width, height;
GdkPixbuf* buf;
gate_int32_t src_x = 0;
gate_int32_t src_y = 0;
gate_int32_t dst_x = 0;
gate_int32_t dst_y = 0;
width = srcimage->width;
height = srcimage->height;
if (size)
{
width = size->width;
height = size->height;
}
if (dst)
{
dst_x = dst->x;
dst_y = dst->y;
}
if (srcpos)
{
src_x = srcpos->x;
src_y = srcpos->y;
}
buf = gate_ui_gtk_raster_to_pixbuf(srcimage, src_x, src_y, width, height);
if (buf == NULL)
{
return GATE_RESULT_OUTOFRESOURCES;
}
gdk_cairo_set_source_pixbuf(cr, buf, dst_x, dst_y);
cairo_paint(cr);
g_object_unref(buf);
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_line(gate_ui_graphics_t* graph, gate_ui_point_t from, gate_ui_point_t to, gate_ui_color_t col, gate_uint32_t linewidth)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
cairo_set_source_rgba(cr, (double)col.r / 255.0, (double)col.g / 255.0, (double)col.b / 255.0, (double)col.a / 255.0);
cairo_set_line_width(cr, (double)linewidth);
cairo_move_to(cr, from.x, from.y);
cairo_line_to(cr, to.x, to.y);
cairo_stroke(cr);
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_rect(gate_ui_graphics_t* graph, gate_ui_position_t rect, gate_ui_color_t const* colline, gate_uint32_t linewidth, gate_ui_color_t const* colfill)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
if (colfill)
{
cairo_set_source_rgba(cr, (double)colfill->r / 255.0, (double)colfill->g / 255.0, (double)colfill->b / 255.0, (double)colfill->a / 255.0);
cairo_rectangle(cr, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
cairo_fill(cr);
}
if (colline)
{
cairo_set_source_rgba(cr, (double)colline->r / 255.0, (double)colline->g / 255.0, (double)colline->b / 255.0, (double)colline->a / 255.0);
cairo_set_line_width(cr, (double)linewidth);
cairo_rectangle(cr, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
cairo_stroke(cr);
}
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_polygon(gate_ui_graphics_t* graph, gate_ui_point_t const* points, gate_size_t pointcount, gate_ui_color_t const* colline, gate_uint32_t linewidth, gate_ui_color_t const* colfill)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
if (pointcount > 2)
{
cairo_move_to(cr, points->x, points->y);
--pointcount;
++points;
while (pointcount-- != 0)
{
cairo_line_to(cr, points->x, points->y);
cairo_move_to(cr, points->x, points->y);
++points;
}
if (colfill)
{
cairo_set_source_rgba(cr, (double)colfill->r / 255.0, (double)colfill->g / 255.0, (double)colfill->b / 255.0, (double)colfill->a / 255.0);
if (colline)
{
cairo_fill_preserve(cr);
}
else
{
cairo_fill(cr);
}
}
if (colline)
{
cairo_set_source_rgba(cr, (double)colline->r / 255.0, (double)colline->g / 255.0, (double)colline->b / 255.0, (double)colline->a / 255.0);
cairo_set_line_width(cr, (double)linewidth);
cairo_stroke(cr);
}
}
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_get_char_size(gate_ui_graphics_t* graph, gate_ui_font_t const* font, gate_char32_t const* chars, gate_size_t charcount, gate_int32_t* textlens)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
gate_size_t ndx;
cairo_set_font_size(cr, font->size);
for (ndx = 0; ndx != charcount; ++ndx)
{
char c[8];
gate_size_t len = gate_char_write_utf8(chars[ndx], &c[0], sizeof(c) - 1);
c[len] = 0;
cairo_text_extents_t extents;
cairo_text_extents(cr, c, &extents);
textlens[ndx] = (gate_int32_t)extents.width + 0.5;
}
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_get_text_size(gate_ui_graphics_t* graph, gate_ui_font_t const* font, gate_string_t const* text, gate_int32_t* width, gate_int32_t* height)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
/*cairo_scaled_font_t* sf = cairo_get_scaled_font(cr);*/
cairo_text_extents_t extents;
gate_cstrbuffer_t buffer;
if(NULL == gate_cstrbuffer_create_string(&buffer, text, false))
{
return GATE_RESULT_OUTOFMEMORY;
}
cairo_set_font_size(cr, font->size);
cairo_text_extents(cr, gate_cstrbuffer_get(&buffer), &extents);
if (width) *width = (gate_int32_t)extents.width + 0.5;
if (height) *height = (gate_int32_t)extents.height + 0.5;
return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_print(gate_ui_graphics_t* graph, gate_ui_font_t const* font, gate_string_t const* text, gate_ui_position_t const* pos)
{
cairo_t* cr = (cairo_t*)graph->resources[0];
gate_cstrbuffer_t buffer;
if(NULL == gate_cstrbuffer_create_string(&buffer, text, false))
{
return GATE_RESULT_OUTOFMEMORY;
}
cairo_set_source_rgb(cr, font->color.r / 255.0, font->color.g / 255.0, font->color.b / 255.0);
cairo_set_font_size(cr, font->size);
cairo_move_to(cr, (double)pos->pos.x, (double)pos->pos.y);
cairo_show_text(cr, gate_cstrbuffer_get(&buffer));
/*
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
cairo_select_font_face(cr, "Purisa",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 13);
cairo_move_to(cr, 20, 30);
cairo_show_text(cr, "Most relationships seem so transitory");
cairo_move_to(cr, 20, 60);
cairo_show_text(cr, "They're all good but not the permanent one");
cairo_move_to(cr, 20, 120);
cairo_show_text(cr, "Who doesn't long for someone to hold");
cairo_move_to(cr, 20, 150);
cairo_show_text(cr, "Who knows how to love you without being told");
cairo_move_to(cr, 20, 180);
cairo_show_text(cr, "Somebody tell me why I'm on my own");
cairo_move_to(cr, 20, 210);
cairo_show_text(cr, "If there's a soulmate for everyone");
*/
return GATE_RESULT_OK;
}
void* gate_ui_graphics_handle(gate_ui_graphics_t* graph)
{
return graph->resources[0];
}
void* gate_ui_graphics_surface(gate_ui_graphics_t* graph)
{
return graph->resources[1];
}
/************
* icon GTK *
************/
/* https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html */
gate_result_t gate_ui_icon_create_stock(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_uint32_t stock_id, gate_uint32_t flags)
{
gate_result_t ret = GATE_RESULT_OK;
char const* gtk_stock_id = NULL;
GtkIconTheme* theme = NULL;
GtkIconInfo* info = NULL;
gint size;
GError* error = NULL;
GdkPixbuf* pixbuf = NULL;
do
{
if (GATE_FLAG_ENABLED(flags, GATE_UI_ICON_FLAG_LARGE))
{
size = 48;
}
else if (GATE_FLAG_ENABLED(flags, GATE_UI_ICON_FLAG_SMALL))
{
size = 16;
}
else
{
size = 32;
}
theme = gtk_icon_theme_get_default();
if (theme == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
break;
}
switch (stock_id)
{
case GATE_UI_ICON_STOCK_APP: gtk_stock_id = "system-run"; break;
case GATE_UI_ICON_STOCK_DOCUMENT: gtk_stock_id = "document-properties"; break;
case GATE_UI_ICON_STOCK_FOLDER: gtk_stock_id = "folder"; break;
case GATE_UI_ICON_STOCK_FOLDEROPEN: gtk_stock_id = "folder-open"; break;
case GATE_UI_ICON_STOCK_NEWFILE: gtk_stock_id = "document-new"; break;
case GATE_UI_ICON_STOCK_OPENFILE: gtk_stock_id = "document-open"; break;
case GATE_UI_ICON_STOCK_SAVEFILE: gtk_stock_id = "document-save"; break;
case GATE_UI_ICON_STOCK_PRINTER: gtk_stock_id = "document-print"; break;
case GATE_UI_ICON_STOCK_CUT: gtk_stock_id = "edit-cut"; break;
case GATE_UI_ICON_STOCK_COPY: gtk_stock_id = "edit-copy"; break;
case GATE_UI_ICON_STOCK_PASTE: gtk_stock_id = "edit-paste"; break;
case GATE_UI_ICON_STOCK_DELETE: gtk_stock_id = "edit-delete"; break;
case GATE_UI_ICON_STOCK_FIND: gtk_stock_id = "edit-find"; break;
case GATE_UI_ICON_STOCK_HELP: gtk_stock_id = "help-contents"; break;
case GATE_UI_ICON_STOCK_STORAGE: gtk_stock_id = "drive-harddisk"; break;
case GATE_UI_ICON_STOCK_COMPUTER: gtk_stock_id = "computer"; break;
case GATE_UI_ICON_STOCK_NETWORK: gtk_stock_id = "network-wired"; break;
case GATE_UI_ICON_STOCK_GLOBE: gtk_stock_id = "applications-internet"; break;
case GATE_UI_ICON_STOCK_SETTINGS: gtk_stock_id = "preferences-system"; break;
case GATE_UI_ICON_STOCK_HOME: gtk_stock_id = "go-home"; break;
case GATE_UI_ICON_STOCK_IMAGE: gtk_stock_id = "image-x-generic"; break;
case GATE_UI_ICON_STOCK_AUDIO: gtk_stock_id = "audio-x-generic"; break;
case GATE_UI_ICON_STOCK_VIDEO: gtk_stock_id = "video-x-generic"; break;
case GATE_UI_ICON_STOCK_MAIL: gtk_stock_id = "mail-send-receive"; break;
default: break;
}
info = gtk_icon_theme_lookup_icon(theme, gtk_stock_id, size, GTK_ICON_LOOKUP_USE_BUILTIN | GTK_ICON_LOOKUP_GENERIC_FALLBACK);
if (info == NULL)
{
ret = GATE_RESULT_NOMATCH;
break;
}
pixbuf = gtk_icon_info_load_icon(info, &error);
if (pixbuf == NULL)
{
ret = GATE_RESULT_FAILED;
break;
}
gate_mem_clear(icon, sizeof(gate_ui_icon_t));
icon->host = host;
icon->flags = flags & (GATE_UI_ICON_FLAG_SMALL | GATE_UI_ICON_FLAG_LARGE);
icon->resources[0] = pixbuf;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_icon_create_native(gate_ui_icon_t* icon, gate_ui_host_t* host, void* handle, gate_uint32_t flags)
{
gate_mem_clear(icon, sizeof(gate_ui_icon_t));
icon->host = host;
icon->flags = flags;
icon->resources[0] = handle;
return GATE_RESULT_OK;
}
gate_result_t gate_ui_icon_create_image(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_rasterimage_t const* image)
{
GdkPixbuf* buf = gate_ui_gtk_raster_to_pixbuf(image, 0, 0, image->width, image->height);
if (buf == NULL)
{
return GATE_RESULT_OUTOFRESOURCES;
}
gate_mem_clear(icon, sizeof(gate_ui_icon_t));
icon->host = host;
icon->flags = 0;
icon->resources[0] = buf;
return GATE_RESULT_OK;
}
gate_result_t gate_ui_icon_create_file(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_string_t const* filepath)
{
gate_result_t ret = GATE_RESULT_FAILED;
char file_buffer[GATE_MAX_FILEPATH_LENGTH];
GtkWidget* image_widget = NULL;
gint pix_size = 0;
gate_uint32_t flags = 0;
GATE_STRING_TO_BUFFER(filepath, file_buffer);
image_widget = gtk_image_new_from_file(file_buffer);
if (image_widget != NULL)
{
pix_size = gtk_image_get_pixel_size(GTK_IMAGE(image_widget));
if (pix_size < 24) flags |= GATE_UI_ICON_FLAG_SMALL;
if (pix_size > 40) flags |= GATE_UI_ICON_FLAG_LARGE;
ret = gate_ui_icon_create_native(icon, host, image_widget, flags);
if (GATE_FAILED(ret))
{
gtk_widget_destroy(image_widget);
}
}
return ret;
}
gate_result_t gate_ui_icon_copy(gate_ui_icon_t* dsticon, gate_ui_icon_t const* srcicon)
{
gate_mem_copy(dsticon, srcicon, sizeof(gate_ui_icon_t));
if (!GATE_FLAG_ENABLED(srcicon->flags, GATE_UI_ICON_FLAG_STOCK))
{
if (srcicon->resources[0])
{
dsticon->resources[0] = g_object_ref((gpointer)srcicon->resources[0]);
}
}
return GATE_RESULT_OK;
}
gate_result_t gate_ui_icon_destroy(gate_ui_icon_t* icon)
{
if (icon->resources[0])
{
g_object_unref((gpointer)icon->resources[0]);
}
gate_mem_clear(icon, sizeof(gate_ui_icon_t));
return GATE_RESULT_OK;
}
/****************
* iconlist GTK *
****************/
gate_result_t gate_ui_iconlist_create(gate_ui_iconlist_t* ilst, gate_ui_host_t* host, gate_int32_t width, gate_int32_t height)
{
gate_result_t ret = GATE_RESULT_FAILED;
GATE_DEBUG_ASSERT(ilst != NULL);
GATE_DEBUG_ASSERT(host != NULL);
do
{
gate_arraylist_t arrlst = gate_arraylist_create(sizeof(gate_ui_icon_t), NULL, 8, NULL, NULL);
if (NULL == ilst->resources[0])
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gate_mem_clear(ilst, sizeof(gate_ui_iconlist_t));
ilst->resources[0] = (void*)arrlst.ptr;
ilst->host = host;
ilst->width = width;
ilst->height = height;
ilst->flags = 0;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_iconlist_destroy(gate_ui_iconlist_t* ilst)
{
gate_result_t ret = GATE_RESULT_OK;
GATE_DEBUG_ASSERT(ilst != NULL);
do
{
gate_ui_icon_t* ptr_icon;<--- The scope of the variable 'ptr_icon' can be reduced. [+]The scope of the variable 'ptr_icon' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t count;
gate_arraylist_t arr = { (struct gate_arraylist_impl*)ilst->resources[0] };
if (gate_arraylist_is_null(arr))
{
break;
}
count = gate_arraylist_length(arr);
if (count != 0)
{
ptr_icon = gate_arraylist_get(arr, 0);
while (count-- != 0)
{
gate_ui_icon_destroy(ptr_icon);
++ptr_icon;
}
}
gate_arraylist_release(&arr);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_size_t gate_ui_iconlist_count(gate_ui_iconlist_t* ilst)
{
gate_arraylist_t arr = { (struct gate_arraylist_impl*)ilst->resources[0] };
if (!gate_arraylist_is_null(arr))
{
return gate_arraylist_length(arr);
}
return 0;
}
gate_result_t gate_ui_iconlist_add_icon(gate_ui_iconlist_t* ilst, gate_ui_icon_t const* icon, gate_intptr_t* icon_key)
{
gate_result_t ret;
gate_ui_icon_t new_icon = GATE_INIT_EMPTY;
GATE_DEBUG_ASSERT(ilst != NULL);
GATE_DEBUG_ASSERT(icon != NULL);
ret = gate_ui_icon_copy(&new_icon, icon);
if (GATE_SUCCEEDED(ret))
{
gate_arraylist_t arr = { (struct gate_arraylist_impl*)ilst->resources[0] };
gate_size_t index = gate_arraylist_length(arr);
if (NULL == gate_arraylist_add(arr, &new_icon))
{
ret = GATE_RESULT_OUTOFMEMORY;
gate_ui_icon_destroy(&new_icon);
}
else
{
if (icon_key)
{
*icon_key = (gate_intptr_t)index;
}
}
}
return ret;
}
gate_result_t gate_ui_iconlist_add_image(gate_ui_iconlist_t* ilst, gate_rasterimage_t const* image, gate_intptr_t* icon_key)
{
gate_ui_icon_t new_icon = GATE_INIT_EMPTY;
gate_result_t ret;
ret = gate_ui_icon_create_image(&new_icon, ilst->host, image);
if (GATE_SUCCEEDED(ret))
{
gate_arraylist_t arr = { (struct gate_arraylist_impl*)ilst->resources[0] };
gate_size_t index = gate_arraylist_length(arr);
if (NULL == gate_arraylist_add(arr, &new_icon))
{
ret = GATE_RESULT_OUTOFMEMORY;
gate_ui_icon_destroy(&new_icon);
}
else
{
if (icon_key)
{
*icon_key = (gate_intptr_t)index;
}
}
}
return ret;
}
gate_result_t gate_ui_iconlist_get_icon(gate_ui_iconlist_t* ilst, gate_intptr_t icon_key, gate_ui_icon_t* icon)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_ui_icon_t new_icon = GATE_INIT_EMPTY;
gate_arraylist_t arr = GATE_INIT_EMPTY;
gate_ui_icon_t const* ptr_icon = NULL;
GATE_DEBUG_ASSERT(ilst != NULL);
arr.ptr = (struct gate_arraylist_impl*)ilst->resources[0];
GATE_DEBUG_ASSERT(gate_arraylist_is_null(arr) == false);
ptr_icon = (gate_ui_icon_t const*)gate_arraylist_get(arr, (gate_size_t)icon_key);
if (NULL == ptr_icon)
{
ret = GATE_RESULT_NOTAVAILABLE;
}
else
{
ret = gate_ui_icon_copy(icon, ptr_icon);
}
return ret;
}
void* gate_ui_iconlist_get_handle(gate_ui_iconlist_t* ilst, gate_intptr_t icon_key)
{
/* TODO */
return NULL;
}
/**************
* cursor GTK *
**************/
gate_result_t gate_ui_cursor_create_stock(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_enumint_t stock_id)
{
gate_result_t ret = GATE_RESULT_FAILED;
char const* cursor_name = NULL;
switch (stock_id)
{
case GATE_UI_CURSOR_STOCK_POINTER: cursor_name = "default"; break;
case GATE_UI_CURSOR_STOCK_BUSY: cursor_name = "wait"; break;
case GATE_UI_CURSOR_STOCK_STARTING: cursor_name = "progress"; break;
case GATE_UI_CURSOR_STOCK_HAND: cursor_name = "pointer"; break;
case GATE_UI_CURSOR_STOCK_TEXT: cursor_name = "text"; break;
case GATE_UI_CURSOR_STOCK_REJECTED: cursor_name = "not-allowed"; break;
case GATE_UI_CURSOR_STOCK_SIZE_ALL: cursor_name = "all-scroll"; break;
case GATE_UI_CURSOR_STOCK_SIZE_LEFTRIGHT: cursor_name = "ew-resize"; break;
case GATE_UI_CURSOR_STOCK_SIZE_UPDOWN: cursor_name = "ns-resize"; break;
case GATE_UI_CURSOR_STOCK_SIZE_LEFTUPRIGHTDOWN: cursor_name = "nwse-resize"; break;
case GATE_UI_CURSOR_STOCK_SIZE_RIGHTUPKEFTDOWN: cursor_name = "nesw-resize"; break;
}
if (cursor_name == NULL)
{
ret = GATE_RESULT_NOTSUPPORTED;
}
else
{
GdkDisplay* const display = gdk_display_get_default();
GdkCursor* const gdk_cursor = gdk_cursor_new_from_name(display, cursor_name);
if (NULL == gdk_cursor)
{
ret = GATE_RESULT_FAILED;
}
else
{
cursor->resources[0] = gdk_cursor;
cursor->resources[1] = display;
cursor->host = host;
cursor->flags = GATE_UI_CURSOR_FLAG_STOCK;
ret = GATE_RESULT_OK;
}
}
return ret;
}
gate_result_t gate_ui_cursor_create_native(gate_ui_cursor_t* cursor, gate_ui_host_t* host, void* handle, gate_enumint_t flags)
{
cursor->resources[0] = handle;
cursor->resources[1] = gdk_cursor_get_image((GdkCursor*)handle);
cursor->host = host;
cursor->flags = flags;
return GATE_RESULT_OK;
}
gate_result_t gate_ui_cursor_create_image(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_rasterimage_t const* image,
gate_uint16_t x, gate_uint16_t y)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_cursor_create_file(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_string_t const* filepath)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_cursor_copy(gate_ui_cursor_t* dstcursor, gate_ui_cursor_t const* srccursor)
{
GdkCursor* const gdk_src_cursor = (GdkCursor*)srccursor->resources[0];
if (gdk_src_cursor == NULL)
{
gate_mem_clear(dstcursor, sizeof(gate_ui_cursor_t));
}
else
{
gate_mem_copy(dstcursor, srccursor, sizeof(gate_ui_cursor_t));
g_object_ref(gdk_src_cursor);
}
return GATE_RESULT_OK;
}
gate_result_t gate_ui_cursor_destroy(gate_ui_cursor_t* cursor)
{
GdkCursor* const gdk_cursor = (GdkCursor*)cursor->resources[0];
if (gdk_cursor)
{
g_object_unref(gdk_cursor);
}
gate_mem_clear(cursor, sizeof(gate_ui_cursor_t));
return GATE_RESULT_OK;
}
#endif /* GATE_UI_GTK */
|