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 *******************************************************************/
15 #include "include/conio.h"
16 #include "include/settings.h"
17 #include "guisettings.h"
23 /* FUNCTIONS ******************************************************************/
25 COLORREF
RGBFromAttrib2(PCONSOLE Console
, WORD Attribute
)
27 HPALETTE hPalette
= Console
->ActiveBuffer
->PaletteHandle
;
30 if (hPalette
== NULL
) return RGBFromAttrib(Console
, Attribute
);
32 GetPaletteEntries(hPalette
, Attribute
, 1, &pe
);
33 return PALETTERGB(pe
.peRed
, pe
.peGreen
, pe
.peBlue
);
37 GuiCopyFromTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
)
40 * This function supposes that the system clipboard was opened.
43 PCONSOLE Console
= Buffer
->Header
.Console
;
46 * Pressing the Shift key while copying text, allows us to copy
47 * text without newline characters (inline-text copy mode).
49 BOOL InlineCopyMode
= (GetKeyState(VK_SHIFT
) & 0x8000);
54 ULONG selWidth
, selHeight
;
55 ULONG xPos
, yPos
, size
;
57 selWidth
= Console
->Selection
.srSelection
.Right
- Console
->Selection
.srSelection
.Left
+ 1;
58 selHeight
= Console
->Selection
.srSelection
.Bottom
- Console
->Selection
.srSelection
.Top
+ 1;
59 DPRINT("Selection is (%d|%d) to (%d|%d)\n",
60 Console
->Selection
.srSelection
.Left
,
61 Console
->Selection
.srSelection
.Top
,
62 Console
->Selection
.srSelection
.Right
,
63 Console
->Selection
.srSelection
.Bottom
);
65 /* Basic size for one line... */
67 /* ... and for the other lines, add newline characters if needed. */
71 * If we are not in inline-text copy mode, each selected line must
72 * finish with \r\n . Otherwise, the lines will be just concatenated.
74 size
+= (selWidth
+ (!InlineCopyMode
? 2 : 0)) * (selHeight
- 1);
76 size
+= 1; /* Null-termination */
77 size
*= sizeof(WCHAR
);
79 /* Allocate memory, it will be passed to the system and may not be freed here */
80 hData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
81 if (hData
== NULL
) return;
83 data
= GlobalLock(hData
);
84 if (data
== NULL
) return;
86 DPRINT("Copying %dx%d selection\n", selWidth
, selHeight
);
89 for (yPos
= 0; yPos
< selHeight
; yPos
++)
91 ptr
= ConioCoordToPointer(Buffer
,
92 Console
->Selection
.srSelection
.Left
,
93 yPos
+ Console
->Selection
.srSelection
.Top
);
94 /* Copy only the characters, leave attributes alone */
95 for (xPos
= 0; xPos
< selWidth
; xPos
++)
98 * Sometimes, applications can put NULL chars into the screen-buffer
99 * (this behaviour is allowed). Detect this and replace by a space.
100 * FIXME - HACK: Improve the way we're doing that (i.e., put spaces
101 * instead of NULLs (or even, nothing) only if it exists a non-null
102 * char *after* those NULLs, before the end-of-line of the selection.
103 * Do the same concerning spaces -- i.e. trailing spaces --).
105 dstPos
[xPos
] = (ptr
[xPos
].Char
.UnicodeChar
? ptr
[xPos
].Char
.UnicodeChar
: L
' ');
109 /* Add newline characters if we are not in inline-text copy mode */
112 if (yPos
!= (selHeight
- 1))
114 wcscat(data
, L
"\r\n");
120 DPRINT("Setting data <%S> to clipboard\n", data
);
124 SetClipboardData(CF_UNICODETEXT
, hData
);
128 GuiPasteToTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
)
131 * This function supposes that the system clipboard was opened.
134 PCONSOLE Console
= Buffer
->Header
.Console
;
140 SHORT VkKey
; // MAKEWORD(low = vkey_code, high = shift_state);
143 hData
= GetClipboardData(CF_UNICODETEXT
);
144 if (hData
== NULL
) return;
146 str
= GlobalLock(hData
);
147 if (str
== NULL
) return;
149 DPRINT("Got data <%S> from clipboard\n", str
);
151 er
.EventType
= KEY_EVENT
;
152 er
.Event
.KeyEvent
.wRepeatCount
= 1;
155 /* \r or \n characters. Go to the line only if we get "\r\n" sequence. */
156 if (CurChar
== L
'\r' && *str
== L
'\n')
163 /* Get the key code (+ shift state) corresponding to the character */
164 VkKey
= VkKeyScanW(CurChar
);
167 DPRINT1("VkKeyScanW failed - Should simulate the key...\n");
171 /* Pressing some control keys */
173 /* Pressing the character key, with the control keys maintained pressed */
174 er
.Event
.KeyEvent
.bKeyDown
= TRUE
;
175 er
.Event
.KeyEvent
.wVirtualKeyCode
= LOBYTE(VkKey
);
176 er
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKeyW(LOBYTE(VkKey
), MAPVK_VK_TO_CHAR
);
177 er
.Event
.KeyEvent
.uChar
.UnicodeChar
= CurChar
;
178 er
.Event
.KeyEvent
.dwControlKeyState
= 0;
179 if (HIBYTE(VkKey
) & 1)
180 er
.Event
.KeyEvent
.dwControlKeyState
|= SHIFT_PRESSED
;
181 if (HIBYTE(VkKey
) & 2)
182 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_CTRL_PRESSED
; // RIGHT_CTRL_PRESSED;
183 if (HIBYTE(VkKey
) & 4)
184 er
.Event
.KeyEvent
.dwControlKeyState
|= LEFT_ALT_PRESSED
; // RIGHT_ALT_PRESSED;
186 ConioProcessInputEvent(Console
, &er
);
188 /* Up all the character and control keys */
189 er
.Event
.KeyEvent
.bKeyDown
= FALSE
;
190 ConioProcessInputEvent(Console
, &er
);
197 GuiPaintTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer
,
198 PGUI_CONSOLE_DATA GuiData
,
202 PCONSOLE Console
= Buffer
->Header
.Console
;
203 // ASSERT(Console == GuiData->Console);
205 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
206 ULONG Line
, Char
, Start
;
209 WORD LastAttribute
, Attribute
;
210 ULONG CursorX
, CursorY
, CursorHeight
;
211 HBRUSH CursorBrush
, OldBrush
;
214 if (Buffer
->Buffer
== NULL
) return;
216 rcFramebuffer
->left
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->left
;
217 rcFramebuffer
->top
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->top
;
218 rcFramebuffer
->right
= Buffer
->ViewOrigin
.X
* GuiData
->CharWidth
+ rcView
->right
;
219 rcFramebuffer
->bottom
= Buffer
->ViewOrigin
.Y
* GuiData
->CharHeight
+ rcView
->bottom
;
221 LeftChar
= rcFramebuffer
->left
/ GuiData
->CharWidth
;
222 TopLine
= rcFramebuffer
->top
/ GuiData
->CharHeight
;
223 RightChar
= rcFramebuffer
->right
/ GuiData
->CharWidth
;
224 BottomLine
= rcFramebuffer
->bottom
/ GuiData
->CharHeight
;
226 if (RightChar
>= Buffer
->ScreenBufferSize
.X
) RightChar
= Buffer
->ScreenBufferSize
.X
- 1;
227 if (BottomLine
>= Buffer
->ScreenBufferSize
.Y
) BottomLine
= Buffer
->ScreenBufferSize
.Y
- 1;
229 LastAttribute
= ConioCoordToPointer(Buffer
, LeftChar
, TopLine
)->Attributes
;
231 SetTextColor(GuiData
->hMemDC
, RGBFromAttrib2(Console
, TextAttribFromAttrib(LastAttribute
)));
232 SetBkColor(GuiData
->hMemDC
, RGBFromAttrib2(Console
, BkgdAttribFromAttrib(LastAttribute
)));
234 OldFont
= SelectObject(GuiData
->hMemDC
, GuiData
->Font
);
236 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
238 WCHAR LineBuffer
[80]; // Buffer containing a part or all the line to be displayed
239 From
= ConioCoordToPointer(Buffer
, LeftChar
, Line
); // Get the first code of the line
243 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
246 * We flush the buffer if the new attribute is different
247 * from the current one, or if the buffer is full.
249 if (From
->Attributes
!= LastAttribute
|| (Char
- Start
== sizeof(LineBuffer
) / sizeof(WCHAR
)))
251 TextOutW(GuiData
->hMemDC
,
252 Start
* GuiData
->CharWidth
,
253 Line
* GuiData
->CharHeight
,
258 Attribute
= From
->Attributes
;
259 if (Attribute
!= LastAttribute
)
261 SetTextColor(GuiData
->hMemDC
, RGBFromAttrib2(Console
, TextAttribFromAttrib(Attribute
)));
262 SetBkColor(GuiData
->hMemDC
, RGBFromAttrib2(Console
, BkgdAttribFromAttrib(Attribute
)));
263 LastAttribute
= Attribute
;
267 *(To
++) = (From
++)->Char
.UnicodeChar
;
270 TextOutW(GuiData
->hMemDC
,
271 Start
* GuiData
->CharWidth
,
272 Line
* GuiData
->CharHeight
,
274 RightChar
- Start
+ 1);
280 if (Buffer
->CursorInfo
.bVisible
&&
281 Buffer
->CursorBlinkOn
&&
282 !Buffer
->ForceCursorOff
)
284 CursorX
= Buffer
->CursorPosition
.X
;
285 CursorY
= Buffer
->CursorPosition
.Y
;
286 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
287 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
289 CursorHeight
= ConioEffectiveCursorSize(Console
, GuiData
->CharHeight
);
291 Attribute
= ConioCoordToPointer(Buffer
, Buffer
->CursorPosition
.X
, Buffer
->CursorPosition
.Y
)->Attributes
;
292 if (Attribute
== DEFAULT_SCREEN_ATTRIB
) Attribute
= Buffer
->ScreenDefaultAttrib
;
294 CursorBrush
= CreateSolidBrush(RGBFromAttrib2(Console
, TextAttribFromAttrib(Attribute
)));
295 OldBrush
= SelectObject(GuiData
->hMemDC
, CursorBrush
);
297 PatBlt(GuiData
->hMemDC
,
298 CursorX
* GuiData
->CharWidth
,
299 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
303 SelectObject(GuiData
->hMemDC
, OldBrush
);
304 DeleteObject(CursorBrush
);
308 SelectObject(GuiData
->hMemDC
, OldFont
);