How to put margin between icon and text of an SWT Button? - eclipse

I would like to have some margin between text and icon (image) on my Button.
That's what I have:
Button btn = new Button(grpInventar, SWT.NONE);
GridData gd_btn = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
gd_btn.widthHint = 154;
btn.setLayoutData(gd_btn);
btn.setAlignment(SWT.LEFT);
btn.setImage(this.getImageByLocation(Application.getIconLocByInventoryClass(Whateverimage.class )));
btn.setText("Text");
I would love to add something like:
btn.setMarginBetweenTextIcon(12)
Is there something out there?

There is no support for this in SWT. You could use an image with empty pixels on the right or text with leading spaces.
Also note that not all platforms support having an image and text in a button.

Related

Unity Editor GUI, change the label width of EditorGUILayout.Toggle

I can't find a way to increase the label width of EditorGUILayout.Toggle. Here's my code, it doesn't do anything and Unity clips the text and cuts it short.
GUILayoutOption[] options = new GUILayoutOption[] {
GUILayout.Width(400.0f),
GUILayout.MinWidth(250.0f),
GUILayout.ExpandWidth(true)
};
MyBoolValue = EditorGUILayout.Toggle("My Long Description Text Here", MyBoolValue, options);
I did try wrapping the Toggle button with
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
But it also didn't do anything. What can I do remove clipping from the text?
Set EditorGUIUtility.labelWidth before doing your Toggle, and then restore it to its original value so you don't mess up any subsequent controls.
float originalValue = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 400;
MyBoolValue = EditorGUILayout.Toggle("My Long Description Text Here", MyBoolValue);
EditorGUIUtility.labelWidth = originalValue;

Hiding SWT Controls

How we can hide the SWT controls?
I know setVisible() method of control class can be used. But the disadvantage of it is, hidden widget will not released and can't be used by other widgets.
Is there any other approach which can be adopted?
You can use layout data. In case of GridLayout, you can use exclude specific widget from being drown on canvas.
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(4, false));
Label hidenLabel = new Label (comp, SWT.NONE);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
hidenLabel.setGridData(gridData );
//hide the button
gridData .exclude = true;
comp.pack();

Use FillLayout with Browser in SWT

I have a view that consists of a heading and a browser. I want the browser to fill my view. This is how it should look:
I have tried out different combinations.
c = new Composite(parent, SWT.NONE);
c.setLayout(new RowLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
Composite wrapper = new Composite(c, SWT.NONE);
wrapper.setBackground(new Color(null, 0, 0, 0));
wrapper.setLayout(new FillLayout(SWT.VERTICAL));
browser = new Browser(wrapper, SWT.WRAP);
browser.setBackground(color);
produces:
Setting a FillLayout to c does not work either. By doing so both, heading and browser, share the view's size and have an equal size.
c = new Composite(parent, SWT.NONE);
c.setLayout(new FillLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
browser = new Browser(c, SWT.WRAP);
browser.setBackground(color);
produces:
I have also tried the following:
composite with filllayout
-> composite with gridlayout
->composite with rowLayout (fixed height, grab right)
->composite with rowLayout
-> composite with fillLayout
-> composite with browser
Assuming that the header and browser have the same parent, you could use a GridLayout like this:
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.FILL, SWT.TOP, true, false ) );
browser.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
Even though a one-cell-grid isn't really what a GridLayout is meant for, it should do the trick. The layout data advise the label to align at the top and use all horizontal space and the browser to use the horizontal and vertical space

How to add an image to a label in SWT

Click here, in this example the text appears on the right side I want it to be under the image.
How to do this using SWT itself?
To show an image with text underneath you'd need two labels and a layout to position them under each other
Composite parent = new ...
RowLayout layout = new RowLayout( SWT.VERTICAL );
layout.center = true;
parent.setLayout( layout );
Label imageLabel = new Label( parent, SWT.NONE );
imageLabel.setImage( ... );
Label textLabel = new Label( parent, SWT.NONE );
textLabel.setText( "text" );
In order to left-align the labels, omit the layout.center = true.
By default, the RowLayout has a spacing of 3 pixels that can be changed through the spacing field.

Toolbar button style

I create toolbar with gtkuimanager in my gtk+ application. How can i change style of toolbar button?
Thank you.
# set a vertical toolbar to the left
self.tools = gtk.Toolbar()
toolbar_item = gtk.ToolButton()
toolbar_item.set_stock_id(gtk.STOCK_APPLY)
toolbar_item.set_label("Show Levels")
toolbar_item.show()
self.tools.insert(toolbar_item, -1)
self.tools.set_orientation(gtk.ORIENTATION_VERTICAL)
containerH.pack_start(self.tools, False, False, 0)