Back to index

lightning-sunbird  0.9+nobinonly
nsCheckButton.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 "nsCheckButton.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 
00052 //-------------------------------------------------------------------------
00053 nsresult
00054 NS_NewCheckButton(nsICheckButton** aControl)
00055 {
00056   NS_PRECONDITION(aControl, "null OUT ptr");
00057   if (nsnull == aControl) {
00058     return NS_ERROR_NULL_POINTER;
00059   }
00060   nsCheckButton* it = new nsCheckButton;
00061   if (!it) {
00062     return NS_ERROR_OUT_OF_MEMORY;
00063   }
00064   NS_ADDREF(it);
00065   *aControl = (nsICheckButton*)it;
00066   return NS_OK;
00067 }
00068 
00069 
00070 NS_IMPL_ADDREF(nsCheckButton)
00071 NS_IMPL_RELEASE(nsCheckButton)
00072 
00073 //-------------------------------------------------------------------------
00074 //
00075 // nsCheckButton constructor
00076 //
00077 //-------------------------------------------------------------------------
00078 nsCheckButton::nsCheckButton() : nsWindow() , nsICheckButton(),
00079   mState(PR_FALSE)
00080 {
00081 }
00082 
00083 
00084 //-------------------------------------------------------------------------
00085 //
00086 // nsCheckButton destructor
00087 //
00088 //-------------------------------------------------------------------------
00089 nsCheckButton::~nsCheckButton()
00090 {
00091 }
00092 
00093 
00102 nsresult nsCheckButton::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00103 {
00104     if (NULL == aInstancePtr) {
00105         return NS_ERROR_NULL_POINTER;
00106     }
00107 
00108     static NS_DEFINE_IID(kICheckButtonIID, NS_ICHECKBUTTON_IID);
00109     if (aIID.Equals(kICheckButtonIID)) {
00110         *aInstancePtr = (void*) ((nsICheckButton*)this);
00111         NS_ADDREF_THIS();
00112         return NS_OK;
00113     }
00114     return nsWindow::QueryInterface(aIID,aInstancePtr);
00115 }
00116 
00117 
00118 //-------------------------------------------------------------------------
00119 //
00120 // Set this button label
00121 //
00122 //-------------------------------------------------------------------------
00123 NS_METHOD nsCheckButton::SetState(const PRBool aState)
00124 {
00125   mState = aState;
00126   if (mWnd) {
00127     BOOL state;
00128     if (aState) 
00129         state = BST_CHECKED;
00130     else
00131         state = BST_UNCHECKED;
00132     ::SendMessage(mWnd, BM_SETCHECK, (WPARAM)state, (LPARAM)0);
00133   }
00134   return NS_OK;
00135 }
00136 
00137 //-------------------------------------------------------------------------
00138 //
00139 // Set this button label
00140 //
00141 //-------------------------------------------------------------------------
00142 NS_METHOD nsCheckButton::GetState(PRBool& aState)
00143 {
00144   if (::SendMessage(mWnd, BM_GETCHECK, (WPARAM)0, (LPARAM)0) == BST_CHECKED)
00145       aState = PR_TRUE;
00146   else
00147       aState = PR_FALSE;
00148   return NS_OK;
00149 }
00150 
00151 //-------------------------------------------------------------------------
00152 //
00153 // Set this button label
00154 //
00155 //-------------------------------------------------------------------------
00156 NS_METHOD nsCheckButton::SetLabel(const nsString& aText)
00157 {
00158   char label[256];
00159   aText.ToCString(label, 256);
00160   label[255] = '\0';
00161   VERIFY(::SetWindowText(mWnd, label));
00162   return NS_OK;
00163 }
00164 
00165 
00166 //-------------------------------------------------------------------------
00167 //
00168 // Get this button label
00169 //
00170 //-------------------------------------------------------------------------
00171 NS_METHOD nsCheckButton::GetLabel(nsString& aBuffer)
00172 {
00173   int actualSize = ::GetWindowTextLength(mWnd)+1;
00174   NS_ALLOC_CHAR_BUF(label, 256, actualSize);
00175   ::GetWindowText(mWnd, label, actualSize);
00176   aBuffer.SetLength(0);
00177   aBuffer.AppendWithConversion(label);
00178   NS_FREE_CHAR_BUF(label);
00179   return NS_OK;
00180 }
00181 
00182 //-------------------------------------------------------------------------
00183 //
00184 // move, paint, resizes message - ignore
00185 //
00186 //-------------------------------------------------------------------------
00187 PRBool nsCheckButton::OnMove(PRInt32, PRInt32)
00188 {
00189   return PR_FALSE;
00190 }
00191 
00192 PRBool nsCheckButton::OnPaint()
00193 {
00194     return PR_FALSE;
00195 }
00196 
00197 PRBool nsCheckButton::OnResize(nsRect &aWindowRect)
00198 {
00199     return PR_FALSE;
00200 }
00201 
00202 //-------------------------------------------------------------------------
00203 //
00204 // return the window class name and initialize the class if needed
00205 //
00206 //-------------------------------------------------------------------------
00207 LPCWSTR nsCheckButton::WindowClassW()
00208 {
00209     return L"BUTTON";
00210 }
00211 
00212 //-------------------------------------------------------------------------
00213 //
00214 // return the window class name and initialize the class if needed
00215 //
00216 //-------------------------------------------------------------------------
00217 LPCTSTR nsCheckButton::WindowClass()
00218 {
00219     return "BUTTON";
00220 }
00221 
00222 
00223 //-------------------------------------------------------------------------
00224 //
00225 // return window styles
00226 //
00227 //-------------------------------------------------------------------------
00228 DWORD nsCheckButton::WindowStyle()
00229 {
00230     return BS_CHECKBOX | WS_CHILD | WS_CLIPSIBLINGS; 
00231 }
00232 
00233 
00234 //-------------------------------------------------------------------------
00235 //
00236 // return window extended styles
00237 //
00238 //-------------------------------------------------------------------------
00239 DWORD nsCheckButton::WindowExStyle()
00240 {
00241     return 0;
00242 }
00243 
00244 
00245 
00250 NS_METHOD nsCheckButton::Paint(nsIRenderingContext& aRenderingContext,
00251                                const nsRect&        aDirtyRect)
00252 {
00253   nsRect rect;
00254   float  appUnits;
00255   float  scale;
00256   nsIDeviceContext * context;
00257   aRenderingContext.GetDeviceContext(context);
00258 
00259   context->GetCanonicalPixelScale(scale);
00260   appUnits = context->DevUnitsToAppUnits();
00261 
00262   GetBoundsAppUnits(rect, appUnits);
00263 
00264   nscoord one   = nscoord(PRFloat64(rect.height) * 1.0/20.0);
00265   nscoord three = nscoord(PRFloat64(rect.width)  * 3.0/20.0);
00266   nscoord five  = nscoord(PRFloat64(rect.width)  * 5.0/20.0);
00267   nscoord six   = nscoord(PRFloat64(rect.height) * 5.0/20.0);
00268   nscoord eight = nscoord(PRFloat64(rect.height) * 7.0/20.0);
00269   nscoord nine  = nscoord(PRFloat64(rect.width)  * 9.0/20.0);
00270   nscoord ten   = nscoord(PRFloat64(rect.height) * 9.0/20.0);
00271 
00272   rect.x      += three;
00273   rect.y      += nscoord(PRFloat64(rect.height) * 3.5 /20.0);
00274   rect.width  = nscoord(PRFloat64(rect.width) * 12.0/20.0);
00275   rect.height = nscoord(PRFloat64(rect.height) * 12.0/20.0);
00276 
00277   aRenderingContext.SetColor(NS_RGB(0,0,0));
00278 
00279   nscoord onePixel  = nscoord((appUnits+0.6F));
00280   DrawScaledRect(aRenderingContext, rect, scale, appUnits);
00281   nscoord x = rect.x;
00282   nscoord y = rect.y;
00283 
00284   if (mState) {
00285     nscoord inc   = nscoord(PRFloat64(rect.height) *   0.75/20.0);
00286     nscoord yy = 0;
00287     for (nscoord i=0;i<4;i++) {
00288       DrawScaledLine(aRenderingContext, x+three, y+eight+yy,  x+five, y+ten+yy, scale, appUnits, PR_FALSE); // top
00289       DrawScaledLine(aRenderingContext, x+five,  y+ten+yy,    x+nine, y+six+yy, scale, appUnits, PR_FALSE); // top
00290       //aRenderingContext.DrawLine(x+three, y+eight+yy,  x+five, y+ten+yy);
00291       //aRenderingContext.DrawLine(x+five,  y+ten+yy,    x+nine, y+six+yy);
00292       yy += nscoord(scale);
00293     }
00294   }
00295 
00296   NS_RELEASE(context);
00297   return NS_OK;
00298 }
00299