Mercurial > dwindows
comparison os2/dw.c @ 1348:64c8eba7c0fc
Fixes for handling leap years on the OS/2 calendar control.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 15 Nov 2011 12:33:10 +0000 |
parents | 2bc23978cfcd |
children | 19aa1af6855e |
comparison
equal
deleted
inserted
replaced
1347:2bc23978cfcd | 1348:64c8eba7c0fc |
---|---|
1652 case WM_BUTTON3DOWN: | 1652 case WM_BUTTON3DOWN: |
1653 { | 1653 { |
1654 POINTS pts = (*((POINTS*)&mp1)); | 1654 POINTS pts = (*((POINTS*)&mp1)); |
1655 int month = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_month")); | 1655 int month = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_month")); |
1656 int year = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_year")); | 1656 int year = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_year")); |
1657 int dayofweek = 1, x, height; | 1657 int dayofweek = 1, x, height, isleapyear = ((year+1) % 4 == 0); |
1658 int daysthismonth = days[month] + (isleapyear && month == 1); | |
1658 RECTL rclArea; | 1659 RECTL rclArea; |
1659 | 1660 |
1660 /* Figure out what day of the week the first day of the month falls on */ | 1661 /* Figure out what day of the week the first day of the month falls on */ |
1661 for(x=0;x<month;x++) | 1662 for(x=0;x<month;x++) |
1662 dayofweek += days[x]; | 1663 dayofweek += days[x]; |
1705 WinPostMsg(hWnd, WM_PAINT, 0, 0); | 1706 WinPostMsg(hWnd, WM_PAINT, 0, 0); |
1706 return (MRESULT)TRUE; | 1707 return (MRESULT)TRUE; |
1707 } | 1708 } |
1708 | 1709 |
1709 /* Check all the valid days of the month */ | 1710 /* Check all the valid days of the month */ |
1710 for(x=dayofweek+7;x<(days[month]+dayofweek+7);x++) | 1711 for(x=dayofweek+7;x<(daysthismonth+dayofweek+7);x++) |
1711 { | 1712 { |
1712 RECTL rclThis = _CalendarDayRect(x, rclArea); | 1713 RECTL rclThis = _CalendarDayRect(x, rclArea); |
1713 if(pts.x < rclThis.xRight && pts.x > rclThis.xLeft && pts.y < rclThis.yTop && pts.y > rclThis.yBottom) | 1714 if(pts.x < rclThis.xRight && pts.x > rclThis.xLeft && pts.y < rclThis.yTop && pts.y > rclThis.yBottom) |
1714 { | 1715 { |
1715 dw_window_set_data(hWnd, "_dw_day", DW_INT_TO_POINTER((x-(dayofweek+7)))); | 1716 dw_window_set_data(hWnd, "_dw_day", DW_INT_TO_POINTER((x-(dayofweek+7)))); |
1726 RECTL rclPaint, rclDraw; | 1727 RECTL rclPaint, rclDraw; |
1727 char buf[100]; | 1728 char buf[100]; |
1728 int day = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_day")); | 1729 int day = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_day")); |
1729 int month = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_month")); | 1730 int month = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_month")); |
1730 int year = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_year")); | 1731 int year = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_year")); |
1731 int dayofweek = 1, x, lastmonth = 11, height; | 1732 int dayofweek = 1, x, lastmonth = 11, height, isleapyear = ((year+1) % 4 == 0); |
1732 POINTL pptl[3]; | 1733 POINTL pptl[3]; |
1733 | 1734 |
1734 /* Figure out the previous month for later use */ | 1735 /* Figure out the previous month for later use */ |
1735 if(month > 0) | 1736 if(month > 0) |
1736 lastmonth = month - 1; | 1737 lastmonth = month - 1; |
1738 /* Make the title */ | 1739 /* Make the title */ |
1739 sprintf(buf, "%s, %d", months[month], year + 1); | 1740 sprintf(buf, "%s, %d", months[month], year + 1); |
1740 | 1741 |
1741 /* Figure out what day of the week the first day of the month falls on */ | 1742 /* Figure out what day of the week the first day of the month falls on */ |
1742 for(x=0;x<month;x++) | 1743 for(x=0;x<month;x++) |
1743 dayofweek += days[x]; | 1744 dayofweek += days[x] + (isleapyear && x == 1); |
1744 dayofweek += (year/4) + year - 1; | 1745 dayofweek += (year/4) + year - 1; |
1745 dayofweek = dayofweek % 7; | 1746 dayofweek = dayofweek % 7; |
1746 | 1747 |
1747 /* Actually draw the control */ | 1748 /* Actually draw the control */ |
1748 hpsPaint = WinBeginPaint(hWnd, 0, 0); | 1749 hpsPaint = WinBeginPaint(hWnd, 0, 0); |
1806 } | 1807 } |
1807 | 1808 |
1808 /* Go through all the days */ | 1809 /* Go through all the days */ |
1809 for(x=0;x<42;x++) | 1810 for(x=0;x<42;x++) |
1810 { | 1811 { |
1812 int daysthismonth = days[month] + (isleapyear && month == 1); | |
1813 int dayslastmonth = days[lastmonth] + (isleapyear && lastmonth == 1); | |
1814 | |
1811 rclDraw = _CalendarDayRect(x+7, rclPaint); | 1815 rclDraw = _CalendarDayRect(x+7, rclPaint); |
1812 if(x < dayofweek) | 1816 if(x < dayofweek) |
1813 { | 1817 { |
1814 GpiSetColor(hpsPaint, CLR_DARKGRAY); | 1818 GpiSetColor(hpsPaint, CLR_DARKGRAY); |
1815 sprintf(buf, "%d", days[lastmonth] - (dayofweek - x - 1)); | 1819 sprintf(buf, "%d", dayslastmonth - (dayofweek - x - 1)); |
1816 } | 1820 } |
1817 else if(x - dayofweek + 1 > days[month]) | 1821 else if(x - dayofweek + 1 > daysthismonth) |
1818 { | 1822 { |
1819 GpiSetColor(hpsPaint, CLR_DARKGRAY); | 1823 GpiSetColor(hpsPaint, CLR_DARKGRAY); |
1820 sprintf(buf, "%d", x - dayofweek - days[month] + 1); | 1824 sprintf(buf, "%d", x - dayofweek - daysthismonth + 1); |
1821 } | 1825 } |
1822 else | 1826 else |
1823 { | 1827 { |
1824 GpiSetColor(hpsPaint, CLR_DARKBLUE); | 1828 GpiSetColor(hpsPaint, CLR_DARKBLUE); |
1825 sprintf(buf, "%d", x - dayofweek + 1); | 1829 sprintf(buf, "%d", x - dayofweek + 1); |