home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CPasswordText 1.0 / CPasswordText / CPasswordText.c next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  3.9 KB  |  139 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CPasswordText.c
  3.  
  4.     A subclass of CDialogText for entering hidden password text. Password 
  5.     text is limited to 100 characters currently, but there is no limitation
  6.     checking. This class replaced TextEdit's draw hook with one that 
  7.     draws bullets.
  8.     
  9.     AUTHOR: Andrew_Gilmartin@Brown.Edu
  10.     REVISION: 2
  11.  
  12. ******************************************************************************/
  13.  
  14. #include "CPasswordText.h"
  15.  
  16.  
  17. /******************************************************************************
  18.  IPasswordText
  19.  
  20.      Initialize a CPasswordText object. The user's password is hidden by
  21.      displaying a bullet for each character. This is done by replacing
  22.      TextEdit draw hook.
  23. ******************************************************************************/
  24.  
  25. void CPasswordText::IPasswordText
  26.     ( CView *anEnclosure
  27.     , CView *aSupervisor
  28.     , short aWidth
  29.     , short aHeight
  30.     , short aHEncl
  31.     , short aVEncl
  32.     , SizingOption aHSizing
  33.     , SizingOption aVSizing
  34.     , short aLineWidth )
  35. {
  36.     /*
  37.         Since I am only replacing the draw hook, we need to fake out
  38.         text edit so that its own width and hit testing calls find the
  39.         right characters. Since the only displayed character is a 
  40.         bullet TE should think all characters have the bullet's 
  41.         width. I use a monospace font to accumplish this.
  42.     */
  43.  
  44.     TextFont( monaco ); // Perhaps I should have a RestoreEnvironment() method...
  45.  
  46.     IDialogText
  47.         ( anEnclosure
  48.         , aSupervisor
  49.         , aWidth
  50.         , aHeight
  51.         , aHEncl
  52.         , aVEncl
  53.         , aHSizing
  54.         , aVSizing
  55.         , aLineWidth );
  56.     
  57.     InstallTEHooks();
  58.  
  59. } /* IPasswordText */
  60.  
  61.  
  62.  
  63. /******************************************************************************
  64.  IViewTemp
  65.  
  66.      Initialize a CPasswordText object from resource template (DlTx). See 
  67.      comments in IPasswordText().
  68. ******************************************************************************/
  69.  
  70. void CPasswordText::IViewTemp
  71.     ( CView *anEnclosure
  72.     , CBureaucrat *aSupervisor
  73.     , Ptr viewData )
  74. {
  75.     TextFont( monaco );
  76.  
  77.     inherited::IViewTemp( anEnclosure, aSupervisor, viewData );
  78.     
  79.     InstallTEHooks();
  80.  
  81. } /* IViewTemp */
  82.  
  83.  
  84.  
  85. /******************************************************************************
  86.  InstallTEHooks
  87.  
  88.     Replace TextEdit's draw hook with one that only draws bullets. 
  89. ******************************************************************************/
  90.  
  91. void CPasswordText::InstallTEHooks( void )
  92. {
  93.     void MyTEDrawHook( void );
  94.     ProcPtr teHook;
  95.  
  96.     teHook = (ProcPtr) StripAddress( MyTEDrawHook );
  97.     TECustomHook( intDrawHook, &teHook, macTE );
  98.  
  99. } /* InstallTEHooks */
  100.  
  101.  
  102.  
  103. /*============================================================================
  104.  MyTEDrawHook
  105.  
  106.     This routine is called any time the various components of a line are drawn.
  107.     The appropriate font, face, and size characteristics have already been set
  108.     into the current port by the time this routine is called.
  109.  
  110.     On entry:    D0  offset into text (word)
  111.                 D1  length of text to draw (word)
  112.                 A0  pointer to text to draw (long)
  113.                 A3  pointer to the locked TextEdit record (long)
  114.                 A4  handle to the locked TextEdit record (long)
  115.  
  116.     Thanks to Chris Wysocki (wysocki@husc.harvard.edu) for example code.
  117. ============================================================================*/
  118.  
  119. static char* gBulletString = /* 100 bullets */
  120.     "••••••••••" "••••••••••" "••••••••••" "••••••••••" "••••••••••"
  121.     "••••••••••" "••••••••••" "••••••••••" "••••••••••" "••••••••••";
  122.  
  123. static void MyTEDrawHook()
  124. {
  125.     asm {
  126.         move.l a0,-(sp)            ; save a0 (Why doesn't pea work?)
  127.         movea.l gBulletString,a0   ; get address of bullet string
  128.         move.l a0,-(sp);           ; push ptr to text
  129.         move.w #0,-(sp)            ; push offset into text
  130.         move.w  d1,-(sp)           ; push length of text to draw (Hope <= 100)
  131.         _DrawText                  ; draw the text
  132.         move.l (sp)+,a0            ; restore a0
  133.     }
  134.  
  135. } /* MyTEDrawHook */
  136.  
  137.  
  138.  
  139.