dialog-switch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. Support of multiply editors and viewers.
  3. Original idea and code: Oleg "Olegarch" Konovalov <olegarch@linuxinside.com>
  4. Copyright (c) 2009, 2010, 2011
  5. The Free Software Foundation
  6. Written by:
  7. Daniel Borca <dborca@yahoo.com>, 2007
  8. Andrew Borodin <aborodin@vmail.ru>, 2010
  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. /* If TIOCGWINSZ supported, make it available here, because window resizing code
  26. * depends on it... */
  27. #ifdef HAVE_SYS_IOCTL_H
  28. #include <sys/ioctl.h>
  29. #endif
  30. #include <termios.h>
  31. #include "lib/global.h"
  32. #include "lib/tty/tty.h" /* LINES, COLS */
  33. #include "lib/tty/win.h" /* do_enter_ca_mode() */
  34. #include "lib/tty/color.h" /* tty_set_normal_attrs() */
  35. #include "lib/widget.h"
  36. #include "lib/event.h"
  37. /*** global variables ****************************************************************************/
  38. Dlg_head *midnight_dlg = NULL;
  39. /*** file scope macro definitions ****************************************************************/
  40. /*** file scope type declarations ****************************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /* List of dialogs: filemanagers, editors, viewers */
  43. static GList *mc_dialogs = NULL;
  44. /* Currently active dialog */
  45. static GList *mc_current = NULL;
  46. /* Is there any dialogs that we have to run after returning to the manager from another dialog */
  47. static gboolean dialog_switch_pending = FALSE;
  48. /*** file scope functions ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. static unsigned char
  51. get_hotkey (int n)
  52. {
  53. return (n <= 9) ? '0' + n : 'a' + n - 10;
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. static void
  57. dialog_switch_goto (GList * dlg)
  58. {
  59. if (mc_current != dlg)
  60. {
  61. Dlg_head *old = (Dlg_head *) mc_current->data;
  62. mc_current = dlg;
  63. if (old == midnight_dlg)
  64. {
  65. /* switch from panels to another dialog (editor, viewer, etc) */
  66. dialog_switch_pending = TRUE;
  67. dialog_switch_process_pending ();
  68. }
  69. else
  70. {
  71. /* switch from editor, viewer, etc to another dialog */
  72. old->state = DLG_SUSPENDED;
  73. if ((Dlg_head *) dlg->data != midnight_dlg)
  74. /* switch to another editor, viewer, etc */
  75. /* return to panels before run the required dialog */
  76. dialog_switch_pending = TRUE;
  77. else
  78. /* switch to panels */
  79. do_refresh ();
  80. }
  81. }
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. #if defined TIOCGWINSZ
  85. static void
  86. dlg_resize_cb (void *data, void *user_data)
  87. {
  88. Dlg_head *d = data;
  89. (void) user_data;
  90. d->callback (d, NULL, DLG_RESIZE, 0, NULL);
  91. }
  92. #endif
  93. /* --------------------------------------------------------------------------------------------- */
  94. /*** public functions ****************************************************************************/
  95. /* --------------------------------------------------------------------------------------------- */
  96. void
  97. dialog_switch_add (Dlg_head * h)
  98. {
  99. GList *dlg;
  100. dlg = g_list_find (mc_dialogs, h);
  101. if (dlg != NULL)
  102. mc_current = dlg;
  103. else
  104. {
  105. mc_dialogs = g_list_prepend (mc_dialogs, h);
  106. mc_current = mc_dialogs;
  107. }
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. void
  111. dialog_switch_remove (Dlg_head * h)
  112. {
  113. GList *this;
  114. if ((Dlg_head *) mc_current->data == h)
  115. this = mc_current;
  116. else
  117. this = g_list_find (mc_dialogs, h);
  118. mc_dialogs = g_list_delete_link (mc_dialogs, this);
  119. /* adjust current dialog */
  120. if (top_dlg != NULL)
  121. mc_current = g_list_find (mc_dialogs, (Dlg_head *) top_dlg->data);
  122. else
  123. mc_current = mc_dialogs;
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. size_t
  127. dialog_switch_num (void)
  128. {
  129. return g_list_length (mc_dialogs);
  130. }
  131. /* --------------------------------------------------------------------------------------------- */
  132. void
  133. dialog_switch_next (void)
  134. {
  135. GList *next;
  136. if (mc_global.widget.midnight_shutdown || mc_current == NULL)
  137. return;
  138. next = g_list_next (mc_current);
  139. if (next == NULL)
  140. next = mc_dialogs;
  141. dialog_switch_goto (next);
  142. }
  143. /* --------------------------------------------------------------------------------------------- */
  144. void
  145. dialog_switch_prev (void)
  146. {
  147. GList *prev;
  148. if (mc_global.widget.midnight_shutdown || mc_current == NULL)
  149. return;
  150. prev = g_list_previous (mc_current);
  151. if (prev == NULL)
  152. prev = g_list_last (mc_dialogs);
  153. dialog_switch_goto (prev);
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. void
  157. dialog_switch_list (void)
  158. {
  159. const size_t dlg_num = g_list_length (mc_dialogs);
  160. int lines, cols;
  161. Listbox *listbox;
  162. GList *h;
  163. int i = 0;
  164. int rv;
  165. if (mc_global.widget.midnight_shutdown || mc_current == NULL)
  166. return;
  167. lines = min ((size_t) (LINES * 2 / 3), dlg_num);
  168. cols = COLS * 2 / 3;
  169. listbox = create_listbox_window (lines, cols, _("Screens"), "[Screen selector]");
  170. for (h = mc_dialogs; h != NULL; h = g_list_next (h))
  171. {
  172. Dlg_head *dlg;
  173. char *title;
  174. dlg = (Dlg_head *) h->data;
  175. if ((dlg != NULL) && (dlg->get_title != NULL))
  176. title = dlg->get_title (dlg, listbox->list->widget.cols - 2); /* FIXME! */
  177. else
  178. title = g_strdup ("");
  179. listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, NULL);
  180. g_free (title);
  181. }
  182. listbox_select_entry (listbox->list, dlg_num - 1 - g_list_position (mc_dialogs, mc_current));
  183. rv = run_listbox (listbox);
  184. if (rv >= 0)
  185. {
  186. h = g_list_nth (mc_dialogs, dlg_num - 1 - rv);
  187. dialog_switch_goto (h);
  188. }
  189. }
  190. /* --------------------------------------------------------------------------------------------- */
  191. int
  192. dialog_switch_process_pending (void)
  193. {
  194. int ret = 0;
  195. while (dialog_switch_pending)
  196. {
  197. Dlg_head *h = (Dlg_head *) mc_current->data;
  198. dialog_switch_pending = FALSE;
  199. h->state = DLG_SUSPENDED;
  200. ret = run_dlg (h);
  201. if (h->state == DLG_CLOSED)
  202. {
  203. destroy_dlg (h);
  204. /* return to panels */
  205. if (mc_global.mc_run_mode == MC_RUN_FULL)
  206. {
  207. mc_current = g_list_find (mc_dialogs, midnight_dlg);
  208. mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "update_panels", NULL);
  209. }
  210. }
  211. }
  212. repaint_screen ();
  213. return ret;
  214. }
  215. /* --------------------------------------------------------------------------------------------- */
  216. void
  217. dialog_switch_got_winch (void)
  218. {
  219. GList *dlg;
  220. for (dlg = mc_dialogs; dlg != NULL; dlg = g_list_next (dlg))
  221. if (dlg != mc_current)
  222. ((Dlg_head *) dlg->data)->winch_pending = TRUE;
  223. }
  224. /* --------------------------------------------------------------------------------------------- */
  225. void
  226. dialog_switch_shutdown (void)
  227. {
  228. while (mc_dialogs != NULL)
  229. {
  230. Dlg_head *dlg = (Dlg_head *) mc_dialogs->data;
  231. run_dlg (dlg);
  232. destroy_dlg (dlg);
  233. }
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. void
  237. clr_scr (void)
  238. {
  239. tty_set_normal_attrs ();
  240. tty_fill_region (0, 0, LINES, COLS, ' ');
  241. tty_refresh ();
  242. }
  243. /* --------------------------------------------------------------------------------------------- */
  244. void
  245. repaint_screen (void)
  246. {
  247. do_refresh ();
  248. tty_refresh ();
  249. }
  250. /* --------------------------------------------------------------------------------------------- */
  251. void
  252. mc_refresh (void)
  253. {
  254. #ifdef WITH_BACKGROUND
  255. if (mc_global.we_are_background)
  256. return;
  257. #endif /* WITH_BACKGROUND */
  258. if (!mc_global.tty.winch_flag)
  259. tty_refresh ();
  260. else
  261. {
  262. /* if winch was caugth, we should do not only redraw screen, but
  263. reposition/resize all */
  264. dialog_change_screen_size ();
  265. }
  266. }
  267. /* --------------------------------------------------------------------------------------------- */
  268. void
  269. dialog_change_screen_size (void)
  270. {
  271. mc_global.tty.winch_flag = FALSE;
  272. #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
  273. #if defined TIOCGWINSZ
  274. #ifndef NCURSES_VERSION
  275. tty_noraw_mode ();
  276. tty_reset_screen ();
  277. #endif
  278. tty_change_screen_size ();
  279. #ifdef HAVE_SLANG
  280. /* XSI Curses spec states that portable applications shall not invoke
  281. * initscr() more than once. This kludge could be done within the scope
  282. * of the specification by using endwin followed by a refresh (in fact,
  283. * more than one curses implementation does this); it is guaranteed to work
  284. * only with slang.
  285. */
  286. SLsmg_init_smg ();
  287. do_enter_ca_mode ();
  288. tty_keypad (TRUE);
  289. tty_nodelay (FALSE);
  290. #endif
  291. /* Inform all suspending dialogs */
  292. dialog_switch_got_winch ();
  293. /* Inform all running dialogs */
  294. g_list_foreach (top_dlg, (GFunc) dlg_resize_cb, NULL);
  295. /* Now, force the redraw */
  296. repaint_screen ();
  297. #endif /* TIOCGWINSZ */
  298. #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
  299. }
  300. /* --------------------------------------------------------------------------------------------- */