history.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2024
  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-2022
  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. /*** forward declarations (file scope functions) *************************************************/
  48. /*** file scope variables ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. /*** file scope functions ************************************************************************/
  51. /* --------------------------------------------------------------------------------------------- */
  52. static cb_ret_t
  53. history_dlg_reposition (WDialog *dlg_head)
  54. {
  55. history_dlg_data *data;
  56. int x = 0, y, he, wi;
  57. WRect r;
  58. /* guard checks */
  59. if (dlg_head == NULL || dlg_head->data.p == NULL)
  60. return MSG_NOT_HANDLED;
  61. data = (history_dlg_data *) dlg_head->data.p;
  62. y = data->y;
  63. he = data->count + 2;
  64. if (he <= y || y > (LINES - 6))
  65. {
  66. he = MIN (he, y - 1);
  67. y -= he;
  68. }
  69. else
  70. {
  71. y++;
  72. he = MIN (he, LINES - y);
  73. }
  74. if (data->x > 2)
  75. x = data->x - 2;
  76. wi = data->max_width + 4;
  77. if ((wi + x) > COLS)
  78. {
  79. wi = MIN (wi, COLS);
  80. x = COLS - wi;
  81. }
  82. rect_init (&r, y, x, he, wi);
  83. return dlg_default_callback (WIDGET (dlg_head), NULL, MSG_RESIZE, 0, &r);
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. static cb_ret_t
  87. history_dlg_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  88. {
  89. switch (msg)
  90. {
  91. case MSG_RESIZE:
  92. return history_dlg_reposition (DIALOG (w));
  93. case MSG_NOTIFY:
  94. {
  95. /* message from listbox */
  96. WDialog *d = DIALOG (w);
  97. switch (parm)
  98. {
  99. case CK_View:
  100. d->ret_value = B_VIEW;
  101. break;
  102. case CK_Edit:
  103. d->ret_value = B_EDIT;
  104. break;
  105. case CK_Enter:
  106. d->ret_value = B_ENTER;
  107. break;
  108. default:
  109. return MSG_NOT_HANDLED;
  110. }
  111. dlg_close (d);
  112. return MSG_HANDLED;
  113. }
  114. default:
  115. return dlg_default_callback (w, sender, msg, parm, data);
  116. }
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. static void
  120. history_create_item (history_descriptor_t *hd, void *data)
  121. {
  122. char *text = (char *) data;
  123. size_t width;
  124. width = str_term_width1 (text);
  125. hd->max_width = MAX (width, hd->max_width);
  126. listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, text, NULL, TRUE);
  127. }
  128. /* --------------------------------------------------------------------------------------------- */
  129. static void *
  130. history_release_item (history_descriptor_t *hd, WLEntry *le)
  131. {
  132. void *text;
  133. (void) hd;
  134. text = le->text;
  135. le->text = NULL;
  136. return text;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. /*** public functions ****************************************************************************/
  140. /* --------------------------------------------------------------------------------------------- */
  141. void
  142. history_descriptor_init (history_descriptor_t *hd, int y, int x, GList *history, int current)
  143. {
  144. hd->list = history;
  145. hd->y = y;
  146. hd->x = x;
  147. hd->current = current;
  148. hd->action = CK_IgnoreKey;
  149. hd->text = NULL;
  150. hd->max_width = 0;
  151. hd->listbox = listbox_new (1, 1, 2, 2, TRUE, NULL);
  152. /* in most cases history list contains string only and no any other data */
  153. hd->create = history_create_item;
  154. hd->release = history_release_item;
  155. hd->free = g_free;
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. void
  159. history_show (history_descriptor_t *hd)
  160. {
  161. GList *z, *hi;
  162. size_t count;
  163. WDialog *query_dlg;
  164. history_dlg_data hist_data;
  165. int dlg_ret;
  166. if (hd == NULL || hd->list == NULL)
  167. return;
  168. hd->max_width = str_term_width1 (_("History")) + 2;
  169. for (z = hd->list; z != NULL; z = g_list_previous (z))
  170. hd->create (hd, z->data);
  171. /* after this, the order of history items is following: recent at begin, oldest at end */
  172. count = listbox_get_length (hd->listbox);
  173. hist_data.y = hd->y;
  174. hist_data.x = hd->x;
  175. hist_data.count = count;
  176. hist_data.max_width = hd->max_width;
  177. query_dlg =
  178. dlg_create (TRUE, 0, 0, 4, 4, WPOS_KEEP_DEFAULT, TRUE, dialog_colors, history_dlg_callback,
  179. NULL, "[History-query]", _("History"));
  180. query_dlg->data.p = &hist_data;
  181. /* this call makes list stick to all sides of dialog, effectively make
  182. it be resized with dialog */
  183. group_add_widget_autopos (GROUP (query_dlg), hd->listbox, WPOS_KEEP_ALL, NULL);
  184. /* to avoid diplicating of (calculating sizes in two places)
  185. code, call history_dlg_callback function here, to set dialog and
  186. controls positions.
  187. The main idea - create 4x4 dialog and add 2x2 list in
  188. center of it, and let dialog function resize it to needed size. */
  189. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  190. if (WIDGET (query_dlg)->rect.y < hd->y)
  191. {
  192. /* history is above base widget -- revert order to place recent item at bottom */
  193. /* revert history direction */
  194. g_queue_reverse (hd->listbox->list);
  195. if (hd->current < 0 || (size_t) hd->current >= count)
  196. listbox_select_last (hd->listbox);
  197. else
  198. listbox_set_current (hd->listbox, count - 1 - (size_t) hd->current);
  199. }
  200. else
  201. {
  202. /* history is below base widget -- keep order to place recent item on top */
  203. if (hd->current > 0)
  204. listbox_set_current (hd->listbox, hd->current);
  205. }
  206. dlg_ret = dlg_run (query_dlg);
  207. if (dlg_ret != B_CANCEL)
  208. {
  209. char *q;
  210. switch (dlg_ret)
  211. {
  212. case B_EDIT:
  213. hd->action = CK_Edit;
  214. break;
  215. case B_VIEW:
  216. hd->action = CK_View;
  217. break;
  218. default:
  219. hd->action = CK_Enter;
  220. }
  221. listbox_get_current (hd->listbox, &q, NULL);
  222. hd->text = g_strdup (q);
  223. }
  224. /* get modified history from dialog */
  225. z = NULL;
  226. for (hi = listbox_get_first_link (hd->listbox); hi != NULL; hi = g_list_next (hi))
  227. /* history is being reverted here again */
  228. z = g_list_prepend (z, hd->release (hd, LENTRY (hi->data)));
  229. /* restore history direction */
  230. if (WIDGET (query_dlg)->rect.y < hd->y)
  231. z = g_list_reverse (z);
  232. widget_destroy (WIDGET (query_dlg));
  233. hd->list = g_list_first (hd->list);
  234. g_list_free_full (hd->list, hd->free);
  235. hd->list = g_list_last (z);
  236. }
  237. /* --------------------------------------------------------------------------------------------- */