xref: /haiku/headers/private/shared/DateTimeEdit.h (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 /*
2  * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		McCall <mccall@@digitalparadise.co.uk>
7  *		Mike Berg <mike@berg-net.us>
8  *		Julun <host.haiku@gmx.de>
9  *		Hamish Morrison <hamish@lavabit.com>
10  */
11 #ifndef _DATE_TIME_EDIT_H
12 #define _DATE_TIME_EDIT_H
13 
14 #include <Control.h>
15 #include <DateFormat.h>
16 #include <DateTime.h>
17 #include <Locale.h>
18 #include <String.h>
19 #include <TimeFormat.h>
20 
21 class BBitmap;
22 class BList;
23 
24 
25 namespace BPrivate {
26 
27 
28 class SectionEdit : public BControl {
29 public:
30 								SectionEdit(const char* name,
31 											uint32 sections, BMessage* message);
32 	virtual						~SectionEdit();
33 
34 	virtual	void				AttachedToWindow();
35 	virtual	void				Draw(BRect updateRect);
36 	virtual	void				MouseDown(BPoint point);
37 	virtual	void				MakeFocus(bool focused = true);
38 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
39 
40 			BSize				MaxSize();
41 			BSize				MinSize();
42 			BSize				PreferredSize();
43 
44 			uint32				CountSections() const;
45 			int32				FocusIndex() const;
46 			BRect				SectionArea() const;
47 
48 	virtual	status_t			Invoke(BMessage* message = NULL);
49 
50 protected:
51 	virtual	void				DrawBorder(const BRect& updateRect);
52 	virtual	void				DrawSection(uint32 index, BRect bounds,
53 									bool isFocus) {}
54 	virtual	void				DrawSeparator(uint32 index, BRect bounds) {}
55 
56 			BRect				FrameForSection(uint32 index);
57 			BRect				FrameForSeparator(uint32 index);
58 
59 	virtual	void				SectionFocus(uint32 index) {}
60 	virtual	void				SectionChange(uint32 index, uint32 value) {}
61 	virtual	void				SetSections(BRect area) {}
62 
63 	virtual	float				SeparatorWidth() = 0;
64 	virtual	float				MinSectionWidth() = 0;
65 	virtual float				PreferredHeight() = 0;
66 
67 	virtual	void				DoUpPress() {}
68 	virtual	void				DoDownPress() {}
69 
70 	virtual	void				PopulateMessage(BMessage* message) = 0;
71 
72 			BRect				fUpRect;
73 			BRect				fDownRect;
74 
75 			int32				fFocus;
76 			uint32				fSectionCount;
77 			uint32				fHoldValue;
78 };
79 
80 
81 class TimeEdit : public SectionEdit {
82 public:
83 								TimeEdit(const char* name,	uint32 sections,
84 										BMessage* message);
85 	virtual						~TimeEdit();
86 
87 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
88 
89 	virtual	void				InitView();
90 	virtual	void				DrawSection(uint32 index, BRect bounds,
91 									bool isfocus);
92 	virtual	void				DrawSeparator(uint32 index, BRect bounds);
93 
94 	virtual	void				SectionFocus(uint32 index);
95 	virtual float				MinSectionWidth();
96 	virtual float				SeparatorWidth();
97 
98 	virtual float				PreferredHeight();
99 	virtual	void				DoUpPress();
100 	virtual	void				DoDownPress();
101 
102 	virtual	void				PopulateMessage(BMessage* message);
103 
104 			void				SetTime(int32 hour, int32 minute, int32 second);
105 			BTime				GetTime();
106 
107 private:
108 			void				_UpdateFields();
109 			void				_CheckRange();
110 			bool				_IsValidDoubleDigit(int32 value);
111 			int32				_SectionValue(int32 index) const;
112 
113 			BDateTime			fTime;
114 			BTimeFormat			fTimeFormat;
115 			bigtime_t			fLastKeyDownTime;
116 			int32				fLastKeyDownInt;
117 
118 			BString				fText;
119 
120 			// TODO: morph the following into a proper class
121 			BDateElement*		fFields;
122 			int					fFieldCount;
123 			int*				fFieldPositions;
124 			int					fFieldPosCount;
125 };
126 
127 
128 class DateEdit : public SectionEdit {
129 public:
130 								DateEdit(const char* name, uint32 sections,
131 										BMessage* message);
132 	virtual						~DateEdit();
133 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
134 
135 	virtual void				InitView();
136 	virtual void				DrawSection(uint32 index, BRect bounds,
137 									bool isfocus);
138 	virtual void				DrawSeparator(uint32 index, BRect bounds);
139 
140 	virtual void				SectionFocus(uint32 index);
141 	virtual float				MinSectionWidth();
142 	virtual float				SeparatorWidth();
143 
144 	virtual float				PreferredHeight();
145 	virtual void				DoUpPress();
146 	virtual void				DoDownPress();
147 
148 	virtual void				PopulateMessage(BMessage* message);
149 
150 
151 			void				SetDate(int32 year, int32 month, int32 day);
152 			BDate				GetDate();
153 
154 private:
155 			void				_UpdateFields();
156 			void				_CheckRange();
157 			bool				_IsValidDoubleDigit(int32 value);
158 			int32				_SectionValue(int32 index) const;
159 
160 			BDate				fDate;
161 			BDateFormat			fDateFormat;
162 			bigtime_t			fLastKeyDownTime;
163 			int32				fLastKeyDownInt;
164 
165 			BString				fText;
166 
167 			// TODO: morph the following into a proper class
168 			BDateElement*		fFields;
169 			int					fFieldCount;
170 			int*				fFieldPositions;
171 			int					fFieldPosCount;
172 };
173 
174 
175 }	// namespace BPrivate
176 
177 
178 #endif	// _DATE_TIME_EDIT_H
179