gtk_container_add function crashes - c

I have the following line in my GTK code:
gtk_container_add(GTK_CONTAINER(main_wind_ob->stack),
GTK_WIDGET(main_wind_ob->product_ob->swindow));
Where main_wind_on->stack is a GtkStack acquired this way
main_wind_ob->stack = GTK_STACK(
gtk_builder_get_object (
main_wind_ob->builder,
"stack"
)
);
Where main_wind_ob->builder is a GtkBuilder object associated with a .ui file where a GtkStack container is declared with the ID stack.
main_wind_ob->product_ob->swindow is a GtkScrolledWindow acquired like this:
main_wind_ob->product_ob->swindow =
gtk_scrolled_window_new(NULL, NULL);
I know neither main_wind_ob->product_ob->swindow or main_wind_ob->stack are not null as I've debugged and checked them.
What I wanted to do was make swindow be the child of the stack, but it just crashes when it reaches the gtk_container_add function call. I am not really sure why. Let me know if there is any extra details I can add to the post, thanks.

Related

How to properly reuse object pointers to widgets in GTK?

Suppose I have some widgets that I want to reuse their pointers, as
they are dynamically allocated and removed.
Example:
GtkWidget *widget1;
GtkWidget *widget2;
GtkWidget *widget3;
int main()
{
widget1 = gtk_new_type1_widget();
widget2 = gtk_new_type2_widget();
widget3 = gtk_new_type3_widget();
gtk_init(0, NULL);
//All other GTK initializing stuff, window
//creation and widgets loading from file...
gtk_main();
return 0;
}
Now suppose that I want to reuse widget1, widget2 and widget3 to point to a new widget. What is the proper to way to "free" the widgets before I allocate a new one? Do I need to call gtk_widget_destroy to clean all references? Or I can call something like g_clear_object?
Example:
gtk_widget_destroy(widget1);
g_clear_object(&widget1);
widget1 = gtk_new_type1_widget();
gtk_widget_destroy(widget2);
g_clear_object(&widget2);
widget2 = gtk_new_type2_widget();
gtk_widget_destroy(widget3);
g_clear_object(&widget3);
widget3 = gtk_new_type3_widget();
Also, a second question related to this one. If I create a function that wraps another gtk object creating function and set some properties for the objects, do I need to call g_object_unref ? Since the reference count will grow.
Example:
GtkWidget *create_widget_with_properties()
{
GtkWidget *widget;
widget = gtk_new_type1_widget();
set_widget_specific_properties(widget);
g_object_unref(widget);
return widget;
}
Appreciate any help.
EDIT: I solved my problem. For those who have the same doubts can
look my post at Gnome discourse. Here are some tips:
Understand reference counting
Understand floating reference concept in GTK
Understand memory management in GTK
If you understand the concepts above you will know how to handle widgets object pointers. There are useful links in the post.
Gtk version: 3.24.20

How do I replace lookup_widget in Glade3/GTK3 code?

I have recently resurrected a project from a decade ago (https://github.com/clancyj4/z80sim) and I'm running into problems that are related to the transition from glade 2 to 3.
One problem is that I use a function lookup_widget which is defined in support.c by glade 2, but is not defined in glade 3. And I don't know how to replace it.
An example of how I use it is:
void init_Code(GtkWidget *Main)
{
GtkWidget *widget;
PangoFontDescription *codefont;
codetext = lookup_widget(Main, "Code_Text");
code_textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(codetext));
I have looked the problem up, of course, but I don't understand the answer which was:
The preferred solution is to pass a pointer to your ENTRY as the user data pointer when you connect the signal, or, if you're using gtk_builder_connect_signals(), store a pointer to ENTRY in your class and pass the class as the user data pointer.
which was referring to:
on_BT_OK_clicked(GtkButton *button, gpointer user_data)
{
//The line directly below is the one I get an error on
GtkWidget *entry = lookup_widget( GTK_WIDGET(button), "ENTRY" );
Any clarification would be most helpful.
EDIT:
I'm pretty sure that the signals are all connected because I use this:
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "z80em2.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "Main"));
gtk_builder_connect_signals(builder, NULL);
As I understand it codetext = lookup_widget(Main, "Code_Text") returns a pointer to the GtkWidget referred to as Code_Text under the Main widget and I don't see how to do that in GTK3.
I haven't used Glade, but if you have "manual" code that connects the signal handler, you should pass a pointer to that other widget in that call. That will then be stored inside GTK+, and passed as the user_pointer argument to the event handler.
So it would become just:
on_BT_OK_clicked(GtkButton *button, gpointer user_data)
{
// This assumes the entry was passed to g_signal_connect() as 'data'
GtkWidget *entry = GTK_WIDGET(user_data);
See g_signal_connect().
If you are planning to rewrite this codebase heavily anyway, you may find it helpful to reorganize the widgets into composite widget templates, which Glade 3 supports, and use gtk_widget_class_bind_template_child() to make all the widgets that you would have otherwise looked up with lookup_widget() into members of your widget class. But it's not a good short-term fix, because it would involve a lot of refactoring.

GtkSourceGutter - How to render icon or text on a specific line

I have inserted custom gtk source gutter renderer pixbuf and I want to render icon on a specific line.
The reference API states that the interface is very similar to that on GtkTreeView, but doesn't work with a tree model.
So... how am I supposed to render data to a specific line if the GtkSourceGutter doesn't work with a tree model?
I checked every function in the entire library, every suggested api and child objects and nothing even hints about that.
It just doesn't make sense. The man page says that the GtkSourceGutterRendererPixbuf is used to display icon IN A CELL.
Doing gtk_source_gutter_renderer_pixbuf_set_pixbuf(renderer, pixbuf); will render the icon for all cells in the gutter.
And if the only way is to draw the pixbuf manually using cairo..what's the point in those renderers ?
How do I render pixbuf in a specific line using the gtksourcegutterrenderer?
I haven't worked with GtkSourceView, but I can give you some clues.
How it's done by GtkSourceView's author
First of all, we need some links:
GtkSourceGutterRendererMarks source code
GtkSourceGutterRendererPixbuf source code
GtkSourceGutterRenderer documentation
Let's start with GtkSourceGutterRendererPixbuf. From it's class_init method we find out, that it overrides only draw method. It's only purpose is to render a pixbuf or icon. Pure drawing.
However, GtkSourceGutterRenderer documentation says, that there is a query-data signal which can be used to tune Renderer's internal state. At this point we should take a look at GtkSourceGutterRendererMarks which is inherited from RendererPixbuf. It doesn't override draw, but overrides query_data. (For some reason GtkSourceGutterRendererClass is not described in the documentation. I don't know why.)
/* Read my comments. */
static void
gutter_renderer_query_data (GtkSourceGutterRenderer *renderer,
GtkTextIter *start,
GtkTextIter *end,
GtkSourceGutterRendererState state)
{
GSList *marks;
GdkPixbuf *pixbuf = NULL;
view = GTK_SOURCE_VIEW (gtk_source_gutter_renderer_get_view (renderer));
buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
marks = gtk_source_buffer_get_source_marks_at_iter (buffer,
start,
NULL);
/* If there are marks, we find a pixbuf for one of them.
* Otherwise pixbuf is NULL. */
if (marks != NULL)
{
size = measure_line_height (view);
pixbuf = composite_marks (view, marks, size);
g_slist_free (marks);
}
/* Now tell parent class to render certain pixbuf
* It will render nothing if pixbuf is NULL. */
g_object_set (G_OBJECT (renderer),
"pixbuf", pixbuf,
NULL);
}
My recommendations.
You want to draw marks at certain lines (e.g. want to highlight current debugger line). If I were you, I would have inherited from RendererPixbuf, overriden query_data and use gtk_text_iter_get_line on GtkTextIter *start. Looks like that's the bare minimum.
Feel free to ask any further questions.
I personally cannot simply agree with the allegation that creating custom objects is easy. It isn't easy, not to everyone.
Mainly, because, this question is tagged c and people who don't know Object-Oriented programming might be unfamiliar with its concepts.
It is a matter of reading and practice.
So do not panic if you don't know how to, for instance create your own widget.
The easiest solution I can think of, doesn't involve creating your own renderer, but rather tell the renderer how to query rendering data.
Just connect the query-data signal on your GtkSourceGutterRenderer to a signal handler that looks like this:
G_MODULE_EXPORT void gutter_renderer_query_data (GtkSourceGutterRenderer *renderer, GtkTextIter *start, GtkTextIter *end, GtkSourceGutterRendererState state)
{
GtkSourceView* view = NULL;
GtkSourceBuffer* buffer = NULL;
GSList* marks = NULL;
GdkPixbuf* pixbuf = NULL;
view = GTK_SOURCE_VIEW(gtk_source_gutter_renderer_get_view(renderer));
buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)));
marks = gtk_source_buffer_get_source_marks_at_iter(buffer, start, NULL);
if(marks != NULL)
{
char *category = gtk_source_mark_get_category(marks->data);
if(!g_strcmp0(category, "CERTAIN_CATEGORY")) /* See note 1) */
pixbuf = gtk_image_get_pixbuf(gtk_image_new_from_file("icon_file_here")); /* See note 2) */
g_slist_free(marks);
}
g_object_set(G_OBJECT(renderer), "pixbuf", pixbuf, "yalign", 0.5, NULL);
}
Notes:
GtkSourceMark shares the GtkSourceGutterRenderer interface so you might want to filter your other source marks, by specifying the category of a source mark that is applied to the certain line. Otherwise your custom renderer pixbuf will also be rendered left to your other source marks.
You should specify the exact pixbuf you want to render internally. Doing this, you won't have to call gtk_source_gutter_renderer_pixbuf_set_pixbuf() . You let the API do the resource handling.

invoke row-activated with libgda-ui

Can someone provide a sample how to access to callbacks with Gnome-db libgda-ui, using this demo https://github.com/GNOME/libgda/blob/master/libgda-ui/demos/grid.c and I want to call a callback when I double click on a row like in gtktree with liststore
I think I've found a solution using the raw-grid
GtkWidget *grid;
GdauiRawGrid *raw_grid;
g_object_get (G_OBJECT (grid), "raw-grid", &raw_grid, NULL);
Then the raw_grid has the callback needed and it have methods like gtk treeview

c - Passing multiple arguments to a callback function in GTK

So, I'm trying to achieve the following: The user shall be able to fill out multiple gtk_entry's and click Apply after that, on being clicked I want the Apply button to emit a signal, something like this:
g_signal_connect (G_OBJECT (Apply), "clicked",
G_CALLBACK(apply_clicked), # an argument #);
Afterwards, in apply_clicked(), I want the entered text to be saved.
My question is: How do I pass those gtk_entry's to my callback function apply_clicked?
If it were only one I'd just set it as # an argument #, but what should I do with multiple entries ?
The typical way of doing this is to do:
g_object_set_data (context_object, "entry1", entry1);
g_object_set_data (context_object, "entry2", entry2);
g_signal_connect (G_OBJECT (Apply), "clicked", G_CALLBACK (apply_clicked), context_object);
and then in apply_clicked:
GtkEntry *entry1 = g_object_get_data (context_object, "entry1");
...
Usually the context_object will be the GtkDialog or whatever these widgets exist on.
Alternatively, if you subclass the GtkDialog, you can do:
struct _MyDialog {
GtkDialog parent_object;
GtkEntry *entry1;
GtkEntry *entry2;
...
};
Then, when constructing your dialog, just set entry1, 2, 3, etc... and you don't need to use the g_object_[g,s]et_data() hack.
create a data structure (a linked list perhaps) to contain pointers to the gtk_entrys and pass that instead. Or better yet, why not just pass a pointer to the object which contains all of thise gtk_entrys?

Resources