drive.nt.c 5.4 KB

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