fb:porticula NoPaste
gfx2gtk.bi
Uploader: | OneCypher |
Datum/Zeit: | 23.03.2010 20:32:29 |
#include once "gtk/gtk.bi"
#ifndef NULL
#define NULL 0
#endif
namespace mycrt
#include "crt.bi"
end namespace
dim shared gtkthread as any ptr
type gfxwin
public:
win as GtkWidget ptr
drawarea as GtkWidget ptr
width as integer
height as integer
fbbuffer as any ptr
gbuffer as ubyte ptr
inkey as string
mouse_x as integer
mouse_y as integer
mouse_b as integer
declare sub refresh()
declare sub close()
declare constructor(w as integer, h as integer)
declare property title as string
declare property title (v as string)
private:
p_title as string
end type
property gfxwin.title as string
return p_title
end property
property gfxwin.title (v as string)
p_title = v
gtk_window_set_title( GTK_WINDOW(win), p_title )
end property
function on_darea_keypress cdecl( byval widget as GtkWidget ptr, byval event as GdkEventKey ptr, byval userdata as gpointer) as gboolean
with *cast(gfxwin ptr, userdata)
.inkey = *event->string
end with
return true
end function
function on_darea_keyrelease cdecl( byval widget as GtkWidget ptr, byval event as GdkEventKey ptr, byval userdata as gpointer) as gboolean
with *cast(gfxwin ptr, userdata)
.inkey = ""
end with
return true
end function
function on_darea_clicked cdecl( byval widget as GtkWidget ptr, byval event as GdkEventButton ptr, byval userdata as gpointer) as gboolean
with *cast(gfxwin ptr, userdata)
.mouse_b = event->button
end with
return true
end function
function on_darea_released cdecl( byval widget as GtkWidget ptr, byval event as GdkEventButton ptr, byval userdata as gpointer) as gboolean
with *cast(gfxwin ptr, userdata)
.mouse_b = 0
end with
return true
end function
function on_darea_move cdecl( byval widget as GtkWidget ptr, byval event as GdkEventMotion ptr, byval userdata as gpointer) as gboolean
with *cast(gfxwin ptr, userdata)
.mouse_x = event->x
.mouse_y = event->y
end with
return true
end function
function on_darea_expose cdecl( byval widget as GtkWidget ptr, byval event as GdkEventExpose ptr, byval userdata as gpointer) as gboolean
Dim pixdata As ubyte Ptr, pitch As Integer
dim MyGFX2GTK as gfxwin ptr = cast(any ptr, userdata)
with *MyGFX2GTK
imageinfo .fbbuffer, ,,, pitch, pixdata
dim p as ubyte ptr
For y As Integer = 0 to .height -1
p = pixdata + y * pitch
mycrt.memcpy @.gbuffer[y*.width*4] , p, (.width *4)
Next
gdk_draw_rgb_32_image( widget->window, widget->style->fg_gc(GTK_STATE_NORMAL), 0, 0, .WIDTH, .HEIGHT, GDK_RGB_DITHER_MAX, .gbuffer, .WIDTH * 4 )
end with
return TRUE
end function
sub gfxwin.refresh()
on_darea_expose ( drawarea, 0, cast(any ptr, @this))
end sub
sub startgtk()
gtk_main()
end sub
constructor gfxwin(w as integer, h as integer)
width = w
height =h
fbbuffer = imagecreate(w,h,RGB(0,0,0))
gbuffer = allocate(4*w*h)
gtk_init( NULL, NULL )
win = gtk_window_new( GTK_WINDOW_TOPLEVEL )
gtk_signal_connect( GTK_OBJECT(win), "delete_event", GTK_SIGNAL_FUNC(@gtk_main_quit), NULL )
drawarea = gtk_drawing_area_new()
gtk_widget_set_size_request( drawarea, w, h )
gtk_container_add( GTK_CONTAINER(win), drawarea )
gtk_widget_add_events(drawarea, GDK_POINTER_MOTION_MASK)
gtk_widget_add_events(drawarea, GDK_BUTTON_PRESS_MASK)
gtk_widget_add_events(drawarea, GDK_BUTTON_RELEASE_MASK)
gtk_widget_add_events(drawarea, GDK_KEY_PRESS_MASK)
gtk_widget_add_events(drawarea, GDK_KEY_RELEASE_MASK)
gtk_widget_set_flags(drawarea, GTK_CAN_FOCUS)
gtk_signal_connect( GTK_OBJECT(drawarea), "expose-event", GTK_SIGNAL_FUNC(@on_darea_expose), cast(any ptr, @This) )
gtk_signal_connect( GTK_OBJECT(drawarea), "motion_notify_event", GTK_SIGNAL_FUNC(@on_darea_move), cast(any ptr, @This) )
gtk_signal_connect( GTK_OBJECT(drawarea), "button_press_event", GTK_SIGNAL_FUNC(@on_darea_clicked), cast(any ptr, @This) )
gtk_signal_connect( GTK_OBJECT(drawarea), "button_release_event", GTK_SIGNAL_FUNC(@on_darea_released), cast(any ptr, @This) )
gtk_signal_connect( GTK_OBJECT(drawarea), "key_press_event", GTK_SIGNAL_FUNC(@on_darea_keypress), cast(any ptr, @This) )
gtk_signal_connect( GTK_OBJECT(drawarea), "key_release_event", GTK_SIGNAL_FUNC(@on_darea_keyrelease), cast(any ptr, @This) )
gtk_widget_show_all( win )
if gtkthread = 0 then gtkthread = threadcreate(cast(any ptr, @startgtk))
end constructor