1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2019 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.Globalization;
23 using Eto.Forms;
24 using GKCore;
25 
26 namespace GKUI.Components
27 {
28     /// <summary>
29     ///
30     /// </summary>
31     public class GKDateBox : MaskedTextBox
32     {
33         private readonly string fRegionalDatePattern;
34 
35 
36         public string RegionalDatePattern
37         {
38             get { return fRegionalDatePattern; }
39         }
40 
41         public string NormalizeDate
42         {
43             get { return GKUtils.GetNormalizeDate(Text, fRegionalDatePattern); }
44             set { Text = GKUtils.GetRegionalDate(value, fRegionalDatePattern); }
45         }
46 
47 
GKDateBox()48         public GKDateBox()
49         {
50             fRegionalDatePattern = GetShortDatePattern();
51             Provider = new FixedMaskedTextProvider(GetMask(fRegionalDatePattern), CultureInfo.InvariantCulture);
52         }
53 
GetMask(string regionalDatePattern)54         private static string GetMask(string regionalDatePattern)
55         {
56             // "00/00/0000"
57             string result = regionalDatePattern.Replace('d', '0').Replace('m', '0').Replace('y', '0');
58             return result;
59         }
60 
GetShortDatePattern()61         private static string GetShortDatePattern()
62         {
63             var culture = CultureInfo.CurrentCulture; // work
64             //var culture = new CultureInfo("en-US"); // debug
65             //var culture = new CultureInfo("hu-HU"); // debug
66 
67             var dtf = culture.DateTimeFormat;
68             var dateSeparators = dtf.DateSeparator.ToCharArray();
69 
70             // may contain a period, a dash, and a slash
71             var result = dtf.ShortDatePattern.ToLowerInvariant();
72 
73             // normalize
74             string[] parts = result.Split(dateSeparators, StringSplitOptions.RemoveEmptyEntries);
75             for (int i = 0; i < parts.Length; i++) {
76                 string part = parts[i];
77                 char firstChar = part[0];
78                 switch (firstChar) {
79                     case 'd':
80                     case 'm':
81                         if (part.Length < 2) {
82                             part = part.PadRight(2, firstChar);
83                         }
84                         break;
85 
86                     case 'y':
87                         if (part.Length < 4) {
88                             part = part.PadRight(4, firstChar);
89                         }
90                         break;
91                 }
92                 parts[i] = part;
93             }
94             result = string.Join("/", parts);
95 
96             return result;
97         }
98     }
99 }
100