home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / samples / sample15 / sample15.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-02  |  5.7 KB  |  191 lines

  1. #include "sample15.h"
  2.  
  3. #include <stdlib.h>
  4.  
  5.  
  6. class MyClient;
  7. class MyMLE: public XMultiLineEdit
  8. {
  9.    public:
  10.       MyClient * owner;   //pointer to the parent
  11.       MyMLE( MyClient * w): XMultiLineEdit( (XWindow*) w, XRect(), 0, MLE_HORZSCROLL | MLE_VERTSCROLL | WIN_VISIBLE)
  12.             { owner = w; }
  13.       void DoControl(XControlEvent*);
  14.       void FocusChanged(BOOL set); //overwrite function to inform the server that we have the focus
  15. };
  16.  
  17.  
  18. class MyClient: public XMDIClient
  19. {
  20.         friend class MyMLE;
  21.                 BOOL loading, saved;
  22.                 MyMLE * mle;
  23.    public:
  24.       //constructor for the mdi-client
  25.       MyClient(XMDIServer * p, char * title=""):XMDIClient(p, IDM_MENU, title, XFrameWindow::defaultStyle )
  26.                {
  27.          mle = new MyMLE(this);    //create a multiline-edit
  28.          SetClient(mle);          //set it as client-window
  29.          saved = TRUE;
  30.          loading = FALSE;
  31.                         XRect r;
  32.                         p->GetClientSize(&r);
  33.                         SetSize( &r );
  34.       }
  35.       virtual void UpdateMenu( XMenuBar * );   //overwrite the method to update the menu
  36.       void Load( char * );
  37. };
  38.  
  39.  
  40. // function to inform the server that we have the focus
  41. void MyMLE :: FocusChanged(BOOL set)
  42. {
  43.    if(set) owner->FocusChanged(set);
  44. }
  45.  
  46.  
  47. void MyMLE :: DoControl( XControlEvent * event )
  48. {
  49.         //ask for the id of the sending window and the type of event
  50.         if( event->GetEventID() == WIN_CHANGED && owner->loading == FALSE)
  51.         {
  52.                 //sender is the MLE, event is that the text will be changed
  53.                 if( owner->saved == TRUE)
  54.                 {
  55.                         XString s;
  56.                         owner->GetText( &s );
  57.                         s += " - changed";
  58.                         owner->SetText( s );
  59.                         owner->saved = FALSE;
  60.                 }
  61.         }
  62. }
  63.  
  64.  
  65. void MyClient :: Load( char * p)
  66. {
  67.    XFile loadfile;
  68.    loading = TRUE;
  69.  
  70.    /* now open the file, fail if given filename is not existing */
  71.    /* open the file for read-access, dont allow any other programm to use the file while it is open*/
  72.    if( loadfile.Open( p, XFILE_FAIL_IF_NEW | XFILE_OPEN_EXISTING, XFILE_SHARE_DENYWRITE | XFILE_READONLY ) == 0)
  73.    {
  74.       XString s;
  75.  
  76.       //how large is the file?
  77.       XFileInfo info;
  78.       loadfile.GetFileInfo( &info );
  79.       LONG size = info.GetFileSize();
  80.       //read the complete file
  81.       loadfile.Read ( (PVOID) s.GetBuffer(info.GetFileSize() + 1), size);
  82.       s.ReleaseBuffer( info.GetFileSize() );
  83.  
  84.       //set the XString content to the mle
  85.       mle->SetText( s );
  86.       //dont∩forget to close the file
  87.       loadfile.Close();
  88.       mle->SetFocus();
  89.    }
  90.    else
  91.       XMessageBox( p, "couldn∩t open File!", MB_OK|MB_ERROR);
  92.    loading = FALSE;
  93. }
  94.  
  95.  
  96. BOOL MyServer :: DoCommand( LONG command)
  97. {
  98.    switch( command )
  99.       {
  100.          //which menuitem was selected?
  101.          case IDM_OPEN:
  102.             {
  103.                /*File/Open selected */
  104.                /* display the file-dialog defined by the system */
  105.                /* set the file-suffix and title of the dialog */
  106.                XFileDialog fileDlg(this, "*.*", NULL, NULL, FD_OPEN | FD_CENTER | FD_MULTIPLESEL);
  107.  
  108.                /* the user selected a file */
  109.                if( fileDlg.GetCommand() == USER_OK)
  110.                {
  111.                   for(int i = 0; i < fileDlg.GetFileCount(); i++)
  112.                   {
  113.                      MyClient * w = new MyClient( this );
  114.                      XString name;
  115.                      fileDlg.GetFileName(&name, i);
  116.                      w->Load(name);
  117.                      w->SetText(name);
  118.                   }
  119.                }
  120.              }
  121.              break;
  122.          case IDM_SAVEAS:
  123.             {
  124.                /*File/Open selected */
  125.                /* display the file-dialog defined by the system */
  126.                /* set the file-suffix and title of the dialog */
  127.                XFileDialog fileDlg(this, "*.TXT", NULL, NULL, FD_SAVEAS | FD_CENTER );
  128.  
  129.                /* the user selected a file */
  130.                if( fileDlg.GetCommand() == USER_OK)
  131.                  {
  132.                     XString fileName;
  133.                     fileDlg.GetFileName( &fileName);  //get filename and path where to save
  134.                     //perfor your save_as code here
  135.                  }
  136.             }
  137.             break;
  138.  
  139.          default:
  140.             return XMDIServer::DoCommand(command);
  141.       }
  142.    return TRUE;
  143. }
  144.  
  145.  
  146. // when we have the focus we can here manipulate the menu of the MDI-Server
  147. void MyClient :: UpdateMenu( XMenuBar * menu)
  148. {
  149.    menu->EnableItem(IDM_CLOSE);
  150.    if( saved == FALSE)
  151.    {
  152.       menu->EnableItem(IDM_SAVE);
  153.       menu->EnableItem(IDM_SAVEAS);
  154.    }
  155.    else
  156.    {
  157.       menu->EnableItem(IDM_SAVE, FALSE);
  158.       menu->EnableItem(IDM_SAVEAS, FALSE);
  159.    }
  160.    menu->EnableItem(IDM_PRINT);
  161. }
  162.  
  163.  
  164. // no client-window is open, reset the menu
  165. void MyServer :: SetEmptyMenu(XMenuBar * menu)
  166. {
  167.    menu->EnableItem(IDM_CLOSE, FALSE);
  168.    menu->EnableItem(IDM_SAVE, FALSE);
  169.    menu->EnableItem(IDM_SAVEAS, FALSE);
  170.    menu->EnableItem(IDM_PRINT, FALSE);
  171. }
  172.  
  173. void main ( int argc, void ** argv)
  174. {
  175.     XApplication::GetApplication()->SetArguments(argc, argv);
  176.  
  177.     MyServer * window = new MyServer();
  178.     window->Init();
  179.  
  180.     for(int i = 0; i < XApplication::GetApplication()->GetArgumentCount(); i++)
  181.     {
  182.         XString buffer;
  183.         XApplication::GetApplication()->GetArgumentValue(i, &buffer);
  184.         MyClient * w = new MyClient( window, buffer);
  185.         w->Load( buffer );
  186.     }
  187.  
  188.     XApplication::GetApplication()->Start(); //dont forget to run the application
  189. }
  190.  
  191.