Back to index

lightning-sunbird  0.9+nobinonly
nsButton.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsButton.h"
00039 #include "nsToolkit.h"
00040 #include "nsColor.h"
00041 #include "nsGUIEvent.h"
00042 #include "nsString.h"
00043 #include "nsStringUtil.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_NewButton(nsIButton** aControl)
00058 {
00059   NS_PRECONDITION(aControl, "null OUT ptr");
00060   if (nsnull == aControl) {
00061     return NS_ERROR_NULL_POINTER;
00062   }
00063   nsButton* it = new nsButton;
00064   if (!it) {
00065     return NS_ERROR_OUT_OF_MEMORY;
00066   }
00067   NS_ADDREF(it);
00068   // set the state flags (if any are provided)
00069   *aControl = (nsIButton*)it;
00070   return NS_OK;
00071 }
00072 
00073 
00074 NS_IMPL_ADDREF(nsButton)
00075 NS_IMPL_RELEASE(nsButton)
00076 
00077 //-------------------------------------------------------------------------
00078 //
00079 // nsButton constructor
00080 //
00081 //-------------------------------------------------------------------------
00082 nsButton::nsButton() : nsWindow() , nsIButton()
00083 {
00084 }
00085 
00086 //-------------------------------------------------------------------------
00087 //
00088 // nsButton destructor
00089 //
00090 //-------------------------------------------------------------------------
00091 nsButton::~nsButton()
00092 {
00093 }
00094 
00103 nsresult nsButton::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00104 {
00105     if (NULL == aInstancePtr) {
00106         return NS_ERROR_NULL_POINTER;
00107     }
00108 
00109     static NS_DEFINE_IID(kIButton, NS_IBUTTON_IID);
00110     if (aIID.Equals(kIButton)) {
00111         *aInstancePtr = (void*) ((nsIButton*)this);
00112         NS_ADDREF_THIS();
00113         return NS_OK;
00114     }
00115 
00116     return nsWindow::QueryInterface(aIID,aInstancePtr);
00117 }
00118 
00119 
00120 //-------------------------------------------------------------------------
00121 //
00122 // Set this button label
00123 //
00124 //-------------------------------------------------------------------------
00125 NS_METHOD nsButton::SetLabel(const nsString& aText)
00126 {
00127 
00128   mLabel = aText;
00129   if (NULL == mWnd) {
00130     return NS_ERROR_FAILURE;
00131   }
00132   VERIFY(::SetWindowText(mWnd, NS_LossyConvertUCS2toASCII(aText).get()));
00133   return NS_OK;
00134 }
00135 
00136 //-------------------------------------------------------------------------
00137 //
00138 // Get this button label
00139 //
00140 //-------------------------------------------------------------------------
00141 NS_METHOD nsButton::GetLabel(nsString& aBuffer)
00142 {
00143   aBuffer = mLabel;
00144 
00145   /*int actualSize = ::GetWindowTextLength(mWnd)+1;
00146   NS_ALLOC_CHAR_BUF(label, 256, actualSize);
00147   ::GetWindowText(mWnd, label, actualSize);
00148   aBuffer.SetLength(0);
00149   aBuffer.Append(label);
00150   NS_FREE_CHAR_BUF(label);
00151   */
00152   return NS_OK;
00153 }
00154 
00155 //-------------------------------------------------------------------------
00156 //
00157 // move, paint, resizes message - ignore
00158 //
00159 //-------------------------------------------------------------------------
00160 PRBool nsButton::OnMove(PRInt32, PRInt32)
00161 {
00162   return PR_FALSE;
00163 }
00164 
00165 PRBool nsButton::OnPaint()
00166 {
00167   //printf("** nsButton::OnPaint **\n");
00168   return PR_FALSE;
00169 }
00170 
00171 PRBool nsButton::OnResize(nsRect &aWindowRect)
00172 {
00173     return PR_FALSE;
00174 }
00175 
00176 //-------------------------------------------------------------------------
00177 //
00178 // return the window class name and initialize the class if needed
00179 //
00180 //-------------------------------------------------------------------------
00181 LPCWSTR nsButton::WindowClassW()
00182 {
00183   return L"BUTTON";
00184 }
00185 
00186 //-------------------------------------------------------------------------
00187 //
00188 // return the window class name and initialize the class if needed
00189 //
00190 //-------------------------------------------------------------------------
00191 LPCTSTR nsButton::WindowClass()
00192 {
00193   return "BUTTON";
00194 }
00195 
00196 //-------------------------------------------------------------------------
00197 //
00198 // return window styles
00199 //
00200 //-------------------------------------------------------------------------
00201 DWORD nsButton::WindowStyle()
00202 { 
00203   return WS_CHILD | WS_CLIPSIBLINGS; 
00204 }
00205 
00206 //-------------------------------------------------------------------------
00207 //
00208 // return window extended styles
00209 //
00210 //-------------------------------------------------------------------------
00211 DWORD nsButton::WindowExStyle()
00212 {
00213   return 0;
00214 }
00215 
00220 NS_METHOD nsButton::Paint(nsIRenderingContext& aRenderingContext,
00221                           const nsRect& aDirtyRect)
00222 {
00223   float appUnits;
00224   float devUnits;
00225   float scale;
00226   nsIDeviceContext * context;
00227   aRenderingContext.GetDeviceContext(context);
00228 
00229   context->GetCanonicalPixelScale(scale);
00230   devUnits = context->AppUnitsToDevUnits();
00231   appUnits = context->DevUnitsToAppUnits();
00232 
00233   nsRect rect;
00234   GetBoundsAppUnits(rect, appUnits);
00235   aRenderingContext.SetColor(NS_RGB(0,0,0));
00236 
00237   nscolor bgColor  = NS_RGB(255,255,255);
00238   nscolor fgColor  = NS_RGB(0,0,0);
00239   nscolor hltColor = NS_RGB(240,240,240);
00240   nscolor sdwColor = NS_RGB(128,128,128);
00241 
00242   {
00243     nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService(kLookAndFeelCID);
00244     if (lookAndFeel) {
00245       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetBackground,  bgColor);
00246       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetForeground,  fgColor);
00247       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DShadow,    sdwColor);
00248       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DHighlight, hltColor);
00249     }
00250   }
00251 
00252   aRenderingContext.SetColor(bgColor);
00253   aRenderingContext.FillRect(rect);
00254 
00255   /*aRenderingContext.SetColor(bgColor);
00256   for (int i=0;i<int(scale);i++) {
00257     aRenderingContext.DrawRect(rect);
00258     rect.x      += 3; 
00259     rect.y      += 3;
00260     rect.width  -= 6; 
00261     rect.height -= 6;
00262   }*/
00263 
00264   nscoord onePixel  = nscoord(scale);
00265   nscoord twoPixels = nscoord(scale*2);
00266 
00267   rect.x      += onePixel; 
00268   rect.y      += onePixel;
00269   rect.width  -= twoPixels; 
00270   rect.height -= twoPixels;
00271 
00272   nscoord right     = rect.x+rect.width;
00273   nscoord bottom    = rect.y+rect.height;
00274 
00275 
00276   // Draw Left & Top
00277   aRenderingContext.SetColor(NS_RGB(225,225,225));
00278   DrawScaledLine(aRenderingContext, rect.x, rect.y, right, rect.y, scale, appUnits, PR_TRUE); // top
00279   DrawScaledLine(aRenderingContext, rect.x, rect.y, rect.x, bottom, scale, appUnits, PR_FALSE); // left
00280 
00281   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, right-onePixel, rect.y+onePixel, scale, appUnits, PR_TRUE); // top + 1
00282   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, rect.x+onePixel, bottom-onePixel, scale, appUnits, PR_FALSE); // left + 1
00283 
00284   // Draw Right & Bottom
00285   aRenderingContext.SetColor(NS_RGB(128,128,128));
00286   DrawScaledLine(aRenderingContext, right, rect.y+onePixel, right, bottom, scale, appUnits, PR_FALSE); // right 
00287   DrawScaledLine(aRenderingContext, rect.x+onePixel, bottom, right, bottom, scale, appUnits, PR_TRUE); // bottom
00288 
00289   //DrawScaledLine(aRenderingContext, right-onePixel, rect.y+twoPixels, right-onePixel, bottom, scale, appUnits, PR_FALSE); // right + 1
00290   //DrawScaledLine(aRenderingContext, rect.x+twoPixels, bottom-onePixel, right, bottom-onePixel, scale, appUnits, PR_TRUE); // bottom + 1
00291  
00292 
00293   nsIFontMetrics* metrics;
00294   context->GetMetricsFor(*mFont, metrics);
00295   aRenderingContext.SetFont(metrics);
00296 
00297   nscoord textWidth;
00298   nscoord textHeight;
00299   aRenderingContext.GetWidth(mLabel, textWidth);
00300 
00301   metrics->GetMaxAscent(textHeight);
00302 
00303   nscoord x = ((rect.width  - textWidth) / 2)  + rect.x;
00304   nscoord y = ((rect.height - textHeight) / 2) + rect.y;
00305   aRenderingContext.DrawString(mLabel, x, y + textHeight);
00306 
00307 
00308   NS_RELEASE(context);
00309   return NS_OK;
00310 }
00311 
00312