From 56ec1d1400a7f1c72989545d75499174354cb913 Mon Sep 17 00:00:00 2001 From: jules Date: Sun, 27 Oct 2013 12:49:17 +0000 Subject: [PATCH] Added a colour ID TreeView::selectedItemBackgroundColourId, and changed the TreeView to fill selected items with this. (The colour is set to transparent by default so this won't affect existing code). Updated the LookAndFeel::drawTreeviewPlusMinusBox and TreeViewItem::paintOpenCloseButton methods to provide more flexibility. --- .../Application/jucer_AppearanceSettings.cpp | 14 +++++++++-- .../Application/jucer_AppearanceSettings.h | 3 +++ .../Source/Application/jucer_CommonHeaders.h | 1 - .../Utility/jucer_JucerTreeViewBase.cpp | 20 +++------------- .../Source/Utility/jucer_JucerTreeViewBase.h | 3 +-- .../lookandfeel/juce_LookAndFeel.cpp | 18 ++++++++------- .../lookandfeel/juce_LookAndFeel.h | 4 +++- .../juce_gui_basics/widgets/juce_TreeView.cpp | 21 ++++++++--------- .../juce_gui_basics/widgets/juce_TreeView.h | 23 +++++++++++++------ .../misc/juce_KeyMappingEditorComponent.cpp | 21 ++++++++++------- 10 files changed, 71 insertions(+), 57 deletions(-) diff --git a/extras/Introjucer/Source/Application/jucer_AppearanceSettings.cpp b/extras/Introjucer/Source/Application/jucer_AppearanceSettings.cpp index 720eeb9388..1830a135cb 100644 --- a/extras/Introjucer/Source/Application/jucer_AppearanceSettings.cpp +++ b/extras/Introjucer/Source/Application/jucer_AppearanceSettings.cpp @@ -38,7 +38,7 @@ namespace AppearanceColours static const ColourInfo colours[] = { { "Main Window Bkgd", mainBackgroundColourId, true, false }, - { "Treeview Highlight", treeviewHighlightColourId, false, false }, + { "Treeview Highlight", TreeView::selectedItemBackgroundColourId, false, false }, { "Code Background", CodeEditorComponent::backgroundColourId, true, false }, { "Line Number Bkgd", CodeEditorComponent::lineNumberBackgroundId, false, false }, @@ -530,7 +530,7 @@ void AppearanceSettings::showEditorWindow (ScopedPointer& ownerPointe IntrojucerLookAndFeel::IntrojucerLookAndFeel() { setColour (mainBackgroundColourId, Colour::greyLevel (0.8f)); - setColour (treeviewHighlightColourId, Colour (0x401111ee)); + setColour (TreeView::selectedItemBackgroundColourId, Colour (0x401111ee)); setColour (TextButton::buttonColourId, Colour (0xffeeeeff)); setColour (ScrollBar::thumbColourId, @@ -789,3 +789,13 @@ void IntrojucerLookAndFeel::drawTableHeaderBackground (Graphics& g, TableHeaderC for (int i = header.getNumColumns (true); --i >= 0;) g.fillRect (header.getColumnPosition (i).removeFromRight (1)); } + +void IntrojucerLookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, const Rectangle& area, + Colour backgroundColour, bool isOpen, bool isMouseOver) +{ + Path p; + p.addTriangle (0.0f, 0.0f, 1.0f, isOpen ? 0.0f : 0.5f, isOpen ? 0.5f : 0.0f, 1.0f); + + g.setColour (backgroundColour.contrasting().withAlpha (isMouseOver ? 0.5f : 0.3f)); + g.fillPath (p, p.getTransformToScaleToFit (area.reduced (area.getHeight() / 8), true)); +} diff --git a/extras/Introjucer/Source/Application/jucer_AppearanceSettings.h b/extras/Introjucer/Source/Application/jucer_AppearanceSettings.h index e2d39edda7..eb657a68f8 100644 --- a/extras/Introjucer/Source/Application/jucer_AppearanceSettings.h +++ b/extras/Introjucer/Source/Application/jucer_AppearanceSettings.h @@ -109,6 +109,9 @@ public: void drawButtonBackground (Graphics&, Button&, const Colour& backgroundColour, bool isMouseOverButton, bool isButtonDown) override; + void drawTreeviewPlusMinusBox (Graphics&, const Rectangle& area, + Colour backgroundColour, bool isOpen, bool isMouseOver) override; + void drawTableHeaderBackground (Graphics&, TableHeaderComponent&) override; static Colour getScrollbarColourForBackground (Colour background); diff --git a/extras/Introjucer/Source/Application/jucer_CommonHeaders.h b/extras/Introjucer/Source/Application/jucer_CommonHeaders.h index a300b722a9..17e3735453 100644 --- a/extras/Introjucer/Source/Application/jucer_CommonHeaders.h +++ b/extras/Introjucer/Source/Application/jucer_CommonHeaders.h @@ -48,7 +48,6 @@ const char* const sourceOrHeaderFileExtensions = "cpp;mm;m;c;cc;cxx;h;hpp;hxx;h enum ColourIds { mainBackgroundColourId = 0x2340000, - treeviewHighlightColourId = 0x2340002, }; #endif // __JUCER_COMMONHEADERS_JUCEHEADER__ diff --git a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp index 122ebcd7b0..b7cd64d460 100644 --- a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp +++ b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.cpp @@ -84,28 +84,14 @@ Font JucerTreeViewBase::getFont() const return Font (getItemHeight() * 0.6f); } -void JucerTreeViewBase::paintItem (Graphics& g, int /*width*/, int /*height*/) -{ - if (isSelected()) - g.fillAll (getOwnerView()->findColour (treeviewHighlightColourId)); -} - float JucerTreeViewBase::getIconSize() const { return jmin (getItemHeight() - 4.0f, 18.0f); } -void JucerTreeViewBase::paintOpenCloseButton (Graphics& g, int width, int height, bool /*isMouseOver*/) +void JucerTreeViewBase::paintOpenCloseButton (Graphics& g, const Rectangle& area, Colour /*backgroundColour*/, bool isMouseOver) { - Path p; - - if (isOpen()) - p.addTriangle (width * 0.2f, height * 0.25f, width * 0.8f, height * 0.25f, width * 0.5f, height * 0.75f); - else - p.addTriangle (width * 0.25f, height * 0.25f, width * 0.8f, height * 0.5f, width * 0.25f, height * 0.75f); - - g.setColour (getOwnerView()->findColour (mainBackgroundColourId).contrasting (0.3f)); - g.fillPath (p); + TreeViewItem::paintOpenCloseButton (g, area, getOwnerView()->findColour (mainBackgroundColourId), isMouseOver); } Colour JucerTreeViewBase::getBackgroundColour() const @@ -113,7 +99,7 @@ Colour JucerTreeViewBase::getBackgroundColour() const Colour background (getOwnerView()->findColour (mainBackgroundColourId)); if (isSelected()) - background = background.overlaidWith (getOwnerView()->findColour (treeviewHighlightColourId)); + background = background.overlaidWith (getOwnerView()->findColour (TreeView::selectedItemBackgroundColourId)); return background; } diff --git a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.h b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.h index 89a4157479..78268c4ae7 100644 --- a/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.h +++ b/extras/Introjucer/Source/Utility/jucer_JucerTreeViewBase.h @@ -39,8 +39,7 @@ public: int getItemWidth() const override { return -1; } int getItemHeight() const override { return 20; } - void paintItem (Graphics& g, int width, int height) override; - void paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver) override; + void paintOpenCloseButton (Graphics&, const Rectangle& area, Colour backgroundColour, bool isMouseOver) override; Component* createItemComponent() override; void itemClicked (const MouseEvent& e) override; void itemSelectionChanged (bool isNowSelected) override; diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp index 6915a0be8f..4082b10259 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp @@ -107,6 +107,7 @@ LookAndFeel::LookAndFeel() TreeView::linesColourId, 0x4c000000, TreeView::backgroundColourId, 0x00000000, TreeView::dragAndDropIndicatorColourId, 0x80ff0000, + TreeView::selectedItemBackgroundColourId, 0x00000000, PopupMenu::backgroundColourId, 0xffffffff, PopupMenu::textColourId, 0xff000000, @@ -913,14 +914,15 @@ Path LookAndFeel::getCrossShape (const float height) } //============================================================================== -void LookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, int x, int y, int w, int h, bool isPlus, bool /*isMouseOver*/) +void LookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, const Rectangle& area, + Colour /*backgroundColour*/, bool isOpen, bool /*isMouseOver*/) { - const int boxSize = ((jmin (16, w, h) << 1) / 3) | 1; + const int boxSize = roundToInt (jmin (16.0f, area.getWidth(), area.getHeight()) * 0.7f) | 1; - x += (w - boxSize) >> 1; - y += (h - boxSize) >> 1; - w = boxSize; - h = boxSize; + const int x = ((int) area.getWidth() - boxSize) / 2 + (int) area.getX(); + const int y = ((int) area.getHeight() - boxSize) / 2 + (int) area.getY(); + const int w = boxSize; + const int h = boxSize; g.setColour (Colour (0xe5ffffff)); g.fillRect (x, y, w, h); @@ -933,7 +935,7 @@ void LookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, int x, int y, int w, in g.fillRect (x + (w - size) * 0.5f, y + centre, size, 1.0f); - if (isPlus) + if (! isOpen) g.fillRect (x + centre, y + (h - size) * 0.5f, 1.0f, size); } @@ -2472,7 +2474,7 @@ void LookAndFeel::drawPropertyPanelSectionHeader (Graphics& g, const String& nam const int buttonSize = (height * 3) / 4; const int buttonIndent = (height - buttonSize) / 2; - drawTreeviewPlusMinusBox (g, buttonIndent, buttonIndent, buttonSize, buttonSize, ! isOpen, false); + drawTreeviewPlusMinusBox (g, buttonIndent, buttonIndent, buttonSize, buttonSize, isOpen, false); const int textX = buttonIndent * 2 + buttonSize + 2; diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h index 8a67f82801..f415bc63c1 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h @@ -270,7 +270,8 @@ public: //============================================================================== /** Draws the + or - box in a treeview. */ - virtual void drawTreeviewPlusMinusBox (Graphics&, int x, int y, int w, int h, bool isPlus, bool isMouseOver); + virtual void drawTreeviewPlusMinusBox (Graphics&, const Rectangle& area, + Colour backgroundColour, bool isOpen, bool isMouseOver); //============================================================================== virtual void fillTextEditorBackground (Graphics&, int width, int height, TextEditor& textEditor); @@ -657,6 +658,7 @@ private: virtual int drawBubble (Graphics&, float, float, float, float, float, float) { return 0; } virtual int getFontForTextButton (TextButton&) { return 0; } virtual int createFileChooserHeaderText (const String&, const String&, GlyphArrangement&, int) { return 0; } + virtual int drawTreeviewPlusMinusBox (Graphics&, int, int, int, int, bool, bool) { return 0; } #endif class GlassWindowButton; diff --git a/modules/juce_gui_basics/widgets/juce_TreeView.cpp b/modules/juce_gui_basics/widgets/juce_TreeView.cpp index bf4248e4c0..59c5b90f8c 100644 --- a/modules/juce_gui_basics/widgets/juce_TreeView.cpp +++ b/modules/juce_gui_basics/widgets/juce_TreeView.cpp @@ -1317,10 +1317,10 @@ void TreeViewItem::paintItem (Graphics&, int, int) { } -void TreeViewItem::paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver) +void TreeViewItem::paintOpenCloseButton (Graphics& g, const Rectangle& area, Colour backgroundColour, bool isMouseOver) { - ownerView->getLookAndFeel() - .drawTreeviewPlusMinusBox (g, 0, 0, width, height, ! isOpen(), isMouseOver); + getOwnerView()->getLookAndFeel() + .drawTreeviewPlusMinusBox (g, area, backgroundColour, isOpen(), isMouseOver); } void TreeViewItem::paintHorizontalConnectingLine (Graphics& g, const Line& line) @@ -1510,7 +1510,12 @@ void TreeViewItem::paintRecursively (Graphics& g, int width) if (g.reduceClipRegion (drawsInLeftMargin ? -indent : 0, 0, drawsInLeftMargin ? itemW + indent : itemW, itemHeight)) + { + if (isSelected()) + g.fillAll (ownerView->findColour (TreeView::selectedItemBackgroundColourId)); + paintItem (g, itemW, itemHeight); + } } const float halfH = itemHeight * 0.5f; @@ -1545,15 +1550,9 @@ void TreeViewItem::paintRecursively (Graphics& g, int width) } if (mightContainSubItems()) - { - Graphics::ScopedSaveState ss (g); - - g.setOrigin (depth * indentWidth, 0); - g.reduceClipRegion (0, 0, indentWidth, itemHeight); - - paintOpenCloseButton (g, indentWidth, itemHeight, + paintOpenCloseButton (g, Rectangle ((float) (depth * indentWidth), 0, (float) indentWidth, (float) itemHeight), + Colours::white, ownerView->viewport->getContentComp()->isMouseOverButton (this)); - } } if (isOpen()) diff --git a/modules/juce_gui_basics/widgets/juce_TreeView.h b/modules/juce_gui_basics/widgets/juce_TreeView.h index 7c6a466029..43a3c00bb9 100644 --- a/modules/juce_gui_basics/widgets/juce_TreeView.h +++ b/modules/juce_gui_basics/widgets/juce_TreeView.h @@ -313,11 +313,14 @@ public: /** Draws the item's open/close button. - If you don't implement this method, the default behaviour is to - call LookAndFeel::drawTreeviewPlusMinusBox(), but you can override - it for custom effects. + If you don't implement this method, the default behaviour is to call + LookAndFeel::drawTreeviewPlusMinusBox(), but you can override it for custom + effects. You may want to override it and call the base-class implementation + with a different backgroundColour parameter, if your implementation has a + background colour other than the default (white). */ - virtual void paintOpenCloseButton (Graphics&, int width, int height, bool isMouseOver); + virtual void paintOpenCloseButton (Graphics&, const Rectangle& area, + Colour backgroundColour, bool isMouseOver); /** Draws the line that connects this item to the vertical line extending below its parent. */ virtual void paintHorizontalConnectingLine (Graphics&, const Line& line); @@ -793,9 +796,10 @@ public: */ enum ColourIds { - backgroundColourId = 0x1000500, /**< A background colour to fill the component with. */ - linesColourId = 0x1000501, /**< The colour to draw the lines with.*/ - dragAndDropIndicatorColourId = 0x1000502 /**< The colour to use for the drag-and-drop target position indicator. */ + backgroundColourId = 0x1000500, /**< A background colour to fill the component with. */ + linesColourId = 0x1000501, /**< The colour to draw the lines with.*/ + dragAndDropIndicatorColourId = 0x1000502, /**< The colour to use for the drag-and-drop target position indicator. */ + selectedItemBackgroundColourId = 0x1000503 /**< The colour to use to fill the background of any selected items. */ }; //============================================================================== @@ -863,6 +867,11 @@ private: void moveIntoSelectedItem(); void moveByPages (int numPages); + #if JUCE_CATCH_DEPRECATED_CODE_MISUSE + // this method has been deprecated - see the new version.. + virtual int paintOpenCloseButton (Graphics&, int, int, bool) { return 0; } + #endif + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeView) }; diff --git a/modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp b/modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp index 50c715346f..ce2dbf83c6 100644 --- a/modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp +++ b/modules/juce_gui_extra/misc/juce_KeyMappingEditorComponent.cpp @@ -271,10 +271,10 @@ public: : 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 override { return String ((int) commandID) + "_id"; } + bool mightContainSubItems() override { return false; } + int getItemHeight() const override { return 20; } + Component* createItemComponent() override { return new ItemComponent (owner, commandID); } private: KeyMappingEditorComponent& owner; @@ -292,19 +292,24 @@ public: : owner (kec), categoryName (name) {} - String getUniqueName() const { return categoryName + "_cat"; } - bool mightContainSubItems() { return true; } - int getItemHeight() const { return 28; } + String getUniqueName() const override { return categoryName + "_cat"; } + bool mightContainSubItems() override { return true; } + int getItemHeight() const override { return 24; } void paintItem (Graphics& g, int width, int height) override { - g.setFont (Font (height * 0.6f, Font::bold)); + g.setFont (Font (height * 0.7f, Font::bold)); g.setColour (owner.findColour (KeyMappingEditorComponent::textColourId)); g.drawText (TRANS (categoryName), 2, 0, width - 2, height, Justification::centredLeft, true); } + void paintOpenCloseButton (Graphics& g, const Rectangle& area, Colour backgroundColour, bool isMouseOver) override + { + TreeViewItem::paintOpenCloseButton (g, area.reduced (4), backgroundColour, isMouseOver); + } + void itemOpennessChanged (bool isNowOpen) override { if (isNowOpen)