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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
|
/* -*- mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* Nautilus
* Copyright (C) 2000 Eazel, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Andy Hertzfeld <andy@eazel.com>
*
* This is the tabs widget for the sidebar, which represents closed panels as folder tabs
*/
#include <config.h>
#include "nautilus-sidebar-tabs.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>
#include <libnautilus-extensions/nautilus-gdk-extensions.h>
#include <libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h>
#include <libnautilus-extensions/nautilus-glib-extensions.h>
#include <libnautilus-extensions/nautilus-global-preferences.h>
#include <libnautilus-extensions/nautilus-gnome-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <libnautilus-extensions/nautilus-scalable-font.h>
#include <libnautilus-extensions/nautilus-theme.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
/* constants for the tab piece pixbuf array */
#define TAB_NORMAL_LEFT 0
#define TAB_NORMAL_FILL 1
#define TAB_NORMAL_NEXT 2
#define TAB_NORMAL_RIGHT 3
#define TAB_NORMAL_EDGE 4
#define TAB_PRELIGHT_LEFT 5
#define TAB_PRELIGHT_FILL 6
#define TAB_PRELIGHT_NEXT 7
#define TAB_PRELIGHT_NEXT_ALT 8
#define TAB_PRELIGHT_RIGHT 9
#define TAB_PRELIGHT_EDGE 10
#define TAB_ACTIVE_LEFT 11
#define TAB_ACTIVE_FILL 12
#define TAB_ACTIVE_RIGHT 13
#define TAB_BACKGROUND 14
#define TAB_BACKGROUND_RIGHT 15
#define TAB_ACTIVE_PRELIGHT_LEFT 16
#define TAB_ACTIVE_PRELIGHT_FILL 17
#define TAB_ACTIVE_PRELIGHT_RIGHT 18
#define LAST_TAB_OFFSET 19
/* data structures */
typedef struct {
gboolean visible;
gboolean prelit;
char *tab_text;
int notebook_page;
GtkWidget *tab_view;
GdkRectangle tab_rect;
} TabItem;
struct NautilusSidebarTabsDetails {
int tab_count;
int total_height;
gboolean title_mode;
GdkRectangle title_rect;
NautilusScalableFont *tab_font;
int font_size;
GdkColor tab_color;
GdkColor background_color;
GdkColor line_color;
GdkColor hilight_color;
GdkColor prelight_color;
GdkColor text_color;
GdkColor prelit_text_color;
GdkPixbuf *tab_piece_images[LAST_TAB_OFFSET];
int tab_height;
int tab_left_offset;
char *title;
gboolean title_prelit;
GList *tab_items;
};
/* constants */
#define TAB_MARGIN 8
#define TITLE_TAB_OFFSET 8
#define NOMINAL_TAB_HEIGHT 18
#define TAB_H_GAP 8
#define TAB_TOP_GAP 3
#define TAB_ROW_V_OFFSET 3
#define TAB_DEFAULT_LEFT_OFFSET 4
#define THEMED_TAB_TEXT_V_OFFSET 7
/* headers */
static void nautilus_sidebar_tabs_initialize_class (NautilusSidebarTabsClass *klass);
static void nautilus_sidebar_tabs_initialize (NautilusSidebarTabs *pixmap);
static int nautilus_sidebar_tabs_expose (GtkWidget *widget,
GdkEventExpose *event);
static void nautilus_sidebar_tabs_destroy (GtkObject *object);
static void nautilus_sidebar_tabs_size_allocate (GtkWidget *widget,
GtkAllocation *allocatoin);
static void nautilus_sidebar_tabs_load_tab_pieces (NautilusSidebarTabs *sidebar_tabs, const char *tab_piece_directory);
static void nautilus_sidebar_tabs_unload_tab_pieces (NautilusSidebarTabs *sidebar_tabs);
static void draw_or_layout_all_tabs (NautilusSidebarTabs *sidebar_tabs,
gboolean layout_only);
static TabItem* tab_item_find_by_name (NautilusSidebarTabs *sidebar_tabs,
const char *name);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusSidebarTabs, nautilus_sidebar_tabs, GTK_TYPE_WIDGET)
static void
nautilus_sidebar_tabs_initialize_class (NautilusSidebarTabsClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
object_class->destroy = nautilus_sidebar_tabs_destroy;
widget_class->expose_event = nautilus_sidebar_tabs_expose;
widget_class->size_allocate = nautilus_sidebar_tabs_size_allocate;
}
/* utilities to set up the text color alternatives */
static void
setup_light_text(NautilusSidebarTabs *sidebar_tabs)
{
gdk_color_parse ("rgb:ff/ff/ff", &sidebar_tabs->details->text_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->text_color, FALSE, TRUE);
}
static void
setup_dark_text(NautilusSidebarTabs *sidebar_tabs)
{
gdk_color_parse ("rgb:00/00/00", &sidebar_tabs->details->text_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->text_color, FALSE, TRUE);
}
/* load the relevant data from the current theme, which is mainly the tab piece images */
static void
nautilus_sidebar_tabs_load_theme_data (NautilusSidebarTabs *sidebar_tabs)
{
char *temp_str;
char *tab_pieces, *tab_piece_path, *tab_piece_theme;
GdkColor color;
int intensity;
/* set up the default values */
sidebar_tabs->details->tab_left_offset = TAB_DEFAULT_LEFT_OFFSET;
sidebar_tabs->details->tab_height = NOMINAL_TAB_HEIGHT;
/* unload the old theme image if necessary */
if (sidebar_tabs->details->tab_piece_images[0] != NULL) {
nautilus_sidebar_tabs_unload_tab_pieces (sidebar_tabs);
}
/* load the tab_pieces image if necessary */
tab_pieces = nautilus_theme_get_theme_data ("sidebar", "TAB_PIECE_IMAGES");
tab_piece_theme = nautilus_theme_get_theme_data ("sidebar", "TAB_PIECE_THEME");
if (tab_pieces) {
if (tab_piece_theme) {
tab_piece_path = nautilus_theme_get_image_path_from_theme (tab_pieces, tab_piece_theme);
} else {
tab_piece_path = nautilus_theme_get_image_path (tab_pieces);
}
g_free (tab_pieces);
g_free (tab_piece_theme);
if (tab_piece_path) {
nautilus_sidebar_tabs_load_tab_pieces (sidebar_tabs, tab_piece_path);
g_free (tab_piece_path);
if (sidebar_tabs->details->tab_piece_images[0]) {
/* load the left offset */
temp_str = nautilus_theme_get_theme_data ("sidebar", "LEFT_OFFSET");
if (temp_str) {
sidebar_tabs->details->tab_left_offset = atoi(temp_str);
g_free (temp_str);
}
sidebar_tabs->details->tab_height = gdk_pixbuf_get_height (sidebar_tabs->details->tab_piece_images[0]);
/* set the text color according to the pixbuf */
nautilus_gdk_pixbuf_average_value (sidebar_tabs->details->tab_piece_images[TAB_NORMAL_FILL], &color);
intensity = (((color.red >> 8) * 77) + ((color.green >> 8) * 150) + ((color.blue >> 8) * 28)) >> 8;
if (intensity < 160) {
setup_light_text (sidebar_tabs);
} else {
setup_dark_text (sidebar_tabs);
}
}
}
}
/* unload the old font if necessary */
if (sidebar_tabs->details->tab_font) {
gtk_object_unref (GTK_OBJECT (sidebar_tabs->details->tab_font));
}
/* use the default font. In the future, it should fetch the font name and properties from the theme */
sidebar_tabs->details->tab_font = NAUTILUS_SCALABLE_FONT (nautilus_scalable_font_new ("helvetica", "bold", NULL, NULL));
sidebar_tabs->details->font_size = 12;
}
/* initialize a newly allocated sidebar tabs object */
static void
nautilus_sidebar_tabs_initialize (NautilusSidebarTabs *sidebar_tabs)
{
GTK_WIDGET_SET_FLAGS (GTK_WIDGET(sidebar_tabs), GTK_NO_WINDOW);
sidebar_tabs->details = g_new0 (NautilusSidebarTabsDetails, 1);
/* set up the default colors used for the structured (non-themed) tabs */
gdk_color_parse ("rgb:99/99/99", &sidebar_tabs->details->tab_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->tab_color, FALSE, TRUE);
gdk_color_parse ("rgb:ee/ee/ee", &sidebar_tabs->details->prelight_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->prelight_color, FALSE, TRUE);
gdk_color_parse ("rgb:ff/ff/ff", &sidebar_tabs->details->background_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->background_color, FALSE, TRUE);
gdk_color_parse ("rgb:00/00/00", &sidebar_tabs->details->line_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->line_color, FALSE, TRUE);
gdk_color_parse ("rgb:d6/d6/d6", &sidebar_tabs->details->hilight_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->hilight_color, FALSE, TRUE);
setup_light_text(sidebar_tabs);
nautilus_sidebar_tabs_load_theme_data (sidebar_tabs);
/* add callback to be notified for theme changes */
nautilus_preferences_add_callback(NAUTILUS_PREFERENCES_THEME,
(NautilusPreferencesCallback) nautilus_sidebar_tabs_load_theme_data,
sidebar_tabs);
sidebar_tabs->details->title_prelit = FALSE;
}
GtkWidget *
nautilus_sidebar_tabs_new (void)
{
return gtk_widget_new (nautilus_sidebar_tabs_get_type (), NULL);
}
/* utility to destroy all the storage used by a tab item */
static void
tab_item_destroy (TabItem *item)
{
g_free (item->tab_text);
g_free (item);
}
static void
tab_item_destroy_cover (gpointer item, gpointer callback_data)
{
g_assert (callback_data == NULL);
tab_item_destroy (item);
}
static void
nautilus_sidebar_tabs_destroy (GtkObject *object)
{
NautilusSidebarTabs *sidebar_tabs = NAUTILUS_SIDEBAR_TABS(object);
/* deallocate the tab piece images, if any */
if (sidebar_tabs->details->tab_piece_images[0] != NULL) {
nautilus_sidebar_tabs_unload_tab_pieces (sidebar_tabs);
}
if (sidebar_tabs->details->tab_font != NULL) {
gtk_object_unref (GTK_OBJECT (sidebar_tabs->details->tab_font));
}
/* release the tab list, if any */
nautilus_g_list_free_deep_custom (sidebar_tabs->details->tab_items,
tab_item_destroy_cover,
NULL);
nautilus_preferences_remove_callback (NAUTILUS_PREFERENCES_THEME,
(NautilusPreferencesCallback) nautilus_sidebar_tabs_load_theme_data,
sidebar_tabs);
g_free (sidebar_tabs->details);
NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}
/* unload the tab piece images, if any */
static void
nautilus_sidebar_tabs_unload_tab_pieces (NautilusSidebarTabs *sidebar_tabs)
{
int index;
for (index = 0; index < LAST_TAB_OFFSET; index++) {
if (sidebar_tabs->details->tab_piece_images[index]) {
gdk_pixbuf_unref (sidebar_tabs->details->tab_piece_images[index]);
sidebar_tabs->details->tab_piece_images[index] = NULL;
}
}
}
/* load the tab piece images */
static GdkPixbuf *
load_tab_piece (const char *piece_directory, const char *piece_name)
{
GdkPixbuf *pixbuf;
gchar *image_path;
image_path = g_strdup_printf ("%s/%s.png", piece_directory, piece_name);
pixbuf = gdk_pixbuf_new_from_file (image_path);
if (pixbuf == NULL) {
g_warning ("cant load tab piece: %s", image_path);
}
g_free (image_path);
return pixbuf;
}
static void
nautilus_sidebar_tabs_load_tab_pieces (NautilusSidebarTabs *sidebar_tabs, const char* tab_piece_directory)
{
sidebar_tabs->details->tab_piece_images[TAB_NORMAL_LEFT] = load_tab_piece (tab_piece_directory, "left-bumper");
sidebar_tabs->details->tab_piece_images[TAB_NORMAL_FILL] = load_tab_piece (tab_piece_directory, "fill");
sidebar_tabs->details->tab_piece_images[TAB_NORMAL_NEXT] = load_tab_piece (tab_piece_directory, "middle-normal-normal");
sidebar_tabs->details->tab_piece_images[TAB_NORMAL_RIGHT] = load_tab_piece (tab_piece_directory, "right-bumper");
sidebar_tabs->details->tab_piece_images[TAB_NORMAL_EDGE] = load_tab_piece (tab_piece_directory, "right-top");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_LEFT] = load_tab_piece (tab_piece_directory, "left-bumper-prelight");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_FILL] = load_tab_piece (tab_piece_directory, "fill-prelight");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_NEXT] = load_tab_piece (tab_piece_directory, "middle-prelight-normal");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_NEXT_ALT] = load_tab_piece (tab_piece_directory, "middle-normal-prelight");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_RIGHT] = load_tab_piece (tab_piece_directory, "right-bumper-prelight");
sidebar_tabs->details->tab_piece_images[TAB_PRELIGHT_EDGE] = load_tab_piece (tab_piece_directory, "right-top-prelight");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_LEFT] = load_tab_piece (tab_piece_directory, "left-bumper-active");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_FILL] = load_tab_piece (tab_piece_directory, "fill-active");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_RIGHT] = load_tab_piece (tab_piece_directory, "right-top-active");
sidebar_tabs->details->tab_piece_images[TAB_BACKGROUND] = load_tab_piece (tab_piece_directory, "fill-empty-space");
sidebar_tabs->details->tab_piece_images[TAB_BACKGROUND_RIGHT] = load_tab_piece (tab_piece_directory, "right-empty-space");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_PRELIGHT_LEFT] = load_tab_piece (tab_piece_directory, "left-bumper-active-prelight");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_PRELIGHT_FILL] = load_tab_piece (tab_piece_directory, "fill-active-prelight");
sidebar_tabs->details->tab_piece_images[TAB_ACTIVE_PRELIGHT_RIGHT] = load_tab_piece (tab_piece_directory, "right-top-active-prelight");
}
/* determine the tab associated with the passed-in coordinates, and pass back the notebook
page index associated with it. */
int nautilus_sidebar_tabs_hit_test (NautilusSidebarTabs *sidebar_tabs, int x, int y)
{
GList *current_item;
TabItem *tab_item;
GdkRectangle *rect_ptr;
int result;
sidebar_tabs->details->total_height = sidebar_tabs->details->tab_height;
current_item = sidebar_tabs->details->tab_items;
/* if we're in title mode, see if we're in the title rectangle */
if (sidebar_tabs->details->title_mode) {
rect_ptr = &sidebar_tabs->details->title_rect;
if ((x >= rect_ptr->x) && (x < rect_ptr->x + rect_ptr->width) &&
(y >= rect_ptr->y) && (y<rect_ptr->y + rect_ptr->height))
return 0;
return -1;
}
if (current_item == NULL)
return -1;
/* loop through the items, seeing it the passed in point is in one of the rectangles */
tab_item = (TabItem*) current_item->data;
if (!tab_item->visible && current_item->next) {
tab_item = (TabItem*) current_item->next->data;
}
result = -1;
while (current_item != NULL) {
tab_item = (TabItem*) current_item->data;
rect_ptr = &tab_item->tab_rect;
if (tab_item->visible) {
if ((x >= rect_ptr->x) && (x < rect_ptr->x + rect_ptr->width) &&
(y >= rect_ptr->y) && (y< rect_ptr->y + rect_ptr->height))
result = tab_item->notebook_page;
}
current_item = current_item->next;
}
return result;
}
/* utility routine to update the total height of all of the tabs */
static int
measure_total_height (NautilusSidebarTabs *sidebar_tabs)
{
/* relayout the tabs and report the result */
draw_or_layout_all_tabs (sidebar_tabs, TRUE);
return sidebar_tabs->details->total_height;
}
/* resize the widget based on the number of tabs */
static void
recalculate_size(NautilusSidebarTabs *sidebar_tabs)
{
GtkWidget *widget = GTK_WIDGET (sidebar_tabs);
/* layout tabs to make sure height measurement is valid */
measure_total_height (sidebar_tabs);
widget->requisition.width = widget->parent ? widget->parent->allocation.width: 136;
if (sidebar_tabs->details->title_mode)
widget->requisition.height = sidebar_tabs->details->tab_height;
else
widget->requisition.height = sidebar_tabs->details->total_height + TAB_TOP_GAP;
gtk_widget_queue_resize (widget);
}
static void
nautilus_sidebar_tabs_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
NautilusSidebarTabs *sidebar_tabs = NAUTILUS_SIDEBAR_TABS(widget);
NAUTILUS_CALL_PARENT_CLASS (GTK_WIDGET_CLASS, size_allocate, (widget, allocation));
/* layout tabs to make sure height measurement is valid */
measure_total_height (sidebar_tabs);
if (!sidebar_tabs->details->title_mode) {
gint delta_height = widget->allocation.height - (sidebar_tabs->details->total_height + TAB_TOP_GAP);
widget->allocation.height -= delta_height;
widget->allocation.y += delta_height;
}
}
/* utility to allocate a gdk_pixbuf and fill it with a specified color */
static GdkPixbuf*
make_colored_pixbuf (int pixbuf_width, int pixbuf_height, GdkColor *color)
{
GdkPixbuf *pixbuf;
int row, col, stride;
char *pixels, *row_pixels;
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf_width, pixbuf_height);
pixels = gdk_pixbuf_get_pixels (pixbuf);
stride = gdk_pixbuf_get_rowstride (pixbuf);
/* loop through and set each pixel */
for (row = 0; row < pixbuf_height; row++) {
row_pixels = (pixels + (row * stride));
for (col = 0; col < pixbuf_width; col++) {
*row_pixels++ = color->red;
*row_pixels++ = color->green;
*row_pixels++ = color->blue;
*row_pixels++ = 255;
}
}
return pixbuf;
}
/* draw a single tab using the default, non-themed approach */
static int
draw_one_tab_plain (NautilusSidebarTabs *sidebar_tabs, GdkGC *gc,
char *tab_name, int x, int y, gboolean prelight_flag, GdkRectangle *tab_rect)
{
int tab_bottom;
int tab_right;
int name_width, name_height;
int total_width;
GtkWidget *widget;
GdkPixbuf *temp_pixbuf;
g_assert (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
/* measure the name and compute the bounding box */
nautilus_scalable_font_measure_text (sidebar_tabs->details->tab_font,
sidebar_tabs->details->font_size,
sidebar_tabs->details->font_size,
tab_name,
strlen (tab_name),
&name_width,
&name_height);
total_width = name_width + 2*TAB_MARGIN;
widget = GTK_WIDGET (sidebar_tabs);
/* set up the tab rectangle for hit-testing */
if (tab_rect) {
tab_rect->x = x;
tab_rect->y = y;
tab_rect->width = total_width;
tab_rect->height = sidebar_tabs->details->tab_height;
}
/* FIXME bugzilla.eazel.com 668:
* we must "ellipsize" the name if it doesn't fit, for now, assume it does
*/
/* fill the tab rectangle with the tab color */
gdk_gc_set_foreground (gc, prelight_flag ? &sidebar_tabs->details->prelight_color : &sidebar_tabs->details->tab_color);
gdk_draw_rectangle (widget->window, gc, TRUE, x, y + 1, total_width, sidebar_tabs->details->tab_height - 1);
/* draw the border */
gdk_gc_set_foreground (gc, &sidebar_tabs->details->line_color);
gdk_draw_line(widget->window, gc, x + 1, y, x + total_width - 2, y);
gdk_draw_line(widget->window, gc, x, y + 1, x, y + sidebar_tabs->details->tab_height - 1);
gdk_draw_line(widget->window, gc, x + total_width - 1, y + 1, x + total_width - 1, y + sidebar_tabs->details->tab_height - 1);
/* draw the highlights for extra dimensionality */
gdk_gc_set_foreground (gc, &sidebar_tabs->details->hilight_color);
gdk_draw_line(widget->window, gc, x + 2, y + 2, x + total_width - 3, y + 2);
gdk_draw_line(widget->window, gc, x + 2, y + 2, x + 2, y + sidebar_tabs->details->tab_height - 1);
/* allocate the pixbuf and fill it with the background color */
temp_pixbuf = make_colored_pixbuf (name_width + 1, name_height + 1, prelight_flag ? &sidebar_tabs->details->prelight_color : &sidebar_tabs->details->tab_color);
/* draw the name into the pixbuf using anti-aliased text */
nautilus_scalable_font_draw_text (sidebar_tabs->details->tab_font, temp_pixbuf,
1, 1,
NULL,
sidebar_tabs->details->font_size, sidebar_tabs->details->font_size,
tab_name, strlen (tab_name),
prelight_flag ? NAUTILUS_RGB_COLOR_WHITE : NAUTILUS_RGB_COLOR_BLACK, 255, FALSE);
nautilus_scalable_font_draw_text (sidebar_tabs->details->tab_font, temp_pixbuf,
0, 0,
NULL,
sidebar_tabs->details->font_size, sidebar_tabs->details->font_size,
tab_name, strlen (tab_name),
prelight_flag ? NAUTILUS_RGB_COLOR_BLACK : NAUTILUS_RGB_COLOR_WHITE, 255, FALSE);
/* blit the pixbuf to the drawable, then release it */
gdk_pixbuf_render_to_drawable_alpha (temp_pixbuf,
widget->window,
0, 0,
x + TAB_MARGIN, y + 5,
name_width + 1, name_height + 1,
GDK_PIXBUF_ALPHA_BILEVEL, 128,
GDK_RGB_DITHER_MAX,
0, 0);
gdk_pixbuf_unref (temp_pixbuf);
/* draw the bottom lines */
tab_bottom = y + sidebar_tabs->details->tab_height - 1;
gdk_gc_set_foreground (gc, &sidebar_tabs->details->line_color);
tab_right = x + 2*TAB_MARGIN + name_width;
gdk_gc_set_foreground (gc, &sidebar_tabs->details->line_color);
gdk_draw_line(widget->window, gc, tab_right, tab_bottom, widget->parent->allocation.width, tab_bottom);
gdk_draw_line(widget->window, gc, 0, tab_bottom, x, tab_bottom);
return name_width + 2*TAB_MARGIN;
}
/* utility to draw a single tab piece into a pixbuf */
static int
draw_tab_piece_aa (NautilusSidebarTabs *sidebar_tabs, GdkPixbuf *dest_pixbuf, int x, int y, int x_limit, int which_piece)
{
GtkWidget *widget;
GdkPixbuf *pixbuf;
int width, height;
int dest_width, dest_height;
int blit_width, blit_height;
widget = GTK_WIDGET (sidebar_tabs);
pixbuf = sidebar_tabs->details->tab_piece_images[which_piece];
/* if there's no pixbuf, just exit, returning a nominal size */
if (pixbuf == NULL) {
return 32;
}
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
dest_width = gdk_pixbuf_get_width (dest_pixbuf);
dest_height = gdk_pixbuf_get_height (dest_pixbuf);
/* trim tab piece to fit within the destination and the passed in limits */
if (x_limit > 0) {
if (x_limit < dest_width) {
dest_width = x_limit;
}
}
if (x + width > dest_width) {
blit_width = dest_width - x;
} else {
blit_width = width;
}
if (y + height > dest_height) {
blit_height = dest_height - y;
} else {
blit_height = height;
}
if (x >= 0 && y >= 0 && width > 0 && height > 0) {
gdk_pixbuf_copy_area (pixbuf,
0, 0,
blit_width, blit_height,
dest_pixbuf,
x, y);
}
return width;
}
/* draw a single tab using the theme image to define it's appearance. This does not
draw the right edge of the tab, as we don't have enough info about the next tab
to do it properly at this time. We draw into an offscreen pixbuf so we can get nice anti-aliased text */
static int
draw_one_tab_themed (NautilusSidebarTabs *sidebar_tabs, GdkPixbuf *tab_pixbuf,
char *tab_name, int x, int y, gboolean prelight_flag,
gboolean first_flag, gboolean prev_invisible, int text_h_offset,
GdkRectangle *tab_rect)
{
GtkWidget *widget;
int name_width, name_height;
int piece_width, tab_width;
int current_pos, right_edge_pos;
int text_x_pos, left_width;
int highlight_offset;
int text_x;
int text_y;
left_width = 0;
widget = GTK_WIDGET (sidebar_tabs);
highlight_offset = prelight_flag && !sidebar_tabs->details->title_mode ? TAB_PRELIGHT_LEFT : 0;
if (sidebar_tabs->details->title_mode) {
highlight_offset += TAB_ACTIVE_LEFT - TAB_NORMAL_LEFT;
if (prelight_flag) {
highlight_offset += TAB_ACTIVE_PRELIGHT_LEFT - TAB_ACTIVE_LEFT;
}
}
/* measure the size of the name */
nautilus_scalable_font_measure_text (sidebar_tabs->details->tab_font,
sidebar_tabs->details->font_size,
sidebar_tabs->details->font_size,
tab_name,
strlen (tab_name),
&name_width,
&name_height);
/* draw the left edge piece */
current_pos = x - widget->allocation.x;
if (first_flag) {
left_width = draw_tab_piece_aa (sidebar_tabs, tab_pixbuf, x, y - widget->allocation.y,
-1, TAB_NORMAL_LEFT + highlight_offset);
current_pos += left_width;
}
/* draw the middle portion in a loop */
text_x_pos = current_pos;
right_edge_pos = current_pos + name_width;
while (current_pos < right_edge_pos) {
piece_width = draw_tab_piece_aa (sidebar_tabs, tab_pixbuf, current_pos, y - widget->allocation.y,
right_edge_pos, TAB_NORMAL_FILL + highlight_offset);
current_pos += piece_width;
}
if (!first_flag && !prev_invisible) {
text_x_pos += text_h_offset;
}
text_x = text_x_pos + 1;
text_y = y - widget->allocation.y + THEMED_TAB_TEXT_V_OFFSET;
nautilus_scalable_font_draw_text (sidebar_tabs->details->tab_font, tab_pixbuf,
text_x, text_y,
NULL,
sidebar_tabs->details->font_size, sidebar_tabs->details->font_size,
tab_name, strlen (tab_name),
NAUTILUS_RGB_COLOR_BLACK, 255, FALSE);
text_x -= 1;
text_y -= 1;
nautilus_scalable_font_draw_text (sidebar_tabs->details->tab_font, tab_pixbuf,
text_x, text_y,
NULL,
sidebar_tabs->details->font_size, sidebar_tabs->details->font_size,
tab_name, strlen (tab_name),
NAUTILUS_RGB_COLOR_WHITE, 255, FALSE);
/* set up the bounds rectangle for later hit-testing */
if (tab_rect) {
tab_rect->x = x;
tab_rect->y = y;
tab_rect->width = current_pos - x;
tab_rect->height = sidebar_tabs->details->tab_height;
}
/* return the total tab width */
tab_width = left_width + name_width;
return tab_width;
}
static int
get_text_offset (void)
{
int offset;
char *temp_str;
temp_str = nautilus_theme_get_theme_data ("sidebar", "TEXT_H_OFFSET");
if (temp_str) {
offset = atoi (temp_str);
g_free (temp_str);
} else {
offset = 0;
}
return offset;
}
/* utility to return the width of the passed in tab */
static int
get_tab_width (NautilusSidebarTabs *sidebar_tabs, TabItem *this_tab, gboolean is_themed, gboolean first_flag)
{
int edge_width, name_width, name_height;
if (this_tab == NULL)
return 0;
if (is_themed) {
if (first_flag)
edge_width = gdk_pixbuf_get_width (sidebar_tabs->details->tab_piece_images[TAB_NORMAL_LEFT]);
else
edge_width = 0;
nautilus_scalable_font_measure_text (sidebar_tabs->details->tab_font,
sidebar_tabs->details->font_size,
sidebar_tabs->details->font_size,
this_tab->tab_text,
strlen (this_tab->tab_text),
&name_width,
&name_height);
} else {
edge_width = 2 * TAB_MARGIN;
name_width = gdk_string_width(GTK_WIDGET (sidebar_tabs)->style->font, this_tab->tab_text);
}
return name_width + edge_width;
}
/* fill the canvas buffer with a tiled pixmap */
static void
draw_pixbuf_tiled_aa (GdkPixbuf *src_pixbuf, GdkPixbuf *dest_pixbuf, int offset)
{
int x, y;
int start_x, start_y;
int end_x, end_y;
int tile_width, tile_height;
int blit_width, blit_height;
tile_width = gdk_pixbuf_get_width (src_pixbuf);
tile_height = gdk_pixbuf_get_height (src_pixbuf);
start_x = 0;
start_y = offset;
end_x = gdk_pixbuf_get_width (dest_pixbuf);
end_y = gdk_pixbuf_get_height (dest_pixbuf);
for (y = start_y; y < end_y; y += tile_height) {
for (x = start_x; x < end_x; x += tile_width) {
if (x + tile_width < end_x) {
blit_width = tile_width;
} else {
blit_width = end_x - x;
}
if (y + tile_height < end_y) {
blit_height = tile_height;
} else {
blit_height = end_y - y;
}
gdk_pixbuf_copy_area (src_pixbuf,
0, 0,
blit_width, blit_height,
dest_pixbuf,
x, y);
}
}
}
/* allocate_cleared_pixbuf allocates a new pixbuf with the passed-in dimensions and
clears it to be transparent */
static GdkPixbuf*
allocate_cleared_pixbuf (int width, int height)
{
guchar *pixels_ptr;
GdkPixbuf *pixbuf;
int y_index, row_stride;
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
/* clear it */
row_stride = gdk_pixbuf_get_rowstride (pixbuf);
pixels_ptr = gdk_pixbuf_get_pixels (pixbuf);
for (y_index = 0; y_index < height; y_index++) {
memset (pixels_ptr, 0, row_stride);
pixels_ptr += row_stride;
}
return pixbuf;
}
/* draw or hit test all of the currently visible tabs */
static void
draw_or_layout_all_tabs (NautilusSidebarTabs *sidebar_tabs, gboolean layout_only)
{
GdkGC *temp_gc;
GdkPixbuf *tab_pixbuf;
TabItem *prev_item;
int tab_width;
int extra_fill;
GList *next_tab;
GtkWidget *widget;
int x_pos, y_pos;
int last_x_pos, last_y_pos;
int cur_x_pos, extra_width;
int y_top, fill_height;
int total_width, total_height, tab_select;
int piece_width, text_h_offset;
int end_piece_width;
gboolean is_themed, changed_rows, needs_compositing;
gboolean first_flag, prev_invisible;
g_assert (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
temp_gc = NULL;
tab_pixbuf = NULL;
text_h_offset = 0;
next_tab = sidebar_tabs->details->tab_items;
widget = GTK_WIDGET (sidebar_tabs);
if (widget->allocation.width <= 0 || widget->allocation.height <= 0)
return;
is_themed = sidebar_tabs->details->tab_piece_images[0] != NULL;
if (is_themed) {
text_h_offset = get_text_offset ();
/* allocate a pixbuf to draw into, and clear it */
if (!layout_only) {
tab_pixbuf = allocate_cleared_pixbuf (widget->allocation.width, widget->allocation.height);
}
}
/* set up the initial positions from the widget */
x_pos = widget->allocation.x + sidebar_tabs->details->tab_left_offset;
y_pos = widget->allocation.y + widget->allocation.height - sidebar_tabs->details->tab_height;
total_height = sidebar_tabs->details->tab_height;
total_width = widget->allocation.x + widget->allocation.width;
if (sidebar_tabs->details->tab_piece_images[TAB_NORMAL_RIGHT]) {
end_piece_width = gdk_pixbuf_get_width (sidebar_tabs->details->tab_piece_images[TAB_NORMAL_RIGHT]);
} else {
end_piece_width = 0;
}
/* allocate a graphic context and clear the space below the top tabs to the background color */
y_top = widget->allocation.y + sidebar_tabs->details->tab_height + TAB_TOP_GAP;
fill_height = widget->allocation.y + widget->allocation.height - y_top;
if (!layout_only) {
temp_gc = gdk_gc_new (widget->window);
if (is_themed) {
g_assert (tab_pixbuf != NULL);
draw_pixbuf_tiled_aa (sidebar_tabs->details->tab_piece_images[TAB_BACKGROUND], tab_pixbuf, sidebar_tabs->details->tab_height + TAB_TOP_GAP);
} else {
gdk_gc_set_foreground (temp_gc, &sidebar_tabs->details->background_color);
gdk_draw_rectangle (widget->window, temp_gc, TRUE, widget->allocation.x, y_top, widget->allocation.width, fill_height);
}
}
/* here's the main loop where we draw as many tabs in a row as will fit. Since we need to know
the state of the next tab to draw the right edge of the current one, things are kind of
complicated - we draw the right edge after positioning the next tab */
first_flag = TRUE; /* keep track of first tab in a row, since it has a different left edge */
prev_item = NULL;
while (next_tab != NULL) {
TabItem *this_item = next_tab->data;
if (this_item->visible && !layout_only) {
prev_invisible = prev_item && !prev_item->visible;
if (is_themed) {
g_assert (tab_pixbuf != NULL);
draw_one_tab_themed (sidebar_tabs, tab_pixbuf, this_item->tab_text, x_pos, y_pos,
this_item->prelit, first_flag, prev_invisible,
text_h_offset, &this_item->tab_rect);
} else {
g_assert (temp_gc != NULL);
draw_one_tab_plain (sidebar_tabs, temp_gc, this_item->tab_text,
x_pos, y_pos, this_item->prelit, &this_item->tab_rect);
}
}
tab_width = get_tab_width (sidebar_tabs, this_item, is_themed, first_flag);
first_flag = FALSE;
prev_item = this_item;
next_tab = next_tab->next;
if (next_tab != NULL)
this_item = (TabItem*) next_tab->data;
else
this_item = NULL;
last_x_pos = x_pos;
last_y_pos = y_pos;
/* bump the x-position, and see if it fits */
x_pos += tab_width;
if (!is_themed)
x_pos += TAB_H_GAP;
if (x_pos + get_tab_width (sidebar_tabs, this_item, is_themed, FALSE) > (total_width - end_piece_width - 8)) {
/* wrap to the next line */
x_pos = widget->allocation.x + sidebar_tabs->details->tab_left_offset;
if (is_themed)
y_pos -= sidebar_tabs->details->tab_height;
else
y_pos -= sidebar_tabs->details->tab_height + TAB_ROW_V_OFFSET;
first_flag = TRUE;
if ((next_tab != NULL) && ((next_tab->next != NULL) || this_item->visible)) {
total_height += sidebar_tabs->details->tab_height;
if (!is_themed)
total_height += TAB_ROW_V_OFFSET;
}
}
/* finish drawing the right edge */
if (is_themed) {
changed_rows = y_pos != last_y_pos;
needs_compositing = FALSE;
if (changed_rows && this_item != NULL /* && this_item->visible */) {
if (prev_item->prelit) {
tab_select = TAB_PRELIGHT_RIGHT;
extra_fill = TAB_PRELIGHT_FILL;
} else {
tab_select = TAB_NORMAL_RIGHT;
extra_fill = TAB_NORMAL_FILL;
}
/* we must do some extra drawing of the fill pattern here, to stretch it out to the edge */
extra_width = gdk_pixbuf_get_width (sidebar_tabs->details->tab_piece_images[extra_fill]);
cur_x_pos = last_x_pos + tab_width;
last_x_pos = total_width - gdk_pixbuf_get_width (sidebar_tabs->details->tab_piece_images[tab_select]) - tab_width;
if (prev_item->visible) {
int x = widget->allocation.x;
int y = widget->allocation.y;
while (cur_x_pos < total_width) {
if (!layout_only)
draw_tab_piece_aa (sidebar_tabs, tab_pixbuf, cur_x_pos - x,
last_y_pos - y, total_width, extra_fill);
cur_x_pos += extra_width;
}
}
} else if ((this_item == NULL) || !this_item->visible) {
tab_select = prev_item->prelit ? TAB_PRELIGHT_EDGE : TAB_NORMAL_EDGE;
needs_compositing = TRUE;
} else {
if (prev_item->prelit) {
tab_select = TAB_PRELIGHT_NEXT;
} else if (this_item->prelit) {
tab_select = TAB_PRELIGHT_NEXT_ALT;
} else {
tab_select = TAB_NORMAL_NEXT;
}
}
if (!prev_item->visible)
tab_select = (!changed_rows && this_item) ? TAB_NORMAL_LEFT : -1;
piece_width = 0;
if (tab_select >= 0) {
GdkPixbuf *temp_pixbuf = sidebar_tabs->details->tab_piece_images[tab_select];
piece_width = gdk_pixbuf_get_width (temp_pixbuf);
if (!layout_only) {
if (needs_compositing) {
int dest_x = last_x_pos + tab_width - widget->allocation.x;
int dest_y = last_y_pos - widget->allocation.y;
gdk_pixbuf_composite (temp_pixbuf, tab_pixbuf,
dest_x,
dest_y,
gdk_pixbuf_get_width (temp_pixbuf),
gdk_pixbuf_get_height (temp_pixbuf),
dest_x,
dest_y,
1.0, 1.0,
GDK_INTERP_BILINEAR,
0xFF);
} else {
piece_width = draw_tab_piece_aa (sidebar_tabs, tab_pixbuf,
last_x_pos + tab_width - widget->allocation.x,
last_y_pos - widget->allocation.y, -1,
tab_select);
}
}
prev_item->tab_rect.width = last_x_pos + tab_width + piece_width - prev_item->tab_rect.x;
}
if (!changed_rows)
x_pos += piece_width;
}
}
/* draw the off-screen buffer to the screen, then release it */
if (!layout_only) {
gdk_gc_unref(temp_gc);
if (is_themed) {
/* draw the pixbuf onto the widget and release it */
gdk_pixbuf_render_to_drawable_alpha (tab_pixbuf,
widget->window,
0, 0,
widget->allocation.x, widget->allocation.y,
widget->allocation.width, widget->allocation.height,
GDK_PIXBUF_ALPHA_BILEVEL, 128,
GDK_RGB_DITHER_MAX,
0, 0);
gdk_pixbuf_unref (tab_pixbuf);
}
}
sidebar_tabs->details->total_height = total_height;
}
/* find a tab with a given name, or return NULL if we can't find one */
static TabItem *
tab_item_find_by_name (NautilusSidebarTabs *sidebar_tabs, const char *name)
{
GList *iterator;
g_return_val_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs), NULL);
g_return_val_if_fail (name != NULL, NULL);
for (iterator = sidebar_tabs->details->tab_items; iterator != NULL; iterator = iterator->next) {
TabItem *tab_item = iterator->data;
g_assert (tab_item != NULL);
g_assert (tab_item->tab_text != NULL);
if (strcmp (tab_item->tab_text, name) == 0) {
return tab_item;
}
}
return NULL;
}
/* handle an expose event by drawing the tabs */
static int
nautilus_sidebar_tabs_expose (GtkWidget *widget, GdkEventExpose *event)
{
NautilusSidebarTabs *sidebar_tabs;
int tab_width, text_offset;
g_return_val_if_fail (NAUTILUS_IS_SIDEBAR_TABS (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (widget->window == NULL) {
return FALSE;
}
if (widget->parent->allocation.width <= 4) {
return FALSE;
}
sidebar_tabs = NAUTILUS_SIDEBAR_TABS (widget);
text_offset = get_text_offset ();
/* draw the tabs */
if (sidebar_tabs->details->title_mode) {
GdkPixbuf *pixbuf;
GdkGC* temp_gc = gdk_gc_new(widget->window);
int tab_height;
int x_pos = widget->allocation.x;
int y_pos = widget->allocation.y;
if (sidebar_tabs->details->tab_piece_images[0]) {
tab_height = gdk_pixbuf_get_height (sidebar_tabs->details->tab_piece_images[0]);
pixbuf = allocate_cleared_pixbuf (widget->allocation.width, tab_height);
tab_width = draw_one_tab_themed (sidebar_tabs, pixbuf, sidebar_tabs->details->title, 0, 0,
sidebar_tabs->details->title_prelit, TRUE, FALSE, text_offset, &sidebar_tabs->details->title_rect);
/* draw the right edge */
draw_tab_piece_aa (sidebar_tabs, pixbuf, tab_width, 0, -1,
sidebar_tabs->details->title_prelit ? TAB_ACTIVE_PRELIGHT_RIGHT : TAB_ACTIVE_RIGHT);
/* transfer the pixmap to the screen */
gdk_pixbuf_render_to_drawable_alpha (pixbuf,
widget->window,
0, 0,
x_pos, y_pos,
widget->allocation.width, tab_height,
GDK_PIXBUF_ALPHA_BILEVEL, 128,
GDK_RGB_DITHER_MAX,
0, 0);
gdk_pixbuf_unref (pixbuf);
} else {
draw_one_tab_plain (sidebar_tabs, temp_gc, sidebar_tabs->details->title,
x_pos + TITLE_TAB_OFFSET, y_pos, sidebar_tabs->details->title_prelit, &sidebar_tabs->details->title_rect);
}
gdk_gc_unref (temp_gc);
} else {
if (sidebar_tabs->details->tab_count > 0) {
draw_or_layout_all_tabs (sidebar_tabs, FALSE);
}
}
return FALSE;
}
/* add a new tab entry, return TRUE if we succeed */
gboolean
nautilus_sidebar_tabs_add_view (NautilusSidebarTabs *sidebar_tabs, const char *name, GtkWidget *new_view, int page_num)
{
TabItem *new_tab_item;
g_return_val_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
g_return_val_if_fail (new_view != NULL, FALSE);
/* Check to see if we already have one with this name, if so, refuse to add it */
if (tab_item_find_by_name (sidebar_tabs, name)) {
g_warning ("nautilus_sidebar_tabs_add_view: Trying to add duplicate item '%s'", name);
return FALSE;
}
/* allocate a new entry, and initialize it */
new_tab_item = g_new0 (TabItem, 1);
new_tab_item->tab_text = g_strdup (name);
new_tab_item->visible = TRUE;
new_tab_item->prelit = FALSE;
new_tab_item->tab_view = new_view;
new_tab_item->notebook_page = page_num;
/* add it to the list */
sidebar_tabs->details->tab_items = g_list_append (sidebar_tabs->details->tab_items, new_tab_item);
sidebar_tabs->details->tab_count += 1;
recalculate_size (sidebar_tabs);
gtk_widget_queue_draw (GTK_WIDGET (sidebar_tabs));
return TRUE;
}
/* return the name of the tab with the passed in index */
char*
nautilus_sidebar_tabs_get_title_from_index (NautilusSidebarTabs *sidebar_tabs, int which_tab)
{
GList *next_tab;
g_return_val_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs), NULL);
for (next_tab = sidebar_tabs->details->tab_items; next_tab != NULL; next_tab = next_tab->next) {
TabItem *item = next_tab->data;
if (item->notebook_page == which_tab)
return g_strdup (item->tab_text);
}
/* shouldn't ever get here... */
return g_strdup ("");
}
/* remove the specified tab entry */
void
nautilus_sidebar_tabs_remove_view (NautilusSidebarTabs *sidebar_tabs, const char *name)
{
GList *next_tab;
TabItem *tab_item;
int old_page_number;
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
g_return_if_fail (name != NULL);
/* Look up the item */
tab_item = tab_item_find_by_name (sidebar_tabs, name);
if (tab_item == NULL) {
g_warning ("nautilus_sidebar_tabs_remove_view: Trying to remove a non-existing item '%s'", name);
return;
}
/* Remove the item from the list */
sidebar_tabs->details->tab_items = g_list_remove (sidebar_tabs->details->tab_items, tab_item);
old_page_number = tab_item->notebook_page;
tab_item_destroy (tab_item);
/* decrement all page numbers greater than the one we're removing */
for (next_tab = sidebar_tabs->details->tab_items; next_tab != NULL; next_tab = next_tab->next) {
TabItem *item = next_tab->data;
if (item->notebook_page >= old_page_number)
item->notebook_page -= 1;
}
sidebar_tabs->details->tab_count -= 1;
recalculate_size (sidebar_tabs);
gtk_widget_queue_draw (GTK_WIDGET (sidebar_tabs));
}
/* prelight a tab, from its associated notebook page number, by setting the prelight flag of
the proper tab and clearing the others */
void
nautilus_sidebar_tabs_prelight_tab (NautilusSidebarTabs *sidebar_tabs, int which_tab)
{
GList *next_tab;
gboolean is_prelit;
gboolean changed = FALSE;
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
if (sidebar_tabs->details->title_mode) {
gboolean is_prelit = which_tab != -1;
if (sidebar_tabs->details->title_prelit != is_prelit) {
sidebar_tabs->details->title_prelit = is_prelit;
changed = TRUE;
}
}
else
for (next_tab = sidebar_tabs->details->tab_items; next_tab != NULL; next_tab = next_tab->next) {
TabItem *item = next_tab->data;
is_prelit = (item->notebook_page == which_tab);
if (item->prelit != is_prelit) {
item->prelit = is_prelit;
changed = TRUE;
}
}
if (changed)
gtk_widget_queue_draw(GTK_WIDGET(sidebar_tabs));
}
/* select a tab, from its associated notebook page number, by making it invisible
and all the others visible */
void
nautilus_sidebar_tabs_select_tab (NautilusSidebarTabs *sidebar_tabs, int which_tab)
{
GList *next_tab;
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
for (next_tab = sidebar_tabs->details->tab_items; next_tab != NULL; next_tab = next_tab->next) {
TabItem *item = next_tab->data;
item->visible = (item->notebook_page != which_tab);
item->prelit = FALSE;
}
recalculate_size(sidebar_tabs);
gtk_widget_queue_draw(GTK_WIDGET(sidebar_tabs));
}
/* utility routine that returns true if the passed-in color is lighter than average */
static gboolean
is_light_color(GdkColor *color)
{
int intensity = (((color->red >> 8) * 77) + ((color->green >> 8) * 150) + ((color->blue >> 8) * 28)) >> 8;
return intensity > 160; /* biased slightly toward dark so default of 0x999999 uses light text light Susan specified */
}
/* set the background color associated with a tab */
void
nautilus_sidebar_tabs_set_color (NautilusSidebarTabs *sidebar_tabs,
const char *color_spec)
{
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
g_return_if_fail (color_spec != NULL);
gdk_color_parse (color_spec, &sidebar_tabs->details->tab_color);
gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (sidebar_tabs)),
&sidebar_tabs->details->tab_color, FALSE, TRUE);
if (sidebar_tabs->details->tab_piece_images[0] == NULL) {
if (is_light_color(&sidebar_tabs->details->tab_color))
setup_dark_text(sidebar_tabs);
else
setup_light_text(sidebar_tabs);
}
gtk_widget_queue_draw (GTK_WIDGET(sidebar_tabs));
}
/* receive a dropped color */
void
nautilus_sidebar_tabs_receive_dropped_color (NautilusSidebarTabs *sidebar_tabs,
int x, int y,
GtkSelectionData *selection_data)
{
guint16 *channels;
char *color_spec;
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
g_return_if_fail (selection_data != NULL);
/* Convert the selection data into a color spec. */
if (selection_data->length != 8 || selection_data->format != 16) {
g_warning ("received invalid color data");
return;
}
channels = (guint16 *) selection_data->data;
color_spec = g_strdup_printf ("rgb:%04hX/%04hX/%04hX", channels[0], channels[1], channels[2]);
nautilus_sidebar_tabs_set_color(sidebar_tabs, color_spec);
g_free (color_spec);
}
/* set the title (used in title mode only) */
void
nautilus_sidebar_tabs_set_title (NautilusSidebarTabs *sidebar_tabs, const char *new_title)
{
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
g_return_if_fail (new_title != NULL);
g_free(sidebar_tabs->details->title);
sidebar_tabs->details->title = g_strdup (new_title);
}
/* set the title mode boolean */
void
nautilus_sidebar_tabs_set_title_mode (NautilusSidebarTabs *sidebar_tabs, gboolean is_title_mode)
{
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
if (sidebar_tabs->details->title_mode != !!is_title_mode) {
sidebar_tabs->details->title_mode = !!is_title_mode;
recalculate_size (sidebar_tabs);
gtk_widget_queue_draw (GTK_WIDGET (sidebar_tabs));
}
}
/* set the visibility of the selected tab */
void
nautilus_sidebar_tabs_set_visible (NautilusSidebarTabs *sidebar_tabs,
const char *name,
gboolean is_visible)
{
TabItem *tab_item;
g_return_if_fail (NAUTILUS_IS_SIDEBAR_TABS (sidebar_tabs));
g_return_if_fail (name != NULL);
/* Look up the item */
tab_item = tab_item_find_by_name (sidebar_tabs, name);
if (tab_item == NULL) {
g_warning ("nautilus_sidebar_tabs_set_visible: Trying to munge a non-existing item '%s'", name);
return;
}
if (tab_item->visible != is_visible) {
tab_item->visible = is_visible;
gtk_widget_queue_draw (GTK_WIDGET (sidebar_tabs));
}
}
|