Class: Wx::SF::ShapeDataObject

Inherits:
DataObject
  • Object
show all
Defined in:
lib/wx/shapes/shape_data_object.rb

Overview

Class encapsulating data object used during clipboard operations with shapes.

Constant Summary collapse

DataFormatID =
Wx::DataFormat.new('ShapeFrameWorkDataFormat1_0')

Instance Method Summary collapse

Constructor Details

#initializeShapeDataObject #initialize(selection) ⇒ ShapeDataObject

Returns a new instance of ShapeDataObject.

Overloads:



16
17
18
19
20
21
22
23
24
# File 'lib/wx/shapes/shape_data_object.rb', line 16

def initialize(selection = nil)
  unless selection.nil? || (selection.is_a?(Array) && selection.all? { |e| e.is_a?(Shape) })
    raise SFException, 'Expected nil or Array<Wx::SF::Shape>'
  end
  super()
  @shapes = selection
  @data = nil
  @format = DataFormatID.get_type
end

Instance Method Details

#get_all_formats(direction) ⇒ Object

List all the formats that we support. By default, the first is treated as the ‘preferred’ format; this can be overridden by providing a get_preferred format.



43
44
45
# File 'lib/wx/shapes/shape_data_object.rb', line 43

def get_all_formats(direction)
  [ DataFormatID, Wx::DF_TEXT, Wx::DF_UNICODETEXT ]
end

#get_as_shapesObject



33
34
35
36
37
38
# File 'lib/wx/shapes/shape_data_object.rb', line 33

def get_as_shapes
  if @format == Wx::DataFormatId::DF_TEXT || @format == Wx::DataFormatId::DF_UNICODETEXT
    @shapes ||= (@data && @data.size>0 ? FIRM.deserialize(@data, format: :yaml) : nil)
  end
  @shapes || []
end

#get_as_textObject



26
27
28
29
30
31
# File 'lib/wx/shapes/shape_data_object.rb', line 26

def get_as_text
  if  @format == DataFormatID.get_type
    @data ||= (@shapes ? @shapes.serialize(format: :yaml) : nil)
  end
  @data || ''
end

#get_data_here(format) ⇒ Object

Do getting the data



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wx/shapes/shape_data_object.rb', line 86

def get_data_here(format)
  case format.get_type
  when Wx::DataFormatId::DF_TEXT, Wx::DataFormatId::DF_UNICODETEXT
    @data
  when DataFormatID.get_type
    begin
      get_as_text
    rescue Exception
      $stderr.puts "#{$!}\n#{$!.backtrace.join("\n")}"
      nil
    end
  else
    nil
  end
end

#get_data_size(format) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/wx/shapes/shape_data_object.rb', line 74

def get_data_size(format)
  case format.get_type
  when Wx::DataFormatId::DF_TEXT, Wx::DataFormatId::DF_UNICODETEXT
    @data ? @data.size : 0
  when DataFormatID.get_type
    get_as_text.bytesize
  else
    0
  end
end

#set_data(format, the_data) ⇒ Object

Do setting the data



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wx/shapes/shape_data_object.rb', line 48

def set_data(format, the_data)
  case format.get_type
  when DataFormatID.get_type
    @shapes = if the_data.size > 0
                begin
                  FIRM.deserialize(the_data, format: :yaml)
                rescue Exception
                  $stderr.puts "#{$!}\n#{$!.backtrace.join("\n")}"
                  return false
                end
              else
                nil
              end
    @data = nil
    @format = format.get_type
    true
  when Wx::DataFormatId::DF_TEXT, Wx::DataFormatId::DF_UNICODETEXT
    @data = the_data
    @shapes = nil
    @format = format.get_type
    true
  else
    false
  end
end