/*************************************************************************** 
 *                                                                         *
 *    File:         mle.cpp                                                *
 *                                                                         *
 *    Description:  This file contains the main entry point into the       *
 *                  Test Program system.                                   *
 *                                                                         *
 *    Copyright:    (C) Copyright "Des" inc 1997.                          *
 *                  All Rights Reserved.                                   *
 *                                                                         *
 *    Date:         27 Apr 1997                                            *
 *                                                                         *
 *    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 "mle.hpp"


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

static void GuiSetup (void);


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

WINDOW  wDialog1;                                /* Dialog1 info           */


WINDOW  wMLE;


/*************************************************************************** 
 *                                                                         *
 *    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 ("mle", "Test Program");
    GuiSaveCmdLine (argc, argv);
    
    pApp = GuiAppInfo ();
    GuiInitWindows (pApp);
    
    /* Initialize global variables */
    memset (&wMLE, 0, sizeof (WINDOW));
    
    GuiSetup ();
    
   /********************************
    * Load and display main window *
    ********************************/
    
    GuiSetWindow (&wDialog1, IDD_DIALOG1, WT_DIALOG, Dialog1WndProc, (HMODULE)0);
    if  (GuiLoadWindow (&wDialog1, GuiDesktopWindow ()))
    {
       /*************************
        * Add entry to tasklist *
        *************************/
        
        strcpy (swctl.szSwtitle, pApp->szPgmTitle);
        
        swctl.hwnd          = wDialog1.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 = wDialog1.hwnd;
        GuiProcessWindow (&wDialog1);
        GuiUnloadWindow (&wDialog1);
    }
    
   /**************************
    * 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);
    GuiRegisterMLE (pApp->hab);
}


/*************************************************************************** 
 *                                                                         *
 *    Function:     Dialog1WndProc                                         *
 *                                                                         *
 *    Description:  This is the window procedure handler for the           *
 *                  Dialog1 window.                                        *
 *                                                                         *
 ***************************************************************************/

PMENTRY Dialog1WndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
    MRESULT mResult = FALSE;
    
    if  (msg == WM_GUIGETWINDOW)
    {
        *((PWINDOW)mp2) = wDialog1;
        return (FALSE);
    }
    
    /* Check if window fully loaded */
    if  (!wDialog1.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_PUSHBUTTONINSET:
                      Dialog1PushButtonInsetClick ();
                      break;
                 case IDD_PUSHBUTTONAPPEND:
                      Dialog1PushButtonAppendClick ();
                      break;
                 
                 default:
                      break;
             }
             return (mResult);
        
       /*************************
        * Control notifications *
        *************************/
        
        case WM_CONTROL:
             if  (!GuiEventLoop (mp1))
                 break;
             switch (SHORT1FROMMP (mp1))
             {
                 case IDD_MLE1:
                      switch (SHORT2FROMMP (mp1))
                      {
                          case MLN_CHANGE:
                               Dialog1MLE1Change ();
                               break;
                      }
                      break;
             }
             GuiEventLoop (0);
             return (mResult);
        
        default:
             break;
    }
    
    return (GuiDefFrameProc (WinDefDlgProc, hwnd, msg, mp1, mp2));
}


void Dialog1MLE1Change (LONG ps)
{
    WINDOW  GuiTmp1;
    
    GuiSetBackColor (ps, RGB_BLUE);
    GuiClearPS (ps);
    GuiSetControl (&wDialog1, &GuiTmp1, IDD_MLE1, WT_MLE);
    GuiSetFont (&GuiTmp1, String("8.helv"));
    GuiSetDrawColor (ps, RGB_WHITE);
    GuiMoveTo (ps, (LONG)10, (LONG)10);
    GuiDrawText (ps, String("Hellow Word"));
}


void Dialog1PushButtonInsetClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   i = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            i = (SHORT)1;
        }
        else
        {
            i += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (i > (SHORT)12) : (i < (SHORT)12))
            break;
        
        GuiInsertItem (&wMLE, i, String("Insert:") + GuiStr (i));
    }
}


void Dialog1PushButtonAppendClick (void)
{
    SHORT   GuiTmp1 = 0;
    SHORT   GuiTmp2 = 0;
    SHORT   i = 0;
    
    GuiTmp1 = TRUE;
    while (FOREVER) /* FOR loop */
    {
        GuiTmp2 = 1;
        
        if (GuiTmp1 == TRUE)
        {
            GuiTmp1 = FALSE;
            i = (SHORT)1;
        }
        else
        {
            i += GuiTmp2;
        }
        
        if ((GuiTmp2 > 0) ? (i > (SHORT)12) : (i < (SHORT)12))
            break;
        
        GuiInsertItem (&wMLE, -1, String("Append:") + GuiStr (i));
    }
}


