#!/usr/bin/env exu
---------------------------------------------
-- buttonstock.exu Aug.4, 2008
-- Demonstrates:
--  scrolled_window
--  tables
--  GTK stock
--    (Hover over buttons with mouse pointer 
--    to see stockname of button
----------------------------------------------
include GtkEngine.e
include GtkEnums.e
include GtkRoutines.e
include dll.e -- for call_back

-------------------------------------------------------------
-- following would be very seldom used, so it's a hard-coded
-- call to Eu's define_c_func, not part of EuGTK
-------------------------------------------------------------
constant gtk_list_ids = define_c_func(GTK,"gtk_stock_list_ids",{},P)

------------------------------------------
function build_table(integer r, integer c)
------------------------------------------
object tbl, btn
integer ct

-- retrieve stock ids from GTK
sequence ids = dealloc(c_func(gtk_list_ids,{}))
  
-- create a table with r rows and c columns, homogeneous sizing
tbl = create(GtkTable,r,c,TRUE)
ct = 0
for col = 0 to c do
  for row = 0 to r do
    ct += 1
    if ct > length(ids) then -- no more stock items
      exit
    else
    -- create a new button from this stock id
      btn = create(GtkButton,ids[ct])
  
    -- attach an appropriate tooltip to the button
      set(btn,"tooltip text",ids[ct])
  
    -- attach new button to the table
    -- col,col+1 means button starts in col, extends to col+1
    -- so items can take up more than one row or one columns
    -- if needed;
       set(tbl,"attach defaults",btn,col,col+1,row,row+1)
  
     end if
  end for
end for

return tbl
end function


 -- create an adustment object to handle h scrollbar
constant hadj = create(GtkAdjustment)
  
 -- create an adjustment object to handle v scrollbar
constant vadj = create(GtkAdjustment)
  
 -- create a main window
constant win = create(GtkWindow)
  set(win,"default_size",900,628)
  set(win,"title","GTK Stock Buttons")
  set(win,"position",GTK_WIN_POS_CENTER)

 -- create a scrolled window (not really a window, just a scrolling container)
constant scroller = create(GtkScrolledWindow,hadj,vadj)
  
 -- create a table to hold the buttons, with 10 rows and 10 columns, homogeneous
atom tbl = create(GtkTable,10,10,TRUE)

-- kill gtk main loop when main window gets closed  
  connect(win,"destroy",quit)

-- add scrolling container to main window
  add(win,scroller)

-- fill the table full of buttons
   tbl = build_table(20,4)

-- put the table into the scrolling container
   set(scroller,"add_with_viewport",tbl)
  
-- show everything  
   show_all(win)

-- enter main GTK event handling loop
   main()

---------------------------------------------------------------
-- copyright 2008 by Irv Mullins, code released under the LGPL
---------------------------------------------------------------