/* 
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 "iowacore.h"
#include <appletfiles.h>
#include <appletcardops.h>
#include <applet.h>
#include <appletmain.h>
#include "iowawindowatts.h"


#define modalitem 1
#define movablemodalitem 2
#define modelessitem 3
#define windoiditem 4


static hdlcard windowattributescard = nil;

static boolean flresetwindow = false;	



static short popuptowindowresource (short ixpopup) {
	
	switch (ixpopup) {
	
		case modalitem:
			return (1024);
			
		case movablemodalitem:
			return (1025);
			
		case modelessitem:
			return (1026);
			
		case windoiditem:
			return (1027);
			
		default:
			return (1024);
		} /*switch*/
	} /*popuptowindowresource*/
	
	
static short windowresourcetopopup (short windresource) {
	
	switch (windresource) {
	
		case 1024:
			return (modalitem);
			
		case 1025:
			return (movablemodalitem);
			
		case 1026:
			return (modelessitem);
			
		case 1027:
			return (windoiditem);
			
		default:
			return (modalitem);
		} /*switch*/
	} /*windowresourcetopopup*/
	
	
static void copytodialog (void) {

	pushmacport (nil);
	
	setfrontglobals ();
		
	if (iowadata != nil) {
		
		setcardtexthandle ("\ptitle", (**iowadata).windowtitle);
	
		setcardnumber ("\pbottomBorder", (**iowadata).bottomborder);
		
		setcardnumber ("\prightBorder", (**iowadata).rightborder);
		
		setcardflag ("\pgrid", (**iowadata).flgrid);
		
		setcardflag ("\pshowGrid", !(**iowadata).flinvisiblegrid);
		
		setcardnumber ("\pgridUnits", (**iowadata).gridunits);
		
		setcardcolorpopup ("\pbackcolor", &(**iowadata).backcolor);
			
		setcardpopupcheckeditem ("\ptype", windowresourcetopopup ((**iowadata).idwindow));
		
		/*setcardflag ("\pisFloater", (**iowadata).floater);*/
		
		/*setcardnumber ("\pidwindow", (**iowadata).idwindow);*/
		}
	else {
		setcardstring ("\ptitle", "\p");
		
		setcardstring ("\pbottomBorder", "\p");
		
		setcardstring ("\prightBorder", "\p");
		
		setcardflag ("\pgrid", false);
		
		setcardflag ("\pshowGrid", false);
		
		setcardnumber ("\pgridUnits", false);
		
		setcardcolorpopup ("\pbackcolor", &whitecolor);
		}
			
	popmacport ();
	} /*copytodialog*/
	
	
static void copyfromdialog (void) {

	long rightborder, bottomborder, idwindow, gridunits;
	boolean flgrid, flinvisiblegrid;
	RGBColor backcolor;
	Handle windowtitle;
	
	getcardnumber ("\pbottomBorder", &bottomborder);
	
	getcardnumber ("\prightBorder", &rightborder);
	
	idwindow = popuptowindowresource (getcardpopup ("\ptype"));
	
	flgrid = getcardbit ("\pgrid");
	
	flinvisiblegrid = !getcardbit ("\pshowGrid");
	
	getcardnumber ("\pgridUnits", &gridunits);
	
	getcardcolorpopup ("\pbackcolor", &backcolor);
	
	getcardtexthandle ("\ptitle", &windowtitle);
	
	pushmacport (nil);
	
	setfrontglobals ();
		
	if (iowadata != nil) {
	
		appprecallback ();
	
		(**iowadata).rightborder = rightborder;
		
		(**iowadata).bottomborder = bottomborder;
		
		(**iowadata).idwindow = idwindow;
		
		(**iowadata).gridunits = gridunits;
		
		(**iowadata).windowtitle = windowtitle;
		
		(**iowadata).flgrid = flgrid;
		
		(**iowadata).flinvisiblegrid = flinvisiblegrid;
		
		(**iowadata).backcolor = backcolor;
		
		madechanges ();
				
		zapcardwindow (); 
				
		apppostcallback ();
		}
			
	popmacport ();
	} /*copyfromdialog*/
	
	
static void checkenable (void) {
	
	setcardenable ("\papply", (**currentcard).runmodechanges);		
	} /*checkenable*/
	
	
static void windowattributesfilter (EventRecord *ev) {
	
	switch ((*ev).what) {
		
		case iowaInitEvent: {			
			currentcard = (hdlcard) (*ev).message;
			
			windowattributescard = currentcard;
			
			copytodialog ();
			
			checkenable ();
	
			break;
			}
			
		case activateEvt: 
			if ((*ev).modifiers & activeFlag)
				copytodialog ();
				
			break;
		
		case iowaCloseEvent:
			currentcard = (hdlcard) (*ev).message; 
			
			appsavedialoginfo (130); /*remember window position*/
			
			windowattributescard = nil; /*we always forget to do this!!! DW 9/13/93*/
			
			break;		
					
		case iowaButtonHitEvent: {
			hdlobject hobject = (hdlobject) (*ev).message;
			bigstring bs;
			
			texthandletostring ((**hobject).objectname, bs);
			
			if (equalstrings (bs, "\papply")) {
			
				currentcard = (hdlcard) (**hobject).owningcard;
	
				copyfromdialog ();
				
				(**currentcard).runmodechanges = false;
				}
							
			break;
			}
			
		case nullEvent: 
			currentcard = (hdlcard) (*ev).message; /*copy into global*/
			
			if (flresetwindow) {
				
				flresetwindow = false;
				
				copytodialog ();
				}
				
			checkenable ();
			
			break;
			
		case iowaSetHandleEvent:
			currentcard = (hdlcard) (*ev).message; /*copy into global*/
			
			break;			
		} /*switch*/
	} /*windowattributesfilter*/
	
	
void resetwindowattributes (void) {
	
	/*
	call this when some attribute of the window changed.
	*/
	
	if (windowattributescard != nil) 
		flresetwindow = true;	
	} /*resetwindowattributes*/
	

void windowattributesdialog (void) {
	
	if (windowattributescard == nil) 		
		appopencardresource (130, &windowattributesfilter);
	else 
		SelectWindow ((**windowattributescard).drawwindow); 
	} /*windowattributesdialog*/