/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.gfui; import gplx.*;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;

import org.eclipse.swt.widgets.Display;

import java.awt.AWTEvent;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class GxwWin_lang extends JFrame implements GxwWin, WindowListener  {
	public void ShowWin() 		{this.setVisible(true);}
	public void HideWin() 		{this.setVisible(false);}
	public boolean Minimized()	{return this.getState() == Frame.ICONIFIED;} public void Minimized_(boolean v) {this.setState(v ? Frame.ICONIFIED : Frame.NORMAL);} 
	public boolean Maximized() 	{return this.getState() == Frame.MAXIMIZED_BOTH;} public void Maximized_(boolean v) {this.setState(v ? Frame.MAXIMIZED_BOTH : Frame.NORMAL);}
	public void CloseWin() 		{this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.dispose();}
	public boolean Pin() 		{return pin;}
	public void Pin_set(boolean val) {
		this.setAlwaysOnTop(val); pin = val;} boolean pin = false;
	public IconAdp IconWin() {return icon;} IconAdp icon;
	public void IconWin_set(IconAdp i) {
		if (i == null) return;
		icon = i;
		this.setIconImage(i.XtoImage());
	}
	public void OpenedCmd_set(GfoInvkAbleCmd v) {whenLoadedCmd = v;} GfoInvkAbleCmd whenLoadedCmd = GfoInvkAbleCmd.Null;	
	public GxwCore_base Core() {return ctrlMgr;} GxwCore_base ctrlMgr;
	public GxwCbkHost Host() {return host;} public void Host_set(GxwCbkHost host) {this.host = host;} GxwCbkHost host = GxwCbkHost_.Null;
	public String TextVal() {return this.getTitle();} public void TextVal_set(String v) {this.setTitle(v);}
	public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
	public void SendKeyDown(IptKey key)				{}
	public void SendMouseMove(int x, int y)			{}
	public void SendMouseDown(IptMouseBtn btn)		{}
	public void windowActivated(WindowEvent e) 		{}
	public void windowClosed(WindowEvent e) 		{}
	public void windowClosing(WindowEvent e) 		{host.DisposeCbk();}
	public void windowDeactivated(WindowEvent e) 	{}
	public void windowDeiconified(WindowEvent e) 	{host.SizeChangedCbk();}
	public void windowIconified(WindowEvent e) 		{host.SizeChangedCbk();}
	public void windowOpened(WindowEvent e) 		{whenLoadedCmd.Invk();}
	@Override public void processKeyEvent(KeyEvent e) 					{if (GxwCbkHost_.ExecKeyEvent(host, e)) 	super.processKeyEvent(e);}
	@Override public void processMouseEvent(MouseEvent e) 				{if (GxwCbkHost_.ExecMouseEvent(host, e)) 	super.processMouseEvent(e);}
	@Override public void processMouseWheelEvent(MouseWheelEvent e) 	{if (GxwCbkHost_.ExecMouseWheel(host, e)) 	super.processMouseWheelEvent(e);}
	@Override public void processMouseMotionEvent(MouseEvent e)			{if (host.MouseMoveCbk(IptEvtDataMouse.new_(IptMouseBtn_.None, IptMouseWheel_.None, e.getX(), e.getY()))) super.processMouseMotionEvent(e);}
	@Override public void paint(Graphics g) {
		if (host.PaintCbk(PaintArgs.new_(GfxAdpBase.new_((Graphics2D)g), RectAdp_.Zero)))	// ClipRect not used by any clients; implement when necessary	
			super.paint(g);
	}
	public void EnableDoubleBuffering() {}
	public void TaskbarVisible_set(boolean val) {} 	public void TaskbarParkingWindowFix(GxwElem form) {}
	public static GxwWin_lang new_() {
		GxwWin_lang rv = new GxwWin_lang();
		rv.ctor_GxwForm();
		return rv;
	}	GxwWin_lang() {}
	void ctor_GxwForm() {
		ctrlMgr = GxwCore_form.new_(this);
		this.setLayout(null);										// use gfui layout
		this.ctrlMgr.BackColor_set(ColorAdp_.White);				// default form backColor to white
		this.setUndecorated(true);									// remove icon, titleBar, minimize, maximize, close, border
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	// JAVA: cannot cancel alt+f4; set Close to noop, and manually control closing by calling this.CloseForm
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
		this.addWindowListener(this);
		GxwBoxListener lnr = new GxwBoxListener(this);
		this.addComponentListener(lnr);
		this.addFocusListener(lnr);	
	}	
}
class GxwWin_jdialog extends JDialog implements GxwWin, WindowListener  {
	public void ShowWin() {this.setVisible(true);}
	public void HideWin() {this.setVisible(false);}
	public void CloseWin() {this.dispose();}//this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.dispose();
	public boolean Maximized() {return false;} public void Maximized_(boolean v) {}
	public boolean Minimized() {return false;} public void Minimized_(boolean v) {} //this.setState(Frame.ICONIFIED);
	public boolean Pin() {return pin;} public void Pin_set(boolean val) {
		this.setAlwaysOnTop(val); pin = val;} boolean pin = false;
	public IconAdp IconWin() {return icon;} IconAdp icon;
	public void IconWin_set(IconAdp i) {
		if (i == null) return;
		icon = i;
		this.setIconImage(i.XtoImage());
	}
	public void OpenedCmd_set(GfoInvkAbleCmd v) {whenLoadedCmd = v;} GfoInvkAbleCmd whenLoadedCmd = GfoInvkAbleCmd.Null;	
	public GxwCore_base Core() {return ctrlMgr;} GxwCore_base ctrlMgr;
	public GxwCbkHost Host() {return host;} public void Host_set(GxwCbkHost host) {this.host = host;} GxwCbkHost host = GxwCbkHost_.Null;
	public String TextVal() {return "";} public void TextVal_set(String v) {}
	public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
	public void SendKeyDown(IptKey key)				{}
	public void SendMouseMove(int x, int y)			{}
	public void SendMouseDown(IptMouseBtn btn)		{}
	public void windowActivated(WindowEvent e) 		{}
	public void windowClosed(WindowEvent e) 		{}
	public void windowClosing(WindowEvent e) 		{host.DisposeCbk();}
	public void windowDeactivated(WindowEvent e) 	{}
	public void windowDeiconified(WindowEvent e) 	{host.SizeChangedCbk();}
	public void windowIconified(WindowEvent e) 		{host.SizeChangedCbk();}
	public void windowOpened(WindowEvent e) 		{whenLoadedCmd.Invk();}
	@Override public void processKeyEvent(KeyEvent e) 					{if (GxwCbkHost_.ExecKeyEvent(host, e)) 	super.processKeyEvent(e);}
	@Override public void processMouseEvent(MouseEvent e) 				{if (GxwCbkHost_.ExecMouseEvent(host, e)) 	super.processMouseEvent(e);}
	@Override public void processMouseWheelEvent(MouseWheelEvent e) 	{if (GxwCbkHost_.ExecMouseWheel(host, e))	super.processMouseWheelEvent(e);}
	@Override public void processMouseMotionEvent(MouseEvent e)			{if (host.MouseMoveCbk(IptEvtDataMouse.new_(IptMouseBtn_.None, IptMouseWheel_.None, e.getX(), e.getY()))) super.processMouseMotionEvent(e);}
	@Override public void paint(Graphics g) {
		if (host.PaintCbk(PaintArgs.new_(GfxAdpBase.new_((Graphics2D)g), RectAdp_.Zero)))	// ClipRect not used by any clients; implement when necessary	
			super.paint(g);
	}
	public void EnableDoubleBuffering() {}
	public void TaskbarVisible_set(boolean val) {} public void TaskbarParkingWindowFix(GxwElem form) {}
	public static GxwWin new_(GxwWin owner) {
		Window ownerWindow = owner instanceof Window ? (Window)owner : null; 
		GxwWin_jdialog rv = new GxwWin_jdialog(ownerWindow);
		rv.ctor_GxwForm();
		return rv;
	}	GxwWin_jdialog(Window owner) {super(owner);}
	void ctor_GxwForm() {
		ctrlMgr = GxwCore_form.new_(this);
		this.setLayout(null);										// use gfui layout
		this.ctrlMgr.BackColor_set(ColorAdp_.White);				// default form backColor to white
		this.setUndecorated(true);									// remove icon, titleBar, minimize, maximize, close, border
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	// JAVA: cannot cancel alt+f4; set CloseOp to noop, and manually control closing by calling this.CloseForm
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
		this.addWindowListener(this);
		GxwBoxListener lnr = new GxwBoxListener(this);
		this.addComponentListener(lnr);
		this.addFocusListener(lnr);	
	}
}
class GxwWin_jwindow extends JWindow implements GxwWin, WindowListener  {
	public void ShowWin() {this.setVisible(true);}
	public void HideWin() {this.setVisible(false);}
	public void CloseWin() {} //this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.dispose();
	public boolean Maximized() {return false;} public void Maximized_(boolean v) {}
	public boolean Minimized() {return false;} public void Minimized_(boolean v) {} 
	public boolean Pin() {return pin;} public void Pin_set(boolean val) {this.setAlwaysOnTop(val); pin = val;} boolean pin = false;
	public IconAdp IconWin() {return icon;} IconAdp icon;
	public void IconWin_set(IconAdp i) {
		if (i == null) return;
		icon = i;
		this.setIconImage(i.XtoImage());
	}
	public void OpenedCmd_set(GfoInvkAbleCmd v) {whenLoadedCmd = v;} GfoInvkAbleCmd whenLoadedCmd = GfoInvkAbleCmd.Null;	
	public GxwCore_base Core() {return ctrlMgr;} GxwCore_base ctrlMgr;
	public GxwCbkHost Host() {return host;} public void Host_set(GxwCbkHost host) {this.host = host;} GxwCbkHost host = GxwCbkHost_.Null;
	public String TextVal() {return "";} public void TextVal_set(String v) {}// this.setTitle(v);
	public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
	public void SendKeyDown(IptKey key)				{}
	public void SendMouseMove(int x, int y)			{}
	public void SendMouseDown(IptMouseBtn btn)		{}
	public void windowActivated(WindowEvent e) 		{}
	public void windowClosed(WindowEvent e) 		{}
	public void windowClosing(WindowEvent e) 		{host.DisposeCbk();}
	public void windowDeactivated(WindowEvent e) 	{}
	public void windowDeiconified(WindowEvent e) 	{host.SizeChangedCbk();}
	public void windowIconified(WindowEvent e) 		{host.SizeChangedCbk();}
	public void windowOpened(WindowEvent e) 		{whenLoadedCmd.Invk();}
	@Override public void processKeyEvent(KeyEvent e) 					{if (GxwCbkHost_.ExecKeyEvent(host, e)) 	super.processKeyEvent(e);}
	@Override public void processMouseEvent(MouseEvent e) 				{if (GxwCbkHost_.ExecMouseEvent(host, e))	super.processMouseEvent(e);}
	@Override public void processMouseWheelEvent(MouseWheelEvent e) 	{if (GxwCbkHost_.ExecMouseWheel(host, e))	super.processMouseWheelEvent(e);}
	@Override public void processMouseMotionEvent(MouseEvent e)			{if (host.MouseMoveCbk(IptEvtDataMouse.new_(IptMouseBtn_.None, IptMouseWheel_.None, e.getX(), e.getY()))) super.processMouseMotionEvent(e);}
	@Override public void paint(Graphics g) {
		if (host.PaintCbk(PaintArgs.new_(GfxAdpBase.new_((Graphics2D)g), RectAdp_.Zero)))	// ClipRect not used by any clients; implement when necessary	
			super.paint(g);
	}
	public void EnableDoubleBuffering() {}
	public void TaskbarVisible_set(boolean val) {} public void TaskbarParkingWindowFix(GxwElem form) {}
	public static GxwWin new_(GxwWin owner) {
//		Window ownerWindow = owner instanceof Window ? (Window)owner : null; 
//		GxwWin_jwindow rv = new GxwWin_jwindow((Window)owner);
		GxwWin_jwindow rv = new GxwWin_jwindow();
		rv.ctor_GxwForm();
//		rv.setUndecorated(true);
		return rv;
	}	GxwWin_jwindow(Window owner) {super(owner);}
	GxwWin_jwindow() {}
	public static GxwWin owner_(GxwWin owner) {
		GxwWin_jwindow rv = new GxwWin_jwindow((Window)owner);
		rv.setFocusable(true);
		rv.setFocusableWindowState(true);
		rv.ctor_GxwForm();
		return rv;
	}
	public void ctor_GxwForm() {
		ctrlMgr = GxwCore_form.new_(this);
		this.setLayout(null);										// use gfui layout
		this.ctrlMgr.BackColor_set(ColorAdp_.White);				// default form backColor to white
//		this.setUndecorated(true);									// remove icon, titleBar, minimize, maximize, close, border
//		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	// JAVA: cannot cancel alt+f4; set CloseOp to noop, and manually control closing by calling this.CloseForm
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
		this.addWindowListener(this);
		GxwBoxListener lnr = new GxwBoxListener(this);
		this.addComponentListener(lnr);
		this.addFocusListener(lnr);	
	}	
}
class GxwCore_form extends GxwCore_lang {
	@Override public void Zorder_front() {
		Window frame = (Window)this.control;
		frame.toFront();
	}
	@Override public void Zorder_back() {
		Window frame = (Window)this.control;
		frame.toBack();
	}
    public static GxwCore_form new_(Component control) {
    	GxwCore_form rv = new GxwCore_form();
		rv.control = control;
		return rv;
	}	GxwCore_form() {}
}



class GxwElemFactory_swt extends GxwElemFactory_base {
	public GxwElemFactory_swt(org.eclipse.swt.widgets.Display display) {
		this.display = display;
	}	Display display;
@gplx.Internal @Override protected GxwElem control_() {return null;}
@gplx.Internal @Override protected GxwWin win_app_() {
	return new Swt_win(display);
}
@gplx.Internal @Override protected GxwWin win_tool_(KeyValHash ctorArgs)	{
	return null;
}
@gplx.Internal @Override protected GxwWin win_toaster_(KeyValHash ctorArgs)	{
	return null;
}
@gplx.Internal @Override protected GxwElem lbl_() {return null;}
@gplx.Internal @Override protected GxwTextFld text_fld_() {return null;}
@gplx.Internal @Override protected GxwTextFld text_memo_() {return null;}
@gplx.Internal @Override protected GxwTextHtml text_html_() {return null;}
@gplx.Internal @Override protected GxwCheckListBox checkListBox_(KeyValHash ctorArgs) {throw Err_.not_implemented_();}
@gplx.Internal @Override protected GxwComboBox comboBox_() {return null;}
@gplx.Internal @Override protected GxwListBox listBox_() {return null;}
}


//#}