Method: CDK::RADIO#initialize

Defined in:
lib/cdk/radio.rb

#initialize(cdkscreen, xplace, yplace, splace, height, width, title, list, list_size, choice_char, def_item, highlight, box, shadow) ⇒ RADIO

Returns a new instance of RADIO.



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

def initialize(cdkscreen, xplace, yplace, splace, height, width, title,
    list, list_size, choice_char, def_item, highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = width
  box_height = height
  widest_item = 0

  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,
  }
  
  self.setBox(box)

  # If the height is a negative value, 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, 5)

  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

  # 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)

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

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

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

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

  # Turn on the keypad.
  @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(xpos))
  else
    @scrollbar_win = nil
  end

  # Set the rest of the variables
  @screen = cdkscreen
  @parent = cdkscreen.window
  @scrollbar_placement = splace
  @widest_item = widest_item
  @left_char = 0
  @selected_item = 0
  @highlight = highlight
  @choice_char = choice_char.ord
  @left_box_char = '['.ord
  @right_box_char = ']'.ord
  @def_item = def_item
  @input_window = @win
  @accepts_focus = true
  @shadow = shadow

  self.setCurrentItem(0)

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

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

  cdkscreen.register(:RADIO, self)
end