This source file includes following definitions.
- history_dlg_reposition
- history_dlg_callback
- history_create_item
- history_release_item
- history_load
- history_save
- history_descriptor_init
- history_show
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include <sys/types.h>
39
40 #include "lib/global.h"
41
42 #include "lib/tty/tty.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
45 #include "lib/keybind.h"
46 #include "lib/fileloc.h"
47 #include "lib/event.h"
48 #include "lib/mcconfig.h"
49
50
51
52
53
54 #define B_VIEW (B_USER + 1)
55 #define B_EDIT (B_USER + 2)
56
57
58
59 typedef struct
60 {
61 int y;
62 int x;
63 size_t count;
64 size_t max_width;
65 } history_dlg_data;
66
67
68
69
70
71
72
73
74
75 static cb_ret_t
76 history_dlg_reposition (WDialog *dlg_head)
77 {
78 history_dlg_data *data;
79 int x = 0, y, he, wi;
80 WRect r;
81
82
83 if (dlg_head == NULL || dlg_head->data.p == NULL)
84 return MSG_NOT_HANDLED;
85
86 data = (history_dlg_data *) dlg_head->data.p;
87
88 y = data->y;
89 he = data->count + 2;
90
91 if (he <= y || y > (LINES - 6))
92 {
93 he = MIN (he, y - 1);
94 y -= he;
95 }
96 else
97 {
98 y++;
99 he = MIN (he, LINES - y);
100 }
101
102 if (data->x > 2)
103 x = data->x - 2;
104
105 wi = data->max_width + 4;
106
107 if ((wi + x) > COLS)
108 {
109 wi = MIN (wi, COLS);
110 x = COLS - wi;
111 }
112
113 rect_init (&r, y, x, he, wi);
114
115 return dlg_default_callback (WIDGET (dlg_head), NULL, MSG_RESIZE, 0, &r);
116 }
117
118
119
120 static cb_ret_t
121 history_dlg_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
122 {
123 switch (msg)
124 {
125 case MSG_RESIZE:
126 return history_dlg_reposition (DIALOG (w));
127
128 case MSG_NOTIFY:
129 {
130
131 WDialog *d = DIALOG (w);
132
133 switch (parm)
134 {
135 case CK_View:
136 d->ret_value = B_VIEW;
137 break;
138 case CK_Edit:
139 d->ret_value = B_EDIT;
140 break;
141 case CK_Enter:
142 d->ret_value = B_ENTER;
143 break;
144 default:
145 return MSG_NOT_HANDLED;
146 }
147
148 dlg_close (d);
149 return MSG_HANDLED;
150 }
151
152 default:
153 return dlg_default_callback (w, sender, msg, parm, data);
154 }
155 }
156
157
158
159 static void
160 history_create_item (history_descriptor_t *hd, void *data)
161 {
162 char *text = (char *) data;
163 size_t width;
164
165 width = str_term_width1 (text);
166 hd->max_width = MAX (width, hd->max_width);
167
168 listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, text, NULL, TRUE);
169 }
170
171
172
173 static void *
174 history_release_item (history_descriptor_t *hd, WLEntry *le)
175 {
176 void *text;
177
178 (void) hd;
179
180 text = le->text;
181 le->text = NULL;
182
183 return text;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 void
198 history_load (const WDialog *h, Widget *w)
199 {
200 char *profile;
201 ev_history_load_save_t event_data;
202
203 if (num_history_items_recorded == 0)
204 return;
205
206 profile = mc_config_get_full_path (MC_HISTORY_FILE);
207 event_data.cfg = mc_config_init (profile, TRUE);
208 event_data.receiver = w;
209
210 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
211
212 mc_config_deinit (event_data.cfg);
213 g_free (profile);
214 }
215
216
217
218
219
220
221
222
223
224
225 void
226 history_save (const WDialog *h, Widget *w)
227 {
228 char *profile;
229 int i;
230
231 if (num_history_items_recorded == 0)
232 return;
233
234 profile = mc_config_get_full_path (MC_HISTORY_FILE);
235 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
236 if (i != -1)
237 close (i);
238
239
240 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
241 {
242 ev_history_load_save_t event_data;
243
244 event_data.cfg = mc_config_init (profile, FALSE);
245 event_data.receiver = w;
246
247 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
248
249 mc_config_save_file (event_data.cfg, NULL);
250 mc_config_deinit (event_data.cfg);
251 }
252
253 g_free (profile);
254 }
255
256
257
258 void
259 history_descriptor_init (history_descriptor_t *hd, int y, int x, GList *history, int current)
260 {
261 hd->list = history;
262 hd->y = y;
263 hd->x = x;
264 hd->current = current;
265 hd->action = CK_IgnoreKey;
266 hd->text = NULL;
267 hd->max_width = 0;
268 hd->listbox = listbox_new (1, 1, 2, 2, TRUE, NULL);
269
270 hd->create = history_create_item;
271 hd->release = history_release_item;
272 hd->free = g_free;
273 }
274
275
276
277 void
278 history_show (history_descriptor_t *hd)
279 {
280 GList *z, *hi;
281 size_t count;
282 WDialog *query_dlg;
283 history_dlg_data hist_data;
284 int dlg_ret;
285
286 if (hd == NULL || hd->list == NULL)
287 return;
288
289 hd->max_width = str_term_width1 (_ ("History")) + 2;
290
291 for (z = hd->list; z != NULL; z = g_list_previous (z))
292 hd->create (hd, z->data);
293
294
295 count = listbox_get_length (hd->listbox);
296
297 hist_data.y = hd->y;
298 hist_data.x = hd->x;
299 hist_data.count = count;
300 hist_data.max_width = hd->max_width;
301
302 query_dlg = dlg_create (TRUE, 0, 0, 4, 4, WPOS_KEEP_DEFAULT, TRUE, dialog_colors,
303 history_dlg_callback, NULL, "[History-query]", _ ("History"));
304 query_dlg->data.p = &hist_data;
305
306
307
308 group_add_widget_autopos (GROUP (query_dlg), hd->listbox, WPOS_KEEP_ALL, NULL);
309
310
311
312
313
314
315 send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
316
317 if (WIDGET (query_dlg)->rect.y < hd->y)
318 {
319
320
321 g_queue_reverse (hd->listbox->list);
322 if (hd->current < 0 || (size_t) hd->current >= count)
323 listbox_select_last (hd->listbox);
324 else
325 listbox_set_current (hd->listbox, count - 1 - (size_t) hd->current);
326 }
327 else
328 {
329
330 if (hd->current > 0)
331 listbox_set_current (hd->listbox, hd->current);
332 }
333
334 dlg_ret = dlg_run (query_dlg);
335 if (dlg_ret != B_CANCEL)
336 {
337 char *q;
338
339 switch (dlg_ret)
340 {
341 case B_EDIT:
342 hd->action = CK_Edit;
343 break;
344 case B_VIEW:
345 hd->action = CK_View;
346 break;
347 default:
348 hd->action = CK_Enter;
349 }
350
351 listbox_get_current (hd->listbox, &q, NULL);
352 hd->text = g_strdup (q);
353 }
354
355
356 z = NULL;
357 for (hi = listbox_get_first_link (hd->listbox); hi != NULL; hi = g_list_next (hi))
358
359 z = g_list_prepend (z, hd->release (hd, LENTRY (hi->data)));
360
361
362 if (WIDGET (query_dlg)->rect.y < hd->y)
363 z = g_list_reverse (z);
364
365 widget_destroy (WIDGET (query_dlg));
366
367 hd->list = g_list_first (hd->list);
368 g_list_free_full (hd->list, hd->free);
369 hd->list = g_list_last (z);
370 }
371
372