Skip to content

Commit

Permalink
added save feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wisehackermonkey committed Sep 23, 2023
1 parent ce185c9 commit 58e71de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Binary file modified build/jot.exe
Binary file not shown.
29 changes: 25 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <windows.h>
#include <stdio.h>

#define ID_TEXTBOX 1
#define ID_ACCEL_SELECTALL 2
#define ID_ACCEL_CLOSE 3
#define ID_ACCEL_SAVE 4

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

Expand All @@ -12,17 +14,18 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdsho
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"JotEdit";
wc.lpszClassName = L"Jot";
wc.lpfnWndProc = WindowProcedure;

if(!RegisterClassW(&wc))
return -1;

HWND hwnd = CreateWindowW(L"JotEdit", L"Jot", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 600, NULL, NULL, NULL, NULL);
HWND hwnd = CreateWindowW(L"Jot", L"Jot", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 600, NULL, NULL, NULL, NULL);

ACCEL accel[] = {
{FCONTROL | FVIRTKEY, 'A', ID_ACCEL_SELECTALL},
{FCONTROL | FVIRTKEY, 'W', ID_ACCEL_CLOSE},
{FCONTROL | FVIRTKEY, 'S', ID_ACCEL_SAVE},
};

HACCEL hAccel = CreateAcceleratorTable(accel, sizeof(accel) / sizeof(ACCEL));
Expand All @@ -40,6 +43,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdsho

return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
static HWND hwndTextbox;
static HBRUSH hBrush;
Expand All @@ -49,7 +53,7 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
hwndTextbox = CreateWindowW(L"Edit", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 500, 600, hwnd, (HMENU)ID_TEXTBOX, NULL, NULL);
hBrush = CreateSolidBrush(RGB(0x44, 0x46, 0x54));
SetFocus(hwndTextbox); // Set focus to the text box
SetFocus(hwndTextbox);
break;

case WM_COMMAND:
Expand All @@ -61,14 +65,31 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
case ID_ACCEL_CLOSE:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_ACCEL_SAVE:
{
int length = GetWindowTextLength(hwndTextbox);
if (length > 0) {
char *buffer = malloc(length + 1); // Dynamically allocate memory for the buffer
GetWindowTextA(hwndTextbox, buffer, length + 1); // Get the text from the text box

FILE *file = fopen("saved_text.txt", "w"); // Open a file in write mode
if (file) {
fwrite(buffer, 1, length, file); // Write the buffer to the file
fclose(file); // Close the file
}
free(buffer); // Free the allocated memory
}
}
break;

}
}
break;

case WM_CTLCOLOREDIT:
if ((HWND)lp == hwndTextbox) {
SetBkColor((HDC)wp, RGB(0x44, 0x46, 0x54));
SetTextColor((HDC)wp, RGB(0xFF, 0xFF, 0xFF)); // setting the text color to white, you can change as you wish
SetTextColor((HDC)wp, RGB(0xFF, 0xFF, 0xFF));
return (LRESULT)hBrush;
}
break;
Expand Down

0 comments on commit 58e71de

Please sign in to comment.