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 <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_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   ::WinSetWindowText(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 PCSZ nsButton::WindowClass()
00182 {
00183   return WC_BUTTON_STRING;
00184 }
00185 
00186 //-------------------------------------------------------------------------
00187 //
00188 // return window styles
00189 //
00190 //-------------------------------------------------------------------------
00191 ULONG nsButton::WindowStyle()
00192 { 
00193   return  WS_CLIPSIBLINGS | BS_PUSHBUTTON; 
00194 }
00195 
00196 //-------------------------------------------------------------------------
00197 //
00198 // return window extended styles
00199 //
00200 //-------------------------------------------------------------------------
00201 ULONG nsButton::WindowExStyle()
00202 {
00203   return 0;
00204 }
00205 
00210 NS_METHOD nsButton::Paint(nsIRenderingContext& aRenderingContext,
00211                           const nsRect& aDirtyRect)
00212 {
00213   float appUnits;
00214   float devUnits;
00215   float scale;
00216   nsIDeviceContext * context;
00217   aRenderingContext.GetDeviceContext(context);
00218 
00219   context->GetCanonicalPixelScale(scale);
00220   devUnits = context->AppUnitsToDevUnits();
00221   appUnits = context->DevUnitsToAppUnits();
00222 
00223   nsRect rect;
00224   GetBoundsAppUnits(rect, appUnits);
00225   aRenderingContext.SetColor(NS_RGB(0,0,0));
00226 
00227   nscolor bgColor  = NS_RGB(255,255,255);
00228   nscolor fgColor  = NS_RGB(0,0,0);
00229   nscolor hltColor = NS_RGB(240,240,240);
00230   nscolor sdwColor = NS_RGB(128,128,128);
00231 
00232   {
00233     nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService(kLookAndFeelCID);
00234     if (lookAndFeel) {
00235       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetBackground,  bgColor);
00236       lookAndFeel->GetColor(nsILookAndFeel::eColor_WidgetForeground,  fgColor);
00237       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DShadow,    sdwColor);
00238       lookAndFeel->GetColor(nsILookAndFeel::eColor_Widget3DHighlight, hltColor);
00239     }
00240   }
00241 
00242   aRenderingContext.SetColor(bgColor);
00243   aRenderingContext.FillRect(rect);
00244 
00245   /*aRenderingContext.SetColor(bgColor);
00246   for (int i=0;i<int(scale);i++) {
00247     aRenderingContext.DrawRect(rect);
00248     rect.x      += 3; 
00249     rect.y      += 3;
00250     rect.width  -= 6; 
00251     rect.height -= 6;
00252   }*/
00253 
00254   nscoord onePixel  = nscoord(scale);
00255   nscoord twoPixels = nscoord(scale*2);
00256 
00257   rect.x      += onePixel; 
00258   rect.y      += onePixel;
00259   rect.width  -= twoPixels; 
00260   rect.height -= twoPixels;
00261 
00262   nscoord right     = rect.x+rect.width;
00263   nscoord bottom    = rect.y+rect.height;
00264 
00265 
00266   // Draw Left & Top
00267   aRenderingContext.SetColor(NS_RGB(225,225,225));
00268   DrawScaledLine(aRenderingContext, rect.x, rect.y, right, rect.y, scale, appUnits, PR_TRUE); // top
00269   DrawScaledLine(aRenderingContext, rect.x, rect.y, rect.x, bottom, scale, appUnits, PR_FALSE); // left
00270 
00271   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, right-onePixel, rect.y+onePixel, scale, appUnits, PR_TRUE); // top + 1
00272   //DrawScaledLine(aRenderingContext, rect.x+onePixel, rect.y+onePixel, rect.x+onePixel, bottom-onePixel, scale, appUnits, PR_FALSE); // left + 1
00273 
00274   // Draw Right & Bottom
00275   aRenderingContext.SetColor(NS_RGB(128,128,128));
00276   DrawScaledLine(aRenderingContext, right, rect.y+onePixel, right, bottom, scale, appUnits, PR_FALSE); // right 
00277   DrawScaledLine(aRenderingContext, rect.x+onePixel, bottom, right, bottom, scale, appUnits, PR_TRUE); // bottom
00278 
00279   //DrawScaledLine(aRenderingContext, right-onePixel, rect.y+twoPixels, right-onePixel, bottom, scale, appUnits, PR_FALSE); // right + 1
00280   //DrawScaledLine(aRenderingContext, rect.x+twoPixels, bottom-onePixel, right, bottom-onePixel, scale, appUnits, PR_TRUE); // bottom + 1
00281  
00282 
00283   nsIFontMetrics* metrics;
00284   context->GetMetricsFor(*mFont, metrics);
00285   aRenderingContext.SetFont(metrics);
00286 
00287   nscoord textWidth;
00288   nscoord textHeight;
00289   aRenderingContext.GetWidth(mLabel, textWidth);
00290 
00291   metrics->GetMaxAscent(textHeight);
00292 
00293   nscoord x = ((rect.width  - textWidth) / 2)  + rect.x;
00294   nscoord y = ((rect.height - textHeight) / 2) + rect.y;
00295   aRenderingContext.DrawString(mLabel, x, y + textHeight);
00296 
00297 
00298   NS_RELEASE(context);
00299   return NS_OK;
00300 }
00301 
00302