Advertisement
alien_fx_fiend

Dictionary GUI-Based Win32 (Multiline Edit Definitions) (Buggy SeeNew)

Jul 29th, 2024 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.58 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 "resource2.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.     HWND hDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), hwnd, [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  107.         switch (message) {
  108.         case WM_INITDIALOG:
  109.         {
  110.             wstring* definition = (wstring*)lParam;
  111.             SetDlgItemText(hDlg, IDC_DEFINITION_EDIT, definition->c_str());
  112.             delete definition;
  113.         }
  114.         return (INT_PTR)TRUE;
  115.         case WM_COMMAND:
  116.             if (LOWORD(wParam) == IDCANCEL) {
  117.                 EndDialog(hDlg, LOWORD(wParam));
  118.                 return (INT_PTR)TRUE;
  119.             }
  120.             break;
  121.         case WM_CLOSE:
  122.             EndDialog(hDlg, 0);
  123.             return (INT_PTR)TRUE;
  124.         }
  125.         return (INT_PTR)FALSE;
  126.         }, (LPARAM)new wstring(message));
  127.     ShowWindow(hDlg, SW_SHOW);
  128. }
  129. /*void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  130.     wstring message = L"Definition of '" + word + L"':\n\n" + definition;
  131.     MessageBox(hwnd, message.c_str(), L"Word Definition", MB_OK | MB_ICONINFORMATION);
  132. }*/
  133.  
  134. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition) {
  135.     Node* current = head;
  136.     while (current) {
  137.         if (current->data.word == oldWord) {
  138.             current->data.word = newWord;
  139.             current->data.definition = newDefinition;
  140.             return;
  141.         }
  142.         current = current->next;
  143.     }
  144. }
  145.  
  146. void listWords(const Node* head, vector<wstring>& words) {
  147.     words.clear();
  148.     const Node* current = head;
  149.     while (current) {
  150.         words.push_back(current->data.word);
  151.         current = current->next;
  152.     }
  153. }
  154.  
  155. void serializeDictionary(const Node* head, const wstring& filename) {
  156.     wofstream file(filename);
  157.     const Node* current = head;
  158.     while (current) {
  159.         file << current->data.word << L";" << current->data.definition << endl;
  160.         current = current->next;
  161.     }
  162.     file.close();
  163. }
  164.  
  165. void deserializeDictionary(Node*& head, const wstring& filename) {
  166.     wifstream file(filename);
  167.     wstring line;
  168.     while (getline(file, line)) {
  169.         size_t pos = line.find(L';');
  170.         if (pos != wstring::npos) {
  171.             wstring word = line.substr(0, pos);
  172.             wstring definition = line.substr(pos + 1);
  173.             addWord(head, word, definition);
  174.         }
  175.     }
  176.     file.close();
  177. }
  178.  
  179. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions) {
  180.     suggestions.clear();
  181.     const Node* current = head;
  182.     while (current && suggestions.size() < 5) {
  183.         if (current->data.word.find(keyword) != wstring::npos) {
  184.             suggestions.push_back(current->data.word);
  185.         }
  186.         current = current->next;
  187.     }
  188. }
  189.  
  190. void updateListBox(HWND hwnd, const vector<wstring>& words) {
  191.     HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
  192.     SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
  193.     for (const auto& word : words) {
  194.         SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)word.c_str());
  195.     }
  196. }
  197.  
  198. // Dialog procedure
  199. INT_PTR CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  200.     switch (uMsg) {
  201.     case WM_INITDIALOG:
  202.         deserializeDictionary(dictionary, L"dictionary.txt");
  203.         listWords(dictionary, words);
  204.         updateListBox(hwnd, words);
  205.         return TRUE;
  206.  
  207.     case WM_COMMAND:
  208.         switch (LOWORD(wParam)) {
  209.         case IDC_ADD_WORD: {
  210.             wchar_t word[256], definition[1024];
  211.             GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  212.             GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  213.             if (wcslen(word) > 0 && wcslen(definition) > 0) {
  214.                 addWord(dictionary, word, definition);
  215.                 listWords(dictionary, words);
  216.                 updateListBox(hwnd, words);
  217.                 serializeDictionary(dictionary, L"dictionary.txt");
  218.                 SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  219.                 SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  220.             }
  221.             return TRUE;
  222.         }
  223.  
  224.         case IDC_DELETE_WORD: {
  225.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  226.             if (index != LB_ERR) {
  227.                 wchar_t word[256];
  228.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  229.                 deleteWord(dictionary, word);
  230.                 listWords(dictionary, words);
  231.                 updateListBox(hwnd, words);
  232.                 serializeDictionary(dictionary, L"dictionary.txt");
  233.             }
  234.             return TRUE;
  235.         }
  236.  
  237.         case IDC_EDIT_WORD: {
  238.             wstring wordToEdit = InputBox(hwnd, L"Enter the word to edit:", L"Edit Word");
  239.             if (!wordToEdit.empty()) {
  240.                 Node* current = dictionary;
  241.                 while (current) {
  242.                     if (current->data.word == wordToEdit) {
  243.                         wstring newWord = InputBox(hwnd, L"Enter the new word:", L"Edit Word");
  244.                         if (!newWord.empty()) {
  245.                             wstring newDefinition = InputBox(hwnd, L"Enter the new definition:", L"Edit Word");
  246.                             if (!newDefinition.empty()) {
  247.                                 editWord(dictionary, wordToEdit, newWord, newDefinition);
  248.                                 listWords(dictionary, words);
  249.                                 updateListBox(hwnd, words);
  250.                                 serializeDictionary(dictionary, L"dictionary.txt");
  251.                             }
  252.                         }
  253.                         break;
  254.                     }
  255.                     current = current->next;
  256.                 }
  257.                 if (!current) {
  258.                     MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  259.                 }
  260.             }
  261.             return TRUE;
  262.         }
  263.  
  264.         case IDC_VIEW_DEFINITION: {
  265.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  266.             if (index != LB_ERR) {
  267.                 wchar_t word[256];
  268.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  269.                 Node* current = dictionary;
  270.                 while (current) {
  271.                     if (current->data.word == word) {
  272.                         DisplayDefinition(hwnd, current->data.word, current->data.definition);
  273.                         break;
  274.                     }
  275.                     current = current->next;
  276.                 }
  277.             }
  278.             return TRUE;
  279.         }
  280.  
  281.         case IDC_SEARCH_INPUT: {
  282.             if (HIWORD(wParam) == EN_CHANGE) {
  283.                 wchar_t query[256];
  284.                 GetDlgItemText(hwnd, IDC_SEARCH_INPUT, query, 256);
  285.                 vector<wstring> suggestions;
  286.                 searchWordRealtime(dictionary, query, suggestions);
  287.                 updateListBox(hwnd, suggestions);
  288.             }
  289.             return TRUE;
  290.         }
  291.  
  292.         case IDCANCEL:
  293.             DestroyWindow(hwnd);
  294.             PostQuitMessage(0);
  295.             return TRUE;
  296.         }
  297.         break;
  298.  
  299.     case WM_CLOSE:
  300.         DestroyWindow(hwnd);
  301.         PostQuitMessage(0);
  302.         return TRUE;
  303.     }
  304.     return FALSE;
  305. }
  306.  
  307. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
  308.     HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
  309.     if (!hDlg) {
  310.         MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
  311.         return 1;
  312.     }
  313.  
  314.     ShowWindow(hDlg, nCmdShow);
  315.  
  316.     MSG msg;
  317.     BOOL bRet;
  318.     while ((bRet = GetMessage(&msg, nullptr, 0, 0)) != 0) {
  319.         if (bRet == -1) {
  320.             // Handle the error and possibly exit
  321.             break;
  322.         }
  323.         else if (!IsWindow(hDlg) || !IsDialogMessage(hDlg, &msg)) {
  324.             TranslateMessage(&msg);
  325.             DispatchMessage(&msg);
  326.         }
  327.     }
  328.  
  329.     return (int)msg.wParam;
  330. }
  331.  
  332. resource2.h::
  333. #ifndef RESOURCE2_H
  334. #define RESOURCE2_H
  335.  
  336. #define IDD_MAINDIALOG                  101
  337. #define IDC_WORD_INPUT                  1001
  338. #define IDC_DEFINITION_INPUT            1002
  339. #define IDC_ADD_WORD                    1003
  340. #define IDC_DELETE_WORD                 1004
  341. #define IDC_SEARCH_INPUT                1005
  342. #define IDC_LISTBOX                     1006
  343. #define IDC_INPUT                       1007  // Add this line if you're using IDC_INPUT
  344. #define IDC_EDIT_WORD                   1008
  345. #define IDC_VIEW_DEFINITION             1009
  346. #define IDD_INPUT                       1010
  347. #define IDD_DEFINITION                  1011
  348. #define IDC_DEFINITION_EDIT             1012
  349.  
  350. #endif // RESOURCE_H
  351.  
  352. Resource2.rc::
  353. #include <windows.h>
  354. #include "resource2.h"
  355.  
  356. IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
  357. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  358. CAPTION "Dictionary Application"
  359. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  360. BEGIN
  361. LTEXT           "Word:", -1, 10, 10, 30, 8
  362. EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  363. LTEXT           "Definition:", -1, 10, 30, 40, 8
  364. EDITTEXT        IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL
  365. PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  366. PUSHBUTTON      "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
  367. PUSHBUTTON      "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
  368. PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 200, 8, 50, 14
  369. LTEXT           "Search:", -1, 10, 50, 30, 8
  370. EDITTEXT        IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
  371. LISTBOX         IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
  372. END
  373.  
  374. IDD_INPUT DIALOGEX 0, 0, 200, 100
  375. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  376. CAPTION "Input"
  377. FONT 8, "MS Shell Dlg"
  378. BEGIN
  379. LTEXT           "Input:", -1, 7, 14, 50, 8
  380. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  381. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  382. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  383. END
  384.  
  385. IDD_DEFINITION DIALOGEX 0, 0, 300, 200
  386. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  387. CAPTION "Definition"
  388. FONT 8, "MS Shell Dlg"
  389. BEGIN
  390. EDITTEXT        IDC_DEFINITION_EDIT, 7, 7, 286, 186, ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL
  391. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement