regedit: Value editor changes.
[Samba/id10ts.git] / source3 / utils / regedit_dialog.c
blob9b64da898ea0857daab7c3d2f26a353eae42718e
1 /*
2 * Samba Unix/Linux SMB client library
3 * Registry Editor
4 * Copyright (C) Christopher Davis 2012
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "regedit_dialog.h"
22 #include "regedit_valuelist.h"
23 #include "util_reg.h"
24 #include "lib/registry/registry.h"
25 #include <stdarg.h>
26 #include <form.h>
28 static int dialog_free(struct dialog *dia)
30 if (dia->window) {
31 delwin(dia->window);
33 if (dia->sub_window) {
34 delwin(dia->sub_window);
36 if (dia->panel) {
37 del_panel(dia->panel);
39 if (dia->choices) {
40 unpost_menu(dia->choices);
41 free_menu(dia->choices);
43 if (dia->choice_items) {
44 ITEM **it;
45 for (it = dia->choice_items; *it != NULL; ++it) {
46 free_item(*it);
50 return 0;
53 struct dialog *dialog_new(TALLOC_CTX *ctx, const char *title, int nlines,
54 int ncols, int y, int x)
56 struct dialog *dia;
58 dia = talloc_zero(ctx, struct dialog);
59 if (dia == NULL) {
60 return NULL;
63 talloc_set_destructor(dia, dialog_free);
65 dia->window = newwin(nlines, ncols, y, x);
66 if (dia->window == NULL) {
67 goto fail;
70 box(dia->window, 0, 0);
71 mvwaddstr(dia->window, 0, 1, title);
73 /* body of the dialog within the box outline */
74 dia->sub_window = derwin(dia->window, nlines - 2, ncols - 2, 1, 1);
75 if (dia->sub_window == NULL) {
76 goto fail;
79 dia->panel = new_panel(dia->window);
80 if (dia->panel == NULL) {
81 goto fail;
84 return dia;
86 fail:
87 talloc_free(dia);
89 return NULL;
93 static void center_dialog_above_window(WINDOW *below, int *nlines, int *ncols,
94 int *y, int *x)
96 int maxy, maxx;
97 int centery, centerx;
99 getmaxyx(below, maxy, maxx);
101 centery = maxy / 2;
102 centerx = maxx / 2;
103 *y = 0;
104 *x = 0;
106 if (*nlines > maxy) {
107 *nlines = maxy;
109 if (*ncols > maxx) {
110 *ncols = maxx;
113 if (*nlines < centery) {
114 *y = centery - *nlines;
116 if (*ncols < centerx) {
117 *x = centerx - *ncols;
121 struct dialog *dialog_center_new(TALLOC_CTX *ctx, const char *title, int nlines,
122 int ncols, WINDOW *below)
124 int y, x;
126 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
128 return dialog_new(ctx, title, nlines, ncols, y, x);
131 struct dialog *dialog_choice_new(TALLOC_CTX *ctx, const char *title,
132 const char **choices, int nlines,
133 int ncols, int y, int x)
135 size_t nchoices, i;
136 struct dialog *dia;
138 dia = dialog_new(ctx, title, nlines, ncols, y, x);
139 if (dia == NULL) {
140 return NULL;
143 dia->menu_window = derwin(dia->sub_window, 1, ncols - 3,
144 nlines - 3, 0);
145 if (dia->menu_window == NULL) {
146 goto fail;
149 for (nchoices = 0; choices[nchoices] != NULL; ++nchoices)
151 dia->choice_items = talloc_zero_array(dia, ITEM *, nchoices + 1);
152 if (dia->choice_items == NULL) {
153 goto fail;
155 for (i = 0; i < nchoices; ++i) {
156 char *desc = talloc_strdup(dia, choices[i]);
157 if (desc == NULL) {
158 goto fail;
160 dia->choice_items[i] = new_item(desc, desc);
161 if (dia->choice_items[i] == NULL) {
162 goto fail;
164 /* store choice index */
165 set_item_userptr(dia->choice_items[i], (void*)(uintptr_t)i);
168 dia->choices = new_menu(dia->choice_items);
169 if (dia->choices == NULL) {
170 goto fail;
173 set_menu_format(dia->choices, 1, ncols);
174 set_menu_win(dia->choices, dia->sub_window);
175 set_menu_sub(dia->choices, dia->menu_window);
176 menu_opts_off(dia->choices, O_SHOWDESC);
177 set_menu_mark(dia->choices, "* ");
178 post_menu(dia->choices);
179 wmove(dia->sub_window, 0, 0);
181 return dia;
183 fail:
184 talloc_free(dia);
186 return NULL;
189 struct dialog *dialog_choice_center_new(TALLOC_CTX *ctx, const char *title,
190 const char **choices, int nlines,
191 int ncols, WINDOW *below)
193 int y, x;
195 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
197 return dialog_choice_new(ctx, title, choices, nlines, ncols, y, x);
200 struct dialog *dialog_confirm_new(TALLOC_CTX *ctx, const char *title,
201 WINDOW *below, const char *msg, ...)
203 va_list ap;
204 struct dialog *dia;
205 char *str;
206 const char *choices[] = {
207 "Ok",
208 "Cancel",
209 NULL
211 int width;
213 va_start(ap, msg);
214 str = talloc_vasprintf(ctx, msg, ap);
215 va_end(ap);
216 if (str == NULL) {
217 return NULL;
220 width = strlen(str) + 2;
222 dia = dialog_choice_center_new(ctx, title, choices, 5, width, below);
223 if (dia == NULL) {
224 return NULL;
227 waddstr(dia->sub_window, str);
228 talloc_free(str);
230 return dia;
233 static int handle_menu_input(MENU *menu, int c)
235 ITEM *item;
237 switch (c) {
238 case KEY_LEFT:
239 menu_driver(menu, REQ_LEFT_ITEM);
240 break;
241 case KEY_RIGHT:
242 menu_driver(menu, REQ_RIGHT_ITEM);
243 break;
244 case KEY_ENTER:
245 case '\n':
246 item = current_item(menu);
247 return (int)(uintptr_t)item_userptr(item);
250 return -1;
253 int dialog_modal_loop(struct dialog *dia)
255 int c;
256 int selection = -1;
258 keypad(dia->window, true);
259 update_panels();
260 doupdate();
262 while (selection == -1) {
263 c = wgetch(dia->window);
264 selection = handle_menu_input(dia->choices, c);
265 update_panels();
266 doupdate();
269 talloc_free(dia);
271 return selection;
274 static void handle_form_input(FORM *frm, int c)
276 switch (c) {
277 case '\n':
278 form_driver(frm, REQ_NEW_LINE);
279 break;
280 case KEY_UP:
281 form_driver(frm, REQ_UP_CHAR);
282 break;
283 case KEY_DOWN:
284 form_driver(frm, REQ_DOWN_CHAR);
285 break;
286 case '\b':
287 case KEY_BACKSPACE:
288 form_driver(frm, REQ_DEL_PREV);
289 break;
290 case KEY_LEFT:
291 form_driver(frm, REQ_LEFT_CHAR);
292 break;
293 case KEY_RIGHT:
294 form_driver(frm, REQ_RIGHT_CHAR);
295 break;
296 default:
297 form_driver(frm, c);
298 break;
302 #define MAX_FIELDS 8
304 enum input_section {
305 IN_NAME,
306 IN_DATA,
307 IN_MENU
310 struct edit_dialog {
311 struct dialog *dia;
312 WINDOW *input_win;
313 FORM *input;
314 FIELD *field[MAX_FIELDS];
315 enum input_section section;
318 static WERROR fill_value_buffer(struct edit_dialog *edit,
319 const struct value_item *vitem)
321 char *tmp;
323 switch (vitem->type) {
324 case REG_DWORD: {
325 uint32_t v = 0;
326 if (vitem->data.length >= 4) {
327 v = IVAL(vitem->data.data, 0);
329 tmp = talloc_asprintf(edit, "0x%x", v);
330 if (tmp == NULL) {
331 return WERR_NOMEM;
333 set_field_buffer(edit->field[1], 0, tmp);
334 talloc_free(tmp);
335 set_field_type(edit->field[1], TYPE_REGEXP,
336 "^ *([0-9]+|0[xX][0-9a-fA-F]+) *$");
337 break;
339 case REG_SZ:
340 case REG_EXPAND_SZ: {
341 const char *s;
343 if (!pull_reg_sz(edit, &vitem->data, &s)) {
344 return WERR_NOMEM;
346 set_field_buffer(edit->field[1], 0, s);
347 break;
349 case REG_MULTI_SZ: {
350 const char **p, **a;
351 char *buf = NULL;
353 if (!pull_reg_multi_sz(edit, &vitem->data, &a)) {
354 return WERR_NOMEM;
356 for (p = a; *p != NULL; ++p) {
357 if (buf == NULL) {
358 buf = talloc_asprintf(edit, "%s\n", *p);
359 } else {
360 buf = talloc_asprintf_append(buf, "%s\n", *p);
362 if (buf == NULL) {
363 return WERR_NOMEM;
366 set_field_buffer(edit->field[1], 0, buf);
367 talloc_free(buf);
372 return WERR_OK;
375 static void set_value(struct edit_dialog *edit, struct registry_key *key,
376 const struct value_item *vitem)
380 static void section_down(struct edit_dialog *edit)
382 switch (edit->section) {
383 case IN_NAME:
384 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
385 edit->section = IN_DATA;
386 set_current_field(edit->input, edit->field[1]);
388 break;
389 case IN_DATA:
390 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
391 edit->section = IN_MENU;
392 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
394 break;
395 case IN_MENU:
396 edit->section = IN_NAME;
397 set_current_field(edit->input, edit->field[0]);
398 break;
402 static void section_up(struct edit_dialog *edit)
404 switch (edit->section) {
405 case IN_NAME:
406 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
407 edit->section = IN_MENU;
408 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
410 break;
411 case IN_DATA:
412 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
413 edit->section = IN_NAME;
414 set_current_field(edit->input, edit->field[0]);
416 break;
417 case IN_MENU:
418 edit->section = IN_DATA;
419 set_current_field(edit->input, edit->field[1]);
420 break;
424 int dialog_edit_value(TALLOC_CTX *ctx, struct registry_key *key,
425 const struct value_item *vitem, WINDOW *below)
427 struct edit_dialog *edit;
428 const char *choices[] = {
429 "Ok",
430 "Cancel",
431 NULL
433 char *title;
434 int nlines, ncols, val_rows;
435 int rv = -1;
437 edit = talloc_zero(ctx, struct edit_dialog);
438 if (edit == NULL) {
439 return -1;
442 title = talloc_asprintf(ctx, "Edit %s value", str_regtype(vitem->type));
443 talloc_free(title);
444 if (title == NULL) {
445 goto finish;
448 nlines = 9;
449 if (vitem->type == REG_MULTI_SZ) {
450 nlines += 4;
452 ncols = 50;
453 edit->dia = dialog_choice_center_new(ctx, title, choices, nlines, ncols, below);
454 if (edit->dia == NULL) {
455 goto finish;
458 /* name */
459 edit->field[0] = new_field(1, ncols - 4, 1, 1, 0, 0);
461 /* data */
462 val_rows = 1;
463 if (vitem->type == REG_MULTI_SZ) {
464 val_rows += 4;
466 edit->field[1] = new_field(val_rows, ncols - 4, 4, 1, 0, 0);
468 set_field_back(edit->field[0], A_REVERSE);
469 set_field_back(edit->field[1], A_REVERSE);
470 field_opts_off(edit->field[0], O_BLANK | O_AUTOSKIP | O_STATIC);
471 field_opts_off(edit->field[1], O_BLANK | O_AUTOSKIP | O_STATIC | O_WRAP);
473 if (vitem) {
474 set_field_buffer(edit->field[0], 0, vitem->value_name);
475 field_opts_off(edit->field[0], O_EDIT);
476 fill_value_buffer(edit, vitem);
479 edit->input = new_form(edit->field);
480 form_opts_off(edit->input, O_NL_OVERLOAD | O_BS_OVERLOAD);
482 edit->input_win = derwin(edit->dia->sub_window, nlines - 3, ncols - 3, 0, 0);
484 set_form_win(edit->input, edit->dia->sub_window);
485 set_form_sub(edit->input, edit->input_win);
486 post_form(edit->input);
487 mvwprintw(edit->dia->sub_window, 0, 0, "Name");
488 mvwprintw(edit->dia->sub_window, 3, 0, "Data");
490 keypad(edit->dia->window, true);
491 update_panels();
492 doupdate();
494 edit->section = IN_NAME;
496 while (1) {
497 int c = wgetch(edit->dia->window);
498 if (c == '\t') {
499 section_down(edit);
500 continue;
501 } else if (c == KEY_BTAB) {
502 section_up(edit);
503 continue;
506 if (edit->section == IN_NAME || edit->section == IN_DATA) {
507 handle_form_input(edit->input, c);
508 } else {
509 rv = handle_menu_input(edit->dia->choices, c);
510 if (rv != -1) {
511 goto finish;
515 update_panels();
516 doupdate();
519 finish:
520 if (edit->dia) {
521 talloc_free(edit->dia);
523 if (rv == DIALOG_OK) {
524 //set_value
527 return rv;