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
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Author :
* Damon Chaplin <damon@helixcode.com>
*
* Copyright 1999, Helix Code, 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
*/
/*
* EIconBar is a subclass of ECanvas for displaying a vertical column of
* icons and descriptions. It provides 2 views - large icons and small icons.
*/
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <gdk-pixbuf/gnome-canvas-pixbuf.h>
#include "e-icon-bar.h"
#include "e-icon-bar-bg-item.h"
#include "../e-text/e-text.h"
/* These are the offsets of the icons & text in both views. Note that the
shadow around icons is drawn in the space between items (as is the
horizontal bar when dragging). */
#define E_ICON_BAR_LARGE_ICON_SPACING 8 /* Spacing between items. */
#define E_ICON_BAR_LARGE_ICON_WIDTH 48
#define E_ICON_BAR_LARGE_ICON_HEIGHT 48
#define E_ICON_BAR_LARGE_ICON_TEXT_X 4
#define E_ICON_BAR_LARGE_ICON_TEXT_Y (E_ICON_BAR_LARGE_ICON_HEIGHT + 4)
#define E_ICON_BAR_SMALL_ICON_SPACING 4 /* Spacing between items. */
#define E_ICON_BAR_SMALL_ICON_WIDTH 24
#define E_ICON_BAR_SMALL_ICON_HEIGHT 24
#define E_ICON_BAR_SMALL_ICON_X 4
#define E_ICON_BAR_SMALL_ICON_TEXT_X (E_ICON_BAR_SMALL_ICON_WIDTH + 6)
/* The space we leave at the top or bottom of the bar when position an item
while it is being edited. This is used since the EIconBar may be in a
EScrolledBar which may show buttons at the top or bottom. */
#define E_ICON_BAR_V_SPACE 22
/* The number of pixels the mouse has to be moved with the button down before
we start a drag. */
#define E_ICON_BAR_DRAG_START_OFFSET 4
/* This is the area at the top & bottom of the bar where we auto-scroll if the
mouse goes into during a drag-and-drop operation. */
#define E_ICON_BAR_DRAG_AUTO_SCROLL_OFFSET 16
/* This is the time between each auto-scroll, when dragging. */
#define E_ICON_BAR_SCROLL_TIMEOUT 30
/* This is the number of timeouts we skip before we start scrolling. */
#define E_ICON_BAR_SCROLL_DELAY 12
static void e_icon_bar_class_init (EIconBarClass *class);
static void e_icon_bar_init (EIconBar *icon_bar);
static void e_icon_bar_destroy (GtkObject *object);
static void e_icon_bar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gint e_icon_bar_leave_notify_event (GtkWidget *widget,
GdkEventCrossing *event);
static gint e_icon_bar_focus_in (GtkWidget *widget,
GdkEventFocus *event);
static gint e_icon_bar_focus_out (GtkWidget *widget,
GdkEventFocus *event);
static gint e_icon_bar_drag_motion (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time);
static void e_icon_bar_drag_leave (GtkWidget *widget,
GdkDragContext *context,
guint time);
static void e_icon_bar_set_dragging_before_item (EIconBar *icon_bar,
gint before_item);
static gboolean e_icon_bar_timeout_handler (gpointer data);
static void e_icon_bar_recalc_common_positions (EIconBar *icon_bar);
static gint e_icon_bar_recalc_item_positions (EIconBar *icon_bar);
static void e_icon_bar_reflow (ECanvas *canvas);
static gint e_icon_bar_find_item (EIconBar *icon_bar,
GnomeCanvasItem *text_item);
static gboolean e_icon_bar_on_item_event (GnomeCanvasItem *item,
GdkEvent *event,
EIconBar *icon_bar);
static gboolean e_icon_bar_large_icons_intersects (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y);
static gboolean e_icon_bar_large_icons_is_before (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y);
static gboolean e_icon_bar_small_icons_intersects (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y);
static gboolean e_icon_bar_small_icons_is_before (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y);
static void e_icon_bar_on_editing_started (EIconBar *icon_bar,
GnomeCanvasItem *item);
static void e_icon_bar_on_editing_stopped (EIconBar *icon_bar,
GnomeCanvasItem *item);
static void e_icon_bar_ensure_edited_item_visible (EIconBar *icon_bar);
static void e_icon_bar_update_highlight (EIconBar *icon_bar);
enum
{
ITEM_SELECTED,
ITEM_DRAGGED,
LAST_SIGNAL
};
static guint e_icon_bar_signals[LAST_SIGNAL] = {0};
static ECanvasClass *parent_class;
GtkType
e_icon_bar_get_type (void)
{
static GtkType e_icon_bar_type = 0;
if (!e_icon_bar_type){
GtkTypeInfo e_icon_bar_info = {
"EIconBar",
sizeof (EIconBar),
sizeof (EIconBarClass),
(GtkClassInitFunc) e_icon_bar_class_init,
(GtkObjectInitFunc) e_icon_bar_init,
NULL, /* reserved 1 */
NULL, /* reserved 2 */
(GtkClassInitFunc) NULL
};
parent_class = gtk_type_class (e_canvas_get_type ());
e_icon_bar_type = gtk_type_unique (e_canvas_get_type (),
&e_icon_bar_info);
}
return e_icon_bar_type;
}
static void
e_icon_bar_class_init (EIconBarClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
ECanvasClass *ecanvas_class;
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
ecanvas_class = E_CANVAS_CLASS( class );
e_icon_bar_signals[ITEM_SELECTED] =
gtk_signal_new ("item_selected",
GTK_RUN_LAST | GTK_RUN_ACTION,
object_class->type,
GTK_SIGNAL_OFFSET (EIconBarClass,
selected_item),
gtk_marshal_NONE__POINTER_INT,
GTK_TYPE_NONE, 2, GTK_TYPE_GDK_EVENT,
GTK_TYPE_INT);
e_icon_bar_signals[ITEM_DRAGGED] =
gtk_signal_new ("item_dragged",
GTK_RUN_LAST | GTK_RUN_ACTION,
object_class->type,
GTK_SIGNAL_OFFSET (EIconBarClass,
dragged_item),
gtk_marshal_NONE__POINTER_INT,
GTK_TYPE_NONE, 2, GTK_TYPE_GDK_EVENT,
GTK_TYPE_INT);
gtk_object_class_add_signals (object_class, e_icon_bar_signals,
LAST_SIGNAL);
/* Method override */
object_class->destroy = e_icon_bar_destroy;
widget_class->size_allocate = e_icon_bar_size_allocate;
widget_class->leave_notify_event = e_icon_bar_leave_notify_event;
widget_class->focus_in_event = e_icon_bar_focus_in;
widget_class->focus_out_event = e_icon_bar_focus_out;
widget_class->drag_motion = e_icon_bar_drag_motion;
widget_class->drag_leave = e_icon_bar_drag_leave;
ecanvas_class->reflow = e_icon_bar_reflow;
class->selected_item = NULL;
}
static void
e_icon_bar_init (EIconBar *icon_bar)
{
GdkColormap *colormap;
gboolean success[E_ICON_BAR_COLOR_LAST];
gint nfailed;
icon_bar->view_type = E_ICON_BAR_LARGE_ICONS;
icon_bar->items = g_array_new (FALSE, FALSE, sizeof (EIconBarItem));
icon_bar->pressed_item_num = -1;
icon_bar->mouse_over_item_num = -1;
icon_bar->editing_item_num = -1;
icon_bar->edit_rect_item = NULL;
icon_bar->in_drag = FALSE;
icon_bar->dragging_before_item_num = -1;
icon_bar->icon_x = 0;
icon_bar->icon_w = 0;
icon_bar->icon_h = 0;
icon_bar->text_x = 0;
icon_bar->text_w = 5;
icon_bar->auto_scroll_timeout_id = 0;
/* Create the background item in the canvas, which handles selections
and drag-and-drop. */
gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (icon_bar)->root),
e_icon_bar_bg_item_get_type (),
"EIconBarBgItem::icon_bar", icon_bar,
NULL);
colormap = gtk_widget_get_colormap (GTK_WIDGET (icon_bar));
icon_bar->colors[E_ICON_BAR_COLOR_TEXT].red = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_TEXT].green = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_TEXT].blue = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_TEXT].red = 0;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_TEXT].green = 0;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_TEXT].blue = 0;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT].red = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT].green = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT].blue = 65535;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT_OUTLINE].red = 0;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT_OUTLINE].green = 0;
icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT_OUTLINE].blue = 0;
nfailed = gdk_colormap_alloc_colors (colormap, icon_bar->colors,
E_ICON_BAR_COLOR_LAST, FALSE,
TRUE, success);
if (nfailed)
g_warning ("Failed to allocate all colors");
}
/**
* e_icon_bar_new:
* @Returns: A new #EIconBar.
*
* Creates a new #EIconBar.
**/
GtkWidget *
e_icon_bar_new (void)
{
GtkWidget *icon_bar;
icon_bar = GTK_WIDGET (gtk_type_new (e_icon_bar_get_type ()));
return icon_bar;
}
static void
e_icon_bar_destroy (GtkObject *object)
{
EIconBar *icon_bar;
icon_bar = E_ICON_BAR (object);
GTK_OBJECT_CLASS (parent_class)->destroy (object);
g_array_free (icon_bar->items, TRUE);
if (icon_bar->auto_scroll_timeout_id != 0) {
gtk_timeout_remove (icon_bar->auto_scroll_timeout_id);
icon_bar->auto_scroll_timeout_id = 0;
}
}
static void
e_icon_bar_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
EIconBar *icon_bar;
gint canvas_width, canvas_height, height;
#if 0
g_print ("In e_icon_bar_size_allocate\n");
#endif
icon_bar = E_ICON_BAR (widget);
GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
canvas_width = GTK_WIDGET (icon_bar)->allocation.width;
canvas_height = GTK_WIDGET (icon_bar)->allocation.height;
/* Reset the y position and widths of all the items to the width of
the canvas, and reset the button labels, so they fit. */
e_icon_bar_recalc_common_positions (icon_bar);
height = e_icon_bar_recalc_item_positions (icon_bar);
gnome_canvas_set_scroll_region (GNOME_CANVAS (widget),
0, 0, canvas_width,
MAX (height, canvas_height - 1));
/* If we are editing an item, make sure it is visible. */
e_icon_bar_ensure_edited_item_visible (icon_bar);
GTK_LAYOUT (widget)->vadjustment->step_increment = 16;
e_icon_bar_update_highlight (icon_bar);
}
/* This sets all the item positions which are the same for all items in the
group. */
static void
e_icon_bar_recalc_common_positions (EIconBar *icon_bar)
{
gint canvas_width;
canvas_width = GTK_WIDGET (icon_bar)->allocation.width;
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
icon_bar->icon_x = (canvas_width - E_ICON_BAR_LARGE_ICON_WIDTH) / 2;
icon_bar->icon_w = E_ICON_BAR_LARGE_ICON_WIDTH;
icon_bar->icon_h = E_ICON_BAR_LARGE_ICON_HEIGHT;
icon_bar->text_x = E_ICON_BAR_LARGE_ICON_TEXT_X;
icon_bar->text_w = MAX (canvas_width - (E_ICON_BAR_LARGE_ICON_TEXT_X * 2), 5);
icon_bar->spacing = E_ICON_BAR_LARGE_ICON_SPACING;
} else {
icon_bar->icon_x = E_ICON_BAR_SMALL_ICON_X;
icon_bar->icon_w = E_ICON_BAR_SMALL_ICON_WIDTH;
icon_bar->icon_h = E_ICON_BAR_SMALL_ICON_HEIGHT;
icon_bar->text_x = E_ICON_BAR_SMALL_ICON_TEXT_X;
icon_bar->text_w = MAX (canvas_width - E_ICON_BAR_SMALL_ICON_TEXT_X, 5);
icon_bar->spacing = E_ICON_BAR_SMALL_ICON_SPACING;
}
}
/* This recalculates the positions of all the items, according to the current
view type and the height of the text items. */
static gint
e_icon_bar_recalc_item_positions (EIconBar *icon_bar)
{
EIconBarItem *item;
gint y, item_num, text_h;
gdouble x1, y1, x2, y2, text_x;
GtkJustification justify;
GtkAnchorType anchor;
gint max_lines;
GdkFont *font;
gboolean line_wrap;
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
justify = GTK_JUSTIFY_CENTER;
anchor = GTK_ANCHOR_N;
max_lines = 2;
text_x = icon_bar->text_x + (icon_bar->text_w / 2);
line_wrap = TRUE;
/*
* Fixme: incorrect
*/
font = GTK_WIDGET (icon_bar)->style->font;
text_h = font->ascent + font->descent;
} else {
justify = GTK_JUSTIFY_LEFT;
anchor = GTK_ANCHOR_NW;
max_lines = 1;
text_x = icon_bar->text_x;
font = GTK_WIDGET (icon_bar)->style->font;
text_h = font->ascent + font->descent;
line_wrap = FALSE;
}
/* Now step through the items, setting the y positions. */
y = icon_bar->spacing;
for (item_num = 0; item_num < icon_bar->items->len; item_num++) {
item = &g_array_index (icon_bar->items,
EIconBarItem, item_num);
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
item->icon_y = y;
item->text_y = y + E_ICON_BAR_LARGE_ICON_TEXT_Y;
} else {
item->text_height = text_h;
item->item_height = MAX (text_h, E_ICON_BAR_SMALL_ICON_HEIGHT);
item->icon_y = y + (item->item_height - E_ICON_BAR_SMALL_ICON_HEIGHT) / 2;
item->text_y = y + (item->item_height - item->text_height) / 2;
}
gnome_canvas_item_set (item->text,
"clip_width", (gdouble) (icon_bar->text_w),
"justification", justify,
"anchor", anchor,
"max_lines", max_lines,
"line_wrap", line_wrap,
"x", text_x,
"y", (gdouble) item->text_y,
NULL);
/* Get the text item's height. */
gnome_canvas_item_get_bounds (item->text, &x1, &y1, &x2, &y2);
item->text_x = x1;
item->text_width = x2 - x1;
item->text_height = y2 - y1;
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
item->item_height = E_ICON_BAR_LARGE_ICON_TEXT_Y
+ item->text_height;
}
gnome_canvas_item_set (item->image,
"GnomeCanvasPixbuf::x", (gdouble)icon_bar->icon_x,
"GnomeCanvasPixbuf::y", (gdouble)item->icon_y,
"GnomeCanvasPixbuf::width_set", TRUE,
"GnomeCanvasPixbuf::height_set", TRUE,
"GnomeCanvasPixbuf::width", (gdouble)icon_bar->icon_w,
"GnomeCanvasPixbuf::height", (gdouble)icon_bar->icon_h,
NULL);
y += item->item_height + icon_bar->spacing;
}
return y;
}
static gint
e_icon_bar_leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
{
EIconBar *icon_bar;
icon_bar = E_ICON_BAR (widget);
GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
/* Make sure no items are highlighted. */
e_icon_bar_item_motion (icon_bar, -1, NULL);
return FALSE;
}
static gint
e_icon_bar_focus_in (GtkWidget *widget,
GdkEventFocus *event)
{
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (E_IS_ICON_BAR (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
GTK_WIDGET_CLASS (parent_class)->focus_in_event (widget, event);
GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
return FALSE;
}
static gint
e_icon_bar_focus_out (GtkWidget *widget,
GdkEventFocus *event)
{
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (E_IS_ICON_BAR (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
GTK_WIDGET_CLASS (parent_class)->focus_out_event (widget, event);
GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
return FALSE;
}
/**
* e_icon_bar_set_view_type:
* @icon_bar: An #EIconBar.
* @view_type: The new view type, %E_ICON_BAR_LARGE_ICONS or
* %E_ICON_BAR_SMALL_ICONS.
*
* Sets the view type of the #EIconBar.
**/
void
e_icon_bar_set_view_type (EIconBar *icon_bar,
EIconBarViewType view_type)
{
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
if (icon_bar->view_type == view_type)
return;
icon_bar->view_type = view_type;
/* Queue a resize of the canvas, so everything is put in the right
positions based on the new view type. */
gtk_widget_queue_resize (GTK_WIDGET (icon_bar));
}
static GdkPixbuf *
flatten_alpha (GdkPixbuf *image, guint rgb)
{
if (!image || !gdk_pixbuf_get_has_alpha (image))
return NULL;
return gdk_pixbuf_composite_color_simple (
image,
gdk_pixbuf_get_width (image),
gdk_pixbuf_get_height (image),
GDK_INTERP_NEAREST,
255,
32,
rgb, rgb);
}
/*
* Creates a 24-bits RGB value from a GdkColor
*/
static guint
rgb_from_gdk_color (GdkColor *color)
{
guint a =
(((color->red >> 8) << 16) |
((color->green >> 8) << 8) |
((color->blue >> 8)));
return a;
}
/**
* e_icon_bar_add_item:
* @icon_bar: An #EIconBar.
* @image: the new item's icon.
* @text: the new item's text.
* @position: the position to place the new item, or -1 to place it last.
*
* Adds an item to the #EIconBar at the given position.
**/
gint
e_icon_bar_add_item (EIconBar *icon_bar,
GdkPixbuf *image,
const gchar *text,
gint position)
{
EIconBarItem item;
GtkJustification justify;
GtkAnchorType anchor;
gint max_lines, retval;
GtkStyle *style;
GdkFont *font;
gdouble text_x, clip_height;
GdkPixbuf *flattened = NULL;
g_return_val_if_fail (E_IS_ICON_BAR (icon_bar), -1);
g_return_val_if_fail (text != NULL, -1);
g_return_val_if_fail (position >= -1, -1);
g_return_val_if_fail (position <= (gint)icon_bar->items->len, -1);
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
justify = GTK_JUSTIFY_CENTER;
anchor = GTK_ANCHOR_N;
max_lines = 2;
text_x = icon_bar->text_x + (icon_bar->text_w / 2);
} else {
justify = GTK_JUSTIFY_LEFT;
anchor = GTK_ANCHOR_NW;
max_lines = 1;
text_x = icon_bar->text_x;
}
gtk_widget_ensure_style (GTK_WIDGET (icon_bar));
style = GTK_WIDGET (icon_bar)->style;
font = style->font;
clip_height = max_lines * (font->ascent + font->descent);
item.text = gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (icon_bar)->root),
e_text_get_type (),
"font_gdk", font,
"fill_color_gdk", &icon_bar->colors[E_ICON_BAR_COLOR_TEXT],
"use_ellipsis", TRUE,
"anchor", anchor,
"editable", TRUE,
"justification", justify,
"line_wrap", TRUE,
"max_lines", max_lines,
"x", text_x,
"y", (gdouble) 0,
"clip", TRUE,
"clip_width", (gdouble) (icon_bar->text_w),
"clip_height", clip_height,
"text", text,
NULL);
gtk_signal_connect (GTK_OBJECT (item.text), "event",
GTK_SIGNAL_FUNC (e_icon_bar_on_item_event),
icon_bar);
flattened = flatten_alpha (image,
rgb_from_gdk_color (&style->bg [GTK_STATE_NORMAL]));
item.image = gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (icon_bar)->root),
gnome_canvas_pixbuf_get_type (),
"GnomeCanvasPixbuf::pixbuf", flattened ? flattened : image,
"GnomeCanvasPixbuf::width", (gdouble) icon_bar->icon_w,
"GnomeCanvasPixbuf::height", (gdouble) icon_bar->icon_h,
NULL);
if (flattened)
/* the canvas item holds the reference now */
gdk_pixbuf_unref (flattened);
gtk_signal_connect (GTK_OBJECT (item.image), "event",
GTK_SIGNAL_FUNC (e_icon_bar_on_item_event),
icon_bar);
item.data = NULL;
item.destroy = NULL;
if (position == -1) {
g_array_append_val (icon_bar->items, item);
retval = icon_bar->items->len - 1;
} else {
g_array_insert_val (icon_bar->items, position, item);
retval = position;
/* FIXME: Should possibly update other indices. */
if (icon_bar->dragged_item_num >= position)
icon_bar->dragged_item_num++;
}
gtk_widget_queue_resize (GTK_WIDGET (icon_bar));
return retval;
}
/**
* e_icon_bar_reorder_item:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item to move.
* @new_position: The new position of the item, which is used after the item
* has been removed from its current position. If @new_position is -1, the item
* is placed last.
*
* Moves an item to a new position within the #EIconBar.
**/
void
e_icon_bar_reorder_item (EIconBar *icon_bar,
gint item_num,
gint new_position)
{
EIconBarItem tmp_item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
g_return_if_fail (new_position >= -1);
g_return_if_fail (new_position < icon_bar->items->len);
tmp_item = g_array_index (icon_bar->items, EIconBarItem, item_num);
g_array_remove_index (icon_bar->items, item_num);
if (new_position == -1)
g_array_append_val (icon_bar->items, tmp_item);
else
g_array_insert_val (icon_bar->items, new_position, tmp_item);
gtk_widget_queue_resize (GTK_WIDGET (icon_bar));
}
/**
* e_icon_bar_remove_item:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item to remove.
*
* Removes an item from the #EIconBar.
**/
void
e_icon_bar_remove_item (EIconBar *icon_bar,
gint item_num)
{
EIconBarItem *item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
if (item->destroy)
item->destroy (item->data);
gtk_object_destroy (GTK_OBJECT (item->text));
gtk_object_destroy (GTK_OBJECT (item->image));
g_array_remove_index (icon_bar->items, item_num);
gtk_widget_queue_resize (GTK_WIDGET (icon_bar));
}
/**
* e_icon_bar_get_item_image:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @Returns: The icon of the given item.
*
* Returns the icon used for the given item.
**/
GdkPixbuf *
e_icon_bar_get_item_image (EIconBar *icon_bar,
gint item_num)
{
EIconBarItem *item;
GdkPixbuf *image = NULL;
g_return_val_if_fail (E_IS_ICON_BAR (icon_bar), NULL);
g_return_val_if_fail (item_num >= 0, NULL);
g_return_val_if_fail (item_num < icon_bar->items->len, NULL);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
gtk_object_get (GTK_OBJECT (item->image),
"GnomeCanvasPixbuf::pixbuf", &image,
NULL);
return image;
}
/**
* e_icon_bar_set_item_image:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @image: The new icon to use for the given item.
*
* Sets the icon to use for the given item.
**/
void
e_icon_bar_set_item_image (EIconBar *icon_bar,
gint item_num,
GdkPixbuf *image)
{
EIconBarItem *item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
gnome_canvas_item_set (item->image,
"GnomeCanvasPixbuf::pixbuf", image,
NULL);
}
/**
* e_icon_bar_get_item_text:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @Returns: The text of the given item.
*
* Returns the text of the given item. This should be freed when no longer
* needed.
**/
gchar*
e_icon_bar_get_item_text (EIconBar *icon_bar,
gint item_num)
{
EIconBarItem *item;
gchar *text;
g_return_val_if_fail (E_IS_ICON_BAR (icon_bar), NULL);
g_return_val_if_fail (item_num >= 0, NULL);
g_return_val_if_fail (item_num < icon_bar->items->len, NULL);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
gtk_object_get (GTK_OBJECT (item->text),
"EText::text", &text,
NULL);
return text;
}
/**
* e_icon_bar_set_item_text:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @text: The new text for the given item.
*
* Sets the text of the given item.
**/
void
e_icon_bar_set_item_text (EIconBar *icon_bar,
gint item_num,
gchar *text)
{
EIconBarItem *item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
gnome_canvas_item_set (item->text,
"EText::text", text,
NULL);
}
/**
* e_icon_bar_get_item_data:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @Returns: The user data associated with the given item.
*
* Returns the user data associated with the given item.
**/
gpointer
e_icon_bar_get_item_data (EIconBar *icon_bar,
gint item_num)
{
EIconBarItem *item;
g_return_val_if_fail (E_IS_ICON_BAR (icon_bar), NULL);
g_return_val_if_fail (item_num >= 0, NULL);
g_return_val_if_fail (item_num < icon_bar->items->len, NULL);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
return item->data;
}
/**
* e_icon_bar_set_item_data:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @data: The user data to set for the given item.
*
* Sets the user data of the given item.
**/
void
e_icon_bar_set_item_data (EIconBar *icon_bar,
gint item_num,
gpointer data)
{
e_icon_bar_set_item_data_full (icon_bar, item_num, data, NULL);
}
/**
* e_icon_bar_set_item_data_full:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
* @data: The user data to set for the given item.
* @destroy: The function to free @data when the item is destroyed.
*
* Sets the user data of the given item, and the function to free the data
* when the item is destroyed.
**/
void
e_icon_bar_set_item_data_full (EIconBar *icon_bar,
gint item_num,
gpointer data,
GtkDestroyNotify destroy)
{
EIconBarItem *item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
item = &g_array_index (icon_bar->items, EIconBarItem, item_num);
if (item->destroy)
item->destroy (item->data);
item->data = data;
item->destroy = destroy;
}
static void
e_icon_bar_reflow (ECanvas *canvas)
{
#if 0
g_print ("In e_icon_bar_on_canvas_reflow\n");
gtk_widget_queue_resize (GTK_WIDGET (canvas));
#endif
}
/* This returns the index of the given item, or -1 if it isn't found. */
static gint
e_icon_bar_find_item (EIconBar *icon_bar,
GnomeCanvasItem *canvas_item)
{
EIconBarItem *item;
gint item_num;
for (item_num = 0; item_num < icon_bar->items->len; item_num++) {
item = &g_array_index (icon_bar->items,
EIconBarItem, item_num);
if (item->text == canvas_item || item->image == canvas_item) {
return item_num;
}
}
return -1;
}
/* When an item has a grab, it will get all events, so we need to use the
position to find the real item. */
static gboolean
e_icon_bar_on_item_event (GnomeCanvasItem *item,
GdkEvent *event,
EIconBar *icon_bar)
{
gint item_num;
switch (event->type) {
case GDK_BUTTON_PRESS:
item_num = e_icon_bar_find_item_at_position (icon_bar,
event->button.x,
event->button.y,
NULL);
/* If the item is not being edited, we handle the event and
stop the signal so the text editing isn't started. */
if (icon_bar->editing_item_num == -1
|| icon_bar->editing_item_num != item_num) {
e_icon_bar_item_pressed (icon_bar, item_num, event);
gtk_signal_emit_stop_by_name (GTK_OBJECT (item),
"event");
return TRUE;
}
break;
case GDK_BUTTON_RELEASE:
item_num = e_icon_bar_find_item_at_position (icon_bar,
event->button.x,
event->button.y,
NULL);
/* If the item is not being edited, we handle the event and
stop the signal so the text editing isn't started. */
if (icon_bar->editing_item_num == -1
|| icon_bar->editing_item_num != item_num) {
e_icon_bar_item_released (icon_bar, item_num, event);
gtk_signal_emit_stop_by_name (GTK_OBJECT (item),
"event");
return TRUE;
}
break;
case GDK_MOTION_NOTIFY:
item_num = e_icon_bar_find_item_at_position (icon_bar,
event->motion.x,
event->motion.y,
NULL);
e_icon_bar_item_motion (icon_bar, item_num, event);
return TRUE;
case GDK_FOCUS_CHANGE:
if (event->focus_change.in)
e_icon_bar_on_editing_started (icon_bar, item);
else
e_icon_bar_on_editing_stopped (icon_bar, item);
return FALSE;
default:
break;
}
return FALSE;
}
void
e_icon_bar_item_pressed (EIconBar *icon_bar,
gint item_num,
GdkEvent *event)
{
gint button;
/* If we are editing an item, and a different item (or anywhere outside
an item) is clicked, stop the edit. If the item being edited is
clicked we just return, since the user may be selecting text. */
if (icon_bar->editing_item_num != -1) {
if (icon_bar->editing_item_num != item_num) {
e_icon_bar_stop_editing_item (icon_bar, TRUE);
}
return;
}
button = event->button.button;
if (button == 1 && item_num != -1) {
icon_bar->pressed_item_num = item_num;
icon_bar->pressed_x = event->button.x;
icon_bar->pressed_y = event->button.y;
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
} else if (button == 3) {
gtk_signal_emit (GTK_OBJECT (icon_bar),
e_icon_bar_signals[ITEM_SELECTED],
event, item_num);
}
}
void
e_icon_bar_item_released (EIconBar *icon_bar,
gint item_num,
GdkEvent *event)
{
gint button;
/* If we are editing an item, just return. */
if (icon_bar->editing_item_num != -1)
return;
button = event->button.button;
if (button == 1) {
if (icon_bar->pressed_item_num != -1
&& icon_bar->pressed_item_num == icon_bar->mouse_over_item_num) {
gtk_signal_emit (GTK_OBJECT (icon_bar),
e_icon_bar_signals[ITEM_SELECTED],
event, item_num);
}
icon_bar->pressed_item_num = -1;
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
}
}
void
e_icon_bar_item_motion (EIconBar *icon_bar,
gint item_num,
GdkEvent *event)
{
gboolean need_redraw = TRUE;
if (event && event->motion.state & GDK_BUTTON1_MASK
&& icon_bar->pressed_item_num != -1) {
if (abs (event->motion.x - icon_bar->pressed_x) > E_ICON_BAR_DRAG_START_OFFSET
|| abs (event->motion.y - icon_bar->pressed_y) > E_ICON_BAR_DRAG_START_OFFSET) {
icon_bar->dragged_item_num = icon_bar->pressed_item_num;
gtk_signal_emit (GTK_OBJECT (icon_bar),
e_icon_bar_signals[ITEM_DRAGGED],
event, icon_bar->dragged_item_num);
/* Don't show the button as pressed while dragging. */
icon_bar->pressed_item_num = -1;
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
}
return;
}
if (icon_bar->mouse_over_item_num == item_num)
return;
/* If we are editing an item, items aren't highlighted so we don't
need a redraw. Also if an item is pressed, we only need a redraw if
item_num or the old mouse_over_item_num is the pressed item. */
if (icon_bar->editing_item_num != -1) {
need_redraw = FALSE;
} else if (icon_bar->pressed_item_num != -1) {
if (icon_bar->pressed_item_num != item_num
&& icon_bar->pressed_item_num != icon_bar->mouse_over_item_num)
need_redraw = FALSE;
}
icon_bar->mouse_over_item_num = item_num;
if (need_redraw)
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
}
/* This returns the index of the item at the given position on the EIconBar,
or -1 if no item is found. If before_item is not NULL, it returns the
item which the mouse is before, or -1 (for dragging). */
gint
e_icon_bar_find_item_at_position (EIconBar *icon_bar,
gint x,
gint y,
gint *before_item)
{
EIconBarItem *item = NULL;
gint item_num;
if (before_item)
*before_item = -1;
for (item_num = 0; item_num < icon_bar->items->len; item_num++) {
item = &g_array_index (icon_bar->items,
EIconBarItem, item_num);
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
if (e_icon_bar_large_icons_intersects (icon_bar, item,
x, y))
return item_num;
if (before_item
&& e_icon_bar_large_icons_is_before (icon_bar,
item, x, y)) {
*before_item = item_num;
return -1;
}
} else {
if (e_icon_bar_small_icons_intersects (icon_bar, item,
x, y))
return item_num;
if (before_item
&& e_icon_bar_small_icons_is_before (icon_bar,
item, x, y)) {
*before_item = item_num;
return -1;
}
}
}
/* If the mouse is below all the items, but inside the items' width,
and before_item is not NULL, we set it to the number of items, so
the dropped item would be added at the end. Note that this assumes
that the item variable points to the last item in the EIconBar. */
if (before_item) {
if (icon_bar->view_type == E_ICON_BAR_LARGE_ICONS) {
if (x < icon_bar->text_x
|| x >= icon_bar->text_x + icon_bar->text_w)
return -1;
if (item == NULL
|| y > item->icon_y + item->item_height)
*before_item = icon_bar->items->len;
} else {
if (x < icon_bar->icon_x
|| x >= icon_bar->text_x + icon_bar->text_w)
return -1;
if (item == NULL) {
*before_item = icon_bar->items->len;
} else {
gint max_y;
max_y = MAX (item->icon_y + icon_bar->icon_h,
item->text_y + item->text_height);
if (y > max_y)
*before_item = icon_bar->items->len;
}
}
}
return -1;
}
static gboolean
e_icon_bar_large_icons_intersects (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y)
{
if (y < item->icon_y || y >= item->text_y + item->text_height)
return FALSE;
if (y < item->icon_y + icon_bar->icon_h) {
if (x < icon_bar->icon_x
|| x >= icon_bar->icon_x + icon_bar->icon_w)
return FALSE;
} else {
if (x < item->text_x
|| x >= item->text_x + item->text_width)
return FALSE;
}
return TRUE;
}
static gboolean
e_icon_bar_large_icons_is_before (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y)
{
if (y < item->icon_y - icon_bar->spacing
|| y >= item->icon_y)
return FALSE;
if (x < icon_bar->text_x || x >= icon_bar->text_x + icon_bar->text_w)
return FALSE;
return TRUE;
}
static gboolean
e_icon_bar_small_icons_intersects (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y)
{
gint min_y, max_y;
min_y = MIN (item->icon_y, item->text_y);
max_y = MAX (item->icon_y + icon_bar->icon_h,
item->text_y + item->text_height);
if (y < min_y || y >= max_y)
return FALSE;
if (x < icon_bar->icon_x || x >= item->text_x + item->text_width)
return FALSE;
return TRUE;
}
static gboolean
e_icon_bar_small_icons_is_before (EIconBar *icon_bar,
EIconBarItem *item,
gint x,
gint y)
{
gint min_y, max_y;
max_y = MIN (item->icon_y, item->text_y);
min_y = max_y - icon_bar->spacing;
if (y < min_y || y >= max_y)
return FALSE;
if (x < icon_bar->icon_x || x >= icon_bar->text_x + icon_bar->text_w)
return FALSE;
return TRUE;
}
/**
* e_icon_bar_start_editing_item:
* @icon_bar: An #EIconBar.
* @item_num: The index of the item.
*
* Turns the item into an editable text field so the user can rename it.
* Editing is stopped automatically when the user hits 'Return' or clicks
* outside the item. It can also be stopped explicitly by calling
* e_icon_bar_stop_editing_item().
**/
void
e_icon_bar_start_editing_item (EIconBar *icon_bar,
gint item_num)
{
EIconBarItem *item;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
g_return_if_fail (item_num >= 0);
g_return_if_fail (item_num < icon_bar->items->len);
item = &g_array_index (icon_bar->items,
EIconBarItem, item_num);
e_canvas_item_grab_focus (item->text);
}
/**
* e_icon_bar_stop_editing_item:
* @icon_bar: An #EIconBar.
* @accept: TRUE if the changes should be accepted, FALSE if the text should be
* changed back to its state before the editing started.
*
* Stops the editing of the items, if any were being edited.
**/
void
e_icon_bar_stop_editing_item (EIconBar *icon_bar,
gboolean accept)
{
EIconBarItem *item;
GtkWidget *toplevel;
g_return_if_fail (E_IS_ICON_BAR (icon_bar));
if (icon_bar->editing_item_num != -1) {
item = &g_array_index (icon_bar->items, EIconBarItem,
icon_bar->editing_item_num);
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_bar));
if (toplevel && GTK_IS_WINDOW (toplevel))
gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
}
}
static void
e_icon_bar_on_editing_started (EIconBar *icon_bar,
GnomeCanvasItem *item)
{
gint item_num;
gdouble x1, y1, x2, y2;
item_num = e_icon_bar_find_item (icon_bar, item);
g_return_if_fail (item_num != -1);
/* Turn off any highlighted item. */
e_icon_bar_item_motion (icon_bar, -1, NULL);
icon_bar->editing_item_num = item_num;
e_icon_bar_ensure_edited_item_visible (icon_bar);
/* Set the fg & bg colors. */
gnome_canvas_item_set (item,
"fill_color_gdk", &icon_bar->colors[E_ICON_BAR_COLOR_EDITING_TEXT],
NULL);
/* Create the edit rect if necessary. */
if (!icon_bar->edit_rect_item) {
icon_bar->edit_rect_item =
gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (icon_bar)->root),
gnome_canvas_rect_get_type(),
"fill_color_gdk", &icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT],
"outline_color_gdk", &icon_bar->colors[E_ICON_BAR_COLOR_EDITING_RECT_OUTLINE],
NULL);
}
gnome_canvas_item_get_bounds (item, &x1, &y1, &x2, &y2);
gnome_canvas_item_set (icon_bar->edit_rect_item,
"x1", x1 - 1,
"y1", y1 - 1,
"x2", x2 + 1,
"y2", y2 + 1,
NULL);
gnome_canvas_item_show (icon_bar->edit_rect_item);
/* Make sure the text item is on top. */
gnome_canvas_item_raise_to_top (item);
}
static void
e_icon_bar_on_editing_stopped (EIconBar *icon_bar,
GnomeCanvasItem *item)
{
gint item_num;
item_num = e_icon_bar_find_item (icon_bar, item);
g_return_if_fail (item_num != -1);
icon_bar->editing_item_num = -1;
e_icon_bar_update_highlight (icon_bar);
/* Reset the fg & bg colors. */
gnome_canvas_item_set (item,
"fill_color_gdk", &icon_bar->colors[E_ICON_BAR_COLOR_TEXT],
NULL);
if (icon_bar->edit_rect_item) {
gnome_canvas_item_hide (icon_bar->edit_rect_item);
}
}
static void
e_icon_bar_ensure_edited_item_visible (EIconBar *icon_bar)
{
EIconBarItem *item;
gint scroll_x, scroll_y, min_scroll_y, max_scroll_y, new_scroll_y;
if (icon_bar->editing_item_num == -1)
return;
item = &g_array_index (icon_bar->items,
EIconBarItem, icon_bar->editing_item_num);
gnome_canvas_get_scroll_offsets (GNOME_CANVAS (icon_bar),
&scroll_x, &scroll_y);
/* The minimum scroll y position is with the text right on the bottom
of the display. */
min_scroll_y = item->text_y + item->text_height + E_ICON_BAR_V_SPACE
- GTK_WIDGET (icon_bar)->allocation.height;
/* The maximum scroll y position is with the text at the top. */
max_scroll_y = item->text_y - E_ICON_BAR_V_SPACE;
new_scroll_y = MAX (scroll_y, min_scroll_y);
new_scroll_y = MIN (new_scroll_y, max_scroll_y);
if (new_scroll_y != scroll_y)
gnome_canvas_scroll_to (GNOME_CANVAS (icon_bar),
scroll_x, new_scroll_y);
}
/* This gets the mouse position and updates the highlight if necessary.
It is called after items are added/deleted/scrolled/edited. */
static void
e_icon_bar_update_highlight (EIconBar *icon_bar)
{
GtkWidget *widget;
gint x, y, item_num;
widget = GTK_WIDGET (icon_bar);
if (!widget->window)
return;
gdk_window_get_pointer (widget->window, &x, &y, NULL);
if (x < 0 || y < 0
|| x > widget->allocation.width || y > widget->allocation.height)
return;
item_num = e_icon_bar_find_item_at_position (icon_bar, x, y, NULL);
e_icon_bar_item_motion (icon_bar, item_num, NULL);
}
static gint
e_icon_bar_drag_motion (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time)
{
EIconBar *icon_bar;
gint item_num, before_item, scroll_x, scroll_y;
g_return_val_if_fail (E_IS_ICON_BAR (widget), FALSE);
icon_bar = E_ICON_BAR (widget);
icon_bar->in_drag = TRUE;
/* Check if the mouse is over or between items, and if so highlight. */
gnome_canvas_get_scroll_offsets (GNOME_CANVAS (icon_bar),
&scroll_x, &scroll_y);
item_num = e_icon_bar_find_item_at_position (icon_bar,
x + scroll_x,
y + scroll_y,
&before_item);
e_icon_bar_item_motion (icon_bar, item_num, NULL);
e_icon_bar_set_dragging_before_item (icon_bar, before_item);
/* Check if the mouse is at the top or bottom of the bar, and if it is
scroll up/down. */
if (y < E_ICON_BAR_DRAG_AUTO_SCROLL_OFFSET)
icon_bar->scrolling_up = TRUE;
else if (y >= widget->allocation.height - E_ICON_BAR_DRAG_AUTO_SCROLL_OFFSET)
icon_bar->scrolling_up = FALSE;
else {
if (icon_bar->auto_scroll_timeout_id != 0) {
gtk_timeout_remove (icon_bar->auto_scroll_timeout_id);
icon_bar->auto_scroll_timeout_id = 0;
}
return FALSE;
}
if (icon_bar->auto_scroll_timeout_id == 0) {
icon_bar->auto_scroll_timeout_id = g_timeout_add (E_ICON_BAR_SCROLL_TIMEOUT, e_icon_bar_timeout_handler, icon_bar);
icon_bar->auto_scroll_delay = E_ICON_BAR_SCROLL_DELAY;
}
return FALSE;
}
static void
e_icon_bar_drag_leave (GtkWidget *widget,
GdkDragContext *context,
guint time)
{
EIconBar *icon_bar;
g_return_if_fail (E_IS_ICON_BAR (widget));
icon_bar = E_ICON_BAR (widget);
icon_bar->in_drag = FALSE;
if (icon_bar->auto_scroll_timeout_id != 0) {
gtk_timeout_remove (icon_bar->auto_scroll_timeout_id);
icon_bar->auto_scroll_timeout_id = 0;
}
if (icon_bar->mouse_over_item_num != -1) {
icon_bar->mouse_over_item_num = -1;
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
}
}
static void
e_icon_bar_set_dragging_before_item (EIconBar *icon_bar,
gint before_item)
{
if (icon_bar->dragging_before_item_num == before_item)
return;
icon_bar->dragging_before_item_num = before_item;
gtk_widget_queue_draw (GTK_WIDGET (icon_bar));
}
static gboolean
e_icon_bar_timeout_handler (gpointer data)
{
EIconBar *icon_bar;
gint scroll_x, scroll_y, new_scroll_y;
GtkAdjustment *adj;
g_return_val_if_fail (E_IS_ICON_BAR (data), FALSE);
icon_bar = E_ICON_BAR (data);
GDK_THREADS_ENTER ();
if (icon_bar->auto_scroll_delay > 0) {
icon_bar->auto_scroll_delay--;
GDK_THREADS_LEAVE ();
return TRUE;
}
gnome_canvas_get_scroll_offsets (GNOME_CANVAS (icon_bar),
&scroll_x, &scroll_y);
adj = GTK_LAYOUT (icon_bar)->vadjustment;
if (icon_bar->scrolling_up)
new_scroll_y = MAX (scroll_y - adj->step_increment, 0);
else
new_scroll_y = MIN (scroll_y + adj->step_increment,
adj->upper - adj->page_size);
if (new_scroll_y != scroll_y)
gnome_canvas_scroll_to (GNOME_CANVAS (icon_bar),
scroll_x, new_scroll_y);
GDK_THREADS_LEAVE ();
return TRUE;
}
|