added focus managment, textfields

This commit is contained in:
dcone%netscape.com 1998-09-10 21:26:46 +00:00
Родитель 47d091204c
Коммит 684277a5d9
8 изменённых файлов: 264 добавлений и 146 удалений

Просмотреть файл

@ -24,8 +24,6 @@
#include "nsString.h"
#include "nsStringUtil.h"
#include <Xm/Text.h>
#define DBG 0
//-------------------------------------------------------------------------
@ -34,11 +32,15 @@
//
//-------------------------------------------------------------------------
nsTextHelper::nsTextHelper(Widget aWidget)
nsTextHelper::nsTextHelper(nsISupports *aOuter):nsITextWidget(aOuter)
{
mWidget = aWidget;
LongRect destRect,viewRect;
PRUint32 teFlags=0;
mIsReadOnly = PR_FALSE;
mIsPassword = PR_FALSE;
WENew(&destRect,&viewRect,teFlags,&mTE_Data);
}
//-------------------------------------------------------------------------
@ -53,22 +55,29 @@ nsTextHelper::~nsTextHelper()
//-------------------------------------------------------------------------
void nsTextHelper::SetMaxTextLength(PRUint32 aChars)
{
XmTextSetMaxLength(mWidget, (int)aChars);
}
//-------------------------------------------------------------------------
PRUint32 nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize)
{
if (!mIsPassword) {
char * str = XmTextGetString(mWidget);
PRUint32 len;
if (!mIsPassword)
{
//char * str = XmTextGetString(mWidget);
aTextBuffer.SetLength(0);
aTextBuffer.Append(str);
PRUint32 len = (PRUint32)strlen(str);
XtFree(str);
//aTextBuffer.Append(str);
//PRUint32 len = (PRUint32)strlen(str);
//XtFree(str);
return len;
} else {
}
else
{
PasswordData * data;
XtVaGetValues(mWidget, XmNuserData, &data, NULL);
//XtVaGetValues(mWidget, XmNuserData, &data, NULL);
aTextBuffer = data->mPassword;
return aTextBuffer.Length();
}
@ -78,22 +87,25 @@ PRUint32 nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize)
PRUint32 nsTextHelper::SetText(const nsString& aText)
{
//printf("SetText Password %d\n", mIsPassword);
if (!mIsPassword) {
if (!mIsPassword)
{
NS_ALLOC_STR_BUF(buf, aText, 512);
XmTextSetString(mWidget, buf);
//XmTextSetString(mWidget, buf);
NS_FREE_STR_BUF(buf);
} else {
}
else
{
PasswordData * data;
XtVaGetValues(mWidget, XmNuserData, &data, NULL);
//(mWidget, XmNuserData, &data, NULL);
data->mPassword = aText;
data->mIgnore = True;
data->mIgnore = PR_TRUE;
char * buf = new char[aText.Length()+1];
memset(buf, '*', aText.Length());
buf[aText.Length()] = 0;
//printf("SetText [%s] [%s]\n", data->mPassword.ToNewCString(), buf);
XmTextSetString(mWidget, buf);
data->mIgnore = False;
}
//XmTextSetString(mWidget, buf);
data->mIgnore = PR_FALSE;
}
return(aText.Length());
}
@ -101,23 +113,26 @@ PRUint32 nsTextHelper::SetText(const nsString& aText)
PRUint32 nsTextHelper::InsertText(const nsString &aText, PRUint32 aStartPos, PRUint32 aEndPos)
{
if (!mIsPassword) {
if (!mIsPassword)
{
NS_ALLOC_STR_BUF(buf, aText, 512);
XmTextInsert(mWidget, aStartPos, buf);
//XmTextInsert(mWidget, aStartPos, buf);
NS_FREE_STR_BUF(buf);
} else {
}
else
{
PasswordData * data;
XtVaGetValues(mWidget, XmNuserData, &data, NULL);
data->mIgnore = True;
//XtVaGetValues(mWidget, XmNuserData, &data, NULL);
data->mIgnore = PR_TRUE;
nsString newText(aText);
data->mPassword.Insert(newText, aStartPos, aText.Length());
char * buf = new char[data->mPassword.Length()+1];
memset(buf, '*', data->mPassword.Length());
buf[data->mPassword.Length()] = 0;
//printf("SetText [%s] [%s]\n", data->mPassword.ToNewCString(), buf);
XmTextInsert(mWidget, aStartPos, buf);
data->mIgnore = False;
}
//XmTextInsert(mWidget, aStartPos, buf);
data->mIgnore = PR_FALSE;
}
return(aText.Length());
}
@ -128,7 +143,7 @@ void nsTextHelper::RemoveText()
char blank[2];
blank[0] = 0;
XmTextSetString(mWidget, blank);
//XmTextSetString(mWidget, blank);
}
//-------------------------------------------------------------------------
@ -144,7 +159,7 @@ PRBool nsTextHelper::SetReadOnly(PRBool aReadOnlyFlag)
"SetReadOnly - Widget is NULL, Create may not have been called!");
PRBool oldSetting = mIsReadOnly;
mIsReadOnly = aReadOnlyFlag;
XmTextSetEditable(mWidget, aReadOnlyFlag?False:True);
//XmTextSetEditable(mWidget, aReadOnlyFlag?False:True);
return(oldSetting);
}
@ -162,21 +177,22 @@ void nsTextHelper::SelectAll()
//-------------------------------------------------------------------------
void nsTextHelper::SetSelection(PRUint32 aStartSel, PRUint32 aEndSel)
{
XmTextPosition left = (XmTextPosition)aStartSel;
XmTextPosition right = (XmTextPosition)aEndSel;
//XmTextPosition left = (XmTextPosition)aStartSel;
//XmTextPosition right = (XmTextPosition)aEndSel;
Time time;
//Time time;
printf("SetSel %d %d\n", left, right);
XmTextSetSelection(mWidget, left, right, 0);
//XmTextSetSelection(mWidget, left, right, 0);
}
//-------------------------------------------------------------------------
void nsTextHelper::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
{
XmTextPosition left;
XmTextPosition right;
//XmTextPosition left;
//XmTextPosition right;
/*
if (XmTextGetSelectionPosition(mWidget, &left, &right)) {
printf("left %d right %d\n", left, right);
*aStartSel = (PRUint32)left;
@ -184,18 +200,19 @@ void nsTextHelper::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
} else {
printf("nsTextHelper::GetSelection Error getting positions\n");
}
*/
}
//-------------------------------------------------------------------------
void nsTextHelper::SetCaretPosition(PRUint32 aPosition)
{
XmTextSetInsertionPosition(mWidget, (XmTextPosition)aPosition);
//XmTextSetInsertionPosition(mWidget, (XmTextPosition)aPosition);
}
//-------------------------------------------------------------------------
PRUint32 nsTextHelper::GetCaretPosition()
{
return (PRUint32)XmTextGetInsertionPosition(mWidget);
//return (PRUint32)XmTextGetInsertionPosition(mWidget);
}

Просмотреть файл

@ -20,23 +20,23 @@
#include "nsITextWidget.h"
#include "nsWindow.h"
#include <Xm/Xm.h>
#include "WASTE.h"
/**
* Base class for nsTextAreaWidget and nsTextWidget
*/
class nsTextHelper
class nsTextHelper : public nsITextWidget
{
public:
nsTextHelper(Widget aWidget);
nsTextHelper(nsISupports *aOuter);
virtual ~nsTextHelper();
virtual void SelectAll();
virtual void SetMaxTextLength(PRUint32 aChars);
virtual PRUint32 GetText(nsString& aTextBuffer, PRUint32 aBufferSize);
virtual PRUint32 SetText(const nsString& aText);
virtual PRUint32 SetText(const nsString &aText);
virtual PRUint32 InsertText(const nsString &aText, PRUint32 aStartPos, PRUint32 aEndPos);
virtual void RemoveText();
virtual void SetPassword(PRBool aIsPassword);
@ -45,13 +45,14 @@ public:
virtual void GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel);
virtual void SetCaretPosition(PRUint32 aPosition);
virtual PRUint32 GetCaretPosition();
//virtual void PreCreateWidget(nsWidgetInitData *aInitData);
virtual PRBool AutoErase();
protected:
Widget mWidget;
PRBool mIsPassword;
PRBool mIsReadOnly;
PRBool mIsPassword;
PRBool mIsReadOnly;
WEReference mTE_Data;
};

Просмотреть файл

@ -21,24 +21,21 @@
#include "nsColor.h"
#include "nsGUIEvent.h"
#include "nsString.h"
#include "nsXtEventHandler.h"
#include <Xm/Text.h>
#define DBG 0
extern int mIsPasswordCallBacksInstalled;
//-------------------------------------------------------------------------
//
// nsTextWidget constructor
//
//-------------------------------------------------------------------------
nsTextWidget::nsTextWidget(nsISupports *aOuter) : nsWindow(aOuter),
mIsPasswordCallBacksInstalled(PR_FALSE),
mMakeReadOnly(PR_FALSE),
mMakePassword(PR_FALSE)
nsTextWidget::nsTextWidget(nsISupports *aOuter): nsWindow(aOuter)
{
mIsPasswordCallBacksInstalled = PR_FALSE;
mMakeReadOnly=PR_FALSE;
mMakePassword=PR_FALSE;
mTE_Data = nsnull;
//mBackground = NS_RGB(124, 124, 124);
}
@ -49,6 +46,8 @@ nsTextWidget::nsTextWidget(nsISupports *aOuter) : nsWindow(aOuter),
//-------------------------------------------------------------------------
nsTextWidget::~nsTextWidget()
{
if(mTE_Data!=nsnull)
WEDispose(mTE_Data);
}
//-------------------------------------------------------------------------
@ -60,64 +59,64 @@ void nsTextWidget::Create(nsIWidget *aParent,
nsIToolkit *aToolkit,
nsWidgetInitData *aInitData)
{
LongRect destRect,viewRect;
PRUint32 teFlags=0;
GrafPtr curport;
mParent = aParent;
aParent->AddChild(this);
Widget parentWidget = nsnull;
if (DBG) fprintf(stderr, "aParent 0x%x\n", aParent);
WindowPtr window = nsnull;
if (aParent) {
parentWidget = (Widget) aParent->GetNativeData(NS_NATIVE_WIDGET);
} else {
parentWidget = (Widget) aAppShell->GetNativeData(NS_NATIVE_SHELL);
}
if (aParent)
window = (WindowPtr) aParent->GetNativeData(NS_NATIVE_WIDGET);
else
if (aAppShell)
window = (WindowPtr) aAppShell->GetNativeData(NS_NATIVE_SHELL);
InitToolkit(aToolkit, aParent);
InitDeviceContext(aContext, parentWidget);
if (DBG) fprintf(stderr, "Parent 0x%x\n", parentWidget);
mWidget = ::XtVaCreateManagedWidget("button",
xmTextWidgetClass,
parentWidget,
XmNwidth, aRect.width,
XmNheight, aRect.height,
XmNrecomputeSize, False,
XmNhighlightOnEnter, False,
XmNeditable, mMakeReadOnly?False:True,
XmNx, aRect.x,
XmNy, aRect.y,
nsnull);
mHelper = new nsTextHelper(mWidget);
if (DBG) fprintf(stderr, "Button 0x%x this 0x%x\n", mWidget, this);
// save the event callback function
mEventCallback = aHandleEventFunction;
InitCallbacks("nsTextWidget");
XtAddCallback(mWidget,
XmNfocusCallback,
nsXtWidget_Focus_Callback,
this);
XtAddCallback(mWidget,
XmNlosingFocusCallback,
nsXtWidget_Focus_Callback,
this);
if (mMakeReadOnly) {
SetReadOnly(PR_TRUE);
}
if (mMakePassword) {
SetPassword(PR_TRUE);
PasswordData * data = new PasswordData();
data->mPassword = "";
XtVaSetValues(mWidget, XmNuserData, data, NULL);
}
mIsMainWindow = PR_FALSE;
mWindowMadeHere = PR_TRUE;
mWindowRecord = (WindowRecord*)window;
mWindowPtr = (WindowPtr)window;
NS_ASSERTION(window!=nsnull,"The WindowPtr for the widget cannot be null")
if (window)
{
InitToolkit(aToolkit, aParent);
if (DBG) fprintf(stderr, "Parent 0x%x\n", window);
// Set the bounds to the local rect
SetBounds(aRect);
mWindowRegion = NewRgn();
SetRectRgn(mWindowRegion,aRect.x,aRect.y,aRect.x+aRect.width,aRect.y+aRect.height);
// save the event callback function
mEventCallback = aHandleEventFunction;
//mMouseDownInButton = PR_FALSE;
//mWidgetArmed = PR_FALSE;
//InitCallbacks("nsButton");
// Initialize the TE record
viewRect.left = aRect.x;
viewRect.top = aRect.y;
viewRect.right = aRect.x+aRect.width;
viewRect.bottom = aRect.y+aRect.height;
destRect = viewRect;
::GetPort(&curport);
::SetPort(mWindowPtr);
WENew(&destRect,&viewRect,teFlags,&mTE_Data);
::SetPort(curport);
InitDeviceContext(mContext, (nsNativeWidget)mWindowPtr);
}
}
@ -158,10 +157,62 @@ nsresult nsTextWidget::QueryObject(REFNSIID aIID, void** aInstancePtr)
//-------------------------------------------------------------------------
PRBool nsTextWidget::OnPaint(nsPaintEvent & aEvent)
{
nsRect therect;
Rect macrect;
GrafPtr theport;
RGBColor blackcolor = {0,0,0};
RgnHandle thergn;
GetPort(&theport);
::SetPort(mWindowPtr);
GetBounds(therect);
nsRectToMacRect(therect,macrect);
thergn = ::NewRgn();
::GetClip(thergn);
::ClipRect(&macrect);
//::EraseRoundRect(&macrect,10,10);
//::PenSize(1,1);
//::FrameRoundRect(&macrect,10,10);
WEActivate(mTE_Data);
WEUpdate(nsnull,mTE_Data);
::PenSize(1,1);
::SetClip(thergn);
::SetPort(theport);
return PR_FALSE;
}
//--------------------------------------------------------------
void nsTextWidget::PrimitiveKeyDown(PRInt16 aKey,PRInt16 aModifiers)
{
PRBool result=PR_TRUE;
WEKey(aKey,aModifiers,mTE_Data);
}
//--------------------------------------------------------------
/*
PRBool
nsTextWidget::DispatchMouseEvent(nsMouseEvent &aEvent)
{
PRBool result=PR_TRUE;
switch (aEvent.message)
{
case NS_MOUSE_LEFT_BUTTON_DOWN:
break;
case NS_MOUSE_LEFT_BUTTON_UP:
break;
case NS_MOUSE_EXIT:
break;
case NS_MOUSE_ENTER:
break;
}
return result;
}
*/
//--------------------------------------------------------------
PRBool nsTextWidget::OnResize(nsSizeEvent &aEvent)
{
@ -171,102 +222,137 @@ PRBool nsTextWidget::OnResize(nsSizeEvent &aEvent)
//--------------------------------------------------------------
void nsTextWidget::SetPassword(PRBool aIsPassword)
{
if (mWidget == nsnull && aIsPassword) {
if ( aIsPassword) {
mMakePassword = PR_TRUE;
return;
}
if (aIsPassword) {
if (!mIsPasswordCallBacksInstalled) {
XtAddCallback(mWidget, XmNmodifyVerifyCallback, nsXtWidget_Text_Callback, NULL);
XtAddCallback(mWidget, XmNactivateCallback, nsXtWidget_Text_Callback, NULL);
if (aIsPassword)
{
if (!mIsPasswordCallBacksInstalled)
{
mIsPasswordCallBacksInstalled = PR_TRUE;
}
} else {
if (mIsPasswordCallBacksInstalled) {
XtRemoveCallback(mWidget, XmNmodifyVerifyCallback, nsXtWidget_Text_Callback, NULL);
XtRemoveCallback(mWidget, XmNactivateCallback, nsXtWidget_Text_Callback, NULL);
}
}
else
{
if (mIsPasswordCallBacksInstalled)
{
mIsPasswordCallBacksInstalled = PR_FALSE;
}
}
mHelper->SetPassword(aIsPassword);
}
}
//mHelper->SetPassword(aIsPassword);
}
//--------------------------------------------------------------
PRBool nsTextWidget::SetReadOnly(PRBool aReadOnlyFlag)
{
if (mWidget == nsnull && aReadOnlyFlag) {
if ( aReadOnlyFlag)
{
mMakeReadOnly = PR_TRUE;
return PR_TRUE;
}
return mHelper->SetReadOnly(aReadOnlyFlag);
}
}
//--------------------------------------------------------------
void nsTextWidget::SetMaxTextLength(PRUint32 aChars)
{
mHelper->SetMaxTextLength(aChars);
}
//--------------------------------------------------------------
PRUint32 nsTextWidget::GetText(nsString& aTextBuffer, PRUint32 aBufferSize) {
return mHelper->GetText(aTextBuffer, aBufferSize);
PRUint32 nsTextWidget::GetText(nsString& aTextBuffer, PRUint32 aBufferSize)
{
Handle thetext;
PRInt32 len,i;
char *str;
thetext = WEGetText(mTE_Data);
len = WEGetTextLength(mTE_Data);
HLock(thetext);
str = new char[len];
for(i=0;i<len;i++)
str[i] = (*thetext)[i];
HUnlock(thetext);
aTextBuffer.SetLength(0);
aTextBuffer.Append(str);
delete str;
return aTextBuffer.Length();
}
//--------------------------------------------------------------
PRUint32 nsTextWidget::SetText(const nsString& aText)
{
return mHelper->SetText(aText);
char buffer[256];
PRInt32 len;
this->RemoveText();
aText.ToCString(buffer,255);
len = strlen(buffer);
WEInsert(buffer,len,0,0,mTE_Data);
return len;
}
//--------------------------------------------------------------
PRUint32 nsTextWidget::InsertText(const nsString &aText, PRUint32 aStartPos, PRUint32 aEndPos)
{
return mHelper->InsertText(aText, aStartPos, aEndPos);
char buffer[256];
PRInt32 len;
aText.ToCString(buffer,255);
len = strlen(buffer);
WEInsert(buffer,len,0,0,mTE_Data);
return(len);
}
//--------------------------------------------------------------
void nsTextWidget::RemoveText()
{
mHelper->RemoveText();
WESetSelection(0, 32000,mTE_Data);
WEDelete(mTE_Data);
}
//--------------------------------------------------------------
void nsTextWidget::SelectAll()
{
mHelper->SelectAll();
WESetSelection(0, 32000,mTE_Data);
}
//--------------------------------------------------------------
void nsTextWidget::SetSelection(PRUint32 aStartSel, PRUint32 aEndSel)
{
mHelper->SetSelection(aStartSel, aEndSel);
WESetSelection(aStartSel, aEndSel,mTE_Data);
}
//--------------------------------------------------------------
void nsTextWidget::GetSelection(PRUint32 *aStartSel, PRUint32 *aEndSel)
{
mHelper->GetSelection(aStartSel, aEndSel);
WEGetSelection((long*)aStartSel,(long*)aEndSel,mTE_Data);
}
//--------------------------------------------------------------
void nsTextWidget::SetCaretPosition(PRUint32 aPosition)
{
mHelper->SetCaretPosition(aPosition);
//mHelper->SetCaretPosition(aPosition);
}
//--------------------------------------------------------------
PRUint32 nsTextWidget::GetCaretPosition()
{
return mHelper->GetCaretPosition();
//return mHelper->GetCaretPosition();
}
//--------------------------------------------------------------
PRBool nsTextWidget::AutoErase()
{
return mHelper->AutoErase();
//return mHelper->AutoErase();
}

Просмотреть файл

@ -21,7 +21,6 @@
#include "nsWindow.h"
#include "nsTextHelper.h"
#include "nsITextWidget.h"
typedef struct _PasswordData {
@ -30,7 +29,7 @@ typedef struct _PasswordData {
} PasswordData;
/**
* Native Motif single line edit control wrapper.
* Native Mac single line edit control wrapper.
*/
class nsTextWidget : public nsWindow
@ -77,13 +76,16 @@ public:
virtual PRUint32 GetCaretPosition();
virtual PRBool AutoErase();
void PrimitiveKeyDown(PRInt16 aKey,PRInt16 aModifiers);
protected:
PRBool mIsPasswordCallBacksInstalled;
nsTextHelper *mHelper;
private:
PRBool mMakeReadOnly;
PRBool mMakePassword;
PRBool mMakeReadOnly;
PRBool mMakePassword;
WEReference mTE_Data;
// this should not be public
static PRInt32 GetOuterOffset() {

Просмотреть файл

@ -29,6 +29,7 @@
PRBool nsToolkit::mInit = PR_FALSE;
nsWindow* nsToolkit::mFocusedWidget = nsnull;
//-------------------------------------------------------------------------
@ -64,6 +65,15 @@ nsToolkit::~nsToolkit()
{
}
//-------------------------------------------------------------------------
//
//
//
//-------------------------------------------------------------------------
void nsToolkit::SetFocus(nsWindow *aFocusWidget)
{
mFocusedWidget = aFocusWidget;
}
//-------------------------------------------------------------------------
//
@ -73,8 +83,6 @@ nsToolkit::~nsToolkit()
NS_DEFINE_IID(kIToolkitIID, NS_ITOOLKIT_IID);
NS_IMPL_ISUPPORTS(nsToolkit,kIToolkitIID);
//-------------------------------------------------------------------------
//
//

Просмотреть файл

@ -21,6 +21,8 @@
#include "nsIToolkit.h"
class nsWindow;
struct MethodInfo;
/**
@ -41,9 +43,12 @@ public:
virtual void Init(PRThread *aThread);
public:
void SetFocus(nsWindow *aFocusWidget); //{ mFocusedWidget = aFocusWidget;}
nsWindow *GetFocus() {return(mFocusedWidget);}
private:
static PRBool mInit;
static nsWindow *mFocusedWidget;
};

Просмотреть файл

@ -28,9 +28,9 @@
#include "nsButton.h"
#include "nsRadioButton.h"
#include "nsCheckButton.h"
#include "nsTextWidget.h"
//#include "nsScrollbar.h"
//#include "nsTextWidget.h"
//#include "nsTextAreaWidget.h"
//#include "nsFileWidget.h"
//#include "nsListBox.h"
@ -176,6 +176,9 @@ nsresult nsWidgetFactory::CreateInstance(nsISupports *aOuter,
else if ( mClassID.Equals(kCCheckButtonCID)) {
inst = new nsCheckButton(aOuter);
}
else if (mClassID.Equals(kCTextWidgetCID)) {
inst = new nsTextWidget(aOuter);
}
@ -190,9 +193,6 @@ nsresult nsWidgetFactory::CreateInstance(nsISupports *aOuter,
inst = nsnull;
fprintf(stderr, "------ NOT CreatingkIScrollbar Scrollbar\n");
}
else if (mClassID.Equals(kCTextWidgetCID)) {
inst = new nsTextWidget(aOuter);
}
else if (mClassID.Equals(kCTextAreaWidgetCID)) {
inst = new nsTextAreaWidget(aOuter);
}

Просмотреть файл

@ -189,9 +189,8 @@ protected:
EVENT_CALLBACK mEventCallback;
nsIDeviceContext *mContext;
nsIFontMetrics *mFontMetrics;
nsToolkit *mToolkit;
nsToolkit *mToolkit;
nsIAppShell *mAppShell;
nsIMouseListener *mMouseListener;
nsIEventListener *mEventListener;