view subtly/gtk.rb @ 0:87b46f01d08d

Initial commit
author unexist
date Sat, 12 Dec 2009 22:33:59 +0100
parents
children 37ddd775df8a
line wrap: on
line source
##
# @package subtly
#
# @file Gtk bindings
# @author Christoph Kappel <unexist@dorfelite.net>
# @version $Id: subtly/gtk.rb,v 0 1260653639.0-3600 unexist $
#
# This program can be distributed under the terms of the GNU GPL.
##

require "ffi"
require "subtly/glib"

module Subtly
  module Gtk
    class Runner # {{{
      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")    

      def initialize
        Subtly::Glib.thread_init(nil)
        Subtly::Gtk::Runner.ffi_init(nil, nil)
      end

      def run
        Subtly::Gtk::Runner.ffi_run
      end

      ## init
      # Init Gtk system
      #
      # @param  [Pointer, #read]  Parent widget
      ##

      attach_function(:ffi_init,                   
        :gtk_init, [ :pointer, :pointer ], :void
      )

      ## run
      # Run Gtk main loop
      ##

      attach_function(:ffi_run,
        :gtk_main, [ ], :void
      )

      ## quit
      # Quit Gtk main loop
      ##

      attach_function(:ffi_quit,
        :gtk_main_quit, [ ], :void
      )
    end # }}}

    class Widget # {{{
      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")

      # States
      enum(:state,
      [
        :normal, 1,
        :active,
        :prelight,
        :selected,
        :insensitive
      ])
      
      ## set_fg
      # Set foreground color of widget
      #
      # @param  [Pointer, #read]  Widget to set color
      # @param  [State,   #read]  Widget state
      # @param  [Pointer, #read]  Color to set
      ##

      attach_function(:set_fg,
        :gtk_widget_modify_fg, [ :pointer, :state, :pointer ], :void
      )

      ## set_fg
      # Set background color of widget
      #
      # @param  [Pointer, #read]  Widget to set color
      # @param  [State,   #read]  Widget state
      # @param  [Pointer, #read]  Color to set
      ##

      attach_function(:set_bg,
        :gtk_widget_modify_bg, [ :pointer, :state, :pointer ], :void
      )
    end # }}}

    class Window # {{{
      attr_accessor :widget

      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")

      def initialize
        @widget = Subtly::Gtk::Window.ffi_new(0)
      end

      def title=(str)
        Subtly::Gtk::Window.ffi_set_title(@widget, str)
      end

      def default_size=(array)
        Subtly::Gtk::Window.ffi_set_size(@widget, array[0], array[1])
      end

      def show_all
        Subtly::Gtk::Window.ffi_show_all(@widget)
      end

      def add(widget)
        Subtly::Gtk::Window.ffi_add(@widget, widget.to_wid)
      end

      def to_wid
        @widget
      end

      ## ffi_new
      # Create window
      #
      # @param  [Fixnum, #read]  Window type, #GtkWindowType
      # @return [Pointer] Return the new widget
      ##
     
      attach_function(:ffi_new,
        :gtk_window_new, [ :int ], :pointer
      )

      ## ffi_set_title
      # Set window title
      #
      # @param  [Pointer, #read]  Window to set title
      # @param  [String,  #read]  Window title
      ##

      attach_function(:ffi_set_title,
        :gtk_window_set_title, [:pointer, :string], :void
      )

      ## ffi_set_size
      # Set default size of widget
      #
      # @param  [Pointer, #read]  Widget to set size
      # @param  [Fixnum,  #read]  Width of the widget
      # @param  [Fixnum,  #read]  Height of the widget
      ##

      attach_function(:ffi_set_size,
        :gtk_window_set_default_size, [ :pointer, :int, :int ], :void
      )      

      ## ffi_show_all
      # Show all child widgets
      #
      # @param  [Pointer, #read]  Parent widget
      ##

      attach_function(:ffi_show_all,
        :gtk_widget_show_all, [ :pointer ], :void
      )

      ## ffi_add_container
      # Add widget to container
      #
      # @param  [Pointer, #read]  Container widget
      # @param  [Pointer, #read]  Widget to add
      ##

      attach_function(:ffi_add,
        :gtk_container_add, [ :pointer, :pointer ], :void
      )
    end # }}}

    class VBox # {{{
      attr_accessor :widget

      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")

      def initialize(homogenous, spacing)
        @widget = Subtly::Gtk::VBox.ffi_new(homogenous, spacing)
      end

      def add(widget, expand, fill, padding)
        Subtly::Gtk::VBox.ffi_add(@widget, widget.to_wid, expand, fill, padding)
      end

      def to_wid
        @widget
      end

      ## create_vbox
      # Create vbox widget
      #
      # @param  [Bool,   #read]  Homogeneous or not
      # @param  [Fixnum, #read]  Default space between children
      # @return [Pointer] Return the new widget
      ##

      attach_function(:ffi_new,
        :gtk_vbox_new, [ :bool, :int ], :pointer
      )

      ## ffi_add
      # Add widget to box
      #
      # @param  [Pointer, #read]  Box widget
      # @param  [Pointer, #read]  Widget to add
      # @param  [Bool,    #read]  Expand widget
      # @param  [Bool,    #read]  Fill widget
      # @param  [Fixnum,  #read]  Padding of children
      ##

      attach_function(:ffi_add,
        :gtk_box_pack_start, [:pointer, :pointer, :bool, :bool, :int], :void
      )      
    end # }}}

    class Label # {{{
      attr_accessor :widget

      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")

      def initialize(str)
        @widget = Subtly::Gtk::Label.ffi_new(str)
      end

      def text=(str)
        Subtly::Gtk::Label.ffi_set_text(@widget, str)
      end

      def to_wid
        @widget
      end
    
      ## create_label
      # Create label widget
      # 
      # @param  [String, #read]  String to display
      # @return [Pointer] Return the new widget
      ##

      attach_function(:ffi_new,
        :gtk_label_new, [ :string ], :pointer
      )

      ## set_text
      # Set widget label text
      # @param  [Pointer, #read]  Widget to set text
      # @param  [String,  #read]  String to set
      ##

      attach_function(:ffi_set_text,
        :gtk_label_set_text, [ :pointer, :string ], :void
      )
    end # }}}

    class Scrolled # {{{
      attr_accessor :widget

      extend FFI::Library

      ffi_lib("libgtk-x11-2.0")

      def initialize
        @widget = Subtly::Gtk::Scrolled.ffi_new(nil, nil)
      end

      def add(widget)
        Subtly::Gtk::Window.ffi_add(@widget, widget.to_wid)
      end

      def to_wid
        @widget
      end

      ## ffi_new
      # Create scrolled window
      #
      # @param  [Pointer, #read]  Horizontal adjustment, #GtkAdjustment
      # @param  [Pointer, #read]  Vertical adjustment, #GtkAdjustment
      # @return [Pointer] Return the new widget
      ##
      
      attach_function(:ffi_new,
        :gtk_scrolled_window_new, [ :pointer, :pointer ], :pointer
      )

      ## ffi_add
      # Add widget to container
      #
      # @param  [Pointer, #read]  Container widget
      # @param  [Pointer, #read]  Widget to add
      ##

      attach_function(:ffi_add,
        :gtk_container_add, [ :pointer, :pointer ], :void
      )      
    end # }}}
  end
end