PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mfiler3-4.4.9/gui.c

#
C | 504 lines | 445 code | 41 blank | 18 comment | 121 complexity | 3588190dd8bd2b9871537b1904820bc3 MD5 | raw file
  1. #include "common.h"
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include "config.h"
  5. #if defined(HAVE_CURSES_H)
  6. #include <curses.h>
  7. #elif defined(HAVE_NCURSES_H)
  8. #include <ncurses.h>
  9. #elif defined(HAVE_NCURSES_NCURSES_H)
  10. #include <ncurses/ncurses.h>
  11. #endif
  12. ///////////////////////////////////////////////////
  13. // ??????????
  14. ///////////////////////////////////////////////////
  15. void merr_msg(char* msg, ...)
  16. {
  17. if(gMainLoop == -1) {
  18. char msg2[1024];
  19. va_list args;
  20. va_start(args, msg);
  21. vsnprintf(msg2, 1024, msg, args);
  22. va_end(args);
  23. const int maxy = mgetmaxy();
  24. const int maxx = mgetmaxx();
  25. if(mis_raw_mode()) {
  26. mclear();
  27. view(TRUE);
  28. mclear_online(maxy-2);
  29. mclear_online(maxy-1);
  30. mmvprintw(maxy-2, 0, "%s", msg2);
  31. mrefresh();
  32. int meta;
  33. mgetch(&meta);
  34. }
  35. else {
  36. fprintf(stderr, "%s", msg2);
  37. }
  38. }
  39. else {
  40. char msg2[1024];
  41. va_list args;
  42. va_start(args, msg);
  43. vsnprintf(msg2, 1024, msg, args);
  44. va_end(args);
  45. fprintf(stderr, "%s", msg2);
  46. }
  47. }
  48. ///////////////////////////////////////////////////
  49. // ?????????????????
  50. ///////////////////////////////////////////////////
  51. void msg_nonstop(char* msg, ...)
  52. {
  53. char msg2[1024];
  54. va_list args;
  55. va_start(args, msg);
  56. vsnprintf(msg2, 1024, msg, args);
  57. va_end(args);
  58. const int maxy = mgetmaxy();
  59. const int maxx = mgetmaxx();
  60. mclear();
  61. view(TRUE);
  62. mclear_online(maxy-2);
  63. mclear_online(maxy-1);
  64. mmvprintw(maxy-2, 0, "%s", msg2);
  65. mrefresh();
  66. }
  67. ///////////////////////////////////////////////////
  68. // ??
  69. ///////////////////////////////////////////////////
  70. char* choise(char* msg, char* str[], int len, int cancel)
  71. {
  72. const int maxy = mgetmaxy();
  73. const int maxx = mgetmaxx();
  74. int cursor = 0;
  75. while(1) {
  76. /// view ///
  77. mclear();
  78. view(TRUE);
  79. mclear_online(maxy-2);
  80. mclear_online(maxy-1);
  81. mmove(maxy-2, 0);
  82. mprintw("%s", msg);
  83. mprintw(" ");
  84. int i;
  85. for(i=0; i< len; i++) {
  86. if(cursor == i) {
  87. mattron(kCAReverse);
  88. mprintw("%s", str[i]);
  89. mattroff();
  90. mprintw(" ");
  91. }
  92. else {
  93. mprintw("%s ", str[i]);
  94. }
  95. }
  96. mmove(maxy-1, maxx-2);
  97. mrefresh();
  98. /// input ///
  99. int meta;
  100. int key = mgetch(&meta);
  101. if(key == 10 || key == 13) {
  102. break;
  103. }
  104. else if(key == 6 || key == KEY_RIGHT) {
  105. cursor++;
  106. if(cursor >= len) cursor = len-1;
  107. }
  108. else if(key == 2 || key == KEY_LEFT) {
  109. cursor--;
  110. if(cursor < 0) cursor= 0;
  111. }
  112. else if(key == 12) { // CTRL-L
  113. mclear_immediately();
  114. }
  115. else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape
  116. return NULL;
  117. }
  118. else {
  119. int i;
  120. for(i=0; i< len; i++) {
  121. if(toupper(key) == toupper(str[i][0])) {
  122. cursor = i;
  123. goto finished;
  124. }
  125. }
  126. }
  127. }
  128. finished:
  129. return str[cursor];
  130. }
  131. ///////////////////////////////////////////////////////////////////
  132. // ?????????
  133. ///////////////////////////////////////////////////////////////////
  134. // result 0: ok 1: cancel
  135. static void input_box_cursor_move(string_obj* input, int* cursor, int v)
  136. {
  137. char* str = string_c_str(input);
  138. int utfpos = str_pointer2kanjipos(kUtf8, str, str + *cursor);
  139. utfpos+=v;
  140. *cursor = str_kanjipos2pointer(kUtf8, str, utfpos) - str;
  141. }
  142. char* gInputBoxMsg;
  143. string_obj* gInputBoxInput = NULL;
  144. int gInputBoxCursor;
  145. void input_box_view()
  146. {
  147. int maxx = mgetmaxx();
  148. int maxy = mgetmaxy();
  149. /// view ///
  150. mclear_online(maxy-2);
  151. mclear_online(maxy-1);
  152. mmvprintw(maxy-2, 0, "%s", gInputBoxMsg);
  153. mmove(maxy-1, 0);
  154. const int len = string_length(gInputBoxInput);
  155. int i;
  156. for(i=0; i< len && i<maxx-1; i++) {
  157. mprintw("%c", string_c_str(gInputBoxInput)[i]);
  158. }
  159. //mmove_immediately(maxy -1, gInputBoxCursor);
  160. mmove(maxy -1, gInputBoxCursor);
  161. }
  162. int input_box(char* msg, char* result, int result_size, char* def_input, int def_cursor)
  163. {
  164. gInputBoxMsg = msg;
  165. int result2 = 0;
  166. gInputBoxCursor = def_cursor;
  167. if(gInputBoxInput) string_delete(gInputBoxInput);
  168. gInputBoxInput = STRING_NEW(def_input);
  169. gView = input_box_view;
  170. while(1) {
  171. input_box_view();
  172. mrefresh();
  173. /// input ///
  174. int meta;
  175. int key = mgetch(&meta);
  176. if(key == 10 || key == 13) {
  177. result2 = 0;
  178. break;
  179. }
  180. else if(key == 6 || key == KEY_RIGHT) {
  181. input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 1);
  182. }
  183. else if(key == 2 || key == KEY_LEFT) {
  184. input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -1);
  185. }
  186. else if(key == 8 || key == KEY_BACKSPACE) { // CTRL-H
  187. if(gInputBoxCursor > 0) {
  188. char* str2 = string_c_str(gInputBoxInput);
  189. int utfpos = str_pointer2kanjipos(kUtf8, str2, str2 + gInputBoxCursor);
  190. char* before_point = str_kanjipos2pointer(kUtf8, str2, utfpos-1);
  191. int new_cursor = before_point-str2;
  192. string_erase(gInputBoxInput, before_point - str2, (str2 + gInputBoxCursor) - before_point);
  193. gInputBoxCursor = new_cursor;
  194. }
  195. }
  196. else if(key == 4 || key == KEY_DC) { // CTRL-D DELETE
  197. char* str2 = string_c_str(gInputBoxInput);
  198. if(string_length(gInputBoxInput) > 0) {
  199. if(gInputBoxCursor < string_length(gInputBoxInput)) {
  200. int utfpos = str_pointer2kanjipos(kUtf8, str2, str2 + gInputBoxCursor);
  201. char* next_point = str_kanjipos2pointer(kUtf8, str2, utfpos+1);
  202. string_erase(gInputBoxInput, gInputBoxCursor, next_point - (str2 + gInputBoxCursor));
  203. }
  204. }
  205. }
  206. else if(key == 1 || key == KEY_HOME) { // CTRL-A
  207. input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -999);
  208. }
  209. else if(key == 5 || key == KEY_END) { // CTRL-E
  210. input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 999);
  211. }
  212. else if(key == 11) { // CTRL-K
  213. string_erase(gInputBoxInput, gInputBoxCursor, string_length(gInputBoxInput)-gInputBoxCursor);
  214. }
  215. else if(key == 21) { // CTRL-U
  216. string_put(gInputBoxInput, "");
  217. gInputBoxCursor = 0;
  218. }
  219. else if(key == 23) { // CTRL-W
  220. if(gInputBoxCursor > 0) {
  221. const char* s = string_c_str(gInputBoxInput);
  222. int pos = gInputBoxCursor-1;
  223. if(s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"') {
  224. while(pos>=0 && (s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"'))
  225. {
  226. pos--;
  227. }
  228. }
  229. while(pos>=0 && s[pos]!=' ' && s[pos]!='/' && s[pos]!='\'' && s[pos]!='"')
  230. {
  231. pos--;
  232. }
  233. string_erase(gInputBoxInput, pos+1, gInputBoxCursor-pos-1);
  234. gInputBoxCursor = pos+1;
  235. }
  236. }
  237. else if(meta==1 && key == 'd') { // Meta-d
  238. const char* s = string_c_str(gInputBoxInput);
  239. if(s[gInputBoxCursor] != 0) {
  240. int pos = gInputBoxCursor;
  241. pos++;
  242. while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
  243. pos++;
  244. }
  245. while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
  246. pos++;
  247. }
  248. string_erase(gInputBoxInput, gInputBoxCursor, pos-gInputBoxCursor);
  249. }
  250. }
  251. else if(meta==1 && key == 'b') { // META-b
  252. if(gInputBoxCursor > 0) {
  253. const char* s = string_c_str(gInputBoxInput);
  254. int pos = gInputBoxCursor;
  255. pos--;
  256. while(pos>=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
  257. pos--;
  258. }
  259. while(pos>=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
  260. pos--;
  261. }
  262. gInputBoxCursor = pos+1;
  263. }
  264. }
  265. else if(meta==1 && key == 'f') { // META-f
  266. const char* s = string_c_str(gInputBoxInput);
  267. if(s[gInputBoxCursor] != 0) {
  268. int pos = gInputBoxCursor;
  269. pos++;
  270. while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) {
  271. pos++;
  272. }
  273. while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') {
  274. pos++;
  275. }
  276. gInputBoxCursor = pos;
  277. }
  278. }
  279. else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape
  280. result2 = 1;
  281. break;
  282. }
  283. else if(key == 12) { // CTRL-L
  284. mclear_immediately();
  285. }
  286. else {
  287. if(meta == 0 && !(key >= 0 && key <= 27)) {
  288. char tmp[128];
  289. snprintf(tmp, 128, "%c", key);
  290. string_insert(gInputBoxInput, gInputBoxCursor, tmp);
  291. gInputBoxCursor++;
  292. }
  293. }
  294. }
  295. gView = NULL;
  296. int maxx = mgetmaxx();
  297. int maxy = mgetmaxy();
  298. xstrncpy(result, string_c_str(gInputBoxInput), result_size);
  299. mmove_immediately(maxy -2, 0);
  300. return result2;
  301. }
  302. /////////////////////////////////////////////////////////////////////////
  303. // ?????????????
  304. /////////////////////////////////////////////////////////////////////////
  305. char* gSelectStrMsg;
  306. char** gSelectStrStr;
  307. int gSelectStrCursor;
  308. int gSelectStrLen;
  309. void select_str_view()
  310. {
  311. int maxx = mgetmaxx();
  312. int maxy = mgetmaxy();
  313. /// view ///
  314. mclear_online(maxy-2);
  315. mclear_online(maxy-1);
  316. mmove(maxy-2, 0);
  317. mprintw("%s", gSelectStrMsg);
  318. // mmove(maxy-1, 0);
  319. mprintw(" ");
  320. int i;
  321. for(i=0; i< gSelectStrLen; i++) {
  322. if(gSelectStrCursor == i) {
  323. mattron(kCAReverse);
  324. mprintw("%s", gSelectStrStr[i]);
  325. mattroff();
  326. mprintw(" ");
  327. }
  328. else {
  329. mprintw("%s ", gSelectStrStr[i]);
  330. }
  331. }
  332. mmove(maxy-1, maxx-2);
  333. }
  334. int select_str(char* msg, char* str[], int len, int cancel)
  335. {
  336. gSelectStrMsg = msg;
  337. gSelectStrStr = str;
  338. gSelectStrLen = len;
  339. gSelectStrCursor = 0;
  340. gView = select_str_view;
  341. while(1) {
  342. filer_view(0);
  343. filer_view(1);
  344. select_str_view();
  345. mrefresh();
  346. /// input ///
  347. int meta;
  348. int key = mgetch(&meta);
  349. if(key == 10 || key == 13) {
  350. break;
  351. }
  352. else if(key == 6 || key == KEY_RIGHT) {
  353. gSelectStrCursor++;
  354. if(gSelectStrCursor >= len) gSelectStrCursor = len-1;
  355. }
  356. else if(key == 2 || key == KEY_LEFT) {
  357. gSelectStrCursor--;
  358. if(gSelectStrCursor < 0) gSelectStrCursor= 0;
  359. }
  360. else if(key == 12) { // CTRL-L
  361. mclear_immediately();
  362. }
  363. else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape
  364. gSelectStrCursor = cancel;
  365. break;
  366. }
  367. else {
  368. int i;
  369. for(i=0; i< len; i++) {
  370. if(toupper(key) == toupper(str[i][0])) {
  371. gSelectStrCursor = i;
  372. goto finished;
  373. }
  374. }
  375. }
  376. }
  377. finished:
  378. gView = NULL;
  379. return gSelectStrCursor;
  380. }
  381. int select_str2(char* msg, char* str[], int len, int cancel)
  382. {
  383. gSelectStrMsg = msg;
  384. gSelectStrStr = str;
  385. gSelectStrLen = len;
  386. gSelectStrCursor = 0;
  387. gView = select_str_view;
  388. while(1) {
  389. select_str_view();
  390. mrefresh();
  391. /// input ///
  392. int meta;
  393. int key = mgetch(&meta);
  394. if(key == 10 || key == 13) {
  395. break;
  396. }
  397. else if(key == 6 || key == KEY_RIGHT) {
  398. gSelectStrCursor++;
  399. if(gSelectStrCursor >= len) gSelectStrCursor = len-1;
  400. }
  401. else if(key == 2 || key == KEY_LEFT) {
  402. gSelectStrCursor--;
  403. if(gSelectStrCursor < 0) gSelectStrCursor= 0;
  404. }
  405. else if(key == 12) { // CTRL-L
  406. mclear_immediately();
  407. }
  408. else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape
  409. gSelectStrCursor = cancel;
  410. break;
  411. }
  412. else {
  413. int i;
  414. for(i=0; i< len; i++) {
  415. if(toupper(key) == toupper(str[i][0])) {
  416. gSelectStrCursor = i;
  417. goto finished;
  418. }
  419. }
  420. }
  421. }
  422. finished:
  423. gView = NULL;
  424. return gSelectStrCursor;
  425. }