home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_2.zip / EXAMPLES / ICLASS / ICLASS.C < prev    next >
C/C++ Source or Header  |  1992-03-04  |  17KB  |  673 lines

  1. /*
  2.  * Copyright (c) 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * IClass implementation.
  25.  */
  26.  
  27. #include "classbuffer.h"
  28. #include "classeditor.h"
  29. #include "classinfo.h"
  30. #include "dialogs.h"
  31. #include "globals.h"
  32. #include "iclass.h"
  33.  
  34. #include <InterViews/adjuster.h>
  35. #include <InterViews/border.h>
  36. #include <InterViews/box.h>
  37. #include <InterViews/button.h>
  38. #include <InterViews/filebrowser.h>
  39. #include <InterViews/font.h>
  40. #include <InterViews/frame.h>
  41. #include <InterViews/glue.h>
  42. #include <InterViews/menu.h>
  43. #include <InterViews/message.h>
  44. #include <InterViews/regexp.h>
  45. #include <InterViews/painter.h>
  46. #include <InterViews/scroller.h>
  47. #include <InterViews/sensor.h>
  48. #include <InterViews/strbrowser.h>
  49. #include <InterViews/streditor.h>
  50. #include <InterViews/textbuffer.h>
  51. #include <InterViews/world.h>
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <sys/types.h>
  57. #include <sys/stat.h>
  58.  
  59. /*****************************************************************************/
  60.  
  61. static const float fspace = .375;                // space in cm
  62.  
  63. static const char* NONE = "<none>";
  64.  
  65. static const int BUFFERSIZE = 100;
  66. static const int FILENAMESIZE = 100;
  67. static const int MINTEXTSIZE = 10000;
  68.  
  69. #define BSRCH_LBL "^R"
  70. #define BSRCH_CODE '\022'
  71.  
  72. #define FSRCH_LBL "^S"
  73. #define FSRCH_CODE '\023'
  74.  
  75. #define GOTO_LBL "^N"
  76. #define GOTO_CODE '\016'
  77.  
  78. #define QUIT_LBL "^Q"
  79. #define QUIT_CODE '\021'
  80.  
  81. #define SCAN_LBL "^V"
  82. #define SCAN_CODE '\026'
  83.  
  84. /*****************************************************************************/
  85.  
  86. class Command : public MenuItem {
  87. public:
  88.     Command(const char* lbl, const char* klbl, char kcode, IClass*);
  89.  
  90.     virtual void Do();
  91. private:
  92.     IClass* _iclass;
  93.     char _kcode;
  94. };
  95.  
  96. Command::Command (
  97.     const char* lbl, const char* klbl, char kcode, IClass* s
  98. ) : ((Interactor*) nil) {
  99.     Insert(
  100.        new HBox(
  101.             new Message(lbl, Left, 2, hfil),
  102.             new Message("   ", Center, 2, hfil),
  103.             new Message(klbl, Right, 2, hfil)
  104.         )
  105.     );
  106.     _iclass = s;
  107.     _kcode = kcode;
  108. }
  109.  
  110. void Command::Do () { _iclass->Command(_kcode); }
  111.  
  112. struct MenuInfo {
  113.     const char* _lbl;
  114.     const char* _klbl;
  115.     char _kcode;
  116. };
  117.  
  118. /*****************************************************************************/
  119.  
  120. IClass::IClass (ClassBuffer* cb) {
  121.     Init(cb);
  122.     Visit("");
  123.     _editor->Edit(_text);
  124. }
  125.  
  126. IClass::~IClass () {
  127.     delete _cbuf;
  128.     delete _buf;
  129.     delete _text;
  130.     delete _lastFile;
  131.     _state->Detach(this);
  132.     Unref(_state);
  133.  
  134.     if (_scanner != nil) delete _scanner;
  135.     if (_fwdSearch != nil) delete _fwdSearch;
  136.     if (_bwdSearch != nil) delete _bwdSearch;
  137.     if (_goto != nil) delete _goto;
  138. }
  139.  
  140. void IClass::Init (ClassBuffer* cb) {
  141.     _cbuf = cb;
  142.     _state = new ButtonState;
  143.     _state->Attach(this);
  144.     _curClass = new StringEditor(_state, "");
  145.     _focus = _curClass;
  146.     _classes = new StringBrowser(_state, 5, 24);
  147.     _parents = new StringBrowser(_state, 2, 24);
  148.     _children = new StringBrowser(_state, 5, 24);
  149.     _fileIndic = new MarginFrame(new Message("testing"));
  150.     _editor = new ClassEditor(_state, 24, 80, 8, Outlined);
  151.     _editor->SetScrollAlignment(TopLeft);
  152.  
  153.     _scanner = nil;
  154.     _fwdSearch = nil;
  155.     _bwdSearch = nil;
  156.     _goto = nil;
  157.  
  158.     _buf = nil;
  159.     _text = nil;
  160.     _lastFile = nil;
  161.  
  162.     input = new Sensor;
  163.     input->Catch(KeyEvent);
  164.     input->Reference();
  165.  
  166.     Insert(Interior());
  167.     UpdateClassesBrowser();
  168. }
  169.  
  170. void IClass::Visit (const char* filename) {
  171.     if (_lastFile == nil || strcmp(_lastFile, filename) != 0) {
  172.         class Message* msg = new Message(filename);
  173.         _fileIndic->Insert(msg);
  174.         _fileIndic->Change(msg);
  175.  
  176.         delete _lastFile;
  177.         _lastFile = strdup(filename);
  178.  
  179.         delete _buf;
  180.         delete _text;
  181.  
  182.         FILE* f = nil;
  183.  
  184.         if (strlen(filename) != 0) {
  185.             f = fopen(filename, "r");
  186.         }
  187.  
  188.         if (f != nil) {
  189.             struct stat filestats;
  190.             stat((char*)filename, &filestats);
  191.             _bufsize = max(round(filestats.st_size * 1.2), MINTEXTSIZE);
  192.             _buf = new char[_bufsize];
  193.             char* b = _buf;
  194.             int remaining = _bufsize;
  195.             while (remaining > 1 && fgets(b, remaining, f) != nil) {
  196.                 int l = strlen(b);
  197.                 remaining -= l;
  198.                 b += l;
  199.             }
  200.             fclose(f);
  201.             _text = new TextBuffer(_buf, b-_buf, _bufsize);
  202.  
  203.         } else {
  204.             _bufsize = MINTEXTSIZE;
  205.             _buf = new char[_bufsize];
  206.             _text = new TextBuffer(_buf, 0, _bufsize);
  207.         }
  208.     }
  209. }
  210.  
  211. void IClass::Run () {
  212.     Event e;
  213.     int value;
  214.  
  215.     do {
  216.         Read(e);
  217.         Handle(e);
  218.  
  219.         _state->GetValue(value);
  220.     } while (value != QUIT_CODE);
  221. }
  222.  
  223. void IClass::Handle (Event& e) {
  224.     if (e.eventType == KeyEvent && e.len > 0) {
  225.         char c = e.keystring[0];
  226.  
  227.         if (!Command(c)) {
  228.             _focus->Handle(e);
  229.         }
  230.  
  231.     } else if (e.target != this && e.eventType != KeyEvent) {
  232.         boolean focusable =
  233.             e.target == _classes || e.target == _parents ||
  234.             e.target == _children || e.target == _editor;
  235.  
  236.         if (focusable && e.button == LEFTMOUSE) {
  237.             UnselectCurClass();
  238.             UnselectBrowsers();
  239.             _focus = e.target;
  240.         }
  241.         e.target->Handle(e);
  242.     }
  243. }
  244.  
  245. boolean IClass::Command (char c) {
  246.     boolean executed = true;
  247.  
  248.     switch (c) {
  249.         case BSRCH_CODE:         BackwardSearchCmd(); break;
  250.         case FSRCH_CODE:         ForwardSearchCmd(); break;
  251.         case GOTO_CODE:          GotoCmd(); break;
  252.         case QUIT_CODE:          QuitCmd(); break;
  253.         case SCAN_CODE:          ScanCmd(); break;
  254.         default:                 executed = false; break;
  255.     }
  256.     return executed;
  257. }
  258.  
  259. boolean IClass::ForwardSearch (const char* string) {
  260.     Regexp re(string);
  261.     boolean successful = false;
  262.  
  263.     if (
  264.         _text->ForwardSearch(&re, _editor->Dot()) >= 0
  265.         || _text->ForwardSearch(&re, _text->BeginningOfText()) >= 0
  266.     ) {
  267.         _editor->Select(re.EndOfMatch(), re.BeginningOfMatch());
  268.         successful = true;
  269.     }
  270.     return successful;
  271. }
  272.  
  273. boolean IClass::BackwardSearch (const char* string) {
  274.     Regexp re(string);
  275.     boolean successful = false;
  276.  
  277.     if (
  278.         _text->BackwardSearch(&re, _editor->Dot()) >= 0
  279.         || _text->BackwardSearch(&re, _text->EndOfText()) >= 0
  280.     ) {
  281.         _editor->Select(re.EndOfMatch(), re.BeginningOfMatch());
  282.         successful = true;
  283.     }
  284.     return successful;
  285. }
  286.  
  287. void IClass::UnselectBrowsers () {
  288.     int index;
  289.  
  290.     if ((index = _classes->Selection()) >= 0) {
  291.         _classes->UnselectAll();
  292.  
  293.     } else if ((index = _parents->Selection()) >= 0) {
  294.         _parents->UnselectAll();
  295.  
  296.     } else if ((index = _children->Selection()) >= 0) {
  297.         _children->UnselectAll();
  298.     }
  299. }
  300.  
  301. void IClass::Update () {
  302.     int value;
  303.     _state->GetValue(value);
  304.  
  305.     if (value == '\r') {
  306.         UpdateCurClass();
  307.         UpdateParentBrowser();
  308.         UpdateChildBrowser();
  309.  
  310.     } else if (value == '\t' || value == '\033') {
  311.         SelectCurClass();
  312.         _focus = _curClass;
  313.  
  314.     } else if (value == '\007') {
  315.         Complain();
  316.     }
  317.  
  318.     if (value != QUIT_CODE) {
  319.         UnselectBrowsers();
  320.         _state->SetValue('\0');
  321.     }
  322. }
  323.  
  324. void IClass::UpdateCurClass () {
  325.     int index;
  326.  
  327.     if ((index = _classes->Selection()) >= 0) {
  328.         UpdateCurClass(_classes->String(index));
  329.  
  330.     } else if ((index = _parents->Selection()) >= 0) {
  331.         UpdateCurClass(_parents->String(index));
  332.  
  333.     } else if ((index = _children->Selection()) >= 0) {
  334.         UpdateCurClass(_children->String(index));
  335.  
  336.     } else {
  337.         UpdateCurClass(_curClass->Text());
  338.     }
  339. }
  340.  
  341. void IClass::UpdateCurClass (const char* string) {
  342.     if (strcmp(string, NONE) != 0) {
  343.         if (strcmp(string, _curClass->Text()) != 0) {
  344.             _curClass->Message(string);
  345.         }
  346.         ClassInfo* info = _cbuf->Info(string);
  347.  
  348.         if (info == nil) {
  349.             SelectCurClass();
  350.             Complain();
  351.  
  352.         } else {
  353.             Visit(info->Path());
  354.             int line = info->LineNumber();
  355.         _editor->Edit(_text, _text->LineIndex(line));
  356.             ForwardSearch(info->Name());
  357.             _focus = _editor;
  358.         }
  359.     }
  360. }
  361.  
  362. void IClass::UpdateClassesBrowser () {
  363.     const char* className;
  364.     int i = 0;
  365.  
  366.     _classes->Clear();
  367.  
  368.     for (;;) {
  369.         className = _cbuf->Class(i++);
  370.         if (className == nil) {
  371.             break;
  372.         } else {
  373.             _classes->Append(className);
  374.         }
  375.     }
  376. }
  377.  
  378. void IClass::UpdateParentBrowser () {
  379.     const char* parent;
  380.     int i = 0;
  381.  
  382.     _parents->Clear();
  383.  
  384.     for (;;) {
  385.         parent = _cbuf->Parent(_curClass->Text(), i++);
  386.  
  387.         if (parent == nil) {
  388.             if (i == 1) {
  389.                 _parents->Append(NONE);
  390.             }
  391.             break;
  392.         } else {
  393.             _parents->Append(parent);
  394.         }
  395.     }
  396. }
  397.  
  398. void IClass::UpdateChildBrowser () {
  399.     const char* child;
  400.     int i = 0;
  401.  
  402.     _children->Clear();
  403.  
  404.     for (;;) {
  405.         child = _cbuf->Child(_curClass->Text(), i++);
  406.         if (child == nil) {
  407.             if (i == 1) {
  408.                 _children->Append(NONE);
  409.             }
  410.             break;
  411.         } else {
  412.             _children->Append(child);
  413.         }
  414.     }
  415. }
  416.  
  417. void IClass::SelectCurClass () {
  418.     _curClass->Select(0, strlen(_curClass->Text()));
  419. }
  420.  
  421. void IClass::UnselectCurClass () {
  422.     _curClass->Select(strlen(_curClass->Text()));
  423. }
  424.  
  425. Interactor* IClass::Interior () {
  426.     int space = round(fspace*cm);
  427.     const char* showButton = GetAttribute("showButton");
  428.     Interactor* selector = SelectedClass();
  429.  
  430.     if (showButton != nil && strcmp(showButton, "true") == 0) {
  431.     selector = new HBox(
  432.         selector,
  433.         new HGlue(space, space, 0),
  434.         new PushButton(" Show ", _state, '\r')
  435.     );
  436.     }
  437.  
  438.     return new VBox(
  439.         new HBox(Commands(), new HGlue),
  440.         new HBorder,
  441.         new MarginFrame(
  442.             new VBox(
  443.                 new HBox(
  444.                     Classes(),
  445.                     new HGlue(space, space, 0),
  446.                     new VBox(
  447.             selector,
  448.                         new VGlue(space, space, 0),
  449.                         Parents(),
  450.                         new VGlue(space, space, 0),
  451.                         Children()
  452.                     )
  453.                 ),
  454.                 new VGlue(space, space, 0),
  455.                 Editor()
  456.             ), space, space, 0
  457.         )
  458.     );
  459. }
  460.  
  461. static MenuInfo fileMenu[] = {
  462.     { "Scan Files/Directories...", SCAN_LBL, SCAN_CODE },
  463.     { "Quit", QUIT_LBL, QUIT_CODE },
  464.     { nil }
  465. };
  466.  
  467. static MenuInfo searchMenu[] = {
  468.     { "Forward Search...", FSRCH_LBL, FSRCH_CODE },
  469.     { "Backward Search...", BSRCH_LBL, BSRCH_CODE },
  470.     { "Go to line...", GOTO_LBL, GOTO_CODE },
  471.     { nil }
  472. };
  473.  
  474. static PulldownMenu* MakePulldown(
  475.     const char* name, MenuInfo* item, IClass* ic
  476. ) {
  477.     PulldownMenu* menu = new PulldownMenu(
  478.     new Message(name, Left, round(.1*cm))
  479.     );
  480.  
  481.     for (MenuInfo* i = item; i->_lbl != nil; i++) {
  482.         menu->Include(new Command(i->_lbl, i->_klbl, i->_kcode, ic));
  483.     }
  484.     return menu;
  485. }
  486.  
  487. Interactor* IClass::Commands () {
  488.     MenuBar* menuBar = new MenuBar;
  489.  
  490.     menuBar->Include(MakePulldown("File", fileMenu, this));
  491.     menuBar->Include(MakePulldown("Search", searchMenu, this));
  492.  
  493.     return menuBar;
  494. }
  495.  
  496. Interactor* IClass::Classes () {
  497.     return new VBox(
  498.         new HBox(
  499.             new Message("classes"),
  500.             new HGlue
  501.         ),
  502.         new Frame(
  503.             AddScroller(_classes)
  504.         )
  505.     );
  506. }
  507.  
  508. Interactor* IClass::SelectedClass () {
  509.     return new VBox(
  510.         new HBox(
  511.             new Message("selected class"),
  512.             new HGlue
  513.         ),
  514.         new Frame(new MarginFrame(_curClass, 2))
  515.     );            
  516. }
  517.  
  518. Interactor* IClass::Parents () {
  519.     return new VBox(
  520.         new HBox(
  521.             new Message("parents"),
  522.             new HGlue
  523.         ),
  524.         new Frame(AddScroller(_parents))
  525.     );
  526. }
  527.  
  528. Interactor* IClass::Children () {
  529.     return new VBox(
  530.         new HBox(
  531.             new Message("children"),
  532.             new HGlue
  533.         ),
  534.         new Frame(AddScroller(_children))
  535.     );
  536. }
  537.  
  538. Interactor* IClass::Editor () {
  539.     HBox* fileBox = new HBox(
  540.         new Message("file: "),
  541.         _fileIndic,
  542.         new HGlue
  543.     );
  544.     fileBox->Propagate(false);
  545.  
  546.     return new VBox(
  547.         fileBox,
  548.         new Frame(AddScroller(_editor))
  549.     );
  550. }
  551.  
  552. Interactor* IClass::AddScroller (Interactor* i) {
  553.     return new HBox(
  554.         new MarginFrame(i, 2),
  555.         new VBorder,
  556.         new VBox(
  557.             new UpMover(i, 2),
  558.             new HBorder,
  559.             new VScroller(i),
  560.             new HBorder,
  561.             new DownMover(i, 2)
  562.         )                
  563.     );
  564. }
  565.  
  566. void IClass::InsertDialog (Interactor* dialog) {
  567.     Frame* framedDialog = new ShadowFrame(dialog,3,3);
  568.     World* world = GetWorld();
  569.     Coord x, y;
  570.  
  571.     Align(Center, 0, 0, x, y);
  572.     GetRelative(x, y, world);
  573.     world->InsertTransient(framedDialog, this, x, y, Center);
  574. }
  575.  
  576. void IClass::RemoveDialog (Interactor* dialog) {
  577.     Frame* framedDialog = (Frame*) dialog->Parent();
  578.  
  579.     GetWorld()->Remove(framedDialog);
  580.     framedDialog->Remove(dialog);
  581.     delete framedDialog;
  582. }
  583.  
  584. void IClass::Complain (const char* msg) {
  585.     if (msg == nil) {
  586.         fprintf(stderr, "%c", '\007');
  587.  
  588.     } else {
  589.         AcknowledgeDialog complaint(msg);
  590.  
  591.         InsertDialog(&complaint);
  592.         complaint.Acknowledge();
  593.         RemoveDialog(&complaint);
  594.     }
  595. }
  596.  
  597. void IClass::ScanCmd () {
  598. /*
  599.     if (_scanner == nil) {
  600.         _scanner = new FileDialog("Scan through files/directories:", ".");
  601.     }
  602.     InsertDialog(_scanner);
  603.  
  604.     if (_scanner->Accept()) {
  605.         FileBrowser* browser = _scanner->GetBrowser();
  606.  
  607.         for (int i = 0; i < browser->Selections(); ++i) {
  608.             int index = browser->Selection(i);
  609.             _cbuf->Search(browser->String(index));
  610.         }
  611.     }
  612.     RemoveDialog(_scanner);
  613. */
  614.     AcknowledgeDialog unimplemented("Sorry, scanning not yet implemented.");
  615.     InsertDialog(&unimplemented);
  616.     unimplemented.Acknowledge();
  617.     RemoveDialog(&unimplemented);
  618.  
  619. }
  620.  
  621. void IClass::QuitCmd () { _state->SetValue(QUIT_CODE); }
  622.  
  623. void IClass::ForwardSearchCmd () {
  624.     if (_fwdSearch == nil) {
  625.         _fwdSearch = new StringDialog("Search forward for: ", round(3*inches));
  626.     }
  627.  
  628.     InsertDialog(_fwdSearch);
  629.     
  630.     if (_fwdSearch->Accept()) {
  631.         if (ForwardSearch(_fwdSearch->String())) {
  632.             _editor->ScrollToSelection();
  633.         } else {
  634.             Complain();
  635.         }
  636.     }
  637.     RemoveDialog(_fwdSearch);
  638. }
  639.  
  640. void IClass::BackwardSearchCmd () {
  641.     if (_bwdSearch == nil) {
  642.         _bwdSearch = new StringDialog("Search backward for:", round(3*inches));
  643.     }
  644.  
  645.     InsertDialog(_bwdSearch);
  646.     
  647.     if (_bwdSearch->Accept()) {
  648.         if (BackwardSearch(_bwdSearch->String())) {
  649.             _editor->ScrollToSelection();
  650.         } else {
  651.             Complain();
  652.         }
  653.     }
  654.     RemoveDialog(_bwdSearch);
  655. }
  656.  
  657. void IClass::GotoCmd () {
  658.     if (_goto == nil) {
  659.         int width = output->GetFont()->Width("9999999");
  660.         _goto = new StringDialog("Go to line:", width);
  661.     }
  662.  
  663.     InsertDialog(_goto);
  664.     
  665.     if (_goto->Accept()) {
  666.         const char* string = _goto->String();
  667.         int line = atoi(string);
  668.         _editor->Select(_text->LineIndex(line-1));
  669.         _editor->ScrollToSelection();
  670.     }
  671.     RemoveDialog(_goto);
  672. }
  673.