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 /* FUNCTIONS ******************************************************************/
23 COLORREF
PaletteRGBFromAttrib(PCONSOLE Console
, WORD Attribute
)
25 HPALETTE hPalette
= Console
->ActiveBuffer
->PaletteHandle
;
28 if (hPalette
== NULL
) return RGBFromAttrib(Console
, Attribute
);
30 GetPaletteEntries(hPalette
, Attribute
, 1, &pe
);
31 return PALETTERGB(pe
.peRed
, pe
.peGreen
, pe
.peBlue
);
35 GuiCopyFromTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
36 PGUI_CONSOLE_DATA GuiData
)
39 * This function supposes that the system clipboard was opened.
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
;
52 ULONG xPos
, yPos
, size
;
54 selWidth
= GuiData
->Selection
.srSelection
.Right
- GuiData
->Selection
.srSelection
.Left
+ 1;
55 selHeight
= GuiData
->Selection
.srSelection
.Bottom
- GuiData
->Selection
.srSelection
.Top
+ 1;
56 DPRINT("Selection is (%d|%d) to (%d|%d)\n",
57 GuiData
->Selection
.srSelection
.Left
,
58 GuiData
->Selection
.srSelection
.Top
,
59 GuiData
->Selection
.srSelection
.Right
,
60 GuiData
->Selection
.srSelection
.Bottom
);
62 #define IS_WHITESPACE(c) ((c) == L'\0' || (c) == L' ' || (c) == L'\t')
64 /* Basic size for one line... */
66 /* ... and for the other lines, add newline characters if needed. */
70 * If we are not in inline-text copy mode, each selected line must
71 * finish with \r\n . Otherwise, the lines will be just concatenated.
73 size
+= (selWidth
+ (!InlineCopyMode
? 2 : 0)) * (selHeight
- 1);
75 size
+= 1; /* Null-termination */
76 size
*= sizeof(WCHAR
);
78 /* Allocate some memory area to be given to the clipboard, so it will not be freed here */
79 hData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
80 if (hData
== NULL
) return;
82 data
= GlobalLock(hData
);
89 DPRINT("Copying %dx%d selection\n", selWidth
, selHeight
);
92 for (yPos
= 0; yPos
< selHeight
; yPos
++)
94 ULONG length
= selWidth
;
96 ptr
= ConioCoordToPointer(Buffer
,
97 GuiData
->Selection
.srSelection
.Left
,
98 GuiData
->Selection
.srSelection
.Top
+ yPos
);
100 /* Trim whitespace from the right */
103 if (IS_WHITESPACE(ptr
[length
-1].Char
.UnicodeChar
))
109 /* Copy only the characters, leave attributes alone */
110 for (xPos
= 0; xPos
< length
; xPos
++)
113 * Sometimes, applications can put NULL chars into the screen-buffer
114 * (this behaviour is allowed). Detect this and replace by a space.
116 dstPos
[xPos
] = (ptr
[xPos
].Char
.UnicodeChar
? ptr
[xPos
].Char
.UnicodeChar
: L
' ');
120 /* Add newline characters if we are not in inline-text copy mode */
123 if (yPos
!= (selHeight
- 1))
125 wcscat(data
, L
"\r\n");
131 DPRINT("Setting data <%S> to clipboard\n", data
);
135 SetClipboardData(CF_UNICODETEXT
, hData
);
139 GuiPasteToTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
140 PGUI_CONSOLE_DATA GuiData
)
143 * This function supposes that the system clipboard was opened.
146 PCONSOLE Console
= Buffer
->Header
.Console
;
152 USHORT VkKey
; // MAKEWORD(low = vkey_code, high = shift_state);
155 hData
= GetClipboardData(CF_UNICODETEXT
);
156 if (hData
== NULL
) return;
158 str
= GlobalLock(hData
);
159 if (str
== NULL
) return;
161 DPRINT("Got data <%S> from clipboard\n", str
);
163 er
.EventType
= KEY_EVENT
;
164 er
.Event
.KeyEvent
.wRepeatCount
= 1;
167 /* \r or \n characters. Go to the line only if we get "\r\n" sequence. */
168 if (CurChar
== L
'\r' && *str
== L
'\n')
175 /* Get the key code (+ shift state) corresponding to the character */
176 VkKey
= VkKeyScanW(CurChar
);
179 DPRINT1("VkKeyScanW failed - Should simulate the key...\n");
183 /* Pressing some control keys */
185 /* Pressing the character key, with the control keys maintained pressed */
186 er
.Event
.KeyEvent
.bKeyDown
= TRUE
;
187 er
.Event
.KeyEvent
.wVirtualKeyCode
= LOBYTE(VkKey
);
188 er
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKeyW(LOBYTE(VkKey
), MAPVK_VK_TO_CHAR
);
189 er
.Event
.KeyEvent
.uChar
.UnicodeChar
= CurChar
;
190 er
.Event
.KeyEvent
.dwControlKeyState
= 0;
191 if (HIBYTE(VkKey
) & 1)
192 er
.Event
.KeyEvent
.dwControlKeyState
|= SHIFT_PRESSED
;
193 if (HIBYTE(VkKey
) & 2)
194 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_CTRL_PRESSED
; // RIGHT_CTRL_PRESSED;
195 if (HIBYTE(VkKey
) & 4)
196 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_ALT_PRESSED
; // RIGHT_ALT_PRESSED;
198 ConioProcessInputEvent(Console
, &er
);
200 /* Up all the character and control keys */
201 er
.Event
.KeyEvent
.bKeyDown
= FALSE
;
202 ConioProcessInputEvent(Console
, &er
);
209 GuiPaintTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
210 PGUI_CONSOLE_DATA GuiData
,
214 PCONSOLE Console
= Buffer
->Header
.Console
;
215 // ASSERT(Console == GuiData->Console);
217 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
218 ULONG Line
, Char
, Start
;
221 WORD LastAttribute
, Attribute
;
222 ULONG CursorX
, CursorY
, CursorHeight
;
223 HBRUSH CursorBrush
, OldBrush
;
226 if (Buffer
->Buffer
== NULL
) return;
228 if (!ConDrvValidateConsoleUnsafe(Console
, CONSOLE_RUNNING
, TRUE
)) return;
230 rcFramebuffer
->left
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->left
;
231 rcFramebuffer
->top
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->top
;
232 rcFramebuffer
->right
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->right
;
233 rcFramebuffer
->bottom
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->bottom
;
235 LeftChar
= rcFramebuffer
->left
/ GuiData
->CharWidth
;
236 TopLine
= rcFramebuffer
->top
/ GuiData
->CharHeight
;
237 RightChar
= rcFramebuffer
->right
/ GuiData
->CharWidth
;
238 BottomLine
= rcFramebuffer
->bottom
/ GuiData
->CharHeight
;
240 if (RightChar
>= Buffer
->ScreenBufferSize
.X
) RightChar
= Buffer
->ScreenBufferSize
.X
- 1;
241 if (BottomLine
>= Buffer
->ScreenBufferSize
.Y
) BottomLine
= Buffer
->ScreenBufferSize
.Y
- 1;
243 LastAttribute
= ConioCoordToPointer(Buffer
, LeftChar
, TopLine
)->Attributes
;
245 SetTextColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(LastAttribute
)));
246 SetBkColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, BkgdAttribFromAttrib(LastAttribute
)));
248 OldFont
= SelectObject(GuiData
->hMemDC
, GuiData
->Font
);
250 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
252 WCHAR LineBuffer
[80]; // Buffer containing a part or all the line to be displayed
253 From
= ConioCoordToPointer(Buffer
, LeftChar
, Line
); // Get the first code of the line
257 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
260 * We flush the buffer if the new attribute is different
261 * from the current one, or if the buffer is full.
263 if (From
->Attributes
!= LastAttribute
|| (Char
- Start
== sizeof(LineBuffer
) / sizeof(WCHAR
)))
265 TextOutW(GuiData
->hMemDC
,
266 Start
* GuiData
->CharWidth
,
267 Line
* GuiData
->CharHeight
,
272 Attribute
= From
->Attributes
;
273 if (Attribute
!= LastAttribute
)
275 SetTextColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(Attribute
)));
276 SetBkColor(GuiData
->hMemDC
, PaletteRGBFromAttrib(Console
, BkgdAttribFromAttrib(Attribute
)));
277 LastAttribute
= Attribute
;
281 *(To
++) = (From
++)->Char
.UnicodeChar
;
284 TextOutW(GuiData
->hMemDC
,
285 Start
* GuiData
->CharWidth
,
286 Line
* GuiData
->CharHeight
,
288 RightChar
- Start
+ 1);
294 if (Buffer
->CursorInfo
.bVisible
&&
295 Buffer
->CursorBlinkOn
&&
296 !Buffer
->ForceCursorOff
)
298 CursorX
= Buffer
->CursorPosition
.X
;
299 CursorY
= Buffer
->CursorPosition
.Y
;
300 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
301 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
303 CursorHeight
= ConioEffectiveCursorSize(Console
, GuiData
->CharHeight
);
305 Attribute
= ConioCoordToPointer(Buffer
, Buffer
->CursorPosition
.X
, Buffer
->CursorPosition
.Y
)->Attributes
;
306 if (Attribute
== DEFAULT_SCREEN_ATTRIB
) Attribute
= Buffer
->ScreenDefaultAttrib
;
308 CursorBrush
= CreateSolidBrush(PaletteRGBFromAttrib(Console
, TextAttribFromAttrib(Attribute
)));
309 OldBrush
= SelectObject(GuiData
->hMemDC
, CursorBrush
);
311 PatBlt(GuiData
->hMemDC
,
312 CursorX
* GuiData
->CharWidth
,
313 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
317 SelectObject(GuiData
->hMemDC
, OldBrush
);
318 DeleteObject(CursorBrush
);
322 SelectObject(GuiData
->hMemDC
, OldFont
);
324 LeaveCriticalSection(&Console
->Lock
);