main_r.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * main_r.c
  3. *
  4. * Released under GPL
  5. *
  6. * Copyright (C) 1998-2003 A.J. van Os
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * Description:
  23. * The main program of !Antiword (RISC OS version)
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "baricon.h"
  29. #include "dbox.h"
  30. #include "event.h"
  31. #include "flex.h"
  32. #include "kernel.h"
  33. #include "menu.h"
  34. #include "res.h"
  35. #include "resspr.h"
  36. #include "wimp.h"
  37. #include "template.h"
  38. #include "wimpt.h"
  39. #include "win.h"
  40. #include "xferrecv.h"
  41. #include "version.h"
  42. #include "antiword.h"
  43. /* The name of this program */
  44. static char *szTask = "!Antiword";
  45. /* The window handle of the choices window */
  46. static wimp_w tChoicesWindow;
  47. /* Info box fields */
  48. #define PURPOSE_INFO_FIELD 2
  49. #define AUTHOR_INFO_FIELD 3
  50. #define VERSION_INFO_FIELD 4
  51. #define STATUS_INFO_FIELD 5
  52. static void
  53. vBarInfo(void)
  54. {
  55. dbox d;
  56. d = dbox_new("ProgInfo");
  57. if (d != NULL) {
  58. dbox_setfield(d, PURPOSE_INFO_FIELD, PURPOSESTRING);
  59. dbox_setfield(d, AUTHOR_INFO_FIELD, AUTHORSTRING);
  60. dbox_setfield(d, VERSION_INFO_FIELD, VERSIONSTRING);
  61. dbox_setfield(d, STATUS_INFO_FIELD, STATUSSTRING);
  62. dbox_show(d);
  63. dbox_fillin(d);
  64. dbox_dispose(&d);
  65. }
  66. } /* end of vBarInfo */
  67. static void
  68. vMouseButtonClick(wimp_mousestr *m)
  69. {
  70. if (m->w == tChoicesWindow) {
  71. vChoicesMouseClick(m);
  72. return;
  73. }
  74. DBG_DEC(m->w);
  75. } /* end of vMouseButtonClick */
  76. static void
  77. vKeyPressed(int chcode, wimp_caretstr *c)
  78. {
  79. DBG_MSG("vKeyPressed");
  80. if (chcode != '\r') {
  81. wimpt_noerr(wimp_processkey(chcode));
  82. return;
  83. }
  84. if (c->w == tChoicesWindow) {
  85. vChoicesKeyPressed(c);
  86. }
  87. } /* end of vKeyPressed */
  88. /*
  89. * Move the given window to the top of the pile.
  90. */
  91. static void
  92. vWindowToFront(wimp_w tWindow)
  93. {
  94. wimp_wstate tWindowState;
  95. wimpt_noerr(wimp_get_wind_state(tWindow, &tWindowState));
  96. tWindowState.o.behind = -1;
  97. wimpt_noerr(wimp_open_wind(&tWindowState.o));
  98. } /* end of vWindowToFront */
  99. /*
  100. *
  101. */
  102. static void
  103. vIconclick(wimp_i tUnused)
  104. {
  105. } /* end of vIconclick */
  106. static void
  107. vSaveSelect(void *pvHandle, char *pcInput)
  108. {
  109. diagram_type *pDiag;
  110. fail(pvHandle == NULL || pcInput == NULL);
  111. pDiag = (diagram_type *)pvHandle;
  112. switch (pcInput[0]) {
  113. case 1:
  114. vScaleOpenAction(pDiag);
  115. break;
  116. case 2:
  117. vSaveDrawfile(pDiag);
  118. break;
  119. case 3:
  120. vSaveTextfile(pDiag);
  121. break;
  122. default:
  123. DBG_DEC(pcInput[0]);
  124. break;
  125. }
  126. } /* end of vMenuSelect */
  127. /*
  128. * Create the window for the text from the given file
  129. */
  130. static diagram_type *
  131. pCreateTextWindow(const char *szFilename)
  132. {
  133. diagram_type *pDiag;
  134. menu pSaveMenu;
  135. DBG_MSG("pCreateTextWindow");
  136. fail(szFilename == NULL || szFilename[0] == '\0');
  137. pDiag = pCreateDiagram(szTask+1, szFilename);
  138. if (pDiag == NULL) {
  139. werr(0, "No new diagram object");
  140. return NULL;
  141. }
  142. win_register_event_handler(pDiag->tMainWindow,
  143. vMainEventHandler, pDiag);
  144. win_register_event_handler(pDiag->tScaleWindow,
  145. vScaleEventHandler, pDiag);
  146. pSaveMenu = menu_new(szTask+1,
  147. ">Scale view,"
  148. ">Save (Drawfile) F3,"
  149. ">Save (Text only) \213F3");
  150. if (pSaveMenu == NULL) {
  151. werr(0, "No new menu object");
  152. return NULL;
  153. }
  154. if (!event_attachmenu(pDiag->tMainWindow,
  155. pSaveMenu, vSaveSelect, pDiag)) {
  156. werr(0, "I can't attach to event");
  157. return NULL;
  158. }
  159. /* Set the window title */
  160. vSetTitle(pDiag);
  161. return pDiag;
  162. } /* end of pCreateTextWindow */
  163. static void
  164. vProcessFile(const char *szFilename, int iFiletype)
  165. {
  166. options_type tOptions;
  167. FILE *pFile;
  168. diagram_type *pDiag;
  169. long lFilesize;
  170. int iWordVersion;
  171. fail(szFilename == NULL || szFilename[0] == '\0');
  172. DBG_MSG(szFilename);
  173. pFile = fopen(szFilename, "rb");
  174. if (pFile == NULL) {
  175. werr(0, "I can't open '%s' for reading", szFilename);
  176. return;
  177. }
  178. lFilesize = lGetFilesize(szFilename);
  179. if (lFilesize < 0) {
  180. (void)fclose(pFile);
  181. werr(0, "I can't get the size of '%s'", szFilename);
  182. return;
  183. }
  184. iWordVersion = iGuessVersionNumber(pFile, lFilesize);
  185. if (iWordVersion < 0 || iWordVersion == 3) {
  186. if (bIsRtfFile(pFile)) {
  187. werr(0, "%s is not a Word Document."
  188. " It is probably a Rich Text Format file",
  189. szFilename);
  190. } if (bIsWordPerfectFile(pFile)) {
  191. werr(0, "%s is not a Word Document."
  192. " It is probably a Word Perfect file",
  193. szFilename);
  194. } else {
  195. werr(0, "%s is not a Word Document.", szFilename);
  196. }
  197. (void)fclose(pFile);
  198. return;
  199. }
  200. /* Reset any reading done during file testing */
  201. rewind(pFile);
  202. if (iFiletype != FILETYPE_MSWORD) {
  203. vGetOptions(&tOptions);
  204. if (tOptions.bAutofiletypeAllowed) {
  205. vSetFiletype(szFilename, FILETYPE_MSWORD);
  206. }
  207. }
  208. pDiag = pCreateTextWindow(szFilename);
  209. if (pDiag == NULL) {
  210. (void)fclose(pFile);
  211. return;
  212. }
  213. (void)bWordDecryptor(pFile, lFilesize, pDiag);
  214. if (bVerifyDiagram(pDiag)) {
  215. vShowDiagram(pDiag);
  216. }
  217. (void)fclose(pFile);
  218. } /* end of vProcessFile */
  219. static void
  220. vProcessDraggedFile(void)
  221. {
  222. char *szTmp;
  223. int iFiletype;
  224. char szFilename[PATH_MAX+1];
  225. iFiletype = xferrecv_checkinsert(&szTmp);
  226. if (iFiletype == -1) {
  227. werr(0, "I failed to import a file");
  228. return;
  229. }
  230. DBG_HEX(iFiletype);
  231. if (strlen(szTmp) >= sizeof(szFilename)) {
  232. werr(1, "Internal error: filename too long");
  233. }
  234. (void)strcpy(szFilename, szTmp);
  235. DBG_MSG(szFilename);
  236. vProcessFile(szFilename, iFiletype);
  237. xferrecv_insertfileok();
  238. } /* end of vProcessDraggedFile */
  239. static void
  240. vEventHandler(wimp_eventstr *pEvent, void *pvUnused)
  241. {
  242. switch (pEvent->e) {
  243. case wimp_ENULL:
  244. break;
  245. case wimp_EREDRAW:
  246. /* handled by the WIMP */
  247. break;
  248. case wimp_EOPEN:
  249. wimpt_noerr(wimp_open_wind(&pEvent->data.o));
  250. break;
  251. case wimp_ECLOSE:
  252. wimpt_noerr(wimp_close_wind(pEvent->data.o.w));
  253. break;
  254. case wimp_EBUT:
  255. vMouseButtonClick(&pEvent->data.but.m);
  256. break;
  257. case wimp_EKEY:
  258. vKeyPressed(pEvent->data.key.chcode, &pEvent->data.key.c);
  259. break;
  260. case wimp_ESEND:
  261. case wimp_ESENDWANTACK:
  262. switch (pEvent->data.msg.hdr.action) {
  263. case wimp_MCLOSEDOWN:
  264. exit(EXIT_SUCCESS);
  265. break;
  266. case wimp_MDATALOAD:
  267. case wimp_MDATAOPEN:
  268. vProcessDraggedFile();
  269. break;
  270. }
  271. }
  272. } /* end of vEventHandler */
  273. static void
  274. vMenuSelect(void *pvUnused, char *Input)
  275. {
  276. switch (*Input) {
  277. case 1:
  278. vBarInfo();
  279. break;
  280. case 2:
  281. vChoicesOpenAction(tChoicesWindow);
  282. vWindowToFront(tChoicesWindow);
  283. break;
  284. case 3:
  285. exit(EXIT_SUCCESS);
  286. break;
  287. default:
  288. break;
  289. }
  290. } /* end of vMenuSelect */
  291. static void
  292. vTemplates(void)
  293. {
  294. wimp_wind *pw;
  295. pw = template_syshandle("Choices");
  296. if (pw == NULL) {
  297. werr(1, "Template 'Choices' can't be found");
  298. }
  299. wimpt_noerr(wimp_create_wind(pw, &tChoicesWindow));
  300. win_register_event_handler(tChoicesWindow, vEventHandler, NULL);
  301. } /* end of vTemplates */
  302. static void
  303. vInitialise(void)
  304. {
  305. menu pBarMenu;
  306. (void)wimpt_init(szTask+1);
  307. res_init(szTask+1);
  308. template_init();
  309. dbox_init();
  310. flex_init();
  311. _kernel_register_slotextend(flex_budge);
  312. drawfobj_init();
  313. vTemplates();
  314. pBarMenu = menu_new(szTask+1, ">Info,Choices...,Quit");
  315. if (pBarMenu == NULL) {
  316. werr(1, "I can't initialise (menu_new)");
  317. }
  318. baricon(szTask, (int)resspr_area(), vIconclick);
  319. if (!event_attachmenu(win_ICONBAR, pBarMenu, vMenuSelect, NULL)) {
  320. werr(1, "I can't initialise (event_attachmenu)");
  321. }
  322. win_register_event_handler(win_ICONBARLOAD, vEventHandler, NULL);
  323. } /* end of vInitialise */
  324. int
  325. main(int argc, char **argv)
  326. {
  327. int iFirst, iFiletype;
  328. vInitialise();
  329. iFirst = iReadOptions(argc, argv);
  330. if (iFirst != 1) {
  331. return EXIT_FAILURE;
  332. }
  333. if (argc > 1) {
  334. iFiletype = iGetFiletype(argv[1]);
  335. if (iFiletype < 0) {
  336. return EXIT_FAILURE;
  337. }
  338. vProcessFile(argv[1], iFiletype);
  339. }
  340. event_setmask(wimp_EMNULL|wimp_EMPTRENTER|wimp_EMPTRLEAVE);
  341. for (;;) {
  342. event_process();
  343. }
  344. } /* end of main */