' Notebook.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"

' This function rotates the position of the tabs
SUB rotate_book CDECL( BYVAL button AS GtkButton PTR, BYVAL notebook AS GtkNotebook PTR )

  VAR tab_pos = gtk_notebook_get_tab_pos(notebook)
  gtk_notebook_set_tab_pos (notebook, (tab_pos + 1) MOD 4)

END SUB

' Add/Remove the page tabs and the borders
SUB tabsborder_book CDECL( BYVAL button AS GtkButton PTR, BYVAL notebook AS GtkNotebook PTR )

  VAR tval = IIF( gtk_notebook_get_show_tabs(notebook), FALSE, TRUE)
  VAR bval = IIF( gtk_notebook_get_show_border(notebook), FALSE, TRUE)

  gtk_notebook_set_show_tabs (notebook, tval)
  gtk_notebook_set_show_border (notebook, bval)

END SUB

' Remove a page from the notebook
SUB remove_book CDECL( BYVAL button AS GtkButton PTR, BYVAL notebook AS GtkNotebook PTR )

  VAR page = gtk_notebook_get_current_page (notebook)
  gtk_notebook_remove_page (notebook, page)
  ' Need to refresh the widget --
  ' This forces the widget to redraw itself.
  gtk_widget_queue_draw (GTK_WIDGET (notebook))

END SUB


' ==============================================
' 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), "Notebook")
g_signal_connect (G_OBJECT (win), "delete-event", _
                  G_CALLBACK (@gtk_main_quit), NULL)

gtk_container_set_border_width (GTK_CONTAINER (win), 10)

VAR table = gtk_table_new (3, 6, FALSE)
gtk_container_add (GTK_CONTAINER (win), table)

' Create a new notebook, place the position of the tabs
VAR notebook = gtk_notebook_new ()
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP)
gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1)
gtk_widget_show (notebook)

' Let's append a bunch of pages to the notebook
FOR i AS guint = 0 TO 4
  VAR bufferf =  "Append Frame " + STR(i + 1)
  VAR bufferl =  "Page " + STR(i + 1)

  VAR frame = gtk_frame_new (STRPTR(bufferf))
  gtk_container_set_border_width (GTK_CONTAINER (frame), 10)
  gtk_widget_set_size_request (frame, 100, 75)
  gtk_widget_show (frame)

  VAR label = gtk_label_new (STRPTR(bufferf))
  gtk_container_add (GTK_CONTAINER (frame), label)
  gtk_widget_show (label)

  label = gtk_label_new (bufferl)
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label)
NEXT

' Now let's add a page to a specific spot
VAR checkbutton = gtk_check_button_new_with_label ("Check me please!")
gtk_widget_set_size_request (checkbutton, 100, 75)
gtk_widget_show (checkbutton)

VAR label = gtk_label_new ("Add page")
gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2)

' Now finally let's prepend pages to the notebook
FOR i AS guint = 0 TO 4
  VAR bufferf = "Prepend Frame " + STR( i + 1)
  VAR bufferl = "Page " + STR( i + 1)

  VAR frame = gtk_frame_new (STRPTR(bufferf))
  gtk_container_set_border_width (GTK_CONTAINER (frame), 10)
  gtk_widget_set_size_request (frame, 100, 75)
  gtk_widget_show (frame)

  VAR label = gtk_label_new (STRPTR(bufferf))
  gtk_container_add (GTK_CONTAINER (frame), label)
  gtk_widget_show (label)

  label = gtk_label_new (bufferl)
  gtk_notebook_prepend_page (GTK_NOTEBOOK (notebook), frame, label)
NEXT

' Set what page to start at (page 4)
gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 3)

' Create a bunch of buttons
VAR button = gtk_button_new_with_label ("close")
g_signal_connect_swapped (G_OBJECT (button), "clicked", _
                          G_CALLBACK (@gtk_main_quit), NULL)
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2)
gtk_widget_show (button)

button = gtk_button_new_with_label ("next page")
g_signal_connect_swapped (G_OBJECT (button), "clicked", _
                          G_CALLBACK (@gtk_notebook_next_page), G_OBJECT(notebook))
gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 1, 2)
gtk_widget_show (button)

button = gtk_button_new_with_label ("prev page")
g_signal_connect_swapped (G_OBJECT (button), "clicked", _
                          G_CALLBACK (@gtk_notebook_prev_page), G_OBJECT(notebook))
gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 1, 2)
gtk_widget_show (button)

button = gtk_button_new_with_label ("tab position")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@rotate_book),  notebook)
gtk_table_attach_defaults (GTK_TABLE (table), button, 3, 4, 1, 2)
gtk_widget_show (button)

button = gtk_button_new_with_label ("tabs/border on/off")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@tabsborder_book), notebook)
gtk_table_attach_defaults (GTK_TABLE (table), button, 4, 5, 1, 2)
gtk_widget_show (button)

button = gtk_button_new_with_label ("remove page")
g_signal_connect (G_OBJECT (button), "clicked", _
                  G_CALLBACK (@remove_book), notebook)
gtk_table_attach_defaults (GTK_TABLE (table), button, 5, 6, 1, 2)
gtk_widget_show (button)

gtk_widget_show (table)
gtk_widget_show (win)

gtk_main ()
