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
|
/* gtkcombo - combo widget for gtk+
* Copyright 1997 Paolo Molaro
*
* 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.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
/* Do NOT, I repeat, NOT, copy any of the code in this file.
* The code here relies on all sorts of internal details of GTK+
*/
#include <string.h>
#include "gtkarrow.h"
#include "gtklabel.h"
#include "gtklist.h"
#include "gtkentry.h"
#include "gtkeventbox.h"
#include "gtkbutton.h"
#include "gtklistitem.h"
#include "gtkscrolledwindow.h"
#include "gtkmain.h"
#include "gtksignal.h"
#include "gtkwindow.h"
#include "gdk/gdkkeysyms.h"
#include "gtkcombo.h"
#include "gtkframe.h"
#include "gtkintl.h"
const gchar *gtk_combo_string_key = "gtk-combo-string-value";
#define COMBO_LIST_MAX_HEIGHT (400)
#define EMPTY_LIST_HEIGHT (15)
enum {
PROP_0,
PROP_ENABLE_ARROW_KEYS,
PROP_ENABLE_ARROWS_ALWAYS,
PROP_CASE_SENSITIVE,
PROP_ALLOW_EMPTY,
PROP_VALUE_IN_LIST
};
static void gtk_combo_class_init (GtkComboClass *klass);
static void gtk_combo_init (GtkCombo *combo);
static void gtk_combo_realize (GtkWidget *widget);
static void gtk_combo_unrealize (GtkWidget *widget);
static void gtk_combo_destroy (GtkObject *combo);
static GtkListItem *gtk_combo_find (GtkCombo *combo);
static gchar * gtk_combo_func (GtkListItem *li);
static gint gtk_combo_focus_idle (GtkCombo *combo);
static gint gtk_combo_entry_focus_out (GtkEntry *entry,
GdkEventFocus *event,
GtkCombo *combo);
static void gtk_combo_get_pos (GtkCombo *combo,
gint *x,
gint *y,
gint *height,
gint *width);
static void gtk_combo_popup_list (GtkCombo *combo);
static void gtk_combo_activate (GtkWidget *widget,
GtkCombo *combo);
static gboolean gtk_combo_popup_button_press (GtkWidget *button,
GdkEventButton *event,
GtkCombo *combo);
static gboolean gtk_combo_popup_button_leave (GtkWidget *button,
GdkEventCrossing *event,
GtkCombo *combo);
static void gtk_combo_update_entry (GtkList *list,
GtkCombo *combo);
static void gtk_combo_update_list (GtkEntry *entry,
GtkCombo *combo);
static gint gtk_combo_button_press (GtkWidget *widget,
GdkEvent *event,
GtkCombo *combo);
static void gtk_combo_button_event_after (GtkWidget *widget,
GdkEvent *event,
GtkCombo *combo);
static gint gtk_combo_list_enter (GtkWidget *widget,
GdkEventCrossing *event,
GtkCombo *combo);
static gint gtk_combo_list_key_press (GtkWidget *widget,
GdkEventKey *event,
GtkCombo *combo);
static gint gtk_combo_entry_key_press (GtkEntry *widget,
GdkEventKey *event,
GtkCombo *combo);
static gint gtk_combo_window_key_press (GtkWidget *window,
GdkEventKey *event,
GtkCombo *combo);
static void gtk_combo_item_destroy (GtkObject *object);
static void gtk_combo_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_combo_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_combo_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static GtkHBoxClass *parent_class = NULL;
static void
gtk_combo_class_init (GtkComboClass * klass)
{
GObjectClass *gobject_class;
GtkObjectClass *oclass;
GtkWidgetClass *widget_class;
gobject_class = (GObjectClass *) klass;
parent_class = gtk_type_class (GTK_TYPE_HBOX);
oclass = (GtkObjectClass *) klass;
widget_class = (GtkWidgetClass *) klass;
gobject_class->set_property = gtk_combo_set_property;
gobject_class->get_property = gtk_combo_get_property;
g_object_class_install_property (gobject_class,
PROP_ENABLE_ARROW_KEYS,
g_param_spec_boolean ("enable_arrow_keys",
_("Enable arrow keys"),
_("Whether the arrow keys move through the list of items"),
TRUE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_ENABLE_ARROWS_ALWAYS,
g_param_spec_boolean ("enable_arrows_always",
_("Always enable arrows"),
_("Whether the arrow keys work, even if the entry contents are not in the list"),
TRUE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_CASE_SENSITIVE,
g_param_spec_boolean ("case_sensitive",
_("Case sensitive"),
_("Whether list item matching is case sensitive"),
FALSE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_ALLOW_EMPTY,
g_param_spec_boolean ("allow_empty",
_("Allow empty"),
_("Whether an empty value may be entered in this field"),
TRUE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_VALUE_IN_LIST,
g_param_spec_boolean ("value_in_list",
_("Value in list"),
_("Whether entered values must already be present in the list"),
FALSE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
oclass->destroy = gtk_combo_destroy;
widget_class->size_allocate = gtk_combo_size_allocate;
widget_class->realize = gtk_combo_realize;
widget_class->unrealize = gtk_combo_unrealize;
}
static void
gtk_combo_destroy (GtkObject *object)
{
GtkCombo *combo = GTK_COMBO (object);
if (combo->popwin)
{
gtk_widget_destroy (combo->popwin);
gtk_widget_unref (combo->popwin);
combo->popwin = NULL;
}
GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
static int
gtk_combo_entry_key_press (GtkEntry * entry, GdkEventKey * event, GtkCombo * combo)
{
GList *li;
/* completion */
if ((event->keyval == GDK_Tab ||
event->keyval == GDK_KP_Tab) &&
(event->state & GDK_MOD1_MASK))
{
GtkEditable *editable = GTK_EDITABLE (entry);
GCompletion * cmpl;
gchar* prefix;
gchar* nprefix = NULL;
gint pos;
if ( !GTK_LIST (combo->list)->children )
return FALSE;
gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
cmpl = g_completion_new ((GCompletionFunc)gtk_combo_func);
g_completion_add_items (cmpl, GTK_LIST (combo->list)->children);
pos = gtk_editable_get_position (editable);
prefix = gtk_editable_get_chars (editable, 0, pos);
g_completion_complete (cmpl, prefix, &nprefix);
if (nprefix && strlen (nprefix) > strlen (prefix))
{
gtk_editable_insert_text (editable, nprefix + pos,
strlen (nprefix) - strlen (prefix), &pos);
gtk_editable_set_position (editable, pos);
}
if (nprefix)
g_free (nprefix);
g_free (prefix);
g_completion_free (cmpl);
return TRUE;
}
if (!combo->use_arrows || !GTK_LIST (combo->list)->children)
return FALSE;
li = g_list_find (GTK_LIST (combo->list)->children, gtk_combo_find (combo));
if ((event->keyval == GDK_Up)
|| (event->keyval == GDK_KP_Up)
|| ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'p') || (event->keyval == 'P'))))
{
if (li)
li = li->prev;
if (!li && combo->use_arrows_always)
{
li = g_list_last (GTK_LIST (combo->list)->children);
}
if (li)
{
gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
return TRUE;
}
}
else if ((event->keyval == GDK_Down)
|| (event->keyval == GDK_KP_Down)
|| ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'n') || (event->keyval == 'N'))))
{
if (li)
li = li->next;
if (!li && combo->use_arrows_always)
{
li = GTK_LIST (combo->list)->children;
}
if (li)
{
gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
return TRUE;
}
}
return FALSE;
}
static int
gtk_combo_window_key_press (GtkWidget *window,
GdkEventKey *event,
GtkCombo *combo)
{
if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)
{
if (GTK_WIDGET_VISIBLE (combo->popwin))
{
gtk_widget_hide (combo->popwin);
if (GTK_WIDGET_HAS_GRAB (combo->popwin))
{
gtk_grab_remove (combo->popwin);
gdk_display_pointer_ungrab (gtk_widget_get_display (window),
event->time);
}
}
gtk_signal_emit_stop_by_name (GTK_OBJECT (window), "key_press_event");
return TRUE;
}
return FALSE;
}
static GtkListItem *
gtk_combo_find (GtkCombo * combo)
{
const gchar *text;
GtkListItem *found = NULL;
gchar *ltext;
gchar *compare_text;
GList *clist;
text = gtk_entry_get_text (GTK_ENTRY (combo->entry));
if (combo->case_sensitive)
compare_text = (gchar *)text;
else
compare_text = g_utf8_casefold (text, -1);
for (clist = GTK_LIST (combo->list)->children;
!found && clist;
clist = clist->next)
{
ltext = gtk_combo_func (GTK_LIST_ITEM (clist->data));
if (!ltext)
continue;
if (!combo->case_sensitive)
ltext = g_utf8_casefold (ltext, -1);
if (strcmp (ltext, compare_text) == 0)
found = clist->data;
if (!combo->case_sensitive)
g_free (ltext);
}
if (!combo->case_sensitive)
g_free (compare_text);
return found;
}
static gchar *
gtk_combo_func (GtkListItem * li)
{
GtkWidget *label;
gchar *ltext = NULL;
ltext = (gchar *) gtk_object_get_data (GTK_OBJECT (li), gtk_combo_string_key);
if (!ltext)
{
label = GTK_BIN (li)->child;
if (!label || !GTK_IS_LABEL (label))
return NULL;
gtk_label_get (GTK_LABEL (label), <ext);
}
return ltext;
}
static gint
gtk_combo_focus_idle (GtkCombo * combo)
{
if (combo)
{
GDK_THREADS_ENTER ();
gtk_widget_grab_focus (combo->entry);
GDK_THREADS_LEAVE ();
}
return FALSE;
}
static gint
gtk_combo_entry_focus_out (GtkEntry * entry, GdkEventFocus * event, GtkCombo * combo)
{
if (combo->value_in_list && !gtk_combo_find (combo))
{
GSource *focus_idle;
/* gdk_beep(); *//* this can be annoying */
if (combo->ok_if_empty && !strcmp (gtk_entry_get_text (entry), ""))
return FALSE;
#ifdef TEST
printf ("INVALID ENTRY: `%s'\n", gtk_entry_get_text (entry));
#endif
gtk_grab_add (GTK_WIDGET (combo));
/* this is needed because if we call gtk_widget_grab_focus()
it isn't guaranteed it's the *last* call before the main-loop,
so the focus can be lost anyway...
the signal_emit_stop doesn't seem to work either...
*/
focus_idle = g_idle_source_new ();
g_source_set_closure (focus_idle,
g_cclosure_new_object (G_CALLBACK (gtk_combo_focus_idle),
G_OBJECT (combo)));
g_source_attach (focus_idle, NULL);
/*gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "focus_out_event"); */
return TRUE;
}
return FALSE;
}
static void
gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
{
GtkBin *popwin;
GtkWidget *widget;
GtkScrolledWindow *popup;
gint real_height;
GtkRequisition list_requisition;
gboolean show_hscroll = FALSE;
gboolean show_vscroll = FALSE;
gint avail_height;
gint min_height;
gint alloc_width;
gint work_height;
gint old_height;
gint old_width;
widget = GTK_WIDGET (combo);
popup = GTK_SCROLLED_WINDOW (combo->popup);
popwin = GTK_BIN (combo->popwin);
gdk_window_get_origin (combo->entry->window, x, y);
real_height = MIN (combo->entry->requisition.height,
combo->entry->allocation.height);
*y += real_height;
avail_height = gdk_screen_get_height (gtk_widget_get_screen (widget)) - *y;
gtk_widget_size_request (combo->list, &list_requisition);
min_height = MIN (list_requisition.height,
popup->vscrollbar->requisition.height);
if (!GTK_LIST (combo->list)->children)
list_requisition.height += EMPTY_LIST_HEIGHT;
alloc_width = (widget->allocation.width -
2 * popwin->child->style->xthickness -
2 * GTK_CONTAINER (popwin->child)->border_width -
2 * GTK_CONTAINER (combo->popup)->border_width -
2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width -
2 * GTK_BIN (popup)->child->style->xthickness);
work_height = (2 * popwin->child->style->ythickness +
2 * GTK_CONTAINER (popwin->child)->border_width +
2 * GTK_CONTAINER (combo->popup)->border_width +
2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
2 * GTK_BIN (popup)->child->style->xthickness);
do
{
old_width = alloc_width;
old_height = work_height;
if (!show_hscroll &&
alloc_width < list_requisition.width)
{
work_height += (popup->hscrollbar->requisition.height +
GTK_SCROLLED_WINDOW_GET_CLASS (combo->popup)->scrollbar_spacing);
show_hscroll = TRUE;
}
if (!show_vscroll &&
work_height + list_requisition.height > avail_height)
{
if (work_height + min_height > avail_height &&
*y - real_height > avail_height)
{
*y -= (work_height + list_requisition.height + real_height);
break;
}
alloc_width -= (popup->vscrollbar->requisition.width +
GTK_SCROLLED_WINDOW_GET_CLASS (combo->popup)->scrollbar_spacing);
show_vscroll = TRUE;
}
} while (old_width != alloc_width || old_height != work_height);
*width = widget->allocation.width;
if (show_vscroll)
*height = avail_height;
else
*height = work_height + list_requisition.height;
if (*x < 0)
*x = 0;
}
static void
gtk_combo_popup_list (GtkCombo * combo)
{
gint height, width, x, y;
gint old_width, old_height;
old_width = combo->popwin->allocation.width;
old_height = combo->popwin->allocation.height;
gtk_combo_get_pos (combo, &x, &y, &height, &width);
/* workaround for gtk_scrolled_window_size_allocate bug */
if (old_width != width || old_height != height)
{
gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
}
gtk_window_move (GTK_WINDOW (combo->popwin), x, y);
gtk_widget_set_usize (combo->popwin, width, height);
gtk_widget_show (combo->popwin);
gtk_widget_grab_focus (combo->popwin);
}
static void
gtk_combo_popdown_list (GtkCombo *combo)
{
combo->current_button = 0;
if (GTK_BUTTON (combo->button)->in_button)
{
GTK_BUTTON (combo->button)->in_button = FALSE;
gtk_button_released (GTK_BUTTON (combo->button));
}
if (GTK_WIDGET_HAS_GRAB (combo->popwin))
{
gtk_grab_remove (combo->popwin);
gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
GDK_CURRENT_TIME);
}
gtk_widget_hide (combo->popwin);
}
static void
gtk_combo_activate (GtkWidget *widget,
GtkCombo *combo)
{
gtk_combo_popup_list (combo);
if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
gtk_widget_grab_focus (combo->entry);
gtk_grab_add (combo->popwin);
gdk_pointer_grab (combo->popwin->window, TRUE,
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK,
NULL, NULL, GDK_CURRENT_TIME);
}
static gboolean
gtk_combo_popup_button_press (GtkWidget *button,
GdkEventButton *event,
GtkCombo *combo)
{
if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
gtk_widget_grab_focus (combo->entry);
if (event->button != 1)
return FALSE;
combo->current_button = event->button;
gtk_combo_popup_list (combo);
gtk_button_pressed (GTK_BUTTON (button));
gtk_grab_add (combo->popwin);
gdk_pointer_grab (combo->popwin->window, TRUE,
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK,
NULL, NULL, GDK_CURRENT_TIME);
GTK_LIST (combo->list)->drag_selection = TRUE;
gtk_grab_add (combo->list);
return TRUE;
}
static gboolean
gtk_combo_popup_button_leave (GtkWidget *button,
GdkEventCrossing *event,
GtkCombo *combo)
{
/* The idea here is that we want to keep the button down if the
* popup is popped up.
*/
return combo->current_button != 0;
}
static void
gtk_combo_update_entry (GtkList * list, GtkCombo * combo)
{
char *text;
gtk_grab_remove (GTK_WIDGET (combo));
gtk_signal_handler_block (GTK_OBJECT (list), combo->list_change_id);
if (list->selection)
{
text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
if (!text)
text = "";
gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
}
gtk_signal_handler_unblock (GTK_OBJECT (list), combo->list_change_id);
}
static void
gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
{
GtkList *list = GTK_LIST (combo->list);
GList *slist = list->selection;
GtkListItem *li;
gtk_grab_remove (GTK_WIDGET (combo));
gtk_signal_handler_block (GTK_OBJECT (entry), combo->entry_change_id);
if (slist && slist->data)
gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
li = gtk_combo_find (combo);
if (li)
gtk_list_select_child (list, GTK_WIDGET (li));
gtk_signal_handler_unblock (GTK_OBJECT (entry), combo->entry_change_id);
}
static gint
gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
{
GtkWidget *child;
child = gtk_get_event_widget (event);
/* We don't ask for button press events on the grab widget, so
* if an event is reported directly to the grab widget, it must
* be on a window outside the application (and thus we remove
* the popup window). Otherwise, we check if the widget is a child
* of the grab widget, and only remove the popup window if it
* is not.
*/
if (child != widget)
{
while (child)
{
if (child == widget)
return FALSE;
child = child->parent;
}
}
gtk_combo_popdown_list (combo);
return TRUE;
}
static void
gtk_combo_button_event_after (GtkWidget *widget,
GdkEvent *event,
GtkCombo *combo)
{
GtkWidget *child;
if (event->type != GDK_BUTTON_RELEASE)
return;
if ((combo->current_button != 0) && (event->button.button == 1))
{
/* This was the initial button press */
combo->current_button = 0;
/* Check to see if we released inside the button */
child = gtk_get_event_widget ((GdkEvent*) event);
while (child && child != (combo->button))
child = child->parent;
if (child == combo->button)
{
gtk_grab_add (combo->popwin);
gdk_pointer_grab (combo->popwin->window, TRUE,
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK,
NULL, NULL, GDK_CURRENT_TIME);
return;
}
}
gtk_combo_popdown_list (combo);
}
static gint
gtk_combo_list_enter (GtkWidget *widget,
GdkEventCrossing *event,
GtkCombo *combo)
{
GtkWidget *event_widget;
event_widget = gtk_get_event_widget ((GdkEvent*) event);
if ((event_widget == combo->list) &&
(combo->current_button != 0) &&
(!GTK_WIDGET_HAS_GRAB (combo->list)))
{
GdkEvent tmp_event;
gint x, y;
GdkModifierType mask;
gtk_grab_remove (combo->popwin);
/* Transfer the grab over to the list by synthesizing
* a button press event
*/
gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
tmp_event.button.type = GDK_BUTTON_PRESS;
tmp_event.button.window = combo->list->window;
tmp_event.button.send_event = TRUE;
tmp_event.button.time = GDK_CURRENT_TIME; /* bad */
tmp_event.button.x = x;
tmp_event.button.y = y;
/* We leave all the XInput fields unfilled here, in the expectation
* that GtkList doesn't care.
*/
tmp_event.button.button = combo->current_button;
tmp_event.button.state = mask;
gtk_widget_event (combo->list, &tmp_event);
}
return FALSE;
}
static int
gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
{
if (event->keyval == GDK_Escape)
{
if (GTK_WIDGET_HAS_GRAB (combo->list))
gtk_list_end_drag_selection (GTK_LIST (combo->list));
gtk_combo_popdown_list (combo);
return TRUE;
}
return FALSE;
}
static void
combo_event_box_realize (GtkWidget *widget)
{
GdkCursor *cursor = gdk_cursor_new_for_screen (gtk_widget_get_screen (widget),
GDK_TOP_LEFT_ARROW);
gdk_window_set_cursor (widget->window, cursor);
gdk_cursor_destroy (cursor);
}
static void
gtk_combo_init (GtkCombo * combo)
{
GtkWidget *arrow;
GtkWidget *frame;
GtkWidget *event_box;
combo->case_sensitive = FALSE;
combo->value_in_list = FALSE;
combo->ok_if_empty = TRUE;
combo->use_arrows = TRUE;
combo->use_arrows_always = FALSE;
combo->entry = gtk_entry_new ();
combo->button = gtk_button_new ();
combo->current_button = 0;
arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
gtk_widget_show (arrow);
gtk_container_add (GTK_CONTAINER (combo->button), arrow);
gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
gtk_widget_show (combo->entry);
gtk_widget_show (combo->button);
combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
(GtkSignalFunc) gtk_combo_update_list, combo);
gtk_signal_connect (GTK_OBJECT (combo->entry), "key_press_event",
(GtkSignalFunc) gtk_combo_entry_key_press, combo);
gtk_signal_connect_after (GTK_OBJECT (combo->entry), "focus_out_event",
(GtkSignalFunc) gtk_combo_entry_focus_out, combo);
combo->activate_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "activate",
(GtkSignalFunc) gtk_combo_activate, combo);
gtk_signal_connect (GTK_OBJECT (combo->button), "button_press_event",
(GtkSignalFunc) gtk_combo_popup_button_press, combo);
gtk_signal_connect (GTK_OBJECT (combo->button), "leave_notify_event",
(GtkSignalFunc) gtk_combo_popup_button_leave, combo);
combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
gtk_widget_ref (combo->popwin);
gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
gtk_signal_connect (GTK_OBJECT (combo->popwin), "key_press_event",
GTK_SIGNAL_FUNC (gtk_combo_window_key_press), combo);
gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
g_signal_connect (event_box, "realize",
G_CALLBACK (combo_event_box_realize), NULL);
gtk_widget_show (event_box);
frame = gtk_frame_new (NULL);
gtk_container_add (GTK_CONTAINER (event_box), frame);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
gtk_widget_show (frame);
combo->popup = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
gtk_container_add (GTK_CONTAINER (frame), combo->popup);
gtk_widget_show (combo->popup);
combo->list = gtk_list_new ();
/* We'll use enter notify events to figure out when to transfer
* the grab to the list
*/
gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
gtk_widget_show (combo->list);
combo->list_change_id = gtk_signal_connect (GTK_OBJECT (combo->list), "selection_changed",
(GtkSignalFunc) gtk_combo_update_entry, combo);
gtk_signal_connect (GTK_OBJECT (combo->popwin), "key_press_event",
(GtkSignalFunc) gtk_combo_list_key_press, combo);
gtk_signal_connect (GTK_OBJECT (combo->popwin), "button_press_event",
GTK_SIGNAL_FUNC (gtk_combo_button_press), combo);
gtk_signal_connect (GTK_OBJECT (combo->list), "event_after",
(GtkSignalFunc) gtk_combo_button_event_after, combo);
/* We connect here on the button, because we'll have a grab on it
* when the event occurs. But we are actually interested in enters
* for the combo->list.
*/
gtk_signal_connect (GTK_OBJECT (combo->button), "enter_notify_event",
GTK_SIGNAL_FUNC (gtk_combo_list_enter), combo);
}
static void
gtk_combo_realize (GtkWidget *widget)
{
GtkCombo *combo = GTK_COMBO (widget);
gtk_window_set_screen (GTK_WINDOW (combo->popwin),
gtk_widget_get_screen (widget));
GTK_WIDGET_CLASS( parent_class )->realize (widget);
}
static void
gtk_combo_unrealize (GtkWidget *widget)
{
GtkCombo *combo = GTK_COMBO (widget);
gtk_combo_popdown_list (combo);
gtk_widget_unrealize (combo->popwin);
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}
GtkType
gtk_combo_get_type (void)
{
static GtkType combo_type = 0;
if (!combo_type)
{
static const GtkTypeInfo combo_info =
{
"GtkCombo",
sizeof (GtkCombo),
sizeof (GtkComboClass),
(GtkClassInitFunc) gtk_combo_class_init,
(GtkObjectInitFunc) gtk_combo_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL,
};
combo_type = gtk_type_unique (GTK_TYPE_HBOX, &combo_info);
}
return combo_type;
}
GtkWidget*
gtk_combo_new (void)
{
return GTK_WIDGET (gtk_type_new (GTK_TYPE_COMBO));
}
void
gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
{
g_return_if_fail (GTK_IS_COMBO (combo));
val = val != FALSE;
ok_if_empty = ok_if_empty != FALSE;
g_object_freeze_notify (G_OBJECT (combo));
if (combo->value_in_list != val)
{
combo->value_in_list = val;
g_object_notify (G_OBJECT (combo), "value_in_list");
}
if (combo->ok_if_empty != ok_if_empty)
{
combo->ok_if_empty = ok_if_empty;
g_object_notify (G_OBJECT (combo), "allow_empty");
}
g_object_thaw_notify (G_OBJECT (combo));
}
void
gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
{
g_return_if_fail (GTK_IS_COMBO (combo));
val = val != FALSE;
if (combo->case_sensitive != val)
{
combo->case_sensitive = val;
g_object_notify (G_OBJECT (combo), "case_sensitive");
}
}
void
gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
{
g_return_if_fail (GTK_IS_COMBO (combo));
val = val != FALSE;
if (combo->use_arrows != val)
{
combo->use_arrows = val;
g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
}
}
void
gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
{
g_return_if_fail (GTK_IS_COMBO (combo));
val = val != FALSE;
if (combo->use_arrows_always != val)
{
g_object_freeze_notify (G_OBJECT (combo));
combo->use_arrows_always = val;
g_object_notify (G_OBJECT (combo), "enable_arrows_always");
if (combo->use_arrows != TRUE)
{
combo->use_arrows = TRUE;
g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
}
g_object_thaw_notify (G_OBJECT (combo));
}
}
void
gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
{
GList *list;
GtkWidget *li;
g_return_if_fail (GTK_IS_COMBO (combo));
g_return_if_fail (strings != NULL);
gtk_combo_popdown_list (combo);
gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
list = strings;
while (list)
{
li = gtk_list_item_new_with_label ((gchar *) list->data);
gtk_widget_show (li);
gtk_container_add (GTK_CONTAINER (combo->list), li);
list = list->next;
}
}
static void
gtk_combo_item_destroy (GtkObject * object)
{
gchar *key;
key = gtk_object_get_data (object, gtk_combo_string_key);
if (key)
{
gtk_object_remove_data (object, gtk_combo_string_key);
g_free (key);
}
}
void
gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
{
gchar *val;
gint connected = 0;
g_return_if_fail (GTK_IS_COMBO (combo));
g_return_if_fail (item != NULL);
val = gtk_object_get_data (GTK_OBJECT (item), gtk_combo_string_key);
if (val)
{
g_free (val);
connected = 1;
}
if (item_value)
{
val = g_strdup (item_value);
gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, val);
if (!connected)
gtk_signal_connect (GTK_OBJECT (item), "destroy",
(GtkSignalFunc) gtk_combo_item_destroy, val);
}
else
{
gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, NULL);
if (connected)
gtk_signal_disconnect_by_data(GTK_OBJECT (item), val);
}
}
static void
gtk_combo_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkCombo *combo;
g_return_if_fail (GTK_IS_COMBO (widget));
g_return_if_fail (allocation != NULL);
GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
combo = GTK_COMBO (widget);
if (combo->entry->allocation.height > combo->entry->requisition.height)
{
GtkAllocation button_allocation;
button_allocation = combo->button->allocation;
button_allocation.height = combo->entry->requisition.height;
button_allocation.y = combo->entry->allocation.y +
(combo->entry->allocation.height - combo->entry->requisition.height)
/ 2;
gtk_widget_size_allocate (combo->button, &button_allocation);
}
}
void
gtk_combo_disable_activate (GtkCombo* combo)
{
g_return_if_fail (GTK_IS_COMBO (combo));
if ( combo->activate_id ) {
gtk_signal_disconnect (GTK_OBJECT(combo->entry), combo->activate_id);
combo->activate_id = 0;
}
}
static void
gtk_combo_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkCombo *combo = GTK_COMBO (object);
switch (prop_id)
{
case PROP_ENABLE_ARROW_KEYS:
gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
break;
case PROP_ENABLE_ARROWS_ALWAYS:
gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
break;
case PROP_CASE_SENSITIVE:
gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
break;
case PROP_ALLOW_EMPTY:
combo->ok_if_empty = g_value_get_boolean (value);
break;
case PROP_VALUE_IN_LIST:
combo->value_in_list = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_combo_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkCombo *combo = GTK_COMBO (object);
switch (prop_id)
{
case PROP_ENABLE_ARROW_KEYS:
g_value_set_boolean (value, combo->use_arrows);
break;
case PROP_ENABLE_ARROWS_ALWAYS:
g_value_set_boolean (value, combo->use_arrows_always);
break;
case PROP_CASE_SENSITIVE:
g_value_set_boolean (value, combo->case_sensitive);
break;
case PROP_ALLOW_EMPTY:
g_value_set_boolean (value, combo->ok_if_empty);
break;
case PROP_VALUE_IN_LIST:
g_value_set_boolean (value, combo->value_in_list);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
|