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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* nautilus-gdk-extensions.c: Graphics routines to augment what's in gdk.
Copyright (C) 1999, 2000 Eazel, Inc.
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: Darin Adler <darin@eazel.com>,
Pavel Cisler <pavel@eazel.com>,
Ramiro Estrugo <ramiro@eazel.com>
*/
#include <stdlib.h>
#include <config.h>
#include "nautilus-gdk-extensions.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>
#include "nautilus-glib-extensions.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-string.h"
#include "nautilus-font-factory.h"
#define GRADIENT_BAND_SIZE 4
/**
* nautilus_fill_rectangle:
* @drawable: Target to draw into.
* @gc: Graphics context (mainly for clip).
* @rectangle: Rectangle to fill.
*
* Fill the rectangle with the foreground color.
* Convenient when you have a GdkRectangle structure.
*/
void
nautilus_fill_rectangle (GdkDrawable *drawable,
GdkGC *gc,
const GdkRectangle *rectangle)
{
gdk_draw_rectangle (drawable, gc, TRUE,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
}
/**
* nautilus_fill_rectangle_with_color:
* @drawable: Target to draw into.
* @gc: Graphics context (mainly for clip).
* @rectangle: Rectangle to fill.
* @color: Color to fill with.
*
* Fill the rectangle with a color.
* Convenient when you have a GdkRectangle structure.
*/
void
nautilus_fill_rectangle_with_color (GdkDrawable *drawable,
GdkGC *gc,
const GdkRectangle *rectangle,
guint32 rgb)
{
GdkGCValues saved_values;
/* FIXME bugzilla.eazel.com 1287: Workaround for a bug in gdk_rgb. */
gdk_rgb_init ();
gdk_gc_get_values (gc, &saved_values);
gdk_rgb_gc_set_foreground (gc, rgb);
nautilus_fill_rectangle (drawable, gc, rectangle);
gdk_gc_set_foreground (gc, &saved_values.foreground);
}
/**
* nautilus_rectangle_contains:
* @rectangle: Rectangle possibly containing a point.
* @x: X coordinate of a point.
* @y: Y coordinate of a point.
*
* Retun TRUE if point is contained inside a rectangle
*/
gboolean
nautilus_rectangle_contains (const GdkRectangle *rectangle,
int x,
int y)
{
g_return_val_if_fail (rectangle != NULL, FALSE);
return rectangle->x <= x && rectangle->x + rectangle->width >= x
&& rectangle->y <= y && rectangle->y + rectangle->height >= y;
}
/**
* nautilus_rectangle_inset:
* @rectangle: Rectangle we are insetting.
* @x: Horizontal ammount to inset by.
* @y: Vertical ammount to inset by.
*
* Inset a rectangle by a given horizontal and vertical ammount.
* Pass in negative inset values to grow the rectangle, positive to
* shrink it.
*/
void
nautilus_rectangle_inset (GdkRectangle *rectangle,
int x,
int y)
{
g_return_if_fail (rectangle != NULL);
rectangle->x += x;
rectangle->width -= 2 * x;
rectangle->y += y;
rectangle->height -= 2 * y;
}
/**
* nautilus_interpolate_color:
* @ratio: Place on line between colors to interpolate.
* @start_color: Color for one end.
* @end_color: Color for the other end
* @interpolated_color: Result.
*
* Compute a color between @start_color and @end_color in color space.
* Currently, the color space used is RGB, but a future version could
* instead do the interpolation in the best color space for expressing
* human perception.
*/
guint32
nautilus_interpolate_color (gdouble ratio,
guint32 start_rgb,
guint32 end_rgb)
{
guchar red, green, blue;
g_return_val_if_fail (ratio >= 0.0, 0);
g_return_val_if_fail (ratio <= 1.0, 0);
red = ((start_rgb >> 16) & 0xFF) * (1.0 - ratio) + ((end_rgb >> 16) & 0xFF) * ratio;
green = ((start_rgb >> 8) & 0xFF) * (1.0 - ratio) + ((end_rgb >> 8) & 0xFF) * ratio;
blue = (start_rgb & 0xFF) * (1.0 - ratio) + (end_rgb & 0xFF) * ratio;
return (((red << 8) | green) << 8) | blue;
}
/**
* nautilus_gradient_new
* @start_color: Color for the top or left.
* @end_color: Color for the bottom or right.
* @is_horizontal: Direction of the gradient.
*
* Create a string that combines the start and end colors along
* with the direction of the gradient in a standard format.
*/
char *
nautilus_gradient_new (const char *start_color,
const char *end_color,
gboolean is_horizontal)
{
g_return_val_if_fail (is_horizontal == FALSE || is_horizontal == TRUE, NULL);
/* Handle the special case where the start and end colors are identical.
Handle the special case where the end color is an empty string.
*/
if (nautilus_strcmp(start_color, end_color) == 0 || end_color == NULL || end_color[0] == '\0') {
return g_strdup (start_color);
}
/* Handle the special case where the start color is an empty string. */
if (start_color == NULL || start_color[0] == '\0') {
return g_strdup (end_color);
}
/* Handle the general case. */
return g_strconcat (start_color, "-", end_color, is_horizontal ? ":h" : NULL, NULL);
}
/**
* nautilus_gradient_is_gradient
* @gradient_spec: A gradient spec. string.
*
* Return true if the spec. specifies a gradient instead of a solid color.
*/
gboolean
nautilus_gradient_is_gradient (const char *gradient_spec)
{
return nautilus_strchr (gradient_spec, '-') != NULL;
}
/**
* nautilus_gradient_is_horizontal
* @gradient_spec: A gradient spec. string.
*
* Return true if the spec. specifies a horizontal gradient.
*/
gboolean
nautilus_gradient_is_horizontal (const char *gradient_spec)
{
size_t length;
length = nautilus_strlen (gradient_spec);
return length >= 2 && gradient_spec[length - 2] == ':' && gradient_spec[length - 1] == 'h';
}
static char *
nautilus_gradient_strip_trailing_direction_if_any (const char *gradient_spec)
{
size_t length;
length = nautilus_strlen (gradient_spec);
if (length >= 2 && gradient_spec[length - 2] == ':'
&& (gradient_spec[length - 1] == 'v' || gradient_spec[length - 1] == 'h')) {
length -= 2;
}
return g_strndup (gradient_spec, length);
}
/* For parsing n-point gradients. Successive calls should pass the next_spec value
* set by the previous call as their first argument - to continue parsing where the
* previous call left off.
*/
char *
nautilus_gradient_parse_one_color_spec (const char *spec, int *percent, const char **next_spec)
{
char *result;
const char *rgb_end_ptr;
const char *percent_ptr;
const char *separator_ptr;
percent_ptr = nautilus_strchr (spec, '%');
separator_ptr = nautilus_strchr (spec, '-');
if (percent_ptr != NULL && (separator_ptr == NULL || percent_ptr < separator_ptr)) {
if (percent != NULL) {
*percent = (int) strtol (percent_ptr + 1, NULL, 10);
}
rgb_end_ptr = percent_ptr;
} else {
if (percent != NULL) {
*percent = 100;
}
rgb_end_ptr = separator_ptr;
}
if (rgb_end_ptr != NULL) {
result = g_strndup (spec, rgb_end_ptr - spec);
} else {
result = nautilus_gradient_strip_trailing_direction_if_any (spec);
}
/* It's important not to use spec after setting *next_spec because it's
* likely that *next_spec == spec.
*/
if (next_spec != NULL) {
*next_spec = (separator_ptr != NULL) ? separator_ptr + 1 : NULL;
}
return result;
}
/* FIXME bugzilla.eazel.com 5076:
* anyone using nautilus_gradient_get_start_color_spec or
* nautilus_gradient_get_end_color_spec is assuming the gradient
* is 2 colors which is questionable.
*
* Callers should be rewritten and these fns eliminated.
*/
/**
* nautilus_gradient_get_start_color_spec
* @gradient_spec: A gradient spec. string.
*
* Return the start color.
* This may be the entire gradient_spec if it's a solid color.
*/
char *
nautilus_gradient_get_start_color_spec (const char *gradient_spec)
{
return nautilus_gradient_parse_one_color_spec (gradient_spec, NULL, NULL);
}
/**
* nautilus_gradient_get_end_color_spec
* @gradient_spec: A gradient spec. string.
*
* Return the end color.
* This may be the entire gradient_spec if it's a solid color.
*/
char *
nautilus_gradient_get_end_color_spec (const char *gradient_spec)
{
char* color = NULL;
do {
g_free (color);
color = nautilus_gradient_parse_one_color_spec (gradient_spec, NULL, &gradient_spec);
} while (gradient_spec != NULL);
return color;
}
/* Do the work shared by all the set_color_spec functions below. */
static char *
nautilus_gradient_set_edge_color (const char *gradient_spec,
const char *edge_color,
gboolean is_horizontal,
gboolean change_end)
{
char *opposite_color;
char *result;
g_return_val_if_fail (edge_color != NULL, g_strdup (gradient_spec));
/* Get the color from the existing gradient spec. for the opposite
edge. This will parse away all the stuff we don't want from the
old gradient spec.
*/
opposite_color = change_end
? nautilus_gradient_get_start_color_spec (gradient_spec)
: nautilus_gradient_get_end_color_spec (gradient_spec);
/* Create a new gradient spec. The nautilus_gradient_new function handles
some special cases, so we don't have to bother with them here.
*/
result = nautilus_gradient_new (change_end ? opposite_color : edge_color,
change_end ? edge_color : opposite_color,
is_horizontal);
g_free (opposite_color);
return result;
}
/**
* nautilus_gradient_set_left_color_spec
* @gradient_spec: A gradient spec. string.
* @left_color: Color spec. to replace left color with.
*
* Changes the left color to what's passed in.
* This creates a horizontal gradient.
*/
char *
nautilus_gradient_set_left_color_spec (const char *gradient_spec,
const char *left_color)
{
return nautilus_gradient_set_edge_color (gradient_spec, left_color, TRUE, FALSE);
}
/**
* nautilus_gradient_set_top_color_spec
* @gradient_spec: A gradient spec. string.
* @top_color: Color spec. to replace top color with.
*
* Changes the top color to what's passed in.
* This creates a vertical gradient.
*/
char *
nautilus_gradient_set_top_color_spec (const char *gradient_spec,
const char *top_color)
{
return nautilus_gradient_set_edge_color (gradient_spec, top_color, FALSE, FALSE);
}
/**
* nautilus_gradient_set_right_color_spec
* @gradient_spec: A gradient spec. string.
* @right_color: Color spec. to replace right color with.
*
* Changes the right color to what's passed in.
* This creates a horizontal gradient.
*/
char *
nautilus_gradient_set_right_color_spec (const char *gradient_spec,
const char *right_color)
{
return nautilus_gradient_set_edge_color (gradient_spec, right_color, TRUE, TRUE);
}
/**
* nautilus_gradient_set_bottom_color_spec
* @gradient_spec: A gradient spec. string.
* @bottom_color: Color spec. to replace bottom color with.
*
* Changes the bottom color to what's passed in.
* This creates a vertical gradient.
*/
char *
nautilus_gradient_set_bottom_color_spec (const char *gradient_spec,
const char *bottom_color)
{
return nautilus_gradient_set_edge_color (gradient_spec, bottom_color, FALSE, TRUE);
}
/**
* nautilus_gdk_color_parse_with_white_default
* @color_spec: A color spec.
* @color: Pointer to place to put resulting color.
*
* The same as gdk_color_parse, except sets the color to white if
* the spec. can't be parsed instead of returning a boolean flag.
*/
void
nautilus_gdk_color_parse_with_white_default (const char *color_spec,
GdkColor *color)
{
if (color_spec == NULL || !gdk_color_parse (color_spec, color)) {
color->red = 0xFFFF;
color->green = 0xFFFF;
color->blue = 0xFFFF;
}
}
/**
* nautilus_parse_rgb_with_white_default
* @color_spec: A color spec.
* Returns: An rgb value.
*
* The same as gdk_color_parse, except sets the color to white if
* the spec. can't be parsed instead of returning a boolean flag
* and returns a guint32 rgb value instead of a GdkColor.
*/
guint32
nautilus_parse_rgb_with_white_default (const char *color_spec)
{
GdkColor color;
if (color_spec == NULL || !gdk_color_parse (color_spec, &color)) {
return NAUTILUS_RGB_COLOR_WHITE;
}
return ((color.red << 8) & NAUTILUS_RGB_COLOR_RED)
| (color.green & NAUTILUS_RGB_COLOR_GREEN)
| ((color.blue >> 8) & NAUTILUS_RGB_COLOR_BLUE);
}
guint32
nautilus_rgb16_to_rgb (gushort r, gushort g, gushort b)
{
guint32 result;
result = (0xff0000 | (r & 0xff00));
result <<= 8;
result |= ((g & 0xff00) | (b >> 8));
return result;
}
guint32
nautilus_rgb8_to_rgb (guchar r, guchar g, guchar b)
{
return nautilus_rgb16_to_rgb (r << 8, g << 8, b << 8);
}
/**
* nautilus_gdk_color_to_rgb
* @color: A GdkColor style color.
* Returns: An rgb value.
*
* Converts from a GdkColor stlye color to a gdk_rgb one.
* Alpha gets set to fully opaque
*/
guint32
nautilus_gdk_color_to_rgb (const GdkColor *color)
{
return nautilus_rgb16_to_rgb (color->red, color->green, color->blue);
}
/**
* nautilus_gdk_rgb_to_color
* @color: a gdk_rgb style value.
*
* Converts from a gdk_rgb value style to a GdkColor one.
* The gdk_rgb color alpha channel is ignored.
*
* Return value: a newly allocated GdkColor.
*/
GdkColor *
nautilus_gdk_rgb_to_color (const guint32 color)
{
GdkColor *result;
result = g_new0 (GdkColor, 1);
result->red = (color & 0xff0000) >> 16 ;
result->green = (color & 0xff00);
result->blue = color << 8;
return result;
}
static guint32
nautilus_shift_color_component (guchar component, float shift_by)
{
guint32 result;
if (shift_by > 1.0) {
result = component * (2 - shift_by);
} else {
result = 0xff - shift_by * (0xff - component);
}
return result & 0xff;
}
/**
* nautilus_rgb_shift_color
* @color: A color.
* @shift_by: darken or lighten factor.
* Returns: An darkened or lightened rgb value.
*
* Darkens (@shift_by > 1) or lightens (@shift_by < 1)
* @color.
*/
guint32
nautilus_rgb_shift_color (guint32 color, float shift_by)
{
guint32 result;
/* shift red by shift_by */
result = nautilus_shift_color_component((color & 0x00ff0000) >> 16, shift_by);
result <<= 8;
/* shift green by shift_by */
result |= nautilus_shift_color_component((color & 0x0000ff00) >> 8, shift_by);
result <<= 8;
/* shift blue by shift_by */
result |= nautilus_shift_color_component((color & 0x000000ff), shift_by);
/* alpha doesn't change */
result |= (0xff000000 & color);
return result;
}
/**
* nautilus_gdk_font_equal
* @font_a_null_allowed: A font or NULL.
* @font_b_null_allowed: A font or NULL.
*
* Calls gdk_font_equal, unless one of the fonts is NULL.
*/
gboolean
nautilus_gdk_font_equal (GdkFont *font_a_null_allowed,
GdkFont *font_b_null_allowed)
{
if (font_a_null_allowed == NULL) {
return font_b_null_allowed == NULL;
}
if (font_b_null_allowed == NULL) {
return FALSE;
}
return gdk_font_equal (font_a_null_allowed, font_b_null_allowed);
}
GdkFont *
nautilus_get_largest_fitting_font (const char *text_to_format, int width, const char *font_template)
{
guint font_index;
int this_width;
char *font_name;
const int font_sizes[] = { 20, 18, 14, 12 };
GdkFont *candidate_font;
char *alt_text_to_format = NULL;
char *temp_str;
char *cr_pos;
temp_str = g_strdup (text_to_format == NULL ? "" : text_to_format);
cr_pos = strchr (temp_str, '\n');
if (cr_pos != NULL) {
*cr_pos = '\0';
alt_text_to_format = cr_pos + 1;
}
candidate_font = NULL;
for (font_index = 0; font_index < NAUTILUS_N_ELEMENTS (font_sizes); font_index++) {
if (candidate_font != NULL) {
gdk_font_unref (candidate_font);
}
font_name = g_strdup_printf (font_template, font_sizes[font_index]);
candidate_font = gdk_fontset_load (font_name);
g_free (font_name);
if (candidate_font != NULL) {
this_width = gdk_string_width (candidate_font, temp_str);
if (alt_text_to_format != NULL) {
int alt_width = gdk_string_width (candidate_font, alt_text_to_format);
if (this_width <= width && alt_width <= width) {
break;
}
} else {
if (this_width <= width) {
break;
}
}
}
}
g_free (temp_str);
return candidate_font;
}
/**
* nautilus_stipple_bitmap:
*
* Get pointer to singleton 50% stippled bitmap.
* This is a global object; do not free.
*/
GdkBitmap *
nautilus_stipple_bitmap ()
{
static GdkBitmap *stipple = NULL;
if (stipple == NULL) {
char stipple_bits[] = { 0x02, 0x01 };
stipple = gdk_bitmap_create_from_data (NULL, stipple_bits, 2, 2);
}
return stipple;
}
/**
* nautilus_gdk_window_bring_to_front:
*
* Raise window and give it focus.
*/
void
nautilus_gdk_window_bring_to_front (GdkWindow *window)
{
gdk_window_raise (window);
gdk_error_trap_push ();
XSetInputFocus (GDK_DISPLAY (),
GDK_WINDOW_XWINDOW (window),
RevertToPointerRoot,
GDK_CURRENT_TIME);
gdk_flush();
gdk_error_trap_pop ();
}
void
nautilus_set_mini_icon (GdkWindow *window,
GdkPixmap *pixmap,
GdkBitmap *mask)
{
GdkAtom icon_atom;
long data[2];
g_return_if_fail (window != NULL);
g_return_if_fail (pixmap != NULL);
data[0] = GDK_WINDOW_XWINDOW (pixmap);
if (mask) {
data[1] = GDK_WINDOW_XWINDOW (mask);
} else {
data[1] = 0;
}
icon_atom = gdk_atom_intern ("KWM_WIN_ICON", FALSE);
gdk_property_change (window, icon_atom, icon_atom,
32, GDK_PROP_MODE_REPLACE,
(guchar *) data, 2);
}
/**
* nautilus_gdk_font_get_bold
* @plain_font: A font.
* Returns: A bold variant of @plain_font or NULL.
*
* Tries to find a bold flavor of a given font. Returns NULL if none is available.
*/
GdkFont *
nautilus_gdk_font_get_bold (const GdkFont *plain_font)
{
const char *plain_name;
const char *scanner;
char *bold_name;
int count;
GSList *p;
GdkFont *result;
GdkFontPrivate *private_plain;
private_plain = (GdkFontPrivate *)plain_font;
if (private_plain->names == NULL) {
return NULL;
}
/* -foundry-family-weight-slant-sel_width-add-style-pixels-points-hor_res-ver_res-spacing-average_width-char_set_registry-char_set_encoding */
bold_name = NULL;
for (p = private_plain->names; p != NULL; p = p->next) {
plain_name = (const char *)p->data;
scanner = plain_name;
/* skip past foundry and family to weight */
for (count = 2; count > 0; count--) {
scanner = strchr (scanner + 1, '-');
if (!scanner) {
break;
}
}
if (!scanner) {
/* try the other names in the list */
continue;
}
g_assert (*scanner == '-');
/* copy "-foundry-family-" over */
scanner++;
bold_name = g_strndup (plain_name, scanner - plain_name);
/* skip weight */
scanner = strchr (scanner, '-');
g_assert (scanner != NULL);
/* FIXME bugzilla.eazel.com 2558:
* some fonts have demibold, etc. instead. We should be able to figure out
* which they are and use them here.
*/
/* add "bold" and copy everything past weight over */
bold_name = g_strconcat (bold_name, "bold", scanner, NULL);
break;
}
if (bold_name == NULL) {
return NULL;
}
result = gdk_fontset_load (bold_name);
g_free (bold_name);
return result;
}
GdkGC *
nautilus_gdk_create_copy_area_gc (GdkWindow *window)
{
GdkGC *copy_area_gc;
g_return_val_if_fail (window != NULL, NULL);
copy_area_gc = gdk_gc_new (window);
gdk_gc_set_function (copy_area_gc, GDK_COPY);
return copy_area_gc;
}
void
nautilus_gdk_gc_ref_if_not_null (GdkGC *gc_or_null)
{
if (gc_or_null != NULL) {
gdk_gc_ref (gc_or_null);
}
}
void
nautilus_gdk_gc_unref_if_not_null (GdkGC *gc_or_null)
{
if (gc_or_null != NULL) {
gdk_gc_unref (gc_or_null);
}
}
/**
* nautilus_string_ellipsize_start:
*
* @string: A a string to be ellipsized.
* @font: A a font used to measure the resulting string width.
* @width: Desired maximum width in pixels.
* Returns: A truncated string at most @width pixels long.
*
* Truncates a string, removing characters from the start and
* replacing them with "..."
*
*/
char *
nautilus_string_ellipsize_start (const char *string, GdkFont *font, int width)
{
int truncate_offset;
if (gdk_string_width (font, string) <= (int) width) {
/* String is already short enough. */
return g_strdup (string);
}
/* Account for the width of the ellipsis. */
width -= gdk_string_width (font, "...");
if (width < 0) {
/* No room even for a an ellipsis. */
return g_strdup ("");
}
/* We could have the following optimization here:
* check if the desired width and original width are considerably different,
* if so, use a binary stride to figure out the resulting string truncation
* offset.
* For now we assume that we are only truncating by a small number of
* characters, in which a linear scan is faster
*/
for (truncate_offset = 0; ; truncate_offset++) {
if (string[truncate_offset] == '\0') {
break;
}
if (gdk_string_width (font, string + truncate_offset) <= (int) width) {
break;
}
}
return g_strdup_printf ("...%s", string + truncate_offset);
}
/**
* nautilus_gdk_window_set_wm_hints_input:
*
* Set the WM_HINTS.input flag to the passed in value
*/
void
nautilus_gdk_window_set_wm_hints_input (GdkWindow *window, gboolean status)
{
Display *dpy;
Window id;
XWMHints *wm_hints;
g_return_if_fail (window != NULL);
dpy = GDK_WINDOW_XDISPLAY (window);
id = GDK_WINDOW_XWINDOW (window);
wm_hints = XGetWMHints (dpy, id);
if (wm_hints == 0) {
wm_hints = XAllocWMHints ();
}
wm_hints->flags |= InputHint;
wm_hints->input = status;
XSetWMHints (dpy, id, wm_hints);
XFree (wm_hints);
}
void
nautilus_gdk_window_set_invisible_cursor (GdkWindow *window)
{
XColor foreColor, backColor;
GdkWindowPrivate *window_private;
Pixmap sourcePixmap, maskPixmap;
Cursor xcursor;
char invisible_cursor_bits[] = {0x0};
char invisible_cursor_mask_bits[] = {0x0};
foreColor.pixel = 0L;
foreColor.red = foreColor.green = foreColor.blue = 0;
foreColor.flags = DoRed | DoGreen | DoBlue;
backColor.pixel = 255L;
backColor.red = backColor.green = backColor.blue = 65535;
backColor.flags = DoRed | DoGreen | DoBlue;
window_private = (GdkWindowPrivate *) window;
sourcePixmap = XCreateBitmapFromData (window_private->xdisplay, window_private->xwindow,
invisible_cursor_bits, 1, 1);
g_assert (sourcePixmap != 0);
maskPixmap = XCreateBitmapFromData (window_private->xdisplay, window_private->xwindow,
invisible_cursor_mask_bits, 1, 1);
g_assert (maskPixmap != 0);
xcursor = XCreatePixmapCursor (window_private->xdisplay, sourcePixmap, maskPixmap,
&foreColor, &backColor, 0, 0);
XFreePixmap (window_private->xdisplay, sourcePixmap);
XFreePixmap (window_private->xdisplay, maskPixmap);
XDefineCursor (window_private->xdisplay, window_private->xwindow, xcursor);
}
#if ! defined (NAUTILUS_OMIT_SELF_CHECK)
static char *
nautilus_gdk_color_as_hex_string (GdkColor color)
{
return g_strdup_printf("rgb:%04hX/%04hX/%04hX",
color.red, color.green, color.blue);
}
static char *
nautilus_self_check_parse (const char *color_spec)
{
GdkColor color;
nautilus_gdk_color_parse_with_white_default (color_spec, &color);
return nautilus_gdk_color_as_hex_string (color);
}
/* Testing string truncation is tough because we do not know what font/
* font metrics to expect on a given system. To work around this we use
* a substring of the original, measure it's length using the given font,
* add the length of the "..." string and use that for truncation.
* The result should then be the substring prepended with a "..."
*/
static char *
nautilus_self_check_ellipsize_start (const char *string, const char *truncate_to_length_string)
{
GdkFont *font;
int truncation_length;
char *result;
/* any old font will do */
font = nautilus_font_factory_get_fallback_font ();
g_assert (font);
/* measure the length we want to truncate to */
truncation_length = gdk_string_width (font, truncate_to_length_string);
truncation_length += gdk_string_width (font, "...");
result = nautilus_string_ellipsize_start (string, font, truncation_length);
gdk_font_unref (font);
return result;
}
void
nautilus_self_check_gdk_extensions (void)
{
GdkFont *font;
/* used to test ellipsize routines */
font = nautilus_font_factory_get_fallback_font ();
g_assert (font);
/* nautilus_interpolate_color */
NAUTILUS_CHECK_INTEGER_RESULT (nautilus_interpolate_color (0.0, 0, 0), 0);
NAUTILUS_CHECK_INTEGER_RESULT (nautilus_interpolate_color (0.0, 0, 0xFFFFFF), 0);
NAUTILUS_CHECK_INTEGER_RESULT (nautilus_interpolate_color (0.5, 0, 0xFFFFFF), 0x7F7F7F);
NAUTILUS_CHECK_INTEGER_RESULT (nautilus_interpolate_color (1.0, 0, 0xFFFFFF), 0xFFFFFF);
/* nautilus_fill_rectangle */
/* Make a GdkImage and fill it, maybe? */
/* nautilus_fill_rectangle_with_color */
/* nautilus_fill_rectangle_with_gradient */
/* nautilus_gradient_new */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_new ("", "", FALSE), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_new ("a", "b", FALSE), "a-b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_new ("a", "b", TRUE), "a-b:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_new ("a", "a", FALSE), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_new ("a", "a", TRUE), "a");
/* nautilus_gradient_is_gradient */
NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_gradient_is_gradient (""), FALSE);
NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_gradient_is_gradient ("-"), TRUE);
NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_gradient_is_gradient ("a"), FALSE);
NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_gradient_is_gradient ("a-b"), TRUE);
NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_gradient_is_gradient ("a-b:h"), TRUE);
/* nautilus_gradient_get_start_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec (""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("-"), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a-b"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a-"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("-b"), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a:h"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a:v"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a:c"), "a:c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a:-b"), "a:");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_start_color_spec ("a:-b:v"), "a:");
/* nautilus_gradient_get_end_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec (""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("-"), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a-b"), "b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a-"), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("-b"), "b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a:h"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a:v"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a:c"), "a:c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a:-b"), "b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_get_end_color_spec ("a:-b:v"), "b");
/* nautilus_gradient_set_left_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("", ""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a", ""), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a", "b"), "b-a:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a-c:v", "b"), "b-c:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a-c:v", "c"), "c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_left_color_spec ("a:-b:v", "d"), "d-b:h");
/* nautilus_gradient_set_top_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("", ""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a", ""), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a", "b"), "b-a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a-c:v", "b"), "b-c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a-c:v", "c"), "c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_top_color_spec ("a:-b:h", "d"), "d-b");
/* nautilus_gradient_set_right_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("", ""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a", ""), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a", "b"), "a-b:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a-c:v", "b"), "a-b:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a-c:v", "c"), "a-c:h");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_right_color_spec ("a:-b:v", "d"), "a:-d:h");
/* nautilus_gradient_set_bottom_color_spec */
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("", ""), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a", ""), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a", "a"), "a");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a", "b"), "a-b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a-c:v", "b"), "a-b");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a-c:v", "c"), "a-c");
NAUTILUS_CHECK_STRING_RESULT (nautilus_gradient_set_bottom_color_spec ("a:-b:h", "d"), "a:-d");
/* nautilus_gdk_color_parse_with_white_default */
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_parse (""), "rgb:FFFF/FFFF/FFFF");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_parse ("a"), "rgb:FFFF/FFFF/FFFF");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_parse ("white"), "rgb:FFFF/FFFF/FFFF");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_parse ("black"), "rgb:0000/0000/0000");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_parse ("rgb:0123/4567/89AB"), "rgb:0123/4567/89AB");
/* nautilus_string_ellipsize_start */
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "0012345678"), "012345678");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "012345678"), "012345678");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "45678"), "...45678");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "5678"), "...5678");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "678"), "...678");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "78"), "...78");
NAUTILUS_CHECK_STRING_RESULT (nautilus_self_check_ellipsize_start ("012345678", "8"), "...8");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_ellipsize_start ("", font, 100), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_ellipsize_start ("test", font, 0), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_ellipsize_start ("test", font, gdk_string_width (font, "...") - 1), "");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_ellipsize_start ("test", font, gdk_string_width (font, "...")), "...");
gdk_font_unref (font);
}
#endif /* ! NAUTILUS_OMIT_SELF_CHECK */
|