Back to index

lightning-sunbird  0.9+nobinonly
nsTextHelper.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 the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 <gtk/gtk.h>
00039 
00040 #include "nsTextHelper.h"
00041 #include "nsTextWidget.h"
00042 #include "nsString.h"
00043 
00044 NS_IMPL_ADDREF_INHERITED(nsTextHelper, nsWidget)
00045 NS_IMPL_RELEASE_INHERITED(nsTextHelper, nsWidget)
00046 
00047 //-------------------------------------------------------------------------
00048 //
00049 // nsTextHelper constructor
00050 //
00051 //-------------------------------------------------------------------------
00052 
00053 nsTextHelper::nsTextHelper() : nsWidget(), nsITextWidget()
00054 {
00055   mIsReadOnly = PR_FALSE;
00056   mIsPassword = PR_FALSE;
00057 }
00058 
00059 //-------------------------------------------------------------------------
00060 //
00061 // nsTextHelper destructor
00062 //
00063 //-------------------------------------------------------------------------
00064 nsTextHelper::~nsTextHelper()
00065 {
00066 }
00067 
00068 //-------------------------------------------------------------------------
00069 //
00070 // Set initial parameters
00071 //
00072 //-------------------------------------------------------------------------
00073 NS_IMETHODIMP nsTextHelper::PreCreateWidget(nsWidgetInitData *aInitData)
00074 {
00075   if (nsnull != aInitData) {
00076     nsTextWidgetInitData* data = (nsTextWidgetInitData *) aInitData;
00077     mIsPassword = data->mIsPassword;
00078     mIsReadOnly = data->mIsReadOnly;
00079   }
00080   return NS_OK;
00081 }
00082 
00083 //-------------------------------------------------------------------------
00084 NS_IMETHODIMP nsTextHelper::SetMaxTextLength(PRUint32 aChars)
00085 {
00086   // This is a normal entry only thing, not a text box
00087   gtk_entry_set_max_length(GTK_ENTRY(mTextWidget), (int)aChars);
00088   return NS_OK;
00089 }
00090 
00091 //-------------------------------------------------------------------------
00092 NS_IMETHODIMP  nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize, PRUint32& aActualSize)
00093 {
00094   char *str = nsnull;
00095   if (GTK_IS_ENTRY(mTextWidget))
00096     {
00097       str = gtk_entry_get_text(GTK_ENTRY(mTextWidget));
00098     }
00099   else if (GTK_IS_TEXT(mTextWidget))
00100     {
00101       str = gtk_editable_get_chars (GTK_EDITABLE (mTextWidget), 0,
00102                                     gtk_text_get_length (GTK_TEXT (mTextWidget)));
00103     }
00104   aTextBuffer.SetLength(0);
00105   aTextBuffer.AppendWithConversion(str);
00106   PRUint32 len = (PRUint32)strlen(str);
00107   aActualSize = len;
00108 
00109   return NS_OK;
00110 }
00111 
00112 //-------------------------------------------------------------------------
00113 NS_IMETHODIMP  nsTextHelper::SetText(const nsString& aText, PRUint32& aActualSize)
00114 {
00115   if (GTK_IS_ENTRY(mTextWidget)) {
00116     gtk_entry_set_text(GTK_ENTRY(mTextWidget),
00117                        (const gchar *)NS_LossyConvertUCS2toASCII(aText).get());
00118   } else if (GTK_IS_TEXT(mTextWidget)) {
00119     gtk_editable_delete_text(GTK_EDITABLE(mTextWidget), 0,
00120                              gtk_text_get_length(GTK_TEXT (mTextWidget)));
00121     gtk_text_insert(GTK_TEXT(mTextWidget),
00122                     nsnull, nsnull, nsnull,
00123                     NS_LossyConvertUCS2toASCII(aText).get(),
00124                     aText.Length());
00125   }
00126 
00127   aActualSize = aText.Length();
00128 
00129   return NS_OK;
00130 }
00131 
00132 //-------------------------------------------------------------------------
00133 NS_IMETHODIMP  nsTextHelper::InsertText(const nsString &aText,
00134                                     PRUint32 aStartPos,
00135                                     PRUint32 aEndPos,
00136                                     PRUint32& aActualSize)
00137 {
00138   gtk_editable_insert_text(GTK_EDITABLE(mTextWidget),
00139                            (const gchar *)NS_LossyConvertUCS2toASCII(aText).get(),
00140                            (gint)aText.Length(), (gint*)&aStartPos);
00141 
00142   aActualSize = aText.Length();
00143 
00144   return NS_OK;
00145 }
00146 
00147 //-------------------------------------------------------------------------
00148 NS_IMETHODIMP  nsTextHelper::RemoveText()
00149 {
00150   if (GTK_IS_ENTRY(mTextWidget)) {
00151     gtk_entry_set_text(GTK_ENTRY(mTextWidget), "");
00152   } else if (GTK_IS_TEXT(mTextWidget)) {
00153     gtk_editable_delete_text(GTK_EDITABLE(mTextWidget), 0,
00154                              gtk_text_get_length(GTK_TEXT (mTextWidget)));
00155   }
00156   return NS_OK;
00157 }
00158 
00159 //-------------------------------------------------------------------------
00160 NS_IMETHODIMP  nsTextHelper::SetPassword(PRBool aIsPassword)
00161 {
00162   mIsPassword = aIsPassword?PR_FALSE:PR_TRUE;
00163   if (GTK_IS_ENTRY(mTextWidget)) {
00164     gtk_entry_set_visibility(GTK_ENTRY(mTextWidget), mIsPassword);
00165   }
00166   // this won't work for gtk_texts
00167   return NS_OK;
00168 }
00169 
00170 //-------------------------------------------------------------------------
00171 NS_IMETHODIMP  nsTextHelper::SetReadOnly(PRBool aReadOnlyFlag, PRBool& aOldReadOnlyFlag)
00172 {
00173   NS_ASSERTION(nsnull != mTextWidget,
00174                "SetReadOnly - Widget is NULL, Create may not have been called!");
00175   aOldReadOnlyFlag = mIsReadOnly;
00176   mIsReadOnly = aReadOnlyFlag?PR_FALSE:PR_TRUE;
00177   gtk_editable_set_editable(GTK_EDITABLE(mTextWidget), mIsReadOnly);
00178   return NS_OK;
00179 }
00180 
00181 
00182 //-------------------------------------------------------------------------
00183 NS_IMETHODIMP nsTextHelper::SelectAll()
00184 {
00185   nsString text;
00186   PRUint32 actualSize = 0;
00187   PRUint32 numChars = GetText(text, 0, actualSize);
00188   SetSelection(0, numChars);
00189   return NS_OK;
00190 }
00191 
00192 
00193 //-------------------------------------------------------------------------
00194 NS_IMETHODIMP nsTextHelper::SetSelection(PRUint32 aStartSel, PRUint32 aEndSel)
00195 {
00196   gtk_editable_select_region(GTK_EDITABLE(mTextWidget), aStartSel, aEndSel);
00197   return NS_OK;
00198 }
00199 
00200 
00201 //-------------------------------------------------------------------------
00202 NS_IMETHODIMP nsTextHelper::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
00203 {
00204 #if 0
00205   XmTextPosition left;
00206   XmTextPosition right;
00207 
00208   if (XmTextGetSelectionPosition(mTextWidget, &left, &right)) {
00209     *aStartSel = (PRUint32)left;
00210     *aEndSel   = (PRUint32)right;
00211   } else {
00212     printf("nsTextHelper::GetSelection Error getting positions\n");
00213     return NS_ERROR_FAILURE;
00214   }
00215 #endif
00216   return NS_OK;
00217 }
00218 
00219 //-------------------------------------------------------------------------
00220 NS_IMETHODIMP  nsTextHelper::SetCaretPosition(PRUint32 aPosition)
00221 {
00222   gtk_editable_set_position(GTK_EDITABLE(mTextWidget), aPosition);
00223   return NS_OK;
00224 }
00225 
00226 //-------------------------------------------------------------------------
00227 NS_IMETHODIMP  nsTextHelper::GetCaretPosition(PRUint32& aPosition)
00228 {
00229   aPosition = (PRUint32)GTK_EDITABLE(mTextWidget)->current_pos;
00230   return NS_OK;
00231 }