Annotation of pmsdk/samples/petzold/chap12/poepoem.c, revision 1.1.1.1

1.1       root        1: /*--------------------------------------------------------
                      2:    POEPOEM.C -- Demonstrates Programmer-Defined Resources
                      3:   --------------------------------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: #define INCL_DOS
                      8: #include <os2.h>
                      9: #include <stdlib.h>
                     10: #include "poepoem.h"
                     11: 
                     12: MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     13: 
                     14: int main (void)
                     15:      {
                     16:      static CHAR  szClientClass [10] ;
                     17:      static CHAR  szTitleBar [40] ;
                     18:      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
                     19:                                  FCF_SIZEBORDER    | FCF_MINMAX   |
                     20:                                  FCF_SHELLPOSITION | FCF_TASKLIST |
                     21:                                  FCF_VERTSCROLL    | FCF_ICON ;
                     22:      HAB          hab ;
                     23:      HMQ          hmq ;
                     24:      HWND         hwndFrame, hwndClient ;
                     25:      QMSG         qmsg ;
                     26: 
                     27:      hab = WinInitialize (0) ;
                     28:      hmq = WinCreateMsgQueue (hab, 0) ;
                     29: 
                     30:      WinLoadString (hab, NULL, IDS_CLASS, sizeof szClientClass, szClientClass);
                     31:      WinLoadString (hab, NULL, IDS_TITLE, sizeof szTitleBar,    szTitleBar) ;
                     32: 
                     33:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     34: 
                     35:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
                     36:                                      &flFrameFlags, szClientClass, szTitleBar,
                     37:                                      0L, NULL, ID_RESOURCE, &hwndClient) ;
                     38: 
                     39:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     40:           WinDispatchMsg (hab, &qmsg) ;
                     41: 
                     42:      WinDestroyWindow (hwndFrame) ;
                     43:      WinDestroyMsgQueue (hmq) ;
                     44:      WinTerminate (hab) ;
                     45:      return 0 ;
                     46:      }
                     47: 
                     48: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                     49:      {
                     50:      static HWND   hwndScroll ;
                     51:      static PCHAR  pResource ;
                     52:      static SEL    selResource ;
                     53:      static SHORT  cxClient, cyClient, cxChar, cyChar, cyDesc,
                     54:                    sScrollPos, sNumLines ;
                     55:      FONTMETRICS   fm ;
                     56:      HPS           hps ;
                     57:      PCHAR         pText ;
                     58:      POINTL        ptl ;
                     59:      SHORT         sLineLength, sLine ;
                     60:      ULONG         ulSegSize ;
                     61:      
                     62:      switch (msg)
                     63:           {
                     64:           case WM_CREATE:
                     65: 
                     66:                     /*-----------------------------------------
                     67:                        Load the resource, get size and address
                     68:                       -----------------------------------------*/
                     69: 
                     70:                DosGetResource (NULL, IDT_TEXT, IDT_POEM, &selResource) ;
                     71:                DosSizeSeg (selResource, &ulSegSize) ;
                     72:                pResource = MAKEP (selResource, 0) ;
                     73: 
                     74:                     /*-----------------------------------------------
                     75:                        Determine how many text lines are in resource
                     76:                       -----------------------------------------------*/
                     77: 
                     78:                pText = pResource ;
                     79: 
                     80:                while (pText - pResource < (USHORT) ulSegSize)
                     81:                     {
                     82:                     if (*pText == '\0' || *pText == '\x1A')
                     83:                          break ;
                     84: 
                     85:                     if (*pText == '\r')
                     86:                          sNumLines ++ ;
                     87: 
                     88:                     pText++ ;
                     89:                     }
                     90: 
                     91:                     /*------------------------------------------
                     92:                        Initialize scroll bar range and position
                     93:                       ------------------------------------------*/
                     94: 
                     95:                hwndScroll = WinWindowFromID (
                     96:                                    WinQueryWindow (hwnd, QW_PARENT, FALSE),
                     97:                                    FID_VERTSCROLL) ;
                     98: 
                     99:                WinSendMsg (hwndScroll, SBM_SETSCROLLBAR,
                    100:                                        MPFROM2SHORT (sScrollPos, 0),
                    101:                                        MPFROM2SHORT (0, sNumLines - 1)) ;
                    102: 
                    103:                     /*----------------------
                    104:                        Query character size
                    105:                       ----------------------*/
                    106: 
                    107:                hps = WinGetPS (hwnd) ;
                    108: 
                    109:                GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
                    110:                cxChar = (SHORT) fm.lAveCharWidth ;
                    111:                cyChar = (SHORT) fm.lMaxBaselineExt ;
                    112:                cyDesc = (SHORT) fm.lMaxDescender ;
                    113: 
                    114:                WinReleasePS (hps) ;
                    115:                return 0 ;
                    116: 
                    117:           case WM_SIZE:
                    118:                cxClient = SHORT1FROMMP (mp2) ;
                    119:                cyClient = SHORT2FROMMP (mp2) ;
                    120:                return 0 ;
                    121: 
                    122:           case WM_CHAR:
                    123:                return WinSendMsg (hwndScroll, msg, mp1, mp2) ;
                    124: 
                    125:           case WM_VSCROLL:
                    126:                switch (SHORT2FROMMP (mp2))
                    127:                     {
                    128:                     case SB_LINEUP:
                    129:                          sScrollPos -= 1 ;
                    130:                          break ;
                    131: 
                    132:                     case SB_LINEDOWN:
                    133:                          sScrollPos += 1 ;
                    134:                          break ;
                    135: 
                    136:                     case SB_PAGEUP:
                    137:                          sScrollPos -= cyClient / cyChar ;
                    138:                          break ;
                    139: 
                    140:                     case SB_PAGEDOWN:
                    141:                          sScrollPos += cyClient / cyChar ;
                    142:                          break ;
                    143: 
                    144:                     case SB_SLIDERPOSITION:
                    145:                          sScrollPos = SHORT1FROMMP (mp2) ;
                    146:                          break ;
                    147:                     }
                    148:                sScrollPos = max (0, min (sScrollPos, sNumLines - 1)) ;
                    149: 
                    150:                if (sScrollPos != (SHORT) WinSendMsg (hwndScroll,
                    151:                                                      SBM_QUERYPOS, 0L, 0L))
                    152:                     {
                    153:                     WinSendMsg (hwndScroll, SBM_SETPOS,
                    154:                                 MPFROM2SHORT (sScrollPos, 0), NULL) ;
                    155:                     WinInvalidateRect (hwnd, NULL, FALSE) ;
                    156:                     }
                    157:                return 0 ;
                    158: 
                    159:           case WM_PAINT:
                    160:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    161:                GpiErase (hps) ;
                    162: 
                    163:                pText = pResource ;
                    164: 
                    165:                for (sLine = 0 ; sLine < sNumLines ; sLine++)
                    166:                     {
                    167:                     sLineLength = 0 ;
                    168: 
                    169:                     while (pText [sLineLength] != '\r')
                    170:                          sLineLength ++ ;
                    171: 
                    172:                     ptl.x = cxChar ;
                    173:                     ptl.y = cyClient - cyChar * (sLine + 1 - sScrollPos)
                    174:                                      + cyDesc ;
                    175:                     
                    176:                     GpiCharStringAt (hps, &ptl, (LONG) sLineLength, pText) ;
                    177: 
                    178:                     pText += sLineLength + 2 ;
                    179:                     }
                    180:                WinEndPaint (hps) ;
                    181:                return 0 ;
                    182: 
                    183:           case WM_DESTROY:
                    184:                DosFreeSeg (selResource) ;
                    185:                return 0 ;
                    186:           }
                    187:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    188:      }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.