/*************************************************************************** 
 *                                                                         *
 *    File:         Listdemo.cpp                                           *
 *                                                                         *
 *    Description:  This file contains the main entry point into the       *
 *                  Test Program system.                                   *
 *                                                                         *
 *    Copyright:    (C) Copyright JBA Software (SDU) 1995.                 *
 *                  All Rights Reserved.                                   *
 *                                                                         *
 *    Date:         26 Oct 1996                                            *
 *                                                                         *
 *    Note:         Generated by the Guidelines code generation facility.  *
 *                                                                         *
 ***************************************************************************/

#define INCL_PM
#define INCL_NLS
#define INCL_BASE

#define GUIDELINES "3.1A"

#include <os2.h>                                 /* OS/2 include file      */
#include <stdio.h>                               /* Standard I/O functions */
#include <stdlib.h>                              /* Misc. functions        */
#include <string.h>                              /* String functions       */

#include "Listdemo.hpp"


/*************************************************************************** 
 *    Private functions.                                                   *
 ***************************************************************************/

static void GuiSetup (void);


/*************************************************************************** 
 *    Global variables.                                                    *
 ***************************************************************************/

WINDOW  wDLG_ListBox;                            /* DLG_ListBox info       */


SHORT   i;
SHORT   j;
SHORT   k = 0;
WINDOW  Listbox[4];
WINDOW  Count[4];
WINDOW  Selection[4];
WINDOW  TopIndex[4];
SHORT   currentlistbox = 0;
SHORT   currentline = 0;


/*************************************************************************** 
 *                                                                         *
 *    Function:     main                                                   *
 *                                                                         *
 *    Description:  This is main entry point into the                      *
 *                  Test Program application.                              *
 *                                                                         *
 ***************************************************************************/

int main (int argc, char *argv[])
{
    SWCNTRL  swctl;
    PAPPINFO pApp;
    
   /******************************
    * Initialize the application *
    ******************************/
    
    GuiInitApp ("Listdemo", "Test Program");
    GuiSaveCmdLine (argc, argv);
    
    pApp = GuiAppInfo ();
    GuiInitWindows (pApp);
    
    /* Initialize global variables */
    i = (SHORT)0;
    j = (SHORT)0;
    memset (Listbox, 0, sizeof (Listbox));
    memset (Count, 0, sizeof (Count));
    memset (Selection, 0, sizeof (Selection));
    memset (TopIndex, 0, sizeof (TopIndex));
    
    GuiSetup ();
    
   /********************************
    * Load and display main window *
    ********************************/
    
    GuiSetWindow (&wDLG_ListBox, IDD_DLG_LISTBOX, WT_DIALOG, DLG_ListBoxWndProc, (HMODULE)0);
    if  (GuiLoadWindow (&wDLG_ListBox, GuiDesktopWindow ()))
    {
       /*************************
        * Add entry to tasklist *
        *************************/
        
        strcpy (swctl.szSwtitle, pApp->szPgmTitle);
        
        swctl.hwnd          = wDLG_ListBox.hwnd;
        swctl.hwndIcon      = (HWND)0;
        swctl.hprog         = (HPROGRAM)0;
        swctl.idProcess     = pApp->pid;
        swctl.idSession     = 0;
        swctl.uchVisibility = SWL_VISIBLE;
        swctl.fbJump        = SWL_JUMPABLE;
        
        WinAddSwitchEntry (&swctl);
        
       /*******************************
        * Process the window template *
        *******************************/
        
        pApp->hwndMain = wDLG_ListBox.hwnd;
        GuiProcessWindow (&wDLG_ListBox);
        GuiUnloadWindow (&wDLG_ListBox);
    }
    
   /**************************
    * Termination processing *
    **************************/
    
    GuiExitWindows ();
    
    return (pApp->sExitCode);
}


/*************************************************************************** 
 *                                                                         *
 *    Function:     GuiSetup                                               *
 *                                                                         *
 *    Description:  This routine performs any processing required          *
 *                  to initialize the runtime environment for this         *
 *                  application or DLL.                                    *
 *                                                                         *
 ***************************************************************************/

static void GuiSetup (void)
{
    PAPPINFO pApp = GuiAppInfo ();
    
    GuiRegisterButton (pApp->hab);
    GuiRegisterEntry (pApp->hab);
    GuiRegisterListbox (pApp->hab);
    GuiRegisterStatic (pApp->hab);
}


/*************************************************************************** 
 *                                                                         *
 *    Function:     DLG_ListBoxWndProc                                     *
 *                                                                         *
 *    Description:  This is the window procedure handler for the           *
 *                  DLG_ListBox window.                                    *
 *                                                                         *
 ***************************************************************************/

PMENTRY DLG_ListBoxWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
    MRESULT mResult = FALSE;
    
    if  (msg == WM_GUIGETWINDOW)
    {
        *((PWINDOW)mp2) = wDLG_ListBox;
        return (FALSE);
    }
    
    /* Check if window fully loaded */
    if  (!wDLG_ListBox.hwnd)
        return (GuiDefFrameProc (WinDefDlgProc, hwnd, msg, mp1, mp2));
    
    switch (msg)
    {
       /***********************
        * Property processing *
        ***********************/
        
        case WM_GUIGETPROPERTY:
        case WM_GUISETPROPERTY:
             break;
        
       /*******************************
        * Window resized/repositioned *
        *******************************/
        
        case WM_WINDOWPOSCHANGED:
             {
                 PSWP pSwp = (PSWP) mp1;
                 if  (pSwp->fl & SWP_SIZE)
                 {
                     GuiBroadcastMsg (hwnd, WM_GUIPARENTRESIZED, 0, 0);
                 }
             }
             break;
        
       /***************************
        * User command processing *
        ***************************/
        
        case WM_COMMAND:
             switch (SHORT1FROMMP (mp1))
             {
                 case IDD_PSH_APPEND:
                      DLG_ListBoxPSH_AppendClick ();
                      break;
                 case IDD_PSH_INSERT:
                      DLG_ListBoxPSH_InsertClick ();
                      break;
                 case IDD_PSH_CLEAR:
                      DLG_ListBoxPSH_ClearClick ();
                      break;
                 case IDD_PSH_DELETE:
                      DLG_ListBoxPSH_DeleteClick ();
                      break;
                 case IDD_PSH_ALL:
                      DLG_ListBoxPSH_AllClick ();
                      break;
                 case IDD_PSH_NONE:
                      DLG_ListBoxPSH_NoneClick ();
                      break;
                 case IDD_PSH_SETTEXT:
                      DLG_ListBoxPSH_SetTextClick ();
                      break;
                 
                 default:
                      break;
             }
             return (mResult);
        
       /**************************
        * Window is being loaded *
        **************************/
        
        case WM_GUIWINDOWLOADED:
             DLG_ListBoxCreate ();
             return (FALSE);
        
       /*************************
        * Control notifications *
        *************************/
        
        case WM_CONTROL:
             if  (!GuiEventLoop (mp1))
                 break;
             switch (SHORT1FROMMP (mp1))
             {
                 case IDD_LST_DEFAULT:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case LN_SELECT:
                               DLG_ListBoxLST_DefaultSelect ();
                               break;
                          case LN_ENTER:
                               DLG_ListBoxLST_DefaultEnter ();
                               break;
                          case LN_SCROLL:
                               DLG_ListBoxLST_DefaultVScroll ();
                               break;
                      }
                      break;
                 case IDD_LST_MULTI:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case LN_SELECT:
                               DLG_ListBoxLST_MultiSelect ();
                               break;
                          case LN_SCROLL:
                               DLG_ListBoxLST_MultiVScroll ();
                               break;
                      }
                      break;
                 case IDD_LST_EXTENDED:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case LN_SELECT:
                               DLG_ListBoxLST_ExtendedSelect ();
                               break;
                          case LN_SCROLL:
                               DLG_ListBoxLST_ExtendedVScroll ();
                               break;
                      }
                      break;
                 case IDD_LST_HSCROLL:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case LN_SELECT:
                               DLG_ListBoxLST_HScrollSelect ();
                               break;
                          case LN_ENTER:
                               DLG_ListBoxLST_HScrollEnter ();
                               break;
                          case LN_SCROLL:
                               DLG_ListBoxLST_HScrollVScroll ();
                               break;
                      }
                      break;
                 case IDD_EDT_ITEM:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case EN_CHANGE:
                               DLG_ListBoxEDT_ItemChange ();
                               break;
                      }
                      break;
             }
             GuiEventLoop (0);
             return (mResult);
        
        default:
             break;
    }
    
    return (GuiDefFrameProc (WinDefDlgProc, hwnd, msg, mp1, mp2));
}


void DLG_ListBoxPSH_AppendClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   GuiTmp3 = 0;
    SHORT   GuiTmp4 = 0;
    SHORT   number = (SHORT) ((int)i + 10);
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            i = i;
        }
        else
        {
            i += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (i > number) : (i < number))
            break;
        
        GuiTmp3 = TRUE;
        while (FOREVER) /* FOR loop */
        {
            GuiTmp4 = 1;
            
            if (GuiTmp3 == TRUE)
            {
                GuiTmp3 = FALSE;
                k = (SHORT)0;
            }
            else
            {
                k += GuiTmp4;
            }
            
            if ((GuiTmp4 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
                break;
            
            GuiInsertItem (&Listbox[k], -1, String("Append: ") + GuiStr (i));
        }
    }
    UpdateStats ();
}


void DLG_ListBoxPSH_InsertClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            k = (SHORT)0;
        }
        else
        {
            k += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
            break;
        
        GuiInsertItem (&Listbox[k], GuiGetShort (&Listbox[k], WPID_LISTBOXSEL), String("Insert: ") + 
                                                                                GuiStr (j));
    }
    j = (SHORT) ((int)j + 1);
    UpdateStats ();
}


void DLG_ListBoxPSH_DeleteClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   index = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            k = (SHORT)0;
        }
        else
        {
            k += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
            break;
        
        index = GuiGetShort (&Listbox[k], WPID_LISTBOXSEL);
        while (FOREVER) /* WHILE loop */
        {
            if (!((int)index != -1))
                break;
            
            GuiDeleteItem (&Listbox[k], index);
            index = GuiGetShort (&Listbox[k], WPID_LISTBOXSEL);
        }
    }
    UpdateStats ();
}


void DLG_ListBoxCreate (void)
{
    WINDOW  GuiTmp1;
    WINDOW  GuiTmp2;
    WINDOW  GuiTmp3;
    
    GuiSetControl (&wDLG_ListBox, &Listbox[0], IDD_LST_DEFAULT, WT_LISTBOX);
    GuiSetControl (&wDLG_ListBox, &Listbox[1], IDD_LST_HSCROLL, WT_LISTBOX);
    GuiSetControl (&wDLG_ListBox, &Listbox[2], IDD_LST_MULTI, WT_LISTBOX);
    GuiSetControl (&wDLG_ListBox, &Listbox[3], IDD_LST_EXTENDED, WT_LISTBOX);
    GuiSetControl (&wDLG_ListBox, &Count[0], IDD_LBL_DEFCOUNT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Count[1], IDD_LBL_HSCOUNT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Count[2], IDD_LBL_MULTCOUNT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Count[3], IDD_LBL_EXTCOUNT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Selection[0], IDD_LBL_DEFSELECT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Selection[1], IDD_LBL_HSSELECT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Selection[2], IDD_LBL_MULTSELECT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &Selection[3], IDD_LBL_EXTSELECT, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &TopIndex[0], IDD_LBL_DEFTOP, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &TopIndex[1], IDD_LBL_HSTOP, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &TopIndex[2], IDD_LBL_MULTTOP, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &TopIndex[3], IDD_LBL_EXTTOP, WT_STATICTEXT);
    GuiSetControl (&wDLG_ListBox, &GuiTmp1, IDD_PSH_SETTEXT, WT_PUSHBUTTON);
    GuiSetShort (&GuiTmp1, WPID_ENABLED, FALSE);
    GuiSetControl (&wDLG_ListBox, &GuiTmp2, IDD_LBL_CURRLIST, WT_STATICTEXT);
    GuiSetString (&GuiTmp2, WPID_TEXT, GuiStr (currentlistbox));
    GuiSetControl (&wDLG_ListBox, &GuiTmp3, IDD_LBL_CURRLINE, WT_STATICTEXT);
    GuiSetString (&GuiTmp3, WPID_TEXT, GuiStr (currentline));
}


void DLG_ListBoxPSH_ClearClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            k = (SHORT)0;
        }
        else
        {
            k += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
            break;
        
        GuiClearItems (&Listbox[k]);
    }
    UpdateStats ();
}


void DLG_ListBoxPSH_AllClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   GuiTmp3 = 0;
    SHORT   GuiTmp4 = 0;
    SHORT   linenumber = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            k = (SHORT)0;
        }
        else
        {
            k += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
            break;
        
        GuiTmp3 = TRUE;
        while (FOREVER) /* FOR loop */
        {
            GuiTmp4 = 1;
            
            if (GuiTmp3 == TRUE)
            {
                GuiTmp3 = FALSE;
                linenumber = (SHORT)0;
            }
            else
            {
                linenumber += GuiTmp4;
            }
            
            if ((GuiTmp4 > 0) ? (linenumber > (SHORT)((int)GuiGetShort (&Listbox[k], WPID_LISTBOXCOUNT) - 
                                                      1)) : (linenumber < (SHORT)((int)GuiGetShort (&Listbox[k], WPID_LISTBOXCOUNT) - 
                                                                                  1)))
                break;
            
            GuiSelectItem (&Listbox[k], linenumber, TRUE);
        }
    }
    UpdateStats ();
}


void DLG_ListBoxPSH_NoneClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   GuiTmp3 = 0;
    SHORT   GuiTmp4 = 0;
    SHORT   linenumber = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            k = (SHORT)0;
        }
        else
        {
            k += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (k > (SHORT)3) : (k < (SHORT)3))
            break;
        
        GuiTmp3 = TRUE;
        while (FOREVER) /* FOR loop */
        {
            GuiTmp4 = 1;
            
            if (GuiTmp3 == TRUE)
            {
                GuiTmp3 = FALSE;
                linenumber = (SHORT)0;
            }
            else
            {
                linenumber += GuiTmp4;
            }
            
            if ((GuiTmp4 > 0) ? (linenumber > (SHORT)((int)GuiGetShort (&Listbox[k], WPID_LISTBOXCOUNT) - 
                                                      1)) : (linenumber < (SHORT)((int)GuiGetShort (&Listbox[k], WPID_LISTBOXCOUNT) - 
                                                                                  1)))
                break;
            
            GuiSelectItem (&Listbox[k], linenumber, FALSE);
        }
    }
    UpdateStats ();
}


void DLG_ListBoxLST_DefaultVScroll (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_DefaultSelect (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_HScrollSelect (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_HScrollVScroll (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_MultiSelect (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_MultiVScroll (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_ExtendedSelect (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_ExtendedVScroll (void)
{
    UpdateStats ();
}


void DLG_ListBoxLST_DefaultEnter (void)
{
    GetTheText ((SHORT)0);
}


void DLG_ListBoxLST_HScrollEnter (void)
{
    GetTheText ((SHORT)1);
}


void DLG_ListBoxPSH_SetTextClick (void)
{
    WINDOW  GuiTmp1;
    WINDOW  GuiTmp2;
    
    GuiSetControl (&wDLG_ListBox, &GuiTmp1, IDD_EDT_ITEM, WT_ENTRYFIELD);
    GuiSetItemText (&Listbox[currentlistbox], currentline, 
    GuiStrTrim (GuiGetString (&GuiTmp1, WPID_TEXT)));
    GuiSetControl (&wDLG_ListBox, &GuiTmp1, IDD_EDT_ITEM, WT_ENTRYFIELD);
    GuiSetString (&GuiTmp1, WPID_TEXT, String(""));
    GuiSetControl (&wDLG_ListBox, &GuiTmp2, IDD_PSH_SETTEXT, WT_PUSHBUTTON);
    GuiSetShort (&GuiTmp2, WPID_ENABLED, FALSE);
}


void DLG_ListBoxEDT_ItemChange (void)
{
    WINDOW  GuiTmp1;
    WINDOW  GuiTmp2;
    
    GuiSetControl (&wDLG_ListBox, &GuiTmp1, IDD_EDT_ITEM, WT_ENTRYFIELD);
    if (GuiStrTrim (GuiGetString (&GuiTmp1, WPID_TEXT)) == String(""))
    {
        GuiSetControl (&wDLG_ListBox, &GuiTmp2, IDD_PSH_SETTEXT, WT_PUSHBUTTON);
        GuiSetShort (&GuiTmp2, WPID_ENABLED, FALSE);
    }
    else
    {
        GuiSetControl (&wDLG_ListBox, &GuiTmp2, IDD_PSH_SETTEXT, WT_PUSHBUTTON);
        GuiSetShort (&GuiTmp2, WPID_ENABLED, TRUE);
    }
}


void UpdateStats (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   labelnumber = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            labelnumber = (SHORT)0;
        }
        else
        {
            labelnumber += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (labelnumber > (SHORT)3) : (labelnumber < (SHORT)3))
            break;
        
        GuiSetString (&Count[labelnumber], WPID_TEXT, GuiStr (GuiGetShort (&Listbox[labelnumber], WPID_LISTBOXCOUNT)));
        GuiSetString (&Selection[labelnumber], WPID_TEXT, GuiStr (GuiGetShort (&Listbox[labelnumber], WPID_LISTBOXSEL)));
        GuiSetString (&TopIndex[labelnumber], WPID_TEXT, GuiStr (GuiGetShort (&Listbox[labelnumber], WPID_LISTBOXTOP)));
    }
}


void GetTheText (SHORT number)
{
    WINDOW  GuiTmp1;
    WINDOW  GuiTmp2;
    WINDOW  GuiTmp3;
    
    currentlistbox = number;
    currentline = GuiGetShort (&Listbox[currentlistbox], WPID_LISTBOXSEL);
    GuiSetControl (&wDLG_ListBox, &GuiTmp1, IDD_LBL_CURRLIST, WT_STATICTEXT);
    GuiSetString (&GuiTmp1, WPID_TEXT, GuiStr (currentlistbox));
    GuiSetControl (&wDLG_ListBox, &GuiTmp2, IDD_LBL_CURRLINE, WT_STATICTEXT);
    GuiSetString (&GuiTmp2, WPID_TEXT, GuiStr (currentline));
    GuiSetControl (&wDLG_ListBox, &GuiTmp3, IDD_EDT_ITEM, WT_ENTRYFIELD);
    GuiSetString (&GuiTmp3, WPID_TEXT, GuiGetItemText (&Listbox[currentlistbox], currentline));
}


void Listdemo (PWINDOW WinOwner)
{
    GuiSetWindow (&wDLG_ListBox, IDD_DLG_LISTBOX, WT_DIALOG, DLG_ListBoxWndProc, (HMODULE)0);
    GuiLoadWindow (&wDLG_ListBox, WinOwner);
    GuiProcessWindow (&wDLG_ListBox);
    GuiUnloadWindow (&wDLG_ListBox);
}


