changeset 5:37ddd775df8a

Updated classes
author unexist
date Sun, 13 Dec 2009 04:31:01 +0100
parents 705abf5135fe
children c86846c6b2da
files subtly/gtk.rb
diffstat 1 files changed, 456 insertions(+), 293 deletions(-) [+]
line wrap: on
line diff
--- a/subtly/gtk.rb	Sun Dec 13 04:30:39 2009 +0100
+++ b/subtly/gtk.rb	Sun Dec 13 04:31:01 2009 +0100
@@ -12,303 +12,466 @@
 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
-      )      
+  class Runner # {{{
+    extend FFI::Library
 
-      ## 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")
+    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
+    def initialize
+      Subtly::GThread.ffi_init(nil)
+      Subtly::Runner.ffi_init(nil, nil)
+    end
 
-      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
+    def run
+      Subtly::Runner.ffi_run
+    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
-      )
+    ## init
+    # Init Gtk system
+    #
+    # @param  [Pointer, #read]  Parent widget
+    ##
 
-      ## ffi_add
-      # Add widget to container
-      #
-      # @param  [Pointer, #read]  Container widget
-      # @param  [Pointer, #read]  Widget to add
-      ##
+    attach_function(:ffi_init,                   
+      :gtk_init, [ :pointer, :pointer ], :void
+    )
 
-      attach_function(:ffi_add,
-        :gtk_container_add, [ :pointer, :pointer ], :void
-      )      
-    end # }}}
-  end
+    ## 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 # {{{
+    attr_accessor :widget
+
+    extend FFI::Library
+
+    ffi_lib("libgtk-x11-2.0")
+
+    # States
+    enum(:state,
+    [
+      :normal, 0,
+      :active,
+      :prelight,
+      :selected,
+      :insensitive
+    ])
+
+    def foreground=(color)
+      col = Subtly::GdkColor.new
+
+      Subtly::Gdk.ffi_parse_color(color, col.pointer)
+      Subtly::Widget.ffi_set_fg(@widget, :normal, col.pointer)
+    end
+
+    def background=(color)
+      col = Subtly::GdkColor.new
+
+      Subtly::Gdk.ffi_parse_color(color, col.pointer)
+      Subtly::Widget.ffi_set_bg(@widget, :normal, col.pointer)      
+    end
+
+    def connect(signal, callback, data)
+      case callback.arity
+        when 2 then
+          Subtly::GObject.ffi_signal_connect2(@widget, signal, callback, data, nil, 0)
+        when 3 then
+          Subtly::GObject.ffi_signal_connect3(@widget, signal, callback, data, nil, 0)
+        when 4 then
+          Subtly::GObject.ffi_signal_connect4(@widget, signal, callback, data, nil, 0)
+      end
+    end
+
+    def alignment(xalign, yalign)
+      Subtly::Widget.ffi_set_alignment(@widget, xalign, yalign)
+    end
+
+    def to_wid
+      @widget
+    end
+    
+    ## ffi_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(:ffi_set_fg,
+      :gtk_widget_modify_fg, [ :pointer, :state, :pointer ], :void
+    )
+
+    ## ffi_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(:ffi_set_bg,
+      :gtk_widget_modify_bg, [ :pointer, :state, :pointer ], :void
+    )
+
+    ## ffi_set_alignment
+    # Set background color of widget
+    #
+    # @param  [Pointer, #read]  Widget to set alignment
+    # @param  [Float,   #read]  X alignment
+    # @param  [Float,   #read]  Y alignment
+    ##
+
+    attach_function(:ffi_set_alignment,
+      :gtk_misc_set_alignment, [ :pointer, :float, :float ], :void
+    )
+  end # }}}
+
+  class Window < Subtly::Widget # {{{
+    extend FFI::Library
+
+    ffi_lib("libgtk-x11-2.0")
+
+    def initialize
+      @widget = Subtly::Window.ffi_new(0)
+    end
+
+    def title=(str)
+      Subtly::Window.ffi_set_title(@widget, str)
+    end
+
+    def klass=(klass)
+      Subtly::Window.ffi_set_klass(@widget, klass[0], klass[1])
+    end
+
+    def size=(array)
+      Subtly::Window.ffi_set_size(@widget, array[0], array[1])
+    end
+
+    def show_all
+      Subtly::Window.ffi_show_all(@widget)
+    end
+
+    def add(widget)
+      Subtly::Window.ffi_add(@widget, widget.to_wid)
+    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_klass
+    # Set window title
+    #
+    # @param  [Pointer, #read]  Window to set class
+    # @param  [String,  #read]  Instance name
+    # @param  [String,  #read]  Class name
+    ##
+
+    attach_function(:ffi_set_klass,
+      :gtk_window_set_wmclass, [:pointer, :string, :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
+    # 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 Box < Subtly::Widget # {{{
+    extend FFI::Library
+
+    ffi_lib("libgtk-x11-2.0")
+
+    def initialize(type, homogenous, spacing)
+      if(:vbox == type)
+        @widget = Subtly::Box.ffi_vbox_new(homogenous, spacing)
+      else
+        @widget = Subtly::Box.ffi_hbox_new(homogenous, spacing)
+      end
+    end
+
+    def add(widget, expand, fill, padding)
+      Subtly::Box.ffi_add(@widget, widget.to_wid, expand, fill, padding)
+    end
+
+    ## ffi_vbox_new
+    # 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_vbox_new,
+      :gtk_vbox_new, [ :bool, :int ], :pointer
+    )
+    
+    ## ffi_hbox_new
+    # Create hbox widget
+    #
+    # @param  [Bool,   #read]  Homogeneous or not
+    # @param  [Fixnum, #read]  Default space between children
+    # @return [Pointer] Return the new widget
+    ##
+
+    attach_function(:ffi_hbox_new,
+      :gtk_hbox_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 < Subtly::Widget# {{{
+    def initialize(single, selectable)
+      @widget = Subtly::Label.ffi_new("Subtly")
+
+      Subtly::Label.ffi_single_line(@widget, single)
+      Subtly::Label.ffi_selectable(@widget,  selectable)
+    end
+
+    def text=(str)
+      Subtly::Label.ffi_set_text(@widget, str)
+    end
+
+    def markup=(str)
+      Subtly::Label.ffi_set_markup(@widget, str)
+    end
+
+    ## ffi_new
+    # Create label widget
+    # 
+    # @param  [String, #read]  String to display
+    # @return [Pointer] Return the new widget
+    ##
+
+    attach_function(:ffi_new,
+      :gtk_label_new, [ :string ], :pointer
+    )
+
+    ## ffi_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
+    )
+
+    ## ffi_set_markup
+    # Set widget label markup
+    # @param  [Pointer, #read]  Widget to set text
+    # @param  [String,  #read]  String to set
+    ##
+
+    attach_function(:ffi_set_markup,
+      :gtk_label_set_markup, [ :pointer, :string ], :void
+    )
+
+    ## ffi_single_line
+    # Set single line mode
+    # @param  [Pointer, #read]  Widget to set text
+    # @param  [Bool,    #read]  Set single line mode
+    ##
+
+    attach_function(:ffi_single_line,
+      :gtk_label_set_single_line_mode, [ :pointer, :bool ], :void
+    )
+
+    ## ffi_selectable
+    # Set selected
+    # @param  [Pointer, #read]  Widget to set text
+    # @param  [Bool,    #read]  Set selectable
+    ##
+
+    attach_function(:ffi_selectable,
+      :gtk_label_set_selectable , [ :pointer, :bool ], :void
+    )
+  end # }}}
+
+  class Entry < Subtly::Widget# {{{
+    def initialize(frame)
+      @widget = Subtly::Entry.ffi_new
+
+      Subtly::Entry.ffi_set_frame(@widget, frame)
+    end
+
+    def text=(str)
+      Subtly::Label.ffi_set_text(@widget, str)
+    end
+
+    ## ffi_new
+    # Create entry widget
+    # 
+    # @return [Pointer] Return the new widget
+    ##
+
+    attach_function(:ffi_new,
+      :gtk_entry_new, [ ], :pointer
+    )
+
+    ## ffi_set_frame
+    # Set widget frame
+    # @param  [Pointer, #read]  Widget to set text
+    # @param  [Bool,  #read]    Set frame
+    ##
+
+    attach_function(:ffi_set_frame,
+      :gtk_entry_set_has_frame, [ :pointer, :bool ], :void
+    )
+
+    ## ffi_set_text
+    # Set widget entry text
+    # @param  [Pointer, #read]  Widget to set text
+    # @param  [String,  #read]  String to set
+    ##
+
+    attach_function(:ffi_set_text,
+      :gtk_entry_set_text, [ :pointer, :string ], :void
+    )
+  end # }}}
+
+  class Scrolled < Subtly::Widget # {{{
+    extend FFI::Library
+
+    ffi_lib("libgtk-x11-2.0")
+
+    def initialize
+      @widget = Subtly::Scrolled.ffi_new(nil, nil)
+    end
+
+    def add(widget)
+      Subtly::Window.ffi_add(@widget, widget.to_wid)
+    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 # }}}
+
+  class Webkit < Subtly::Widget # {{{
+    extend FFI::Library
+
+    ffi_lib("libwebkit-1.0")
+
+    def initialize
+      @widget = Subtly::Webkit.ffi_new
+    end
+
+    def uri=(uri)
+      Subtly::Webkit.ffi_load_uri(@widget, uri)
+    end
+
+    ## ffi_new
+    # Create webkit view
+    #
+    # @return [Pointer] Return the new view
+    ##
+
+    attach_function(:ffi_new,
+      :webkit_web_view_new, [ ], :pointer
+    )
+
+    ## ffi_load_uri
+    # Load given uri in view
+    #
+    # @param  [Pointer, #read]  Webkit view, #GtkWidget
+    # @param  [String, #read]   Uri to load
+    ##
+
+    attach_function(:ffi_load_uri,
+      :webkit_web_view_load_uri, [ :pointer, :string ], :void
+    )
+  end # }}}
 end