workspace.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Workspace management routines
  2. typedef char FILENAME[8+1+8+1];
  3. enum { WI_FOLD=0, WI_FILE };
  4. typedef struct {
  5. int type; // =WI_FILE
  6. FILENAME name;
  7. int open;
  8. HANDLE openhd;
  9. } WFILE;
  10. #define NUM_DEFLTTYPE 2
  11. typedef struct {
  12. int type; // =WI_FOLD
  13. char title[24];
  14. int deflttype[NUM_DEFLTTYPE];
  15. int expanded;
  16. } WFOLD;
  17. typedef union {
  18. int type;
  19. WFILE f;
  20. WFOLD d;
  21. char pad[32];
  22. } WITEM;
  23. typedef struct {
  24. // General info
  25. char prettyname[32];
  26. char iname[8+1];
  27. char foldname[8+1];
  28. int type;
  29. int loaded;
  30. // Settings
  31. int search_attr;
  32. // Content
  33. int nitems;
  34. WITEM items[0];
  35. } WSP;
  36. enum { WSPT_SIMPLE=0, WSPT_GAME=1, WSPT_UTIL=2, WSPT_OTHER=3 };
  37. #define WSP_MAX_ITEMS 32
  38. WSP *wsp=0;
  39. char temp_wsp[sizeof(WSP)+WSP_MAX_ITEMS*sizeof(WITEM)];
  40. #include "Files.h"
  41. void WspPromptSave();
  42. int WspAddFold(char *fold);
  43. void WspNew() {
  44. WSP this_w,*w=&this_w;
  45. WspPromptSave();
  46. memset(w,0,sizeof(WSP));
  47. wzStart("New project","3");
  48. while (wzTextClr("Project name:",w->prettyname,31))
  49. // while (wzTextClr("Project internal name:",w->iname,8))
  50. while (wzTextClr("Project folder:",w->foldname,8)) {
  51. if (!ValidateSymName(w->foldname)) wzErr("Please enter a valid folder name");
  52. strtolower(w->foldname);
  53. while (wzChoiceDeflt("Project type:",
  54. wzList("Simple program","Game","Utility","Other"),&w->type,WSPT_SIMPLE)) {
  55. wzDone("Your project is now created. "
  56. "The project browser (accessible through 2ND-[VAR-LINK]) will now be opened for you.");
  57. strcpy(w->iname,"project");
  58. w->loaded=0;
  59. *(WSP *)temp_wsp=*w;
  60. wsp=(WSP *)temp_wsp;
  61. WspAddFold("Default");
  62. return;
  63. }
  64. }
  65. }
  66. void WspShowBrowser();
  67. void WspSave() {
  68. WSP *w=wsp;
  69. char tn[20];
  70. strcpy(tn,w->foldname);
  71. strcat(tn,"\\");
  72. strcat(tn,w->iname);
  73. SYM_STR sym=SYMSTR(tn);
  74. EM_moveSymFromExtMem(tn,HS_NULL);
  75. rename(tn,"_wsptemp");
  76. SYM_STR foldss=SYMSTR(w->foldname);
  77. if (!SymFindHome(foldss).folder) FolderAdd(foldss);
  78. FILE *fp=fopen(tn,"wb");
  79. fwrite(w,sizeof(WSP)+w->nitems*sizeof(WITEM),1,fp);
  80. fwrite((char[]){0,'W','S','P',0,OTH_TAG},1,6,fp);
  81. if (ferror(fp)) {
  82. fclose(fp);
  83. unlink(tn);
  84. rename("_wsptemp",tn);
  85. EM_moveSymToExtMem(tn,HS_NULL);
  86. SimpleDlg("Save project","Error : the project could not be saved.",B_CENTER,W_NORMAL|ICO_WARN);
  87. return;
  88. }
  89. fclose(fp);
  90. unlink("_wsptemp");
  91. if (!EM_moveSymToExtMem(tn,HS_NULL))
  92. SimpleDlg("Save project",
  93. "Warning : the project could not be archived. Please go to the [VAR-LINK] "
  94. "window, delete the files you don't need, and archive it.",B_CENTER,W_NORMAL|ICO_WARN);
  95. WspShowBrowser();
  96. }
  97. void WspPromptSave() {
  98. if (wsp) {
  99. if (YesNoDlg("GT-Dev","Save the current project?"))
  100. WspSave();
  101. }
  102. }
  103. int WspFindNearItem(char *wi_name,int wi_type,int i) {
  104. int n=wsp->nitems-i;
  105. WITEM *wip=wsp->items+i;
  106. while (n--) {
  107. if (wip->type<wi_type) return i;
  108. if (wip->type==wi_type && strcmp(wip->f.name,wi_name)>=0) return i;
  109. i++; wip++;
  110. }
  111. return i;
  112. }
  113. int WspFindItem(char *wi_name,int wi_type,int i) {
  114. int j=WspFindNearItem(wi_name,wi_type,i);
  115. if (j<wsp->nitems && wsp->items[j].type==wi_type && !strcmp(wsp->items[j].f.name,wi_name))
  116. return j;
  117. return -1;
  118. }
  119. WITEM *WspAddItem(int i) {
  120. if (wsp->nitems>=WSP_MAX_ITEMS) return NULL;
  121. WITEM *wi=wsp->items+i;
  122. memmove(wi+1,wi,(wsp->nitems-i)*sizeof(WITEM));
  123. wsp->nitems++;
  124. memset(wi,0,sizeof(WITEM));
  125. return wi;
  126. }
  127. int WspAddFile(char *file,char *dfold) {
  128. int i=WspFindItem(dfold,WI_FOLD,0);
  129. if (i<0) return 0;
  130. WFILE *wf=&(WspAddItem(WspFindNearItem(file,WI_FILE,i+1))->f);
  131. if (!wf) return 0;
  132. wf->type=WI_FILE;
  133. strcpy(wf->name,file);
  134. // everything else is set to 0 :)
  135. return 1;
  136. }
  137. int WspAddFold(char *fold) {
  138. // asm("0:bra 0b");
  139. WFOLD *wd=&(WspAddItem(WspFindNearItem(fold,WI_FOLD,0))->d);
  140. if (!wd) return 0;
  141. wd->type=WI_FOLD;
  142. strcpy(wd->title,fold);
  143. // everything else is set to 0 :)
  144. return 1;
  145. }
  146. DEFINE_XPLOOP_CB(callback) {
  147. if (!key) {
  148. char *Menu[5]={"Help","New"DOTS,"Import\xA0","Rename","Setup"};
  149. if (xc->s[xc->sel].i==1) Menu[4]=NULL;
  150. MenuContents=Menu;
  151. return XPLCB_CONT;
  152. } else if (key==KEY_F1) {
  153. return XPLCB_BREAK;
  154. }
  155. return XPLCB_CONT;
  156. }
  157. void WspShowBrowser() {
  158. int h=8;
  159. WIN_RECT win;
  160. XP_C *xc=XpLoadItems(0);
  161. PushScr();
  162. dialog("Project browser",120,h+XP_H*XP_N,B_CENTER|B_ROUNDED,&win);
  163. DStr(win.x0>>2,win.y0,"Project view :");
  164. xc->sel=0;
  165. XP_S *xs;
  166. if ((xs=XpLoop(xc,win.y0+h,callback,NULL))) {
  167. }
  168. PopScr();
  169. free(xc);
  170. MenuContents=NULL;
  171. }