How do I make the gtk code work? - c

vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
...
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
...
The above code will generate the warning below:
Gtk-WARNING **: Attempting to add a
widget with type GtkFixed to a
GtkWindow, but as a GtkBin subclass a
GtkWindow can only contain one widget
at a time; it already contains a
widget of type GtkVBox
Which results in frame is not shown in the window.
How can I make both vbox and frame show?

Put them both in a surrounding vbox (if you want to stack them vertically, that is):
parentVbox = gtk_vbox_new(FALSE, 0);
vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(parentVbox), vbox, TRUE, TRUE, 0);
...
frame = gtk_fixed_new();
gtk_box_pack_start(GTK_BOX(parentVbox), frame, TRUE, 0);
...
gtk_container_add(GTK_CONTAINER(window), parentVbox);

The error you are having is because every widget can only contain one widget (you want vbox and frame to be contained by window), even though the widget contained may be a complex one with several other widgets contained.
To put a widget inside a box you have to use gtk_box_pack_start ().
Gnome Reference Manual link:
http://library.gnome.org/devel/gtk/stable/GtkBox.html#gtk-box-pack-start
vbox = gtk_vbox_new(FALSE, 0);
frame = gtk_fixed_new();
gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0); //the frame goes inside vbox
...
gtk_container_add(GTK_CONTAINER(window), vbox); //the vbox is contained by window

Related

How to use images in GTK Stack Switcher using C

I'm making application in gtk using C. I have a GtkStack with GtkStackSwitcher and I don't know how to set images/icons to buttons in stack switcher. I had similar problem with application in gtkmm and C++ but I was able to find required function in the documentation. This time, after searching the documentation for GtkStack, GtkStackSwitcher and GtkContainer, I didn't find anything useful in GtkStack and GtkStackSwitcher. In GtkContainer there is function gtk_container_child_set_property (). It may be the function I'm looking for but I have no idea how to put an icon-name into GValue and if it's possible.
To sum up - can I set icon to GtkStackSwitcher's button with mentioned functions or using any other method?
Edit:
Maybe it's possible to achieve this with css? Setting background-image for GtkStack and GtkStackSwticher doesn't work but setting background_image for buttons works. Works very bad but works. The image doesn't fit the button and button doesn't resize to be the image size (If i set button new from pixbuf the button does resize). So is it possible with css or is it a dead end?
From the GtkStack documentation, at Child Properties, you can see the property "icon-name":
The “icon-name” child property
“icon-name” gchar *
The icon name of the child page.
Flags: Read / Write
Default value: NULL
As you pointed out, we can use gtk_container_child_set_property on the GtkStack (a GtkContainer) and set the icon. The problem is that the stack uses the icon or the title, not both.
Here is a simple example in C code:
#include <gtk/gtk.h>
int main (int argc, char** argv) {
GtkBox *box;
GtkStack *stack;
GtkLabel *label1;
GtkLabel *label2;
GtkWindow *window;
GtkStackSwitcher *switcher;
GValue iconval1 = G_VALUE_INIT;
GValue iconval2 = G_VALUE_INIT;
gtk_init (&argc, &argv);
g_value_init (&iconval1, G_TYPE_STRING);
g_value_init (&iconval2, G_TYPE_STRING);
window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
box = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 6));
stack = GTK_STACK(gtk_stack_new ());
switcher = GTK_STACK_SWITCHER(gtk_stack_switcher_new ());
label1 = GTK_LABEL(gtk_label_new("Stack Page 1"));
label2 = GTK_LABEL(gtk_label_new("Stack Page 2"));
gtk_stack_add_titled(stack, GTK_WIDGET(label1), "Page 1", "Page 1");
gtk_stack_add_titled(stack, GTK_WIDGET(label2), "Page 2", "Page 2");
gtk_widget_set_halign (GTK_WIDGET(switcher), GTK_ALIGN_CENTER);
g_value_set_string(&iconval1, "zoom-in-symbolic.symbolic");
g_value_set_string(&iconval2, "zoom-out-symbolic.symbolic");
gtk_container_child_set_property(GTK_CONTAINER(stack), GTK_WIDGET(label1), "icon-name", &iconval1);
gtk_container_child_set_property(GTK_CONTAINER(stack), GTK_WIDGET(label2), "icon-name", &iconval2);
gtk_stack_switcher_set_stack (switcher, stack);
gtk_box_pack_start (box, GTK_WIDGET(switcher), FALSE, FALSE, 6);
gtk_box_pack_start (box, GTK_WIDGET(stack), TRUE, TRUE, 6);
gtk_container_add (GTK_CONTAINER(window), GTK_WIDGET(box));
g_signal_connect(G_OBJECT(window), "destroy", gtk_main_quit, NULL);
gtk_widget_show_all (GTK_WIDGET(window));
gtk_main ();
return 0;
}
Compile it with:
gcc -o test main.c `pkg-config --cflags --libs gtk+-3.0`
and the result should be:
EDIT:
As requested in the comments:
Can you tell me also how to change icon sizes of stack switcher icons?
I see that stack switcher has property "icon-size"...
GtkStackSwitcher has the property "icon-size" but it was introduced in Gtk+ 3.20. So, in order to use this property there is this requirement.
To set a property to which Gtk+ does not provide a setter/getter you should use g_object_set (or set_full).
Using the code above:
...
switcher = GTK_STACK_SWITCHER(gtk_stack_switcher_new ());
g_object_set(G_OBJECT(switcher), "icon-size", GTK_ICON_SIZE_LARGE_TOOLBAR, NULL);
label1 = GTK_LABEL(gtk_label_new("Stack Page 1"));
...
The property is a gint value so you can try out some values and verify the size. There is also a enumerated type containing default sizes for icons, it's GtkIconSize. In the example i've used GTK_ICON_SIZE_LARGE_TOOLBAR (24px).

GTK+ Threading with OpenGL's GLUT/FreeGLUT

I am fairly new to C/C++ programming, so please excuse me if something sounds stupid to you...
I am currently working on a Visual Studio C++ 2010 project that involves opengl, glut/freeglut, and gtk+.
The OpenGL is used to take an input image, manipulate its pixels based on various parameters before calling glDrawPixels() to draw the pixels from buffer for display in a glut/freeglut window.
Now I am trying to make a UI so user can adjust the parameters and see the resulting image at runtime.
What I have so far is one GTK window that contains all of the GTK widgets, and a glut/freeglut window that houses the OpenGL image. How it works is that when the button on the UI is clicked, a new GTK thread is created to display the glut/freeglut window (that contains the OpenGL rendered image.
My question is, how do I make glut/freeglut to render updated OpenGL image and display it in the same glut/freeglut window? I am not sure if it has to do with GTK threading or it's just glut/freeglut, or both. Does anyone have any advice?
Here's the stripped down code I have:
#include<freeglut.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <gtk/gtk.h>
GtkWidget *window;
GtkWidget *vbox, *hbox;
GtkWidget *button;
gint tmp_pos;
GThread *thread;
gint progress = 0;
G_LOCK_DEFINE_STATIC(progress);
GError *error = NULL;
void display()
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glRasterPos2i(x_offset,y_offset);
glDrawPixels(x2-x1+1,y2-y1+1,GL_LUMINANCE,GL_UNSIGNED_BYTE,tmp_buf);
glFlush();
}
static gpointer runGL(gpointer data)
{
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
if(Running_Mode==0)
glutInitWindowSize(3*Test_Size,Test_Size); /* 500 x 500 pixel window */
else
glutInitWindowSize(Test_Size,Test_Size); /* 500 x 500 pixel window */
glutInitWindowPosition(600,0); /* place window top left on display */
glutCreateWindow("Simulation"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMainLoop(); /* enter event loop */
return (NULL);
}
/* create a g_thread for glut window */
void GLThread()
{
thread = g_thread_create(runGL, (gpointer)button, TRUE, &error);
g_print("Thread created");
if(!thread)
{
g_print("Error: %s\n", error->message);
return(-1);
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
if(! g_thread_supported())
g_thread_init( NULL );
gdk_threads_init();
/* Obtain gtk's global lock */
gdk_threads_enter();
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (GTK_WIDGET (window), 200, 100);
gtk_window_set_title (GTK_WINDOW (window), "GTK Entry");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect_swapped (window, "delete-event",
G_CALLBACK (gtk_widget_destroy),
window);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
hbox = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
/* call the GLThread function when the button is clicked */
g_signal_connect_swapped (button, "clicked", G_CALLBACK (GLThread), window);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
gtk_widget_set_can_default (button, TRUE);
gtk_widget_show (button);
gtk_widget_show(window);
gtk_main();
gdk_threads_leave();
}
First things first: Free-/GLUT is not a part of OpenGL. It's a 3rd party library with a similar scope (just much more simpler) like GTK+. If you're using GTK+ you don't need GLUT. Instead you should use the GTK+ OpenGL Widget provided by GTKGlExt.
I am currently working on a Visual Studio C++ 2010 project that involves opengl, glut/freeglut, and gtk+.
Why would you want to mix GLUT and GTK+? There's absolutely no sane reason to do this. GLUT is a simple application framework (creates windows, provides the event loop), GTK+ is an application framework (creates windows, provides the event loop, has widgets).
Choose one, don't mix them.
I am not sure if it has to do with GTK threading or it's just glut/freeglut, or both. Does anyone have any advice?
The problem is, that you have two libraries fighting to do the same thing (processing user input events).

How to center a dialog window on the main window in GTK?

I want to print small warning messages. To achieve that, I'm creating a new window and I want it to be centered on the main window. So far I've tried setting its parent with gtk_widget_set_parent, using GTK_WINDOW_POPUP and GTK_WINDOW_TOPLEVEL but nothing worked.
How to center the window on the main window?
Here's the code for the function:
static void pop_warning(char *title, char *text)
{
GtkWidget *warning_window;
GtkWidget *box;
GtkWidget *label;
GtkWidget *button;
warning_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(warning_window), title);
g_signal_connect(GTK_WINDOW(warning_window), "destroy", G_CALLBACK(gtk_widget_destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(warning_window), 20);
gtk_window_set_resizable(GTK_WINDOW(warning_window), FALSE);
//window is the main window
gtk_widget_set_parent(warning_window, window);
gtk_window_set_position(GTK_WINDOW(warning_window), GTK_WIN_POS_CENTER_ON_PARENT);
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER(warning_window), box);
label = gtk_label_new(text);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, FALSE, 0);
button = gtk_button_new_with_label("Ok");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), warning_window);
gtk_box_pack_start(GTK_BOX(box), button, TRUE, FALSE, 0);
gtk_widget_show_all(warning_window);
}
gtk_window_set_position(GTK_WINDOW(warning_window), GTK_WIN_POS_CENTER_ON_PARENT);
only works if you first call
gtk_window_set_transient_for(GtkWindow *window, GtkWindow *parent);
as mentioned explicitly in the documentation.
You could also try to use GTK_WIN_POS_CENTER_ALWAYS that has not the above mentioned constrain - at least not according the docs.

C Gtk Issue: Adding a VBox to the Main Window

I'm writing a C game using the GTK library for the interface. The idea is to have two main containers: one for holding the buttons and other widgets for letting the user choose settings, and the other for displaying and moving the images during the actual gameplay. I'm using a VBox to hold the menu widgets, and a Fixed container to hold the game sprites. I have here all of the lines of code relating to the VBox and Fixed containers in the main() method:
GtkWidget* vbox;
GtkWidget* fixed;
...
int main(int argc, char** argv) {
// (The rest of this code block is in the main method)
...
// Make a vertical box for the menu widgets.
vbox = gtk_vbox_new(TRUE, 0);
// Add the menu widgets to the vbox.
gtk_box_pack_start(GTK_BOX(vbox), label1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button2, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
// Make a new fixed container, which allows its children
// widgets to be moved dynamically.
fixed = gtk_fixed_new();
// Add the game widgets to the fixed container.
gtk_fixed_put(GTK_FIXED(fixed), player1, x, y);
gtk_fixed_put(GTK_FIXED(fixed), player2, x, y + 40);
gtk_fixed_put(GTK_FIXED(fixed), ball, x + 80, y);
gtk_fixed_put(GTK_FIXED(fixed), wall, x + 120, y);
// Add the fixed container to the window.
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show(vbox);
gtk_widget_show(fixed);
In one of my buttons' actions, I have the following, to initiate gameplay:
gtk_container_remove(GTK_CONTAINER(window), vbox);
gtk_container_add(GTK_CONTAINER(window), fixed);
Which works without any issue. However, after a certain condition is met, I have another function called, which does the opposite:
gtk_container_remove(GTK_CONTAINER(window), fixed);
gtk_container_add(GTK_CONTAINER(window), vbox);
This causes the following issue:
(a.out:11762): Gtk-CRITICAL **: IA__gtk_container_add: assertion `GTK_IS_WIDGET (widget)' failed
I have looked at the widget hierarchy, and verified that VBox falls under the category of GtkWidget. The above error did not occur when I initially added the VBox to the window in the main() method. Is there something I'm not aware of when adding this container to the window after gtk_main() has been called?
The problem is taht GtkWidgets are reference counted but you don't own any reference. This is usual for GtkWidgets, because as long as the widget is visible the window system holds one reference to them. But as long as you remove the widget from its container, the reference count drops to zero and it is destroyed.
The first time it works because there is a floating reference, that exist just after the widget is first created.
Your easiest solution is just to hold a reference to your two moving widgets: call g_object_ref_sink just after creating them to convert the floating reference to a real one. But don't forget to call g_object_unref when you are done with them!
Other option would be to just increment/decrement the counter while you are moving them around, but you still need to sink the reference to fixed to work as expected:
g_object_ref_sink(fixed);
Then:
g_object_ref(vbox);
gtk_container_remove(GTK_CONTAINER(window), vbox);
gtk_container_add(GTK_CONTAINER(window), fixed);
g_object_unref(fixed);
And:
g_object_ref(fixed);
gtk_container_remove(GTK_CONTAINER(window), fixed);
gtk_container_add(GTK_CONTAINER(window), vbox);
g_object_unref(vbox);

GTK and scrolling text view

This is what I have so far
GtkWidget* createConsoleBox()
{
GtkWidget* textArea = gtk_text_view_new();
GtkWidget* scrollbar = gtk_vscrollbar_new(gtk_text_view_get_vadjustment(GTK_TEXT_VIEW(textArea)));
GtkWidget* textEntry = gtk_entry_new();
GtkWidget* console = gtk_table_new(3, 2, FALSE);
gtk_table_attach_defaults(GTK_TABLE(console), textArea, 0, 1, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(console), scrollbar, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 2, 1, 2);
return console;
}
I want the text view to be scrollable as the text begins to fill the box, but the box keeps on expanding to accommodate more text. How to do I limit the size of the text view and create a scrollable text view.
Thanks in advance :-)
I'm afraid you've misunderstood how scrollbars work in GTK; usually you don't create a scrollbar directly, but you place the widget you would like to scroll in a GtkScrolledWindow. This creates scrollbars automatically and connects them to the widget inside the scrolled window; in your case, the text view.
Here's what your createConsoleBox() function should look like:
GtkWidget* createConsoleBox()
{
GtkWidget* textArea = gtk_text_view_new();
GtkWidget* scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
GtkWidget* textEntry = gtk_entry_new();
GtkWidget* console = gtk_table_new(3, 1, FALSE);
gtk_container_add(GTK_CONTAINER(scrolledwindow), textArea);
gtk_table_attach_defaults(GTK_TABLE(console), scrolledwindow, 0, 1, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 1, 1, 2);
return console;
}
What you experience is the result of the widget asking more space to its parent container.
Unless the parent container has some rules forbidding the expansion, it will give as much space as child widget asks.
A common way to avoid this is to set a given size for the child widget with gtk_widget_set_size_request(), followed by some way to make sure the parent can give shrink or grow, depending on the parent properties.
This sample code show one way to accomplish this.
#include <gtk/gtk.h>
GtkWidget* createConsoleBox()
{
GtkWidget* textArea = gtk_text_view_new();
GtkWidget* scrollbar= gtk_vscrollbar_new(gtk_text_view_get_vadjustment(GTK_TEXT_VIEW(textArea)));
GtkWidget* textEntry = gtk_entry_new();
GtkWidget* console = gtk_table_new(3, 2, FALSE);
gtk_table_attach_defaults(GTK_TABLE(console), textArea, 0, 1, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(console), scrollbar, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(console), textEntry, 0, 2, 1, 2);
//This code sets the preferred size for the widget, so it does not ask for extra space
gtk_widget_set_size_request(textArea, 320, 240);
return console;
}
int main(int argc,char* argv[]){
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Simple Sample");
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);// does not matter this size
gtk_container_add(GTK_CONTAINER(window), createConsoleBox());
gtk_widget_show_all(window);
gtk_window_set_resizable(GTK_WINDOW(window),FALSE);//because of this
gtk_main();
return 0;
}
gtk_window_set_resizable() is meant to make the window un-resizeable by the user (the App can still resize it), but has the extra propertie of tighting up the window to the size of its child widget. Each GtkContainer has it way of setting up expansion, tighness, etc. Is only matter of experimentation to find the right one for your needs.
If the window resizable property had been set to TRUE, the textarea would still have the given size for it, the container would just put a lot of extra space between the individual widgets inside the GtkTable.

Resources