home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / main_r.c < prev    next >
C/C++ Source or Header  |  2001-05-06  |  8KB  |  387 lines

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