Back to index

lightning-sunbird  0.9+nobinonly
nsTextWidget.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsTextWidget.h"
00040 #include "nsToolkit.h"
00041 #include "nsColor.h"
00042 #include "nsGUIEvent.h"
00043 #include "nsString.h"
00044 #include <windows.h>
00045 
00046 #include "nsILookAndFeel.h"
00047 #include "nsWidgetsCID.h"
00048 #include "nsIComponentManager.h"
00049 
00050 #include "nsIDeviceContext.h"
00051 #include "nsIFontMetrics.h"
00052 
00053 static NS_DEFINE_IID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
00054 
00055 //-------------------------------------------------------------------------
00056 nsresult
00057 NS_NewTextWidget(nsITextWidget** aControl)
00058 {
00059   NS_PRECONDITION(aControl, "null OUT ptr");
00060   if (nsnull == aControl) {
00061     return NS_ERROR_NULL_POINTER;
00062   }
00063   nsTextWidget* it = new nsTextWidget;
00064   if (!it) {
00065     return NS_ERROR_OUT_OF_MEMORY;
00066   }
00067   NS_ADDREF(it);
00068   *aControl = (nsITextWidget*)it;
00069   return NS_OK;
00070 }
00071 
00072 NS_IMPL_ADDREF(nsTextWidget)
00073 NS_IMPL_RELEASE(nsTextWidget)
00074 
00075 
00076 //-------------------------------------------------------------------------
00077 //
00078 // nsTextWidget constructor
00079 //
00080 //-------------------------------------------------------------------------
00081 nsTextWidget::nsTextWidget() : nsTextHelper()
00082 {
00083   mBackground = NS_RGB(124, 124, 124);
00084 }
00085 
00086 //-------------------------------------------------------------------------
00087 //
00088 // nsTextWidget destructor
00089 //
00090 //-------------------------------------------------------------------------
00091 nsTextWidget::~nsTextWidget()
00092 {
00093 }
00094 
00095 //-------------------------------------------------------------------------
00096 //
00097 // Query interface implementation
00098 //
00099 //-------------------------------------------------------------------------
00100 nsresult nsTextWidget::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00101 {
00102     nsresult result = nsWindow::QueryInterface(aIID, aInstancePtr);
00103 
00104     if (result == NS_NOINTERFACE && aIID.Equals(NS_GET_IID(nsITextWidget))) {
00105         *aInstancePtr = (void*) ((nsITextWidget*)this);
00106         NS_ADDREF_THIS();
00107         result = NS_OK;
00108     }
00109 
00110     return result;
00111 }
00112 
00113 // -----------------------------------------------------------------------
00114 //
00115 // Subclass (or remove the subclass from) this component's nsWindow
00116 // this is need for filtering out the "ding" when the return key is pressed
00117 //
00118 // -----------------------------------------------------------------------
00119 void nsTextWidget::SubclassWindow(BOOL bState)
00120 {
00121   if (NULL != mWnd) {
00122     NS_PRECONDITION(::IsWindow(mWnd), "Invalid window handle");
00123     
00124     if (bState) {
00125         // change the nsWindow proc
00126         mPrevWndProc = (WNDPROC)::SetWindowLong(mWnd, GWL_WNDPROC, 
00127                                                  (LONG)nsTextWidget::TextWindowProc);
00128         NS_ASSERTION(mPrevWndProc, "Null standard window procedure");
00129         // connect the this pointer to the nsWindow handle
00130         SetNSWindowPtr(mWnd, this);
00131     } 
00132     else {
00133         (void) ::SetWindowLong(mWnd, GWL_WNDPROC, (LONG)mPrevWndProc);
00134         SetNSWindowPtr(mWnd, NULL);
00135         mPrevWndProc = NULL;
00136     }
00137   }
00138 }
00139 
00140 //-------------------------------------------------------------------------
00141 //
00142 // the nsTextWidget procedure for all nsTextWidget in this toolkit
00143 // this is need for filtering out the "ding" when the return key is pressed
00144 //
00145 //-------------------------------------------------------------------------
00146 LRESULT CALLBACK nsTextWidget::TextWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00147 {
00148     // Filters the "ding" when hitting the return key
00149     if (msg == WM_CHAR) {
00150       long chCharCode = (TCHAR) wParam;    // character code 
00151       if (chCharCode == 13 || chCharCode == 9) {
00152         return 0L;
00153       }
00154     }
00155 
00156     return nsWindow::WindowProc(hWnd, msg, wParam, lParam);
00157 }
00158 
00159 
00160 //-------------------------------------------------------------------------
00161 //
00162 // move, paint, resizes message - ignore
00163 //
00164 //-------------------------------------------------------------------------
00165 PRBool nsTextWidget::OnMove(PRInt32, PRInt32)
00166 {
00167   return PR_FALSE;
00168 }
00169 
00170 PRBool nsTextWidget::OnPaint()
00171 {
00172     return PR_FALSE;
00173 }
00174 
00175 
00176 PRBool nsTextWidget::OnResize(nsRect &aWindowRect)
00177 {
00178     return PR_FALSE;
00179 }
00180 
00181 //-------------------------------------------------------------------------
00182 //
00183 // return the window class name and initialize the class if needed
00184 //
00185 //-------------------------------------------------------------------------
00186 LPCWSTR nsTextWidget::WindowClassW()
00187 {
00188   return(nsTextHelper::WindowClassW());
00189 }
00190 
00191 //-------------------------------------------------------------------------
00192 //
00193 // return the window class name and initialize the class if needed
00194 //
00195 //-------------------------------------------------------------------------
00196 LPCTSTR nsTextWidget::WindowClass()
00197 {
00198   return(nsTextHelper::WindowClass());
00199 }
00200 
00201 //-------------------------------------------------------------------------
00202 //
00203 // return window styles
00204 //
00205 //-------------------------------------------------------------------------
00206 DWORD nsTextWidget::WindowStyle()
00207 {
00208   return(nsTextHelper::WindowStyle());
00209 }
00210 
00211 
00212 //-------------------------------------------------------------------------
00213 //
00214 // return window extended styles
00215 //
00216 //-------------------------------------------------------------------------
00217 
00218 DWORD nsTextWidget::WindowExStyle()
00219 {
00220     return WS_EX_CLIENTEDGE;
00221 }
00222 
00223 
00224 //-------------------------------------------------------------------------
00225 //
00226 // get position/dimensions
00227 //
00228 //-------------------------------------------------------------------------
00229 
00230 NS_METHOD nsTextWidget::GetBounds(nsRect &aRect)
00231 {
00232     nsWindow::GetNonClientBounds(aRect);
00233     return NS_OK;
00234 }
00235 
00240 NS_METHOD nsTextWidget::Paint(nsIRenderingContext& aRenderingContext,
00241                               const nsRect& aDirtyRect)
00242 {
00243   nsRect rect;
00244   float  appUnits;
00245   float  scale;
00246   nsIDeviceContext * context;
00247   aRenderingContext.GetDeviceContext(context);
00248 
00249   context->GetCanonicalPixelScale(scale);
00250   appUnits = context->DevUnitsToAppUnits();
00251 
00252   GetBoundsAppUnits(rect, appUnits);
00253 
00254   aRenderingContext.SetColor(NS_RGB(0,0,0));
00255 
00256   nscolor bgColor  = NS_RGB(255,255,255);
00257   nscolor fgColor  = NS_RGB(0,0,0);
00258   nscolor hltColor = NS_RGB(240,240,240);
00259   nscolor sdwColor = NS_RGB(128,128,128);
00260   nscolor txtBGColor = NS_RGB(255,255,255);
00261   nscolor txtFGColor = NS_RGB(0,0,0);
00262   {
00263     nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService(kLookAndFeelCID);
00264     if (lookAndFeel) {
00265       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetBackground,  bgColor);
00266       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetForeground,  fgColor);
00267       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DShadow,    sdwColor);
00268       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DHighlight, hltColor);
00269       lookAndFeel->GetColor(nsILookAndFeel::eColor_TextBackground,    txtBGColor);
00270       lookAndFeel->GetColor(nsILookAndFeel::eColor_TextForeground,    txtFGColor);
00271     }
00272   }
00273 
00274   aRenderingContext.SetColor(txtBGColor);
00275   aRenderingContext.FillRect(rect);
00276 
00277   // Paint Black border
00278   //nsBaseWidget::Paint(aRenderingContext, aDirtyRect);
00279 
00280   nscoord onePixel  = nscoord(scale);
00281   nscoord twoPixels = nscoord(scale*2);
00282 
00283   rect.x      += onePixel; 
00284   rect.y      += onePixel;
00285   rect.width  -= twoPixels+onePixel; 
00286   rect.height -= twoPixels+onePixel;
00287 
00288   nscoord right     = rect.x+rect.width;
00289   nscoord bottom    = rect.y+rect.height;
00290 
00291 
00292   // Draw Left & Top
00293   aRenderingContext.SetColor(NS_RGB(128,128,128));
00294   DrawScaledLine(aRenderingContext, rect.x, rect.y, right, rect.y, scale, appUnits, PR_TRUE); // top
00295   DrawScaledLine(aRenderingContext, rect.x, rect.y, rect.x, bottom, scale, appUnits, PR_FALSE); // left
00296 
00297   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, right-onePixel, rect.y+onePixel, scale, appUnits, PR_TRUE); // top + 1
00298   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, rect.x+onePixel, bottom-onePixel, scale, appUnits, PR_FALSE); // left + 1
00299 
00300   // Draw Right & Bottom
00301   aRenderingContext.SetColor(NS_RGB(192,192,192));
00302   DrawScaledLine(aRenderingContext, right, rect.y+onePixel, right, bottom, scale, appUnits, PR_FALSE); // right 
00303   DrawScaledLine(aRenderingContext, rect.x+onePixel, bottom, right, bottom, scale, appUnits, PR_TRUE); // bottom
00304 
00305   //DrawScaledLine(aRenderingContext, right-onePixel, rect.y+twoPixels, right-onePixel, bottom, scale, appUnits, PR_FALSE); // right + 1
00306   //DrawScaledLine(aRenderingContext, rect.x+twoPixels, bottom-onePixel, right, bottom-onePixel, scale, appUnits, PR_TRUE); // bottom + 1
00307   
00308 
00309   nsIFontMetrics* metrics;
00310   context->GetMetricsFor(*mFont, metrics);
00311   aRenderingContext.SetFont(metrics);
00312 
00313   nscoord textWidth;
00314   nscoord textHeight;
00315   aRenderingContext.GetWidth(mText, textWidth);
00316 
00317   metrics->GetMaxAscent(textHeight);
00318 
00319   nscoord x = (twoPixels * 2)  + rect.x;
00320   nscoord y = ((rect.height - textHeight) / 2) + rect.y;
00321   aRenderingContext.SetColor(txtFGColor);
00322   if (!mIsPassword) {
00323     aRenderingContext.DrawString(mText, x, y + textHeight);
00324   } else {
00325     nsString astricks;
00326     PRUint32 i;
00327     for (i=0;i<mText.Length();i++) {
00328       astricks.AppendLiteral("*");
00329     }
00330     aRenderingContext.DrawString(astricks, x, y + textHeight);
00331 
00332   }
00333 
00334   NS_RELEASE(context);
00335 
00336   return NS_OK;
00337 }
00338 
00339