dialog-switch.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2009, 2010 Free Software Foundation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. * Original idea and code: Oleg "Olegarch" Konovalov <olegarch@linuxinside.com>
  19. * Written by: 2007 Daniel Borca <dborca@yahoo.com>
  20. * 2010 Andrew Borodin <aborodin@vmail.ru>
  21. */
  22. /** \file dialog-switch.c
  23. * \brief Source: support of multiply editors and viewers.
  24. */
  25. #include <config.h>
  26. #include "lib/global.h"
  27. #include "lib/tty/tty.h" /* LINES, COLS */
  28. #include "dialog.h"
  29. #include "widget.h"
  30. #include "layout.h" /* repaint_screen() */
  31. #include "main-widgets.h" /* midnight_dlg */
  32. #include "main.h" /* midnight_shutdown */
  33. #include "wtools.h" /* Listbox */
  34. #include "dialog-switch.h"
  35. /*** global variables **************************************************/
  36. /*** file scope macro definitions **************************************/
  37. /*** file scope type declarations **************************************/
  38. /*** file scope variables **********************************************/
  39. /* List of dialogs: filemanagers, editors, viewers */
  40. static GList *mc_dialogs = NULL;
  41. /* Currently active dialog */
  42. static GList *mc_current = NULL;
  43. /* Is there any dialogs that we have to run after returning to the manager from another dialog */
  44. static gboolean dialog_switch_pending = FALSE;
  45. /*** file scope functions **********************************************/
  46. static unsigned char
  47. get_hotkey (int n)
  48. {
  49. return (n <= 9) ? '0' + n : 'a' + n - 10;
  50. }
  51. static void
  52. dialog_switch_goto (GList *dlg)
  53. {
  54. if (mc_current != dlg)
  55. {
  56. Dlg_head *old = (Dlg_head *) mc_current->data;
  57. mc_current = dlg;
  58. if (old == midnight_dlg)
  59. {
  60. /* switch from panels to another dialog (editor, viewer, etc) */
  61. dialog_switch_pending = TRUE;
  62. dialog_switch_process_pending ();
  63. }
  64. else
  65. {
  66. /* switch from editor, viewer, etc to another dialog */
  67. old->state = DLG_SUSPENDED;
  68. if ((Dlg_head *) dlg->data != midnight_dlg)
  69. /* switch to another editor, viewer, etc */
  70. /* return to panels before run the required dialog */
  71. dialog_switch_pending = TRUE;
  72. else
  73. /* switch to panels */
  74. do_refresh ();
  75. }
  76. }
  77. }
  78. /*** public functions **************************************************/
  79. void
  80. dialog_switch_add (Dlg_head *h)
  81. {
  82. GList *dlg;
  83. dlg = g_list_find (mc_dialogs, h);
  84. if (dlg != NULL)
  85. mc_current = dlg;
  86. else
  87. {
  88. mc_dialogs = g_list_prepend (mc_dialogs, h);
  89. mc_current = mc_dialogs;
  90. }
  91. }
  92. void
  93. dialog_switch_remove (Dlg_head *h)
  94. {
  95. GList *this;
  96. if ((Dlg_head *) mc_current->data == h)
  97. this = mc_current;
  98. else
  99. this = g_list_find (mc_dialogs, h);
  100. mc_dialogs = g_list_delete_link (mc_dialogs, this);
  101. /* adjust current dialog */
  102. if (top_dlg != NULL)
  103. mc_current = g_list_find (mc_dialogs, (Dlg_head *) top_dlg->data);
  104. else
  105. mc_current = mc_dialogs;
  106. }
  107. size_t
  108. dialog_switch_num (void)
  109. {
  110. return g_list_length (mc_dialogs);
  111. }
  112. void
  113. dialog_switch_next (void)
  114. {
  115. GList *next;
  116. if (midnight_shutdown || mc_current == NULL)
  117. return;
  118. next = g_list_next (mc_current);
  119. if (next == NULL)
  120. next = mc_dialogs;
  121. dialog_switch_goto (next);
  122. }
  123. void
  124. dialog_switch_prev (void)
  125. {
  126. GList *prev;
  127. if (midnight_shutdown || mc_current == NULL)
  128. return;
  129. prev = g_list_previous (mc_current);
  130. if (prev == NULL)
  131. prev = g_list_last (mc_dialogs);
  132. dialog_switch_goto (prev);
  133. }
  134. void
  135. dialog_switch_list (void)
  136. {
  137. const size_t dlg_num = g_list_length (mc_dialogs);
  138. int lines, cols;
  139. Listbox *listbox;
  140. GList *h;
  141. int i = 0;
  142. int rv;
  143. if (midnight_shutdown || mc_current == NULL)
  144. return;
  145. lines = min ((size_t) (LINES * 2/3), dlg_num);
  146. cols = COLS * 2/3;
  147. listbox = create_listbox_window (lines, cols, _("Screens"), "[Screen selector]");
  148. for (h = mc_dialogs; h != NULL; h = g_list_next (h))
  149. {
  150. Dlg_head *dlg;
  151. char *title;
  152. dlg = (Dlg_head *) h->data;
  153. if ((dlg != NULL) && (dlg->get_title != NULL))
  154. title = dlg->get_title (dlg, listbox->list->widget.cols - 2); /* FIXME! */
  155. else
  156. title = g_strdup ("");
  157. listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, NULL);
  158. g_free (title);
  159. }
  160. listbox_select_entry (listbox->list, dlg_num - 1 - g_list_position (mc_dialogs, mc_current));
  161. rv = run_listbox (listbox);
  162. if (rv >= 0)
  163. {
  164. h = g_list_nth (mc_dialogs, dlg_num - 1 - rv);
  165. dialog_switch_goto (h);
  166. }
  167. }
  168. int
  169. dialog_switch_process_pending (void)
  170. {
  171. int ret = 0;
  172. while (dialog_switch_pending)
  173. {
  174. Dlg_head *h = (Dlg_head *) mc_current->data;
  175. dialog_switch_pending = FALSE;
  176. h->state = DLG_SUSPENDED;
  177. ret = run_dlg (h);
  178. if (h->state == DLG_CLOSED)
  179. {
  180. destroy_dlg (h);
  181. /* return to panels */
  182. if (mc_run_mode == MC_RUN_FULL)
  183. {
  184. mc_current = g_list_find (mc_dialogs, midnight_dlg);
  185. update_panels (UP_OPTIMIZE, UP_KEEPSEL);
  186. }
  187. }
  188. }
  189. repaint_screen ();
  190. return ret;
  191. }
  192. void
  193. dialog_switch_got_winch (void)
  194. {
  195. GList *dlg;
  196. for (dlg = mc_dialogs; dlg != NULL; dlg = g_list_next (dlg))
  197. if (dlg != mc_current)
  198. ((Dlg_head *) dlg->data)->winch_pending = TRUE;
  199. }
  200. void
  201. dialog_switch_shutdown (void)
  202. {
  203. while (mc_dialogs != NULL)
  204. {
  205. Dlg_head *dlg = (Dlg_head *) mc_dialogs->data;
  206. run_dlg (dlg);
  207. destroy_dlg (dlg);
  208. }
  209. }