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 <os2.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   WinSetWindowText(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 = ::WinQueryWindowTextLength(mWnd)+1;
00150   NS_ALLOC_CHAR_BUF(label, 256, actualSize);
00151   ::WinQueryWindowText(mWnd, actualSize, label);
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 PCSZ nsLabel::WindowClass()
00185 {
00186   return WC_STATIC_STRING;
00187 }
00188 
00189 //-------------------------------------------------------------------------
00190 //
00191 // return window styles
00192 //
00193 //-------------------------------------------------------------------------
00194 ULONG nsLabel::WindowStyle()
00195 { 
00196   ULONG style =  WS_CLIPSIBLINGS | SS_TEXT; 
00197   switch (mAlignment) {
00198     case eAlign_Right : style |= DT_RIGHT; break;
00199     case eAlign_Left  : style |= DT_LEFT;  break;
00200     case eAlign_Center: style |= DT_CENTER;break;
00201     default :
00202       style |= DT_LEFT; 
00203   }
00204   return style;
00205 }
00206 
00207 //-------------------------------------------------------------------------
00208 //
00209 // return window extended styles
00210 //
00211 //-------------------------------------------------------------------------
00212 ULONG nsLabel::WindowExStyle()
00213 {
00214   return 0;
00215 }
00216 
00217 
00218 //-------------------------------------------------------------------------
00219 //
00220 // get position/dimensions
00221 //
00222 //-------------------------------------------------------------------------
00223 
00224 NS_METHOD nsLabel::GetBounds(nsRect &aRect)
00225 {
00226   return nsWindow::GetBounds(aRect);
00227 }
00228 
00229 //-------------------------------------------------------------------------
00230 NS_METHOD nsLabel::GetPreferredSize(PRInt32& aWidth, PRInt32& aHeight)
00231 {
00232   if (nsnull == mContext) {
00233     return NS_ERROR_FAILURE;
00234   }
00235   //nsIFontMetrics * fm = GetFont();;
00236  // mDeviceContext->GetMetricsFor(mFont, &fm);
00237 
00238   nsIFontMetrics* metrics;
00239   mContext->GetMetricsFor(*mFont, metrics);
00240 
00241   nsString text;
00242   GetLabel(text);
00243 
00244   nsIRenderingContext *cx;
00245   mContext->CreateRenderingContext(this, cx);
00246   cx->SetFont(metrics);
00247   nscoord string_height, string_width;
00248   metrics->GetHeight(string_height);
00249   cx->GetWidth(text, string_width);
00250   NS_RELEASE(cx);
00251   NS_RELEASE(metrics);
00252 
00253   if (mPreferredWidth != 0) {
00254     aWidth = mPreferredWidth;
00255   } else {
00256     aWidth = string_width+8;
00257   }
00258 
00259   if (mPreferredHeight != 0) {
00260     aHeight = mPreferredHeight;
00261   } else {
00262     aHeight = string_height+8;
00263   }
00264 
00265   return NS_OK;
00266 }
00267 
00268 //-------------------------------------------------------------------------
00269 NS_METHOD nsLabel::SetPreferredSize(PRInt32 aWidth, PRInt32 aHeight)
00270 {
00271   mPreferredWidth  = aWidth;
00272   mPreferredHeight = aHeight;
00273   return NS_OK;
00274 }
00275