I am pretty new to GTK and I want to know how to resize the entry boxes size and and the spacing between label and boxes ?
Also, how to receive the input value from the entry box for further usage you know, like the C function "scanf". Thanks you and sorry for my bad English
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static void destroy(GtkWidget *widget, gpointer data){
gtk_main_quit ();
}
static void initialize_window(GtkWidget* window) {
gtk_window_set_title(GTK_WINDOW(window),"My Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
}
int main (int argc, char *argv[]){
GtkWidget *window,*table,*label,*entry, *entry1, *label1, *label3, *label2, *entry2;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
initialize_window(window);
table = gtk_table_new(4, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
label = gtk_label_new ("Circle with standard formula:(x-a)^2 + (y-b)^2 =r*r");
gtk_misc_set_alignment (GTK_MISC (label), 70, 70);
gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
entry = gtk_entry_new ();
gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 1, 2);
label1 = gtk_label_new ("Input value of a:");
gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
gtk_table_attach_defaults(GTK_TABLE (table), label1, 0, 1, 1, 2);
entry1 = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table), entry1, 1, 2, 2, 3);
label2=gtk_label_new("Input the value of b:");
gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
gtk_table_attach_defaults(GTK_TABLE (table), label2, 0, 1, 2, 3);
label3=gtk_label_new("Input the value of r:");
gtk_table_set_homogeneous(GTK_TABLE(table), TRUE);
gtk_table_attach_defaults(GTK_TABLE (table), label3, 0, 1, 3, 4);
entry2 = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table), entry2, 1, 2, 3, 4);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
In order to get the text within one of your entry boxes use the gtk_entry_get_text function found in more detail here: https://developer.gnome.org/gtk3/stable/GtkEntry.html#gtk-entry-get-text
For instance:
buffer = gtk_entry_get_text(&entry1);
As far as changing spacing between elements goes the Gtk documentation notes that if you do not need individual row spacing then GtkGrid is preferred as it appears many of the table functions are deprecated since Gtk 3.4.
To set the spacing between rows use gtk_table_set_row_spacing found here: https://developer.gnome.org/gtk3/stable/GtkTable.html#gtk-table-set-row-spacing
and just below that is listed the information on gtk_table_set_col_spacing.
Setting the padding within table cells is a bit different and would be controlled using x-padding and y-padding properties of the child elements, more information on this is listed at the bottom of the GtkTable page.
Related
this code show me on xubuntu 21.04 form with size (400px width + 400px height) and with ONE button.
How can I add to this code two buttons?
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data) {
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_present(GTK_WINDOW(window));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
// Create a new button
GtkWidget *button = gtk_button_new_with_label ("press 123");
gtk_window_set_child (GTK_WINDOW (window), button);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
}
int main(int argc, char *argv[]) {
g_autoptr(GtkApplication) app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
In GTK4, I have found that a window (GtkWindow or GtkApplicationWindow) can only have one child. So to include multiple widgets within a window (such as a label and three buttons) one usually has to first create a grid object (GtkGrid), place the widgets within the grid at specified rows and columns, and then set the grid as the child of the window. Using your sample code above, I revised the code to look like the following:
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data)
{
GtkWidget *window = gtk_application_window_new(app);
GtkWidget *grid = gtk_grid_new();
GtkWidget *label = gtk_label_new("Hello World");
//gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
gtk_grid_set_column_spacing(GTK_GRID(grid),10);
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
// Create a new button
GtkWidget *button1 = gtk_button_new_with_label ("Press 1");
GtkWidget *button2 = gtk_button_new_with_label ("Press 2");
GtkWidget *button3 = gtk_button_new_with_label ("Press 3");
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 3, 1);
gtk_grid_attach(GTK_GRID(grid), button1, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button2, 1, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button3, 2, 1, 1, 1);
gtk_window_set_child (GTK_WINDOW (window), grid);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button3, "clicked", G_CALLBACK (gtk_window_close),
window);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char *argv[])
{
g_autoptr(GtkApplication) app = gtk_application_new(NULL,
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
This results in establishing a window with the two additional buttons you wanted. I did not know what type of functions those buttons should trigger, so for my revisions to your sample code, I just hooked the two additional buttons to the same closure signal. Following is a sample of the window when I run the program.
I hope that helps you out.
Regards.
Additional Notes:
Regarding the request as how to add "label2" and then have that label's text updated to "Network Connections", the following additions to your sample program could provide a way to do this.
First, a callback function for updating the new label's text would be added to the program (usually at the beginning of the program).
void on_button1_clicked (GtkLabel *lbl)
{
gtk_label_set_text(lbl, "Network Connections");
}
Then, within the activation function, the new label would be defined.
GtkWidget *label2 = gtk_label_new("");
Next, the new label widget would be added to the grid (in this example, it was added next to the "label" widget in the first row).
gtk_grid_attach(GTK_GRID(grid), label2, 1, 0, 2, 1);
Finally, since the request in the comment was to have the label's text updated to "Network Connections", the signal connection for the first button would be revised to call the new "on_button1_clicked" callback function and passing the "label2" widget instead of the "window" widget.
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (on_button1_clicked), label2);
The result should net the desired behavior.
Hopefully, that addresses your comment.
Regards.
so i am currently trying to achieve the following: Create a gui for the user were the user can see a livefeed of the webcam and also have two buttons left of the livefeed to start and stop the recording. all this has to run on a raspberry pi.
I am using a basler camera so i can not use the videocapture object but using the pylon sdk (sdk for basler cameras) i managed to grab an image and convert it into a opencv::mat.
however im stuck on two points:
first i do not know how to display an opencv::mat on a gtk widget at all.
the second problem is that i do not know how to get a live stream. I tried to use the g_timeout_add function but then the ui gets unresponsive. it calls the function in background but since i call it every 100ms it seems to be stuck forever in the function and therefore i can not click on any buttons.
i have included some of my code below.
#include <gtk/gtk.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include <pylon/PylonIncludes.h>
using namespace Pylon;
using namespace cv;
using namespace std;
void callback(GtkWidget *widget, gpointer labelstatus) {}
gint delete_event(GtkWidget *widget, GdkEvent *event) {
gtk_main_quit ();
}
static gboolean livefeed_func(gpointer data) {
Pylon::PylonAutoInitTerm autoInitTerm;
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
camera.Open();
GenApi::INodeMap& nodemap = camera.GetNodeMap();
.
. Some other pylon related stuff
.
CImageFormatConverter formatConverter;
formatConverter.OutputPixelFormat= PixelType_BGR8packed;
CPylonImage pylonImage;
Mat openCvImage;
cv::Size frameSize = Size((int)width->GetValue(), (int)height->GetValue());
.
. Some other pylon related stuff
.
formatConverter.Convert(pylonImage, ptrGrabResult);
openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());
imwrite("tmp_name.jpg", openCvImage);
}
int main(int argc, char *argv[]) {
//GTK
GtkWidget *window;
GtkWidget *table;
GtkWidget *labelstatus;
GtkWidget *labellog;
GtkWidget *buttonexit;
GtkWidget *buttonstart;
GtkWidget *buttonstop;
GtkWidget *livefeed;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Table");
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (delete_event), NULL);
table = gtk_table_new (6, 6, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
labelstatus = gtk_label_new ("Status: Idle");
gtk_table_attach_defaults (GTK_TABLE(table), labelstatus, 0, 2, 0, 1);
gtk_widget_show (labelstatus);
buttonstart = gtk_button_new_with_label ("Start");
gtk_signal_connect (GTK_OBJECT (buttonstart), "clicked", GTK_SIGNAL_FUNC (callback), (gpointer) labelstatus);
gtk_table_attach_defaults (GTK_TABLE(table), buttonstart, 0, 1, 1, 2);
gtk_widget_show (buttonstart);
buttonstop = gtk_button_new_with_label ("Stop");
gtk_signal_connect (GTK_OBJECT (buttonstop), "clicked", GTK_SIGNAL_FUNC (callback), NULL);
gtk_table_attach_defaults (GTK_TABLE(table), buttonstop, 1, 2, 1, 2);
gtk_widget_show (buttonstop);
labellog = gtk_label_new ("11:31:54: Started Recording\n11:36:54: Stopped Recording");
gtk_table_attach_defaults (GTK_TABLE(table), labellog, 0, 2, 2, 3);
gtk_widget_show (labellog);
buttonexit = gtk_button_new_with_label ("Quit");
gtk_signal_connect (GTK_OBJECT (buttonexit), "clicked",GTK_SIGNAL_FUNC (delete_event), NULL);
gtk_table_attach_defaults (GTK_TABLE(table), buttonexit, 0, 2, 3, 4);
gtk_widget_show (buttonexit);
livefeed = gtk_drawing_area_new();
gtk_table_attach_defaults (GTK_TABLE(table), livefeed, 2, 5, 0, 4);
gtk_widget_show (livefeed);
gtk_widget_show (table);
gtk_widget_show (window);
g_timeout_add(100, livefeed_func, livefeed);
gtk_main ();
}
so if i run this code it will display the gtk window and then be unresponsive and write images until i cancel the process.
So to sum it up:
is there a way to execute background tasks in gtk without the ui becoming unresponsive
how can i display an opencv::Mat in a gtk window/widget?
int main(int argc, char **argv)
{
GtkWidget *pWindow;
GtkWidget *pVBox;
GtkWidget *pEntry;
GtkWidget *pButton;
GtkWidget *pLabel;
GtkWidget *text_view;
GtkWidget *scrollbar;
gtk_init(&argc, &argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pWindow), "My IRC");
gtk_window_set_default_size(GTK_WINDOW(pWindow), 800, 600);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
pVBox = gtk_vbox_new(TRUE, 0);
pEntry = gtk_entry_new();
pLabel = gtk_label_new(NULL);
text_view = gtk_text_new(NULL, NULL);
scrollbar = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
gtk_box_pack_start(GTK_BOX(pVBox), scrollbar, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(pVBox), pEntry, TRUE, FALSE, 0);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), text_view);
g_signal_connect(G_OBJECT(pEntry), "activate", G_CALLBACK(on_activate_entry), (GtkWidget*) text_view);
gtk_widget_show_all(pWindow);
gtk_main();
return EXIT_SUCCESS;
}
I want to make the text_view box bigger than the other one. I couldn't find any solutions in the GTK documentation.
PS: It's GTK 2.0.
The two boolean arguments of gtk_box_pack_start() are expand and fill. When expand is true, the widget gets extra space after allocating other widgets. When fill is true, the widget is resized to fill that space. So what you want to do instead is
// expand AND fill - fills all available space
gtk_box_pack_start(GTK_BOX(pVBox), scrollbar, TRUE, TRUE, 0);
// NO expand AND NO fill - only uses what it needs
gtk_box_pack_start(GTK_BOX(pVBox), pEntry, FALSE, FALSE, 0);
Here's a page with more information. Note that you can use the [hv](expand|align) properties with boxes as well as grids (and use gtk_container_add(), which acts like gtk_box_pack_start()).
I am trying to write the interface for my music manager using GTK+. The program was compiled successfully. However, when I executed it, the machine returned errors:
(dingo_draft:6462): Gtk-WARNING **: Can't set a parent on widget which has a parent
(dingo_draft:6462): Gtk-CRITICAL **: gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed
**
Gtk:ERROR:/build/buildd/gtk+2.0-2.20.1/gtk/gtkwidget.c:8760:gtk_widget_real_map: assertion failed: (gtk_widget_get_realized (widget))
Aborted
Here is the source code of the program. Please note that this is just the interface written in GTK+. I did not add any signals in yet. I think there might be some problems with the GTK+ functions, but I could not locate where the errors occured:
/* This is the interface design draft for the media player
/* This does not include the signals for widgets. Just a plain draft */
#include <gtk/gtk.h>
#include <gst/gst.h>
int main(int argc, char *argv[]) {
/* Initialize gtk+ & gstreamer */
gtk_init(&argc, &argv);
gst_init(&argc, &argv);
/* Create mainwindow (mainwindow) */
GtkWidget *mainwindow;
mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(mainwindow), "Music Manager");
/* Create the vbox containing searchbox & song list (treevbox) */
GtkWidget *searchbox, *treesong, *scrollsong, *treevbox;
/* GtkWidget *colname, *coltime; */
GtkCellRenderer *namerender, *timerender;
treesong = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treesong), FALSE);
namerender = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treesong), -1, "Songs", namerender, "text", 0, NULL);
timerender = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treesong), -1, "Time", timerender, "text", 1, NULL);
scrollsong = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollsong), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scrollsong), treesong);
searchbox = gtk_entry_new();
treevbox = gtk_vbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(treevbox), searchbox);
gtk_box_pack_start_defaults(GTK_BOX(treevbox), scrollsong);
/* Create the song info section (infohbox) */
GtkWidget *songname, *songinfo, *coverart;
GtkWidget *infohbox, *imagevbox;
coverart = gtk_image_new_from_file("music-notes.png");
songname = gtk_label_new("<i>Song Name</i>");
songinfo = gtk_label_new("<b>Artist:</b> \n <b>Track</b> \n <b>Album</b> \n <b>Year</b> \n <b>Genre</b> \n <b>Rating</b>");
infohbox = gtk_hbox_new(TRUE, 0);
imagevbox = gtk_vbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(imagevbox), coverart);
gtk_box_pack_start_defaults(GTK_BOX(imagevbox), songname);
gtk_box_pack_start_defaults(GTK_BOX(infohbox), imagevbox);
gtk_box_pack_start_defaults(GTK_BOX(infohbox), songinfo);
/* Create drawing area for video display (previewarea) */
GtkWidget *previewarea;
previewarea = gtk_drawing_area_new();
gtk_widget_set_size_request(previewarea, 300, 200);
/* Create actions tree view (ltreeview) */
enum {
COL_ICON = 0,
COL_ACTION,
NUM_COLS
};
GtkCellRenderer *lrenderer;
GtkTreeModel *lmodel;
GtkWidget *ltreeview;
GtkListStore *lliststore;
GtkTreeIter liter;
ltreeview = gtk_tree_view_new();
lrenderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ltreeview), -1, "Icon", lrenderer, "pixbuf", COL_ICON, NULL);
lrenderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ltreeview), -1, "Actions", lrenderer, "text", COL_ACTION, NULL);
lliststore = gtk_list_store_new(NUM_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING);
gtk_list_store_append(lliststore, &liter);
gtk_list_store_set(lliststore, &liter, COL_ICON, gdk_pixbuf_new_from_file("now-playing.png", NULL), COL_ACTION, "Now Playing", -1);
gtk_list_store_append(lliststore, &liter);
gtk_list_store_set(lliststore, &liter, COL_ICON, gdk_pixbuf_new_from_file("music.png", NULL), COL_ACTION, "Music", -1);
gtk_list_store_append(lliststore, &liter);
gtk_list_store_set(lliststore, &liter, COL_ICON, gdk_pixbuf_new_from_file("video.png", NULL), COL_ACTION, "Videos", -1);
gtk_list_store_append(lliststore, &liter);
gtk_list_store_set(lliststore, &liter, COL_ICON, gdk_pixbuf_new_from_file("playlist.png", NULL), COL_ACTION, "Playlists", -1);
lmodel = GTK_TREE_MODEL(lliststore);
gtk_tree_view_set_model(GTK_TREE_VIEW(ltreeview), lmodel);
/* g_object_unref(lmodel); */
/* Create the top control bar (controlhbox) */
GtkWidget *prevbutton, *nextbutton, *hscale, *cursong;
GtkWidget *curpos, *duration, *volumebutton, *playbutton;
GtkAdjustment *progress, *volumeadj;
GtkWidget *buttonhbox, *proghbox, *infovbox, *controlhbox;
prevbutton = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(prevbutton), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS, GTK_ICON_SIZE_SMALL_TOOLBAR));
playbutton = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(playbutton), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_DND));
nextbutton = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(nextbutton), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_SMALL_TOOLBAR));
progress = GTK_ADJUSTMENT(gtk_adjustment_new(0.00, 0.00, 0.00, 0.00, 0.00, 0.00));
hscale = gtk_hscale_new(progress);
gtk_scale_set_draw_value(GTK_SCALE(hscale), FALSE);
gtk_widget_set_size_request(hscale, 500, NULL);
cursong = gtk_label_new("Song's Name");
curpos = gtk_label_new("0:00");
duration = gtk_label_new("0:00");
volumebutton = gtk_volume_button_new();
volumeadj = GTK_ADJUSTMENT(gtk_adjustment_new(0.70, 0.00, 1.00, 0.01, 0.00, 0.00));
gtk_scale_button_set_adjustment(GTK_SCALE_BUTTON(volumebutton), volumeadj);
gtk_widget_set_size_request(volumebutton, 32, 32);
buttonhbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(buttonhbox), prevbutton);
gtk_box_pack_start_defaults(GTK_BOX(buttonhbox), playbutton);
gtk_box_pack_start_defaults(GTK_BOX(buttonhbox), nextbutton);
proghbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(proghbox), curpos);
gtk_box_pack_start_defaults(GTK_BOX(proghbox), hscale);
gtk_box_pack_start_defaults(GTK_BOX(proghbox), duration);
controlhbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(controlhbox), buttonhbox);
gtk_box_pack_start_defaults(GTK_BOX(controlhbox), proghbox);
gtk_box_pack_start_defaults(GTK_BOX(controlhbox), volumebutton);
/* Create panes (tophpaned) and pack widgets into this (tophpaned)*/
GtkWidget *tophpaned, *subhpaned, *vpaned;
vpaned = gtk_vpaned_new();
gtk_paned_add1(GTK_PANED(vpaned), songinfo);
gtk_paned_add2(GTK_PANED(vpaned), previewarea);
subhpaned = gtk_hpaned_new();
gtk_paned_pack1(GTK_PANED(subhpaned), vpaned, TRUE, TRUE);
gtk_paned_pack2(GTK_PANED(subhpaned), treevbox, TRUE, TRUE);
tophpaned = gtk_hpaned_new();
gtk_paned_pack1(GTK_PANED(tophpaned), ltreeview, TRUE, TRUE);
gtk_paned_pack2(GTK_PANED(tophpaned), subhpaned, TRUE, TRUE);
/* Create an topvbox to pack all the above stuffs in, */
/* then add topvbox to window */
GtkWidget *topvbox;
topvbox = gtk_vbox_new(TRUE, 0);
gtk_box_pack_start_defaults(GTK_BOX(topvbox), controlhbox);
gtk_box_pack_start_defaults(GTK_BOX(topvbox), tophpaned);
gtk_container_add(GTK_CONTAINER(mainwindow), topvbox);
/* Show all widgets & draw the interface */
gtk_widget_show_all(mainwindow);
gtk_main();
return 0;
}
Thank you for answering my question! I appreciate your helps!
You are trying to put songinfo into two different containers: into infohbox and into vpaned.
As Havoc says, gdb can help you find the exact code that is giving you a warning. I can offer two additional suggestions:
Build your interface in Glade. This will help you to see what's going on with it as you build it. It will also make it more maintainable, and help you avoid these kinds of errors.
Before you ask a question on Stack Overflow with a big code dump, try and make a minimal program that reproduces the problem. Often you will have solved the problem yourself by the time you have done this.
Run in a debugger (gdb), set a breakpoint on the first warning message (used to be g_logv that all warnings went through, but check glib
source if needed), then type "bt" in gdb when it breaks to see which line caused the warning.
Another approach is to set G_DEBUG=fatal-warnings in the environment, then again use gdb, no need for a breakpoint just backtrace when the app crashes on the warning.
Another possible step is to run in Valgrind and fix its complaints if you have bad memory accesses or writes.
I'm trying to get the input text from a text box in a callback function when the user changes something it it (on "changed").
The code goes as follow:
#include <stdio.h>
#include <gtk/gtk.h>
void enter_callback( GtkWidget *widget, GtkEditable *buffer)
{
printf("%s",gtk_editable_get_chars(buffer, 0, -1));
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *text;
GtkWidget *table;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
table = gtk_table_new (2, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
text=gtk_text_new(NULL, NULL);
gtk_text_set_editable(text, TRUE);
gtk_signal_connect(GTK_OBJECT(text), "changed", GTK_SIGNAL_FUNC(enter_callback), (GtkEditable*)text);
gtk_table_attach_defaults(GTK_TABLE(table), text, 0, 1, 0, 1);
gtk_container_border_width (GTK_CONTAINER (window), 40);
gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);
gtk_widget_show(text);
gtk_widget_show(window);
gtk_widget_show(table);
gtk_main();
return 0;
}
The code compiles just right, I'm compiling it on Code::Blocks on debug, checking output on the console by printf. The problem is I get <NULL> as a callback everytime I change something on the textbox. How can I get the correct output?
SOLUTION:
As noted by Washu, gtk_text is deprecated and gtk_text_view should be used instead.
According to the GTK documentation, GtkText is deprecated, buggy, and should not be used. You should instead be using the GtkTextView widget via gtk_text_view_new.
You can use GtkEntry widget too. And use gtk_entry_get_text () (which return const gchar * value) that to get text from GtkEntry, for instance.