/******************************************************************************
 JXSRTextBase.h

	Interface for the JXSRTextBase class

	Copyright © 1996 by John Lindal. All rights reserved.

 ******************************************************************************/

#ifndef _H_JXSRTextBase
#define _H_JXSRTextBase

#include <JXTEBase.h>

class JXSRTextDialogBase;

class JXSRTextBase : public JXTEBase
{
public:

	enum SRCmdIndex
	{
		kSRFindCmd = 1,
		kSRFindPrevCmd,
		kSRFindNextCmd,
		kSREnterSearchStrCmd,
		kSREnterReplaceStrCmd,
		kSRFindSelectionBackCmd, 
		kSRFindSelectionFwdCmd,
		kSRReplaceCmd,
		kSRReplaceAndFindPrevCmd, 
		kSRReplaceAndFindNextCmd,
		kSRReplaceAllForwardCmd, 
		kSRReplaceAllBackwardCmd,

		kSRCmdCount = kSRReplaceAllBackwardCmd
	};

	enum SCmdIndex
	{
		kSFindCmd = 1,
		kSFindPrevCmd,
		kSFindNextCmd,
		kSEnterSearchStrCmd,
		kSFindSelectionBackCmd, 
		kSFindSelectionFwdCmd,

		kSCmdCount = kSFindSelectionFwdCmd
	};

	virtual ~JXSRTextBase();

	void		AppendSearchMenu(JXSRTextDialogBase* dialog,
									JXMenuBar* menuBar, const JXMenu::Style menuStyle);
	void		ShareSearchMenu(JXSRTextBase* obj);
	JXTextMenu*	GetSearchMenu() const;
	JBoolean	GetSearchMenuCmd(const JIndex index, SCmdIndex* cmd) const;

	void		AppendSearchReplaceMenu(JXSRTextDialogBase* dialog,
									JXMenuBar* menuBar, const JXMenu::Style menuStyle);
	void		ShareSearchReplaceMenu(JXSRTextBase* obj);
	JXTextMenu*	GetSearchReplaceMenu() const;
	JBoolean	GetSearchReplaceMenuCmd(const JIndex index, SRCmdIndex* cmd) const;

	void		SetFindDialogShortcut(const JCharacter* str);

	JBoolean	SRSearchForward();
	JBoolean	SRSearchBackward();
	JBoolean	EnterSearchSelection();
	JBoolean	EnterReplaceSelection();
	JBoolean	SearchSelectionForward();
	JBoolean	SearchSelectionBackward();
	JBoolean	ReplaceSelection();
	JBoolean	ReplaceAndSearchForward();
	JBoolean	ReplaceAndSearchBackward();
	JBoolean	ReplaceAllForward();
	JBoolean	ReplaceAllBackward();

	virtual void	HandleKeyPress(const int key, const JXKeyModifiers& modifiers);
	virtual void	HandleShortcut(const int key, const JXKeyModifiers& modifiers);

protected:

	JXSRTextBase(const Type type, const JBoolean breakCROnly,
					const JBoolean pasteStyledText,
					JXScrollbarSet* scrollbarSet, JXContainer* enclosure,
					const HSizingOption hSizing, const VSizingOption vSizing,
					const JCoordinate x, const JCoordinate y,
					const JCoordinate w, const JCoordinate h);

	virtual void	HandleMouseDown(const JPoint& pt, const JXMouseButton button,
									const JSize clickCount,
									const JXButtonStates& buttonStates,
									const JXKeyModifiers& modifiers);

	virtual void	Receive(JBroadcaster* sender, const Message& message);
	void			SetAsActiveTextEditor();

private:

	JXTextMenu*			itsSearchMenu;				// owned by the menu bar
	JXTextMenu*			itsSearchReplaceMenu;		// owned by the menu bar
	JXMenu::Style		itsSearchMenuStyle;
	JXMenu::Style		itsSearchReplaceMenuStyle;
	JXSRTextDialogBase*	itsSearchDialog;

private:

	void	UpdateSearchMenu();
	void	HandleSearchMenu(const JIndex index);

	void	UpdateSearchReplaceMenu();
	void	HandleSearchReplaceMenu(const JIndex index);

	void	AdjustRangesForReplace(JArray<JIndexRange>* list);
	void	PrivateReplaceSelection(const JString& replaceStr,
									const JBoolean preserveCase,
									const JBoolean replaceIsRegex,
									const JRegex& regex,
									const JArray<JIndexRange>& submatchList);

	// not allowed

	JXSRTextBase(const JXSRTextBase& source);
	const JXSRTextBase& operator=(const JXSRTextBase& source);
};


/******************************************************************************
 Search for selection

 ******************************************************************************/

inline JBoolean
JXSRTextBase::SearchSelectionForward()
{
	return JConvertToBoolean( EnterSearchSelection() && SRSearchForward() );
}

inline JBoolean
JXSRTextBase::SearchSelectionBackward()
{
	return JConvertToBoolean( EnterSearchSelection() && SRSearchBackward() );
}

/******************************************************************************
 Replace and search again

 ******************************************************************************/

inline JBoolean
JXSRTextBase::ReplaceAndSearchForward()
{
	return JI2B(ReplaceSelection() && SRSearchForward());
}

inline JBoolean
JXSRTextBase::ReplaceAndSearchBackward()
{
	return JI2B(ReplaceSelection() && SRSearchBackward());
}

#endif
