include libgen.h for basename
[elinks.git] / src / dialogs / edit.c
blob1787cf6c8895fed6df667376d6c16cb24358533c
1 /* Generic support for edit/search historyitem/bookmark dialog */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/dialog.h"
12 #include "dialogs/edit.h"
13 #include "intl/gettext/libintl.h"
14 #include "session/session.h"
15 #include "terminal/terminal.h"
16 #include "util/color.h"
17 #include "util/memory.h"
18 #include "util/string.h"
21 /* TODO: Move to bfu/. It has no bussiness to do in dialogs/. --pasky */
24 static widget_handler_status_T
25 my_cancel_dialog(struct dialog_data *dlg_data, struct widget_data *widget_data)
27 ((void (*)(struct dialog *)) widget_data->widget->data)(dlg_data->dlg);
28 return cancel_dialog(dlg_data, widget_data);
32 /* Edits an item's fields.
33 * If parent is defined, then that points to a dialog that should be sent
34 * an update when the add is done.
36 * If either of src_name or src_url are NULL, try to obtain the name and url
37 * of the current document. If you want to create two null fields, pass in a
38 * pointer to a zero length string (""). */
39 /* TODO: In bookmark/dialogs.c most users seem to want also the dialog_data
40 * so we should make when_*() functions take dialog_data instead. --jonas */
41 void
42 do_edit_dialog(struct terminal *term, int intl, unsigned char *title,
43 const unsigned char *src_name,
44 const unsigned char *src_url,
45 struct session *ses, struct dialog_data *parent,
46 done_handler_T *when_done,
47 void when_cancel(struct dialog *),
48 void *done_data,
49 enum edit_dialog_type dialog_type)
51 /* [gettext_accelerator_context(do_edit_dialog)] */
52 unsigned char *name, *url;
53 struct dialog *dlg;
55 if (intl) title = _(title, term);
57 /* Number of fields in edit dialog --Zas */
58 #define EDIT_WIDGETS_COUNT 5
60 /* Create the dialog */
61 dlg = calloc_dialog(EDIT_WIDGETS_COUNT, 2 * MAX_STR_LEN);
62 if (!dlg) return;
64 name = get_dialog_offset(dlg, EDIT_WIDGETS_COUNT);
65 url = name + MAX_STR_LEN;
67 /* Get the name */
68 if (!src_name) {
69 /* Unknown name. */
70 get_current_title(ses, name, MAX_STR_LEN);
71 } else {
72 /* Known name. */
73 safe_strncpy(name, src_name, MAX_STR_LEN);
76 /* Get the url */
77 if (!src_url) {
78 /* Unknown . */
79 get_current_url(ses, url, MAX_STR_LEN);
80 } else {
81 /* Known url. */
82 safe_strncpy(url, src_url, MAX_STR_LEN);
85 dlg->title = title;
86 dlg->layouter = generic_dialog_layouter;
87 dlg->layout.maximize_width = 1;
88 dlg->udata = parent;
89 dlg->udata2 = done_data;
91 if (dialog_type == EDIT_DLG_ADD)
92 add_dlg_field(dlg, _("Name", term), 0, 0, check_nonempty, MAX_STR_LEN, name, NULL);
93 else
94 add_dlg_field(dlg, _("Name", term), 0, 0, NULL, MAX_STR_LEN, name, NULL);
96 add_dlg_field(dlg, _("URL", term), 0, 0, NULL, MAX_STR_LEN, url, NULL);
98 add_dlg_ok_button(dlg, _("~OK", term), B_ENTER, when_done, dlg);
99 add_dlg_button(dlg, _("C~lear", term), 0, clear_dialog, NULL);
101 if (when_cancel)
102 add_dlg_button(dlg, _("~Cancel", term), B_ESC,
103 my_cancel_dialog, when_cancel);
104 else
105 add_dlg_button(dlg, _("~Cancel", term), B_ESC,
106 cancel_dialog, NULL);
108 add_dlg_end(dlg, EDIT_WIDGETS_COUNT);
110 do_dialog(term, dlg, getml(dlg, (void *) NULL));
112 #undef EDIT_WIDGETS_COUNT