Erase a row from a Gtkmm::Table - c++

How do I erase a row from a Gtkmm::Table in C++?
Documentation is really poor :-(. I have tried using resize() with no effect, and also calling remove at a Gtk::Widget& that I obtained through get_children(), but the I get segfault...

If Gtkmm::Table is GtkTable then gtk_container_remove should, probably, do what you need. I've made a small example (see below) it removes all label widget from the table container.
#include <gtk/gtk.h>
void remove_widgets(GtkWidget *widget, gpointer data)
{
if (GTK_IS_LABEL(widget))
{
g_print("remove label\n");
gtk_container_remove(GTK_CONTAINER(data), widget);
}
}
void remove_rows(GtkWidget *button, GtkWidget* data)
{
g_print("remove rows with labels\n");
gtk_container_foreach(GTK_CONTAINER(data), remove_widgets, data);
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *table;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request (window, 300, 250);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_title(GTK_WINDOW(window), "Test Table Remove");
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
table = gtk_table_new(3, 2, FALSE);
gtk_table_set_col_spacings(GTK_TABLE(table), 3);
GtkWidget *label0 = gtk_label_new("Test Label 0");
gtk_table_attach(GTK_TABLE(table), label0, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label1 = gtk_label_new("Test Label 1");
gtk_table_attach(GTK_TABLE(table), label1, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label2 = gtk_label_new("Test Label 3");
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label3 = gtk_label_new("Test Label 4");
gtk_table_attach(GTK_TABLE(table), label3, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *button0 = gtk_button_new_with_label("Remove Labels");
gtk_widget_set_size_request(button0, 70, 30);
gtk_table_attach(GTK_TABLE(table), button0, 0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
g_signal_connect(G_OBJECT(button0), "clicked", G_CALLBACK(remove_rows), table);
gtk_container_add(GTK_CONTAINER(window), table);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
hope this helps, regards

Related

How do I make cube faces opaque on OpenGL?

I'm writing a program to draw a cube on OpenGL and rotate it continuously on mouse clicks. At particular angles, I'm able to see through the cube (transparent). I've enabled Depth Test, so I don't know why this is happening. I am not sure if I have enabled it correctly.
#include <math.h>
#include <vector>
#include <Windows.h>
#include <gl\glut.h>
using namespace std;
void myInit() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glOrtho(-2, 2, -2, 2, 2, -2);
glMatrixMode(GL_MODELVIEW);
}
float Cube[][3] = { {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1} };
float Colors[][3] = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1} };
int axis = 0, theta[3] = {0, 0, 0};
void draw_face (int a, int b, int c, int d) {
glBegin(GL_QUADS);
glColor3fv(Colors[a]);
glVertex3fv(Cube[a]);
glColor3fv(Colors[b]);
glVertex3fv(Cube[b]);
glColor3fv(Colors[c]);
glVertex3fv(Cube[c]);
glColor3fv(Colors[d]);
glVertex3fv(Cube[d]);
glEnd();
}
void draw_cube () {
draw_face(0, 3, 2, 1);
draw_face(2, 3, 7, 6);
draw_face(0, 4, 7, 3);
draw_face(1, 2, 6, 5);
draw_face(4, 5, 6, 7);
draw_face(0, 1, 5, 4);
}
void spin_cube() {
theta[axis] += 2;
if (theta[axis] > 360)
theta[axis] = -360;
glutPostRedisplay();
}
void idle_func() {
Sleep(10);
spin_cube();
}
void mouse_func(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void myDrawing() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
draw_cube();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glutDisplayFunc(myDrawing);
glutIdleFunc(idle_func);
glutMouseFunc(mouse_func);
myInit();
glutMainLoop();
}
Multiple issues:
You aren't requesting a depth buffer from GLUT:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
Solution: OR in GLUT_DEPTH to make sure GLUT requests some depth buffer bits from the OS during GL context creation:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
You're calling glEnable(GL_DEPTH_TEST) before GLUT has created a GL context:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// no GL context yet
glEnable(GL_DEPTH_TEST);
Solution: Move the glEnable() to after glutCreateWindow() so it has a current GL context to work with:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glEnable(GL_DEPTH_TEST);
...
You never clear the depth buffer:
void myDrawing() {
// where's GL_DEPTH_BUFFER_BIT?
glClear(GL_COLOR_BUFFER_BIT);
...
Solution: OR in GL_DEPTH_BUFFER_BIT to your glClear() argument:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
All the fixes together:
#include <cmath>
#include <vector>
#include <GL/glut.h>
using namespace std;
void myInit() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glOrtho(-2, 2, -2, 2, 2, -2);
glMatrixMode(GL_MODELVIEW);
}
float Cube[][3] = { {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1} };
float Colors[][3] = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1} };
int axis = 0, theta[3] = {0, 0, 0};
void draw_face (int a, int b, int c, int d) {
glBegin(GL_QUADS);
glColor3fv(Colors[a]);
glVertex3fv(Cube[a]);
glColor3fv(Colors[b]);
glVertex3fv(Cube[b]);
glColor3fv(Colors[c]);
glVertex3fv(Cube[c]);
glColor3fv(Colors[d]);
glVertex3fv(Cube[d]);
glEnd();
}
void draw_cube () {
draw_face(0, 3, 2, 1);
draw_face(2, 3, 7, 6);
draw_face(0, 4, 7, 3);
draw_face(1, 2, 6, 5);
draw_face(4, 5, 6, 7);
draw_face(0, 1, 5, 4);
}
void spin_cube() {
theta[axis] += 2;
if (theta[axis] > 360)
theta[axis] = -360;
glutPostRedisplay();
}
void idle_func() {
Sleep(10);
spin_cube();
}
void mouse_func(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void myDrawing() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
draw_cube();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(myDrawing);
glutIdleFunc(idle_func);
glutMouseFunc(mouse_func);
myInit();
glutMainLoop();
}

How to use GTK RC files in GTK2

This is my code:
int main(int argc, char *argv[] ) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkButton *button;
gtk_rc_parse("style.rc");
window = gtk_dialog_new();
gtk_widget_set_name(window, "window");
g_signal_connect(window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_fullscreen(GTK_WINDOW(window));
button = GTK_BUTTON(gtk_button_new_with_label ("Quit"));
gtk_widget_set_name(GTK_WIDGET(button), "button");
g_signal_connect_swapped (GTK_WIDGET(button), "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_widget_set_can_default (GTK_WIDGET(button), TRUE);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
gtk_widget_grab_default (GTK_WIDGET(button));
gtk_widget_show (GTK_WIDGET(button));
gtk_widget_show (window);
gtk_main();
}
And this is my style.rc:
style "button"
{
fg[PRELIGHT] = { 0, 1.0, 1.0 }
bg[PRELIGHT] = { 0, 0, 1.0 }
bg[ACTIVE] = { 1.0, 0, 0 }
fg[ACTIVE] = { 0, 1.0, 0 }
bg[NORMAL] = { 1.0, 1.0, 0 }
fg[NORMAL] = { .99, 0, .99 }
bg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
fg[INSENSITIVE] = { 1.0, 0, 1.0 }
}
widget "*GtkButton" style "button"
But my button looks totally standard. Am I using the rc file wrong? It is copied from the official GTK tutorial.
Also, there are no errors if I type in a random name instead of style.rc, so I don't know if it's being registered at all.

How convert text to pixel array in QT

I'm new user of QT. Is it possible to convert for example one character or string to array where 0 means white field and 1 means black field? For example when I have character "a" saved as QString or any representation of font and I would like to get following array:
int array[9][6] = { //"a" char array
{0, 0, 0, 0, 0, 0}, //- - - - - -
{0, 1, 1, 1, 1, 0}, //- * * * * -
{1, 0, 0, 0, 0, 1}, //* - - - - *
{0, 0, 0, 0, 0, 1}, //- - - - - *
{0, 1, 1, 1, 1, 1}, //- * * * * *
{1, 0, 0, 0, 0, 1}, //* - - - - *
{1, 0, 0, 0, 1, 1}, //* - - - * *
{0, 1, 1, 1, 0, 1}, //- * * * - *
{0, 0, 0, 0, 0, 0}, //- - - - - -
};
Is there any way in QT for reach that goal ? I hope I describe my problem as clear as possible and someone can help me with this?
And very important thing size array must depend on selected font size, so if I choose for example 30 pt font, array should be appropriate bigger than 10 pt font.
Thank You very much.
Best regards.
You can draw text onto a bitmap image, which will give you a bitmap containing text. Which is a pixel array, for all intents and purposes.
Note that you will also have to use a "bitmap font", regular fonts won't really do the trick as most have been created to utilize antialiasing and won't result in a crisp and readable bitmap image.
Qt also offers font metrics, which can give you a clear idea how big text or individual characters are.
I misinterpreted the question on my first read through... well, here is what was asked for...
#include <QApplication>
#include <QPixmap>
#include <QLabel>
#include <QPainter>
#include <QDebug>
#include <QVector>
#include <QFontMetrics>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSize size;
// QPixmap pixmap(100,100);
QImage image(20,20, QImage::Format_Mono);// Change the width and height here as needed if your letters get too big
image.fill(0);
{
QPainter painter(&image);
//// alternative to fill method
// painter.setBrush(Qt::black);
// painter.drawRect(0,0,image.width(), image.height());
painter.setPen(Qt::white);
QFontMetrics fm(painter.font());
size.setHeight(fm.height());
size.setWidth(fm.width('a'));
painter.drawText(0,fm.height(),"a");
}
QLabel label;
label.setPixmap(QPixmap::fromImage(image).scaled(400,400, Qt::KeepAspectRatio));
label.show();
QVector <QVector <int> > array(size.height(), QVector < int> (size.width(), 0));
for(int r = 0; r < size.height(); r++)
for(int c = 0; c < size.width(); c++)
array[r][c] = (image.pixelColor(c,r) == Qt::white ? 1 : 0);
// qDebug() << size << array;
foreach(QVector <int> row, array)
qDebug() << row;
return a.exec();
}
Output:
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 0, 0, 0, 0, 0)
QVector(0, 1, 1, 1, 0, 0)
QVector(0, 0, 0, 0, 1, 0)
QVector(0, 1, 1, 1, 1, 0)
QVector(1, 0, 0, 0, 1, 0)
QVector(1, 0, 0, 0, 1, 0)
QVector(0, 1, 1, 1, 1, 0)

Is it possible to set the color of a ScrollWindow?

I have the following code,:
GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;
gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);
For some reason, the color of the window but not the scroll window is set to black. How can I change this?
All you need is a GtkEventBox.
GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;
gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);
GtkWidget *box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(box), outside);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), box);
gtk_widget_modify_bg(box, GTK_STATE_NORMAL, &color);
gtk_widget_show (box);
//gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
//gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);
If this is Gtk3 you should be using CSS: https://developer.gnome.org/gtk3/stable/GtkCssProvider.html
Something like this sets the background color of all children of scrolled windows:
scrolledwindow * {
background-color: red;
}
Obviously in real usage you would probably be more specific by giving your widget a custom class itself.
Check out the inspector too to make trying out CSS easier: https://wiki.gnome.org/Projects/GTK+/Inspector

how to stretch an image in GTK+?

I'm using the following code to set the background Image but as size of image is small I want to stretch the size to fit the screen or if the image is larger than screen in that case too I need the same.
Im using Gtk+3.2
#include <gtk/gtk.h>
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *layout;
GtkWidget *image;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 290, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
layout = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER (window), layout);
gtk_widget_show(layout);
image = gtk_image_new_from_file("/home/my_background_image.jpg");
gtk_layout_put(GTK_LAYOUT(layout), image, 0, 0);
button = gtk_button_new_with_label("Button");
gtk_layout_put(GTK_LAYOUT(layout), button, 150, 50);
gtk_widget_set_size_request(button, 80, 35);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I dont think you really need layout for setting background image.
You can overload "draw" signal and in handler you can paint background image.
The following code will work for you.
gboolean GtkDrawing::window_draw_cb (GtkWidget * widget, cairo_t * cr, cairo_surface_t* m_bgImage)
{
gint root_width,root_height;
cairo_set_source_surface (cr,m_bgImage, 0, 0);
gtk_window_get_size (GTK_WINDOW(widget), &root_width, &root_height);
cairo_rectangle (cr, 0, 0,root_width, root_height);
cairo_fill (cr);
//Enable Below code to draw child widget after background rendering
//gtk_widget_draw (childwidget, cr);
//cairo_fill (cr);
return TRUE;
}
cairo_surface_t* m_bgImage=cairo_image_surface_create_from_png("/home/my_background_image.png");
g_signal_connect (G_OBJECT (window), "draw", G_CALLBACK(GtkDrawing::window_draw_cb), m_bgImage);
In the above handler function, to stretch the image you can modify cairo_rectangle parameters.