-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_wnd.c
1335 lines (1164 loc) · 34.4 KB
/
main_wnd.c
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Shlwapi.h>
#include <winuser.h>
#include "main_wnd.h"
#include "resource.h"
#include "tray_icon.h"
#include "about_dialog.h"
#include "defs.h"
#include "working_hours.h"
#include "dbg_wnd.h"
/******************************************************************************/
/* Private */
/******************************************************************************/
/**
* @brief Main window data
*/
typedef struct tagMAINWNDDATA
{
HWND hwnd; /**< Main window handle */
HICON hMainIcon; /**< Main Icon handle */
HMENU hTrayIconMenu; /**< Menu for the tray icon */
BOOL bOnStartup; /**< Is being run on startup */
HFONT hWorkHoursFont; /**< Font for the hours worked in the dialog box */
COLORREF crWorkHoursCol;/**< Color of the working hours */
WHTIME whtLastUpdate; /**< Time of last window update */
WHTIME whtWorkingTime; /**< Time spent working */
BOOL bPauseChangeUpdate;/**< Allow W-H to update when pause time changes */
WHLUA WhLua; /**< Working hours state */
LPSTR lpLuaCode; /**< String containing the current Lua code */
BOOL bLuaReady; /**< Lua state initialized and not being editted */
HWND hwndDebug; /**< Debug window handle */
BOOL bIsPaused; /**< Is work paused */
HICON hPauseIcon; /**< Pause work icon */
HICON hResumeIcon; /**< Resume work icon */
} MAINWNDDATA, *LPMAINWNDDATA;
/* Tray icon notification messages */
#define WM_TRAY_ICON (WM_USER + 0)
/* ID of the tray icon */
#define TRAY_ICON_ID 0
/* Timer ID for the working hours update */
#define WH_TIMER_ID 10
/* Working hours timer period in milliseconds */
#define WH_TIMER_PERIOD 1000
/* Time format used by the working hours */
#define WH_TIME_FORMAT "HH':'mm"
/* Get Main window data pointer */
#define GetMainWindowData(hWnd) ((LPMAINWNDDATA)(GetWindowLongPtr((hWnd), \
GWLP_USERDATA)))
/* Path to file containing the Lua code */
#define WH_LUA_CODE_FILE "working-hours.lua"
/**
* @brief Number of a windows message sent on creation of the taskbar
*
*/
static UINT g_uTakbarCreatedMessage = 0;
/**
* @brief Update notification icon balloon text
*
* @param hwnd Main window handle
*
* @return FALSE on failure
*/
static BOOL UpdateTryIconText(
HWND hwnd
)
{
TCHAR lptstrTimeWorked[64];
TCHAR lptstrTrayBalloon[64];
DWORD_PTR lpArgs[] = {(DWORD_PTR)lptstrTimeWorked};
/* Get the current working time */
if(0 == GetDlgItemText(hwnd, IDC_WORK_TIME, lptstrTimeWorked,
sizeof(lptstrTimeWorked)/sizeof(TCHAR)))
return FALSE;
/* Format balloon text */
if (!FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, TEXT(PROJECT_NAME) TEXT(" (%1)"), 0,
0, lptstrTrayBalloon, sizeof(lptstrTrayBalloon)/sizeof(TCHAR),
(va_list *)lpArgs))
return FALSE;
/* Update tray icon balloon */
TrayUpdateText(hwnd, TRAY_ICON_ID, lptstrTrayBalloon);
return TRUE;
}
/**
* @brief Get the current time in W-H format
*
* @param[out] whtime Current time
*/
static VOID GetCurrentWHTime(LPWHTIME whtime)
{
SYSTEMTIME st;
/* Get current time */
GetLocalTime(&st);
/* Convert */
WhSystimeToWht(whtime, &st);
}
/**
* @brief Read time of arrival from the GUI
*
* @param hwnd Main window
* @param[out] whtime Structure to obtain the time
* @return TRUE on success
*/
static BOOL ReadArrivalWHTime(HWND hwnd, LPWHTIME whtime)
{
SYSTEMTIME st;
if(GDT_VALID != SendDlgItemMessage(hwnd, IDC_ARR_TIME,
DTM_GETSYSTEMTIME, 0, (WPARAM)(&st)))
{
return FALSE;
}
/* Convert */
WhSystimeToWht(whtime, &st);
return TRUE;
}
/**
* @brief Update working hours text
*
* @param hwnd Main window handle
* @param[in] whtime Working hours
*/
static VOID UpdateWorkingHoursText(HWND hwnd, LPCWHTIME whtime)
{
SYSTEMTIME st;
TCHAR lptstrTimeWorked[64];
/* Convert */
WhWhtToSystime(&st, whtime);
/* Format time spent working into a string */
if(0 != GetTimeFormat(LOCALE_CUSTOM_DEFAULT, 0, &st,
TEXT(WH_TIME_FORMAT), lptstrTimeWorked,
(sizeof(lptstrTimeWorked)/sizeof(TCHAR)) - 1))
{
/* Update time worked control */
SetDlgItemText(hwnd, IDC_WORK_TIME, lptstrTimeWorked);
/* Invalidate entire working hours counter */
InvalidateRect(GetDlgItem(hwnd, IDC_WORK_TIME), NULL, FALSE);
}
}
/**
* @brief Update leave time text
*
* @param hwnd Main window handle
* @param[in] whtime Leave time
*/
static VOID UpdateLeaveTimeText(HWND hwnd, LPCWHTIME whtime)
{
SYSTEMTIME st;
TCHAR lptstrLeaveTime[64];
/* Convert */
WhWhtToSystime(&st, whtime);
/* Format time spent working into a string for tray icon */
if(0 != GetTimeFormat(LOCALE_CUSTOM_DEFAULT, 0, &st, TEXT(WH_TIME_FORMAT),
lptstrLeaveTime, (sizeof(lptstrLeaveTime)/sizeof(TCHAR)) - 1))
{
/* Update control text */
SetDlgItemText(hwnd, IDC_LEAVE_TIME, lptstrLeaveTime);
}
}
/**
* @brief Get the value of the pause time
*
* @param hwnd Main window handle
* @return Pause time in minutes
*/
static WORD GetPauseTime(
HWND hwnd
)
{
BOOL success = FALSE;
WORD pauseTime;
pauseTime = GetDlgItemInt(hwnd, IDC_PAUSE_TIME, &success, FALSE);
return success ? pauseTime : 0;
}
/**
* @brief Calculate minutes from W-H Time
*
* @param[in] whtime Time to convert
* @return Time in minutes
*/
static WORD WHTimeToMinutes(
LPCWHTIME whtime
)
{
return (whtime->wHour * 60) + whtime->wMinute;
}
/**
* @brief Procedure called when working hours count is to be updated
*
* @param hwnd Main window handle
* @param bForceUpdate Perform an update regardless the last update time value
*/
static VOID UpdateWorkingHours(
HWND hwnd,
BOOL bForceUpdate
)
{
LPMAINWNDDATA lpData = GetMainWindowData(hwnd);
WHTIME whtArrival, whtNow, whtNewWorkingTime, whtLeave;
WORD pauseTime;
/* Check if Lua is ready */
if(!lpData->bLuaReady)
return;
/* Get current time */
GetCurrentWHTime(&whtNow);
if(bForceUpdate || lpData->whtLastUpdate.wMinute != whtNow.wMinute ||
lpData->whtLastUpdate.wHour != whtNow.wHour)
{
/* Update last update time */
lpData->whtLastUpdate = whtNow;
/* Get arrival time */
if(!ReadArrivalWHTime(hwnd, &whtArrival))
{
return;
}
/* Get Pause Time */
pauseTime = GetPauseTime(hwnd);
/* Calculate current time spent working */
if(!WhCalculate(&(lpData->WhLua), &whtArrival, &whtNow, pauseTime,
&whtNewWorkingTime, &(lpData->crWorkHoursCol)))
{
return;
}
/* Increment pause time and re-calculate, if paused */
if (lpData->bIsPaused && !bForceUpdate)
{
if (lpData->whtWorkingTime.wMinute != whtNewWorkingTime.wMinute ||
lpData->whtWorkingTime.wHour != whtNewWorkingTime.wHour)
{
pauseTime += WHTimeToMinutes(&whtNewWorkingTime) -
WHTimeToMinutes(&(lpData->whtWorkingTime));
if(!WhCalculate(&(lpData->WhLua), &whtArrival, &whtNow,
pauseTime, &whtNewWorkingTime,
&(lpData->crWorkHoursCol)))
{
return;
}
}
}
/* Remember work time */
lpData->whtWorkingTime = whtNewWorkingTime;
/* Update pause time and prevent recursive call of update */
lpData->bPauseChangeUpdate = FALSE;
SetDlgItemInt(hwnd, IDC_PAUSE_TIME, pauseTime, FALSE);
lpData->bPauseChangeUpdate = TRUE;
UpdateWorkingHoursText(hwnd, &whtNewWorkingTime);
/* Update tray icon balloon */
UpdateTryIconText(hwnd);
/* Calculate leave time */
if(!WhLeaveTime(&(lpData->WhLua), &whtArrival, pauseTime, &whtLeave))
{
return;
}
UpdateLeaveTimeText(hwnd, &whtLeave);
}
}
/**
* @brief Get the handle of the monitor with the mouse cursor on
*
* @ret Monitor handle
*/
static HMONITOR MonitorFromCursor(
VOID
)
{
POINT pt;
if(!GetCursorPos(&pt))
{
pt.x = 0;
pt.y = 0;
}
return MonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
}
/**
* @brief Place the window in the center of the screen with mouse cursor
*
* @param hWnd Handle of the window to be moved
*/
static VOID CenterWindow(
HWND hWnd
)
{
MONITORINFO mi;
/* Move windows to the center of monitor */
mi.cbSize = sizeof(MONITORINFO);
if(FALSE != GetMonitorInfo(MonitorFromCursor(), &mi))
{
RECT rc;
if(TRUE == GetWindowRect(hWnd, &rc))
{
LONG lWidth = rc.right - rc.left;
LONG lHeight = rc.bottom - rc.top;
MoveWindow(hWnd,
((mi.rcWork.right - mi.rcWork.left) / 2) +
mi.rcWork.left - (lWidth / 2),
((mi.rcWork.bottom - mi.rcWork.top) / 2) +
mi.rcWork.top - (lHeight / 2),
lWidth,
lHeight,
TRUE);
}
}
}
/**
* @brief Show or hide main window
*
* @param hWnd Main window handle
* @param bShow FALSE to hide the window
*/
static VOID ShowMainWnd(
HWND hWnd,
BOOL bShow
)
{
LPMAINWNDDATA lpData = GetMainWindowData(hWnd);
if(bShow)
{
SetForegroundWindow(hWnd);
ShowWindow(hWnd, SW_SHOW);
if(NULL != lpData->hwndDebug)
ShowWindow(lpData->hwndDebug, SW_SHOW);
/* Move windows to the screen with cursor */
CenterWindow(hWnd);
}
else
{
ShowWindow(hWnd, SW_HIDE);
if(NULL != lpData->hwndDebug)
ShowWindow(lpData->hwndDebug, SW_HIDE);
}
}
/**
* @brief Destroy all objects in a Main Window data structure and free memory
* taken by this structure
*/
static VOID DestroyMainWndData(
LPMAINWNDDATA lpData
)
{
if(NULL != lpData)
{
if(NULL != lpData->hMainIcon)
DestroyIcon(lpData->hMainIcon);
if(NULL != lpData->hTrayIconMenu)
DestroyMenu(lpData->hTrayIconMenu);
if(NULL != lpData->hWorkHoursFont)
DeleteObject(lpData->hWorkHoursFont);
WhLuaDestroy(&(lpData->WhLua));
if(NULL != lpData->lpLuaCode)
HeapFree(g_hHeap,0, lpData->lpLuaCode);
if(NULL != lpData->hwndDebug)
DestroyWindow(lpData->hwndDebug);
if(NULL != lpData->hPauseIcon)
DestroyIcon(lpData->hPauseIcon);
if(NULL != lpData->hResumeIcon)
DestroyIcon(lpData->hResumeIcon);
HeapFree(g_hHeap, 0, lpData);
}
}
/**
* @brief Allocate and initialize main window data structure
*
* @return New Main window data structure pointer
*/
static LPMAINWNDDATA CreateMainWndData(VOID)
{
LPMAINWNDDATA lpData;
/* Allocate data structure associated with the main window */
lpData = HeapAlloc(g_hHeap, 0, sizeof(MAINWNDDATA));
if(NULL == lpData)
return NULL;
/* Initialize main window data */
lpData->hMainIcon = NULL;
lpData->hTrayIconMenu = NULL;
lpData->bOnStartup = FALSE;
lpData->hWorkHoursFont = NULL;
lpData->crWorkHoursCol = (COLORREF)GetSysColor(COLOR_BTNTEXT);
lpData->whtLastUpdate.wHour = 0;
lpData->whtLastUpdate.wMinute = 0;
lpData->whtWorkingTime.wHour = 0;
lpData->whtWorkingTime.wMinute = 0;
lpData->bPauseChangeUpdate = FALSE;
WhLuaInit(&(lpData->WhLua));
lpData->lpLuaCode = NULL;
lpData->bLuaReady = FALSE;
lpData->hwndDebug = NULL;
lpData->bIsPaused = FALSE;
lpData->hPauseIcon = NULL;
lpData->hResumeIcon = NULL;
return lpData;
}
/**
* @brief Process 'Run at startup' menu selection
*
* @param hwnd Main window handle
* @param bOnStartup Run on startup
*
* @return TRUE on success
*/
static BOOL OnRunAtStartup(
HWND hwnd,
BOOL bOnStartup
)
{
LPMAINWNDDATA lpData;
HKEY hKey;
/* Get main window data */
lpData = GetMainWindowData(hwnd);
/* Open Windows/Run key */
if (RegOpenKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0,
KEY_SET_VALUE | KEY_WOW64_32KEY, &hKey) !=
ERROR_SUCCESS)
{
return FALSE;
}
if (bOnStartup)
{
DWORD dwRes;
static TCHAR lpExeName[1024];
/* Get path to current EXE file */
dwRes = GetModuleFileName(NULL, lpExeName, 1024);
/* Return, if filename was nor received correctly */
if (dwRes == 0 && dwRes >= 1024)
{
RegCloseKey(hKey);
return FALSE;
}
/* Try to write filename of current executable to registry */
if (ERROR_SUCCESS != RegSetValueEx(hKey, lpProjectName, 0,
REG_SZ, (LPBYTE)lpExeName, (dwRes + 1) * sizeof(TCHAR)))
{
RegCloseKey(hKey);
return FALSE;
}
}
else
{
/* Delete registry value holding filename of VUT Disk Mapper */
RegDeleteValue(hKey, lpProjectName);
}
/* Check or uncheck the menu item */
CheckMenuItem(GetMenu(hwnd), IDM_RUNATSTARTUP,
MF_BYCOMMAND | (bOnStartup ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(lpData->hTrayIconMenu, IDM_RUNATSTARTUP,
MF_BYCOMMAND | (bOnStartup ? MF_CHECKED : MF_UNCHECKED));
/* Remember the settings */
lpData->bOnStartup = bOnStartup;
/* Close all registry keys */
RegCloseKey(hKey);
return TRUE;
}
/**
* @brief Replace Lua code buffer and load new Lua state
*
* The function will free the original buffer, if any is present and load it
* in a new Lua state.
*
* @param[in,out] lpData Main window data structure
* @param[in] lpNewLuaCode Buffer containing the new Lua code
*/
static BOOL LuaLoadCode(
LPMAINWNDDATA lpData,
LPSTR lpNewLuaCode
)
{
if(NULL != lpData->lpLuaCode)
{
HeapFree(g_hHeap, 0, lpData->lpLuaCode);
}
lpData->lpLuaCode = lpNewLuaCode;
/* Initialize Lua state */
lpData->bLuaReady = WhLuaReset(&(lpData->WhLua));
if(lpData->bLuaReady)
{
if(!WhLuaDoString(&(lpData->WhLua), "", lpData->lpLuaCode))
{
WhLuaErrorMessage(&(lpData->WhLua));
return FALSE;
}
}
return lpData->bLuaReady;
}
/**
* @brief Reload Lua code either form file or the default
*
* @param[in,out] lpData Window data structure
*
* @return @c TRUE on success
*/
static BOOL ReloadLuaCode(
LPMAINWNDDATA lpData
)
{
LPSTR lpLuaCode;
/* Load the default Lua code */
lpLuaCode = WhLuaLoadCode(TEXT(WH_LUA_CODE_FILE));
if(NULL == lpLuaCode)
lpLuaCode = WhLuaLoadDefaultCode();
/* Set the loaded Lua code */
return LuaLoadCode(lpData, lpLuaCode);
}
/**
* @brief Handle menu item to load default code
*
* @param hwnd Main window handle
*/
static VOID OnScriptLoadDefault(
HWND hwnd
)
{
LPSTR lpLuaCode;
LPMAINWNDDATA lpData;
/* Get main window data */
lpData = GetMainWindowData(hwnd);
lpLuaCode = WhLuaLoadDefaultCode();
if(LuaLoadCode(lpData, lpLuaCode) &&
WhLuaSaveCode(TEXT(WH_LUA_CODE_FILE), lpLuaCode))
{
MessageBox(hwnd, TEXT("Successfully loaded the default Lua script. "
"Default script stored in '" WH_LUA_CODE_FILE "' file."),
TEXT("Lua Export"), MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox(hwnd, TEXT("Could not load the default Lua script."),
TEXT("Lua Export"), MB_OK | MB_ICONWARNING);
}
}
/**
* @brief Handle menu item to reload Lua code
*
* @param hwnd Main window handle
*/
static VOID OnScriptReload(
HWND hwnd
)
{
LPMAINWNDDATA lpData;
/* Get main window data */
lpData = GetMainWindowData(hwnd);
if(ReloadLuaCode(lpData))
{
MessageBox(hwnd, TEXT("Successfully reloaded the Lua script."),
TEXT("Lua Reload"), MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox(hwnd, TEXT("Could not reload the Lua script."),
TEXT("Lua Reload"), MB_OK | MB_ICONWARNING);
}
}
/**
* @brief Show the window displaying Lua debug messages
*
* @param hwnd Main window handle
* @param bShow Show or hide the debug window
*/
static VOID OnDbgWnd(
HWND hwnd,
BOOL bShow
)
{
LPMAINWNDDATA lpData;
/* Get main window data */
lpData = GetMainWindowData(hwnd);
if(bShow)
{
/* Destroy already existing window */
if(NULL != lpData->hwndDebug)
{
DestroyWindow(lpData->hwndDebug);
}
lpData->hwndDebug = DbgWndCreate(hwnd);
}
else
{
if(NULL != lpData->hwndDebug)
{
DestroyWindow(lpData->hwndDebug);
lpData->hwndDebug = NULL;
}
}
if(NULL != lpData->hwndDebug)
{
CheckMenuItem(GetMenu(hwnd), IDM_DBG_WND, MF_BYCOMMAND | MF_CHECKED);
}
else
{
CheckMenuItem(GetMenu(hwnd), IDM_DBG_WND, MF_BYCOMMAND | MF_UNCHECKED);
}
/* Inform Lua module about debug window status change */
WhLuaSetDebugWnd(&(lpData->WhLua), lpData->hwndDebug);
}
/**
* @brief Check if application is registered to run at startup
*
* @param hwnd Main window handle
*/
static VOID IsRegisteredToRunAtStartup(
HWND hwnd
)
{
LPMAINWNDDATA lpData;
HKEY hKey;
LONG lRes;
/* Get main window data */
lpData = GetMainWindowData(hwnd);
/* Open Windows/Run key */
if (RegOpenKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0,
KEY_QUERY_VALUE | KEY_WOW64_32KEY, &hKey) !=
ERROR_SUCCESS)
{
return;
}
/* Check if specified registry value exists */
lRes = RegQueryValueEx(hKey, lpProjectName, NULL,
NULL, NULL, NULL);
/* Close all registry keys */
RegCloseKey(hKey);
/* If value exists */
lpData->bOnStartup = (ERROR_SUCCESS == lRes);
/* Check or uncheck the menu item */
CheckMenuItem(GetMenu(hwnd), IDM_RUNATSTARTUP,
MF_BYCOMMAND | ((lpData->bOnStartup) ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(lpData->hTrayIconMenu, IDM_RUNATSTARTUP,
MF_BYCOMMAND | ((lpData->bOnStartup) ? MF_CHECKED : MF_UNCHECKED));
}
/**
* @brief Set arrival time to current time
*
* @param hwnd Main window hadle
*/
static VOID SetArrivalToNow(
HWND hwnd
)
{
SYSTEMTIME now;
GetLocalTime(&now);
SendDlgItemMessage(hwnd, IDC_ARR_TIME, DTM_SETSYSTEMTIME, GDT_VALID,
(LPARAM)(&now));
}
/******************************************************************************/
/* Windows Messages */
/******************************************************************************/
/**
* @brief Process WM_INITDIALOG message
*
* @param hwndInitialControl A handle to the control to receive the default
* keyboard focus
* @param[in] lpData Additional initialization data
* @return Set the keyboard focus
*/
static BOOL OnInitDialog(
HWND hwnd,
HWND hwndInitialControl,
LPVOID lpAdditionalData
)
{
LPMAINWNDDATA lpData = (LPMAINWNDDATA)lpAdditionalData;
/* Store window handle */
lpData->hwnd = hwnd;
/* Store Pointer to the data structure with the window */
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lpData);
/* Set icon */
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)lpData->hMainIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)lpData->hMainIcon);
/* Set play pause default icon */
SendDlgItemMessage(hwnd, IDC_PLAY_PAUSE, BM_SETIMAGE, IMAGE_ICON,
(LPARAM)lpData->hPauseIcon);
/* Add tray icon */
TrayIconAdd(hwnd, TRAY_ICON_ID, WM_TRAY_ICON, lpData->hMainIcon);
/* Check or uncheck the 'Run at startup' menu item */
IsRegisteredToRunAtStartup(hwnd);
/* Configure Pause time control */
SetDlgItemInt(hwnd, IDC_PAUSE_TIME, 0, FALSE);
SendDlgItemMessage(hwnd, IDC_PAUSE_TIME_SPIN, UDM_SETBUDDY,
(WPARAM)GetDlgItem(hwnd, IDC_PAUSE_TIME), 0);
SendDlgItemMessage(hwnd, IDC_PAUSE_TIME_SPIN, UDM_SETRANGE32, 0, 60 * 24);
/* Change arrival time format */
SendDlgItemMessage(hwnd, IDC_ARR_TIME, DTM_SETFORMAT, 0,
(LPARAM)TEXT(WH_TIME_FORMAT));
SetArrivalToNow(hwnd);
/* Create worked hours font */
lpData->hWorkHoursFont = CreateFont(46, 0, 0, 0, FW_BOLD, FALSE, FALSE,
FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
SendDlgItemMessage(hwnd, IDC_WORK_TIME, WM_SETFONT,
(WPARAM)lpData->hWorkHoursFont, MAKELPARAM(TRUE, 0));
/* Set window handle for parenting Lua error message-boxes */
WhLuaSetErrorParentWnd(&(lpData->WhLua), hwnd);
/* Reload Lua code form file or default */
ReloadLuaCode(lpData);
/* Start working hours update timer */
SetTimer(hwnd, WH_TIMER_ID, WH_TIMER_PERIOD, NULL);
/* Force an update working hours and leave time now */
UpdateWorkingHours(hwnd, TRUE);
return TRUE;
}
/**
* @brief called when window is to be closed
*
* @param hwnd Main window handle
*
* @return TRUE if message is processed
*/
static INT_PTR OnClose(
HWND hwnd
)
{
#ifndef _DEBUG
/* Hide the window */
ShowMainWnd(hwnd, FALSE);
#else
DestroyWindow(hwnd);
#endif
return TRUE;
}
/**
* @brief Called when window is to be destroyed
*
* @param hwnd Main window handle
*
* @return TRUE if message is processed
*/
static INT_PTR OnDestroy(
HWND hwnd
)
{
LPMAINWNDDATA lpData = GetMainWindowData(hwnd);
/* Remove tray icon */
TrayIconRemove(hwnd, TRAY_ICON_ID);
if(NULL != lpData)
{
/* Destroy data associated with the window */
DestroyMainWndData(lpData);
}
/* Remove pointer to non-existing data */
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)NULL);
/* Quit the application */
PostQuitMessage(0);
return TRUE;
}
/**
* @brief Received when a timer elapses
*
* @param hwnd Main window handle
* @param idEvent Timer ID
* @param lpTimerProc Pointer to procedure registered with the timer
*
* @return TRUE if message is processed
*/
static INT_PTR OnTimer(
HWND hwnd,
UINT_PTR idEvent,
TIMERPROC lpTimerProc
)
{
switch(idEvent)
{
case WH_TIMER_ID:
UpdateWorkingHours(hwnd, FALSE);
return TRUE;
}
return FALSE;
}
/**
* @brief Notification sent by a control
*
* @param hwnd Main window control
* @param lpNmhdr Pointer to notification information structure
*
* @return TRUE if message is processed
*/
static INT_PTR OnNotify(
HWND hwnd,
LPNMHDR lpNmhdr
)
{
switch(lpNmhdr->idFrom)
{
case IDC_ARR_TIME:
if(DTN_DATETIMECHANGE == lpNmhdr->code)
{
/* Update time spent working when change of arrival time has
* occurred */
UpdateWorkingHours(hwnd, TRUE);
return TRUE;
}
else
return FALSE;
}
return FALSE;
}
/**
* @brief Command sent by a control
*
* @param hwnd Main window handle
* @param wNotifCode Control-specific notification code
* @param wControlID Dialog Control ID
* @param hwndC Control window handle
*
* @return TRUE if message is processed
*/
static INT_PTR OnControlCommand(
HWND hwnd,
WORD wNotifCode,
WORD wControlID,
HWND hwndC
)
{
switch(wControlID)
{
case IDC_WORK_TIME:
/* Deselect this control and take focus away from this control */
if(GetFocus() == GetDlgItem(hwnd, IDC_WORK_TIME))
{
SendDlgItemMessage(hwnd, IDC_WORK_TIME, EM_SETSEL, (WPARAM)-1,
(LPARAM)0);
SetFocus(GetDlgItem(hwnd, IDC_ARR_TIME));
}
return FALSE;
case IDC_PLAY_PAUSE:
{
LPMAINWNDDATA lpData = GetMainWindowData(hwnd);
if (lpData->bIsPaused)
{
lpData->bIsPaused = FALSE;
SendDlgItemMessage(hwnd, IDC_PLAY_PAUSE, BM_SETIMAGE, IMAGE_ICON,
(LPARAM)lpData->hPauseIcon);
EnableWindow(GetDlgItem(hwnd, IDC_PAUSE_TIME), TRUE);
EnableWindow(GetDlgItem(hwnd, IDC_PAUSE_TIME_SPIN), TRUE);
}
else
{
lpData->bIsPaused = TRUE;
SendDlgItemMessage(hwnd, IDC_PLAY_PAUSE, BM_SETIMAGE, IMAGE_ICON,
(LPARAM)lpData->hResumeIcon);
EnableWindow(GetDlgItem(hwnd, IDC_PAUSE_TIME), FALSE);
EnableWindow(GetDlgItem(hwnd, IDC_PAUSE_TIME_SPIN), FALSE);
}
return TRUE;
}
case IDC_PAUSE_TIME:
if (EN_CHANGE == wNotifCode)
{
LPMAINWNDDATA lpData = GetMainWindowData(hwnd);
/* Update, only when it is allowed to prevent stack overflow by
* recursively calling update function */
if (lpData->bPauseChangeUpdate)
{
UpdateWorkingHours(hwnd, TRUE);
}
return TRUE;
}
else
{
return FALSE;
}
}
return FALSE;
}
/**
* @brief Command sent by a menu or an accelerator
*
* @param hwnd Main window handle
* @param wID Menu item or accelerator ID
* @param bIsMenu TRUE if sent by menu item
*
* @return TRUE if message is processed
*/
static INT_PTR OnMenuAccCommand(
HWND hwnd,
WORD wID,
BOOL bIsMenu
)
{