Customizing GTK with CSS: Altering button background color on click event

Currently I am working on creating an application using GTK and styling it with CSS. One of the requirements is to change the background of a button when clicked, specifically for a built-in video player. The desired behavior is to have the 'play' button's background change when the video is playing, revert back to default when paused, and for both buttons to reset to default when stopped. What would be the best approach to implement this functionality?

I came across the function

gtk_widget_override_background_color
, but it has been deprecated since version 3.16 and is not recommended for new code. Additionally, my attempt at using button:focus in CSS did not produce the desired results. The background color changes on click, but interactions like adjusting volume also trigger the change, which is not intended.

UPDATE. Upon reflection, I may have phrased my question in a misleading manner. While button:focus does work as expected, it does not fully meet my needs. Allow me to clarify with an example: 1. Initially (after selecting a video), the screen displays a message "press play" https://i.sstatic.net/XYBjO.jpg

  1. When 'play' is clicked, the 'play' button receives focus in the CSS Code snippet from C:
    gtk_widget_set_name(GTK_WIDGET(play_button), "play_button_1");

And in the CSS:

button#play_button_1:focus {
     background-color: #67181B;
    }

Resulting in the player looking like this after clicking "play": https://i.sstatic.net/kUNJs.jpg

  1. This setup works well as long as only the "play", "pause", and "stop" buttons are used. However, when other controls like "+" or "-" (for volume) are interacted with, the 'play' button loses focus and appears like this (the video continues playing despite no focus on the "-"): https://i.sstatic.net/0ySlF.jpg

My goal is to maintain the changed background of the "play" button while the video is playing, regardless of focus on other buttons. I hope this clarifies my objective further.

Answer №1

After some trial and error, I managed to come up with a solution that may not be perfect, but it gets the job done. If anyone has a better approach, I am open to suggestions. Instead of lumping all my CSS styles into one file, I created separate files for each function that required a background change. For example, I have a "play" button which corresponds to a play.css file:

button#play_button_1 {
background-color: #67181B;
}

button#pause_button_1 {
background-color: #D5D6DB;
}

Then, for each button's functionality, I included the following code:

  GtkCssProvider *provider;
  GdkDisplay *display;
  GdkScreen *screen;

  provider = gtk_css_provider_new ();
  display = gdk_display_get_default ();
  screen = gdk_display_get_default_screen (display);
  gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER   (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  const gchar* path_to_css = "play.css";
  GError *error = 0;
  gtk_css_provider_load_from_file(provider, g_file_new_for_path(path_to_css), &error);

I repeated this process for the pause button and loaded the initial CSS file again when the "stop" button was pressed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hover over the text below to see an image appear in its place

I'm looking to swap out some text with an image when hovering over it, using basic HTML/CSS. NO: show the image below the text NO: display the text on top of the image NO: replace one image with another YES: replace the text with an image ...

Fluctuating problem arises when placing one div over another div while hovering

Sorry if the title isn't clear. Basically, I have a div that when hovered over, triggers another div to appear with display:block, showing it above the original div. It works well but there is some flickering when moving the cursor over the second div ...

What are the steps for saving a user-defined function in GDB?

Whenever I exit GDB, all the user-defined functions disappear. I'm certain there must be a way to persist them between sessions. ...

Unable to simultaneously move two objects in opposite directions in OpenGL

In my current project, I am facing an issue where I am able to move one tank in a specific direction using the translatef function. However, when I try to move another tank in the opposite direction, both tanks end up moving in the same direction along w ...

Set the position of a div element to be fixed

I am currently working on a straightforward application that requires implementing a parallax effect. Here are the methods I have experimented with so far: - Inserting an image within a div with the class parallax. - Subsequently, adding an overlay div ...

Customize default progress bar in browsers

One feature of my website allows users to update their personal information, profile image, background image, and more. After clicking the submit button, a loading screen appears with a progress percentage displayed at the bottom left corner of the page (i ...

Guide to updating the button's color when clicked

I need to change the color of an iconic button when it is clicked. The default color is the primary color, but when the button is clicked, it should change to red. This functionality is working correctly. After clicking the button, two hidden buttons shoul ...

Choosing nested elements using CSS

Looking to target a specific element within a shared class, I'm struggling to find the right method to pinpoint the exact entry of this element. What I need to do is hide the current photo (pic2.jpg) and replace it with another image (pic3.jpg) in the ...

How can I adjust the border opacity in a React application?

Can we adjust the border color opacity in React when using a theme color that is imported? For example, if our CSS includes: borderBottomColor: theme.palette.primary.main and we are importing the theme with Material UI using makeStyles, is there a way to ...

Moving object sideways in 100px intervals

I'm struggling to solve this issue. Currently, I have multiple div elements (each containing some content) that I need to drag and drop horizontally. However, I specifically want them to move in increments of 100px (meaning the left position should b ...

Webster Barron code script

I'm attempting to replicate the concept of this video https://www.superhi.com/video/barron-webster using text, but I am encountering difficulties in achieving the desired effect. The design text is currently overlapping my name and displaying in rev ...

How to remove duplicate elements from a linked list using C programming?

I have been working on removing duplicates from a linked list, aiming for the resulting list to be free of any duplicates. For instance, starting with [1,1,2,3,4,4,4,5], the final list should be [1,2,3,4,5]. Here is the code I have written for this purpose ...

React Form Hook: restoring uniform width to selectors once more

Looking to create a form using React form hooks and Material UI? Check out this link to codesandbox to see the code in action. In the CreateEmployee.tsx file under components\execute folder, you'll find the necessary code. There's also a UI ...

Tips for showcasing an entire array in Visual Studio Studio while debugging

I'm currently troubleshooting a C program in VS Studio. I have MinGw installed and configured successfully. However, I am facing an issue where the debug window is not displaying all the contents of an int array[4]. In the debug window, my array A sho ...

Ways to display the md-card content based on the selected item in Angular2

1. I have 3 categories: ALL, Images, and Videos. When I select ALL, I want both videos and images to display in two separate cards. If I choose either Image or Video, only that specific type of content should be displayed. https://i.sstatic.net/4ezDw.png ...

Trouble with saving the positioning after drag and drop

I am currently facing an issue with my drag-and-drop script where the coordinates of positioned relative divs are not being saved in the same position when I reload. These divs are contained within a container with specific height and width, restricting t ...

Eliminate all numerical values from the array

Develop a function called remove_digits that takes in two arrays of integers. The first array consists of a series of numbers, while the second array contains individual digits. The objective is to eliminate all digits from the second array that are also p ...

Error: Local variable initialized uninitialized

Just starting out learning C and still navigating through the process. Looking for help on why my program doesn't work as intended. Any advice on how to handle the errors I encounter? #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include ...

Steps to ensure a div spans the entire height of the browser window

In the design of my webpage, I have organized content into two columns - a left div and a right div. The right div is styled with a grey background-color, and I am looking to make it expand vertically based on the user's browser window height. Curren ...

What benefits can be derived from utilizing assignments to compound literals?

While learning about compound literals, I discovered that they are l-values and can be assigned to. Curious about this aspect, I conducted an experiment which surprisingly compiled without warnings using gcc -Wall -Wextra -pedantic: struct foo { int x; ...