~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/widget/src/gtk/nsTextHelper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include <gtk/gtk.h>
 
39
 
 
40
#include "nsTextHelper.h"
 
41
#include "nsTextWidget.h"
 
42
#include "nsString.h"
 
43
 
 
44
NS_IMPL_ADDREF_INHERITED(nsTextHelper, nsWidget)
 
45
NS_IMPL_RELEASE_INHERITED(nsTextHelper, nsWidget)
 
46
 
 
47
//-------------------------------------------------------------------------
 
48
//
 
49
// nsTextHelper constructor
 
50
//
 
51
//-------------------------------------------------------------------------
 
52
 
 
53
nsTextHelper::nsTextHelper() : nsWidget(), nsITextWidget()
 
54
{
 
55
  mIsReadOnly = PR_FALSE;
 
56
  mIsPassword = PR_FALSE;
 
57
}
 
58
 
 
59
//-------------------------------------------------------------------------
 
60
//
 
61
// nsTextHelper destructor
 
62
//
 
63
//-------------------------------------------------------------------------
 
64
nsTextHelper::~nsTextHelper()
 
65
{
 
66
}
 
67
 
 
68
//-------------------------------------------------------------------------
 
69
//
 
70
// Set initial parameters
 
71
//
 
72
//-------------------------------------------------------------------------
 
73
NS_IMETHODIMP nsTextHelper::PreCreateWidget(nsWidgetInitData *aInitData)
 
74
{
 
75
  if (nsnull != aInitData) {
 
76
    nsTextWidgetInitData* data = (nsTextWidgetInitData *) aInitData;
 
77
    mIsPassword = data->mIsPassword;
 
78
    mIsReadOnly = data->mIsReadOnly;
 
79
  }
 
80
  return NS_OK;
 
81
}
 
82
 
 
83
//-------------------------------------------------------------------------
 
84
NS_IMETHODIMP nsTextHelper::SetMaxTextLength(PRUint32 aChars)
 
85
{
 
86
  // This is a normal entry only thing, not a text box
 
87
  gtk_entry_set_max_length(GTK_ENTRY(mTextWidget), (int)aChars);
 
88
  return NS_OK;
 
89
}
 
90
 
 
91
//-------------------------------------------------------------------------
 
92
NS_IMETHODIMP  nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize, PRUint32& aActualSize)
 
93
{
 
94
  char *str = nsnull;
 
95
  if (GTK_IS_ENTRY(mTextWidget))
 
96
    {
 
97
      str = gtk_entry_get_text(GTK_ENTRY(mTextWidget));
 
98
    }
 
99
  else if (GTK_IS_TEXT(mTextWidget))
 
100
    {
 
101
      str = gtk_editable_get_chars (GTK_EDITABLE (mTextWidget), 0,
 
102
                                    gtk_text_get_length (GTK_TEXT (mTextWidget)));
 
103
    }
 
104
  aTextBuffer.SetLength(0);
 
105
  aTextBuffer.AppendWithConversion(str);
 
106
  PRUint32 len = (PRUint32)strlen(str);
 
107
  aActualSize = len;
 
108
 
 
109
  return NS_OK;
 
110
}
 
111
 
 
112
//-------------------------------------------------------------------------
 
113
NS_IMETHODIMP  nsTextHelper::SetText(const nsString& aText, PRUint32& aActualSize)
 
114
{
 
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(),
 
124
                    aText.Length());
 
125
  }
 
126
 
 
127
  aActualSize = aText.Length();
 
128
 
 
129
  return NS_OK;
 
130
}
 
131
 
 
132
//-------------------------------------------------------------------------
 
133
NS_IMETHODIMP  nsTextHelper::InsertText(const nsString &aText,
 
134
                                    PRUint32 aStartPos,
 
135
                                    PRUint32 aEndPos,
 
136
                                    PRUint32& aActualSize)
 
137
{
 
138
  gtk_editable_insert_text(GTK_EDITABLE(mTextWidget),
 
139
                           (const gchar *)NS_LossyConvertUCS2toASCII(aText).get(),
 
140
                           (gint)aText.Length(), (gint*)&aStartPos);
 
141
 
 
142
  aActualSize = aText.Length();
 
143
 
 
144
  return NS_OK;
 
145
}
 
146
 
 
147
//-------------------------------------------------------------------------
 
148
NS_IMETHODIMP  nsTextHelper::RemoveText()
 
149
{
 
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)));
 
155
  }
 
156
  return NS_OK;
 
157
}
 
158
 
 
159
//-------------------------------------------------------------------------
 
160
NS_IMETHODIMP  nsTextHelper::SetPassword(PRBool aIsPassword)
 
161
{
 
162
  mIsPassword = aIsPassword?PR_FALSE:PR_TRUE;
 
163
  if (GTK_IS_ENTRY(mTextWidget)) {
 
164
    gtk_entry_set_visibility(GTK_ENTRY(mTextWidget), mIsPassword);
 
165
  }
 
166
  // this won't work for gtk_texts
 
167
  return NS_OK;
 
168
}
 
169
 
 
170
//-------------------------------------------------------------------------
 
171
NS_IMETHODIMP  nsTextHelper::SetReadOnly(PRBool aReadOnlyFlag, PRBool& aOldReadOnlyFlag)
 
172
{
 
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);
 
178
  return NS_OK;
 
179
}
 
180
 
 
181
 
 
182
//-------------------------------------------------------------------------
 
183
NS_IMETHODIMP nsTextHelper::SelectAll()
 
184
{
 
185
  nsString text;
 
186
  PRUint32 actualSize = 0;
 
187
  PRUint32 numChars = GetText(text, 0, actualSize);
 
188
  SetSelection(0, numChars);
 
189
  return NS_OK;
 
190
}
 
191
 
 
192
 
 
193
//-------------------------------------------------------------------------
 
194
NS_IMETHODIMP nsTextHelper::SetSelection(PRUint32 aStartSel, PRUint32 aEndSel)
 
195
{
 
196
  gtk_editable_select_region(GTK_EDITABLE(mTextWidget), aStartSel, aEndSel);
 
197
  return NS_OK;
 
198
}
 
199
 
 
200
 
 
201
//-------------------------------------------------------------------------
 
202
NS_IMETHODIMP nsTextHelper::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
 
203
{
 
204
#if 0
 
205
  XmTextPosition left;
 
206
  XmTextPosition right;
 
207
 
 
208
  if (XmTextGetSelectionPosition(mTextWidget, &left, &right)) {
 
209
    *aStartSel = (PRUint32)left;
 
210
    *aEndSel   = (PRUint32)right;
 
211
  } else {
 
212
    printf("nsTextHelper::GetSelection Error getting positions\n");
 
213
    return NS_ERROR_FAILURE;
 
214
  }
 
215
#endif
 
216
  return NS_OK;
 
217
}
 
218
 
 
219
//-------------------------------------------------------------------------
 
220
NS_IMETHODIMP  nsTextHelper::SetCaretPosition(PRUint32 aPosition)
 
221
{
 
222
  gtk_editable_set_position(GTK_EDITABLE(mTextWidget), aPosition);
 
223
  return NS_OK;
 
224
}
 
225
 
 
226
//-------------------------------------------------------------------------
 
227
NS_IMETHODIMP  nsTextHelper::GetCaretPosition(PRUint32& aPosition)
 
228
{
 
229
  aPosition = (PRUint32)GTK_EDITABLE(mTextWidget)->current_pos;
 
230
  return NS_OK;
 
231
}