ATS/GTK Tutorial: | ||
---|---|---|
<<< Previous | Next >>> |
Let us write a program that creates a window containing a button. The button can be be clicked to issue the message "Hello, world". In addition, the window can be closed by a simple click.
The following code essentially outlines an implementation of such a program:
fun mymain ( ) : GtkWindow1 = let // val window = window_create () val button = button_create () // val () = window_add_button (window, button) // val () = button_handle_clicked (button) val () = window_handle_destroy (window) val () = window_handle_delete_event (window) // val () = gtk_widget_show (window) val () = gtk_widget_show_unref (button) // in window end // end of [mymain] |
// extern fun window_create (): GtkWindow1 extern fun button_create (): GtkButton1 extern fun window_add_button (!GtkWindow1, !GtkButton1): void extern fun button_handle_clicked (!GtkButton1): void extern fun window_handle_destroy (!GtkWindow1): void extern fun window_handle_delete_event (!GtkWindow1): void // |
The function window_create is implemented as follows:
implement window_create () = let // val widget = gtk_window_new (GTK_WINDOW_TOPLEVEL) // val ((*void*)) = assertloc (ptrcast(widget) > 0) // val ((*void*)) = gtk_window_set_title (widget, (gstring)"Hello, world") // in widget end // end of [window_create] |
The function button_create is implemented as follows:
implement button_create () = let // val widget = gtk_button_new_with_label ((gstring)"Hello, world") // val ((*void*)) = assertloc (ptrcast(widget) > 0) // in widget end // end of [button_create] |
The function window_add_button is implemented as follows:
implement window_add_button (window, button) = let // val () = gtk_container_set_border_width (window, (guint)10) // in // gtk_container_add (window, button) // end (* end of [window_add_button] *) |
The function button_handle_clicked is implemented as follows:
implement button_handle_clicked (button) = () where { // fun f (): void = println! ("Hello, world") // val id = g_signal_connect ( button, (gsignal)"clicked", G_CALLBACK(f), (gpointer)NULL ) (* end of [val] *) // } (* end of [button_handle_clicked] *) |
The function window_handle_destroy is implemented as follows:
implement window_handle_destroy (window) = () where { // val id = g_signal_connect ( window, (gsignal)"destroy", G_CALLBACK(gtk_main_quit), (gpointer)NULL ) (* end of [val] *) // } (* end of [window_handle_destroy] *) |
The function window_handle_delete_event is implemented as follows:
implement window_handle_delete_event (window) = () where { // fun f (x: GtkWindow1): gboolean = let val () = gtk_widget_destroy0 (x) in GTRUE end // val id = g_signal_connect ( window, (gsignal)"delete-event", G_CALLBACK(f), (gpointer)NULL ) (* end of [val] *) // } (* end of [window_handle_delete_event] *) |
The function gtk_widget_show_unref essentially calls gtk_widget_show and then decrease the reference count of its argument by 1. If the last line in the body of mymain changes to the following one:
then a type-error is to be reported during typechecking due to the linear value button being leaked. Getting type-errors as such is a great advantage that one can have when programming with ATS/GTK.The entirety of the presented code can be found in chap_hello.dats, and there is a Makefile available for compiling the file.
<<< Previous | Home | Next >>> |
Let us start! | Up | Box Packing |