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
|
/* vim:set sw=4 expandtab cino=(0:
*
* This file is a part of hildon
*
* Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
*
* Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
*
* 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; version 2.1 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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**
* SECTION:hildon-date-editor
* @short_description: A widget which queries a date from user or opens
* a HildonCalendarPopup.
* @see_also: #HildonCalendarPopup, #HildonTimeEditor
*
* HildonDateEditor is a widget with three entry fields (day, month,
* year) and an icon (button): clicking on the icon opens up a
* HildonCalendarPopup.
*
* <example>
* <programlisting>
* guint y, m, d;
* GtkDialog *dialog;
* GtkWidget *date_editor;
* <!-- -->
* dialog = GTK_DIALOG (gtk_dialog_new ());
* date_editor = hildon_date_editor_new ();
* <!-- -->
* gtk_box_pack_start (GTK_BOX (dialog->vbox), gtk_label_new ("Choose a date"), FALSE, FALSE, 10);
* gtk_box_pack_start (GTK_BOX (dialog->vbox), date_editor, FALSE, FALSE, 10);
* gtk_dialog_add_button (dialog, "Close", GTK_RESPONSE_CANCEL);
* <!-- -->
* gtk_widget_show_all (GTK_WIDGET (dialog));
* gtk_dialog_run (dialog);
* <!-- -->
* hildon_date_editor_get_date (HILDON_DATE_EDITOR (date_editor), &y, &m, &d);
* g_debug ("Date: %u-%u-%u", y, m, d);
* <!-- -->
* </programlisting>
* </example>
*
*/
#undef HILDON_DISABLE_DEPRECATED
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libintl.h>
#include <gdk/gdkkeysyms.h>
#include "hildon-date-editor.h"
#include "hildon-calendar-popup.h"
#include "hildon-defines.h"
#include "hildon-marshalers.h"
#include "hildon-enum-types.h"
#include "hildon-time-editor.h"
#include "hildon-banner.h"
#include "hildon-date-editor-private.h"
#include "hildon-private.h"
#define _(string) dgettext("hildon-libs", string)
#define c_(string) dgettext("hildon-common-strings", string)
#define ENTRY_BORDERS 11
#define DATE_EDITOR_HEIGHT 30
#define DAY_ENTRY_WIDTH 2
#define MONTH_ENTRY_WIDTH 2
#define YEAR_ENTRY_WIDTH 4
#define DEFAULT_MIN_YEAR 1970
#define DEFAULT_MAX_YEAR 2037
static GtkContainerClass* parent_class;
static void
hildon_date_editor_class_init (HildonDateEditorClass *editor_class);
static void
hildon_date_editor_init (HildonDateEditor *editor);
static gboolean
hildon_date_editor_icon_press (GtkWidget *widget,
gpointer data);
static gboolean
hildon_date_editor_released (GtkWidget *widget,
gpointer data);
static gboolean
hildon_date_editor_keypress (GtkWidget *widget,
GdkEventKey *event,
gpointer data);
static gboolean
hildon_date_editor_keyrelease (GtkWidget *widget,
GdkEventKey *event,
gpointer data);
static gboolean
hildon_date_editor_clicked (GtkWidget *widget,
gpointer data);
static gint
hildon_date_editor_entry_validate (GtkWidget *widget,
gpointer data);
static void
hildon_date_editor_entry_changed (GtkEditable *widget,
gpointer data);
static gboolean
hildon_date_editor_entry_focus_out (GtkWidget *widget,
GdkEventFocus *event,
gpointer data);
static gboolean
hildon_date_editor_date_error (HildonDateEditor *editor,
HildonDateTimeError type);
static gboolean
hildon_date_editor_entry_focus_in (GtkWidget *widget,
GdkEventFocus *event,
gpointer data);
static void
hildon_date_editor_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void
hildon_date_editor_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void
hildon_child_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data);
static void
hildon_date_editor_destroy (GtkObject *self);
static void
hildon_date_editor_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void
hildon_date_editor_size_request (GtkWidget *widget,
GtkRequisition *requisition);
static gboolean
hildon_date_editor_focus (GtkWidget *widget,
GtkDirectionType direction);
static gboolean
hildon_date_editor_entry_select_all (GtkWidget *widget);
/* Property indices */
enum
{
PROP_0,
PROP_DAY,
PROP_MONTH,
PROP_YEAR,
PROP_MIN_YEAR,
PROP_MAX_YEAR
};
enum
{
DATE_ERROR,
LAST_SIGNAL
};
static guint date_editor_signals[LAST_SIGNAL] = { 0 };
/**
* hildon_date_editor_get_type:
*
* Initializes and returns the type of a hildon date editor.
*
* Returns: GType of #HildonDateEditor
*/
GType G_GNUC_CONST
hildon_date_editor_get_type (void)
{
static GType editor_type = 0;
if (! editor_type) {
static const GTypeInfo editor_info = {
sizeof (HildonDateEditorClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) hildon_date_editor_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (HildonDateEditor),
0, /* n_preallocs */
(GInstanceInitFunc) hildon_date_editor_init,
};
editor_type = g_type_register_static (GTK_TYPE_CONTAINER,
"HildonDateEditor",
&editor_info, 0);
}
return editor_type;
}
static void
hildon_date_editor_class_init (HildonDateEditorClass *editor_class)
{
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (editor_class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (editor_class);
GObjectClass *gobject_class = G_OBJECT_CLASS (editor_class);
parent_class = g_type_class_peek_parent (editor_class);
g_type_class_add_private (editor_class, sizeof (HildonDateEditorPrivate));
gobject_class->set_property = hildon_date_editor_set_property;
gobject_class->get_property = hildon_date_editor_get_property;
widget_class->size_request = hildon_date_editor_size_request;
widget_class->size_allocate = hildon_date_editor_size_allocate;
widget_class->focus = hildon_date_editor_focus;
container_class->forall = hildon_child_forall;
GTK_OBJECT_CLASS(editor_class)->destroy = hildon_date_editor_destroy;
editor_class->date_error = (gpointer) hildon_date_editor_date_error;
date_editor_signals[DATE_ERROR] =
g_signal_new ("date-error",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (HildonDateEditorClass, date_error),
g_signal_accumulator_true_handled, NULL,
_hildon_marshal_BOOLEAN__ENUM,
G_TYPE_BOOLEAN, 1, HILDON_TYPE_DATE_TIME_ERROR);
/**
* HildonDateEditor:year:
*
* Current year.
*/
g_object_class_install_property (gobject_class, PROP_YEAR,
g_param_spec_uint ("year",
"Current year",
"Current year",
1, 10000,
2007,
G_PARAM_READABLE | G_PARAM_WRITABLE));
/**
* HildonDateEditor:month:
*
* Current month.
*/
g_object_class_install_property (gobject_class, PROP_MONTH,
g_param_spec_uint ("month",
"Current month",
"Current month",
1, 12,
1,
G_PARAM_READABLE | G_PARAM_WRITABLE));
/**
* HildonDateEditor:day:
*
* Current day.
*/
g_object_class_install_property (gobject_class, PROP_DAY,
g_param_spec_uint ("day",
"Current day",
"Current day",
1, 31,
1,
G_PARAM_READABLE | G_PARAM_WRITABLE));
/**
* HildonDateEditor:min-year:
*
* Minimum valid year.
*/
g_object_class_install_property (gobject_class, PROP_MIN_YEAR,
g_param_spec_uint ("min-year",
"Minimum valid year",
"Minimum valid year",
1, 10000,
DEFAULT_MIN_YEAR,
G_PARAM_READWRITE));
/**
* HildonDateEditor:max-year:
*
* Maximum valid year.
*/
g_object_class_install_property (gobject_class, PROP_MAX_YEAR,
g_param_spec_uint ("max-year",
"Maximum valid year",
"Maximum valid year",
1, 10000,
DEFAULT_MAX_YEAR,
G_PARAM_READWRITE));
}
/* Forces setting of the icon to certain state. Used initially
and from the actual setter function */
static void
real_set_calendar_icon_state (HildonDateEditorPrivate *priv,
gboolean pressed)
{
g_assert (priv);
gtk_image_set_from_icon_name (GTK_IMAGE (priv->calendar_icon),
pressed ? "qgn_widg_datedit_pr" : "qgn_widg_datedit", HILDON_ICON_SIZE_SMALL);
priv->calendar_icon_pressed = pressed;
}
/* Sets the icon to given state (normal/pressed). Returns
info if the state actually changed. */
static gboolean
hildon_date_editor_set_calendar_icon_state (HildonDateEditor *editor,
gboolean pressed)
{
HildonDateEditorPrivate *priv;
g_assert (HILDON_IS_DATE_EDITOR (editor));
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (pressed != priv->calendar_icon_pressed) {
real_set_calendar_icon_state (priv, pressed);
return TRUE;
}
return FALSE;
}
/* Packing day, month and year entries depend on locale settings
We find out the order and all separators by converting a known
date to default format and inspecting the result string */
static void
apply_locale_field_order (HildonDateEditorPrivate *priv)
{
GDate locale_test_date;
GtkWidget *delim;
gchar buffer[256];
gchar *iter, *delim_text;
g_date_set_dmy (&locale_test_date, 1, 2, 1970);
(void) g_date_strftime (buffer, sizeof (buffer), "%x", &locale_test_date);
iter = buffer;
while (*iter)
{
gchar *endp;
unsigned long value;
/* Try to convert the current location into number. */
value = strtoul (iter, &endp, 10);
/* If the conversion didn't progress or the detected value was
unknown (we used a fixed date, you remember), we treat
current position as a literal */
switch (value)
{
case 1:
gtk_box_pack_start (GTK_BOX (priv->d_box_date),
priv->d_entry, FALSE, FALSE, 0);
break;
case 2:
gtk_box_pack_start (GTK_BOX (priv->d_box_date),
priv->m_entry, FALSE, FALSE, 0);
break;
case 70: /* %x format uses only 2 numbers for some locales */
case 1970:
gtk_box_pack_start (GTK_BOX (priv->d_box_date),
priv->y_entry, FALSE, FALSE, 0);
break;
default:
/* All non-number characters starting from current position
form the delimeter */
for (endp = iter; *endp; endp++)
if (g_ascii_isdigit (*endp))
break;
/* Now endp points one place past the delimeter text */
delim_text = g_strndup (iter, endp - iter);
delim = gtk_label_new (delim_text);
gtk_box_pack_start (GTK_BOX (priv->d_box_date),
delim, FALSE, FALSE, 0);
priv->delims = g_list_append (priv->delims, delim);
g_free(delim_text);
break;
};
iter = endp;
}
}
static void
hildon_date_editor_init (HildonDateEditor *editor)
{
HildonDateEditorPrivate *priv;
GDate cur_date;
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
GTK_WIDGET_SET_FLAGS (GTK_WIDGET (editor), GTK_NO_WINDOW);
gtk_widget_push_composite_child ();
/* initialize values */
g_date_clear (&cur_date, 1);
g_date_set_time (&cur_date, time (NULL));
priv->day = g_date_get_day (&cur_date);
priv->month = g_date_get_month (&cur_date);
priv->year = g_date_get_year (&cur_date);
priv->min_year = DEFAULT_MIN_YEAR;
priv->max_year = DEFAULT_MAX_YEAR;
/* make widgets */
priv->frame = gtk_frame_new (NULL);
gtk_container_set_border_width (GTK_CONTAINER (priv->frame), 0);
priv->d_entry = gtk_entry_new ();
priv->m_entry = gtk_entry_new ();
priv->y_entry = gtk_entry_new ();
#ifdef MAEMO_GTK
g_object_set (G_OBJECT(priv->d_entry), "hildon-input-mode",
HILDON_GTK_INPUT_MODE_NUMERIC, NULL);
g_object_set (G_OBJECT(priv->m_entry), "hildon-input-mode",
HILDON_GTK_INPUT_MODE_NUMERIC, NULL);
g_object_set (G_OBJECT(priv->y_entry), "hildon-input-mode",
HILDON_GTK_INPUT_MODE_NUMERIC, NULL);
#endif
/* set entry look */
gtk_entry_set_width_chars (GTK_ENTRY (priv->d_entry), DAY_ENTRY_WIDTH);
gtk_entry_set_width_chars (GTK_ENTRY (priv->m_entry), MONTH_ENTRY_WIDTH);
gtk_entry_set_width_chars (GTK_ENTRY (priv->y_entry), YEAR_ENTRY_WIDTH);
gtk_entry_set_max_length (GTK_ENTRY (priv->d_entry), DAY_ENTRY_WIDTH);
gtk_entry_set_max_length (GTK_ENTRY (priv->m_entry), MONTH_ENTRY_WIDTH);
gtk_entry_set_max_length (GTK_ENTRY (priv->y_entry), YEAR_ENTRY_WIDTH);
gtk_entry_set_has_frame (GTK_ENTRY (priv->d_entry), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY (priv->m_entry), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY (priv->y_entry), FALSE);
gtk_widget_set_composite_name (priv->d_entry, "day_entry");
gtk_widget_set_composite_name (priv->m_entry, "month_entry");
gtk_widget_set_composite_name (priv->y_entry, "year_entry");
priv->d_box_date = gtk_hbox_new (FALSE, 0);
priv->d_button_image = gtk_button_new ();
priv->calendar_icon = gtk_image_new ();
real_set_calendar_icon_state (priv, FALSE);
GTK_WIDGET_UNSET_FLAGS (priv->d_button_image, GTK_CAN_FOCUS | GTK_CAN_DEFAULT);
apply_locale_field_order (priv);
gtk_container_add (GTK_CONTAINER (priv->frame), priv->d_box_date);
gtk_container_add (GTK_CONTAINER (priv->d_button_image), priv->calendar_icon);
gtk_button_set_relief (GTK_BUTTON (priv->d_button_image), GTK_RELIEF_NONE);
gtk_button_set_focus_on_click (GTK_BUTTON (priv->d_button_image), FALSE);
gtk_widget_set_parent (priv->frame, GTK_WIDGET (editor));
gtk_widget_set_parent (priv->d_button_image, GTK_WIDGET (editor));
gtk_widget_show_all (priv->frame);
gtk_widget_show_all (priv->d_button_image);
/* image button signal connects */
g_signal_connect (GTK_OBJECT (priv->d_button_image), "pressed",
G_CALLBACK (hildon_date_editor_icon_press), editor);
g_signal_connect (GTK_OBJECT (priv->d_button_image), "released",
G_CALLBACK (hildon_date_editor_released), editor);
g_signal_connect (GTK_OBJECT (priv->d_button_image), "clicked",
G_CALLBACK (hildon_date_editor_clicked), editor);
g_signal_connect (GTK_OBJECT (priv->d_button_image), "key_press_event",
G_CALLBACK (hildon_date_editor_keypress), editor);
/* entry signal connects */
g_signal_connect (GTK_OBJECT (priv->d_entry), "focus-in-event",
G_CALLBACK (hildon_date_editor_entry_focus_in), editor);
g_signal_connect (GTK_OBJECT (priv->m_entry), "focus-in-event",
G_CALLBACK (hildon_date_editor_entry_focus_in), editor);
g_signal_connect (GTK_OBJECT (priv->y_entry), "focus-in-event",
G_CALLBACK (hildon_date_editor_entry_focus_in), editor);
g_signal_connect (GTK_OBJECT (priv->d_entry), "focus-out-event",
G_CALLBACK (hildon_date_editor_entry_focus_out), editor);
g_signal_connect (GTK_OBJECT (priv->m_entry), "focus-out-event",
G_CALLBACK (hildon_date_editor_entry_focus_out), editor);
g_signal_connect (GTK_OBJECT (priv->y_entry), "focus-out-event",
G_CALLBACK (hildon_date_editor_entry_focus_out), editor);
g_signal_connect (GTK_OBJECT (priv->d_entry), "key-press-event",
G_CALLBACK (hildon_date_editor_keypress), editor);
g_signal_connect (GTK_OBJECT (priv->m_entry), "key-press-event",
G_CALLBACK (hildon_date_editor_keypress), editor);
g_signal_connect (GTK_OBJECT (priv->y_entry), "key-press-event",
G_CALLBACK (hildon_date_editor_keypress), editor);
g_signal_connect (GTK_OBJECT (priv->d_entry), "key-release-event",
G_CALLBACK (hildon_date_editor_keyrelease), editor);
g_signal_connect(GTK_OBJECT(priv->m_entry), "key-release-event",
G_CALLBACK(hildon_date_editor_keyrelease), editor);
g_signal_connect (GTK_OBJECT (priv->y_entry), "key-release-event",
G_CALLBACK (hildon_date_editor_keyrelease), editor);
hildon_date_editor_set_date (editor, priv->year, priv->month, priv->day);
g_signal_connect (GTK_OBJECT (priv->d_entry), "changed",
G_CALLBACK (hildon_date_editor_entry_changed), editor);
g_signal_connect (GTK_OBJECT (priv->m_entry), "changed",
G_CALLBACK (hildon_date_editor_entry_changed), editor);
g_signal_connect (GTK_OBJECT (priv->y_entry), "changed",
G_CALLBACK (hildon_date_editor_entry_changed), editor);
gtk_widget_pop_composite_child ();
}
static void
hildon_date_editor_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
HildonDateEditor *editor = HILDON_DATE_EDITOR (object);
HildonDateEditorPrivate *priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
gint val;
g_assert (priv);
switch (param_id)
{
case PROP_YEAR:
hildon_date_editor_set_year (editor, g_value_get_uint (value));
break;
case PROP_MONTH:
hildon_date_editor_set_month (editor, g_value_get_uint (value));
break;
case PROP_DAY:
hildon_date_editor_set_day (editor, g_value_get_uint (value));
break;
case PROP_MIN_YEAR:
val = g_value_get_uint (value);
priv->min_year = val;
/* Clamp current year */
if (hildon_date_editor_get_year (editor) < priv->min_year)
hildon_date_editor_set_year (editor, priv->min_year);
break;
case PROP_MAX_YEAR:
val = g_value_get_uint (value);
priv->max_year = val;
/* Clamp current year */
if (hildon_date_editor_get_year (editor) > priv->max_year)
hildon_date_editor_set_year (editor, priv->max_year);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
hildon_date_editor_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
HildonDateEditor *editor = HILDON_DATE_EDITOR (object);
HildonDateEditorPrivate *priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
switch (param_id)
{
case PROP_YEAR:
g_value_set_uint (value, hildon_date_editor_get_year (editor));
break;
case PROP_MONTH:
g_value_set_uint (value, hildon_date_editor_get_month (editor));
break;
case PROP_DAY:
g_value_set_uint (value, hildon_date_editor_get_day (editor));
break;
case PROP_MIN_YEAR:
g_value_set_uint (value, priv->min_year);
break;
case PROP_MAX_YEAR:
g_value_set_uint (value, priv->max_year);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
break;
}
}
static void
hildon_child_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
HildonDateEditor *editor;
HildonDateEditorPrivate *priv;
g_assert (HILDON_IS_DATE_EDITOR (container));
g_assert (callback);
editor = HILDON_DATE_EDITOR (container);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (include_internals) {
(*callback) (priv->frame, callback_data);
(*callback) (priv->d_button_image, callback_data);
}
}
static void
hildon_date_editor_destroy (GtkObject *self)
{
HildonDateEditorPrivate *priv;
priv = HILDON_DATE_EDITOR_GET_PRIVATE (self);
g_assert (priv);
if (priv->frame) {
gtk_widget_unparent (priv->frame);
priv->frame = NULL;
}
if (priv->d_button_image) {
gtk_widget_unparent (priv->d_button_image);
priv->d_button_image = NULL;
}
if (priv->delims) {
g_list_free (priv->delims);
priv->delims = NULL;
}
if (GTK_OBJECT_CLASS (parent_class)->destroy)
GTK_OBJECT_CLASS (parent_class)->destroy (self);
}
/**
* hildon_date_editor_new:
*
* Creates a new date editor. The current system date
* is shown in the editor.
*
* Returns: pointer to a new @HildonDateEditor widget.
*/
GtkWidget*
hildon_date_editor_new (void)
{
return (GtkWidget *) g_object_new (HILDON_TYPE_DATE_EDITOR, NULL);
}
/**
* hildon_date_editor_set_date:
* @date: the @HildonDateEditor widget
* @year: year
* @month: month
* @day: day
*
* Sets the date shown in the editor.
*/
void
hildon_date_editor_set_date (HildonDateEditor *editor,
guint year,
guint month,
guint day)
{
HildonDateEditorPrivate *priv;
g_return_if_fail (HILDON_IS_DATE_EDITOR (editor));
/* This function cannot be implemented by calling
component setters, since applying the individual
values one by one can make the date temporarily
invalid (depending on what the previous values were),
which in turn causes that the desired date
is not set (even though it's valid). We must set all the
components at one go and not try to do any validation etc
there in between. */
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (g_date_valid_dmy (day, month, year))
{
GDate date;
gchar buffer[256];
priv->year = year;
priv->month = month;
priv->day = day;
g_date_set_dmy (&date, day, month, year);
/* We apply the new values, but do not want automatic focus move
etc to take place */
g_snprintf (buffer, sizeof (buffer), "%04d", year);
g_signal_handlers_block_by_func (priv->y_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->y_entry), buffer);
g_signal_handlers_unblock_by_func (priv->y_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_date_strftime (buffer, sizeof (buffer), "%m", &date);
g_signal_handlers_block_by_func (priv->m_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->m_entry), buffer);
g_signal_handlers_unblock_by_func (priv->m_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_date_strftime (buffer, sizeof (buffer), "%d", &date);
g_signal_handlers_block_by_func (priv->d_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->d_entry), buffer);
g_signal_handlers_unblock_by_func (priv->d_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_object_notify (G_OBJECT (editor), "year");
g_object_notify (G_OBJECT (editor), "month");
g_object_notify (G_OBJECT (editor), "day");
}
}
/**
* hildon_date_editor_get_date:
* @date: the @HildonDateEditor widget
* @year: year
* @month: month
* @day: day
*
* Gets the date represented by the date editor.
* You can pass NULL to any of the pointers if
* you're not interested in obtaining it.
*
*/
void
hildon_date_editor_get_date (HildonDateEditor *date,
guint *year,
guint *month,
guint *day)
{
HildonDateEditorPrivate *priv;
g_return_if_fail (HILDON_IS_DATE_EDITOR (date));
priv = HILDON_DATE_EDITOR_GET_PRIVATE (date);
/* FIXME: The role of priv->{day,month,year} members vs. entry contents
is unclear. They do not neccesarily match and still the texts are
used as return values and members for some internal validation!!
At least a partly reason is to allow empty text to become
0 return value, while members are restricted to valid ranges?!
However, if we change the current way, we are likely to break
some applications if they rely on some specific way how this
widget currently handles empty values and temporarily invalid values.
The key issue is this: What should the _get methods return while
user is editing a field and the result is incomplete. The
partial result? The last good result? If we return partial result
we also need a way to inform if the date is not valid. Current
implementation is some kind of hybrid of these two...
for example:
hildon_date_editor_set_day(editor, hildon_date_editor_get_day(editor));
easily fails, since set_day tries to force validity while get_day
doesn't do that.
Proposal: Always return the same values that are shown in the
fields. We add a separate flag (Or use GDate) to
indicate if the current date is valid. This would allow
setters to make the date invalid as well. */
if (year != NULL)
*year = /*priv->year;*/
(guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->y_entry)));
if (month != NULL)
*month = /*priv->month;*/
(guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->m_entry)));
if (day != NULL)
*day = /*priv->day;*/
(guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->d_entry)));
}
/* icon button press event */
static gboolean
hildon_date_editor_icon_press (GtkWidget *widget,
gpointer data)
{
g_assert (GTK_IS_WIDGET (widget));
g_assert (HILDON_IS_DATE_EDITOR (data));
hildon_date_editor_set_calendar_icon_state (HILDON_DATE_EDITOR (data), TRUE);
return FALSE;
}
static gboolean
hildon_date_editor_entry_focus_in (GtkWidget *widget,
GdkEventFocus *event,
gpointer data)
{
g_idle_add ((GSourceFunc) hildon_date_editor_entry_select_all, GTK_ENTRY (widget));
return FALSE;
}
static void
popup_calendar_dialog (HildonDateEditor *ed)
{
guint y = 0, m = 0, d = 0;
GtkWidget *popup;
GtkWidget *parent;
guint result;
GValue val = {0, };
hildon_date_editor_get_date (ed, &y, &m, &d);
parent = gtk_widget_get_ancestor (GTK_WIDGET (ed), GTK_TYPE_WINDOW);
popup = hildon_calendar_popup_new (GTK_WINDOW (parent), y, m, d);
g_value_init (&val, G_TYPE_INT);
/* Set max/min year in calendar popup to date editor values */
g_object_get_property (G_OBJECT (ed), "min-year", &val);
g_object_set_property (G_OBJECT (popup), "min-year", &val);
g_object_get_property (G_OBJECT (ed), "max-year", &val);
g_object_set_property (G_OBJECT (popup), "max-year", &val);
/* Pop up calendar */
result = gtk_dialog_run (GTK_DIALOG (popup));
switch (result) {
case GTK_RESPONSE_OK:
case GTK_RESPONSE_ACCEPT:
hildon_calendar_popup_get_date (HILDON_CALENDAR_POPUP (popup), &y,
&m, &d);
hildon_date_editor_set_date (ed, y, m, d);
}
gtk_widget_destroy (popup);
}
/* button released */
static gboolean
hildon_date_editor_released (GtkWidget *widget,
gpointer data)
{
HildonDateEditor *ed;
g_assert (GTK_IS_WIDGET (widget));
g_assert (HILDON_IS_DATE_EDITOR (data));
ed = HILDON_DATE_EDITOR (data);
/* restores the icon state. The clicked cycle raises the dialog */
hildon_date_editor_set_calendar_icon_state (ed, FALSE);
return FALSE;
}
/* button released */
static gboolean
hildon_date_editor_clicked (GtkWidget *widget,
gpointer data)
{
HildonDateEditor *ed;
g_assert (GTK_IS_WIDGET (widget));
g_assert (HILDON_IS_DATE_EDITOR (data));
ed = HILDON_DATE_EDITOR (data);
/* restores the non-clicked button state and raises the dialog */
hildon_date_editor_set_calendar_icon_state (ed, FALSE);
popup_calendar_dialog (ed);
return FALSE;
}
/* This is called whenever some editor filed loses the focus and
when the all of the fields are filled.
Earlier this was called whenever an entry changed */
/* FIXME: Validation on focus_out is broken by concept */
static gint
hildon_date_editor_entry_validate (GtkWidget *widget,
gpointer data)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
gint d, m, y, max_days;
gboolean r; /* temp return values for signals */
const gchar *text;
gint error_code = HILDON_DATE_TIME_ERROR_NO_ERROR;
g_assert (HILDON_IS_DATE_EDITOR (data));
g_assert (GTK_IS_ENTRY (widget));
ed = HILDON_DATE_EDITOR (data);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
g_assert (priv);
if (priv->skip_validation)
return error_code;
/*check if the calling entry is empty*/
text = gtk_entry_get_text (GTK_ENTRY (widget));
if(text == NULL || text[0] == 0)
{
if (widget == priv->d_entry)
g_signal_emit (ed, date_editor_signals[DATE_ERROR], 0, HILDON_DATE_TIME_ERROR_EMPTY_DAY, &r);
else if(widget == priv->m_entry)
g_signal_emit (ed, date_editor_signals[DATE_ERROR], 0, HILDON_DATE_TIME_ERROR_EMPTY_MONTH, &r);
else
g_signal_emit (ed, date_editor_signals[DATE_ERROR], 0, HILDON_DATE_TIME_ERROR_EMPTY_YEAR, &r);
/* restore empty entry to safe value */
hildon_date_editor_set_date (ed, priv->year, priv->month, priv->day);
return error_code;
}
/* Ok, we now check validity. Some fields can be empty */
text = gtk_entry_get_text (GTK_ENTRY (priv->d_entry));
if (text == NULL || text[0] == 0) return error_code;
d = atoi (text);
text = gtk_entry_get_text (GTK_ENTRY (priv->m_entry));
if (text == NULL || text[0] == 0) return error_code;
m = atoi (text);
text = gtk_entry_get_text (GTK_ENTRY (priv->y_entry));
if (text == NULL || text[0] == 0) return error_code;
y = atoi (text);
/* Did it actually change? */
if (d != priv->day || m != priv->month || y != priv->year)
{
/* We could/should use hildon_date_editor_set_year and such functions
* to set the date, instead of use gtk_entry_set_text, and then change
* the priv member but hildon_date_editor_set_year and such functions
* check if the date is valid, we do want to do date validation check
* here according to spec */
/* Validate month */
if (widget == priv->m_entry) {
if (m < 1) {
error_code = HILDON_DATE_TIME_ERROR_MIN_MONTH;
m = 1;
}
else if (m > 12) {
error_code = HILDON_DATE_TIME_ERROR_MAX_MONTH;
m = 12;
}
}
/* Validate year */
if(widget == priv->y_entry) {
if (y < priv->min_year) {
error_code = HILDON_DATE_TIME_ERROR_MIN_YEAR;
y = priv->min_year;
}
else if (y > priv->max_year) {
error_code = HILDON_DATE_TIME_ERROR_MAX_YEAR;
y = priv->max_year;
}
}
/* Validate day. We have to do this in every case, since
changing month or year can make the day number to be invalid */
max_days = g_date_get_days_in_month (m,y);
if(d < 1) {
error_code = HILDON_DATE_TIME_ERROR_MIN_DAY;
d = 1;
}
else if (d > max_days) {
if (d > 31) {
error_code = HILDON_DATE_TIME_ERROR_MAX_DAY;
d = max_days;
}
else { /* the date does not exist (is invalid) */
error_code = HILDON_DATE_TIME_ERROR_INVALID_DATE;
/* check what was changed and restore previous value */
if (widget == priv->y_entry)
y = priv->year;
else if (widget == priv->m_entry)
m = priv->month;
else
d = priv->day;
}
}
if (error_code != HILDON_DATE_TIME_ERROR_NO_ERROR)
{
g_signal_emit (ed, date_editor_signals[DATE_ERROR], 0, error_code, &r);
g_idle_add ((GSourceFunc) hildon_date_editor_entry_select_all, widget);
}
}
/* Fix and reformat the date after error signal is processed.
reformatting can be needed even in a such case that numerical
values of the date components are the same as earlier. */
hildon_date_editor_set_date (ed, y, m, d);
return error_code;
}
/* Idle callback */
static gboolean
hildon_date_editor_entry_select_all (GtkWidget *widget)
{
GDK_THREADS_ENTER ();
gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
GDK_THREADS_LEAVE ();
return FALSE;
}
/* When entry becomes full, we move the focus to the next field.
If we are on the last field, the whole contents are validated. */
static void
hildon_date_editor_entry_changed (GtkEditable *ed,
gpointer data)
{
GtkEntry *entry;
gint error_code;
HildonDateEditorPrivate *priv;
priv = HILDON_DATE_EDITOR_GET_PRIVATE (HILDON_DATE_EDITOR (data));
g_assert (GTK_IS_ENTRY (ed));
g_assert (HILDON_IS_DATE_EDITOR (data));
g_assert (priv);
entry = GTK_ENTRY (ed);
/* If day entry is full, move to next entry or validate */
if (g_utf8_strlen (gtk_entry_get_text (entry), -1) == gtk_entry_get_max_length (entry))
{
error_code = hildon_date_editor_entry_validate (GTK_WIDGET (entry), data);
if (error_code == HILDON_DATE_TIME_ERROR_NO_ERROR)
{
priv->skip_validation = TRUE;
gtk_widget_child_focus (GTK_WIDGET (data), GTK_DIR_RIGHT);
}
} else {
priv->skip_validation = FALSE;
}
}
static gboolean
hildon_date_editor_keyrelease (GtkWidget *widget,
GdkEventKey *event,
gpointer data)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
g_return_val_if_fail (data, FALSE);
g_return_val_if_fail (widget, FALSE);
ed = HILDON_DATE_EDITOR (data);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
if (event->keyval == GDK_KP_Enter ||
event->keyval == GDK_Return ||
event->keyval == GDK_ISO_Enter) {
if (hildon_date_editor_set_calendar_icon_state (ed, FALSE))
{
popup_calendar_dialog (ed);
return TRUE;
}
} else if (event->keyval == GDK_Escape)
priv->skip_validation = FALSE;
return FALSE;
}
/* keyboard handling */
static gboolean
hildon_date_editor_keypress (GtkWidget *widget,
GdkEventKey *event,
gpointer data)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
g_assert (HILDON_IS_DATE_EDITOR (data));
g_assert (GTK_IS_ENTRY (widget));
ed = HILDON_DATE_EDITOR (data);
switch (event->keyval) {
case GDK_Return:
case GDK_ISO_Enter:
/* Ignore return value, since we want to handle event at all times.
otherwise vkb would popup when the keyrepeat starts. */
hildon_date_editor_set_calendar_icon_state (ed, TRUE);
return TRUE;
case GDK_Escape:
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
priv->skip_validation = TRUE;
break;
default:
break;
}
return FALSE;
}
static gboolean
hildon_date_editor_entry_focus_out (GtkWidget *widget,
GdkEventFocus *event,
gpointer data)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
g_assert (HILDON_IS_DATE_EDITOR (data));
ed = HILDON_DATE_EDITOR (data);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
g_assert (priv);
hildon_date_editor_entry_validate (widget, data);
priv->skip_validation = FALSE;
return FALSE;
}
static gboolean
hildon_date_editor_date_error (HildonDateEditor *editor,
HildonDateTimeError type)
{
HildonDateEditorPrivate *priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
switch (type)
{
case HILDON_DATE_TIME_ERROR_MAX_DAY:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_maximum_value"), 31);
break;
case HILDON_DATE_TIME_ERROR_MAX_MONTH:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_maximum_value"), 12);
break;
case HILDON_DATE_TIME_ERROR_MAX_YEAR:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_maximum_value"), priv->max_year);
break;
case HILDON_DATE_TIME_ERROR_MIN_DAY:
case HILDON_DATE_TIME_ERROR_MIN_MONTH:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_minimum_value"), 1);
break;
case HILDON_DATE_TIME_ERROR_MIN_YEAR:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_minimum_value"), priv->min_year);
break;
case HILDON_DATE_TIME_ERROR_EMPTY_DAY:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_set_a_value_within_range"), 1, 31);
break;
case HILDON_DATE_TIME_ERROR_EMPTY_MONTH:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_set_a_value_within_range"), 1, 12);
break;
case HILDON_DATE_TIME_ERROR_EMPTY_YEAR:
hildon_banner_show_informationf (GTK_WIDGET (editor), NULL, _("ckct_ib_set_a_value_within_range"),
priv->min_year, priv->max_year);
break;
case HILDON_DATE_TIME_ERROR_INVALID_CHAR:
hildon_banner_show_information (GTK_WIDGET (editor), NULL, c_("ckct_ib_illegal_character"));
break;
case HILDON_DATE_TIME_ERROR_INVALID_DATE:
hildon_banner_show_information (GTK_WIDGET (editor), NULL, _("ckct_ib_date_does_not_exist"));
break;
default:
/*default error message ?*/
break;
}
return TRUE;
}
static void
hildon_date_editor_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
GtkRequisition f_req, img_req;
g_assert (GTK_IS_WIDGET (widget));
g_assert (requisition != NULL);
ed = HILDON_DATE_EDITOR (widget);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
g_assert (priv);
/* Our own children affect our size */
gtk_widget_size_request (priv->frame, &f_req);
gtk_widget_size_request (priv->d_button_image, &img_req);
/* calculate our size */
requisition->width = f_req.width + img_req.width + HILDON_MARGIN_DEFAULT;
/* FIXME: Fixed size is bad! We should use the maximum of our children, but
doing so would break current pixel specifications, since
the text entry by itself is already 30px tall + then frame takes
something */
requisition->height = DATE_EDITOR_HEIGHT;
}
static void
hildon_date_editor_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
HildonDateEditor *ed;
HildonDateEditorPrivate *priv;
GtkAllocation f_alloc, img_alloc;
GtkRequisition req;
GtkRequisition max_req;
GList *iter;
gboolean rtl;
g_assert (GTK_IS_WIDGET (widget));
g_assert (allocation != NULL);
ed = HILDON_DATE_EDITOR (widget);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
widget->allocation = *allocation;
gtk_widget_get_child_requisition (widget, &max_req);
/* Center vertically */
f_alloc.y = img_alloc.y = allocation->y +
MAX (allocation->height - max_req.height, 0) / 2;
/* Center horizontally */
f_alloc.x = img_alloc.x = allocation->x +
MAX (allocation->width - max_req.width, 0) / 2;
/* calculate allocations */
if (GTK_WIDGET_VISIBLE (widget)) {
/* allocate frame */
gtk_widget_get_child_requisition (priv->frame, &req);
f_alloc.width = req.width;
f_alloc.height = max_req.height;
/* allocate icon */
gtk_widget_get_child_requisition (priv->d_button_image,
&req);
img_alloc.x += f_alloc.width + HILDON_MARGIN_DEFAULT;
img_alloc.width = req.width;
img_alloc.height = max_req.height;
if (rtl)
{
img_alloc.x = f_alloc.x;
f_alloc.x += img_alloc.width + HILDON_MARGIN_DEFAULT;
}
if (GTK_WIDGET_VISIBLE (priv->d_button_image)) {
gtk_widget_size_allocate (priv->d_button_image, &img_alloc);
}
if (GTK_WIDGET_VISIBLE (priv->frame)) {
gtk_widget_size_allocate (priv->frame, &f_alloc);
}
}
/* FIXME: We really should not alloc delimeters by hand (since they
are not our own children, but we need to force to appear
higher. This ugly hack is needed to compensate the forced
height in size_request. */
for (iter = priv->delims; iter; iter = iter->next)
{
GtkWidget *delim;
GtkAllocation alloc;
delim = GTK_WIDGET (iter->data);
alloc = delim->allocation;
alloc.height = max_req.height;
alloc.y = priv->d_entry->allocation.y - 2;
gtk_widget_size_allocate (delim, &alloc);
}
}
static gboolean
hildon_date_editor_focus (GtkWidget *widget,
GtkDirectionType direction)
{
gboolean retval;
GtkDirectionType effective_direction;
g_assert (HILDON_IS_DATE_EDITOR (widget));
retval = hildon_private_composite_focus (widget, direction, &effective_direction);
if (retval == TRUE)
return GTK_WIDGET_CLASS (parent_class)->focus (widget, effective_direction);
else
return FALSE;
}
/**
* hildon_date_editor_set_year:
* @editor: the @HildonDateEditor widget
* @year: year
*
* Sets the year shown in the editor.
*
* Returns: TRUE if the year is valid and has been set.
*/
gboolean
hildon_date_editor_set_year (HildonDateEditor *editor,
guint year)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR (editor), FALSE);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (g_date_valid_dmy (priv->day, priv->month, year))
{
gchar buffer[256];
priv->year = year;
g_snprintf (buffer, sizeof (buffer), "%04d", year);
/* We apply the new day, but do not want automatic focus move
etc to take place */
g_signal_handlers_block_by_func (priv->y_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->y_entry), buffer);
g_signal_handlers_unblock_by_func (priv->y_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_object_notify (G_OBJECT(editor), "year");
return TRUE;
}
return FALSE;
}
/**
* hildon_date_editor_set_month:
* @editor: the @HildonDateEditor widget
* @month: month
*
* Sets the month shown in the editor.
*
* Returns: TRUE if the month is valid and has been set.
*/
gboolean
hildon_date_editor_set_month (HildonDateEditor *editor,
guint month)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR (editor), FALSE);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (g_date_valid_dmy (priv->day, month, priv->year))
{
GDate date;
gchar buffer[256];
priv->month = month;
g_date_set_dmy (&date, priv->day, month, priv->year);
g_date_strftime (buffer, sizeof(buffer), "%m", &date);
/* We apply the new day, but do not want automatic focus move
etc to take place */
g_signal_handlers_block_by_func (priv->m_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->m_entry), buffer);
g_signal_handlers_unblock_by_func (priv->m_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_object_notify (G_OBJECT (editor), "month");
return TRUE;
}
return FALSE;
}
/**
* hildon_date_editor_set_day:
* @editor: the @HildonDateEditor widget
* @day: day
*
* Sets the day shown in the editor.
*
* Returns: TRUE if the day is valid and has been set.
*/
gboolean
hildon_date_editor_set_day (HildonDateEditor *editor,
guint day)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR (editor), FALSE);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
if (g_date_valid_dmy (day, priv->month, priv->year))
{
GDate date;
gchar buffer[256];
priv->day = day;
g_date_set_dmy (&date, day, priv->month, priv->year);
g_date_strftime (buffer, sizeof (buffer), "%d", &date);
/* We apply the new day, but do not want automatic focus move
etc to take place */
g_signal_handlers_block_by_func (priv->d_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
gtk_entry_set_text (GTK_ENTRY (priv->d_entry), buffer);
g_signal_handlers_unblock_by_func (priv->d_entry,
(gpointer) hildon_date_editor_entry_changed, editor);
g_object_notify (G_OBJECT(editor), "day");
return TRUE;
}
return FALSE;
}
/**
* hildon_date_editor_get_year:
* @editor: the @HildonDateEditor widget
*
* Returns: the current year shown in the editor.
*/
guint
hildon_date_editor_get_year (HildonDateEditor *editor)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR(editor), 0);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
return (guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->y_entry)));
}
/**
* hildon_date_editor_get_month:
* @editor: the @HildonDateEditor widget
*
* Gets the month shown in the editor.
*
* Returns: the current month shown in the editor.
*/
guint
hildon_date_editor_get_month (HildonDateEditor *editor)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR (editor), 0);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
return (guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->m_entry)));
}
/**
* hildon_date_editor_get_day:
* @editor: the @HildonDateEditor widget
*
* Gets the day shown in the editor.
*
* Returns: the current day shown in the editor
*/
guint
hildon_date_editor_get_day (HildonDateEditor *editor)
{
HildonDateEditorPrivate *priv;
g_return_val_if_fail (HILDON_IS_DATE_EDITOR (editor), 0);
priv = HILDON_DATE_EDITOR_GET_PRIVATE (editor);
g_assert (priv);
return (guint) atoi (gtk_entry_get_text (GTK_ENTRY (priv->d_entry)));
}
|