Related
There is defined a GTK tree view and one of the column is rendered the toggle button. Now every row is showing the toggle button, is there a way to remove or hide the toggle button from some row completely. For example I have following example
#include <gtk/gtk.h>
enum {
COL_NUM_LIST = 0,
COL_TOGGLE,
COL_STRING,
NUM_COLS
};
static GtkTreeModel*
create_and_fill_model(void) {
GtkTreeStore *treestore;
GtkTreeIter toplevel, child;
treestore = gtk_tree_store_new(NUM_COLS,
G_TYPE_STRING,
G_TYPE_BOOLEAN,
G_TYPE_STRING);
/* Append a top level row and leave it empty */
gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel,
COL_NUM_LIST, "1",
COL_TOGGLE, TRUE,
COL_STRING, "Foo Bar",
-1);
/* Append a second top level row, and fill it with some data */
gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel,
COL_NUM_LIST, "2",
COL_TOGGLE, TRUE,
COL_STRING, "", // empty
-1);
/* Append a child to the second top level row, and fill in some data */
gtk_tree_store_append(treestore, &child, &toplevel);
gtk_tree_store_set(treestore, &child,
COL_NUM_LIST, "3",
COL_TOGGLE, FALSE,
COL_STRING, "Not needed toggle button here",
-1);
return GTK_TREE_MODEL(treestore);
}
void string_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data) {
char *string;
gtk_tree_model_get(model, iter, COL_STRING, &string, -1);
g_object_set(renderer, "text", string, NULL);
}
void toggle_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data) {
int bol;
gtk_tree_model_get(model, iter, COL_TOGGLE, &bol, -1);
if (bol == 0)
g_object_set(renderer, "active", NULL, NULL); // Can we somehow null or remove toggle button
else
g_object_set(renderer, "active", TRUE, NULL);
}
static GtkWidget*
create_view_and_model(void) {
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
view = gtk_tree_view_new();
// --- Column #1 ---
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "#");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
// toggle renderer
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_NUM_LIST);
// --- Column #2 ---
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Toggle");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
// toggle renderer
renderer = gtk_cell_renderer_toggle_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_cell_data_func(col, renderer,
toggle_cell_data_func, NULL, NULL);
// --- Column #3 ---
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "String");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
// text renderer
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_cell_data_func(col, renderer,
string_cell_data_func, NULL, NULL);
model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
g_object_unref(model); /* destroy model automatically with view */
gtk_tree_selection_set_mode(
gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
GTK_SELECTION_NONE);
return view;
}
int main(int argc, char **argv) {
GtkWidget *window;
GtkWidget *view;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Compiled and run with GTK 3 , it creates following windows
So, I don't need toggle button at #3 leave of tree. How can I hide it.
P.S : I know that using GTK, I can set the state of button to disable or inconsistent, but can I completely hide it?
You could probably revise the properties you are setting via the "g_object_set" function as in the following code snippet.
void toggle_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
int bol;
gtk_tree_model_get(model, iter, COL_TOGGLE, &bol, -1);
if (bol == 0)
g_object_set(renderer, "visible", NULL, NULL); // Can we somehow null or remove toggle button
else
{
g_object_set(renderer, "visible", TRUE, "active", TRUE, NULL);
}
}
For the toggle button that should not appear, I utilized the "visible" property in lieu of the "active" property. That yielded the following sample image.
See if that helps.
Regards.
I have 2 threads
1)Holds the GTK main and gtk screen display codes (code is explained below) 2)generates key events according to user rquirement
if() block i ported into my code. but result is same. Once the signal is generated .after that its not coming to 2nd thread(signal generation thread). Have put debug prints ,but its not happening Seems its waiting on gtk_main on first thread.
What my code is :
void S1(void)
{
GtkWidget *Win_1;
GtkBuilder *builder;
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "/home/glade/glade1.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK(kp_event), NULL);
gtk_widget_show_all(window);
gtk_main();
}
kp_event()
{
gtk_widget_destroy (window);
S2();
}
S2 is same as S1,only screen item difference.Am calling S2() from keypress handler of S1 & vice versa. Since i have no keyboards attached,need to change two screens base on some user input via sockets or something.
You may need to call gtk_main() just one time, and use gtk_widget_hide() and gtk_window_present(), instead of gtk_widget_destroy(), declaring window1 and window2 as global variables, and creating the two windows at startup. A sample code:
GtkWidget * window1;
GtkWidget * window2;
void S1() {
// create the window
window1 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
// do not call gtk_main()
}
void S2() {
// create the window
window2 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_2"));
// do not call gtk_main()
}
kp_event_S1() {
gtk_widget_hide(window1);
gtk_window_present(GTK_WINDOW(window2));
}
kp_event_S2() {
gtk_widget_hide(window2);
gtk_window_present(GTK_WINDOW(window1));
}
int main() {
gtk_init();
S1();
S2();
gtk_widget_hide(window2);
gtk_main();
}
If you don't want to use global variables, you can do:
GtkWidget * S1() {
// create the window
GtkWidget * window1 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
return window1;
}
GtkWidget * S2() {
// create the window
GtkWidget * window2 = GTK_WIDGET (gtk_builder_get_object (builder, "Win_2"));
return window2;
}
gboolean kp_event_S1(GtkWidget * window, GdkEvent e, gpointer user_data) {
gtk_widget_hide(window);
gtk_window_present(GTK_WINDOW(user_data));
}
gboolean kp_event_S2(GtkWidget * window, GdkEvent e, gpointer user_data) {
gtk_widget_hide(window);
gtk_window_present(GTK_WINDOW(user_data));
}
int main() {
gtk_init();
GtkWidget * w1 = S1();
GtkWidget * w2 = S2();
gtk_widget_hide(w2);
g_signal_connect (G_OBJECT (w1), "key-press-event", G_CALLBACK(kp_event_S1), (gpointer)w2);
g_signal_connect (G_OBJECT (w2), "key-press-event", G_CALLBACK(kp_event_S2), (gpointer)w1);
gtk_main();
}
I am new at drawing with Cairo and GTK, and the program I'm working on needs to draw a circle tiling of 500x500 or 1000x1000. Also, there are some work to do before drawing but right now I am focused on the drawing part which will involve mouse interaction to change the color of any circle.
So, the tiling is the same, and over time the circles have to change their color (all of them). I have to check with each circle and perform an operation, and after I check all circles, I have to display the changes. This process has to be performed any number of times.
Right now I have the tiling with a scrolled window, but just with this it takes a lot of time the scrolling. Thanks in advance. My code is next:
#include <cairo.h>
#include <gtk/gtk.h>
#include <math.h>
static void do_drawing(cairo_t *, GtkWidget *);
static int cellRadius=5;
static int cellDiameter=10;
static int latticeSideSize=500;
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer user_data){
do_drawing(cr, widget);
return FALSE;
}
static void do_drawing(cairo_t *cr, GtkWidget *widget)
{
int i=0,j=0;
GtkWidget *win = gtk_widget_get_toplevel(widget);
int width, height;
gtk_window_get_size(GTK_WINDOW(win), &width, &height);
cairo_set_line_width(cr, .5);
cairo_set_source_rgb(cr, 0.69, 0.19, 0);
cairo_save (cr);
for(i=0;i<latticeSideSize;i++){
for(j=0;j<latticeSideSize;j++){
if(i%2 == 0){
cairo_arc(cr, cellRadius + 2*cellRadius + j*cellDiameter, cellRadius + cellRadius + i*cellDiameter, cellRadius, 0, 2 * M_PI);
cairo_stroke(cr);
}else{
cairo_arc(cr, cellRadius + cellRadius + j*cellDiameter, cellRadius + cellRadius + i*cellDiameter, cellRadius, 0, 2 * M_PI);
cairo_stroke(cr);
}
}
}
cairo_restore (cr);
}
static void destroy( GtkWidget *widget, gpointer data ){
gtk_main_quit ();
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *scrolled_window;
GtkWidget *darea;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(scrolled_window), darea);
gtk_container_add(GTK_CONTAINER(window), scrolled_window);
g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), NULL);
g_signal_connect(G_OBJECT(scrolled_window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request( scrolled_window, 500, 500 );
gtk_window_set_default_size(GTK_WINDOW(window), 1024, 800);
gtk_widget_set_hexpand( scrolled_window, TRUE );
gtk_widget_set_vexpand( scrolled_window, TRUE );
gtk_window_set_title(GTK_WINDOW(window), "HexaGrid");
gtk_widget_set_size_request(darea,cellDiameter*latticeSideSize + 20,cellDiameter*latticeSideSize + 20);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER (scrolled_window), 10);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
There's two problems here. First, you're doing the same fairly demanding calculation (the circle) 250000 times per draw, so 15 million circles per second if scrolling was smooth: that's not a realistic requirement. You should probably do the circle once and then apply the same result as surface pattern with CAIRO_EXTEND_REPEAT extend mode. You set the position of the pattern by using cairo_translate() and use cairo_set_source() to set your circle pattern as the source and then cairo_rectangle() + cairo_fill() to draw it. Cairo samples contain an example using a bitmap.
If some of the circles need to be in different color, you could draw some or all of them "manually" (without the repeating extend mode) but using a pattern is still probably a good idea to avoid calculating the circle many times.
Second, in the case of complex widgets it makes sense to not draw the whole widget but only the dirty region: see draw-signal documentation.
#include "UI.h"
GtkWidget* create_main_frame(gint wid, gint hgt)
{
GtkWidget* main_frame = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* window attributes */
gtk_window_set_title( GTK_WINDOW(main_frame), "Welcome!!" );
gtk_window_set_default_size( GTK_WINDOW(main_frame), wid, hgt );
/* signals */
g_signal_connect(main_frame, "destroy", G_CALLBACK(gtk_main_quit), NULL);
return main_frame;
}
GtkWidget* create_scrolled_window(void)
{
GtkWidget* scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
gtk_container_set_border_width( GTK_CONTAINER(scrolled_window), 10 );
return scrolled_window;
}
GtkWidget* create_box(GtkOrientation orn, gint spc)
{
GtkWidget* box = gtk_box_new(orn, spc);
//gtk_container_set_border_width( GTK_CONTAINER(box), 5 );
return box;
}
GtkWidget* create_layout(guint wid, guint hgt)
{
GtkWidget* layout = gtk_layout_new(NULL, NULL);
gtk_layout_set_size( GTK_LAYOUT(layout), wid, hgt );
return layout;
}
GtkWidget* create_grid(guint rsp, guint csp)
{
GtkWidget* grid = gtk_grid_new();
gtk_grid_set_row_spacing( GTK_GRID(grid), rsp );
gtk_grid_set_column_spacing( GTK_GRID(grid), csp );
return grid;
}
/*
GtkWidget* create_token_button(Token* tkn)
{
char parsed_value[11];
char* string = parse_to_string(tkn -> value, parsed_value);
GtkWidget* button = gtk_button_new_with_label(value);
//gtk_signal_connect(button, "clicked", G_CALLBACK, );
return button;
}
*/
GtkWidget* create_commodity_button(Commodity* com)
{
GtkWidget* button = gtk_button_new_with_label(com -> name);
//gtk_signal_connect(button, "clicked", G_CALLBACK, );
return button;
}
#include "UI.h"
int main(int argc, char* argv[])
{
int i, j;
/* vending machine */
VendingMachine* vending_machine;
/* frames */
GtkWidget* main_frame;
GtkWidget* scrolled_window;
GtkWidget* commodity_box, * commodity_layout, * commodity_grid, * commodity_button;
/* initialize */
gtk_init(&argc, &argv);
make_new_vending_machine_memory(&vending_machine, sizeof(VendingMachine) );
boot_vending_machine(vending_machine, 13, 13);
/* setting main frame */
main_frame = create_main_frame(1000, 750);
/* setting commodty frames and button table */
commodity_box = create_box(GTK_ORIENTATION_HORIZONTAL, 10);
scrolled_window = create_scrolled_window();
commodity_layout = create_layout(500, 700);
commodity_grid = create_grid(10, 10);
for (i = 0; i < 13; i++)
for (j = 0; j < 13; j++) {
commodity_button = create_commodity_button(&vending_machine -> commodity[i][j]);
gtk_grid_attach( GTK_GRID(commodity_grid), commodity_button, i * 300, j * 300, 5, 7 );
}
/* start adding and packing */
gtk_layout_put( GTK_LAYOUT(commodity_layout), commodity_grid, 10, 10 );
gtk_container_add( GTK_CONTAINER(scrolled_window), commodity_layout );
gtk_box_pack_start( GTK_BOX(commodity_box), scrolled_window, TRUE, TRUE, 10 );
gtk_container_add( GTK_CONTAINER(main_frame), commodity_box );
/* show all */
gtk_widget_show_all(main_frame);
gtk_main();
return 0;
}
I'm done writing the basic functions that are needed in my small vending machine project.
Now, I'm trying to make an UI out of the functions with GTK+. And I'm very confused since
this is the first time I'm actually using GTK.
I want a grid of commodity buttons that should be displayed on the left side, but I got
pretty much stuck on this part. What I'm trying to do is, since I "malloc"ed the commodities
for the purpose of adding and remove comms, I want a scroll bar attached to the comm window.
what I did as you can see in the source code
I made a grid of comm buttons and added on a layout widget.
I added the layout on a scrollbar widget
I added that scrollbar on a box and packed it.
I added that box to the main window.
the result is well "not satisfing"
I'm struggling through the GNOME official APIs
can somebody help me with this??
If you don't see how to create your user interface, give Glade a try. You'll be able to quickly try and see how the widgets fit together.
I would like to make the background transparent, and only the widgets are visible.
Here is my code:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
// Title
gtk_window_set_title(GTK_WINDOW (window), "Transparency");
//gtk_window_set_opacity(GTK_WINDOW(window), 0.5);
// CSS
GtkCssProvider *provider = gtk_css_provider_new();
GdkDisplay *display = gdk_display_get_default();
GdkScreen *screen = gdk_display_get_default_screen(display);
gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_css_provider_load_from_data(GTK_CSS_PROVIDER (provider),
"GtkWindow {\n"
" background-color: rgba(0,0,0,0);\n"
"}\n",
-1, NULL);
g_object_unref (provider);
// Window
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_resize(GTK_WINDOW(window), 400, 300);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I use gtk3. When the program execute, it just shows black. The CSS (or rgba) function does not work.
I try to use gtk_window_set_opacity(), but it also just shows black.
How do I fix my code?
I followed the link suggested by the comment, but unfortunately it was written for Gtk 2. I have re-worked it for Gtk 3. (I am using Gtk 3.8, but as far as I know it does not use anything deprecated in Gtk 3.10). The program produces a green semi-transparent square with button in it. Of course, you could make the square completely transparent by changing the last argument for the function cairo_set_source_rgba to 0.
Note: I compiled this with the following command (assuming you call the file transparent.c):
gcc -o transparent transparent.c `pkg-config gtk+-3.0 --libs --cflags`
Here is the code:
Version for C
/**
* Original code by: Mike - http://plan99.net/~mike/blog (now a dead link--unable to find it).
* Modified by karlphillip for StackExchange:
* (https://stackoverflow.com/questions/3908565/how-to-make-gtk-window-background-transparent)
* Re-worked for Gtk 3 by Louis Melahn, L.C., January 30, 2014.
*/
#include <gtk/gtk.h>
static void screen_changed(GtkWidget *widget, GdkScreen *old_screen, gpointer user_data);
static gboolean draw(GtkWidget *widget, cairo_t *new_cr, gpointer user_data);
static void clicked(GtkWindow *win, GdkEventButton *event, gpointer user_data);
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
gtk_window_set_title(GTK_WINDOW(window), "Alpha Demo");
g_signal_connect(G_OBJECT(window), "delete-event", gtk_main_quit, NULL);
gtk_widget_set_app_paintable(window, TRUE);
g_signal_connect(G_OBJECT(window), "draw", G_CALLBACK(draw), NULL);
g_signal_connect(G_OBJECT(window), "screen-changed", G_CALLBACK(screen_changed), NULL);
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(clicked), NULL);
GtkWidget* fixed_container = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed_container);
GtkWidget* button = gtk_button_new_with_label("button1");
gtk_widget_set_size_request(button, 100, 100);
gtk_container_add(GTK_CONTAINER(fixed_container), button);
screen_changed(window, NULL, NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
gboolean supports_alpha = FALSE;
static void screen_changed(GtkWidget *widget, GdkScreen *old_screen, gpointer userdata)
{
/* To check if the display supports alpha channels, get the visual */
GdkScreen *screen = gtk_widget_get_screen(widget);
GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
if (!visual)
{
printf("Your screen does not support alpha channels!\n");
visual = gdk_screen_get_system_visual(screen);
supports_alpha = FALSE;
}
else
{
printf("Your screen supports alpha channels!\n");
supports_alpha = TRUE;
}
gtk_widget_set_visual(widget, visual);
}
static gboolean draw(GtkWidget *widget, cairo_t *cr, gpointer userdata)
{
cairo_save (cr);
if (supports_alpha)
{
cairo_set_source_rgba (cr, 0.5, 1.0, 0.50, 0.5); /* transparent */
}
else
{
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* opaque white */
}
/* draw the background */
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_restore (cr);
return FALSE;
}
static void clicked(GtkWindow *win, GdkEventButton *event, gpointer user_data)
{
/* toggle window manager frames */
gtk_window_set_decorated(win, !gtk_window_get_decorated(win));
}
Version for C++
I include a very similar program, this time written for gtkmm in C++. It can be compiled with the following command:
g++ -otransparent main.cpp transparent.cpp `pkg-config gtkmm-3.0 --cflags --libs` -std=c++11
Note that I used some of the features in the new C++-11 standard, so you will need a compiler that supports them. (If you don't have one, you just have to replace the auto keyword when it appears with the appropriate type, which you can figure out from the definition of the function.) There are three files: main.cpp, transparent.h, and transparent.cpp.
main.cpp
/**
* main.cpp
*
* Code adapted from 'alphademo.c' by Mike
* (http://plan99.net/~mike/blog--now a dead link--unable to find it.)
* as modified by karlphillip for StackExchange:
* (https://stackoverflow.com/questions/3908565/how-to-make-gtk-window-background-transparent)
* Re-worked for Gtkmm 3.0 by Louis Melahn, L.C. January 31, 2014.
*/
#include "transparent.h"
#include
int main (int argc, char *argv[])
{
Glib::RefPtr app = Gtk::Application::create(argc, argv, "org.gtkmm.example.transparent");
Transparent transparent;
//Shows the window and returns when it is closed.
return app->run(transparent);
}
transparent.h
/**
* transparent.h
*
* Code adapted from 'alphademo.c' by Mike
* (http://plan99.net/~mike/blog--now a dead link--unable to find it.)
* as modified by karlphillip for StackExchange:
* (https://stackoverflow.com/questions/3908565/how-to-make-gtk-window-background-transparent)
* Re-worked for Gtkmm 3.0 by Louis Melahn, L.C. January 31, 2014.
*/
#ifndef TRANSPARENT_H_
#define TRANSPARENT_H_
#include <iostream>
#include <gtk/gtk.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/alignment.h>
class Transparent : public Gtk::Window
{
private:
std::string _buttonLabel;
public:
Transparent();
void set_visual(Glib::RefPtr<Gdk::Visual> visual);
virtual ~Transparent();
protected:
// Signal handlers:
// Note that on_draw is actually overriding a virtual function
// from the Gtk::Window class. I set it as virtual here in case
// someone wants to override it again in a derived class.
void on_button_clicked();
virtual bool on_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr);
void on_screen_changed(const Glib::RefPtr<Gdk::Screen>& previous_screen);
bool on_window_clicked(GdkEventButton* event);
// Member widgets:
Gtk::Alignment _alignment;
Gtk::Button _button;
bool _SUPPORTS_ALPHA = false;
};
#endif /* TRANSPARENT_H_ */
transparent.cpp
/**
* transparent.cpp
*
* Code adapted from 'alphademo.c' by Mike
* (http://plan99.net/~mike/blog--now a dead link--unable to find it.)
* as modified by karlphillip for StackExchange:
* (https://stackoverflow.com/questions/3908565/how-to-make-gtk-window-background-transparent)
* Re-worked for Gtkmm 3.0 by Louis Melahn, L.C. January 31, 2014.
*/
#include "transparent.h"
Transparent::Transparent() :
_buttonLabel("Button1"),
_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START, 0.0, 0.0), // Aligns the button.
_button(_buttonLabel) // Creates a new button with label '_buttonLabel'.
{
// Set up the top-level window.
set_title("Transparency test");
set_default_size(400,400);
set_decorated(false);
add_events(Gdk::BUTTON_PRESS_MASK);
set_position(Gtk::WIN_POS_CENTER);
set_app_paintable(true);
// Signal handlers
signal_draw().connect(sigc::mem_fun(*this, &Transparent::on_draw));
signal_screen_changed().connect(sigc::mem_fun(*this, &Transparent::on_screen_changed));
signal_button_press_event().connect(sigc::mem_fun(*this, &Transparent::on_window_clicked));
_button.signal_clicked().connect(sigc::mem_fun(*this, &Transparent::on_button_clicked));
// Widgets
on_screen_changed(get_screen());
// This will add the aligner.
add(_alignment);
// Now pack the button into the aligner.
_alignment.add(_button);
// Set up the button
_button.set_size_request(100, 100);
// Show the window and all its children.
show_all();
}
Transparent::~Transparent()
{
}
void Transparent::on_button_clicked()
{
std::cout << "The button '" << _buttonLabel << "' was pressed." << std::endl;
}
bool Transparent::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
cr->save();
if (_SUPPORTS_ALPHA) {
cr->set_source_rgba(0.5, 1.0, 0.5, 0.5); // transparent
} else {
cr->set_source_rgb(0.5, 1.0, 0.5); // opaque
}
cr->set_operator(Cairo::OPERATOR_SOURCE);
cr->paint();
cr->restore();
return Gtk::Window::on_draw(cr);
}
/**
* Checks to see if the display supports alpha channels
*/
void Transparent::on_screen_changed(const Glib::RefPtr<Gdk::Screen>& previous_screen) {
auto screen = get_screen();
auto visual = screen->get_rgba_visual();
if (!visual) {
std::cout << "Your screen does not support alpha channels!" << std::endl;
} else {
std::cout << "Your screen supports alpha channels!" << std::endl;
_SUPPORTS_ALPHA = TRUE;
}
set_visual(visual);
}
/**
* This simply adds a method which seems to be missing in Gtk::Widget,
* so I had to use Gtk+ manually.
*
* Sets the visual for 'this' (the current widget).
*/
void Transparent::set_visual(Glib::RefPtr<Gdk::Visual> visual) {
gtk_widget_set_visual(GTK_WIDGET(gobj()), visual->gobj());
}
/**
* If I click somewhere other than the button, this toggles
* between having window decorations and not having them.
*/
bool Transparent::on_window_clicked(GdkEventButton* event) {
set_decorated(!get_decorated());
return false;
}
Hope this helps!
While struggling with the same issue, I have noticed that if I call gtk_window_set_opacity() on the toplevel window after the show_all function, making the whole window (partial) transparent works for me. Give this a try:
gtk_widget_show_all ( window );
gtk_widget_set_opacity (GTK_WIDGET (window), 0.5);
Does that work for you too?