summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/pane.rb
blob: 91741e175cd85655ff0aaa51783795b2eb7e53ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true

module Axlsx
  # Pane options for a worksheet.
  #
  # @note The recommended way to manage the pane options is via SheetView#pane
  # @see SheetView#pane
  class Pane
    include Axlsx::OptionsParser
    include Axlsx::SerializedAttributes
    # Creates a new {Pane} object
    # @option options [Symbol] active_pane Active Pane
    # @option options [Symbol] state Split State
    # @option options [Cell, String] top_left_cell Top Left Visible Cell
    # @option options [Integer] x_split Horizontal Split Position
    # @option options [Integer] y_split Vertical Split Position
    def initialize(options = {})
      # defaults
      @active_pane = @state = @top_left_cell = nil
      @x_split = @y_split = 0
      parse_options options
    end

    serializable_attributes :active_pane, :state, :top_left_cell, :x_split, :y_split

    # Active Pane
    # The pane that is active.
    # Options are
    #  * bottom_left:  Bottom left pane, when both vertical and horizontal
    #                  splits are applied. This value is also used when only
    #                  a horizontal split has been applied, dividing the pane
    #                  into upper and lower regions. In that case, this value
    #                  specifies the bottom pane.
    #  * bottom_right: Bottom right pane, when both vertical and horizontal
    #                  splits are applied.
    #  * top_left:     Top left pane, when both vertical and horizontal splits
    #                  are applied. This value is also used when only a horizontal
    #                  split has been applied, dividing the pane into upper and lower
    #                  regions. In that case, this value specifies the top pane.
    #                  This value is also used when only a vertical split has
    #                  been applied, dividing the pane into right and left
    #                  regions. In that case, this value specifies the left pane
    #  * top_right:    Top right pane, when both vertical and horizontal
    #                  splits are applied. This value is also used when only
    #                  a vertical split has been applied, dividing the pane
    #                  into right and left regions. In that case, this value
    #                  specifies the right pane.
    # @see type
    # @return [Symbol]
    # default nil
    attr_reader :active_pane

    # Split State
    # Indicates whether the pane has horizontal / vertical
    # splits, and whether those splits are frozen.
    # Options are
    #  * frozen:       Panes are frozen, but were not split being frozen. In
    #                  this state, when the panes are unfrozen again, a single
    #                  pane results, with no split. In this state, the split
    #                  bars are not adjustable.
    #  * frozen_split: Panes are frozen and were split before being frozen. In
    #                  this state, when the panes are unfrozen again, the split
    #                  remains, but is adjustable.
    #  * split:        Panes are split, but not frozen. In this state, the split
    #                  bars are adjustable by the user.
    # @see type
    # @return [Symbol]
    # default nil
    attr_reader :state

    # Top Left Visible Cell
    # Location of the top left visible cell in the bottom
    # right pane (when in Left-To-Right mode).
    # @see type
    # @return [String]
    # default nil
    attr_reader :top_left_cell

    # Horizontal Split Position
    # Horizontal position of the split, in 1/20th of a point; 0 (zero)
    # if none. If the pane is frozen, this value indicates the number
    # of columns visible in the top pane.
    # @see type
    # @return [Integer]
    # default 0
    attr_reader :x_split

    # Vertical Split Position
    # Vertical position of the split, in 1/20th of a point; 0 (zero)
    # if none. If the pane is frozen, this value indicates the number
    # of rows visible in the left pane.
    # @see type
    # @return [Integer]
    # default 0
    attr_reader :y_split

    # @see active_pane
    def active_pane=(v)
      Axlsx.validate_pane_type(v)
      @active_pane = Axlsx.camel(v.to_s, false)
    end

    # @see state
    def state=(v)
      Axlsx.validate_split_state_type(v)
      @state = Axlsx.camel(v.to_s, false)
    end

    # @see top_left_cell
    def top_left_cell=(v)
      cell = (v.instance_of?(Axlsx::Cell) ? v.r_abs : v)
      Axlsx.validate_string(cell)
      @top_left_cell = cell
    end

    # @see x_split
    def x_split=(v); Axlsx.validate_unsigned_int(v); @x_split = v end

    # @see y_split
    def y_split=(v); Axlsx.validate_unsigned_int(v); @y_split = v end

    # Serializes the data validation
    # @param [String] str
    # @return [String]
    def to_xml_string(str = +'')
      finalize
      serialized_tag 'pane', str
    end

    private

    def finalize
      if @state == 'frozen' && @top_left_cell.nil?
        row = @y_split || 0
        column = @x_split || 0
        @top_left_cell = "#{('A'..'ZZ').to_a[column]}#{row + 1}"
      end
    end
  end
end