c - Swap two items in GList (GLib) -
I find it difficult to figure out how to swap two objects in a glow I want to swap two items in the list It is necessary to change their order during rendering.
For example, I want to work in gtktreeview to move objects up or down. I try this to proceed:
typedef struct settings settings; Structure Settings {GList * l; }; Typedef structure preset preset; Structure preset {char * title; Float freak; }; Settings sts; Zero move_up_button (GtkWidget * widget, gpointer (data)) {predetermined * ps; Int * row, pos; ..................................... line = gtk_tree_path_get_indices (path); Ps = g_list_nth_data (sts.l, * line); G_assert (ps); Pos = g_list_index (sts.l, (gpointer) ps); Pos--; Sts.l = g_list_remove (sts.l, (gpointer) ps); Sts.l = g_list_insert (sts.l, (gpointer) ps, pos); .......................................}
How can it simplify the use of extracting unused usage and inserting glist function?
thanks
I think you constantly Talking about the elements
In that case, A and B want to swap two elements (in that order). Then you need to make sure that
- Element indicates A (i.e., that is before it), instead indicates B;
- The current point for element B is my service; And
- B-digit A.
Try this:
GList * element_a, * element_b; ... / * swap element A and B * / element_a- & gt; Previous- gt; Next = element_b; Element_b-> Prev = element_a- & gt; Previous; Element_a-> Next = element_b-> next; Element_b-> Next-> Prev = element_a; Element_b-> Next = element_a; Element_a-> Prev = element_b;
Edit: While trying to use the instead of looking at the code you added to your question Instead, the list elements use the symbols G_list_remove
and g_list_inert
:
GList * button_element, * previous_Element; .... line = gtk_tree_path_get_indices (path); Button_editle = g_list_nth (sts.l, * line); G_assert (button_element-> data); / * Swap the button with your predecessor, if one * / previous _element = button_element-> gt; East; If (previous_limement) {if (predecessor_elment-> previous) {previous_elment-> gtc; Next-> next = button_element; Button_Element-> Prev = predecessor_elment-> Previous; } The other {/ * predecessor element is the key of the list, which we must update * / sts.l = button_element; Button_Element-> Prev = NULL; } Previous _elment-> gt; Next = button_elment-> next; If (button_elment-> next) {button_element-> Next-> gt = previous = predecessor_limment; } Button_lement-> Next = previous _lement; Preceding_lement-> gt = pre = button_lement; }
Comments
Post a Comment