Class: Writexlsx::Package::Button

Inherits:
Object
  • Object
show all
Includes:
Utility::Common, Utility::Drawing, Utility::StringWidth
Defined in:
lib/write_xlsx/package/button.rb

Constant Summary

Constants included from Utility::StringWidth

Utility::StringWidth::CHAR_WIDTHS, Utility::StringWidth::DEFAULT_COL_PIXELS, Utility::StringWidth::MAX_DIGIT_WIDTH, Utility::StringWidth::PADDING

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

Instance Method Summary collapse

Methods included from Utility::Drawing

#pixels_to_points, #shape_style_base, #v_shape_attributes_base, #v_shape_style_base, #write_anchor, #write_auto_fill, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke

Methods included from Utility::StringWidth

#xl_string_pixel_width

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(worksheet, row, col, params, default_row_pixels, button_number) ⇒ Button

Returns a new instance of Button.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
68
# File 'lib/write_xlsx/package/button.rb', line 15

def initialize(worksheet, row, col, params, default_row_pixels, button_number)
  @worksheet = worksheet
  @default_row_pixels = default_row_pixels

  # Set the button caption.
  caption = params[:caption] || "Button #{button_number}"

  @font = { _caption: caption }

  # Set the macro name.
  @macro = if params[:macro]
             "[0]!#{params[:macro]}"
           else
             "[0]!Button#{button_number}_Click"
           end

  # Set the alt text for the button.
  @description = params[:description]

  # Ensure that a width and height have been set.
  default_height = @default_row_pixels
  width  = params[:width]  || DEFAULT_COL_PIXELS
  height = params[:height] || default_row_pixels

  # Scale the size of the button box if required.
  width  *= params[:x_scale] if params[:x_scale]
  height *= params[:y_scale] if params[:y_scale]

  # Round the dimensions to the nearest pixel.
  width  = (0.5 + width).to_i
  height = (0.5 + height).to_i

  # Set the x/y offsets.
  x_offset = params[:x_offset] || 0
  y_offset = params[:y_offset] || 0

  start_row = row
  start_col = col

  # Calculate the positions of button object.
  vertices = @worksheet.position_object_pixels(
    start_col,
    start_row,
    x_offset,
    y_offset,
    width,
    height
  )

  # Add the width and height for VML.
  vertices << [width, height]

  @vertices = vertices
end

Instance Method Details

#colorObject



86
87
88
# File 'lib/write_xlsx/package/button.rb', line 86

def color
  'buttonFace [67]'
end

#fill_attributesObject

attributes for <v:fill> element.



112
113
114
115
116
117
# File 'lib/write_xlsx/package/button.rb', line 112

def fill_attributes
  [
    ['color2',             'buttonFace [67]'],
    ['o:detectmouseclick', 't']
  ]
end

#style_additionObject



90
91
92
# File 'lib/write_xlsx/package/button.rb', line 90

def style_addition
  ['mso-wrap-style:tight']
end

#typeObject



82
83
84
# File 'lib/write_xlsx/package/button.rb', line 82

def type
  '#_x0000_t201'
end

#v_shape_attributes(id, z_index) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/write_xlsx/package/button.rb', line 70

def v_shape_attributes(id, z_index)
  attributes = v_shape_attributes_base(id)
  attributes << ['alt', @description] if @description

  attributes << ['style', (v_shape_style_base(z_index, @vertices) + style_addition).join]
  attributes << ['o:button',    't']
  attributes << ['fillcolor',   color]
  attributes << ['strokecolor', 'windowText [64]']
  attributes << ['o:insetmode', 'auto']
  attributes
end

#write_client_dataObject

Write the <x:ClientData> element.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/write_xlsx/package/button.rb', line 148

def write_client_data
  attributes = [%w[ObjectType Button]]

  @writer.tag_elements('x:ClientData', attributes) do
    # Write the x:Anchor element.
    write_anchor
    # Write the x:PrintObject element.
    write_print_object
    # Write the x:AutoFill element.
    write_auto_fill
    # Write the x:FmlaMacro element.
    write_fmla_macro
    # Write the x:TextHAlign element.
    write_text_halign
    # Write the x:TextVAlign element.
    write_text_valign
  end
end

#write_fmla_macroObject

Write the <x:FmlaMacro> element.



177
178
179
# File 'lib/write_xlsx/package/button.rb', line 177

def write_fmla_macro
  @writer.data_element('x:FmlaMacro', @macro)
end

#write_print_objectObject

Write the <x:PrintObject> element.



170
171
172
# File 'lib/write_xlsx/package/button.rb', line 170

def write_print_object
  @writer.data_element('x:PrintObject', 'False')
end

#write_rotation_lockObject

Write the <o:lock> element.



122
123
124
125
126
127
128
# File 'lib/write_xlsx/package/button.rb', line 122

def write_rotation_lock
  attributes = [
    ['v:ext',    'edit'],
    %w[rotation t]
  ]
  @writer.empty_tag('o:lock', attributes)
end

#write_shape(writer, id, z_index) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/write_xlsx/package/button.rb', line 94

def write_shape(writer, id, z_index)
  @writer = writer

  attributes = v_shape_attributes(id, z_index)

  @writer.tag_elements('v:shape', attributes) do
    # Write the v:fill element.
    write_fill
    # Write the o:lock element.
    write_rotation_lock
    # Write the v:textbox element.
    write_textbox
    # Write the x:ClientData element.
    write_client_data
  end
end

#write_text_halignObject

Write the <x:TextHAlign> element.



184
185
186
# File 'lib/write_xlsx/package/button.rb', line 184

def write_text_halign
  @writer.data_element('x:TextHAlign', 'Center')
end

#write_text_valignObject

Write the <x:TextVAlign> element.



191
192
193
# File 'lib/write_xlsx/package/button.rb', line 191

def write_text_valign
  @writer.data_element('x:TextVAlign', 'Center')
end

#write_textboxObject

Write the <v:textbox> element.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/write_xlsx/package/button.rb', line 133

def write_textbox
  attributes = [
    ['style', 'mso-direction-alt:auto'],
    ['o:singleclick', 'f']
  ]

  @writer.tag_elements('v:textbox', attributes) do
    # Write the div element.
    write_div('center', @font)
  end
end