File: ConWin.cpp

package info (click to toggle)
vbindiff 3.0-beta5-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 632 kB
  • sloc: cpp: 2,299; sh: 1,003; perl: 110; makefile: 96; lisp: 13
file content (437 lines) | stat: -rw-r--r-- 12,419 bytes parent folder | download | duplicates (2)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//--------------------------------------------------------------------
//
//   VBinDiff
//   Copyright 1997-2017 by Christopher J. Madsen
//
//   Support class for Win32 console mode applications
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see <https://www.gnu.org/licenses/>.
//--------------------------------------------------------------------

#include "config.h"

#include "ConWin.hpp"

void exitMsg(int status, const char* message); // From vbindiff.cpp

//====================================================================
// Colors:
//--------------------------------------------------------------------

#define F_BLACK 0
#define F_RED   FOREGROUND_RED
#define F_WHITE (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE)
#define F_YELLOW (FOREGROUND_GREEN|FOREGROUND_RED)
#define B_BLUE  BACKGROUND_BLUE
#define B_WHITE (BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE)

static const WORD colorStyle[] = {
  F_WHITE|B_BLUE,                       // cBackground
  F_WHITE|B_BLUE,                       // cPromptWin
  F_WHITE|B_BLUE|FOREGROUND_INTENSITY,  // cPromptKey
  F_WHITE|B_BLUE|FOREGROUND_INTENSITY,  // cPromptBdr
  F_BLACK|B_WHITE,                      // cCurrentMode
  F_BLACK|B_WHITE,                      // cFileName
  F_WHITE|B_BLUE,                       // cFileWin
  F_RED|B_BLUE|FOREGROUND_INTENSITY,    // cFileDiff
  F_YELLOW|B_BLUE|FOREGROUND_INTENSITY  // cFileEdit
};

//====================================================================
// Class ConWindow:
//--------------------------------------------------------------------
HANDLE  ConWindow::inBuf  = INVALID_HANDLE_VALUE;
HANDLE  ConWindow::scrBuf = INVALID_HANDLE_VALUE;

static DWORD  origInMode = 0;    // The original input mode

//////////////////////////////////////////////////////////////////////
// Static Member Functions:
//--------------------------------------------------------------------
// Start up the window system:
//
// Allocates a screen buffer and sets input mode:
//
// Returns:
//   true:   Everything set up properly
//   false:  Unable to initialize

bool ConWindow::startup()
{
  inBuf = GetStdHandle(STD_INPUT_HANDLE);
  if (inBuf == INVALID_HANDLE_VALUE)
    return false;

  scrBuf = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE,
                                     0, NULL, // No sharing
                                     CONSOLE_TEXTMODE_BUFFER, NULL);

  if (scrBuf == INVALID_HANDLE_VALUE)
    return false;

  if (!SetConsoleActiveScreenBuffer(scrBuf) ||
      !SetConsoleMode(scrBuf, 0) ||
      !GetConsoleMode(inBuf, &origInMode) ||
      !SetConsoleMode(inBuf, 0)) {
    CloseHandle(scrBuf);
    return false;
  }

  return true;
} // end ConWindow::startup

//--------------------------------------------------------------------
// Shut down the window system:
//
// Deallocate the screen buffer and restore the original input mode.

void ConWindow::shutdown()
{
  if (origInMode)
    SetConsoleMode(inBuf, origInMode);
  if (scrBuf != INVALID_HANDLE_VALUE)
    CloseHandle(scrBuf);
  scrBuf = INVALID_HANDLE_VALUE;
} // end ConWindow::shutdown

//--------------------------------------------------------------------
// Return the current screen size:

void ConWindow::getScreenSize(int& x, int& y)
{
  CONSOLE_SCREEN_BUFFER_INFO  info;

  if (GetConsoleScreenBufferInfo(scrBuf, &info)) {
    x = info.dwSize.X;
    y = info.dwSize.Y;
  } else {
    x = y = 0;
  }
} // end ConWindow::getScreenSize

//--------------------------------------------------------------------
// Make the cursor invisible:

void ConWindow::hideCursor()
{
  CONSOLE_CURSOR_INFO  info;

  if (GetConsoleCursorInfo(scrBuf, &info)) {
    info.bVisible = FALSE;
    SetConsoleCursorInfo(scrBuf, &info);
  }
} // end ConWindow::hideCursor

//--------------------------------------------------------------------
// Read the next key down event:
//
// Output:
//   event:  Contains a key down event

void ConWindow::readKey(KEY_EVENT_RECORD& event)
{
  INPUT_RECORD  e;

  for (;;) {
    DWORD  count = 0;
    while (!count)
      ReadConsoleInput(inBuf, &e, 1, &count);

    if ((e.EventType == KEY_EVENT) && e.Event.KeyEvent.bKeyDown) {
      event = e.Event.KeyEvent;
      return;
    }
  } // end forever
} // end ConWindow::readKey

//--------------------------------------------------------------------
// Curses-compatible readKey function:

int ConWindow::readKey()
{
  KEY_EVENT_RECORD  e;

  readKey(e);
  switch (e.wVirtualKeyCode) {
   case VK_ESCAPE:  return KEY_ESCAPE;
   case VK_TAB:     return KEY_TAB;
   case VK_BACK:    return KEY_BACKSPACE;
   case VK_RETURN:  return KEY_RETURN;
   case VK_DELETE:  return KEY_DC;
   case VK_INSERT:  return KEY_IC;
   case VK_HOME:    return KEY_HOME;
   case VK_END:     return KEY_END;
   case VK_UP:      return KEY_UP;
   case VK_DOWN:    return KEY_DOWN;
   case VK_LEFT:    return KEY_LEFT;
   case VK_RIGHT:   return KEY_RIGHT;
  }

  return e.uChar.AsciiChar;
} // end ConWindow::readKey

//--------------------------------------------------------------------
// Make the cursor visible:

void ConWindow::showCursor(bool insert)
{
  CONSOLE_CURSOR_INFO  info;

  if (GetConsoleCursorInfo(scrBuf, &info)) {
    info.bVisible = TRUE;
    info.dwSize = (insert ? 10 : 100);
    SetConsoleCursorInfo(scrBuf, &info);
  }
} // end ConWindow::showCursor

//////////////////////////////////////////////////////////////////////
// Member Functions:
//--------------------------------------------------------------------
// Constructor:

ConWindow::ConWindow()
: data(NULL)
{
} // end ConWindow::ConWindow

//--------------------------------------------------------------------
// Destructor:

ConWindow::~ConWindow()
{
  delete[] data;
} // end ConWindow::~ConWindow

//--------------------------------------------------------------------
// Initialize the window:
//
// Must be called only once, before any other functions are called.
// Allocates the data structures and clears the window buffer, but
// does not display anything.
//
// Input:
//   x,y:           The position of the window in the screen buffer
//   width,height:  The size of the window
//   attrib:        The default attributes for the window

void ConWindow::init(short x, short y, short width, short height, Style style)
{
  ASSERT(data == NULL);
  attribs = colorStyle[style];
  pos.X  = x;
  pos.Y  = y;

  resize(width, height);
} // end ConWindow::init

//--------------------------------------------------------------------
// Draw a box in the window:
//
// Input:
//   x,y:           The upper left corner of the box in the window
//   width,height:  The size of the box
//   tl,tr:         The characters for the top left & top right corners
//   bl,br:         The characters for the bottom left & right corners
//   horiz:         The character for horizontal lines
//   vert:          The character for vertical lines

void ConWindow::box(short x, short y, short width, short height,
                    char tl, char tr, char bl, char br, char horiz, char vert)
{
  ASSERT((height > 1) && (width > 1));

  PCHAR_INFO  c1 = data + x + size.X * y;
  PCHAR_INFO  c  = c1;
  PCHAR_INFO  c2 = c1 + width - 1;

  c1->Char.AsciiChar = tl;
  c1->Attributes = attribs;
  c2->Char.AsciiChar = tr;
  c2->Attributes = attribs;

  while (++c < c2) {
    c->Char.AsciiChar = horiz;
    c->Attributes = attribs;
  }

  c  =  c1 + size.X * (height-1);
  for (;;) {
    c1 += size.X;
    c2 += size.X;
    if (c1 == c) break;
    c1->Char.AsciiChar = c2->Char.AsciiChar = vert;
    c1->Attributes = c2->Attributes = attribs;
  }

  c1->Char.AsciiChar = bl;
  c1->Attributes = attribs;
  c2->Char.AsciiChar = br;
  c2->Attributes = attribs;

  while (++c1 < c2) {
    c1->Char.AsciiChar = horiz;
    c1->Attributes = attribs;
  }
} // end ConWindow::box

//--------------------------------------------------------------------
// Draw a box with a single line around the window's border:

void ConWindow::border()
{
  box(0,0,size.X,size.Y, '\332','\277', '\300','\331', '\304', '\263');
} // end ConWindow::border

//--------------------------------------------------------------------
// Clear the window:

void ConWindow::clear()
{
  PCHAR_INFO  c = data;
  for (int i = size.X * size.Y; i > 0; ++c, --i) {
    c->Char.AsciiChar = ' ';
    c->Attributes = attribs;
  }
} // end ConWindow::clear

//--------------------------------------------------------------------
// Write a string using the current attributes:
//
// Input:
//   x,y:  The start of the string in the window
//   s:    The string to write

void ConWindow::put(short x, short y, const char* s)
{
  PCHAR_INFO  out = data + x + size.X * y;

  while (*s) {
    out->Char.AsciiChar = *s;
    out->Attributes = attribs;
    ++out;
    ++s;
  }
} // end ConWindow::put

///void ConWindow::put(short x, short y, const String& s)
///{
///  PCHAR_INFO  out = data + x + size.X * y;
///  StrConstItr  c = s.begin();
///
///  while (c != s.end()) {
///    out->Char.AsciiChar = *c;
///    out->Attributes = attribs;
///    ++out;
///    ++c;
///  }
///} // end ConWindow::put

//--------------------------------------------------------------------
// Change the attributes of characters in the window:
//
// Input:
//   x,y:    The position in the window to start changing attributes
//   color:  The attribute to set
//   count:  The number of characters to change

void ConWindow::putAttribs(short x, short y, Style color, short count)
{
  PCHAR_INFO  c = data + x + size.X * y;

  while (count--)
    (c++)->Attributes = colorStyle[color];
} // end ConWindow::putAttribs

//--------------------------------------------------------------------
// Write a character using the current attributes:
//
// Input:
//   x,y:    The position in the window to start writing
//   c:      The character to write
//   count:  The number of characters to write

void ConWindow::putChar(short x, short y, char c, short count)
{
  PCHAR_INFO  ci = data + x + size.X * y;

  while (count--) {
    ci->Char.AsciiChar = c;
    (ci++)->Attributes = attribs;
  }
} // end ConWindow::putAttribs

//--------------------------------------------------------------------
void ConWindow::resize(short width, short height)
{
  if ((size.X != width) || (size.Y != height)) {
    size.X = width;
    size.Y = height;

    delete [] data;
    data = new CHAR_INFO[size.X * size.Y];
  } // end if new size

  clear();
} // end ConWindow::resize

//--------------------------------------------------------------------
void ConWindow::setAttribs(Style color)
{
  attribs = colorStyle[color];
} // end ConWindow::setAttribs

//--------------------------------------------------------------------
// Position the cursor in the window:
//
// There is only one cursor, and each window does not maintain its own
// cursor position.
//
// Input:
//   x,y:    The position in the window for the cursor

void ConWindow::setCursor(short x, short y)
{
  ASSERT((x>=0)&&(x<size.X)&&(y>=0)&&(y<size.Y));
  COORD c;
  c.X = x + pos.X;
  c.Y = y + pos.Y;
  SetConsoleCursorPosition(scrBuf, c);
} // end ConWindow::setCursor

//--------------------------------------------------------------------
// Display the window on the screen:
//
// This is the only function that actually displays anything.
//
// Input:
//   margin:  Exclude this many characters around the edge (default 0)

void ConWindow::update(unsigned short margin)
{
  SMALL_RECT r;
  r.Left   = pos.X + margin;
  r.Top    = pos.Y + margin;
  r.Right  = pos.X + size.X - 1 - margin;
  r.Bottom = pos.Y + size.Y - 1 - margin;

  const COORD offset = {margin, margin};

  WriteConsoleOutput(scrBuf, data, size, offset, &r);
} // end ConWindow::update

//--------------------------------------------------------------------
// Local Variables:
// cjm-related-file: "ConWin.hpp"
//     c-file-style: "cjm"
// End: