Ignore:
Timestamp:
Aug 3, 1999, 11:22:34 PM (26 years ago)
Author:
sandervl
Message:

DrawState & DrawTextEx ported by Rene Pronk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/user32/uitools.cpp

    r221 r407  
    1 /* $Id: uitools.cpp,v 1.3 1999-06-26 15:29:11 achimha Exp $ */
     1/* $Id: uitools.cpp,v 1.4 1999-08-03 21:22:33 sandervl Exp $ */
    22/*
    33 * User Interface Functions
     
    77 * Copyright 1999 Achim Hasenmueller
    88 * Copyright 1999 Christoph Bratschi
     9 * Copyright 1999 Rene Pronk
    910 */
    1011
     
    13741375    return FALSE;
    13751376}
     1377
     1378
     1379/*****************************************************************************
     1380 * Name      : int WIN32API DrawTextExA
     1381 * Purpose   : The DrawTextEx function draw the text in the rectangle
     1382 * Parameters:
     1383 * Variables :
     1384 * Result    : If the function succeeds, the return value is the height of the
     1385 *             text, otherwise zero.
     1386 * Remark    : TODO: Returned number of characters is always the entire string
     1387 *             since there is no way to know the real answer this way.
     1388 * Status    : PARTIALLY IMPLEMENTED AND TESTED
     1389 *
     1390 * Author    : Rene Pronk [Thu, 1999/07/29 15:03]
     1391 *****************************************************************************/
     1392
     1393int WIN32API DrawTextExA (HDC hdc, LPTSTR lpchText, int cchText, LPRECT lprc,
     1394                          UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams) {
     1395
     1396   int result;
     1397
     1398   dprintf(("USER32:DrawTextExA (%08xh,%s,%08xh,%08xh,%08xh,%08xh).\n",
     1399            hdc, lpchText, cchText, lprc, dwDTFormat, lpDTParams));
     1400
     1401   if (lpDTParams != NULL) {
     1402           // 'create' margins
     1403           lprc->left += lpDTParams->iLeftMargin;
     1404           lprc->right -= lpDTParams->iRightMargin;
     1405
     1406           // just assume all the text has been drawn
     1407           if (cchText != -1)
     1408                   lpDTParams->uiLengthDrawn = cchText;
     1409           else {
     1410                   // determine string length
     1411                   int size = 0;
     1412                   while ((BYTE) *(lpchText + size) != 0)
     1413                           size ++;
     1414                   lpDTParams->uiLengthDrawn = size;
     1415           }
     1416   }
     1417
     1418   result = DrawTextA (hdc, lpchText, cchText, lprc, dwDTFormat);
     1419
     1420   if (lpDTParams != NULL) {
     1421           // don't forget to restore the margins
     1422           lprc->left -= lpDTParams->iLeftMargin;
     1423           lprc->right += lpDTParams->iRightMargin;
     1424   }
     1425
     1426   return result;
     1427}
     1428
     1429
     1430
     1431/*****************************************************************************
     1432 * Name      : int WIN32API DrawTextExW
     1433 * Purpose   : The DrawTextEx function draw the text in the rectangle
     1434 * Parameters:
     1435 * Variables :
     1436 * Result    : If the function succeeds, the return value is the height of the
     1437 *             text, otherwise zero.
     1438 * Remark    : TODO: Returned number of characters is always the entire string
     1439 *             since there is no way to know the real answer this way.
     1440 * Status    : PARTIALLY IMPLEMENTED AND TESTED
     1441 *
     1442 * Author    : Rene Pronk [Thu, 1999/07/29 15:03]
     1443 *****************************************************************************/
     1444
     1445int WIN32API DrawTextExW (HDC hdc, LPTSTR lpchText, int cchText, LPRECT lprc,
     1446                          UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams) {
     1447
     1448   char *astring = UnicodeToAsciiString((LPWSTR)lpchText);
     1449   int   rc;
     1450
     1451   dprintf(("USER32:DrawTextExW (%08xh,%s,%08xh,%08xh,%08xh,%08xh).\n",
     1452            hdc, astring, cchText, lprc, dwDTFormat, lpDTParams));
     1453
     1454   rc = DrawTextExA (hdc, astring, cchText, lprc, dwDTFormat, lpDTParams);
     1455   FreeAsciiString(astring);
     1456   return(rc);
     1457}
     1458
     1459
     1460/******************************************************************************
     1461 *
     1462 * This function is used by Paint_DrawState which is inturn used by both
     1463 * DrawStateA and DrawStateW. The code is directly borrowed from Wine.
     1464 *
     1465 ******************************************************************************/
     1466
     1467static BOOL Paint_DrawStateJam(HDC hdc, UINT opcode,
     1468                               DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
     1469                               LPRECT rc, UINT dtflags, BOOL unicode)
     1470{
     1471    HDC memdc;
     1472    HBITMAP hbmsave;
     1473    BOOL retval;
     1474    INT cx = rc->right - rc->left;
     1475    INT cy = rc->bottom - rc->top;
     1476
     1477    switch(opcode)
     1478    {
     1479    case DST_TEXT:
     1480    case DST_PREFIXTEXT:
     1481        if(unicode)
     1482            return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
     1483        else
     1484            return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
     1485
     1486    case DST_ICON:
     1487        return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
     1488
     1489    case DST_BITMAP:
     1490        memdc = CreateCompatibleDC(hdc);
     1491        if(!memdc) return FALSE;
     1492        hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
     1493        if(!hbmsave)
     1494        {
     1495            DeleteDC(memdc);
     1496            return FALSE;
     1497        }
     1498        retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
     1499        SelectObject(memdc, hbmsave);
     1500        DeleteDC(memdc);
     1501        return retval;
     1502
     1503    case DST_COMPLEX:
     1504        if(func)
     1505            return func(hdc, lp, wp, cx, cy);
     1506        else
     1507            return FALSE;
     1508    }
     1509    return FALSE;
     1510}
     1511
     1512
     1513/******************************************************************************
     1514 *
     1515 * This function is used by both DrawStateA and DrawStateW. The code is directly
     1516 * borrowed from Wine
     1517 *
     1518 ******************************************************************************/
     1519BOOL Paint_DrawState (HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
     1520                      INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode)
     1521{
     1522    HBITMAP hbm, hbmsave;
     1523    HFONT hfsave;
     1524    HBRUSH hbsave;
     1525    HDC memdc;
     1526    RECT rc;
     1527    UINT dtflags = DT_NOCLIP;
     1528    COLORREF fg, bg;
     1529    UINT opcode = flags & 0xf;
     1530    INT len = wp;
     1531    BOOL retval, tmp;
     1532
     1533    if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
     1534    {
     1535        if(unicode)
     1536            len = lstrlenW((LPWSTR)lp);
     1537        else
     1538            len = lstrlenA((LPSTR)lp);
     1539   }
     1540
     1541    /* Find out what size the image has if not given by caller */
     1542    if(!cx || !cy)
     1543    {
     1544        SIZE s;
     1545        BITMAP bmp;
     1546
     1547        switch(opcode)
     1548        {
     1549        case DST_TEXT:
     1550        case DST_PREFIXTEXT:
     1551            if(unicode)
     1552                retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
     1553            else
     1554                retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
     1555            if(!retval) return FALSE;
     1556            break;
     1557
     1558        case DST_ICON:
     1559            /* Just assume the icon has the size given by GetSystemMetrics. This should be
     1560               valid since both CreateIcon and LoadIcon can't create icons with different
     1561               sizes. I wouldn't how else to get the size of the icon. RP
     1562            */
     1563            s.cx = GetSystemMetrics (SM_CXICON);
     1564            s.cy = GetSystemMetrics (SM_CYICON);
     1565            break;
     1566
     1567        case DST_BITMAP:
     1568            retval = GetObjectA ((HBITMAP) lp, sizeof (BITMAP), &bmp);
     1569            if(retval == 0) return FALSE;
     1570            s.cx = bmp.bmWidth;
     1571            s.cy = bmp.bmHeight;
     1572            break;
     1573
     1574        case DST_COMPLEX: /* cx and cy must be set in this mode */
     1575            return FALSE;
     1576        }
     1577
     1578        if(!cx) cx = s.cx;
     1579        if(!cy) cy = s.cy;
     1580    }
     1581
     1582    rc.left   = x;
     1583    rc.top    = y;
     1584    rc.right  = x + cx;
     1585    rc.bottom = y + cy;
     1586
     1587    if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
     1588        dtflags |= DT_RIGHT;
     1589    if(opcode == DST_TEXT)
     1590        dtflags |= DT_NOPREFIX;
     1591
     1592    /* For DSS_NORMAL we just jam in the image and return */
     1593    if((flags & 0x7ff0) == DSS_NORMAL)
     1594    {
     1595        return Paint_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
     1596    }
     1597
     1598    /* For all other states we need to convert the image to B/W in a local bitmap */
     1599    /* before it is displayed */
     1600    fg = SetTextColor(hdc, RGB(0, 0, 0));
     1601    bg = SetBkColor(hdc, RGB(255, 255, 255));
     1602    hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
     1603    memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
     1604    retval = FALSE; /* assume failure */
     1605
     1606    /* From here on we must use "goto cleanup" when something goes wrong */
     1607    hbm     = CreateBitmap(cx, cy, 1, 1, NULL);
     1608    if(!hbm) goto cleanup;
     1609    memdc   = CreateCompatibleDC(hdc);
     1610    if(!memdc) goto cleanup;
     1611    hbmsave = (HBITMAP)SelectObject(memdc, hbm);
     1612    if(!hbmsave) goto cleanup;
     1613    rc.left = rc.top = 0;
     1614    rc.right = cx;
     1615    rc.bottom = cy;
     1616    if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
     1617    SetBkColor(memdc, RGB(255, 255, 255));
     1618    SetTextColor(memdc, RGB(0, 0, 0));
     1619    hfsave  = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
     1620    if(!hfsave && (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)) goto cleanup;
     1621    tmp = Paint_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
     1622    if(hfsave) SelectObject(memdc, hfsave);
     1623    if(!tmp) goto cleanup;
     1624
     1625    /* These states cause the image to be dithered */
     1626    if(flags & (DSS_UNION|DSS_DISABLED))
     1627    {
     1628        WORD wPattern55AA[] = { 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa };
     1629        HBITMAP hPattern55AABitmap = CreateBitmap( 8, 8, 1, 1, wPattern55AA );
     1630        HBRUSH hPattern55AABrush = CreatePatternBrush(hPattern55AABitmap);
     1631
     1632        hbsave = (HBRUSH)SelectObject(memdc, hPattern55AABrush);
     1633        if(!hbsave) goto cleanup;
     1634        tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
     1635        if(hbsave) SelectObject(memdc, hbsave);
     1636        if(!tmp) goto cleanup;
     1637    }
     1638
     1639    hbsave = (HBRUSH)SelectObject(hdc, hbr ? hbr : GetStockObject(WHITE_BRUSH));
     1640    if(!hbsave) goto cleanup;
     1641
     1642    if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
     1643
     1644    /* DSS_DEFAULT makes the image boldface */
     1645    if(flags & DSS_DEFAULT)
     1646    {
     1647        if(!BitBlt(hdc, x+1, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
     1648    }
     1649
     1650    retval = TRUE; /* We succeeded */
     1651
     1652cleanup:
     1653    SetTextColor(hdc, fg);
     1654    SetBkColor(hdc, bg);
     1655
     1656    if(hbsave)  SelectObject(hdc, hbsave);
     1657    if(hbmsave) SelectObject(memdc, hbmsave);
     1658    if(hbm)     DeleteObject(hbm);
     1659    if(memdc)   DeleteDC(memdc);
     1660
     1661    return retval;
     1662}
     1663
     1664
     1665
     1666/*****************************************************************************
     1667 * Name      : BOOL WIN32API DrawStateA
     1668 * Purpose   : Draws a bitmap, icon, text or complex object in one of the following
     1669 *             states: normal, disabled, union or mono
     1670 * Parameters:
     1671 * Variables :
     1672 * Result    : If the function succeeds, the return value is a handle of the
     1673 *               window station associated with the calling process.
     1674 *             If the function fails, the return value is NULL. This can occur
     1675 *               if the calling process is not an application written for Windows
     1676 *               NT. To get extended error information, call GetLastError.
     1677 * Remark    : Disabled doesn't look like the Windows implementation, but it does
     1678 *             look disabled.
     1679 * Status    : PARTIALLY IMPLEMENTED
     1680 *
     1681 * Author    : Rene Pronk [Sun, 1999/07/25 21:27]
     1682 *****************************************************************************/
     1683
     1684BOOL WIN32API DrawStateA(HDC hdc, HBRUSH hbc, DRAWSTATEPROC lpOutputFunc, LPARAM lData,
     1685                         WPARAM wData, int x, int y, int cx, int cy, UINT fuFlags)
     1686{
     1687  dprintf(("USER32:DrawStateA (%08xh,%08xh,%08xh,%08xh,%08xh,%d,%d,%d,%d,%08x).\n",
     1688         hdc, hbc, lpOutputFunc, lData, wData, x, y, cx, cy, fuFlags));
     1689
     1690  return Paint_DrawState (hdc, hbc, lpOutputFunc, lData, wData, x, y ,cx, cy, fuFlags, FALSE);
     1691}
     1692
     1693
     1694
     1695/*****************************************************************************
     1696 * Name      : BOOL WIN32API DrawStateW
     1697 * Purpose   : Draws a bitmap, icon, text or complex object in one of the following
     1698 *             states: normal, disabled, union or mono
     1699 * Parameters:
     1700 * Variables :
     1701 * Result    : If the function succeeds, the return value is a handle of the
     1702 *               window station associated with the calling process.
     1703 *             If the function fails, the return value is NULL. This can occur
     1704 *               if the calling process is not an application written for Windows
     1705 *               NT. To get extended error information, call GetLastError.
     1706 * Remark    : Disabled doesn't look like the Windows implementation, but it does
     1707 *             look disabled.
     1708 * Status    : PARTIALLY IMPLEMENTED
     1709 *
     1710 * Author    : Rene Pronk [Sun, 1999/07/25 21:27]
     1711 *****************************************************************************/
     1712
     1713BOOL WIN32API DrawStateW(HDC hdc, HBRUSH hbc, DRAWSTATEPROC lpOutputFunc, LPARAM lData,
     1714                         WPARAM wData, int x, int y, int cx, int cy, UINT fuFlags)
     1715{
     1716  dprintf(("USER32:DrawStateW (%08xh,%08xh,%08xh,%08xh,%08xh,%d,%d,%d,%d,%08x).\n",
     1717         hdc, hbc, lpOutputFunc, lData, wData, x, y, cx, cy, fuFlags));
     1718
     1719  return Paint_DrawState (hdc, hbc, lpOutputFunc, lData, wData, x, y ,cx, cy, fuFlags, TRUE);
     1720}
Note: See TracChangeset for help on using the changeset viewer.