update
[midnight-commander.git] / os2 / drive.os2.c
blobfabc900f367d91d757da6ef860baac752920ecce
1 /* Ch-Drive command for OS/2 operating system
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <config.h>
20 #ifndef __os2__
21 #error This file is for the OS/2 operating systems.
22 #else
24 #define INCL_DOSDEVICES /* Device values */
25 #define INCL_DOSERRORS /* Error values */
26 #include <os2.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "tty.h"
31 #include "mad.h"
32 #include "util.h"
33 #include "win.h"
34 #include "color.h"
35 #include "dlg.h"
36 #include "widget.h"
37 #include "dialog.h" /* For do_refresh() */
39 #include "dir.h"
40 #include "panel.h" /* Needed for the externs */
41 #include "file.h"
42 #include "main.h"
43 #include "drive.h"
45 struct Dlg_head *drive_dlg;
46 WPanel *this_panel;
48 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg);
49 static void drive_dlg_refresh (void);
50 static void drive_cmd();
52 #define B_DRIVE_BASE 100
54 void drive_cmd_a(WPanel *p)
56 this_panel = left_panel;
57 /* .ado: Drive change can only be done within view_listing */
58 if (p == this_panel) {
59 if (get_current_type() != view_listing) {
60 return;
62 } else {
63 if (get_other_type() != view_listing) {
64 return;
67 drive_cmd();
71 void drive_cmd_b(WPanel *p)
73 this_panel = right_panel;
74 /* .ado: Drive change can only be done within view_listing */
75 if (p == this_panel) {
76 if (get_current_type() != view_listing) {
77 return;
79 } else {
80 if (get_other_type() != view_listing) {
81 return;
84 drive_cmd();
88 void drive_chg(WPanel *panel)
90 this_panel = panel;
91 drive_cmd();
94 #define MAX_LGH 13
96 static void drive_cmd()
98 register int i;
99 register int j;
100 int nDrivesAvail = 0; /* Available drives */
101 int nNewDrive; /* New drive */
102 char szTempBuf[7];
103 ULONG uDriveNum = 0; /* Drive number (A=1, B=2 ...) */
104 ULONG uDriveMap = 0; /* Mapping */
105 char sDrvs[26]; /* Char-Mapping */
106 char curDisk;
108 // Dialogbox position
109 int x_pos; /* X-Position for the dialog */
110 int y_pos = (LINES-6)/2-3; /* Center on y */
111 int y_height;
112 int x_width;
114 int m_drv;
115 void *widget;
117 APIRET rc = NO_ERROR; /* Return code */
119 memset(sDrvs, (char) 0, 26);
120 rc = DosQueryCurrentDisk(&uDriveNum, &uDriveMap);
122 if (rc != NO_ERROR) {
123 message(0, " ERROR ", " Internal Error with drive_cmd ");
124 return;
127 j = 0;
128 for (i=0; i < 26; i++) {
129 if ( (uDriveMap<<(31-i)) >> 31) {
130 sDrvs[j++] = (char) ((int) 'A' + i);
131 nDrivesAvail++;
135 /* Create Dialog */
136 do_refresh ();
138 m_drv = ((nDrivesAvail > MAX_LGH) ? MAX_LGH: nDrivesAvail);
139 x_pos = this_panel->widget.x + (this_panel->widget.cols - m_drv*3)/2 + 2; /* Center on x, relative to panel */
141 if (nDrivesAvail > MAX_LGH) {
142 y_height = 8;
143 x_width = 33;
144 } else {
145 y_height = 6;
146 x_width = (nDrivesAvail - 1) * 2 + 9;
149 drive_dlg = create_dlg (y_pos, x_pos,
150 y_height,
151 x_width,
152 dialog_colors,
153 drive_dlg_callback,
154 "[ChDrive]",
155 "drive",
156 DLG_NONE);
158 x_set_dialog_title (drive_dlg, "Change Drive");
160 if (nDrivesAvail>MAX_LGH) {
161 for (i = 0; i < nDrivesAvail - MAX_LGH; i++) {
162 sprintf(szTempBuf, "%c", sDrvs[nDrivesAvail-i-1]);
163 add_widgetl (drive_dlg,
164 button_new (5, (m_drv-i-1)*2+4 - (MAX_LGH*2 - nDrivesAvail) * 2,
165 B_DRIVE_BASE+((int) szTempBuf[0] - (int) 'A'),
166 HIDDEN_BUTTON,
167 szTempBuf,
168 0, NULL, NULL),
169 XV_WLAY_RIGHTOF);
172 /* Add a button for each drive */
173 for (i = 0; i < m_drv; i++) {
174 sprintf (szTempBuf, "%c", sDrvs[m_drv-i-1]);
175 add_widgetl(drive_dlg,
176 button_new (3,
177 (m_drv-i-1)*2+4,
178 B_DRIVE_BASE+((int)szTempBuf[0] - (int) 'A'),
179 HIDDEN_BUTTON,
180 szTempBuf,
182 NULL,
183 NULL),
184 XV_WLAY_RIGHTOF);
187 run_dlg(drive_dlg);
189 /* do action */
190 if (drive_dlg->ret_value != B_CANCEL) {
191 nNewDrive = drive_dlg->ret_value - B_DRIVE_BASE;
192 curDisk = (char) (nNewDrive + (int) 'A');
193 rc = DosSetDefaultDisk(nNewDrive + 1);
194 if (rc != NO_ERROR) {
195 message(1, " Error ", " Invalid Drive %c: \n", curDisk);
196 } else {
197 getcwd(this_panel->cwd, sizeof(this_panel->cwd) - 2);
198 // if getcwd returns NULL --> error!
199 // rc = DosQueryCurrentDisk(&uDriveNum, &uDriveMap);
200 if (toupper(curDisk) == toupper(*(this_panel->cwd))) {
201 clean_dir (&this_panel->dir, this_panel->count);
202 this_panel->count = do_load_dir (&this_panel->dir,
203 this_panel->sort_type,
204 this_panel->reverse,
205 this_panel->case_sensitive,
206 this_panel->filter);
207 this_panel->top_file = 0;
208 this_panel->selected = 0;
209 this_panel->marked = 0;
210 this_panel->total = 0;
211 show_dir(this_panel);
212 reread_cmd();
214 // rc = DosSetDefaultDisk(uDriveNum);
217 destroy_dlg (drive_dlg);
218 repaint_screen ();
221 static int drive_dlg_callback (Dlg_head *h, int Par, int Msg)
223 char buffer [10];
225 switch (Msg) {
226 #ifndef HAVE_X
227 case DLG_DRAW:
228 drive_dlg_refresh ();
229 break;
230 #endif
232 return 0;
235 static void drive_dlg_refresh (void)
237 attrset (dialog_colors[0]);
238 dlg_erase (drive_dlg);
239 draw_box (drive_dlg, 1, 1, drive_dlg->lines-2, drive_dlg->cols-2);
241 attrset (dialog_colors[2]);
242 dlg_move (drive_dlg, 1, drive_dlg->cols/2 - 7);
243 addstr (" Change Drive ");
246 #endif