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 <os2.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(::WinIsWindow(0, mWnd), "Invalid window handle");
00123     
00124     if (bState) {
00125         // change the nsWindow proc
00126         mPrevWndProc = WinSubclassWindow(mWnd, TextWindowProc);
00127         NS_ASSERTION(mPrevWndProc, "Null standard window procedure");
00128         // connect the this pointer to the nsWindow handle
00129         SetNSWindowPtr(mWnd, this);
00130     } 
00131     else {
00132         WinSubclassWindow(mWnd, mPrevWndProc);
00133         SetNSWindowPtr(mWnd, NULL);
00134         mPrevWndProc = NULL;
00135     }
00136   }
00137 }
00138 
00139 //-------------------------------------------------------------------------
00140 //
00141 // the nsTextWidget procedure for all nsTextWidget in this toolkit
00142 // this is need for filtering out the "ding" when the return key is pressed
00143 //
00144 //-------------------------------------------------------------------------
00145 MRESULT EXPENTRY nsTextWidget::TextWindowProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
00146 {
00147     // Filters the "ding" when hitting the return key
00148     if (msg == WM_CHAR) {
00149       long chCharCode =  LONGFROMMP(mp1);    // character code 
00150       if (chCharCode == 13 || chCharCode == 9) {
00151         return 0L;
00152       }
00153     }
00154 
00155     return fnwpNSWindow(hWnd, msg, mp1, mp2);
00156 }
00157 
00158 
00159 //-------------------------------------------------------------------------
00160 //
00161 // move, paint, resizes message - ignore
00162 //
00163 //-------------------------------------------------------------------------
00164 PRBool nsTextWidget::OnMove(PRInt32, PRInt32)
00165 {
00166   return PR_FALSE;
00167 }
00168 
00169 PRBool nsTextWidget::OnPaint()
00170 {
00171     return PR_FALSE;
00172 }
00173 
00174 
00175 PRBool nsTextWidget::OnResize(nsRect &aWindowRect)
00176 {
00177     return PR_FALSE;
00178 }
00179 
00180 //-------------------------------------------------------------------------
00181 //
00182 // return the window class name and initialize the class if needed
00183 //
00184 //-------------------------------------------------------------------------
00185 PCSZ nsTextWidget::WindowClass()
00186 {
00187   return(nsTextHelper::WindowClass());
00188 }
00189 
00190 //-------------------------------------------------------------------------
00191 //
00192 // return window styles
00193 //
00194 //-------------------------------------------------------------------------
00195 ULONG nsTextWidget::WindowStyle()
00196 {
00197   return(nsTextHelper::WindowStyle());
00198 }
00199 
00200 
00201 //-------------------------------------------------------------------------
00202 //
00203 // return window extended styles
00204 //
00205 //-------------------------------------------------------------------------
00206 
00207 ULONG nsTextWidget::WindowExStyle()
00208 {
00209     return 0;
00210 }
00211 
00212 
00213 //-------------------------------------------------------------------------
00214 //
00215 // get position/dimensions
00216 //
00217 //-------------------------------------------------------------------------
00218 
00219 NS_METHOD nsTextWidget::GetBounds(nsRect &aRect)
00220 {
00221     nsWindow::GetNonClientBounds(aRect);
00222     return NS_OK;
00223 }
00224 
00229 NS_METHOD nsTextWidget::Paint(nsIRenderingContext& aRenderingContext,
00230                               const nsRect& aDirtyRect)
00231 {
00232   nsRect rect;
00233   float  appUnits;
00234   float  scale;
00235   nsIDeviceContext * context;
00236   aRenderingContext.GetDeviceContext(context);
00237 
00238   context->GetCanonicalPixelScale(scale);
00239   appUnits = context->DevUnitsToAppUnits();
00240 
00241   GetBoundsAppUnits(rect, appUnits);
00242 
00243   aRenderingContext.SetColor(NS_RGB(0,0,0));
00244 
00245   nscolor bgColor  = NS_RGB(255,255,255);
00246   nscolor fgColor  = NS_RGB(0,0,0);
00247   nscolor hltColor = NS_RGB(240,240,240);
00248   nscolor sdwColor = NS_RGB(128,128,128);
00249   nscolor txtBGColor = NS_RGB(255,255,255);
00250   nscolor txtFGColor = NS_RGB(0,0,0);
00251   {
00252     nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService(kLookAndFeelCID);
00253     if (lookAndFeel) {
00254       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetBackground,  bgColor);
00255       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetForeground,  fgColor);
00256       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DShadow,    sdwColor);
00257       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DHighlight, hltColor);
00258       lookAndFeel->GetColor(nsILookAndFeel::eColor_TextBackground,    txtBGColor);
00259       lookAndFeel->GetColor(nsILookAndFeel::eColor_TextForeground,    txtFGColor);
00260     }
00261   }
00262 
00263   aRenderingContext.SetColor(txtBGColor);
00264   aRenderingContext.FillRect(rect);
00265 
00266   // Paint Black border
00267   //nsBaseWidget::Paint(aRenderingContext, aDirtyRect);
00268 
00269   nscoord onePixel  = nscoord(scale);
00270   nscoord twoPixels = nscoord(scale*2);
00271 
00272   rect.x      += onePixel; 
00273   rect.y      += onePixel;
00274   rect.width  -= twoPixels+onePixel; 
00275   rect.height -= twoPixels+onePixel;
00276 
00277   nscoord right     = rect.x+rect.width;
00278   nscoord bottom    = rect.y+rect.height;
00279 
00280 
00281   // Draw Left & Top
00282   aRenderingContext.SetColor(NS_RGB(128,128,128));
00283   DrawScaledLine(aRenderingContext, rect.x, rect.y, right, rect.y, scale, appUnits, PR_TRUE); // top
00284   DrawScaledLine(aRenderingContext, rect.x, rect.y, rect.x, bottom, scale, appUnits, PR_FALSE); // left
00285 
00286   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, right-onePixel, rect.y+onePixel, scale, appUnits, PR_TRUE); // top + 1
00287   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, rect.x+onePixel, bottom-onePixel, scale, appUnits, PR_FALSE); // left + 1
00288 
00289   // Draw Right & Bottom
00290   aRenderingContext.SetColor(NS_RGB(192,192,192));
00291   DrawScaledLine(aRenderingContext, right, rect.y+onePixel, right, bottom, scale, appUnits, PR_FALSE); // right 
00292   DrawScaledLine(aRenderingContext, rect.x+onePixel, bottom, right, bottom, scale, appUnits, PR_TRUE); // bottom
00293 
00294   //DrawScaledLine(aRenderingContext, right-onePixel, rect.y+twoPixels, right-onePixel, bottom, scale, appUnits, PR_FALSE); // right + 1
00295   //DrawScaledLine(aRenderingContext, rect.x+twoPixels, bottom-onePixel, right, bottom-onePixel, scale, appUnits, PR_TRUE); // bottom + 1
00296   
00297 
00298   nsIFontMetrics* metrics;
00299   context->GetMetricsFor(*mFont, metrics);
00300   aRenderingContext.SetFont(metrics);
00301 
00302   nscoord textWidth;
00303   nscoord textHeight;
00304   aRenderingContext.GetWidth(mText, textWidth);
00305 
00306   metrics->GetMaxAscent(textHeight);
00307 
00308   nscoord x = (twoPixels * 2)  + rect.x;
00309   nscoord y = ((rect.height - textHeight) / 2) + rect.y;
00310   aRenderingContext.SetColor(txtFGColor);
00311   if (!mIsPassword) {
00312     aRenderingContext.DrawString(mText, x, y + textHeight);
00313   } else {
00314     nsString astricks;
00315     PRUint32 i;
00316     for (i=0;i<mText.Length();i++) {
00317       astricks.AppendLiteral("*");
00318     }
00319     aRenderingContext.DrawString(astricks, x, y + textHeight);
00320 
00321   }
00322 
00323   NS_RELEASE(context);
00324 
00325   return NS_OK;
00326 }
00327 
00328