Class: Editor

Inherits:
Menu
  • Object
show all
Includes:
Dynamic
Defined in:
lib/editor.rb

Constant Summary collapse

@@name =
"Edit menu"

Instance Attribute Summary

Attributes inherited from Menu

#path, #style

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Menu

#encode_with, #filter_entries, #get_font_string, #init_with, #items, #remove_item, #set_item

Methods included from Item

#name=

Constructor Details

#initialize(parent) ⇒ Editor

Returns a new instance of Editor.



19
20
21
22
# File 'lib/editor.rb', line 19

def initialize parent
    @parent = parent
    self.style = parent.style
end

Class Method Details

.nameObject



11
12
13
# File 'lib/editor.rb', line 11

def self.name
    @@name
end

Instance Method Details

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/editor.rb', line 28

def execute
    entries = []
    selection = nil
    while selection != "" && (entries.index selection) != entries.length - 1
        entries = ["Name (#{@parent.name})"]
        entries << "Style"
        entries << "Entries"
        entries << "Run menu"
        entries << "Done"
        selection = show_menu entries, @@name
        case entries.index selection
        when 0 then @parent.name = show_menu([@parent.name], "Edit Name")
        when 1 then @parent.style = show_style_menu
        when 2 then show_entries_menu
        when 3 then show_run_menu_menu
        end
    end
    false
end

#nameObject



15
16
17
# File 'lib/editor.rb', line 15

def name
    "> #{Editor.name}"
end

#show_add_menuObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/editor.rb', line 165

def show_add_menu
    entries = []
    entries = ["Command"]
    entries << "Menu"
    entries << "Done"
    selection = nil
    while selection != "" && (entries.index selection) != entries.length - 1
        selection = show_menu entries, "Add items"
        case entries.index selection
        when 0 then show_edit_cmd_menu
        when 1 then show_edit_menu_menu 
        end
    end
end

#show_edit_cmd_menu(command = Command.new("New command", "")) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/editor.rb', line 180

def show_edit_cmd_menu command = Command.new("New command", "")
    new = false
    if (@parent.remove_item command).nil?
        new = true
    end
    entries = []
    selection = nil
    delete = false
    while selection != "" && (entries.index selection) != entries.length - 1
        entries = ["Name (#{command.name})"]
        entries << "Command (#{command.command})"
        if new
            string = "Add command"
        else
            string = "Edit command"
            if delete
                entries << "Undo"
            else
                entries << "Delete"
            end
        end
        entries << "Done"
        selection = show_menu entries, "Add Command"
        case entries.index selection
        when 0 
            new_name = show_menu([command.name], "Enter a name")
            command.name = new_name unless new_name.empty?
        when 1 
            history = History.new 1, 1
            history.update command.command unless command.command.nil? or command.command.empty?
            new_command = Run_Menu.new(self, history, "Enter a command").show_menu
            command.command = new_command unless new_command.empty?
        when 2 then delete = !delete unless new
        end
    end
    delete = true if selection == "" && new
    @parent.set_item command unless delete
end

#show_edit_menuObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/editor.rb', line 150

def show_edit_menu
    selection = nil
    while selection != "" && @parent.items[selection].nil?
        selection = show_menu @parent.items.keys.sort, "Select item to edit"
        item = @parent.items[selection]
        unless item.nil?
            if item.is_a? Command
                show_edit_cmd_menu item
            else
                show_edit_menu_menu item
            end
        end
    end
end

#show_edit_menu_menu(menu = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/editor.rb', line 219

def show_edit_menu_menu menu = nil
    new = menu.nil?
    if new
        menu = Menu.new "New menu"
        menu.style = @parent.style
    end
    @parent.remove_item menu
    entries = []
    selection = nil
    delete = false
    while selection != "" && (entries.index selection) != entries.length - 1
        entries = ["Name (#{menu.name})"]
        if new
            string = "Add menu"
        else
            string = "Edit menu"
            if delete
                entries << "Undo"
            else
                entries << "Remove"
            end
        end
        entries << "Done"
        selection = show_menu entries, string
        if (entries.index selection) == 0
            new_name = show_menu [], "Enter a name"
            menu.name = new_name unless new_name.empty?
        end
        delete = !delete if (entries.index selection) == 1 && !new
    end
    delete = true if selection == "" && new
    @parent.set_item menu unless delete
end

#show_entries_menuObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/editor.rb', line 136

def show_entries_menu
    entries = ["Edit/Remove entries"]
    entries << "Add entries"
    entries << "Done"
    selection = nil
    while selection != "" && (entries.index selection) != 2
        selection = show_menu entries, "Edit entries"
        case entries.index selection
        when 0 then show_edit_menu
        when 1 then show_add_menu
        end
    end
end

#show_history_menuObject



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
# File 'lib/editor.rb', line 67

def show_history_menu
    entries = []
    selection = nil
    clear = false
    while selection != "" && (entries.index selection) != entries.length - 1
        entries = ["History length (#{@parent.history.length})"]
        entries << "Items shown in run menu (#{@parent.history.show_num_items})"
        if clear
            entries << "Undo"
        else
            entries << "Clear history"
        end
        entries << "Done"

        selection = show_menu entries, "History"
        case entries.index selection
        when 0
            selection = (show_menu [], "Enter a number")
            number = selection.to_i
            @parent.history.length = number unless number < 1 || number.to_s != selection
        when 1
            selection = (show_menu [], "Enter a number")
            number = (show_menu [], "Enter a number").to_i
            @parent.history.show_num_items = number unless number.to_s != selection
        when 2 then clear = !clear
        when (entries.length - 1) then @parent.history.clear if clear
        end
    end
end

#show_menu(entries, name, show_edit_menue = false) ⇒ Object



132
133
134
# File 'lib/editor.rb', line 132

def show_menu entries, name, show_edit_menue = false
    super
end

#show_run_menu_menuObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/editor.rb', line 48

def show_run_menu_menu
    entries = []
    selection = nil
    while selection != "" && (entries.index selection) != entries.length - 1
        if @parent.run_menu?
            entries = ["Disable run menu"]
        else
            entries = ["Enable run menu"]
        end
        entries << "History"
        entries << "Done"
        selection = show_menu entries, "Run menu"
        case entries.index selection
        when 0 then @parent.run_menu = !@parent.run_menu?
        when 1 then show_history_menu
        end
    end
end

#show_style_menuObject



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
# File 'lib/editor.rb', line 97

def show_style_menu
    style = @parent.style
    entries = []
    selection = nil
    while selection != "" && (entries.index selection) != entries.length - 1
        entries = ["Background:          (#{style.color :bg})"]
        entries << "Foreground:          (#{style.color :fg})"
        entries << "Selected Background: (#{style.color :bg_hi})"
        entries << "Selected Foreground: (#{style.color :fg_hi})"
        if style.font.nil?
            entries << "Font:                (default)"
        else
            entries << "Font:                (#{style.font})"
        end
        entries << "Done"
        selection = show_menu entries, "Edit style"
        colors = [:bg, :fg, :bg_hi, :fg_hi]
        unless (entries.index selection).nil?
            if (entries.index selection) < 4
                style.set_color colors[entries.index selection], show_menu([style.color(colors[entries.index selection])], "Enter a color")
            elsif (entries.index selection) < 5
                if style.font.nil?
                    selection = show_menu(["default"], "Set font")
                else
                    selection = show_menu([style.font], "Set font")
                end
                unless selection.nil? || selection == "default"
                    style.font = selection
                end
            end
        end
    end
    style
end

#to_sObject



24
25
26
# File 'lib/editor.rb', line 24

def to_s
    "Edit menu"
end