Class: Win32::Clipboard

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlclipboard.rb

Class Method Summary collapse

Class Method Details

.registerHTMLObject



11
12
13
14
15
# File 'lib/htmlclipboard.rb', line 11

def self.registerHTML
  if not @cfHTML
    @cfHTML = Win32::Clipboard.RegisterClipboardFormat("HTML Format")
  end
end

.set_html_data(clip_data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/htmlclipboard.rb', line 29

def self.set_html_data(clip_data)
  sDataStart  = "<HTML><BODY>\n<!--StartFragment -->"
  sDataEnd = "<!--EndFragment -->\n</BODY></HTML>"
  sData = ""
  #

  # Clipboard Header:   looks like ->

  # Version: 1.0

  #    StartHTML:0000000000

  #    EndHTML:0000000000

  #    StartFragment:0000000000

  #    EndFragment:0000000000

  sDataHdr = "Version:1.0\r\nStartHTML:aaaaaaaaaa\r\nEndHTML:bbbbbbbbbb\r\n"
  sDataHdr += "StartFragment:cccccccccc\r\nEndFragment:dddddddddd\r\n"
  sData = sDataHdr + sDataStart + clip_data + sDataEnd

  sData = sData.gsub(/aaaaaaaaaa/) { zeroFmt(sDataHdr.length) }
  sData = sData.gsub(/bbbbbbbbbb/) { zeroFmt(sData.length) }
  sData = sData.gsub(/cccccccccc/) { zeroFmt(sDataHdr.length + sDataStart.length) }
  sData = sData.gsub(/dddddddddd/) { zeroFmt(sDataHdr.length + sDataStart.length + clip_data.length) }

  if not @cfHTML
    self.registerHTML
  end

  self.open
  EmptyClipboard()
  hmem = GlobalAlloc(GHND, sData.length + 10)
  mem  = GlobalLock(hmem)
  memcpy(mem, sData, sData.length)
  begin
    if SetClipboardData(@cfHTML, hmem) == 0
      raise Error, "SetClipboardData() failed; " + get_last_error
    end
  ensure
   GlobalFree(hmem)
   self.close
  end
  self
end

.zeroFmt(nbr) ⇒ Object

zeroFmt(nbr)

--  Takes a number and formats with zero's in front.
    1234 => "0000001234"
    23   => "0000000023"


23
24
25
26
27
# File 'lib/htmlclipboard.rb', line 23

def self.zeroFmt(nbr)
  zeros = "0"*10
  len = nbr.to_s.length        # this is the number of digits in the number  2784 => 4

  return zeros.slice(0..(zeros.length - len - 1)) + nbr.to_s
end