1 /* MikMod module player
2 (c) 1998 - 2000 Miodrag Vallat and others - see file AUTHORS for
3 complete list.
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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 02111-1307, USA.
19 */
20
21 /*==============================================================================
22
23 $Id: mdialog.c,v 1.1.1.1 2004/01/16 02:07:40 raph Exp $
24
25 Some common dialog types
26
27 ==============================================================================*/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "mwidget.h"
38 #include "mdialog.h"
39 #include "display.h"
40 #include "mutilities.h"
41
42 typedef struct {
43 handleDlgFunc handle_dlg;
44 WIDGET *w;
45 void *input;
46 void *data;
47 int min, max;
48 } DLG_DATA;
49
handle_focus(struct WIDGET * w,int focus)50 static int handle_focus(struct WIDGET *w, int focus)
51 {
52 if (focus == FOCUS_ACTIVATE) {
53 DLG_DATA *data = (DLG_DATA *) w->data;
54 if (data) {
55 int button = -1;
56 if (w->type == TYPE_BUTTON)
57 button = ((WID_BUTTON *) w)->active;
58
59 if ((button <= 0) && (data->min >= 0) && (data->max >= 0)) {
60 int value = atoi((char*)data->input);
61 if ((value < data->min) || (value > data->max))
62 return focus;
63 }
64 if (data->handle_dlg(data->w, button, data->input, data->data)) {
65 free(data);
66 dialog_close(w->d);
67 }
68 } else
69 dialog_close(w->d);
70 return EVENT_HANDLED;
71 }
72 return focus;
73 }
74
init_dlg_data(handleDlgFunc handle_dlg,WIDGET * w,void * input,void * data)75 static DLG_DATA *init_dlg_data(handleDlgFunc handle_dlg,
76 WIDGET *w, void *input, void *data)
77 {
78 DLG_DATA *dlg_data = NULL;
79 if (handle_dlg) {
80 dlg_data = (DLG_DATA *) malloc(sizeof(DLG_DATA));
81 dlg_data->handle_dlg = handle_dlg;
82 dlg_data->w = w;
83 dlg_data->input = input;
84 dlg_data->data = data;
85 dlg_data->min = dlg_data->max = -1;
86 }
87 return dlg_data;
88 }
89
90 /* Opens a message box
91 msg : text to display,can contain '\n'
92 button: ".&..|...|...",&: hotkey,e.g.: "&Yes|&No"
93 active: active button(0...n)
94 warn : open message box with ATTR_WARNING?
95 data : passed to handle_dlg */
dlg_message_open(const char * msg,const char * button,int active,BOOL warn,handleDlgFunc handle_dlg,void * data)96 void dlg_message_open(const char *msg, const char *button, int active, BOOL warn,
97 handleDlgFunc handle_dlg, void *data)
98 {
99 WIDGET *w;
100 DIALOG *d = dialog_new();
101
102 if (warn) dialog_set_attr (d,ATTR_WARNING);
103 wid_label_add(d, 1, msg);
104 w = wid_button_add(d, 2, button, active);
105 if (handle_dlg)
106 wid_set_func(w, NULL, handle_focus,
107 init_dlg_data(handle_dlg, w, NULL, data));
108 dialog_open(d, "Message");
109 }
110
111 /* Shows a message. If errno is set a text describing the errno
112 error code is appended to the message. */
dlg_error_show(const char * txt,...)113 void dlg_error_show(const char *txt, ...)
114 {
115 va_list args;
116 char *err = NULL;
117 int len;
118
119 if (errno) {
120 #ifdef HAVE_STRERROR
121 err = strerror(errno);
122 #else
123 err = (errno >= sys_nerr) ? "(unknown error)" : sys_errlist[errno];
124 #endif
125 }
126 va_start(args, txt);
127 VSNPRINTF (storage, STORAGELEN, txt, args);
128 va_end(args);
129
130 len = strlen(storage);
131 if (len<STORAGELEN-2 && err && *err) {
132 strncat (storage,"\n",STORAGELEN-len);
133 strncat (storage,err,STORAGELEN-len-1);
134 }
135 dlg_message_open(storage, "&Ok", 0, 1, NULL, NULL);
136 }
137
138 /* Opens a string input dialog
139 msg : text to display,can contain '\n'
140 str : default text
141 length: max allowed input length */
dlg_input_str(const char * msg,const char * buttons,const char * str,int length,handleDlgFunc handle_dlg,void * data)142 void dlg_input_str(const char *msg, const char *buttons, const char *str, int length,
143 handleDlgFunc handle_dlg, void *data)
144 {
145 WIDGET *w, *str_wid;
146 DLG_DATA *dlg_data;
147 DIALOG *d = dialog_new();
148
149 if (msg)
150 wid_label_add(d, 1, msg);
151 str_wid = wid_str_add(d, 1, str, length);
152 w = wid_button_add(d, 2, buttons, 0);
153
154 dlg_data = init_dlg_data(handle_dlg, str_wid, ((WID_STR*)str_wid)->input, data);
155 wid_set_func(str_wid, NULL, handle_focus, dlg_data);
156 wid_set_func(w, NULL, handle_focus, dlg_data);
157
158 dialog_open(d, "Enter string");
159 }
160
161 /* Opens an integer input dialog
162 msg : text to display,can contain '\n'
163 value : default integer
164 min,max: min,max allowed values */
dlg_input_int(const char * msg,const char * buttons,int value,int min,int max,handleDlgFunc handle_dlg,void * data)165 void dlg_input_int(const char *msg, const char *buttons, int value, int min, int max,
166 handleDlgFunc handle_dlg, void *data)
167 {
168 char title[40];
169 WIDGET *w, *int_wid;
170 DLG_DATA *dlg_data;
171 DIALOG *d = dialog_new();
172
173 if (msg)
174 wid_label_add(d, 1, msg);
175 sprintf(title, "%d", max);
176 int_wid = wid_int_add(d, 1, value, strlen(title));
177 w = wid_button_add(d, 2, buttons, 0);
178
179 dlg_data = init_dlg_data(handle_dlg, int_wid, ((WID_INT*)int_wid)->input, data);
180 dlg_data->min = min;
181 dlg_data->max = max;
182 wid_set_func(int_wid, NULL, handle_focus, dlg_data);
183 wid_set_func(w, NULL, handle_focus, dlg_data);
184
185 sprintf(title, "Enter value(%d - %d)", min, max);
186 dialog_open(d, title);
187 }
188
189 /* ex:set ts=4: */
190