home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IFB / STRINGBU.C < prev    next >
C/C++ Source or Header  |  1992-02-04  |  4KB  |  135 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.  * a RadioButton with an editable value
  25.  */
  26.  
  27. #include "stringbu.h"
  28.  
  29. #include <InterViews/event.h>
  30. #include <InterViews/font.h>
  31. #include <InterViews/painter.h>
  32. #include <InterViews/shape.h>
  33. #include <InterViews/textdisplay.h>
  34. #include <ctype.h>
  35. #include <string.h>
  36.  
  37. StringButton::StringButton (
  38.     ButtonState* s, const char* sample
  39. ) : (sample, s, nil) {
  40.     Init();
  41. }
  42.  
  43. StringButton::StringButton (
  44.     const char* name, ButtonState* s, const char* sample
  45. ) : (sample, s, nil) {
  46.     SetInstance(name);
  47.     Init();
  48. }
  49.  
  50. StringButton::~StringButton () {
  51.     delete text;
  52.     delete display;
  53. }
  54.  
  55. void StringButton::Init () {
  56.     text = new char[1000];
  57.     text[0] = '\0';
  58.     value = (void*)text;
  59.     display = new TextDisplay();        
  60.     display->ReplaceText(0, text, strlen(text));
  61.     display->CaretStyle(NoCaret);
  62.     offset = 0;
  63. }
  64.  
  65. void StringButton::Reconfig () {
  66.     RadioButton::Reconfig();
  67.     shape->width += 4;
  68.     shape->height += 4;
  69. }
  70.  
  71. void StringButton::Resize () {
  72.     int h = output->GetFont()->Height();
  73.     offset = h;
  74.     display->LineHeight(h);
  75.     display->Resize(offset+2, 2, xmax-2, ymax-2);
  76. }
  77.  
  78. void StringButton::Handle (Event& e) {
  79.     if (e.eventType == DownEvent && e.x > ymax) {
  80.         hit = true;
  81.         Refresh();
  82.         boolean done = false;
  83.         int p = strlen(text);
  84.         display->Draw(output, canvas);
  85.         display->CaretStyle(BarCaret);
  86.         do {
  87.             switch (e.eventType) {
  88.             case KeyEvent:
  89.                 char c = e.keystring[0];
  90.                 if (e.len > 0) {
  91.                     if (c == '\015') {
  92.                         done = true;
  93.                     } else if (c == '\177') {
  94.                         if (p > 0) {
  95.                             --p;
  96.                             text[p] = '\0';
  97.                             display->DeleteText(0, p, 1);
  98.                         }
  99.                     } else if (!iscntrl(c)) {
  100.                         text[p] = c;
  101.                         display->InsertText(0, p, &c, 1);
  102.                         ++p;
  103.                         text[p] = '\0';
  104.                     }
  105.                 }
  106.                 break;
  107.             case DownEvent:
  108.                 if (e.target != this) {
  109.                     UnRead(e);
  110.                     done = true;
  111.                 }
  112.                 break;
  113.             }
  114.             display->Caret(0, p);
  115.             if (!done) {
  116.                 Read(e);
  117.             }
  118.         } while (!done);
  119.         display->CaretStyle(NoCaret);
  120.         hit = false;
  121.         Refresh();
  122.         subject->SetValue(value);
  123.         subject->Notify();
  124.     } else {
  125.         RadioButton::Handle(e);
  126.     }
  127. }
  128.  
  129. void StringButton::Redraw (Coord l, Coord b, Coord r, Coord t) {
  130.     display->Draw(output, canvas);
  131.     display->Redraw(l, b, r, t);
  132.     output->Rect(canvas, offset, 0, xmax, ymax);
  133.     Refresh();
  134. }
  135.