drive.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Ch-Drive command for Win32
  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. #include <string.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include "../src/global.h"
  23. #include "../src/tty.h"
  24. #include "../src/util.h"
  25. #include "../src/win.h"
  26. #include "../src/color.h"
  27. #include "../src/dlg.h"
  28. #include "../src/widget.h"
  29. #include "../src/dialog.h"
  30. #include "../src/dir.h"
  31. #include "../src/panel.h"
  32. #include "../src/main.h"
  33. #include "../src/cmd.h"
  34. #include "util_win32.h"
  35. struct Dlg_head *drive_dlg;
  36. WPanel *this_panel;
  37. static int drive_dlg_callback (Dlg_head *h, int Par, int Msg);
  38. static void drive_dlg_refresh (void);
  39. static void drive_cmd(void);
  40. #define B_DRIVE_BASE 100
  41. #define MAX_LGH 13 /* Length for drives */
  42. static void drive_cmd()
  43. {
  44. int i, nNewDrive, nDrivesAvail;
  45. char szTempBuf[7], szDrivesAvail[27*4], *p;
  46. /* Dialogbox position */
  47. int x_pos;
  48. int y_pos = (LINES-6)/2-3;
  49. int y_height;
  50. int x_width;
  51. int m_drv;
  52. /* Get drives name and count */
  53. #ifdef NATIVE_WIN32
  54. GetLogicalDriveStrings (255, szDrivesAvail);
  55. for (nDrivesAvail = 0, p = szDrivesAvail; *p; nDrivesAvail++)
  56. p+=4;
  57. #else
  58. unsigned long uDriveNum, uDriveMap;
  59. nDrivesAvail = 0;
  60. p = szDrivesAvail;
  61. DosQueryCurrentDisk(&uDriveNum, &uDriveMap);
  62. for (i = 0; i < 26; i++) {
  63. if ( uDriveMap & (1 << i) ) {
  64. *p = 'A' + i;
  65. p += 4;
  66. nDrivesAvail++;
  67. }
  68. }
  69. *p = 0;
  70. #endif
  71. /* Create Dialog */
  72. do_refresh ();
  73. m_drv = ((nDrivesAvail > MAX_LGH) ? MAX_LGH: nDrivesAvail);
  74. /* Center on x, relative to panel */
  75. x_pos = this_panel->widget.x + (this_panel->widget.cols - m_drv*3)/2 + 2;
  76. if (nDrivesAvail > MAX_LGH) {
  77. y_height = 8;
  78. x_width = 33;
  79. } else {
  80. y_height = 6;
  81. x_width = (nDrivesAvail - 1) * 2 + 9;
  82. }
  83. drive_dlg = create_dlg (y_pos, x_pos, y_height, x_width, dialog_colors,
  84. drive_dlg_callback, "[ChDrive]", "Change Drive", DLG_NONE);
  85. if (nDrivesAvail>MAX_LGH) {
  86. for (i = 0; i < nDrivesAvail - MAX_LGH; i++) {
  87. p -= 4;
  88. sprintf(szTempBuf, "&%c", *p);
  89. add_widget (drive_dlg,
  90. button_new (5,
  91. (m_drv-i-1)*2+4 - (MAX_LGH*2 - nDrivesAvail) * 2,
  92. B_DRIVE_BASE + nDrivesAvail - i - 1,
  93. HIDDEN_BUTTON,
  94. szTempBuf, 0, NULL, NULL));
  95. }
  96. }
  97. /* Add a button for each drive */
  98. for (i = 0; i < m_drv; i++) {
  99. p -= 4;
  100. sprintf (szTempBuf, "&%c", *p);
  101. add_widget (drive_dlg,
  102. button_new (3, (m_drv-i-1)*2+4, B_DRIVE_BASE+m_drv-i-1,
  103. HIDDEN_BUTTON, szTempBuf, 0, NULL, NULL));
  104. }
  105. run_dlg(drive_dlg);
  106. /* do action */
  107. if (drive_dlg->ret_value != B_CANCEL) {
  108. int errocc = 0; /* no error */
  109. int rtn;
  110. char drvLetter;
  111. nNewDrive = drive_dlg->ret_value - B_DRIVE_BASE;
  112. drvLetter = (char) *(szDrivesAvail + (nNewDrive*4));
  113. #ifdef NATIVE_WIN32
  114. if (win32_GetPlatform() == OS_WinNT) { /* Windows NT */
  115. rtn = _chdrive(drvLetter - 'A' + 1);
  116. } else { /* Windows 95 */
  117. rtn = 1;
  118. SetCurrentDirectory(szDrivesAvail+(nNewDrive*4));
  119. }
  120. #else
  121. rtn = DosSetDefaultDisk(nNewDrive + 1);
  122. #endif
  123. if (rtn == -1)
  124. errocc = 1;
  125. else {
  126. getcwd (this_panel->cwd, sizeof (this_panel->cwd)-2);
  127. if (toupper(drvLetter) == toupper(*(this_panel->cwd))) {
  128. clean_dir (&this_panel->dir, this_panel->count);
  129. this_panel->count = do_load_dir(&this_panel->dir,
  130. this_panel->sort_type,
  131. this_panel->reverse,
  132. this_panel->case_sensitive,
  133. this_panel->filter);
  134. this_panel->top_file = 0;
  135. this_panel->selected = 0;
  136. this_panel->marked = 0;
  137. this_panel->total = 0;
  138. show_dir(this_panel);
  139. reread_cmd();
  140. } else
  141. errocc = 1;
  142. }
  143. if (errocc)
  144. message (1, "Error", " Cannot access drive %c: ",
  145. *(szDrivesAvail+(nNewDrive*4)) );
  146. }
  147. destroy_dlg (drive_dlg);
  148. repaint_screen ();
  149. }
  150. void drive_cmd_a()
  151. {
  152. this_panel = left_panel;
  153. drive_cmd();
  154. }
  155. void drive_cmd_b()
  156. {
  157. this_panel = right_panel;
  158. drive_cmd();
  159. }
  160. void drive_chg(WPanel *panel)
  161. {
  162. this_panel = panel;
  163. drive_cmd();
  164. }
  165. static int drive_dlg_callback (Dlg_head *h, int Par, int Msg)
  166. {
  167. switch (Msg) {
  168. case DLG_DRAW:
  169. drive_dlg_refresh ();
  170. break;
  171. }
  172. return 0;
  173. }
  174. static void drive_dlg_refresh (void)
  175. {
  176. attrset (dialog_colors[0]);
  177. dlg_erase (drive_dlg);
  178. draw_box (drive_dlg, 1, 1, drive_dlg->lines-2, drive_dlg->cols-2);
  179. attrset (dialog_colors[2]);
  180. dlg_move (drive_dlg, 1, drive_dlg->cols/2 - 7);
  181. addstr (" Change Drive ");
  182. }