I am using gtk_overlay_new () to create an overlay in gtk, is there any way i can add some background color to it? so far i have not been successful in finding anything in the documentation.
Use: void gtk_widget_override_background_color();
https://developer.gnome.org/gtk3/unstable/GtkWidget.html#gtk-widget-override-background-color
In GTK4 gtk_widget_override_background_color is no longer available. The same can be achieved with CSS using a style context.
GtkStyleContext *context = gtk_widget_get_style_context(someGtkWidget);
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_data (provider, ".someClass { background: rgba(255,0,0,1)}", -1); //red
gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_style_context_add_class (context, "someClass");
Related
I have managed to figure out the basics of the Theme editor and set the basic values for the Components I am interested in.
I am using ComponentSelector Components across the application, so I am able to choose any Component easily.
Now, I want to change the background color of specific Buttons in run time. What would be the best way to do so?
First of all, for a new app, you should use the theme.css file instead of the Theme editor.
Said that, changing the background color of specific Buttons in run time using the ComponentSelector can be done in the following way.
Assume to have a theme.css containing:
#Constants {
includeNativeBool: true;
}
Button {
margin: 0.5mm;
padding: 0.5mm;
color: white;
border: none;
}
Button-Red {
cn1-derive: Button;
background-color: red;
}
Button-Green {
cn1-derive: Button;
background-color: green;
}
Case of only one Button (note that I use the .asComponent() method to select only one Component, even if it's not necessary in this case):
Form hi = new Form("Hi World", BoxLayout.y());
Button myBtn = new Button("My fun button", "Button-Red");
hi.add(FlowLayout.encloseCenter(myBtn));
hi.show();
UITimer.timer(2000, false, hi, () -> {
// this code changes the color of the button after two second
ComponentSelector.$("Button-Red").asComponent().setUIID("Button-Green");
hi.revalidate();
});
Case of multiple Buttons:
Form hi = new Form("Hi World", BoxLayout.y());
Button myBtn1 = new Button("My fun button 1", "Button-Red");
Button myBtn2 = new Button("My fun button 2", "Button-Red");
hi.add(FlowLayout.encloseCenter(myBtn1));
hi.add(FlowLayout.encloseCenter(myBtn2));
hi.show();
UITimer.timer(2000, false, hi, () -> {
// this code changes the color of the buttons after two second
ComponentSelector.$("Button-Red").setUIID("Button-Green");
hi.revalidate();
});
Of course you can use different ways to select the components you are interested to, according to the documentation: https://www.codenameone.com/javadoc/com/codename1/ui/ComponentSelector.html
I am using react and three.js to display some 3D models in the browser. Everything works fine as long as the three.js component is the only element with visible content on the page. The three.js canvas will resize bigger and smaller just fine. You can wrap it in other containers and it still works fine. The issue starts when another element or component is added as a sibling.
I want to have a viewport with a fixed width sidebar. I'm doing this with a flexbox row container, wrapped around a sidebar component (a simple div with min-width set), and the responsive three.js component.
Resizing the window bigger works fine, the canvas fills the browser window appropriately. However, when resizing the window smaller, the canvas does not properly re-calculate it's size in relation to the available space left over by the static width sidebar, resulting in a canvas that does not fully fit in the browser and introducing scrollbars.
The canvas does shrink some, but not enough to keep it fully in the browser window. The same issue occurs vertically if a component sits above or below the three.js component. If you recompile the app while the browser window is smaller, the refreshed view will have the canvas properly resized.
Following the advice of many of the answers here on StackOverflow, the three.js react code looks something like this:
export class Viewport extends React.Component {
componentDidMount() {
const width = this.mount.clientWidth;
const height = this.mount.clientHeight;
window.addEventListener("resize", this.handleWindowResize);
// setup scene
this.scene = new THREE.Scene();
//setup camera
this.camera = new THREE.PerspectiveCamera( 75, width / height, 0.1, 1000 );
this.camera.position.set( 0, 5, 10 );
// setup rendering
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setClearColor('#666666');
this.renderer.setSize(width, height, false);
this.mount.appendChild(this.renderer.domElement);
// setup geo
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: '#433F81' });
this.cube = new THREE.Mesh(geometry, material);
this.scene.add(this.cube);
...
}
componentWillUnmount() {
window.removeEventListener("resize", this.handleWindowResize);
this.mount.removeChild(this.renderer.domElement);
}
handleWindowResize = () => {
const width = this.mount.clientWidth;
const height = this.mount.clientHeight;
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(width, height, false);
};
render() {
return (
<div className="viewport" style={{display: 'flex', width: "100%", height: "100%"}} ref={(mount) => { this.mount = mount }} />
);
}
}
The styling is simple css flexbox, with the elements set to full width and height (100%), with the sidebar having a set min-width.
.flex-row-container {
display: flex;
flex-flow: row;
height: 100%;
width: 100%;
}
.sidebar {
display: block;
min-width: 250px;
background-color: var(--color-mid-gray);
margin: 3px 4px 3px 4px;
box-sizing: border-box;
border: 1px solid var(--color-mid-gray);
outline: 3px solid var(--color-mid-gray);
}
What am I missing?
So after commenting, I think I got it working how you want.
Basically,
I removed setting display:flex on the viewport, as this is a child of an element (the flex-row-container div) with display:flex, it does not need it, but does need flex:1 1 auto to set how it will display within the flexbox (which I did in the css rather than the JSX).
The problem then became that the viewport div would expand, but not contract (because it contains the canvas), so adding overflow:hidden fixed that.
After making those changes, this.mount.clientWidth would get the desired result that could then be used to set the three canvas size.
See This Fiddle.
I added rendering and animation to the example so the result could be easily seen.
Hope this helps.
You might be getting incorrect values by using element.clientWidth. Have you tried using element.getBoundingClientRect() instead?
const rect = this.mount.getBoundingClientRect();
const width = rect.width;
const height = rect.height;
I'm new in the gtk's world and I cannot find a solution to this little problem.
I have this button :
But I would like that the small shadow behind the button can be removed.
I'd like this result :
How can I solve it?
Button's code:
sum_button = gtk_button_new_from_stock(GTK_STOCK_ADD);
/**/
style = gtk_widget_get_style(sum_button);
style->bg[GTK_STATE_PRELIGHT] = style->bg[GTK_STATE_NORMAL];
gtk_widget_set_style(sum_button, style);
/**/
gtk_widget_modify_bg(sum_button, GTK_STATE_NORMAL, &color2);
gtk_button_set_relief(GTK_BUTTON(sum_button), GTK_RELIEF_HALF);
g_signal_connect(G_OBJECT(sum_button), "clicked", G_CALLBACK(PrintNumber),&t_data);
gtk_box_pack_start(GTK_BOX (hbox3), sum_button, TRUE, TRUE, 0);
Try
gtk_button_set_relief(GTK_BUTTON(sum_button), GTK_RELIEF_NONE);
But then you can not set the background color:
When the button's border relief is set to none, it acts like a label as in it is "transparent", it has the same bg color as its parent container.
Take a look to this thread: https://stackoverflow.com/a/1709648/1606345
If you are writing new code, consider using GTK 3, where you can do this with CSS directly:
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(provider,
"button {"
" border: 0px;"
" background-color: green;"
" color: white;"
"}", -1, &error);
GtkStyleContext *context = gtk_widget_get_style_context(button);
gtk_style_context_add_provider(context, provider,
GTK_STYLE_PROVIDER_PRIORITY_USER);
I am trying to change the background color and text color for combbox and is using following style
.root
{
//-back:#152635;
-darker:#272B30;
-dark:#3A3F44;
-normal:#52575C; // #555
-light:#7A8288; // #999
-lighter:#999; // #eee
-primary:-light;
-success:#62c462;
-info:#5bc0de;
-warning:#f89406;
-danger:#ee5f5b;
-fore-color:#C8C8C8;
-fx-background-color:-darker ;
-focused-color:#64c8c8;
-border-color:derive(-darker,50%);
-fx-text-fill:-fore-color;
-fx-font-size: 14px;
-fx-font-weight:100;
-fx-font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.combo-box-base {
-fx-background-color:-dark;
-fx-text-fill:-fore-color;
-fx-min-width:150px; }
.combo-box-base *.arrow-button {
-fx-background-color:derive(-dark,-10%); }
.combo-box-base *.arrow {
-fx-background-color:-normal; }
.combo-box *.list-view {
-fx-background-color:-normal;
-fx-text-fill:-fore-color; }
Design looks good in Scene builder but in my application, font and background color of drop down list is not changed which is little bit surprised to me. Please help me to find out what is wrong in my css.
While #James_D solution works, if you override the background color you won't see hover, filled or selected pseudo classes.
This will give you the chance to define your styling for them too:
.combo-box-popup > .list-view {
-fx-background-color: -normal; // or define other color to highlight the border
}
.combo-box-popup *.list-cell {
-fx-background: -normal;
-fx-selection-bar: -light; // filled:hover
-fx-cell-focus-inner-border: -dark; // filled:selected:hover
-fx-text-fill: -fore-color;
}
You need to apply the style to the list cells, not the list view:
.combo-box *.list-view .list-cell {
-fx-background-color:-normal;
-fx-text-fill:-fore-color; }
I have the following code:
void Test()
{
currentImage.Source = GetBitmap();
RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96.0, 96.0, PixelFormats.Default);
rtb.Render(currentImage);
}
This code is supposed to render currentImage, which is an Image control in my xaml to a RenderTargetBitmap.
It doesn't work, rtb returns a blank image, the problem is currentImage didn't render itself yet and so this behavior is expected, I think...
To workaround this problem, I wrote this code:
void Test()
{
currentImage.Source = GetBitmap();
this.Dispatcher.BeginInvoke((Action)delegate()
{
RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96.0, 96.0, PixelFormats.Default);
rtb.Render(currentImage);
}, System.Windows.Threading.DispatcherPriority.Render, null);
}
Basically, I wait for currentImage to be rendered and then I can get it properly rendered to my RenderTargetBitmap.
Is there any way to make it work without using this workaround? Force the Image control to render in memory maybe?
thanks!
use a ViewBox to render in memory
Grid grid = new System.Windows.Controls.Grid() { Background = Brushes.Blue, Width = 200, Height = 200 };
Viewbox viewbox = new Viewbox();
viewbox.Child = grid; //control to render
viewbox.Measure(new System.Windows.Size(200, 200));
viewbox.Arrange(new Rect(0, 0, 200, 200));
viewbox.UpdateLayout();
RenderTargetBitmap render = new RenderTargetBitmap(200, 200, 150, 150, PixelFormats.Pbgra32);
render.Render(viewbox);
I think this is a BETTER answer . The viewbox didn't work completely as expected, and it turned out to be an unnecessary overhead.
Here is a copy of that answer (instead of just link)
You need to force a render of the item, or wait for the item to be rendered. You can then use the ActualHeight and ActualWidth properties.
To force a render:
MenuItem item = new MenuItem();
item.Header = "bling";
item.Icon = someIcon;
//Force render
item.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
item.Arrange(new Rect(item.DesiredSize));
In this example the MenuItem has not been given an explicit height or width. However, forcing the render will render it taking the supplied header text and icon into consideration.