Back to index

lightning-sunbird  0.9+nobinonly
nsMaiInterfaceEditableText.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:expandtab:shiftwidth=4:tabstop=4:
00003  */
00004 /* ***** BEGIN LICENSE BLOCK *****
00005  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  *
00008  * The contents of this file are subject to the Mozilla Public
00009  * License Version 1.1 (the "License"); you may not use this file
00010  * except in compliance with the License. You may obtain a copy of
00011  * the License at http://www.mozilla.org/MPL/
00012  *
00013  * Software distributed under the License is distributed on an "AS
00014  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00015  * implied. See the License for the specific language governing
00016  * rights and limitations under the License.
00017  *
00018  * The Original Code is mozilla.org code.
00019  *
00020  * The Initial Developer of the Original Code is Sun Microsystems, Inc.
00021  * Portions created by Sun Microsystems are Copyright (C) 2002 Sun
00022  * Microsystems, Inc. All Rights Reserved.
00023  *
00024  * Original Author: Bolian Yin (bolian.yin@sun.com)
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the NPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the NPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nsString.h"
00043 #include "nsMaiInterfaceEditableText.h"
00044 
00045 G_BEGIN_DECLS
00046 
00047 static void interfaceInitCB(AtkEditableTextIface *aIface);
00048 
00049 /* editabletext interface callbacks */
00050 static gboolean setRunAttributesCB(AtkEditableText *aText,
00051                                    AtkAttributeSet *aAttribSet,
00052                                    gint aStartOffset,
00053                                    gint aEndOffset);
00054 static void setTextContentsCB(AtkEditableText *aText, const gchar *aString);
00055 static void insertTextCB(AtkEditableText *aText,
00056                          const gchar *aString, gint aLength, gint *aPosition);
00057 static void copyTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos);
00058 static void cutTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos);
00059 static void deleteTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos);
00060 static void pasteTextCB(AtkEditableText *aText, gint aPosition);
00061 
00062 G_END_DECLS
00063 
00064 MaiInterfaceEditableText::MaiInterfaceEditableText(nsAccessibleWrap *aAccWrap):
00065     MaiInterface(aAccWrap)
00066 {
00067 }
00068 
00069 MaiInterfaceEditableText::~MaiInterfaceEditableText()
00070 {
00071 }
00072 
00073 MaiInterfaceType
00074 MaiInterfaceEditableText::GetType()
00075 {
00076     return MAI_INTERFACE_EDITABLE_TEXT;
00077 }
00078 
00079 const GInterfaceInfo *
00080 MaiInterfaceEditableText::GetInterfaceInfo()
00081 {
00082     static const GInterfaceInfo atk_if_editabletext_info = {
00083         (GInterfaceInitFunc)interfaceInitCB,
00084         (GInterfaceFinalizeFunc) NULL,
00085         NULL
00086     };
00087     return &atk_if_editabletext_info;
00088 }
00089 
00090 /* statics */
00091 
00092 void
00093 interfaceInitCB(AtkEditableTextIface *aIface)
00094 {
00095     NS_ASSERTION(aIface, "Invalid aIface");
00096     if (!aIface)
00097         return;
00098 
00099     aIface->set_run_attributes = setRunAttributesCB;
00100     aIface->set_text_contents = setTextContentsCB;
00101     aIface->insert_text = insertTextCB;
00102     aIface->copy_text = copyTextCB;
00103     aIface->cut_text = cutTextCB;
00104     aIface->delete_text = deleteTextCB;
00105     aIface->paste_text = pasteTextCB;
00106 }
00107 
00108 /* static, callbacks for atkeditabletext virutal functions */
00109 
00110 gboolean
00111 setRunAttributesCB(AtkEditableText *aText, AtkAttributeSet *aAttribSet,
00112                    gint aStartOffset, gint aEndOffset)
00113 
00114 {
00115     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00116     NS_ENSURE_TRUE(accWrap, FALSE);
00117 
00118     nsCOMPtr<nsIAccessibleEditableText> accText;
00119     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00120                             getter_AddRefs(accText));
00121     NS_ENSURE_TRUE(accText, FALSE);
00122 
00123     nsCOMPtr<nsISupports> attrSet;
00124     /* how to insert attributes into nsISupports ??? */
00125 
00126     nsresult rv = accText->SetAttributes(aStartOffset, aEndOffset,
00127                                          attrSet);
00128     return NS_FAILED(rv) ? FALSE : TRUE;
00129 }
00130 
00131 void
00132 setTextContentsCB(AtkEditableText *aText, const gchar *aString)
00133 {
00134     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00135     if (!accWrap)
00136         return;
00137 
00138     nsCOMPtr<nsIAccessibleEditableText> accText;
00139     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00140                             getter_AddRefs(accText));
00141     if (!accText)
00142         return;
00143 
00144     MAI_LOG_DEBUG(("EditableText: setTextContentsCB, aString=%s", aString));
00145 
00146     NS_ConvertUTF8toUCS2 strContent(aString);
00147     nsresult rv = accText->SetTextContents(strContent);
00148 
00149     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00150                      "MaiInterfaceEditableText::SetTextContents, failed\n");
00151 }
00152 
00153 void
00154 insertTextCB(AtkEditableText *aText,
00155              const gchar *aString, gint aLength, gint *aPosition)
00156 {
00157     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00158     if (!accWrap)
00159         return;
00160 
00161     nsCOMPtr<nsIAccessibleEditableText> accText;
00162     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00163                             getter_AddRefs(accText));
00164     if (!accText)
00165         return;
00166 
00167     NS_ConvertUTF8toUCS2 strContent(aString);
00168 
00169     // interface changed in nsIAccessibleEditabelText.idl ???
00170     //
00171     // PRInt32 pos = *aPosition;
00172     // nsresult rv = accText->InsertText(strContent, aLength, &pos);
00173     // *aPosition = pos;
00174 
00175     nsresult rv = accText->InsertText(strContent, *aPosition);
00176     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00177                      "MaiInterfaceEditableText::InsertText, failed\n");
00178 
00179     MAI_LOG_DEBUG(("EditableText: insert aString=%s, aLength=%d, aPosition=%d",
00180                    aString, aLength, *aPosition));
00181 }
00182 
00183 void
00184 copyTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos)
00185 {
00186     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00187     if (!accWrap)
00188         return;
00189 
00190     nsCOMPtr<nsIAccessibleEditableText> accText;
00191     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00192                             getter_AddRefs(accText));
00193     if (!accText)
00194         return;
00195 
00196     MAI_LOG_DEBUG(("EditableText: copyTextCB, aStartPos=%d, aEndPos=%d",
00197                    aStartPos, aEndPos));
00198     nsresult rv = accText->CopyText(aStartPos, aEndPos);
00199     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00200                      "MaiInterfaceEditableText::CopyText, failed\n");
00201 }
00202 
00203 void
00204 cutTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos)
00205 {
00206     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00207     if (!accWrap)
00208         return;
00209 
00210     nsCOMPtr<nsIAccessibleEditableText> accText;
00211     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00212                             getter_AddRefs(accText));
00213     if (!accText)
00214         return;
00215     MAI_LOG_DEBUG(("EditableText: cutTextCB, aStartPos=%d, aEndPos=%d",
00216                    aStartPos, aEndPos));
00217     nsresult rv = accText->CutText(aStartPos, aEndPos);
00218     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00219                      "MaiInterfaceEditableText::CutText, failed\n");
00220 }
00221 
00222 void
00223 deleteTextCB(AtkEditableText *aText, gint aStartPos, gint aEndPos)
00224 {
00225     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00226     if (!accWrap)
00227         return;
00228 
00229     nsCOMPtr<nsIAccessibleEditableText> accText;
00230     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00231                             getter_AddRefs(accText));
00232     if (!accText)
00233         return;
00234 
00235     MAI_LOG_DEBUG(("EditableText: deleteTextCB, aStartPos=%d, aEndPos=%d",
00236                    aStartPos, aEndPos));
00237     nsresult rv = accText->DeleteText(aStartPos, aEndPos);
00238     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00239                      "MaiInterfaceEditableText::DeleteText, failed\n");
00240 }
00241 
00242 void
00243 pasteTextCB(AtkEditableText *aText, gint aPosition)
00244 {
00245     nsAccessibleWrap *accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
00246     if (!accWrap)
00247         return;
00248 
00249     nsCOMPtr<nsIAccessibleEditableText> accText;
00250     accWrap->QueryInterface(NS_GET_IID(nsIAccessibleEditableText),
00251                             getter_AddRefs(accText));
00252     if (!accText)
00253         return;
00254 
00255     MAI_LOG_DEBUG(("EditableText: pasteTextCB, aPosition=%d", aPosition));
00256     nsresult rv = accText->PasteText(aPosition);
00257     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
00258                      "MaiInterfaceEditableText::PasteText, failed\n");
00259 }