Source

utclock / mainwin.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Code to handle the main window */
#include <Windows.H>
#include <String.H>
#include <StdLib.H>
#pragma hdrstop

#include "UTClock.RH" /* resources */
#include "MainWin.H" /* ours */
#include "Glbls.H" /* globals */

S_MAINWINDOW MainWindow; /* the main window (instance) */
/*-------------------------------------------------------------------------*/
void MainWindow_Construct( S_PMAINWINDOW Pobject)
{
	/* local vars */
   CREATESTRUCT cs; /* window creation info */
   const char defCap[] = "Universal Time Counter"; /* default caption of window */

	/* member hookup */
   Pobject->GetHandle = MainWindow_GetHandle;
   Pobject->GetCaption = MainWindow_GetCaption;
   Pobject->SetCaption = MainWindow_SetCaption;
   Pobject->GetWidth = MainWindow_GetWidth;
   Pobject->SetWidth = MainWindow_SetWidth;
   Pobject->GetHeight = MainWindow_GetHeight;
   Pobject->SetHeight = MainWindow_SetHeight;
   Pobject->GetTop = MainWindow_GetTop;
   Pobject->GetLeft = MainWindow_GetLeft;
   Pobject->SetTop = MainWindow_SetTop;
   Pobject->SetLeft = MainWindow_SetLeft;
   Pobject->IsVisible = MainWindow_IsVisible;
   Pobject->Show = MainWindow_Show;
   Pobject->Hide = MainWindow_Hide;
   Pobject->Repaint = MainWindow_Repaint;
   Pobject->UpdateWindowPos = MainWindow_UpdateWindowPos;
   Pobject->GetClientWidth = MainWindow_GetClientWidth;
   Pobject->GetClientHeight = MainWindow_GetClientHeight;
   Pobject->RestrictSize = MainWindow_RestrictSize;

   /* defaults */
   Pobject->_handle = NULL;
   Pobject->_visible = FALSE; /* hidden before created */
   Pobject->_reshow = FALSE; /* not shown yet */
   Pobject->_clientHeight = 0; /* N/A */
   Pobject->_clientWidth = 0; /* N/A */

   /* set the default caption of the window */
   Pobject->_caption = (char*)malloc(strlen(defCap)+1);
   strcpy(Pobject->_caption, defCap);

   Pobject->_width = CW_USEDEFAULT;
   Pobject->_height = CW_USEDEFAULT;
   Pobject->_top = CW_USEDEFAULT;
   Pobject->_left = CW_USEDEFAULT;

   Pobject->_classInfo.style = CS_HREDRAW | CS_VREDRAW;
   Pobject->_classInfo.lpfnWndProc = MainWndProc;
   Pobject->_classInfo.hInstance = _HInstance; /* identifies us */
   Pobject->_classInfo.hIcon = LoadIcon(_HInstance, MAKEINTRESOURCE(IDI_CLOCK));
   Pobject->_classInfo.hCursor = LoadCursor(NULL, IDC_ARROW);
   Pobject->_classInfo.hbrBackground = (HBRUSH)COLOR_WINDOW;
   Pobject->_classInfo.lpszMenuName = MAKEINTRESOURCE(IDM_MAINMENU);
   Pobject->_classInfo.lpszClassName = "UTClockClass";

   /* Register the window class */
   #if defined(_Windows) && !defined(BI_PLAT_WIN32) && !defined(__WIN32__) /* Win16? */
   if (!_HPrevInst) /* We are the first instance of a program? */
   {
   #endif /* Win16? */
   	if (!RegisterClass(&Pobject->_classInfo))
   	{
   		MessageBox(NULL, "Cannot register main window class, abandon all hope!", "UTClock - Fatal Error", MB_OK + MB_ICONSTOP + MB_SYSTEMMODAL);
      	abort();
   	}
   #if defined(_Windows) && !defined(BI_PLAT_WIN32) && !defined(__WIN32__) /* Win16? */
   }
   #endif /* Win16? */

   /* create a window creation structure to pass to the window proc */
   cs.lpCreateParams = &Pobject->_classInfo; /* unsure this is correct */
   cs.hInstance = _HInstance; /* it's us! */
   cs.hMenu = LoadMenu(_HInstance, MAKEINTRESOURCE(IDM_MAINMENU)); /* load and store the menu */
   Pobject->_menu = cs.hMenu; /* store the handle in a good place for destrcution later */
   cs.hwndParent = NULL;
   cs.cy = Pobject->_height;
   cs.cx = Pobject->_width;
   cs.y = Pobject->_top;
   cs.x = Pobject->_left;
   cs.style = Pobject->_classInfo.style;
   cs.lpszName = Pobject->_caption; /* not sure */
   cs.lpszClass = Pobject->_classInfo.lpszClassName;
   cs.dwExStyle = 0; /* N/A */

   /* create the window */
   Pobject->_handle = CreateWindow(Pobject->_classInfo.lpszClassName,
   							Pobject->_caption,
                  		WS_BORDER | WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_THICKFRAME,
   							Pobject->_left,
                        Pobject->_top,
                  		Pobject->_width,
                        Pobject->_height,
                  		HWND_DESKTOP,
                  		NULL, /* the menu is the class's */
                  		_HInstance,
                  		&cs);
	return;
}
/*-------------------------------------------------------------------------*/
void MainWindow_Destruct(S_PMAINWINDOW Pobject)
{
	if (Pobject->_handle)
   {
   	DestroyWindow(Pobject->_handle);
      Pobject->_handle = NULL;
   }
   if (Pobject->_classInfo.lpszClassName)
   {
   	UnregisterClass(Pobject->_classInfo.lpszClassName, _HInstance);
      Pobject->_classInfo.lpszClassName = NULL;
   }
	if (Pobject->_classInfo.hIcon)
   {
   	DestroyIcon(Pobject->_classInfo.hIcon);
      Pobject->_classInfo.hIcon = NULL;
   }
   if (Pobject->_menu)
   {
   	DestroyMenu(Pobject->_menu);
      Pobject->_menu = NULL;
   }
   if (Pobject->_caption)
   {
   	free(Pobject->_caption);
      Pobject->_caption = 0;
   }
	return;
}
/*-------------------------------------------------------------------------*/
HWND MainWindow_GetHandle(void* parent) /* gets the window handle of the window */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   return s->_handle; /* give handle */
}
/*-------------------------------------------------------------------------*/
const char* MainWindow_GetCaption(void* parent) /* gets the caption of the window */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   return s->_caption; /* give pointer to caption */
}
/*-------------------------------------------------------------------------*/
const char* MainWindow_SetCaption(void* parent, const char* NewCaption) /* sets the window caption */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */

   if (s->_caption) /* already have a caption? */
   {
   	free(s->_caption); /* bin it */
      s->_caption = NULL; /* mark as free */
   }

   if (NewCaption) /* pointer valid? */
   {
   	if (strlen(NewCaption) > 0) /* some text to copy? */
      {
      	s->_caption = (char*)malloc(strlen(NewCaption)+1); /* allocate space for new caption */
         if (s->_caption) /* space allocation successful? */
         	strcpy(s->_caption, NewCaption); /* copy caption text */
      }
   } /* (NewCaption) */

   /* caption copied, now to update the window */
   SetWindowText(s->GetHandle(s), s->_caption);

   return s->_caption; /* return our buffers address (don't know why) */
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetWidth(void* parent)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT WindowRect; /* window's rectangular area */

   GetWindowRect(s->GetHandle(s), &WindowRect); /* ask windows for info */
   s->_width = WindowRect.right - WindowRect.left; /* update object */

   return s->_width; /* give width */
}
/*-------------------------------------------------------------------------*/
int MainWindow_SetWidth(void* parent, int NewWidth) /* Sets width, returns old width */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   int oldwidth = s->_width; /* save old */
   s->_width = NewWidth; /* set new */

   /* do window processing if required */
   if (oldwidth != NewWidth) /* differs? */
   {
   	s->UpdateWindowPos(s);
   }

   return oldwidth; /* give old */
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetHeight(void* parent)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT WindowRect; /* window's rectangular area */

   GetWindowRect(s->GetHandle(s), &WindowRect); /* ask windows for info */
   s->_height = WindowRect.bottom - WindowRect.top; /* update object */

   return s->_height; /* give height */
}
/*-------------------------------------------------------------------------*/
int MainWindow_SetHeight(void* parent, int NewHeight) /* sets height, returns old height */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   int oldheight = s->_height; /* save old */
   s->_height = NewHeight; /* set new */

   /* do window processing if required */
   if (oldheight != NewHeight) /* differs? */
   {
   	s->UpdateWindowPos(s);
   }

   return oldheight; /* give old value to caller */
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetTop(void* parent)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT WindowRect; /* window's rectangular area */

   GetWindowRect(s->GetHandle(s), &WindowRect); /* ask windows for info */
   s->_top = WindowRect.top; /* update object */

   return s->_top; /* give top */
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetLeft(void* parent)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT WindowRect; /* window's rectangular area */

   GetWindowRect(s->GetHandle(s), &WindowRect); /* ask windows for info */
   s->_left = WindowRect.left; /* update object */

   return s->_left; /* give left */
}
/*-------------------------------------------------------------------------*/
int MainWindow_SetTop(void* parent, int NewTop)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   int oldtop = s->_top; /* save old */
   s->_top = NewTop; /* set new */

   /* do window processing if required */
   if (NewTop != oldtop) /* Differs? */
   {
   	s->UpdateWindowPos(s);
   }

   return oldtop; /* give old value */
}
/*-------------------------------------------------------------------------*/
int MainWindow_SetLeft(void* parent, int NewLeft)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   int oldleft = s->_left; /* save old */
   s->_left = NewLeft; /* set new */

   /* do window processing if required */
   if (NewLeft != oldleft) /* differs? */
   {
   	s->UpdateWindowPos(s);
   }

   return oldleft; /* give old value */
}
/*-------------------------------------------------------------------------*/
BOOL MainWindow_IsVisible(void* parent) /* returns _visible */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   return s->_visible; /* give visibility flag of window */
}
/*-------------------------------------------------------------------------*/
BOOL MainWindow_Show(void* parent) /* shows the window, rets FALSE if already so */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   if (!s->_visible) /* window hidden? */
   {
   	ShowWindow(s->_handle, (s->_reshow) ? SW_SHOW : _nCmdShow); /* show the window (according to the command line sent to WinMain() */
      UpdateWindow(s->_handle); /* posts us back a paint message */
      s->_visible = TRUE; /* remember it's shown */
      s->_reshow = TRUE; /* next will be a reshow */
      return TRUE; /* success */
   }
   return FALSE; /* failure (already shown) */
}
/*-------------------------------------------------------------------------*/
BOOL MainWindow_Hide(void* parent) /* hides the window, rets FALSE if not visible */
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   if (s->_visible) /* window visible? */
   {
   	ShowWindow(s->_handle, SW_HIDE); /* hide the window */
      s->_visible = FALSE; /* remember it's hidden */
      return TRUE; /* success */
   }
   return TRUE; /* success (already hidden) */
}
/*-------------------------------------------------------------------------*/
void MainWindow_Repaint(void* parent)
{
	/* maunual repaint of main window */

   /* locals */
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT updateRegion; /* rectangle to draw */
   HDC hDC; /* Device context */
   PAINTSTRUCT ps; /* paint information */

	if (GetUpdateRect(s->_handle, &updateRegion, TRUE) == 0) /* want to know where to redraw */
   {
   	/* there's nothing to redraw */
      return; /* don't waste processing time */
   }

   /* draw region here */
   hDC = BeginPaint(s->_handle, &ps); /* get a device context */

   MoveToEx(hDC, 0, 0, NULL);
   LineTo(hDC, s->GetClientWidth(s)-1, 0); /* draw line across top of client area (it separates the menu from the window) */

   EndPaint(s->GetHandle(s), &ps); /* release the device context */

	return; /* done */
}
/*-------------------------------------------------------------------------*/
void MainWindow_UpdateWindowPos(void* parent)
{
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
	MoveWindow(s->GetHandle(s), s->GetLeft(s), s->GetTop(s), s->GetWidth(s), s->GetHeight(s), TRUE);
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetClientWidth(void* parent)
{
	/* locals */
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT wr; /* Window's rect area */

   GetClientRect(s->GetHandle(s), &wr); /* ask windows */
   s->_clientWidth = wr.right; /* right was assigned a width by windows, it's not really a right */

   return s->_clientWidth; /* give to caller */
}
/*-------------------------------------------------------------------------*/
int MainWindow_GetClientHeight(void* parent)
{
	/* locals */
	S_PMAINWINDOW s = (S_MAINWINDOW*)parent; /* convert pointer */
   RECT wr; /* Window's rect area */

   GetClientRect(s->GetHandle(s), &wr); /* ask windows */
   s->_clientHeight = wr.bottom; /* bottom was assigned a height by Windows, it's not what it says it is at all */

   return s->_clientHeight; /* give to caller */
}
/*-------------------------------------------------------------------------*/
#pragma argsused /* stop parameter not used warning */
void MainWindow_RestrictSize(void* parent, MINMAXINFO FAR* PMinMaxInfo)
{
	/* locals */
	RECT DesktopWindowRect;

	/* stop the window being over-minimized (keep it reasonable) */
	PMinMaxInfo->ptMinTrackSize.x = 250;
   PMinMaxInfo->ptMinTrackSize.y = 100;
   /* Stop oversize, remember that a small clock should not fill the entire screen */
   PMinMaxInfo->ptMaxTrackSize.x = 375;
   PMinMaxInfo->ptMaxTrackSize.y = 250;
   /* If the user really tries to maximize this small clock, let it fill half the screen */
   GetWindowRect(GetDesktopWindow(), &DesktopWindowRect); /* get desktop dimensions */
   PMinMaxInfo->ptMaxSize.y = (DesktopWindowRect.bottom-1) /2;
   PMinMaxInfo->ptMaxSize.x = (DesktopWindowRect.right-1) /2;
	return; /* message has been processed */

   /* We did not need to know the parent window, but we had it
   sent to use to keep convention with all the other member
   functions */
}
/*-------------------------------------------------------------------------*/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	/* Windowing procedure for this window */
   switch (msg)
   {
   	case WM_CLOSE : /* close the window */
      {
         MainWindow_Destruct(&MainWindow); /* destroy app object, this calls DestroyWindow() */
      	break;
      }
      case WM_DESTROY : /* window is being destroyed */
      {
      	PostQuitMessage(0); /* want to stop message loop now */
         break;
      }
      case WM_GETMINMAXINFO : /* Forced min/max sizes */
      {
      	MainWindow.RestrictSize(&MainWindow, (MINMAXINFO FAR*)lParam);
      	break; /* message has been processed */
      }
      case WM_COMMAND : /* a menu message? */
      {
      	switch (wParam) /* switch on menu item */
         {
            case CM_WINDOW_EXIT : /* request to close */
            {
            	BYTE msgrec; /* message not recieved yet */
               do
               {
               	msgrec = (BYTE)PostMessage(MainWindow.GetHandle(&MainWindow), WM_CLOSE, 0, 0L); /* request to close main window */
               } while (!msgrec); /* persist until message is recieved */

               break; /* end of menuitem */
            }
            case CM_WINDOW_REPAINT : /* request to repaint? */
      		{
      			UpdateWindow(MainWindow.GetHandle(&MainWindow)); /* force paint of window */
         		break; /* end of menuitem */
      		}
            default : /* oops! */
            {
            	MessageBox(MainWindow.GetHandle(&MainWindow), "Invalid menu item", NULL, MB_OK);
            }
         }
         break; /* end of message */
      }
      case WM_PAINT : /* window needs repainting? */
      {
      	MainWindow.Repaint(&MainWindow); /* window specific painting */
         return DefWindowProc(hwnd, msg, wParam, lParam); /* do windows window repaint also */
      }
   	default : /* not a message I wish to process */
      {
      	/* call default processor */
      	return DefWindowProc(hwnd, msg, wParam, lParam);
      }
   } /* end of message switch */

   return 0; /* Processed given message */
}
/*-------------------------------------------------------------------------*/