/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include <appletdefs.h>
#include <appletkb.h>
#include <appleticons.h>
#include <appletfiles.h>
#include "iowacore.h"
#include <appletcardops.h>
#include "iowascript.h"
#include "iowaquickscript.h"


static hdlcard quickscriptcard = nil;


static void getscripttext (Handle *hscript) {
	
	getcardtexthandle ("\pscript", hscript);
	} /*getscripttext*/
	

static void runquickscript (void) {
	
	OSType idlanguage;
	Handle hscript, hreturns;
	bigstring errorstring;
	 
	getlanguageid (getcardpopup ("\planguages") - 1, &idlanguage);
	
	getcardtexthandle ("\pscript", &hscript);
	
	if (!runlangscript (hscript, nil, idlanguage, true, errorstring, &hreturns)) {
		
		if (stringlength (errorstring) > 0)
			alertdialog (errorstring);
		
		return;
		}

	setcardtexthandle ("\presult", hreturns);
	
	disposehandle (hreturns);
	} /*runquickscript*/
	
	
static boolean handleiconclick (hdlobject h, boolean fl2click) {
	
	runquickscript ();
	
	return (true);
	} /*handleiconclick*/
	
	
static void initdialog (void) {
	
	MenuHandle hlangmenu;
	Handle hscript;
	
	getlangmenu (&hlangmenu);
	
	setcardpopupfrommenu ("\planguages", hlangmenu, iowaprefs.ixdefaultquickscriptlanguage);
	
	DisposeMenu (hlangmenu);
	
	newfilledhandle (&iowaprefs.savedquickscript, iowaprefs.lensavedquickscript, &hscript);
	
	setcardtexthandle ("\pscript", hscript);
	
	disposehandle (hscript);
	
	setcardicon ("\prunIcon", atVerticalCenter + atHorizontalCenter, 0, nil, handleiconclick);
	} /*initdialog*/
	
	
static void quickscriptfilter (EventRecord *ev) {
	
	switch ((*ev).what) {
		
		case iowaInitEvent: 
			currentcard = (hdlcard) (*ev).message;
			
			quickscriptcard = currentcard;
			
			initdialog ();
			
			break;
			
		case iowaCloseEvent:
			currentcard = (hdlcard) (*ev).message; 
			
			/*save off elements of the iowaprefs record*/ {
				
				Handle hscript;
				
				iowaprefs.ixdefaultquickscriptlanguage = getcardpopup ("\planguages");
			
				getcardtexthandle ("\pscript", &hscript);
				
				iowaprefs.lensavedquickscript = gethandlesize (hscript);
				
				moveleft (*hscript, &iowaprefs.savedquickscript, iowaprefs.lensavedquickscript);
				}
			
			appsavedialoginfo (136); /*remember window position*/
			
			quickscriptcard = nil; /*we always forget to do this!!! DW 9/13/93*/
			
			break;		
					
		case iowaSetHandleEvent:
			currentcard = (hdlcard) (*ev).message; /*copy into global*/
			
			break;			
			
		case keyDown: case autoKey:
			setkeyboardstatus (*ev); /*set fields of keyboardstatus record*/
			
			if (keyboardstatus.chkb == chenter)
				runquickscript ();
			
			break;
		} /*switch*/
	} /*quickscriptfilter*/
	
	
void iowaopenquickscript (void) {
	
	if (quickscriptcard == nil) 		
		appopencardresource (136, &quickscriptfilter);
	else 
		SelectWindow ((**quickscriptcard).drawwindow); 
	} /*iowaopenquickscript*/