Added support for generating tooltip events

This commit is contained in:
kmcclusk 1998-05-04 19:25:25 +00:00
Родитель 90c5a32479
Коммит 9ad3d2ec08
5 изменённых файлов: 350 добавлений и 32 удалений

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

@ -107,29 +107,35 @@ struct nsKeyEvent : public nsGUIEvent {
#define NS_WINDOW_START 100
// Sent when window is created
// Widget is being created
#define NS_CREATE (NS_WINDOW_START)
// Sent when window is destroyed
// Widget is being destroyed
#define NS_DESTROY (NS_WINDOW_START + 1)
// Sent when the window is resized
// Widget was resized
#define NS_SIZE (NS_WINDOW_START + 2)
// Sent when window gains focus
// Widget gained focus
#define NS_GOTFOCUS (NS_WINDOW_START + 3)
// Sent when window loses focus
// Widget lost focus
#define NS_LOSTFOCUS (NS_WINDOW_START + 4)
// Sent when the window needs to be repainted
// Widget needs to be repainted
#define NS_PAINT (NS_WINDOW_START + 30)
// Sent when a key is pressed down within a window
// Key is pressed down within a window
#define NS_KEY_UP (NS_WINDOW_START + 32)
// Sent when a key is released within a window
// Key is released within a window
#define NS_KEY_DOWN (NS_WINDOW_START + 33)
// Sent when the window has been moved to a new location.
// Window has been moved to a new location.
// The events point contains the x, y location in screen coordinates
#define NS_MOVE (NS_WINDOW_START + 34)
// Sent when a tab control's selected tab has changed
// Tab control's selected tab has changed
#define NS_TABCHANGE (NS_WINDOW_START + 35)
// Tooltip should be shown
#define NS_SHOW_TOOLTIP (NS_WINDOW_START + 36)
// Tooltip should be hidden
#define NS_HIDE_TOOLTIP (NS_WINDOW_START + 37)
#define NS_MOUSE_MESSAGE_START 300
#define NS_MOUSE_MOVE (NS_MOUSE_MESSAGE_START)
#define NS_MOUSE_LEFT_BUTTON_UP (NS_MOUSE_MESSAGE_START + 1)

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

@ -383,6 +383,35 @@ class nsIWidget : public nsISupports {
virtual void SetTitle(const nsString& aTitle) = 0;
/**
* Set the collection of tooltip rectangles.
* A NS_SHOW_TOOLTIP event is generated when the mouse hovers over one
* of the rectangles. a NS_HIDE_TOOLTIP event is generated when the mouse
* is moved or a new tooltip is displayed.
*
* @param aNumberOfTips number of tooltip areas.
* @param aTooltipArea array of x,y,width,height rectangles specifying hot areas
*
*/
virtual void SetTooltips(PRUint32 aNumberOfTips,const nsRect* aTooltipAreas) = 0;
/**
* Update the collection of tooltip rectangles. The number of tooltips must
* match the original number of tooltips specified in SetTooltips. Must be called
* after calling SetTooltips.
*
* @param aNewTips array of x,y,width,height rectangles specifying the new hot areas
*
*/
virtual void UpdateTooltips(const nsRect* aNewTips) = 0;
/**
* Remove the collection of tooltip rectangles.
*/
virtual void RemoveTooltips() = 0;
};

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

@ -39,6 +39,111 @@ nsWindow * mCurrentWindow = NULL;
static NS_DEFINE_IID(kIWidgetIID, NS_IWIDGET_IID);
// DoCreateTooltip - creates a tooltip control and adds some tools
// to it.
// Returns the handle of the tooltip control if successful or NULL
// otherwise.
// hwndOwner - handle of the owner window
//
// Global variable
// g_hinst - handle of the application instance
extern HINSTANCE g_hinst;
void nsWindow::AddTooltip(HWND hwndOwner,nsRect& aRect)
{
// HWND hwndTT; // handle of tooltip
TOOLINFO ti; // tool information
// Make sure the common control DLL is loaded
InitCommonControls();
// Create a tooltip control for the window if needed
if (mTooltip == (HWND) NULL) {
mTooltip = CreateWindow(TOOLTIPS_CLASS, (LPSTR) NULL, TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, (HMENU) NULL,
nsToolkit::mDllInstance,
NULL);
}
if (mTooltip == (HWND) NULL)
return;
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hwndOwner;
ti.hinst = nsToolkit::mDllInstance;
ti.uId = 0;
ti.lpszText = (LPSTR)" "; // must set text to
// something for tooltip to give events;
ti.rect.left = aRect.x;
ti.rect.top = aRect.y;
ti.rect.right = aRect.x + aRect.width;
ti.rect.bottom = aRect.y + aRect.height;
if (!SendMessage(mTooltip, TTM_ADDTOOL, 0,
(LPARAM) (LPTOOLINFO) &ti))
return;
// return hwndTT;
}
//-------------------------------------------------------------------------
//
// Setup initial tooltip rectangles
//
//-------------------------------------------------------------------------
void nsWindow::SetTooltips(PRUint32 aNumberOfTips,const nsRect* aTooltipAreas)
{
RemoveTooltips();
for (int i = 0; i < (int)aNumberOfTips; i++) {
AddTooltip(mWnd, (nsRect)(aTooltipAreas[i]));
}
}
//-------------------------------------------------------------------------
//
// Update all tooltip rectangles
//
//-------------------------------------------------------------------------
void nsWindow::UpdateTooltips(const nsRect* aNewTips)
{
TOOLINFO ti;
// Get the number of tooltips
UINT count = ::SendMessage(mTooltip, TTM_GETTOOLCOUNT, 0, 0);
NS_ASSERTION(count > 0, "Called UpdateTooltips before calling SetTooltips");
for (UINT i = 0; i < count; i++) {
::SendMessage(mTooltip, TTM_ENUMTOOLS, i, (LPARAM) (LPTOOLINFO)&ti);
ti.rect.left = aNewTips[i].x;
ti.rect.top = aNewTips[i].y;
ti.rect.right = aNewTips[i].x + aNewTips[i].width;
ti.rect.bottom = aNewTips[i].y + aNewTips[i].height;
::SendMessage(mTooltip, TTM_NEWTOOLRECT, 0, (LPARAM) (LPTOOLINFO)&ti);
}
}
//-------------------------------------------------------------------------
//
// Remove all tooltip rectangles
//
//-------------------------------------------------------------------------
void nsWindow::RemoveTooltips()
{
TOOLINFO ti;
long val;
// Get the number of tooltips
UINT count = ::SendMessage(mTooltip, TTM_GETTOOLCOUNT, 0, (LPARAM)&val);
for (UINT i = 0; i < count; i++) {
::SendMessage(mTooltip, TTM_ENUMTOOLS, i,(LPARAM) (LPTOOLINFO)&ti);
::SendMessage(mTooltip, TTM_DELTOOL, 0, (LPARAM) (LPTOOLINFO)&ti);
}
}
//-------------------------------------------------------------------------
//
// Convert nsEventStatus value to a windows boolean
@ -113,6 +218,18 @@ PRBool nsWindow::DispatchEvent(nsGUIEvent* event)
}
}
//-------------------------------------------------------------------------
//
// Dispatch standard event
//
//-------------------------------------------------------------------------
PRBool nsWindow::DispatchStandardEvent(PRUint32 aMsg)
{
nsGUIEvent event;
InitEvent(event, aMsg);
return(DispatchEvent(&event));
}
//-------------------------------------------------------------------------
//
@ -170,6 +287,7 @@ nsWindow::nsWindow(nsISupports *aOuter) : nsObject(aOuter)
mIsControlDown = PR_FALSE;
mIsAltDown = PR_FALSE;
mIsDestroying = PR_FALSE;
mTooltip = NULL;
}
@ -303,9 +421,7 @@ void nsWindow::Create(nsIWidget *aParent,
// call the event callback to notify about creation
nsGUIEvent event;
InitEvent(event, NS_CREATE);
DispatchEvent(&event);
DispatchStandardEvent(NS_CREATE);
SubclassWindow(TRUE);
}
@ -393,9 +509,7 @@ void nsWindow::Create(nsNativeWindow aParent,
VERIFY(mWnd);
// call the event callback to notify about creation
nsGUIEvent event;
InitEvent(event, NS_CREATE);
DispatchEvent(&event);
DispatchStandardEvent(NS_CREATE);
SubclassWindow(TRUE);
}
@ -427,6 +541,11 @@ void nsWindow::Destroy()
if (mPalette) {
VERIFY(::DeleteObject(mPalette));
}
// Destroy the tooltip control
if (mTooltip) {
VERIFY(::DestroyWindow(mTooltip));
}
}
@ -913,6 +1032,30 @@ void nsWindow::Scroll(PRInt32 aDx, PRInt32 aDy, nsRect *aClipRect)
::UpdateWindow(mWnd);
}
//-------------------------------------------------------------------------
//
// Relay mouse events to the tooltip control
//
//-------------------------------------------------------------------------
void nsWindow::RelayMouseEvent(UINT aMsg, WPARAM wParam, LPARAM lParam)
{
printf("relaying event\n");
#if 0
MSG msg;
msg.hwnd = mWnd;
msg.message = aMsg;
msg.wParam = wParam;
msg.lParam = lParam;
msg.time = ::GetMessageTime();
DWORD pos = ::GetMessagePos();
POINT pt;
msg.pt.x = LOWORD(pos);
msg.pt.y = HIWORD(pos);
::SendMessage(mTooltip, TTM_RELAYEVENT, 0, (LPARAM)(LPMSG) &msg);
#endif
}
//-------------------------------------------------------------------------
//
// Every function that needs a thread switch goes through this function
@ -999,11 +1142,20 @@ PRBool nsWindow::ProcessMessage(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT
{
LPNMHDR pnmh = (LPNMHDR) lParam;
if (pnmh->code == TCN_SELCHANGE) {
nsGUIEvent event;
InitEvent(event, NS_TABCHANGE);
DispatchEvent(&event);
result = PR_TRUE;
switch (pnmh->code) {
case TCN_SELCHANGE: {
DispatchStandardEvent(NS_TABCHANGE);
result = PR_TRUE;
}
break;
case TTN_SHOW:
result = DispatchStandardEvent(NS_SHOW_TOOLTIP);
break;
case TTN_POP:
result = DispatchStandardEvent(NS_HIDE_TOOLTIP);
break;
}
}
break;
@ -1064,14 +1216,17 @@ PRBool nsWindow::ProcessMessage(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT
break;
case WM_MOUSEMOVE:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_MOVE);
break;
case WM_LBUTTONDOWN:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_LEFT_BUTTON_DOWN);
break;
case WM_LBUTTONUP:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_LEFT_BUTTON_UP);
break;
@ -1082,10 +1237,12 @@ PRBool nsWindow::ProcessMessage(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT
break;
case WM_MBUTTONDOWN:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_MIDDLE_BUTTON_DOWN);
break;
case WM_MBUTTONUP:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_MIDDLE_BUTTON_UP);
break;
@ -1094,10 +1251,12 @@ PRBool nsWindow::ProcessMessage(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT
break;
case WM_RBUTTONDOWN:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_RIGHT_BUTTON_DOWN);
break;
case WM_RBUTTONUP:
//RelayMouseEvent(msg,wParam, lParam);
result = DispatchMouseEvent(NS_MOUSE_RIGHT_BUTTON_UP);
break;
@ -1312,9 +1471,7 @@ void nsWindow::OnDestroy()
mToolkit = NULL;
}
nsGUIEvent event;
InitEvent(event, NS_DESTROY);
DispatchEvent(&event);
DispatchStandardEvent(NS_DESTROY);
}
@ -1481,9 +1638,7 @@ PRBool nsWindow::DispatchFocus(PRUint32 aEventType)
{
// call the event callback
if (mEventCallback) {
nsGUIEvent event;
InitEvent(event, aEventType);
return(DispatchEvent(&event));
return(DispatchStandardEvent(aEventType));
}
return PR_FALSE;

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

@ -109,6 +109,9 @@ public:
virtual nsIToolkit* GetToolkit();
virtual void SetBorderStyle(nsBorderStyle aBorderStyle);
virtual void SetTitle(const nsString& aTitle);
virtual void SetTooltips(PRUint32 aNumberOfTips,const nsRect* aTooltipAreas);
virtual void RemoveTooltips();
virtual void UpdateTooltips(const nsRect* aNewTips);
virtual void AddMouseListener(nsIMouseListener * aListener);
virtual void AddEventListener(nsIEventListener * aListener);
@ -150,9 +153,13 @@ protected:
void InitEvent(nsGUIEvent& event, PRUint32 aEventType);
PRBool DispatchEvent(nsGUIEvent* event);
PRBool DispatchStandardEvent(PRUint32 aMsg);
void AddTooltip(HWND hwndOwner, nsRect& aRect);
void RelayMouseEvent(UINT aMsg, WPARAM wParam, LPARAM lParam);
protected:
HWND mWnd;
HWND mTooltip;
HPALETTE mPalette;
WNDPROC mPrevWndProc;
EVENT_CALLBACK mEventCallback;
@ -380,7 +387,19 @@ protected:
void SetTitle(const nsString& aTitle) \
{ \
nsWindow::SetTitle(aTitle); \
}
} \
void SetTooltips(PRUint32 aNumberOfTips,const nsRect* aTooltipAreas) \
{ \
nsWindow::SetTooltips(aNumberOfTips, aTooltipAreas); \
} \
void UpdateTooltips(const nsRect* aNewTips) \
{ \
nsWindow::UpdateTooltips(aNewTips); \
} \
void RemoveTooltips() \
{ \
nsWindow::RemoveTooltips(); \
}

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

@ -68,6 +68,10 @@ nsIListBox *gMultiListBox = NULL;
nsIWidget *movingWidget = NULL;
nsIScrollbar *scrollbar = NULL;
nsITabWidget *tabWidget = NULL;
nsIButton *toolTipButton1 = NULL;
nsIButton *toolTipButton2 = NULL;
nsIButton *tooltipWindow = NULL;
char * gFailedMsg = NULL;
@ -90,6 +94,16 @@ char * gFailedMsg = NULL;
#define kShowBtn "Show Btn"
#define kBrowseBtn "Browse..."
#define kTooltip1_x 400
#define kTooltip1_y 100
#define kTooltip1_width 100
#define kTooltip1_height 100
#define kTooltip2_x 200
#define kTooltip2_y 300
#define kTooltip2_width 100
#define kTooltip2_height 100
// class ids
static NS_DEFINE_IID(kCWindowCID, NS_WINDOW_CID);
static NS_DEFINE_IID(kCChildCID, NS_CHILD_CID);
@ -345,6 +359,27 @@ int createTestButton(nsIWidget * aWin,
return aX + aWidth;
}
/**--------------------------------------------------------------------------------
*
*/
nsIButton* createSimpleButton(nsIWidget * aWin,
char * aTitle,
int aX,
int aY,
int aWidth,
EVENT_CALLBACK aHandleEventFunction) {
nsIButton *button;
nsRect rect(aX, aY, aWidth, 25);
NSRepository::CreateInstance(kCButtonCID, nsnull, kIButtonIID, (LPVOID*)&button);
button->Create(window, rect, aHandleEventFunction, NULL);
nsString label(aTitle);
button->SetLabel(label);
button->Show(PR_TRUE);
// NS_RELEASE(button);
return button;
}
/**--------------------------------------------------------------------------------
* List Test Handler
*--------------------------------------------------------------------------------
@ -701,6 +736,17 @@ nsEventStatus PR_CALLBACK HandleEvent(nsGUIEvent *aEvent)
nsEventStatus result = nsEventStatus_eIgnore;
switch(aEvent->message) {
case NS_SHOW_TOOLTIP:
statusText->SetText("Show tooltip");
tooltipWindow->Move(aEvent->point.x + 5, aEvent->point.y + 5);
tooltipWindow->Show(PR_TRUE);
break;
case NS_HIDE_TOOLTIP:
statusText->SetText("Hide tooltip");
tooltipWindow->Show(PR_FALSE);
break;
case NS_MOVE:
char str[256];
sprintf(str, "Moved window to %d,%d", aEvent->point.x, aEvent->point.y);
@ -861,6 +907,57 @@ nsEventStatus PR_CALLBACK HandleTabEvent(nsGUIEvent *aEvent)
}
void SetTooltipPos(int pos, nsIWidget *aWidget, nsIButton *aButton1, nsIButton *aButton2)
{
switch(pos) {
case 1: {
nsRect* tips1 = {&nsRect(kTooltip1_x,kTooltip1_y,
kTooltip1_width,kTooltip1_height)};
aWidget->SetTooltips(1, tips1);
aButton1->Move(kTooltip1_x, kTooltip1_y);
aButton2->Move(kTooltip1_x + kTooltip1_width,
kTooltip1_y + kTooltip1_height);
}
break;
case 2: {
nsRect* tips2 = {&nsRect(kTooltip2_x,kTooltip2_y,
kTooltip2_width,kTooltip2_height)};
aWidget->SetTooltips(1, tips2);
aButton1->Move(kTooltip2_x, kTooltip2_y);
aButton2->Move(kTooltip2_x + kTooltip2_width,
kTooltip2_y + kTooltip2_height);
}
break;
}
}
nsEventStatus MoveTooltip(int aPos, nsGUIEvent *aEvent)
{
switch(aEvent->message) {
case NS_MOUSE_LEFT_BUTTON_DOWN:
SetTooltipPos(aPos, window, toolTipButton1, toolTipButton2);
break;
}
return(nsEventStatus_eConsumeDoDefault);
}
nsEventStatus PR_CALLBACK TooltipPos1(nsGUIEvent *aEvent)
{
return(MoveTooltip(1, aEvent));
}
nsEventStatus PR_CALLBACK TooltipPos2(nsGUIEvent *aEvent)
{
return(MoveTooltip(2, aEvent));
}
/**--------------------------------------------------------------------------------
@ -924,10 +1021,20 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmd
//
NSRepository::CreateInstance(kCWindowCID, nsnull, kIWidgetIID, (LPVOID*)&window);
nsRect rect(100, 100, 600, 700);
window->Create((nsIWidget*)NULL, rect, HandleEvent, NULL);
window->SetTitle("TOP-LEVEL window");
tooltipWindow = createSimpleButton(window, "INSERT <tooltip> here", 0, 0, 150, 0);
tooltipWindow->Show(PR_FALSE);
toolTipButton1 = createSimpleButton(window, "Tooltip \\/\\/",400, 100, 100, 0);
toolTipButton2 = createSimpleButton(window, "Tooltip /\\/\\",500, 200, 100, 0);
createTestButton(window, "Move Tooltip pos 1", 450, 150, 130, TooltipPos1);
createTestButton(window, "Move Tooltip pos 2", 450, 175, 130, TooltipPos2);
SetTooltipPos(1, window, toolTipButton1, toolTipButton2);
//
// create a child
//
@ -946,6 +1053,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmd
child->Create(window, rect, HandleEvent, NULL);
//child->SetBackgroundColor(NS_RGB(255, 255, 0));
child->SetForegroundColor(NS_RGB(255, 0, 0));
NS_RELEASE(child); // the parent keeps a reference on this child
y += rect.height + 5;
@ -1162,15 +1271,15 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmd
// create a tab widget
//
rect.SetRect(x, y, 300, 50);
rect.SetRect(300, 500, 200, 50);
NSRepository::CreateInstance(kCTabWidgetCID, nsnull, kITabWidgetIID, (LPVOID*)&tabWidget);
tabWidget->Create(window, rect, HandleTabEvent, NULL);
nsString tabs[] = {"low", "medium", "high" };
tabWidget->SetTabs(3, tabs);
tabWidget->Show(PR_TRUE);
y += rect.height + 5;
x = 10;
// y += rect.height + 5;
// x = 10;
//
// create a Radio Group