//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odother
//
//   CLASSES: none
//
//   ORIGINS: 27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995-1997
//   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========================================
//
// @(#) 1.1 nt/src/samples/JavaPart/Bean.cpp, odjavapart, od96nt, odnt9714c 3/19/97 04:07:27 [ 4/2/97 17:55:06 ]

#include <AltPoint.h>

#ifndef IBMSample_BEAN
#include "Bean.h"
#endif

#ifndef IBMSample_TAG
#include "Tag.h"
#endif

#ifndef IBMSample_MISC
#include "Misc.h"
#endif

#ifndef SOM_ODFacet_xh
#include <Facet.xh>
#endif

#ifndef SOM_ODFrame_xh
#include <Frame.xh>
#endif

#ifndef SOM_ODShape_xh
#include <Shape.xh>
#endif


#include <qprintf.h>


/*
 *  Global variable for indicating wether the Java runtime has been
 *  initialized for this process (comes from Applet.cpp)...
 */
extern BOOL gRuntimeInitialized;

FARPROC OriginalBeanWndProc = NULL;


LRESULT CALLBACK BeanWndProcSubclass(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   /* We let the JavaPart facet window know about clicks on the Bean window */
   /* This way, it can activate itself, and display JavaPart's menu */

   if (uMsg==WM_MOUSEACTIVATE) {
      HWND JavaPartFacetHWND = GetParent(hwnd); //jwa: this call fails sometimes
      if (JavaPartFacetHWND==NULL) { //jwa: workaround for failure of above call
         JavaPartFacetHWND = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
      } /* endif - workaround */

      PostMessage(JavaPartFacetHWND, WM_MOUSEACTIVATE, (WPARAM) 0, (LPARAM) 0);
//    return(MA_NOACTIVATE); //jwa: this causes neither window to be active???
// } else {
//    return(CallWindowProc(OriginalBeanWndProc, hwnd, uMsg, wParam, lParam));
   } /* endif */

   return(CallWindowProc(OriginalBeanWndProc, hwnd, uMsg, wParam, lParam));
}


/*-------------------------------------------------------------------+
|                        BeanShowConsole ()                          |
| ================================================================== |
|                                                                    |
| Function:    Receives all Bean status messages.                    |
|                                                                    |
|              We will display the Java console when an message is   |
|              received indicating that an error occured while       |
|              attempting to load the bean.                          |
|                                                                    |
+-------------------------------------------------------------------*/
JE_CLI_FN(int) BeanShowConsole (const char* string)
{
	/*
	 *  Pop up the Java console if an error occurs...
	 */
	if (strstr (string, "ERROR") || 
	    strstr (string, "FileNotFoundException") ||
	    strstr (string, "ClassNotFoundException"))
		Bean::ShowConsole ();

        return 0;
}


/*-------------------------------------------------------------------+
|                           Bean::Bean ()                            |
| ================================================================== |
|                                                                    |
| Function:    Bean default constructor                              |
|                                                                    |
| Algorithm:   o  Set default values...                              |
|                                                                    |
| Parameters:  parentWindow - Bean's parent window                   |
|                                                                    |
+-------------------------------------------------------------------*/
Bean::Bean (JavaPart* javaPart, BeanTag* tag) : 
	fJavaPart (javaPart), fBeanTag (tag)
{
}


/*-------------------------------------------------------------------+
|                        Bean::~Bean ()                          |
| ================================================================== |
|                                                                    |
| Function:    Bean destructor                                       |
|                                                                    |
| Algorithm:   o  Dispose of the bean (this destroys it and its HWND |
|              o  Delete it's tag info                               |
|                                                                    |
+-------------------------------------------------------------------*/
Bean::~Bean ()
{
	jeBeanDispose (fBean);

	delete fBeanTag;
}


/*-------------------------------------------------------------------+
|                          Bean::Init ()                           |
| ================================================================== |
|                                                                    |
| Function:    Initialize the Java Bean...                           |
|                                                                    |
| Algorithm:   o  Initialize the JavaRuntime...                      |
|              o  Set proxy info                                     |
|              o  Get parent window                                  |
|              o  Initialize Bean                                    |
|              o  Resize containing frame                            |
|                                                                    |
+-------------------------------------------------------------------*/
void Bean::Init ()
{
#ifdef DEBUG
	qprintf ("Entering Bean::Init\n");
#endif

	/*
	 *  Initialize the Java runtime once for each process...
	 */
	if (gRuntimeInitialized == FALSE) {
		gRuntimeInitialized = TRUE;

		jeInitJavaRuntime ();

		jeRegisterShowConsole (BeanShowConsole);

            /*
             *  Pass the proxy information on to the Java runtime...
             */
            char proxyHost [255];
            char proxyPort [10];
            char proxySet  [6];

            GetProxy (proxyHost, proxyPort, proxySet);

            jeSetProperty ("proxySet", proxySet);
            jeSetProperty ("proxyHost", proxyHost);
            jeSetProperty ("proxyPort", proxyPort);
            jeSetProperty ("trustProxy", "true");
	}

	/*
	 *  Get our parent window via the part's facet...
	 */
	Environment *ev = somGetGlobalEnvironment();

	ODFacet* facet = fJavaPart->_get_fFacet(ev);
	HWND parentHWND = facet->GetFacetHWND (ev);

#ifdef DEBUG
	qprintf ("Bean info: \n\n className = '%s'\n", fBeanTag->fClassName);
#endif

	/*
	 *  Initialize the bean...
	 */
      fBean = kODNULL;
      jeBeanInstantiate(&fBean, fBeanTag->fClassName);

      HWND beanHWND = kODNULL;
      jeBeanEmbed(&beanHWND, fBean);

      SetParent(beanHWND, parentHWND); //jwa: this call sometimes fails on my NT 4.0 system
      if (GetParent(beanHWND)==NULL) { //jwa: workaround for failure of above call
         SetWindowLong(beanHWND, GWL_HWNDPARENT, (LONG) parentHWND);
      } /* endif - workaround */

      OriginalBeanWndProc = (FARPROC) SetWindowLong(beanHWND, GWL_WNDPROC,
         (LONG) &BeanWndProcSubclass);

#ifdef DEBUG
      qprintf("beanHWND==0x%x, parentHWND==0x%x\n", beanHWND, parentHWND);
#endif

	/*
	 *  Change the size of our containing frame...
	 */
	fJavaPart->SetFrameSize (ev, facet->GetFrame (ev), fBeanTag->fWidth, 
		fBeanTag->fHeight+20 /* allow for bean's title bar */);

      /* Maximize the bean window */
      /* jwa: Note that this call will need to be removed when J-Empower */
      /* is updated to not have a title bar affixed to the bean          */
      ShowWindow(beanHWND, SW_MAXIMIZE);
}



/*-------------------------------------------------------------------+
|                         Bean::Resize ()                          |
| ================================================================== |
|                                                                    |
| Function:    Resize the Java Bean...                             |
|                                                                    |
| Parameters:  width  - new width (in pixels)                        |
|              height - new height (in pixels)                       |
|                                                                    |
+-------------------------------------------------------------------*/
void Bean::Resize (int width, int height)
{
// jwa: There ought to be a jeBeanResize API, like demonstrated in the comment below
//    jeBeanResize (fBean, width, height);

      HWND beanHWND = jeHWNDFromBean(fBean);
      SetWindowPos(beanHWND, 0, 0, 0, width, height, SWP_NOZORDER | SWP_SHOWWINDOW);
}


/*-------------------------------------------------------------------+
|                      Bean::ShowConsole ()                        |
| ================================================================== |
|                                                                    |
| Function:    Show the Java console...                              |
|                                                                    |
+-------------------------------------------------------------------*/
void Bean::ShowConsole ()
{
//	jeConsoleShow (TRUE); //jwa: API changed name in JEmpower 1.1
	jeConsoleSetVisible (TRUE);
}


/*-------------------------------------------------------------------+
|                      Bean::HideConsole ()                        |
| ================================================================== |
|                                                                    |
| Function:    Hide the Java console...                              |
|                                                                    |
+-------------------------------------------------------------------*/
void Bean::HideConsole ()
{
//	jeConsoleShow (FALSE); //jwa: API changed name in JEmpower 1.1
	jeConsoleSetVisible (FALSE);
}


/*-------------------------------------------------------------------+
|                        Bean::GetBeanTag ()                         |
| ================================================================== |
|                                                                    |
| Function:    Return the BeanTag.  Called when externalizing or     |
|              cloning...                                            |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag* Bean::GetBeanTag ()
{
	return fBeanTag;
}
