Split window control, listview and treeview moved to child window.
[reactos.git] / rosapps / regedit / treeview.c
1 /*
2 * ReactOS regedit
3 *
4 * treeview.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include <windowsx.h>
38 #include <assert.h>
39 #define ASSERT assert
40 #include "main.h"
41 #include "treeview.h"
42
43
44 // Global variables and constants
45 // Image_Open, Image_Closed, and Image_Root - integer variables for
46 // indexes of the images.
47 // CX_BITMAP and CY_BITMAP - width and height of an icon.
48 // NUM_BITMAPS - number of bitmaps to add to the image list.
49 int Image_Open;
50 int Image_Closed;
51 int Image_Root;
52
53 #define CX_BITMAP 16
54 #define CY_BITMAP 16
55 #define NUM_BITMAPS 3
56
57
58 // AddItemToTree - adds items to a tree view control.
59 // Returns the handle to the newly added item.
60 // hwndTV - handle to the tree view control.
61 // lpszItem - text of the item to add.
62 // nLevel - level at which to add the item.
63
64 HTREEITEM AddItemToTree(HWND hwndTV, LPTSTR lpszItem, int nLevel)
65 {
66 TVITEM tvi;
67 TVINSERTSTRUCT tvins;
68 static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST;
69 static HTREEITEM hPrevRootItem = NULL;
70 static HTREEITEM hPrevLev2Item = NULL;
71 HTREEITEM hti;
72
73 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
74
75 // Set the text of the item.
76 tvi.pszText = lpszItem;
77 tvi.cchTextMax = lstrlen(lpszItem);
78
79 // Assume the item is not a parent item, so give it an image.
80 tvi.iImage = Image_Root;
81 tvi.iSelectedImage = Image_Root;
82
83 tvi.cChildren = 1;
84
85
86 // Save the heading level in the item's application-defined data area.
87 tvi.lParam = (LPARAM)nLevel;
88
89 tvins.item = tvi;
90 tvins.hInsertAfter = hPrev;
91
92 // Set the parent item based on the specified level.
93 if (nLevel == 1)
94 tvins.hParent = TVI_ROOT;
95 else if (nLevel == 2)
96 tvins.hParent = hPrevRootItem;
97 else
98 tvins.hParent = hPrevLev2Item;
99
100 // Add the item to the tree view control.
101 hPrev = (HTREEITEM)SendMessage(hwndTV, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins);
102
103 // Save the handle to the item.
104 if (nLevel == 1)
105 hPrevRootItem = hPrev;
106 else if (nLevel == 2)
107 hPrevLev2Item = hPrev;
108
109 // The new item is a child item. Give the parent item a
110 // closed folder bitmap to indicate it now has child items.
111 if (nLevel > 1) {
112 hti = TreeView_GetParent(hwndTV, hPrev);
113 tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
114 tvi.hItem = hti;
115 tvi.iImage = Image_Closed;
116 tvi.iSelectedImage = Image_Closed;
117 TreeView_SetItem(hwndTV, &tvi);
118 }
119
120 return hPrev;
121 }
122
123 static void init_output(HWND hWnd)
124 {
125 // TCHAR b[16];
126 // HFONT old_font;
127 HDC hdc = GetDC(hWnd);
128
129 // if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, _T("1000"), 0, b, 16) > 4)
130 // Globals.num_sep = b[1];
131 // else
132 // Globals.num_sep = _T('.');
133
134 // old_font = SelectFont(hdc, Globals.hFont);
135 // GetTextExtentPoint32(hdc, _T(" "), 1, &Globals.spaceSize);
136 // SelectFont(hdc, old_font);
137 ReleaseDC(hWnd, hdc);
138 }
139 /*
140 HTREEITEM AddEntryToTree(HWND hwndTV, Entry* entry)
141 {
142 HTREEITEM hItem = 0;
143 return hItem;
144 }
145 */
146
147 static BOOL InitTreeViewItems(HWND hwndTV)
148 {
149 HTREEITEM hItem;
150
151 hItem = AddItemToTree(hwndTV, _T("My Computer"), 1);
152 AddItemToTree(hwndTV, _T("HKEY_CLASSES_ROOT"), 2);
153 AddItemToTree(hwndTV, _T("HKEY_CURRENT_USER"), 2);
154 AddItemToTree(hwndTV, _T("HKEY_LOCAL_MACHINE"), 2);
155 AddItemToTree(hwndTV, _T("HKEY_USERS"), 2);
156 AddItemToTree(hwndTV, _T("HKEY_CURRENT_CONFIG"), 2);
157
158 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
159 return TRUE;
160 }
161
162 // InitTreeViewImageLists - creates an image list, adds three bitmaps
163 // to it, and associates the image list with a tree view control.
164 // Returns TRUE if successful, or FALSE otherwise.
165 // hwndTV - handle to the tree view control.
166
167 static BOOL InitTreeViewImageLists(HWND hwndTV)
168 {
169 HIMAGELIST himl; // handle to image list
170 HBITMAP hbmp; // handle to bitmap
171
172 // Create the image list.
173 if ((himl = ImageList_Create(CX_BITMAP, CY_BITMAP,
174 FALSE, NUM_BITMAPS, 0)) == NULL)
175 return FALSE;
176
177 // Add the open file, closed file, and document bitmaps.
178 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_OPEN_FILE));
179 Image_Open = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
180 DeleteObject(hbmp);
181
182 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CLOSED_FILE));
183 Image_Closed = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
184 DeleteObject(hbmp);
185
186 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ROOT));
187 Image_Root = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
188 DeleteObject(hbmp);
189
190 // Fail if not all of the images were added.
191 if (ImageList_GetImageCount(himl) < 3)
192 return FALSE;
193
194 // Associate the image list with the tree view control.
195 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
196
197 return TRUE;
198 }
199
200 #ifndef _MSC_VER
201 #define NMTVDISPINFO TV_DISPINFO
202 #define NMTVDISPINFO TV_DISPINFO
203 #endif
204
205 static void OnGetDispInfo(NMTVDISPINFO* ptvdi)
206 {
207 /*
208 Entry* entry = (Entry*)ptvdi->item.lParam;
209 ASSERT(entry);
210
211 if (ptvdi->item.mask & TVIF_CHILDREN ) {
212 ptvdi->item.cChildren = 5;
213 }
214 if (ptvdi->item.mask & TVIF_IMAGE) {
215 ptvdi->item.iImage = Image_Root;
216 }
217 if (ptvdi->item.mask & TVIF_SELECTEDIMAGE) {
218 ptvdi->item.iSelectedImage = Image_Closed;
219 }
220 if (ptvdi->item.mask & TVIF_TEXT) {
221 ptvdi->item.pszText = entry->data.cFileName;
222 ptvdi->item.cchTextMax = lstrlen(entry->data.cFileName);
223 }
224 static BOOL OnExpand(int flag, HTREEITEM* pti)
225 {
226 TRACE(_T("TreeWndProc(...) OnExpand()\n"));
227 return TRUE;
228 }
229
230 static BOOL OnExpanding(HWND hWnd, NMTREEVIEW* pnmtv)
231 {
232 static int expanding;
233
234 if (expanding) return FALSE;
235 expanding = TRUE;
236 expanding = FALSE;
237 return TRUE;
238 }
239 */
240 }
241
242 /*
243 static BOOL OnSelChanged(NMTREEVIEW* pnmtv)
244 {
245 LPARAM parm = pnmtv->itemNew.lParam;
246 ChildWnd* child = (ChildWnd*)pnmtv->itemNew.lParam;
247 return TRUE;
248 }
249 */
250
251 ////////////////////////////////////////////////////////////////////////////////
252 static WNDPROC g_orgTreeWndProc;
253
254 static LRESULT CALLBACK TreeWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
255 {
256 // ChildWnd* child = (ChildWnd*)GetWindowLong(GetParent(hWnd), GWL_USERDATA);
257 // Pane* pane = (Pane*)GetWindowLong(hWnd, GWL_USERDATA);
258 // ASSERT(child);
259 switch (message) {
260 default:
261 break;
262 }
263 return CallWindowProc(g_orgTreeWndProc, hWnd, message, wParam, lParam);
264 }
265
266 // CreateTreeView - creates a tree view control.
267 // Returns the handle to the new control if successful,
268 // or NULL otherwise.
269 // hwndParent - handle to the control's parent window.
270
271 HWND CreateTreeView(HWND hwndParent/*, Pane* pane*/, int id, LPTSTR lpszPathName)
272 {
273 RECT rcClient; // dimensions of client area
274 HWND hwndTV; // handle to tree view control
275 // static int s_init = 0;
276 // Entry* entry = pane->root;
277 // pane->treePane = 1;
278
279 // Get the dimensions of the parent window's client area, and create
280 // the tree view control.
281 GetClientRect(hwndParent, &rcClient);
282 hwndTV = CreateWindowEx(0, WC_TREEVIEW, _T("Tree View"),
283 WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
284 0, 0, rcClient.right, rcClient.bottom,
285 hwndParent, (HMENU)id, hInst, NULL);
286 // Initialize the image list, and add items to the control.
287 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV)) {
288 DestroyWindow(hwndTV);
289 return NULL;
290 }
291
292 SetWindowLong(hwndTV, GWL_USERDATA, (LPARAM)0);
293 g_orgTreeWndProc = SubclassWindow(hwndTV, TreeWndProc);
294 //SendMessage(hwndTV, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
295
296 // insert entries into treectrl
297 // if (entry) {
298 // insert_tree_entries(hwndTV, entry, 0);
299 // }
300
301 // calculate column widths
302 // if (!s_init) {
303 // s_init = 1;
304 // init_output(hwndTV);
305 // }
306
307 return hwndTV;
308 }
309