Patch by Jonathon Wilson:
[reactos.git] / reactos / apps / tests / carets / carets.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include "resource.h"
4
5 static int CaretWidth = 2;
6 static int CaretHeight = 16;
7 static int CharWidth = 10;
8 static int CharHeight = 16;
9 static HBITMAP CaretBitmap;
10
11 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
12
13 int WINAPI
14 WinMain(HINSTANCE hInstance,
15 HINSTANCE hPrevInstance,
16 LPSTR lpszCmdLine,
17 int nCmdShow)
18 {
19 WNDCLASS wc;
20 MSG msg;
21 HWND hWnd;
22
23 CaretBitmap = LoadBitmap(hInstance, (LPCTSTR)IDB_CARET);
24
25 wc.lpszClassName = "CaretTestClass";
26 wc.lpfnWndProc = MainWndProc;
27 wc.style = CS_VREDRAW | CS_HREDRAW;
28 wc.hInstance = hInstance;
29 wc.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
30 wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
31 wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
32 wc.lpszMenuName = NULL;
33 wc.cbClsExtra = 0;
34 wc.cbWndExtra = 0;
35 if (RegisterClass(&wc) == 0)
36 {
37 fprintf(stderr, "RegisterClass failed (last error 0x%lX)\n",
38 GetLastError());
39 return(1);
40 }
41
42 hWnd = CreateWindow(wc.lpszClassName,
43 "Caret Test",
44 WS_OVERLAPPEDWINDOW,
45 0,
46 0,
47 200,
48 250,
49 NULL,
50 NULL,
51 hInstance,
52 NULL);
53 if (hWnd == NULL)
54 {
55 fprintf(stderr, "CreateWindow failed (last error 0x%lX)\n",
56 GetLastError());
57 return(1);
58 }
59
60 ShowWindow(hWnd, nCmdShow);
61
62 while(GetMessage(&msg, NULL, 0, 0))
63 {
64 TranslateMessage(&msg);
65 DispatchMessage(&msg);
66 }
67
68 return msg.wParam;
69 }
70
71 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
72 {
73 POINT pt;
74 switch(msg)
75 {
76 case WM_ACTIVATE:
77 switch(LOWORD(wParam))
78 {
79 case WA_ACTIVE:
80 case WA_CLICKACTIVE:
81 if(!ShowCaret(hWnd))
82 DbgPrint("ShowCaret(0x%x)\n", hWnd);
83 break;
84 case WA_INACTIVE:
85 if(!HideCaret(hWnd))
86 DbgPrint("HideCaret(0x%x)\n", hWnd);
87 break;
88 }
89 break;
90
91 case WM_KEYDOWN:
92 if(!GetCaretPos(&pt))
93 {
94 DbgPrint("GetCaretPos() failed!\n");
95 break;
96 }
97 switch(wParam)
98 {
99 case VK_LEFT:
100 pt.x -= CharWidth;
101 break;
102 case VK_UP:
103 pt.y -= CharHeight;
104 break;
105 case VK_RIGHT:
106 pt.x += CharWidth;
107 break;
108 case VK_DOWN:
109 pt.y += CharHeight;
110 break;
111 }
112 if(!SetCaretPos(pt.x, pt.y))
113 DbgPrint("SetCaretPos() failed!\n");
114 break;
115
116 case WM_RBUTTONDOWN:
117 if(!CreateCaret(hWnd, CaretBitmap, 0, 0))
118 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
119 else
120 if(!ShowCaret(hWnd))
121 DbgPrint("ShowCaret(0x%x)\n", hWnd);
122 break;
123
124 case WM_LBUTTONDOWN:
125 if(!CreateCaret(hWnd, (HBITMAP)0, CaretWidth, CaretHeight))
126 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
127 else
128 if(!ShowCaret(hWnd))
129 DbgPrint("ShowCaret(0x%x)\n", hWnd);
130 break;
131
132 case WM_CREATE:
133 if(!CreateCaret(hWnd, (HBITMAP)0, CaretWidth, CaretHeight))
134 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
135 else
136 if(!SetCaretPos(1, 1))
137 DbgPrint("SetCaretPos(%i, %i) failed!\n", 1, 1);
138 break;
139
140 case WM_DESTROY:
141 if(!DestroyCaret())
142 DbgPrint("DestroyCaret() failed!\n");
143 PostQuitMessage(0);
144 break;
145
146 default:
147 return DefWindowProc(hWnd, msg, wParam, lParam);
148 }
149 return 0;
150 }