Mercurial > dwindows
comparison os2/dw.c @ 1344:af989b9f1767
Initial try at a custom calendar widget for OS/2.... not finished...
But I needed to switch computers... fixes coming soon.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 15 Nov 2011 01:59:00 +0000 |
parents | b4a23eab81fa |
children | e29e24af9b6d |
comparison
equal
deleted
inserted
replaced
1343:d4c874f13e9b | 1344:af989b9f1767 |
---|---|
1603 ptl2.y = rclPaint.yTop - 1; | 1603 ptl2.y = rclPaint.yTop - 1; |
1604 GpiMove(hpsPaint, &ptl1); | 1604 GpiMove(hpsPaint, &ptl1); |
1605 GpiLine(hpsPaint, &ptl2); | 1605 GpiLine(hpsPaint, &ptl2); |
1606 } | 1606 } |
1607 | 1607 |
1608 | |
1609 #define CALENDAR_BORDER 3 | |
1610 | |
1611 /* Returns a rectangle for a single day on the calendar */ | |
1612 RECTL _CalendarDayRect(int position, RECTL rclPaint) | |
1613 { | |
1614 int height = rclPaint.yTop - rclPaint.yBottom - (CALENDAR_BORDER*2); | |
1615 int width = rclPaint.xRight - rclPaint.xLeft - (CALENDAR_BORDER*2); | |
1616 /* There are 7 rows... 5 for the day numbers... | |
1617 * 1 for the Month/Year and 1 for the day names. | |
1618 */ | |
1619 int row = position / 7; | |
1620 int col = position % 7; | |
1621 int cellwidth = height / 7; | |
1622 int cellheight = width / 7; | |
1623 | |
1624 /* Create a new box */ | |
1625 rclPaint.xLeft = cellwidth * col; | |
1626 rclPaint.xRight = rclPaint.xLeft + cellwidth; | |
1627 /* We only handle 6 of the 7 rows */ | |
1628 rclPaint.yBottom = cellheight * (6-row); | |
1629 rclPaint.yTop = rclPaint.yBottom + cellheight; | |
1630 dw_debug("Cell height %d width %d position %d row %d, col %d (%d,%d)-(%d,%d)\n", cellheight, cellwidth, position, row, col, rclPaint.xLeft, rclPaint.yBottom, rclPaint.xRight, rclPaint.yTop); | |
1631 return rclPaint; | |
1632 } | |
1633 | |
1634 /* This procedure handles drawing of a status border */ | |
1635 MRESULT EXPENTRY _calendarproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2) | |
1636 { | |
1637 WindowData *blah = (WindowData *)WinQueryWindowPtr(hWnd, QWP_USER); | |
1638 PFNWP oldproc = 0; | |
1639 | |
1640 if(blah) | |
1641 { | |
1642 oldproc = blah->oldproc; | |
1643 | |
1644 switch(msg) | |
1645 { | |
1646 case WM_PAINT: | |
1647 { | |
1648 HPS hpsPaint; | |
1649 RECTL rclPaint, rclDraw; | |
1650 char buf[100], font[50] = { 0 }; | |
1651 /* How many days are in each month usually (not including leap years) */ | |
1652 static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | |
1653 static char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; | |
1654 static char *daysofweek[] = { "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday" }; | |
1655 int day = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_day")); | |
1656 int month = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_month")); | |
1657 int year = DW_POINTER_TO_INT(dw_window_get_data(hWnd, "_dw_year")); | |
1658 int dayofweek = 1, x, lastmonth = 11; | |
1659 | |
1660 /* Figure out the previous month for later use */ | |
1661 if(month > 0) | |
1662 lastmonth = month - 1; | |
1663 | |
1664 dw_debug("day %d month %d year %d lastmonth %d\n", day, month, year, lastmonth); | |
1665 | |
1666 #if 0 | |
1667 /* Check the current font and create a bold one to draw the month and selected */ | |
1668 if(WinQueryPresParam(hWnd, PP_FONTNAMESIZE, 0, NULL, 50, font, QPF_NOINHERIT)) | |
1669 { | |
1670 strcpy(buf, font); | |
1671 /* Check to make sure it isn't already bold */ | |
1672 if(!strstr(buf, " Bold")) | |
1673 strcat(buf, " Bold"); | |
1674 WinSetPresParam(hWnd, PP_FONTNAMESIZE, strlen(buf)+1, buf); | |
1675 } | |
1676 | |
1677 dw_debug("Original font %s bold font %s\n", font, buf); | |
1678 #endif | |
1679 | |
1680 /* Make the title */ | |
1681 sprintf(buf, "%s, %d", months[month], year + 1); | |
1682 | |
1683 /* Figure out what day of the week the first day of the month falls on */ | |
1684 for(x=0;x<month;x++) | |
1685 dayofweek += days[x]; | |
1686 dayofweek += (year/4) + year - 1; | |
1687 dayofweek = dayofweek % 7; | |
1688 | |
1689 dw_debug("Title %s dayofweek %d\n", buf, dayofweek); | |
1690 | |
1691 /* Actually draw the control */ | |
1692 hpsPaint = WinBeginPaint(hWnd, 0, 0); | |
1693 WinQueryWindowRect(hWnd, &rclPaint); | |
1694 WinFillRect(hpsPaint, &rclPaint, CLR_PALEGRAY); | |
1695 | |
1696 /* Draw the Month and Year at the top */ | |
1697 GpiSetColor(hpsPaint, CLR_BLACK); | |
1698 rclDraw = rclPaint; | |
1699 rclDraw.yBottom = ((rclDraw.yTop - (CALENDAR_BORDER*2))/7) * 6; | |
1700 WinDrawText(hpsPaint, -1, (PCH)buf, &rclDraw, DT_TEXTATTRS, DT_TEXTATTRS, DT_VCENTER | DT_CENTER | DT_TEXTATTRS); | |
1701 | |
1702 #if 0 | |
1703 dw_debug("Restoring font\n"); | |
1704 /* Restore the original font */ | |
1705 WinSetPresParam(hWnd, PP_FONTNAMESIZE, strlen(font)+1, font); | |
1706 #endif | |
1707 | |
1708 /* Draw a border around control */ | |
1709 GpiSetColor(hpsPaint, CLR_DARKGRAY); | |
1710 _Top(hpsPaint, rclPaint); | |
1711 _Left(hpsPaint, rclPaint); | |
1712 /* With shadow */ | |
1713 GpiSetColor(hpsPaint, CLR_WHITE); | |
1714 _Right(hpsPaint, rclPaint); | |
1715 _Bottom(hpsPaint, rclPaint); | |
1716 | |
1717 /* Draw the days of the week */ | |
1718 GpiSetColor(hpsPaint, CLR_DARKBLUE); | |
1719 for(x=0;x<7;x++) | |
1720 { | |
1721 rclDraw = _CalendarDayRect(x, rclPaint); | |
1722 dw_debug("Pos %d day of week %s\n", x, daysofweek[x]); | |
1723 WinDrawText(hpsPaint, -1, (PCH)daysofweek[x], &rclDraw, DT_TEXTATTRS, DT_TEXTATTRS, DT_VCENTER | DT_CENTER | DT_TEXTATTRS); | |
1724 } | |
1725 | |
1726 /* Go through all the days */ | |
1727 for(x=0;x<35;x++) | |
1728 { | |
1729 rclDraw = _CalendarDayRect(x+7, rclPaint); | |
1730 if(x < dayofweek) | |
1731 sprintf(buf, "%d", days[lastmonth] - (dayofweek - x - 1)); | |
1732 else if(x - dayofweek + 1 > days[month]) | |
1733 sprintf(buf, "%d", x - dayofweek - days[month] + 1); | |
1734 else | |
1735 sprintf(buf, "%d", x - dayofweek + 1); | |
1736 dw_debug("Pos %d day %s\n", x, buf); | |
1737 WinDrawText(hpsPaint, -1, (PCH)buf, &rclDraw, DT_TEXTATTRS, DT_TEXTATTRS, DT_VCENTER | DT_CENTER | DT_TEXTATTRS); | |
1738 } | |
1739 | |
1740 /* Draw a border around selected day */ | |
1741 rclPaint = _CalendarDayRect(day + dayofweek + 8, rclPaint); | |
1742 GpiSetColor(hpsPaint, CLR_DARKGRAY); | |
1743 _Top(hpsPaint, rclPaint); | |
1744 _Left(hpsPaint, rclPaint); | |
1745 /* With shadow */ | |
1746 GpiSetColor(hpsPaint, CLR_WHITE); | |
1747 _Right(hpsPaint, rclPaint); | |
1748 _Bottom(hpsPaint, rclPaint); | |
1749 dw_debug("Done\n"); | |
1750 | |
1751 WinEndPaint(hpsPaint); | |
1752 | |
1753 return (MRESULT)TRUE; | |
1754 } | |
1755 } | |
1756 if(oldproc) | |
1757 return oldproc(hWnd, msg, mp1, mp2); | |
1758 } | |
1759 | |
1760 return WinDefWindowProc(hWnd, msg, mp1, mp2); | |
1761 } | |
1762 | |
1763 | |
1608 /* This procedure handles drawing of a status border */ | 1764 /* This procedure handles drawing of a status border */ |
1609 MRESULT EXPENTRY _statusproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2) | 1765 MRESULT EXPENTRY _statusproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2) |
1610 { | 1766 { |
1611 PFNWP *blah = WinQueryWindowPtr(hWnd, QWP_USER); | 1767 PFNWP *blah = WinQueryWindowPtr(hWnd, QWP_USER); |
1612 | 1768 |
11208 * text: The text to be display by the static text widget. | 11364 * text: The text to be display by the static text widget. |
11209 * id: An ID to be used with dw_window_from_id() or 0L. | 11365 * id: An ID to be used with dw_window_from_id() or 0L. |
11210 */ | 11366 */ |
11211 HWND API dw_calendar_new(ULONG id) | 11367 HWND API dw_calendar_new(ULONG id) |
11212 { | 11368 { |
11213 char *text = "dummy calendar"; | |
11214 WindowData *blah = calloc(sizeof(WindowData), 1); | 11369 WindowData *blah = calloc(sizeof(WindowData), 1); |
11370 DATETIME dt; | |
11215 HWND tmp = WinCreateWindow(HWND_OBJECT, | 11371 HWND tmp = WinCreateWindow(HWND_OBJECT, |
11216 WC_STATIC, | 11372 WC_STATIC, |
11217 (PSZ)text, | 11373 NULL, |
11218 WS_VISIBLE | SS_TEXT, | 11374 WS_VISIBLE | SS_TEXT, |
11219 0,0,2000,1000, | 11375 0,0,2000,1000, |
11220 NULLHANDLE, | 11376 NULLHANDLE, |
11221 HWND_TOP, | 11377 HWND_TOP, |
11222 id, | 11378 id, |
11223 NULL, | 11379 NULL, |
11224 NULL); | 11380 NULL); |
11225 blah->oldproc = WinSubclassWindow(tmp, _textproc); | 11381 blah->oldproc = WinSubclassWindow(tmp, _calendarproc); |
11226 WinSetWindowPtr(tmp, QWP_USER, blah); | 11382 WinSetWindowPtr(tmp, QWP_USER, blah); |
11227 dw_window_set_font(tmp, DefaultFont); | 11383 dw_window_set_font(tmp, DefaultFont); |
11228 dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_PALEGRAY); | 11384 if(!DosGetDateTime(&dt)) |
11229 WinSetWindowText(tmp, (PSZ)text); | 11385 dw_calendar_set_date(tmp, dt.year, dt.month, dt.day); |
11230 return tmp; | 11386 return tmp; |
11231 } | 11387 } |
11232 | 11388 |
11233 /* | 11389 /* |
11234 * The following are stubs | 11390 * The following are stubs |
11235 */ | 11391 */ |
11236 void API dw_calendar_set_date( HWND window, unsigned int year, unsigned int month, unsigned int day ) | 11392 void API dw_calendar_set_date( HWND window, unsigned int year, unsigned int month, unsigned int day ) |
11237 { | 11393 { |
11238 char tmp[30]; | 11394 /* Need to be 0 based */ |
11239 sprintf( tmp, "%4.4d-%2.2d-%2.2d", year, month, day); | 11395 if(year > 0) |
11240 WinSetWindowText(window, (PSZ)tmp); | 11396 year--; |
11397 if(month > 0) | |
11398 month--; | |
11399 if(day > 0) | |
11400 day--; | |
11401 | |
11402 dw_window_set_data(window, "_dw_year", DW_INT_TO_POINTER(year)); | |
11403 dw_window_set_data(window, "_dw_month", DW_INT_TO_POINTER(month)); | |
11404 dw_window_set_data(window, "_dw_day", DW_INT_TO_POINTER(day)); | |
11405 /* Make it redraw */ | |
11406 WinPostMsg(window, WM_PAINT, 0, 0); | |
11241 } | 11407 } |
11242 | 11408 |
11243 void API dw_calendar_get_date( HWND window, unsigned int *year, unsigned int *month, unsigned int *day ) | 11409 void API dw_calendar_get_date( HWND window, unsigned int *year, unsigned int *month, unsigned int *day ) |
11244 { | 11410 { |
11245 window = window; | 11411 if(year) |
11246 *year = *month = *day = 0; | 11412 *year = DW_POINTER_TO_UINT(dw_window_get_data(window, "_dw_year")) + 1; |
11247 } | 11413 if(month) |
11414 *month = DW_POINTER_TO_UINT(dw_window_get_data(window, "_dw_month")) + 1; | |
11415 if(day) | |
11416 *day = DW_POINTER_TO_UINT(dw_window_get_data(window, "_dw_day")) + 1; | |
11417 } |