2 * Copyright (c) 2009, 2010 Free Software Foundation
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.
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.
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
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>
23 /** \file dialog-switch.c
24 * \brief Source: support of multiply editors and viewers.
29 #include "lib/global.h"
30 #include "lib/tty/tty.h" /* LINES, COLS */
34 #include "layout.h" /* repaint_screen() */
35 #include "main-widgets.h" /* midnight_dlg */
36 #include "main.h" /* midnight_shutdown */
37 #include "wtools.h" /* Listbox */
38 #include "dialog-switch.h"
40 /*** global variables **************************************************/
42 /*** file scope macro definitions **************************************/
44 /*** file scope type declarations **************************************/
46 /*** file scope variables **********************************************/
48 /* List of dialogs: filemanagers, editors, viewers */
49 static GList
*mc_dialogs
= NULL
;
50 /* Currently active dialog */
51 static GList
*mc_current
= NULL
;
52 /* Is there any dialogs that we have to run after returning to the manager from another dialog */
53 static gboolean dialog_switch_pending
= FALSE
;
55 /*** file scope functions **********************************************/
60 return (n
<= 9) ? '0' + n
: 'a' + n
- 10;
64 dialog_switch_goto (GList
*dlg
)
66 if (mc_current
!= dlg
)
68 Dlg_head
*old
= (Dlg_head
*) mc_current
->data
;
72 if (old
== midnight_dlg
)
74 /* switch from panels to another dialog (editor, viewer, etc) */
75 dialog_switch_pending
= TRUE
;
76 dialog_switch_process_pending ();
80 /* switch from editor, viewer, etc to another dialog */
81 old
->state
= DLG_SUSPENDED
;
83 if ((Dlg_head
*) dlg
->data
!= midnight_dlg
)
84 /* switch to another editor, viewer, etc */
85 /* return to panels before run the required dialog */
86 dialog_switch_pending
= TRUE
;
88 /* switch to panels */
94 /*** public functions **************************************************/
97 dialog_switch_add (Dlg_head
*h
)
101 dlg
= g_list_find (mc_dialogs
, h
);
107 mc_dialogs
= g_list_prepend (mc_dialogs
, h
);
108 mc_current
= mc_dialogs
;
113 dialog_switch_remove (Dlg_head
*h
)
117 if ((Dlg_head
*) mc_current
->data
== h
)
120 this = g_list_find (mc_dialogs
, h
);
126 next
= g_list_next (this);
127 mc_dialogs
= g_list_delete_link (mc_dialogs
, this);
128 mc_current
= next
== NULL
? mc_dialogs
: next
;
133 dialog_switch_num (void)
135 return g_list_length (mc_dialogs
);
139 dialog_switch_next (void)
143 if (midnight_shutdown
|| mc_current
== NULL
)
146 next
= g_list_next (mc_current
);
150 dialog_switch_goto (next
);
154 dialog_switch_prev (void)
158 if (midnight_shutdown
|| mc_current
== NULL
)
161 prev
= g_list_previous (mc_current
);
163 prev
= g_list_last (mc_dialogs
);
165 dialog_switch_goto (prev
);
169 dialog_switch_list (void)
171 const size_t dlg_num
= g_list_length (mc_dialogs
);
178 if (midnight_shutdown
|| mc_current
== NULL
)
181 lines
= min ((size_t) (LINES
* 2/3), dlg_num
);
184 listbox
= create_listbox_window (lines
, cols
, _("Screens"), "[Screen selector]");
186 for (h
= mc_dialogs
; h
!= NULL
; h
= g_list_next (h
))
191 dlg
= (Dlg_head
*) h
->data
;
193 if ((dlg
!= NULL
) && (dlg
->get_title
!= NULL
))
194 title
= dlg
->get_title (dlg
, listbox
->list
->widget
.cols
- 2); /* FIXME! */
196 title
= g_strdup ("");
198 listbox_add_item (listbox
->list
, LISTBOX_APPEND_BEFORE
, get_hotkey (i
++), title
, NULL
);
203 listbox_select_entry (listbox
->list
, dlg_num
- 1 - g_list_position (mc_dialogs
, mc_current
));
204 rv
= run_listbox (listbox
);
208 h
= g_list_nth (mc_dialogs
, dlg_num
- 1 - rv
);
209 dialog_switch_goto (h
);
214 dialog_switch_process_pending (void)
218 while (dialog_switch_pending
)
220 Dlg_head
*h
= (Dlg_head
*) mc_current
->data
;
222 dialog_switch_pending
= FALSE
;
223 h
->state
= DLG_SUSPENDED
;
225 if (h
->state
== DLG_CLOSED
)
228 /* return to panels */
229 if (mc_run_mode
== MC_RUN_FULL
)
231 mc_current
= g_list_find (mc_dialogs
, midnight_dlg
);
232 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
243 dialog_switch_got_winch (void)
247 for (dlg
= mc_dialogs
; dlg
!= NULL
; dlg
= g_list_next (dlg
))
248 if (dlg
!= mc_current
)
249 ((Dlg_head
*) dlg
->data
)->winch_pending
= TRUE
;
253 dialog_switch_shutdown (void)
255 while (mc_dialogs
!= NULL
)
257 Dlg_head
*dlg
= (Dlg_head
*) mc_dialogs
->data
;