summaryrefslogtreecommitdiff
path: root/lib/vdr/osd/menu.rb
blob: f266923681bea03a5e11821b4f8b031df30cfefd (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# vdr-scripting - A plugin for the Linux Video Disk Recorder
# Copyright (c) 2009 Tobias Grimm <vdr@e-tobi.net>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#

module Vdr
  module Osd
    #
    # The Menu class represents a basic OSD menu with a title
    # and a bunch of items.
    #
    class Menu < Vdr::Swig::COsdMenu
      # The title of the menu
      attr_reader :title

      # Creates a new menu, optionally with the given _title_
      def initialize(title=nil) # :yields: menu
        @title = title
        super(@title)
        @items = []
        @red_help = @green_help = @yellow_help = @blue_help = nil
        @keypress_event_handler = {}
        yield(self) if block_given?
      end

      # Sets the _title_ of the menu
      def title=(title)
        @title = title
        set_title(@title)
      end

      # Adds the Item _item_ to the menu
      def add_item(item)
        @items << item
        add(item)
      end

      # Item reference returning an Item by index
      def [](index)
        return @items[index]
      end

      # Opens the given _menu_ as a sub menu
      def open_sub_menu(menu)
        add_sub_menu(menu)
      end

      # Request to close the menu
      def close
        @close_request = :close
      end

      # Request to close all menus
      def close_all
        @close_request = :close_all
      end

      # Create and add a new item with the given _title_ and returns the added
      # Item
      def add_new_item(title)
        item = Item.new(title)
        add_item(item)
        return item
      end

      # Sets the help text for the red button
      def red_help=(text)
        @red_help = text
        update_help_texts
      end

      # Sets the help text for the green button
      def green_help=(text)
        @green_help = text
        update_help_texts
      end

      # Sets the help text for the yellow button
      def yellow_help=(text)
        @yellow_help = text
        update_help_texts
      end

      # Sets the help text for the blue button
      def blue_help=(text)
        @blue_help = text
        update_help_texts
      end
      
      #
      # Sets an event handler for the key press event. If _key_ is not
      # given, the event handler will be called for any key, otherwise
      # just for the specific key. In the latter case, no parameter is
      # passed to the event handler.
      # 
      #     menu.on_keypress do |key|
      #       case key
      #         when :key_red
      #           puts "Red key pressed"
      #         when :key_yellow
      #           puts "Yellow key pressed"
      #        end
      #     end
      #
      #     menu.on_keypress(:key_red) { puts 'Red key pressed' }
      #
      def on_keypress(key=:key_any, &event_handler) # :yield: key
        @keypress_event_handler[key] = event_handler
      end
      
      # Deletes all menu items
      def clear
        super
      end
      
      # Refresh the menu. This must be called, when the menu has been changed.
      def refresh
        display
      end
      
      protected

      def process_key(key) # :nodoc:
        state = super(key)
        case @close_request
        when :close
          return Vdr::Swig::OsBack
        when :close_all
          return Vdr::Swig::OsEnd
        end
        key_symbol = KEYMAP[key]
        if @keypress_event_handler[:key_any]
          @keypress_event_handler[:key_any].call(key_symbol)
        end
        if @keypress_event_handler[key_symbol]
          @keypress_event_handler[key_symbol].call
        end
        return state
      end
      
      private
      
      KEYMAP =
      {
        Vdr::Swig::KRed    => :key_red,
        Vdr::Swig::KGreen  => :key_green,
        Vdr::Swig::KYellow => :key_yellow,
        Vdr::Swig::KBlue   => :key_blue,
      }

      def update_help_texts
        set_help(@red_help, @green_help, @yellow_help, @blue_help)
      end
    end
  end
end