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