Advertisement
alien_fx_fiend

Dictionary GUI-Based Win32 (Outmoded)

Jul 29th, 2024 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.21 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.             DestroyWindow(hwnd);
  269.             PostQuitMessage(0);
  270.             return TRUE;
  271.         }
  272.         break;
  273.  
  274.     case WM_CLOSE:
  275.         DestroyWindow(hwnd);
  276.         PostQuitMessage(0);
  277.         return TRUE;
  278.     }
  279.     return FALSE;
  280. }
  281.  
  282. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
  283.     HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
  284.     if (!hDlg) {
  285.         MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
  286.         return 1;
  287.     }
  288.  
  289.     ShowWindow(hDlg, nCmdShow);
  290.  
  291.     MSG msg;
  292.     BOOL bRet;
  293.     while ((bRet = GetMessage(&msg, nullptr, 0, 0)) != 0) {
  294.         if (bRet == -1) {
  295.             // Handle the error and possibly exit
  296.             break;
  297.         }
  298.         else if (!IsWindow(hDlg) || !IsDialogMessage(hDlg, &msg)) {
  299.             TranslateMessage(&msg);
  300.             DispatchMessage(&msg);
  301.         }
  302.     }
  303.  
  304.     return (int)msg.wParam;
  305. }
  306.  
  307. Resource.rc::
  308. #include <windows.h>
  309. #include "resource.h"
  310.  
  311. IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
  312. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  313. CAPTION "Dictionary Application"
  314. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  315. BEGIN
  316. LTEXT           "Word:", -1, 10, 10, 30, 8
  317. EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  318. LTEXT           "Definition:", -1, 10, 30, 40, 8
  319. EDITTEXT        IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL
  320. PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  321. PUSHBUTTON      "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
  322. PUSHBUTTON      "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
  323. PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 200, 8, 50, 14
  324. LTEXT           "Search:", -1, 10, 50, 30, 8
  325. EDITTEXT        IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
  326. LISTBOX         IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
  327. END
  328.  
  329. IDD_INPUT DIALOGEX 0, 0, 200, 100
  330. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  331. CAPTION "Input"
  332. FONT 8, "MS Shell Dlg"
  333. BEGIN
  334. LTEXT           "Input:", -1, 7, 14, 50, 8
  335. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  336. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  337. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  338. END
  339.  
  340. Resource.h::
  341. #ifndef RESOURCE_H
  342. #define RESOURCE_H
  343.  
  344. #define IDD_MAINDIALOG                  101
  345. #define IDC_WORD_INPUT                  1001
  346. #define IDC_DEFINITION_INPUT            1002
  347. #define IDC_ADD_WORD                    1003
  348. #define IDC_DELETE_WORD                 1004
  349. #define IDC_SEARCH_INPUT                1005
  350. #define IDC_LISTBOX                     1006
  351. #define IDC_INPUT                       1007  // Add this line if you're using IDC_INPUT
  352. #define IDC_EDIT_WORD                   1008
  353. #define IDC_VIEW_DEFINITION             1009
  354. #define IDD_INPUT                       1010
  355.  
  356. #endif // RESOURCE_H
  357.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement