home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mlelib.zip / mlelib / editor.cpp next >
C/C++ Source or Header  |  1993-10-08  |  10KB  |  391 lines

  1. /*************************************************************************
  2. *
  3. * filename        : editor.cpp    (implementation of class EDITOR)
  4. *
  5. * description    : manage a list of Multi-Line Entryfields
  6. *                  (e.g for "file"-menus)
  7. *
  8. * methods        : EDITOR::WindowFromFile    - get MLE from filename
  9. *                  EDITOR::ActiveWindow        - get handle of active MLE
  10. *                  EDITOR::NewFile            - create an empty MLE
  11. *                  EDITOR::OpenFile            - load a file to a MLE
  12. *                  EDITOR::SaveFile            - save the contents of an MLE
  13. *                  EDITOR::SaveFileAs        - save the contents of an MLE
  14. *                  EDITOR::CloseFile            - close an MLE
  15. *                  EDITOR::SaveAllFiles        - save the contents of all MLEs
  16. *                  EDITOR::CloseAllFiles        - close all MLEs
  17. *                  EDITOR::Cut                - move text to the clipboard
  18. *                  EDITOR::Copy                - copy text to the clipboard
  19. *                  EDITOR::Paste                - insert text from the clipboard
  20. *                  EDITOR::Clear                - remove text
  21. *                  EDITOR::ChangeColor        - change all MLE's colors
  22. *
  23. * APIs            : DosEnterCritSec
  24. *                  DosExitCritSec
  25. *
  26. * Used Classes    : class MLE
  27. *
  28. * copyright (C) 1993 Jörg Caumanns (caumanns@cs.tu-berlin.de)
  29. *
  30. *************************************************************************/
  31. #define INCL_DOSPROCESS
  32. #include <os2.h>
  33. #include <string.h>
  34.  
  35. #include "editor.h"
  36. #include "mle.h"
  37.  
  38.  
  39. /*************************************************************************
  40. *
  41. * Method: WindowFromFile (private method)
  42. *
  43. * Descr.: Get the handle of the MLE where the specified file was loaded to.
  44. *
  45. * Impl. : Search all all open MLEs for the file
  46. *
  47. * APIs    : [none]
  48. *
  49. * Param.: CHAR *pszFile        - filename
  50. *
  51. * Return: MLE* MLE-handle (NULL if file is not loaded to any MLE)
  52. *
  53. *************************************************************************/
  54. MLE *EDITOR::WindowFromFile(CHAR *pszFile)    {
  55.     MLE_LIST *plst = pmlelist;
  56.  
  57.     if(pszFile)
  58.       for(; plst; plst = plst->next)
  59.         if(plst->pmle && (plst->pmle)->LoadedFile() &&
  60.                                 !strcmp(pszFile, (plst->pmle)->LoadedFile()))
  61.           return plst->pmle;
  62.     return (MLE*)0;
  63.     }
  64.  
  65.  
  66. /*************************************************************************
  67. *
  68. * Method: ActiveWindow (private method)
  69. *
  70. * Descr.: Get the handle of the MLE that has the focus
  71. *
  72. * Impl. : Get the handle of a valid MLE and call its 'ActiveMLE'-method
  73. *
  74. * APIs    : [none]
  75. *
  76. * Param.: [none]
  77. *
  78. * Return: MLE* MLE-handle (NULL if no MLE has the focus)
  79. *
  80. *************************************************************************/
  81. MLE *EDITOR::ActiveWindow(VOID)    {
  82.     MLE_LIST *plst = pmlelist;
  83.  
  84.     for(; plst; plst = plst->next)
  85.         if(plst->pmle)
  86.             return (pmlelist->pmle)->ActiveMLE();
  87.  
  88.     return (MLE *)0;
  89.     }
  90.  
  91.  
  92. /*************************************************************************
  93. *
  94. * Method: NewFile
  95. *
  96. * Descr.: Create an empty MLE
  97. *
  98. * Impl. : An empty MLE is created and put to the list of open MLEs
  99. *
  100. * APIs    : DosEnterCritSec
  101. *          DosExitCritSec
  102. *
  103. * Param.: [none]
  104. *
  105. * Return: BOOL fSuccess (TRUE if MLE was created successfully)
  106. *
  107. *************************************************************************/
  108. BOOL EDITOR::NewFile(VOID)    {
  109.     MLE *pmle = new MLE;
  110.     MLE_LIST *plst = new MLE_LIST;
  111.  
  112.     if(pmle->OpenFile(NULL, MLE_NEWFILE))    {
  113.         DosEnterCritSec();
  114.         plst->pmle         = pmle;
  115.         plst->next        = pmlelist;
  116.         pmlelist        = plst;
  117.         DosExitCritSec();
  118.         return TRUE;
  119.         }
  120.     return FALSE;
  121.     }
  122.  
  123.  
  124. /*************************************************************************
  125. *
  126. * Method: OpenFile
  127. *
  128. * Descr.: Create an empty MLE and load a file to it
  129. *
  130. * Impl. : A file is loaded to an empty MLE. The MLE is put to the list
  131. *          of open MLEs
  132. *
  133. * APIs    : DosEnterCritSec
  134. *          DosExitCritSec
  135. *
  136. * Param.: CHAR *pszFile        - name of the file to be loaded (optional)
  137. *                              If no parameter is given, a dialogbox
  138. *                             is used to ask the user for a filename.
  139. *
  140. * Return: BOOL fSuccess (TRUE if MLE was created successfully)
  141. *
  142. *************************************************************************/
  143. BOOL EDITOR::OpenFile(CHAR *pszFile)    {
  144.     MLE *pmle = new MLE;
  145.     MLE_LIST *plst = new MLE_LIST;
  146.  
  147.     if(pmle->OpenFile(pszFile))    {
  148.         DosEnterCritSec();
  149.         plst->pmle         = pmle;
  150.         plst->next        = pmlelist;
  151.         pmlelist        = plst;
  152.         DosExitCritSec();
  153.         return TRUE;
  154.         }
  155.     return FALSE;
  156.     }
  157.  
  158.  
  159. /*************************************************************************
  160. *
  161. * Method: SaveFile
  162. *
  163. * Descr.: Save the contents of an MLE
  164. *
  165. * Impl. : If an argument is given the specified file is saved.
  166. *          If no argument is given, the contents of the active MLE is
  167. *          saved. No saving is done, if the file hasn't changed.
  168. *
  169. * APIs    : [none]
  170. *
  171. * Param.: CHAR *pszFile        - name of the file to be saved (optional)
  172. *
  173. * Return: BOOL fSuccess (TRUE if file was saved)
  174. *
  175. *************************************************************************/
  176. BOOL EDITOR::SaveFile(CHAR *pszFile)    {
  177.     MLE *pmle;
  178.  
  179.     if((pmle = (pszFile)? WindowFromFile(pszFile) : ActiveWindow()) ==(MLE *)0)
  180.         return FALSE;
  181.  
  182.     if(pmle->Touched())
  183.         return pmle->SaveFile();
  184.  
  185.     return TRUE;
  186.     }
  187.  
  188.  
  189. /*************************************************************************
  190. *
  191. * Method: SaveFileAs
  192. *
  193. * Descr.: Save the contents of an MLE after asking the user for
  194. *          a new filename ("save as"-dialogbox)
  195. *
  196. * Impl. : If an argument is given the specified file is saved.
  197. *          If no argument is given, the contents of the active MLE is
  198. *          saved.
  199. *
  200. * APIs    : [none]
  201. *
  202. * Param.: CHAR *pszFile        - name of the file to be saved (optional)
  203. *
  204. * Return: BOOL fSuccess (TRUE if file was saved)
  205. *
  206. *************************************************************************/
  207. BOOL EDITOR::SaveFileAs(CHAR *pszFile)    {
  208.     MLE *pmle;
  209.  
  210.     if((pmle = (pszFile)? WindowFromFile(pszFile) : ActiveWindow()) ==(MLE *)0)
  211.         return FALSE;
  212.  
  213.     return pmle->SaveFileAs();
  214.     }
  215.  
  216.  
  217. /*************************************************************************
  218. *
  219. * Method: CloseFile
  220. *
  221. * Descr.: Save the contents of an MLE and close the MLE
  222. *
  223. * Impl. : If an argument is given the specified file is saved and closed.
  224. *          If no argument is given, the contents of the active MLE is
  225. *          saved and the MLE closed. No saving is done, if the file
  226. *          hasn't changed.
  227. *
  228. * APIs    : [none]
  229. *
  230. * Param.: CHAR *pszFile        - name of the file to be closed (optional)
  231. *
  232. * Return: BOOL fSuccess (TRUE if file was closed successfully)
  233. *
  234. *************************************************************************/
  235. BOOL EDITOR::CloseFile(CHAR *pszFile)    {
  236.     MLE *pmle;
  237.  
  238.     if((pmle = (pszFile)? WindowFromFile(pszFile) : ActiveWindow()) ==(MLE *)0)
  239.         return FALSE;
  240.  
  241.     return pmle->CloseFile();
  242.     }
  243.  
  244.  
  245. /*************************************************************************
  246. *
  247. * Method: SaveAllFiles
  248. *
  249. * Descr.: Save the contents of all MLEs
  250. *
  251. * Impl. : Check all MLEs one after another and save their contents if
  252. *          it has changed
  253. *
  254. * APIs    : [none]
  255. *
  256. * Param.: [none]
  257. *
  258. * Return: BOOL fSuccess (TRUE if all files have been saved)
  259. *
  260. *************************************************************************/
  261. BOOL EDITOR::SaveAllFiles(VOID)    {
  262.     BOOL ret = TRUE;
  263.  
  264.     for(MLE_LIST *plst = pmlelist; plst; plst = plst->next)
  265.         if(plst->pmle && (plst->pmle)->Touched())
  266.             ret &= (plst->pmle)->SaveFile();
  267.  
  268.     return ret;
  269.     }
  270.  
  271.  
  272. /*************************************************************************
  273. *
  274. * Method: CloseAllFiles
  275. *
  276. * Descr.: Close all MLEs and save their contents
  277. *
  278. * Impl. : Close an MLE after another
  279. *
  280. * APIs    : [none]
  281. *
  282. * Param.: [none]
  283. *
  284. * Return: BOOL fSuccess (TRUE if all files have been closed)
  285. *
  286. *************************************************************************/
  287. BOOL EDITOR::CloseAllFiles(VOID)    {
  288.     BOOL ret = TRUE;
  289.  
  290.     for(MLE_LIST *plst = pmlelist; plst; plst = plst->next)
  291.         if(plst->pmle)
  292.             ret &= (plst->pmle)->CloseFile();
  293.  
  294.     return ret;
  295.     }
  296.  
  297.  
  298. /*************************************************************************
  299. *
  300. * Method: Cut, Copy, Paste, Clear
  301. *
  302. * Descr.: Move text between an MLE and the clipboard
  303. *
  304. * Impl. : Call the appropriate method of the active MLE
  305. *
  306. * APIs    : [none]
  307. *
  308. * Param.: [none]
  309. *
  310. * Return: -
  311. *
  312. *************************************************************************/
  313. VOID EDITOR::Cut(VOID)    {
  314.     MLE *pmle;
  315.     if((pmle = ActiveWindow()) != (MLE *)0)
  316.         pmle->Cut();
  317.     }
  318.  
  319. VOID EDITOR::Copy(VOID)    {
  320.     MLE *pmle;
  321.     if((pmle = ActiveWindow()) != (MLE *)0)
  322.         pmle->Copy();
  323.     }
  324.  
  325. VOID EDITOR::Paste(VOID)    {
  326.     MLE *pmle;
  327.     if((pmle = ActiveWindow()) != (MLE *)0)
  328.         pmle->Paste();
  329.     }
  330.  
  331. VOID EDITOR::Clear(VOID)    {
  332.     MLE *pmle;
  333.     if((pmle = ActiveWindow()) != (MLE *)0)
  334.         pmle->Clear();
  335.     }
  336.  
  337.  
  338. /*************************************************************************
  339. *
  340. * Method: SetColor
  341. *
  342. * Descr.: Change the foreground- and background-color of all MLEs
  343. *
  344. * Impl. : Change the colors of one MLE after another
  345. *
  346. * APIs    : [none]
  347. *
  348. * Param.: LONG clrForeground    - new foreground-color
  349. *           LONG clrBackground    - new background-color
  350. *
  351. * Return: -
  352. *
  353. *************************************************************************/
  354. VOID EDITOR::SetColor(LONG clrForeground, LONG clrBackground)    {
  355.  
  356.     for(MLE_LIST *plst = pmlelist; plst; plst = plst->next)
  357.         if(plst->pmle)
  358.             (plst->pmle)->SetColor(clrForeground, clrBackground);
  359.     }
  360.  
  361.  
  362. /*************************************************************************
  363. *
  364. * Method: destructor
  365. *
  366. * Descr.: do some cleanup
  367. *
  368. * Impl. : Close all MLEs and free all resources
  369. *
  370. * APIs    : [none]
  371. *
  372. * Param.: [none]
  373. *
  374. * Return: -
  375. *
  376. *************************************************************************/
  377. EDITOR::~EDITOR()    {
  378.  
  379.     CloseAllFiles();
  380.  
  381.     MLE_LIST *plst = pmlelist;
  382.     MLE_LIST *tmp = (plst)? plst->next : (MLE_LIST*)0;
  383.     for(; plst; plst = tmp, tmp = (plst)? plst->next : (MLE_LIST*)0)    {
  384.         delete plst->pmle;
  385.         delete plst;
  386.         }
  387.     }
  388.  
  389.  
  390.  
  391.