//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odother
//
//   CLASSES: none
//
//   ORIGINS: 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========================================
//
// @(#) 1.2 nt/src/samples/JavaPart/applet.cpp, odjavapart, od96nt, odnt9714c 3/19/97 03:57:49 [ 4/2/97 17:55:01 ]

#ifndef IBMSample_APPLET
#include "Applet.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

/*
 *  Global variable for indicating wether the Java runtime has been
 *  initialized for this process...
 */
BOOL gRuntimeInitialized = FALSE;

#include <qprintf.h>

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

        return 0;
}


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


/*-------------------------------------------------------------------+
|                        Applet::~Applet ()                          |
| ================================================================== |
|                                                                    |
| Function:    Applet destructor                                     |
|                                                                    |
| Algorithm:   o  Stop and destroy the applet                        |
|              o  Destroy it's window                                |
|                                                                    |
+-------------------------------------------------------------------*/
Applet::~Applet ()
{
//	HWND window = jeHWNDFromHandle (fApplet); //jwa: API changed name in JEmpower 1.1
	HWND window = jeHWNDFromApplet (fApplet);

	jeAppletStop (fApplet);
	jeAppletDestroy (fApplet);

	DestroyWindow (window);

	delete fAppletTag;
}


/*-------------------------------------------------------------------+
|                          Applet::Init ()                           |
| ================================================================== |
|                                                                    |
| Function:    Initialize the Java Applet...                         |
|                                                                    |
| Algorithm:   o  Initialize the JavaRuntime...                      |
|              o  Set proxy info                                     |
|              o  Get parent window                                  |
|              o  Initialize Applet                                  |
|              o  Resize containing frame                            |
|                                                                    |
+-------------------------------------------------------------------*/
void Applet::Init ()
{
	/*
	 *  Initialize the Java runtime once for each process...
	 */
	if (gRuntimeInitialized == FALSE) {
		gRuntimeInitialized = TRUE;

		jeInitJavaRuntime ();

		jeRegisterShowConsole (MyShowConsole);
	}

	/*
	 *  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 parentWindow = facet->GetFacetHWND (ev);

#ifdef DEBUG
	qprintf ("Applet info: \n\n url = '%s'\n code = '%s'\n codeBase = '%s'\n", 
		fAppletTag->fUrl, fAppletTag->fCode, fAppletTag->fCodeBase);
#endif

	/*
	 *  Initialize the applet...
	 */
	jeAppletInit (&fApplet, 
		      1, 
		      (JENATIVEWND) parentWindow,
		      fAppletTag->fUrl, 
		      fAppletTag->fCode,
		      kODNULL, //jwa: parameter added in JEmpower 1.1, for reloading serialized applets
		      fAppletTag->fCodeBase, 
		      fAppletTag->fWidth,
		      fAppletTag->fHeight, 
		      fAppletTag->fNumArgs, 
		      (const char**) fAppletTag->fArgs);

//	HWND window = jeHWNDFromHandle (fApplet); //jwa: API changed name in JEmpower 1.1
//	HWND window = jeHWNDFromApplet (fApplet); //jwa: Unneeded - see comment below

//	ShowWindow (window, TRUE);                //jwa: Now, we call jeAppletShow() instead
	jeAppletShow(fApplet, TRUE); //jwa

	/*
	 *  Change the size of our containing frame...
	 */
	fJavaPart->SetFrameSize (ev, facet->GetFrame (ev), fAppletTag->fWidth, 
				 fAppletTag->fHeight);
}


/*-------------------------------------------------------------------+
|                          Applet::Start ()                          |
| ================================================================== |
|                                                                    |
| Function:    Start the Java Applet...                              |
|                                                                    |
+-------------------------------------------------------------------*/
void Applet::Start ()
{
	jeAppletStart (fApplet);
}


/*-------------------------------------------------------------------+
|                          Applet::Stop ()                           |
| ================================================================== |
|                                                                    |
| Function:    Stop the Java Applet...                               |
|                                                                    |
+-------------------------------------------------------------------*/
void Applet::Stop ()
{
	jeAppletStop (fApplet);
}


/*-------------------------------------------------------------------+
|                         Applet::Resize ()                          |
| ================================================================== |
|                                                                    |
| Function:    Resize the Java Applet...                             |
|                                                                    |
| Parameters:  width  - new width (in pixels)                        |
|              height - new height (in pixels)                       |
|                                                                    |
+-------------------------------------------------------------------*/
void Applet::Resize (int width, int height)
{
	jeAppletResize (fApplet, width, height);
}


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


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


/*-------------------------------------------------------------------+
|                      Applet::GetAppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    Return the AppletTag.  Called when externalizing or   |
|              cloning...                                            |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag* Applet::GetAppletTag ()
{
	return fAppletTag;
}
