Advertisement
alien_fx_fiend

Dictionary GUI-Based App Win32 (Semi-Finished)

Jul 29th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.48 KB | None | 0 0
  1. realtime-search.cpp::
  2. #include <Windows.h>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <vector>
  8. #include "resource.h"
  9.  
  10. using namespace std;
  11.  
  12. // Structure to represent a dictionary entry
  13. struct DictionaryEntry {
  14.     wstring word;
  15.     wstring definition;
  16. };
  17.  
  18. // Structure to represent a node in the linked list
  19. struct Node {
  20.     DictionaryEntry data;
  21.     Node* next;
  22. };
  23.  
  24. Node* dictionary = nullptr;
  25. vector<wstring> words;
  26.  
  27. // Function prototypes
  28. void insertNode(Node*& head, const DictionaryEntry& entry);
  29. void addWord(Node*& head, const wstring& word, const wstring& definition);
  30. void deleteWord(Node*& head, const wstring& keyword);
  31. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title);
  32. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition);
  33. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition);
  34. void listWords(const Node* head, vector<wstring>& words);
  35. void serializeDictionary(const Node* head, const wstring& filename);
  36. void deserializeDictionary(Node*& head, const wstring& filename);
  37. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions);
  38. void updateListBox(HWND hwnd, const vector<wstring>& words);
  39.  
  40. // Function implementations
  41. void insertNode(Node*& head, const DictionaryEntry& entry) {
  42.     Node* newNode = new Node{ entry, nullptr };
  43.     if (!head) {
  44.         head = newNode;
  45.     }
  46.     else {
  47.         Node* current = head;
  48.         while (current->next) {
  49.             current = current->next;
  50.         }
  51.         current->next = newNode;
  52.     }
  53. }
  54.  
  55. void addWord(Node*& head, const wstring& word, const wstring& definition) {
  56.     insertNode(head, { word, definition });
  57. }
  58.  
  59. void deleteWord(Node*& head, const wstring& keyword) {
  60.     if (!head) return;
  61.  
  62.     if (head->data.word == keyword) {
  63.         Node* temp = head;
  64.         head = head->next;
  65.         delete temp;
  66.         return;
  67.     }
  68.  
  69.     Node* current = head;
  70.     while (current->next && current->next->data.word != keyword) {
  71.         current = current->next;
  72.     }
  73.  
  74.     if (current->next) {
  75.         Node* temp = current->next;
  76.         current->next = current->next->next;
  77.         delete temp;
  78.     }
  79. }
  80.  
  81. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title) {
  82.     wchar_t input[256] = L"";
  83.     if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
  84.         [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  85.             switch (message) {
  86.             case WM_INITDIALOG:
  87.                 SetWindowText(GetDlgItem(hDlg, IDC_INPUT), (LPCWSTR)lParam);
  88.                 return (INT_PTR)TRUE;
  89.             case WM_COMMAND:
  90.                 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
  91.                     EndDialog(hDlg, LOWORD(wParam));
  92.                     return (INT_PTR)TRUE;
  93.                 }
  94.                 break;
  95.             }
  96.             return (INT_PTR)FALSE;
  97.         }, (LPARAM)prompt.c_str()) == IDOK) {
  98.         GetDlgItemText(hwnd, IDC_INPUT, input, 256);
  99.         return input;
  100.     }
  101.     return L"";
  102. }
  103.  
  104. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  105.     wstring message = L"Definition of '" + word + L"':\n\n" + definition;
  106.     MessageBox(hwnd, message.c_str(), L"Word Definition", MB_OK | MB_ICONINFORMATION);
  107. }
  108.  
  109. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition) {
  110.     Node* current = head;
  111.     while (current) {
  112.         if (current->data.word == oldWord) {
  113.             current->data.word = newWord;
  114.             current->data.definition = newDefinition;
  115.             return;
  116.         }
  117.         current = current->next;
  118.     }
  119. }
  120.  
  121. void listWords(const Node* head, vector<wstring>& words) {
  122.     words.clear();
  123.     const Node* current = head;
  124.     while (current) {
  125.         words.push_back(current->data.word);
  126.         current = current->next;
  127.     }
  128. }
  129.  
  130. void serializeDictionary(const Node* head, const wstring& filename) {
  131.     wofstream file(filename);
  132.     const Node* current = head;
  133.     while (current) {
  134.         file << current->data.word << L";" << current->data.definition << endl;
  135.         current = current->next;
  136.     }
  137.     file.close();
  138. }
  139.  
  140. void deserializeDictionary(Node*& head, const wstring& filename) {
  141.     wifstream file(filename);
  142.     wstring line;
  143.     while (getline(file, line)) {
  144.         size_t pos = line.find(L';');
  145.         if (pos != wstring::npos) {
  146.             wstring word = line.substr(0, pos);
  147.             wstring definition = line.substr(pos + 1);
  148.             addWord(head, word, definition);
  149.         }
  150.     }
  151.     file.close();
  152. }
  153.  
  154. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions) {
  155.     suggestions.clear();
  156.     const Node* current = head;
  157.     while (current && suggestions.size() < 5) {
  158.         if (current->data.word.find(keyword) != wstring::npos) {
  159.             suggestions.push_back(current->data.word);
  160.         }
  161.         current = current->next;
  162.     }
  163. }
  164.  
  165. void updateListBox(HWND hwnd, const vector<wstring>& words) {
  166.     HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
  167.     SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
  168.     for (const auto& word : words) {
  169.         SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)word.c_str());
  170.     }
  171. }
  172.  
  173. // Dialog procedure
  174. INT_PTR CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  175.     switch (uMsg) {
  176.     case WM_INITDIALOG:
  177.         deserializeDictionary(dictionary, L"dictionary.txt");
  178.         listWords(dictionary, words);
  179.         updateListBox(hwnd, words);
  180.         return TRUE;
  181.  
  182.     case WM_COMMAND:
  183.         switch (LOWORD(wParam)) {
  184.         case IDC_ADD_WORD: {
  185.             wchar_t word[256], definition[1024];
  186.             GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  187.             GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  188.             if (wcslen(word) > 0 && wcslen(definition) > 0) {
  189.                 addWord(dictionary, word, definition);
  190.                 listWords(dictionary, words);
  191.                 updateListBox(hwnd, words);
  192.                 serializeDictionary(dictionary, L"dictionary.txt");
  193.                 SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  194.                 SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  195.             }
  196.             return TRUE;
  197.         }
  198.  
  199.         case IDC_DELETE_WORD: {
  200.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  201.             if (index != LB_ERR) {
  202.                 wchar_t word[256];
  203.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  204.                 deleteWord(dictionary, word);
  205.                 listWords(dictionary, words);
  206.                 updateListBox(hwnd, words);
  207.                 serializeDictionary(dictionary, L"dictionary.txt");
  208.             }
  209.             return TRUE;
  210.         }
  211.  
  212.         case IDC_EDIT_WORD: {
  213.             wstring wordToEdit = InputBox(hwnd, L"Enter the word to edit:", L"Edit Word");
  214.             if (!wordToEdit.empty()) {
  215.                 Node* current = dictionary;
  216.                 while (current) {
  217.                     if (current->data.word == wordToEdit) {
  218.                         wstring newWord = InputBox(hwnd, L"Enter the new word:", L"Edit Word");
  219.                         if (!newWord.empty()) {
  220.                             wstring newDefinition = InputBox(hwnd, L"Enter the new definition:", L"Edit Word");
  221.                             if (!newDefinition.empty()) {
  222.                                 editWord(dictionary, wordToEdit, newWord, newDefinition);
  223.                                 listWords(dictionary, words);
  224.                                 updateListBox(hwnd, words);
  225.                                 serializeDictionary(dictionary, L"dictionary.txt");
  226.                             }
  227.                         }
  228.                         break;
  229.                     }
  230.                     current = current->next;
  231.                 }
  232.                 if (!current) {
  233.                     MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  234.                 }
  235.             }
  236.             return TRUE;
  237.         }
  238.  
  239.         case IDC_VIEW_DEFINITION: {
  240.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  241.             if (index != LB_ERR) {
  242.                 wchar_t word[256];
  243.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  244.                 Node* current = dictionary;
  245.                 while (current) {
  246.                     if (current->data.word == word) {
  247.                         DisplayDefinition(hwnd, current->data.word, current->data.definition);
  248.                         break;
  249.                     }
  250.                     current = current->next;
  251.                 }
  252.             }
  253.             return TRUE;
  254.         }
  255.  
  256.         case IDC_SEARCH_INPUT: {
  257.             if (HIWORD(wParam) == EN_CHANGE) {
  258.                 wchar_t query[256];
  259.                 GetDlgItemText(hwnd, IDC_SEARCH_INPUT, query, 256);
  260.                 vector<wstring> suggestions;
  261.                 searchWordRealtime(dictionary, query, suggestions);
  262.                 updateListBox(hwnd, suggestions);
  263.             }
  264.             return TRUE;
  265.         }
  266.  
  267.         case IDCANCEL:
  268.             EndDialog(hwnd, 0);
  269.             return TRUE;
  270.         }
  271.         break;
  272.  
  273.     case WM_CLOSE:
  274.         EndDialog(hwnd, 0);
  275.         return TRUE;
  276.     }
  277.     return FALSE;
  278. }
  279.  
  280. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
  281.     HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
  282.     if (!hDlg) {
  283.         MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
  284.         return 1;
  285.     }
  286.  
  287.     ShowWindow(hDlg, nCmdShow);
  288.  
  289.     MSG msg;
  290.     while (GetMessage(&msg, 0, 0, 0)) {
  291.         if (!IsDialogMessage(hDlg, &msg)) {
  292.             TranslateMessage(&msg);
  293.             DispatchMessage(&msg);
  294.         }
  295.     }
  296.  
  297.     return (int)msg.wParam;
  298. }
  299.  
  300. Resource.rc::
  301. #include <windows.h>
  302. #include "resource.h"
  303.  
  304. IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
  305. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  306. CAPTION "Dictionary Application"
  307. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  308. BEGIN
  309. LTEXT           "Word:", -1, 10, 10, 30, 8
  310. EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  311. LTEXT           "Definition:", -1, 10, 30, 40, 8
  312. EDITTEXT        IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL
  313. PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  314. PUSHBUTTON      "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
  315. PUSHBUTTON      "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
  316. PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 200, 8, 50, 14
  317. LTEXT           "Search:", -1, 10, 50, 30, 8
  318. EDITTEXT        IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
  319. LISTBOX         IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
  320. END
  321.  
  322. IDD_INPUT DIALOGEX 0, 0, 200, 100
  323. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  324. CAPTION "Input"
  325. FONT 8, "MS Shell Dlg"
  326. BEGIN
  327. LTEXT           "Input:", -1, 7, 14, 50, 8
  328. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  329. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  330. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  331. END
  332.  
  333. /*IDD_INPUT DIALOGEX 0, 0, 200, 100
  334. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  335. CAPTION "Input"
  336. FONT 8, "MS Shell Dlg"
  337. BEGIN
  338. LTEXT           "Input:", -1, 7, 14, 50, 8
  339. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  340. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  341. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  342. END*/
  343.  
  344. //PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  345. //PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 260, 68, 50, 14
  346.  
  347. Resource.h::
  348. #ifndef RESOURCE_H
  349. #define RESOURCE_H
  350.  
  351. #define IDD_MAINDIALOG                  101
  352. #define IDC_WORD_INPUT                  1001
  353. #define IDC_DEFINITION_INPUT            1002
  354. #define IDC_ADD_WORD                    1003
  355. #define IDC_DELETE_WORD                 1004
  356. #define IDC_SEARCH_INPUT                1005
  357. #define IDC_LISTBOX                     1006
  358. #define IDC_INPUT                       1007  // Add this line if you're using IDC_INPUT
  359. #define IDC_EDIT_WORD                   1008
  360. #define IDC_VIEW_DEFINITION             1009
  361. #define IDD_INPUT                       1010
  362.  
  363. #endif // RESOURCE_H
  364.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement