1 | /* pmtty.cc
|
---|
2 | Copyright (c) 1996 Eberhard Mattes
|
---|
3 |
|
---|
4 | This file is part of pmgdb.
|
---|
5 |
|
---|
6 | pmgdb is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | pmgdb is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with pmgdb; see the file COPYING. If not, write to
|
---|
18 | the Free Software Foundation, 59 Temple Place - Suite 330,
|
---|
19 | Boston, MA 02111-1307, USA. */
|
---|
20 |
|
---|
21 |
|
---|
22 | // TODO: jump to end on input
|
---|
23 |
|
---|
24 | #define INCL_DOS
|
---|
25 | #define INCL_WIN
|
---|
26 | #include <os2.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include "string.h"
|
---|
30 | #include "pmapp.h"
|
---|
31 | #include "pmframe.h"
|
---|
32 | #include "pmtxt.h"
|
---|
33 | #include "pmtty.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | #define IDT_CURSOR 1
|
---|
37 |
|
---|
38 | pmtty::pmtty (pmapp *in_app, unsigned in_id, ULONG frame_flags,
|
---|
39 | const SWP *pswp, const char *fontnamesize)
|
---|
40 | : pmtxt (in_app, in_id, frame_flags, pswp, fontnamesize)
|
---|
41 | {
|
---|
42 | x = 0; y = 0;
|
---|
43 | line_width = max_line_len;
|
---|
44 | cursor_enabled = false; cursor_painted = false; cursor_hidden = 0;
|
---|
45 | sel_attr = get_default_attr ();
|
---|
46 | create_hpsCursor ();
|
---|
47 | cursor_width = (int)WinQuerySysValue (HWND_DESKTOP, SV_CXBORDER);
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | pmtty::~pmtty ()
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | void pmtty::set_line_width (int width)
|
---|
57 | {
|
---|
58 | if (width > max_line_len)
|
---|
59 | width = max_line_len;
|
---|
60 | if (x >= width)
|
---|
61 | puts ("\n", 1);
|
---|
62 | line_width = width;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void pmtty::create_hpsCursor ()
|
---|
67 | {
|
---|
68 | hpsCursor = WinGetPS (get_hwndClient ());
|
---|
69 | set_font (hpsCursor);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | // Roll our own cursor because a cursor created with WinCreateCursor
|
---|
75 | // can be hidden and destroyed only by the thread which created it.
|
---|
76 |
|
---|
77 | void pmtty::paint_cursor ()
|
---|
78 | {
|
---|
79 | POINTL ptl;
|
---|
80 |
|
---|
81 | if (get_bbox_pos (hpsCursor, &ptl, y, x))
|
---|
82 | {
|
---|
83 | // TODO: Nicer cursor (WinDrawPointer?)
|
---|
84 | rclCursor.xLeft = ptl.x;
|
---|
85 | rclCursor.xRight = rclCursor.xLeft + cursor_width;
|
---|
86 | rclCursor.yBottom = ptl.y;
|
---|
87 | rclCursor.yTop = rclCursor.yBottom + get_cyChar ();
|
---|
88 | WinInvertRect (hpsCursor, &rclCursor);
|
---|
89 | cursor_painted = true;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | void pmtty::remove_cursor ()
|
---|
95 | {
|
---|
96 | WinInvertRect (hpsCursor, &rclCursor);
|
---|
97 | cursor_painted = false;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | inline void pmtty::cursor_on ()
|
---|
102 | {
|
---|
103 | if (--cursor_hidden == 0 && cursor_enabled && !cursor_painted
|
---|
104 | && WinQueryFocus (HWND_DESKTOP) == get_hwndClient ())
|
---|
105 | paint_cursor ();
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | inline void pmtty::cursor_off ()
|
---|
110 | {
|
---|
111 | if (++cursor_hidden == 1 && cursor_enabled && cursor_painted)
|
---|
112 | remove_cursor ();
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | void pmtty::puts1 (const char *s, size_t len)
|
---|
117 | {
|
---|
118 | while (x + (int)len >= line_width)
|
---|
119 | {
|
---|
120 | size_t n = line_width - x;
|
---|
121 | put (y, x, n, s, sel_attr, true);
|
---|
122 | x = 0; ++y;
|
---|
123 | s += n; len -= n;
|
---|
124 | }
|
---|
125 | if (len != 0)
|
---|
126 | {
|
---|
127 | put (y, x, len, s, sel_attr, true);
|
---|
128 | x += len;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void pmtty::puts (const char *s, size_t len)
|
---|
134 | {
|
---|
135 | if (len == 0)
|
---|
136 | return;
|
---|
137 | cursor_off ();
|
---|
138 | while (len != 0)
|
---|
139 | {
|
---|
140 | size_t i;
|
---|
141 | for (i = 0; i < len; ++i)
|
---|
142 | if (s[i] == '\n' || s[i] == '\t')
|
---|
143 | break;
|
---|
144 | if (i >= len)
|
---|
145 | {
|
---|
146 | puts1 (s, len);
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | if (i != 0)
|
---|
150 | puts1 (s, i);
|
---|
151 | if (s[i] == '\t')
|
---|
152 | {
|
---|
153 | int tab_width = (x | 7) + 1 - x;
|
---|
154 | puts1 (" ", tab_width);
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | x = 0; ++y;
|
---|
159 | }
|
---|
160 | len -= i + 1; s += i + 1;
|
---|
161 | }
|
---|
162 | if (x == 0)
|
---|
163 | put (y, x, 0, "", sel_attr, true);
|
---|
164 | // TODO: Optionally scroll horizontally/vertically to make cursor visible
|
---|
165 | cursor_on ();
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | class pmtty_const_str
|
---|
170 | {
|
---|
171 | public:
|
---|
172 | char *str;
|
---|
173 | pmtty_const_str (char c, size_t len)
|
---|
174 | {
|
---|
175 | str = new char[len + 1];
|
---|
176 | memset (str, c, len);
|
---|
177 | str[len] = 0;
|
---|
178 | }
|
---|
179 | };
|
---|
180 |
|
---|
181 |
|
---|
182 | static pmtty_const_str spaces (' ', pmtxt::max_line_len);
|
---|
183 |
|
---|
184 | void pmtty::backspace (int n)
|
---|
185 | {
|
---|
186 | if (n > x)
|
---|
187 | n = x;
|
---|
188 | if (n > 0)
|
---|
189 | {
|
---|
190 | x -= n;
|
---|
191 | cursor_off ();
|
---|
192 | put (y, x, n, spaces.str, sel_attr, true);
|
---|
193 | cursor_on ();
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | void pmtty::cursor (bool on)
|
---|
199 | {
|
---|
200 | if (on && !cursor_enabled)
|
---|
201 | {
|
---|
202 | cursor_enabled = true;
|
---|
203 | if (cursor_hidden == 0
|
---|
204 | && WinQueryFocus (HWND_DESKTOP) == get_hwndClient ())
|
---|
205 | paint_cursor ();
|
---|
206 | // TODO: Restart timer when SV_CURSORRATE is changed
|
---|
207 | WinStartTimer (get_hab (), get_hwndClient (), IDT_CURSOR,
|
---|
208 | WinQuerySysValue (HWND_DESKTOP, SV_CURSORRATE));
|
---|
209 | }
|
---|
210 | else if (!on && cursor_enabled)
|
---|
211 | {
|
---|
212 | WinStopTimer (get_hab(), get_hwndClient (), IDT_CURSOR);
|
---|
213 | cursor_enabled = false;
|
---|
214 | if (cursor_painted)
|
---|
215 | remove_cursor ();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | MRESULT pmtty::wm_setfocus (HWND, ULONG, MPARAM, MPARAM mp2)
|
---|
221 | {
|
---|
222 | if (SHORT1FROMMP (mp2))
|
---|
223 | {
|
---|
224 | if (cursor_enabled && cursor_hidden == 0 && !cursor_painted)
|
---|
225 | paint_cursor ();
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | if (cursor_painted)
|
---|
230 | remove_cursor ();
|
---|
231 | }
|
---|
232 | return 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | MRESULT pmtty::wm_timer (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
|
---|
237 | {
|
---|
238 | if (SHORT1FROMMP (mp1) == IDT_CURSOR)
|
---|
239 | {
|
---|
240 | if (cursor_painted)
|
---|
241 | remove_cursor ();
|
---|
242 | else if (cursor_enabled && cursor_hidden == 0
|
---|
243 | && WinQueryFocus (HWND_DESKTOP) == get_hwndClient ())
|
---|
244 | paint_cursor ();
|
---|
245 | return FALSE;
|
---|
246 | }
|
---|
247 | else
|
---|
248 | return WinDefWindowProc (hwnd, msg, mp1, mp2);
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | MRESULT pmtty::wm_presparamchanged (HWND hwnd, ULONG msg,
|
---|
253 | MPARAM mp1, MPARAM mp2)
|
---|
254 | {
|
---|
255 | parent::wm_presparamchanged (hwnd, msg, mp1, mp2);
|
---|
256 | if (LONGFROMMP (mp1) == PP_FONTNAMESIZE && !get_initializing_font ())
|
---|
257 | {
|
---|
258 | WinReleasePS (hpsCursor);
|
---|
259 | create_hpsCursor ();
|
---|
260 | }
|
---|
261 | return 0;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | void pmtty::font_changed ()
|
---|
266 | {
|
---|
267 | set_font (hpsCursor);
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | void pmtty::painting (bool start)
|
---|
272 | {
|
---|
273 | if (start)
|
---|
274 | cursor_off ();
|
---|
275 | else
|
---|
276 | cursor_on ();
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | void pmtty::delete_all ()
|
---|
281 | {
|
---|
282 | cursor_off ();
|
---|
283 | x = 0; y = 0;
|
---|
284 | parent::delete_all ();
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | bool pmtty::get_xy (POINTL *ptl)
|
---|
289 | {
|
---|
290 | return get_bbox_pos (hpsCursor, ptl, y, x);
|
---|
291 | }
|
---|