| 1 | # Copyright 2008, 2009, 2010, 2011, 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 | package Gtk2::Ex::MenuView::Menu; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Gtk2; | ||
| 23 | |||
| 24 | # 1.240 for some non-copying in the GValue boxed handling needed so | ||
| 25 | # signal_chain_from_overridden in _do_size_request writes its result to the | ||
| 26 | # right place. (Or 1.230 pre-release.) | ||
| 27 | # | ||
| 28 | # use Glib 1.240; | ||
| 29 | |||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | #use Smart::Comments; | ||
| 33 | |||
| 34 | our $VERSION = 5; | ||
| 35 | |||
| 36 | use Glib::Object::Subclass | ||
| 37 | 'Gtk2::Menu', | ||
| 38 | signals => { map => \&_do_map, | ||
| 39 | # size_request => \&_do_size_request, | ||
| 40 | }; | ||
| 41 | |||
| 42 | use constant { _DIRTY_ITEM => 1, | ||
| 43 | _DIRTY_SEPARATOR => 2 }; | ||
| 44 | |||
| 45 | sub INIT_INSTANCE { | ||
| 46 | my ($menu) = @_; | ||
| 47 | $menu->{'size_request_ids'} = Glib::Ex::SignalIds->new | ||
| 48 | ($menu, $menu->signal_connect (size_request => \&_do_size_request)); | ||
| 49 | } | ||
| 50 | |||
| 51 | # sub SET_PROPERTY { | ||
| 52 | # my ($menu, $pspec, $newval) = @_; | ||
| 53 | # my $pname = $pspec->get_name; | ||
| 54 | # $menu->{$pname} = $newval; # per default GET_PROPERTY | ||
| 55 | # } | ||
| 56 | |||
| 57 | # 'show' signal class closure | ||
| 58 | sub _do_map { | ||
| 59 | my ($menu) = @_; | ||
| 60 | ### MenuView Menu map: "$menu" | ||
| 61 | $menu->_freshen; | ||
| 62 | return shift->signal_chain_from_overridden(@_); | ||
| 63 | } | ||
| 64 | |||
| 65 | # 'show' signal class closure | ||
| 66 | sub _do_size_request { | ||
| 67 | my ($menu, $req) = @_; | ||
| 68 | ### MenuView Menu size-request: "$menu" | ||
| 69 | |||
| 70 | $menu->_freshen; | ||
| 71 | # return shift->signal_chain_from_overridden(@_); | ||
| 72 | |||
| 73 | undef $menu->{'size_request_ids'}; | ||
| 74 | my $sreq = $menu->size_request; | ||
| 75 | $menu->{'size_request_ids'} = Glib::Ex::SignalIds->new | ||
| 76 | ($menu, $menu->signal_connect (size_request => \&_do_size_request)); | ||
| 77 | $req->width ($sreq->width); | ||
| 78 | $req->height ($sreq->height); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _freshen { | ||
| 82 | my ($menu) = @_; | ||
| 83 | ### MenuView _freshen(): "$menu" | ||
| 84 | |||
| 85 | $menu->{'all_dirty'} || $menu->{'dirty'} || return; | ||
| 86 | |||
| 87 | my $menuview = $menu->_get_menuview; | ||
| 88 | ### menuview: "$menuview" | ||
| 89 | my $model = $menuview && $menuview->{'model'}; | ||
| 90 | ### model: "$model" | ||
| 91 | my $path = $model && $menu->_get_path; | ||
| 92 | ### path: ($path && $path->to_string) | ||
| 93 | |||
| 94 | # $len can be fetched once from the model as item updates are not allowed | ||
| 95 | # to change the rows | ||
| 96 | my $len = 0; | ||
| 97 | if ($path) { | ||
| 98 | # iter undef for depth==0 empty path, otherwise get_iter() of path | ||
| 99 | my $iter; | ||
| 100 | if ($path->get_depth == 0 || ($iter = $model->get_iter ($path))) { | ||
| 101 | # length from iter: $iter | ||
| 102 | $len = $model->iter_n_children ($iter); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | ### $len | ||
| 106 | |||
| 107 | # This loop allows for new dirtiness caused by a model set_value() in an | ||
| 108 | # item update. Looking at all_dirty each time is probably unnecessary, | ||
| 109 | # only if maybe _do_row_changed() decided to collapse to all_dirty when | ||
| 110 | # all rows are indeed dirty. | ||
| 111 | for (;;) { | ||
| 112 | my $saw_dirty = 0; | ||
| 113 | foreach my $i (0 .. $len-1) { | ||
| 114 | $saw_dirty |= ($menuview->_freshen_item ($menu, $path, $i) || 0); | ||
| 115 | } | ||
| 116 | if (! $saw_dirty) { last; } | ||
| 117 | } | ||
| 118 | delete $menu->{'dirty'}; | ||
| 119 | |||
| 120 | if (my $children = $menu->{'children'}) { | ||
| 121 | # use a pop() loop instead of a single splice() out so as not to lose | ||
| 122 | # track of items if one of the _remove_item()s errors-out | ||
| 123 | while (@$children > $len) { | ||
| 124 | if (my $item = pop @$children) { | ||
| 125 | $menu->_remove_item ($item); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _remove_item { | ||
| 132 | my ($class_or_menu, $item) = @_; | ||
| 133 | ### _remove_item "$item" | ||
| 134 | |||
| 135 | my $menu = $item->get_parent || return; | ||
| 136 | if (my $separator = delete $item->{'Gtk2::Ex::MenuView.separator'}) { | ||
| 137 | $menu->remove ($separator); | ||
| 138 | $separator->destroy; | ||
| 139 | } | ||
| 140 | $menu->remove ($item); | ||
| 141 | $item->set_submenu (undef); | ||
| 142 | $item->destroy; # in case circular ref from child AccelLabel | ||
| 143 | } | ||
| 144 | |||
| 145 | #------------------------------------------------------------------------------ | ||
| 146 | |||
| 147 | # $menu is either $self or a sub-menu, return the $self MenuView | ||
| 148 | sub _get_menuview { | ||
| 149 | my ($menu) = @_; | ||
| 150 | for (;;) { | ||
| 151 | if ($menu->isa('Gtk2::Ex::MenuView')) { return $menu; } | ||
| 152 | my $item = $menu->get_attach_widget || last; | ||
| 153 | $menu = $item->get_parent || last; | ||
| 154 | } | ||
| 155 | return undef; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _get_path { | ||
| 159 | my ($menu) = @_; | ||
| 160 | ### MenuView Menu _get_path(): $menu | ||
| 161 | if ($menu->isa('Gtk2::Ex::MenuView')) { | ||
| 162 | ### self | ||
| 163 | return Gtk2::TreePath->new; # empty path | ||
| 164 | } | ||
| 165 | my $item = $menu->get_attach_widget || do { | ||
| 166 | ### oops, unattached menu | ||
| 167 | return undef; | ||
| 168 | }; | ||
| 169 | return ($menu->item_get_mmpi($item))[2]; | ||
| 170 | } | ||
| 171 | |||
| 172 | # mmpi from the menu or submenu | ||
| 173 | # sub get_mmpi { | ||
| 174 | # my ($menu) = @_; | ||
| 175 | # ### MenuView Menu get_mmpi: $menu | ||
| 176 | # if ($menu->isa('Gtk2::Ex::MenuView')) { | ||
| 177 | # ### self | ||
| 178 | # return ($menu, | ||
| 179 | # $menu->{'model'}, | ||
| 180 | # Gtk2::TreePath->new, | ||
| 181 | # undef); | ||
| 182 | # } | ||
| 183 | # my $item = $menu->get_attach_widget || do { | ||
| 184 | # ### oops, unattached menu | ||
| 185 | # return undef; | ||
| 186 | # }; | ||
| 187 | # return $menu->item_get_mmpi ($item); | ||
| 188 | # } | ||
| 189 | |||
| 190 | #------------------------------------------------------------------------------ | ||
| 191 | |||
| 192 | sub _item_get_index { | ||
| 193 | my ($menu, $item) = @_; | ||
| 194 | my $children = $menu->{'children'}; | ||
| 195 | foreach my $i (0 .. $#$children) { | ||
| 196 | if (my $child = $children->[$i]) { | ||
| 197 | if ($item == $child | ||
| 198 | || ($item == ($child->{'Gtk2::Ex::MenuView.separator'}||0))) { | ||
| 199 | return $i; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | return undef; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub item_get_path { | ||
| 207 | my ($class, $item) = @_; | ||
| 208 | return ($class->item_get_mmpi($item))[2]; | ||
| 209 | } | ||
| 210 | sub item_get_mmpi { | ||
| 211 | my ($class, $item) = @_; | ||
| 212 | ### Menu item_get_mmpi(): "$item" | ||
| 213 | |||
| 214 | # unless (ref $class_or_menu) { | ||
| 215 | # my $menu = $item->get_parent || return; | ||
| 216 | # ### class method, recurse to menu: "$menu" | ||
| 217 | # return $menu->item_get_mmpi ($item); | ||
| 218 | # } | ||
| 219 | |||
| 220 | my $path = Gtk2::TreePath->new; | ||
| 221 | for (;;) { | ||
| 222 | my $menu = $item->get_parent || do { | ||
| 223 | ### oops, unattached item | ||
| 224 | return; | ||
| 225 | }; | ||
| 226 | my $i = _item_get_index ($menu, $item); | ||
| 227 | if (! defined $i) { | ||
| 228 | ### oops, item not a child | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | $path->prepend_index ($i); | ||
| 232 | |||
| 233 | if ($menu->isa('Gtk2::Ex::MenuView')) { | ||
| 234 | ### path: $path->to_string | ||
| 235 | my $model = $menu->{'model'} || do { | ||
| 236 | ### oops, no model in menuview | ||
| 237 | return; | ||
| 238 | }; | ||
| 239 | my $iter = $model->get_iter($path) || do { | ||
| 240 | ### oops, no iter for path | ||
| 241 | return; | ||
| 242 | }; | ||
| 243 | return ($menu, $model, $path, $iter); | ||
| 244 | } | ||
| 245 | |||
| 246 | $item = $menu->get_attach_widget || do { | ||
| 247 | ### oops, unattached menu | ||
| 248 | return; | ||
| 249 | }; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | # sub insert { | ||
| 254 | # my ($self, $child, $pos) = @_; | ||
| 255 | # if ($pos >= 0 && $pos < $self->{'model_pos'}) { | ||
| 256 | # $self->{'model_pos'}--; | ||
| 257 | # } | ||
| 258 | # $self->SUPER::insert ($child, $pos); | ||
| 259 | # } | ||
| 260 | # sub prepend { | ||
| 261 | # my ($self, $child) = @_; | ||
| 262 | # $self->{'model_pos'}--; | ||
| 263 | # $self->SUPER::prepend ($child); | ||
| 264 | # } | ||
| 265 | |||
| 266 | 1; | ||
| 267 | __END__ |