Back to index

lightning-sunbird  0.9+nobinonly
nsLabel.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 "nsLabel.h"
00039 #include "nsILabel.h"
00040 #include "nsToolkit.h"
00041 #include "nsColor.h"
00042 #include "nsGUIEvent.h"
00043 #include "nsString.h"
00044 #include "nsStringUtil.h"
00045 #include "nsIFontMetrics.h"
00046 #include "nsIDeviceContext.h"
00047 #include <windows.h>
00048 
00049 //-------------------------------------------------------------------------
00050 nsresult
00051 NS_NewLabel(nsILabel** aControl)
00052 {
00053   NS_PRECONDITION(aControl, "null OUT ptr");
00054   if (nsnull == aControl) {
00055     return NS_ERROR_NULL_POINTER;
00056   }
00057   nsLabel* it = new nsLabel;
00058   if (!it) {
00059     return NS_ERROR_OUT_OF_MEMORY;
00060   }
00061   NS_ADDREF(it);
00062   *aControl = (nsILabel*)it;
00063   return NS_OK;
00064 }
00065 
00066 NS_IMPL_ADDREF(nsLabel)
00067 NS_IMPL_RELEASE(nsLabel)
00068 
00069 //-------------------------------------------------------------------------
00070 //
00071 // nsLabel constructor
00072 //
00073 //-------------------------------------------------------------------------
00074 nsLabel::nsLabel() : nsWindow(), nsILabel()
00075 {
00076   mAlignment = eAlign_Left;
00077 }
00078 
00079 //-------------------------------------------------------------------------
00080 //
00081 //
00082 //-------------------------------------------------------------------------
00083 NS_METHOD nsLabel::PreCreateWidget(nsWidgetInitData *aInitData)
00084 {
00085   if (nsnull != aInitData) {
00086     nsLabelInitData* data = (nsLabelInitData *) aInitData;
00087     mAlignment = data->mAlignment;
00088   }
00089   return NS_OK;
00090 }
00091 
00092 //-------------------------------------------------------------------------
00093 //
00094 // nsLabel destructor
00095 //
00096 //-------------------------------------------------------------------------
00097 nsLabel::~nsLabel()
00098 {
00099 }
00100 
00101 //-------------------------------------------------------------------------
00102 //
00103 // Query interface implementation
00104 //
00105 //-------------------------------------------------------------------------
00106 nsresult nsLabel::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00107 {
00108   nsresult result = nsWindow::QueryInterface(aIID, aInstancePtr);
00109 
00110   static NS_DEFINE_IID(kILabelIID, NS_ILABEL_IID);
00111   if (result == NS_NOINTERFACE && aIID.Equals(kILabelIID)) {
00112       *aInstancePtr = (void*) ((nsILabel*)this);
00113       NS_ADDREF_THIS();
00114       result = NS_OK;
00115   }
00116 
00117   return result;
00118 }
00119 
00120 //-------------------------------------------------------------------------
00121 //
00122 // Set this button label
00123 //
00124 //-------------------------------------------------------------------------
00125 NS_METHOD nsLabel::SetAlignment(nsLabelAlignment aAlignment)
00126 {
00127   mAlignment = aAlignment;
00128   return NS_OK;
00129 }
00130 
00131 //-------------------------------------------------------------------------
00132 //
00133 // Set this button label
00134 //
00135 //-------------------------------------------------------------------------
00136 NS_METHOD nsLabel::SetLabel(const nsString& aText)
00137 {
00138   VERIFY(::SetWindowText(mWnd, NS_LossyConvertUCS2toASCII(aText).get()));
00139   return NS_OK;
00140 }
00141 
00142 //-------------------------------------------------------------------------
00143 //
00144 // Get this button label
00145 //
00146 //-------------------------------------------------------------------------
00147 NS_METHOD nsLabel::GetLabel(nsString& aBuffer)
00148 {
00149   int actualSize = ::GetWindowTextLength(mWnd)+1;
00150   NS_ALLOC_CHAR_BUF(label, 256, actualSize);
00151   ::GetWindowText(mWnd, label, actualSize);
00152   aBuffer.SetLength(0);
00153   aBuffer.AppendWithConversion(label);
00154   NS_FREE_CHAR_BUF(label);
00155   return NS_OK;
00156 }
00157 
00158 //-------------------------------------------------------------------------
00159 //
00160 // move, paint, resizes message - ignore
00161 //
00162 //-------------------------------------------------------------------------
00163 PRBool nsLabel::OnMove(PRInt32, PRInt32)
00164 {
00165   return PR_FALSE;
00166 }
00167 
00168 PRBool nsLabel::OnPaint()
00169 {
00170   //printf("** nsLabel::OnPaint **\n");
00171   return PR_FALSE;
00172 }
00173 
00174 PRBool nsLabel::OnResize(nsRect &aWindowRect)
00175 {
00176     return PR_FALSE;
00177 }
00178 
00179 //-------------------------------------------------------------------------
00180 //
00181 // return the window class name and initialize the class if needed
00182 //
00183 //-------------------------------------------------------------------------
00184 LPCWSTR nsLabel::WindowClassW()
00185 {
00186   return L"STATIC";
00187 }
00188 
00189 //-------------------------------------------------------------------------
00190 //
00191 // return the window class name and initialize the class if needed
00192 //
00193 //-------------------------------------------------------------------------
00194 LPCTSTR nsLabel::WindowClass()
00195 {
00196   return "STATIC";
00197 }
00198 
00199 //-------------------------------------------------------------------------
00200 //
00201 // return window styles
00202 //
00203 //-------------------------------------------------------------------------
00204 DWORD nsLabel::WindowStyle()
00205 { 
00206   DWORD style =  WS_CHILD | WS_CLIPSIBLINGS; 
00207   switch (mAlignment) {
00208     case eAlign_Right : style |= SS_RIGHT; break;
00209     case eAlign_Left  : style |= SS_LEFT;  break;
00210     case eAlign_Center: style |= SS_CENTER;break;
00211     default :
00212       style |= SS_LEFT; 
00213   }
00214   return style;
00215 }
00216 
00217 //-------------------------------------------------------------------------
00218 //
00219 // return window extended styles
00220 //
00221 //-------------------------------------------------------------------------
00222 DWORD nsLabel::WindowExStyle()
00223 {
00224   return 0;
00225 }
00226 
00227 
00228 //-------------------------------------------------------------------------
00229 //
00230 // get position/dimensions
00231 //
00232 //-------------------------------------------------------------------------
00233 
00234 NS_METHOD nsLabel::GetBounds(nsRect &aRect)
00235 {
00236   return nsWindow::GetBounds(aRect);
00237 }
00238 
00239 //-------------------------------------------------------------------------
00240 NS_METHOD nsLabel::GetPreferredSize(PRInt32& aWidth, PRInt32& aHeight)
00241 {
00242   if (nsnull == mContext) {
00243     return NS_ERROR_FAILURE;
00244   }
00245   //nsIFontMetrics * fm = GetFont();;
00246  // mDeviceContext->GetMetricsFor(mFont, &fm);
00247 
00248   nsIFontMetrics* metrics;
00249   mContext->GetMetricsFor(*mFont, metrics);
00250 
00251   nsString text;
00252   GetLabel(text);
00253 
00254   nsIRenderingContext *cx;
00255   mContext->CreateRenderingContext(this, cx);
00256   cx->SetFont(metrics);
00257   nscoord string_height, string_width;
00258   metrics->GetHeight(string_height);
00259   cx->GetWidth(text, string_width);
00260   NS_RELEASE(cx);
00261   NS_RELEASE(metrics);
00262 
00263   if (mPreferredWidth != 0) {
00264     aWidth = mPreferredWidth;
00265   } else {
00266     aWidth = string_width+8;
00267   }
00268 
00269   if (mPreferredHeight != 0) {
00270     aHeight = mPreferredHeight;
00271   } else {
00272     aHeight = string_height+8;
00273   }
00274 
00275   return NS_OK;
00276 }
00277 
00278 //-------------------------------------------------------------------------
00279 NS_METHOD nsLabel::SetPreferredSize(PRInt32 aWidth, PRInt32 aHeight)
00280 {
00281   mPreferredWidth  = aWidth;
00282   mPreferredHeight = aHeight;
00283   return NS_OK;
00284 }
00285