What could cause glutMainLoop() to not call the function passed to glutDisplayFunc()? - eclipse

Trying to get a very simple opengl/glut/glew program up and running. Currently the display() function passed to glutDisplayFunc() is not being called. When executed, init() sets everything up and gives me a blank white window, but display never fills it with the generated points. Here is the init() function which is called just before entering the glutMainLoop():
void init()
{
//generate points
const int NumPoints = 5000;
point3 points[NumPoints];
point3 vertices[3] = {point3(-1.0, -1.0, 0.0),
point3(0.0, 1.0, 0.0),
point3(1.0, -1.0, 0.0)};
points[0] = point3(0.25, 0.5, 0.0);
for(int k = 1; k < NumPoints; k++)
{
int j = rand() % 3;
points[k] = (points[k-1]+vertices[j])/2.0;
}
//load shaders and use the resulting shader program
GLuint program = InitShader("shaders/vshader.glsl", "shaders/fshader.glsl");
GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if( !linked ){
std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char *logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
glUseProgram( program );
//create Vertex-Array object
GLuint aBuffer;
glGenVertexArrays(1, &aBuffer);
glBindVertexArray((GLuint)&aBuffer);
//create Buffer object
GLuint buffer;
//glGenBuffers(1, &buffer);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points),
points, GL_STATIC_DRAW);
//initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition");
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glClearColor( 1.0, 1.0, 1.0, 1.0); // white background
}
and here are the main() and display() functions:
void display()
{
fprintf(stderr, "display called!\n");
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 5000);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutDisplayFunc(display);
glutCreateWindow("Program 1");
glewInit();
init();
glutMainLoop();
return 0;
}
using eclipse CDT C/C++ with MinGW. Debugging shows that glutMainLoop is indeed being called but I can't follow it past that. Could it be a shader issue? They are reportedly compiling and linking fine, but here they are
vshader.glsl
#version 150
in vec4 vPosition;
void main() {
gl_Position = vPosition;
}
.
fshader.glsl
#version 150
out vec4 fColor;
void main() {
fColor = vec4(1.0, 0.0, 0.0, 1.0);
}
edit: the shaders definitely work. By inserting glutIdleFunc(display); into the main(), the program executes properly and draws all the expected points. So like I originally thought, for some reason glutMainLoop() just doesnt want to call the function passed to glutDisplayFunc()? Or am I doing something terribly wrong?

GLUT can create multiple windows. glutDisplayFunc operates on the currently active window, so you must call glutCreateWindow before the glut…Func functions.

Related

modulo to cycle though a compute buffer not working as expected

The Setup
In my compute-shader I have a StructuredBuffer that is storing an amount of colors. There is also an int variable storing the amount of colors in total ( _colres ). From another script a node tree is dispatched into the shader every frame. The amount of nodes changes dynamically. Because of that the points buffer containing the nodes is at a fixed size of 8192 that the amount of nodes never exceeds.
The Problem
When I'm now trying to draw the points I am storing in the points buffer, oddly enough only every third color is displayed, starting at index [0] (tested for up to 12 colors -> [0],[3],[6],[9]).
Result[pointsBuffer[id.x].xy] = colorsBuffer[id.x % _colres];
What I tried
I used the fmod() function instead but was presented with the same result. Individually targeting stored colors by hard-coding the index has worked so my guess would be that the colors buffer is not the problem. Maybe it has something to do with all the empty spaces in the pointsbuffer but I could't figure it out.
The Question(s)
Is there a fundamental problem I am overlooking?
Is there some other simple way to cycle through the indices of my colorsbuffer that works in this scenario?
Detailed Information
System:
Unity Version 2021.2.12f1 using the HDRP on MacOS Monterey 12.2.1
Compute-Shader
#pragma kernel DrawPoints
// texture
shared RWTexture2D<float4> Result;
int _texres;
int _colres;
// buffer
StructuredBuffer<float2> pointsBuffer;
StructuredBuffer<float4> colorsBuffer;
[numthreads(64,1,1)]
void DrawPoints (uint3 id : SV_DispatchThreadID)
{
if ((pointsBuffer[id.x].x * pointsBuffer[id.x].y) > 0) Result[pointsBuffer[id.x].xy] = colorsBuffer[id.x % _colres];
}
C# Setup
public differentialGrowth diffGrowth;
int texResolution = 4096;
int colorAmount = 12;
RenderTexture settingRef;
Material target;
ComputeShader shader;
RenderTexture outputTexture;
ComputeBuffer pointsBuffer;
ComputeBuffer colorsBuffer;
int pointsHandle;
void Start()
{
outputTexture = new RenderTexture(settingRef);
outputTexture.enableRandomWrite = true;
outputTexture.Create();
// INIT
pointsHandle = shader.FindKernel("DrawPoints");
shader.SetInt("_texres", texResolution);
shader.SetInt("_colres", colorAmount);
int stride = (3) * 4; // every component as a float (3) * 4 bytes per float
pointsBuffer = new ComputeBuffer(8192, stride);
stride = (4) * 4;
colorsBuffer = new ComputeBuffer(colorAmount, stride);
shader.SetTexture( pointsHandle, "Result", outputTexture );
target.SetTexture("_MainTex", outputTexture);
Color[] testColors = new Color[colorAmount];
testColors[0] = new Color(1, 0, 0, 0); //red _ yes
testColors[1] = new Color(0, 1, 0, 0); //green
testColors[2] = new Color(0, 0, 1, 0); //blue
testColors[3] = new Color(1, 1, 0, 0); //yellow _yes
testColors[4] = new Color(0, 1, 1, 0); //cyan
testColors[5] = new Color(1, 0, 1, 0); //magenta
testColors[6] = new Color(0.5f, 0, 1, 0); //mix6 _yes
testColors[7] = new Color(1, 0.5f, 1, 0); //mix7
testColors[8] = new Color(0.5f, 0, 1, 0); //mix8
testColors[9] = new Color(0.5f, 0.5f, 1, 0); //mix9 _yes
testColors[10] = new Color(0.5f, 0.5f, 1, 0); //mix10
testColors[11] = new Color(0.5f, 0.5f, 1, 0); //mix11
}
void Update()
{
pointsBuffer.SetData(diffGrowth.nodes.Points);
shader.SetBuffer(pointsHandle, "colorsBuffer", colorsBuffer);
shader.SetBuffer(pointsHandle, "pointsBuffer", pointsBuffer);
shader.Dispatch(pointsHandle, 128, 1, 1);
}
private void OnDestroy()
{
if (pointsBuffer != null) pointsBuffer.Dispose();
if (colorsBuffer != null) colorsBuffer.Dispose();
}
}

How to draw a poppler document in a gtkmm DrawingArea

I am trying to draw a PDF with poppler gtk and a gtkmm DrawingArea, but it is not working, I am not sure what is wrong. The drawing area does not draw the document. I know the drawing area works otherwise with Cairo::Context::stroke(). Do I need to use a more gtk approach and wrap widget's to gtkmm?
Code:
// PdfViewer.h
#include <gtkmm.h>
#include <poppler.h>
#include "DrawingAreaFoo.h"
class PdfViewer: public Gtk::Box
{
public:
PdfViewer();
virtual ~PdfViewer();
private:
PopplerDocument *m_document;
PopplerPage *m_page;
DrawingAreaFoo m_drawingArea;
};
// PdfViewer.cpp
#include "PdfViewer.h"
PdfViewer::PdfViewer():
{
const char * uri = "file:////path/to/file/pdf.pdf";
m_document = poppler_document_new_from_file (uri, NULL, NULL);
auto total_pages = poppler_document_get_n_pages (m_document);
pack_start(m_drawingArea, TRUE, TRUE);
int w, h;
double width, height;
m_page = poppler_document_get_page (m_document, 0);
poppler_page_get_size (m_page, &width, &height);
w = (int) ceil(width);
h = (int) ceil(height);
cairo_surface_t * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
m_drawingArea.drawSurface(surface);
}
PdfViewer::~PdfViewer(){}
// DrawinAreFoo.h
#include <gtkmm.h>
class DrawingAreaFoo : public Gtk::DrawingArea
{
public:
DrawingAreaFoo();
virtual ~DrawingAreaFoo();
void drawSurface (cairo_surface_t * surface);
protected:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
Cairo::RefPtr<Cairo::Surface> m_refSurface;
};
// DrawinAreFoo.cpp
#include "DrawingAreaFoo.h"
DrawingAreaFoo::DrawingAreaFoo() {}
DrawingAreaFoo::~DrawingAreaFoo() {}
bool DrawingAreaFoo::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
if (m_refSurface)
{
cr->set_source(m_refSurface, 0, 0);
cr->paint();
}
return true;
}
void DrawingAreaFoo::drawSurface (cairo_surface_t * surface)
{
m_refSurface = Cairo::RefPtr<Cairo::Surface>{new Cairo::Surface(surface)} ;
Glib::RefPtr<Gdk::Window> win = get_window();
if (win)
{
Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
win->invalidate_rect(r, false);
}
}
UPDATE
The following after cairo_image_surface_create makes the code work.
cairo_t *cr = cairo_create (surface);
poppler_page_render (m_page, cr);
cairo_destroy (cr);
Added
cairo_t *cr = cairo_create (surface);
poppler_page_render (m_page, cr);
cairo_destroy (cr);
after
cairo_surface_t * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);

when launching boost::thread the .exe chrashes

This is my function:
void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag tagger, uint8_t chNum, int mask) {
double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];
int newmask;
double chDiff;
int edgeDiff;
int i;
while (runAcquisition) {
for (i = 0; i < chNum; i++) {
cmd_getThresh_getEdge(cmd_buffer, i, oldChannelvoltage, oldEdge);
}
Sleep(500);
newmask = 0;
for (i = 0; i < chNum; i++) {
cmd_getThresh_getEdge(cmd_buffer, i, newChannelvoltage, newEdge);
chDiff = oldChannelvoltage[i] - newChannelvoltage[i];
edgeDiff = oldEdge[i] - newEdge[i];
//printf("\nOld: %.2f, New: %.2f -> DIFF = %.2f", oldChannelvoltage[i], newChannelvoltage[i], diff);
if (chDiff != 0) {
WARN(newChannelvoltage[i] > 1.5, newChannelvoltage[i] = 1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, 1.5);
WARN(newChannelvoltage[i] < -1.5, newChannelvoltage[i] = -1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, -1.5);
tagger.SetInputThreshold(i + 1, newChannelvoltage[i]);
}
if (edgeDiff) {
if (!newEdge[i]) newmask += 1 << i;
}
}
if (newmask != mask) {
tagger.SetInversionMask(newmask);
mask = newmask;
}
}
delete[] oldChannelvoltage;
delete[] newChannelvoltage;
delete[] oldEdge;
delete[] newEdge;
}
When I launch the thread from the main() it crashes:
int main(int argc, char** argv) {
int mask = 0;
cmdbuf* cmd_buffer;
CTimeTag tagger;
//some code ....
//......
boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, tagger, 16, mask);
boost::thread th(cmdChangeSettThread);
//some other code ...
return 0;
}
Any idea ??
I thought the problem was caused by the arrays I'm using in the function but I can't figure out how to solve the problem.
Thank you very much!
You need to wait for the thread to finish in main.
If the thread destructor is called and the thread is still running terminate() is called.
th.join(); // should stop the application crashing.
return 0;
}
PS. None of this is good:
double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];
Use a vector (or an array).
Get a code review: http://codereview.stackexchange.com
Thank you everybody! I found the problem!! I was stupidly passing the object CTimeTag tagger by value, I'm sorry if I wasn't super clear in presenting the problem!
So now the function definition is:
void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag *tagger, tt_buf* buffer, uint8_t chNum, int mask)
and when I'm calling it with boost::bind I have:
boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, &tagger, buffer, 16, mask);
Thank you again!

Fast drawing tilings GTK and Cairo

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.

How do I make a gtkwindow background transparent on Linux?

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?