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
|
/* GTK - The GIMP Toolkit
* gtkrecentchoosermenu.c - Recently used items menu widget
* Copyright (C) 2005, Emmanuele Bassi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <gdk/gdkscreen.h>
#include "gtkrecentmanager.h"
#include "gtkrecentfilter.h"
#include "gtkrecentchooser.h"
#include "gtkrecentchooserutils.h"
#include "gtkrecentchooserprivate.h"
#include "gtkrecentchoosermenu.h"
#include "gtkstock.h"
#include "gtkicontheme.h"
#include "gtkiconfactory.h"
#include "gtkintl.h"
#include "gtksettings.h"
#include "gtkmenushell.h"
#include "gtkmenuitem.h"
#include "gtkimagemenuitem.h"
#include "gtkseparatormenuitem.h"
#include "gtkmenu.h"
#include "gtkimage.h"
#include "gtkobject.h"
#include "gtktooltips.h"
#include "gtktypebuiltins.h"
#include "gtkrecentmanager.h"
#include "gtkrecentfilter.h"
#include "gtkrecentchooser.h"
#include "gtkrecentchooserutils.h"
#include "gtkrecentchooserprivate.h"
#include "gtkrecentchoosermenu.h"
#include "gtkalias.h"
struct _GtkRecentChooserMenuPrivate
{
/* the recent manager object */
GtkRecentManager *manager;
/* size of the icons of the menu items */
gint icon_size;
/* RecentChooser properties */
gint limit;
guint show_private : 1;
guint show_not_found : 1;
guint show_tips : 1;
guint show_icons : 1;
guint local_only : 1;
guint show_numbers : 1;
GtkRecentSortType sort_type;
GtkRecentSortFunc sort_func;
gpointer sort_data;
GDestroyNotify sort_data_destroy;
GSList *filters;
GtkRecentFilter *current_filter;
guint local_manager : 1;
gulong manager_changed_id;
gulong populate_id;
/* tooltips for our bookmark items*/
GtkTooltips *tooltips;
};
enum {
PROP_0,
PROP_SHOW_NUMBERS
};
#define FALLBACK_ICON_SIZE 32
#define FALLBACK_ITEM_LIMIT 10
#define GTK_RECENT_CHOOSER_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_CHOOSER_MENU, GtkRecentChooserMenuPrivate))
static void gtk_recent_chooser_menu_finalize (GObject *object);
static GObject *gtk_recent_chooser_menu_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params);
static void gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface);
static void gtk_recent_chooser_menu_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_recent_chooser_menu_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static gboolean gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
const gchar *uri,
GError **error);
static gchar * gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser);
static gboolean gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
const gchar *uri,
GError **error);
static void gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
const gchar *uri);
static void gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser);
static void gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser);
static GList * gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser);
static GtkRecentManager *gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser);
static void gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
GtkRecentSortFunc sort_func,
gpointer sort_data,
GDestroyNotify data_destroy);
static void gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
GtkRecentFilter *filter);
static void gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
GtkRecentFilter *filter);
static GSList * gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser);
static void gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
GtkRecentFilter *filter);
static void gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu);
static void gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
gboolean show_tips);
static void set_recent_manager (GtkRecentChooserMenu *menu,
GtkRecentManager *manager);
static void chooser_set_sort_type (GtkRecentChooserMenu *menu,
GtkRecentSortType sort_type);
static gint get_icon_size_for_widget (GtkWidget *widget);
static void item_activate_cb (GtkWidget *widget,
gpointer user_data);
static void manager_changed_cb (GtkRecentManager *manager,
gpointer user_data);
G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserMenu,
gtk_recent_chooser_menu,
GTK_TYPE_MENU,
G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
gtk_recent_chooser_iface_init))
static void
gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
{
iface->set_current_uri = gtk_recent_chooser_menu_set_current_uri;
iface->get_current_uri = gtk_recent_chooser_menu_get_current_uri;
iface->select_uri = gtk_recent_chooser_menu_select_uri;
iface->unselect_uri = gtk_recent_chooser_menu_unselect_uri;
iface->select_all = gtk_recent_chooser_menu_select_all;
iface->unselect_all = gtk_recent_chooser_menu_unselect_all;
iface->get_items = gtk_recent_chooser_menu_get_items;
iface->get_recent_manager = gtk_recent_chooser_menu_get_recent_manager;
iface->set_sort_func = gtk_recent_chooser_menu_set_sort_func;
iface->add_filter = gtk_recent_chooser_menu_add_filter;
iface->remove_filter = gtk_recent_chooser_menu_remove_filter;
iface->list_filters = gtk_recent_chooser_menu_list_filters;
}
static void
gtk_recent_chooser_menu_class_init (GtkRecentChooserMenuClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->constructor = gtk_recent_chooser_menu_constructor;
gobject_class->finalize = gtk_recent_chooser_menu_finalize;
gobject_class->set_property = gtk_recent_chooser_menu_set_property;
gobject_class->get_property = gtk_recent_chooser_menu_get_property;
_gtk_recent_chooser_install_properties (gobject_class);
/**
* GtkRecentChooserMenu:show-numbers
*
* Whether the first ten items in the menu should be prepended by
* a number acting as a unique mnemonic.
*
* Since: 2.10
*/
g_object_class_install_property (gobject_class,
PROP_SHOW_NUMBERS,
g_param_spec_boolean ("show-numbers",
P_("Show Numbers"),
P_("Whether the items should be displayed with a number"),
FALSE,
G_PARAM_READWRITE));
g_type_class_add_private (klass, sizeof (GtkRecentChooserMenuPrivate));
}
static void
gtk_recent_chooser_menu_init (GtkRecentChooserMenu *menu)
{
GtkRecentChooserMenuPrivate *priv;
priv = GTK_RECENT_CHOOSER_MENU_GET_PRIVATE (menu);
menu->priv = priv;
priv->show_icons= TRUE;
priv->show_numbers = FALSE;
priv->show_tips = FALSE;
priv->show_not_found = TRUE;
priv->show_private = FALSE;
priv->local_only = TRUE;
priv->limit = FALLBACK_ITEM_LIMIT;
priv->sort_type = GTK_RECENT_SORT_NONE;
priv->icon_size = FALLBACK_ICON_SIZE;
priv->current_filter = NULL;
priv->tooltips = gtk_tooltips_new ();
g_object_ref_sink (priv->tooltips);
}
static void
gtk_recent_chooser_menu_finalize (GObject *object)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
GtkRecentChooserMenuPrivate *priv = menu->priv;
g_signal_handler_disconnect (priv->manager, priv->manager_changed_id);
priv->manager_changed_id = 0;
if (priv->populate_id)
g_source_remove (priv->populate_id);
priv->manager = NULL;
if (priv->sort_data_destroy)
{
priv->sort_data_destroy (priv->sort_data);
priv->sort_data_destroy = NULL;
priv->sort_data = NULL;
priv->sort_func = NULL;
}
if (priv->tooltips)
g_object_unref (priv->tooltips);
if (priv->current_filter)
g_object_unref (priv->current_filter);
G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->finalize (object);
}
static GObject *
gtk_recent_chooser_menu_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params)
{
GtkRecentChooserMenu *menu;
GObject *object;
object = G_OBJECT_CLASS (gtk_recent_chooser_menu_parent_class)->constructor (type,
n_construct_properties,
construct_params);
menu = GTK_RECENT_CHOOSER_MENU (object);
g_assert (menu->priv->manager);
return object;
}
static void
gtk_recent_chooser_menu_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
switch (prop_id)
{
case PROP_SHOW_NUMBERS:
menu->priv->show_numbers = g_value_get_boolean (value);
break;
case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
set_recent_manager (menu, g_value_get_object (value));
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
menu->priv->show_private = g_value_get_boolean (value);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
menu->priv->show_not_found = g_value_get_boolean (value);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
gtk_recent_chooser_menu_set_show_tips (menu, g_value_get_boolean (value));
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
menu->priv->show_icons = g_value_get_boolean (value);
break;
case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
G_STRFUNC,
G_OBJECT_TYPE_NAME (object));
break;
case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
menu->priv->local_only = g_value_get_boolean (value);
break;
case GTK_RECENT_CHOOSER_PROP_LIMIT:
menu->priv->limit = g_value_get_int (value);
break;
case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
chooser_set_sort_type (menu, g_value_get_enum (value));
break;
case GTK_RECENT_CHOOSER_PROP_FILTER:
gtk_recent_chooser_menu_set_current_filter (menu, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_recent_chooser_menu_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (object);
switch (prop_id)
{
case PROP_SHOW_NUMBERS:
g_value_set_boolean (value, menu->priv->show_numbers);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
g_value_set_boolean (value, menu->priv->show_tips);
break;
case GTK_RECENT_CHOOSER_PROP_LIMIT:
g_value_set_int (value, menu->priv->limit);
break;
case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
g_value_set_boolean (value, menu->priv->local_only);
break;
case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
g_value_set_enum (value, menu->priv->sort_type);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
g_value_set_boolean (value, menu->priv->show_private);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
g_value_set_boolean (value, menu->priv->show_not_found);
break;
case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
g_value_set_boolean (value, menu->priv->show_icons);
break;
case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
g_warning ("%s: Choosers of type `%s' do not support selecting multiple items.",
G_STRFUNC,
G_OBJECT_TYPE_NAME (object));
break;
case GTK_RECENT_CHOOSER_PROP_FILTER:
g_value_set_object (value, menu->priv->current_filter);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gtk_recent_chooser_menu_set_current_uri (GtkRecentChooser *chooser,
const gchar *uri,
GError **error)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
GList *children, *l;
GtkWidget *menu_item = NULL;
gboolean found = FALSE;
children = gtk_container_get_children (GTK_CONTAINER (menu));
for (l = children; l != NULL; l = l->next)
{
GtkRecentInfo *info;
menu_item = GTK_WIDGET (l->data);
info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
if (!info)
continue;
if (strcmp (uri, gtk_recent_info_get_uri (info)) == 0)
{
gtk_menu_shell_activate_item (GTK_MENU_SHELL (menu),
menu_item,
TRUE);
found = TRUE;
break;
}
}
g_list_free (children);
if (!found)
{
g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
_("No recently used resource found with URI `%s'"),
uri);
}
return found;
}
static gchar *
gtk_recent_chooser_menu_get_current_uri (GtkRecentChooser *chooser)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
GtkWidget *menu_item;
GtkRecentInfo *info;
menu_item = gtk_menu_get_active (GTK_MENU (menu));
if (!menu_item)
return NULL;
info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
if (!info)
return NULL;
return g_strdup (gtk_recent_info_get_uri (info));
}
static gboolean
gtk_recent_chooser_menu_select_uri (GtkRecentChooser *chooser,
const gchar *uri,
GError **error)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
GList *children, *l;
GtkWidget *menu_item = NULL;
gboolean found = FALSE;
children = gtk_container_get_children (GTK_CONTAINER (menu));
for (l = children; l != NULL; l = l->next)
{
GtkRecentInfo *info;
menu_item = GTK_WIDGET (l->data);
info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
if (!info)
continue;
if (0 == strcmp (uri, gtk_recent_info_get_uri (info)))
found = TRUE;
}
g_list_free (children);
if (!found)
{
g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
_("No recently used resource found with URI `%s'"),
uri);
return FALSE;
}
else
{
gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), menu_item);
return TRUE;
}
}
static void
gtk_recent_chooser_menu_unselect_uri (GtkRecentChooser *chooser,
const gchar *uri)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
}
static void
gtk_recent_chooser_menu_select_all (GtkRecentChooser *chooser)
{
g_warning (_("This function is not implemented for "
"widgets of class '%s'"),
g_type_name (G_OBJECT_TYPE (chooser)));
}
static void
gtk_recent_chooser_menu_unselect_all (GtkRecentChooser *chooser)
{
g_warning (_("This function is not implemented for "
"widgets of class '%s'"),
g_type_name (G_OBJECT_TYPE (chooser)));
}
static void
gtk_recent_chooser_menu_set_sort_func (GtkRecentChooser *chooser,
GtkRecentSortFunc sort_func,
gpointer sort_data,
GDestroyNotify data_destroy)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
GtkRecentChooserMenuPrivate *priv = menu->priv;
if (priv->sort_data_destroy)
{
priv->sort_data_destroy (priv->sort_data);
priv->sort_func = NULL;
priv->sort_data = NULL;
priv->sort_data_destroy = NULL;
}
if (sort_func)
{
priv->sort_func = sort_func;
priv->sort_data = sort_data;
priv->sort_data_destroy = data_destroy;
}
}
static gint
sort_recent_items_mru (GtkRecentInfo *a,
GtkRecentInfo *b,
gpointer unused)
{
g_assert (a != NULL && b != NULL);
return (gtk_recent_info_get_modified (a) < gtk_recent_info_get_modified (b));
}
static gint
sort_recent_items_lru (GtkRecentInfo *a,
GtkRecentInfo *b,
gpointer unused)
{
g_assert (a != NULL && b != NULL);
return (gtk_recent_info_get_modified (a) > gtk_recent_info_get_modified (b));
}
/* our proxy sorting function */
static gint
sort_recent_items_proxy (gpointer *a,
gpointer *b,
gpointer user_data)
{
GtkRecentInfo *info_a = (GtkRecentInfo *) a;
GtkRecentInfo *info_b = (GtkRecentInfo *) b;
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
if (menu->priv->sort_func)
return (* menu->priv->sort_func) (info_a,
info_b,
menu->priv->sort_data);
/* fallback */
return 0;
}
static void
chooser_set_sort_type (GtkRecentChooserMenu *menu,
GtkRecentSortType sort_type)
{
if (menu->priv->sort_type == sort_type)
return;
menu->priv->sort_type = sort_type;
}
static GList *
gtk_recent_chooser_menu_get_items (GtkRecentChooser *chooser)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (chooser);
GtkRecentChooserMenuPrivate *priv;
gint limit;
GtkRecentSortType sort_type;
GList *items;
GCompareDataFunc compare_func;
gint length;
priv = menu->priv;
if (!priv->manager)
return NULL;
limit = gtk_recent_chooser_get_limit (chooser);
sort_type = gtk_recent_chooser_get_sort_type (chooser);
switch (sort_type)
{
case GTK_RECENT_SORT_NONE:
compare_func = NULL;
break;
case GTK_RECENT_SORT_MRU:
compare_func = (GCompareDataFunc) sort_recent_items_mru;
break;
case GTK_RECENT_SORT_LRU:
compare_func = (GCompareDataFunc) sort_recent_items_lru;
break;
case GTK_RECENT_SORT_CUSTOM:
compare_func = (GCompareDataFunc) sort_recent_items_proxy;
break;
default:
g_assert_not_reached ();
break;
}
items = gtk_recent_manager_get_items (priv->manager);
if (!items)
return NULL;
if (compare_func)
items = g_list_sort_with_data (items, compare_func, menu);
length = g_list_length (items);
if ((limit != -1) && (length > limit))
{
GList *clamp, *l;
clamp = g_list_nth (items, limit - 1);
l = clamp->next;
clamp->next = NULL;
g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
g_list_free (l);
}
return items;
}
static GtkRecentManager *
gtk_recent_chooser_menu_get_recent_manager (GtkRecentChooser *chooser)
{
GtkRecentChooserMenuPrivate *priv;
priv = GTK_RECENT_CHOOSER_MENU (chooser)->priv;
return priv->manager;
}
static void
gtk_recent_chooser_menu_add_filter (GtkRecentChooser *chooser,
GtkRecentFilter *filter)
{
GtkRecentChooserMenu *menu;
menu = GTK_RECENT_CHOOSER_MENU (chooser);
gtk_recent_chooser_menu_set_current_filter (menu, filter);
}
static void
gtk_recent_chooser_menu_remove_filter (GtkRecentChooser *chooser,
GtkRecentFilter *filter)
{
GtkRecentChooserMenu *menu;
menu = GTK_RECENT_CHOOSER_MENU (chooser);
if (filter == menu->priv->current_filter)
{
g_object_unref (menu->priv->current_filter);
menu->priv->current_filter = NULL;
g_object_notify (G_OBJECT (menu), "filter");
}
}
static GSList *
gtk_recent_chooser_menu_list_filters (GtkRecentChooser *chooser)
{
GtkRecentChooserMenu *menu;
GSList *retval = NULL;
menu = GTK_RECENT_CHOOSER_MENU (chooser);
if (menu->priv->current_filter)
retval = g_slist_prepend (retval, menu->priv->current_filter);
return retval;
}
static void
gtk_recent_chooser_menu_set_current_filter (GtkRecentChooserMenu *menu,
GtkRecentFilter *filter)
{
GtkRecentChooserMenuPrivate *priv;
priv = menu->priv;
if (priv->current_filter)
g_object_unref (G_OBJECT (priv->current_filter));
if (filter)
{
priv->current_filter = filter;
g_object_ref_sink (priv->current_filter);
}
g_object_notify (G_OBJECT (menu), "filter");
}
static gboolean
get_is_recent_filtered (GtkRecentChooserMenu *menu,
GtkRecentInfo *info)
{
GtkRecentChooserMenuPrivate *priv;
GtkRecentFilter *current_filter;
GtkRecentFilterInfo filter_info;
GtkRecentFilterFlags needed;
gboolean retval;
g_assert (info != NULL);
priv = menu->priv;
if (!priv->current_filter)
return FALSE;
current_filter = priv->current_filter;
needed = gtk_recent_filter_get_needed (current_filter);
filter_info.contains = GTK_RECENT_FILTER_URI | GTK_RECENT_FILTER_MIME_TYPE;
filter_info.uri = gtk_recent_info_get_uri (info);
filter_info.mime_type = gtk_recent_info_get_mime_type (info);
if (needed & GTK_RECENT_FILTER_DISPLAY_NAME)
{
filter_info.display_name = gtk_recent_info_get_display_name (info);
filter_info.contains |= GTK_RECENT_FILTER_DISPLAY_NAME;
}
else
filter_info.uri = NULL;
if (needed & GTK_RECENT_FILTER_APPLICATION)
{
filter_info.applications = (const gchar **) gtk_recent_info_get_applications (info, NULL);
filter_info.contains |= GTK_RECENT_FILTER_APPLICATION;
}
else
filter_info.applications = NULL;
if (needed & GTK_RECENT_FILTER_GROUP)
{
filter_info.groups = (const gchar **) gtk_recent_info_get_groups (info, NULL);
filter_info.contains |= GTK_RECENT_FILTER_GROUP;
}
else
filter_info.groups = NULL;
if (needed & GTK_RECENT_FILTER_AGE)
{
filter_info.age = gtk_recent_info_get_age (info);
filter_info.contains |= GTK_RECENT_FILTER_AGE;
}
else
filter_info.age = -1;
retval = gtk_recent_filter_filter (current_filter, &filter_info);
/* this we own */
if (filter_info.applications)
g_strfreev ((gchar **) filter_info.applications);
if (filter_info.groups)
g_strfreev ((gchar **) filter_info.groups);
return !retval;
}
/* taken from libeel/eel-strings.c */
static gchar *
escape_underscores (const gchar *string)
{
gint underscores;
const gchar *p;
gchar *q;
gchar *escaped;
if (!string)
return NULL;
underscores = 0;
for (p = string; *p != '\0'; p++)
underscores += (*p == '_');
if (underscores == 0)
return g_strdup (string);
escaped = g_new (char, strlen (string) + underscores + 1);
for (p = string, q = escaped; *p != '\0'; p++, q++)
{
/* Add an extra underscore. */
if (*p == '_')
*q++ = '_';
*q = *p;
}
*q = '\0';
return escaped;
}
static void
gtk_recent_chooser_menu_add_tip (GtkRecentChooserMenu *menu,
GtkRecentInfo *info,
GtkWidget *item)
{
GtkRecentChooserMenuPrivate *priv;
gchar *path, *tip_text;
g_assert (info != NULL);
g_assert (item != NULL);
priv = menu->priv;
if (!priv->tooltips)
return;
path = gtk_recent_info_get_uri_display (info);
tip_text = g_strdup_printf (_("Open '%s'"), path);
gtk_tooltips_set_tip (priv->tooltips,
item,
tip_text,
NULL);
g_free (path);
g_free (tip_text);
}
static GtkWidget *
gtk_recent_chooser_menu_create_item (GtkRecentChooserMenu *menu,
GtkRecentInfo *info,
gint count)
{
GtkRecentChooserMenuPrivate *priv;
gchar *label;
GtkWidget *item, *image;
GdkPixbuf *icon;
g_assert (info != NULL);
priv = menu->priv;
if (priv->show_numbers)
{
gchar *name, *escaped;
name = g_strdup (gtk_recent_info_get_display_name (info));
if (!name)
name = g_strdup (_("Unknown item"));
escaped = escape_underscores (name);
/* avoid clashing mnemonics */
if (count <= 10)
label = g_strdup_printf ("_%d. %s", count, escaped);
else
label = g_strdup_printf ("%d. %s", count, escaped);
item = gtk_image_menu_item_new_with_mnemonic (label);
g_free (escaped);
g_free (name);
}
else
{
label = g_strdup (gtk_recent_info_get_display_name (info));
item = gtk_image_menu_item_new_with_label (label);
}
if (priv->show_icons)
{
icon = gtk_recent_info_get_icon (info, priv->icon_size);
image = gtk_image_new_from_pixbuf (icon);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
}
g_signal_connect (item, "activate",
G_CALLBACK (item_activate_cb),
menu);
out:
g_free (label);
return item;
}
/* removes the items we own from the menu */
static void
gtk_recent_chooser_menu_dispose_items (GtkRecentChooserMenu *menu)
{
GList *children, *l;
children = gtk_container_get_children (GTK_CONTAINER (menu));
for (l = children; l != NULL; l = l->next)
{
GtkWidget *menu_item = GTK_WIDGET (l->data);
gint mark = 0;
/* check for our mark, in order to remove just the items we own */
mark = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item),
"gtk-recent-menu-mark"));
if (mark == 1)
{
GtkRecentInfo *info;
/* destroy the attached RecentInfo struct, if found */
info = g_object_get_data (G_OBJECT (menu_item), "gtk-recent-info");
if (info)
g_object_set_data_full (G_OBJECT (menu_item), "gtk-recent-info",
NULL, NULL);
/* and finally remove the item from the menu */
gtk_container_remove (GTK_CONTAINER (menu), menu_item);
}
}
g_list_free (children);
}
typedef struct
{
GList *items;
gint n_items;
gint loaded_items;
GtkRecentChooserMenu *menu;
} MenuPopulateData;
static gboolean
idle_populate_func (gpointer data)
{
MenuPopulateData *pdata;
GtkRecentChooserMenuPrivate *priv;
GtkRecentInfo *info;
gboolean retval;
GtkWidget *item;
GDK_THREADS_ENTER ();
pdata = (MenuPopulateData *) data;
priv = pdata->menu->priv;
priv->populate_id = 0;
if (!pdata->items)
{
pdata->items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (pdata->menu));
if (!pdata->items)
{
item = gtk_menu_item_new_with_label (_("No items found"));
gtk_widget_set_sensitive (item, FALSE);
/* we also mark this item, so that it gets removed when rebuilding
* the menu on the next map event
*/
g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
GINT_TO_POINTER (1));
gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
gtk_widget_show (item);
/* no items: add a placeholder menu */
GDK_THREADS_LEAVE ();
return FALSE;
}
/* reverse the list */
pdata->items = g_list_reverse (pdata->items);
pdata->n_items = g_list_length (pdata->items);
pdata->loaded_items = 0;
}
info = g_list_nth_data (pdata->items, pdata->loaded_items);
/* skip non-local items on request */
if (priv->local_only &&
!gtk_recent_info_is_local (info))
{
goto check_and_return;
}
/* skip private items on request */
if (!priv->show_private &&
gtk_recent_info_get_private_hint (info))
{
goto check_and_return;
}
/* skip non-existing items on request */
if (!priv->show_not_found &&
!gtk_recent_info_exists (info))
{
goto check_and_return;
}
/* filter items based on the currently set filter object */
if (get_is_recent_filtered (pdata->menu, info))
{
goto check_and_return;
}
item = gtk_recent_chooser_menu_create_item (pdata->menu,
info,
pdata->loaded_items);
if (!item)
goto check_and_return;
gtk_recent_chooser_menu_add_tip (pdata->menu, info, item);
/* FIXME
*
* We should really place our items taking into account user
* defined menu items; this would also remove the need of
* reverting the scan order.
*/
gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
gtk_widget_show (item);
/* mark the menu item as one of our own */
g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
GINT_TO_POINTER (1));
/* attach the RecentInfo object to the menu item, and own a reference
* to it, so that it will be destroyed with the menu item when it's
* not needed anymore.
*/
g_object_set_data_full (G_OBJECT (item), "gtk-recent-info",
gtk_recent_info_ref (info),
(GDestroyNotify) gtk_recent_info_unref);
check_and_return:
pdata->loaded_items += 1;
if (pdata->loaded_items == pdata->n_items)
{
g_list_foreach (pdata->items, (GFunc) gtk_recent_info_unref, NULL);
g_list_free (pdata->items);
retval = FALSE;
}
else
retval = TRUE;
GDK_THREADS_LEAVE ();
return retval;
}
static void
idle_populate_clean_up (gpointer data)
{
g_slice_free (MenuPopulateData, data);
}
static void
gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
{
MenuPopulateData *pdata;
if (menu->priv->populate_id)
return;
pdata = g_slice_new (MenuPopulateData);
pdata->items = NULL;
pdata->n_items = 0;
pdata->loaded_items = 0;
pdata->menu = menu;
menu->priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));
/* dispose our menu items first */
gtk_recent_chooser_menu_dispose_items (menu);
menu->priv->populate_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 30,
idle_populate_func,
pdata,
idle_populate_clean_up);
}
/* bounce activate signal from the recent menu item widget
* to the recent menu widget
*/
static void
item_activate_cb (GtkWidget *widget,
gpointer user_data)
{
GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (user_data);
_gtk_recent_chooser_item_activated (chooser);
}
/* we force a redraw if the manager changes when we are showing */
static void
manager_changed_cb (GtkRecentManager *manager,
gpointer user_data)
{
GtkRecentChooserMenu *menu = GTK_RECENT_CHOOSER_MENU (user_data);
gtk_recent_chooser_menu_populate (menu);
}
static void
set_recent_manager (GtkRecentChooserMenu *menu,
GtkRecentManager *manager)
{
if (menu->priv->manager)
{
g_signal_handler_disconnect (menu, menu->priv->manager_changed_id);
menu->priv->manager = NULL;
}
if (manager)
menu->priv->manager = manager;
else
menu->priv->manager = gtk_recent_manager_get_default ();
if (menu->priv->manager)
menu->priv->manager_changed_id = g_signal_connect (menu->priv->manager, "changed",
G_CALLBACK (manager_changed_cb),
menu);
/* (re)populate the menu */
gtk_recent_chooser_menu_populate (menu);
}
static gint
get_icon_size_for_widget (GtkWidget *widget)
{
GtkSettings *settings;
gint width, height;
if (gtk_widget_has_screen (widget))
settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
else
settings = gtk_settings_get_default ();
if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
&width, &height))
return MAX (width, height);
return FALLBACK_ICON_SIZE;
}
static void
gtk_recent_chooser_menu_set_show_tips (GtkRecentChooserMenu *menu,
gboolean show_tips)
{
if (menu->priv->show_tips == show_tips)
return;
g_assert (menu->priv->tooltips != NULL);
if (show_tips)
gtk_tooltips_enable (menu->priv->tooltips);
else
gtk_tooltips_disable (menu->priv->tooltips);
menu->priv->show_tips = show_tips;
}
/*
* Public API
*/
/**
* gtk_recent_chooser_menu_new:
*
* Creates a new #GtkRecentChooserMenu widget.
*
* This kind of widget shows the list of recently used resources as
* a menu, each item as a menu item. Each item inside the menu might
* have an icon, representing its MIME type, and a number, for mnemonic
* access.
*
* This widget implements the #GtkRecentChooser interface.
*
* This widget creates its own #GtkRecentManager object. See the
* gtk_recent_chooser_menu_new_for_manager() function to know how to create
* a #GtkRecentChooserMenu widget bound to another #GtkRecentManager object.
*
* Return value: a new #GtkRecentChooserMenu
*
* Since: 2.10
*/
GtkWidget *
gtk_recent_chooser_menu_new (void)
{
return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
"recent-manager", NULL,
NULL);
}
/**
* gtk_recent_chooser_menu_new_for_manager:
* @manager: a #GtkRecentManager
*
* Creates a new #GtkRecentChooserMenu widget using @manager as
* the underlying recently used resources manager.
*
* This is useful if you have implemented your own recent manager,
* or if you have a customized instance of a #GtkRecentManager
* object or if you wish to share a common #GtkRecentManager object
* among multiple #GtkRecentChooser widgets.
*
* Return value: a new #GtkRecentChooserMenu, bound to @manager.
*
* Since: 2.10
*/
GtkWidget *
gtk_recent_chooser_menu_new_for_manager (GtkRecentManager *manager)
{
g_return_val_if_fail (manager == NULL || GTK_IS_RECENT_MANAGER (manager), NULL);
return g_object_new (GTK_TYPE_RECENT_CHOOSER_MENU,
"recent-manager", manager,
NULL);
}
/**
* gtk_recent_chooser_menu_get_show_numbers:
* @menu: a #GtkRecentChooserMenu
*
* Returns the value set by gtk_recent_chooser_menu_set_show_numbers().
*
* Return value: %TRUE if numbers should be shown.
*
* Since: 2.10
*/
gboolean
gtk_recent_chooser_menu_get_show_numbers (GtkRecentChooserMenu *menu)
{
g_return_val_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu), FALSE);
return menu->priv->show_numbers;
}
/**
* gtk_recent_chooser_menu_set_show_numbers:
* @menu: a #GtkRecentChooserMenu
* @show_numbers: whether to show numbers
*
* Sets whether a number should be added to the items of @menu. The
* numbers are shown to provide a unique character for a mnemonic to
* be used inside the menu item's label. Only the first the items
* get a number to avoid clashes.
*
* Since: 2.10
*/
void
gtk_recent_chooser_menu_set_show_numbers (GtkRecentChooserMenu *menu,
gboolean show_numbers)
{
g_return_if_fail (GTK_IS_RECENT_CHOOSER_MENU (menu));
if (menu->priv->show_numbers == show_numbers)
return;
menu->priv->show_numbers = show_numbers;
g_object_notify (G_OBJECT (menu), "show-numbers");
}
#define __GTK_RECENT_CHOOSER_MENU_C__
#include "gtkaliasdef.c"
|