drive.c 5.2 KB

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