home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / nsadrnam.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.3 KB  |  285 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // NSAdrNam.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23.  
  24. #include "nsadrlst.h"
  25. #include "nsadrnam.h"
  26. #include "addrbook.h"
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CNSAddressNameEditField
  30.  
  31. CNSAddressNameEditField::CNSAddressNameEditField()
  32. {
  33.     m_pIAddressParent = NULL;
  34.     m_bNameCompletion = FALSE;
  35.     m_bAttemptNameCompletion = TRUE;
  36.     m_bSetTimerForCompletion = FALSE;
  37.     m_uTypedownClock = 0;
  38. }
  39.  
  40. CNSAddressNameEditField::~CNSAddressNameEditField()
  41. {
  42. }
  43.  
  44.  
  45. BOOL CNSAddressNameEditField::Create( CWnd *pParent )
  46. {
  47.     return CGenericEdit::Create(ES_MULTILINE|ES_WANTRETURN|ES_LEFT|ES_AUTOHSCROLL, CRect(0,0,0,0), pParent, (UINT)IDC_STATIC );
  48. }
  49.  
  50.  
  51. BEGIN_MESSAGE_MAP(CNSAddressNameEditField, CGenericEdit)
  52.     //{{AFX_MSG_MAP(CNSAddressNameEditField)
  53.     ON_WM_KILLFOCUS()
  54.     ON_WM_TIMER ()
  55.     ON_WM_CHAR()
  56.     ON_WM_CREATE()
  57.     ON_WM_KEYDOWN()
  58.     ON_WM_LBUTTONDBLCLK()
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CNSAddressNameEditField message handlers
  65.  
  66.  
  67. BOOL CNSAddressNameEditField::PreTranslateMessage( MSG* pMsg )
  68. {
  69.     if (pMsg->message == WM_KEYDOWN)
  70.     {
  71.         if (pMsg->wParam == 'v' || pMsg->wParam == 'V')
  72.         {
  73.             if (GetKeyState(VK_CONTROL)&0x8000)
  74.             {
  75.                 Paste();
  76.                 return TRUE;
  77.             }
  78.         } 
  79.         else if (pMsg->wParam == VK_INSERT)
  80.         {
  81.             if (GetKeyState(VK_SHIFT)&0x8000)
  82.             {
  83.                 Paste();
  84.                 return TRUE;
  85.             }
  86.             else if (GetKeyState(VK_CONTROL)&0x8000)
  87.             {
  88.                 Copy();
  89.                 return TRUE;
  90.             }
  91.         }
  92.         else if (pMsg->wParam == 'x' || pMsg->wParam == 'X')
  93.         {
  94.             if (GetKeyState(VK_CONTROL)&0x8000)
  95.             {
  96.                 Cut();
  97.                 return TRUE;
  98.             }
  99.         } 
  100.         else if (pMsg->wParam == 'c' || pMsg->wParam == 'C')
  101.         {
  102.             if (GetKeyState(VK_CONTROL)&0x8000)
  103.             {
  104.                 Copy();
  105.                 return TRUE;
  106.             }
  107.         } 
  108.     }
  109.     return CGenericEdit::PreTranslateMessage(pMsg);
  110. }
  111.  
  112. void CNSAddressNameEditField::OnKillFocus(CWnd * pNewWnd) 
  113. {
  114.     if (GetParent()->SendMessage(WM_NOTIFYSELECTIONCHANGE))
  115.         CGenericEdit::OnKillFocus(pNewWnd);
  116.     else
  117.         SetSel(0, -1);
  118. }
  119.  
  120. void CNSAddressNameEditField::DrawNameCompletion()
  121. {
  122.     if (m_pIAddressParent)
  123.     {
  124.         CString cs;
  125.         GetWindowText(cs);
  126.         char * pName = m_pIAddressParent->NameCompletion((char*)((const char *)cs));
  127.         CDC * pDC = GetDC();
  128.         UINT iPre = cs.GetLength();
  129.         CFont * pOldFont = pDC->SelectObject(CFont::FromHandle(m_cfTextFont));
  130.         int iPixOffset = pDC->GetTextExtent(cs,iPre).cx;
  131.         CRect rect;
  132.         GetClientRect(rect);
  133.         rect.left = iPixOffset;
  134.         if (pName)
  135.         {
  136.             m_bNameCompletion = TRUE;
  137.             COLORREF crOld = pDC->SetTextColor(RGB(150,150,150));
  138.             COLORREF crOldBk = pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  139.             if (iPre < strlen(pName))
  140.                 pDC->DrawText(&pName[iPre],strlen(&pName[iPre]),rect,DT_VCENTER|DT_LEFT);
  141.             pDC->SetTextColor(crOld);
  142.             pDC->SetBkColor(crOldBk);
  143.             free(pName);
  144.         }
  145.         else
  146.         {  
  147.             m_bNameCompletion = FALSE;
  148.             InvalidateRect(rect);
  149.         }
  150.         pDC->SelectObject(pOldFont);
  151.         ReleaseDC(pDC);
  152.     }
  153. }
  154.  
  155.  
  156. #define NAME_COMPLETION_TIMER        12
  157. #define COMPLETION_SPEED             750
  158.  
  159. void CNSAddressNameEditField::OnTimer(UINT nID)
  160. {
  161.     if (nID == NAME_COMPLETION_TIMER) {
  162.         KillTimer(m_uTypedownClock);
  163.         m_uTypedownClock = 0;
  164.         DrawNameCompletion();
  165.     }
  166. }
  167.  
  168.  
  169. void CNSAddressNameEditField::OnChar( UINT nChar, UINT nRepCnt, UINT nFlags ) 
  170. {
  171.     switch (nChar)
  172.     {
  173.         case VK_ESCAPE:
  174.             return;
  175.         case VK_DELETE:
  176.             break;
  177.         default:
  178.             if ( ((CNSAddressList *)GetParent())->OnKeyPress( this, nChar, nRepCnt, nFlags ) )
  179.                 return;
  180.     }
  181.     CGenericEdit::OnChar( nChar, nRepCnt, nFlags );
  182.     if (m_bAttemptNameCompletion)
  183.     {
  184.         if (m_bSetTimerForCompletion)
  185.         {
  186.             if (m_uTypedownClock) {
  187.                 KillTimer(m_uTypedownClock);
  188.             }
  189.             m_uTypedownClock = SetTimer(NAME_COMPLETION_TIMER, COMPLETION_SPEED, NULL);
  190.         }
  191.         else
  192.             DrawNameCompletion();
  193.     }
  194. }
  195.  
  196. int CNSAddressNameEditField::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  197. {
  198.     if (CGenericEdit::OnCreate(lpCreateStruct) == -1)
  199.         return -1;
  200.  
  201.    uint32 count;
  202.    DIR_Server* pab = NULL;
  203.  
  204.    DIR_GetPersonalAddressBook (theApp.m_directories, &pab);
  205.    if (pab) {
  206.         AB_GetEntryCount(pab, theApp.m_pABook, &count, ABTypeAll, NULL);
  207.         if (count > 1500)
  208.             m_bSetTimerForCompletion = TRUE;
  209.    }
  210.     return 0;
  211. }
  212.  
  213. void CNSAddressNameEditField::SetCSID(int16 iCSID) 
  214. {
  215.     if(m_hWnd)
  216.     {
  217.         CDC * pdc = GetDC();
  218.         if (m_cfTextFont) {
  219.             theApp.ReleaseAppFont(m_cfTextFont);
  220.         }
  221.         LOGFONT lf;            
  222.         memset(&lf,0,sizeof(LOGFONT));
  223.  
  224.         lf.lfPitchAndFamily = FF_MODERN | FIXED_PITCH;
  225.         strcpy(lf.lfFaceName, IntlGetUIFixFaceName(iCSID));
  226.         lf.lfHeight = -MulDiv(NS_ADDRESSFONTSIZE,pdc->GetDeviceCaps(LOGPIXELSY),72);
  227.         lf.lfQuality = PROOF_QUALITY;
  228.         lf.lfCharSet = IntlGetLfCharset(iCSID); 
  229.         m_cfTextFont = theApp.CreateAppFont( lf );
  230.         if (!(lf.lfPitchAndFamily & FIXED_PITCH))
  231.                 m_bAttemptNameCompletion = FALSE;
  232.         ::SendMessage(GetSafeHwnd(), WM_SETFONT, (WPARAM)m_cfTextFont, FALSE);
  233.         TEXTMETRIC tm;
  234.         pdc->GetTextMetrics(&tm);
  235.         m_iTextHeight = tm.tmHeight;
  236.  
  237.         ReleaseDC(pdc);
  238.     }
  239. }
  240. void CNSAddressNameEditField::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  241. {
  242.     switch (nChar) 
  243.     {
  244.         case VK_HOME: 
  245.             {
  246.                 int nStart, nEnd;
  247.                 GetSel(nStart,nEnd);
  248.                 if ((!nStart)&&(!nEnd)) 
  249.                     ((CNSAddressList *)GetParent())->OnKeyPress(this,nChar,nRepCnt,nFlags); 
  250.             }
  251.             break;
  252.         case VK_END: 
  253.             {
  254.                 int nStart, nEnd;
  255.                 GetSel(nStart,nEnd);
  256.                 if (nStart == nEnd)
  257.                     if (nStart == LineLength())
  258.                         ((CNSAddressList *)GetParent())->OnKeyPress(this,nChar,nRepCnt,nFlags); 
  259.             }
  260.             break;
  261.         case VK_UP:
  262.         case VK_DOWN:
  263.             ((CNSAddressList *)GetParent())->OnKeyPress(this,nChar,nRepCnt,nFlags); 
  264.             break;
  265.         case VK_DELETE:
  266.             {
  267.                 CString cs;
  268.                 GetWindowText(cs);
  269.                 if (!cs.GetLength())
  270.                 {
  271.                     ((CNSAddressList *)GetParent())->OnKeyPress(this,nChar,nRepCnt,nFlags); 
  272.                     return;
  273.                 }
  274.             }
  275.             break;
  276.     }
  277.     CGenericEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  278. }
  279.  
  280. void CNSAddressNameEditField::OnLButtonDblClk(UINT nFlags, CPoint point) 
  281. {
  282.     SetSel(0,-1);
  283. }
  284.  
  285.