Process Hacker
infodlg.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * information dialog
4  *
5  * Copyright (C) 2010 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <phapp.h>
24 #include <windowsx.h>
25 
26 static RECT MinimumSize = { -1, -1, -1, -1 };
27 
28 static INT_PTR CALLBACK PhpInformationDlgProc(
29  _In_ HWND hwndDlg,
30  _In_ UINT uMsg,
31  _In_ WPARAM wParam,
32  _In_ LPARAM lParam
33  )
34 {
35  switch (uMsg)
36  {
37  case WM_INITDIALOG:
38  {
39  PWSTR string = (PWSTR)lParam;
40  PPH_LAYOUT_MANAGER layoutManager;
41 
42  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
43 
44  SetDlgItemText(hwndDlg, IDC_TEXT, string);
45 
46  layoutManager = PhAllocate(sizeof(PH_LAYOUT_MANAGER));
47  PhInitializeLayoutManager(layoutManager, hwndDlg);
48  PhAddLayoutItem(layoutManager, GetDlgItem(hwndDlg, IDC_TEXT), NULL,
50  PhAddLayoutItem(layoutManager, GetDlgItem(hwndDlg, IDOK), NULL,
52  PhAddLayoutItem(layoutManager, GetDlgItem(hwndDlg, IDC_COPY), NULL,
54  PhAddLayoutItem(layoutManager, GetDlgItem(hwndDlg, IDC_SAVE), NULL,
56 
57  if (MinimumSize.left == -1)
58  {
59  RECT rect;
60 
61  rect.left = 0;
62  rect.top = 0;
63  rect.right = 200;
64  rect.bottom = 140;
65  MapDialogRect(hwndDlg, &rect);
66  MinimumSize = rect;
67  MinimumSize.left = 0;
68  }
69 
70  SetProp(hwndDlg, L"LayoutManager", (HANDLE)layoutManager);
71  SetProp(hwndDlg, L"String", (HANDLE)string);
72  }
73  break;
74  case WM_DESTROY:
75  {
76  PPH_LAYOUT_MANAGER layoutManager;
77 
78  layoutManager = (PPH_LAYOUT_MANAGER)GetProp(hwndDlg, L"LayoutManager");
79  PhDeleteLayoutManager(layoutManager);
80  PhFree(layoutManager);
81  RemoveProp(hwndDlg, L"String");
82  RemoveProp(hwndDlg, L"LayoutManager");
83  }
84  break;
85  case WM_COMMAND:
86  {
87  switch (LOWORD(wParam))
88  {
89  case IDCANCEL:
90  case IDOK:
91  EndDialog(hwndDlg, IDOK);
92  break;
93  case IDC_COPY:
94  {
95  HWND editControl;
96  LONG selStart;
97  LONG selEnd;
98  PWSTR buffer;
99  PH_STRINGREF string;
100 
101  editControl = GetDlgItem(hwndDlg, IDC_TEXT);
102  SendMessage(editControl, EM_GETSEL, (WPARAM)&selStart, (LPARAM)&selEnd);
103  buffer = (PWSTR)GetProp(hwndDlg, L"String");
104 
105  if (selStart == selEnd)
106  {
107  // Select and copy the entire string.
108  PhInitializeStringRefLongHint(&string, buffer);
109  Edit_SetSel(editControl, 0, -1);
110  }
111  else
112  {
113  string.Buffer = buffer + selStart;
114  string.Length = (selEnd - selStart) * 2;
115  }
116 
117  PhSetClipboardString(hwndDlg, &string);
118  SetFocus(editControl);
119  }
120  break;
121  case IDC_SAVE:
122  {
123  static PH_FILETYPE_FILTER filters[] =
124  {
125  { L"Text files (*.txt)", L"*.txt" },
126  { L"All files (*.*)", L"*.*" }
127  };
128  PVOID fileDialog;
129 
130  fileDialog = PhCreateSaveFileDialog();
131 
132  PhSetFileDialogFilter(fileDialog, filters, sizeof(filters) / sizeof(PH_FILETYPE_FILTER));
133  PhSetFileDialogFileName(fileDialog, L"Information.txt");
134 
135  if (PhShowFileDialog(hwndDlg, fileDialog))
136  {
137  NTSTATUS status;
138  PPH_STRING fileName;
139  PPH_FILE_STREAM fileStream;
140 
141  fileName = PhGetFileDialogFileName(fileDialog);
142  PhAutoDereferenceObject(fileName);
143 
144  if (NT_SUCCESS(status = PhCreateFileStream(
145  &fileStream,
146  fileName->Buffer,
147  FILE_GENERIC_WRITE,
148  FILE_SHARE_READ,
150  0
151  )))
152  {
153  PH_STRINGREF string;
154 
156  PhInitializeStringRef(&string, (PWSTR)GetProp(hwndDlg, L"String"));
157  PhWriteStringAsUtf8FileStream(fileStream, &string);
158  PhDereferenceObject(fileStream);
159  }
160 
161  if (!NT_SUCCESS(status))
162  PhShowStatus(hwndDlg, L"Unable to create the file", status, 0);
163  }
164 
165  PhFreeFileDialog(fileDialog);
166  }
167  break;
168  }
169  }
170  break;
171  case WM_SIZE:
172  {
173  PPH_LAYOUT_MANAGER layoutManager;
174 
175  layoutManager = (PPH_LAYOUT_MANAGER)GetProp(hwndDlg, L"LayoutManager");
176  PhLayoutManagerLayout(layoutManager);
177  }
178  break;
179  case WM_SIZING:
180  {
181  PhResizingMinimumSize((PRECT)lParam, wParam, MinimumSize.right, MinimumSize.bottom);
182  }
183  break;
184  }
185 
186  return FALSE;
187 }
188 
190  _In_ HWND ParentWindowHandle,
191  _In_ PWSTR String
192  )
193 {
194  DialogBoxParam(
196  MAKEINTRESOURCE(IDD_INFORMATION),
197  ParentWindowHandle,
198  PhpInformationDlgProc,
199  (LPARAM)String
200  );
201 }