| 1 | /* |
| 2 | * Copyright © 2013 Canonical Limited |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the licence, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Author: Ryan Lortie <[email protected]> |
| 18 | */ |
| 19 | |
| 20 | #include "config.h"cxx . . $ `<file>/build/config.h`/ |
| 21 | |
| 22 | #include "gtkmenutrackerprivate.h"cxx . . $ `<file>/gtk/gtkmenutrackerprivate.h`/ |
| 23 | |
| 24 | /*< private > |
| 25 | * GtkMenuTracker: |
| 26 | * |
| 27 | * GtkMenuTracker is a simple object to ease implementations of GMenuModel. |
| 28 | * Given a GtkActionObservable (usually a GActionMuxer) along with a |
| 29 | * GMenuModel, it will tell you which menu items to create and where to place |
| 30 | * them. If a menu item is removed, it will tell you the position of the menu |
| 31 | * item to remove. |
| 32 | * |
| 33 | * Using GtkMenuTracker is fairly simple. The only guarantee you must make |
| 34 | * to GtkMenuTracker is that you must obey all insert signals and track the |
| 35 | * position of items that GtkMenuTracker gives you. That is, GtkMenuTracker |
| 36 | * expects positions of all the latter items to change when it calls your |
| 37 | * insertion callback with an early position, as it may ask you to remove |
| 38 | * an item with a readjusted position later. |
| 39 | * |
| 40 | * GtkMenuTracker will give you a GtkMenuTrackerItem in your callback. You |
| 41 | * must hold onto this object until a remove signal is emitted. This item |
| 42 | * represents a single menu item, which can be one of three classes: normal item, |
| 43 | * separator, or submenu. |
| 44 | * |
| 45 | * Certain properties on the GtkMenuTrackerItem are mutable, and you must |
| 46 | * listen for changes in the item. For more details, see the documentation |
| 47 | * for GtkMenuTrackerItem along with GMenuModel's reference. |
| 48 | * |
| 49 | * The idea of @with_separators is for special cases where menu models may |
| 50 | * be tracked in places where separators are not available, like in toplevel |
| 51 | * "File", “Edit” menu bars. Ignoring separator items is wrong, as GtkMenuTracker |
| 52 | * expects the position to change, so we must tell GtkMenuTracker to ignore |
| 53 | * separators itself. |
| 54 | */ |
| 55 | |
| 56 | typedef struct _GtkMenuTrackerSectioncxx . . $ _GtkMenuTrackerSection# GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection#; |
| 57 | |
| 58 | struct _GtkMenuTrackercxx . . $ _GtkMenuTracker# |
| 59 | { |
| 60 | GtkActionObservablecxx . . $ GtkActionObservable# *observablecxx . . $ _GtkMenuTracker#observable.; |
| 61 | guintcxx . . $ guint# merge_sectionscxx . . $ _GtkMenuTracker#merge_sections. : 1; |
| 62 | guintcxx . . $ guint# mac_os_modecxx . . $ _GtkMenuTracker#mac_os_mode. : 1; |
| 63 | GtkMenuTrackerInsertFunccxx . . $ GtkMenuTrackerInsertFunc# insert_funccxx . . $ _GtkMenuTracker#insert_func.; |
| 64 | GtkMenuTrackerRemoveFunccxx . . $ GtkMenuTrackerRemoveFunc# remove_funccxx . . $ _GtkMenuTracker#remove_func.; |
| 65 | gpointercxx . . $ gpointer# user_datacxx . . $ _GtkMenuTracker#user_data.; |
| 66 | |
| 67 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *toplevelcxx . . $ _GtkMenuTracker#toplevel.; |
| 68 | }; |
| 69 | |
| 70 | struct _GtkMenuTrackerSectioncxx . . $ _GtkMenuTrackerSection# |
| 71 | { |
| 72 | gpointercxx . . $ gpointer# modelcxx . . $ _GtkMenuTrackerSection#model.; /* may be a GtkMenuTrackerItem or a GMenuModel */ |
| 73 | GSListcxx . . $ GSList# *itemscxx . . $ _GtkMenuTrackerSection#items.; |
| 74 | char *action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace.; |
| 75 | |
| 76 | guintcxx . . $ guint# separator_labelcxx . . $ _GtkMenuTrackerSection#separator_label. : 1; |
| 77 | guintcxx . . $ guint# with_separatorscxx . . $ _GtkMenuTrackerSection#with_separators. : 1; |
| 78 | guintcxx . . $ guint# has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator. : 1; |
| 79 | guintcxx . . $ guint# is_fakecxx . . $ _GtkMenuTrackerSection#is_fake. : 1; |
| 80 | |
| 81 | gulongcxx . . $ gulong# handlercxx . . $ _GtkMenuTrackerSection#handler.; |
| 82 | }; |
| 83 | |
| 84 | static GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# * gtk_menu_tracker_section_newcxx . . $ gtk_menu_tracker_section_new(9d925445a1992c24). (GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker, |
| 85 | GMenuModelcxx . . $ GMenuModel# *model, |
| 86 | gbooleancxx . . $ gboolean# with_separators, |
| 87 | gbooleancxx . . $ gboolean# separator_label, |
| 88 | int offset, |
| 89 | const char *action_namespace); |
| 90 | static void gtk_menu_tracker_section_freecxx . . $ gtk_menu_tracker_section_free(407e3ada6965c556). (GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section); |
| 91 | |
| 92 | static GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# * |
| 93 | gtk_menu_tracker_section_find_modelcxx . . $ gtk_menu_tracker_section_find_model(f8f303715a72b873). (GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section, |
| 94 | gpointercxx . . $ gpointer# model, |
| 95 | int *offset) |
| 96 | { |
| 97 | GSListcxx . . $ GSList# *item; |
| 98 | |
| 99 | if (section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator.) |
| 100 | (*offset)++; |
| 101 | |
| 102 | if (section->modelcxx . . $ _GtkMenuTrackerSection#model. == model) |
| 103 | return section; |
| 104 | |
| 105 | for (item = section->itemscxx . . $ _GtkMenuTrackerSection#items.; item; item = item->nextcxx . . $ _GSList#next.) |
| 106 | { |
| 107 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *subsection = item->datacxx . . $ _GSList#data.; |
| 108 | |
| 109 | if (subsection) |
| 110 | { |
| 111 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *found_section; |
| 112 | |
| 113 | found_section = gtk_menu_tracker_section_find_modelcxx . . $ gtk_menu_tracker_section_find_model(f8f303715a72b873). (subsection, model, offset); |
| 114 | |
| 115 | if (found_section) |
| 116 | return found_section; |
| 117 | } |
| 118 | else |
| 119 | (*offset)++; |
| 120 | } |
| 121 | |
| 122 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 123 | } |
| 124 | |
| 125 | /* this is responsible for syncing the showing of a separator for a |
| 126 | * single subsection (and its children). |
| 127 | * |
| 128 | * we only ever show separators if we have _actual_ children (ie: we do |
| 129 | * not show a separator if the section contains only empty child |
| 130 | * sections). it’s difficult to determine this on-the-fly, so we have |
| 131 | * this separate function to come back later and figure it out. |
| 132 | * |
| 133 | * 'section' is that section. |
| 134 | * |
| 135 | * 'tracker' is passed in so that we can emit callbacks when we decide |
| 136 | * to add/remove separators. |
| 137 | * |
| 138 | * 'offset' is passed in so we know which position to emit in our |
| 139 | * callbacks. ie: if we add a separator right at the top of this |
| 140 | * section then we would emit it with this offset. deeper inside, we |
| 141 | * adjust accordingly. |
| 142 | * |
| 143 | * could_have_separator is true in two situations: |
| 144 | * |
| 145 | * - our parent section had with_separators defined and there are items |
| 146 | * before us (ie: we should add a separator if we have content in |
| 147 | * order to divide us from the items above) |
| 148 | * |
| 149 | * - if we had a “label” attribute set for this section |
| 150 | * |
| 151 | * parent_model and parent_index are passed in so that we can give them |
| 152 | * to the insertion callback so that it can see the label (and anything |
| 153 | * else that happens to be defined on the section). |
| 154 | * |
| 155 | * we iterate each item in ourselves. for subsections, we recursively |
| 156 | * run ourselves to sync separators. after we are done, we notice if we |
| 157 | * have any items in us or if we are completely empty and sync if our |
| 158 | * separator is shown or not. |
| 159 | */ |
| 160 | static int |
| 161 | gtk_menu_tracker_section_sync_separatorscxx . . $ gtk_menu_tracker_section_sync_separators(fd63c7e72b69d614). (GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section, |
| 162 | GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker, |
| 163 | int offset, |
| 164 | gbooleancxx . . $ gboolean# could_have_separator, |
| 165 | GMenuModelcxx . . $ GMenuModel# *parent_model, |
| 166 | int parent_index) |
| 167 | { |
| 168 | gbooleancxx . . $ gboolean# should_have_separator; |
| 169 | int n_items = 0; |
| 170 | GSListcxx . . $ GSList# *item; |
| 171 | int i = 0; |
| 172 | |
| 173 | for (item = section->itemscxx . . $ _GtkMenuTrackerSection#items.; item; item = item->nextcxx . . $ _GSList#next.) |
| 174 | { |
| 175 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *subsection = item->datacxx . . $ _GSList#data.; |
| 176 | |
| 177 | if (subsection) |
| 178 | { |
| 179 | gbooleancxx . . $ gboolean# separator; |
| 180 | |
| 181 | separator = (section->with_separatorscxx . . $ _GtkMenuTrackerSection#with_separators. && n_items > 0) || subsection->separator_labelcxx . . $ _GtkMenuTrackerSection#separator_label.; |
| 182 | |
| 183 | /* Only pass the parent_model and parent_index in case they may be used to create the separator. */ |
| 184 | n_items += gtk_menu_tracker_section_sync_separatorscxx . . $ gtk_menu_tracker_section_sync_separators(fd63c7e72b69d614). (subsection, tracker, offset + n_items, |
| 185 | separator, |
| 186 | separator ? section->modelcxx . . $ _GtkMenuTrackerSection#model. : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 187 | separator ? i : 0); |
| 188 | } |
| 189 | else |
| 190 | n_items++; |
| 191 | |
| 192 | i++; |
| 193 | } |
| 194 | |
| 195 | should_have_separator = !section->is_fakecxx . . $ _GtkMenuTrackerSection#is_fake. && could_have_separator && n_items != 0; |
| 196 | |
| 197 | if (should_have_separator > section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator.) |
| 198 | { |
| 199 | /* Add a separator */ |
| 200 | GtkMenuTrackerItemcxx . . $ GtkMenuTrackerItem# *separator; |
| 201 | |
| 202 | separator = _gtk_menu_tracker_item_newcxx . . $ _gtk_menu_tracker_item_new(b0d1372369447e8f). (tracker->observablecxx . . $ _GtkMenuTracker#observable., parent_model, parent_index, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!); |
| 203 | (* tracker->insert_funccxx . . $ _GtkMenuTracker#insert_func.) (separator, offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 204 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (separator); |
| 205 | |
| 206 | section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator. = TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!; |
| 207 | } |
| 208 | else if (should_have_separator < section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator.) |
| 209 | { |
| 210 | /* Remove a separator */ |
| 211 | (* tracker->remove_funccxx . . $ _GtkMenuTracker#remove_func.) (offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 212 | section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator. = FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!; |
| 213 | } |
| 214 | |
| 215 | n_items += section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator.; |
| 216 | |
| 217 | return n_items; |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | gtk_menu_tracker_item_visibility_changedcxx . . $ gtk_menu_tracker_item_visibility_changed(fbd22c9c9c3e2045). (GtkMenuTrackerItemcxx . . $ GtkMenuTrackerItem# *item, |
| 222 | GParamSpeccxx . . $ GParamSpec# *pspec, |
| 223 | gpointercxx . . $ gpointer# user_data) |
| 224 | { |
| 225 | GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker = user_data; |
| 226 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section; |
| 227 | gbooleancxx . . $ gboolean# is_now_visible; |
| 228 | gbooleancxx . . $ gboolean# was_visible; |
| 229 | int offset = 0; |
| 230 | |
| 231 | is_now_visible = gtk_menu_tracker_item_get_is_visiblecxx . . $ gtk_menu_tracker_item_get_is_visible(2d2c72eb589aeff). (item); |
| 232 | |
| 233 | /* remember: the item is our model */ |
| 234 | section = gtk_menu_tracker_section_find_modelcxx . . $ gtk_menu_tracker_section_find_model(f8f303715a72b873). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel., item, &offset); |
| 235 | g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`! (section); |
| 236 | |
| 237 | was_visible = section->itemscxx . . $ _GtkMenuTrackerSection#items. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 238 | |
| 239 | if (is_now_visible == was_visible) |
| 240 | return; |
| 241 | |
| 242 | if (is_now_visible) |
| 243 | { |
| 244 | section->itemscxx . . $ _GtkMenuTrackerSection#items. = g_slist_prependcxx . . $ g_slist_prepend(6eb923d8c45a5456). (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 245 | (* tracker->insert_funccxx . . $ _GtkMenuTracker#insert_func.) (section->modelcxx . . $ _GtkMenuTrackerSection#model., offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | section->itemscxx . . $ _GtkMenuTrackerSection#items. = g_slist_delete_linkcxx . . $ g_slist_delete_link(2dea58bd92967320). (section->itemscxx . . $ _GtkMenuTrackerSection#items., section->itemscxx . . $ _GtkMenuTrackerSection#items.); |
| 250 | (* tracker->remove_funccxx . . $ _GtkMenuTracker#remove_func.) (offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 251 | } |
| 252 | |
| 253 | gtk_menu_tracker_section_sync_separatorscxx . . $ gtk_menu_tracker_section_sync_separators(fd63c7e72b69d614). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel., tracker, 0, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 254 | } |
| 255 | |
| 256 | static int |
| 257 | gtk_menu_tracker_section_measurecxx . . $ gtk_menu_tracker_section_measure(685c47008260c6dc). (GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section) |
| 258 | { |
| 259 | GSListcxx . . $ GSList# *item; |
| 260 | int n_items; |
| 261 | |
| 262 | if (section == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 263 | return 1; |
| 264 | |
| 265 | n_items = 0; |
| 266 | |
| 267 | if (section->has_separatorcxx . . $ _GtkMenuTrackerSection#has_separator.) |
| 268 | n_items++; |
| 269 | |
| 270 | for (item = section->itemscxx . . $ _GtkMenuTrackerSection#items.; item; item = item->nextcxx . . $ _GSList#next.) |
| 271 | n_items += gtk_menu_tracker_section_measurecxx . . $ gtk_menu_tracker_section_measure(685c47008260c6dc). (item->datacxx . . $ _GSList#data.); |
| 272 | |
| 273 | return n_items; |
| 274 | } |
| 275 | |
| 276 | static void |
| 277 | gtk_menu_tracker_remove_itemscxx . . $ gtk_menu_tracker_remove_items(3b8289bdcd5cd088). (GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker, |
| 278 | GSListcxx . . $ GSList# **change_point, |
| 279 | int offset, |
| 280 | int n_items) |
| 281 | { |
| 282 | int i; |
| 283 | |
| 284 | g_return_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:652:9`! (change_point != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 285 | |
| 286 | for (i = 0; i < n_items; i++) |
| 287 | { |
| 288 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *subsection; |
| 289 | int n; |
| 290 | |
| 291 | subsection = (*change_point)->datacxx . . $ _GSList#data.; |
| 292 | *change_point = g_slist_delete_linkcxx . . $ g_slist_delete_link(2dea58bd92967320). (*change_point, *change_point); |
| 293 | |
| 294 | n = gtk_menu_tracker_section_measurecxx . . $ gtk_menu_tracker_section_measure(685c47008260c6dc). (subsection); |
| 295 | gtk_menu_tracker_section_freecxx . . $ gtk_menu_tracker_section_free(407e3ada6965c556). (subsection); |
| 296 | |
| 297 | while (n--) |
| 298 | (* tracker->remove_funccxx . . $ _GtkMenuTracker#remove_func.) (offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | gtk_menu_tracker_add_itemscxx . . $ gtk_menu_tracker_add_items(a558f316fe52225e). (GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker, |
| 304 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section, |
| 305 | GSListcxx . . $ GSList# **change_point, |
| 306 | int offset, |
| 307 | GMenuModelcxx . . $ GMenuModel# *model, |
| 308 | int position, |
| 309 | int n_items) |
| 310 | { |
| 311 | g_return_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:652:9`! (change_point != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 312 | |
| 313 | while (n_items--) |
| 314 | { |
| 315 | GMenuModelcxx . . $ GMenuModel# *submenu; |
| 316 | |
| 317 | submenu = g_menu_model_get_item_linkcxx . . $ g_menu_model_get_item_link(4405510a32dc3092). (model, position + n_items, G_MENU_LINK_SECTIONcxx . . $ `<external>/17783962342109199441/gmenumodel.h:113:9`!); |
| 318 | g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`! (submenu != model); |
| 319 | |
| 320 | if (submenu != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && tracker->merge_sectionscxx . . $ _GtkMenuTracker#merge_sections.) |
| 321 | { |
| 322 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *subsection; |
| 323 | char *action_namespace = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 324 | gbooleancxx . . $ gboolean# has_label; |
| 325 | |
| 326 | has_label = g_menu_model_get_item_attributecxx . . $ g_menu_model_get_item_attribute(6c3537ee983e861a). (model, position + n_items, |
| 327 | G_MENU_ATTRIBUTE_LABELcxx . . $ `<external>/17783962342109199441/gmenumodel.h:74:9`!, "s", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 328 | |
| 329 | g_menu_model_get_item_attributecxx . . $ g_menu_model_get_item_attribute(6c3537ee983e861a). (model, position + n_items, |
| 330 | G_MENU_ATTRIBUTE_ACTION_NAMESPACEcxx . . $ `<external>/17783962342109199441/gmenumodel.h:53:9`!, "s", &action_namespace); |
| 331 | |
| 332 | if (section->action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace.) |
| 333 | { |
| 334 | char *namespace; |
| 335 | |
| 336 | namespace = g_strjoincxx . . $ g_strjoin(a849077ff1b8d812). (".", section->action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace., action_namespace, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 337 | subsection = gtk_menu_tracker_section_newcxx . . $ gtk_menu_tracker_section_new(9d925445a1992c24). (tracker, submenu, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, has_label, offset, namespace); |
| 338 | g_freecxx . . $ g_free(5d22bdacc48458e8). (namespace); |
| 339 | } |
| 340 | else |
| 341 | subsection = gtk_menu_tracker_section_newcxx . . $ gtk_menu_tracker_section_new(9d925445a1992c24). (tracker, submenu, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, has_label, offset, action_namespace); |
| 342 | |
| 343 | *change_point = g_slist_prependcxx . . $ g_slist_prepend(6eb923d8c45a5456). (*change_point, subsection); |
| 344 | g_freecxx . . $ g_free(5d22bdacc48458e8). (action_namespace); |
| 345 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (submenu); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | GtkMenuTrackerItemcxx . . $ GtkMenuTrackerItem# *item; |
| 350 | |
| 351 | item = _gtk_menu_tracker_item_newcxx . . $ _gtk_menu_tracker_item_new(b0d1372369447e8f). (tracker->observablecxx . . $ _GtkMenuTracker#observable., model, position + n_items, |
| 352 | tracker->mac_os_modecxx . . $ _GtkMenuTracker#mac_os_mode., |
| 353 | section->action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace., submenu != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 354 | |
| 355 | /* In the case that the item may disappear we handle that by |
| 356 | * treating the item that we just created as being its own |
| 357 | * subsection. This happens as so: |
| 358 | * |
| 359 | * - the subsection is created without the possibility of |
| 360 | * showing a separator |
| 361 | * |
| 362 | * - the subsection will have either 0 or 1 item in it at all |
| 363 | * times: either the shown item or not (in the case it is |
| 364 | * hidden) |
| 365 | * |
| 366 | * - the created item acts as the "model" for this section |
| 367 | * and we use its "visiblity-changed" signal in the same |
| 368 | * way that we use the "items-changed" signal from a real |
| 369 | * GMenuModel |
| 370 | * |
| 371 | * We almost never use the '->model' stored in the section for |
| 372 | * anything other than lookups and for dropped the ref and |
| 373 | * disconnecting the signal when we destroy the menu, and we |
| 374 | * need to do exactly those things in this case as well. |
| 375 | * |
| 376 | * The only other thing that '->model' is used for is in the |
| 377 | * case that we want to show a separator, but we will never do |
| 378 | * that because separators are not shown for this fake section. |
| 379 | */ |
| 380 | if (gtk_menu_tracker_item_may_disappearcxx . . $ gtk_menu_tracker_item_may_disappear(2d2c72eb589aeff). (item)) |
| 381 | { |
| 382 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *fake_section; |
| 383 | |
| 384 | fake_section = g_new0cxx . . $ `<external>/1123956534522462559/gmem.h:335:9`! (GtkMenuTrackerSection, 1); |
| 385 | fake_section->is_fakecxx . . $ _GtkMenuTrackerSection#is_fake. = TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!; |
| 386 | fake_section->modelcxx . . $ _GtkMenuTrackerSection#model. = g_object_refcxx . . $ `<external>/1341695927569185362/gobject.h:533:9`! (item); |
| 387 | fake_section->handlercxx . . $ _GtkMenuTrackerSection#handler. = g_signal_connectcxx . . $ `<external>/15952772511652638895/gsignal.h:548:9`! (item, "notify::is-visible", |
| 388 | G_CALLBACKcxx . . $ `<external>/16473872192229286895/gclosure.h:73:9`! (gtk_menu_tracker_item_visibility_changed), |
| 389 | tracker); |
| 390 | *change_point = g_slist_prependcxx . . $ g_slist_prepend(6eb923d8c45a5456). (*change_point, fake_section); |
| 391 | |
| 392 | if (gtk_menu_tracker_item_get_is_visiblecxx . . $ gtk_menu_tracker_item_get_is_visible(2d2c72eb589aeff). (item)) |
| 393 | { |
| 394 | (* tracker->insert_funccxx . . $ _GtkMenuTracker#insert_func.) (item, offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 395 | fake_section->itemscxx . . $ _GtkMenuTrackerSection#items. = g_slist_prependcxx . . $ g_slist_prepend(6eb923d8c45a5456). (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 396 | } |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | /* In the normal case, we store NULL in the linked list. |
| 401 | * The measurement and lookup code count NULL always as |
| 402 | * exactly 1: an item that will always be there. |
| 403 | */ |
| 404 | (* tracker->insert_funccxx . . $ _GtkMenuTracker#insert_func.) (item, offset, tracker->user_datacxx . . $ _GtkMenuTracker#user_data.); |
| 405 | *change_point = g_slist_prependcxx . . $ g_slist_prepend(6eb923d8c45a5456). (*change_point, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 406 | } |
| 407 | |
| 408 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (item); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static void |
| 414 | gtk_menu_tracker_model_changedcxx . . $ gtk_menu_tracker_model_changed(125e262ec8477910). (GMenuModelcxx . . $ GMenuModel# *model, |
| 415 | int position, |
| 416 | int removed, |
| 417 | int added, |
| 418 | gpointercxx . . $ gpointer# user_data) |
| 419 | { |
| 420 | GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker = user_data; |
| 421 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section; |
| 422 | GSListcxx . . $ GSList# **change_point; |
| 423 | int offset = 0; |
| 424 | int i; |
| 425 | |
| 426 | /* First find which section the changed model corresponds to, and the |
| 427 | * position of that section within the overall menu. |
| 428 | */ |
| 429 | section = gtk_menu_tracker_section_find_modelcxx . . $ gtk_menu_tracker_section_find_model(f8f303715a72b873). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel., model, &offset); |
| 430 | g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`! (section); |
| 431 | |
| 432 | /* Next, seek through that section to the change point. This gives us |
| 433 | * the correct GSList** to make the change to and also finds the final |
| 434 | * offset at which we will make the changes (by measuring the number |
| 435 | * of items within each item of the section before the change point). |
| 436 | */ |
| 437 | change_point = §ion->itemscxx . . $ _GtkMenuTrackerSection#items.; |
| 438 | for (i = 0; i < position; i++) |
| 439 | { |
| 440 | offset += gtk_menu_tracker_section_measurecxx . . $ gtk_menu_tracker_section_measure(685c47008260c6dc). ((*change_point)->datacxx . . $ _GSList#data.); |
| 441 | change_point = &(*change_point)->nextcxx . . $ _GSList#next.; |
| 442 | } |
| 443 | |
| 444 | /* We remove items in order and add items in reverse order. This |
| 445 | * means that the offset used for all inserts and removes caused by a |
| 446 | * single change will be the same. |
| 447 | * |
| 448 | * This also has a performance advantage: GtkMenuShell stores the |
| 449 | * menu items in a linked list. In the case where we are creating a |
| 450 | * menu for the first time, adding the items in reverse order means |
| 451 | * that we only ever insert at index zero, prepending the list. This |
| 452 | * means that we can populate in O(n) time instead of O(n^2) that we |
| 453 | * would do by appending. |
| 454 | */ |
| 455 | gtk_menu_tracker_remove_itemscxx . . $ gtk_menu_tracker_remove_items(3b8289bdcd5cd088). (tracker, change_point, offset, removed); |
| 456 | gtk_menu_tracker_add_itemscxx . . $ gtk_menu_tracker_add_items(a558f316fe52225e). (tracker, section, change_point, offset, model, position, added); |
| 457 | |
| 458 | /* The offsets for insertion/removal of separators will be all over |
| 459 | * the place, however... |
| 460 | */ |
| 461 | gtk_menu_tracker_section_sync_separatorscxx . . $ gtk_menu_tracker_section_sync_separators(fd63c7e72b69d614). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel., tracker, 0, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 462 | } |
| 463 | |
| 464 | static void |
| 465 | gtk_menu_tracker_section_freecxx . . $ gtk_menu_tracker_section_free(407e3ada6965c556). (GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section) |
| 466 | { |
| 467 | if (section == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 468 | return; |
| 469 | |
| 470 | g_signal_handler_disconnectcxx . . $ g_signal_handler_disconnect(5500c4293b1bbef0). (section->modelcxx . . $ _GtkMenuTrackerSection#model., section->handlercxx . . $ _GtkMenuTrackerSection#handler.); |
| 471 | g_slist_free_fullcxx . . $ g_slist_free_full(5c74ad7c36eb6b1f). (section->itemscxx . . $ _GtkMenuTrackerSection#items., (GDestroyNotifycxx . . $ GDestroyNotify#) gtk_menu_tracker_section_freecxx . . $ gtk_menu_tracker_section_free(407e3ada6965c556).); |
| 472 | g_freecxx . . $ g_free(5d22bdacc48458e8). (section->action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace.); |
| 473 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (section->modelcxx . . $ _GtkMenuTrackerSection#model.); |
| 474 | g_freecxx . . $ g_free(5d22bdacc48458e8). (section); |
| 475 | } |
| 476 | |
| 477 | static GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# * |
| 478 | gtk_menu_tracker_section_newcxx . . $ gtk_menu_tracker_section_new(9d925445a1992c24). (GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker, |
| 479 | GMenuModelcxx . . $ GMenuModel# *model, |
| 480 | gbooleancxx . . $ gboolean# with_separators, |
| 481 | gbooleancxx . . $ gboolean# separator_label, |
| 482 | int offset, |
| 483 | const char *action_namespace) |
| 484 | { |
| 485 | GtkMenuTrackerSectioncxx . . $ GtkMenuTrackerSection# *section; |
| 486 | |
| 487 | section = g_new0cxx . . $ `<external>/1123956534522462559/gmem.h:335:9`! (GtkMenuTrackerSection, 1); |
| 488 | section->modelcxx . . $ _GtkMenuTrackerSection#model. = g_object_refcxx . . $ `<external>/1341695927569185362/gobject.h:533:9`! (model); |
| 489 | section->with_separatorscxx . . $ _GtkMenuTrackerSection#with_separators. = with_separators; |
| 490 | section->action_namespacecxx . . $ _GtkMenuTrackerSection#action_namespace. = g_strdupcxx . . $ `<external>/7822753393113922331/gstrfuncs.h:324:9`! (action_namespace); |
| 491 | section->separator_labelcxx . . $ _GtkMenuTrackerSection#separator_label. = separator_label; |
| 492 | |
| 493 | gtk_menu_tracker_add_itemscxx . . $ gtk_menu_tracker_add_items(a558f316fe52225e). (tracker, section, §ion->itemscxx . . $ _GtkMenuTrackerSection#items., offset, model, 0, g_menu_model_get_n_itemscxx . . $ g_menu_model_get_n_items(49634fd29c03e76b). (model)); |
| 494 | section->handlercxx . . $ _GtkMenuTrackerSection#handler. = g_signal_connectcxx . . $ `<external>/15952772511652638895/gsignal.h:548:9`! (model, "items-changed", G_CALLBACKcxx . . $ `<external>/16473872192229286895/gclosure.h:73:9`! (gtk_menu_tracker_model_changed), tracker); |
| 495 | |
| 496 | return section; |
| 497 | } |
| 498 | |
| 499 | /*< private > |
| 500 | * gtk_menu_tracker_new: |
| 501 | * @model: the model to flatten |
| 502 | * @with_separators: if the toplevel should have separators (ie: TRUE |
| 503 | * for menus, FALSE for menubars) |
| 504 | * @merge_sections: if sections should have their items merged in the |
| 505 | * usual way or reported only as separators (which can be queried to |
| 506 | * manually handle the items) |
| 507 | * @mac_os_mode: if this is on behalf of the Mac OS menubar |
| 508 | * @action_namespace: the passed-in action namespace |
| 509 | * @insert_func: insert callback |
| 510 | * @remove_func: remove callback |
| 511 | * @user_data user data for callbacks |
| 512 | * |
| 513 | * Creates a GtkMenuTracker for @model, holding a ref on @model for as |
| 514 | * long as the tracker is alive. |
| 515 | * |
| 516 | * This flattens out the model, merging sections and inserting |
| 517 | * separators where appropriate. It monitors for changes and performs |
| 518 | * updates on the fly. It also handles action_namespace for subsections |
| 519 | * (but you will need to handle it yourself for submenus). |
| 520 | * |
| 521 | * When the tracker is first created, @insert_func will be called many |
| 522 | * times to populate the menu with the initial contents of @model |
| 523 | * (unless it is empty), before gtk_menu_tracker_new() returns. For |
| 524 | * this reason, the menu that is using the tracker ought to be empty |
| 525 | * when it creates the tracker. |
| 526 | * |
| 527 | * Future changes to @model will result in more calls to @insert_func |
| 528 | * and @remove_func. |
| 529 | * |
| 530 | * The position argument to both functions is the linear 0-based |
| 531 | * position in the menu at which the item in question should be inserted |
| 532 | * or removed. |
| 533 | * |
| 534 | * For @insert_func, @model and @item_index are used to get the |
| 535 | * information about the menu item to insert. @action_namespace is the |
| 536 | * action namespace that actions referred to from that item should place |
| 537 | * themselves in. Note that if the item is a submenu and the |
| 538 | * “action-namespace” attribute is defined on the item, it will _not_ be |
| 539 | * applied to the @action_namespace argument as it is meant for the |
| 540 | * items inside of the submenu, not the submenu item itself. |
| 541 | * |
| 542 | * @is_separator is set to %TRUE in case the item being added is a |
| 543 | * separator. @model and @item_index will still be meaningfully set in |
| 544 | * this case -- to the section menu item corresponding to the separator. |
| 545 | * This is useful if the section specifies a label, for example. If |
| 546 | * there is an “action-namespace” attribute on this menu item then it |
| 547 | * should be ignored by the consumer because GtkMenuTracker has already |
| 548 | * handled it. |
| 549 | * |
| 550 | * When using GtkMenuTracker there is no need to hold onto @model or |
| 551 | * monitor it for changes. The model will be unreffed when |
| 552 | * gtk_menu_tracker_free() is called. |
| 553 | */ |
| 554 | GtkMenuTrackercxx . . $ GtkMenuTracker# * |
| 555 | gtk_menu_tracker_newcxx . . $ gtk_menu_tracker_new(c776749331e6c703). (GtkActionObservablecxx . . $ GtkActionObservable# *observable, |
| 556 | GMenuModelcxx . . $ GMenuModel# *model, |
| 557 | gbooleancxx . . $ gboolean# with_separators, |
| 558 | gbooleancxx . . $ gboolean# merge_sections, |
| 559 | gbooleancxx . . $ gboolean# mac_os_mode, |
| 560 | const char *action_namespace, |
| 561 | GtkMenuTrackerInsertFunccxx . . $ GtkMenuTrackerInsertFunc# insert_func, |
| 562 | GtkMenuTrackerRemoveFunccxx . . $ GtkMenuTrackerRemoveFunc# remove_func, |
| 563 | gpointercxx . . $ gpointer# user_data) |
| 564 | { |
| 565 | GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker; |
| 566 | |
| 567 | tracker = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (GtkMenuTracker, 1); |
| 568 | tracker->merge_sectionscxx . . $ _GtkMenuTracker#merge_sections. = merge_sections; |
| 569 | tracker->mac_os_modecxx . . $ _GtkMenuTracker#mac_os_mode. = mac_os_mode; |
| 570 | tracker->observablecxx . . $ _GtkMenuTracker#observable. = g_object_refcxx . . $ `<external>/1341695927569185362/gobject.h:533:9`! (observable); |
| 571 | tracker->insert_funccxx . . $ _GtkMenuTracker#insert_func. = insert_func; |
| 572 | tracker->remove_funccxx . . $ _GtkMenuTracker#remove_func. = remove_func; |
| 573 | tracker->user_datacxx . . $ _GtkMenuTracker#user_data. = user_data; |
| 574 | |
| 575 | tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel. = gtk_menu_tracker_section_newcxx . . $ gtk_menu_tracker_section_new(9d925445a1992c24). (tracker, model, with_separators, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, 0, action_namespace); |
| 576 | gtk_menu_tracker_section_sync_separatorscxx . . $ gtk_menu_tracker_section_sync_separators(fd63c7e72b69d614). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel., tracker, 0, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 577 | |
| 578 | return tracker; |
| 579 | } |
| 580 | |
| 581 | GtkMenuTrackercxx . . $ GtkMenuTracker# * |
| 582 | gtk_menu_tracker_new_for_item_linkcxx . . $ gtk_menu_tracker_new_for_item_link(1fd6cac66d61909e). (GtkMenuTrackerItemcxx . . $ GtkMenuTrackerItem# *item, |
| 583 | const char *link_name, |
| 584 | gbooleancxx . . $ gboolean# merge_sections, |
| 585 | gbooleancxx . . $ gboolean# mac_os_mode, |
| 586 | GtkMenuTrackerInsertFunccxx . . $ GtkMenuTrackerInsertFunc# insert_func, |
| 587 | GtkMenuTrackerRemoveFunccxx . . $ GtkMenuTrackerRemoveFunc# remove_func, |
| 588 | gpointercxx . . $ gpointer# user_data) |
| 589 | { |
| 590 | GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker; |
| 591 | GMenuModelcxx . . $ GMenuModel# *submenu; |
| 592 | char *namespace; |
| 593 | |
| 594 | submenu = _gtk_menu_tracker_item_get_linkcxx . . $ _gtk_menu_tracker_item_get_link(9ad15bd9ee87aa90). (item, link_name); |
| 595 | namespace = _gtk_menu_tracker_item_get_link_namespacecxx . . $ _gtk_menu_tracker_item_get_link_namespace(6db36927926f714f). (item); |
| 596 | |
| 597 | tracker = gtk_menu_tracker_newcxx . . $ gtk_menu_tracker_new(c776749331e6c703). (_gtk_menu_tracker_item_get_observablecxx . . $ _gtk_menu_tracker_item_get_observable(e7e76f096fda5e46). (item), submenu, |
| 598 | TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!, merge_sections, mac_os_mode, |
| 599 | namespace, insert_func, remove_func, user_data); |
| 600 | |
| 601 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (submenu); |
| 602 | g_freecxx . . $ g_free(5d22bdacc48458e8). (namespace); |
| 603 | |
| 604 | return tracker; |
| 605 | } |
| 606 | |
| 607 | /*< private > |
| 608 | * gtk_menu_tracker_free: |
| 609 | * @tracker: a GtkMenuTracker |
| 610 | * |
| 611 | * Frees the tracker, ... |
| 612 | */ |
| 613 | void |
| 614 | gtk_menu_tracker_freecxx . . $ gtk_menu_tracker_free(150a0784250ac808). (GtkMenuTrackercxx . . $ GtkMenuTracker# *tracker) |
| 615 | { |
| 616 | gtk_menu_tracker_section_freecxx . . $ gtk_menu_tracker_section_free(407e3ada6965c556). (tracker->toplevelcxx . . $ _GtkMenuTracker#toplevel.); |
| 617 | g_object_unrefcxx . . $ g_object_unref(5d22bdacc48458e8). (tracker->observablecxx . . $ _GtkMenuTracker#observable.); |
| 618 | g_freecxx . . $ g_free(5d22bdacc48458e8). (tracker); |
| 619 | } |