From 5fcf94bfe496b7b1527fd69cdc73c708df9a0bee Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Mon, 20 Apr 2015 15:22:27 +0100 Subject: [PATCH 1/3] added EditableTextCustomComponent (based on Label) to WidgetsDemo --- examples/Demo/Source/Demos/WidgetsDemo.cpp | 62 +++++++++++++++++++--- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/examples/Demo/Source/Demos/WidgetsDemo.cpp b/examples/Demo/Source/Demos/WidgetsDemo.cpp index e808316aeb..f5ffee3dca 100644 --- a/examples/Demo/Source/Demos/WidgetsDemo.cpp +++ b/examples/Demo/Source/Demos/WidgetsDemo.cpp @@ -800,10 +800,20 @@ public: } else { - // for any other column, just return 0, as we'll be painting these columns directly. - - jassert (existingComponentToUpdate == 0); - return 0; + EditableTextCustomComponent* textLabel = (EditableTextCustomComponent*) existingComponentToUpdate; + + // If an existing component is being passed-in for updating, we'll re-use it, but + // if not, we'll have to create one. + if (textLabel == 0) + textLabel = new EditableTextCustomComponent (*this); + + textLabel->setRowAndColumn(rowNumber, "Title"); + + // The ID column will be a non-editable text column: + if (columnId == 1) + textLabel->setEditable(false, false, false); + + return textLabel; } } @@ -832,8 +842,7 @@ public: return widest + 8; } - // A couple of quick methods to set and get the "rating" value when the user - // changes the combo box + // A couple of quick methods to set and get cell values when the user changes them int getRating (const int rowNumber) const { return dataList->getChildElement (rowNumber)->getIntAttribute ("Rating"); @@ -843,6 +852,16 @@ public: { dataList->getChildElement (rowNumber)->setAttribute ("Rating", newRating); } + + String getCellText (const String& columnName, const int rowNumber) + { + return dataList->getChildElement (rowNumber)->getStringAttribute (columnName); + } + + void setCellText (const String& columnName, const int rowNumber, const String& newCellText) + { + dataList->getChildElement (rowNumber)->setAttribute (columnName, newCellText); + } //============================================================================== void resized() override @@ -860,6 +879,37 @@ private: XmlElement* columnList; // A pointer to the sub-node of demoData that contains the list of columns XmlElement* dataList; // A pointer to the sub-node of demoData that contains the list of data rows int numRows; // The number of rows of data we've got + + //============================================================================== + // This is a custom component containing an editable textfield, which we're use + // for the table's text columns. + class EditableTextCustomComponent : public Label + { + public: + EditableTextCustomComponent (TableDemoComponent& owner_) + : owner (owner_) + { + setEditable (false, true, false); + } + + void textWasEdited() override + { + owner.setCellText (columnName, rowNumber, getText()); + } + + void setRowAndColumn (const int newRowNumber, const String& newColumnName) + { + rowNumber = newRowNumber; + columnName = newColumnName; + setText(owner.getCellText(columnName, rowNumber), dontSendNotification); + } + + private: + TableDemoComponent& owner; + int rowNumber; + String columnName; + }; + //============================================================================== // This is a custom component containing a combo box, which we're going to put inside From ee3fe813e14fea8dfd99acbb544b701538373f4d Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Mon, 20 Apr 2015 16:34:12 +0100 Subject: [PATCH 2/3] fixed issue with label text being white and invisible; some refactoring --- examples/Demo/Source/Demos/WidgetsDemo.cpp | 49 +++++++++++----------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/examples/Demo/Source/Demos/WidgetsDemo.cpp b/examples/Demo/Source/Demos/WidgetsDemo.cpp index f5ffee3dca..d8b89dacc0 100644 --- a/examples/Demo/Source/Demos/WidgetsDemo.cpp +++ b/examples/Demo/Source/Demos/WidgetsDemo.cpp @@ -785,7 +785,12 @@ public: Component* refreshComponentForCell (int rowNumber, int columnId, bool /*isRowSelected*/, Component* existingComponentToUpdate) override { - if (columnId == 5) // If it's the ratings column, we'll return our custom component.. + if (columnId == 1 || columnId == 7) // The ID and Length columns do not have a custom component + { + jassert (existingComponentToUpdate == 0); + return 0; + } + else if (columnId == 5) // For the ratings column, we return the custom combobox component { RatingColumnCustomComponent* ratingsBox = (RatingColumnCustomComponent*) existingComponentToUpdate; @@ -798,20 +803,15 @@ public: return ratingsBox; } - else + else // The other columns are editable text columns, for which we use the custom Label component { EditableTextCustomComponent* textLabel = (EditableTextCustomComponent*) existingComponentToUpdate; - // If an existing component is being passed-in for updating, we'll re-use it, but - // if not, we'll have to create one. + // same as above... if (textLabel == 0) textLabel = new EditableTextCustomComponent (*this); - textLabel->setRowAndColumn(rowNumber, "Title"); - - // The ID column will be a non-editable text column: - if (columnId == 1) - textLabel->setEditable(false, false, false); + textLabel->setRowAndColumn (rowNumber, columnId); return textLabel; } @@ -822,7 +822,7 @@ public: int getColumnAutoSizeWidth (int columnId) override { if (columnId == 5) - return 100; // (this is the ratings column, containing a custom component) + return 100; // (this is the ratings column, containing a custom combobox component) int widest = 32; @@ -853,14 +853,15 @@ public: dataList->getChildElement (rowNumber)->setAttribute ("Rating", newRating); } - String getCellText (const String& columnName, const int rowNumber) + String getText (const int columnNumber, const int rowNumber) const { - return dataList->getChildElement (rowNumber)->getStringAttribute (columnName); + return dataList->getChildElement (rowNumber)->getStringAttribute ( getAttributeNameForColumnId(columnNumber)); } - void setCellText (const String& columnName, const int rowNumber, const String& newCellText) + void setText (const int columnNumber, const int rowNumber, const String& newText) { - dataList->getChildElement (rowNumber)->setAttribute (columnName, newCellText); + const String& columnName = table.getHeader().getColumnName (columnNumber); + dataList->getChildElement (rowNumber)->setAttribute (columnName, newText); } //============================================================================== @@ -879,10 +880,9 @@ private: XmlElement* columnList; // A pointer to the sub-node of demoData that contains the list of columns XmlElement* dataList; // A pointer to the sub-node of demoData that contains the list of data rows int numRows; // The number of rows of data we've got - + //============================================================================== - // This is a custom component containing an editable textfield, which we're use - // for the table's text columns. + // This is a custom Label component, which we use for the table's editable text columns. class EditableTextCustomComponent : public Label { public: @@ -890,24 +890,25 @@ private: : owner (owner_) { setEditable (false, true, false); + setColour (textColourId, Colours::black); } void textWasEdited() override { - owner.setCellText (columnName, rowNumber, getText()); + owner.setText (columnId, row, getText()); } - void setRowAndColumn (const int newRowNumber, const String& newColumnName) + // Our demo code will call this when we may need to update our contents + void setRowAndColumn (const int newRow, const int newColumn) { - rowNumber = newRowNumber; - columnName = newColumnName; - setText(owner.getCellText(columnName, rowNumber), dontSendNotification); + row = newRow; + columnId = newColumn; + setText (owner.getText(columnId, row), dontSendNotification); } private: TableDemoComponent& owner; - int rowNumber; - String columnName; + int row, columnId; }; From f04e0f04625de1270eb4785ad4532eb9595cbfa9 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Mon, 20 Apr 2015 16:45:21 +0100 Subject: [PATCH 3/3] JuceDemo: make Tables in WidgetDemo look nicer by adding zebra striping --- examples/Demo/Source/Demos/WidgetsDemo.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/Demo/Source/Demos/WidgetsDemo.cpp b/examples/Demo/Source/Demos/WidgetsDemo.cpp index d8b89dacc0..f3f4210b7b 100644 --- a/examples/Demo/Source/Demos/WidgetsDemo.cpp +++ b/examples/Demo/Source/Demos/WidgetsDemo.cpp @@ -741,10 +741,13 @@ public: } // This is overloaded from TableListBoxModel, and should fill in the background of the whole row - void paintRowBackground (Graphics& g, int /*rowNumber*/, int /*width*/, int /*height*/, bool rowIsSelected) override + void paintRowBackground (Graphics& g, int rowNumber, int /*width*/, int /*height*/, bool rowIsSelected) override { if (rowIsSelected) g.fillAll (Colours::lightblue); + + else if (rowNumber % 2) + g.fillAll (Colour (0xffeeeeee)); } // This is overloaded from TableListBoxModel, and must paint any cells that aren't using custom