Some tests on component codin, if someone may find it useful


#1

hi to all…
this is just an adaptation of some of the classes i’ve found somewhere, i needed a color picker that behaves just like a modal window to let you pick up a colour when asked. anyway i’m working on some new widgets of mine, particularly some for direct user interaction (knobs, x-y joysticks, bezier curves). they will be ready next days !

bye

/*
        // just acts like a AlertWindow

	Colour c = ColourChooser::showColorSelector(Colours::black);
*/
class ColourChooser  : public DialogWindow, SimpleListBoxModel
{
protected:

	Colour* colours;
	String* stringColours;   
	int numberColours;
  
	TextButton* closeButton;
	TextButton* okButton;
	TextButton* cancelButton;
	SimpleListBox* list; 
	ResizerComponent *resizer;
	ColourSelector* selector;

	int dx, dy;

public:

    //==============================================================================
    ColourChooser()
        : DialogWindow (T("ColourChooser"), Colour::greyLevel (0.9f), false)
    {
		setOpaque (true);
		setVisible (true);
		setResizeable (true);
		setRepaintsOnMouseActivity(false);
		addToDesktop (windowHasDropShadow);
    
		// Initialisation des variables       
		numberColours = 139;
		colours = new Colour[numberColours]();
		stringColours = new String[numberColours];
    
		// Déclarations en pagaille
		colours[0] = Colours::transparentWhite;
		stringColours[0] = T("Colours::transparentWhite");    
		colours[1] = Colours::transparentBlack;
		stringColours[1] = T("Colours::transparentBlack");    
		colours[2] = Colours::black;
		stringColours[2] = T("Colours::black");    
		colours[3] = Colours::white;
		stringColours[3] = T("Colours::white");    
		colours[4] = Colours::blue;
		stringColours[4] = T("Colours::blue");    
		colours[5] = Colours::grey;
		stringColours[5] = T("Colours::grey");    
		colours[6] = Colours::green;
		stringColours[6] = T("Colours::green");    
		colours[7] = Colours::red;
		stringColours[7] = T("Colours::red");    
		colours[8] = Colours::yellow;
		stringColours[8] = T("Colours::yellow");    
		colours[9] = Colours::aliceblue;
		stringColours[9] = T("Colours::aliceblue");
    
		colours[10] = Colours::antiquewhite;
		stringColours[10] = T("Colours::antiquewhite");    
		colours[11] = Colours::aqua;
		stringColours[11] = T("Colours::aqua");    
		colours[12] = Colours::aquamarine;
		stringColours[12] = T("Colours::aquamarine");    
		colours[13] = Colours::azure;
		stringColours[13] = T("Colours::azure");    
		colours[14] = Colours::beige;
		stringColours[14] = T("Colours::beige");    
		colours[15] = Colours::bisque;
		stringColours[15] = T("Colours::bisque");    
		colours[16] = Colours::blanchedalmond;
		stringColours[16] = T("Colours::blanchedalmond");    
		colours[17] = Colours::blueviolet;
		stringColours[17] = T("Colours::blueviolet");    
		colours[18] = Colours::brown;
		stringColours[18] = T("Colours::brown");    
		colours[19] = Colours::burlywood;
		stringColours[19] = T("Colours::burlywood");    
    
		colours[20] = Colours::cadetblue;
		stringColours[20] = T("Colours::cadetblue");    
		colours[21] = Colours::chartreuse;
		stringColours[21] = T("Colours::chartreuse");    
		colours[22] = Colours::chocolate;
		stringColours[22] = T("Colours::chocolate");    
		colours[23] = Colours::coral;
		stringColours[23] = T("Colours::coral");    
		colours[24] = Colours::cornflowerblue;
		stringColours[24] = T("Colours::cornflowerblue");    
		colours[25] = Colours::cornsilk;
		stringColours[25] = T("Colours::cornsilk");    
		colours[26] = Colours::crimson;
		stringColours[26] = T("Colours::crimson");    
		colours[27] = Colours::cyan;
		stringColours[27] = T("Colours::cyan");    
		colours[28] = Colours::darkblue;
		stringColours[28] = T("Colours::darkblue");    
		colours[29] = Colours::darkcyan;
		stringColours[29] = T("Colours::darkcyan");
    
		colours[30] = Colours::darkgoldenrod;
		stringColours[30] = T("Colours::darkgoldenrod");    
		colours[31] = Colours::darkgrey;
		stringColours[31] = T("Colours::darkgrey");    
		colours[32] = Colours::darkgreen;
		stringColours[32] = T("Colours::darkgreen");    
		colours[33] = Colours::darkkhaki;
		stringColours[33] = T("Colours::darkkhaki");    
		colours[34] = Colours::darkmagenta;
		stringColours[34] = T("Colours::darkmagenta");    
		colours[35] = Colours::darkolivegreen;
		stringColours[35] = T("Colours::darkolivegreen");    
		colours[36] = Colours::darkorange;
		stringColours[36] = T("Colours::darkorange");    
		colours[37] = Colours::darkorchid;
		stringColours[37] = T("Colours::darkorchid");    
		colours[38] = Colours::darkred;
		stringColours[38] = T("Colours::darkred");    
		colours[39] = Colours::darksalmon;
		stringColours[39] = T("Colours::darksalmon");
    
		colours[40] = Colours::darkseagreen;
		stringColours[40] = T("Colours::darkseagreen");    
		colours[41] = Colours::darkslateblue;
		stringColours[41] = T("Colours::darkslateblue");    
		colours[42] = Colours::darkslategrey;
		stringColours[42] = T("Colours::darkslategrey");    
		colours[43] = Colours::darkturquoise;
		stringColours[43] = T("Colours::darkturquoise");    
		colours[44] = Colours::darkviolet;
		stringColours[44] = T("Colours::darkviolet");    
		colours[45] = Colours::deeppink;
		stringColours[45] = T("Colours::deeppink");    
		colours[46] = Colours::deepskyblue;
		stringColours[46] = T("Colours::deepskyblue");    
		colours[47] = Colours::dimgrey;
		stringColours[47] = T("Colours::dimgrey");    
		colours[48] = Colours::dodgerblue;
		stringColours[48] = T("Colours::dodgerblue");    
		colours[49] = Colours::firebrick;
		stringColours[49] = T("Colours::firebrick");
    
		colours[50] = Colours::floralwhite;
		stringColours[50] = T("Colours::floralwhite");    
		colours[51] = Colours::forestgreen;
		stringColours[51] = T("Colours::forestgreen");    
		colours[52] = Colours::fuchsia;
		stringColours[52] = T("Colours::fuchsia");
		colours[53] = Colours::gainsboro;
		stringColours[53] = T("Colours::gainsboro");
		colours[54] = Colours::gold;
		stringColours[54] = T("Colours::gold");
		colours[55] = Colours::goldenrod;
		stringColours[55] = T("Colours::goldenrod");
		colours[56] = Colours::greenyellow;
		stringColours[56] = T("Colours::greenyellow");
		colours[57] = Colours::honeydew;
		stringColours[57] = T("Colours::honeydew");
		colours[58] = Colours::hotpink;
		stringColours[58] = T("Colours::hotpink");
		colours[59] = Colours::indianred;
		stringColours[59] = T("Colours::indianred");
       
		colours[60] = Colours::indigo;
		stringColours[60] = T("Colours::indigo");
		colours[61] = Colours::ivory;
		stringColours[61] = T("Colours::ivory");
		colours[62] = Colours::khaki;
		stringColours[62] = T("Colours::khaki");
		colours[63] = Colours::lavender;
		stringColours[63] = T("Colours::lavender");
		colours[64] = Colours::lavenderblush;
		stringColours[64] = T("Colours::lavenderblush");
		colours[65] = Colours::lemonchiffon;
		stringColours[65] = T("Colours::lemonchiffon");
		colours[66] = Colours::lightblue;
		stringColours[66] = T("Colours::lightblue");
		colours[67] = Colours::lightcoral;
		stringColours[67] = T("Colours::lightcoral");
		colours[68] = Colours::lightcyan;
		stringColours[68] = T("Colours::lightcyan");
		colours[69] = Colours::lightgoldenrodyellow;
		stringColours[69] = T("Colours::lightgoldenrodyellow");
        
		colours[70] = Colours::lightgreen;
		stringColours[70] = T("Colours::lightgreen");
		colours[71] = Colours::lightgrey;
		stringColours[71] = T("Colours::lightgrey");
		colours[72] = Colours::lightpink;
		stringColours[72] = T("Colours::lightpink");
		colours[73] = Colours::lightsalmon;
		stringColours[73] = T("Colours::lightsalmon");
		colours[74] = Colours::lightseagreen;
		stringColours[74] = T("Colours::lightseagreen");
		colours[75] = Colours::lightskyblue;
		stringColours[75] = T("Colours::lightskyblue");
		colours[76] = Colours::lightslategrey;
		stringColours[76] = T("Colours::lightslategrey");
		colours[77] = Colours::lightsteelblue;
		stringColours[77] = T("Colours::lightsteelblue");
		colours[78] = Colours::lightyellow;
		stringColours[78] = T("Colours::lightyellow");
		colours[79] = Colours::lime;
		stringColours[79] = T("Colours::lime");
    
		colours[80] = Colours::limegreen;
		stringColours[80] = T("Colours::limegreen");
		colours[81] = Colours::linen;
		stringColours[81] = T("Colours::linen");
		colours[82] = Colours::magenta;
		stringColours[82] = T("Colours::magenta");
		colours[83] = Colours::maroon;
		stringColours[83] = T("Colours::maroon");
		colours[84] = Colours::mediumaquamarine;
		stringColours[84] = T("Colours::mediumaquamarine");
		colours[85] = Colours::mediumblue;
		stringColours[85] = T("Colours::mediumblue");
		colours[86] = Colours::mediumorchid;
		stringColours[86] = T("Colours::mediumorchid");
		colours[87] = Colours::mediumpurple;
		stringColours[87] = T("Colours::mediumpurple");
		colours[88] = Colours::mediumseagreen;
		stringColours[88] = T("Colours::mediumseagreen");
		colours[89] = Colours::mediumslateblue;
		stringColours[89] = T("Colours::mediumslateblue");   
    
		colours[90] = Colours::mediumspringgreen;
		stringColours[90] = T("Colours::mediumspringgreen");
		colours[91] = Colours::mediumturquoise;
		stringColours[91] = T("Colours::mediumturquoise");
		colours[92] = Colours::mediumvioletred;
		stringColours[92] = T("Colours::mediumvioletred");
		colours[93] = Colours::midnightblue;
		stringColours[93] = T("Colours::midnightblue");
		colours[94] = Colours::mintcream;
		stringColours[94] = T("Colours::mintcream");
		colours[95] = Colours::mistyrose;
		stringColours[95] = T("Colours::mistyrose");
		colours[96] = Colours::navajowhite;
		stringColours[96] = T("Colours::navajowhite");
		colours[97] = Colours::navy;
		stringColours[97] = T("Colours::navy");
		colours[98] = Colours::oldlace;
		stringColours[98] = T("Colours::oldlace");
		colours[99] = Colours::olive;
		stringColours[99] = T("Colours::olive");
    
		colours[100] = Colours::olivedrab;
		stringColours[100] = T("Colours::olivedrab");
		colours[101] = Colours::orange;
		stringColours[101] = T("Colours::orange");
		colours[102] = Colours::orangered;
		stringColours[102] = T("Colours::orangered");
		colours[103] = Colours::orchid;
		stringColours[103] = T("Colours::orchid");
		colours[104] = Colours::palegoldenrod;
		stringColours[104] = T("Colours::palegoldenrod");
		colours[105] = Colours::palegreen;
		stringColours[105] = T("Colours::palegreen");
		colours[106] = Colours::paleturquoise;
		stringColours[106] = T("Colours::paleturquoise");
		colours[107] = Colours::palevioletred;
		stringColours[107] = T("Colours::palevioletred");
		colours[108] = Colours::papayawhip;
		stringColours[108] = T("Colours::papayawhip");
		colours[109] = Colours::peachpuff;
		stringColours[109] = T("Colours::peachpuff");
        
		colours[110] = Colours::peru;
		stringColours[110] = T("Colours::peru");
		colours[111] = Colours::pink;
		stringColours[111] = T("Colours::pink");
		colours[112] = Colours::plum;
		stringColours[112] = T("Colours::plum");
		colours[113] = Colours::powderblue;
		stringColours[113] = T("Colours::powderblue");
		colours[114] = Colours::purple;
		stringColours[114] = T("Colours::purple");
		colours[115] = Colours::rosybrown;
		stringColours[115] = T("Colours::rosybrown");
		colours[116] = Colours::royalblue;
		stringColours[116] = T("Colours::royalblue");
		colours[117] = Colours::saddlebrown;
		stringColours[117] = T("Colours::saddlebrown");
		colours[118] = Colours::salmon;
		stringColours[118] = T("Colours::salmon");
		colours[119] = Colours::sandybrown;
		stringColours[119] = T("Colours::sandybrown");   
    
		colours[120] = Colours::seagreen;
		stringColours[120] = T("Colours::seagreen");
		colours[121] = Colours::seashell;
		stringColours[121] = T("Colours::seashell");
		colours[122] = Colours::sienna;
		stringColours[122] = T("Colours::sienna");
		colours[123] = Colours::silver;
		stringColours[123] = T("Colours::silver");
		colours[124] = Colours::skyblue;
		stringColours[124] = T("Colours::skyblue");
		colours[125] = Colours::slateblue;
		stringColours[125] = T("Colours::slateblue");
		colours[126] = Colours::slategrey;
		stringColours[126] = T("Colours::slategrey");
		colours[127] = Colours::snow;
		stringColours[127] = T("Colours::snow");
		colours[128] = Colours::springgreen;
		stringColours[128] = T("Colours::springgreen");
		colours[129] = Colours::steelblue;
		stringColours[129] = T("Colours::steelblue");
       
		colours[130] = Colours::tan;
		stringColours[130] = T("Colours::tan");
		colours[131] = Colours::teal;
		stringColours[131] = T("Colours::teal");
		colours[132] = Colours::thistle;
		stringColours[132] = T("Colours::thistle");
		colours[133] = Colours::tomato;
		stringColours[133] = T("Colours::tomato");
		colours[134] = Colours::turquoise;
		stringColours[134] = T("Colours::turquoise");
		colours[135] = Colours::violet;
		stringColours[135] = T("Colours::violet");
		colours[136] = Colours::wheat;
		stringColours[136] = T("Colours::wheat");
		colours[137] = Colours::whitesmoke;
		stringColours[137] = T("Colours::whitesmoke");
		colours[138] = Colours::yellowgreen;
		stringColours[138] = T("Colours::yellowgreen");

		addAndMakeVisible(closeButton = new TextButton("X",Colours::yellow,Colours::black,Colours::white,"Close"));
		closeButton->addActionListener(this);

		addAndMakeVisible(list = new SimpleListBox("files",this));
		list->setRowHeight(20);
		list->setMultipleSelectionEnabled(false);
		list->setOutlineColour(Colours::lightgrey,1);

		addAndMakeVisible(selector = new ColourSelector());


		addAndMakeVisible(okButton = new TextButton("Ok",Colours::yellow,Colours::black,Colours::white,"Ok"));
		okButton->addActionListener(this);

		addAndMakeVisible(cancelButton = new TextButton("Cancel",Colours::yellow,Colours::black,Colours::white,"Cancel"));
		cancelButton->addActionListener(this);

		addAndMakeVisible(resizer = new ResizerComponent(this));

		centreWithSize( 400, 300 );
		toFront(true);
	}             
  
	~ColourChooser()
	{
		delete[] stringColours;
		delete[] colours;

		deleteAllChildren();
	}



	static Colour showColorSelector( const Colour& defColour )
	{
		ColourChooser chooser;
		chooser.selector->setCurrentColour( defColour );
		int ret = chooser.runModalLoop();
		if( ret == 0 )	// bad returns
			return defColour;
		else			// good returns
			return chooser.selector->getCurrentColour();
	}



	void resized()
	{
		const int margin = 5;
		const int buttonHeight = 25;
		const int titleHeight = 30;
		
		list->setBounds( margin,titleHeight+margin,180,getHeight() - buttonHeight - 3*margin - titleHeight);
		selector->setBounds(list->getRight()+ margin,
								list->getY(),
								getWidth() - list->getRight()-2*margin,
								list->getHeight());


		closeButton->setBounds((getWidth()-30)-margin, margin, 30, buttonHeight);

		okButton->setBounds(getWidth()/4, list->getBottom() + margin, getWidth() / 4, buttonHeight);
		cancelButton->setBounds(getWidth()/4*2, list->getBottom() + margin, getWidth() / 4, buttonHeight);

		resizer->setBounds (getWidth() - 16, getHeight() - 16, 16, 16);
	}


	void paint (Graphics& g)
	{
		DialogWindow::paint(g);
	}    


	int getNumRows() { return numberColours; }

	void selectedRowsChanged (int row)
	{
		selector->setCurrentColour( colours[row] );
		repaint();
	}

	void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected)
	{
		if (rowNumber >= numberColours)	
			return;

		g.fillAll(colours[rowNumber].withAlpha(.2f));
		if (rowIsSelected)
		{
			g.setColour( Colours::red );
			g.drawRect( g.getClipLeft(), g.getClipTop(), g.getClipWidth(), g.getClipHeight() );
		}		
 
		g.drawText(stringColours[rowNumber],
				0+5, 5, width-5, height-5,
				Justification::centredLeft, true);
	}


	void actionListenerCallback (const String& source)
	{ 
		if (source == closeButton->getName())
		{
			exitModalState(0);
		}
		else if (source == okButton->getName())
		{
			exitModalState(1);
		}
		else if (source == cancelButton->getName())
		{
			exitModalState(0);
		}
	}        

	void mouseDown (const MouseEvent& e)
	{
        dx = e.x;
        dy = e.y;
		DialogWindow::mouseDown(e);
	}

	void mouseDrag (const MouseEvent& e)
	{
	    int x = getMouseX() - dx;
	    int y = getMouseY() - dy;

		// keep the whole thing on-screen
		const Rectangle wholeScreen (Desktop::getInstance()
                                    .getAllMonitorDisplayAreas().getBounds());

		x = jlimit (wholeScreen.getX(), wholeScreen.getRight() - getWidth(), x);
		y = jlimit (wholeScreen.getY(), wholeScreen.getBottom() - getHeight(), y);

		setTopLeftPosition (x, y);
	}

	void mouseDoubleClick (const MouseEvent& e)
	{
		// keep the whole thing on-screen
		const Rectangle wholeScreen (Desktop::getInstance()
                                    .getAllMonitorDisplayAreas().getBounds());
		setBounds(
			wholeScreen.getX(),
			wholeScreen.getY(),
			wholeScreen.getWidth(),
			wholeScreen.getHeight()
		);

		DialogWindow::mouseDoubleClick(e);
	}

};

#2

I’m not dreaming, it’s my code :shock: :lol:

First, I think that should be better to enter the Juce’s colours into an array using this kind of code :

namespace SkinColours { extern const Colour* colours; const int colours_number = 139; };

static const Colour temp1[]={Colours::transparentWhite,Colours::transparentBlack, Colours::black,Colours::white,Colours::blue,Colours::grey,Colours::green, Colours::red,Colours::yellow,Colours::aliceblue,Colours::antiquewhite, Colours::aqua,Colours::aquamarine,Colours::azure,Colours::beige,Colours::bisque, Colours::blanchedalmond,Colours::blueviolet,Colours::brown,Colours::burlywood, Colours::cadetblue,Colours::chartreuse,Colours::chocolate,Colours::coral, Colours::cornflowerblue,Colours::cornsilk,Colours::crimson,Colours::cyan, Colours::darkblue,Colours::darkcyan,Colours::darkgoldenrod,Colours::darkgrey, Colours::darkgreen,Colours::darkkhaki,Colours::darkmagenta,Colours::darkolivegreen, Colours::darkorange,Colours::darkorchid,Colours::darkred,Colours::darksalmon, Colours::darkseagreen,Colours::darkslateblue,Colours::darkslategrey, Colours::darkturquoise,Colours::darkviolet,Colours::deeppink,Colours::deepskyblue, Colours::dimgrey,Colours::dodgerblue,Colours::firebrick,Colours::floralwhite, Colours::forestgreen,Colours::fuchsia,Colours::gainsboro,Colours::gold, Colours::goldenrod,Colours::greenyellow,Colours::honeydew,Colours::hotpink, Colours::indianred,Colours::indigo,Colours::ivory,Colours::khaki,Colours::lavender, Colours::lavenderblush,Colours::lemonchiffon,Colours::lightblue, Colours::lightcoral,Colours::lightcyan,Colours::lightgoldenrodyellow, Colours::lightgreen,Colours::lightgrey,Colours::lightpink,Colours::lightsalmon, Colours::lightseagreen,Colours::lightskyblue,Colours::lightslategrey, Colours::lightsteelblue,Colours::lightyellow,Colours::lime,Colours::limegreen, Colours::linen,Colours::magenta,Colours::maroon,Colours::mediumaquamarine, Colours::mediumblue,Colours::mediumorchid,Colours::mediumpurple, Colours::mediumseagreen,Colours::mediumslateblue,Colours::mediumspringgreen, Colours::mediumturquoise,Colours::mediumvioletred,Colours::midnightblue, Colours::mintcream,Colours::mistyrose,Colours::navajowhite,Colours::navy, Colours::oldlace,Colours::olive,Colours::olivedrab,Colours::orange, Colours::orangered,Colours::orchid,Colours::palegoldenrod,Colours::palegreen, Colours::paleturquoise,Colours::palevioletred,Colours::papayawhip, Colours::peachpuff,Colours::peru,Colours::pink,Colours::plum, Colours::powderblue,Colours::purple,Colours::rosybrown,Colours::royalblue, Colours::saddlebrown,Colours::salmon,Colours::sandybrown,Colours::seagreen, Colours::seashell,Colours::sienna,Colours::silver,Colours::skyblue, Colours::slateblue,Colours::slategrey,Colours::snow,Colours::springgreen, Colours::steelblue,Colours::tan,Colours::teal,Colours::thistle, Colours::tomato,Colours::turquoise,Colours::violet,Colours::wheat, Colours::whitesmoke,Colours::yellowgreen}; const Colour* SkinColours::colours = (const Colour*)temp1;

So it’s static and faster to write :wink:

And the second thing I wanted to tell you is the existance of a class called ColourSelector :lol: You can see an example of use on the Juce executable demo… Here is an example code from the demo :

// show a ColourSelector component inside a DialogWindow DialogWindow* dw = new DialogWindow (T("Colour selector demo"), Colours::lightgrey, true); dw->setContentComponent (new ColourSelector()); dw->centreWithSize (400, 400); dw->setResizeable (true); dw->runModalLoop();


#3

yeah wolfen… is basically your code with some modifications.
basic stuff, indeed, nothing new or interesting. i’m new into juce and i’m experimenting the possibilities… but the behaviour of the ColorChooser is like a AlertWindow, and with 1 line of code your get the same result as 5 lines. when your project gets bigger and bigger is better to set up a class that do the things you have most replicated in the source, and not replicate much lines every times i need that even if the lines are “only” 5.
don’t want to start a war. but if juce is made for simplicity, why not simplify it more ?

  Colour toChoose = ColourChooser::showColorSelector(Colours::lightgrey);

  // if you do not click "ok", lightgrey is preserved in return

VS

  DialogWindow* dw = new DialogWindow (T("Colour selector demo"), Colours::lightgrey, true);
  dw->setContentComponent (new ColourSelector());
  dw->centreWithSize (400, 400);
  dw->setResizeable (true);
  dw->runModalLoop();

  // maybe you have to take care of getting back the colour into a variable, delete the window... 

thanx however for the notice :wink:


#4

Yeah, but in the first case you have written a big class before, and there’s nothing stopping you to put my 5 lines into a static class that you can call with one line of code…

So who is the winner ? :lol:


#5

dunnow. maybe we can call a lawyer…

ehehehe :lol:

lou


#6

A lawyer ? hmm…

Counterstrike ? Streetfighter ? :lol:


#7

better a UFC style combat… in an octagon!!!

lou


#8