dialogs.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #if 0
  2. void SearchDlg(int replace,int key) {
  3. int r;
  4. if ((r=DialogProcess(key)))
  5. switch (r) {
  6. case DLG_OK:
  7. ResetSearch();
  8. if (!DoSearch(replace))
  9. return 0;
  10. break;
  11. }
  12. return 1;
  13. }
  14. #endif
  15. #include "aXplore.h"
  16. void boxProcess(EDIT_LINE *e,int key) {
  17. if (key==KEY_BACKSPACE) delEditLine(e,1);
  18. else if (key>=14 && key<=255) { if (insEditLine(e,1)) getEditLinePos(e)[-1]=(char)key; }
  19. else if (key==KEY_LEFT) ScrollUp(&e->scr);
  20. else if (key==KEY_RIGHT) ScrollDn(&e->scr);
  21. }
  22. int checkProcess(int v,int a,int key) {
  23. if (key==KEY_LEFT || key==KEY_RIGHT)
  24. return v^a;
  25. return v;
  26. }
  27. /* Search dialog box :
  28. Text to find [...]
  29. Replace with [...]
  30. [*] Whole word only
  31. [*] Case-sensitive matching/Match case
  32. [*] Search all files
  33. [*] Create search results form
  34. [*] Regular expression
  35. */
  36. #define SDLG_BUF_SIZE 128
  37. #define SDLG_LEFT_MARGIN 14
  38. #define SDLG_NUM_OPTS 3
  39. void boxDisp(EDIT_LINE *e,
  40. char *s,int x,int y,int sel,int blink,char *buf,char *inittext,int x0,int x1,int init) {
  41. if (init) {
  42. newEditLine(e,buf,SDLG_BUF_SIZE,x0,x1-1,y+1);
  43. insEditLine(e,strlen(inittext));
  44. strcpy(buf,inittext);
  45. }
  46. VariableDStr(x,y+1,s);
  47. updEditLine(e);
  48. drawEditLine(e,sel,blink);
  49. }
  50. enum { SDLG_WW=0x1, SDLG_CS=0x2, SDLG_SA=0x4, SDLG_ALL=0x40, SDLG_REPL=0x80, };
  51. char text_find[SDLG_BUF_SIZE],text_repl[SDLG_BUF_SIZE];
  52. int search_attr=0;
  53. int search_coord=0;
  54. int SearchDlg(int rep) {
  55. WIN_RECT win;
  56. int key;
  57. int n_it=1+rep+SDLG_NUM_OPTS,sel_it=0,blink=0;
  58. int a=search_attr,s;
  59. char buf_find[SDLG_BUF_SIZE],buf_repl[SDLG_BUF_SIZE];
  60. EDIT_LINE box_find,box_repl;
  61. int init=1,x,y;
  62. PushScr();
  63. while (1) {
  64. dialog((rep?"Replace text":"Find text"),120,8+(rep?8:0)+6*SDLG_NUM_OPTS,B_MOVEAROUND|B_ROUNDED,&win);
  65. x=physical_to_virtual(win.x0); y=win.y0;
  66. boxDisp(&box_find,"Text to find:",x,y,sel_it-0,blink,
  67. buf_find,text_find,x+SDLG_LEFT_MARGIN,win.x1>>2,init), y+=8;
  68. if (rep)
  69. boxDisp(&box_repl,"Replace with:",x,y,sel_it-1,blink,
  70. buf_repl,text_repl,x+SDLG_LEFT_MARGIN,win.x1>>2,init), y+=8;
  71. s=sel_it-rep-1;
  72. drawCheckBox("Whole word only", a,SDLG_WW,x,y,s-0), y+=6;
  73. drawCheckBox("Case-sensitive", a,SDLG_CS,x,y,s-1), y+=6;
  74. drawCheckBox("Search all files",a,SDLG_SA,x,y,s-2), y+=6;
  75. init=0;
  76. key=WaitLoop(&blink);
  77. if (!key) continue;
  78. blink=0;
  79. if (key==KEY_UP && sel_it) sel_it--;
  80. else if (key==KEY_DOWN && sel_it<n_it-1) sel_it++;
  81. if (sel_it<=rep)
  82. boxProcess(sel_it?&box_repl:&box_find,key);
  83. else
  84. a=checkProcess(a,1<<(sel_it-rep-1),key);
  85. if (key==KEY_ENTER) {
  86. strcpy(text_find,buf_find);
  87. if (rep) strcpy(text_repl,buf_repl);
  88. search_attr=(a&~(SDLG_REPL|SDLG_ALL))|(rep?SDLG_REPL:0);
  89. return 1;
  90. } else if (key==KEY_ESC) return 0;
  91. }
  92. PopScr();
  93. }
  94. void InitSearch() {
  95. text_find[0]=0;
  96. text_repl[0]=0;
  97. search_attr=0;
  98. }
  99. #define is_c_idch(c) ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || c=='_' || c=='$')
  100. int DoSearch() {
  101. int a=search_attr,f=0;
  102. if (*text_find) {
  103. char *p=tptr+curpos,*q=text_find;
  104. char v=*q++,v2=(!(a&SDLG_CS) && isalpha(v)?v^0x20:v),c,d;
  105. int n,need_nonidch=(a&SDLG_WW?is_c_idch(v):0);
  106. while (*p) {
  107. if ((*p==v && (p++,1)) || *p++==v2) {
  108. char *s=p;
  109. if (need_nonidch && is_c_idch(p[-2])) continue;
  110. if (a&SDLG_CS) {
  111. while ((c=*q++))
  112. if (*p++!=c) goto not_here;
  113. } else {
  114. while ((c=*q++))
  115. if ((d=*p++)!=c) {
  116. if ((d^=c)!=0x20) goto not_here;
  117. c|=d;
  118. if (!(c>='a' && c<='z')) goto not_here;
  119. }
  120. }
  121. if ((a&SDLG_WW) && is_c_idch(p[-1]) && is_c_idch(*p)) goto not_here;
  122. // we have a match :)
  123. f++;
  124. sel1=cpos=(s-tptr)-1;
  125. sel2=curpos=selpos=cpos+strlen(text_find);
  126. if (a&SDLG_REPL) {
  127. sel_del(); // this is legal because we updated sel1 & sel2
  128. insert(n=strlen(text_repl));
  129. selpos=cpos;
  130. cpos=selpos-n;
  131. memcpy(tptr+cpos,text_repl,n);
  132. }
  133. if (!(a&SDLG_ALL)) return 1;
  134. not_here:
  135. p=s;
  136. q=text_find+1;
  137. }
  138. }
  139. }
  140. if (f) return -1;
  141. PushScr();
  142. SimpleDlg("Find text","\nText not found!\n",B_CENTER,W_NORMAL);
  143. PopScr();
  144. return 0;
  145. }
  146. char wzTitle[40];
  147. char *wzStepPtr=0;
  148. #define wzStep (*wzStepPtr)
  149. int wzSuccess=0;
  150. int wzText(char *prompt,char *dest,int n) {
  151. WIN_RECT win;
  152. int key;
  153. int blink=0;
  154. EDIT_LINE box;
  155. int init=1,x,y;
  156. char buf[n+1];
  157. wzStep++;
  158. PushScr();
  159. while (1) {
  160. dialog(wzTitle,120,12,B_CENTER|B_ROUNDED,&win);
  161. x=win.x0>>2; y=win.y0+2;
  162. boxDisp(&box,prompt,x,y,0,blink,
  163. buf,dest,x+strlen(prompt)+1,win.x1>>2,init), y+=8;
  164. init=0;
  165. key=WaitLoop(&blink);
  166. if (!key) continue;
  167. blink=0;
  168. boxProcess(&box,key);
  169. if (key==KEY_ENTER) {
  170. strcpy(dest,buf);
  171. PopScr();
  172. return 1;
  173. } else if (key==KEY_ESC) {
  174. wzStep-=2;
  175. PopScr();
  176. return 0;
  177. }
  178. }
  179. }
  180. int wzTextClr(char *prompt,char *dest,int n) {
  181. dest[0]=0;
  182. return wzText(prompt,dest,n);
  183. }
  184. int wzChoice(char *prompt,char **choice,int choicen,int *dest) {
  185. wzStep++;
  186. XP_C *xc=XpLoadList(choice,choicen);
  187. int h=8;
  188. WIN_RECT win;
  189. PushScr();
  190. dialog(wzTitle,120,h+XP_H*XP_N,B_CENTER|B_ROUNDED,&win);
  191. VariableDStr(physical_to_virtual(win.x0),win.y0,prompt);
  192. xc->sel=*dest;
  193. if (!XpLoop(xc,win.y0+h,NULL,NULL)) {
  194. PopScr();
  195. wzStep-=2;
  196. free(xc);
  197. return 0;
  198. } else {
  199. PopScr();
  200. *dest=xc->sel;
  201. free(xc);
  202. return 1;
  203. }
  204. }
  205. int wzChoiceDeflt(char *prompt,char **choice,int choicen,int *dest,int v) {
  206. *dest=v;
  207. return wzChoice(prompt,choice,choicen,dest);
  208. }
  209. void wzStart(char *title,char *numSteps) {
  210. strcpy(wzTitle,title);
  211. strcat(wzTitle," - Step ");
  212. wzStepPtr=wzTitle+strlen(wzTitle);
  213. strcat(wzTitle,"x of ");
  214. strcat(wzTitle,numSteps);
  215. wzSuccess=0;
  216. wzStep='0';
  217. }
  218. void wzDone(char *text) {
  219. wzSuccess=1;
  220. wzTitle[strlen(wzTitle)-sizeof("- Step x of y")]='\0';
  221. // asm("0:bra 0b");
  222. PushScr();
  223. SimpleDlg(wzTitle,text,B_CENTER,W_NORMAL|ICO_INFO);
  224. PopScr();
  225. }
  226. #define wzList(a...) ((char *[]){a}),sizeof((char *[]){a})/sizeof(char *)
  227. #define wzErr(m) ({SimpleDlg(wzTitle,m,B_CENTER,W_NORMAL|ICO_ERROR);continue;})
  228. #if 0
  229. typedef struct {
  230. int t,id,x,y;
  231. union {
  232. TEXT txt;
  233. SCROLL sc;
  234. char *s;
  235. } d;
  236. } DLG_ITEM;
  237. typedef struct {
  238. SCROLL sel;
  239. char *title;
  240. int w,h,a; // width, height, attr
  241. DLG_ITEM i[];
  242. } DLG;
  243. int DialogProcess(int key) {
  244. DLG *d=cur_dlg;
  245. DLG_ITEM *i;
  246. int s;
  247. if (key==KEY_UP)
  248. ScrollUp(&d->sel);
  249. else if (key==KEY_DOWN)
  250. ScrollDn(&d->sel);
  251. else if (key==KEY_ENTER)
  252. return DLG_OK;
  253. else if (key==KEY_ESC)
  254. return DLG_ESC;
  255. ScrollUpdNoWrap(&d->sel);
  256. s=d->sel.sel;
  257. i=&d->i[s];
  258. if (i->t<=0)
  259. return DialogProcess(key);
  260. switch (i->t) {
  261. case DT_TBOX:
  262. cur_text=i->d.txt;
  263. return KeyProcess(key,TM_DIALOG);
  264. case DT_MLTBOX:
  265. cur_text=i->d.txt;
  266. return KeyProcess(key,TM_DIALOG|TM_MULTILINE);
  267. case DT_CBOX:
  268. case DT_DROPDOWN:
  269. if (key==KEY_LEFT)
  270. ScrollUp(&i->d.sc);
  271. else if (key==KEY_RIGHT)
  272. ScrollDn(&i->d.sc);
  273. ScrollUpd(&i->d.sc);
  274. break;
  275. case DT_TEXT:
  276. break;
  277. }
  278. return 0;
  279. }
  280. void DialogDisp() {
  281. DLG *d=cur_dlg;
  282. int n=cur_dlg->sel.n;
  283. DLG_ITEM *i=d->i;
  284. dialog(d->title,d->w,d->h,d->a);
  285. while (n--) {
  286. switch (i->t) {
  287. case DT_TEXT:
  288. VariableDStr(i->x,i->y,i->d.s);
  289. break;
  290. }
  291. i++;
  292. }
  293. }
  294. enum {
  295. #define DL_SKIP(x) -(x)
  296. DL_END=0,
  297. };
  298. int DialogLoadSub(int *p,DLG_ITEM *i,int x0) {
  299. int n=0,x=x0,y=0,z;
  300. while (*p) {
  301. switch ((z=*p++)) {
  302. case DL_TEXT:
  303. if (i) {
  304. i->t=DT_TEXT;
  305. i->id=*p++;
  306. i->x=x; i->y=y;
  307. i->d.s=p;
  308. while (*p++);
  309. } else { p++; while (*p++); }
  310. n++;
  311. y+=char_height+1;
  312. break;
  313. default: // DL_SKIP(-z)
  314. p-=z;
  315. break;
  316. }
  317. }
  318. }
  319. void DialogLoad(int *p) {
  320. }
  321. #endif