This commit is contained in:
rods 1998-06-16 18:25:35 +00:00
parent 8dcb1be175
commit 688c7a0577
2 changed files with 392 additions and 0 deletions

View File

@ -0,0 +1,288 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsRadioButton.h"
#include "nsColor.h"
#include "nsGUIEvent.h"
#include "nsString.h"
#include "nsStringUtil.h"
#include "nsXtEventHandler.h"
#include <Xm/ToggleB.h>
#include <Xm/RowColumn.h>
#define DBG 0
//-------------------------------------------------------------------------
//
// nsRadioButton constructor
//
//-------------------------------------------------------------------------
nsRadioButton::nsRadioButton(nsISupports *aOuter) :
nsWindow(aOuter),
mIsArmed(PR_FALSE)
{
}
//-------------------------------------------------------------------------
//
// nsRadioButton destructor
//
//-------------------------------------------------------------------------
nsRadioButton::~nsRadioButton()
{
}
//-------------------------------------------------------------------------
//
// nsRadioButton Creator
//
//-------------------------------------------------------------------------
void nsRadioButton::Create(nsIWidget *aParent,
const nsRect &aRect,
EVENT_CALLBACK aHandleEventFunction,
nsIDeviceContext *aContext,
nsIToolkit *aToolkit,
nsWidgetInitData *aInitData)
{
Widget parentWidget = nsnull;
if (DBG) fprintf(stderr, "aParent 0x%x\n", aParent);
if (aParent) {
parentWidget = (Widget) aParent->GetNativeData(NS_NATIVE_WIDGET);
} else {
parentWidget = (Widget) aInitData ;
}
if (DBG) fprintf(stderr, "Parent 0x%x\n", parentWidget);
mWidget = ::XmCreateRadioBox(parentWidget, "radio", nsnull, 0);
XtVaSetValues(mWidget, XmNwidth, aRect.width,
XmNheight, aRect.height,
XmNx, aRect.x,
XmNy, aRect.y,
nsnull);
mRadioBtn = ::XmCreateToggleButton(mWidget, "radio", nsnull, 0);
if (DBG) fprintf(stderr, "Button 0x%x this 0x%x\n", mWidget, this);
// save the event callback function
mEventCallback = aHandleEventFunction;
InitCallbacks();
XtAddCallback(mRadioBtn,
XmNarmCallback,
nsXtWidget_Toggle_ArmCallback,
this);
XtAddCallback(mRadioBtn,
XmNdisarmCallback,
nsXtWidget_Toggle_DisArmCallback,
this);
}
//-------------------------------------------------------------------------
//
// nsRadioButton Creator
//
//-------------------------------------------------------------------------
void nsRadioButton::Create(nsNativeWindow aParent,
const nsRect &aRect,
EVENT_CALLBACK aHandleEventFunction,
nsIDeviceContext *aContext,
nsIToolkit *aToolkit,
nsWidgetInitData *aInitData)
{
}
//-------------------------------------------------------------------------
//
// Query interface implementation
//
//-------------------------------------------------------------------------
nsresult nsRadioButton::QueryObject(REFNSIID aIID, void** aInstancePtr)
{
static NS_DEFINE_IID(kIRadioButtonIID, NS_IRADIOBUTTON_IID);
if (aIID.Equals(kIRadioButtonIID)) {
AddRef();
*aInstancePtr = (void**) &mAggWidget;
return NS_OK;
}
return nsWindow::QueryObject(aIID, aInstancePtr);
}
//-------------------------------------------------------------------------
//
// Armed
//
//-------------------------------------------------------------------------
void nsRadioButton::Armed()
{
mIsArmed = PR_TRUE;
mValueWasSet = PR_FALSE;
mInitialState = XmToggleButtonGetState(mRadioBtn);
if (DBG) printf("Arm: InitialValue: %d\n", mInitialState);
}
//-------------------------------------------------------------------------
//
// DisArmed
//
//-------------------------------------------------------------------------
void nsRadioButton::DisArmed()
{
if (DBG) printf("DisArm: InitialValue: %d\n", mInitialState);
if (DBG) printf("DisArm: ActualValue: %d\n", XmToggleButtonGetState(mRadioBtn));
if (DBG) printf("DisArm: mValueWasSet %d\n", mValueWasSet);
if (DBG) printf("DisArm: mNewValue %d\n", mNewValue);
if (mValueWasSet) {
XmToggleButtonSetState(mRadioBtn, mNewValue, TRUE);
} else {
XmToggleButtonSetState(mRadioBtn, mInitialState, TRUE);
}
mIsArmed = PR_FALSE;
}
//-------------------------------------------------------------------------
//
// Set this button label
//
//-------------------------------------------------------------------------
void nsRadioButton::SetState(PRBool aState)
{
int state = aState;
if (mIsArmed) {
mNewValue = aState;
mValueWasSet = PR_TRUE;
}
XmToggleButtonSetState(mRadioBtn, aState, TRUE);
}
//-------------------------------------------------------------------------
//
// Set this button label
//
//-------------------------------------------------------------------------
PRBool nsRadioButton::GetState()
{
int state = XmToggleButtonGetState(mRadioBtn);
if (mIsArmed) {
if (mValueWasSet) {
return mNewValue;
} else {
return state;
}
} else {
return state;
}
}
//-------------------------------------------------------------------------
//
// Set this button label
//
//-------------------------------------------------------------------------
void nsRadioButton::SetLabel(const nsString& aText)
{
NS_ALLOC_STR_BUF(label, aText, 256);
XmString str;
str = XmStringCreate(label, XmFONTLIST_DEFAULT_TAG);
XtVaSetValues(mRadioBtn, XmNlabelString, str, nsnull);
NS_FREE_STR_BUF(label);
XmStringFree(str);
}
//-------------------------------------------------------------------------
//
// Get this button label
//
//-------------------------------------------------------------------------
void nsRadioButton::GetLabel(nsString& aBuffer)
{
XmString str;
XtVaGetValues(mRadioBtn, XmNlabelString, &str, nsnull);
char * text;
if (XmStringGetLtoR(str, XmFONTLIST_DEFAULT_TAG, &text)) {
aBuffer.SetLength(0);
aBuffer.Append(text);
XtFree(text);
}
XmStringFree(str);
}
//-------------------------------------------------------------------------
//
// move, paint, resizes message - ignore
//
//-------------------------------------------------------------------------
PRBool nsRadioButton::OnMove(PRInt32, PRInt32)
{
return PR_FALSE;
}
PRBool nsRadioButton::OnPaint(nsPaintEvent &aEvent)
{
return PR_FALSE;
}
PRBool nsRadioButton::OnResize(nsSizeEvent &aEvent)
{
return PR_FALSE;
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
#define GET_OUTER() \
((nsRadioButton*) ((char*)this - nsRadioButton::GetOuterOffset()))
PRBool nsRadioButton::AggRadioButton::GetState()
{
return GET_OUTER()->GetState();
}
void nsRadioButton::AggRadioButton::SetState(PRBool aState)
{
GET_OUTER()->SetState(aState);
}
void nsRadioButton::AggRadioButton::SetLabel(const nsString& aText)
{
GET_OUTER()->SetLabel(aText);
}
void nsRadioButton::AggRadioButton::GetLabel(nsString& aText)
{
GET_OUTER()->GetLabel(aText);
}
//----------------------------------------------------------------------
BASE_IWIDGET_IMPL(nsRadioButton, AggRadioButton);

View File

@ -0,0 +1,104 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsRadioButton_h__
#define nsRadioButton_h__
#include "nsWindow.h"
#include "nsIRadioButton.h"
/**
* Native Motif Radiobutton wrapper
*/
class nsRadioButton : public nsWindow
{
public:
nsRadioButton(nsISupports *aOuter);
virtual ~nsRadioButton();
NS_IMETHOD QueryObject(REFNSIID aIID, void** aInstancePtr);
void Create(nsIWidget *aParent,
const nsRect &aRect,
EVENT_CALLBACK aHandleEventFunction,
nsIDeviceContext *aContext = nsnull,
nsIToolkit *aToolkit = nsnull,
nsWidgetInitData *aInitData = nsnull);
void Create(nsNativeWindow aParent,
const nsRect &aRect,
EVENT_CALLBACK aHandleEventFunction,
nsIDeviceContext *aContext = nsnull,
nsIToolkit *aToolkit = nsnull,
nsWidgetInitData *aInitData = nsnull);
// nsIRadioButton part
virtual void SetLabel(const nsString& aText);
virtual void GetLabel(nsString& aBuffer);
virtual PRBool OnMove(PRInt32 aX, PRInt32 aY);
virtual PRBool OnPaint(nsPaintEvent &aEvent);
virtual PRBool OnResize(nsSizeEvent &aEvent);
virtual void SetState(PRBool aState);
virtual PRBool GetState();
// These are needed to Override the auto check behavior
void Armed();
void DisArmed();
private:
// this should not be public
static PRInt32 GetOuterOffset() {
return offsetof(nsRadioButton,mAggWidget);
}
Widget mRadioBtn;
Boolean mInitialState;
Boolean mNewValue;
Boolean mValueWasSet;
Boolean mIsArmed;
// Aggregator class and instance variable used to aggregate in the
// nsIButton interface to nsRadioButton w/o using multiple
// inheritance.
class AggRadioButton : public nsIRadioButton {
public:
AggRadioButton();
virtual ~AggRadioButton();
AGGRRGATE_METHOD_DEF
// nsIRadioButton
virtual void SetLabel(const nsString &aText);
virtual void GetLabel(nsString &aBuffer);
virtual void SetState(PRBool aState);
virtual PRBool GetState();
};
AggRadioButton mAggWidget;
};
#endif // nsRadioButton_h__