summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/sample/demos-jp/rmt
blob: dcfb328fc856e313e42248edce5d3baff6741f8b (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
172
173
174
175
176
177
178
179
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
218
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env ruby

# rmt --
# This script implements a simple remote-control mechanism for
# Tk applications.  It allows you to select an application and
# then type commands to that application.

require 'tk'

class Rmt
  def initialize(parent=nil)
    win = self

    unless parent
      parent = TkRoot.new
    end
    root = TkWinfo.toplevel(parent)
    root.minsize(1,1)

    # The instance variable below keeps track of the remote application
    # that we're sending to.  If it's an empty string then we execute
    # the commands locally.
    @app = 'local'
    @mode = 'Ruby'

    # The instance variable below keeps track of whether we're in the
    # middle of executing a command entered via the text.
    @executing = 0

    # The instance variable below keeps track of the last command executed,
    # so it can be re-executed in response to !! commands.
    @lastCommand = ""

    # Create menu bar.  Arrange to recreate all the information in the
    # applications sub-menu whenever it is cascaded to.

    TkFrame.new(root, 'relief'=>'raised', 'bd'=>2) {|f|
      pack('side'=>'top', 'fill'=>'x')
      TkMenubutton.new(f, 'text'=>'File', 'underline'=>0) {|mb|
        TkMenu.new(mb) {|mf|
          mb.menu(mf)
          TkMenu.new(mf) {|ma|
            postcommand proc{win.fillAppsMenu ma}
            mf.add('cascade', 'label'=>'Select Application',
                   'menu'=>ma, 'underline'=>0)
          }
          add('command', 'label'=>'Quit',
              'command'=>proc{root.destroy}, 'underline'=>0)
        }
        pack('side'=>'left')
      }
    }

    # Create text window and scrollbar.

    @txt = TkText.new(root, 'relief'=>'sunken', 'bd'=>2, 'setgrid'=>true) {
      yscrollbar(TkScrollbar.new(root){pack('side'=>'right', 'fill'=>'y')})
      pack('side'=>'left')
    }

    @promptEnd = TkTextMark.new(@txt, 'insert')

    # Create a binding to forward commands to the target application,
    # plus modify many of the built-in bindings so that only information
    # in the current command can be deleted (can still set the cursor
    # earlier in the text and select and insert;  just can't delete).

    @txt.bindtags([@txt, TkText, root, 'all'])
    @txt.bind('Return', proc{
                @txt.set_insert('end - 1c')
                @txt.insert('insert', "\n")
                win.invoke
                Tk.callback_break
              })
    @txt.bind('Delete', proc{
                begin
                  @txt.tag_remove('sel', 'sel.first', @promptEnd)
                rescue
                end
                if @txt.tag_nextrange('sel', '1.0', 'end') == []
                  if @txt.compare('insert', '<', @promptEnd)
                    Tk.callback_break
                  end
                end
              })
    @txt.bind('BackSpace', proc{
                begin
                  @txt.tag_remove('sel', 'sel.first', @promptEnd)
                rescue
                end
                if @txt.tag_nextrange('sel', '1.0', 'end') == []
                  if @txt.compare('insert', '<', @promptEnd)
                    Tk.callback_break
                  end
                end
              })
    @txt.bind('Control-d', proc{
                if @txt.compare('insert', '<', @promptEnd)
                  Tk.callback_break
                end
              })
    @txt.bind('Control-k', proc{
                if @txt.compare('insert', '<', @promptEnd)
                  @txt.set_insert(@promptEnd)
                end
              })
    @txt.bind('Control-t', proc{
                if @txt.compare('insert', '<', @promptEnd)
                  Tk.callback_break
                end
              })
    @txt.bind('Meta-d', proc{
                if @txt.compare('insert', '<', @promptEnd)
                  Tk.callback_break
                end
              })
    @txt.bind('Meta-BackSpace', proc{
                if @txt.compare('insert', '<=', @promptEnd)
                  Tk.callback_break
                end
              })
    @txt.bind('Control-h', proc{
                if @txt.compare('insert', '<=', @promptEnd)
                  Tk.callback_break
                end
              })

    @txt.tag_configure('bold', 'font'=>['Courier', 12, 'bold'])

    @app = Tk.appname('rmt')
    if (@app =~ /^rmt(.*)$/)
      root.title("Tk Remote Controller#{$1}")
      root.iconname("Tk Remote#{$1}")
    end
    prompt
    @txt.focus
    #@app = TkWinfo.appname(TkRoot.new)
  end

  def tkTextInsert(w,s)
    return if s == ""
    begin
      if w.compare('sel.first','<=','insert') \
        && w.compare('sel.last','>=','insert')
        w.tag_remove('sel', 'sel.first', @promptEnd)
        w.delete('sel.first', 'sel.last')
      end
    rescue
    end
    w.insert('insert', s)
    w.see('insert')
  end

  # The method below is used to print out a prompt at the
  # insertion point (which should be at the beginning of a line
  # right now).

  def prompt
    @txt.insert('insert', "#{@app}: ")
    @promptEnd.set('insert')
    @promptEnd.gravity = 'left'
    @txt.tag_add('bold', "#{@promptEnd.path} linestart", @promptEnd)
  end

  # The method below executes a command (it takes everything on the
  # current line after the prompt and either sends it to the remote
  # application or executes it locally, depending on "app".

  def invoke
    cmd = @txt.get(@promptEnd, 'insert')
    @executing += 1
    case (@mode)
    when 'Tcl'
      if Tk.info('complete', cmd)
        if (cmd == "!!\n")
          cmd = @lastCommand
        else
          @lastCommand = cmd
        end
        begin
          msg = Tk.appsend(@app, false, cmd)
        rescue
          msg = "Error: #{$!}"
        end
        @txt.insert('insert', msg + "\n") if msg != ""
        prompt
        @promptEnd.set('insert')
      end

    when 'Ruby'
      if (cmd == "!!\n")
        cmd = @lastCommand
      end
      complete = true
      begin
        eval("proc{#{cmd}}")
      rescue
        complete = false
      end
      if complete
        @lastCommand = cmd
        begin
#         msg = Tk.appsend(@app, false,
#                          'ruby',
#                          '"(' + cmd.gsub(/[][$"]/, '\\\\\&') + ').to_s"')
          msg = Tk.rb_appsend(@app, false, cmd)
        rescue
          msg = "Error: #{$!}"
        end
        @txt.insert('insert', msg + "\n") if msg != ""
        prompt
        @promptEnd.set('insert')
      end
    end

    @executing -= 1
    @txt.yview_pickplace('insert')
  end

  # The following method is invoked to change the application that
  # we're talking to.  It also updates the prompt for the current
  # command, unless we're in the middle of executing a command from
  # the text item (in which case a new prompt is about to be output
  # so there's no need to change the old one).

  def newApp(appName, mode)
    @app = appName
    @mode = mode
    if @executing == 0
      @promptEnd.gravity = 'right'
      @txt.delete("#{@promptEnd.path} linestart", @promptEnd)
      @txt.insert(@promptEnd, "#{appName}: ")
      @txt.tag_add('bold', "#{@promptEnd.path} linestart", @promptEnd)
      @promptEnd.gravity = 'left'
    end
  end

  # The method below will fill in the applications sub-menu with a list
  # of all the applications that currently exist.

  def fillAppsMenu(menu)
    win = self
    begin
      menu.delete(0,'last')
    rescue
    end
    TkWinfo.interps.sort.each{|ip|
      begin
        if Tk.appsend(ip, false, 'info commands ruby') == ""
          mode = 'Tcl'
        else
          mode = 'Ruby'
        end
        menu.add('command', 'label'=>format("%s    (#{mode}/Tk)", ip),
                 'command'=>proc{win.newApp ip, mode})
      rescue
        menu.add('command', 'label'=>format("%s (unknown Tk)", ip),
                 'command'=>proc{win.newApp ip, mode}, 'state'=>'disabled')
      end
    }
    menu.add('command', 'label'=>format("local    (Ruby/Tk)"),
             'command'=>proc{win.newApp 'local', 'Ruby'})
  end
end

Rmt.new

Tk.mainloop