' FB example to draw an image in a windows
'
' Reviewed by TJF (2014), gdk_draw_rgb_image is deprecated -> use cairo instead
' Details: http://developer.gnome.org/gtk/

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

#DEFINE IMAGE_WIDTH  256
#DEFINE IMAGE_HEIGHT 256

DIM SHARED rgbbuf(IMAGE_WIDTH * IMAGE_HEIGHT * 3) AS BYTE

'':::::
FUNCTION on_darea_expose CDECL( _
  BYVAL widget AS GtkWidget PTR, _
  BYVAL event AS GdkEventExpose PTR, _
  BYVAL userdata AS gpointer) AS gboolean

  '' draw the 24-bit RGB image (conversions are done automatically)
  gdk_draw_rgb_image( _
    GDK_DRAWABLE(widget->window), _
    widget->style->fg_gc(GTK_STATE_NORMAL), _
    0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, _
    GDK_RGB_DITHER_MAX, @rgbbuf(0), IMAGE_WIDTH * 3 )

  '' return OK
  RETURN TRUE
END FUNCTION

'' main

'' initialize Gtk
gtk_init(@__FB_ARGC__, @__FB_ARGV__)

'' create a window
VAR win = gtk_window_new( GTK_WINDOW_TOPLEVEL )
gtk_window_set_title( GTK_WINDOW(win), "RGB Test" )
g_signal_connect( G_OBJECT(win), "delete_event", _
                  G_CALLBACK(@gtk_main_quit), NULL )

'' create a drawable area on it
VAR drawarea = gtk_drawing_area_new( )
gtk_widget_set_size_request( drawarea, IMAGE_WIDTH, IMAGE_HEIGHT )
gtk_container_add( GTK_CONTAINER(win), drawarea )

'' set the callback for when the window is redrawn
g_signal_connect( G_OBJECT(drawarea), "expose-event", _
                  G_CALLBACK(@on_darea_expose), NULL )
gtk_widget_show_all( win )

'' create an 24-bit RGB image
VAR i = 0
FOR y AS LONG = 0 TO IMAGE_HEIGHT - 1
  FOR x AS LONG = 0 TO IMAGE_WIDTH - 1
    rgbbuf(i)   = x - (x AND 31)                    '' R
    rgbbuf(i+1) = (x \ 32) * 4 + y - (y AND 31)     '' G
    rgbbuf(i+2) = y - (y AND 31)                    '' B
    i += 3
  NEXT
NEXT

'' run..
gtk_main()

END
