| 1 | package QWizard::Generator::Gtk2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | my $VERSION = '3.15'; | ||
| 5 | use Gtk2 -init; | ||
| 6 | require Exporter; | ||
| 7 | use QWizard::Generator; | ||
| 8 | use IO::File; | ||
| 9 | |||
| 10 | @QWizard::Generator::Gtk2::ISA = qw(Exporter QWizard::Generator); | ||
| 11 | |||
| 12 | # use strict yells if we use an unquoted FALSE | ||
| 13 | use Glib qw(FALSE TRUE); | ||
| 14 | |||
| 15 | my $have_gd_graph = eval { require GD::Graph::lines; }; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $type = shift; | ||
| 19 | my ($class) = ref($type) || $type; | ||
| 20 | my $self = {'keep_working_hook' => \&QWizard::Generator::backup_params}; | ||
| 21 | |||
| 22 | # collect arguments | ||
| 23 | for (my $i = 0; $i <= $#_; $i += 2) { | ||
| 24 | $self->{$_[$i]} = $_[$i+1]; | ||
| 25 | } | ||
| 26 | bless($self, $class); | ||
| 27 | |||
| 28 | # register our various handlers | ||
| 29 | $self->add_handler('text',\&QWizard::Generator::Gtk2::do_entry, | ||
| 30 | [['single','name'], | ||
| 31 | ['default'], | ||
| 32 | ['forced','0'], | ||
| 33 | ['single','size'], | ||
| 34 | ['single','maxsize'], | ||
| 35 | ['single','submit'], | ||
| 36 | ['single','refresh_on_change']]); | ||
| 37 | $self->add_handler('checkbox',\&QWizard::Generator::Gtk2::do_checkbox, | ||
| 38 | [['multi','values'], | ||
| 39 | ['default'], | ||
| 40 | ['single','name'], | ||
| 41 | ['single','submit'], | ||
| 42 | ['single','refresh_on_change'], | ||
| 43 | ['single','button_label']]); | ||
| 44 | $self->add_handler('label', | ||
| 45 | \&QWizard::Generator::Gtk2::do_label, | ||
| 46 | [['multi','values']]); | ||
| 47 | $self->add_handler('radio', | ||
| 48 | \&QWizard::Generator::Gtk2::do_radio, | ||
| 49 | [['values,labels', " "], | ||
| 50 | ['default'], | ||
| 51 | ['single','name'], | ||
| 52 | ['single','submit'], | ||
| 53 | ['single','refresh_on_change'], | ||
| 54 | ['single','icons'], | ||
| 55 | ['single','noiconpadding'], | ||
| 56 | ]); | ||
| 57 | $self->add_handler('paragraph', | ||
| 58 | \&QWizard::Generator::Gtk2::do_paragraph, | ||
| 59 | [['multi','values'], | ||
| 60 | ['single','preformatted'], | ||
| 61 | ['single','width']]); | ||
| 62 | $self->add_handler('hidetext',\&QWizard::Generator::Gtk2::do_entry, | ||
| 63 | [['single','name'], | ||
| 64 | ['default'], | ||
| 65 | ['forced','1'], | ||
| 66 | ['single','size'], | ||
| 67 | ['single','maxsize'], | ||
| 68 | ['single','submit'], | ||
| 69 | ['single','refresh_on_change']]); | ||
| 70 | $self->add_handler('textbox',\&QWizard::Generator::Gtk2::do_textbox, | ||
| 71 | [['single','name'], | ||
| 72 | ['default'], | ||
| 73 | ['single','size'], | ||
| 74 | ['single','maxsize'], | ||
| 75 | ['single','submit'], | ||
| 76 | ['single','refresh_on_change']]); | ||
| 77 | $self->add_handler('menu', | ||
| 78 | \&QWizard::Generator::Gtk2::do_menu, | ||
| 79 | [['values,labels'], | ||
| 80 | ['default'], | ||
| 81 | ['single','name'], | ||
| 82 | ['single','submit'], | ||
| 83 | ['single','refresh_on_change']]); | ||
| 84 | $self->add_handler('unknown', | ||
| 85 | \&QWizard::Generator::Gtk2::do_unknown, | ||
| 86 | []); | ||
| 87 | $self->add_handler('table', | ||
| 88 | \&QWizard::Generator::Gtk2::do_table, | ||
| 89 | [['norecurse','values'], | ||
| 90 | ['norecurse','headers']]); | ||
| 91 | $self->add_handler('bar', | ||
| 92 | \&QWizard::Generator::Gtk2::do_bar, | ||
| 93 | [['norecurse','values']]); | ||
| 94 | $self->add_handler('image', | ||
| 95 | \&QWizard::Generator::Gtk2::do_image, | ||
| 96 | [['norecurse','imgdata'], | ||
| 97 | ['norecurse','image'], | ||
| 98 | ['single','imagealt'], | ||
| 99 | ['single','addscalebuttons']]); | ||
| 100 | $self->add_handler('graph', | ||
| 101 | \&QWizard::Generator::Gtk2::do_graph, | ||
| 102 | [['norecurse','values'], | ||
| 103 | ['norecursemulti','graph_options']]); | ||
| 104 | $self->add_handler('multi_checkbox', | ||
| 105 | \&QWizard::Generator::Gtk2::do_multicheckbox, | ||
| 106 | [['multi','default'], | ||
| 107 | ['values,labels'], | ||
| 108 | ['single','refresh_on_change']]); | ||
| 109 | $self->add_handler('button', | ||
| 110 | \&QWizard::Generator::Gtk2::do_button, | ||
| 111 | [['single','values'], | ||
| 112 | ['default'], | ||
| 113 | ['single','icon'], | ||
| 114 | ['single','padding'], | ||
| 115 | ['noexpand','runcode']]); | ||
| 116 | $self->add_handler('fileupload', | ||
| 117 | \&QWizard::Generator::Gtk2::do_fileupload, | ||
| 118 | [['single','name'], | ||
| 119 | ['default']]); | ||
| 120 | $self->add_handler('filedownload', | ||
| 121 | \&QWizard::Generator::Gtk2::do_filedownload, | ||
| 122 | [['single','name'], | ||
| 123 | ['default'], | ||
| 124 | ['single','data'], | ||
| 125 | ['noexpand','datafn']]); | ||
| 126 | $self->init_default_storage(); | ||
| 127 | |||
| 128 | # force some default image search paths | ||
| 129 | $self->{'imgpaths'} = [] if (!exists($self->{'imgpaths'})); | ||
| 130 | $self->{'imgpaths'} = [$self->{'imgpaths'}] | ||
| 131 | if (ref($self->{'imgpaths'}) ne 'ARRAY'); | ||
| 132 | push @{$self->{'imgpaths'}}, '/usr/share/icons/Bluecurve/16x16/stock/'; | ||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub our_exit { | ||
| 137 | Gtk2->main_quit; | ||
| 138 | exit; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub create_qw_label { | ||
| 142 | my ($self, $text, $indent, $icon, $activatewidget, $noimagespacing) = @_; | ||
| 143 | my $label = Gtk2::Label->new($text); | ||
| 144 | $noimagespacing ||= 0; | ||
| 145 | $label->set_line_wrap(TRUE); | ||
| 146 | $label->set_justify('GTK_JUSTIFY_LEFT'); | ||
| 147 | $label->set_padding(($indent ? 30 : 10), 1); | ||
| 148 | $label->set_alignment(0, 0); | ||
| 149 | $label->set_selectable(TRUE); | ||
| 150 | |||
| 151 | # set with an accelerator if we have a widget to bind to | ||
| 152 | if ($activatewidget) { | ||
| 153 | $label->set_markup_with_mnemonic($text); | ||
| 154 | $label->set_mnemonic_widget($activatewidget); | ||
| 155 | } | ||
| 156 | |||
| 157 | my $iconpath = $self->find_image_path($icon); | ||
| 158 | |||
| 159 | # no icon? -> We're done | ||
| 160 | if (!$icon || ! -f $iconpath) { | ||
| 161 | my ($padx, $pady) = $label->get_padding(); | ||
| 162 | $label->set_padding($padx + $noimagespacing, $pady); | ||
| 163 | # if both wantarray && they requested an icon, return an array | ||
| 164 | # (can't use just wantarray since func(create_qw_label("hi")) | ||
| 165 | # doesn't work for simple calling usage) | ||
| 166 | return (undef, $label, undef) if (wantarray && $icon); | ||
| 167 | return $label; | ||
| 168 | } | ||
| 169 | |||
| 170 | # if an icon is requested then create an icon to display to the | ||
| 171 | # left of the text | ||
| 172 | my $hbox = Gtk2::HBox->new(FALSE, 6); | ||
| 173 | my $image = Gtk2::Image->new_from_file($iconpath); | ||
| 174 | $hbox->pack_start(Gtk2::Image->new_from_file($iconpath), FALSE, FALSE, 0) | ||
| 175 | if ($iconpath); | ||
| 176 | $hbox->pack_start($label, FALSE, FALSE, 0); | ||
| 177 | |||
| 178 | return ($hbox, $label, $image) if (wantarray); | ||
| 179 | return $hbox; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub goto_top { | ||
| 183 | my $self = shift; | ||
| 184 | $self->{'generator'}->remove_all_table_entries(); | ||
| 185 | $self->{'qwizard'}->reset_qwizard(); | ||
| 186 | Gtk2->main_quit(); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub call_callbacks { | ||
| 190 | # call callbacks | ||
| 191 | if (exists($_[0]->{'qwsubwidgets'})) { | ||
| 192 | foreach my $subwid (@{$_[0]->{'qwsubwidgets'}}) { | ||
| 193 | # XXX: pass $_[1] or something to remove or is it auto-cleaned? | ||
| 194 | call_callbacks($subwid); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | if (exists($_[0]->{'qwend'})) { | ||
| 198 | $_[0]->{'qwend'}->($_[0]); | ||
| 199 | } | ||
| 200 | # remove widget | ||
| 201 | if ($_[1]) { | ||
| 202 | $_[1]->remove($_[0]); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | # for each widget in a table, allow a callback to be run | ||
| 207 | sub remove_table_entries { | ||
| 208 | my $tbl = shift; | ||
| 209 | $tbl->foreach(\&call_callbacks, $tbl); | ||
| 210 | } | ||
| 211 | |||
| 212 | # for each of the tables that populated the last screen, allow | ||
| 213 | # callbacks for each of them to to be run. | ||
| 214 | sub remove_all_table_entries { | ||
| 215 | my $self = shift; | ||
| 216 | foreach my $table (@{$self->{'tables'}}) { | ||
| 217 | remove_table_entries($table); | ||
| 218 | } | ||
| 219 | |||
| 220 | # if we had previous multiple tables, we need to delete all but | ||
| 221 | # the first one. | ||
| 222 | while ($#{$self->{'removals'}} > 0) { | ||
| 223 | my $obj = pop(@{$self->{'removals'}}); | ||
| 224 | my $from = $obj->{'removefrom'} || $self->{'vbox'}; | ||
| 225 | $from->remove($obj); | ||
| 226 | } | ||
| 227 | @{$self->{'tables'}} = ($self->{'tables'}[0]); | ||
| 228 | @{$self->{'frames'}} = ($self->{'frames'}[0]); | ||
| 229 | @{$self->{'removals'}} = ($self->{'removals'}[0]); | ||
| 230 | $self->{'qtable'} = $self->{'tables'}[0]; | ||
| 231 | $self->{'qframe'} = $self->{'frames'}[0]; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Note: NOT called as a generator-> object. First arg is expected to | ||
| 235 | # be a widget. | ||
| 236 | sub goto_refresh { | ||
| 237 | my $self = shift; | ||
| 238 | $self->{'generator'}{'doingrefresh'} = 1; | ||
| 239 | $self->{'generator'}->qwparam('redo_screen',1); | ||
| 240 | goto_next($self, @_); | ||
| 241 | } | ||
| 242 | |||
| 243 | # Note: NOT called as a generator-> object. First arg is expected to | ||
| 244 | # be a widget. | ||
| 245 | sub goto_next { | ||
| 246 | my $self = shift; | ||
| 247 | my $generator = $self->{'generator'}; | ||
| 248 | if ($self->{'qbuttonname'}) { | ||
| 249 | $self->{'generator'}->qwparam($self->{'qbuttonname'}, | ||
| 250 | $self->{'qbuttonval'}); | ||
| 251 | } | ||
| 252 | $generator->remove_all_table_entries(); | ||
| 253 | $generator->{'window'}->window->set_cursor($generator->{'pausecursor'}); | ||
| 254 | Gtk2->main_quit(); | ||
| 255 | } | ||
| 256 | |||
| 257 | # Note: NOT called as a generator-> object. First arg is expected to | ||
| 258 | # be a widget. | ||
| 259 | sub goto_prev { | ||
| 260 | my ($self) = @_; | ||
| 261 | $self->{'generator'}->revert_params(); | ||
| 262 | $self->{'generator'}->remove_all_table_entries(); | ||
| 263 | Gtk2->main_quit(); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub our_mainloop { | ||
| 267 | my ($self, $wiz, $p) = @_; | ||
| 268 | $self->{'progwindow'}->hide if ($self->{'progwindow'}); | ||
| 269 | $self->{'window'}->show_all; | ||
| 270 | if ($self->{'nointro'}) { | ||
| 271 | $self->{'introsw'}->hide; | ||
| 272 | $self->{'gtk2intro'}->hide; | ||
| 273 | } | ||
| 274 | if ($self->qwparam('allow_refresh') || | ||
| 275 | $wiz->{'allow_refresh'} || | ||
| 276 | $p->{'allow_refresh'}) { | ||
| 277 | $self->{'refreshbut'}->show(); | ||
| 278 | } else { | ||
| 279 | $self->{'refreshbut'}->hide(); | ||
| 280 | } | ||
| 281 | $self->{'window'}->window->set_cursor($self->{'normalcursor'}); | ||
| 282 | $self->{'mainscrolledwindow'}->get_vscrollbar()->set_value(0) | ||
| 283 | if (!$self->{'doingrefresh'}); | ||
| 284 | $self->{'doingrefresh'} = 0; | ||
| 285 | Gtk2->main; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub add_qframe { | ||
| 289 | my ($self, $title, $where) = @_; | ||
| 290 | $where ||= $self->{'vbox'}; | ||
| 291 | $self->{'qtable'} = Gtk2::Table->new(3, 3, FALSE); | ||
| 292 | push @{$self->{'tables'}}, $self->{'qtable'}; | ||
| 293 | if (defined($title)) { | ||
| 294 | $self->{'qframe'} = Gtk2::Frame->new($title); | ||
| 295 | $self->{'qframe'}->set_border_width(5); | ||
| 296 | $self->{'qframe'}->add($self->{'qtable'}); | ||
| 297 | $self->{'qframe'}{'removefrom'} = $where; | ||
| 298 | $where->pack_start($self->{'qframe'}, FALSE, FALSE, 0); | ||
| 299 | push @{$self->{'frames'}}, $self->{'qframe'}; | ||
| 300 | push @{$self->{'removals'}}, $self->{'qframe'}; | ||
| 301 | } else { | ||
| 302 | $where->pack_start($self->{'qtable'}, FALSE, FALSE, 0); | ||
| 303 | $self->{'qtable'}{'removefrom'} = $where; | ||
| 304 | push @{$self->{'removals'}}, $self->{'qtable'}; | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | sub get_scrolled_vbox { | ||
| 309 | my ($self) = shift; | ||
| 310 | |||
| 311 | my $sw = Gtk2::ScrolledWindow->new(); | ||
| 312 | $sw->set_policy('automatic','automatic'); | ||
| 313 | |||
| 314 | my $vb = Gtk2::VBox->new(FALSE, 6); | ||
| 315 | $sw->add_with_viewport($vb); | ||
| 316 | |||
| 317 | return ($vb, $sw); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub init_screen { | ||
| 321 | my ($self, $wiz, $title) = @_; | ||
| 322 | if (!$self->{'window'}) { | ||
| 323 | |||
| 324 | # create the window object itself | ||
| 325 | $self->{'window'} = Gtk2::Window->new('toplevel'); | ||
| 326 | $self->{'window'}->set_title($title); | ||
| 327 | $self->{'window'}->set_border_width(5); | ||
| 328 | $self->{'window'}->set_default_size(900,650); | ||
| 329 | $self->{'window'}->signal_connect(delete_event => \&our_exit); | ||
| 330 | # $self->{'window'}->signal_connect(destroy => \&our_exit); | ||
| 331 | |||
| 332 | # create cursors | ||
| 333 | $self->{'pausecursor'} = Gtk2::Gdk::Cursor->new('watch'); | ||
| 334 | $self->{'normalcursor'} = Gtk2::Gdk::Cursor->new('arrow'); | ||
| 335 | |||
| 336 | ## parentvbox: the master vbox | ||
| 337 | # contains the topbar, the main widgets (mainhbox), and the buttons | ||
| 338 | $self->{'parentvbox'} = Gtk2::VBox->new(FALSE, 6); | ||
| 339 | $self->{'window'}->add($self->{'parentvbox'}); | ||
| 340 | |||
| 341 | ## the top bar | ||
| 342 | # is a table | ||
| 343 | $self->{'topbar'} = Gtk2::Table->new(4,4,FALSE); | ||
| 344 | $self->{'parentvbox'}->pack_start($self->{'topbar'}, FALSE, FALSE, 0); | ||
| 345 | |||
| 346 | # mainhbox: The main widget section, after the title | ||
| 347 | $self->{'mainhbox'} = Gtk2::HBox->new(FALSE,3); | ||
| 348 | $self->{'parentvbox'}->pack_start($self->{'mainhbox'}, TRUE, TRUE, 0); | ||
| 349 | |||
| 350 | ## packing the left/center/right frames | ||
| 351 | # children of the mainhbox | ||
| 352 | # | ||
| 353 | # the left frame of the hbox | ||
| 354 | $self->{'outerpane'} = Gtk2::HPaned->new(); | ||
| 355 | $self->{'mainhbox'}->pack_start($self->{'outerpane'}, | ||
| 356 | TRUE, TRUE, 0); | ||
| 357 | $self->{'mainleftpane'} = Gtk2::HPaned->new(); | ||
| 358 | $self->{'outerpane'}->pack1($self->{'mainleftpane'}, TRUE, TRUE); | ||
| 359 | |||
| 360 | ($self->{'leftvbox'}, | ||
| 361 | $self->{'leftscrolledwindow'}) = $self->get_scrolled_vbox(); | ||
| 362 | |||
| 363 | #Gtk2::VBox->new(FALSE,3); | ||
| 364 | $self->{'mainleftpane'}->pack1($self->{'leftscrolledwindow'}, | ||
| 365 | FALSE, TRUE); | ||
| 366 | $self->{'lefttable'} = Gtk2::Table->new(4,4,FALSE); | ||
| 367 | $self->{'leftvbox'}->pack_start($self->{'lefttable'}, FALSE, FALSE, 0); | ||
| 368 | |||
| 369 | # the center frame box | ||
| 370 | $self->{'centervbox'} = Gtk2::VBox->new(FALSE,3); | ||
| 371 | $self->{'mainleftpane'}->pack2($self->{'centervbox'}, TRUE, TRUE); | ||
| 372 | |||
| 373 | # the right frame of the hbox | ||
| 374 | ($self->{'rightvbox'}, | ||
| 375 | $self->{'rightscrolledwindow'}) = $self->get_scrolled_vbox(); | ||
| 376 | |||
| 377 | $self->{'outerpane'}->pack2($self->{'rightscrolledwindow'}, | ||
| 378 | FALSE, TRUE); | ||
| 379 | $self->{'righttable'} = Gtk2::Table->new(4,4,FALSE); | ||
| 380 | $self->{'rightvbox'}->pack_start($self->{'righttable'}, | ||
| 381 | FALSE, FALSE, 0); | ||
| 382 | |||
| 383 | ## packing the center widget box | ||
| 384 | |||
| 385 | # the title name | ||
| 386 | $self->{'gtk2title'} = Gtk2::Label->new($title); | ||
| 387 | $self->{'centervbox'}->pack_start($self->{'gtk2title'}, | ||
| 388 | FALSE, FALSE, 0); | ||
| 389 | |||
| 390 | # the scrolled window that makes up the questions | ||
| 391 | ($self->{'vbox'}, $self->{'mainscrolledwindow'}) = | ||
| 392 | $self->get_scrolled_vbox('main', $self->{'centervbox'}); | ||
| 393 | |||
| 394 | $self->{'centervbox'}->pack_start($self->{'mainscrolledwindow'}, | ||
| 395 | TRUE, TRUE, 0); | ||
| 396 | |||
| 397 | # XXX: the label ends up being too tall; not sure why | ||
| 398 | # (it's not the frame; removing that it's still too big) | ||
| 399 | ($self->{'introvbox'}, $self->{'introsw'}) = | ||
| 400 | $self->get_scrolled_vbox(); | ||
| 401 | |||
| 402 | # the introduction | ||
| 403 | $self->{'gtk2intro'} = $self->create_qw_label(''); | ||
| 404 | $self->{'gtk2intro'}->set_max_width_chars(80); | ||
| 405 | $self->{'gtk2intro'}->set_line_wrap(TRUE); | ||
| 406 | $self->{'introvbox'}->pack_start($self->{'gtk2intro'}, FALSE, FALSE, 0); | ||
| 407 | $self->{'vbox'}->pack_start($self->{'introsw'}, FALSE, FALSE, 0); | ||
| 408 | |||
| 409 | # bottom row buts done in do_ok_cancel | ||
| 410 | # get added as children of the parentvbox to be full width on bottom | ||
| 411 | |||
| 412 | $self->add_qframe(''); | ||
| 413 | } | ||
| 414 | $self->initialize_auto_accelerator(); | ||
| 415 | } | ||
| 416 | |||
| 417 | sub process_gtk2_events { | ||
| 418 | Gtk2->main_iteration_do(FALSE); # once at least to be sure | ||
| 419 | while (Gtk2->events_pending) { | ||
| 420 | Gtk2->main_iteration_do(FALSE); | ||
| 421 | } | ||
| 422 | Gtk2::Gdk->flush; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub finished { | ||
| 426 | my $self = shift; | ||
| 427 | if ($self->{'window'}) { | ||
| 428 | $self->{'window'}->hide(); | ||
| 429 | |||
| 430 | $self->process_gtk2_events; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | sub set_progress { | ||
| 435 | my ($self, $progress, $text) = @_; | ||
| 436 | if (!$self->{'progress'}) { | ||
| 437 | $self->{'progwindow'} = Gtk2::Window->new('toplevel'); | ||
| 438 | $self->{'progress'} = Gtk2::ProgressBar->new; | ||
| 439 | return if (!$self->{'progress'} || !$self->{'progress'}); | ||
| 440 | $self->{'progwindow'}->add($self->{'progress'}); | ||
| 441 | $self->{'progwindow'}->set_title("Progress"); | ||
| 442 | if ($self->{'window'}) { | ||
| 443 | my ($x, $y) = $self->{'window'}->get_position(); | ||
| 444 | my ($w, $h) = $self->{'window'}->get_size(); | ||
| 445 | $self->{'progwindow'}->move($x + int($w/2), $y + int($h/2)); | ||
| 446 | } | ||
| 447 | $self->{'progwindow'}->show_all; | ||
| 448 | } | ||
| 449 | $self->{'progress'}->set_text($text || (int(100*$progress) . "%")); | ||
| 450 | $self->{'progress'}->show(); | ||
| 451 | $self->{'progress'}->set_fraction($progress); | ||
| 452 | $self->process_gtk2_events; | ||
| 453 | } | ||
| 454 | |||
| 455 | # | ||
| 456 | # note: new widgets added here and memorized for if() statements need to be | ||
| 457 | # discarded from the self hash in finished(); | ||
| 458 | # | ||
| 459 | sub do_ok_cancel { | ||
| 460 | my ($self, $nexttext, $wiz, $p) = @_; | ||
| 461 | if (!$self->{'bot'}) { | ||
| 462 | my $hb; | ||
| 463 | $self->{'bot'} = Gtk2::HBox->new (FALSE, 6); | ||
| 464 | $self->{'bot'}->set_border_width(3); | ||
| 465 | $self->{'parentvbox'}->pack_start($self->{'bot'}, FALSE, FALSE, 0); | ||
| 466 | |||
| 467 | if (!$self->{'prevbut'}) { | ||
| 468 | $self->{'prevbut'} = Gtk2::Button->new(); | ||
| 469 | ($hb, $self->{'prevbutlab'}, $self->{'prevbuticon'}) = | ||
| 470 | $self->create_qw_label($wiz->{'back_text'} || '_Back', | ||
| 471 | FALSE, 'gtk-go-back.png', $self->{'prevbut'}); | ||
| 472 | $self->{'prevbut'}->add($hb || $self->{'prevbutlab'}); | ||
| 473 | |||
| 474 | $self->{'bot'}->pack_start($self->{'prevbut'}, FALSE, FALSE, 0); | ||
| 475 | $self->{'prevbut'}->signal_connect(clicked => \&goto_prev); | ||
| 476 | $self->{'prevbut'}->{'generator'} = $self; | ||
| 477 | } | ||
| 478 | if (!$self->{'nextbut'}) { | ||
| 479 | $self->{'nextbut'} = Gtk2::Button->new(); | ||
| 480 | ($hb, $self->{'nextbutlab'}, $self->{'nextbuticon'}) = | ||
| 481 | $self->create_qw_label($nexttext || $wiz->{'next_text'} || '_Next', | ||
| 482 | FALSE, 'gtk-go-forward.png', $self->{'nextbut'}); | ||
| 483 | $self->{'nextbut'}->add($hb || $self->{'nextbutlab'}); | ||
| 484 | $self->{'bot'}->pack_start($self->{'nextbut'}, FALSE, FALSE, 0); | ||
| 485 | $self->{'nextbut'}->signal_connect(clicked => \&goto_next); | ||
| 486 | $self->{'nextbut'}->{'generator'} = $self; | ||
| 487 | } | ||
| 488 | if (!$self->{'refreshbut'}) { | ||
| 489 | $self->{'refreshbut'} = Gtk2::Button->new(); | ||
| 490 | ($hb, $self->{'refreshbutlab'}, $self->{'refreshbuticon'}) = | ||
| 491 | $self->create_qw_label('_Refresh', | ||
| 492 | FALSE, 'gtk-refresh.png', $self->{'refreshbut'}); | ||
| 493 | $self->{'refreshbut'}->add($hb || $self->{'refreshbutlab'}); | ||
| 494 | |||
| 495 | $self->{'refreshbut'} = Gtk2::Button->new('_Refresh'); | ||
| 496 | $self->{'bot'}->pack_start($self->{'refreshbut'}, FALSE, FALSE, 0); | ||
| 497 | $self->{'refreshbut'}->signal_connect(clicked => \&goto_refresh); | ||
| 498 | $self->{'refreshbut'}->{'generator'} = $self; | ||
| 499 | } | ||
| 500 | if (!$self->{'canbut'} && !$self->{'no_cancel'}) { | ||
| 501 | $self->{'canbut'} = Gtk2::Button->new(); | ||
| 502 | ($hb, $self->{'cancelbutlab'}, $self->{'cancelbuticon'}) = | ||
| 503 | $self->create_qw_label($wiz->qwparam('QWizard_Cancel') || | ||
| 504 | $wiz->{'cancel_text'} || 'Cancel', | ||
| 505 | FALSE, 'gtk-cancel.png', $self->{'cancelbut'}); | ||
| 506 | $self->{'canbut'}->add($hb || $self->{'cancelbutlab'}); | ||
| 507 | |||
| 508 | $self->{'bot'}->pack_end($self->{'canbut'}, FALSE, FALSE, 0); | ||
| 509 | $self->{'canbut'}->signal_connect(clicked => \&goto_top); | ||
| 510 | $self->{'canbut'}->{'generator'} = $self; | ||
| 511 | $self->{'canbut'}->{'qwizard'} = $wiz; | ||
| 512 | } | ||
| 513 | } else { | ||
| 514 | $self->{'nextbutlab'}->set_markup_with_mnemonic($nexttext || 'Ok'); | ||
| 515 | } | ||
| 516 | |||
| 517 | # see if we have backup places to get to. If not, grey out the button | ||
| 518 | if ($#{$self->{'backupvars'}} > -1) { | ||
| 519 | $self->{'prevbut'}->set_sensitive(TRUE); | ||
| 520 | } else { | ||
| 521 | $self->{'prevbut'}->set_sensitive(FALSE); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | # put stuff at a particular spot in the current table | ||
| 527 | sub put_it { | ||
| 528 | my ($self, $w, $row, $col) = @_; | ||
| 529 | |||
| 530 | # try and determine the correct spot to put something in the | ||
| 531 | # resulting widget table. Callers could have passed a spot | ||
| 532 | # (rare), or we might be slowly building and incromenteing | ||
| 533 | # counters to get to the right spot. | ||
| 534 | if (!$row) { | ||
| 535 | if (exists($self->{'currentrow'}) && defined($self->{'currentrow'})) { | ||
| 536 | $row = $self->{'currentrow'}; | ||
| 537 | } else { | ||
| 538 | $row = $self->{'currentq'}; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | if (!$col) { | ||
| 542 | if (exists($self->{'currentcol'}) && defined($self->{'currentcol'})) { | ||
| 543 | $col = $self->{'currentcol'}; | ||
| 544 | } else { | ||
| 545 | $col = 2; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | # If the widget is a simple string, then we create a label out of it. | ||
| 550 | if (ref($w) eq '') { | ||
| 551 | $w = $self->create_qw_label($w); | ||
| 552 | $w->set_alignment(0, 0) if ($col == 1); | ||
| 553 | } | ||
| 554 | |||
| 555 | # Finally, place the widget in the table at the calculated spot | ||
| 556 | # column 1 is filled, and all others are fill and expanded both | ||
| 557 | |||
| 558 | if (ref($self->{'qtable'}) eq 'Gtk2::Table') { | ||
| 559 | # Parent widget is a table. | ||
| 560 | # Pack it in our calculated spot | ||
| 561 | $self->{'qtable'}->attach($w, $col, $col+1, $row, $row+1, | ||
| 562 | ($col == 1 ? [qw(fill)] : [qw(fill)]), | ||
| 563 | ($col == 1 ? [qw(fill)] : [qw(fill)]), | ||
| 564 | # ($col == 1 ? [qw(fill)] : [qw(fill expand)]), | ||
| 565 | # ($col == 1 ? [qw(fill)] : [qw(fill expand)]), | ||
| 566 | ($col == 1 ? 1 : 0), | ||
| 567 | 0); | ||
| 568 | } elsif (ref($self->{'qtable'}) =~ /Gtk2::.Box/) { | ||
| 569 | # container is a box. Packet it at the start. | ||
| 570 | # XXX: allow filling/expanding/etc. | ||
| 571 | $self->{'qtable'}->pack_start($w, FALSE, FALSE, 0); | ||
| 572 | print "box: " . ref($self->{'qtable'}) . "\n"; | ||
| 573 | } else { | ||
| 574 | # assume a generic single container. Probably shouldn't be | ||
| 575 | # getting here. | ||
| 576 | $self->{'qtable'}->add($w); | ||
| 577 | } | ||
| 578 | |||
| 579 | # Remember the last widget. | ||
| 580 | # 1) used for tables that need to remember a list of sub-widgets | ||
| 581 | # 2) XXX: future key bindings | ||
| 582 | $self->{'lastwidget'} = $w; | ||
| 583 | |||
| 584 | # # bind the tab and alt-tab key presses to forward and backward widgets | ||
| 585 | # if (ref($w) =~ /Entry|Menu|Text|Button|Checkbutton|Radio|Optionmenu/) { | ||
| 586 | # if ($self->{'lastw'}) { | ||
| 587 | # $self->{'lastw'}->bind('<Tab>',[\&tab_next, $w, $self]); | ||
| 588 | # $w->bind('<Alt-Key-Tab>',[\&tab_next, $self->{'lastw'}, $self]); | ||
| 589 | # } | ||
| 590 | # $self->{'lastw'} = $w; | ||
| 591 | # } | ||
| 592 | } | ||
| 593 | |||
| 594 | sub set_default { | ||
| 595 | my ($self, $q, $def) = @_; | ||
| 596 | return if (!exists($q->{'name'}) || !defined($def)); | ||
| 597 | $self->qwparam($q->{'name'}, $def); | ||
| 598 | } | ||
| 599 | |||
| 600 | ###################################################################### | ||
| 601 | # QWizard functions for doing stuff. | ||
| 602 | |||
| 603 | sub wait_for { | ||
| 604 | my ($self, $wiz, $next, $p) = @_; | ||
| 605 | $self->do_ok_cancel($next, $wiz, $p); | ||
| 606 | $self->our_mainloop($wiz, $p); | ||
| 607 | |||
| 608 | return 1; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub do_error { | ||
| 612 | my ($self, $q, $wiz, $p, $err) = @_; | ||
| 613 | $self->{'currentq'}++; | ||
| 614 | $self->{'qadd'}++; | ||
| 615 | # XXX: make font red | ||
| 616 | my $lb = $self->create_qw_label($err); | ||
| 617 | $lb->set_markup("<span weight=\"bold\" foreground=\"red\">$err</span>"); | ||
| 618 | $self->put_it($lb, undef, 2); | ||
| 619 | } | ||
| 620 | |||
| 621 | sub do_question { | ||
| 622 | my ($self, $q, $wiz, $p, $text, $qcount) = @_; | ||
| 623 | my $top = $self->{'qtable'}; | ||
| 624 | my $l; | ||
| 625 | $self->{'currentq'} = $qcount + $self->{'qadd'}; | ||
| 626 | |||
| 627 | # | ||
| 628 | # Get the actual help text, in case this is a subroutine. | ||
| 629 | # | ||
| 630 | my $helpdesc = $q->{'helpdesc'}; | ||
| 631 | if (ref($helpdesc) eq "CODE") { | ||
| 632 | $helpdesc = $helpdesc->(); | ||
| 633 | } | ||
| 634 | |||
| 635 | # add the question | ||
| 636 | my $hf = Gtk2::HBox->new(); | ||
| 637 | $l = $self->create_qw_label($text, $q->{'indent'}); | ||
| 638 | # pulled from something else... use or not? | ||
| 639 | # $l->set_alignment(0, 0); | ||
| 640 | $self->{'lastquestion'} = $l; | ||
| 641 | $self->{'lasttext'} = $text; | ||
| 642 | $hf->pack_start($l, FALSE,FALSE,0); | ||
| 643 | |||
| 644 | # add a help bubble if needed | ||
| 645 | if ($q->{'helptext'}) { | ||
| 646 | # XXX Ideally make this button smaller. | ||
| 647 | my $but = Gtk2::Button->new('[?]'); | ||
| 648 | $but->set_relief('none'); | ||
| 649 | $but->signal_connect(clicked => \&do_helptext_button); | ||
| 650 | $but->{'helptext'} = $q->{'helptext'}; | ||
| 651 | $but->{'generator'} = $self; | ||
| 652 | $but->set_border_width(0); | ||
| 653 | $hf->pack_start($but, FALSE, FALSE, 0); | ||
| 654 | } | ||
| 655 | |||
| 656 | if ($helpdesc) { | ||
| 657 | # if (!$self->qwpref('usehelpballons')) { | ||
| 658 | my $f = Gtk2::VBox->new(); | ||
| 659 | |||
| 660 | $f->set_spacing(0); | ||
| 661 | $l->set_padding(($q->{'indent'} ? 30 : 10), 0); | ||
| 662 | $f->pack_start($hf, FALSE,FALSE,0); | ||
| 663 | my $helpl = $self->create_qw_label($helpdesc, $q->{'indent'}); | ||
| 664 | $helpl->set_padding(($q->{'indent'} ? 40 : 20), 0); | ||
| 665 | $f->pack_start($helpl, FALSE, FALSE, 0); | ||
| 666 | $self->put_it($f, undef, 1); | ||
| 667 | # } else { | ||
| 668 | # # XXX: help bubble? | ||
| 669 | # $self->put_it($hf, undef, 1); | ||
| 670 | # } | ||
| 671 | } else { | ||
| 672 | $self->put_it($hf, undef, 1); | ||
| 673 | } | ||
| 674 | } | ||
| 675 | |||
| 676 | sub do_helptext_button { | ||
| 677 | my $but = shift; | ||
| 678 | my $self = $but->{'generator'}; | ||
| 679 | |||
| 680 | # get the text | ||
| 681 | my $helptext = $but->{'helptext'}; | ||
| 682 | if (ref($helptext) eq "CODE") { | ||
| 683 | $helptext = $helptext->(); | ||
| 684 | } | ||
| 685 | |||
| 686 | my $win; | ||
| 687 | $win = Gtk2::Window->new('toplevel'); | ||
| 688 | $win->set_title("Help"); | ||
| 689 | $win->set_border_width(5); | ||
| 690 | $win->set_default_size(300,200); | ||
| 691 | |||
| 692 | my $p = $self->create_qw_label($helptext); | ||
| 693 | $p->set_line_wrap(FALSE); | ||
| 694 | $win->add($p); | ||
| 695 | $win->show_all; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub start_primaries { | ||
| 699 | my $self = shift; | ||
| 700 | |||
| 701 | # forget about past side counts | ||
| 702 | delete $self->{'sides'}; | ||
| 703 | } | ||
| 704 | |||
| 705 | sub end_primaries { | ||
| 706 | my $self = shift; | ||
| 707 | if (!$self->{'sides'}{'righttable'}) { | ||
| 708 | $self->{'oldrightpos'} = $self->{'outerpane'}->get_position(); | ||
| 709 | $self->{'outerpane'}->set_position(4000); | ||
| 710 | } else { | ||
| 711 | my ($w, $h) = $self->{'window'}->get_size(); | ||
| 712 | $self->{'outerpane'}->set_position($self->{'oldrightpos'} || $w-200); | ||
| 713 | } | ||
| 714 | |||
| 715 | if (!$self->{'sides'}{'lefttable'}) { | ||
| 716 | $self->{'oldleftpos'} = $self->{'mainleftpane'}->get_position(); | ||
| 717 | $self->{'mainleftpane'}->set_position(0); | ||
| 718 | } else { | ||
| 719 | $self->{'mainleftpane'}->set_position($self->{'oldleftpos'} || 200); | ||
| 720 | } | ||
| 721 | } | ||
| 722 | |||
| 723 | |||
| 724 | |||
| 725 | sub start_questions { | ||
| 726 | my ($self, $wiz, $p, $title, $intro) = @_; | ||
| 727 | $self->{'gtk2title'}->set_markup("<span size=\"x-large\" underline=\"single\">$title</span>"); | ||
| 728 | # $self->{'gtk2title'}->set_pattern("_" x length($title)); | ||
| 729 | if ($intro) { | ||
| 730 | $self->{'gtk2intro'}->set_text($intro); | ||
| 731 | $self->{'nointro'} = 0; | ||
| 732 | } else { | ||
| 733 | # GRR... can't hide here since a show_all comes later. | ||
| 734 | $self->{'gtk2intro'}->set_label(''); | ||
| 735 | $self->{'nointro'} = 1; | ||
| 736 | } | ||
| 737 | |||
| 738 | $self->{radiogroups} = {}; | ||
| 739 | $self->{'qadd'} = 0; | ||
| 740 | |||
| 741 | return; | ||
| 742 | # XXX: intro | ||
| 743 | $self->{'qintro'}->delete('1.0','end'); | ||
| 744 | if ($intro) { | ||
| 745 | $self->{'qintro'}->configure(-height => length($intro)/80 + 1); | ||
| 746 | $self->{'qintro'}->insert('end',$intro); | ||
| 747 | } else { | ||
| 748 | $self->{'qintro'}->configure(-height => 0); | ||
| 749 | } | ||
| 750 | } | ||
| 751 | |||
| 752 | sub end_questions { | ||
| 753 | my $self = shift; | ||
| 754 | # this makes us keep adding new table rows during a merge | ||
| 755 | $self->{'qadd'} = $self->{'currentq'} + 1; | ||
| 756 | $self->{'lastw'} = undef; | ||
| 757 | } | ||
| 758 | |||
| 759 | |||
| 760 | ################################################## | ||
| 761 | # widgets | ||
| 762 | ################################################## | ||
| 763 | |||
| 764 | sub start_bar { | ||
| 765 | my ($self, $wiz, $name) = @_; | ||
| 766 | |||
| 767 | $self->add_qframe($name); | ||
| 768 | $self->{'inbar'} = 1; | ||
| 769 | } | ||
| 770 | |||
| 771 | sub end_bar { | ||
| 772 | my ($self, $wiz, $name) = @_; | ||
| 773 | |||
| 774 | $self->add_qframe($name); | ||
| 775 | delete($self->{'inbar'}); | ||
| 776 | } | ||
| 777 | |||
| 778 | sub do_bar { | ||
| 779 | my ($self, $q, $wiz, $p, $widgets) = @_; | ||
| 780 | |||
| 781 | $self->start_bar($wiz, undef); | ||
| 782 | $self->do_a_table([$widgets], $self->{'qtable'}, -1, $wiz, $q, $p); | ||
| 783 | $self->end_bar($wiz, ''); | ||
| 784 | } | ||
| 785 | |||
| 786 | sub do_top_bar { | ||
| 787 | my ($self, $q, $wiz, $p, $widgets) = @_; | ||
| 788 | |||
| 789 | my $oldtable = $self->{'qtable'}; | ||
| 790 | $self->{'qtable'} = $self->{'topbar'}; | ||
| 791 | push @{$self->{'tables'}}, $self->{'qtable'}; | ||
| 792 | $self->do_a_table([$widgets], $self->{'qtable'}, -1, $wiz, $q, $p); | ||
| 793 | $self->{'qtable'} = $oldtable; | ||
| 794 | } | ||
| 795 | |||
| 796 | sub do_side { | ||
| 797 | my ($self, $spot, $q, $wiz, $p, $widgets) = @_; | ||
| 798 | |||
| 799 | my $oldtable = $self->{'qtable'}; | ||
| 800 | $self->{'qtable'} = $self->{$spot}; | ||
| 801 | |||
| 802 | push @{$self->{'tables'}}, $self->{'qtable'}; | ||
| 803 | |||
| 804 | my @tableinfo; | ||
| 805 | foreach my $w (@$widgets) { | ||
| 806 | next if (!$w); | ||
| 807 | if (ref($w) eq 'ARRAY') { | ||
| 808 | |||
| 809 | # special side grouping | ||
| 810 | # XXX: make border stand out. | ||
| 811 | |||
| 812 | my $title = ""; | ||
| 813 | |||
| 814 | my $sidetable = $self->{'qtable'}; | ||
| 815 | $title = shift(@$w) if (ref($w->[0]) eq ''); | ||
| 816 | |||
| 817 | $self->add_qframe($title, $self->{'leftvbox'}); | ||
| 818 | |||
| 819 | if (0) { | ||
| 820 | # old OTHER way to create a box | ||
| 821 | my $vb = Gtk2::VBox->new(FALSE, 10); | ||
| 822 | my $sidetable = $self->{'qtable'}; | ||
| 823 | $self->{'qtable'} = $vb; | ||
| 824 | |||
| 825 | # An encapsulated set maybe with a title | ||
| 826 | if (ref($w->[0]) eq '') { | ||
| 827 | my $title = $self->create_qw_label(""); | ||
| 828 | $title->set_markup("<span size=\"large\" underline=\"single\">" . shift(@$w) . "</span>"); | ||
| 829 | $vb->pack_start($title, FALSE, FALSE, 0); | ||
| 830 | } | ||
| 831 | } | ||
| 832 | |||
| 833 | foreach my $widget (@$w) { | ||
| 834 | #print "widget: $widget\n"; | ||
| 835 | $wiz->ask_question($p, $widget); | ||
| 836 | } | ||
| 837 | $self->{'qtable'} = $sidetable; | ||
| 838 | |||
| 839 | ## OLD $self->{'leftvbox'}->pack_start($vb, FALSE, FALSE, 0); | ||
| 840 | } else { | ||
| 841 | push @tableinfo, [$w]; | ||
| 842 | } | ||
| 843 | $self->{'sides'}{$spot}++; | ||
| 844 | } | ||
| 845 | |||
| 846 | $self->do_a_table(\@tableinfo, $self->{'qtable'}, -1, $wiz, $q, $p); | ||
| 847 | $self->{'qtable'} = $oldtable; | ||
| 848 | } | ||
| 849 | |||
| 850 | sub do_left_side { | ||
| 851 | my $self = shift; | ||
| 852 | $self->do_side('lefttable', @_); | ||
| 853 | } | ||
| 854 | |||
| 855 | sub do_right_side { | ||
| 856 | my $self = shift; | ||
| 857 | $self->do_side('righttable', @_); | ||
| 858 | } | ||
| 859 | |||
| 860 | sub do_button { | ||
| 861 | my ($self, $q, $wiz, $p, $vals, $def, $icon, $padding, $runcode) = @_; | ||
| 862 | $vals = $self->add_accelerator($vals); | ||
| 863 | |||
| 864 | my $but = Gtk2::Button->new(); | ||
| 865 | my $butlab = | ||
| 866 | $self->create_qw_label($vals, FALSE, $icon, $but); | ||
| 867 | $butlab->set_padding($padding || 3, 0); | ||
| 868 | $but->add($butlab); | ||
| 869 | |||
| 870 | # how the heck do you make small buttons in gtk2??? | ||
| 871 | # $but->set_border_width(0); | ||
| 872 | # $but->set_relief('GTK_RELIEF_NONE'); | ||
| 873 | # $but->set_size_request(40,32); | ||
| 874 | |||
| 875 | # this is breaking the QWizard spirit somewhat because it can't | ||
| 876 | # work easily via the web and must-refresh pages, but for GUI only | ||
| 877 | # allow their own callback function instead | ||
| 878 | if (defined($runcode) && ref($runcode) eq 'CODE') { | ||
| 879 | $but->signal_connect(clicked => $runcode); | ||
| 880 | } else { | ||
| 881 | $but->signal_connect(clicked => \&goto_next); | ||
| 882 | } | ||
| 883 | |||
| 884 | $but->{'qbuttonname'} = $q->{'name'}; | ||
| 885 | $but->{'qbuttonval'} = $def || $q->{'default'}; # XXX: hack for refresh; doesn't deal with code refs properly though. ugh. | ||
| 886 | $but->{'generator'} = $self; | ||
| 887 | $self->put_it($but); | ||
| 888 | } | ||
| 889 | |||
| 890 | sub do_filedownload { | ||
| 891 | my ($self, $q, $wiz, $p, $name, $def, $data, $datafn) = @_; | ||
| 892 | |||
| 893 | # A file upload box is created via a button to request a file. | ||
| 894 | |||
| 895 | my $but = Gtk2::Button->new($def || 'Output To File...'); | ||
| 896 | $but->signal_connect(clicked => \&create_filedownload_screen); | ||
| 897 | $but->{'qbuttonname'} = $q->{'name'}; | ||
| 898 | $but->{'qbuttonval'} = $def; | ||
| 899 | $but->{'generator'} = $self; | ||
| 900 | $but->{'parent_button'} = $but; | ||
| 901 | $but->{'data'} = $data; | ||
| 902 | $but->{'datafn'} = $datafn; | ||
| 903 | $but->{'q'} = $q; | ||
| 904 | $but->{'wiz'} = $wiz; | ||
| 905 | $but->{'p'} = $p; | ||
| 906 | $self->put_it($but); | ||
| 907 | } | ||
| 908 | |||
| 909 | sub do_fileupload { | ||
| 910 | my ($self, $q, $wiz, $p, $name, $def) = @_; | ||
| 911 | |||
| 912 | # A file upload box is created via a button to request a file. | ||
| 913 | |||
| 914 | my $but = Gtk2::Button->new($def || 'Select File...'); | ||
| 915 | $but->signal_connect(clicked => \&create_fileupload_screen); | ||
| 916 | $but->{'qbuttonname'} = $q->{'name'}; | ||
| 917 | $but->{'qbuttonval'} = $def; | ||
| 918 | $but->{'generator'} = $self; | ||
| 919 | $but->{'parent_button'} = $but; | ||
| 920 | $self->put_it($but); | ||
| 921 | } | ||
| 922 | |||
| 923 | sub create_fileupload_screen { | ||
| 924 | my $parent_button = shift; | ||
| 925 | |||
| 926 | # create the widget screen | ||
| 927 | my $fs = Gtk2::FileSelection->new("Select a file"); | ||
| 928 | |||
| 929 | # define the action for the Ok button. | ||
| 930 | my $ok = $fs->ok_button; | ||
| 931 | $ok->{'pwidget'} = $fs; | ||
| 932 | $ok->{'generator'} = $parent_button->{'generator'}; | ||
| 933 | $ok->{'qwname'} = $parent_button->{'qbuttonname'}; | ||
| 934 | $ok->{'parent_button'} = $parent_button; | ||
| 935 | $ok->signal_connect('clicked' => | ||
| 936 | sub { | ||
| 937 | my $val = $_[0]{'pwidget'}->get_filename(); | ||
| 938 | # save the value | ||
| 939 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, | ||
| 940 | $val); | ||
| 941 | # close the widget | ||
| 942 | $_[0]->{'pwidget'}->hide_all; | ||
| 943 | |||
| 944 | # change the button text | ||
| 945 | # (truncate just to file name first). | ||
| 946 | $val =~ s/.*\///; | ||
| 947 | $_[0]->{'parent_button'}->set_label($val); | ||
| 948 | }); | ||
| 949 | |||
| 950 | # define the action for the Cancel button. | ||
| 951 | my $can = $fs->cancel_button; | ||
| 952 | $can->{'pwidget'} = $fs; | ||
| 953 | $can->{'generator'} = $parent_button->{'generator'}; | ||
| 954 | $can->{'qwname'} = $parent_button->{'qwname'}; | ||
| 955 | $can->signal_connect('clicked' => | ||
| 956 | sub { | ||
| 957 | # close the widget | ||
| 958 | $_[0]->{'pwidget'}->hide_all; | ||
| 959 | }); | ||
| 960 | $fs->show_all; | ||
| 961 | } | ||
| 962 | |||
| 963 | sub create_filedownload_screen { | ||
| 964 | my ($parent_button) = @_; | ||
| 965 | |||
| 966 | # create the widget screen | ||
| 967 | my $fs = Gtk2::FileSelection->new("Select a file"); | ||
| 968 | |||
| 969 | # define the action for the Ok button. | ||
| 970 | my $ok = $fs->ok_button; | ||
| 971 | $ok->{'pwidget'} = $fs; | ||
| 972 | $ok->{'generator'} = $parent_button->{'generator'}; | ||
| 973 | $ok->{'qwname'} = $parent_button->{'qbuttonname'}; | ||
| 974 | $ok->{'data'} = $parent_button->{'data'}; | ||
| 975 | $ok->{'datafn'} = $parent_button->{'datafn'}; | ||
| 976 | $ok->{'q'} = $parent_button->{'q'}; | ||
| 977 | $ok->{'p'} = $parent_button->{'p'}; | ||
| 978 | $ok->{'wiz'} = $parent_button->{'wiz'}; | ||
| 979 | $ok->{'parent_button'} = $parent_button; | ||
| 980 | $ok->signal_connect('clicked' => | ||
| 981 | sub { | ||
| 982 | my $but = $_[0]; | ||
| 983 | |||
| 984 | # open the file to save the data in | ||
| 985 | my $filename = $but->{'pwidget'}->get_filename(); | ||
| 986 | # save the value | ||
| 987 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, | ||
| 988 | $filename); | ||
| 989 | my $fileh = new IO::File; | ||
| 990 | $fileh->open(">" . $filename); | ||
| 991 | |||
| 992 | |||
| 993 | # save the question data field | ||
| 994 | if ($but->{'data'}) { | ||
| 995 | print $fileh $but->{'data'}; | ||
| 996 | } | ||
| 997 | |||
| 998 | # call the datafn routine as well | ||
| 999 | if ($but->{'datafn'} && | ||
| 1000 | ref($but->{'datafn'}) eq 'CODE') { | ||
| 1001 | $but->{'datafn'}($fileh, $filename, | ||
| 1002 | $but->{'wiz'}, | ||
| 1003 | $but->{'p'}, | ||
| 1004 | $but->{'q'}); | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | # close the output file | ||
| 1008 | $fileh->close(); | ||
| 1009 | |||
| 1010 | # close the widget | ||
| 1011 | $_[0]->{'pwidget'}->hide_all; | ||
| 1012 | }); | ||
| 1013 | |||
| 1014 | # define the action for the Cancel button. | ||
| 1015 | my $can = $fs->cancel_button; | ||
| 1016 | $can->{'pwidget'} = $fs; | ||
| 1017 | $can->{'generator'} = $parent_button->{'generator'}; | ||
| 1018 | $can->{'qwname'} = $parent_button->{'qwname'}; | ||
| 1019 | $can->signal_connect('clicked' => | ||
| 1020 | sub { | ||
| 1021 | # close the widget | ||
| 1022 | $_[0]->{'pwidget'}->hide_all; | ||
| 1023 | }); | ||
| 1024 | $fs->show_all; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | sub check_callback { | ||
| 1028 | if ($_[0]->get_active) { | ||
| 1029 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, | ||
| 1030 | $_[0]->{'qwvals'}[0]); | ||
| 1031 | } else { | ||
| 1032 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, | ||
| 1033 | $_[0]->{'qwvals'}[1]); | ||
| 1034 | } | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | sub do_checkbox { | ||
| 1038 | my ($self, $q, $wiz, $p, $vals, $def, $name, | ||
| 1039 | $submit, $refresh_on_change, $checkbutton_label) = @_; | ||
| 1040 | $vals = [1, 0] if ($#$vals == -1); | ||
| 1041 | my $cb = Gtk2::CheckButton->new(); | ||
| 1042 | if ($checkbutton_label) { | ||
| 1043 | $cb->set_label($checkbutton_label); | ||
| 1044 | } | ||
| 1045 | my $text = $self->add_accelerator($self->{'lasttext'}); | ||
| 1046 | if ($text ne $self->{'lasttext'} || $self->{'lasttext'} =~ /_/) { | ||
| 1047 | $self->{'lastquestion'}->set_markup_with_mnemonic($text); | ||
| 1048 | $self->{'lastquestion'}->set_mnemonic_widget($cb); | ||
| 1049 | } | ||
| 1050 | if (defined($def) && $def eq $vals->[0]) { | ||
| 1051 | $cb->set_active(TRUE); | ||
| 1052 | } | ||
| 1053 | @{$cb->{'qwvals'}} = @$vals; | ||
| 1054 | $cb->{'generator'} = $self; | ||
| 1055 | $cb->{'qwname'} = $name; | ||
| 1056 | $cb->{'qwend'} = \&check_callback; | ||
| 1057 | if ($submit) { | ||
| 1058 | $cb->signal_connect(clicked => \&goto_next); | ||
| 1059 | } | ||
| 1060 | if ($refresh_on_change) { | ||
| 1061 | $cb->signal_connect(clicked => \&goto_refresh); | ||
| 1062 | } | ||
| 1063 | $self->put_it($cb); | ||
| 1064 | $self->set_default($q, $def); | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | # set all buttons to on | ||
| 1068 | sub set_all_boxes { | ||
| 1069 | my $wid = shift; | ||
| 1070 | my $checkboxes = $wid->{'boxes'}; | ||
| 1071 | foreach my $checkbox (@$checkboxes) { | ||
| 1072 | $checkbox->set_active(TRUE); | ||
| 1073 | } | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | # set all buttons to on | ||
| 1077 | sub unset_all_boxes { | ||
| 1078 | my $wid = shift; | ||
| 1079 | my $checkboxes = $wid->{'boxes'}; | ||
| 1080 | foreach my $checkbox (@$checkboxes) { | ||
| 1081 | $checkbox->set_active(FALSE); | ||
| 1082 | } | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | # set all buttons to on | ||
| 1086 | sub toggle_all_boxes { | ||
| 1087 | my $wid = shift; | ||
| 1088 | my $checkboxes = $wid->{'boxes'}; | ||
| 1089 | foreach my $checkbox (@$checkboxes) { | ||
| 1090 | if ($checkbox->get_active) { | ||
| 1091 | $checkbox->set_active(FALSE); | ||
| 1092 | } else { | ||
| 1093 | $checkbox->set_active(TRUE); | ||
| 1094 | } | ||
| 1095 | } | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | sub do_multicheckbox { | ||
| 1099 | my ($self, $q, $wiz, $p, $defs, $vals, $labels, $refresh_on_change) = @_; | ||
| 1100 | my $tf = Gtk2::VBox->new(FALSE, 3); | ||
| 1101 | my $count = -1; | ||
| 1102 | my @buts; | ||
| 1103 | foreach my $v (@$vals) { | ||
| 1104 | $count++; | ||
| 1105 | my $l = (($labels->{$v}) ? $labels->{$v} : "$v"); | ||
| 1106 | my $lb = $self->create_qw_label($l); | ||
| 1107 | my $c = Gtk2::CheckButton->new(); | ||
| 1108 | my $text = $self->add_accelerator($l); | ||
| 1109 | if ($text ne $l || $l =~ /_/) { | ||
| 1110 | $lb->set_markup_with_mnemonic($text); | ||
| 1111 | $lb->set_mnemonic_widget($c); | ||
| 1112 | } | ||
| 1113 | my $hb = Gtk2::HBox->new(FALSE, 0); | ||
| 1114 | |||
| 1115 | $hb->pack_start($c, FALSE, FALSE, 0); | ||
| 1116 | $hb->pack_start($lb, FALSE, FALSE, 0); | ||
| 1117 | $tf->pack_start($hb, FALSE, FALSE, 0); | ||
| 1118 | |||
| 1119 | # special handling when a screen refrsh is in progress | ||
| 1120 | # normally this is handled by QWizard itself with the default | ||
| 1121 | # flag, but for multi-checkboxes we have more than one name. | ||
| 1122 | |||
| 1123 | if ($wiz->qwparam('redoing_now')) { | ||
| 1124 | $c->set_active(TRUE) if ($wiz->qwparam($q->{'name'} . $v) eq $v); | ||
| 1125 | } else { | ||
| 1126 | # set the value now | ||
| 1127 | $self->{'datastore'}->set($q->{'name'} . $v, $defs->[$count]); | ||
| 1128 | |||
| 1129 | # test to see if the current default value matches and | ||
| 1130 | # then turn the checkbox to on. | ||
| 1131 | if ($defs->[$count] eq $v) { | ||
| 1132 | $c->set_active(TRUE); | ||
| 1133 | } | ||
| 1134 | } | ||
| 1135 | $c->{'qwvals'} = [$v,undef]; | ||
| 1136 | $c->{'generator'} = $self; | ||
| 1137 | $c->{'qwname'} = $q->{'name'} . $v; | ||
| 1138 | $c->{'qwend'} = \&check_callback; | ||
| 1139 | if ($refresh_on_change) { | ||
| 1140 | $c->signal_connect(clicked => \&goto_refresh); | ||
| 1141 | } | ||
| 1142 | push @buts, $c; | ||
| 1143 | |||
| 1144 | push @{$wiz->{'passvars'}},$q->{'name'} . $v; | ||
| 1145 | push @{$tf->{'qwsubwidgets'}}, $c; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | if (!$q->{'notoggles'}) { | ||
| 1149 | my $hb = Gtk2::HBox->new(FALSE, 0); | ||
| 1150 | |||
| 1151 | my $but = Gtk2::Button->new('Set All'); | ||
| 1152 | $but->signal_connect(clicked => \&set_all_boxes); | ||
| 1153 | $but->{'boxes'} = \@buts; | ||
| 1154 | $hb->pack_start($but, FALSE, FALSE, 0); | ||
| 1155 | |||
| 1156 | $but = Gtk2::Button->new('Unset All'); | ||
| 1157 | $but->signal_connect(clicked => \&unset_all_boxes); | ||
| 1158 | $but->{'boxes'} = \@buts; | ||
| 1159 | $hb->pack_start($but, FALSE, FALSE, 0); | ||
| 1160 | |||
| 1161 | $but = Gtk2::Button->new('Toggle All'); | ||
| 1162 | $but->signal_connect(clicked => \&toggle_all_boxes); | ||
| 1163 | $but->{'boxes'} = \@buts; | ||
| 1164 | $hb->pack_start($but, FALSE, FALSE, 0); | ||
| 1165 | |||
| 1166 | $tf->pack_start($hb, FALSE, FALSE, 0); | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | $self->put_it($tf); | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | sub do_radio { | ||
| 1173 | my ($self, $q, $wiz, $p, $vals, $labels, $def, $name, | ||
| 1174 | $submit, $refresh_on_change, $icons, $noiconpadding) = @_; | ||
| 1175 | my $vb = Gtk2::VBox->new(); | ||
| 1176 | my (@ws); | ||
| 1177 | |||
| 1178 | foreach my $v (@$vals) { | ||
| 1179 | my $text = (($labels->{$v}) ? $labels->{$v} : "$v"); | ||
| 1180 | $text = $self->add_accelerator($text); | ||
| 1181 | my $rb = Gtk2::RadioButton->new($self->{'radiogroups'}{$name}); | ||
| 1182 | my $label = $self->create_qw_label($text, FALSE, $icons->{$v}, $rb, | ||
| 1183 | $noiconpadding); | ||
| 1184 | $rb->add($label); | ||
| 1185 | if ($v eq $def) { | ||
| 1186 | $rb->set_active(TRUE); | ||
| 1187 | } | ||
| 1188 | $rb->{'qwname'} = $name; | ||
| 1189 | $rb->{'generator'} = $self; | ||
| 1190 | $rb->{'qwvalue'} = $v; | ||
| 1191 | push @ws, $rb; | ||
| 1192 | if ($submit) { | ||
| 1193 | $rb->signal_connect(clicked => \&goto_next); | ||
| 1194 | } | ||
| 1195 | if ($refresh_on_change) { | ||
| 1196 | $rb->signal_connect(clicked => \&goto_refresh); | ||
| 1197 | } | ||
| 1198 | $vb->pack_start($rb, FALSE, FALSE, 0); | ||
| 1199 | $self->{'radiogroups'}{$name} = $rb->get_group(); | ||
| 1200 | } | ||
| 1201 | $vb->{'rwidgets'} = \@ws; | ||
| 1202 | $vb->{'qwend'} = sub { | ||
| 1203 | foreach my $w (@{$_[0]->{'rwidgets'}}) { | ||
| 1204 | if ($w->get_active()) { | ||
| 1205 | $w->{'generator'}->qwparam($w->{'qwname'}, $w->{'qwvalue'}); | ||
| 1206 | last; | ||
| 1207 | } | ||
| 1208 | } | ||
| 1209 | }; | ||
| 1210 | $self->put_it($vb); | ||
| 1211 | $self->set_default($q, $def); | ||
| 1212 | } | ||
| 1213 | |||
| 1214 | sub do_label { | ||
| 1215 | my ($self, $q, $wiz, $p, $vals, $def) = @_; | ||
| 1216 | if (defined ($vals)) { | ||
| 1217 | foreach my $i (@$vals) { | ||
| 1218 | $self->put_it($i); | ||
| 1219 | } | ||
| 1220 | } | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | sub do_paragraph { | ||
| 1224 | my ($self, $q, $wiz, $p, $vals, $preformatted, $width) = @_; | ||
| 1225 | my $w = $width || 40; | ||
| 1226 | foreach my $i (@$vals) { | ||
| 1227 | my $t; | ||
| 1228 | $t = $self->create_qw_label($i); | ||
| 1229 | if ($preformatted) { | ||
| 1230 | $t->set_line_wrap(FALSE) | ||
| 1231 | } else { | ||
| 1232 | # XXX use width argument to define where to wrap | ||
| 1233 | $t->set_line_wrap(TRUE) | ||
| 1234 | } | ||
| 1235 | $self->put_it($t); | ||
| 1236 | } | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | sub do_menu { | ||
| 1240 | my ($self, $q, $wiz, $p, $vals, $labels, $def, $name, | ||
| 1241 | $submit, $refresh_on_change) = @_; | ||
| 1242 | |||
| 1243 | my $optionmenu = Gtk2::OptionMenu->new(); | ||
| 1244 | $optionmenu->{'generator'} = $self; | ||
| 1245 | $optionmenu->{'qwname'} = $name; | ||
| 1246 | my $menu = Gtk2::Menu->new(); | ||
| 1247 | |||
| 1248 | my $h = 0; | ||
| 1249 | my $activem; | ||
| 1250 | my $activenum = 0; | ||
| 1251 | foreach my $v (@$vals) { | ||
| 1252 | my $mitem; | ||
| 1253 | if ($labels->{$v}) { | ||
| 1254 | $mitem = Gtk2::MenuItem->new($labels->{$v}); | ||
| 1255 | } else { | ||
| 1256 | $mitem = Gtk2::MenuItem->new($v); | ||
| 1257 | } | ||
| 1258 | if ((defined($def) && $def eq $v) || | ||
| 1259 | (!$def && !exists($optionmenu->{'finalval'}))) { | ||
| 1260 | $activem = $mitem; | ||
| 1261 | $activenum = $h; | ||
| 1262 | $optionmenu->{'finalval'} = $v; | ||
| 1263 | $mitem->{'nosubmityet'} = 1; | ||
| 1264 | } | ||
| 1265 | $mitem->{'qwvalue'} = $v; | ||
| 1266 | $mitem->{'finalvalref'} = \$optionmenu->{'finalval'}; | ||
| 1267 | $mitem->{'submit'} = $submit; | ||
| 1268 | $mitem->{'refresh_on_change'} = $refresh_on_change; | ||
| 1269 | $mitem->{'generator'} = $self; | ||
| 1270 | $mitem->signal_connect('activate' => | ||
| 1271 | sub { | ||
| 1272 | # set the final value upon selection | ||
| 1273 | ${$_[0]->{'finalvalref'}} = | ||
| 1274 | $_[0]->{'qwvalue'}; | ||
| 1275 | |||
| 1276 | # auto-submit requested (unless | ||
| 1277 | # still setting up). | ||
| 1278 | goto_next($_[0]) | ||
| 1279 | if ($_[0]->{'submit'} && | ||
| 1280 | !$_[0]->{'nosubmityet'}); | ||
| 1281 | # refresh on change requested (unless | ||
| 1282 | # still setting up). | ||
| 1283 | goto_refresh($_[0]) | ||
| 1284 | if ($_[0]->{'refresh_on_change'} && | ||
| 1285 | !$_[0]->{'nosubmityet'}); | ||
| 1286 | }); | ||
| 1287 | |||
| 1288 | $menu->attach($mitem, 0, 1, $h, $h+1); | ||
| 1289 | $h++; | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | $optionmenu->{'qwend'} = sub { | ||
| 1293 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, $_[0]->{'finalval'}); | ||
| 1294 | }; | ||
| 1295 | $optionmenu->set_menu($menu); | ||
| 1296 | $self->put_it($optionmenu); | ||
| 1297 | if ($activem) { | ||
| 1298 | $menu->set_active($activenum); | ||
| 1299 | $menu->activate_item($activem, 1); | ||
| 1300 | delete $activem->{'nosubmityet'}; | ||
| 1301 | } | ||
| 1302 | $self->set_default($q, $def); | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | sub do_entry { | ||
| 1306 | my ($self, $q, $wiz, $p, $name, $def, $hide, | ||
| 1307 | $size, $maxsize, $submit, $refresh_on_change) = @_; | ||
| 1308 | # XXX: refresh_on_change | ||
| 1309 | # XXX: submit | ||
| 1310 | |||
| 1311 | my $e = Gtk2::Entry->new(); | ||
| 1312 | $e->set_text($def); | ||
| 1313 | $e->{'qwend'} = sub { | ||
| 1314 | $_[0]->{'generator'}->qwparam($name, $_[0]->get_text()); | ||
| 1315 | }; | ||
| 1316 | $e->{'generator'} = $self; | ||
| 1317 | |||
| 1318 | if ($maxsize) { | ||
| 1319 | $e->set_max_length($maxsize); | ||
| 1320 | } | ||
| 1321 | if ($size) { | ||
| 1322 | $e->set_width_chars($size) | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | # | ||
| 1326 | # Set up a value to use if the text shouldn't be echoed to the screen. | ||
| 1327 | # | ||
| 1328 | if ($hide) { | ||
| 1329 | $e->set_invisible_char("*"); | ||
| 1330 | $e->set_visibility(FALSE); | ||
| 1331 | } | ||
| 1332 | |||
| 1333 | $self->put_it($e); | ||
| 1334 | $self->set_default($q, $def); | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | sub do_textbox { | ||
| 1338 | my ($self, $q, $wiz, $p, $name, $def) = @_; | ||
| 1339 | |||
| 1340 | my $tb = Gtk2::TextBuffer->new(); | ||
| 1341 | $tb->set_text($def); | ||
| 1342 | my $tv = Gtk2::TextView->new_with_buffer($tb); | ||
| 1343 | $tv->set_size_request(300,150); | ||
| 1344 | $tv->{'qwend'} = sub { | ||
| 1345 | my @bounds = $_[0]->get_buffer()->get_bounds(); | ||
| 1346 | $_[0]->{'generator'}->qwparam($_[0]->{'qwname'}, | ||
| 1347 | $_[0]->get_buffer()->get_text(@bounds, TRUE)); | ||
| 1348 | }; | ||
| 1349 | $tv->{'generator'} = $self; | ||
| 1350 | $tv->{'qwname'} = $name; | ||
| 1351 | |||
| 1352 | $self->put_it($tv); | ||
| 1353 | $self->set_default($q, $def); | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | sub do_separator { | ||
| 1357 | my ($self, $q, $wiz, $p, $text) = @_; | ||
| 1358 | my $where = $self->{'qf'}; | ||
| 1359 | $self->{'currentq'}++; | ||
| 1360 | $self->{'qadd'}++; | ||
| 1361 | my $lab = Gtk2::Label->new(); | ||
| 1362 | $self->put_it($lab); | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | ################################################## | ||
| 1366 | # Display | ||
| 1367 | ################################################## | ||
| 1368 | |||
| 1369 | sub do_a_table_widget { | ||
| 1370 | my ($self, $wiz, $p, $containedin, $column, $colnum, $rowc) = @_; | ||
| 1371 | |||
| 1372 | my $oldqt = $self->{'qtable'}; | ||
| 1373 | $self->{'qtable'} = $containedin; | ||
| 1374 | |||
| 1375 | my $oldq = $self->{'currentq'}; | ||
| 1376 | |||
| 1377 | my $oldrow = $self->{'currentrow'}; | ||
| 1378 | $self->{'currentrow'} = $rowc; | ||
| 1379 | |||
| 1380 | my $oldc = $self->{'currentcol'}; | ||
| 1381 | $self->{'currentcol'} = $colnum; | ||
| 1382 | |||
| 1383 | my $subname = $wiz->ask_question($p, $column); | ||
| 1384 | push @{$wiz->{'passvars'}}, $subname if ($subname); | ||
| 1385 | push @{$containedin->{'qwsubwidgets'}}, $self->{'lastwidget'}; | ||
| 1386 | |||
| 1387 | $self->{'qtable'} = $oldqt; | ||
| 1388 | $self->{'currentq'} = $oldq; | ||
| 1389 | if ($oldc) { | ||
| 1390 | $self->{'currentcol'} = $oldc; | ||
| 1391 | } else { | ||
| 1392 | delete $self->{'currentcol'}; | ||
| 1393 | } | ||
| 1394 | if ($oldrow) { | ||
| 1395 | $self->{'currentrow'} = $oldrow; | ||
| 1396 | } else { | ||
| 1397 | delete $self->{'currentrow'}; | ||
| 1398 | } | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | |||
| 1402 | sub do_a_table { | ||
| 1403 | my ($self, $table, $parentt, $rowc, $wiz, $q, $p) = @_; | ||
| 1404 | |||
| 1405 | foreach my $row (@$table) { | ||
| 1406 | my $col = 0; | ||
| 1407 | $rowc++; | ||
| 1408 | foreach my $column (@$row) { | ||
| 1409 | if (ref($column) =~ /Gtk2/) { | ||
| 1410 | # a native Gtk2 widget. Just add it. | ||
| 1411 | $parentt->attach_defaults($column, $col, $col + 1, | ||
| 1412 | $rowc, $rowc+1); | ||
| 1413 | $col++; | ||
| 1414 | } elsif (ref($column) eq "ARRAY") { | ||
| 1415 | # sub table | ||
| 1416 | my $newt = Gtk2::Table->new(4,4,FALSE); | ||
| 1417 | $self->do_a_table($column, $newt, -1, $wiz, $q, $p); | ||
| 1418 | $parentt->attach_defaults($newt, $col, $col+1, $rowc, $rowc+1); | ||
| 1419 | $col++; | ||
| 1420 | push @{$parentt->{'qwsubwidgets'}}, $newt; | ||
| 1421 | } elsif (ref($column) eq "HASH") { | ||
| 1422 | $self->do_a_table_widget($wiz, $p, $parentt, $column, | ||
| 1423 | $col, $rowc); | ||
| 1424 | $col++; | ||
| 1425 | } else { | ||
| 1426 | $parentt->attach_defaults($self->create_qw_label($column), | ||
| 1427 | $col, $col + 1, $rowc, $rowc+1); | ||
| 1428 | $col++; | ||
| 1429 | } | ||
| 1430 | } | ||
| 1431 | } | ||
| 1432 | } | ||
| 1433 | |||
| 1434 | sub do_table { | ||
| 1435 | my ($self, $q, $wiz, $p, $table, $headers) = @_; | ||
| 1436 | |||
| 1437 | my $fixed = ($headers) ? 1 : 0; | ||
| 1438 | |||
| 1439 | my $tab = Gtk2::Table->new(4,4,FALSE); | ||
| 1440 | $tab->set_border_width(4); | ||
| 1441 | |||
| 1442 | if ($headers) { | ||
| 1443 | my $col = 0; | ||
| 1444 | foreach my $column (@$headers) { | ||
| 1445 | # XXX: mark up bold? | ||
| 1446 | $tab->attach_defaults($self->create_qw_label($column), | ||
| 1447 | $col, $col+1, 0, 1); | ||
| 1448 | $col++; | ||
| 1449 | } | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | $self->do_a_table($table, $tab, $fixed-1, $wiz, $q, $p); | ||
| 1453 | $self->put_it($tab); | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | sub do_graph { | ||
| 1457 | my $self = shift; | ||
| 1458 | my ($q, $wiz, $p, $data, $gopts) = @_; | ||
| 1459 | |||
| 1460 | # a graph is really a file with a special generator. | ||
| 1461 | $self->do_image($q, $wiz, $p, $self->do_graph_data(@_), undef, "[graph]"); | ||
| 1462 | } | ||
| 1463 | |||
| 1464 | ############################################## | ||
| 1465 | # | ||
| 1466 | sub scale_img { | ||
| 1467 | my $but = shift; | ||
| 1468 | my $img = $but->{'img'}; | ||
| 1469 | my $gdimg = $img->{'origpixbuf'}; | ||
| 1470 | |||
| 1471 | # get curernt image size | ||
| 1472 | my $wid = $gdimg->get_width(); | ||
| 1473 | my $hei = $gdimg->get_height(); | ||
| 1474 | |||
| 1475 | # set the total image size to the sum of all button presses | ||
| 1476 | $img->{'currentsize'} += $but->{'scale'}; | ||
| 1477 | $img->{'currentsize'} = 0 if ($img->{'currentsize'} < 0); | ||
| 1478 | |||
| 1479 | $gdimg = | ||
| 1480 | $gdimg->scale_simple($wid * $img->{'currentsize'}, | ||
| 1481 | $hei * $img->{'currentsize'}, 'GDK_INTERP_NEAREST'); | ||
| 1482 | # $wid = $gdimg->get_width(); | ||
| 1483 | # $hei = $gdimg->get_height(); | ||
| 1484 | $img->set_from_pixbuf($gdimg); | ||
| 1485 | # print "img: $wid x $hei","\n"; | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | sub orig_size { | ||
| 1489 | my $but = shift; | ||
| 1490 | my $img = $but->{'img'}; | ||
| 1491 | my $gdimg = $img->{'origpixbuf'}; | ||
| 1492 | $img->{'scale'} = 1; | ||
| 1493 | $img->set_from_pixbuf($img->{'origpixbuf'}); | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | sub create_scale_but { | ||
| 1497 | my ($hb, $lab, $dist, $img) = @_; | ||
| 1498 | |||
| 1499 | my $but = Gtk2::Button->new($lab); | ||
| 1500 | $but->signal_connect(clicked => \&scale_img); | ||
| 1501 | $but->{'img'} = $img; | ||
| 1502 | $but->{'scale'} = $dist; | ||
| 1503 | $hb->pack_end($but, FALSE, FALSE, 0); | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | sub do_image { | ||
| 1507 | my $self = shift; | ||
| 1508 | my ($q, $wiz, $p, $datastr, $filestr, $imgalt, $scaleopts) = @_; | ||
| 1509 | |||
| 1510 | my $img; | ||
| 1511 | if ($datastr) { | ||
| 1512 | $filestr = $self->create_temp_file('.png',$datastr); | ||
| 1513 | } else { | ||
| 1514 | $filestr = $self->find_image_path($filestr); | ||
| 1515 | } | ||
| 1516 | # image file | ||
| 1517 | $img = Gtk2::Image->new_from_file($filestr); | ||
| 1518 | |||
| 1519 | if ($img) { | ||
| 1520 | my $vb = Gtk2::VBox->new(); | ||
| 1521 | my $but; | ||
| 1522 | |||
| 1523 | if ($scaleopts) { | ||
| 1524 | my $hb = Gtk2::HBox->new(); | ||
| 1525 | |||
| 1526 | create_scale_but($hb, "25% >", .25, $img); | ||
| 1527 | create_scale_but($hb, "10% >", .1, $img); | ||
| 1528 | |||
| 1529 | my $but = Gtk2::Button->new("Original Size"); | ||
| 1530 | $but->signal_connect(clicked => \&orig_size); | ||
| 1531 | $hb->pack_end($but, FALSE, FALSE, 0); | ||
| 1532 | |||
| 1533 | create_scale_but($hb, "< 10%", -.1, $img); | ||
| 1534 | create_scale_but($hb, "< 25%", -.25, $img); | ||
| 1535 | |||
| 1536 | $vb->pack_end($hb, FALSE, FALSE, 0); | ||
| 1537 | } | ||
| 1538 | |||
| 1539 | $vb->pack_end($img, FALSE, FALSE, 0); | ||
| 1540 | |||
| 1541 | $img->{'currentsize'} = 1; | ||
| 1542 | my ($arg1, $arg2) = $img->get_pixbuf(); | ||
| 1543 | |||
| 1544 | $img->{'origpixbuf'} = $arg1; | ||
| 1545 | $but->{'img'} = $img if ($but); | ||
| 1546 | |||
| 1547 | $self->put_it($vb); | ||
| 1548 | } else { | ||
| 1549 | $self->put_it(Gtk2::Label->new($imgalt || "Broken Image")); | ||
| 1550 | } | ||
| 1551 | } | ||
| 1552 | |||
| 1553 | ################################################## | ||
| 1554 | # Trees | ||
| 1555 | ################################################## | ||
| 1556 | |||
| 1557 | sub do_tree { | ||
| 1558 | my ($self, $q, $wiz, $p, $labels, $expand_all, $def) = @_; | ||
| 1559 | |||
| 1560 | my $tv = new Gtk2::TreeView(); | ||
| 1561 | my $mod = new Gtk2::TreeStore('Glib::String'); | ||
| 1562 | $tv->set_model($mod); | ||
| 1563 | |||
| 1564 | my @expand; | ||
| 1565 | if ($def) { | ||
| 1566 | #ensure that the default is initially visible | ||
| 1567 | my $cur = $def; | ||
| 1568 | unshift @expand, $cur; | ||
| 1569 | until ($cur eq $q->{'root'}) { | ||
| 1570 | $cur = get_name($q->{'parent'}->($wiz, $cur)); | ||
| 1571 | if (!$cur) { | ||
| 1572 | # uh oh; can't find path to the default. | ||
| 1573 | print STDERR "QWizard/Gtk2: can't find path to root=$q->{root} for default tree node for: $q->{name}\n"; | ||
| 1574 | @expand = (); | ||
| 1575 | last; | ||
| 1576 | } | ||
| 1577 | unshift @expand, $cur; | ||
| 1578 | } | ||
| 1579 | $self->{'datastore'}->set($q->{'name'}, $def) if $q->{'name'}; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | # | ||
| 1583 | # remember a bunch of important stuff in the view | ||
| 1584 | # | ||
| 1585 | $mod->{'qwdata'}{'qw'} = $wiz; | ||
| 1586 | $mod->{'qwdata'}{'q'} = $q; | ||
| 1587 | $mod->{'qwdata'}{'labels'} = $labels; | ||
| 1588 | $mod->{'qwdata'}{'first'} = 1; | ||
| 1589 | $mod->{'qwdata'}{'expand_all'} = $expand_all; | ||
| 1590 | $mod->{'qwdata'}{'refresh_on_change'} = | ||
| 1591 | $q->{'refresh_on_change'}; | ||
| 1592 | $mod->{'qwdata'}{'generator'} = $wiz->{'generator'}; | ||
| 1593 | $mod->{'qwdata'}{'datamap'}{'0'} = $q->{'root'}; | ||
| 1594 | |||
| 1595 | # | ||
| 1596 | # add the root node | ||
| 1597 | # | ||
| 1598 | my $iter = $mod->append(undef); | ||
| 1599 | $mod->set($iter, 0, $q->{'root'}); | ||
| 1600 | $mod->append($iter); # add bogus child | ||
| 1601 | |||
| 1602 | # | ||
| 1603 | # When a row expands, add all the children | ||
| 1604 | # | ||
| 1605 | $tv->signal_connect("row-expanded", | ||
| 1606 | sub { | ||
| 1607 | my ($treeview,$piter,$path) = @_; | ||
| 1608 | $self->add_children($treeview, $piter, | ||
| 1609 | $path->to_string()); | ||
| 1610 | }); | ||
| 1611 | |||
| 1612 | # | ||
| 1613 | # When a row collapses, remove the sub data (probably not needed | ||
| 1614 | # but cleaner and removes memory) | ||
| 1615 | # | ||
| 1616 | $tv->signal_connect("row-collapsed", | ||
| 1617 | sub { | ||
| 1618 | # remove all children from the tree | ||
| 1619 | my ($treeview,$iter,$path) = @_; | ||
| 1620 | |||
| 1621 | my $model = $treeview->get_model(); | ||
| 1622 | |||
| 1623 | # remove children | ||
| 1624 | while (my $i = $model->iter_children($iter)) { | ||
| 1625 | $model->remove($i); | ||
| 1626 | } | ||
| 1627 | |||
| 1628 | # attach a bogus one to enuser the | ||
| 1629 | # parent has the hierarchial widget | ||
| 1630 | # still. | ||
| 1631 | $model->append($iter); | ||
| 1632 | |||
| 1633 | return 1; | ||
| 1634 | }); | ||
| 1635 | |||
| 1636 | # | ||
| 1637 | # when the cursor changes, we pick this as our select value | ||
| 1638 | # | ||
| 1639 | $tv->signal_connect("cursor-changed", | ||
| 1640 | sub { | ||
| 1641 | my ($path, $col) = $_[0]->get_cursor(); | ||
| 1642 | my $mod = $_[0]->get_model(); | ||
| 1643 | my $qname = $mod->{'qwdata'}{'q'}{'name'}; | ||
| 1644 | my $pname = $path->to_string(); | ||
| 1645 | my $val = $mod->{'qwdata'}{'datamap'}{$pname}; | ||
| 1646 | $mod->{'qwdata'}{'qw'}->qwparam($qname, $val); | ||
| 1647 | if ($mod->{'qwdata'}{'first'}) { | ||
| 1648 | delete($mod->{'qwdata'}{'first'}); | ||
| 1649 | } else { | ||
| 1650 | goto_refresh($mod->{'qwdata'}) | ||
| 1651 | if ($mod->{'qwdata'}{'refresh_on_change'}); | ||
| 1652 | } | ||
| 1653 | }); | ||
| 1654 | |||
| 1655 | |||
| 1656 | # | ||
| 1657 | # Add the rendering type | ||
| 1658 | # | ||
| 1659 | my $col = Gtk2::TreeViewColumn->new; | ||
| 1660 | $col->set_title("Name"); | ||
| 1661 | |||
| 1662 | my $cell = Gtk2::CellRendererText->new; | ||
| 1663 | $col->pack_start ($cell, 1); | ||
| 1664 | $col->add_attribute ($cell, text => 0); | ||
| 1665 | $tv->append_column($col); | ||
| 1666 | |||
| 1667 | # | ||
| 1668 | # Do initial expasion to find the default | ||
| 1669 | # | ||
| 1670 | if ($expand_all ne '0' && $expand_all ne '') { | ||
| 1671 | for (my $i = 0; $i <= (int($expand_all) || 10); $i++) { | ||
| 1672 | $tv->expand_all(); | ||
| 1673 | } | ||
| 1674 | } elsif ($#expand > -1) { | ||
| 1675 | shift @expand; # drop the root node | ||
| 1676 | $tv->{'expand'} = \@expand; | ||
| 1677 | $tv->expand_row(new Gtk2::TreePath("0"), FALSE); | ||
| 1678 | $tv->set_cursor(new Gtk2::TreePath($tv->{'qwdata'}{'cursor'})) | ||
| 1679 | if ($tv->{'qwdata'}{'cursor'}); | ||
| 1680 | } | ||
| 1681 | |||
| 1682 | $self->put_it($tv); | ||
| 1683 | } | ||
| 1684 | |||
| 1685 | sub get_name { | ||
| 1686 | my $node = shift; | ||
| 1687 | |||
| 1688 | if (ref($node) eq 'HASH') { | ||
| 1689 | return $node->{'name'}; | ||
| 1690 | } else { | ||
| 1691 | return $node; | ||
| 1692 | } | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | sub add_children { | ||
| 1696 | my ($self, $tv, $piter, $path) = @_; | ||
| 1697 | my $model = $tv->get_model(); | ||
| 1698 | my $count = 0; | ||
| 1699 | my $q = $model->{'qwdata'}{'q'}; | ||
| 1700 | my $wiz = $model->{'qwdata'}{'qw'}; | ||
| 1701 | my $labels = $model->{'qwdata'}{'labels'}; | ||
| 1702 | my $expand = $tv->{'expand'}; | ||
| 1703 | |||
| 1704 | # get a list of children | ||
| 1705 | my $children = | ||
| 1706 | $q->{'children'}->($wiz, $model->{'qwdata'}{'datamap'}{$path}); | ||
| 1707 | |||
| 1708 | if (!$children || $#$children == -1) { | ||
| 1709 | # probably will never get here if the rest of the code was good... | ||
| 1710 | $model->remove($model->iter_nth_child($piter, 0)); | ||
| 1711 | return; | ||
| 1712 | } | ||
| 1713 | |||
| 1714 | # append the path with new suffixes :N:M:... | ||
| 1715 | $path .= ":" if ($path ne ''); | ||
| 1716 | |||
| 1717 | # add each child node. | ||
| 1718 | foreach my $child (@$children) { | ||
| 1719 | add_node($wiz, $tv, $model, $child, $q, $piter, $labels, | ||
| 1720 | $path . "$count"); | ||
| 1721 | $count++; | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | # remove bogus node | ||
| 1725 | $model->remove($model->iter_nth_child($piter, 0)); | ||
| 1726 | |||
| 1727 | # expading further if needed | ||
| 1728 | $tv->expand_row(new Gtk2::TreePath($tv->{'qwdata'}{'expand_row'}), FALSE) | ||
| 1729 | if (exists($tv->{'qwdata'}{'expand_row'})); | ||
| 1730 | |||
| 1731 | } | ||
| 1732 | |||
| 1733 | sub add_node { | ||
| 1734 | my ($wiz, $tv, $model, $node, $q, $piter, $labels, $path) = @_; | ||
| 1735 | |||
| 1736 | my $label; | ||
| 1737 | my $name = get_name($node); | ||
| 1738 | if (ref($node) eq 'HASH') { | ||
| 1739 | $label = $node->{'label'}; | ||
| 1740 | } | ||
| 1741 | |||
| 1742 | $label = $label || $labels->{$name} || $name; | ||
| 1743 | |||
| 1744 | #text of the node is the label. name is the identifier. | ||
| 1745 | |||
| 1746 | my $child = $model->insert($piter, -1); | ||
| 1747 | $model->set($child, 0, $label); | ||
| 1748 | $model->{'qwdata'}{'datamap'}{$path} = $name; | ||
| 1749 | |||
| 1750 | # test to see if the new node has children | ||
| 1751 | my $ans = $q->{'children'}->($wiz, $name); | ||
| 1752 | |||
| 1753 | # add a bogus child to make the drop down widget appear | ||
| 1754 | $model->insert($child, -1) if ($ans && $#$ans > -1); | ||
| 1755 | |||
| 1756 | # look to see if we're a child that needs opening in the default pass | ||
| 1757 | if (defined($tv->{'expand'}) && | ||
| 1758 | $#{$tv->{'expand'}} > -1 && | ||
| 1759 | $tv->{'expand'}[0] eq $name) { | ||
| 1760 | shift @{$tv->{'expand'}}; # shift of the current one; | ||
| 1761 | |||
| 1762 | if ($#{$tv->{'expand'}} == -1) { | ||
| 1763 | # this is the last node, selecte it | ||
| 1764 | $tv->{'qwdata'}{'cursor'} = $path; | ||
| 1765 | delete($tv->{'qwdata'}{'expand_row'}); | ||
| 1766 | } else { | ||
| 1767 | # tell the sub row to expand | ||
| 1768 | $tv->{'qwdata'}{'expand_row'} = $path; | ||
| 1769 | } | ||
| 1770 | } | ||
| 1771 | } | ||
| 1772 | |||
| 1773 | ################################################## | ||
| 1774 | # | ||
| 1775 | # Automatic updating for monitors. | ||
| 1776 | # | ||
| 1777 | |||
| 1778 | sub do_autoupd | ||
| 1779 | { | ||
| 1780 | # | ||
| 1781 | # Dummy routine for now! | ||
| 1782 | # | ||
| 1783 | warn "Gtk2.do_autoupd: currently no automatic updating is defined for Gtk2. This should be fixed RSN.\n" | ||
| 1784 | } | ||
| 1785 | |||
| 1786 | ################################################## | ||
| 1787 | # unknown type errors | ||
| 1788 | # | ||
| 1789 | sub do_unknown { | ||
| 1790 | my ($self, $q, $wiz, $p) = @_; | ||
| 1791 | $self->put_it("Unknown question type $q->{type} not handled in primary '$p->{module_name}'.\nIt is highly likely this application will no longer function properly beyond this point."); | ||
| 1792 | } | ||
| 1793 | |||
| 1794 | |||
| 1795 | ################################################## | ||
| 1796 | # action confirm | ||
| 1797 | ################################################## | ||
| 1798 | |||
| 1799 | sub start_confirm { | ||
| 1800 | my ($self, $wiz) = @_; | ||
| 1801 | |||
| 1802 | $self->remove_all_table_entries(); | ||
| 1803 | $self->put_it('Wrapping up.',1,1); | ||
| 1804 | $self->put_it('Do you want to commit the following changes:',2,1); | ||
| 1805 | $self->{'resultf'} = $self->create_qw_label(''); | ||
| 1806 | $self->put_it($self->{'resultf'},3,1); | ||
| 1807 | } | ||
| 1808 | |||
| 1809 | sub end_confirm { | ||
| 1810 | my ($self, $wiz) = @_; | ||
| 1811 | # this will be deleted by the cancel button if they press it. | ||
| 1812 | $self->do_hidden($wiz, 'wiz_confirmed', 'Commit'); | ||
| 1813 | $self->do_ok_cancel($wiz->qwparam('QWizard_commit') || "_Commit", $wiz); | ||
| 1814 | $self->our_mainloop(); | ||
| 1815 | return 1; | ||
| 1816 | } | ||
| 1817 | |||
| 1818 | sub do_confirm_message { | ||
| 1819 | my ($self, $wiz, $msg) = @_; | ||
| 1820 | $self->{'resultf'}->set_text($self->{'resultf'}->get_text() . $msg . "\n"); | ||
| 1821 | } | ||
| 1822 | |||
| 1823 | sub canceled_confirm { | ||
| 1824 | my ($self, $wiz) = @_; | ||
| 1825 | goto_top(); | ||
| 1826 | } | ||
| 1827 | |||
| 1828 | ################################################## | ||
| 1829 | # actions | ||
| 1830 | ################################################## | ||
| 1831 | |||
| 1832 | sub start_actions { | ||
| 1833 | my ($self, $wiz) = @_; | ||
| 1834 | $self->remove_all_table_entries(); | ||
| 1835 | $self->put_it('Processing your request...',1,1); | ||
| 1836 | $self->{'resultf'} = $self->create_qw_label(''); | ||
| 1837 | $self->put_it($self->{'resultf'},2,1); | ||
| 1838 | } | ||
| 1839 | |||
| 1840 | sub end_actions { | ||
| 1841 | my ($self, $wiz) = @_; | ||
| 1842 | $self->put_it('Done',3,1); | ||
| 1843 | $self->do_ok_cancel($wiz->qwparam('QWizard_finish') || "_Finish", $wiz); | ||
| 1844 | $self->clear_params(); | ||
| 1845 | $self->our_mainloop(); | ||
| 1846 | return 1; | ||
| 1847 | } | ||
| 1848 | |||
| 1849 | sub do_action_output { | ||
| 1850 | my ($self, $wiz, $action) = @_; | ||
| 1851 | $self->{'resultf'}->set_text($self->{'resultf'}->get_text() . $action . "\n"); | ||
| 1852 | } | ||
| 1853 | |||
| 1854 | sub do_action_error { | ||
| 1855 | my ($self, $wiz, $errstr) = @_; | ||
| 1856 | # XXX: make red | ||
| 1857 | $self->{'resultf'}->set_text($self->{'resultf'}->get_text() . $errstr . "\n"); | ||
| 1858 | } | ||
| 1859 | |||
| 1860 | 1; |