August 08, 2008
Hi there,

I am reading SWT: A Developer's Notebook and tried some code in the book,sure I also tried to re-organize the structure and to see what happens.In the attached simple code,the Option menu,Window menu and toobar item CHECK does not work properly.When I click on it,the application quit immediatley.I don't know why.

Can anyboy help me?
Thanks and best Regards,
Sam


August 08, 2008
Sam Hu Wrote:

> Hi there,
> 
> I am reading SWT: A Developer's Notebook and tried some code in the book,sure I also tried to re-organize the structure and to see what happens.In the attached simple code,the Option menu,Window menu and toobar item CHECK does not work properly.When I click on it,the application quit immediatley.I don't know why.
> 
> Can anyboy help me?
> Thanks and best Regards,
> Sam
> 
I tried to open the attached code to see whether other people can open it ,but I failed.So I post the code here:

/***************************************************/
module dwtEx01;
import dwt.widgets.Widget;
import dwt.widgets.Shell;
import dwt.widgets.Display;
import dwt.widgets.Menu;
import dwt.widgets.MenuItem;
import dwt.widgets.ToolBar;
import dwt.widgets.ToolItem;
import dwt.widgets.ToolTip;
import dwt.graphics.Image;
import dwt.widgets.Dialog;
import dwt.widgets.MessageBox;
import dwt.DWT;
import dwt.events.SelectionEvent;
import dwt.events.SelectionListener;


import tango.io.Stdout;

public class SubForm
{
	this(Shell parent,int style)
	{
		Shell shell=new Shell(parent,style);
		shell.setText("Sub Form");
		shell.setSize(300,300);
		shell.open;
	}

}
class MyDialog:Dialog
{
	this(Shell parent)
	{
		super(parent);
	}

	void open()
	{

		Shell parent=getParent;
		Shell shell=new Shell(parent,DWT.DIALOG_TRIM|DWT.APPLICATION_MODAL);
		shell.setSize(150,80);
		shell.setText("Modal dialog");
		Display display=parent.getDisplay;
		shell.open;




		while(!shell.isDisposed)
		{
			if(!display.readAndDispatch)
			display.sleep;
		}
	}
}

public class Form
{
	private:
	Display display;
	Shell shell;
	Menu menu;
	ToolBar bar;;

	SubForm subForm;
	SubForm[] subFormArray;
	MyDialog myDialog;

	public:
	this()
	{
		initializeComponents;

	}

	void initializeComponents()
	{
		display=new Display;
		shell =new Shell(display);
		//Image img=new Image(display,r"C:\dmd\html\d\d002.ico");
		shell.setSize(500,500);


		shell.setText("Example 01");
		//shell.setImage(img);

		//ToolBar:
		bar=createToolBar;

		//main menu:
		menu=createMainMenu;
		shell.setMenuBar(menu);

		//add pop-up menu to the form:
		shell.setMenu(createPopMenu);

		//all done
		shell.open;


	/+
		subFormArray=
	[
		new SubForm(shell,DWT.DIALOG_TRIM),
		new SubForm(shell,DWT.SHELL_TRIM),
		new SubForm(shell,DWT.BORDER),
		new SubForm(shell,DWT.NO_TRIM)/*no close,no title,no min,no max*/
	];

		MyDialog myDialog=new MyDialog(shell);
		myDialog.open;

	+/
		while(!shell.isDisposed)
		{
			if(!display.readAndDispatch)
			display.sleep;
		}
		display.dispose;


	}

	private:
	Menu createMainMenu()
	{
		menu=new Menu(shell,DWT.BAR);

		//menu item File:
		final MenuItem file=new MenuItem(menu,DWT.CASCADE);
		file.setText("&File");

		//File Menu:
		final Menu fileMenu=new Menu(shell,DWT.DROP_DOWN);
		file.setMenu(fileMenu);

		//menu item open:
		final MenuItem openItem=new MenuItem(fileMenu,DWT.CASCADE);
		openItem.setText("&Open");

		//submenu attach to open menu:
		final Menu subMenu=new Menu(shell,DWT.DROP_DOWN);


		//subMenu content:
		final MenuItem childItem=new MenuItem(subMenu,DWT.PUSH);
		childItem.setText("Child");
		childItem.setAccelerator(DWT.ALT+'C');


		final MenuItem dialogItem=new MenuItem(subMenu,DWT.PUSH);
		dialogItem.setText("Dialog");
		dialogItem.setAccelerator(DWT.CTRL+'D');

		openItem.setMenu(subMenu);
		//menu item --:
		MenuItem separator=new MenuItem(fileMenu,DWT.SEPARATOR);

		//menu item exit:
		MenuItem exitItem=new MenuItem(fileMenu,DWT.PUSH);
		exitItem.setText("E&xit");

		//Menu item Edit
		final MenuItem edit=new MenuItem(menu,DWT.CASCADE);
		edit.setText("Edit");

		final Menu editMenu=new Menu(shell,DWT.DROP_DOWN);
		edit.setMenu(editMenu);

		final MenuItem cutItem=new MenuItem(editMenu,DWT.PUSH);
		cutItem.setText("Cut");

		final MenuItem copyItem=new MenuItem(editMenu,DWT.PUSH);
		copyItem.setText("Copy");

		final MenuItem pasteItem=new MenuItem(editMenu,DWT.PUSH);
		pasteItem.setText("Paste");

		//check and radio button menu:
		final MenuItem options=new MenuItem(menu,DWT.CASCADE);
		options.setText("Options");
		final Menu optionMenu=new Menu(shell,DWT.DROP_DOWN);

		final MenuItem checkItem=new MenuItem(optionMenu,DWT.CHECK);
		checkItem.setText("Checked Option");
		checkItem.setAccelerator(DWT.CTRL+'C');
		checkItem.setSelection(true);


		final MenuItem opionsSeparator=new MenuItem(optionMenu,DWT.SEPARATOR);
		final MenuItem radioItem1=new MenuItem(optionMenu,DWT.RADIO);
		radioItem1.setText("Radio One");
		radioItem1.setAccelerator(DWT.ALT+'R');

		final MenuItem radioItem2=new MenuItem(optionMenu,DWT.RADIO);
		radioItem2.setText("Radio Two");
		radioItem2.setSelection(true);

		options.setMenu(optionMenu);
		//Window Menu:
		final MenuItem window=new MenuItem(menu,DWT.CASCADE);
		window.setText("&Window");

		final Menu windowMenu=new Menu(shell,DWT.DROP_DOWN);
		window.setMenu(windowMenu);

		final MenuItem maxItem=new MenuItem(windowMenu,DWT.PUSH);
		maxItem.setText("Maximize");

		final MenuItem minItem=new MenuItem(windowMenu,DWT.PUSH);
		minItem.setText("Minimize");

		//help menu:
		final MenuItem help=new MenuItem(menu,DWT.CASCADE);
		help.setText("&Help");

		final Menu helpMenu=new Menu(shell,DWT.DROP_DOWN);
		help.setMenu(helpMenu);

		final MenuItem aboutItem=new MenuItem(helpMenu,DWT.PUSH);
		aboutItem.setText("&About");

		//////////////////////////////////////////////////////////////////////////////////
		//					Event Listener Here (Selection Listener)					//
		//////////////////////////////////////////////////////////////////////////////////

	/+
		childItem.addSelectionListener(new class SelectionListener{
			public void widgetSelected(SelectionEvent e)
			{
				Shell myParent=cast(Shell)childItem.getParent.getParent;
				subForm=new SubForm(myParent,DWT.DIALOG_TRIM);
			}
			public void widgetDefaultSelected(SelectionEvent e){/*leave empty */}
			});
	+/
		childItem.addSelectionListener(new openChildSelectionListener);
		dialogItem.addSelectionListener(new openDialogSelectionListener);

		checkItem.addSelectionListener(new checkMenuClickListener(checkItem));


		radioItem1.addSelectionListener(new class SelectionListener{
			public void widgetSelected(SelectionEvent e)
			{
				if(radioItem1.getSelection)
				{
					MessageBox.showInfo("RadioItem1 is selected.","HaHA");

				}
			}
			public void widgetDefaultSelected(SelectionEvent e){}
		});
		radioItem2.addSelectionListener(new class SelectionListener{
			public void widgetSelected(SelectionEvent e)
			{
				if(radioItem2.getSelection)
				MessageBox.showInfo("RadioItem2 is selected.","HAHA");
			}
			public void widgetDefaultSelected(SelectionEvent e){}
		});

		minItem.addSelectionListener(new class SelectionListener{
			public void widgetSelected(SelectionEvent e)
			{
				//Shell parent=cast(Shell)minItem.getParent.getParent;
				shell.setMinimized(true);
			}
			public void widgetDefaultSelected(SelectionEvent e){}
		});

		maxItem.addSelectionListener(new class SelectionListener{
			public void widgetSelected(SelectionEvent e)
			{
				//Shell parent=cast(Shell)maxItem.getParent.getParent;
				shell.setMaximized(true);
			}
			public void widgetDefaultSelected(SelectionEvent e){}
		});

		return menu;
	}
	Menu createPopMenu()
	{
		final Menu popMenu=new Menu(shell,DWT.POP_UP);


		final MenuItem openChildItem=new MenuItem(popMenu,DWT.PUSH);
		openChildItem.setText("&Open child");

		final MenuItem openDialogItem=new MenuItem(popMenu,DWT.PUSH);
		openDialogItem.setText("Open &dialog");

		openChildItem.addSelectionListener(new  openChildSelectionListener);
		openDialogItem.addSelectionListener(new openDialogSelectionListener);

		return popMenu;
	}

	ToolBar createToolBar()
	{

		final ToolBar bar =new ToolBar(shell,DWT.HORIZONTAL);
		bar.setSize(300,70);
		bar.setLocation(0,0);

		final ToolItem openChildItemBar=new ToolItem(bar,DWT.PUSH);
		//final Image image=new Image(display,r"C:\dmd\html\d\d002.ico");
		//openChildItemBar.setImage(image );
		openChildItemBar.setText("Child Window");

		openChildItemBar.setToolTipText("This will open the child form");
		openChildItemBar.addSelectionListener(new openChildSelectionListener);

		final ToolItem separatorBar1=new ToolItem(bar,DWT.SEPARATOR);

		final ToolItem openToolItem=new ToolItem(bar,DWT.CHECK);
		//openToolItem.setImage(new Image(display,r"C:\Program Files\Tencent\QQ\AirDLIcon\1381game.ico"));
		openToolItem.setText("Check Bar");

		openToolItem.addSelectionListener(new checkMenuClickListener(openToolItem));

		final ToolItem separatorBar2=new ToolItem(bar,DWT.SEPARATOR);

		final ToolItem radioBarItem1=new ToolItem(bar,DWT.RADIO);
		//radioBarItem1.setImage(new Image(display,r"C:\Program Files\Tencent\QQ\AirDLIcon\1381love.ico"));
		radioBarItem1.setText("Radio1");

		final ToolItem radioBarItem2=new ToolItem(bar,DWT.RADIO);
		//radioBarItem2.setImage(new Image(display,r"C:\Program Files\Tencent\QQ\AirDLIcon\taotao.ico"));
		radioBarItem2.setText("Radio2");

		return bar;
	}

	class openChildSelectionListener:SelectionListener
	{
		public void widgetSelected(SelectionEvent e)
		{
			//code here
			SubForm subForm=new SubForm(shell,DWT.SHELL_TRIM);
		}
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//leave blank
		}
	}
	class openDialogSelectionListener:SelectionListener
	{
		public void widgetSelected(SelectionEvent e)
		{
			//code here
			Shell parent =cast(Shell)menu.getParent;
			//MessageBox.showInfo(parent==shell?"Yes":"No");
			MyDialog myDialog=new MyDialog(parent);
			myDialog.open;
		}
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//leave blank
		}
	}
	class checkMenuClickListener:SelectionListener
	{
		private:
		MenuItem item;
		ToolItem toolItem;
		public:
		this(MenuItem item)
		{

			this.item=item;

		}
		this(ToolItem toolItem)
		{
				this.toolItem=toolItem;
		}
		public void widgetSelected(SelectionEvent e)
		{
			if(item.getSelection||toolItem.getSelection)
			{
				MessageBox.showInfo("Checked","HAHA");
			}
			else

				MessageBox.showInfo("Unchecked","HAHA");



		}
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//leave blank
		}
	}
}

	int main(char[][] args)
	{
		scope Form form=new Form;

		return 0;
	}

/**************************************************/