home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / spl / src / splwin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  14.5 KB  |  625 lines

  1. /* splwin.cpp:  spell window class procedures
  2.  
  3.     Copyright (C) 1993, 1994 John-Marc Chandonia
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "spl.hpp"
  24. #include "general.hpp"
  25. #include "spldlg.h"
  26. #include "bookwin.hpp"
  27. #include "splwin.hpp"
  28. #include "spldlgs.hpp"
  29. #include "ea.hpp"
  30.  
  31. // stuff defined in the main program
  32. extern HAB hab;
  33. extern HINI hini;
  34. extern BYTE xfer_buffer[10240];
  35. extern int windows;
  36. extern char changed_file_name[256];
  37. extern HWND hwndspellhelp;
  38.  
  39. spellwindow *firstspell=NULL;
  40.  
  41. spellwindow::spellwindow(spelllist *spll, bookwindow *p) {    
  42.     char *windowname;
  43.  
  44.     ULONG flFlags = FCF_TITLEBAR |
  45.     FCF_SIZEBORDER|
  46.     FCF_MINMAX|
  47.     FCF_SYSMENU|
  48.     FCF_MENU|
  49.     FCF_ACCELTABLE|
  50.     FCF_TASKLIST;
  51.  
  52.     // add new window as the first one
  53.     next=firstspell;
  54.     if (firstspell!=NULL) firstspell->prev=this;
  55.     firstspell=this;
  56.     prev=NULL;
  57.  
  58.     readonly=true;
  59.     sl=spll;
  60.     parent = p;
  61.  
  62.     if (sl) windowname=sl->s->name;
  63.     else windowname="Spell";
  64.  
  65.     // create the window 
  66.     hwndframe = WinCreateStdWindow(HWND_DESKTOP,
  67.                    WS_VISIBLE,
  68.                    &flFlags,
  69.                    (PSZ)splclass,
  70.                    (PSZ)windowname,
  71.                    WS_VISIBLE,
  72.                    0,
  73.                    SPELLMENU,
  74.                    NULL);
  75.  
  76.     // add F1-sensitive help
  77.     if (hwndspellhelp)
  78.     WinAssociateHelpInstance(hwndspellhelp,hwndframe);
  79. }
  80.  
  81. spellwindow::~spellwindow() {
  82.     // remove from linked list
  83.     if (this==firstspell) firstspell=next;
  84.     if (next!=NULL) next->prev=prev;
  85.     if (prev!=NULL) prev->next=next;
  86.  
  87.     // remove from display
  88.     WinDestroyWindow(hwndframe);
  89. }
  90.  
  91. // empty out mle and show a spell in it
  92. void mle_show_spell(HWND hwndmle, spell *s) {
  93.     IPT insertion_point=0;
  94.  
  95.     // remove old spell
  96.     IPT text_length;
  97.     text_length=(IPT)WinSendMsg(hwndmle,
  98.                 MLM_QUERYTEXTLENGTH,
  99.                 0,0);
  100.  
  101.     // hide window for speed:
  102.     WinShowWindow(hwndmle,FALSE);
  103.  
  104.     WinSendMsg(hwndmle,
  105.            MLM_DELETE,
  106.            0,
  107.            (MPARAM)text_length);
  108.  
  109.     // set up transfer buffer
  110.     WinSendMsg(hwndmle,
  111.            MLM_SETIMPORTEXPORT,
  112.            (MPARAM)(&xfer_buffer),
  113.            (MPARAM)10240);
  114.  
  115.     WinSendMsg(hwndmle,
  116.            MLM_FORMAT,
  117.            (MPARAM)MLFIE_CFTEXT,
  118.            0);
  119.  
  120.     s->s_print((char *)xfer_buffer);
  121.  
  122.     WinSendMsg(hwndmle,
  123.            MLM_IMPORT,
  124.            (MPARAM)(&insertion_point),
  125.            (MPARAM)strlen((char *)xfer_buffer));
  126.  
  127.     // show window again
  128.     WinShowWindow(hwndmle,TRUE);
  129. }
  130.  
  131. void edit_save_changes(spellwindow *mywin) {
  132.     IPT insertion_point;
  133.     ULONG text_length;
  134.     char *tmpfilename;
  135.     FILE *tmpfile;
  136.     spell *mod_spell, *old_spell;
  137.     char *bptr;
  138.     int i;
  139.  
  140.     WinSendMsg(mywin->hwndmle,MLM_SETREADONLY,(MPARAM)TRUE,0);
  141.     mywin->readonly=true;
  142.  
  143.         
  144.     // set up transfer buffer
  145.     text_length=(ULONG)WinSendMsg(mywin->hwndmle,
  146.                   MLM_QUERYTEXTLENGTH,
  147.                   0,0);
  148.     WinSendMsg(mywin->hwndmle,
  149.            MLM_SETIMPORTEXPORT,
  150.            (MPARAM)(&xfer_buffer),
  151.            (MPARAM)10240);
  152.     WinSendMsg(mywin->hwndmle,
  153.            MLM_FORMAT,
  154.            (MPARAM)MLFIE_CFTEXT,
  155.            0);
  156.     
  157.     // get spell into transfer buffer
  158.     insertion_point=0;
  159.  
  160.     WinSendMsg(mywin->hwndmle,
  161.            MLM_EXPORT,
  162.            (MPARAM)(&insertion_point),
  163.            (MPARAM)(&text_length));
  164.     
  165.     // write spell to file
  166.     tmpfilename=tmpnam(NULL);
  167.     tmpfile=fopen(tmpfilename,"w");
  168.     bptr=(char *)xfer_buffer;
  169.     i=0;
  170.     while (*bptr!=(char)0) {
  171.     if (*bptr==(char)13) {
  172.         fprintf(tmpfile,"\n");
  173.         bptr++;
  174.         i=0;
  175.     }
  176.     else if ((i>65) && (*bptr==' ')) {
  177.         fprintf(tmpfile,"\n");
  178.         i=0;
  179.     }
  180.     else fprintf(tmpfile,"%c",*bptr);
  181.     bptr++;
  182.     i++;
  183.     }
  184.     fprintf(tmpfile,"\n");
  185.     fclose(tmpfile);
  186.     
  187.     // read it back in, deleting tmp file
  188.     tmpfile=fopen(tmpfilename,"r");
  189.     if (mywin->sl->s->type=='M') mod_spell=new magespell;
  190.     else mod_spell=new priestspell;
  191.     mod_spell->f_read(tmpfile);
  192.     fclose(tmpfile);
  193.     //unlink(tmpfilename);
  194.  
  195.     old_spell=mywin->sl->s;
  196.  
  197.     // change spell in all windows
  198.     change_all(old_spell,mod_spell);
  199.  
  200.     // write the spell to the end of "changed"
  201.     tmpfile=fopen(changed_file_name,"a");
  202.     fprintf(tmpfile,"-----\n");
  203.     mod_spell->f_print(tmpfile);
  204.     fclose(tmpfile);
  205.  
  206.     // show the new spell window
  207.     find_and_show_spell(mywin->parent, mod_spell);
  208.  
  209.     // remove old spell, and my window
  210.     delete old_spell;
  211.     WinSendMsg(mywin->hwnd,
  212.            WM_CLOSE,
  213.            0,
  214.            0); 
  215. }
  216.  
  217. // change mle to given spellist, if everything's OK.
  218. void mle_set_spell(spellwindow *mywin, spelllist *sl) {
  219.     if (sl!=NULL) {
  220.     if (!mywin->readonly) {
  221.         ULONG response = 
  222.         WinMessageBox(HWND_DESKTOP,
  223.                   HWND_DESKTOP,
  224.                   (PSZ)"You are in the middle of editing.  Any changes will be lost.  Are you sure?",
  225.                   (PSZ)"Warning!",
  226.                   102,
  227.                   MB_YESNO|MB_ICONEXCLAMATION);
  228.         if (response==MBID_NO) return;
  229.         mywin->readonly=true;
  230.     }
  231.     mywin->sl=sl;
  232.     WinSetWindowText(mywin->hwndframe,(PSZ)mywin->sl->s->name);
  233.     mle_show_spell(mywin->hwndmle,mywin->sl->s);
  234.     }
  235. }
  236.  
  237. // load and set window attributes for a spellwindow
  238. void spellwindow::load_attr() {
  239.     SWP swp;
  240.     spell_attr sa;
  241.     boolean foundpos;
  242.     ULONG ultemp;
  243.     long ltemp;
  244.     SHORT w,h;
  245.     boolean master_list_win = false;
  246.     char *filename=NULL;
  247.  
  248.     if (parent) {
  249.     master_list_win = parent->master_list_win;
  250.     filename = parent->filename;
  251.     }
  252.  
  253.     // get default values
  254.     WinQueryTaskSizePos(hab,0,&swp);
  255.     sa.x = swp.x;
  256.     sa.y = swp.y;
  257.     sa.cx = swp.cx;
  258.     sa.cy = swp.cy;
  259.     sa.foregroundcolor = 666;
  260.     sa.backgroundcolor = 666;
  261.     sa.fontnamesize[0] = (char)0;
  262.  
  263.     foundpos=false;
  264.     if (master_list_win) { 
  265.     ultemp=sizeof(spell_attr);
  266.     PrfQueryProfileData(hini,
  267.                 "spellbook",
  268.                 "spellattr",
  269.                 &sa,
  270.                 &ultemp);
  271.     foundpos=true;
  272.     }
  273.     else if (filename) {
  274.     ltemp=sizeof(spell_attr);
  275.     if (getEA(filename,
  276.           ".spellattr",
  277.           &sa,
  278.           <emp)==0)
  279.         foundpos=true;
  280.     }
  281.  
  282.     if (!foundpos) {
  283.     // get generic width, height; use random x,y;
  284.     ultemp=sizeof(spell_attr);
  285.     PrfQueryProfileData(hini,
  286.                 "spellbook",
  287.                 "newspellattr",
  288.                 &sa,
  289.                 &ultemp);
  290.     sa.x = swp.x;
  291.     sa.y = swp.y;
  292.     }
  293.  
  294.     // make sure we aren't off screen.
  295.     w = WinQuerySysValue(HWND_DESKTOP,SV_CXSCREEN);
  296.     h = WinQuerySysValue(HWND_DESKTOP,SV_CYSCREEN);
  297.     if (sa.x+sa.cx > w) sa.x = w - sa.cx;
  298.     if (sa.y+sa.cy > h) sa.y = h - sa.cy;
  299.     
  300.     // set the position & size
  301.     hwndframe = WinQueryWindow(hwnd,QW_PARENT);
  302.     WinSetWindowPos(hwndframe,
  303.             HWND_TOP,
  304.             sa.x,sa.y,
  305.             sa.cx,sa.cy,
  306.             SWP_SIZE|SWP_SHOW|SWP_MOVE|SWP_ZORDER);
  307.  
  308.     // set colors and fonts
  309.     if (sa.foregroundcolor!=666)
  310.     WinSetPresParam(hwndmle,
  311.             PP_FOREGROUNDCOLOR,
  312.             sizeof(ULONG),
  313.             &sa.foregroundcolor);
  314.     if (sa.backgroundcolor!=666)
  315.     WinSetPresParam(hwndmle,
  316.             PP_BACKGROUNDCOLOR,
  317.             sizeof(ULONG),
  318.             &sa.backgroundcolor);
  319.     if (sa.fontnamesize[0]) {
  320.     WinSetPresParam(hwndmle,
  321.             PP_FONTNAMESIZE,
  322.             (ULONG)(strlen(sa.fontnamesize)+1),
  323.             sa.fontnamesize);
  324.     }
  325.             
  326. }
  327.  
  328. // save visible attributes of spellwindow
  329. void spellwindow::save_attr() {
  330.     SWP swp;
  331.     spell_attr sa;
  332.     ULONG attrfound;
  333.     ULONG attrvalue[32];
  334.     ULONG rc;
  335.     boolean master_list_win = false;
  336.     char *filename=NULL;
  337.  
  338.     if (parent) {
  339.     master_list_win = parent->master_list_win;
  340.     filename = parent->filename;
  341.     }
  342.     
  343.     WinQueryWindowPos(hwndframe,&swp);
  344.     sa.x = swp.x;
  345.     sa.y = swp.y;
  346.     sa.cx = swp.cx;
  347.     sa.cy = swp.cy;
  348.  
  349.     rc=WinQueryPresParam(hwndmle,
  350.              PP_FOREGROUNDCOLOR,
  351.              0,
  352.              &attrfound,
  353.              sizeof(attrvalue),
  354.              (PVOID)attrvalue,
  355.              0);
  356.     if (rc && (attrfound == PP_FOREGROUNDCOLOR))
  357.     memcpy(&sa.foregroundcolor,attrvalue,sizeof(ULONG));
  358.     else sa.foregroundcolor=666;
  359.  
  360.     rc=WinQueryPresParam(hwndmle,
  361.              PP_BACKGROUNDCOLOR,
  362.              0,
  363.              &attrfound,
  364.              sizeof(attrvalue),
  365.              (PVOID)attrvalue,
  366.              0);
  367.     if (rc && (attrfound == PP_BACKGROUNDCOLOR))
  368.     memcpy(&sa.backgroundcolor,attrvalue,sizeof(ULONG));
  369.     else sa.backgroundcolor=666;
  370.  
  371.     rc=WinQueryPresParam(hwndmle,
  372.              PP_FONTNAMESIZE,
  373.              0,
  374.              &attrfound,
  375.              sizeof(attrvalue),
  376.              (PVOID)attrvalue,
  377.              0);
  378.     if (rc && (attrfound == PP_FONTNAMESIZE)) {
  379.     if (rc>127) rc=127;
  380.     sa.fontnamesize[rc]=(char)0;
  381.     memcpy(sa.fontnamesize,attrvalue,rc);
  382.     }
  383.     else sa.fontnamesize[0]=(char)0;
  384.  
  385.  
  386.     if (master_list_win) {
  387.     PrfWriteProfileData(hini,
  388.                 "spellbook",
  389.                 "spellattr",
  390.                 &sa,
  391.                 sizeof(spell_attr));
  392.     }
  393.     else if (filename) {
  394.     putEA(filename,
  395.           ".spellattr",
  396.           &sa,
  397.           sizeof(spell_attr)); 
  398.     }
  399.     else {
  400.     PrfWriteProfileData(hini,
  401.                 "spellbook",
  402.                 "newspellattr",
  403.                 &sa,
  404.                 sizeof(spell_attr));
  405.     }
  406. }
  407.  
  408. // window function for spell window:
  409. MRESULT EXPENTRY spell_window_func(HWND hwnd, ULONG msg, 
  410.                    MPARAM param1, MPARAM param2) {
  411.     spellwindow *mywin;
  412.     int cx,cy;
  413.     SWP winpos,swp;
  414.     ULONG ultemp;
  415.     SHORT w,h;
  416.  
  417.     switch (msg) {
  418.     case WM_ERASEBACKGROUND:
  419.     return (MRESULT)TRUE;
  420.  
  421.     case WM_CREATE:
  422.     // save handle 
  423.     firstspell->hwnd=hwnd;
  424.  
  425.     // save pointer to my class in window word
  426.     WinSetWindowPtr(hwnd,
  427.             0,
  428.             firstspell);
  429.  
  430.     // find out my size 
  431.     WinQueryWindowPos(hwnd,&winpos);
  432.     cx=winpos.cx;
  433.     cy=winpos.cy;
  434.  
  435.     // create multi line entry filling whole window 
  436.     firstspell->hwndmle=
  437.         WinCreateWindow(
  438.                 hwnd,
  439.                 WC_MLE,
  440.                 (PSZ)NULL,
  441.                 MLS_READONLY | MLS_WORDWRAP | MLS_VSCROLL,
  442.                     (LONG)0,
  443.                 (LONG)0,
  444.                 (LONG)cx,
  445.                 (LONG)cy,
  446.                 hwnd,
  447.                 HWND_TOP,
  448.                 windows++,
  449.                 NULL,
  450.                 NULL);
  451.  
  452.     firstspell->load_attr();
  453.  
  454.     // fill mle with spell 
  455.     mle_show_spell(firstspell->hwndmle, firstspell->sl->s);
  456.  
  457.     // and activate it 
  458.     WinShowWindow(firstspell->hwndmle,TRUE);
  459.  
  460.     break;
  461.  
  462.     case WM_SIZE:
  463.     mywin=(spellwindow *)WinQueryWindowPtr(hwnd,
  464.                            0);
  465.  
  466.     cx=SHORT1FROMMP(param2);
  467.     cy=SHORT2FROMMP(param2);
  468.     // tell the MLE to change its size 
  469.     WinSetWindowPos(mywin->hwndmle,0,0,0,cx,cy,SWP_SIZE);
  470.  
  471.     break;
  472.  
  473.  
  474.     // menu is activated
  475.     case WM_INITMENU:
  476.     mywin=(spellwindow *)WinQueryWindowPtr(hwnd,
  477.                            0);
  478.     switch(SHORT1FROMMP(param1)) {
  479.     case EDIT_SUBMENU:
  480.         MRESULT mr1, mr2;
  481.         BOOL enable;
  482.  
  483.         // see if text is selected in MLE
  484.         mr1 = WinSendMsg(mywin->hwndmle, MLM_QUERYSEL,MPFROMSHORT(MLFQS_MINSEL), NULL);
  485.         mr2 = WinSendMsg(mywin->hwndmle, MLM_QUERYSEL,MPFROMSHORT(MLFQS_MAXSEL), NULL);
  486.         if (mr1 != mr2) enable = TRUE;
  487.         else enable = FALSE;
  488.  
  489.         enable_menu_item(HWNDFROMMP(param2), EDIT_COPY, enable);
  490.  
  491.         if (mywin->readonly) enable=FALSE;
  492.         enable_menu_item(HWNDFROMMP(param2), EDIT_CUT, enable);
  493.         enable_menu_item(HWNDFROMMP(param2), EDIT_CLEAR, enable);
  494.  
  495.         // if undoable, and !readonly, enable undo.
  496.         if (!mywin->readonly) {
  497.         mr1 =  WinSendMsg(mywin->hwndmle, MLM_QUERYUNDO, NULL, NULL);
  498.         if (mr1 != 0) enable = TRUE;
  499.         else enable = FALSE;
  500.         }
  501.         enable_menu_item(HWNDFROMMP(param2), EDIT_UNDO, enable);
  502.  
  503.         // if clipboard has text, and !readonly, enable paste.
  504.         if (!mywin->readonly) {
  505.         if(WinOpenClipbrd(hab))
  506.         {
  507.             ULONG ulFmtInfo;
  508.             if (WinQueryClipbrdFmtInfo(hab, CF_TEXT, &ulFmtInfo))
  509.             enable = TRUE;
  510.             else
  511.             enable = FALSE;
  512.             WinCloseClipbrd(hab);
  513.         }
  514.         else
  515.             enable = TRUE;
  516.         }
  517.         enable_menu_item(HWNDFROMMP(param2), EDIT_PASTE, enable);
  518.  
  519.         // enable other menus depending on readonly status
  520.         if (mywin->readonly) enable=TRUE;
  521.         else enable=FALSE;
  522.         enable_menu_item(HWNDFROMMP(param2),EDIT_START,enable);
  523.         enable_menu_item(HWNDFROMMP(param2),EDIT_SAVECHANGES,!enable);
  524.         enable_menu_item(HWNDFROMMP(param2),EDIT_DISCARDCHANGES,!enable);
  525.  
  526.         break;
  527.     }
  528.     break;
  529.     
  530.     // command from menu 
  531.     case WM_COMMAND:
  532.     mywin=(spellwindow *)WinQueryWindowPtr(hwnd,
  533.                            0);
  534.     
  535.     switch(SHORT1FROMMP(param1)) {
  536.     case NEXTSPELL:
  537.         mle_set_spell(mywin,mywin->sl->next);
  538.         break;
  539.  
  540.     case PREVSPELL:
  541.         mle_set_spell(mywin,mywin->sl->prev);
  542.         break;
  543.  
  544.     case EDIT_UNDO:
  545.         WinSendMsg(mywin->hwndmle,MLM_UNDO,0,0);
  546.         break;
  547.  
  548.     case EDIT_CUT:
  549.         WinSendMsg(mywin->hwndmle,MLM_CUT,0,0);
  550.         break;
  551.  
  552.     case EDIT_CLEAR:
  553.         WinSendMsg(mywin->hwndmle,MLM_CLEAR,0,0);
  554.         break;
  555.  
  556.     case EDIT_COPY:
  557.         WinSendMsg(mywin->hwndmle,MLM_COPY,0,0);
  558.         break;
  559.  
  560.     case EDIT_PASTE:
  561.         WinSendMsg(mywin->hwndmle,MLM_PASTE,0,0);
  562.         break;
  563.  
  564.     case EDIT_START:
  565.         WinSendMsg(mywin->hwndmle,MLM_SETREADONLY,(MPARAM)FALSE,0);
  566.         mywin->readonly=false;
  567.         break;
  568.  
  569.     case EDIT_SAVECHANGES:
  570.         edit_save_changes(mywin);
  571.         break;
  572.  
  573.     case EDIT_DISCARDCHANGES:
  574.         WinSendMsg(mywin->hwndmle,MLM_SETREADONLY,(MPARAM)TRUE,0);
  575.         mywin->readonly=true;
  576.         mle_show_spell(mywin->hwndmle,mywin->sl->s);
  577.         break;
  578.  
  579.     case ABOUT:
  580.         WinDlgBox(HWND_DESKTOP,
  581.               hwnd,
  582.               about_dlg_func,
  583.               0L,
  584.               ABOUT_DLG,
  585.               NULL);
  586.         break;
  587.  
  588.     case HELP_INDEX:
  589.         WinSendMsg(hwndspellhelp,
  590.                HM_HELP_INDEX,
  591.                0,0);
  592.         break;
  593.  
  594.     case HELP_GENERAL:
  595.         WinSendMsg(hwndspellhelp,
  596.                HM_DISPLAY_HELP,
  597.                MPFROMSHORT(38),
  598.                HM_RESOURCEID);
  599.         break;
  600.  
  601.     case HELP_HELP:
  602.         WinSendMsg(hwndspellhelp,
  603.                HM_DISPLAY_HELP,
  604.                0,0);
  605.         break;
  606.     }
  607.     break;
  608.  
  609.     case WM_CLOSE:
  610.     mywin=(spellwindow *)WinQueryWindowPtr(hwnd,0);
  611.     delete mywin;
  612.     return (MRESULT)TRUE;
  613.     break;
  614.  
  615.     case WM_DESTROY:
  616.     mywin=(spellwindow *)WinQueryWindowPtr(hwnd,0);
  617.     mywin->save_attr();
  618.     break;
  619.     }
  620.     return WinDefWindowProc(hwnd, msg, param1, param2);
  621. }
  622.  
  623.  
  624.  
  625.