annotate gui-main.cpp @ 5:54edd85237ab

use signal to send input to qt interpreter object
author John W. Eaton <jwe@octave.org>
date Wed, 22 May 2019 18:07:37 -0400
parents 0e154787183d
children 04867eba6428
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 #include <iostream>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 #include <sstream>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 #include <string>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 #include <cstdlib>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 #include <cstring>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 #include <QApplication>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 #include <QKeyEvent>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 #include <QTextDocument>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 #include <QTextEdit>
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
12 #include <QTimer>
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 #include "gui-main.h"
3
52c033864347 add build instructions to NOTES file
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
15 #include "main.h"
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 #include "gui-main.h"
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
18 #include "interpreter.h"
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
19 #include "parser.h"
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 #include <readline/readline.h>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 #include <readline/history.h>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 namespace gui
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 {
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
26 static int available_char = 0;
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
27
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
28 static command_window *calc_interaction_window = 0;
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 static inline int ctrl (int c)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 return c & 0x1f;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
35 static int getc (FILE *)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
36 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
37 int tmp = available_char;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
38 available_char = 0;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
39 return tmp;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
40 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
41
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
42 static void redisplay (void)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
43 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
44 if (calc_interaction_window)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
45 calc_interaction_window->redisplay ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
46 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
47
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
48 static void prep_term (int)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
49 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
50 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
51
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
52 static void deprep_term (void)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
53 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
54 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
55
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
56 static void accept_line (char *line)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
57 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
58 if (calc_interaction_window)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
59 calc_interaction_window->accept_line (line ? line : "");
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
60 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
61
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
62 static void display_completion_matches (char **matches, int num_matches,
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
63 int /* max_length */)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
64 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
65 if (calc_interaction_window)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
66 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
67 std::ostringstream buf;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
68
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
69 if (num_matches > 1)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
70 buf << "\n";
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
71
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
72 for (int i = 1; i < num_matches; i++)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
73 buf << matches[i] << "\n";
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
74
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
75 calc_interaction_window->insert_at_end (buf.str ());
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
76
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
77 calc_interaction_window->redisplay ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
78 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
79 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
80
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
81 static void readline_init (void)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
82 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
83 rl_initialize ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
84
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
85 rl_getc_function = getc;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
86 rl_redisplay_function = redisplay;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
87 rl_prep_term_function = prep_term;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
88 rl_deprep_term_function = deprep_term;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
89 rl_completion_display_matches_hook = display_completion_matches;
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
90
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
91 rl_callback_handler_install (">> ", accept_line);
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
92 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
93
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
94 static void readline_fini (void)
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
95 {
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
96 rl_callback_handler_remove ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
97 }
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
98
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 command_window::command_window (QWidget *p)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 : QTextEdit (p),
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 m_buffer (new QTextDocument ()),
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
102 m_interpreter (new calc::qt_interpreter ()),
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 beg_mark (), prompt_mark ()
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 setWindowTitle ("Qt::TextEdit example");
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 setMinimumSize (QSize (600, 400));
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 setDocument (m_buffer);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 connect (this, SIGNAL (result_available (double)),
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 this, SLOT (handle_result (double)));
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
114 connect (this, SIGNAL (input_char_available (int)),
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
115 this, SLOT (handle_input_char (int)));
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
116
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
117 connect (this, SIGNAL (accept_line_signal (const QString&)),
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
118 m_interpreter, SLOT (accept_input_line (const QString&)));
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
119
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 insert_at_end
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 ("Qt Example Calculator.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 "Available operations: + - * / ^ ()\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123 "Semicolon terminates statement.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124 "Up Arrow key moves to previous line in the command history.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
125 "Down Arrow key moves to next line in the comand history.\n\n");
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
126
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127 beg_mark = set_mark ();
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
128
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
129 // Defer initializing and executing the interpreter until after the main
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
130 // window and QApplication are running to prevent race conditions
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
131 QTimer::singleShot (0, m_interpreter, SLOT (execute (void)));
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
132 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
133
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
134 // Accept an input line, parse and possibly execute it.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
136 void command_window::accept_line (const std::string& line)
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
137 {
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
138 if (calc::debug_mode)
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
139 std::cerr << "accept: " << line << std::endl;
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
140
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
141 insert_at_end ("\n");
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
142
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
143 if (! line.empty ())
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
144 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 add_history (line.c_str ());
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
146 using_history ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
147
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
148 emit accept_line_signal (QString::fromStdString (line));
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
149 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
150 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
151
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
152 // Redisplay current command line.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
153
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
154 void command_window::redisplay (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
155 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
156 erase_line ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
157
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
158 std::string line = rl_line_buffer ? rl_line_buffer : "";
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 3
diff changeset
159 std::string prompt = (rl_prompt && parser::beg_of_stmt) ? rl_prompt : "";
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
160
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
161 insert_line (prompt, line);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
162
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
163 scroll_to_bottom ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
164
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
165 QTextCursor cursor = textCursor ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
166
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
167 cursor.setPosition (prompt_mark + rl_point, QTextCursor::MoveAnchor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
168
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
169 setTextCursor (cursor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
170 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
171
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
172 void command_window::insert_at_end (const std::string& text)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
173 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
174 scroll_to_bottom ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
175
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
176 insert_at_cursor (text);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
177 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
178
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
179 void command_window::emit_error (const std::string& msg)
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
180 {
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
181 insert_at_end ("parse error: " + msg);
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
182
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
183 rl_abort (0, 0);
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
184 }
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
185
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
186 void command_window::emit_result (double value)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
187 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
188 emit result_available (value);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
189 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
190
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
191 // FIXME: do we really need this extra function?
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
192 void command_window::handle_result (double value)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
193 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
194 insert_result (value);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
195 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
196
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
197 void command_window::keyPressEvent (QKeyEvent *event)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
198 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
199 if (! event)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
200 return;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
201
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
202 if (event->type () == QEvent::KeyPress)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
203 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
204 int key = event->key ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
205
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
206 switch (key)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
207 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
208 case Qt::Key_Return:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
209 key = 0x0A;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
210 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
211
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
212 case Qt::Key_Backspace:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
213 key = 0x08;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
214 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
215
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
216 case Qt::Key_Tab:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
217 key = 0x09;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
218 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
219
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
220 case Qt::Key_Escape:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
221 key = 0x1b;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
222 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
223
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
224 case Qt::Key_Up:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
225 case Qt::Key_Down:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
226 case Qt::Key_Right:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
227 case Qt::Key_Left:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
228 key = do_arrow_key (key);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
229 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
230
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
231 default:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
232 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
233 switch (event->modifiers ())
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
234 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
235 case Qt::ControlModifier:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
236 if (key > 0x3f && key < 0x7b)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
237 key &= 0x1f;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
238 else
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
239 key = -1;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
240 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
241
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
242 default:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
243 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
244 // Don't shoot me, this is just a demo...
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
245 QString text = event->text ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
246 QByteArray latin_text = text.toLatin1 ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
247 key = latin_text[0];
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
248 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
249 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
250 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
251 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
252 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
253 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
254
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
255 if (key >= 0)
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
256 emit input_char_available (key);
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
257 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
258 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
259
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
260 void command_window::handle_input_char (int key)
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
261 {
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
262 available_char = key;
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
263 rl_callback_read_char ();
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
264 }
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
265
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
266 int command_window::do_arrow_key (int arrow_key)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
267 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
268 int retval = 0;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
269
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
270 available_char = 0x1b;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
271 rl_callback_read_char ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
272
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
273 available_char = '[';
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
274 rl_callback_read_char ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
275
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
276 switch (arrow_key)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
277 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
278 case Qt::Key_Up:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
279 retval = 'A';
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
280 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
281
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
282 case Qt::Key_Down:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
283 retval = 'B';
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
284 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
285
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
286 case Qt::Key_Right:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
287 retval = 'C';
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
288 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
289
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
290 case Qt::Key_Left:
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
291 retval = 'D';
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
292 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
293 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
294
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
295 return retval;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
296 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
297
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
298 // Retrieve a command from the history list and insert it on the
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
299 // current command.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
300
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
301 void command_window::history (bool up)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
302 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
303 HIST_ENTRY *entry = up ? previous_history () : next_history ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
304
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
305 if (entry)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
306 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
307 erase_line ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
308
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
309 std::string prompt = rl_prompt ? rl_prompt : "";
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
310
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
311 insert_line (prompt, entry->line);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
312 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
313 else if (! up)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
314 erase_line ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
315
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
316 scroll_to_bottom ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
317 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
318
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
319 void command_window::erase_line (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
320 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
321 QTextCursor cursor = textCursor ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
322
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
323 cursor.movePosition (QTextCursor::End);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
324 cursor.select (QTextCursor::LineUnderCursor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
325 cursor.removeSelectedText ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
326
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
327 setTextCursor (cursor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
328 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
329
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
330 void command_window::insert_at_cursor (const std::string& text)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
331 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
332 QTextCursor cursor = textCursor ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
333
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
334 cursor.insertText (QString::fromStdString (text));
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
335
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
336 setTextCursor (cursor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
337 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
338
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
339 void command_window::insert_line (const std::string& prompt,
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
340 const std::string& line)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
341 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
342 beg_mark = set_mark ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
343
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
344 insert_at_cursor (prompt);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
345
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
346 prompt_mark = set_mark ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
347
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
348 insert_at_cursor (line);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
349 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
350
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
351 int command_window::set_mark (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
352 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
353 return textCursor ().position ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
354 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
355
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
356 void command_window::insert_result (double value)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
357 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
358 std::ostringstream buf;
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
359
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
360 buf << "ans = " << value << "\n";
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
361
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
362 insert_at_cursor (buf.str ());
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
363
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
364 beg_mark = set_mark ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
365
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
366 scroll_to_bottom ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
367 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
368
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
369 void command_window::scroll_to_bottom (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
370 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
371 QTextCursor cursor = textCursor ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
372
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
373 cursor.movePosition (QTextCursor::End);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
374
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
375 setTextCursor (cursor);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
376 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
377
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
378 int main (int argc, char *argv[])
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
379 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
380 QApplication app (argc, argv);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
381
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
382 calc_interaction_window = new command_window ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
383
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
384 calc_interaction_window->show ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
385
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
386 readline_init ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
387
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
388 int status = app.exec ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
389
5
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
390 readline_fini ();
54edd85237ab use signal to send input to qt interpreter object
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
391
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
392 return status;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
393 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
394
1
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
395 void emit_error (const std::string& msg)
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
396 {
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
397 calc_interaction_window->emit_error (msg);
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
398 }
08df60a01bc1 debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents: 0
diff changeset
399
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
400 void emit_result (double value)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
401 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
402 calc_interaction_window->emit_result (value);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
403 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
404 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
405
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
406 // -- Variable: rl_getc_func_t * rl_getc_function
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
407 // If non-zero, Readline will call indirectly through this pointer to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
408 // get a character from the input stream. By default, it is set to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
409 // `rl_getc', the default Readline character input function (*note
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
410 // Character Input::).
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
411
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
412 // -- Variable: rl_voidfunc_t * rl_redisplay_function
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
413 // If non-zero, Readline will call indirectly through this pointer to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
414 // update the display with the current contents of the editing buffer.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
415 // By default, it is set to `rl_redisplay', the default Readline
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
416 // redisplay function (*note Redisplay::).
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
417
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
418 // -- Variable: rl_vintfunc_t * rl_prep_term_function
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
419 // If non-zero, Readline will call indirectly through this pointer to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
420 // initialize the terminal. The function takes a single argument, an
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
421 // `int' flag that says whether or not to use eight-bit characters.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
422 // By default, this is set to `rl_prep_terminal' (*note Terminal
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
423 // Management::).
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
424
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
425 // -- Variable: rl_voidfunc_t * rl_deprep_term_function
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
426 // If non-zero, Readline will call indirectly through this pointer to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
427 // reset the terminal. This function should undo the effects of
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
428 // `rl_prep_term_function'. By default, this is set to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
429 // `rl_deprep_terminal' (*note Terminal Management::).
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
430
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
431 // -- Function: void rl_callback_handler_install (const char *prompt,
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
432 // rl_vcpfunc_t *lhandler)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
433 // Set up the terminal for readline I/O and display the initial
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
434 // expanded value of PROMPT. Save the value of LHANDLER to use as a
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
435 // function to call when a complete line of input has been entered.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
436 // The function takes the text of the line as an argument.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
437
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
438 // -- Function: void rl_callback_read_char (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
439 // Whenever an application determines that keyboard input is
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
440 // available, it should call `rl_callback_read_char()', which will
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
441 // read the next character from the current input source. If that
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
442 // character completes the line, `rl_callback_read_char' will invoke
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
443 // the LHANDLER function saved by `rl_callback_handler_install' to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
444 // process the line. Before calling the LHANDLER function, the
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
445 // terminal settings are reset to the values they had before calling
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
446 // `rl_callback_handler_install'. If the LHANDLER function returns,
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
447 // the terminal settings are modified for Readline's use again.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
448 // `EOF' is indicated by calling LHANDLER with a `NULL' line.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
449
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
450 // -- Function: void rl_callback_handler_remove (void)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
451 // Restore the terminal to its initial state and remove the line
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
452 // handler. This may be called from within a callback as well as
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
453 // independently. If the LHANDLER installed by
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
454 // `rl_callback_handler_install' does not exit the program, either
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
455 // this function or the function referred to by the value of
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
456 // `rl_deprep_term_function' should be called before the program
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
457 // exits to reset the terminal settings.
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
458
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
459 // -- Variable: rl_compdisp_func_t * rl_completion_display_matches_hook
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
460 // If non-zero, then this is the address of a function to call when
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
461 // completing a word would normally display the list of possible
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
462 // matches. This function is called in lieu of Readline displaying
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
463 // the list. It takes three arguments: (`char **'MATCHES, `int'
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
464 // NUM_MATCHES, `int' MAX_LENGTH) where MATCHES is the array of
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
465 // matching strings, NUM_MATCHES is the number of strings in that
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
466 // array, and MAX_LENGTH is the length of the longest string in that
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
467 // array. Readline provides a convenience function,
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
468 // `rl_display_match_list', that takes care of doing the display to
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
469 // Readline's output stream. That function may be called from this
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
470 // hook.