Method: CDK::SELECTION#initialize

Defined in:
lib/cdk/components/selection.rb

#initialize(cdkscreen, xplace, yplace, splace, height, width, title, list, list_size, choices, choice_count, highlight, box, shadow) ⇒ SELECTION

Returns a new instance of SELECTION.



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
# File 'lib/cdk/components/selection.rb', line 7

def initialize(cdkscreen, xplace, yplace, splace, height, width, title,
    list, list_size, choices, choice_count, highlight, box, shadow)
  super()
  widest_item = -1
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = width
  bindings = {
    CDK::BACKCHAR => Ncurses::KEY_PPAGE,
    CDK::FORCHAR  => Ncurses::KEY_NPAGE,
    'g'           => Ncurses::KEY_HOME,
    '1'           => Ncurses::KEY_HOME,
    'G'           => Ncurses::KEY_END,
    '<'           => Ncurses::KEY_HOME,
    '>'           => Ncurses::KEY_END,
  }

  if choice_count <= 0
    self.destroy
    return nil
  end

  @choice = []
  @choicelen = []

  self.setBox(box)

  # If the height is a negative value, the height will be ROWS-height,
  # otherwise the height will be the given height.
  box_height = CDK.setWidgetDimension(parent_height, height, 0)

  # If the width is a negative value, the width will be COLS-width,
  # otherwise the width will be the given width
  box_width = CDK.setWidgetDimension(parent_width, width, 0)
  box_width = self.setTitle(title, box_width)

  # Set the box height.
  if @title_lines > box_height
    box_height = @title_lines = [list_size, 8].min, + 2 * border_size
  end

  @maxchoicelen = 0

  # Adjust the box width if there is a scroll bar.
  if splace == CDK::LEFT || splace == CDK::RIGHT
    box_width += 1
    @scrollbar = true
  else
    @scrollbar = false
  end

  # Make sure we didn't extend beyond the dimensions of the window.
  @box_width = [box_width, parent_width].min
  @box_height = [box_height, parent_height].min

  self.setViewSize(list_size)

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Make the selection window.
  @win = Ncurses::WINDOW.new(@box_height, @box_width, ypos, xpos)

  # Is the window nil?
  if @win.nil?
    self.destroy
    return nil
  end

  # Turn the keypad on for this window.
  @win.keypad(true)

  # Create the scrollbar window.
  if splace == CDK::RIGHT
    @scrollbar_win = @win.subwin(self.maxViewSize, 1,
        self.SCREEN_YPOS(ypos), xpos + @box_width - @border_size - 1)
  elsif splace == CDK::LEFT
    @scrollbar_win = @win.subwin(self.maxViewSize, 1,
        self.SCREEN_YPOS(ypos), self.SCREEN_XPOS(ypos))
  else
    @scrollbar_win = nil
  end

  # Set the rest of the variables
  @screen = cdkscreen
  @parent = cdkscreen.window
  @scrollbar_placement = splace
  @max_left_char = 0
  @left_char = 0
  @highlight = highlight
  @choice_count = choice_count
  @accepts_focus = true
  @input_window = @win
  @shadow = shadow

  self.setCurrentItem(0)

  # Each choice has to be converted from string to chtype array
  (0...choice_count).each do |j|
    choicelen = []
    @choice << char2Chtype(choices[j], choicelen, [])
    @choicelen << choicelen[0]
    @maxchoicelen = [@maxchoicelen, choicelen[0]].max
  end

  # Each item in the needs to be converted to chtype array
  widest_item = self.createList(list, list_size)
  if widest_item > 0
    self.updateViewWidth(widest_item)
  elsif list_size > 0
    self.destroy
    return nil
  end

  # Do we need to create a shadow.
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  # Setup the key bindings
  bindings.each do |from, to|
    self.bind(:SELECTION, from, :getc, to)
  end

  # Register this baby.
  cdkscreen.register(:SELECTION, self)
end