history.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2019
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009-2019
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file history.c
  25. * \brief Source: show history
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h" /* LINES, COLS */
  32. #include "lib/strutil.h"
  33. #include "lib/widget.h"
  34. #include "lib/keybind.h" /* CK_* */
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. #define B_VIEW (B_USER + 1)
  38. #define B_EDIT (B_USER + 2)
  39. /*** file scope type declarations ****************************************************************/
  40. typedef struct
  41. {
  42. int y;
  43. int x;
  44. size_t count;
  45. size_t max_width;
  46. } history_dlg_data;
  47. /*** file scope variables ************************************************************************/
  48. /*** file scope functions ************************************************************************/
  49. static cb_ret_t
  50. history_dlg_reposition (WDialog * dlg_head)
  51. {
  52. history_dlg_data *data;
  53. int x = 0, y, he, wi;
  54. /* guard checks */
  55. if ((dlg_head == NULL) || (dlg_head->data == NULL))
  56. return MSG_NOT_HANDLED;
  57. data = (history_dlg_data *) dlg_head->data;
  58. y = data->y;
  59. he = data->count + 2;
  60. if (he <= y || y > (LINES - 6))
  61. {
  62. he = MIN (he, y - 1);
  63. y -= he;
  64. }
  65. else
  66. {
  67. y++;
  68. he = MIN (he, LINES - y);
  69. }
  70. if (data->x > 2)
  71. x = data->x - 2;
  72. wi = data->max_width + 4;
  73. if ((wi + x) > COLS)
  74. {
  75. wi = MIN (wi, COLS);
  76. x = COLS - wi;
  77. }
  78. dlg_set_position (dlg_head, y, x, he, wi);
  79. return MSG_HANDLED;
  80. }
  81. /* --------------------------------------------------------------------------------------------- */
  82. static cb_ret_t
  83. history_dlg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  84. {
  85. switch (msg)
  86. {
  87. case MSG_RESIZE:
  88. return history_dlg_reposition (DIALOG (w));
  89. case MSG_NOTIFY:
  90. {
  91. /* message from listbox */
  92. WDialog *d = DIALOG (w);
  93. switch (parm)
  94. {
  95. case CK_View:
  96. d->ret_value = B_VIEW;
  97. break;
  98. case CK_Edit:
  99. d->ret_value = B_EDIT;
  100. break;
  101. case CK_Enter:
  102. d->ret_value = B_ENTER;
  103. break;
  104. default:
  105. return MSG_NOT_HANDLED;
  106. }
  107. dlg_stop (d);
  108. return MSG_HANDLED;
  109. }
  110. default:
  111. return dlg_default_callback (w, sender, msg, parm, data);
  112. }
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. static void
  116. history_create_item (history_descriptor_t * hd, void *data)
  117. {
  118. char *text = (char *) data;
  119. size_t width;
  120. width = str_term_width1 (text);
  121. hd->max_width = MAX (width, hd->max_width);
  122. listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, text, NULL, TRUE);
  123. }
  124. /* --------------------------------------------------------------------------------------------- */
  125. static void *
  126. history_release_item (history_descriptor_t * hd, WLEntry * le)
  127. {
  128. void *text;
  129. (void) hd;
  130. text = le->text;
  131. le->text = NULL;
  132. return text;
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. /*** public functions ****************************************************************************/
  136. /* --------------------------------------------------------------------------------------------- */
  137. void
  138. history_descriptor_init (history_descriptor_t * hd, int y, int x, GList * history, int current)
  139. {
  140. hd->list = history;
  141. hd->y = y;
  142. hd->x = x;
  143. hd->current = current;
  144. hd->action = CK_IgnoreKey;
  145. hd->text = NULL;
  146. hd->max_width = 0;
  147. hd->listbox = listbox_new (1, 1, 2, 2, TRUE, NULL);
  148. /* in most cases history list contains string only and no any other data */
  149. hd->create = history_create_item;
  150. hd->release = history_release_item;
  151. hd->free = g_free;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. void
  155. history_show (history_descriptor_t * hd)
  156. {
  157. GList *z, *hi;
  158. size_t count;
  159. WDialog *query_dlg;
  160. history_dlg_data hist_data;
  161. int dlg_ret;
  162. if (hd == NULL || hd->list == NULL)
  163. return;
  164. hd->max_width = str_term_width1 (_("History")) + 2;
  165. for (z = hd->list; z != NULL; z = g_list_previous (z))
  166. hd->create (hd, z->data);
  167. /* after this, the order of history items is following: recent at begin, oldest at end */
  168. count = listbox_get_length (hd->listbox);
  169. hist_data.y = hd->y;
  170. hist_data.x = hd->x;
  171. hist_data.count = count;
  172. hist_data.max_width = hd->max_width;
  173. query_dlg =
  174. dlg_create (TRUE, 0, 0, 4, 4, WPOS_KEEP_DEFAULT, TRUE, dialog_colors, history_dlg_callback,
  175. NULL, "[History-query]", _("History"));
  176. query_dlg->data = &hist_data;
  177. /* this call makes list stick to all sides of dialog, effectively make
  178. it be resized with dialog */
  179. add_widget_autopos (query_dlg, hd->listbox, WPOS_KEEP_ALL, NULL);
  180. /* to avoid diplicating of (calculating sizes in two places)
  181. code, call history_dlg_callback function here, to set dialog and
  182. controls positions.
  183. The main idea - create 4x4 dialog and add 2x2 list in
  184. center of it, and let dialog function resize it to needed size. */
  185. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  186. if (WIDGET (query_dlg)->y < hd->y)
  187. {
  188. /* history is above base widget -- revert order to place recent item at bottom */
  189. /* revert history direction */
  190. g_queue_reverse (hd->listbox->list);
  191. if (hd->current < 0 || (size_t) hd->current >= count)
  192. listbox_select_last (hd->listbox);
  193. else
  194. listbox_select_entry (hd->listbox, count - 1 - (size_t) hd->current);
  195. }
  196. else
  197. {
  198. /* history is below base widget -- keep order to place recent item on top */
  199. if (hd->current > 0)
  200. listbox_select_entry (hd->listbox, hd->current);
  201. }
  202. dlg_ret = dlg_run (query_dlg);
  203. if (dlg_ret != B_CANCEL)
  204. {
  205. char *q;
  206. switch (dlg_ret)
  207. {
  208. case B_EDIT:
  209. hd->action = CK_Edit;
  210. break;
  211. case B_VIEW:
  212. hd->action = CK_View;
  213. break;
  214. default:
  215. hd->action = CK_Enter;
  216. }
  217. listbox_get_current (hd->listbox, &q, NULL);
  218. hd->text = g_strdup (q);
  219. }
  220. /* get modified history from dialog */
  221. z = NULL;
  222. for (hi = listbox_get_first_link (hd->listbox); hi != NULL; hi = g_list_next (hi))
  223. /* history is being reverted here again */
  224. z = g_list_prepend (z, hd->release (hd, LENTRY (hi->data)));
  225. /* restore history direction */
  226. if (WIDGET (query_dlg)->y < hd->y)
  227. z = g_list_reverse (z);
  228. dlg_destroy (query_dlg);
  229. hd->list = g_list_first (hd->list);
  230. g_list_free_full (hd->list, hd->free);
  231. hd->list = g_list_last (z);
  232. }
  233. /* --------------------------------------------------------------------------------------------- */