Advertisement
alien_fx_fiend

Dictionary GUI-Based Win32 (Multiline Definitions + Edit Word Working!!) (Search Buggy)

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