home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / Frotzs5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-11  |  27.2 KB  |  940 lines

  1. // FROTZS5.CPP
  2. //
  3. //
  4. // (C) Copyright Frederic Bouvry 1997
  5. //
  6. //
  7.  
  8.  
  9. #define NBFONTS 4
  10.  
  11. // * 20 on the S5 :-)
  12. const static fontstwips[NBFONTS] =
  13. {140, 160, 180, 220};
  14.  
  15. #include <eikfnlab.h>
  16. #include <coeutils.h>
  17. #include <eikcfdlg.h>
  18. #include <eikdoc.h>
  19. #include <eikon.rsg>
  20. #include <eikfutil.h>
  21. #include "frotzs5.h"
  22. #include "s5api.h"
  23. #include "s5inter.h"
  24.  
  25. void CKeyTimer::Start(CFrotzAppUi *ptr, int delay)
  26. {
  27.   papp = ptr;
  28.   ConstructL();
  29.   CActiveScheduler::Add(this);
  30.   After(delay);
  31. }
  32.  
  33. void CKeyTimer::RunL(void)
  34. {
  35.     if(papp->waitingkey)
  36.     {
  37.         papp->waitingkey = 0;
  38.         papp->SemaKey.Signal();
  39.     }
  40. }
  41.  
  42. int DlgOpen(char *pathsrc, char *file)
  43. {
  44. TFileName fileName = _L(pathsrc);
  45. if(!EikFileUtils::PathExists(fileName))
  46.   fileName = _L("C:\\Documents\\");
  47. fileName += _L(file);
  48. CEikFileOpenDialog* dialog=new(ELeave) CEikFileOpenDialog(&fileName);
  49. if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
  50.     {
  51.     int len = fileName.Length();
  52.     Srvmemcpy(file, (void *)fileName.Ptr(), len);
  53.     file[len] = '\0';
  54.     return 1;
  55.     }
  56. return 0;
  57. }
  58.  
  59. int DlgSave(char *pathsrc, char *file)
  60. {
  61. TFileName fileName = _L(pathsrc);
  62. if(!EikFileUtils::PathExists(fileName))
  63.   fileName = _L("C:\\Documents\\");
  64. fileName += _L(file);
  65. CEikFileSaveAsDialog* dialog=new(ELeave) CEikFileSaveAsDialog(&fileName);
  66. if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_SAVEAS))
  67.     {
  68.     int len = fileName.Length();
  69.     Srvmemcpy(file, (void *)fileName.Ptr(), len);
  70.     file[len] = '\0';
  71.     return 1;
  72.     }
  73. return 0;
  74. }
  75.  
  76. //**********************************
  77. //CMainServServer
  78. //**********************************
  79.  
  80. CMainServServer::CMainServServer(TInt aPriority)
  81.     : CServer(aPriority)
  82.     {
  83.     __DECLARE_NAME(_S("FrotzServer"));
  84.     }
  85.  
  86.  
  87. // Create and start a new Main server.
  88. CMainServServer* CMainServServer::New(CFrotzAppUi *ptr)
  89.     {
  90.     ptr->pS = new CMainServServer(EPriority);
  91.     ptr->pS->papp = ptr;
  92.     __ASSERT_ALWAYS(ptr->pS!=NULL,PanicServer(ESvrCreateServer));
  93.     HBufC *pN=MAIN_SERVER_NAME.Alloc();
  94.     __ASSERT_ALWAYS(pN!=NULL,PanicServer(ESvrCreateServer));
  95.     ptr->pS->iName=pN;
  96.     TInt r=ptr->pS->Start();
  97.     __ASSERT_ALWAYS(r==KErrNone,PanicServer(ESvrStartServer));
  98.     return ptr->pS;
  99.     }
  100.  
  101.  
  102. // Create a new server session.
  103. CSession *CMainServServer::NewSessionL(RThread aClient, const TVersion &aVersion) const
  104.     {
  105.     // check we're the right version
  106.     TVersion v(KMainServMajorVersionNumber,KMainServMinorVersionNumber,KMainServBuildVersionNumber);
  107.     if (!User::QueryVersionSupported(v,aVersion))
  108.         User::Leave(KErrNotSupported);
  109.     // make new session
  110.     CMainServSession *pss = CMainServSession::NewL(aClient, (CMainServServer*)this);
  111.     pss->papp = papp;
  112.     return pss;
  113.     }
  114.  
  115.  
  116. //**********************************
  117. //CMainServSession
  118. //**********************************
  119.  
  120.  
  121. // constructor - must pass client to CSession
  122. CMainServSession::CMainServSession(RThread &aClient, CMainServServer * aServer)
  123.     : CSession(aClient)
  124.     {
  125.     __DECLARE_NAME(_S("FrotzServer"));
  126.     aServer = aServer;
  127.     }
  128.  
  129. CMainServSession* CMainServSession::NewL(RThread &aClient, CMainServServer * aServer)
  130.     {
  131.     return new(ELeave) CMainServSession(aClient,aServer);
  132.     }
  133.  
  134.  
  135. void CMainServSession::ServiceL(const RMessage& aMessage)
  136.     {
  137.     DispatchMessageL(aMessage);
  138.     aMessage.Complete(err);
  139.     }
  140.  
  141.  
  142.  
  143. // service a client request; test the opcode and then do appropriate servicing
  144. void CMainServSession::DispatchMessageL(const RMessage &aMessage)
  145.     {
  146.     switch (aMessage.Function())
  147.       {
  148.       case EMainServPrintf:
  149.            papp->iCons->Printf(_L(aMessage.Ptr0()));
  150.            return;
  151.       case EMainServGetCh:
  152.            {
  153.            TKeyEvent iKeyEvent;
  154.            if(!papp->keyready)
  155.              {
  156.              err = 0;
  157.              return;
  158.              }
  159.            if(papp->iKeyEventQ->Count() == 0)
  160.              {
  161.              papp->keyready = 0;
  162.              err = 0;
  163.              return;
  164.              }
  165.            papp->iKeyEventQ->Remove(&iKeyEvent);
  166.            if(papp->iKeyEventQ->Count() == 0)
  167.              papp->keyready = 0;
  168.            err = iKeyEvent.iCode;
  169.            return;
  170.            }
  171.  
  172.       case EMainServKeyTimer:
  173.            {
  174.            int delay;
  175.            delay = *((int *)aMessage.Ptr0());
  176.            if(papp->keytimer)
  177.               {
  178.               papp->keytimer->Cancel();
  179.               delete papp->keytimer;
  180.               papp->keytimer = NULL;
  181.               }
  182.            papp->keytimer = new CKeyTimer();
  183.            papp->keytimer->Start(papp, delay);
  184.            return;
  185.            }
  186.       case EMainServEraseRect:
  187.           {
  188.           TRect r;
  189.           int inv, attr;
  190.           r = *((TRect *)aMessage.Ptr0());
  191.           if(r.iTl.iX >= papp->cscreenwidth)
  192.               return;
  193.           if(r.iBr.iX > papp->cscreenwidth)
  194.             r.iBr.iX = papp->cscreenwidth;
  195.           if(r.iBr.iY > papp->cscreenheight)
  196.             r.iBr.iY = papp->cscreenheight;
  197.           inv = *((int *)aMessage.Ptr1());
  198.           attr = (inv & 1 ? ATT_INVERSE : 0);
  199.           papp->iCons->ClearChars(r, attr);
  200.           return;
  201.           }
  202.       case EMainServScroll:
  203.           {
  204.           TRect r;
  205.           TPoint p;
  206.           r = *((TRect *)aMessage.Ptr0());
  207.           if(r.iTl.iX >= papp->cscreenwidth)
  208.               return;
  209.           if(r.iBr.iX > papp->cscreenwidth)
  210.             r.iBr.iX = papp->cscreenwidth;
  211.           if(r.iBr.iY > papp->cscreenheight)
  212.             r.iBr.iY = papp->cscreenheight;
  213.           p = *((TPoint *)aMessage.Ptr1());
  214.           papp->iCons->ScrollChars(r, p);
  215.           return;
  216.           }
  217.       case EMainServCursor:
  218.            {
  219.            int curon;
  220.            curon = *((int *)aMessage.Ptr0());
  221.            if(curon)
  222.                papp->iCons->DrawCursor();
  223.            else
  224.                papp->iCons->HideCursor();
  225.            return;
  226.            }
  227.       case EMainServTextAttr:
  228.            {
  229.            int attr;
  230.            papp->boldtoggled = 0;
  231.            attr = *((int *)aMessage.Ptr0());
  232.            papp->cattr = (attr & 1 ? ATT_INVERSE : 0);
  233.            if(papp->cboldmode)
  234.              {
  235.              papp->cattr |= ATT_BOLD;
  236.              papp->cattr |= (attr & 2 ? ATT_UNDERLINE : 0);
  237.              papp->cattr |= (attr & 4 ? ATT_ITALIC : 0);
  238.                papp->iCons->SetAtt(papp->cattr);
  239.              }
  240.            else
  241.              {
  242.              papp->cattr |= (attr & 2 ? ATT_ITALIC : 0);
  243.              papp->cattr |= (attr & 4 ? ATT_BOLD : 0);
  244.                papp->iCons->SetAtt(papp->cattr);
  245.              }
  246.            return;
  247.            }
  248.       case EMainServPutChar:
  249.            {
  250.            int attr;
  251.            TPoint pt;
  252.            TPtrC8 p((unsigned char *)aMessage.Ptr2(), 1);
  253.            pt.iX = *((int *)aMessage.Ptr0());
  254.            pt.iY = *((int *)aMessage.Ptr1());
  255.            if(pt.iX >= papp->cscreenwidth)
  256.              return;
  257.            if(pt.iY >= papp->cscreenheight)
  258.              return;
  259.            papp->iCons->SetCursorPosAbs(pt);
  260.            if(papp->cattr & ATT_INVERSE)
  261.              papp->iCons->ClearChars(TRect(pt, pt), ATT_INVERSE);
  262.            
  263.            if(papp->boldtoggled)
  264.                {
  265.                papp->boldtoggled = 0;
  266.                if(papp->cboldmode)
  267.                  {
  268.                  attr = ATT_BOLD;
  269.                  if(papp->cattr & ATT_ITALIC)
  270.                    attr |= ATT_UNDERLINE;
  271.                  if(papp->cattr & ATT_BOLD)
  272.                    attr |= ATT_ITALIC;
  273.                  }
  274.                else
  275.                  {
  276.                       attr = 0;
  277.                  if(papp->cattr & ATT_UNDERLINE)
  278.                    attr |= ATT_ITALIC;
  279.                  if(papp->cattr & ATT_ITALIC)
  280.                    attr |= ATT_BOLD;
  281.                  }
  282.                if(papp->cattr & ATT_INVERSE)
  283.                  attr |= ATT_INVERSE;
  284.                papp->cattr = attr;
  285.                papp->iCons->SetAtt(attr);
  286.                }
  287.            papp->iCons->Write(p);
  288.            return;
  289.            }
  290.       case EMainServPutString:
  291.           // Displays a string at the specified cursor location
  292.         {
  293.            TPoint pt;
  294.            pt.iX = *((int *)aMessage.Ptr0());
  295.            pt.iY = *((int *)aMessage.Ptr1());
  296.            if(pt.iX >= papp->cscreenwidth) return;
  297.            if(pt.iY >= papp->cscreenheight) return;
  298.            papp->iCons->SetCursorPosAbs(pt);
  299.            papp->iCons->Write(_L(aMessage.Ptr2()));
  300.            return;
  301.           }
  302.       case EMainServSetCursor:
  303.           {
  304.           TPoint pt;
  305.           pt = *((TPoint *)aMessage.Ptr0());
  306.           if(pt.iX >= papp->cscreenwidth)
  307.             return;
  308.           if(pt.iY >= papp->cscreenheight)
  309.             return;
  310.           papp->iCons->SetCursorPosAbs(pt);
  311.           return;
  312.           }
  313.       case EMainServDlgOpen:
  314.         {
  315.          int len;
  316.         TFileName fpath;
  317.         err = DlgOpen((char *)aMessage.Ptr0(), (char *)aMessage.Ptr1());
  318.         if(err)
  319.           {
  320.           fpath = EikFileUtils::DriveAndPathFromFullName(_L((char *)aMessage.Ptr1()));
  321.           len = fpath.Length();
  322.           Srvmemcpy(papp->savepath, (void *)fpath.Ptr(), len);
  323.           }
  324.            papp->iControl->SetFocus(ETrue,EDrawNow);
  325.         return;
  326.         }
  327.       case EMainServDlgSave:
  328.         {
  329.          int len;
  330.         TFileName fpath;
  331.         err = DlgSave((char *)aMessage.Ptr0(), (char *)aMessage.Ptr1());
  332.         if(err)
  333.           {
  334.           fpath = EikFileUtils::DriveAndPathFromFullName(_L((char *)aMessage.Ptr1()));
  335.           len = fpath.Length();
  336.           Srvmemcpy(papp->savepath, (void *)fpath.Ptr(), len);
  337.           }
  338.            papp->iControl->SetFocus(ETrue,EDrawNow);
  339.         return;
  340.         }
  341.       case EMainServGetApp:
  342.         {
  343.         CFrotzAppUi **perr = (CFrotzAppUi **)&err;
  344.         *perr = papp;
  345.         return;
  346.         }
  347.       case EMainServGetStoryName:
  348.         {
  349.         Srvstrcpy((char *)aMessage.Ptr0(), papp->thestoryname);
  350.         return;
  351.         }
  352.       case EMainServTerminate:
  353.         {
  354.         papp->storyrunning = 0;
  355.          papp->iCons->HideCursor();
  356.         papp->DrawUserScreen();
  357.         err = 0;
  358.         return;
  359.         }
  360.         // requests we don't understand at all are a different thing,
  361.         // so panic the client here, this function also completes the message
  362.     default:
  363.           PanicClient(EBadRequest);
  364.           return;
  365.       }
  366.     }
  367.  
  368.  
  369. // panic the client
  370. void CMainServSession::PanicClient(TInt aPanic) const
  371.     {
  372.     Panic(_L("MainServ server"),aPanic);
  373.     }
  374.  
  375.  
  376. // write to the client thread; if unsuccessful, panic the client
  377. void CMainServSession::Write(const TAny* aPtr,const TDesC8& aDes,TInt anOffset)
  378.     {
  379.     TRAPD(ret,WriteL(aPtr,aDes,anOffset);)
  380.     if (ret!=KErrNone)
  381.         PanicClient(EBadDescriptor);
  382.     }
  383.  
  384. //**********************************
  385. //Global functions
  386. //**********************************
  387.  
  388. // Panic the server
  389. GLDEF_C void PanicServer(TMainServPanic aPanic)
  390.     {
  391.     User::Panic(_L("Main server panic"),aPanic);
  392.     }
  393. ////////////////////////////////////////////////////////////////////////////////////////
  394. //                            CFrotzAppUi
  395. ////////////////////////////////////////////////////////////////////////////////////////
  396.  
  397.  
  398.  
  399. //////////////////////////////////////////////////////
  400.  
  401. void CFrotzAppUi::HandleCommandL(TInt aCommand)
  402.     {
  403.       // Handle the command generated by:
  404.       //   1. menu item selection
  405.       //   2. short-cut key press
  406. switch (aCommand)
  407.         {
  408.         // Copy command
  409.     case EEikCmdEditCut:            // <---Pre-defined command id
  410.     case EEikCmdEditCopy:           // <---Pre-defined command id
  411.     case EEikCmdEditPaste:          // <---Pre-defined command id
  412.     case EEikCmdEditSelectAll:      // <---Pre-defined command id
  413.         iEikonEnv->InfoMsg(_L("Not used by Frotz"));
  414.         return;
  415.         // Infrared command
  416.     case EEikCmdIrdaSend:
  417.     case EEikCmdIrdaReceive:
  418.         iEikonEnv->InfoMsg(_L("Deathmatch mode not implemented :-)"));
  419.         break;
  420.         // Help
  421.         // Zoom in command
  422.     case EEikCmdZoomIn:             // <---Pre-defined command id
  423.         {
  424.          if(storyrunning)
  425.           {
  426.           iEikonEnv->InfoMsg(_L("Terminate active story first"));
  427.           return;
  428.           }
  429.         cfontsize++;
  430.         if(cfontsize == NBFONTS)
  431.             cfontsize = 0;
  432.         iCons->ClearScreen();
  433.         iCons->SetFontL(TFontSpec(_L("Courier"), fontstwips[cfontsize]));
  434.         cscreenwidth = (iControl->Size().iWidth/iControl->CharSize().iWidth) - 1;
  435.         cscreenheight = iControl->Size().iHeight/iControl->CharSize().iHeight;
  436.         DrawUserScreen();
  437.         break;
  438.         }
  439.         // Zoom out command
  440.     case EEikCmdZoomOut:            // <---Pre-defined command id       
  441.         {
  442.          if(storyrunning)
  443.           {
  444.           iEikonEnv->InfoMsg(_L("Terminate active story first"));
  445.           return;
  446.           }
  447.         if(cfontsize == 0)
  448.             cfontsize = NBFONTS - 1;
  449.         else
  450.             --cfontsize;
  451.         iCons->ClearScreen();
  452.         iCons->SetFontL(TFontSpec(_L("Courier"), fontstwips[cfontsize]));
  453.         cscreenwidth = (iControl->Size().iWidth/iControl->CharSize().iWidth) - 1;
  454.         cscreenheight = iControl->Size().iHeight/iControl->CharSize().iHeight;
  455.         DrawUserScreen();
  456.         break;
  457.         }
  458.         // Show toolbar command
  459.     case EEikCmdShowToolbar:
  460.         // Screen too small for a Toolbar
  461.         if (consolepixelwidth < 600) break;
  462.         {
  463.          if(storyrunning)
  464.           {
  465.           iEikonEnv->InfoMsg(_L("Terminate active story first"));
  466.           return;
  467.           }
  468.         ctoolbar = (ctoolbar ? 0 : 1);
  469.         delete iCons;
  470.         iCons = new(ELeave) CEikConsoleScreen;
  471.         if(ctoolbar)
  472.             MakeToolBarVisible(ETrue);
  473.         else {
  474.             MakeToolBarVisible(EFalse);
  475.         }
  476.         iCons->ConstructL(_L("FrotzConsole"), TSize((ctoolbar ? (consolepixelwidth-70): consolepixelwidth),consolepixelheight), 0, EEikConsWinInPixels);
  477.         iControl=iCons->ConsoleControl();
  478.         iControl->SetFocus(ETrue,EDrawNow);
  479.         iCons->HideCursor();
  480.         iCons->SetFontL(TFontSpec(_L("Courier"), fontstwips[cfontsize]));
  481.         cscreenwidth = (iControl->Size().iWidth/iControl->CharSize().iWidth) - 1;
  482.         cscreenheight = iControl->Size().iHeight/iControl->CharSize().iHeight;
  483.         DrawUserScreen();
  484.         break;
  485.         }
  486.     case EEikCmdTerminate:
  487.           {
  488.           TKeyEvent iKeyEvent;
  489.           if(!storyrunning)
  490.             {
  491.             iEikonEnv->InfoMsg(_L("No active story"));
  492.             return;
  493.             }
  494.           CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  495.           if(!dialog->ExecuteLD(R_EXAMPLE_KILL_DIALOG))
  496.             return;
  497.           if(keytimer)
  498.             {
  499.             keytimer->Cancel();
  500.             delete keytimer;
  501.             keytimer = NULL;
  502.             }
  503.           waitingkey = 0;
  504.             FrotzThread.Terminate(0);
  505.           while(iKeyEventQ->Count() > 0)
  506.               iKeyEventQ->Remove(&iKeyEvent);
  507.           keyready = 0;
  508.           waitingkey = 0;
  509.           storyrunning = 0;
  510.             cattr = 0;
  511.           iCons->HideCursor();
  512.           iCons->SetAtt(cattr);
  513.           DrawUserScreen();
  514.         
  515.           break;
  516.           }
  517.     case EEikCmdOpenStory:
  518.         {
  519.         TKeyEvent iKeyEvent;
  520.         if(storyrunning)
  521.           {
  522.           CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  523.           if(!dialog->ExecuteLD(R_EXAMPLE_KILL_DIALOG))
  524.             return;
  525.           if(keytimer)
  526.             {
  527.             keytimer->Cancel();
  528.             delete keytimer;
  529.             keytimer = NULL;
  530.             }
  531.           waitingkey = 0;
  532.             FrotzThread.Terminate(0);
  533.           while(iKeyEventQ->Count() > 0)
  534.               iKeyEventQ->Remove(&iKeyEvent);
  535.           keyready = 0;
  536.           waitingkey = 0;
  537.           storyrunning = 0;
  538.           cattr = 0;
  539.           iCons->HideCursor();
  540.           iCons->SetAtt(cattr);
  541.           DrawUserScreen();
  542.           }
  543.         cattr = 0;
  544.         iCons->HideCursor();
  545.         iCons->SetAtt(cattr);
  546.         thestoryname[0] = '\0';
  547.         if(DlgOpen(storypath, thestoryname))
  548.             {
  549.             TInt err;
  550.             TBuf<20> threadName;
  551.             TInt num=0;
  552.              int len;
  553.             TFileName fpath;
  554.             fpath = EikFileUtils::DriveAndPathFromFullName(_L(thestoryname));
  555.             len = fpath.Length();
  556.             Srvmemcpy(storypath, (void *)fpath.Ptr(), len);
  557.             iControl->SetFocus(ETrue,EDrawNow);
  558.             do
  559.                 {
  560.                 threadName.Format(_L("FR%02d"),num++); // !! review the title
  561.                 err=FrotzThread.Create(threadName,FrotzStartFunction, 70000,1000, 2000000, &FrotzThread, EOwnerProcess);
  562.                 } while(err==KErrAlreadyExists);
  563.             if (!err)
  564.                 {
  565.                 FrotzThread.Resume();
  566.                 }
  567.             }
  568.         break;
  569.         }
  570.     case EEikCmdExit:
  571.         {
  572.         if(storyrunning)
  573.           {
  574.           TKeyEvent iKeyEvent;
  575.           CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  576.           if(!dialog->ExecuteLD(R_EXAMPLE_KILL_DIALOG))
  577.             return;
  578.           waitingkey = 0;
  579.           FrotzThread.Terminate(0);
  580.           waitingkey = 0;
  581.           while(iKeyEventQ->Count() > 0)
  582.               iKeyEventQ->Remove(&iKeyEvent);
  583.           storyrunning = 0;
  584.           }
  585.         UpdateProfile();
  586.         CBaActiveScheduler::Exit();
  587.         break;
  588.         }
  589.     case EEikCmdBoldMode:
  590.         {
  591.         cboldmode = (cboldmode ? 0 : 1);
  592.         if(storyrunning)
  593.           boldtoggled = 1;
  594.         else
  595.           DrawUserScreen();
  596.         break;
  597.         }
  598.     case EEikCmdAbout:
  599.         {
  600.         CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  601.         dialog->ExecuteLD(R_EXAMPLE_ABOUT_DIALOG);
  602.         }
  603.         break;
  604.     default :
  605.         break;
  606.         }
  607.     }
  608.  
  609. void CFrotzAppUi::ConstructL()
  610.         {
  611.     Srvstrcpy(storypath, "C:\\Frotz\\Story\\");
  612.     Srvstrcpy(savepath, "C:\\Frotz\\Save\\");
  613.     cfontsize = 1;
  614.     ctoolbar = 1;
  615.     cboldmode = 1;
  616.     // Screen size in pixels 
  617.     consolepixelwidth = iCoeEnv->ScreenDevice()->SizeInPixels().iWidth;
  618.     consolepixelheight = iCoeEnv->ScreenDevice()->SizeInPixels().iHeight;
  619.     ReadProfile();
  620.     cattr = 0;
  621.     storyrunning = 0;
  622.     keyready = 0;
  623.     keytimer = NULL;
  624.     waitingkey = 0;
  625.     BaseConstructL();
  626.     SemaKey.CreateLocal(0);
  627.     iMainSvr = CMainServServer::New(this);
  628.     // notify the kernel that a server has started.
  629.     #if defined (__WINS__)
  630.     UserSvr::ServerStarted();
  631.     #endif
  632.     // all well
  633.     iCons = new(ELeave) CEikConsoleScreen;
  634.     if(ctoolbar && consolepixelwidth >= 600)
  635.         MakeToolBarVisible(ETrue);
  636.     else {
  637.         MakeToolBarVisible(EFalse);
  638.         ctoolbar = 0;
  639.     }
  640.     CEikFileNameLabel* filenameLabel=(CEikFileNameLabel*) iToolBar->ControlById(EFrotzCmdFileName);
  641.     filenameLabel->UpdateL();
  642.     filenameLabel->DrawNow();
  643.     iKeyEventQ=new(ELeave) CCirBuf<TKeyEvent>;
  644.     iKeyEventQ->SetLengthL(40);     // buffer length, too high? too low?
  645.     iCons->ConstructL(_L("FrotzConsole"), TSize((ctoolbar ? (consolepixelwidth-70): consolepixelwidth),consolepixelheight), 0, EEikConsWinInPixels);
  646.     iControl=iCons->ConsoleControl();
  647.     iControl->SetFocus(ETrue,EDrawNow);
  648.     iCons->HideCursor();
  649.     iCons->SetFontL(TFontSpec(_L("Courier"), fontstwips[cfontsize]));
  650.     cscreenwidth = (iControl->Size().iWidth/iControl->CharSize().iWidth) - 1;
  651.     cscreenheight = iControl->Size().iHeight/iControl->CharSize().iHeight;
  652.     DrawUserScreen();
  653.     }
  654.  
  655.  
  656. CFrotzAppUi::~CFrotzAppUi()
  657.     {
  658.     if(keytimer)
  659.       {
  660.       keytimer->Cancel();
  661.       delete keytimer;
  662.       }
  663.     delete iCons;
  664.     delete iMainSvr;
  665.     delete iKeyEventQ;
  666.     }
  667.  
  668.  
  669. void CFrotzAppUi::MakeToolBarVisible(TBool aVisible)
  670.     {
  671.     iToolBar->MakeVisible(aVisible);
  672.     }
  673.  
  674.  
  675. // needed for setting radio button in menu pane
  676. void CFrotzAppUi::DynInitMenuPaneL(TInt aMenuId,CEikMenuPane* aMenuPane)
  677.     {
  678.     switch (aMenuId)
  679.         {
  680.     case R_EXAMPLE_VIEW_MENU: // Identify the menu pane using its ID
  681.         {
  682.         if (consolepixelwidth < 600) 
  683.             aMenuPane->SetItemDimmed(EEikCmdShowToolbar,ETrue);
  684.         break;
  685.         }
  686.  
  687.     default:
  688.         break;
  689.     
  690.         } //End of switch
  691.     }
  692.  
  693. void CFrotzAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
  694.     {
  695.     TKeyEvent keyev = aKeyEvent;
  696.     if(aType != EEventKey)
  697.       return;
  698.     if((keyev.iCode == 8) && (keyev.iModifiers & EModifierShift))
  699.         keyev.iCode = 0x1006;
  700.     if(keyev.iModifiers & EModifierCtrl)
  701.     {
  702.         if(keyev.iScanCode == EStdKeyRightArrow)
  703.             keyev.iCode = 0x1000;
  704.         else
  705.         if(keyev.iScanCode == EStdKeyLeftArrow)
  706.             keyev.iCode = 0x1001;
  707.     }
  708.  
  709.     if(iKeyEventQ->Add(&keyev) == 0)
  710.         CEikonEnv::Beep();
  711.     keyready = 1;
  712.  
  713.     // Clear timer if it is running
  714.     if(keytimer)
  715.     {
  716.             keytimer->Cancel();
  717.             delete keytimer;
  718.             keytimer = NULL;
  719.     }
  720.     // client may be waiting on this key event
  721.     if(waitingkey) 
  722.     {
  723.         waitingkey = 0; 
  724.         SemaKey.Signal();
  725.     }
  726.     }
  727.  
  728. ////////////////////////////////////////////////////////////////////////////////////////
  729. //                            CFrotzDocument
  730. ////////////////////////////////////////////////////////////////////////////////////////
  731.  
  732.  
  733. class CFrotzDocument : public CEikDocument
  734.     {
  735. public:
  736.     CFrotzDocument(CEikApplication& aApp): CEikDocument(aApp) { }
  737. private: // from CApaDocument
  738.     CEikAppUi* CreateAppUiL();
  739.     };
  740.  
  741.  
  742. CEikAppUi* CFrotzDocument::CreateAppUiL()
  743.     {
  744.         return(new(ELeave) CFrotzAppUi);
  745.     }
  746.  
  747.  
  748.  
  749. ////////////////////////////////////////////////////////////////////////////////////////
  750. //                            CFrotzApplication
  751. ////////////////////////////////////////////////////////////////////////////////////////
  752.  
  753.  
  754. class CFrotzApplication : public CEikApplication
  755.     {
  756. private: // from CApaApplication
  757.     CApaDocument* CreateDocumentL();
  758.     TUid AppDllUid() const;
  759.     };
  760.  
  761.  
  762. const TUid KUidTstApp={0x1000055B};
  763.  
  764.  
  765. TUid CFrotzApplication::AppDllUid() const
  766.     {
  767.     return(KUidTstApp);
  768.     }
  769.  
  770.  
  771. CApaDocument* CFrotzApplication::CreateDocumentL()
  772.     {
  773.     return(new(ELeave) CFrotzDocument(*this));
  774.     }
  775. //
  776. // EXPORTed functions
  777. //
  778.  
  779. EXPORT_C CApaApplication* NewApplication()
  780.         {
  781.     return(new CFrotzApplication);
  782.     }
  783.  
  784.  
  785. GLDEF_C TInt E32Dll(TDllReason)
  786.     {
  787.     return(KErrNone);
  788.     }
  789.  
  790. /////////////////////////////////////////////////////////
  791. // FrotzThread
  792.  
  793. TInt FrotzStartFunction(TAny* aParam)
  794.     {
  795.     struct sg *pg = new (struct sg);
  796.     pg->ss= new RMainServ();
  797.     SrvConnect(pg, aParam);
  798.     char *argv[] = {"frotzs5", "123456789.123"};
  799.     frotzmain(2, argv, pg);
  800.     SrvDisconnect(pg);
  801.     return(0);
  802.     }
  803.  
  804. const TUid PROFILE_UID={0x1000055C};
  805.  
  806. void CFrotzAppUi::UpdateProfile()
  807.     {
  808.     int cflags1 = 0,cflags2 = 0;
  809.     int cflags3 = 0,cflags4 = 0;
  810.       // Open the "ini" file. Application architecture creates the "ini" file
  811.       // if it does not exist
  812.     CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  813.  
  814.       // The stream containing the background preference will be associated
  815.       // with a Uid. AssignL() creates the stream if it does not exist
  816.     RDictionaryWriteStream writeStream;
  817.     writeStream.AssignLC(*iniFile,PROFILE_UID);
  818.     writeStream.WriteInt32L(cfontsize);
  819.     writeStream.WriteInt32L(ctoolbar);
  820.     if(cboldmode)
  821.       cflags1 |= 1;
  822.     writeStream.WriteInt32L(cflags1);
  823.     writeStream.WriteInt32L(cflags2);
  824.     writeStream.WriteInt32L(cflags3);
  825.     writeStream.WriteInt32L(cflags4);
  826.     writeStream.WriteL((TUint8 *)storypath, 128);
  827.     writeStream.WriteL((TUint8 *)savepath, 128);
  828.     writeStream.CommitL();
  829.     CleanupStack::PopAndDestroy();
  830.      
  831.     // Commit changes to the store
  832.     if (iniFile->Commit()!=KErrNone)
  833.         iniFile->RevertL(); 
  834.  
  835.       // Destroys the "ini" file store object and closes
  836.       // the "ini" file
  837.     CleanupStack::PopAndDestroy(); // iniFile
  838.     }
  839.  
  840. void CFrotzAppUi::ReadProfile()
  841.     {
  842.     char storyp[128];
  843.     char savep[128];
  844.     int cf, ct;
  845.     int cflags1,cflags2;
  846.     int cflags3,cflags4;
  847.       // Open the "ini" file. Application architecture creates the "ini" file
  848.       // if it does not exist
  849.     CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  850.     
  851.       // The stream containing the background preference will be associated
  852.       // with a Uid.
  853.       // Open the stream associted with the Uid; it will be created
  854.       // if it does not exist.
  855.     RDictionaryReadStream readStream;
  856.     readStream.OpenL(*iniFile,PROFILE_UID);
  857.  
  858.       // If the stream has just been created (because the "ini" file has just
  859.       // been created), it will be "empty" and attempting to read from 
  860.       // the stream will leave with a KErrEof.
  861.       // If the stream is not "empty", background now contains the
  862.       // value stored.
  863.     TRAPD(err, cf = readStream.ReadInt32L());
  864.     if (!err)
  865.       TRAPD(err, ct = readStream.ReadInt32L());
  866.     if (!err)
  867.       {
  868.         cfontsize = cf;
  869.         ctoolbar = ct;
  870.       }
  871.     if (!err)
  872.       TRAPD(err, cflags1 = readStream.ReadInt32L());
  873.     cboldmode = cflags1 & 1;
  874.       
  875.     if (!err)
  876.       TRAPD(err, cflags2 = readStream.ReadInt32L());
  877.     if (!err)
  878.       TRAPD(err, cflags3 = readStream.ReadInt32L());
  879.     if (!err)
  880.       TRAPD(err, cflags4 = readStream.ReadInt32L());
  881.     if (!err)
  882.       TRAPD(err, readStream.ReadL((TUint8 *)storyp, 128));
  883.     if (!err)
  884.       TRAPD(err, readStream.ReadL((TUint8 *)savep, 128));
  885.     readStream.Close();
  886.  
  887.       // Can now delete the "ini" file object (i.e. close the "ini" file)
  888.     CleanupStack::PopAndDestroy();
  889.      
  890.     if (!err)
  891.       {
  892.         Srvstrcpy(storypath, storyp);
  893.         Srvstrcpy(savepath, savep);
  894.       }
  895.       // Return the value retrieved from the "ini" file
  896.     }
  897.  
  898. void CFrotzAppUi::DrawUserScreen()
  899. {
  900.     int msw = cscreenwidth /2;
  901.     int msh = cscreenheight /2;
  902.     iCons->ClearScreen();
  903.     iCons->SetCursorPosAbs(TPoint(msw - 16, msh - 5));
  904.     if(cboldmode)
  905.       iCons->SetAtt(ATT_BOLD | ATT_UNDERLINE);
  906.     else
  907.       iCons->SetAtt(ATT_ITALIC);
  908.     iCons->Write(_L("FrotzS5 v1.5 by FrÈdÈric Bouvry"));
  909.     iCons->SetCursorPosAbs(TPoint(msw - 14, msh - 3));
  910.     if(cboldmode)
  911.       iCons->SetAtt(ATT_BOLD);
  912.     else
  913.       iCons->SetAtt(0);
  914.     iCons->Write(_L("You may now select your font"));
  915.     iCons->SetCursorPosAbs(TPoint(msw - 14, msh - 2));
  916.     iCons->Write(_L("size and toggle the toolbar"));
  917.     iCons->SetCursorPosAbs(TPoint(msw - 13, msh-1));
  918.     iCons->Write(_L("by using the zoom buttons,"));
  919.     iCons->SetCursorPosAbs(TPoint(msw - 9, msh));
  920.     iCons->Write(_L("Ctrl-t and Ctrl-b"));
  921.     iCons->SetCursorPosAbs(TPoint(msw - 7, msh + 2));
  922.     if(cboldmode)
  923.       {
  924.       iCons->SetAtt(ATT_BOLD | ATT_ITALIC);
  925.       iCons->Write(_L("Bold mode On"));
  926.       }
  927.     else
  928.       {
  929.       iCons->SetAtt(ATT_BOLD);
  930.       iCons->Write(_L("Bold mode Off"));
  931.       }
  932.     iCons->SetCursorPosAbs(TPoint(msw - 18, msh + 4));
  933.     if(cboldmode)
  934.       iCons->SetAtt(ATT_BOLD);
  935.     else
  936.       iCons->SetAtt(0);
  937.     iCons->Printf(_L("Current console width/height: %dx%d"), cscreenwidth, cscreenheight);
  938.     iCons->SetAtt(0);
  939. }
  940.