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
Related
I want to create a window in Gtk that looks something like this.
widgets in glade
The left pane in the top window has a button labelled "Add to list". I have configured the callback for this button to add the widget "list_entry" into the GtkListBox in the right pane. It does so after changing the label of the widget from "label" to the text present in a text entry present just above it.
So, basically this button add a new entry into a list placed in the right pane(might be seen empty). This entry is a simple widget consisting of an icon and a label( at the bottom).
Now, I want the button "Add to list" to create new copies of this entry every time I click it and append that entry to the list in the right pane. But every time I add a new entry to the list, it simply overwrite the previous entry of the list and gives a warning ->
Attempting to add a widget with type GtkBox to a container of type GtkListBoxRow, but the widget is already inside a container of type GtkListBoxRow, please remove the widget from its existing container first.
I am using GtkListBox for the list in the right pane and the GtkBox for the new entry that I am supposed to add.
This is the code for the "click" callback of the button "Add to list"
static void
dw_left_add_btn_cb(CcPrintersPanel * self){
GtkLabel* row_list_label = (GtkLabel*) gtk_builder_get_object (self->builder, "list_entry_label");
GtkEntry* text_entry_to_add = (GtkEntry*) gtk_builder_get_object (self->builder, "dw_left_enter_text");
// debug
if ( self->list_serv == NULL || row_list_label == NULL) g_debug ("One of object for generating the list did'nt load up in the builder");
gtk_label_set_label (row_list_label, gtk_entry_get_text (text_entry_to_add));
gtk_entry_set_text(text_entry_to_add,"");
GtkWidget* row_list = (GtkWidget*) gtk_builder_get_object (self->builder, "list_entry");
gtk_list_box_insert (self->list_serv, row_list, -1);
}
I wanted to ask you how to create an entirely new widget( for every new entry ) from a widget that was loaded into Builder using a .ui file. Please also advise me if you have some other way of implementing this.
GtkBuilder is not a factory of widgets. It's more like a carton of widgets. Some pseudocode:
GtkWidget *widget1, *widget2;
builder = gtk_builder_new();
widget1 = gtk_builder_get_object(builder, "label");
widget2 = gtk_builder_get_object(builder, "label"); // this will not produce another label but return existing one
// (widget1 == widget2), they point to same object
// let's unpack another carton of widgets...
builder2 = gtk_builder_new();
widget2 = gtk_builder_get_object(builder2, "label");
// (widget1 != widget2), now that's really 2 distinct widgets
Hence the warning. You try to add widget to a ListBoxRow, but this exact widget is already in a ListBoxRow. When you want to dynamically create another widget, you must create another GtkBuilder.
static void
dw_left_add_btn_cb(CcPrintersPanel * self){
GtkBuilder *b = gtk_builder_new();
GtkLabel* w = (GtkLabel*) gtk_builder_get_object (b, "list_entry_label");
gtk_list_box_insert (self->list_serv, w, -1);
}
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.
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.
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.
I build a composite widget and would like it to have it's own accelerators (hotkeys) available only when it is in focus. The only Idea I have so far of how to accomplish this is to change out the accelerator group in the top level when ever my widget goes in and out of focus. It seems like there should be a better way.
Each widget class exposes pointers to its handlers as part of the widget class' struct. You can patch these at runtime, after calling gtk_init() but before entering the app's main loop.
So for instance, you could patch the key-press handler for GtkFoo like this:
static gboolean (*oldFooKeyPress)(GtkWidget* _widget, GdkEventKey* _event);
...
GtkWidgetClass* fooClass = GTK_WIDGET_CLASS(g_type_class_ref(GTK_TYPE_FOO));
oldFooKeyPress = fooClass->key_press_event;
fooClass->key_press_event = myFooKeyPress;
Then you could write myFooKeyPress() like this:
static gboolean myFooKeyPress(GtkWidget* widget, GdkEventKey* event)
{
if (widget is one I am interested in &&
event is a special accelerator for that widget)
{
do something special here
and maybe return
}
return oldFooKeyPress(widget, event);
}
As I recall, when you do the patching above, invoking GTK_TYPE_FOO will initialize the Foo widget class if it hasn't already been initialized.
Looks like I need to use key bindings instead of accelerators
http://library.gnome.org/devel/gtk/unstable/gtk-Bindings.html
Cross reference of the GtkTextView shows how
http://www.koders.com/c/fid959C3555A3004EA74AD6E0276122FC19673F9912.aspx?s=sort
Here is class init function which is the solution I was looking for
static void
webview_class_init (WebviewClass *klass)
{
GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
GtkBindingSet *binding_set;
signals[ZOOM_IN] = g_signal_new_class_handler("zoom_in",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_CALLBACK (webview_zoom_in),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 1,
G_TYPE_BOOLEAN, TRUE);
binding_set = gtk_binding_set_by_class (klass);
gtk_binding_entry_add_signal (binding_set, GDK_plus, GDK_CONTROL_MASK,
"zoom_in", 1,
G_TYPE_BOOLEAN, TRUE);
}
See this for implementing accelerators for a focused widget only
Steps:
Create new GSimpleActionGroup and gtk_widget_insert_action_group it to your specific widget.
Add the action into the new action group
Use gtk_application_set_accels_for_action() to set a shortcut for that specific action(name).