Class: Win32::HtmlClipboard

Inherits:
Clipboard show all
Defined in:
lib/win32/html_clipboard.rb

Overview

The HtmlClipboard class is a subclass of Clipboard that explicitly handles text in HTML format.

Constant Summary collapse

CF_HTML =

Clipboard format value

RegisterClipboardFormat("HTML Format")

Constants inherited from Clipboard

Clipboard::BITMAP, Clipboard::DIB, Clipboard::ENHMETAFILE, Clipboard::HDROP, Clipboard::OEMTEXT, Clipboard::TEXT, Clipboard::UNICODETEXT, Clipboard::VERSION

Constants included from Windows::Constants

Windows::Constants::BI_BITFIELDS, Windows::Constants::BI_RGB, Windows::Constants::CF_TEXT, Windows::Constants::GHND, Windows::Constants::GWL_USERDATA, Windows::Constants::GWL_WNDPROC, Windows::Constants::WM_CHANGECBCHAIN, Windows::Constants::WM_CLOSE, Windows::Constants::WM_DESTROY, Windows::Constants::WM_DRAWCLIPBOARD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Clipboard

empty, format_available?, format_name, formats, notify_change, num_formats, register_format

Constructor Details

#initializeHtmlClipboard

:nodoc:



80
81
82
83
84
85
86
# File 'lib/win32/html_clipboard.rb', line 80

def initialize # :nodoc:
  @html      = nil
  @fragment  = nil
  @selection = nil
  @source    = nil
  @version   = nil
end

Class Method Details

.dataObject

This method is nearly identical to the Clipboard.data method, but it decodes the data to preserve the HTML formatting.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/win32/html_clipboard.rb', line 98

def self.data
  begin
    self.open
    if IsClipboardFormatAvailable(CF_HTML)
      handle = GetClipboardData(CF_HTML)
      size   = GlobalSize(handle)
      ptr    = FFI::Pointer.new(:char, handle)

      clip_data = decode_data(ptr.read_bytes(size))
    else
      clip_data = ''
    end
  ensure
    self.close
  end
  clip_data
end

.data_detailsObject

Returns the block marker information for the HTML, or an empty string if there is no clipboard data.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/win32/html_clipboard.rb', line 119

def self.data_details
  clip_data = data
  string = ""
  unless clip_data.empty?
    string << "prefix=>>>#{@prefix}<<<END"
    string << "version=>>>#{@version}<<<END"
    string << "selection=>>>#{@selection}<<<END"
    string << "fragment=>>>#{@fragment}<<<END"
    string << "html=>>>#{@html}<<<END"
    string << "source=>>>#{@source}<<<END"
  end
  string
end

.html_format?Boolean

Returns a boolean indicating whether or not the clipboard contains data in HTML format.

Returns:

  • (Boolean)


91
92
93
# File 'lib/win32/html_clipboard.rb', line 91

def self.html_format?
  format_available?(CF_HTML)
end

.set_data(fragment, selection = nil, html = nil, source = nil) ⇒ Object

Put a well-formed fragment of HTML on the clipboard.

The selection, if provided, must be a literal string within a fragment.

The html value, if provided, must be a well formed HTML document that textually contains a fragment and its required markers.

The source, if provided, should include a scheme (file, http, or https) plus a file name. The default is file:// + __FILE__.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/win32/html_clipboard.rb', line 144

def self.set_data(fragment, selection=nil, html=nil, source=nil)
  selection ||= fragment
  html      ||= DEFAULT_HTML_BODY % fragment
  source    ||= 'file://' + __FILE__

  fragment_start  = html.index(fragment)
  fragment_end    = fragment_start + fragment.length
  selection_start = html.index(selection)
  selection_end   = selection_start + selection.length

  clip_data = encode_data(
    html,
    fragment_start,
    fragment_end,
    selection_start,
    selection_end,
    source
  )

  self.open
  EmptyClipboard()

  # Global Allocate a movable piece of memory.
  hmem = GlobalAlloc(GHND, clip_data.length + 4)
  mem  = GlobalLock(hmem)
  mem.write_bytes(clip_data, 0, clip_data.size)

  clip_data2 = fragment.gsub(/<[^>]+?>/,'')
  hmem2 = GlobalAlloc(GHND, clip_data2.length + 4)
  mem2  = GlobalLock(hmem2)
  mem2.write_bytes(clip_data2, 0, clip_data2.size)

  # Set the new data
  begin
    if SetClipboardData(CF_HTML, hmem) == 0
      raise SystemCallError.new('SetClipboardData', FFI.errno)
    end

    if SetClipboardData(CF_TEXT, hmem2) == 0
      raise SystemCallError.new('SetClipboardData', FFI.errno)
    end
  ensure
    GlobalFree(hmem)
    GlobalFree(hmem2)
    self.close
  end
  self
end