/*
     File name:    TESTFAD.C
     Description:  This source file demonstrates the use of
     the Fader custom control in application programs.  The
     base source code for this module was first generated
     using the CASE:W code generator in verbose mode.
*/

#define EXTERN
#include "TESTFAD.h" /* Generated by CASE:W */
#include "fadbox.h" /* Generated by SDK Dialog Box Editor */
#include "fader.h"  /* Needed for fader custom control */

BOOL NEAR PASCAL RegisterControlClass (HANDLE hInstance);

/* Windows 3.0 Main Program Body  */

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
                        LPSTR lpszCmdLine, int nCmdShow)
{
 MSG      msg;   /* MSG structure to store your messages */
 int      nRc;   /* return value from Register Classes   */
 HANDLE   hLibFader;

 strcpy(szarAppName, "TESTFAD");
 hInst = hInstance;
 if(!hPrevInstance)
   {
    if ((nRc = nCwRegisterClasses()) == -1)
      {
       /* registering one of the windows failed  */
       LoadString(hInst, IDS_ERR_REGISTER_CLASS,
                szarString, sizeof(szarString));
       MessageBox(NULL, szarString,
                NULL, MB_ICONEXCLAMATION);
       return nRc;
      }
   }

 /* create application's Main window    */
 hWndMain = CreateWindow(
    szarAppName, NULL, WS_CAPTION | WS_SYSMENU
    | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME
    | WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPED,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
    NULL, NULL, hInst, NULL);

 if(hWndMain == NULL)
   {
    LoadString(hInst, IDS_ERR_CREATE_WINDOW, szarString, sizeof(szarString));
    MessageBox(NULL, szarString, NULL, MB_ICONEXCLAMATION);
    return IDS_ERR_CREATE_WINDOW;
   }
 ShowWindow(hWndMain, nCmdShow);            /* display main window      */
 hLibFader = LoadLibrary("FADER.DLL");
 if (hLibFader < 32)
   return(0);

 while(GetMessage(&msg, NULL, 0, 0))        /* Until WM_QUIT message    */
   {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }

 /* Do cleanup before exiting from the application  */
 UnregisterClass(szarAppName, hInst);
 FreeLibrary(hLibFader);
 return msg.wParam;
} /*  End of WinMain  */


/* Main Window Procedure  */

LONG FAR PASCAL WndProc(HWND hWnd, WORD Message, WORD wParam, LONG lParam)
{
 switch (Message)
   {
    case WM_COMMAND:
         switch (wParam)
           {
            case IDM_D_FADERS:
                 {
                  FARPROC lpfnFADBOXMsgProc;

                  lpfnFADBOXMsgProc = MakeProcInstance(
                            (FARPROC)FADBOXMsgProc, hInst);
                  DialogBox(hInst, (LPSTR)"FADBOX",
                            hWnd, lpfnFADBOXMsgProc);
                  FreeProcInstance(lpfnFADBOXMsgProc);
                 }
                 break;
            case IDM_D_EXIT:
                 DestroyWindow(hWnd);
                 if (hWnd == hWndMain)
                   PostQuitMessage(0);
                 hWndMain = 0;
                 break;
            default:
                return DefWindowProc(hWnd, Message,
                                    wParam, lParam);
           }
         break;        /* End of WM_COMMAND */
    case WM_CLOSE:  /* close the window  */
         DestroyWindow(hWnd);
         if (hWnd == hWndMain)
           PostQuitMessage(0);
         hWndMain = 0;
         break;     /* End of WM_CLOSE       */
    default:
         return DefWindowProc(hWnd, Message, wParam, lParam);
   }
 return 0L;
}     /* End of WndProc   */

/* Dialog Window Procedure  */
BOOL FAR PASCAL FADBOXMsgProc(HWND hWndDlg, WORD Message,
                                WORD wParam, LONG lParam)
{
 char text_val[40];
 int x;
 BOOL lTranslated;

 switch(Message)
   {
    case WM_INITDIALOG:
         SetDlgItemText(hWndDlg, FADER_EDIT,
                (LPSTR)"TESTING");
         break; /* End of WM_INITDLG  */
    case WM_CLOSE:
         EndDialog(hWndDlg, TRUE);
         break; /* End of WM_CLOSE */
    case WM_COMMAND:
         switch(wParam)
           {
            case FADER_OK:
         /* Ignore data values entered into the controls  */
         /* and dismiss the dialog window returning FALSE */
                 EndDialog(hWndDlg, FALSE);
                 break;
            case FADER_1:
                 switch (HIWORD(lParam)) {
                    case FDRN_THUMBTRACK:
                    case FDRN_ENDFADER:
   /* User has changed the current value of the Fader */
   /* Request the current value from the Fader */
                       x = (int) SendMessage(LOWORD(lParam),
                                    FDRM_GETLOGVALUE, 0, 0);
                       wsprintf(text_val,"%d",x);
                       SetDlgItemText(hWndDlg, FADER_EDIT,
                                        (LPSTR)text_val);
                       break;
                     }
                 break;
            case FADER_EDIT:
 /* If the value in the edit box changes, we will set the
    fader to use this as the new logical position */
                 if (HIWORD(lParam) == EN_CHANGE) {
                     x = GetDlgItemInt(hWndDlg, FADER_EDIT,
                          (BOOL FAR *)&lTranslated, FALSE);
                     SendDlgItemMessage(hWndDlg, FADER_1,
                                    FDRM_SETLOGVALUE, x, 0);
                     }
                 break;
           }
         break;    /* End of WM_COMMAND   */

    default:
        return FALSE;
   }
 return TRUE;
} /* End of FADBOXMsgProc  */


/* nCwRegisterClasses Function   */
int nCwRegisterClasses(void)
{
 WNDCLASS  wndclass; /* struct to define a window class */

 wndclass.style = CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc = WndProc;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = 0;
 wndclass.hInstance = hInst;
 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wndclass.lpszMenuName = szarAppName;
 wndclass.lpszClassName = szarAppName;

 if(!RegisterClass(&wndclass))
   return -1;

 return 0;
}  /* End of nCwRegisterClasses */
