dialog-switch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. Support of multiply editors and viewers.
  3. Original idea and code: Oleg "Olegarch" Konovalov <olegarch@linuxinside.com>
  4. Copyright (C) 2009-2017
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Daniel Borca <dborca@yahoo.com>, 2007
  8. Andrew Borodin <aborodin@vmail.ru>, 2010, 2013
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file dialog-switch.c
  22. * \brief Source: support of multiply editors and viewers.
  23. */
  24. #include <config.h>
  25. #include "lib/global.h"
  26. #include "lib/tty/tty.h" /* LINES, COLS */
  27. #include "lib/tty/color.h" /* tty_set_normal_attrs() */
  28. #include "lib/widget.h"
  29. #include "lib/event.h"
  30. /*** global variables ****************************************************************************/
  31. WDialog *midnight_dlg = NULL;
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /* List of dialogs: filemanagers, editors, viewers */
  36. static GList *mc_dialogs = NULL;
  37. /* Currently active dialog */
  38. static GList *mc_current = NULL;
  39. /* Is there any dialogs that we have to run after returning to the manager from another dialog */
  40. static gboolean dialog_switch_pending = FALSE;
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static unsigned char
  44. get_hotkey (int n)
  45. {
  46. return (n <= 9) ? '0' + n : 'a' + n - 10;
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. static void
  50. dialog_switch_suspend (void *data, void *user_data)
  51. {
  52. (void) user_data;
  53. if (data != mc_current->data)
  54. widget_set_state (WIDGET (data), WST_SUSPENDED, TRUE);
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. static void
  58. dialog_switch_goto (GList * dlg)
  59. {
  60. if (mc_current != dlg)
  61. {
  62. WDialog *old = DIALOG (mc_current->data);
  63. mc_current = dlg;
  64. if (old == midnight_dlg)
  65. {
  66. /* switch from panels to another dialog (editor, viewer, etc) */
  67. dialog_switch_pending = TRUE;
  68. dialog_switch_process_pending ();
  69. }
  70. else
  71. {
  72. /* switch from editor, viewer, etc to another dialog */
  73. widget_set_state (WIDGET (old), WST_SUSPENDED, TRUE);
  74. if (DIALOG (dlg->data) != midnight_dlg)
  75. /* switch to another editor, viewer, etc */
  76. /* return to panels before run the required dialog */
  77. dialog_switch_pending = TRUE;
  78. else
  79. {
  80. /* switch to panels */
  81. widget_set_state (WIDGET (midnight_dlg), WST_ACTIVE, TRUE);
  82. do_refresh ();
  83. }
  84. }
  85. }
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. static void
  89. dialog_switch_resize (WDialog * d)
  90. {
  91. if (widget_get_state (WIDGET (d), WST_ACTIVE))
  92. send_message (d, NULL, MSG_RESIZE, 0, NULL);
  93. else
  94. d->winch_pending = TRUE;
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. /*** public functions ****************************************************************************/
  98. /* --------------------------------------------------------------------------------------------- */
  99. void
  100. dialog_switch_add (WDialog * h)
  101. {
  102. GList *dlg;
  103. dlg = g_list_find (mc_dialogs, h);
  104. if (dlg != NULL)
  105. mc_current = dlg;
  106. else
  107. {
  108. mc_dialogs = g_list_prepend (mc_dialogs, h);
  109. mc_current = mc_dialogs;
  110. }
  111. /* suspend forced all other screens */
  112. g_list_foreach (mc_dialogs, dialog_switch_suspend, NULL);
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. void
  116. dialog_switch_remove (WDialog * h)
  117. {
  118. GList *this;
  119. if (DIALOG (mc_current->data) == h)
  120. this = mc_current;
  121. else
  122. this = g_list_find (mc_dialogs, h);
  123. mc_dialogs = g_list_delete_link (mc_dialogs, this);
  124. /* adjust current dialog */
  125. if (top_dlg != NULL)
  126. mc_current = g_list_find (mc_dialogs, DIALOG (top_dlg->data));
  127. else
  128. mc_current = mc_dialogs;
  129. /* resume forced the current screen */
  130. if (mc_current != NULL)
  131. widget_set_state (WIDGET (mc_current->data), WST_ACTIVE, TRUE);
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. size_t
  135. dialog_switch_num (void)
  136. {
  137. return g_list_length (mc_dialogs);
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. void
  141. dialog_switch_next (void)
  142. {
  143. GList *next;
  144. if (mc_global.midnight_shutdown || mc_current == NULL)
  145. return;
  146. next = g_list_next (mc_current);
  147. if (next == NULL)
  148. next = mc_dialogs;
  149. dialog_switch_goto (next);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. dialog_switch_prev (void)
  154. {
  155. GList *prev;
  156. if (mc_global.midnight_shutdown || mc_current == NULL)
  157. return;
  158. prev = g_list_previous (mc_current);
  159. if (prev == NULL)
  160. prev = g_list_last (mc_dialogs);
  161. dialog_switch_goto (prev);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. void
  165. dialog_switch_list (void)
  166. {
  167. const size_t dlg_num = g_list_length (mc_dialogs);
  168. int lines, cols;
  169. Listbox *listbox;
  170. GList *h, *selected;
  171. int i = 0;
  172. if (mc_global.midnight_shutdown || mc_current == NULL)
  173. return;
  174. lines = MIN ((size_t) (LINES * 2 / 3), dlg_num);
  175. cols = COLS * 2 / 3;
  176. listbox = create_listbox_window (lines, cols, _("Screens"), "[Screen selector]");
  177. for (h = mc_dialogs; h != NULL; h = g_list_next (h))
  178. {
  179. WDialog *dlg;
  180. char *title;
  181. dlg = DIALOG (h->data);
  182. if ((dlg != NULL) && (dlg->get_title != NULL))
  183. title = dlg->get_title (dlg, WIDGET (listbox->list)->cols - 2);
  184. else
  185. title = g_strdup ("");
  186. listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, h, FALSE);
  187. g_free (title);
  188. }
  189. selected = run_listbox_with_data (listbox, mc_current);
  190. if (selected != NULL)
  191. dialog_switch_goto (selected);
  192. }
  193. /* --------------------------------------------------------------------------------------------- */
  194. int
  195. dialog_switch_process_pending (void)
  196. {
  197. int ret = 0;
  198. while (dialog_switch_pending)
  199. {
  200. WDialog *h = DIALOG (mc_current->data);
  201. Widget *wh = WIDGET (h);
  202. dialog_switch_pending = FALSE;
  203. widget_set_state (wh, WST_SUSPENDED, TRUE);
  204. ret = dlg_run (h);
  205. if (widget_get_state (wh, WST_CLOSED))
  206. {
  207. dlg_destroy (h);
  208. /* return to panels */
  209. if (mc_global.mc_run_mode == MC_RUN_FULL)
  210. {
  211. mc_current = g_list_find (mc_dialogs, midnight_dlg);
  212. mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "update_panels", NULL);
  213. }
  214. }
  215. }
  216. repaint_screen ();
  217. return ret;
  218. }
  219. /* --------------------------------------------------------------------------------------------- */
  220. void
  221. dialog_switch_got_winch (void)
  222. {
  223. GList *dlg;
  224. for (dlg = mc_dialogs; dlg != NULL; dlg = g_list_next (dlg))
  225. if (dlg != mc_current)
  226. DIALOG (dlg->data)->winch_pending = TRUE;
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. void
  230. dialog_switch_shutdown (void)
  231. {
  232. while (mc_dialogs != NULL)
  233. {
  234. WDialog *dlg = DIALOG (mc_dialogs->data);
  235. dlg_run (dlg);
  236. dlg_destroy (dlg);
  237. }
  238. }
  239. /* --------------------------------------------------------------------------------------------- */
  240. void
  241. clr_scr (void)
  242. {
  243. tty_set_normal_attrs ();
  244. tty_fill_region (0, 0, LINES, COLS, ' ');
  245. tty_refresh ();
  246. }
  247. /* --------------------------------------------------------------------------------------------- */
  248. void
  249. repaint_screen (void)
  250. {
  251. do_refresh ();
  252. tty_refresh ();
  253. }
  254. /* --------------------------------------------------------------------------------------------- */
  255. void
  256. mc_refresh (void)
  257. {
  258. #ifdef ENABLE_BACKGROUND
  259. if (mc_global.we_are_background)
  260. return;
  261. #endif /* ENABLE_BACKGROUND */
  262. if (mc_global.tty.winch_flag == 0)
  263. tty_refresh ();
  264. else
  265. {
  266. /* if winch was caugth, we should do not only redraw screen, but
  267. reposition/resize all */
  268. dialog_change_screen_size ();
  269. }
  270. }
  271. /* --------------------------------------------------------------------------------------------- */
  272. void
  273. dialog_change_screen_size (void)
  274. {
  275. GList *d;
  276. mc_global.tty.winch_flag = 0;
  277. tty_change_screen_size ();
  278. #ifdef HAVE_SLANG
  279. tty_keypad (TRUE);
  280. tty_nodelay (FALSE);
  281. #endif
  282. /* Inform all suspending dialogs */
  283. dialog_switch_got_winch ();
  284. /* Inform all running dialogs from first to last */
  285. for (d = g_list_last (top_dlg); d != NULL; d = g_list_previous (d))
  286. dialog_switch_resize (DIALOG (d->data));
  287. /* Now, force the redraw */
  288. repaint_screen ();
  289. }
  290. /* --------------------------------------------------------------------------------------------- */