2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: win32ss/user/winsrv/consrv/frontends/gui/text.c
5 * PURPOSE: GUI Terminal Front-End - Support for text-mode screen-buffers
6 * PROGRAMMERS: Gé van Geldorp
9 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
12 /* INCLUDES *******************************************************************/
21 /* GLOBALS ********************************************************************/
23 #define IS_WHITESPACE(c) ((c) == L'\0' || (c) == L' ' || (c) == L'\t')
25 /* FUNCTIONS ******************************************************************/
27 COLORREF
PaletteRGBFromAttrib(PCONSOLE Console
, WORD Attribute
)
29 HPALETTE hPalette
= Console
->ActiveBuffer
->PaletteHandle
;
32 if (hPalette
== NULL
) return RGBFromAttrib(Console
, Attribute
);
34 GetPaletteEntries(hPalette
, Attribute
, 1, &pe
);
35 return PALETTERGB(pe
.peRed
, pe
.peGreen
, pe
.peBlue
);
39 CopyBlock(PTEXTMODE_SCREEN_BUFFER Buffer
,
40 PSMALL_RECT Selection
)
43 * Pressing the Shift key while copying text, allows us to copy
44 * text without newline characters (inline-text copy mode).
46 BOOL InlineCopyMode
= !!(GetKeyState(VK_SHIFT
) & 0x8000);
51 ULONG selWidth
, selHeight
;
55 DPRINT("CopyBlock(%u, %u, %u, %u)\n",
56 Selection
->Left
, Selection
->Top
, Selection
->Right
, Selection
->Bottom
);
58 /* Prevent against empty blocks */
59 if (Selection
== NULL
) return;
60 if (Selection
->Left
> Selection
->Right
|| Selection
->Top
> Selection
->Bottom
)
63 selWidth
= Selection
->Right
- Selection
->Left
+ 1;
64 selHeight
= Selection
->Bottom
- Selection
->Top
+ 1;
66 /* Basic size for one line... */
68 /* ... and for the other lines, add newline characters if needed. */
72 * If we are not in inline-text copy mode, each selected line must
73 * finish with \r\n . Otherwise, the lines will be just concatenated.
75 size
+= (selWidth
+ (!InlineCopyMode
? 2 : 0)) * (selHeight
- 1);
79 DPRINT1("This case must never happen, because selHeight is at least == 1\n");
82 size
+= 1; /* Null-termination */
83 size
*= sizeof(WCHAR
);
85 /* Allocate some memory area to be given to the clipboard, so it will not be freed here */
86 hData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
87 if (hData
== NULL
) return;
89 data
= GlobalLock(hData
);
96 DPRINT("Copying %dx%d selection\n", selWidth
, selHeight
);
99 for (yPos
= 0; yPos
< selHeight
; yPos
++)
101 ULONG length
= selWidth
;
103 ptr
= ConioCoordToPointer(Buffer
,
105 Selection
->Top
+ yPos
);
107 /* Trim whitespace from the right */
110 if (IS_WHITESPACE(ptr
[length
-1].Char
.UnicodeChar
))
116 /* Copy only the characters, leave attributes alone */
117 for (xPos
= 0; xPos
< length
; xPos
++)
120 * Sometimes, applications can put NULL chars into the screen-buffer
121 * (this behaviour is allowed). Detect this and replace by a space.
123 *dstPos
++ = (ptr
[xPos
].Char
.UnicodeChar
? ptr
[xPos
].Char
.UnicodeChar
: L
' ');
126 /* Add newline characters if we are not in inline-text copy mode */
129 if (yPos
!= (selHeight
- 1))
131 wcscat(dstPos
, L
"\r\n");
137 DPRINT("Setting data <%S> to clipboard\n", data
);
141 SetClipboardData(CF_UNICODETEXT
, hData
);
145 CopyLines(PTEXTMODE_SCREEN_BUFFER Buffer
,
152 ULONG NumChars
, size
;
153 ULONG xPos
, yPos
, xBeg
, xEnd
;
155 DPRINT("CopyLines((%u, %u) ; (%u, %u))\n",
156 Begin
->X
, Begin
->Y
, End
->X
, End
->Y
);
158 /* Prevent against empty blocks... */
159 if (Begin
== NULL
|| End
== NULL
) return;
160 /* ... or malformed blocks */
161 if (Begin
->Y
> End
->Y
|| (Begin
->Y
== End
->Y
&& Begin
->X
> End
->X
)) return;
163 /* Compute the number of characters to copy */
164 if (End
->Y
== Begin
->Y
) // top == bottom
166 NumChars
= End
->X
- Begin
->X
+ 1;
168 else // if (End->Y > Begin->Y)
170 NumChars
= Buffer
->ScreenBufferSize
.X
- Begin
->X
;
172 if (End
->Y
>= Begin
->Y
+ 2)
174 NumChars
+= (End
->Y
- Begin
->Y
- 1) * Buffer
->ScreenBufferSize
.X
;
177 NumChars
+= End
->X
+ 1;
180 size
= (NumChars
+ 1) * sizeof(WCHAR
); /* Null-terminated */
182 /* Allocate some memory area to be given to the clipboard, so it will not be freed here */
183 hData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
184 if (hData
== NULL
) return;
186 data
= GlobalLock(hData
);
193 DPRINT("Copying %d characters\n", NumChars
);
197 * We need to walk per-lines, and not just looping in the big screen-buffer
198 * array, because of the way things are stored inside it. The downside is
199 * that it makes the code more complicated.
201 for (yPos
= Begin
->Y
; (yPos
<= End
->Y
) && (NumChars
> 0); yPos
++)
203 xBeg
= (yPos
== Begin
->Y
? Begin
->X
: 0);
204 xEnd
= (yPos
== End
->Y
? End
->X
: Buffer
->ScreenBufferSize
.X
- 1);
206 ptr
= ConioCoordToPointer(Buffer
, 0, yPos
);
208 /* Copy only the characters, leave attributes alone */
209 for (xPos
= xBeg
; (xPos
<= xEnd
) && (NumChars
-- > 0); xPos
++)
212 * Sometimes, applications can put NULL chars into the screen-buffer
213 * (this behaviour is allowed). Detect this and replace by a space.
215 *dstPos
++ = (ptr
[xPos
].Char
.UnicodeChar
? ptr
[xPos
].Char
.UnicodeChar
: L
' ');
219 DPRINT("Setting data <%S> to clipboard\n", data
);
223 SetClipboardData(CF_UNICODETEXT
, hData
);
228 GetSelectionBeginEnd(PCOORD Begin
, PCOORD End
,
229 PCOORD SelectionAnchor
,
230 PSMALL_RECT SmallRect
);
233 GuiCopyFromTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
234 PGUI_CONSOLE_DATA GuiData
)
237 * This function supposes that the system clipboard was opened.
240 BOOL LineSelection
= GuiData
->LineSelection
;
242 DPRINT("Selection is (%d|%d) to (%d|%d) in %s mode\n",
243 GuiData
->Selection
.srSelection
.Left
,
244 GuiData
->Selection
.srSelection
.Top
,
245 GuiData
->Selection
.srSelection
.Right
,
246 GuiData
->Selection
.srSelection
.Bottom
,
247 (LineSelection
? "line" : "block"));
251 CopyBlock(Buffer
, &GuiData
->Selection
.srSelection
);
257 GetSelectionBeginEnd(&Begin
, &End
,
258 &GuiData
->Selection
.dwSelectionAnchor
,
259 &GuiData
->Selection
.srSelection
);
261 CopyLines(Buffer
, &Begin
, &End
);
266 GuiPasteToTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
267 PGUI_CONSOLE_DATA GuiData
)
270 * This function supposes that the system clipboard was opened.
273 PCONSOLE Console
= Buffer
->Header
.Console
;
279 USHORT VkKey
; // MAKEWORD(low = vkey_code, high = shift_state);
282 hData
= GetClipboardData(CF_UNICODETEXT
);
283 if (hData
== NULL
) return;
285 str
= GlobalLock(hData
);
286 if (str
== NULL
) return;
288 DPRINT("Got data <%S> from clipboard\n", str
);
290 er
.EventType
= KEY_EVENT
;
291 er
.Event
.KeyEvent
.wRepeatCount
= 1;
294 /* \r or \n characters. Go to the line only if we get "\r\n" sequence. */
295 if (CurChar
== L
'\r' && *str
== L
'\n')
302 /* Get the key code (+ shift state) corresponding to the character */
303 VkKey
= VkKeyScanW(CurChar
);
306 DPRINT1("VkKeyScanW failed - Should simulate the key...\n");
310 /* Pressing some control keys */
312 /* Pressing the character key, with the control keys maintained pressed */
313 er
.Event
.KeyEvent
.bKeyDown
= TRUE
;
314 er
.Event
.KeyEvent
.wVirtualKeyCode
= LOBYTE(VkKey
);
315 er
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKeyW(LOBYTE(VkKey
), MAPVK_VK_TO_CHAR
);
316 er
.Event
.KeyEvent
.uChar
.UnicodeChar
= CurChar
;
317 er
.Event
.KeyEvent
.dwControlKeyState
= 0;
318 if (HIBYTE(VkKey
) & 1)
319 er
.Event
.KeyEvent
.dwControlKeyState
|= SHIFT_PRESSED
;
320 if (HIBYTE(VkKey
) & 2)
321 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_CTRL_PRESSED
; // RIGHT_CTRL_PRESSED;
322 if (HIBYTE(VkKey
) & 4)
323 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_ALT_PRESSED
; // RIGHT_ALT_PRESSED;
325 ConioProcessInputEvent(Console
, &er
);
327 /* Up all the character and control keys */
328 er
.Event
.KeyEvent
.bKeyDown
= FALSE
;
329 ConioProcessInputEvent(Console
, &er
);
336 GuiPaintTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
337 PGUI_CONSOLE_DATA GuiData
,
341 PCONSOLE Console
= Buffer
->Header
.Console
;
342 // ASSERT(Console == GuiData->Console);
344 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
345 ULONG Line
, Char
, Start
;
348 WORD LastAttribute
, Attribute
;
349 ULONG CursorX
, CursorY
, CursorHeight
;
350 HBRUSH CursorBrush
, OldBrush
;
353 if (Buffer
->Buffer
== NULL
) return;
355 if (!ConDrvValidateConsoleUnsafe(Console
, CONSOLE_RUNNING
, TRUE
)) return;
357 rcFramebuffer
->left
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->left
;
358 rcFramebuffer
->top
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->top
;
359 rcFramebuffer
->right
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->right
;
360 rcFramebuffer
->bottom
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->bottom
;
362 LeftChar
= rcFramebuffer
->left
/ GuiData
->CharWidth
;
363 TopLine
= rcFramebuffer
->top
/ GuiData
->CharHeight
;
364 RightChar
= rcFramebuffer
->right
/ GuiData
->CharWidth
;
365 BottomLine
= rcFramebuffer
->bottom
/ GuiData
->CharHeight
;
367 if (RightChar
>= Buffer
->ScreenBufferSize
.X
) RightChar
= Buffer
->ScreenBufferSize
.X
- 1;
368 if (BottomLine
>= Buffer
->ScreenBufferSize
.Y
) BottomLine
= Buffer
->ScreenBufferSize
.Y
- 1;
370 LastAttribute
= ConioCoordToPointer(Buffer
, LeftChar
, TopLine
)->Attributes
;
372 SetTextColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(LastAttribute
)));
373 SetBkColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, BkgdAttribFromAttrib(LastAttribute
)));
375 OldFont
= SelectObject(GuiData
->hMemDC
, GuiData
->Font
);
377 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
379 WCHAR LineBuffer
[80]; // Buffer containing a part or all the line to be displayed
380 From
= ConioCoordToPointer(Buffer
, LeftChar
, Line
); // Get the first code of the line
384 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
387 * We flush the buffer if the new attribute is different
388 * from the current one, or if the buffer is full.
390 if (From
->Attributes
!= LastAttribute
|| (Char
- Start
== sizeof(LineBuffer
) / sizeof(WCHAR
)))
392 TextOutW(GuiData
->hMemDC
,
393 Start
* GuiData
->CharWidth
,
394 Line
* GuiData
->CharHeight
,
399 Attribute
= From
->Attributes
;
400 if (Attribute
!= LastAttribute
)
402 SetTextColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(Attribute
)));
403 SetBkColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, BkgdAttribFromAttrib(Attribute
)));
404 LastAttribute
= Attribute
;
408 *(To
++) = (From
++)->Char
.UnicodeChar
;
411 TextOutW(GuiData
->hMemDC
,
412 Start
* GuiData
->CharWidth
,
413 Line
* GuiData
->CharHeight
,
415 RightChar
- Start
+ 1);
421 if (Buffer
->CursorInfo
.bVisible
&&
422 Buffer
->CursorBlinkOn
&&
423 !Buffer
->ForceCursorOff
)
425 CursorX
= Buffer
->CursorPosition
.X
;
426 CursorY
= Buffer
->CursorPosition
.Y
;
427 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
428 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
430 CursorHeight
= ConioEffectiveCursorSize(Console
, GuiData
->CharHeight
);
432 Attribute
= ConioCoordToPointer(Buffer
, Buffer
->CursorPosition
.X
, Buffer
->CursorPosition
.Y
)->Attributes
;
433 if (Attribute
== DEFAULT_SCREEN_ATTRIB
) Attribute
= Buffer
->ScreenDefaultAttrib
;
435 CursorBrush
= CreateSolidBrush(PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(Attribute
)));
436 OldBrush
= SelectObject(GuiData
->hMemDC
, CursorBrush
);
438 PatBlt(GuiData
->hMemDC
,
439 CursorX
* GuiData
->CharWidth
,
440 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
444 SelectObject(GuiData
->hMemDC
, OldBrush
);
445 DeleteObject(CursorBrush
);
449 SelectObject(GuiData
->hMemDC
, OldFont
);
451 LeaveCriticalSection(&Console
->Lock
);