aboutsummaryrefslogtreecommitdiffstats
path: root/alv/copilot/wx.moon
blob: 9e2ad93cc746e6790105bcdd717c1372c8f77579 (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
----
-- wxLua Copilot GUI.
--
-- @classmod WXCopilot
import Logger, version from require 'alv'
import parse_args, Copilot from require 'alv.copilot.base'

require 'wx'
import
  wxID_ABOUT, wxID_OPEN, wxID_EXIT, wxID_ANY, wxVERTICAL,
  wxFrame, wxMenuBar, wxMenu, wxPanel, wxBoxSizer, wxTextCtrl, wxSplitterWindow
from wx

STARTSTOP = 100

class WXLogger extends Logger
  new: (level, @eval_ctrl, @run_ctrl) =>
    super level

  set_time: (time) =>
    @ctrl = switch time
      when 'eval' then @eval_ctrl
      when 'run' then @run_ctrl
      else error "invalid time '#{time}'"
 
  put: (message) =>
    @ctrl\AppendText message .. '\n'

class WXCopilot extends Copilot
  new: (arg) =>
    super parse_args arg

    @app = wx.wxGetApp!
    @app.VendorName = 'alive'
    @app.AppName = 'alive wxCopilot'

    fileMenu = wxMenu {
      { wxID_ABOUT, '&About',        'About alive wxCopilot' }
      { wxID_OPEN,  '&Open\tCtrl-O', 'Open Script'           }
      {                                                      }
      { wxID_EXIT,  'E&xit\tCtrl-Q', 'Exit Program'          }
    }
    runMenu = wxMenu {
      { STARTSTOP, '&Start/Stop\tCtrl-P', 'Start/Stop Script Execution' }
    }
    @menuBar = with wxMenuBar!
      \Append fileMenu, '&File'
      \Append runMenu, '&Run'

    @frame = wxFrame wx.NULL, wxID_ANY, @app\GetAppName!
    @frame\SetMenuBar @menuBar
    @status = @frame\CreateStatusBar 1

    @update_status!

    splitter = wxSplitterWindow @frame, wx.wxID_ANY

    eval, @eval_ctrl = @mkPanel splitter, 'eval-time messages'

    run, @run_ctrl = @mkPanel splitter, 'run-time messages'

    splitter\SetMinimumPaneSize 60
    splitter\SplitHorizontally eval, run, 0

    sizer = with wxBoxSizer wx.wxVERTICAL
      \Add splitter, 1, wx.wxEXPAND + wx.wxALL, 10

    @frame\SetAutoLayout true
    @frame\SetSizer sizer
    @frame\Show true

    @frame\Connect wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, @\do_about
    @frame\Connect wxID_OPEN, wx.wxEVT_COMMAND_MENU_SELECTED, @\do_open
    @frame\Connect wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, -> @frame\Close!
    @frame\Connect STARTSTOP, wx.wxEVT_COMMAND_MENU_SELECTED, @\do_startstop
    @frame\Connect wxID_ANY, wx.wxEVT_IDLE, @\do_idle

  do_about: =>
    wx.wxMessageBox "alive #{version.tag} wxCopilot.

built using #{wxlua.wxLUA_VERSION_STRING} on #{wx.wxVERSION_STRING}",
      "About wxCopilot",
      wx.wxOK + wx.wxICON_INFORMATION,
      @frame

  do_open: =>
    dialog = wx.wxFileDialog @frame, 'Change Script', '', '',
                             'Alive scripts (*.alv)|*.alv|All files (*)|*',
                             wx.wxFD_OPEN + wx.wxFD_FILE_MUST_EXIST

    if dialog\ShowModal! == wx.wxID_OK
      @paused = false
      @open dialog\GetPath!
      @update_status!

    dialog\Destroy!

  do_startstop: (event) =>
    if @file
      @paused = not @paused
      @update_status!

  do_idle: (event) =>
    if not @paused
      event\RequestMore true
      @tick!

  update_status: =>
    startstop = @menuBar\FindItem STARTSTOP
    if not @file
      @status\SetStatusText "No script loaded."
      startstop\Enable false
    else
      startstop\Enable true
      if @paused
        @status\SetStatusText "Paused."
      else
        @status\SetStatusText "Running."

  mkPanel: (parent, name) =>
    panel = wxPanel parent, wxID_ANY
    sizer = wxBoxSizer wxVERTICAL
    panel\SetSizer sizer

    sizer\Add wx.wxStaticText panel, wx.wxID_ANY, name
    log = wxTextCtrl panel, wxID_ANY, '', wx.wxDefaultPosition,
                     wx.wxDefaultSize, wx.wxTE_MULTILINE + wx.wxTE_READONLY
    sizer\Add log, 1, wx.wxEXPAND | wx.wxBOTTOM, 5

    panel, log

  run: =>
    WXLogger\init @args.log, copilot.eval_ctrl, copilot.run_ctrl
    @app\MainLoop!

{
  :WXCopilot
}