home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CURLEditField.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  4.6 KB  |  189 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. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  20. //    CURLEditField.cp
  21. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  22.  
  23. #include "CURLEditField.h"
  24.  
  25. #include <UDrawingState.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. #include "net.h"    // for URL_Struct
  30.  
  31. #include "CNSContext.h"    // for CNSContext messages
  32. #include "ufilemgr.h" // for CFileMgr::FileNameFromURL
  33.  
  34. #include "libmocha.h" // for LM_StripWysiwygURLPrefix
  35.  
  36. const size_t cCompareWholeString = 0;
  37.  
  38. struct urlFilter {
  39.     const char*    string;
  40.     size_t        comparisonLength;
  41. };
  42.  
  43. // table of URL's not to display
  44. const Uint8 numFilteredURLs = 3;
  45. const urlFilter cAboutFilter = { "about:document", cCompareWholeString    };
  46. const urlFilter cMailtoFilter = { "mailto:",    7    };
  47. const urlFilter cNetHelpFilter = { "nethelp:",    8    };
  48. const urlFilter* filteredURLs[] =
  49. {
  50.     &cAboutFilter,
  51.     &cMailtoFilter,
  52.     &cNetHelpFilter
  53. };
  54.  
  55. CURLEditField::CURLEditField(LStream *inStream) :
  56. CTSMEditField(inStream), mURLStringInSync(false)
  57. {
  58. }
  59.  
  60. Boolean CURLEditField::HandleKeyPress(const EventRecord& inKeyEvent)
  61. {
  62.     char        c = inKeyEvent.message & charCodeMask;
  63.     Boolean        handled = false;
  64.     
  65.     if ((c == char_Enter) || (c == char_Return))
  66.     {
  67.         CStr255    url;
  68.         GetDescriptor(url);
  69.         BroadcastMessage(msg_UserSubmittedURL, &url);
  70.         handled = true;
  71.     }
  72.     else
  73.     {
  74.         handled = CTSMEditField::HandleKeyPress(inKeyEvent);
  75.         if (handled)
  76.         {
  77.             if ((c != char_Tab) && mURLStringInSync)
  78.             {
  79.                 BroadcastMessage(msg_UserChangedURL);
  80.                 mURLStringInSync = false;
  81.             }
  82.         }
  83.     }
  84.     return handled;
  85. }
  86.  
  87. void CURLEditField::DrawSelf()
  88. {
  89.     StColorPenState::Normalize();
  90.  
  91.     Rect theFrame;
  92.     CalcLocalFrameRect(theFrame);
  93.     ::EraseRect(&theFrame);
  94.  
  95.     CTSMEditField::DrawSelf();
  96. }
  97.  
  98. void CURLEditField::ClickSelf(const SMouseDownEvent& inMouseDown)
  99. {
  100.     Boolean    wasTarget = IsTarget();
  101.     
  102.     if (wasTarget && GetClickCount() == 3)
  103.         SelectAll();
  104.     else
  105.     {
  106.         CTSMEditField::ClickSelf(inMouseDown);
  107.         
  108.         // If we just switched the target to ourselves and the user
  109.         // didn't just make a partial selection of the text,
  110.         // then we select the entire field for convenience.
  111.         //
  112.         if (!wasTarget && IsTarget())
  113.         {
  114.             TEHandle    textEditHandle = GetMacTEH();
  115.             
  116.             if ( (**textEditHandle).selStart == (**textEditHandle).selEnd )          
  117.                 SelectAll();
  118.         }
  119.     }
  120. }
  121.  
  122. void CURLEditField::ListenToMessage(MessageT inMessage, void* ioParam)
  123. {
  124.     if (ioParam)
  125.     {
  126.         switch (inMessage)
  127.         {
  128.             // NOTE: We assume we only get this broadcast from the main
  129.             // CNSContext
  130.             case msg_NSCStartLoadURL:
  131.             case msg_NSCLayoutNewDocument:
  132.                 URL_Struct* theURL = (URL_Struct*)ioParam;
  133.                 // 1997-03-23 pkc
  134.                 // Call LM_StripWysiwygURLPrefix to strip those pesky
  135.                 // "wysiwyg" URL's. According to Brendan, LM_StripWysiwygURLPrefix
  136.                 // doesn't allocate a new string.
  137.                 CStr255 newString(LM_StripWysiwygURLPrefix(theURL->address));
  138.                 if (DisplayURL(newString))
  139.                 {
  140.                     SetDescriptor(newString);
  141.                     mURLStringInSync = true;
  142.                 }
  143.         }
  144.     }
  145. }
  146.  
  147. // Filter function to determine whether or not to display URL
  148.  
  149. Boolean CURLEditField::DisplayURL(CStr255& inURL)
  150. {
  151.     // if inURL is in filteredURLs table, return false
  152.     for(int i = 0; i < numFilteredURLs; i++)
  153.     {
  154.         const urlFilter* filter = filteredURLs[i];
  155.         if (filter->comparisonLength == cCompareWholeString)
  156.         {
  157.             if (XP_STRCMP(filter->string, (char*)inURL) == 0)
  158.                 return false;
  159.         }
  160.         else
  161.         {
  162.             if (XP_STRNCMP(filter->string, (char*)inURL, filter->comparisonLength) == 0)
  163.                 return false;
  164.         }
  165.     }
  166.     return true;
  167. }
  168.  
  169. Boolean CURLEditField::ObeyCommand(CommandT inCommand,void *ioParam)
  170. {
  171.     if (inCommand == msg_TabSelect)
  172.     {
  173.         if (IsVisible())
  174.             return true;
  175.         else
  176.             return false;
  177.     }
  178.     else
  179.     {
  180.         return CTSMEditField::ObeyCommand(inCommand, ioParam);
  181.     }
  182. }
  183.  
  184. void CURLEditField::BeTarget()
  185. {
  186.     CTSMEditField::BeTarget();
  187.     SelectAll();
  188. }
  189.