soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SCalendar.h
Go to the documentation of this file.
1/**
2 * @file SCalendar.h
3 * @brief Calendar Control
4 * @version v1.0
5 * @author soui
6 * @date 2014-05-25
7 *
8 * @details This class implements a calendar control for date selection and display.
9 */
10
11#ifndef __SCALENDAR__H__
12#define __SCALENDAR__H__
13
14#include "core/SWnd.h"
15
16SNSBEGIN
17
18/**
19 * @class SCalendarCore
20 * @brief Calendar Core Class
21 * @details This class is the core of the calendar, with most functions being static.
22 */
23class SOUI_EXP SCalendarCore {
24 public:
25 /**
26 * @brief Determines if a year is a leap year.
27 * @param wYear The year to check.
28 * @param bLeapYear [output] TRUE if leap year, FALSE if common year.
29 * @return TRUE -- Success, FALSE -- Failure
30 * @details Checks if the given year is a leap year.
31 * Note: Valid range is (1600 to 6999).
32 */
33 static BOOL IsLeapYear(WORD wYear, BOOL &bLeapYear);
34
35 /**
36 * @brief Calculates the day number within the year (starting from 0, where 0 is January 1st).
37 * @param wYear The year.
38 * @param wMonth The month.
39 * @param wDay The day.
40 * @param wDays [output] The day number within the year.
41 * @return TRUE -- Success, FALSE -- Failure
42 * @details Given a date, calculates the day number within that year, starting from 0.
43 * Note: Valid range is (START_YEAR to END_YEAR-1).
44 */
45 static BOOL GetDaysNumInYear(WORD wYear, WORD wMonth, WORD wDay, WORD &wDays);
46
47 /**
48 * @brief Calculates the month and day from the day number within the year.
49 * @param wYear The year.
50 * @param wDays The day number within the year.
51 * @param wMonth [output] The month.
52 * @param wDay [output] The day.
53 * @return TRUE -- Success, FALSE -- Failure
54 * @details Given a year and day number, calculates the corresponding month and day.
55 * Note: Valid range is (START_YEAR to END_YEAR-1).
56 */
57 static BOOL GetDateFromDays(WORD wYear, WORD wDays, WORD &wMonth, WORD &wDay);
58
59 /**
60 * @brief Returns the day of the week.
61 * @param wYear The year.
62 * @param wMonth The month.
63 * @param wDay The day.
64 * @return Returns: 0, 1, 2, 3, 4, 5, 6 corresponding to Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday; -1 if date is invalid or out of range.
65 * @details Given a date, returns the day of the week.
66 * Note: Valid range is (START_YEAR-01-01 to END_YEAR-12-31).
67 */
68 static short GetDayOfWeek(WORD wYear, WORD wMonth, WORD wDay);
69
70 /**
71 * @brief Returns the number of days in a specified month.
72 * @param wYear The year.
73 * @param wMonth The month.
74 * @return Returns the number of days in the month, returns 0 if the month/year is invalid.
75 * @details Given a year and month, returns the number of days in that month.
76 * Note: Valid range is (START_YEAR-01 to END_YEAR-12).
77 */
78 static WORD GetDaysOfMonth(WORD wYear, WORD wMonth);
79
80 /**
81 * @brief Validates the year, month, and day.
82 * @param wYear The year.
83 * @param wMonth The month.
84 * @param wDay The day.
85 * @return FALSE -- Failure, TRUE -- Success
86 * @details Validates the given date.
87 * Note: Valid range is (START_YEAR-01-01 to END_YEAR-12-31).
88 */
89 static BOOL DateCheck(WORD wYear, WORD wMonth, WORD wDay);
90
91 /**
92 * @brief Formats the year display.
93 * @param iYear The year.
94 * @return The formatted year string.
95 * @details Formats the year display, using the Chinese zodiac for the lunar year.
96 */
97 static SStringT FormatYear(WORD iYear);
98
99 /**
100 * @brief Formats the month display.
101 * @param iMonth The month.
102 * @return The formatted month string.
103 */
104 static SStringT FormatMonth(WORD iMonth);
105
106 /**
107 * @brief Formats the day display.
108 * @param iDay The day.
109 * @return The formatted day string.
110 */
111 static SStringT FormatDay(WORD iDay);
112};
113
114// Button macro definitions
115#define HIT_NULL -1 /**< No Hit */
116#define HIT_LEFT -10 /**< Left button (previous month) */
117#define HIT_RIGHT -11 /**< Right button (next month) */
118#define HIT_YEAR -12 /**< Year-month button (not used yet)*/
119#define HIT_YEAR_1 -13 /**< Year-month button (not used yet)*/
120#define HIT_YEAR_2 -14 /**< Year-month button (not used yet)*/
121#define HIT_YEAR_3 -15 /**< Year-month button (not used yet)*/
122#define HIT_TODAY 42 /**< Today button */
123
124// Calendar display states
125#define SHOW_MONTH -101 /**< Show month */
126#define SHOW_YEAR -102 /**< Show year */
127#define SHOW_YEAR_DECADE -103 /**< Show decade */
128#define SHOW_YEAR_CENTURY -104 /**< Show century */
129
130/**
131 * @class SCalendar
132 * @brief Calendar Control Class
133 * @details This class implements a calendar control for date selection and display.
134 */
135class SOUI_EXP SCalendar : public SWindow {
136 DEF_SOBJECT(SWindow, L"calendar")
137
138 public:
139 /**
140 * @brief Constructor
141 * @param iYear Initial year
142 * @param iMonth Initial month
143 * @param iDay Initial day
144 */
145 SCalendar(WORD iYear, WORD iMonth, WORD iDay);
146
147 /**
148 * @brief Default constructor
149 */
150 SCalendar();
151
152 /**
153 * @brief Destructor
154 */
155 ~SCalendar();
156
157 /**
158 * @brief Gets the current year.
159 * @return The current year.
160 */
161 WORD GetYear();
162
163 /**
164 * @brief Gets the current month.
165 * @return The current month.
166 */
167 WORD GetMonth();
168
169 /**
170 * @brief Gets the current day.
171 * @return The current day.
172 */
173 WORD GetDay();
174
175 /**
176 * @brief Gets the current date.
177 * @param iYear [output] The year.
178 * @param iMonth [output] The month.
179 * @param iDay [output] The day.
180 */
181 void GetDate(WORD &iYear, WORD &iMonth, WORD &iDay);
182
183 /**
184 * @brief Sets the date.
185 * @param iYear The year.
186 * @param iMonth The month.
187 * @param iDay The day.
188 * @param nBtnType Button type.
189 * @param bNotify Whether to notify events.
190 * @return TRUE -- Success, FALSE -- Failure
191 */
192 BOOL SetDate(WORD iYear, WORD iMonth, WORD iDay, int nBtnType = HIT_NULL, bool bNotify = false);
193
194 protected:
195 /**
196 * @brief Called when the language changes.
197 * @return HRESULT indicating the result of the operation.
198 */
199 virtual HRESULT OnLanguageChanged();
200
201 protected:
202 /**
203 * @brief Initializes the control.
204 */
205 void Init();
206
207 /**
208 * @brief Paints the control.
209 * @param pRT The rendering target.
210 */
211 void OnPaint(IRenderTarget *pRT);
212
213 /**
214 * @brief Handles left mouse button down event.
215 * @param nFlags Key state.
216 * @param point Mouse position.
217 */
218 void OnLButtonDown(UINT nFlags, CPoint point);
219
220 /**
221 * @brief Handles left mouse button up event.
222 * @param nFlags Key state.
223 * @param point Mouse position.
224 */
225 void OnLButtonUp(UINT nFlags, CPoint point);
226
227 /**
228 * @brief Handles mouse move event.
229 * @param nFlags Key state.
230 * @param pt Mouse position.
231 */
232 void OnMouseMove(UINT nFlags, CPoint pt);
233
234 /**
235 * @brief Handles mouse leave event.
236 */
237 void OnMouseLeave();
238
239 /**
240 * @brief Handles mouse wheel event.
241 * @param nFlags Key state.
242 * @param zDelta Wheel delta.
243 * @param pt Mouse position.
244 * @return TRUE -- Success, FALSE -- Failure
245 */
246 BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
247
248 SOUI_ATTRS_BEGIN()
249 ATTR_LAYOUTSIZE(L"yearHeight", m_nYearMonthHeight, TRUE)
250 ATTR_LAYOUTSIZE(L"weekHeight", m_nWeekHeight, TRUE)
251 ATTR_LAYOUTSIZE(L"todayHeight", m_nFooterHeight, TRUE)
252 ATTR_COLOR(L"colorSelText", m_crSelText, TRUE)
253 ATTR_COLOR(L"colorOtherText", m_crOtherDayText, TRUE)
254 ATTR_COLOR(L"colorSelBg", m_crSelDayBack, TRUE)
255 ATTR_COLOR(L"colorHoverText", m_crHoverText, TRUE)
256 ATTR_SKIN(L"prevSkin", m_pSkinPrev, TRUE)
257 ATTR_SKIN(L"nextSkin", m_pSkinNext, TRUE)
258 ATTR_SKIN(L"daySkin", m_pSkinDay, TRUE)
259 ATTR_SKIN(L"weekSkin", m_pSkinWeek, TRUE)
260 ATTR_I18NSTRT(L"textSunday", m_strWeek[0], TRUE)
261 ATTR_I18NSTRT(L"textMonday", m_strWeek[1], TRUE)
262 ATTR_I18NSTRT(L"textTuesday", m_strWeek[2], TRUE)
263 ATTR_I18NSTRT(L"textWednesday", m_strWeek[3], TRUE)
264 ATTR_I18NSTRT(L"textThursday", m_strWeek[4], TRUE)
265 ATTR_I18NSTRT(L"textFriday", m_strWeek[5], TRUE)
266 ATTR_I18NSTRT(L"textSaturday", m_strWeek[6], TRUE)
267 SOUI_ATTRS_END()
268
269 protected:
270 /**
271 * @brief Called when the control is created.
272 * @param lp Creation parameters.
273 * @return HRESULT indicating the result of the operation.
274 */
275 int OnCreate(LPVOID lp);
276
277 SOUI_MSG_MAP_BEGIN()
278 MSG_WM_CREATE(OnCreate)
279 MSG_WM_PAINT_EX(OnPaint)
280 MSG_WM_LBUTTONDOWN(OnLButtonDown)
281 MSG_WM_LBUTTONUP(OnLButtonUp)
282 MSG_WM_MOUSEMOVE(OnMouseMove)
283 MSG_WM_MOUSELEAVE(OnMouseLeave)
284 MSG_WM_MOUSEWHEEL(OnMouseWheel)
285 SOUI_MSG_MAP_END()
286
287 protected:
288 /**
289 * @brief Tests the mouse click position.
290 * @param pt Mouse position.
291 * @return The item corresponding to the click position.
292 */
293 int HitTest(const CPoint &pt);
294
295 /**
296 * @brief Draws the calendar header (year and month).
297 * @param pRT The rendering target.
298 * @param rect The drawing area.
299 */
300 void DrawYearMonth(IRenderTarget *pRT, const CRect &rect);
301
302 /**
303 * @brief Draws the week days.
304 * @param pRT The rendering target.
305 * @param rect The drawing area.
306 */
307 void DrawWeek(IRenderTarget *pRT, const CRect &rect);
308
309 /**
310 * @brief Draws the day.
311 * @param pRT The rendering target.
312 * @param rcDay The day area.
313 * @param nItem The day item.
314 */
315 void DrawDay(IRenderTarget *pRT, CRect &rcDay, int nItem);
316
317 /**
318 * @brief Draws the "Today" button.
319 * @param pRT The rendering target.
320 * @param rcDay The "Today" button area.
321 */
322 void DrawToday(IRenderTarget *pRT, CRect &rcDay);
323
324 /**
325 * @brief Gets the item drawing area.
326 * @param nItem The item index.
327 * @param rcItem [output] The item area.
328 */
329 void GetItemRect(int nItem, CRect &rcItem);
330
331 /**
332 * @brief Sets the previous month.
333 */
334 void SetLastMonth();
335
336 /**
337 * @brief Sets the next month.
338 */
339 void SetNextMonth();
340
341 /**
342 * @brief Sets the previous year.
343 */
344 void SetLastYear();
345
346 /**
347 * @brief Sets the next year.
348 */
349 void SetNextYear();
350
351 /**
352 * @brief Sets the previous decade.
353 */
354 void SetLastYearDecade();
355
356 /**
357 * @brief Sets the next decade.
358 */
359 void SetNextYearDecade();
360
361 /**
362 * @brief Sets the previous century.
363 */
364 void SetLastYearCentury();
365
366 /**
367 * @brief Sets the next century.
368 */
369 void SetNextYearCentury();
370
371 /**
372 * @brief Sets the year and month.
373 * @param iYear The year.
374 * @param iMonth The month.
375 */
376 void SetYearMonth(int iYear, int iMonth);
377
378 /**
379 * @brief Paints the month view.
380 * @param pRT The rendering target.
381 */
382 void OnPaintMonth(IRenderTarget *pRT);
383
384 /**
385 * @brief Paints the year, decade, and century views.
386 * @param pRT The rendering target.
387 */
389
390 /**
391 * @brief Draws the year, decade, and century items.
392 * @param pRT The rendering target.
393 * @param rect The drawing area.
394 * @param nItem The item index.
395 */
396 void DrawYearDecadeCentury(IRenderTarget *pRT, const CRect &rect, int nItem);
397
398 public:
399 /**
400 * @brief Sets the calendar display type.
401 * @param showType The display type.
402 */
403 void SetShowType(int showType);
404
405 /**
406 * @brief Sets the year, decade, and century views.
407 */
409
410 protected:
411 /**
412 * @brief Height of the year and month.
413 */
415
416 /**
417 * @brief Height of the week.
418 */
420
421 /**
422 * @brief Height of the "Today" button.
423 */
425
426 /**
427 * @brief Selected text color.
428 */
429 COLORREF m_crSelText;
430
431 /**
432 * @brief Color of other days' text.
433 */
435
436 /**
437 * @brief Background color of the selected day.
438 */
440
441 /**
442 * @brief Color of the hover text.
443 */
445
446 /**
447 * @brief Skin for the previous button.
448 */
450
451 /**
452 * @brief Skin for the next button.
453 */
455
456 /**
457 * @brief Skin for the day.
458 */
460
461 /**
462 * @brief Skin for the week.
463 */
465
466 /**
467 * @brief Header text.
468 */
470
471 /**
472 * @brief Date information structure.
473 */
474 struct wDayInfo
475 {
476 WORD iDay; // Calendar day
477 int nType; // -1 previous month, 0 current month, 1 next month
478 };
479
480 /**
481 * @brief Date information array.
482 */
484
485 /**
486 * @brief Date area.
487 */
488 CRect m_rcDays;
489
490 /**
491 * @brief "Today" button area.
492 */
494
495 /**
496 * @brief Selected item.
497 */
499
500 /**
501 * @brief Hover item.
502 */
504
505 /**
506 * @brief Current year.
507 */
509
510 /**
511 * @brief Current month.
512 */
514
515 /**
516 * @brief Current date.
517 */
518 SYSTEMTIME m_Today;
519
520 /**
521 * @brief Calendar display state.
522 */
524
525 /**
526 * @brief Calendar display state when mouse button is down.
527 */
529
530 /**
531 * @brief Month or year information structure.
532 */
534 {
535 WORD iMonthOrYear; // Calendar month-year-decade-century
536 int nType; // -1 previous, 0 current, 1 next
537 };
538
539 /**
540 * @brief Month or year information array.
541 */
543};
544
545SNSEND
546
547#endif // __SCALENDAR__H__
#define HIT_NULL
Definition SCalendar.h:115
SOUI基础DUI窗口模块
Smart pointer class for managing COM-style reference-counted objects.
Calendar Core Class.
Definition SCalendar.h:23
static BOOL DateCheck(WORD wYear, WORD wMonth, WORD wDay)
Validates the year, month, and day.
static WORD GetDaysOfMonth(WORD wYear, WORD wMonth)
Returns the number of days in a specified month.
static BOOL GetDaysNumInYear(WORD wYear, WORD wMonth, WORD wDay, WORD &wDays)
Calculates the day number within the year (starting from 0, where 0 is January 1st).
Definition SCalendar.cpp:63
static short GetDayOfWeek(WORD wYear, WORD wMonth, WORD wDay)
Returns the day of the week.
static SStringT FormatYear(WORD iYear)
Formats the year display.
static BOOL GetDateFromDays(WORD wYear, WORD wDays, WORD &wMonth, WORD &wDay)
Calculates the month and day from the day number within the year.
Definition SCalendar.cpp:86
static SStringT FormatDay(WORD iDay)
Formats the day display.
static SStringT FormatMonth(WORD iMonth)
Formats the month display.
static BOOL IsLeapYear(WORD wYear, BOOL &bLeapYear)
Determines if a year is a leap year.
Definition SCalendar.cpp:46
COLORREF m_crSelDayBack
Background color of the selected day.
Definition SCalendar.h:439
void DrawToday(IRenderTarget *pRT, CRect &rcDay)
Draws the "Today" button.
void SetShowType(int showType)
Sets the calendar display type.
SAutoRefPtr< ISkinObj > m_pSkinNext
Skin for the next button.
Definition SCalendar.h:454
void SetYearDecadeCentury()
Sets the year, decade, and century views.
int m_showTypeLbdown
Calendar display state when mouse button is down.
Definition SCalendar.h:528
int m_nSelItem
Selected item.
Definition SCalendar.h:498
SLayoutSize m_nFooterHeight
Height of the "Today" button.
Definition SCalendar.h:424
SAutoRefPtr< ISkinObj > m_pSkinDay
Skin for the day.
Definition SCalendar.h:459
void SetNextYearCentury()
Sets the next century.
SYSTEMTIME m_Today
Current date.
Definition SCalendar.h:518
void SetNextYearDecade()
Sets the next decade.
void SetYearMonth(int iYear, int iMonth)
Sets the year and month.
int m_nHoverItem
Hover item.
Definition SCalendar.h:503
SAutoRefPtr< ISkinObj > m_pSkinPrev
Skin for the previous button.
Definition SCalendar.h:449
WORD m_iYear
Current year.
Definition SCalendar.h:508
void Init()
Initializes the control.
CRect m_rcToday
"Today" button area.
Definition SCalendar.h:493
int HitTest(const CPoint &pt)
Tests the mouse click position.
void SetNextYear()
Sets the next year.
void OnPaint(IRenderTarget *pRT)
Paints the control.
void DrawWeek(IRenderTarget *pRT, const CRect &rect)
Draws the week days.
void SetLastYear()
Sets the previous year.
void DrawYearMonth(IRenderTarget *pRT, const CRect &rect)
Draws the calendar header (year and month).
virtual HRESULT OnLanguageChanged()
Called when the language changes.
COLORREF m_crHoverText
Color of the hover text.
Definition SCalendar.h:444
void OnPaintMonth(IRenderTarget *pRT)
Paints the month view.
BOOL SetDate(WORD iYear, WORD iMonth, WORD iDay, int nBtnType=-1, bool bNotify=false)
Sets the date.
void SetLastYearCentury()
Sets the previous century.
void OnPaintYearDecadeCentury(IRenderTarget *pRT)
Paints the year, decade, and century views.
void GetDate(WORD &iYear, WORD &iMonth, WORD &iDay)
Gets the current date.
int m_showType
Calendar display state.
Definition SCalendar.h:523
void OnMouseMove(UINT nFlags, CPoint pt)
Handles mouse move event.
CRect m_rcDays
Date area.
Definition SCalendar.h:488
STrText m_strWeek[7]
Header text.
Definition SCalendar.h:469
void SetLastMonth()
Sets the previous month.
SLayoutSize m_nYearMonthHeight
Height of the year and month.
Definition SCalendar.h:414
void OnMouseLeave()
Handles mouse leave event.
void OnLButtonUp(UINT nFlags, CPoint point)
Handles left mouse button up event.
void OnLButtonDown(UINT nFlags, CPoint point)
Handles left mouse button down event.
void SetLastYearDecade()
Sets the previous decade.
wDayInfo m_arrDays[42]
Date information array.
Definition SCalendar.h:483
COLORREF m_crSelText
Selected text color.
Definition SCalendar.h:429
void SetNextMonth()
Sets the next month.
COLORREF m_crOtherDayText
Color of other days' text.
Definition SCalendar.h:434
wMonthOrYearInfo m_arrMonthOrYear[12]
Month or year information array.
Definition SCalendar.h:542
WORD GetMonth()
Gets the current month.
SCalendar(WORD iYear, WORD iMonth, WORD iDay)
Constructor.
void GetItemRect(int nItem, CRect &rcItem)
Gets the item drawing area.
WORD m_iMonth
Current month.
Definition SCalendar.h:513
SLayoutSize m_nWeekHeight
Height of the week.
Definition SCalendar.h:419
BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
Handles mouse wheel event.
WORD GetDay()
Gets the current day.
SAutoRefPtr< ISkinObj > m_pSkinWeek
Skin for the week.
Definition SCalendar.h:464
WORD GetYear()
Gets the current year.
void DrawYearDecadeCentury(IRenderTarget *pRT, const CRect &rect, int nItem)
Draws the year, decade, and century items.
void DrawDay(IRenderTarget *pRT, CRect &rcDay, int nItem)
Draws the day.
int OnCreate(LPVOID lp)
Called when the control is created.
布局大小类
Definition SLayoutSize.h:10
Class for handling text with translation support.
Definition SWnd.h:230
SWindow()
Constructor.
Definition Swnd.cpp:104
Interface for rendering target objects.
Definition SRender-i.h:1440
Date information structure.
Definition SCalendar.h:475
Month or year information structure.
Definition SCalendar.h:534