1998-03-28 05:44:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2004-04-25 20:36:22 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-03-28 05:44:41 +03:00
|
|
|
*
|
2004-04-25 20:36:22 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-03-28 05:44:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-25 20:36:22 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2004-04-25 20:36:22 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-03-28 05:44:41 +03:00
|
|
|
|
|
|
|
#include "CContextMenuAttachment.h"
|
1998-06-23 01:21:28 +04:00
|
|
|
#include "xp.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
|
|
|
|
#include "CDrawingState.h"
|
|
|
|
#include "CApplicationEventAttachment.h"
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
CContextMenuAttachment::CContextMenuAttachment(LStream* inStream)
|
|
|
|
: LAttachment(inStream)
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
*inStream >> mMenuID >> mTextTraitsID;
|
1998-10-09 01:56:26 +04:00
|
|
|
mHostView = dynamic_cast<LPane*>(LAttachable::GetDefaultAttachable());
|
|
|
|
Assert_(mHostView);
|
1998-03-28 05:44:41 +03:00
|
|
|
mHostCommander = dynamic_cast<LCommander*>(LAttachable::GetDefaultAttachable());
|
1998-10-09 01:56:26 +04:00
|
|
|
if ( !mHostCommander )
|
|
|
|
mHostCommander = LCommander::GetDefaultCommander();
|
|
|
|
Assert_(mHostCommander);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
CContextMenuAttachment::CContextMenuAttachment ( ResIDT inMenuID, ResIDT inTextTraitsID )
|
|
|
|
: LAttachment(), mMenuID(inMenuID), mTextTraitsID(inTextTraitsID)
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
mHostView = dynamic_cast<LPane*>(LAttachable::GetDefaultAttachable());
|
1998-03-28 05:44:41 +03:00
|
|
|
Assert_(mHostView);
|
1998-10-09 01:56:26 +04:00
|
|
|
mHostCommander = dynamic_cast<LCommander*>(LAttachable::GetDefaultAttachable());
|
|
|
|
if ( !mHostCommander )
|
|
|
|
mHostCommander = LCommander::GetDefaultCommander();
|
1998-03-28 05:44:41 +03:00
|
|
|
Assert_(mHostCommander);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
CContextMenuAttachment::~CContextMenuAttachment()
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
} // CContextMenuAttachment::CContextMenuAttachment
|
|
|
|
|
|
|
|
//-----------------------------------
|
1998-08-25 03:58:57 +04:00
|
|
|
EClickState CContextMenuAttachment::WaitMouseAction(
|
1998-03-28 05:44:41 +03:00
|
|
|
const Point& inInitialPoint,
|
|
|
|
Int32 inWhen,
|
|
|
|
Int32 inDelay)
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
//mHostView->FocusDraw();
|
|
|
|
|
|
|
|
if (CApplicationEventAttachment::CurrentEventHasModifiers(controlKey))
|
|
|
|
{
|
|
|
|
return eMouseTimeout;
|
|
|
|
}
|
|
|
|
|
1998-06-23 01:21:28 +04:00
|
|
|
if (inDelay == kDefaultDelay) // GetDblTime() can be 48, 32 or 20 ticks
|
|
|
|
inDelay = MAX(GetDblTime(), 30); // but 20 is too short to display the popup
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
while (::StillDown())
|
|
|
|
{
|
|
|
|
Point theCurrentPoint;
|
|
|
|
::GetMouse(&theCurrentPoint);
|
1998-08-25 03:58:57 +04:00
|
|
|
if ((abs(theCurrentPoint.h - inInitialPoint.h) >= kMouseHysteresis) ||
|
|
|
|
(abs(theCurrentPoint.v - inInitialPoint.v) >= kMouseHysteresis))
|
1998-03-28 05:44:41 +03:00
|
|
|
return eMouseDragging;
|
|
|
|
Int32 now = ::TickCount();
|
|
|
|
if (abs( now - inWhen ) > inDelay)
|
|
|
|
return eMouseTimeout;
|
|
|
|
}
|
|
|
|
return eMouseUpEarly;
|
|
|
|
} // CContextMenuAttachment::WaitMouseAction
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
UInt16 CContextMenuAttachment::PruneMenu(LMenu* inMenu)
|
|
|
|
// Prune the menu. Either enable each item, or remove it. Returns the menu count.
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
|
|
|
|
// Flag bits
|
|
|
|
#define kKeepItemDisabled 1<<0 /* Don't remove this item, show it disabled */
|
|
|
|
#define kSupercommanderCannotEnable 1<<1
|
|
|
|
#define kWantsToBeDefault 1<<2
|
|
|
|
// (NOTE: Bit 7 is not usable, because the icon ID must have this bit set!)
|
|
|
|
MenuHandle menuH = inMenu->GetMacMenuH();
|
|
|
|
Int16 index = 0; CommandT command;
|
|
|
|
Boolean previousItemWasSeparator = true; // true for first item
|
|
|
|
Boolean isSeparator = false;
|
|
|
|
Int16 itemCount = ::CountMItems(menuH);
|
|
|
|
UInt16 defaultItem = 0;
|
|
|
|
UInt16 firstEnabledItem = 0;
|
|
|
|
while (inMenu->FindNextCommand(index, command))
|
|
|
|
{
|
|
|
|
Boolean enabled = false;
|
|
|
|
Str255 commandText; // may be changed by host commander!
|
|
|
|
::GetMenuItemText(menuH, index, commandText);
|
|
|
|
Int16 itemFlags; // icon ID used for flags
|
|
|
|
::GetItemIcon(menuH, index, &itemFlags);
|
|
|
|
// Get rid of the icon ID (which we use for the flag hack)
|
|
|
|
if (itemFlags)
|
|
|
|
::SetItemIcon(menuH, index, 0);
|
|
|
|
if (command > 0)
|
|
|
|
{
|
|
|
|
Boolean usesMark = false;
|
|
|
|
Char16 outMark;
|
|
|
|
Boolean statusKnown = false;
|
|
|
|
// If a command is not to be enabled by virtue of its super alone, check
|
|
|
|
// the super first. If the super enables it, it's outta there.
|
|
|
|
if ((itemFlags & kSupercommanderCannotEnable) != 0)
|
|
|
|
{
|
|
|
|
LCommander* super = mHostCommander->GetSuperCommander();
|
|
|
|
if (super)
|
|
|
|
{
|
|
|
|
Boolean superEnabled = false;
|
|
|
|
mHostCommander->ProcessCommandStatus(
|
|
|
|
command, superEnabled, usesMark, outMark, commandText);
|
|
|
|
if (superEnabled)
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
statusKnown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Normal case. Ask the commander, let it use super if appropriate:
|
|
|
|
if (!statusKnown)
|
|
|
|
mHostCommander->ProcessCommandStatus(
|
|
|
|
command, enabled, usesMark, outMark, commandText);
|
|
|
|
}
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
::EnableItem(menuH, index);
|
|
|
|
::SetMenuItemText(menuH, index, commandText);
|
|
|
|
// Allow a separator on the next line
|
|
|
|
previousItemWasSeparator = false;
|
|
|
|
// First item that says "I can be default" wins...
|
|
|
|
if (defaultItem == 0 && (itemFlags & kWantsToBeDefault))
|
|
|
|
defaultItem = index;
|
|
|
|
if (firstEnabledItem == 0)
|
|
|
|
firstEnabledItem = index;
|
|
|
|
}
|
|
|
|
else // disabled, including separators
|
|
|
|
{
|
|
|
|
isSeparator = commandText[1] == '-';
|
|
|
|
if (!isSeparator || previousItemWasSeparator)
|
|
|
|
{
|
|
|
|
if (!isSeparator && (itemFlags & kKeepItemDisabled))
|
|
|
|
{
|
|
|
|
::DisableItem(menuH, index);
|
|
|
|
previousItemWasSeparator = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inMenu->RemoveItem(index);
|
|
|
|
// decrement index so that we refind the next item
|
|
|
|
index--;
|
|
|
|
itemCount--;
|
|
|
|
if (!itemCount) break;
|
|
|
|
// Uh Oh, all items after the last separator were removed!
|
|
|
|
if (index == itemCount && previousItemWasSeparator)
|
|
|
|
{
|
|
|
|
inMenu->RemoveItem(index);
|
|
|
|
index--;
|
|
|
|
itemCount--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isSeparator)
|
|
|
|
previousItemWasSeparator = true;
|
|
|
|
}
|
|
|
|
} // while
|
|
|
|
// The caller will only show the menu if we return a result > 0. So handle the
|
|
|
|
// case when there are items, but all items are disabled.
|
|
|
|
if (defaultItem == 0 && itemCount > 0)
|
|
|
|
defaultItem = firstEnabledItem ? firstEnabledItem : 1;
|
|
|
|
return defaultItem;
|
|
|
|
} // CContextMenuAttachment::PruneMenu
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
void CContextMenuAttachment::DoPopup(const SMouseDownEvent& inMouseDown, EClickState& outResult)
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
1998-06-23 01:21:28 +04:00
|
|
|
try
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
|
|
|
LMenu* menu = BuildMenu();
|
|
|
|
MenuHandle menuH = menu->GetMacMenuH();
|
|
|
|
|
|
|
|
CommandT command = 0;
|
|
|
|
// Call PruneMenu to strip out disabled commands. Do nothing if no commands
|
|
|
|
// are available.
|
|
|
|
UInt16 defaultItem = PruneMenu(menu);
|
|
|
|
if (defaultItem > 0)
|
|
|
|
{
|
|
|
|
Int16 saveFont = ::LMGetSysFontFam();
|
|
|
|
Int16 saveSize = ::LMGetSysFontSize();
|
|
|
|
if (mTextTraitsID)
|
|
|
|
{
|
|
|
|
TextTraitsH traitsH = UTextTraits::LoadTextTraits(mTextTraitsID);
|
|
|
|
if (traitsH)
|
|
|
|
{
|
|
|
|
::LMSetSysFontFam((**traitsH).fontNumber);
|
|
|
|
::LMSetSysFontSize((**traitsH).size);
|
|
|
|
::LMSetLastSPExtra(-1L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Handle the actual insertion into the hierarchical menubar
|
|
|
|
::InsertMenu(menuH, hierMenu);
|
|
|
|
// Bring up the menu.
|
|
|
|
Point whereGlobal = inMouseDown.wherePort;
|
|
|
|
mHostView->PortToGlobalPoint(whereGlobal);
|
|
|
|
Int16 result = ::PopUpMenuSelect(
|
|
|
|
menuH,
|
|
|
|
whereGlobal.v - 5,
|
|
|
|
whereGlobal.h - 5,
|
|
|
|
defaultItem);
|
|
|
|
mExecuteHost = false;
|
1998-08-25 03:58:57 +04:00
|
|
|
outResult = eMouseHandledByAttachment;
|
1998-03-28 05:44:41 +03:00
|
|
|
// Restore the system font
|
|
|
|
::LMSetSysFontFam(saveFont);
|
|
|
|
::LMSetSysFontSize(saveSize);
|
|
|
|
::LMSetLastSPExtra(-1L);
|
|
|
|
if (result)
|
|
|
|
command = menu->CommandFromIndex(result);
|
|
|
|
}
|
|
|
|
delete menu;
|
|
|
|
if (command)
|
|
|
|
mHostCommander->ObeyCommand(command, (void*)&inMouseDown);
|
|
|
|
}
|
1998-06-23 01:21:28 +04:00
|
|
|
catch(...)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
} // CContextMenuAttachment::DoPopup
|
|
|
|
|
1998-06-23 01:21:28 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
Boolean CContextMenuAttachment::Execute( MessageT inMessage, void *ioParam )
|
1998-03-28 05:44:41 +03:00
|
|
|
// Overridden to listen for multiple messages (context menu and context menu cursor)
|
1998-06-23 01:21:28 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
|
|
|
Boolean executeHost = true;
|
|
|
|
|
1998-06-23 01:21:28 +04:00
|
|
|
if ((inMessage == msg_ContextMenu) || (inMessage == msg_ContextMenuCursor))
|
|
|
|
{
|
1998-03-28 05:44:41 +03:00
|
|
|
ExecuteSelf(inMessage, ioParam);
|
|
|
|
executeHost = mExecuteHost;
|
|
|
|
}
|
|
|
|
return executeHost;
|
1998-06-23 01:21:28 +04:00
|
|
|
} // CContextMenuAttachment::Execute
|
1998-03-28 05:44:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------
|
|
|
|
void CContextMenuAttachment::ExecuteSelf(
|
|
|
|
MessageT inMessage,
|
|
|
|
void* ioParam)
|
|
|
|
//-----------------------------------
|
|
|
|
{
|
|
|
|
mExecuteHost = true;
|
|
|
|
if (!mHostView || !mHostCommander)
|
|
|
|
return;
|
1998-06-23 01:21:28 +04:00
|
|
|
switch ( inMessage )
|
|
|
|
{
|
1998-03-28 05:44:41 +03:00
|
|
|
case msg_ContextMenu:
|
1998-06-23 01:21:28 +04:00
|
|
|
mHostView->FocusDraw(); // so that coordinate computations are correct.
|
1998-03-28 05:44:41 +03:00
|
|
|
SExecuteParams& params = *(SExecuteParams*)ioParam;
|
|
|
|
const SMouseDownEvent& mouseDown = *params.inMouseDown;
|
|
|
|
params.outResult = WaitMouseAction(
|
|
|
|
mouseDown.whereLocal,
|
1998-06-23 01:21:28 +04:00
|
|
|
mouseDown.macEvent.when);
|
1998-03-28 05:44:41 +03:00
|
|
|
if (params.outResult == eMouseTimeout)
|
|
|
|
DoPopup(mouseDown, params.outResult);
|
|
|
|
break;
|
|
|
|
case msg_ContextMenuCursor:
|
|
|
|
Assert_(ioParam != NULL);
|
|
|
|
ChangeCursor ( (EventRecord*)ioParam );
|
|
|
|
break;
|
|
|
|
} // case of which message
|
|
|
|
} // CContextMenuAttachment::ExecuteSelf
|
|
|
|
|
1998-06-23 01:21:28 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
LMenu* CContextMenuAttachment::BuildMenu( )
|
|
|
|
// THROWS OSErr on error
|
|
|
|
// Reads in the menu with the id given in the PPob. This can be overridden to read the
|
|
|
|
// menu from some other place, such as from the back-end.
|
|
|
|
//----------------------------------------------------------------------------------------
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
|
|
|
LMenu* menu = new LMenu(mMenuID);
|
|
|
|
MenuHandle menuH = menu->GetMacMenuH();
|
|
|
|
if (!menuH)
|
1998-06-23 01:21:28 +04:00
|
|
|
throw (OSErr)ResError();
|
1998-03-28 05:44:41 +03:00
|
|
|
::DetachResource((Handle)menuH);
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
|
|
|
} // CContextMenuAttachment::BuildMenu
|
|
|
|
|
1998-06-23 01:21:28 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void CContextMenuAttachment::ChangeCursor( const EventRecord* inEvent )
|
1998-03-28 05:44:41 +03:00
|
|
|
// Makes the mouse the contextual menu cursor from OS8 if cmd key is down
|
1998-06-23 01:21:28 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
|
|
|
if ( inEvent->modifiers & controlKey ) {
|
|
|
|
CursHandle contextCurs = ::GetCursor ( kContextualCursorID );
|
|
|
|
::SetCursor ( *contextCurs );
|
|
|
|
mExecuteHost = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
::InitCursor();
|
|
|
|
} // ChangeCursor
|