| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2017, 2019 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-MenuView. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-MenuView is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Gtk2-Ex-MenuView is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-MenuView. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | # maybe: | ||
| 20 | # add-tearoffs - toplevel or all? | ||
| 21 | # | ||
| 22 | |||
| 23 | |||
| 24 | package Gtk2::Ex::MenuView; | ||
| 25 | 2 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 0 | ▶ | use Gtk2 1.200; # for GDK_PRIORITY_REDRAW, and bug fixes probably |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 30 | |||
| 31 | use Glib::Ex::SignalIds; | ||
| 32 | use Glib::Ex::SourceIds; | ||
| 33 | use Glib::Ex::SignalBits; | ||
| 34 | use Gtk2::Ex::MenuView::Menu; | ||
| 35 | |||
| 36 | # uncomment this to run the ### lines | ||
| 37 | #use Smart::Comments; | ||
| 38 | |||
| 39 | our $VERSION = 5; | ||
| 40 | |||
| 41 | use constant _submenu_class => 'Gtk2::Ex::MenuView::Menu'; | ||
| 42 | |||
| 43 | BEGIN { | ||
| 44 | Glib::Type->register_enum ('Gtk2::Ex::MenuView::WantActivate', | ||
| 45 | no => 0, | ||
| 46 | leaf => 1, | ||
| 47 | all => 2); | ||
| 48 | Glib::Type->register_enum ('Gtk2::Ex::MenuView::WantVisible', | ||
| 49 | no => 0, | ||
| 50 | show => 1, | ||
| 51 | show_all => 2); | ||
| 52 | } | ||
| 53 | |||
| 54 | use Glib::Object::Subclass | ||
| 55 | _submenu_class(), | ||
| 56 | signals => { | ||
| 57 | 'item-create-or-update' => | ||
| 58 | { param_types => ['Gtk2::MenuItem', | ||
| 59 | 'Gtk2::TreeModel', | ||
| 60 | 'Gtk2::TreePath', | ||
| 61 | 'Gtk2::TreeIter'], | ||
| 62 | return_type => 'Gtk2::MenuItem', | ||
| 63 | flags => ['action','run-last'], | ||
| 64 | accumulator => \&Glib::Ex::SignalBits::accumulator_first_defined, | ||
| 65 | }, | ||
| 66 | |||
| 67 | 'separator-create-or-update' => | ||
| 68 | { param_types => ['Gtk2::MenuItem', | ||
| 69 | 'Gtk2::TreeModel', | ||
| 70 | 'Gtk2::TreePath', | ||
| 71 | 'Gtk2::TreeIter'], | ||
| 72 | return_type => 'Gtk2::MenuItem', | ||
| 73 | flags => ['action'], | ||
| 74 | accumulator => \&Glib::Ex::SignalBits::accumulator_first_defined, | ||
| 75 | }, | ||
| 76 | |||
| 77 | activate => | ||
| 78 | { param_types => ['Gtk2::MenuItem', | ||
| 79 | 'Gtk2::TreeModel', | ||
| 80 | 'Gtk2::TreePath', | ||
| 81 | 'Gtk2::TreeIter'], | ||
| 82 | return_type => undef }, | ||
| 83 | }, | ||
| 84 | properties => [ Glib::ParamSpec->object | ||
| 85 | ('model', | ||
| 86 | (do { | ||
| 87 | my $str = 'Model'; | ||
| 88 | eval { require Locale::Messages; | ||
| 89 | Locale::Messages::dgettext('gtk20-properties',$str) | ||
| 90 | } || $str }), | ||
| 91 | 'TreeModel to display.', | ||
| 92 | 'Gtk2::TreeModel', | ||
| 93 | Glib::G_PARAM_READWRITE), | ||
| 94 | |||
| 95 | Glib::ParamSpec->enum | ||
| 96 | ('want-activate', | ||
| 97 | 'Want activate', | ||
| 98 | 'Whether to connect and generate a unified activate signal.', | ||
| 99 | 'Gtk2::Ex::MenuView::WantActivate', | ||
| 100 | 'leaf', | ||
| 101 | Glib::G_PARAM_READWRITE), | ||
| 102 | |||
| 103 | Glib::ParamSpec->enum | ||
| 104 | ('want-visible', | ||
| 105 | 'Want visible', | ||
| 106 | 'Whether to automatically make items visible.', | ||
| 107 | 'Gtk2::Ex::MenuView::WantVisible', | ||
| 108 | 'show_all', | ||
| 109 | Glib::G_PARAM_READWRITE), | ||
| 110 | |||
| 111 | ]; | ||
| 112 | |||
| 113 | |||
| 114 | # TODO: | ||
| 115 | # | ||
| 116 | # dirty 0, 1=item, 2=separator | ||
| 117 | # | ||
| 118 | # current_item_at_indices ... | ||
| 119 | # $menu->get_model_items | ||
| 120 | # $menu->model_items_array | ||
| 121 | |||
| 122 | # $menuview->menu_at_path | ||
| 123 | # $menuview->model_items | ||
| 124 | # $menuview->foreach_model_item | ||
| 125 | # item_get_indices | ||
| 126 | |||
| 127 | # mnemonics | ||
| 128 | # accel key from model example | ||
| 129 | # circular protection pay attention to model changes ? | ||
| 130 | |||
| 131 | #------------------------------------------------------------------------------ | ||
| 132 | |||
| 133 | # sub INIT_INSTANCE { | ||
| 134 | # my ($self) = @_; | ||
| 135 | # } | ||
| 136 | |||
| 137 | sub SET_PROPERTY { | ||
| 138 | my ($self, $pspec, $newval) = @_; | ||
| 139 | my $pname = $pspec->get_name; | ||
| 140 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 141 | |||
| 142 | if ($pname eq 'model') { | ||
| 143 | my $model = $newval; | ||
| 144 | Scalar::Util::weaken (my $weak_self = $self); | ||
| 145 | my $ref_weak_self = \$weak_self; | ||
| 146 | $self->{'model_ids'} = $model && Glib::Ex::SignalIds->new | ||
| 147 | ($model, | ||
| 148 | $model->signal_connect (row_changed => \&_do_row_changed, | ||
| 149 | $ref_weak_self), | ||
| 150 | $model->signal_connect (row_deleted => \&_do_row_deleted, | ||
| 151 | $ref_weak_self), | ||
| 152 | $model->signal_connect (row_inserted => \&_do_row_inserted, | ||
| 153 | $ref_weak_self), | ||
| 154 | $model->signal_connect (rows_reordered => \&_do_rows_reordered, | ||
| 155 | $ref_weak_self), | ||
| 156 | $model->signal_connect | ||
| 157 | (row_has_child_toggled => \&_do_row_has_child_toggled, $ref_weak_self)); | ||
| 158 | _dirty_all_menus ($self); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _freshen_item { | ||
| 163 | my ($self, $menu, $menu_path, $i) = @_; | ||
| 164 | ### _freshen_item() number: $i | ||
| 165 | my $model = $self->{'model'} || return; | ||
| 166 | |||
| 167 | if (delete $menu->{'all_dirty'}) { | ||
| 168 | ### all_dirty, make dirty array | ||
| 169 | my $menu_iter = ($menu_path->get_depth | ||
| 170 | ? $model->get_iter($menu_path) || do { | ||
| 171 | ### no iter for menu_path | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | : undef); | ||
| 175 | my $len = $model->iter_n_children ($menu_iter); | ||
| 176 | $menu->{'dirty'} ||= []; | ||
| 177 | @{$menu->{'dirty'}} = ((Gtk2::Ex::MenuView::Menu::_DIRTY_ITEM() | ||
| 178 | | Gtk2::Ex::MenuView::Menu::_DIRTY_SEPARATOR()) | ||
| 179 | x $len); | ||
| 180 | } | ||
| 181 | |||
| 182 | ### dirty_bits: $menu->{'dirty'}->[$i] | ||
| 183 | my $dirty_bits = delete $menu->{'dirty'}->[$i] || do { | ||
| 184 | ### not dirty, no freshen needed | ||
| 185 | return; | ||
| 186 | }; | ||
| 187 | # still dirty if recursive freshens such as item_at_indices() look, but | ||
| 188 | # cleared when this _freshen_item() returns | ||
| 189 | local $menu->{'dirty'}->[$i] = $dirty_bits; | ||
| 190 | |||
| 191 | my $item_path = $menu_path->copy; | ||
| 192 | $item_path->append_index ($i); | ||
| 193 | |||
| 194 | my $item_iter = $model->get_iter($item_path) || do { | ||
| 195 | ### no iter for item_path | ||
| 196 | return; | ||
| 197 | }; | ||
| 198 | |||
| 199 | my $key = $item_path->to_string; | ||
| 200 | ### in progress: $self->{'item_update_in_progress'} | ||
| 201 | my $in_progress = $self->{'item_update_in_progress'} || {}; | ||
| 202 | if ($in_progress->{$key}) { | ||
| 203 | ### croak for recursion | ||
| 204 | croak "Recursive item create or update for path=$key"; | ||
| 205 | } | ||
| 206 | local $self->{'item_update_in_progress'} = { %$in_progress, $key => 1 }; | ||
| 207 | ### flag in_progress to: $self->{'item_update_in_progress'} | ||
| 208 | |||
| 209 | my $children = ($menu->{'children'} ||= []); | ||
| 210 | my $item = $children->[$i]; | ||
| 211 | my $leaf = ! $model->iter_has_child ($item_iter); | ||
| 212 | |||
| 213 | my ($old_separator, $submenu); | ||
| 214 | if ($item) { | ||
| 215 | $old_separator = $item->{'Gtk2::Ex::MenuView.separator'}; | ||
| 216 | $submenu = $item->get_submenu; | ||
| 217 | } | ||
| 218 | |||
| 219 | if ($dirty_bits & Gtk2::Ex::MenuView::Menu::_DIRTY_ITEM()) { | ||
| 220 | my $old_item = $item; | ||
| 221 | $item = $self->signal_emit ('item-create-or-update', | ||
| 222 | $old_item, | ||
| 223 | $model, | ||
| 224 | $item_path, | ||
| 225 | $item_iter); | ||
| 226 | ### _item_create: $item | ||
| 227 | |||
| 228 | unless ($item && $old_item && $item == $old_item) { | ||
| 229 | if ($old_item) { | ||
| 230 | $menu->_remove_item ($old_item); | ||
| 231 | delete $children->[$i]; # so _item_index_to_menu_pos() doesn't see it | ||
| 232 | undef $old_separator; # destroyed by _remove_item() | ||
| 233 | } | ||
| 234 | if ($item) { | ||
| 235 | if ((my $want_activate = $self->get('want-activate')) ne 'no') { | ||
| 236 | # Connect to both leaf and non-leaf rows and filter in the handler, | ||
| 237 | # since a row might gain or lose a submenu at any time. There won't | ||
| 238 | # be many non-leafs so not much is wasted by this. | ||
| 239 | $item->signal_connect (activate => \&_do_item_activate); | ||
| 240 | } | ||
| 241 | if ((my $want_visible = $self->get('want-visible')) ne 'no') { | ||
| 242 | $item->$want_visible; # 'show' or 'show_all' | ||
| 243 | } | ||
| 244 | $menu->insert ($item, _item_index_to_menu_pos($menu,$i)); | ||
| 245 | $children->[$i] = $item; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | if ($item) { | ||
| 251 | if ($leaf) { | ||
| 252 | undef $submenu; | ||
| 253 | } else { | ||
| 254 | if (! $submenu) { | ||
| 255 | ### create submenu | ||
| 256 | $submenu = $self->_submenu_class->new; | ||
| 257 | $submenu->{'all_dirty'} = 1; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | $item->set_submenu ($submenu); | ||
| 261 | } | ||
| 262 | |||
| 263 | if ($item | ||
| 264 | && ($dirty_bits & Gtk2::Ex::MenuView::Menu::_DIRTY_SEPARATOR())) { | ||
| 265 | my $item_iter = $model->get_iter($item_path) || return; | ||
| 266 | my $separator = $self->signal_emit ('separator-create-or-update', | ||
| 267 | $old_separator, | ||
| 268 | $model, | ||
| 269 | $item_path, | ||
| 270 | $item_iter); | ||
| 271 | unless ($old_separator && $separator && $old_separator == $separator) { | ||
| 272 | if ($old_separator) { | ||
| 273 | $menu->remove ($old_separator); | ||
| 274 | $old_separator->destroy; | ||
| 275 | } | ||
| 276 | if ($separator) { | ||
| 277 | my $pos = _item_index_to_menu_pos ($menu, $i); | ||
| 278 | $item->{'Gtk2::Ex::MenuView.separator'} = $separator; | ||
| 279 | $menu->insert ($separator, $pos); | ||
| 280 | } else { | ||
| 281 | delete $item->{'Gtk2::Ex::MenuView.separator'}; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | ### freshen return 1 | ||
| 287 | return 1; | ||
| 288 | } | ||
| 289 | |||
| 290 | # 'activate' signal handler on each item child | ||
| 291 | sub _do_item_activate { | ||
| 292 | my ($item) = @_; | ||
| 293 | ### MenuView activate: $item | ||
| 294 | |||
| 295 | # shouldn't normally get a signal when not within a menu, but allow for | ||
| 296 | # perhaps the model changing without signals yet processed | ||
| 297 | my ($menuview,$model,$path,$iter) = Gtk2::Ex::MenuView->item_get_mmpi($item) | ||
| 298 | or do { | ||
| 299 | ### no model row for activated item | ||
| 300 | return; | ||
| 301 | }; | ||
| 302 | if ($menuview->get('want-activate') eq 'leaf' | ||
| 303 | && $model->iter_has_child ($iter)) { | ||
| 304 | ### no activate on leaf | ||
| 305 | return; | ||
| 306 | } | ||
| 307 | $menuview->signal_emit ('activate', $item, $model, $path, $iter); | ||
| 308 | } | ||
| 309 | |||
| 310 | sub _item_index_to_menu_pos { | ||
| 311 | my ($menu, $i) = @_; | ||
| 312 | ### _item_index_to_menu_pos(): $i | ||
| 313 | ### menu: "@{[$menu->get_children]}" | ||
| 314 | my $children = $menu->{'children'} || return -1; | ||
| 315 | ### children: "@{[grep {defined} @$children]}" | ||
| 316 | my $pos = -1; | ||
| 317 | OUTER: for ( ; $i < @$children; $i++) { | ||
| 318 | if (my $after = $children->[$i]) { | ||
| 319 | $after = $after->{'Gtk2::Ex::MenuView.separator'} || $after; | ||
| 320 | |||
| 321 | foreach my $child ($menu->get_children) { | ||
| 322 | $pos++; | ||
| 323 | if ($child == $after) { last OUTER; } | ||
| 324 | } | ||
| 325 | ### oops, not found in menu: "$after" | ||
| 326 | ### assert: 0 | ||
| 327 | return -1; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | ### _item_index_to_menu_pos(): "$i -> $pos" | ||
| 331 | return $pos; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub _dirty_all_menus { | ||
| 335 | my ($self) = @_; | ||
| 336 | ### _dirty_all_menus | ||
| 337 | |||
| 338 | my $menu = $self; | ||
| 339 | my @pending; | ||
| 340 | do { | ||
| 341 | _dirty_menu($menu); | ||
| 342 | push @pending, map { my $submenu = $_->get_submenu; | ||
| 343 | ($submenu ? ($submenu) : ()) } | ||
| 344 | @{$menu->{'children'}}; | ||
| 345 | } while ($menu = pop @pending); | ||
| 346 | } | ||
| 347 | # sub _menushellbits_menu_and_submenus { | ||
| 348 | # my ($menu) = @_; | ||
| 349 | # my @pending; | ||
| 350 | # my @ret; | ||
| 351 | # do { | ||
| 352 | # push @ret, $menu; | ||
| 353 | # push @pending, grep {defined} map {$_->get_submenu} $menu->get_children; | ||
| 354 | # } while ($menu = pop @pending); | ||
| 355 | # return @ret; | ||
| 356 | # } | ||
| 357 | |||
| 358 | |||
| 359 | #------------------------------------------------------------------------------ | ||
| 360 | # dirtiness etc | ||
| 361 | |||
| 362 | sub _idle_freshen { | ||
| 363 | my ($menu) = @_; | ||
| 364 | if ($menu->mapped) { | ||
| 365 | my $self = $menu->_get_menuview; | ||
| 366 | Scalar::Util::weaken (my $weak_self = $self); | ||
| 367 | $self->{'idle'} ||= Glib::Ex::SourceIds->new | ||
| 368 | (Glib::Idle->add (\&_do_idle, \$weak_self, | ||
| 369 | Gtk2::GTK_PRIORITY_RESIZE - 1)); # just before resize | ||
| 370 | } | ||
| 371 | } | ||
| 372 | sub _do_idle { | ||
| 373 | my ($ref_weak_self) = @_; | ||
| 374 | ### _do_idle | ||
| 375 | my $self = $$ref_weak_self || return; | ||
| 376 | delete $self->{'idle'}; | ||
| 377 | |||
| 378 | my $menu = $self; | ||
| 379 | my @pending; | ||
| 380 | do { | ||
| 381 | if ($menu->mapped) { | ||
| 382 | $menu->_freshen; | ||
| 383 | } | ||
| 384 | push @pending, map {$_->get_submenu || ()} @{$menu->{'children'}}; | ||
| 385 | } while ($menu = pop @pending); | ||
| 386 | |||
| 387 | return 0; # Glib::SOURCE_REMOVE | ||
| 388 | } | ||
| 389 | |||
| 390 | # mark $menu as all dirty | ||
| 391 | sub _dirty_menu { | ||
| 392 | my ($menu) = @_; | ||
| 393 | delete $menu->{'dirty'}; | ||
| 394 | $menu->{'all_dirty'} ||= do { | ||
| 395 | _idle_freshen ($menu); | ||
| 396 | 1; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | # mark $menu item number $i as dirty | ||
| 401 | sub _dirty_add { | ||
| 402 | my ($self, $menu, $i, $dirty_bits) = @_; | ||
| 403 | ### _dirty_add(): $i, "$menu", $dirty_bits | ||
| 404 | |||
| 405 | if (! $menu->{'all_dirty'}) { | ||
| 406 | my $dirty = ($menu->{'dirty'} ||= []); | ||
| 407 | $dirty->[$i] |= do { | ||
| 408 | _idle_freshen ($self); | ||
| 409 | $dirty_bits; # item | ||
| 410 | }; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _dirty_item_and_following_separator { | ||
| 415 | my ($self, $menu, $path) = @_; | ||
| 416 | |||
| 417 | my $i = ($path->get_indices)[-1]; | ||
| 418 | _dirty_add ($self, $menu, $i, | ||
| 419 | Gtk2::Ex::MenuView::Menu::_DIRTY_ITEM()); | ||
| 420 | |||
| 421 | if (my $model = $self->{'model'}) { | ||
| 422 | $path = $path->copy; | ||
| 423 | $path->next; | ||
| 424 | if ($model->get_iter($path)) { | ||
| 425 | _dirty_add ($self, $menu, $i+1, | ||
| 426 | Gtk2::Ex::MenuView::Menu::_DIRTY_SEPARATOR()); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | # return item at $path if it currently exists, or undef if not | ||
| 432 | sub _current_item_at_path { | ||
| 433 | my ($self, $path) = @_; | ||
| 434 | my @indices = $path->get_indices | ||
| 435 | or return undef; # empty path | ||
| 436 | my $item; | ||
| 437 | my $menu = $self; | ||
| 438 | for (;;) { | ||
| 439 | $item = $menu->{'children'}->[shift @indices] || return undef; | ||
| 440 | @indices || last; | ||
| 441 | $menu = $item->get_submenu || return undef; | ||
| 442 | } | ||
| 443 | return $item; | ||
| 444 | } | ||
| 445 | |||
| 446 | # return menu containing the item at $path, if that menu currently exists | ||
| 447 | # (the item itself doesn't have to), or undef if no such menu | ||
| 448 | sub _current_menu_at_path { | ||
| 449 | my ($self, $path) = @_; | ||
| 450 | if ($path->get_depth == 0) { return $self; } | ||
| 451 | my $item = $self->_current_item_at_path($path) || return undef; | ||
| 452 | return $item->get_submenu; | ||
| 453 | } | ||
| 454 | |||
| 455 | # return menu containing the item at $path, if that menu currently exists | ||
| 456 | # (the item itself doesn't have to), or undef if no such menu | ||
| 457 | sub _current_menu_containing_path { | ||
| 458 | my ($self, $path) = @_; | ||
| 459 | if ($path->get_depth == 1) { return $self; } | ||
| 460 | $path = $path->copy; | ||
| 461 | $path->up; | ||
| 462 | return $self->_current_menu_at_path($path); | ||
| 463 | } | ||
| 464 | |||
| 465 | #------------------------------------------------------------------------------ | ||
| 466 | # model changes | ||
| 467 | |||
| 468 | # 'row-has-child-toggled' callback from model | ||
| 469 | # | ||
| 470 | # Called only for rows, not when the toplevel goes between empty and | ||
| 471 | # non-empty. FIXME: Not yet documented that an item updates with subrow | ||
| 472 | # emptiness like this. | ||
| 473 | # | ||
| 474 | sub _do_row_has_child_toggled { | ||
| 475 | my ($model, $path, $iter, $ref_weak_self) = @_; | ||
| 476 | ### MenuView row-has-child-toggled: $path->to_string | ||
| 477 | my $self = $$ref_weak_self || return; | ||
| 478 | |||
| 479 | my $item = $self->_current_item_at_path($path) || return; | ||
| 480 | if ($model->iter_has_child($iter)) { | ||
| 481 | $item->set_submenu (undef); | ||
| 482 | } | ||
| 483 | # update display for rows or no-rows and create submenu if became | ||
| 484 | # non-empty | ||
| 485 | _dirty_add ($self, $item->get_parent, ($path->get_indices)[-1], | ||
| 486 | Gtk2::Ex::MenuView::Menu::_DIRTY_ITEM()); | ||
| 487 | } | ||
| 488 | |||
| 489 | # 'row-changed' callback from model | ||
| 490 | sub _do_row_changed { | ||
| 491 | my ($model, $path, $iter, $ref_weak_self) = @_; | ||
| 492 | ### MenuView row changed: $path->to_string | ||
| 493 | my $self = $$ref_weak_self || return; | ||
| 494 | |||
| 495 | my $menu = $self->_current_menu_containing_path($path) || do { | ||
| 496 | ### no menu for it currently | ||
| 497 | return; | ||
| 498 | }; | ||
| 499 | _dirty_item_and_following_separator ($self, $menu, $path); | ||
| 500 | } | ||
| 501 | |||
| 502 | # 'row-deleted' callback from model | ||
| 503 | sub _do_row_deleted { | ||
| 504 | my ($model, $path, $ref_weak_self) = @_; | ||
| 505 | ### MenuView row deleted: $path->to_string | ||
| 506 | my $self = $$ref_weak_self || return; | ||
| 507 | |||
| 508 | my $menu = $self->_current_menu_containing_path($path) || do { | ||
| 509 | ### no menu for this yet | ||
| 510 | return; | ||
| 511 | }; | ||
| 512 | |||
| 513 | my $i = ($path->get_indices)[-1]; | ||
| 514 | _splice_maybe ($menu->{'dirty'}, $i,1); # delete | ||
| 515 | if (my $item = _splice_maybe ($menu->{'children'}, $i,1)) { | ||
| 516 | $menu->_remove_item ($item); | ||
| 517 | } | ||
| 518 | |||
| 519 | # update following row for its separator, if there's a following row | ||
| 520 | if ($model->get_iter($path)) { | ||
| 521 | _dirty_add ($self, $menu, $i, | ||
| 522 | Gtk2::Ex::MenuView::Menu::_DIRTY_SEPARATOR()); | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | # 'row-inserted' callback from model | ||
| 527 | sub _do_row_inserted { | ||
| 528 | my ($model, $path, $iter, $ref_weak_self) = @_; | ||
| 529 | ### MenuView row inserted: $path->to_string | ||
| 530 | my $self = $$ref_weak_self || return; | ||
| 531 | |||
| 532 | my $menu = $self->_current_menu_containing_path($path) || do { | ||
| 533 | ### no menu for this yet | ||
| 534 | return; | ||
| 535 | }; | ||
| 536 | my $i = ($path->get_indices)[-1]; | ||
| 537 | |||
| 538 | # shift up arrays if necessary | ||
| 539 | _splice_maybe ($menu->{'children'}, $i,0, undef); # insert | ||
| 540 | _splice_maybe ($menu->{'dirty'}, $i,0, 1); # insert | ||
| 541 | |||
| 542 | _dirty_item_and_following_separator ($self, $menu, $path); | ||
| 543 | } | ||
| 544 | |||
| 545 | # 'rows-reordered' callback from model | ||
| 546 | sub _do_rows_reordered { | ||
| 547 | my ($model, $path, $iter, $aref, $ref_weak_self) = @_; | ||
| 548 | my $self = $$ref_weak_self || return; | ||
| 549 | ### MenuView rows reordered | ||
| 550 | |||
| 551 | my $menu = $self->_current_menu_at_path($path) || do { | ||
| 552 | ### no menu for this yet | ||
| 553 | return; | ||
| 554 | }; | ||
| 555 | my $children = $menu->{'children'} || return; | ||
| 556 | my @new_children; | ||
| 557 | my $pos = _item_index_to_menu_pos ($menu, 0); | ||
| 558 | foreach my $newpos (0 .. $#$aref) { | ||
| 559 | my $oldpos = $aref->[$newpos]; | ||
| 560 | if (my $item = $children->[$oldpos]) { | ||
| 561 | $new_children[$newpos] = $item; | ||
| 562 | |||
| 563 | # ENHANCE-ME: defer this to _freshen(), with a 'dirty_reorder' or | ||
| 564 | # if 'all_dirty' reinserting coped with reorder | ||
| 565 | if (my $separator = $item->{'Gtk2::Ex::MenuView.separator'}) { | ||
| 566 | $menu->reorder_child ($separator, $pos++); | ||
| 567 | } | ||
| 568 | ### reorder_child item to: $pos | ||
| 569 | $menu->reorder_child ($item, $pos++); | ||
| 570 | } | ||
| 571 | } | ||
| 572 | @$children = @new_children; | ||
| 573 | |||
| 574 | # ENHANCE-ME: could reorder the dirty flags and just update separators | ||
| 575 | # with different preceding | ||
| 576 | _dirty_menu ($menu); | ||
| 577 | } | ||
| 578 | |||
| 579 | #------------------------------------------------------------------------------ | ||
| 580 | |||
| 581 | sub item_at_path { | ||
| 582 | my ($self, $path) = @_; | ||
| 583 | return $self->item_at_indices ($path->get_indices); | ||
| 584 | } | ||
| 585 | |||
| 586 | sub item_at_indices { | ||
| 587 | my $self = shift; | ||
| 588 | ### item_at_indices(): @_ | ||
| 589 | $self->{'model'} || return undef; | ||
| 590 | |||
| 591 | my $menu = $self; | ||
| 592 | my $menu_path = Gtk2::TreePath->new; # of $menu | ||
| 593 | my $item; | ||
| 594 | while (@_) { | ||
| 595 | my $i = shift; | ||
| 596 | $self->_freshen_item ($menu, $menu_path, $i); | ||
| 597 | (($item = $menu->{'children'}->[$i]) | ||
| 598 | && ($menu = $item->get_submenu)) | ||
| 599 | or last; | ||
| 600 | $menu_path->append_index($i); | ||
| 601 | } | ||
| 602 | return $item; | ||
| 603 | } | ||
| 604 | |||
| 605 | #------------------------------------------------------------------------------ | ||
| 606 | |||
| 607 | # _splice_maybe($aref,$offset,$len, $repl...) | ||
| 608 | # A splice() of @$aref, but only if $aref is not undef and $offset is not | ||
| 609 | # past its end. | ||
| 610 | sub _splice_maybe { | ||
| 611 | if (my $aref = shift) { | ||
| 612 | if ((my $pos = shift) <= $#$aref) { | ||
| 613 | splice @$aref, $pos, @_; # $len and values | ||
| 614 | } | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | #------------------------------------------------------------------------------ | ||
| 619 | |||
| 620 | # sub insert { | ||
| 621 | # my ($self, $child, $pos) = @_; | ||
| 622 | # if ($pos >= 0 && $pos < $self->{'model_pos'}) { | ||
| 623 | # $self->{'model_pos'}--; | ||
| 624 | # } | ||
| 625 | # $self->SUPER::insert ($child, $pos); | ||
| 626 | # } | ||
| 627 | # sub prepend { | ||
| 628 | # my ($self, $child) = @_; | ||
| 629 | # $self->{'model_pos'}--; | ||
| 630 | # $self->SUPER::prepend ($child); | ||
| 631 | # } | ||
| 632 | |||
| 633 | 1; | ||
| 634 | __END__ | ||