2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "diskeditor.h"
40 print_partedit_item(WINDOW *partitions, struct partedit_item *items,
41 int item, int nscroll, int selected)
43 chtype attr = A_NORMAL;
45 int y = item - nscroll + 1;
47 wattrset(partitions, selected ? item_selected_attr : item_attr);
48 wmove(partitions, y, MARGIN + items[item].indentation*2);
49 dlg_print_text(partitions, items[item].name, 10, &attr);
50 wmove(partitions, y, 17);
51 wattrset(partitions, item_attr);
53 humanize_number(sizetext, 7, items[item].size, "B", HN_AUTOSCALE,
55 dlg_print_text(partitions, sizetext, 8, &attr);
56 wmove(partitions, y, 25);
57 dlg_print_text(partitions, items[item].type, 15, &attr);
58 wmove(partitions, y, 40);
59 if (items[item].mountpoint != NULL)
60 dlg_print_text(partitions, items[item].mountpoint, 8, &attr);
64 diskeditor_show(const char *title, const char *cprompt,
65 struct partedit_item *items, int nitems, int *selected, int *nscroll)
67 WINDOW *dialog, *partitions;
69 const char *buttons[] =
70 { "Create", "Delete", "Modify", "Revert", "Auto", "Finish", NULL };
71 const char *help_text[] = {
72 "Add a new partition", "Delete selected partition or partitions",
73 "Change partition type or mountpoint",
74 "Revert changes to disk setup", "Use guided partitioning tool",
75 "Exit partitioner (will ask whether to save changes)", NULL };
78 int height, width, min_width;
79 int partlist_height, partlist_width;
82 int cur_button = 5, cur_part = 0;
83 int result = DLG_EXIT_UNKNOWN;
85 static DLG_KEYS_BINDING binding[] = {
87 DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
88 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
89 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
90 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
91 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
92 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
93 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
99 static DLG_KEYS_BINDING binding2[] = {
102 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
103 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
104 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
105 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
106 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ),
107 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
108 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ),
109 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
110 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
111 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
116 * Set up editor window.
118 prompt = dlg_strclone(cprompt);
122 partlist_height = 10;
123 dlg_tab_correct_str(prompt);
124 dlg_button_layout(buttons, &min_width);
125 dlg_auto_size(title, prompt, &height, &width, 2, min_width);
126 height += partlist_height;
127 partlist_width = width - 2*MARGIN;
128 dlg_print_size(height, width);
129 dlg_ctl_size(height, width);
131 x = dlg_box_x_ordinate(width);
132 y = dlg_box_y_ordinate(height);
134 dialog = dlg_new_window(height, width, y, x);
135 dlg_register_window(dialog, "diskeditorbox", binding);
136 dlg_register_buttons(dialog, "diskeditorbox", buttons);
138 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
139 dlg_draw_bottom_box(dialog);
140 dlg_draw_title(dialog, title);
141 wattrset(dialog, dialog_attr);
143 /* Partition list sub-window */
144 partitions = dlg_sub_window(dialog, partlist_height, partlist_width,
146 dlg_register_window(partitions, "partlist", binding2);
147 dlg_register_buttons(partitions, "partlist", buttons);
148 wattrset(partitions, menubox_attr);
150 dlg_item_help(help_text[cur_button]);
151 dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
152 cur_button, FALSE, width);
153 dlg_print_autowrap(dialog, prompt, height, width);
155 if (selected != NULL)
156 cur_part = *selected;
158 cur_scroll = *nscroll;
159 if (cur_part - cur_scroll >= partlist_height - 2 ||
160 cur_part - cur_scroll < 0)
161 cur_scroll = cur_part;
164 dlg_draw_box(dialog, 3, 1, partlist_height, partlist_width,
165 menubox_border_attr, menubox_attr);
166 for (i = cur_scroll; i < MIN(cur_scroll + partlist_height - 2, nitems);
168 print_partedit_item(partitions, items, i, cur_scroll,
170 if (nitems > partlist_height - 2)
171 dlg_draw_arrows(partitions, cur_scroll > 0,
172 nitems > cur_scroll + partlist_height - 2,
173 partlist_width - 5, 0, partlist_height - 1);
174 wrefresh(partitions);
176 while (result == DLG_EXIT_UNKNOWN) {
177 key = dlg_mouse_wgetch(dialog, &fkey);
178 if ((i = dlg_char_to_button(key, buttons)) >= 0) {
180 dlg_item_help(help_text[cur_button]);
181 dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
182 cur_button, FALSE, width);
190 case DLGK_FIELD_NEXT:
191 cur_button = dlg_next_button(buttons, cur_button);
194 dlg_item_help(help_text[cur_button]);
195 dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
196 cur_button, FALSE, width);
198 case DLGK_FIELD_PREV:
199 cur_button = dlg_prev_button(buttons, cur_button);
202 dlg_item_help(help_text[cur_button]);
203 dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
204 cur_button, FALSE, width);
207 if (cur_part == nitems - 1)
208 break; /* End of list */
210 /* Deselect old item */
211 print_partedit_item(partitions, items, cur_part,
213 /* Select new item */
215 if (cur_part - cur_scroll >= partlist_height - 2) {
216 cur_scroll = cur_part;
219 print_partedit_item(partitions, items, cur_part,
221 wrefresh(partitions);
225 break; /* Start of list */
227 /* Deselect old item */
228 print_partedit_item(partitions, items, cur_part,
230 /* Select new item */
232 if (cur_part - cur_scroll < 0) {
233 cur_scroll = cur_part;
236 print_partedit_item(partitions, items, cur_part,
238 wrefresh(partitions);
241 cur_scroll += (partlist_height - 2);
242 if (cur_scroll + partlist_height - 2 >= nitems)
243 cur_scroll = nitems - (partlist_height - 2);
246 if (cur_part < cur_scroll)
247 cur_part = cur_scroll;
250 cur_scroll -= (partlist_height - 2);
253 if (cur_part >= cur_scroll + partlist_height - 2)
254 cur_part = cur_scroll;
256 case DLGK_PAGE_FIRST:
258 cur_part = cur_scroll;
261 cur_scroll = nitems - (partlist_height - 2);
264 cur_part = cur_scroll;
269 if (is_DLGK_MOUSE(key)) {
270 cur_button = key - M_EVENT;
271 dlg_item_help(help_text[cur_button]);
272 dlg_draw_buttons(dialog, height - 2*MARGIN, 0,
273 buttons, cur_button, FALSE, width);
281 if (selected != NULL)
282 *selected = cur_part;
284 *nscroll = cur_scroll;
286 dlg_del_window(partitions);
287 dlg_del_window(dialog);
288 dlg_mouse_free_regions();