| 1 | package DBIx::dbMan::Interface::tkgui; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBIx::dbMan::History; | ||
| 5 | use Tk; | ||
| 6 | use base 'DBIx::dbMan::Interface'; | ||
| 7 | |||
| 8 | require Tk::ROText; | ||
| 9 | |||
| 10 | our $VERSION = '0.05'; | ||
| 11 | |||
| 12 | 1; | ||
| 13 | |||
| 14 | sub init { | ||
| 15 | my $obj = shift; | ||
| 16 | |||
| 17 | $obj->SUPER::init(@_); | ||
| 18 | |||
| 19 | $obj->{sql} = ''; | ||
| 20 | |||
| 21 | $obj->{mw} = new MainWindow; | ||
| 22 | $obj->{mw}->title('dbMan'); | ||
| 23 | $obj->{mw}->appname('dbMan'); | ||
| 24 | $obj->{mw}->client('dbMan'); | ||
| 25 | |||
| 26 | $obj->{menubar} = $obj->{mw}->Frame(-borderwidth => 2, -relief => 'raised') | ||
| 27 | ->pack(-side => 'top', -fill => 'x'); | ||
| 28 | $obj->{consoleframe} = $obj->{mw}->Frame(-borderwidth => 2, -relief => 'raised') | ||
| 29 | ->pack(-side => 'top', -fill => 'both', -expand => 1); | ||
| 30 | $obj->{cmdlineframe} = $obj->{mw}->Frame->pack(-side => 'bottom', -fill => 'x'); | ||
| 31 | $obj->{promptlabel} = $obj->{cmdlineframe}->Label(-text => $obj->{-lang}->str($obj->get_prompt()), | ||
| 32 | -justify => 'left')->pack(-side => 'left'); | ||
| 33 | $obj->{dobutton} = $obj->{cmdlineframe}->Button(-default => 'active', -text => 'Do', | ||
| 34 | -command => sub { $obj->handle_do; })->pack(-side => 'right'); | ||
| 35 | $obj->{cmdline} = $obj->{cmdlineframe}->Entry(-textvariable => \$obj->{sql}) | ||
| 36 | ->pack(-side => 'bottom', -fill => 'x', -expand => 1); | ||
| 37 | $obj->{cmdline}->bind('<KeyPress-Return>',sub { $obj->handle_do; }); | ||
| 38 | $obj->{cmdline}->bind('<KeyPress-Up>',sub { $obj->prev_history; }); | ||
| 39 | $obj->{cmdline}->bind('<KeyPress-Down>',sub { $obj->next_history; }); | ||
| 40 | $obj->{console} = $obj->{consoleframe}->Scrolled('ROText', -scrollbars => 'sre', | ||
| 41 | -wrap => 'none')->pack(-fill => 'both', -expand => 1); | ||
| 42 | |||
| 43 | $obj->{history}->load_and_store; | ||
| 44 | =comment | ||
| 45 | $readline'rl_completion_function = sub { | ||
| 46 | my ($text,$line,$start) = @_; | ||
| 47 | my %action = (action => 'LINE_COMPLETE', | ||
| 48 | text => $text, line => $line, start => $start); | ||
| 49 | do { | ||
| 50 | %action = $obj->{-core}->handle_action(%action); | ||
| 51 | } until ($action{processed}); | ||
| 52 | return @{$action{list}} if ref $action{list} eq 'ARRAY'; | ||
| 53 | return $action{list} if $action{list}; | ||
| 54 | return (); | ||
| 55 | }; | ||
| 56 | =cut | ||
| 57 | } | ||
| 58 | |||
| 59 | sub prev_history { | ||
| 60 | my $obj = shift; | ||
| 61 | my $hist = $obj->{history}->prev; | ||
| 62 | print $hist."\n"; | ||
| 63 | # NOT IMPLEMENTED YET | ||
| 64 | } | ||
| 65 | |||
| 66 | sub next_history { | ||
| 67 | my $obj = shift; | ||
| 68 | my $hist = $obj->{history}->next; | ||
| 69 | print $hist."\n"; | ||
| 70 | # NOT IMPLEMENTED YET | ||
| 71 | } | ||
| 72 | |||
| 73 | sub handle_do { | ||
| 74 | my $obj = shift; | ||
| 75 | |||
| 76 | my %action = (); | ||
| 77 | |||
| 78 | do { | ||
| 79 | %action = $obj->get_action(); | ||
| 80 | do { | ||
| 81 | %action = $obj->{-core}->handle_action(%action); | ||
| 82 | } until ($action{processed}); | ||
| 83 | exit if $action{action} eq 'QUIT'; | ||
| 84 | } until ($action{action} ne 'IDLE'); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub loop { | ||
| 88 | my $obj = shift; | ||
| 89 | |||
| 90 | $obj->{cmdline}->focus; | ||
| 91 | |||
| 92 | Tk::MainLoop(); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub get_command { | ||
| 96 | my $obj = shift; | ||
| 97 | |||
| 98 | my $cmd = $obj->{sql}; | ||
| 99 | if ($cmd) { | ||
| 100 | $obj->{sql} = ''; | ||
| 101 | $obj->{history}->add($cmd); | ||
| 102 | } | ||
| 103 | |||
| 104 | return $cmd; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub print { | ||
| 108 | my $obj = shift; | ||
| 109 | $obj->{console}->insert('end',join '',$obj->{-lang}->str(@_)); | ||
| 110 | $obj->{console}->see('end'); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub render_size { | ||
| 114 | my $obj = shift; | ||
| 115 | return 80; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub prompt { | ||
| 119 | my $obj = shift; | ||
| 120 | $obj->SUPER::prompt(@_); | ||
| 121 | $obj->{promptlabel}->configure(-text => $obj->{-lang}->str($obj->get_prompt())) if defined $obj->{promptlabel}; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub can_pager { | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub is_utf8 { | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | # what is needed in this tkgui ? | ||
| 133 | # ... registering/unregistering menu items | ||
| 134 | # ... starting handle event from menu items and other actions (bind etc.) | ||
| 135 | # ... autocompletation | ||
| 136 | # ... dynamic render_size | ||
| 137 | # ... single/multiline command line |