1 /* Generic support for edit/search historyitem/bookmark dialog */
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include <string.h>
8 
9 #include "elinks.h"
10 
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"
19 
20 
21 /* TODO: Move to bfu/. It has no bussiness to do in dialogs/. --pasky */
22 
23 
24 static widget_handler_status_T
my_cancel_dialog(struct dialog_data * dlg_data,struct widget_data * widget_data)25 my_cancel_dialog(struct dialog_data *dlg_data, struct widget_data *widget_data)
26 {
27 	((void (*)(struct dialog *)) widget_data->widget->data)(dlg_data->dlg);
28 	return cancel_dialog(dlg_data, widget_data);
29 }
30 
31 
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.
35  *
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
do_edit_dialog(struct terminal * term,int intl,unsigned char * title,const unsigned char * src_name,const unsigned char * src_url,struct session * ses,struct dialog_data * parent,done_handler_T * when_done,void when_cancel (struct dialog *),void * done_data,enum edit_dialog_type dialog_type)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)
50 {
51 	unsigned char *name, *url;
52 	struct dialog *dlg;
53 
54 	if (intl) title = _(title, term);
55 
56 	/* Number of fields in edit dialog --Zas */
57 #define EDIT_WIDGETS_COUNT 5
58 
59 	/* Create the dialog */
60 	dlg = calloc_dialog(EDIT_WIDGETS_COUNT, 2 * MAX_STR_LEN);
61 	if (!dlg) return;
62 
63 	name = get_dialog_offset(dlg, EDIT_WIDGETS_COUNT);
64 	url = name + MAX_STR_LEN;
65 
66 	/* Get the name */
67 	if (!src_name) {
68 		/* Unknown name. */
69 		get_current_title(ses, name, MAX_STR_LEN);
70 	} else {
71 		/* Known name. */
72 		safe_strncpy(name, src_name, MAX_STR_LEN);
73 	}
74 
75 	/* Get the url */
76 	if (!src_url) {
77 		/* Unknown . */
78 		get_current_url(ses, url, MAX_STR_LEN);
79 	} else {
80 		/* Known url. */
81 		safe_strncpy(url, src_url, MAX_STR_LEN);
82 	}
83 
84 	dlg->title = title;
85 	dlg->layouter = generic_dialog_layouter;
86 	dlg->layout.maximize_width = 1;
87 	dlg->udata = parent;
88 	dlg->udata2 = done_data;
89 
90 	if (dialog_type == EDIT_DLG_ADD)
91 		add_dlg_field(dlg, _("Name", term), 0, 0, check_nonempty, MAX_STR_LEN, name, NULL);
92 	else
93 		add_dlg_field(dlg, _("Name", term), 0, 0, NULL, MAX_STR_LEN, name, NULL);
94 
95 	add_dlg_field(dlg, _("URL", term), 0, 0, NULL, MAX_STR_LEN, url, NULL);
96 
97 	add_dlg_ok_button(dlg, _("~OK", term), B_ENTER, when_done, dlg);
98 	add_dlg_button(dlg, _("C~lear", term), 0, clear_dialog, NULL);
99 
100 	if (when_cancel)
101 		add_dlg_button(dlg, _("~Cancel", term), B_ESC,
102 			       my_cancel_dialog, when_cancel);
103 	else
104 		add_dlg_button(dlg, _("~Cancel", term), B_ESC,
105 			       cancel_dialog, NULL);
106 
107 	add_dlg_end(dlg, EDIT_WIDGETS_COUNT);
108 
109 	do_dialog(term, dlg, getml(dlg, NULL));
110 
111 #undef EDIT_WIDGETS_COUNT
112 }
113