[SETUPLIB][USETUP] Introduce a 'SetupLib' library. CORE-13544
[reactos.git] / base / setup / usetup / fslist.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS text-mode setup
22 * FILE: base/setup/usetup/fslist.c
23 * PURPOSE: Filesystem list functions
24 * PROGRAMMER: Eric Kohl
25 * Casper S. Hornstrup (chorns@users.sourceforge.net)
26 */
27
28 #include "usetup.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* FUNCTIONS ****************************************************************/
34
35 static VOID
36 AddProvider(
37 IN OUT PFILE_SYSTEM_LIST List,
38 IN PCWSTR FileSystemName, // Redundant, I need to check whether this is reaaaaally needed....
39 IN PFILE_SYSTEM FileSystem)
40 {
41 PFILE_SYSTEM_ITEM Item;
42
43 Item = (PFILE_SYSTEM_ITEM)RtlAllocateHeap(ProcessHeap, 0, sizeof(*Item));
44 if (!Item)
45 return;
46
47 Item->FileSystemName = FileSystemName;
48 Item->FileSystem = FileSystem;
49 Item->QuickFormat = TRUE;
50 InsertTailList(&List->ListHead, &Item->ListEntry);
51
52 if (!FileSystem)
53 return;
54
55 Item = (PFILE_SYSTEM_ITEM)RtlAllocateHeap(ProcessHeap, 0, sizeof(*Item));
56 if (!Item)
57 return;
58
59 Item->FileSystemName = FileSystemName;
60 Item->FileSystem = FileSystem;
61 Item->QuickFormat = FALSE;
62 InsertTailList(&List->ListHead, &Item->ListEntry);
63 }
64
65 static VOID
66 InitializeFileSystemList(
67 IN PFILE_SYSTEM_LIST List)
68 {
69 ULONG Count;
70 PFILE_SYSTEM FileSystems;
71
72 FileSystems = GetRegisteredFileSystems(&Count);
73 if (!FileSystems || Count == 0)
74 return;
75
76 while (Count--)
77 {
78 AddProvider(List, FileSystems->FileSystemName, FileSystems);
79 ++FileSystems;
80 }
81 }
82
83 PFILE_SYSTEM_LIST
84 CreateFileSystemList(
85 IN SHORT Left,
86 IN SHORT Top,
87 IN BOOLEAN ForceFormat,
88 IN PCWSTR SelectFileSystem)
89 {
90 PFILE_SYSTEM_LIST List;
91 PFILE_SYSTEM_ITEM Item;
92 PLIST_ENTRY ListEntry;
93
94 List = (PFILE_SYSTEM_LIST)RtlAllocateHeap(ProcessHeap, 0, sizeof(*List));
95 if (List == NULL)
96 return NULL;
97
98 List->Left = Left;
99 List->Top = Top;
100 List->Selected = NULL;
101 InitializeListHead(&List->ListHead);
102
103 InitializeFileSystemList(List);
104 if (!ForceFormat)
105 {
106 /* Add the 'Keep existing filesystem' dummy provider */
107 AddProvider(List, NULL, NULL);
108 }
109
110 /* Search for SelectFileSystem in list */
111 ListEntry = List->ListHead.Flink;
112 while (ListEntry != &List->ListHead)
113 {
114 Item = CONTAINING_RECORD(ListEntry, FILE_SYSTEM_ITEM, ListEntry);
115 if (Item->FileSystemName && wcscmp(SelectFileSystem, Item->FileSystemName) == 0)
116 {
117 List->Selected = Item;
118 break;
119 }
120 ListEntry = ListEntry->Flink;
121 }
122 if (!List->Selected)
123 List->Selected = CONTAINING_RECORD(List->ListHead.Flink, FILE_SYSTEM_ITEM, ListEntry);
124
125 return List;
126 }
127
128 VOID
129 DestroyFileSystemList(
130 IN PFILE_SYSTEM_LIST List)
131 {
132 PLIST_ENTRY ListEntry;
133 PFILE_SYSTEM_ITEM Item;
134
135 ListEntry = List->ListHead.Flink;
136 while (!IsListEmpty(&List->ListHead))
137 {
138 ListEntry = RemoveHeadList(&List->ListHead);
139 Item = CONTAINING_RECORD(ListEntry, FILE_SYSTEM_ITEM, ListEntry);
140 RtlFreeHeap(ProcessHeap, 0, Item);
141 }
142
143 RtlFreeHeap(ProcessHeap, 0, List);
144 }
145
146 VOID
147 DrawFileSystemList(
148 IN PFILE_SYSTEM_LIST List)
149 {
150 PLIST_ENTRY ListEntry;
151 PFILE_SYSTEM_ITEM Item;
152 COORD coPos;
153 DWORD Written;
154 ULONG Index = 0;
155 CHAR Buffer[128];
156
157 ListEntry = List->ListHead.Flink;
158 while (ListEntry != &List->ListHead)
159 {
160 Item = CONTAINING_RECORD(ListEntry, FILE_SYSTEM_ITEM, ListEntry);
161
162 coPos.X = List->Left;
163 coPos.Y = List->Top + (SHORT)Index;
164 FillConsoleOutputAttribute(StdOutput,
165 FOREGROUND_WHITE | BACKGROUND_BLUE,
166 sizeof(Buffer),
167 coPos,
168 &Written);
169 FillConsoleOutputCharacterA(StdOutput,
170 ' ',
171 sizeof(Buffer),
172 coPos,
173 &Written);
174
175 if (Item->FileSystemName)
176 {
177 if (Item->QuickFormat)
178 snprintf(Buffer, sizeof(Buffer), MUIGetString(STRING_FORMATDISK1), Item->FileSystemName);
179 else
180 snprintf(Buffer, sizeof(Buffer), MUIGetString(STRING_FORMATDISK2), Item->FileSystemName);
181 }
182 else
183 {
184 snprintf(Buffer, sizeof(Buffer), MUIGetString(STRING_KEEPFORMAT));
185 }
186
187 if (ListEntry == &List->Selected->ListEntry)
188 CONSOLE_SetInvertedTextXY(List->Left,
189 List->Top + (SHORT)Index,
190 Buffer);
191 else
192 CONSOLE_SetTextXY(List->Left,
193 List->Top + (SHORT)Index,
194 Buffer);
195 Index++;
196 ListEntry = ListEntry->Flink;
197 }
198 }
199
200 VOID
201 ScrollDownFileSystemList(
202 IN PFILE_SYSTEM_LIST List)
203 {
204 if (List->Selected->ListEntry.Flink != &List->ListHead)
205 {
206 List->Selected = CONTAINING_RECORD(List->Selected->ListEntry.Flink, FILE_SYSTEM_ITEM, ListEntry);
207 DrawFileSystemList(List);
208 }
209 }
210
211 VOID
212 ScrollUpFileSystemList(
213 IN PFILE_SYSTEM_LIST List)
214 {
215 if (List->Selected->ListEntry.Blink != &List->ListHead)
216 {
217 List->Selected = CONTAINING_RECORD(List->Selected->ListEntry.Blink, FILE_SYSTEM_ITEM, ListEntry);
218 DrawFileSystemList(List);
219 }
220 }
221
222 /* EOF */