Browse Source

Bit of tidying-up in KeyPressMappingSet and related classes.

tags/2021-05-28
jules 12 years ago
parent
commit
d0fa359f72
5 changed files with 92 additions and 117 deletions
  1. +4
    -5
      modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp
  2. +45
    -52
      modules/juce_gui_basics/commands/juce_KeyPressMappingSet.cpp
  3. +8
    -13
      modules/juce_gui_basics/commands/juce_KeyPressMappingSet.h
  4. +32
    -47
      modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp
  5. +3
    -0
      modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.h

+ 4
- 5
modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp View File

@@ -26,7 +26,7 @@
ApplicationCommandManager::ApplicationCommandManager()
: firstTarget (nullptr)
{
keyMappings = new KeyPressMappingSet (this);
keyMappings = new KeyPressMappingSet (*this);
Desktop::getInstance().addFocusChangeListener (this);
}
@@ -256,14 +256,13 @@ ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget(
if (c != nullptr)
{
ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c);
// if we're focused on a ResizableWindow, chances are that it's the content
// component that really should get the event. And if not, the event will
// still be passed up to the top level window anyway, so let's send it to the
// content comp.
if (resizableWindow != nullptr && resizableWindow->getContentComponent() != nullptr)
c = resizableWindow->getContentComponent();
if (ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c))
if (resizableWindow->getContentComponent() != nullptr)
c = resizableWindow->getContentComponent();
if (ApplicationCommandTarget* const target = findTargetForComponent (c))
return target;


+ 45
- 52
modules/juce_gui_basics/commands/juce_KeyPressMappingSet.cpp View File

@@ -23,13 +23,9 @@
==============================================================================
*/
KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager* const commandManager_)
: commandManager (commandManager_)
KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager& cm)
: commandManager (cm)
{
// A manager is needed to get the descriptions of commands, and will be called when
// a command is invoked. So you can't leave this null..
jassert (commandManager_ != nullptr);
Desktop::getInstance().addFocusChangeListener (this);
}
@@ -78,9 +74,7 @@ void KeyPressMappingSet::addKeyPress (const CommandID commandID,
}
}
const ApplicationCommandInfo* const ci = commandManager->getCommandForID (commandID);
if (ci != nullptr)
if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID))
{
CommandMapping* const cm = new CommandMapping();
cm->commandID = commandID;
@@ -98,9 +92,9 @@ void KeyPressMappingSet::resetToDefaultMappings()
{
mappings.clear();
for (int i = 0; i < commandManager->getNumCommands(); ++i)
for (int i = 0; i < commandManager.getNumCommands(); ++i)
{
const ApplicationCommandInfo* const ci = commandManager->getCommandForIndex (i);
const ApplicationCommandInfo* const ci = commandManager.getCommandForIndex (i);
for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
{
@@ -116,7 +110,7 @@ void KeyPressMappingSet::resetToDefaultMapping (const CommandID commandID)
{
clearAllKeyPresses (commandID);
const ApplicationCommandInfo* const ci = commandManager->getCommandForID (commandID);
const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID);
for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
{
@@ -152,13 +146,13 @@ void KeyPressMappingSet::removeKeyPress (const KeyPress& keypress)
{
for (int i = mappings.size(); --i >= 0;)
{
CommandMapping* const cm = mappings.getUnchecked(i);
CommandMapping& cm = *mappings.getUnchecked(i);
for (int j = cm->keypresses.size(); --j >= 0;)
for (int j = cm.keypresses.size(); --j >= 0;)
{
if (keypress == cm->keypresses [j])
if (keypress == cm.keypresses [j])
{
cm->keypresses.remove (j);
cm.keypresses.remove (j);
sendChangeMessage();
}
}
@@ -212,7 +206,7 @@ void KeyPressMappingSet::invokeCommand (const CommandID commandID,
info.millisecsSinceKeyPressed = millisecsSinceKeyPressed;
info.originatingComponent = originatingComponent;
commandManager->invoke (info, false);
commandManager.invoke (info, false);
}
//==============================================================================
@@ -275,18 +269,18 @@ XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefault
for (int i = 0; i < mappings.size(); ++i)
{
const CommandMapping* const cm = mappings.getUnchecked(i);
const CommandMapping& cm = *mappings.getUnchecked(i);
for (int j = 0; j < cm->keypresses.size(); ++j)
for (int j = 0; j < cm.keypresses.size(); ++j)
{
if (defaultSet == nullptr
|| ! defaultSet->containsMapping (cm->commandID, cm->keypresses.getReference (j)))
|| ! defaultSet->containsMapping (cm.commandID, cm.keypresses.getReference (j)))
{
XmlElement* const map = doc->createNewChildElement ("MAPPING");
map->setAttribute ("commandId", String::toHexString ((int) cm->commandID));
map->setAttribute ("description", commandManager->getDescriptionOfCommand (cm->commandID));
map->setAttribute ("key", cm->keypresses.getReference (j).getTextDescription());
map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
}
}
}
@@ -295,17 +289,17 @@ XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefault
{
for (int i = 0; i < defaultSet->mappings.size(); ++i)
{
const CommandMapping* const cm = defaultSet->mappings.getUnchecked(i);
const CommandMapping& cm = *defaultSet->mappings.getUnchecked(i);
for (int j = 0; j < cm->keypresses.size(); ++j)
for (int j = 0; j < cm.keypresses.size(); ++j)
{
if (! containsMapping (cm->commandID, cm->keypresses.getReference (j)))
if (! containsMapping (cm.commandID, cm.keypresses.getReference (j)))
{
XmlElement* const map = doc->createNewChildElement ("UNMAPPING");
map->setAttribute ("commandId", String::toHexString ((int) cm->commandID));
map->setAttribute ("description", commandManager->getDescriptionOfCommand (cm->commandID));
map->setAttribute ("key", cm->keypresses.getReference (j).getTextDescription());
map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
}
}
}
@@ -315,34 +309,33 @@ XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefault
}
//==============================================================================
bool KeyPressMappingSet::keyPressed (const KeyPress& key,
Component* originatingComponent)
bool KeyPressMappingSet::keyPressed (const KeyPress& key, Component* const originatingComponent)
{
bool commandWasDisabled = false;
for (int i = 0; i < mappings.size(); ++i)
{
CommandMapping* const cm = mappings.getUnchecked(i);
CommandMapping& cm = *mappings.getUnchecked(i);
if (cm->keypresses.contains (key))
if (cm.keypresses.contains (key))
{
const ApplicationCommandInfo* const ci = commandManager->getCommandForID (cm->commandID);
if (ci != nullptr
&& (ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (cm.commandID))
{
ApplicationCommandInfo info (0);
if (commandManager->getTargetForCommand (cm->commandID, info) != 0)
if ((ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
{
if ((info.flags & ApplicationCommandInfo::isDisabled) == 0)
{
invokeCommand (cm->commandID, key, true, 0, originatingComponent);
return true;
}
else
ApplicationCommandInfo info (0);
if (commandManager.getTargetForCommand (cm.commandID, info) != nullptr)
{
commandWasDisabled = true;
if ((info.flags & ApplicationCommandInfo::isDisabled) == 0)
{
invokeCommand (cm.commandID, key, true, 0, originatingComponent);
return true;
}
else
{
commandWasDisabled = true;
}
}
}
}
@@ -362,13 +355,13 @@ bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* o
for (int i = mappings.size(); --i >= 0;)
{
CommandMapping* const cm = mappings.getUnchecked(i);
CommandMapping& cm = *mappings.getUnchecked(i);
if (cm->wantsKeyUpDownCallbacks)
if (cm.wantsKeyUpDownCallbacks)
{
for (int j = cm->keypresses.size(); --j >= 0;)
for (int j = cm.keypresses.size(); --j >= 0;)
{
const KeyPress key (cm->keypresses.getReference (j));
const KeyPress key (cm.keypresses.getReference (j));
const bool isDown = key.isCurrentlyDown();
int keyPressEntryIndex = 0;
@@ -407,7 +400,7 @@ bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* o
keysDown.remove (keyPressEntryIndex);
}
invokeCommand (cm->commandID, key, isDown, millisecs, originatingComponent);
invokeCommand (cm.commandID, key, isDown, millisecs, originatingComponent);
used = true;
}
}


+ 8
- 13
modules/juce_gui_basics/commands/juce_KeyPressMappingSet.h View File

@@ -103,7 +103,7 @@ public:
@see ApplicationCommandManager
*/
explicit KeyPressMappingSet (ApplicationCommandManager* commandManager);
explicit KeyPressMappingSet (ApplicationCommandManager& commandManager);
/** Creates an copy of a KeyPressMappingSet. */
KeyPressMappingSet (const KeyPressMappingSet& other);
@@ -112,7 +112,7 @@ public:
~KeyPressMappingSet();
//==============================================================================
ApplicationCommandManager* getCommandManager() const noexcept { return commandManager; }
ApplicationCommandManager& getCommandManager() const noexcept { return commandManager; }
//==============================================================================
/** Returns a list of keypresses that are assigned to a particular command.
@@ -172,7 +172,6 @@ public:
//==============================================================================
/** Looks for a command that corresponds to a keypress.
@returns the UID of the command or 0 if none was found
*/
CommandID findCommandForKeyPress (const KeyPress& keyPress) const noexcept;
@@ -216,15 +215,15 @@ public:
//==============================================================================
/** @internal */
bool keyPressed (const KeyPress& key, Component* originatingComponent);
bool keyPressed (const KeyPress&, Component* originatingComponent);
/** @internal */
bool keyStateChanged (bool isKeyDown, Component* originatingComponent);
/** @internal */
void globalFocusChanged (Component* focusedComponent);
void globalFocusChanged (Component*);
private:
//==============================================================================
ApplicationCommandManager* commandManager;
ApplicationCommandManager& commandManager;
struct CommandMapping
{
@@ -243,13 +242,9 @@ private:
OwnedArray <KeyPressTime> keysDown;
void handleMessage (const Message& message);
void invokeCommand (const CommandID commandID,
const KeyPress& keyPress,
const bool isKeyDown,
const int millisecsSinceKeyPressed,
Component* const originatingComponent) const;
void handleMessage (const Message&);
void invokeCommand (const CommandID, const KeyPress&, const bool isKeyDown,
const int millisecsSinceKeyPressed, Component* originator) const;
KeyPressMappingSet& operator= (const KeyPressMappingSet&);
JUCE_LEAK_DETECTOR (KeyPressMappingSet);


+ 32
- 47
modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp View File

@@ -26,20 +26,18 @@
class KeyMappingEditorComponent::ChangeKeyButton : public Button
{
public:
ChangeKeyButton (KeyMappingEditorComponent& owner_,
const CommandID commandID_,
const String& keyName,
const int keyNum_)
ChangeKeyButton (KeyMappingEditorComponent& kec, const CommandID command,
const String& keyName, const int keyIndex)
: Button (keyName),
owner (owner_),
commandID (commandID_),
keyNum (keyNum_)
owner (kec),
commandID (command),
keyNum (keyIndex)
{
setWantsKeyboardFocus (false);
setTriggeredOnMouseDown (keyNum >= 0);
setTooltip (keyNum_ < 0 ? TRANS("adds a new key-mapping")
: TRANS("click to change this key-mapping"));
setTooltip (keyIndex < 0 ? TRANS("adds a new key-mapping")
: TRANS("click to change this key-mapping"));
}
void paintButton (Graphics& g, bool /*isOver*/, bool /*isDown*/)
@@ -83,25 +81,20 @@ public:
void fitToContent (const int h) noexcept
{
if (keyNum < 0)
{
setSize (h, h);
}
else
{
Font f (h * 0.6f);
setSize (jlimit (h * 4, h * 8, 6 + f.getStringWidth (getName())), h);
}
setSize (jlimit (h * 4, h * 8, 6 + Font (h * 0.6f).getStringWidth (getName())), h);
}
//==============================================================================
class KeyEntryWindow : public AlertWindow
{
public:
KeyEntryWindow (KeyMappingEditorComponent& owner_)
KeyEntryWindow (KeyMappingEditorComponent& kec)
: AlertWindow (TRANS("New key-mapping"),
TRANS("Please press a key combination now..."),
AlertWindow::NoIcon),
owner (owner_)
owner (kec)
{
addButton (TRANS("Ok"), 1);
addButton (TRANS("Cancel"), 0);
@@ -123,7 +116,7 @@ public:
if (previousCommand != 0)
message << "\n\n" << TRANS("(Currently assigned to \"")
<< owner.getMappings().getCommandManager()->getNameOfCommand (previousCommand) << "\")";
<< owner.getCommandManager().getNameOfCommand (previousCommand) << "\")";
setMessage (message);
return true;
@@ -168,7 +161,7 @@ public:
AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
TRANS("Change key-mapping"),
TRANS("This key is already assigned to the command \"")
+ owner.getMappings().getCommandManager()->getNameOfCommand (previousCommand)
+ owner.getCommandManager().getNameOfCommand (previousCommand)
+ TRANS("\"\n\nDo you want to re-assign it to this new command instead?"),
TRANS("Re-assign"),
TRANS("Cancel"),
@@ -209,8 +202,8 @@ private:
class KeyMappingEditorComponent::ItemComponent : public Component
{
public:
ItemComponent (KeyMappingEditorComponent& owner_, const CommandID commandID_)
: owner (owner_), commandID (commandID_)
ItemComponent (KeyMappingEditorComponent& kec, const CommandID command)
: owner (kec), commandID (command)
{
setInterceptsMouseClicks (false, true);
@@ -239,7 +232,7 @@ public:
g.setFont (getHeight() * 0.7f);
g.setColour (findColour (KeyMappingEditorComponent::textColourId));
g.drawFittedText (owner.getMappings().getCommandManager()->getNameOfCommand (commandID),
g.drawFittedText (owner.getCommandManager().getNameOfCommand (commandID),
4, 0, jmax (40, getChildComponent (0)->getX() - 5), getHeight(),
Justification::centredLeft, true);
}
@@ -272,19 +265,14 @@ private:
class KeyMappingEditorComponent::MappingItem : public TreeViewItem
{
public:
MappingItem (KeyMappingEditorComponent& owner_, const CommandID commandID_)
: owner (owner_), commandID (commandID_)
{
}
MappingItem (KeyMappingEditorComponent& kec, const CommandID command)
: owner (kec), commandID (command)
{}
String getUniqueName() const { return String ((int) commandID) + "_id"; }
bool mightContainSubItems() { return false; }
int getItemHeight() const { return 20; }
Component* createItemComponent()
{
return new ItemComponent (owner, commandID);
}
String getUniqueName() const { return String ((int) commandID) + "_id"; }
bool mightContainSubItems() { return false; }
int getItemHeight() const { return 20; }
Component* createItemComponent() { return new ItemComponent (owner, commandID); }
private:
KeyMappingEditorComponent& owner;
@@ -298,10 +286,9 @@ private:
class KeyMappingEditorComponent::CategoryItem : public TreeViewItem
{
public:
CategoryItem (KeyMappingEditorComponent& owner_, const String& name)
: owner (owner_), categoryName (name)
{
}
CategoryItem (KeyMappingEditorComponent& kec, const String& name)
: owner (kec), categoryName (name)
{}
String getUniqueName() const { return categoryName + "_cat"; }
bool mightContainSubItems() { return true; }
@@ -323,13 +310,11 @@ public:
{
if (getNumSubItems() == 0)
{
Array <CommandID> commands (owner.getMappings().getCommandManager()->getCommandsInCategory (categoryName));
const Array <CommandID> commands (owner.getCommandManager().getCommandsInCategory (categoryName));
for (int i = 0; i < commands.size(); ++i)
{
if (owner.shouldCommandBeIncluded (commands[i]))
addSubItem (new MappingItem (owner, commands[i]));
}
}
}
else
@@ -351,8 +336,8 @@ class KeyMappingEditorComponent::TopLevelItem : public TreeViewItem,
private ChangeListener
{
public:
TopLevelItem (KeyMappingEditorComponent& owner_)
: owner (owner_)
TopLevelItem (KeyMappingEditorComponent& kec)
: owner (kec)
{
setLinesDrawnForSubItems (false);
owner.getMappings().addChangeListener (this);
@@ -371,11 +356,11 @@ public:
const OpennessRestorer opennessRestorer (*this);
clearSubItems();
const StringArray categories (owner.getMappings().getCommandManager()->getCommandCategories());
const StringArray categories (owner.getCommandManager().getCommandCategories());
for (int i = 0; i < categories.size(); ++i)
{
const Array <CommandID> commands (owner.getMappings().getCommandManager()->getCommandsInCategory (categories[i]));
const Array <CommandID> commands (owner.getCommandManager().getCommandsInCategory (categories[i]));
int count = 0;
for (int j = 0; j < commands.size(); ++j)
@@ -469,14 +454,14 @@ void KeyMappingEditorComponent::resized()
//==============================================================================
bool KeyMappingEditorComponent::shouldCommandBeIncluded (const CommandID commandID)
{
const ApplicationCommandInfo* const ci = mappings.getCommandManager()->getCommandForID (commandID);
const ApplicationCommandInfo* const ci = mappings.getCommandManager().getCommandForID (commandID);
return ci != nullptr && (ci->flags & ApplicationCommandInfo::hiddenFromKeyEditor) == 0;
}
bool KeyMappingEditorComponent::isCommandReadOnly (const CommandID commandID)
{
const ApplicationCommandInfo* const ci = mappings.getCommandManager()->getCommandForID (commandID);
const ApplicationCommandInfo* const ci = mappings.getCommandManager().getCommandForID (commandID);
return ci != nullptr && (ci->flags & ApplicationCommandInfo::readOnlyInKeyEditor) != 0;
}


+ 3
- 0
modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.h View File

@@ -63,6 +63,9 @@ public:
/** Returns the KeyPressMappingSet that this component is acting upon. */
KeyPressMappingSet& getMappings() const noexcept { return mappings; }
/** Returns the ApplicationCommandManager that this component is connected to. */
ApplicationCommandManager& getCommandManager() const noexcept { return mappings.getCommandManager(); }
//==============================================================================
/** Can be overridden if some commands need to be excluded from the list.


Loading…
Cancel
Save