home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / programming / c / oui / string.cc < prev    next >
C/C++ Source or Header  |  1998-04-23  |  4KB  |  126 lines

  1. // $Id: string.cc 1.4 1998/01/13 20:02:05 dlorre Exp dlorre $
  2. #include <exec/lists.h>
  3. #include <intuition/imageclass.h>
  4. #include <intuition/gadgetclass.h>
  5. #include <libraries/gadtools.h>
  6. #include <string.h>
  7. #include <mydebug.h>
  8.  
  9. #include <proto/dos.h>
  10. #include <proto/intuition.h>
  11.  
  12. #include "gadgets/string.h"
  13. #include "gadgetlist.h"
  14. #include "window.h"
  15. #include "screen.h"
  16.  
  17.  
  18. // ========================================================================
  19. // ==========================  STRING CLASS ===============================
  20. // ========================================================================
  21.  
  22. string::string(gadgetlist *gl,
  23.                void (window::*func)(gadget *, unsigned long, unsigned short),
  24.                const char *title,
  25.                const char *text,
  26.                long max,
  27.                unsigned long flags,
  28.                unsigned long justify) : gadget(gl, func), maxsize(max)
  29. {
  30. long pens, apens ;
  31.  
  32.     pens =  gl->win->ws->xpens[BLACK_PEN] << 8 ;
  33.     apens = gl->win->ws->xpens[WHITE_PEN] << 8 ;
  34.     pens += gl->win->ws->xpens[WHITE_PEN] ;
  35.     apens += gl->win->ws->xpens[BLACK_PEN] ;
  36.     gl->ng->ng_GadgetText = (UBYTE *)title ;
  37.     underkey(title) ;
  38.     gl->ng->ng_Flags = flags ;
  39.  
  40.     // since the font style can be modified several times
  41.     // in a window opening process, this is a reliable way to be sure
  42.     // that the wanted font will be used
  43.  
  44.     gfont = OpenFont(gl->ng->ng_TextAttr) ;
  45.  
  46.     im = (Image *)NewObject(NULL, (UBYTE *)"frameiclass",
  47.         IA_Top,             -2,
  48.         IA_Left,            -3,
  49.         IA_Width,           gl->ng->ng_Width+6,
  50.         IA_Height,          gl->ng->ng_Height+4,
  51.         IA_Recessed,        FALSE,
  52.         IA_EdgesOnly,       TRUE,
  53.         IA_FrameType,       FRAME_RIDGE,
  54.         TAG_DONE) ;
  55.  
  56.     buffer = new char[max+1] ;
  57.  
  58.     gad = gl->gad = (Gadget *)NewObject(NULL, (UBYTE *)"strgclass",
  59.             GA_Top,                 gl->ng->ng_TopEdge,
  60.             GA_Left,                gl->ng->ng_LeftEdge,
  61.             GA_Width,               gl->ng->ng_Width,
  62.             GA_Height,              gl->ng->ng_Height,
  63.             GA_ID,                  id,
  64.             GA_Image,               im,
  65.  
  66.             GA_RelVerify,           TRUE,
  67.  
  68.             GA_Previous,            gl->gad,
  69.             GA_TabCycle,            TRUE,
  70.  
  71.             STRINGA_MaxChars,       max,
  72.             text?STRINGA_TextVal:TAG_IGNORE,        text,
  73.             STRINGA_Pens,           pens,
  74.             STRINGA_ActivePens,     apens,
  75.             STRINGA_Justification,  justify,
  76.             STRINGA_Buffer,         buffer,
  77.             STRINGA_Font,           gfont,
  78.             TAG_END) ;
  79.  
  80.     curstring = new char[max+1] ;
  81.     strcpy(curstring, buffer) ;
  82. }
  83.  
  84. string::~string()
  85. {
  86.     if (gad) DisposeObject(gad) ;
  87.     if (im) DisposeObject(im) ;
  88.     if (buffer) delete buffer ;
  89.     if (curstring) delete curstring ;
  90.     if (gfont) CloseFont(gfont) ;
  91. }
  92.  
  93. void string::action(unsigned long classe, unsigned short code)
  94. {
  95. LONG l = strlen(buffer) ;
  96.  
  97.     if (maxsize < l) {
  98.         maxsize = l ;
  99.         delete curstring ;
  100.         curstring = new char[maxsize+1] ;
  101.     }
  102.     strcpy(curstring, buffer) ;
  103.     gadget::action(classe, code) ;
  104. }
  105.  
  106. void string::set(STRPTR text)
  107. {
  108. LONG l = strlen(text) ;
  109.  
  110.     if (maxsize < l) {
  111.         maxsize = l ;
  112.         delete curstring ;
  113.         curstring = new char[maxsize+1] ;
  114.     }
  115.     strcpy(curstring, text) ;
  116.     SetGadgetAttrs(gad, w, NULL,
  117.         STRINGA_TextVal,    text,
  118.         TAG_END) ;
  119. }
  120.  
  121. void string::keystroke(BOOL shifted)
  122. {
  123.     ActivateGadget(gad, w, NULL) ;
  124. }
  125.  
  126.