'' translated from an "C" example written by Vijay Kumar B.
'
' Reviewed by TJF (2014)
' Details: http://developer.gnome.org/gtk/

#INCLUDE ONCE "Gir/Gtk-3.0.bi"
'#INCLUDE ONCE "Gir/Gtk-2.0.bi"
#INCLUDE ONCE "Gir/_GObjectMacros-2.0.bi"

'':::::
'' Callback for close button
SUB on_button_clicked CDECL (BYVAL button AS GtkWidget PTR, BYVAL buffer AS GtkTextBuffer PTR)
  DIM AS GtkTextIter start, end_

  '' Obtain iters for the start and end of points of the buffer
  gtk_text_buffer_get_start_iter( buffer, @start )
  gtk_text_buffer_get_end_iter( buffer, @end_ )

  '' Get the entire buffer text.
  VAR text = gtk_text_buffer_get_text( buffer, @start, @end_, FALSE )

  '' Print the text
  g_print( "%s", text )
  g_free( text )

  gtk_main_quit( )
END SUB

''::::: main

' Initialize the toolkit
gtk_init(@__FB_ARGC__, @__FB_ARGV__)

'' Create a Window.
VAR win = gtk_window_new( GTK_WINDOW_TOPLEVEL )
gtk_window_set_title( GTK_WINDOW( win ), "Simple Multiline Text Input" )

'' Set a decent default size for the window.
gtk_window_set_default_size( GTK_WINDOW( win ), 200, 200 )
g_signal_connect( G_OBJECT( win ), "destroy", _
                  G_CALLBACK( @gtk_main_quit ), _
                  NULL )

'' Create a vertical box for TextView and button
VAR vbox = gtk_vbox_new( FALSE, 2 )
gtk_container_add( GTK_CONTAINER( win ), vbox )

'' Create a scrollable area
VAR scroll = gtk_scrolled_window_new(NULL, NULL)
gtk_box_pack_start( GTK_BOX( vbox ), scroll, 1, 1, 0 )

'' Create a multiline text widget.
VAR text_view = gtk_text_view_new( )
gtk_container_add( GTK_CONTAINER( scroll ), text_view )

'' Obtaining the buffer associated with the widget.
VAR buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( text_view ) )
'' Set the default buffer text.
gtk_text_buffer_set_text( buffer, "Hello Text View!", -1 )

'' Create a close button.
VAR button = gtk_button_new_with_label( "Close & Output" )
gtk_box_pack_start( GTK_BOX( vbox ), button, 0, 0, 0)
g_signal_connect( G_OBJECT( button ), "clicked", _
                  G_CALLBACK( @on_button_clicked ), _
                  buffer )

gtk_widget_show_all( win )

gtk_main( )
