home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DLGHIGH.ZIP / DLGHIGH.C < prev    next >
Text File  |  1990-11-17  |  18KB  |  440 lines

  1. /*══════════════════════════════════════════════════════════════════════════╕
  2.  │                                                                          │
  3.  │            D I A L O G . C - Demonstrate LS_OWNERDRAW Listbox            │
  4.  │                                                                          │
  5.  ╘══════════════════════════════════════════════════════════════════════════*/
  6. /*══════════════════════════════════════════════════════════════════════════╕
  7.  │                                                                          │
  8.  │                  Copyright (c) Bunker Hill Software 1990                 │
  9.  │                                                                          │
  10.  │                           Bunker Hill Software                           │
  11.  │                              P.O. Box 18138                              │
  12.  │                       Encino, California 91416-8138                      │
  13.  │                                                                          │
  14.  ╘══════════════════════════════════════════════════════════════════════════*/
  15. /*══════════════════════════════════════════════════════════════════════════╕
  16.  │                    D I A L O G . C    E D I T    L O G                   │
  17.  │                                                                          │
  18.  │ Edit        Date        Person  Modification                             │
  19.  ╘══════════════════════════════════════════════════════════════════════════╛
  20.    dlghigh.c   11/07/90    ajd     create basic window, setup dialog, etc...
  21.  
  22.    FillListBox 11/10/90    ajd     create function to fill the list box.
  23.                                    also create .txt file with news headings
  24.                                    in it. headings came from comtex news
  25.                                    several months previous.
  26.  
  27.    fonts       11/10/90    ajd     problems getting fonts set properly.
  28.                                    documentation for WinSetPresParm () seems
  29.                                    unclear and not complete.
  30.  
  31.    fonts       11/12/90    ajd     WinSetPresParam () needs to have the
  32.                                    font passed to it as "8.Courier" with
  33.                                    PP_FONTNAMESIZE. MS docs say this is
  34.                                    to be used for font size, I was assuming
  35.                                    that PP_FONTHANDLE was the font name...
  36.  
  37.    SubStr      11/13/90    ajd     routine to search for substring withing
  38.                                    a string. takes two pointers to type
  39.                                    char.
  40.  
  41.    hps         11/14/90    ajd     change WinGetPS () to after WinSetPresParm
  42.                                    so it doesn't get the old font settings.
  43.  
  44.    SYSCLR      11/15/90    ajd     change the color using SYSCLR_HELPHILITE
  45.                                    so that it will work right for all color
  46.                                    settings.
  47.  
  48.  *══════════════════════════════════════════════════════════════════════════*/
  49. /*══════════════════════════════════════════════════════════════════════════╕
  50.  │ Author:     Alan DuBoff, CIS ID: 76662,660                               │
  51.  │   Date:     November, 1990                                               │
  52.  ╘══════════════════════════════════════════════════════════════════════════*/
  53. /*══════════════════════════════════════════════════════════════════════════╕
  54.  │                                                        F U N C T I O N S │
  55.  │    SYNOPSIS:    allow for multiple color inside a      ───────────────── │
  56.  │                 listbox. concept could be used for multiple fonts also.  │
  57.  │ DESCRIPTION:    not only have multiple colors, but to have multiple      │
  58.  │                 colors in a single listbox item. it would be easier      │
  59.  │                 to use different colors for each item, but I needed      │
  60.  │                 to have a Keyword highlited.                             │
  61.  │     RETURNS:    Nothing                                                  │
  62.  │                                                                          │
  63.  │    CAUTIONS:    Use Routines at your own risk!                           │
  64.  │                                                                          │
  65.  │                                                                          │
  66.  │    Special Thanks to Guy Scharf who helped me with unclear documentation │
  67.  │    inside the Microsoft PM Toolkit 1.21 and IBM Toolkit 1.2. Guy Scharf  │
  68.  │    can be reached on Compu$erve as ID: 76702,557                         │
  69.  ╘══════════════════════════════════════════════════════════════════════════*/
  70.  
  71. #define INCL_WIN
  72. #define INCL_GPI
  73. #include <os2.h>
  74. #include <malloc.h>
  75. #include <math.h>
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include <ctype.h>
  80. #include "dlghigh.h"
  81.  
  82. MRESULT EXPENTRY ClientWndProc     (HWND, USHORT, MPARAM, MPARAM);
  83. MRESULT EXPENTRY SelectDlgProc     (HWND, USHORT, MPARAM, MPARAM);
  84. SHORT            FillListBox       (HWND);
  85. SHORT            SubStr            (CHAR *, CHAR *);
  86.  
  87. CHAR       szClientClass[] = "Dlghigh";
  88. HWND       hwndFrame;
  89. PHWND      hwndClient;
  90. BOOL       fDialogOpen = FALSE;
  91.  
  92. int main (void)
  93. {
  94.    static ULONG flFrameFlags = FCF_TITLEBAR        | FCF_SYSMENU   |
  95.                                FCF_SIZEBORDER      | FCF_MINMAX    |
  96.                                FCF_TASKLIST        | FCF_ACCELTABLE|
  97.                                FCF_SHELLPOSITION   | FCF_MENU;
  98.    
  99.    HAB         hab; 
  100.    HMQ         hmq;
  101.    QMSG        qmsg;
  102.    BOOL        fSuccess;
  103.                
  104.    hab = WinInitialize (0);
  105.    hmq = WinCreateMsgQueue (hab, 0);
  106.  
  107.    WinRegisterClass (
  108.                    hab,
  109.                    szClientClass,
  110.                    ClientWndProc,
  111.                    CS_SIZEREDRAW,
  112.                    0);
  113.  
  114.    hwndFrame = WinCreateStdWindow (
  115.                    HWND_DESKTOP,
  116.                    WS_VISIBLE,
  117.                    &flFrameFlags,
  118.                    szClientClass,
  119.                    NULL,
  120.                    0,
  121.                    0,
  122.                    ID_DIALOG,
  123.                    &hwndClient);
  124.  
  125.    fSuccess = WinSetWindowPos (
  126.                    hwndFrame,
  127.                    NULL,
  128.                    0,
  129.                    0,
  130.                    0,
  131.                    0,
  132.                    SWP_MAXIMIZE);
  133.  
  134.    if (hwndFrame != NULL)
  135.    {
  136.        while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  137.            WinDispatchMsg (hab, &qmsg);
  138.  
  139.        WinDestroyWindow (hwndFrame);
  140.        WinDestroyMsgQueue (hmq);
  141.        WinTerminate (hab);
  142.        return 0;
  143.    }
  144. }
  145.  
  146. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  147. {
  148.    static HWND     hwndMenu;
  149.    HPS             hps;
  150.  
  151.    switch (msg)
  152.    {
  153.        case WM_CREATE:
  154.  
  155.            hwndMenu = WinWindowFromID (
  156.                        WinQueryWindow (hwnd, QW_PARENT, FALSE), FID_MENU);
  157.  
  158.            return 0;
  159.  
  160.        case WM_COMMAND:
  161.            switch (COMMANDMSG(&msg)->cmd)
  162.            {
  163.                case IDM_OPENDLG:
  164.  
  165.                    if (!fDialogOpen)
  166.                        WinDlgBox (
  167.                                hwnd,
  168.                                hwnd,
  169.                                SelectDlgProc,
  170.                                0,          
  171.                                IDD_DIALOG,
  172.                                NULL);
  173.                    else
  174.                        WinMessageBox (HWND_DESKTOP, hwnd,
  175.                                "Dialog already open", "Dialog Error", 0,
  176.                                MB_NOICON | MB_OK);
  177.  
  178.  
  179.                    return 0;
  180.  
  181.                case IDM_EXIT:
  182.  
  183.                    if (MBID_OK == WinMessageBox (
  184.                                    HWND_DESKTOP,
  185.                                    hwndClient,
  186.                                    "Really want to end program?",
  187.                                    szClientClass,
  188.                                    0,
  189.                                    MB_OKCANCEL | MB_ICONQUESTION))
  190.                        WinSendMsg (hwnd, WM_CLOSE, 0L, 0L);
  191.  
  192.                    return 0;
  193.            }
  194.            break;
  195.  
  196.        case WM_PAINT:
  197.            hps = WinBeginPaint (hwnd, NULL, NULL);
  198.            GpiErase (hps);
  199.            WinEndPaint (hps);
  200.            return 0;
  201.    }
  202.    return WinDefWindowProc (hwnd, msg, mp1, mp2);
  203. }
  204.  
  205. SHORT FillListBox (HWND hwnd)
  206. {
  207.    FILE        *ItemsFILE;
  208.    CHAR        szItemText[78];
  209.    SHORT       sDone = 0, sIsEOF;
  210.  
  211.    WinSendDlgItemMsg (hwnd, IDD_LIST, LM_DELETEALL, NULL, NULL);
  212.  
  213.    if ((ItemsFILE = fopen ("items.txt", "rb")) == NULL)
  214.    {
  215.        return 1;
  216.    }
  217.  
  218.    while (!sDone)
  219.    {
  220.        fread (szItemText, 77, 1, ItemsFILE);
  221.        sIsEOF = feof (ItemsFILE);
  222.        if (sIsEOF)
  223.        {
  224.            sDone = TRUE;
  225.            break;
  226.        }
  227.        szItemText [77] = '\0';
  228.        WinSendDlgItemMsg (hwnd, IDD_LIST, LM_INSERTITEM, 0, szItemText);
  229.    }
  230.    fclose (ItemsFILE);
  231.    return 0;
  232. }
  233.  
  234. MRESULT EXPENTRY SelectDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  235. {
  236.    HPS             hps;                /* handle to presentation space */
  237.    FONTMETRICS     fm;                 /* fontmetric structure         */
  238.    CHAR            szItemText[80],     /* pointer to text for items    */
  239.                    szTemp[45],         /* asciiz string for message box*/
  240.                    szKeyword[]="SELL"; /* keyword to highlight         */
  241.    OWNERITEM FAR   *poi;               /* Pointer to OWNERITEM struct  */
  242.    RECTL           rcl;                /* Rectangle for WM_DRAWITEM    */
  243.    COLOR           clrFG, clrBG;       /* Foreground/Background colors */
  244.    SHORT           sError, sKeyWordPos;
  245.    MRESULT         mrItem;
  246.    static LONG     lAveChWidth;
  247.  
  248.    switch (msg)
  249.    {
  250.        case WM_INITDLG:    /* this message is sent after WM_MEASUREITEM
  251.                             */
  252.  
  253.            sError = FillListBox (hwnd);    /* fill list box            */
  254.  
  255.            if (sError)                     /* if error, report         */
  256.            {
  257.                WinMessageBox (HWND_DESKTOP, hwnd,
  258.                        "Can't open ITEMS.TXT File", "File I/O", 0,
  259.                        MB_NOICON | MB_OK);
  260.                WinDismissDlg (hwnd, TRUE);
  261.            }
  262.            else
  263.                fDialogOpen = TRUE;
  264.  
  265.            break;
  266.  
  267.        case WM_MEASUREITEM:
  268.            /*  this message comes previous to WM_INITDLG and needs
  269.                to have the height of the dialog item you will be
  270.                drawing. note i'm setting the fontsize previous to
  271.                getting the presentation space. this is important
  272.                or the values you will receive will be for the
  273.                previous settings.
  274.             */
  275.  
  276.            /*  this sets the font to 8 point Courier, you must
  277.                have Courier Fonts on the computer to run this
  278.                example.
  279.             */
  280.            WinSetPresParam (hwnd,
  281.                            PP_FONTNAMESIZE,
  282.                            24L,
  283.                            (PVOID) "8.Courier");
  284.  
  285.            hps = WinGetPS (hwnd);          /* get presentation space   */
  286.                                            
  287.                                            /* get fontmetrics info     */
  288.            GpiQueryFontMetrics (hps, (LONG) sizeof (FONTMETRICS), &fm);
  289.  
  290.                                            /* store width for later    */
  291.            lAveChWidth = fm.lAveCharWidth;
  292.  
  293.            WinReleasePS (hps);             /* release pres space       */
  294.  
  295.            /* notice i'm adding 2 to this return value so in this
  296.               example i have one extra row of pels on each side
  297.               of the listbox item. i am centering the item when
  298.               i'm drawing it (DT_VCENTER) so it will be easier
  299.               to read this small font i'm using.
  300.             */
  301.  
  302.            return ((MRESULT) (fm.lMaxBaselineExt + 2L));
  303.  
  304.        case WM_DRAWITEM:
  305.  
  306.            poi = mp2;                  /* pointer to owner item    */
  307.  
  308.            if (poi->fsState == TRUE)   /* should cell be highlited?*/
  309.            {
  310.                clrFG = SYSCLR_HILITEFOREGROUND;
  311.                clrBG = SYSCLR_HILITEBACKGROUND;
  312.            }
  313.            else
  314.            {
  315.                clrFG = CLR_NEUTRAL;
  316.                clrBG = CLR_BACKGROUND;
  317.            }
  318.  
  319.            WinSendMsg (poi->hwnd,      /* get item text to write   */
  320.                        LM_QUERYITEMTEXT,
  321.                        (MPARAM) MAKEULONG (poi->idItem, 80),
  322.                        (MPARAM) szItemText);
  323.  
  324.                                        /*  get coordinates of rect
  325.                                            to write.
  326.                                         */
  327.            rcl.xLeft   = poi->rclItem.xLeft;
  328.            rcl.xRight  = poi->rclItem.xRight;
  329.            rcl.yTop    = poi->rclItem.yTop;
  330.            rcl.yBottom = poi->rclItem.yBottom;
  331.  
  332.            WinDrawText (poi->hps,
  333.                            -1,         /* null terminated string   */
  334.                            szItemText, /* text to write            */
  335.                            &rcl,       /* area to write text       */
  336.                            clrFG,      /* foreground color         */
  337.                            clrBG,      /* background color         */
  338.                            DT_LEFT | DT_VCENTER | DT_ERASERECT);
  339.                                        /* text drawing flags       */
  340.            
  341.                                        /*  search for keyword in
  342.                                            text to highlite
  343.                                         */
  344.            sKeyWordPos = SubStr (szItemText, szKeyword);
  345.  
  346.            if (sKeyWordPos >= 0)       /* is keyword found ?       */
  347.            {
  348.                /*  adjust the rectangle for drawing the keyword
  349.                 */
  350.                rcl.xLeft += sKeyWordPos * lAveChWidth;
  351.                rcl.xRight = rcl.xLeft + (strlen (szKeyword) * lAveChWidth);
  352.  
  353.                /*  change foreground color to help highlited color to
  354.                    highlite word found from SubStr ().
  355.                 */
  356.                clrFG = SYSCLR_HELPHILITE;
  357.  
  358.                WinDrawText (poi->hps,
  359.                                strlen (szKeyword), /*  length of text  */
  360.                                szKeyword,          /* text to write    */
  361.                                &rcl,               /* area to write    */
  362.                                clrFG,              /* foreground color */
  363.                                clrBG,              /* background color */
  364.                                DT_LEFT | DT_VCENTER | DT_ERASERECT);
  365.            }                                       /* text drawing flags */
  366.  
  367.            /*  these values *must* be set to FALSE to tell control
  368.                we don't need this rectangle inverted since we have
  369.                already taken care of it ourself.
  370.             */
  371.            poi->fsState = poi->fsStateOld = FALSE;
  372.  
  373.            return ((MRESULT) TRUE);
  374.  
  375.        case WM_CONTROL:
  376.            switch (SHORT1FROMMP (mp1))
  377.            {
  378.                case IDD_LIST:
  379.                    switch (SHORT2FROMMP (mp1))
  380.                    {
  381.                        case LN_ENTER:
  382.                            /*  if we receive a LN_ENTER message
  383.                                query the selection and display
  384.                                selection in WinMessageBox
  385.                             */
  386.                            mrItem = WinSendDlgItemMsg (hwnd,
  387.                                            SHORT1FROMMP (mp1),
  388.                                            LM_QUERYSELECTION,
  389.                                            0L,
  390.                                            0L);
  391.                            /* format the string with selection
  392.                             */
  393.                            sprintf (szTemp, "%s%d",
  394.                                        "Index of selected item: ",
  395.                                        SHORT1FROMMR (mrItem));
  396.  
  397.                            WinMessageBox (HWND_DESKTOP, hwnd,
  398.                                        szTemp, "Item Selected", 0,
  399.                                        MB_NOICON | MB_OK);
  400.  
  401.                        return 0;
  402.                    }
  403.                    break;
  404.            }
  405.            break;
  406.    }
  407.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  408. }
  409.  
  410. /* this routine searches a string for a substring. it returns
  411.  * the position of the substring in the string to allow the
  412.  * reseting of the rectangle area for drawing. if you didn't
  413.  * use a non-proportional font you could use DT_QUERYEXTENT
  414.  * in a WinDrawText function above and adjust accordingly. I
  415.  * prefer this method as proportional spaced fonts for long
  416.  * strings do not look good to my eye and i wanted to have
  417.  * the time on the items.
  418.  */
  419.  
  420. SHORT SubStr (CHAR *pchString, CHAR *pchSubString)
  421. {                                                                    
  422.    SHORT   sCheckLen = 0, sLocation = 0,
  423.            sSubStringLen = strlen (pchSubString);
  424.  
  425.    while (*pchString)
  426.    {
  427.       sCheckLen = 0;
  428.       while (*(pchSubString + sCheckLen) == *(pchString + sCheckLen)
  429.           && *(pchSubString + sCheckLen))
  430.                sCheckLen++;
  431.       if (sCheckLen == sSubStringLen)
  432.                return (sLocation);
  433.       sLocation++;
  434.       pchString++;
  435.    }
  436.    return (-1);
  437. }
  438.  
  439.  
  440.