1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
40
#include "nsTextHelper.h"
41
#include "nsTextWidget.h"
44
NS_IMPL_ADDREF_INHERITED(nsTextHelper, nsWidget)
45
NS_IMPL_RELEASE_INHERITED(nsTextHelper, nsWidget)
47
//-------------------------------------------------------------------------
49
// nsTextHelper constructor
51
//-------------------------------------------------------------------------
53
nsTextHelper::nsTextHelper() : nsWidget(), nsITextWidget()
55
mIsReadOnly = PR_FALSE;
56
mIsPassword = PR_FALSE;
59
//-------------------------------------------------------------------------
61
// nsTextHelper destructor
63
//-------------------------------------------------------------------------
64
nsTextHelper::~nsTextHelper()
68
//-------------------------------------------------------------------------
70
// Set initial parameters
72
//-------------------------------------------------------------------------
73
NS_IMETHODIMP nsTextHelper::PreCreateWidget(nsWidgetInitData *aInitData)
75
if (nsnull != aInitData) {
76
nsTextWidgetInitData* data = (nsTextWidgetInitData *) aInitData;
77
mIsPassword = data->mIsPassword;
78
mIsReadOnly = data->mIsReadOnly;
83
//-------------------------------------------------------------------------
84
NS_IMETHODIMP nsTextHelper::SetMaxTextLength(PRUint32 aChars)
86
// This is a normal entry only thing, not a text box
87
gtk_entry_set_max_length(GTK_ENTRY(mTextWidget), (int)aChars);
91
//-------------------------------------------------------------------------
92
NS_IMETHODIMP nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize, PRUint32& aActualSize)
95
if (GTK_IS_ENTRY(mTextWidget))
97
str = gtk_entry_get_text(GTK_ENTRY(mTextWidget));
99
else if (GTK_IS_TEXT(mTextWidget))
101
str = gtk_editable_get_chars (GTK_EDITABLE (mTextWidget), 0,
102
gtk_text_get_length (GTK_TEXT (mTextWidget)));
104
aTextBuffer.SetLength(0);
105
aTextBuffer.AppendWithConversion(str);
106
PRUint32 len = (PRUint32)strlen(str);
112
//-------------------------------------------------------------------------
113
NS_IMETHODIMP nsTextHelper::SetText(const nsString& aText, PRUint32& aActualSize)
115
if (GTK_IS_ENTRY(mTextWidget)) {
116
gtk_entry_set_text(GTK_ENTRY(mTextWidget),
117
(const gchar *)NS_LossyConvertUCS2toASCII(aText).get());
118
} else if (GTK_IS_TEXT(mTextWidget)) {
119
gtk_editable_delete_text(GTK_EDITABLE(mTextWidget), 0,
120
gtk_text_get_length(GTK_TEXT (mTextWidget)));
121
gtk_text_insert(GTK_TEXT(mTextWidget),
122
nsnull, nsnull, nsnull,
123
NS_LossyConvertUCS2toASCII(aText).get(),
127
aActualSize = aText.Length();
132
//-------------------------------------------------------------------------
133
NS_IMETHODIMP nsTextHelper::InsertText(const nsString &aText,
136
PRUint32& aActualSize)
138
gtk_editable_insert_text(GTK_EDITABLE(mTextWidget),
139
(const gchar *)NS_LossyConvertUCS2toASCII(aText).get(),
140
(gint)aText.Length(), (gint*)&aStartPos);
142
aActualSize = aText.Length();
147
//-------------------------------------------------------------------------
148
NS_IMETHODIMP nsTextHelper::RemoveText()
150
if (GTK_IS_ENTRY(mTextWidget)) {
151
gtk_entry_set_text(GTK_ENTRY(mTextWidget), "");
152
} else if (GTK_IS_TEXT(mTextWidget)) {
153
gtk_editable_delete_text(GTK_EDITABLE(mTextWidget), 0,
154
gtk_text_get_length(GTK_TEXT (mTextWidget)));
159
//-------------------------------------------------------------------------
160
NS_IMETHODIMP nsTextHelper::SetPassword(PRBool aIsPassword)
162
mIsPassword = aIsPassword?PR_FALSE:PR_TRUE;
163
if (GTK_IS_ENTRY(mTextWidget)) {
164
gtk_entry_set_visibility(GTK_ENTRY(mTextWidget), mIsPassword);
166
// this won't work for gtk_texts
170
//-------------------------------------------------------------------------
171
NS_IMETHODIMP nsTextHelper::SetReadOnly(PRBool aReadOnlyFlag, PRBool& aOldReadOnlyFlag)
173
NS_ASSERTION(nsnull != mTextWidget,
174
"SetReadOnly - Widget is NULL, Create may not have been called!");
175
aOldReadOnlyFlag = mIsReadOnly;
176
mIsReadOnly = aReadOnlyFlag?PR_FALSE:PR_TRUE;
177
gtk_editable_set_editable(GTK_EDITABLE(mTextWidget), mIsReadOnly);
182
//-------------------------------------------------------------------------
183
NS_IMETHODIMP nsTextHelper::SelectAll()
186
PRUint32 actualSize = 0;
187
PRUint32 numChars = GetText(text, 0, actualSize);
188
SetSelection(0, numChars);
193
//-------------------------------------------------------------------------
194
NS_IMETHODIMP nsTextHelper::SetSelection(PRUint32 aStartSel, PRUint32 aEndSel)
196
gtk_editable_select_region(GTK_EDITABLE(mTextWidget), aStartSel, aEndSel);
201
//-------------------------------------------------------------------------
202
NS_IMETHODIMP nsTextHelper::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
206
XmTextPosition right;
208
if (XmTextGetSelectionPosition(mTextWidget, &left, &right)) {
209
*aStartSel = (PRUint32)left;
210
*aEndSel = (PRUint32)right;
212
printf("nsTextHelper::GetSelection Error getting positions\n");
213
return NS_ERROR_FAILURE;
219
//-------------------------------------------------------------------------
220
NS_IMETHODIMP nsTextHelper::SetCaretPosition(PRUint32 aPosition)
222
gtk_editable_set_position(GTK_EDITABLE(mTextWidget), aPosition);
226
//-------------------------------------------------------------------------
227
NS_IMETHODIMP nsTextHelper::GetCaretPosition(PRUint32& aPosition)
229
aPosition = (PRUint32)GTK_EDITABLE(mTextWidget)->current_pos;