Method: CDK::VIEWER#initialize

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

#initialize(cdkscreen, xplace, yplace, height, width, buttons, button_count, button_highlight, box, shadow) ⇒ VIEWER

Returns a new instance of VIEWER.



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

def initialize(cdkscreen, xplace, yplace, height, width,
    buttons, button_count, button_highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = width
  box_height = height
  button_width = 0
  button_adj = 0
  button_pos = 1
  bindings = {
      CDK::BACKCHAR => Ncurses::KEY_PPAGE,
      'b'           => Ncurses::KEY_PPAGE,
      'B'           => Ncurses::KEY_PPAGE,
      CDK::FORCHAR  => Ncurses::KEY_NPAGE,
      ' '           => Ncurses::KEY_NPAGE,
      'f'           => Ncurses::KEY_NPAGE,
      'F'           => Ncurses::KEY_NPAGE,
      '|'           => Ncurses::KEY_HOME,
      '$'           => Ncurses::KEY_END,
  }

  self.setBox(box)

  box_height = CDK.setWidgetDimension(parent_height, height, 0)
  box_width = CDK.setWidgetDimension(parent_width, width, 0)

  # 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 viewer window.
  @win= Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  if @win.nil?
    self.destroy
    return nil
  end

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

  # Create the buttons.
  @button_count = button_count
  @button = []
  @button_len = []
  @button_pos = []
  if button_count > 0
    (0...button_count).each do |x|
      button_len = []
      @button << char2Chtype(buttons[x], button_len, [])
      @button_len << button_len[0]
      button_width += @button_len[x] + 1
    end
    button_adj = (box_width - button_width) / (button_count + 1)
    button_pos = 1 + button_adj
    (0...button_count).each do |x|
      @button_pos << button_pos
      button_pos += button_adj + @button_len[x]
    end
  end

  # Set the rest of the variables.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @shadow_win = nil
  @button_highlight = button_highlight
  @box_height = box_height
  @box_width = box_width - 2
  @view_size = height - 2
  @input_window = @win
  @shadow = shadow
  @current_button = 0
  @current_top = 0
  @length = 0
  @left_char = 0
  @max_left_char = 0
  @max_top_line = 0
  @characters = 0
  @list_size = -1
  @show_line_info = 1
  @exit_type = :EARLY_EXIT

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

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

  cdkscreen.register(:VIEWER, self)
end