help link marks escaped in (I hope) all the document.
[midnight-commander.git] / pc / drive.c
blobc4aeab5e79cc0a6110ea6c1468d6545a265ee59f
1 /* Ch-Drive command for Win32
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/util.h"
30 #include "../src/win.h"
31 #include "../src/color.h"
32 #include "../src/dlg.h"
33 #include "../src/widget.h"
34 #include "../src/dialog.h"
35 #include "../src/dir.h"
36 #include "../src/panel.h"
37 #include "../src/main.h"
38 #include "../src/cmd.h"
39 #include "util_win32.h"
41 struct Dlg_head *drive_dlg;
42 WPanel *this_panel;
44 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg);
45 static void drive_dlg_refresh (void);
46 static void drive_cmd(void);
48 #define B_DRIVE_BASE 100
49 #define MAX_LGH 13 /* Length for drives */
51 static void drive_cmd()
53 int i, nNewDrive, nDrivesAvail;
54 char szTempBuf[7], szDrivesAvail[27*4], *p;
56 /* Dialogbox position */
57 int x_pos;
58 int y_pos = (LINES-6)/2-3;
59 int y_height;
60 int x_width;
62 int m_drv;
64 /* Get drives name and count */
65 #ifdef NATIVE_WIN32
66 GetLogicalDriveStrings (255, szDrivesAvail);
67 for (nDrivesAvail = 0, p = szDrivesAvail; *p; nDrivesAvail++)
68 p+=4;
69 #else
70 unsigned long uDriveNum, uDriveMap;
71 nDrivesAvail = 0;
72 p = szDrivesAvail;
73 DosQueryCurrentDisk(&uDriveNum, &uDriveMap);
74 for (i = 0; i < 26; i++) {
75 if ( uDriveMap & (1 << i) ) {
76 *p = 'A' + i;
77 p += 4;
78 nDrivesAvail++;
81 *p = 0;
82 #endif
84 /* Create Dialog */
85 do_refresh ();
87 m_drv = ((nDrivesAvail > MAX_LGH) ? MAX_LGH: nDrivesAvail);
88 /* Center on x, relative to panel */
89 x_pos = this_panel->widget.x + (this_panel->widget.cols - m_drv*3)/2 + 2;
91 if (nDrivesAvail > MAX_LGH) {
92 y_height = 8;
93 x_width = 33;
94 } else {
95 y_height = 6;
96 x_width = (nDrivesAvail - 1) * 2 + 9;
99 drive_dlg = create_dlg (y_pos, x_pos, y_height, x_width, dialog_colors,
100 drive_dlg_callback, "[ChDrive]", "Change Drive", DLG_NONE);
102 if (nDrivesAvail>MAX_LGH) {
103 for (i = 0; i < nDrivesAvail - MAX_LGH; i++) {
104 p -= 4;
105 sprintf(szTempBuf, "&%c", *p);
106 add_widget (drive_dlg,
107 button_new (5,
108 (m_drv-i-1)*2+4 - (MAX_LGH*2 - nDrivesAvail) * 2,
109 B_DRIVE_BASE + nDrivesAvail - i - 1,
110 HIDDEN_BUTTON,
111 szTempBuf, 0, NULL, NULL));
115 /* Add a button for each drive */
116 for (i = 0; i < m_drv; i++) {
117 p -= 4;
118 sprintf (szTempBuf, "&%c", *p);
119 add_widget (drive_dlg,
120 button_new (3, (m_drv-i-1)*2+4, B_DRIVE_BASE+m_drv-i-1,
121 HIDDEN_BUTTON, szTempBuf, 0, NULL, NULL));
124 run_dlg(drive_dlg);
126 /* do action */
127 if (drive_dlg->ret_value != B_CANCEL) {
128 int errocc = 0; /* no error */
129 int rtn;
130 char drvLetter;
132 nNewDrive = drive_dlg->ret_value - B_DRIVE_BASE;
133 drvLetter = (char) *(szDrivesAvail + (nNewDrive*4));
134 #ifdef NATIVE_WIN32
135 if (win32_GetPlatform() == OS_WinNT) { /* Windows NT */
136 rtn = _chdrive(drvLetter - 'A' + 1);
137 } else { /* Windows 95 */
138 rtn = 1;
139 SetCurrentDirectory(szDrivesAvail+(nNewDrive*4));
141 #else
142 rtn = DosSetDefaultDisk(nNewDrive + 1);
143 #endif
144 if (rtn == -1)
145 errocc = 1;
146 else {
147 getcwd (this_panel->cwd, sizeof (this_panel->cwd)-2);
148 if (toupper(drvLetter) == toupper(*(this_panel->cwd))) {
149 clean_dir (&this_panel->dir, this_panel->count);
150 this_panel->count = do_load_dir(&this_panel->dir,
151 this_panel->sort_type,
152 this_panel->reverse,
153 this_panel->case_sensitive,
154 this_panel->filter);
155 this_panel->top_file = 0;
156 this_panel->selected = 0;
157 this_panel->marked = 0;
158 this_panel->total = 0;
159 show_dir(this_panel);
160 reread_cmd();
161 } else
162 errocc = 1;
164 if (errocc)
165 message (1, "Error", " Cannot access drive %c: ",
166 *(szDrivesAvail+(nNewDrive*4)) );
168 destroy_dlg (drive_dlg);
169 repaint_screen ();
172 void drive_cmd_a()
174 this_panel = left_panel;
175 drive_cmd();
178 void drive_cmd_b()
180 this_panel = right_panel;
181 drive_cmd();
184 void drive_chg(WPanel *panel)
186 this_panel = panel;
187 drive_cmd();
190 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg)
192 switch (Msg) {
193 case DLG_DRAW:
194 drive_dlg_refresh ();
195 break;
197 return 0;
200 static void drive_dlg_refresh (void)
202 attrset (dialog_colors[0]);
203 dlg_erase (drive_dlg);
204 draw_box (drive_dlg, 1, 1, drive_dlg->lines-2, drive_dlg->cols-2);
206 attrset (dialog_colors[2]);
207 dlg_move (drive_dlg, 1, drive_dlg->cols/2 - 7);
208 addstr (" Change Drive ");