//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odconfig
//
//   CLASSES: none
//
//   ORIGINS: 82,27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//   	
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
/* @(#)Z 1.13 nt/src/registry/odreggui/prefedit.cpp, odconfig, od96nt, odnt9712d 97/03/21 17:17:41 (97/03/06 14:27:17) */
// ------------------------------------------------------------------------
//
//  File:      prefedit.cpp
//  Contains:  Registry GUI source
//
// ************************************************************************
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <odnltyps.h>
#include "prefedit.h"
#include "odregutl.h"
#include <iostream.h>
#include <iostream.h>
#include <ISOStr.h>
#include <ODRegApi.h>
#include <ODTypes.h>


BOOL    CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);


char **parse_partslist(char *list, unsigned long size, int &index);
unsigned long set_editor_pref(char *editor);
int SetPartEditor(HWND hdwnd, int i, char **kindlist, char ***partlist);

char szWinName[] = "MyPref";

HINSTANCE hInst;

nl_catd regCatHandle;

enum {BUFFER_SIZE =  1024};

int WINAPI WinMain (HINSTANCE hThisInst, HINSTANCE hPreInst, LPSTR lpszArgs,
                    int nWinMode)

{

  HWND hwnd;
  MSG msg;
  WNDCLASSEX wcl;
  HANDLE hAccel;
  DWORD  err;

    setlocale(LC_MESSAGES, NULL);
    regCatHandle = catopen("ODRegUtl.cat", 0);
    if (regCatHandle == CATD_ERR)
       printf("Cannot open message catalog ODRegUtl.cat.\n");

// define window class, wcl
    wcl.hInstance     = hThisInst;
    wcl.lpszClassName = szWinName;
    wcl.lpfnWndProc   = WindowFunc;
    wcl.style         = 0;
    wcl.cbSize        = sizeof(WNDCLASSEX);
    wcl.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wcl.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wcl.lpszMenuName  = NULL;
    wcl.cbClsExtra    = 0;
    wcl.cbWndExtra    = 0;
    wcl.hbrBackground = NULL;
    wcl.hIconSm       = NULL;

// Register Class
    if (!RegisterClassEx(&wcl))
       {
       err = GetLastError();
       return(0);
       }

// create window after registration
 hwnd = CreateWindow (
       szWinName,
       NULL,
       WS_OVERLAPPEDWINDOW,            // Window Style
       CW_USEDEFAULT,                  // X coord
       CW_USEDEFAULT,                  // Y coord
       CW_USEDEFAULT,                  // Width
       CW_USEDEFAULT,                  // Height
       HWND_DESKTOP,                   // No Parent Window
       NULL,                           // No Window
       hThisInst,                      // Handle  for Instance
       NULL );                         // Extra Parms


  // Load in the resource
  hInst = LoadLibrary("ODPREFED.DLL");
  if (hInst == NULL)
     printf(catgets(regCatHandle, REGGUI_SET, REGGUI_70, RC_NOT_OPEN));
  else
  {
     //   Update the main window with Win32 messages
     UpdateWindow(hwnd);

     // Start dialogue box
     PostMessage(hwnd, WM_COMMAND, IDM_DIALOG, 0);


     // Create Message Loop
     while (GetMessage(&msg, NULL, 0, 0))
     {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
     }
  }

  return (msg.wParam);
}


// Windows calls function with message from message queue
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
    case WM_COMMAND:
       switch (LOWORD(wParam))
       {
          case IDM_DIALOG:
             DialogBox(hInst, "PrefEdit", hwnd,(FARPROC) DialogFunc);
             PostQuitMessage(0);
             break;
       }
    case WM_DESTROY:
       PostQuitMessage(0);
       break;
    default:
       return DefWindowProc(hwnd, message, wParam, lParam);
   }
   return 0;
}


//--------------------------------------------------------------------
// Dialog Box Function for selecting preferred editor
//--------------------------------------------------------------------
BOOL CALLBACK DialogFunc(HWND hdwnd, UINT message, WPARAM wParam, LPARAM lParam)
{


int i, i2 = 0;
int  &index = i2;
char str[254];
static char **kindlist = NULL;
static char **partlist = NULL;
char buf_list[BUFFER_SIZE];
unsigned long rc;
unsigned long size;
char displayname[BUFFER_SIZE];

  switch (message)
  {
    //------------------------------------------------------
    //  Initialize the Part Kind listbox, display Kinds list
    //------------------------------------------------------
    case WM_INITDIALOG:

       // Get a list of registered Part Kinds
        size = BUFFER_SIZE;
        rc = ODQueryPartKindList(0,  buf_list, (ODULong) &size);
        if (rc != RM_NO_ERROR)
          {
          sprintf(str,
                  catgets(regCatHandle, REGGUI_SET, REGGUI_40, KIND_FAIL),
                  rc);
          MessageBox(hdwnd, str,
                     catgets(regCatHandle, REGGUI_SET, REGGUI_10, WARN_TITLE),
                     MB_OK);
          EndDialog(hdwnd, 0);
          return 0;
          }

       kindlist = parse_partslist(buf_list, size, index);
       SendDlgItemMessage(hdwnd, IDD_LBOX1, LB_SETHORIZONTALEXTENT, (WPARAM) 800, 0);
       for (i=0; i<=index; i++)
          {
          size = BUFFER_SIZE;
          rc = ODQueryKindDisplayName(kindlist[i],  displayname, (ODULong) &size);

          SendDlgItemMessage(hdwnd, IDD_LBOX1, LB_ADDSTRING, 0, (LPARAM)displayname);
          }
       SendDlgItemMessage(hdwnd, IDD_LBOX1, LB_SETCURSEL, 0, 0);

       // Display the part editor list for the first part kind in the list
       SetPartEditor(hdwnd, 0, kindlist, &partlist);

       return 1;

    case WM_COMMAND:
       switch (LOWORD(wParam))
       {

       //------------------------------------------------
       //  Part Kind Listbox
       //------------------------------------------------
       case IDD_LBOX1:  // user single or double clicks Kinds for Part Handler List
         if ((HIWORD(wParam)==LBN_DBLCLK) || (HIWORD(wParam) == LBN_SELCHANGE))
           {

           i = SendDlgItemMessage(hdwnd, IDD_LBOX1, LB_GETCURSEL, 0, 0);
           rc = SetPartEditor(hdwnd, i, kindlist, &partlist);
           
           return rc;

           }
         return 1;
       //------------------------------------------------
       //  OK PushButton - Set Editor Preference & exit
       //------------------------------------------------
       case IDOK:
         i = SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_GETCURSEL, 0, 0);
         SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_GETTEXT, (WPARAM)i, (LPARAM)str);
         rc = set_editor_pref(partlist[i]);
        if (rc != RM_NO_ERROR)
          {
          sprintf(str,
                  catgets(regCatHandle, REGGUI_SET, REGGUI_50, REG_FAIL),
                  rc);
          MessageBox(hdwnd, str,
                     catgets(regCatHandle, REGGUI_SET, REGGUI_10, WARN_TITLE),
                     MB_OK);
          return 0;
          }
        free(kindlist);
        free(partlist);
        EndDialog(hdwnd, 0);
        return 1;
       //------------------------------------------------
       //  Cancel PushButton - Exit dialog box w/o change
       //------------------------------------------------
       case IDCANCEL:
         free(kindlist);
         free(partlist);
         EndDialog(hdwnd, 0);
         return 1;
       //------------------------------------------------
       //  Help PushButton - Temporary Message Box Help
       //------------------------------------------------
       case IDD_HELP:
         MessageBox(hdwnd,
                    catgets(regCatHandle, REGGUI_SET, REGGUI_60, HELP_MSG),
                    "", MB_OK);
         return 1;

       }

  }
  return 0;
}

//----------------------------------------------------------------------
//  parse_partslist
//----------------------------------------------------------------------

char **parse_partslist(char *list, unsigned long size, int &index)
{
int i=0;
char *common, *wd, *end;

     wd = list;
     end = list + size;
     char **namelist = (char **)malloc((size / 2) * sizeof(char *));

     // Build an array using the part Handler list returned from
     // ODQueryPartHandlerList with each array element containing
     // a part kind
     while ( wd < end)
     {
             if (common = strchr(wd, ','))
             {
                     namelist[i] = (char *) malloc(common - wd + 1);
                     *(namelist[i] + (common - wd)) = '\00';
                     memcpy(namelist[i++], wd, common-wd);
                     wd = common +1;
             }
             else
             {
                     namelist[i] = (char *) malloc(end - wd + 1);
                     *(namelist[i] + (end - wd)) = '\00';
                     memcpy(namelist[i], wd, end-wd);
                     wd = end;
             }
     }
     index = i;

return (namelist);
}

//----------------------------------------------------------------------
//  SetEditorPref
//----------------------------------------------------------------------
unsigned long set_editor_pref(char *editor)
{

int rc= 0;

     //--------------------------------------------------
     // uninstall selected editor
     //--------------------------------------------------

     rc = ODDeregisterPartHandler(editor);
     if (rc == RM_NO_ERROR)
     {

     //--------------------------------------------------
     // reinstall this editor (makes it the "preferred editor")
     //--------------------------------------------------
     rc = ODRegisterPartHandlerClass((ISOString)editor);
     }

        return(rc);
}

//----------------------------------------------------------------------
//  SetPartEditor - Get a list of registered part editors based on the
//                  incoming part kind.
//----------------------------------------------------------------------
int SetPartEditor(HWND hdwnd, int i, char **kindlist, char ***partlist)
{
   int j, i2 = 0;
   int  &index = i2;
   char str[254];
   char buf_list[BUFFER_SIZE];
   unsigned long rc;
   unsigned long size;
   char displayname[BUFFER_SIZE];

   // Retrieve the part kind from the list box
   SendDlgItemMessage(hdwnd, IDD_LBOX1, LB_GETTEXT, (WPARAM)i, (LPARAM)str);

   // Get a list of registered Part Handlers bases on kind 'str'
   size = BUFFER_SIZE;
   rc =  ODQueryPartHandlerList(kindlist[i],  buf_list, (ODULong) &size);
   if (rc != RM_NO_ERROR)
   {
      sprintf(str,
              catgets(regCatHandle, REGGUI_SET, REGGUI_30, PART_FAIL),
              rc);
      MessageBox(hdwnd, str,
                catgets(regCatHandle, REGGUI_SET, REGGUI_10, WARN_TITLE),
                MB_OK);
      rc = 0;
   }
   else
   {
      // populate the part editor listbox
      SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_RESETCONTENT, 0, 0);
      if (*partlist)
         free(*partlist);
      *partlist = parse_partslist(buf_list, size, index);

      SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_SETHORIZONTALEXTENT, (WPARAM) 800, 0);

      for (j=0; j<=index; j++)
      {
         size = BUFFER_SIZE;
         rc = ODQueryHandlerDisplayName((*partlist)[j], displayname, (ODULong) &size);
         SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_ADDSTRING, 0, (LPARAM)displayname);
      }
      SendDlgItemMessage(hdwnd, IDD_LBOX2, LB_SETCURSEL, 0, 0);
      rc = 1;
   }

   return rc;
}
