class Gtk2_window_settings

A class that remembers settings for a given window and restrores them by using a given database.

Constants

ALLOWED_ARGS

Allowed given arguments.

DB_SCHEMA

How the database should be made to look like.

Attributes

window[R]

The window that this objects controls.

Public Class Methods

new(args) click to toggle source
# File lib/gtk2_window_settings.rb, line 29
def initialize(args)
  #Check arguments and initialize variables.
  @args = args
  @args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ALLOWED_ARGS.include?(key)
  end
  
  @db = @args[:db]
  @window = @args[:window]
  @name = @args[:name]
  
  #Check structure of database and load window-settings.
  Knj::Db::Revision.new.init_db("db" => @db, "schema" => DB_SCHEMA)
  
  if @data = @db.single(:Gtk2_window_settings, :name => @name)
    @id = @data[:id]
  else
    @id = @db.insert(:Gtk2_window_Settings, {:name => @name}, :return_id => true)
    @data = @db.single(:Gtk2_window_settings, :id => @id)
  end
  
  #Resize and move window to saved size (if saved).
  if @data[:width].to_i > 0 and @data[:height].to_i > 0
    #Use timeout to avoid any other size-code.
    Gtk.timeout_add(25) do
      @window.resize(@data[:width].to_i, @data[:height].to_i)
      false
    end
  end
  
  @window.move(@data[:pos_x].to_i, @data[:pos_y].to_i) if @data[:pos_registered].to_i == 1
  
  #Initialize events for the window.
  @window.signal_connect_after(:size_request, &self.method(:on_window_size_request))
  @window.signal_connect(:configure_event, &self.method(:on_window_moved))
end

Public Instance Methods

on_window_moved(*args) click to toggle source

Called when the window is moved on the screen. Writes the new size to the database.

# File lib/gtk2_window_settings.rb, line 88
def on_window_moved(*args)
  Gtk.timeout_remove(@moved_timeout) if @moved_timeout
  
  @moved_timeout = Gtk.timeout_add(500) do
    @moved_timeout = nil
    
    if @window and !@window.destroyed?
      pos = @window.position
      @db.update(:Gtk2_window_settings, {:pos_x => pos[0], :pos_y => pos[1], :pos_registered => 1}, {:id => @id}) if pos[0].to_i >= 0 and pos[1].to_i >= 0
    end
    
    false
  end
  
  #If false isnt returned the event might be canceled, which can lead to very buggy behaviour.
  return false
end
on_window_size_request(*args) click to toggle source

Called when the window is resized. Writes the new size to the database.

# File lib/gtk2_window_settings.rb, line 67
def on_window_size_request(*args)
  Gtk.timeout_remove(@size_request_timeout) if @size_request_timeout
  
  @size_request_timeout = Gtk.timeout_add(500, &lambda{
    begin
      @size_request_timeout = nil
      
      if @window and !@window.destroyed?
        size = @window.size
        @db.update(:Gtk2_window_settings, {:width => size[0], :height => size[1]}, {:id => @id}) if size[0].to_i > 0 and size[1].to_i > 0
      end
    ensure
      return false
    end
  })
  
  #If false isnt returned the event might be canceled, which can lead to very buggy behaviour.
  return false
end