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  *   <bstell@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <gtk/gtk.h>
00040 
00041 #include "nsLabel.h"
00042 #include "nsString.h"
00043 
00044 NS_IMPL_ADDREF_INHERITED(nsLabel, nsWidget)
00045 NS_IMPL_RELEASE_INHERITED(nsLabel, nsWidget)
00046 NS_IMPL_QUERY_INTERFACE2(nsLabel, nsILabel, nsIWidget)
00047 
00048 //-------------------------------------------------------------------------
00049 //
00050 // nsLabel constructor
00051 //
00052 //-------------------------------------------------------------------------
00053 nsLabel::nsLabel() : nsWidget(), nsILabel()
00054 {
00055   mAlignment = eAlign_Left;
00056 }
00057 
00058 //-------------------------------------------------------------------------
00059 //
00060 // nsLabel destructor
00061 //
00062 //-------------------------------------------------------------------------
00063 nsLabel::~nsLabel()
00064 {
00065 }
00066 
00067 
00068 //-------------------------------------------------------------------------
00069 //
00070 // Create the nativeLabel widget
00071 //
00072 //-------------------------------------------------------------------------
00073 NS_IMETHODIMP  nsLabel::CreateNative(GtkObject *parentWindow)
00074 {
00075   unsigned char alignment = GetNativeAlignment();
00076 
00077   mWidget = gtk_label_new("");
00078   gtk_widget_set_name(mWidget, "nsLabel");
00079   gtk_misc_set_alignment(GTK_MISC(mWidget), 0.0, alignment);
00080 
00081   return NS_OK;
00082 }
00083 
00084 //-------------------------------------------------------------------------
00085 //
00086 //
00087 //-------------------------------------------------------------------------
00088 NS_IMETHODIMP nsLabel::PreCreateWidget(nsWidgetInitData *aInitData)
00089 {
00090   if (nsnull != aInitData) {
00091     nsLabelInitData* data = (nsLabelInitData *) aInitData;
00092     mAlignment = data->mAlignment;
00093   }
00094   return NS_OK;
00095 }
00096 
00097 //-------------------------------------------------------------------------
00098 //
00099 // Set alignment
00100 //
00101 //-------------------------------------------------------------------------
00102 NS_IMETHODIMP nsLabel::SetAlignment(nsLabelAlignment aAlignment)
00103 {
00104   GtkJustification align;
00105 
00106   mAlignment = aAlignment;
00107 
00108   align = GetNativeAlignment();
00109   gtk_misc_set_alignment(GTK_MISC(mWidget), 0.0, align);
00110   return NS_OK;
00111 }
00112 
00113 //-------------------------------------------------------------------------
00114 //
00115 //
00116 //
00117 //-------------------------------------------------------------------------
00118 GtkJustification nsLabel::GetNativeAlignment()
00119 {
00120   switch (mAlignment) {
00121     case eAlign_Right : return GTK_JUSTIFY_RIGHT;
00122     case eAlign_Left  : return GTK_JUSTIFY_LEFT;
00123     case eAlign_Center: return GTK_JUSTIFY_CENTER;
00124     default :
00125       return GTK_JUSTIFY_LEFT;
00126   }
00127   return GTK_JUSTIFY_LEFT;
00128 }
00129 
00130 //-------------------------------------------------------------------------
00131 //
00132 // Set this button label
00133 //
00134 //-------------------------------------------------------------------------
00135 NS_METHOD nsLabel::SetLabel(const nsString& aText)
00136 {
00137   gtk_label_set(GTK_LABEL(mWidget),
00138                 NS_LossyConvertUCS2toASCII(aText).get());
00139   return NS_OK;
00140 
00141 }
00142 
00143 //-------------------------------------------------------------------------
00144 //
00145 // Get this button label
00146 //
00147 //-------------------------------------------------------------------------
00148 NS_METHOD nsLabel::GetLabel(nsString& aBuffer)
00149 {
00150   char * text;
00151   gtk_label_get(GTK_LABEL(mWidget), &text);
00152   aBuffer.SetLength(0);
00153   aBuffer.AppendWithConversion(text);
00154   return NS_OK;
00155 }