Skip to content

Commit

Permalink
added some comments
Browse files Browse the repository at this point in the history
added nice caret
  • Loading branch information
tnightingale committed Oct 3, 2010
1 parent e7e8b73 commit dcf1b9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
18 changes: 16 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,44 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hprevInstance, LPSTR lspszCmdParam
NULL
);

// Create window storage struct.
pWData = (PWDATA) malloc(sizeof(PWDATA));
pWData->state = COMMAND;
pWData->hCom = INVALID_HANDLE_VALUE;

// Set default window storage values.
pWData->state = COMMAND; // Program state.
pWData->hCom = INVALID_HANDLE_VALUE; // Comm port handle.

// Prepare output storage struct.
pWData->pOutput = (POUTPUT) malloc(sizeof(OUTPUT));
pWData->pOutput->size = OUTPUTBUFFSIZE;
pWData->pOutput->pos = 0;
pWData->pOutput->out = (TCHAR*) malloc(pWData->pOutput->size * sizeof(TCHAR));

// Initially white-out the output buffer.
for (i = 0; i < pWData->pOutput->size; i++) {
pWData->pOutput->out[i] = ' ';
}

// Set up the cursor struct with default values.
pWData->cursor.xCaret = 0;
pWData->cursor.yCaret = 0;
pWData->cursor.cxBuffer = 0;
pWData->cursor.cyBuffer = 0;
pWData->cursor.cxChar = 0;
pWData->cursor.cyChar = 0;

// Set window struct to the window.
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pWData);

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Message handling uses PeekMessage() instead of GetMessage.
// Essentially this will handle all messages when they arrive
// on the queue. But whenever the queue is empty, poll the
// serial port.
// This provides us with a very clean method of constantly polling the
// open serial port with out jamming the program.
while (TRUE) {
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
if (Msg.message == WM_QUIT) {
Expand All @@ -122,6 +135,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hprevInstance, LPSTR lspszCmdParam
}
else {
if (pWData->state == CONNECT) {
// If no messages, poll the open serial port.
pollPort(hwnd, pWData);
}
}
Expand Down
44 changes: 40 additions & 4 deletions session.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,34 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)

switch (Message) {

case WM_SIZE:
case WM_SETFOCUS:
hdc = GetDC(hwnd);
hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
SelectObject(hdc, hFont);
GetTextMetrics(hdc, &tm);
pWData = (PWDATA) GetWindowLongPtr(hwnd, GWLP_USERDATA);

pWData->cursor.cxChar = tm.tmMaxCharWidth;
pWData->cursor.cyChar = tm.tmHeight;

if (pWData->state == CONNECT) {
// Create a solid black caret.
CreateCaret(hwnd, NULL, 2, pWData->cursor.cyChar);
SetCaretPos(pWData->cursor.xCaret * pWData->cursor.cxChar, pWData->cursor.yCaret * pWData->cursor.cyChar);
// Display the caret.
ShowCaret(hwnd);
}
break;

case WM_KILLFOCUS:
// The window is losing the keyboard focus, so destroy the caret.
DestroyCaret();
break;

case WM_SIZE:
hdc = GetDC(hwnd);
pWData = (PWDATA) GetWindowLongPtr(hwnd, GWLP_USERDATA);

pWData->cursor.cxBuffer = max(1, LOWORD(lParam) / pWData->cursor.cxChar);
pWData->cursor.cyBuffer = max(1, HIWORD(lParam) / pWData->cursor.cyChar);

Expand Down Expand Up @@ -126,6 +144,10 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)

menu = GetMenu(hwnd);
setMenu(menu, MF_GRAYED);

CreateCaret(hwnd, NULL, 2, pWData->cursor.cyChar);
SetCaretPos(pWData->cursor.xCaret * pWData->cursor.cxChar, pWData->cursor.yCaret * pWData->cursor.cyChar);
ShowCaret(hwnd);
break;
}
break;
Expand All @@ -141,6 +163,7 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
menu = GetMenu(hwnd);
setMenu(menu, MF_ENABLED);
InvalidateRect(hwnd, NULL, FALSE);
DestroyCaret();
}
break;

Expand Down Expand Up @@ -169,20 +192,28 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
// v

default:
HideCaret(hwnd);
Transmit(pWData->hCom, wParam);
if (!outputAddChar((TCHAR) wParam, pWData->pOutput)) {
MessageBox(hwnd, TEXT("MASSIVE ERROR."), NULL, MB_ICONERROR);
CloseHandle(pWData->hCom);
PostQuitMessage(0);
}
//InvalidateRect(hwnd, NULL, FALSE);

// Prepare for printing.
hdc = GetDC(hwnd);
hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
SelectObject(hdc, hFont);

// Print character.
printChar(hwnd, &pWData->cursor, pWData->pOutput, hdc);
ReleaseDC(hwnd, hdc);

// Increment caret position.
pWData->cursor.xCaret = ++pWData->cursor.xCaret % pWData->cursor.cxBuffer;
pWData->cursor.yCaret = max(0, pWData->pOutput->pos / pWData->cursor.cxBuffer);
SetCaretPos(pWData->cursor.xCaret * pWData->cursor.cxChar, pWData->cursor.yCaret * pWData->cursor.cyChar);
ShowCaret(hwnd);
}
}
break;
Expand All @@ -191,6 +222,7 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
pWData = (PWDATA) GetWindowLongPtr(hwnd, GWLP_USERDATA);
hdc = BeginPaint(hwnd, &paintstruct); // Acquire DC

// Prepare for painting output.
hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
SelectObject(hdc, hFont);
printOut(hwnd, &pWData->cursor, pWData->pOutput, hdc);
Expand Down Expand Up @@ -316,12 +348,16 @@ void pollPort(HWND hwnd, PWDATA pWData) {
PostQuitMessage(0);
}

//InvalidateRect(hwnd, NULL, FALSE);
// Prepare to print character.
hdc = GetDC(hwnd);
hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
SelectObject(hdc, hFont);

// Print character.
printChar(hwnd, &pWData->cursor, pWData->pOutput, hdc);
ReleaseDC(hwnd, hdc);

// Repostion caret.
pWData->cursor.xCaret = ++pWData->cursor.xCaret % pWData->cursor.cxBuffer;
pWData->cursor.yCaret = max(0, pWData->pOutput->pos / pWData->cursor.cxBuffer);
}
Expand Down

0 comments on commit dcf1b9f

Please sign in to comment.