' Paned.bas
' Translated from C to FB by TinyCla, 2006
'
' Reviewed by TJF (2011-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"

' Create the list of "messages"
FUNCTION create_list CDECL( ) AS GtkWidget PTR

  DIM AS GtkTreeIter iter

  ' Create a new scrolled window, with scrollbars only if needed
  VAR scrolled_window = gtk_scrolled_window_new (NULL, NULL)
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC)

  VAR model = CAST(GtkWidget PTR, gtk_list_store_new (1, G_TYPE_STRING))
  VAR tree_view = gtk_tree_view_new ()
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),  tree_view)
  gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model))
  gtk_widget_show (tree_view)

  ' Add some messages to the window
  FOR i AS guint = 0 TO 9
    VAR msg = "Message #" + STR(i + 1)
    gtk_list_store_append (GTK_LIST_STORE (model), @iter)
    gtk_list_store_set (GTK_LIST_STORE (model),  @iter, 0, STRPTR(msg), -1)
  NEXT i

  VAR cell = gtk_cell_renderer_text_new ()
  VAR column = gtk_tree_view_column_new_with_attributes ("Messages", cell, "text", 0, NULL)

  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), GTK_TREE_VIEW_COLUMN (column))

  RETURN scrolled_window

END FUNCTION

' Add some text to our text widget - this is a callback that is invoked
' when our window is realized. We could also force our window to be
' realized with gtk_widget_realize, but it would have to be part of
' a hierarchy first
SUB insert_text CDECL(BYVAL buffer AS GtkTextBuffer PTR)

  DIM AS GtkTextIter iter
  DIM AS gchar PTR buf_text = @(_
    !"From: pathfinder@nasa.gov\n" _
    !"To: mom@nasa.gov\n" _
    !"Subject: Made it!\n" _
    !"\n" _
    !"We just got in this morning. The weather has been\n" _
    !"great - clear but cold, and there are lots of fun sights.\n" _
    !"Sojourner says hi. See you soon.\n" _
    !" -Path\n" _
    )

  gtk_text_buffer_get_iter_at_offset (buffer, @iter, 0)
  gtk_text_buffer_insert (buffer, @iter,  buf_text, -1)

END SUB

' Create a scrolled text area that displays a "message"
FUNCTION create_text CDECL( ) AS GtkWidget PTR

  VAR view_text = gtk_text_view_new ()
  VAR buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view_text))

  VAR scrolled_window = gtk_scrolled_window_new (NULL, NULL)
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC)

  gtk_container_add (GTK_CONTAINER (scrolled_window), view_text)
  insert_text (buffer)

  gtk_widget_show_all (scrolled_window)

  RETURN scrolled_window

END FUNCTION


' ==============================================
' Main
' ==============================================

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

VAR win = gtk_window_new (GTK_WINDOW_TOPLEVEL)
gtk_window_set_title (GTK_WINDOW (win), "Paned Windows")
g_signal_connect (G_OBJECT (win), "delete-event", _
                  G_CALLBACK (@gtk_main_quit), NULL)
gtk_container_set_border_width (GTK_CONTAINER (win), 10)
gtk_widget_set_size_request (GTK_WIDGET (win), 450, 400)

' create a vpaned widget and add it to our toplevel window
VAR vpaned = gtk_vpaned_new ()
gtk_container_add (GTK_CONTAINER (win), vpaned)
gtk_widget_show (vpaned)

' Now create the contents of the two halves of the window
VAR list = create_list ()
gtk_paned_add1 (GTK_PANED (vpaned), list)
gtk_widget_show (list)

VAR text = create_text ()
gtk_paned_add2 (GTK_PANED (vpaned), text)
gtk_widget_show (text)
gtk_widget_show (win)

gtk_main ()
