home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch10 / WordCountWindow.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.6 KB  |  209 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. /////////////////////////////////////////////////////////////
  22. // WordCountWindow.C: Test the InterruptibleCmd class
  23. /////////////////////////////////////////////////////////////
  24. #include "Application.h"
  25. #include "InfoDialogManager.h"
  26. #include "WordCountWindow.h"
  27. #include "CountWordsCmd.h"
  28. #include "SelectFileCmd.h"
  29. #include "QuitCmd.h"
  30. #include "MenuBar.h"
  31. #include "CmdList.h"
  32. #include <Xm/List.h>
  33. #include <X11/cursorfont.h>
  34. #include <stdio.h>
  35.  
  36. WordCountWindow::WordCountWindow ( char *name ) : MenuWindow ( name )
  37. {
  38.     // Empty
  39. }
  40.  
  41. Widget WordCountWindow::createWorkArea ( Widget parent )
  42. {
  43.     _list = XmCreateScrolledList ( parent, "list", NULL, 0 );
  44.     
  45.     XtManageChild ( _list );
  46.     
  47.     return XtParent ( _list );
  48. }
  49.  
  50. void WordCountWindow::createMenuPanes()
  51. {
  52.     // Create the command objects for this menu
  53.     
  54.     Cmd  *quit       = new QuitCmd ( "Quit" , TRUE );
  55.     Cmd  *selectFile = 
  56.     new SelectFileCmd ( "selectFile" , 
  57.                TRUE, 
  58.                &WordCountWindow::countWordsCallback,
  59.                (void *) this );
  60.     
  61.     // Create a list of commands and install it in a menu pane
  62.     
  63.     CmdList  *cmdList = new CmdList();
  64.     cmdList->add ( selectFile );
  65.     cmdList->add ( quit );
  66.     _menuBar->addCommands ( cmdList, "Application" );
  67. }
  68.  
  69. void WordCountWindow::countWordsCallback ( void *clientData, 
  70.                       char *filename )
  71. {
  72.     WordCountWindow *obj = (WordCountWindow *) clientData;
  73.     
  74.     obj->countWords( filename );
  75. }
  76.  
  77. void WordCountWindow::countWords ( char * filename)
  78. {
  79.     if ( !filename )  // Catch NULL file names
  80.     return;
  81.     
  82.     // Instantiate a Cmd object to count words in the given file
  83.     
  84.     CountWordsCmd *task = new CountWordsCmd ( "countWords",
  85.                          TRUE, filename );
  86.     
  87.     setBusyCursor();  // Display a busy cursor
  88.     
  89.     XtVaSetValues ( _list,   // Remove any items currently in the list
  90.            XmNitems,     NULL,
  91.            XmNitemCount, 0,
  92.            NULL );
  93.     
  94.     // Execute the cmd, providing a function to be called when finished
  95.     
  96.     task->execute ( &WordCountWindow::taskFinishedCallback , 
  97.            (void *) this );
  98. }
  99.  
  100. void WordCountWindow::taskFinishedCallback ( InterruptibleCmd *cmd, 
  101.                         Boolean      interrupted,
  102.                         void        *clientData)
  103. {
  104.     CountWordsCmd   *cwObj = (CountWordsCmd *)   cmd;
  105.     WordCountWindow *obj   = (WordCountWindow *) clientData;
  106.     
  107.     // If the user interrupted the task, just confirm the interrupt.
  108.     // Otherwise, call taskFinished() to process the results.
  109.     
  110.     if ( interrupted )
  111.     theInfoDialogManager->post ("Interrupted!" );
  112.     else
  113.     obj->taskFinished ( cwObj );
  114.     
  115.     // Complete the operation by restoring a normal cursor and
  116.     // freeing the InterruptibleCmd object.
  117.     
  118.     obj->setNormalCursor();
  119.     delete cwObj;
  120. }
  121.  
  122. void WordCountWindow::taskFinished ( CountWordsCmd *cwObj )
  123. {
  124.     int       i;
  125.     char      buf[100];
  126.     XmString *xmstrList;
  127.     
  128.     // Report the number of unique words.
  129.     
  130.     sprintf ( buf, "This file contains %d unique words.",
  131.          cwObj->numWords());
  132.     
  133.     theInfoDialogManager->post ( buf );
  134.     
  135.     // Create an array of compound strings large
  136.     // enough to hold the results
  137.     
  138.     xmstrList = new XmString[cwObj->numWords()];
  139.     
  140.     // Retrieve each word, format the results, and
  141.     // add an entry to the compound string array 
  142.     
  143.     for ( i = 0; i < cwObj->numWords(); i++ )
  144.     {
  145.     char buf[BUFSIZ];
  146.     
  147.     sprintf ( buf, 
  148.          "%-5d %s",
  149.          cwObj->getCount(i), cwObj->getWord(i) );
  150.     xmstrList[i] = XmStringCreateSimple ( buf );
  151.     }
  152.     
  153.     // Display the array of compound strings in the list  
  154.     
  155.     XtVaSetValues ( _list,
  156.            XmNitems, xmstrList,
  157.            XmNitemCount, cwObj->numWords(),
  158.            NULL );
  159.     
  160.     // The XmList widget makes its own copy of the compound strings
  161.     // so free all local copies.
  162.     
  163.     for ( i = 0; i < cwObj->numWords(); i++ )
  164.     XmStringFree ( xmstrList[i] );
  165.     
  166.     delete []xmstrList;
  167. }
  168.  
  169. void WordCountWindow::setBusyCursor()
  170. {
  171.     // Do nothing if the widget has not been realized
  172.     
  173.     if ( XtIsRealized ( _w ) )
  174.     {
  175.     // If this is the first time, create the busy cursor
  176.     
  177.     if ( !_busyCursor )
  178.         _busyCursor = XCreateFontCursor ( XtDisplay ( _w ),
  179.                          XC_watch );
  180.     
  181.     // Install the busy cursor for this window's top-level shell
  182.     
  183.     XDefineCursor ( XtDisplay ( _w ),
  184.                XtWindow ( _w ),
  185.                _busyCursor );
  186.     }    
  187. }
  188.  
  189. void WordCountWindow::setNormalCursor()
  190. {
  191.     // Do nothing if the widget has not been realized
  192.     
  193.     if ( XtIsRealized ( _w ) )
  194.     {
  195.     // If this is the first time, create the busy cursor
  196.     
  197.     if ( !_normalCursor )
  198.         _normalCursor = XCreateFontCursor ( XtDisplay ( _w ),
  199.                            XC_left_ptr );
  200.     
  201.     // Install the left pointer cursor as the normal
  202.     // cursor for this window's top-level shell
  203.     
  204.     XDefineCursor ( XtDisplay ( _w ),
  205.                XtWindow ( _w ),
  206.                _normalCursor );
  207.     }
  208. }
  209.