#include "task_manager.
h"
#include <windows.h>
#include <string>
#include <vector>
#include <tuple>
#include <iostream>
#include "database.h"
// Global window handles
HWND hTaskList, hAddBtn, hTodoBtn;
// Function declarations
void ShowAddTaskWindow(HINSTANCE hInstance);
void ShowTodoListWindow(HINSTANCE hInstance);
LRESULT CALLBACK TaskListProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam) {
switch (msg) {
case WM_CREATE:
// Create the ListBox, buttons for adding tasks and viewing to-do list
hTaskList = CreateWindowW(L"LISTBOX", nullptr, WS_CHILD | WS_VISIBLE |
WS_VSCROLL | LBS_NOTIFY,
20, 20, 400, 200, hwnd, nullptr, nullptr, nullptr);
hAddBtn = CreateWindowW(L"BUTTON", L"Add Task", WS_CHILD | WS_VISIBLE |
BS_PUSHBUTTON,
440, 20, 100, 30, hwnd, (HMENU)1, nullptr, nullptr);
hTodoBtn = CreateWindowW(L"BUTTON", L"To-Do List", WS_CHILD |
WS_VISIBLE | BS_PUSHBUTTON,
440, 60, 100, 30, hwnd, (HMENU)2, nullptr, nullptr);
ReloadTaskList(hTaskList);
break;
case WM_COMMAND:
if (LOWORD(wParam) == 1) {
ShowAddTaskWindow(GetModuleHandleW(nullptr)); // Open add_task.cpp
window
}
else if (LOWORD(wParam) == 2) {
ShowTodoListWindow(GetModuleHandleW(nullptr)); // Open to-do list
window
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
return 0;
}
void ReloadTaskList(HWND hListBox) {
// Clear the ListBox
SendMessageW(hListBox, LB_RESETCONTENT, 0, 0);
// Load all tasks from the database
auto tasks = db_LoadAllTasks();
// Display each task
for (const auto& task : tasks) {
std::wstring displayText;
// Unpack the tuple
int id;
std::string name, deadline, timeRequired;
std::tie(id, name, deadline, timeRequired) = task;
// Format: TaskName [Deadline] - TimeRequired ( if completed)
displayText = std::wstring(name.begin(), name.end()) +
L" [" + std::wstring(deadline.begin(), deadline.end()) +
L"] - " + std::wstring(timeRequired.begin(), timeRequired.end());
// Add to ListBox
SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)displayText.c_str());
}
}
int ShowTaskListWindow(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSW wc = { 0 };
wc.lpfnWndProc = TaskListProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"TaskListWindow";
RegisterClassW(&wc);
HWND hwnd = CreateWindowW(
L"TaskListWindow",
L"Task Scheduler - Task List",
WS_OVERLAPPEDWINDOW,
100, 100, 600, 300,
nullptr, nullptr, hInstance, nullptr
);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}