Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

WIP: generate types and methods using GObject introspection #27

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b6a2277
begin with instrospection: Add GIRepository, GIBaseInfo
bfredl Dec 30, 2013
d69cdc5
add GINamespace, access methdods
bfredl Dec 30, 2013
295315c
subclass on different GIInfo types
bfredl Dec 30, 2013
8527baa
Fix indexing + add some accessors
bfredl Dec 30, 2013
8dc4933
some fixes + simple testing
bfredl Dec 30, 2013
64b58dd
extract types of arguments (+ cleanup)
bfredl Dec 31, 2013
be28474
Call methods (with one argument :)
bfredl Dec 31, 2013
34a2e44
Merge branch 'master' of https://github.com/JuliaLang/Gtk.jl into girepo
bfredl Dec 31, 2013
5cf04bc
load correct shared libraries + Clutter test
bfredl Jan 1, 2014
1034c29
work on Namespace generation
bfredl Jan 1, 2014
077620f
fix Gtk test
bfredl Jan 1, 2014
6290375
String passing
bfredl Jan 1, 2014
789e5d4
gimport interface (proof of concept)
bfredl Jan 1, 2014
5791fdf
subtyping support
bfredl Jan 1, 2014
77aa401
julia 0.3 compat & simpler const generation ( JuliaLang/julia#5276 )
bfredl Jan 2, 2014
a095c2c
joining together GI.Gtk and Gtk (ugly, but mostly works)
bfredl Jan 2, 2014
7c55395
forgot const
bfredl Jan 2, 2014
f67406e
demonstration of using autogenerated wrappers
bfredl Jan 2, 2014
22421df
fix ambigous constructors and restore show(GObject)
bfredl Jan 2, 2014
c262f50
some gtype and debug cleanup
bfredl Jan 2, 2014
fd413b4
clean up quot
bfredl Jan 3, 2014
ef2dd55
GType generation w/o using introspection
bfredl Jan 3, 2014
9305818
factor out signal code for GObject module
bfredl Jan 3, 2014
cebe8e8
Merge branch 'master' of https://github.com/JuliaLang/Gtk.jl into girepo
bfredl Jan 3, 2014
7d7f7ab
stab at splitting out Glib.jl (still many missing exports)
bfredl Jan 3, 2014
4e271f1
revert GdkPixbuf change + remove trailing whitespace
bfredl Jan 4, 2014
4cd86b4
make @gimport work again
bfredl Jan 4, 2014
3e7ae33
split the modules
bfredl Jan 4, 2014
189003e
namespace functions
bfredl Jan 4, 2014
2a94ccc
work on constants
bfredl Jan 4, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deps/ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ if OS_NAME == :Windows
const libgobject = "libgobject-2.0-0"
const libglib = "libglib-2.0-0"
const libgdk_pixbuf = "libgdk_pixbuf-2.0-0"
const libgi = "libgirepository-1.0-0" # unverifie
else
const libgobject = "libgobject-2.0"
const libglib = "libglib-2.0"
const libgdk_pixbuf = "libgdk_pixbuf-2.0"
const libgi = "libgirepository-1.0"
end
60 changes: 49 additions & 11 deletions src/Gtk.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
# julia Gtk interface

module GLib
import Base: convert, show, showall, showcompact, run, size, length, getindex, setindex!,
insert!, push!, unshift!, shift!, pop!, splice!, delete!,
start, next, done, parent, isempty, empty!, first, last, in,
eltype, copy
import Base.Graphics: width, height, getgc
export GObject, GObjectI, @GType, make_gvalue
export Enum, GError, GValue, gvalue
export GSList, gslist,gslist2, gc_ref, gc_ref_closure
export signal_connect, signal_emit
export bytestring, GConnectFlags
include(joinpath("..","deps","ext.jl"))
bytestring(s) = Base.bytestring(s)
bytestring(s::Symbol) = s
bytestring(s::Ptr{Uint8},own::Bool) = UTF8String(pointer_to_array(s,int(ccall(:strlen,Csize_t,(Ptr{Uint8},),s)),own))

ccall((:g_type_init,libgobject),Void,())
include("MutableTypes.jl")
using MutableTypes
include("gslist.jl")
include("gobject.jl")
include("gvalues.jl")
include("gerror.jl")
include("signals.jl")
sizeof_gclosure = 0
function init()
global sizeof_gclosure = WORD_SIZE
closure = C_NULL
while closure == C_NULL
sizeof_gclosure += WORD_SIZE
closure = ccall((:g_closure_new_simple,libgobject),Ptr{Void},(Int,Ptr{Void}),sizeof_gclosure,C_NULL)
end
ccall((:g_closure_sink,libgobject),Void,(Ptr{Void},),closure)
end
init()
end

module Gtk
using Cairo
include("MutableTypes.jl")
using .MutableTypes
using GLib
using GLib.MutableTypes

import Base: convert, show, showall, run, size, length, getindex, setindex!,
import Base: convert, show, showall, showcompact, run, size, length, getindex, setindex!,
insert!, push!, unshift!, shift!, pop!, splice!, delete!,
start, next, done, parent, isempty, empty!, first, last, in,
eltype, copy
import Base.Graphics: width, height, getgc
import Cairo: destroy

# gimport interface (not final in any way)
export @gimport

# generic interface:
export width, height, #minsize, maxsize
reveal, configure, draw, cairo_context,
Expand Down Expand Up @@ -71,18 +111,15 @@ export GdkKeySyms, GdkScrollDirection, GtkJustification
# set_position

# local function, handles Symbol and makes UTF8-strings easier
bytestring(s) = Base.bytestring(s)
bytestring(s::Symbol) = s
bytestring(s::Ptr{Uint8},own::Bool) = UTF8String(pointer_to_array(s,int(ccall(:strlen,Csize_t,(Ptr{Uint8},),s)),own))

typealias Index Union(Integer,AbstractVector{TypeVar(:I,Integer)})

include(joinpath("..","deps","ext.jl"))
ccall((:g_type_init,libgobject),Void,())
include("gslist.jl")

#module GI
#include("girepo.jl")
#include("giimport.jl")
#end
include("gtktypes.jl")
include("gvalues.jl")
include("gerror.jl")
include("gdk.jl")
include("events.jl")
include("container.jl")
Expand Down Expand Up @@ -124,6 +161,7 @@ for container in subtypes(GtkContainerI,true)
@eval $(symbol(string(container)))(child::GtkWidgetI,vargs...) = push!($container(vargs...),child)
end
for orientable in tuple(:GtkPaned, :GtkScale, [sym.name.name for sym in subtypes(GtkBoxI)]...)
if endswith(string(orientable),"I") continue end # Sorry :(
@eval $orientable(orientation::Symbol,vargs...) = $orientable(
(orientation==:v ? true :
(orientation==:h ? false :
Expand Down
14 changes: 7 additions & 7 deletions src/buttons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
#GtkSwitch — A "light switch" style toggle
#GtkLockButton — A widget to unlock or lock privileged operations

@GType GtkButton <: GtkBin
@GType GtkButton
GtkButton() = GtkButton(ccall((:gtk_button_new,libgtk),Ptr{GObject},()))
GtkButton(title::String) =
GtkButton(ccall((:gtk_button_new_with_mnemonic,libgtk),Ptr{GObject},
(Ptr{Uint8},), bytestring(title)))

@GType GtkCheckButton <: GtkBin
@GType GtkCheckButton
GtkCheckButton() = GtkCheckButton(ccall((:gtk_check_button_new,libgtk),Ptr{GObject},()))
GtkCheckButton(title::String) =
GtkCheckButton(ccall((:gtk_check_button_new_with_mnemonic,libgtk),Ptr{GObject},
(Ptr{Uint8},), bytestring(title)))

@GType GtkToggleButton <: GtkBin
@GType GtkToggleButton
GtkToggleButton() = GtkToggleButton(ccall((:gtk_toggle_button_new,libgtk),Ptr{GObject},()))
GtkToggleButton(title::String) =
GtkToggleButton(ccall((:gtk_toggle_button_new_with_mnemonic,libgtk),Ptr{GObject},
(Ptr{Uint8},), bytestring(title)))

if gtk_version >= 3
@GType GtkSwitch <: GtkWidget
@GType GtkSwitch
GtkSwitch() = GtkSwitch(ccall((:gtk_switch_new,libgtk),Ptr{GObject},()))
function GtkSwitch(active::Bool)
b = GtkSwitch()
Expand All @@ -43,7 +43,7 @@ else
const GtkSwitch = GtkToggleButton
end

@GType GtkRadioButton <: GtkBin
@GType GtkRadioButton
GtkRadioButton(group::Ptr{Void}=C_NULL) =
GtkRadioButton(ccall((:gtk_radio_button_new,libgtk),Ptr{GObject},
(Ptr{Void},),group))
Expand Down Expand Up @@ -157,7 +157,7 @@ for btn in (:GtkCheckButton, :GtkToggleButton, :GtkRadioButton)
end


@GType GtkLinkButton <: GtkBin
@GType GtkLinkButton
GtkLinkButton(uri::String) =
GtkLinkButton(ccall((:gtk_link_button_new,libgtk),Ptr{GObject},
(Ptr{Uint8},),bytestring(uri)))
Expand All @@ -177,7 +177,7 @@ end

#TODO: @GType GtkScaleButton

@GType GtkVolumeButton <: GtkBin
@GType GtkVolumeButton
GtkVolumeButton() = GtkVolumeButton(ccall((:gtk_volume_button_new,libgtk),Ptr{GObject},()))
function GtkVolumeButton(value::Real) # 0<=value<=1
b = GtkVolumeButton()
Expand Down
62 changes: 34 additions & 28 deletions src/displays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ convert{P<:Ptr}(::Type{P}, a::MatrixStrided) = convert(P, a.p)
bstride(a::MatrixStrided,i) = (i == 1 ? sizeof(eltype(a)) : (i == 2 ? a.rowstride : 0))
bstride(a,i) = stride(a,i)*sizeof(eltype(a))

@GType GdkPixbuf
#not a gobject, when whe get support for glib structs this can be automated too
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for this states that it is a GObject.

type GdkPixbuf
handle::Ptr{GdkPixbuf}
#FIXME: refcounting
GdkPixbuf(handle::Ptr{GdkPixbuf }) = (handle != C_NULL ? new(handle) : error("Cannot construct $gname with a NULL pointer"))
end
convert(::Type{Ptr{GdkPixbuf}},w::GdkPixbuf) = w.handle

# Example constructors:
#GdkPixbuf(filename="", width=-1, height=-1, preserve_aspect_ratio=true)
Expand All @@ -161,84 +167,84 @@ function GdkPixbuf(;stream=nothing,resource_path=nothing,filename=nothing,xpm_da
"GdkPixbuf must have at most one stream, resource_path, filename, xpm_data, inline_data, or data argument")
@assert(source_count==0 || data!==nothing || has_alpha===nothing,
"GdkPixbuf can only set the has-alpha property for new buffers")
local pixbuf::Ptr{GObject}
local pixbuf::Ptr{GdkPixbuf}
if stream !== nothing
@assert(false, "not implemented yet")
elseif resource_path !== nothing
GError() do error_check
if width == -1 && height == -1
pixbuf = ccall((:gdk_pixbuf_new_from_resource,libgdk_pixbuf),Ptr{GObject},(Ptr{Uint8},Ptr{Ptr{GError}}),bytestring(resource_path),error_check)
pixbuf = ccall((:gdk_pixbuf_new_from_resource,libgdk_pixbuf),Ptr{GdkPixbuf},(Ptr{Uint8},Ptr{Ptr{GError}}),bytestring(resource_path),error_check)
else
pixbuf = ccall((:gdk_pixbuf_new_from_resource_at_scale,libgdk_pixbuf),Ptr{GObject},
pixbuf = ccall((:gdk_pixbuf_new_from_resource_at_scale,libgdk_pixbuf),Ptr{GdkPixbuf},
(Ptr{Uint8},Cint,Cint,Cint,Ptr{Ptr{GError}}),bytestring(resource_path),width,height,preserve_aspect_ratio,error_check)
end
return pixbuf !== C_NULL
end
elseif filename !== nothing
GError() do error_check
if width == -1 && height == -1
pixbuf = ccall((:gdk_pixbuf_new_from_file,libgdk_pixbuf),Ptr{GObject},(Ptr{Uint8},Ptr{Ptr{GError}}),bytestring(filename),error_check)
pixbuf = ccall((:gdk_pixbuf_new_from_file,libgdk_pixbuf),Ptr{GdkPixbuf},(Ptr{Uint8},Ptr{Ptr{GError}}),bytestring(filename),error_check)
else
pixbuf = ccall((:gdk_pixbuf_new_from_file_at_scale,libgdk_pixbuf),Ptr{GObject},
pixbuf = ccall((:gdk_pixbuf_new_from_file_at_scale,libgdk_pixbuf),Ptr{GdkPixbuf},
(Ptr{Uint8},Cint,Cint,Cint,Ptr{Ptr{GError}}),bytestring(filename),width,height,preserve_aspect_ratio,error_check)
end
return pixbuf !== C_NULL
end
elseif xpm_data !== nothing
@assert(width==-1 && height==-1,"GdkPixbuf cannot set the width/height of a image from xpm_data")
GError() do error_check
pixbuf = ccall((:gdk_pixbuf_new_from_xpm_data,libgdk_pixbuf),Ptr{GObject},(Ptr{Ptr{Uint8}},),xpm_data)
pixbuf = ccall((:gdk_pixbuf_new_from_xpm_data,libgdk_pixbuf),Ptr{GdkPixbuf},(Ptr{Ptr{Uint8}},),xpm_data)
return pixbuf !== C_NULL
end
elseif inline_data !== nothing
@assert(width==-1 && height==-1,"GdkPixbuf cannot set the width/height of a image from inline_data")
GError() do error_check
pixbuf = ccall((:gdk_pixbuf_new_from_inline,libgdk_pixbuf),Ptr{GObject},(Cint,Ptr{Uint8},Cint,Ptr{Ptr{GError}}),sizeof(inline_data),inline_data,true,error_check)
pixbuf = ccall((:gdk_pixbuf_new_from_inline,libgdk_pixbuf),Ptr{GdkPixbuf},(Cint,Ptr{Uint8},Cint,Ptr{Ptr{GError}}),sizeof(inline_data),inline_data,true,error_check)
return pixbuf !== C_NULL
end
elseif data !== nothing # RGB or RGBA array, packed however you wish
@assert(width==-1 && height==-1,"GdkPixbuf cannot set the width/height of a image from data")
alpha = convert(Bool,has_alpha)
width = size(data,1)*bstride(data,1)/(3+int(alpha))
height = size(data,2)
pixbuf = ccall((:gdk_pixbuf_new_from_data,libgdk_pixbuf),Ptr{GObject},
pixbuf = ccall((:gdk_pixbuf_new_from_data,libgdk_pixbuf),Ptr{GdkPixbuf},
(Ptr{Uint8},Cint,Cint,Cint,Cint,Cint,Cint,Ptr{Void},Any),
data,0,alpha,8,width,height,bstride(data,2),
gc_ref_closure(data),data)
else
@assert(width!=-1 && height!=-1,"GdkPixbuf requires a width, height, and has_alpha to create an uninitialized pixbuf")
alpha = convert(Bool,has_alpha)
pixbuf = ccall((:gdk_pixbuf_new,libgdk_pixbuf),Ptr{GObject},
pixbuf = ccall((:gdk_pixbuf_new,libgdk_pixbuf),Ptr{GdkPixbuf},
(Cint,Cint,Cint,Cint,Cint),0,alpha,8,width,height)
end
return GdkPixbuf(pixbuf)
end
#GdkPixbufLoader for new with type/mimetype
#GdkPixbuf(callback, stream, width=-1, height=-1, preserve_aspect_ratio=true)

copy(img::GdkPixbuf) = GdkPixbuf(ccall((:gdk_pixbuf_copy,libgdk_pixbuf),Ptr{GObject},(Ptr{GObject},),img))
slice(img::GdkPixbuf,x,y) = GdkPixbuf(ccall((:gdk_pixbuf_new_subpixbuf,libgdk_pixbuf),Ptr{GObject},
(Ptr{GObject},Cint,Cint,Cint,Cint),img,first(x)-1,first(y)-1,length(x),length(y)))
width(img::GdkPixbuf) = ccall((:gdk_pixbuf_get_width,libgdk_pixbuf),Cint,(Ptr{GObject},),img)
height(img::GdkPixbuf) = ccall((:gdk_pixbuf_get_height,libgdk_pixbuf),Cint,(Ptr{GObject},),img)
copy(img::GdkPixbuf) = GdkPixbuf(ccall((:gdk_pixbuf_copy,libgdk_pixbuf),Ptr{GdkPixbuf},(Ptr{GdkPixbuf},),img))
slice(img::GdkPixbuf,x,y) = GdkPixbuf(ccall((:gdk_pixbuf_new_subpixbuf,libgdk_pixbuf),Ptr{GdkPixbuf},
(Ptr{GdkPixbuf},Cint,Cint,Cint,Cint),img,first(x)-1,first(y)-1,length(x),length(y)))
width(img::GdkPixbuf) = ccall((:gdk_pixbuf_get_width,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img)
height(img::GdkPixbuf) = ccall((:gdk_pixbuf_get_height,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img)
size(a::GdkPixbuf,i::Integer) = (i == 1 ? width(a) : (i == 2 ? height(a) : 1))
size(a::GdkPixbuf) = (width(a),height(a))
Base.ndims(::GdkPixbuf) = 2
function bstride(img::GdkPixbuf,i)
if i == 1
convert(Cint, div(ccall((:gdk_pixbuf_get_bits_per_sample,libgdk_pixbuf),Cint,(Ptr{GObject},),img) *
ccall((:gdk_pixbuf_get_n_channels,libgdk_pixbuf),Cint,(Ptr{GObject},),img) + 7, 8))
convert(Cint, div(ccall((:gdk_pixbuf_get_bits_per_sample,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img) *
ccall((:gdk_pixbuf_get_n_channels,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img) + 7, 8))
elseif i == 2
ccall((:gdk_pixbuf_get_rowstride,libgdk_pixbuf),Cint,(Ptr{GObject},),img)
ccall((:gdk_pixbuf_get_rowstride,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img)
else
convert(Cint,0)
end
end
size(img::GdkPixbuf) = (width(img),height(img))
function eltype(img::GdkPixbuf)
#nbytes = stride(img,1)
nbytes = convert(Cint, div(ccall((:gdk_pixbuf_get_bits_per_sample,libgdk_pixbuf),Cint,(Ptr{GObject},),img) *
ccall((:gdk_pixbuf_get_n_channels,libgdk_pixbuf),Cint,(Ptr{GObject},),img) + 7, 8))
nbytes = convert(Cint, div(ccall((:gdk_pixbuf_get_bits_per_sample,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img) *
ccall((:gdk_pixbuf_get_n_channels,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img) + 7, 8))
if nbytes == 3
RGB
elseif nbytes == 4
Expand All @@ -249,9 +255,9 @@ function eltype(img::GdkPixbuf)
end
function convert(::Type{MatrixStrided},img::GdkPixbuf)
MatrixStrided(
convert(Ptr{eltype(img)},ccall((:gdk_pixbuf_get_pixels,libgdk_pixbuf),Ptr{Void},(Ptr{GObject},),img)),
convert(Ptr{eltype(img)},ccall((:gdk_pixbuf_get_pixels,libgdk_pixbuf),Ptr{Void},(Ptr{GdkPixbuf},),img)),
width=width(img), height=height(img),
rowstride=ccall((:gdk_pixbuf_get_rowstride,libgdk_pixbuf),Cint,(Ptr{GObject},),img))
rowstride=ccall((:gdk_pixbuf_get_rowstride,libgdk_pixbuf),Cint,(Ptr{GdkPixbuf},),img))
end
getindex(img::GdkPixbuf,x::Index,y::Index) = convert(MatrixStrided,img)[x,y]
setindex!(img::GdkPixbuf,pix,x::Index,y::Index) = setindex!(convert(MatrixStrided,img),pix,x,y)
Expand Down Expand Up @@ -287,8 +293,8 @@ baremodule GtkIconSize
end
end

@GType GtkImage <: GtkWidget
GtkImage(pixbuf::GdkPixbuf) = GtkImage(ccall((:gtk_image_new_from_pixbuf,libgtk),Ptr{GObject},(Ptr{GObject},),pixbuf))
@GType GtkImage
GtkImage(pixbuf::GdkPixbuf) = GtkImage(ccall((:gtk_image_new_from_pixbuf,libgtk),Ptr{GObject},(Ptr{GdkPixbuf},),pixbuf))
GtkImage(filename::String) = GtkImage(ccall((:gtk_image_new_from_file,libgtk),Ptr{GObject},(Ptr{Uint8},),bytestring(filename)))

function GtkImage(;resource_path=nothing,filename=nothing,icon_name=nothing,size::Symbol=:invalid)
Expand All @@ -310,14 +316,14 @@ end
empty!(img::GtkImage) = ccall((:gtk_image_clear,libgtk),Void,(Ptr{GObject},),img)
GdkPixbuf(img::GtkImage) = GdkPixbuf(ccall((:gtk_image_get_pixbuf,libgtk),Ptr{GObject},(Ptr{GObject},),img))

@GType GtkProgressBar <: GtkWidget
@GType GtkProgressBar
GtkProgressBar() = GtkProgressBar(ccall((:gtk_progress_bar_new,libgtk),Ptr{GObject},()))
pulse(progress::GtkProgressBar) = ccall((:gtk_progress_bar_pulse,libgtk),Void,(Ptr{GObject},),progress)

@GType GtkSpinner <: GtkWidget
@GType GtkSpinner
GtkSpinner() = GtkSpinner(ccall((:gtk_spinner_new,libgtk),Ptr{GObject},()))

@GType GtkStatusbar <: GtkBox
@GType GtkStatusbar
GtkStatusbar() = GtkStatusbar(ccall((:gtk_statusbar_new,libgtk),Ptr{GObject},()))
context_id(status::GtkStatusbar,source) =
ccall((:gtk_statusbar_get_context_id,libgtk),Cuint,(Ptr{GObject},Ptr{Uint8}),
Expand All @@ -340,5 +346,5 @@ empty!(status::GtkStatusbar,context) =
#GtkInfoBar() = GtkInfoBar(ccall((:gtk_info_bar_new,libgtk),Ptr{GObject},())

@GType GtkStatusIcon
GtkStatusIcon() = GtkStatusIcon(ccall((:gtk_status_icon_new,libgtk),Ptr{GObject},()))


Loading