* screen.c (panel_callback): Revert last change, it has bad side
[midnight-commander.git] / pc / drive.c
blobfe8bf6a2a3664e2b67e7130df272060bf59cb331
1 /* Ch-Drive command for Windows NT and OS/2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 Bug:
18 the code will not work if you have more drives than those that
19 can fit in a panel.
22 #include <config.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include "../src/global.h"
28 #include "../src/tty.h"
29 #include "../src/mad.h"
30 #include "../src/util.h"
31 #include "../src/win.h"
32 #include "../src/color.h"
33 #include "../src/dlg.h"
34 #include "../src/widget.h"
35 #include "../src/dialog.h"
36 #include "../src/dir.h"
37 #include "../src/panel.h"
38 #include "../src/main.h"
39 #include "../src/cmd.h"
40 #include "util_win32.h"
42 struct Dlg_head *drive_dlg;
43 WPanel *this_panel;
45 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg);
46 static void drive_dlg_refresh (void);
47 static void drive_cmd(void);
49 #define B_DRIVE_BASE 100
50 #define MAX_LGH 13 /* Length for drives */
52 static void drive_cmd()
54 int i, nNewDrive, nDrivesAvail;
55 char szTempBuf[7], szDrivesAvail[27*4], *p;
57 /* Dialogbox position */
58 int x_pos;
59 int y_pos = (LINES-6)/2-3;
60 int y_height;
61 int x_width;
63 int m_drv;
65 /* Get drives name and count */
66 #ifdef _OS_NT
67 GetLogicalDriveStrings (255, szDrivesAvail);
68 for (nDrivesAvail = 0, p = szDrivesAvail; *p; nDrivesAvail++)
69 p+=4;
70 #else
71 unsigned long uDriveNum, uDriveMap;
72 nDrivesAvail = 0;
73 p = szDrivesAvail;
74 DosQueryCurrentDisk(&uDriveNum, &uDriveMap);
75 for (i = 0; i < 26; i++) {
76 if ( uDriveMap & (1 << i) ) {
77 *p = 'A' + i;
78 p += 4;
79 nDrivesAvail++;
82 *p = 0;
83 #endif
85 /* Create Dialog */
86 do_refresh ();
88 m_drv = ((nDrivesAvail > MAX_LGH) ? MAX_LGH: nDrivesAvail);
89 /* Center on x, relative to panel */
90 x_pos = this_panel->widget.x + (this_panel->widget.cols - m_drv*3)/2 + 2;
92 if (nDrivesAvail > MAX_LGH) {
93 y_height = 8;
94 x_width = 33;
95 } else {
96 y_height = 6;
97 x_width = (nDrivesAvail - 1) * 2 + 9;
100 drive_dlg = create_dlg (y_pos, x_pos, y_height, x_width, dialog_colors,
101 drive_dlg_callback, "[ChDrive]", "drive", DLG_NONE);
103 x_set_dialog_title (drive_dlg, "Change Drive");
105 if (nDrivesAvail>MAX_LGH) {
106 for (i = 0; i < nDrivesAvail - MAX_LGH; i++) {
107 p -= 4;
108 sprintf(szTempBuf, "&%c", *p);
109 add_widget (drive_dlg,
110 button_new (5,
111 (m_drv-i-1)*2+4 - (MAX_LGH*2 - nDrivesAvail) * 2,
112 B_DRIVE_BASE + nDrivesAvail - i - 1,
113 HIDDEN_BUTTON,
114 szTempBuf, 0, NULL, NULL));
118 /* Add a button for each drive */
119 for (i = 0; i < m_drv; i++) {
120 p -= 4;
121 sprintf (szTempBuf, "&%c", *p);
122 add_widget (drive_dlg,
123 button_new (3, (m_drv-i-1)*2+4, B_DRIVE_BASE+m_drv-i-1,
124 HIDDEN_BUTTON, szTempBuf, 0, NULL, NULL));
127 run_dlg(drive_dlg);
129 /* do action */
130 if (drive_dlg->ret_value != B_CANCEL) {
131 int errocc = 0; /* no error */
132 int rtn;
133 char drvLetter;
135 nNewDrive = drive_dlg->ret_value - B_DRIVE_BASE;
136 drvLetter = (char) *(szDrivesAvail + (nNewDrive*4));
137 #ifdef _OS_NT
138 if (win32_GetPlatform() == OS_WinNT) { /* Windows NT */
139 rtn = _chdrive(drvLetter - 'A' + 1);
140 } else { /* Windows 95 */
141 rtn = 1;
142 SetCurrentDirectory(szDrivesAvail+(nNewDrive*4));
144 #else
145 rtn = DosSetDefaultDisk(nNewDrive + 1);
146 #endif
147 if (rtn == -1)
148 errocc = 1;
149 else {
150 getcwd (this_panel->cwd, sizeof (this_panel->cwd)-2);
151 if (toupper(drvLetter) == toupper(*(this_panel->cwd))) {
152 clean_dir (&this_panel->dir, this_panel->count);
153 this_panel->count = do_load_dir(&this_panel->dir,
154 this_panel->sort_type,
155 this_panel->reverse,
156 this_panel->case_sensitive,
157 this_panel->filter);
158 this_panel->top_file = 0;
159 this_panel->selected = 0;
160 this_panel->marked = 0;
161 this_panel->total = 0;
162 show_dir(this_panel);
163 reread_cmd();
164 } else
165 errocc = 1;
167 if (errocc)
168 message (1, " Error ", " Cannot access drive %c: ",
169 *(szDrivesAvail+(nNewDrive*4)) );
171 destroy_dlg (drive_dlg);
172 repaint_screen ();
175 void drive_cmd_a()
177 this_panel = left_panel;
178 drive_cmd();
181 void drive_cmd_b()
183 this_panel = right_panel;
184 drive_cmd();
187 void drive_chg(WPanel *panel)
189 this_panel = panel;
190 drive_cmd();
193 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg)
195 switch (Msg) {
196 case DLG_DRAW:
197 drive_dlg_refresh ();
198 break;
200 return 0;
203 static void drive_dlg_refresh (void)
205 attrset (dialog_colors[0]);
206 dlg_erase (drive_dlg);
207 draw_box (drive_dlg, 1, 1, drive_dlg->lines-2, drive_dlg->cols-2);
209 attrset (dialog_colors[2]);
210 dlg_move (drive_dlg, 1, drive_dlg->cols/2 - 7);
211 addstr (" Change Drive ");