| View previous topic :: View next topic |
| Author |
Message |
donaciano2000 General User

Joined: 22 Oct 2007 Posts: 14
|
Posted: Tue Oct 25, 2011 5:30 pm Post subject: How to create a Color Picker in BASIC at runtime |
|
|
Many posts were used in the making of this. Certainly this one http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=25499&p=116386 helped out.
I needed a color picker and couldn't find a way to load one in BASIC so I sort of redid the built in one manually.
Colors taken from the W3C colors table on wikipedia.org
Please post any improvements.
Known issues : Does not free button listeners and does not close dialog after clicking a button. Probably other memory leaks. Also the dialog box height is not quite right.
Pluses: Adjustable box size, box gap, row count, and lots of nice pretty colors with help text showing their names.
| Code: |
sub ColorPickerDialog
ColorVals = array("&HCD5C5C","&HF08080","&HFA8072","&HE9967A","&HFFA07A","&HFF0000","&HDC143C","&HB22222",_
"&H8B0000","&HFFC0CB","&HFFB6C1","&HFF69B4","&HFF1493","&HC71585","&HDB7093","&HFFA07A",_
"&HFF7F50","&HFF6347","&HFF4500","&HFF8C00","&HFFA500","&HFFD700","&HFFFF00","&HFFFFE0",_
"&HFFFACD","&HFAFAD2","&HFFEFD5","&HFFE4B5","&HFFDAB9","&HEEE8AA","&HF0E68C","&HBDB76B",_
"&HE6E6FA","&HD8BFD8","&HDDA0DD","&HEE82EE","&HDA70D6","&HFF00FF","&HFF00FF","&HBA55D3",_
"&H9370DB","&H8A2BE2","&H9400D3","&H9932CC","&H8B008B","&H800080","&H4B0082","&H483D8B",_
"&H6A5ACD","&H7B68EE","&HADFF2F","&H7FFF00","&H7CFC00","&H00FF00","&H32CD32","&H98FB98",_
"&H90EE90","&H00FA9A","&H00FF7F","&H3CB371","&H2E8B57","&H228B22","&H008000","&H006400",_
"&H9ACD32","&H6B8E23","&H808000","&H556B2F","&H66CDAA","&H8FBC8F","&H20B2AA","&H008B8B",_
"&H008080","&H00FFFF","&H00FFFF","&HE0FFFF","&HAFEEEE","&H7FFFD4","&H40E0D0","&H48D1CC",_
"&H00CED1","&H5F9EA0","&H4682B4","&HB0C4DE","&HB0E0E6","&HADD8E6","&H87CEEB","&H87CEFA",_
"&H00BFFF","&H1E90FF","&H6495ED","&H4169E1","&H0000FF","&H0000CD","&H00008B","&H000080",_
"&H191970","&HFFF8DC","&HFFEBCD","&HFFE4C4","&HFFDEAD","&HF5DEB3","&HDEB887","&HD2B48C",_
"&HBC8F8F","&HF4A460","&HDAA520","&HB8860B","&HCD853F","&HD2691E","&H8B4513","&HA0522D",_
"&HA52A2A","&H800000","&HFFFFFF","&HFFFAFA","&HF0FFF0","&HF5FFFA","&HF0FFFF","&HF0F8FF",_
"&HF8F8FF","&HF5F5F5","&HFFF5EE","&HF5F5DC","&HFDF5E6","&HFFFAF0","&HFFFFF0","&HFAEBD7",_
"&HFAF0E6","&HFFF0F5","&HFFE4E1","&HDCDCDC","&HD3D3D3","&HC0C0C0","&HA9A9A9","&H808080",_
"&H696969","&H778899","&H708090","&H2F4F4F","&H000000")
ColorTexts= array("IndianRed","LightCoral","Salmon","DarkSalmon","LightSalmon","Red","Crimson","FireBrick",_
"DarkRed","Pink","LightPink","HotPink","DeepPink","MediumVioletRed","PaleVioletRed",_
"LightSalmon","Coral","Tomato","OrangeRed","DarkOrange","Orange","Gold","Yellow",_
"LightYellow","LemonChiffon","LightGoldenrodYellow","PapayaWhip","Moccasin","PeachPuff",_
"PaleGoldenrod","Khaki","DarkKhaki","Lavender","Thistle","Plum","Violet","Orchid",_
"Fuchsia","Magenta","MediumOrchid","MediumPurple","BlueViolet","DarkViolet","DarkOrchid",_
"DarkMagenta","Purple","Indigo","DarkSlateBlue","SlateBlue","MediumSlateBlue",_
"GreenYellow","Chartreuse","LawnGreen","Lime","LimeGreen","PaleGreen","LightGreen",_
"MediumSpringGreen","SpringGreen","MediumSeaGreen","SeaGreen","ForestGreen","Green",_
"DarkGreen","YellowGreen","OliveDrab","Olive","DarkOliveGreen","MediumAquamarine",_
"DarkSeaGreen","LightSeaGreen","DarkCyan","Teal","Aqua","Cyan","LightCyan",_
"PaleTurquoise","Aquamarine","Turquoise","MediumTurquoise","DarkTurquoise","CadetBlue",_
"SteelBlue","LightSteelBlue","PowderBlue","LightBlue","SkyBlue","LightSkyBlue",_
"DeepSkyBlue","DodgerBlue","CornflowerBlue","RoyalBlue","Blue","MediumBlue","DarkBlue",_
"Navy","MidnightBlue","Cornsilk","BlanchedAlmond","Bisque","NavajoWhite","Wheat",_
"BurlyWood","Tan","RosyBrown","SandyBrown","Goldenrod","DarkGoldenrod","Peru",_
"Chocolate","SaddleBrown","Sienna","Brown","Maroon","White","Snow","Honeydew",_
"MintCream","Azure","AliceBlue","GhostWhite","WhiteSmoke","Seashell","Beige","OldLace",_
"FloralWhite","Ivory","AntiqueWhite","Linen","LavenderBlush","MistyRose","Gainsboro",_
"LightGrey","Silver","DarkGray","Gray","DimGray","LightSlateGray","SlateGray",_
"DarkSlateGray","Black")
oDoc = thisComponent
oSheet = oDoc.Sheets.getByIndex(0)
BoxSize = 14
BoxNum = UBound(ColorVals)+1
Rows = 8
BoxGap = 3
HeadGap = BoxSize / 2
oDialogModel = createUnoService( "com.sun.star.awt.UnoControlDialogModel" )
oDialogModel.Title = "Color Picker"
oDialogModel.BackgroundColor = &HCCCCCC
oDialogControl = createUnoService( "com.sun.star.awt.UnoControlDialog" )
oDialogControl.setModel( oDialogModel )
oDialogControl.setPosSize( 0, 0, (BoxSize+BoxGap) * (Rows+1),BoxSize/2+ HeadGap+((BoxNum\Rows+1))*(BoxSize+BoxGap) , com.sun.star.awt.PosSize.POSSIZE)
FOR I = 0 to (BoxNum-1)
oModel = oDialogModel.createInstance("com.sun.star.form.component.CommandButton")
oModel.BackgroundColor = ColorVals(I)
oModel.PushButtonType = com.sun.star.awt.PushButtonType.STANDARD 'OK
oModel.DefaultButton = false
oModel.HelpText = ColorTexts(I)
oDialogModel.insertByName("ColorOK"+I, oModel)
oModel.Name="ColorOK"+I
oControl = oDialogControl.getControl("ColorOK"+I)
oModel.Label = ""
oControl.setPosSize(((I MOD Rows) * (BoxSize+BoxGap))+(BoxSize+BoxGap)/2,(BoxSize+BoxGap)*(I\Rows)+HeadGap,_
BoxSize,BoxSize, com.sun.star.awt.PosSize.POSSIZE)
oControl.ActionCommand = ColorVals(I)
oListener = CreateUnoListener("Button_", "com.sun.star.awt.XActionListener")
oControl.addActionListener(oListener)
NEXT
oDialogControl.setVisible( True )
oDialogControl.execute()
oDialogControl.endExecute
end sub
Sub Button_actionPerformed( oEv As com.sun.star.awt.ActionEvent )
msgbox oEv.ActionCommand
End Sub
Sub Button_disposing( oEv As com.sun.star.lang.EventObject)
End Sub
|
|
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Wed Oct 26, 2011 6:41 am Post subject: |
|
|
Hi,
You do not need to prepare your action listener for each buttons. Make a listener instance and set it to all buttons. ActionCommand and event object allows to detect which button is pressed.
Hmm, to create form button on the dialog is two times or more slower than to make an awt button.
You do not need to call endExecute method after execute method finished, it only exit execute loop. Call dispose method to delete your dialog. |
|
| Back to top |
|
 |
donaciano2000 General User

Joined: 22 Oct 2007 Posts: 14
|
Posted: Tue Nov 15, 2011 10:06 am Post subject: Re: How to create a Color Picker in BASIC at runtime |
|
|
Thanks for the advice Hanya!
Making the changes you recommended gave about a 4x speedup.
Changes:
ONE listener used instead of one for each button
awt button used instead of form button. This also allowed removing some properties. Previously I tried art buttons but they didn't look good. I found that this could be fixed by setting NativeWidgetLook to false.
dispose added after execute to free memory
One thing left... how to dismiss the control from the button action?
Updated code:
| Code: | sub ColorPickerDialog
ColorVals = array("&HCD5C5C","&HF08080","&HFA8072","&HE9967A","&HFFA07A","&HFF0000","&HDC143C","&HB22222",_
"&H8B0000","&HFFC0CB","&HFFB6C1","&HFF69B4","&HFF1493","&HC71585","&HDB7093","&HFFA07A",_
"&HFF7F50","&HFF6347","&HFF4500","&HFF8C00","&HFFA500","&HFFD700","&HFFFF00","&HFFFFE0",_
"&HFFFACD","&HFAFAD2","&HFFEFD5","&HFFE4B5","&HFFDAB9","&HEEE8AA","&HF0E68C","&HBDB76B",_
"&HE6E6FA","&HD8BFD8","&HDDA0DD","&HEE82EE","&HDA70D6","&HFF00FF","&HFF00FF","&HBA55D3",_
"&H9370DB","&H8A2BE2","&H9400D3","&H9932CC","&H8B008B","&H800080","&H4B0082","&H483D8B",_
"&H6A5ACD","&H7B68EE","&HADFF2F","&H7FFF00","&H7CFC00","&H00FF00","&H32CD32","&H98FB98",_
"&H90EE90","&H00FA9A","&H00FF7F","&H3CB371","&H2E8B57","&H228B22","&H008000","&H006400",_
"&H9ACD32","&H6B8E23","&H808000","&H556B2F","&H66CDAA","&H8FBC8F","&H20B2AA","&H008B8B",_
"&H008080","&H00FFFF","&H00FFFF","&HE0FFFF","&HAFEEEE","&H7FFFD4","&H40E0D0","&H48D1CC",_
"&H00CED1","&H5F9EA0","&H4682B4","&HB0C4DE","&HB0E0E6","&HADD8E6","&H87CEEB","&H87CEFA",_
"&H00BFFF","&H1E90FF","&H6495ED","&H4169E1","&H0000FF","&H0000CD","&H00008B","&H000080",_
"&H191970","&HFFF8DC","&HFFEBCD","&HFFE4C4","&HFFDEAD","&HF5DEB3","&HDEB887","&HD2B48C",_
"&HBC8F8F","&HF4A460","&HDAA520","&HB8860B","&HCD853F","&HD2691E","&H8B4513","&HA0522D",_
"&HA52A2A","&H800000","&HFFFFFF","&HFFFAFA","&HF0FFF0","&HF5FFFA","&HF0FFFF","&HF0F8FF",_
"&HF8F8FF","&HF5F5F5","&HFFF5EE","&HF5F5DC","&HFDF5E6","&HFFFAF0","&HFFFFF0","&HFAEBD7",_
"&HFAF0E6","&HFFF0F5","&HFFE4E1","&HDCDCDC","&HD3D3D3","&HC0C0C0","&HA9A9A9","&H808080",_
"&H696969","&H778899","&H708090","&H2F4F4F","&H000000")
ColorTexts= array("IndianRed","LightCoral","Salmon","DarkSalmon","LightSalmon","Red","Crimson","FireBrick",_
"DarkRed","Pink","LightPink","HotPink","DeepPink","MediumVioletRed","PaleVioletRed",_
"LightSalmon","Coral","Tomato","OrangeRed","DarkOrange","Orange","Gold","Yellow",_
"LightYellow","LemonChiffon","LightGoldenrodYellow","PapayaWhip","Moccasin","PeachPuff",_
"PaleGoldenrod","Khaki","DarkKhaki","Lavender","Thistle","Plum","Violet","Orchid",_
"Fuchsia","Magenta","MediumOrchid","MediumPurple","BlueViolet","DarkViolet","DarkOrchid",_
"DarkMagenta","Purple","Indigo","DarkSlateBlue","SlateBlue","MediumSlateBlue",_
"GreenYellow","Chartreuse","LawnGreen","Lime","LimeGreen","PaleGreen","LightGreen",_
"MediumSpringGreen","SpringGreen","MediumSeaGreen","SeaGreen","ForestGreen","Green",_
"DarkGreen","YellowGreen","OliveDrab","Olive","DarkOliveGreen","MediumAquamarine",_
"DarkSeaGreen","LightSeaGreen","DarkCyan","Teal","Aqua","Cyan","LightCyan",_
"PaleTurquoise","Aquamarine","Turquoise","MediumTurquoise","DarkTurquoise","CadetBlue",_
"SteelBlue","LightSteelBlue","PowderBlue","LightBlue","SkyBlue","LightSkyBlue",_
"DeepSkyBlue","DodgerBlue","CornflowerBlue","RoyalBlue","Blue","MediumBlue","DarkBlue",_
"Navy","MidnightBlue","Cornsilk","BlanchedAlmond","Bisque","NavajoWhite","Wheat",_
"BurlyWood","Tan","RosyBrown","SandyBrown","Goldenrod","DarkGoldenrod","Peru",_
"Chocolate","SaddleBrown","Sienna","Brown","Maroon","White","Snow","Honeydew",_
"MintCream","Azure","AliceBlue","GhostWhite","WhiteSmoke","Seashell","Beige","OldLace",_
"FloralWhite","Ivory","AntiqueWhite","Linen","LavenderBlush","MistyRose","Gainsboro",_
"LightGrey","Silver","DarkGray","Gray","DimGray","LightSlateGray","SlateGray",_
"DarkSlateGray","Black")
oDoc = thisComponent
oSheet = oDoc.Sheets.getByIndex(0)
BoxSize = 14
BoxNum = UBound(ColorVals)+1
Rows = 8
BoxGap = 3
HeadGap = BoxSize / 2
oDialogModel = createUnoService( "com.sun.star.awt.UnoControlDialogModel" )
oDialogModel.Title = "Color Picker"
oDialogModel.BackgroundColor = &HCCCCCC
oDialogControl = createUnoService( "com.sun.star.awt.UnoControlDialog" )
oDialogControl.setModel( oDialogModel )
oDialogControl.setPosSize( 0, 0, (BoxSize+BoxGap) * (Rows+1),BoxSize/2+ HeadGap+((BoxNum\Rows+1))*(BoxSize+BoxGap) , com.sun.star.awt.PosSize.POSSIZE)
oListener = CreateUnoListener("Button_", "com.sun.star.awt.XActionListener")
FOR I = 0 to (BoxNum-1)
oModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel")
oModel.BackgroundColor = ColorVals(I)
oModel.HelpText = ColorTexts(I)
oModel.Name="ColorOK"+I
oDialogModel.insertByName("ColorOK"+I, oModel)
oControl = oDialogControl.getControl("ColorOK"+I)
oControl.setPosSize(((I MOD Rows) * (BoxSize+BoxGap))+(BoxSize+BoxGap)/2,(BoxSize+BoxGap)*(I\Rows)+HeadGap,_
BoxSize,BoxSize, com.sun.star.awt.PosSize.POSSIZE)
oControl.ActionCommand = ColorVals(I)
oControl.addActionListener(oListener)
NEXT
'Xray oDialogControl
oDialogControl.setVisible( True )
oDialogControl.getPeer().setProperty("NativeWidgetLook",FALSE)
oDialogControl.execute()
oDialogControl.dispose()
end sub
|
|
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Wed Nov 16, 2011 3:19 am Post subject: |
|
|
| Quote: | | One thing left... how to dismiss the control from the button action? | Remove your control from the control container with removeControl method and remove it's model from name container of the dialog model, after that dispose your control. If you see afterimage of the removed control after the removing of the control, simply make a request to update your dialog within invalidate method of css.awt.XWindowPeer interface. |
|
| Back to top |
|
 |
donaciano2000 General User

Joined: 22 Oct 2007 Posts: 14
|
Posted: Thu Nov 17, 2011 3:52 am Post subject: Re: How to create a Color Picker in BASIC at runtime |
|
|
Thanks for the tip. I tried doing all that but just made crashes every time and could never get it to work soooo...
I just added the following at the top:
| Code: |
Private RootControl as object
|
Put this in the ColorPickerDialog sub just before I make all the buttons:
| Code: |
RootControl = oDialogControl
|
Then in buttonActionPerformed I added:
| Code: |
RootControl.endExecute()
|
Which dismisses it. Hopefully it doesn't leak.  |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Thu Nov 17, 2011 4:57 am Post subject: |
|
|
| If you call dispose method of your dialog, all its child controls are also disposed. Or do you want to dispose something other than the dialog control? |
|
| Back to top |
|
 |
donaciano2000 General User

Joined: 22 Oct 2007 Posts: 14
|
Posted: Thu Nov 17, 2011 5:28 am Post subject: |
|
|
Now it's working perfectly. Click a color, get it's value and close the window. It also looks a lot like the built in one.
The only thing is that I don't like using a global variable, was hoping to be able to close the dialog from the button action sub by moving up through the objects.
If there was something like :
| Code: | Sub Button_actionPerformed( oEv As com.sun.star.awt.ActionEvent )
colorDialog = oEv.source.model.parent.parent
colorDialog.endExecute
End Sub |
Then that would be the cleanest and easiest way to dismiss the dialog window. But I could never find a way to get past the button model and find the dialog it's inside of.
oEv.source.model works and gets me the button model but I can't get any higher from there. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|