| 1 | package Gtk2::Ex::FormFactory; | ||
| 2 | |||
| 3 | $VERSION = "0.66"; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use base qw( Gtk2::Ex::FormFactory::Container ); | ||
| 8 | |||
| 9 | use Gtk2; | ||
| 10 | use Gtk2::Ex::FormFactory::Loader; | ||
| 11 | use Scalar::Util; | ||
| 12 | |||
| 13 | sub get_type { "form_factory" } | ||
| 14 | |||
| 15 | use Gtk2::Ex::FormFactory::Context; | ||
| 16 | use Gtk2::Ex::FormFactory::Layout; | ||
| 17 | use Gtk2::Ex::FormFactory::Rules; | ||
| 18 | |||
| 19 | sub get_context { shift->{context} } | ||
| 20 | sub get_sync { shift->{sync} } | ||
| 21 | sub get_layouter { shift->{layouter} } | ||
| 22 | sub get_rule_checker { shift->{rule_checker} } | ||
| 23 | sub get_gtk_size_groups { shift->{gtk_size_groups} } | ||
| 24 | sub get_parent_ff { shift->{parent_ff} } | ||
| 25 | sub get_widgets_by_name { shift->{widgets_by_name} } | ||
| 26 | sub get_buffered { shift->{buffered} } | ||
| 27 | |||
| 28 | sub set_context { shift->{context} = $_[1] } | ||
| 29 | sub set_sync { shift->{sync} = $_[1] } | ||
| 30 | sub set_layouter { shift->{layouter} = $_[1] } | ||
| 31 | sub set_rule_checker { shift->{rule_checker} = $_[1] } | ||
| 32 | sub set_gtk_size_groups { shift->{gtk_size_groups} = $_[1] } | ||
| 33 | sub set_parent_ff { shift->{parent_ff} = $_[1] } | ||
| 34 | sub set_widgets_by_name { shift->{widgets_by_name} = $_[1] } | ||
| 35 | sub set_buffered { shift->{buffered} = $_[1] } | ||
| 36 | |||
| 37 | sub get_form_factory { shift } | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $class = shift; | ||
| 41 | my %par = @_; | ||
| 42 | my ($context, $parent_ff, $sync, $layouter, $rule_checker) = | ||
| 43 | @par{'context','parent_ff','sync','layouter','rule_checker'}; | ||
| 44 | |||
| 45 | my $self = $class->SUPER::new(@_); | ||
| 46 | |||
| 47 | $sync = 1 unless defined $sync; | ||
| 48 | $context ||= Gtk2::Ex::FormFactory::Context->new; | ||
| 49 | $layouter ||= Gtk2::Ex::FormFactory::Layout->new; | ||
| 50 | $rule_checker ||= Gtk2::Ex::FormFactory::Rules->new; | ||
| 51 | |||
| 52 | $self->set_parent_ff ($parent_ff); | ||
| 53 | $self->set_context ($context); | ||
| 54 | $self->set_sync ($sync); | ||
| 55 | $self->set_layouter ($layouter); | ||
| 56 | $self->set_rule_checker ($rule_checker); | ||
| 57 | $self->set_gtk_size_groups ({}); | ||
| 58 | $self->set_widgets_by_name ({}); | ||
| 59 | $self->set_buffered (1); | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub cleanup { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | $self->SUPER::cleanup(@_); | ||
| 68 | |||
| 69 | $self->set_gtk_size_groups({}); | ||
| 70 | $self->set_widgets_by_name({}); | ||
| 71 | |||
| 72 | 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub open { | ||
| 76 | my $self = shift; | ||
| 77 | my %par = @_; | ||
| 78 | my ($hide) = $par{'hide'}; | ||
| 79 | |||
| 80 | #-- First build all widgets as implemented in the Widget class | ||
| 81 | $self->build(); | ||
| 82 | |||
| 83 | #-- Now show all widgets, if we shouldn't keep the hided | ||
| 84 | $self->show if not $hide; | ||
| 85 | |||
| 86 | 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub build { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | return if $self->get_built; | ||
| 93 | |||
| 94 | #-- first register all widgets of this FormFactory | ||
| 95 | #-- to resolve cross references between Widgets | ||
| 96 | #-- during building. | ||
| 97 | $self->register_all_widgets($self); | ||
| 98 | |||
| 99 | #-- Now call Container's build | ||
| 100 | $self->SUPER::build(); | ||
| 101 | |||
| 102 | 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub show { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | #-- Show all widgets | ||
| 109 | foreach my $child ( @{$self->get_content} ) { | ||
| 110 | $child->get_gtk_parent_widget->show_all; | ||
| 111 | } | ||
| 112 | |||
| 113 | #-- And finally connect the changed signals - this need to be | ||
| 114 | #-- done *after* showing the widgets. For containers ->show() | ||
| 115 | #-- could trigger updates which cause object updates. Since | ||
| 116 | #-- ->update() isn't called yet, this would invalidate the | ||
| 117 | #-- object's state. | ||
| 118 | $self->connect_signals; | ||
| 119 | |||
| 120 | 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub update { | ||
| 124 | my $self = shift; | ||
| 125 | $self->update_all; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub ok { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | $self->apply; | ||
| 132 | $self->close; | ||
| 133 | |||
| 134 | 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub apply { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | if ( $self->get_sync ) { | ||
| 141 | $self->commit_proxy_buffers_all; | ||
| 142 | } else { | ||
| 143 | $self->apply_changes_all; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub cancel { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | $self->discard_proxy_buffers_all if $self->get_sync; | ||
| 153 | $self->close; | ||
| 154 | |||
| 155 | 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub close { | ||
| 159 | my $self = shift; | ||
| 160 | |||
| 161 | $_->get_gtk_parent_widget->destroy for @{$self->get_content}; | ||
| 162 | |||
| 163 | $self->cleanup; | ||
| 164 | |||
| 165 | 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub register_all_widgets { | ||
| 169 | my $self = shift; | ||
| 170 | my ($widget) = @_; | ||
| 171 | |||
| 172 | if ( $widget->isa("Gtk2::Ex::FormFactory::Container") ) { | ||
| 173 | $self->register_widget($widget); | ||
| 174 | foreach my $child ( @{$widget->get_content} ) { | ||
| 175 | $self->register_all_widgets($child); | ||
| 176 | } | ||
| 177 | } else { | ||
| 178 | $self->register_widget($widget); | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub register_widget { | ||
| 185 | my $self = shift; | ||
| 186 | my ($widget) = @_; | ||
| 187 | |||
| 188 | $self->get_widgets_by_name->{$widget->get_name} = $widget; | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | print "UNBUFFERED: widget=".$widget->get_name." object=".$widget->get_object."\n" | ||
| 193 | if $widget->get_object && | ||
| 194 | !$self->get_context | ||
| 195 | ->get_proxy($widget->get_object) | ||
| 196 | ->get_buffered; | ||
| 197 | print "BUFFERED: widget=".$widget->get_name." object=".$widget->get_object."\n" | ||
| 198 | if $widget->get_object && | ||
| 199 | $self->get_context | ||
| 200 | ->get_proxy($widget->get_object) | ||
| 201 | ->get_buffered; | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | $self->set_buffered(0) if $widget->get_object && | ||
| 206 | !$self->get_context | ||
| 207 | ->get_proxy($widget->get_object) | ||
| 208 | ->get_buffered; | ||
| 209 | |||
| 210 | 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub get_form_factory_gtk_window { | ||
| 214 | my $self = shift; | ||
| 215 | |||
| 216 | my $gtk_window; | ||
| 217 | foreach my $child ( @{$self->get_content} ) { | ||
| 218 | if ( $child->isa("Gtk2::Ex::FormFactory::Window") ) { | ||
| 219 | $gtk_window = $child->get_gtk_parent_widget; | ||
| 220 | last; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | return $gtk_window; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub open_confirm_window { | ||
| 228 | my $self = shift; | ||
| 229 | my %par = @_; | ||
| 230 | my ($message, $yes_callback, $no_callback, $position, $with_cancel) = | ||
| 231 | @par{'message','yes_callback','no_callback','position','with_cancel'}; | ||
| 232 | my ($yes_label, $no_label) = | ||
| 233 | @par{'yes_label','no_label'}; | ||
| 234 | |||
| 235 | $position ||= "center-on-parent"; | ||
| 236 | $yes_label ||= "gtk-yes"; | ||
| 237 | $no_label ||= "gtk-no"; | ||
| 238 | |||
| 239 | my $confirm = Gtk2::MessageDialog->new_with_markup ( | ||
| 240 | $self->get_form_factory_gtk_window, | ||
| 241 | ["modal","destroy-with-parent"], | ||
| 242 | "question", | ||
| 243 | "none", | ||
| 244 | $message | ||
| 245 | ); | ||
| 246 | |||
| 247 | $confirm->add_buttons ("gtk-cancel", "cancel") if $with_cancel; | ||
| 248 | $confirm->add_buttons ($no_label, "no"); | ||
| 249 | $confirm->add_buttons ($yes_label, "yes"); | ||
| 250 | |||
| 251 | $confirm->signal_connect("response", sub { | ||
| 252 | my ($widget, $answer) = @_; | ||
| 253 | if ( $answer eq 'yes' ) { | ||
| 254 | &$yes_callback() if $yes_callback; | ||
| 255 | } | ||
| 256 | if ( $answer eq 'no' ) { | ||
| 257 | &$no_callback() if $no_callback; | ||
| 258 | } | ||
| 259 | $widget->destroy; | ||
| 260 | 1; | ||
| 261 | }); | ||
| 262 | |||
| 263 | $confirm->set_position ($position); | ||
| 264 | $confirm->show; | ||
| 265 | |||
| 266 | 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub open_message_window { | ||
| 270 | my $self = shift; | ||
| 271 | my %par = @_; | ||
| 272 | my ($type, $message, $position) = | ||
| 273 | @par{'type','message','position'}; | ||
| 274 | |||
| 275 | $position ||= "center-on-parent"; | ||
| 276 | $type ||= "info"; | ||
| 277 | |||
| 278 | my $confirm = Gtk2::MessageDialog->new_with_markup ( | ||
| 279 | $self->get_form_factory_gtk_window, | ||
| 280 | ["modal","destroy-with-parent"], | ||
| 281 | $type, | ||
| 282 | "ok", | ||
| 283 | $message | ||
| 284 | ); | ||
| 285 | |||
| 286 | $confirm->signal_connect("response", sub { | ||
| 287 | my ($widget) = @_; | ||
| 288 | $widget->destroy; | ||
| 289 | 1; | ||
| 290 | }); | ||
| 291 | $confirm->set_position ($position); | ||
| 292 | $confirm->show; | ||
| 293 | |||
| 294 | 1; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub get_image_path { | ||
| 298 | my $class = shift; | ||
| 299 | my ($filename) = @_; | ||
| 300 | |||
| 301 | foreach my $dir ( @INC ) { | ||
| 302 | return "$dir/$filename" if -f "$dir/$filename"; | ||
| 303 | } | ||
| 304 | |||
| 305 | return; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub change_mouse_cursor { | ||
| 309 | my $self = shift; | ||
| 310 | my ($type, $gtk_window) = @_; | ||
| 311 | |||
| 312 | $gtk_window ||= $self->get_form_factory_gtk_window; | ||
| 313 | return unless $gtk_window; | ||
| 314 | |||
| 315 | my $cursor = $type ? Gtk2::Gdk::Cursor->new($type) : undef; | ||
| 316 | $gtk_window->window->set_cursor($cursor); | ||
| 317 | Gtk2->main_iteration while Gtk2->events_pending; | ||
| 318 | |||
| 319 | 1; | ||
| 320 | } | ||
| 321 | |||
| 322 | 1; | ||
| 323 | |||
| 324 | __END__ |