# .require. dotnet
# .compile-only.
"""
A very basic Windows Presentation Foundation (WPF) application to get you started.
"""

@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework'
@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore'
@ref 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase'

use System.Windows
use System.Windows.Controls
use System.Windows.Controls.Primitives
use System.Windows.Media
use System.Windows.Threading


class MainWindow inherits Window

	cue init
		base.init
		
		.title = 'WPF Test'

		panel = StackPanel()
		.content = panel
		
		for i in 10
			button = Button(content='Click me.', tag=i)
			listen button.click, ref .buttonClicked
			panel.children.add(button)

	def buttonClicked(sender, ev as RoutedEventArgs )
		sender.content = 'Hey! You have clicked me!  ([sender.tag])'


class Program

	def main is shared
		has STAThread
		Program().run
	
	def run
		app = Application()
		listen app.dispatcherUnhandledException, ref .dispatcherUnhandledException
		# other events:
		# .startup StartupEventArgs .args
		# .activated EventArgs
		# .deactivated EventArgs
		# .sessionEnding SessionEndingCancelEventArgs  .reasonSessionEnding  .cancel
		# .exit ExitEventArgs .applicationExitCode
		app.run(MainWindow())

	def dispatcherUnhandledException(sender, args as DispatcherUnhandledExceptionEventArgs)
		# you could log and/or email the exception
		# of course, you don't want this interception when using a debugger
		msg = 'Exception: ' + args.exception.message
		MessageBox.show(msg, 'Unhandled Exception', MessageBoxButton.OK)
		args.handled = true
