view subtly/webkit.rb @ 45:b1354c094d9b

Added javascript keys
author unexist
date Sun, 24 Jan 2010 03:06:30 +0100
parents 0d42b4c43eb7
children
line wrap: on
line source

##
# @package subtly
#
# @file Webkit bindings
# @author Christoph Kappel <unexist@dorfelite.net>
# @version $Id: subtly/webkit.rb,v 45 1264298790.0-3600 unexist $
#
# This program can be distributed under the terms of the GNU GPL.
##

require "ffi"
require "subtly/gtk"
require "subtly/soup"

module Subtly
  class Webkit < Subtly::Gtk::Widget # {{{
    # Current uri
    attr_accessor :uri

    # Soup session
    attr_accessor :session

    extend FFI::Library

    ffi_lib("libwebkit-1.0")

    ## initialize
    # Create webkit view
    #
    # @return [Oject] New view
    ##

    def initialize
      @widget  = Subtly::Webkit.ffi_new
      @session = Subtly::Soup::Session.new(Subtly::Webkit.ffi_get_session)
    end

    ## uri
    # Load given uri in view
    #
    # @param [Pointer, #read]  Webkit view, #GtkWidget
    #
    # @return [String] Uri in webkit view
    ##

    def uri
      Subtly::Webkit.ffi_get_uri(@widget)
    end

    ## uri=
    # Set given uri
    #
    # @param [String, #read] uri  Uri to set
    ##
    
    def uri=(uri)
      Subtly::Webkit.ffi_set_uri(@widget, uri)
    end

    ## title
    # Get title or uri loaded
    #
    # @return [String] Title of uri
    ##

    def title
      Subtly::Webkit.ffi_get_title(@widget)
    end

    ## back
    # Step back in browser history
    #
    # @param [Fixnum, #read]  steps  Steps to move back
    ##

    def back(steps = 1)
      steps *= -1 unless(0 > steps)
      Subtly::Webkit.ffi_go_steps(@widget, steps)
    end

    ## forward
    # Step forward in history
    #
    # @param [Fixnum, #read]  steps  Steps to move forward
    ##

    def forward(steps = 1)
      Subtly::Webkit.ffi_go_steps(@widget, steps)
    end

    ## stop
    # Stop page loading
    ##

    def stop
      Subtly::Webkit.ffi_stop(@widget)
    end

    ## reload
    # Reload page from source or cache
    #
    # @param [Bool, #read]  use_cache  Use cache or not
    ##

    def reload(use_cache)
      if(use_cache)
        Subtly::Webkit.ffi_reload_cache(@widget)
      else
        Subtly::Webkit.ffi_reload(@widget)
      end
    end

    ## zoom
    # Set zoom level
    #
    # @param [Fixnum, #read] level Level step
    ##

    def zoom=(level)
      if(0 < level)
        Subtly::Webkit.ffi_zoom_in(@widget)
      else
        Subtly::Webkit.ffi_zoom_out(@widget)
      end
    end

    ## zoom
    # Get zoom level
    #
    # @return [Float] Zoom level
    ##

    def zoom
      Subtly::Webkit.ffi_zoom_level(@widget)
    end

    ## find
    # Find text
    #
    # @param [String, #read]  str      String to look for
    # @param [Bool,   #read]  forward  Whther to find forward
    ##

    def find(str, forward)
      Subtly::Webkit.ffi_find_text(@widget, str, false, forward, true)
    end

    ## mark
    # Mark text matches
    #
    # @param [String, #read]  str  String to look for
    ##

    def mark(str)
      Subtly::Webkit.ffi_mark(@widget, str, false, 0)
    end

    ## unmark
    # Unmark text matches
    ##

    def unmark
      Subtly::Webkit.ffi_unmark(@widget)
    end

    ## highlight=
    # Enable text highlights
    #
    # @param [Bool, #read]  high  Whether to enable highlight
    ##

    def highlight=(high)
      Subtly::Webkit.ffi_highlight(@widget, high)
    end

    ## proxy=
    # Set proxy
    #
    # @param [String, #read]  uri  Uri to proxy
    ##

    def proxy=(uri)
      @session.proxy = uri #< Delegate to session
    end

    ## execute
    # Execute script
    #
    # @param [String, #read]  script  Script to execute
    ##

    def execute(script)
      Subtly::Webkit.ffi_execute(@widget, script)
    end

    private

    ## ffi_new
    # Create webkit view
    #
    # @return [Pointer] New view
    ##

    attach_function(:ffi_new,
      :webkit_web_view_new, [ ], :pointer
    )

    ## ffi_get_uri
    # Load given uri in view
    #
    # @param [Pointer, #read] web_view  A #WebKitWebView
    #
    # @return [String] Uri in webkit view
    ##

    attach_function(:ffi_get_uri,
      :webkit_web_view_get_uri, [ :pointer ], :string
    )

    ## ffi_set_uri
    # Set given uri in view
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    # @param [String,  #read] uri        Uri to set
    ##

    attach_function(:ffi_set_uri,
      :webkit_web_view_load_uri, [ :pointer, :string ], :void
    )

    ## ffi_get_title
    # Get title or uri loaded
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    #
    # @return [String] Title of uri
    ##

    attach_function(:ffi_get_title,
      :webkit_web_view_get_title, [ :pointer ], :string
     )

    ## ffi_go_steps
    # Go back or forward in history
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    # @param [Int,     #read]  steps     Steps to go
    ##

    attach_function(:ffi_go_steps,
      :webkit_web_view_go_back_or_forward, [ :pointer, :int ], :void
    )

    ## ffi_stop
    # Stop page loading
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##


    attach_function(:ffi_stop,
      :webkit_web_view_stop_loading, [ :pointer ], :void
    )

    ## ffi_reload_cache
    # Reload view from cache
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##

    attach_function(:ffi_reload_cache,
      :webkit_web_view_reload, [ :pointer ], :void
    )

    ## ffi_reload
    # Reload view bypassing cache
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##

    attach_function(:ffi_reload,
      :webkit_web_view_reload_bypass_cache, [ :pointer ], :void
    )

    ## ffi_zoom_level
    # Get zoom level
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView

    attach_function(:ffi_zoom_level,
      :webkit_web_view_get_zoom_level, [ :pointer ], :float
    )

    ## ffi_zoom_in
    # Reload view bypassing cache
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##

    attach_function(:ffi_zoom_in,
      :webkit_web_view_zoom_in, [ :pointer ], :void
    )

    ## ffi_zoom_out
    # Reload view bypassing cache
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##

    attach_function(:ffi_zoom_out,
      :webkit_web_view_zoom_out, [ :pointer ], :void
    )

    ## ffi_get_target_list
    # Gets the DND target list
    #
    # @param [Pointer, #read]  web_view  A #WebKitWebView
    ##

    attach_function(:ffi_get_target_list,
      :webkit_web_view_get_paste_target_list, [ :pointer ], :pointer
    )

    ## ffi_find_text
    # Find text
    #
    # @param [Pointer, #read]  web_view        A #WebKitWebView
    # @param [String,  #read]  text            String to look for
    # @param [Bool,    #read]  case_sensitive  Whether to respect case
    # @param [Bool,    #read]  forward         Whether to find forward
    # @param [Bool,    #read]  wrap            Whether to continue at end
    #
    # @return [Bool]  Success or not
    ##

    attach_function(:ffi_find_text,
      :webkit_web_view_search_text, 
      [ :pointer, :string, :bool, :bool, :bool ],
      :bool
    )

    ## ffi_mark
    # Mark text matches
    #
    # @param [Pointer, #read]  web_view        A #WebKitWebView
    # @param [String,  #read]  string          String to look for
    # @param [Bool,    #read]  case_sensitive  Whether to respect case
    # @param [Fixnum,  #read]  limit           Number of highlights
    ##

    attach_function(:ffi_mark,
      :webkit_web_view_mark_text_matches, 
      [ :pointer, :string, :bool, :uint ], 
      :uint
    )

    ## ffi_unmark
    # Unmark text matches
    #
    # @param [Pointer, #read]  web_view   A #WebKitWebView
    ##

    attach_function(:ffi_unmark,
      :webkit_web_view_unmark_text_matches, [ :pointer ], :void
    )

    ## ffi_highlight
    # Enable text highlights
    #
    # @param [Pointer, #read]  web_view   A #WebKitWebView
    # @param [Bool,    #read]  highlight  Whether to enable highlight
    ##

    attach_function(:ffi_highlight,
      :webkit_web_view_set_highlight_text_matches, [ :pointer, :bool ], :void
    )

    ## ffi_highlight
    # Enable text highlights
    #
    # @return [Pointer] A #SoupSession
    ##

    attach_function(:ffi_get_session,
      :webkit_get_default_session, [ ], :pointer
    )

    ## ffi_execute
    # Execute script
    #
    # @param [Pointer, #read] web_view  A #WebKitWebView
    # @param [String,  #read] script    A Javascript
    ##

    attach_function(:ffi_execute,
      :webkit_web_view_execute_script, [ :pointer, :string ], :void
    )
  end # }}}
end