blib/lib/App/Asciio/GTK/Asciio/Dialogs.pm untested

filter: Statementstmt 0.0% 0 / 187 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 240 CC 8 SCAR 15.7
CC
8
Cov
0%
display_edit_dialog_for_mini_edit_mode
12.0
get_color_from_user
6.0
display_quit_dialog
6.0
CRAP 4.8
1  
2 package App::Asciio::GTK::Asciio ;
3 $|++ ;
4  
5 use strict;
6 use warnings;
7  
8 use Data::TreeDumper ;
9 use Data::TreeDumper::Renderer::GTK ;
10 use List::Util qw(max) ;
11  
12 use App::Asciio::String ;
13  
14 #-----------------------------------------------------------------------------
15  
16 sub get_color_from_user
17 {
18 my ($self, $previous_color) = @_ ;
19  
20 my $color = Gtk3::Gdk::Color->new (map { $_ * 65535 } @{$previous_color});
21 my $dialog = Gtk3::ColorSelectionDialog->new ("Changing color");
22  
23 my $colorsel = $dialog->get_color_selection;
24  
25 $colorsel->set_previous_color($color);
26 $colorsel->set_current_color($color);
27 $colorsel->set_has_palette(TRUE);
28  
29 my $response = $dialog->run;
30  
31 if ($response eq 'ok')
32 {
33 $color = $colorsel->get_current_color;
34 }
35  
36 $dialog->destroy;
37  
38 return [$color->red / 65535, $color->green / 65535, $color->blue / 65535] ;
39 }
40  
41 #-----------------------------------------------------------------------------
42  
43 sub show_dump_window
44 {
45 my ($self, $data, $title, @dumper_setup) = @_ ;
46  
47 my $window = new Gtk3::Window() ;
48  
49 my $dialog = Gtk3::Dialog->new($title, $window, 'destroy-with-parent') ;
50 $dialog->set_default_size(600, 800);
51  
52 my $vbox = Gtk3::VBox->new(FALSE, 5);
53 $vbox->pack_start(Gtk3::Label->new (""), FALSE, FALSE, 0);
54 $vbox->add(Gtk3::Label->new (""));
55  
56 # tree
57 my $treedumper = Data::TreeDumper::Renderer::GTK->new
58 (
59 data => $data,
60 dumper_setup => {@dumper_setup}
61 );
62  
63 $treedumper->collapse_all;
64 $treedumper->set_hexpand(TRUE) ;
65 $treedumper->set_vexpand(TRUE) ;
66  
67 my $scroller = Gtk3::ScrolledWindow->new();
68 $scroller->set_hexpand(TRUE) ;
69 $scroller->set_vexpand(TRUE) ;
70 $scroller->add($treedumper);
71  
72 $vbox->add ($scroller) ;
73 $treedumper->show() ;
74 $scroller->show();
75 $vbox->show() ;
76  
77 $dialog->get_content_area()->add($vbox) ;
78  
79 $dialog->run() ;
80 $dialog->destroy ;
81 }
82  
83  
84 #-----------------------------------------------------------------------------
85  
86 sub display_message_modal
87 {
88 my ($self, $message) = @_ ;
89  
90 my $window = new Gtk3::Window() ;
91  
92 my $dialog = Gtk3::MessageDialog->new
93 (
94 $window,
95 'destroy-with-parent' ,
96 'other' ,
97 'close' ,
98 $message ,
99 ) ;
100  
101 $dialog->modify_font(Pango::FontDescription->from_string($self->get_font_as_string()));
102  
103 $dialog->signal_connect(response => sub { $dialog->destroy ; 1 }) ;
104 $dialog->run() ;
105 }
106  
107 #-----------------------------------------------------------------------------
108  
109 sub display_yes_no_cancel_dialog
110 {
111 my ($self, $title, $text) = @_ ;
112  
113 my $window = new Gtk3::Window() ;
114  
115 my $dialog = Gtk3::Dialog->new($title, $window, 'destroy-with-parent') ;
116 $dialog->set_default_size (300, 150);
117 $dialog->add_button ('gtk-yes' => 'yes');
118 $dialog->add_button ('gtk-no' => 'no');
119 $dialog->add_button ('gtk-cancel' => 'cancel');
120  
121 my $label = Gtk3::Label->new($text);
122 $dialog->get_content_area->add ($label);
123 $label->show;
124  
125 my $result = $dialog->run() ;
126  
127 $dialog->destroy ;
128  
129 return $result ;
130 }
131  
132 #-----------------------------------------------------------------------------
133  
134 sub display_quit_dialog
135 {
136 my ($self, $title, $text) = @_ ;
137  
138 my $window = Gtk3::Window->new() ;
139  
140 my $dialog = Gtk3::Dialog->new($title, $window, 'destroy-with-parent') ;
141 $dialog->set_default_size (300, 150);
142  
143 add_button_with_icon ($dialog, 'Continue editing', 'gtk-cancel' => 'cancel');
144 add_button_with_icon ($dialog, 'Save and Quit', 'gtk-save' => 999);
145 add_button_with_icon ($dialog, 'Quit and lose changes', 'gtk-ok' => 'ok');
146  
147 my $label = Gtk3::Label->new($text);
148 $label->show;
149  
150 $dialog->get_content_area->add ($label);
151  
152 my $result = $dialog->run() ;
153 $result = 'save_and_quit' if "$result" eq "999" ;
154  
155 $dialog->destroy ;
156  
157 return $result ;
158 }
159  
160 sub add_button_with_icon
161 {
162 # code by Muppet
163 my ($dialog, $text, $stock_id, $response_id) = @_;
164  
165 my $button = create_button ($text, $stock_id);
166 $button->show;
167  
168 $dialog->add_action_widget ($button, $response_id);
169 }
170  
171 sub create_button
172 {
173 # code by Muppet
174 my ($text, $stock_id) = @_;
175  
176 my $button = Gtk3::Button->new ();
177  
178 #
179 # This setup is cribbed from gtk_button_construct_child()
180 # in gtkbutton.c. It does not handle all the details like
181 # left-to-right ordering and alignment and such, as in the
182 # real button code.
183 #
184 my $image = Gtk3::Image->new_from_stock ($stock_id, 'button');
185 my $label = Gtk3::Label->new ($text); # accepts mnemonics
186 $label->set_mnemonic_widget ($button);
187  
188 my $hbox = Gtk3::HBox->new ();
189 $hbox->pack_start ($image, FALSE, FALSE, 0);
190 $hbox->pack_start ($label, FALSE, FALSE, 0);
191  
192 $hbox->show_all ();
193  
194 $button->add ($hbox);
195  
196 return $button;
197 }
198  
199 #-----------------------------------------------------------------------------
200  
201 sub on_focus_out_event
202 {
203 my ($self, $event, $window) = @_;
204 $self->response(1) ;
205 }
206  
207 #-----------------------------------------------------------------------------
208  
209 sub display_edit_dialog
210 {
211 my ($self, $title, $text, $asciio, $X, $Y, $text_begin_x, $text_begin_y) = @_ ;
212 if(($asciio->{EDIT_TEXT_INLINE} != 0) && (defined $X) && (defined $Y))
213 {
214 return $self->display_edit_dialog_for_mini_edit_mode($title, $text, $asciio, $X, $Y, $text_begin_x, $text_begin_y) ;
215 }
216 else
217 {
218 return $self->display_edit_dialog_for_normal_mode($title, $text, $asciio) ;
219 }
220 }
221  
222 #-----------------------------------------------------------------------------
223  
224 sub display_edit_dialog_for_normal_mode
225 {
226 my ($self, $title, $text, $asciio) = @_ ;
227  
228 $text ='' unless defined $text ;
229  
230 my $window = new Gtk3::Window() ;
231  
232 my $dialog = Gtk3::Dialog->new($title, $window, 'destroy-with-parent') ;
233 $dialog->set_position("mouse");
234 $dialog->set_border_width(0);
235 $dialog->set_default_size (500, 400);
236 $dialog->add_button ('gtk-ok' => 'ok');
237  
238 my $vbox = Gtk3::VBox->new(FALSE, 5) ;
239 $vbox->pack_start(Gtk3::Label->new(""), FALSE, FALSE, 0) ;
240 $vbox->add(Gtk3::Label->new("")) ;
241  
242 my $textview = Gtk3::TextView->new;
243 $textview->modify_font(Pango::FontDescription->from_string($asciio->get_font_as_string()));
244 my $buffer = $textview->get_buffer;
245 $buffer->insert ($buffer->get_end_iter, $text);
246  
247 my $scroller = Gtk3::ScrolledWindow->new();
248 $scroller->set_hexpand(TRUE);
249 $scroller->set_vexpand(TRUE);
250 $scroller->add($textview);
251 $vbox->add($scroller);
252  
253 $textview->show();
254 $scroller->show();
255 $vbox->show();
256  
257 $dialog->get_content_area->add ($vbox) ;
258  
259 $dialog->run() ;
260  
261 my $new_text = $textview->get_buffer->get_text($buffer->get_start_iter, $buffer->get_end_iter, TRUE) ;
262  
263 $dialog->destroy ;
264  
265 return $new_text
266 }
267  
268 #-----------------------------------------------------------------------------
269  
270 sub display_edit_dialog_for_mini_edit_mode
271 {
272 my ($self, $title, $text, $asciio, $X, $Y, $text_begin_x, $text_begin_y) = @_ ;
273  
274 $text ='' unless defined $text ;
275 my @text_lines ;
276 if($text)
277 {
278 @text_lines = split("\n", $text) ;
279 }
280 else
281 {
282 @text_lines = ('') ;
283 }
284  
285 my $text_width = max(map {unicode_length($_)} @text_lines);
286 my $text_heigh = @text_lines;
287 $text_width = max($text_width, 3) ;
288 $text_heigh =max($text_heigh, 3) ;
289  
290 my ($character_width, $character_height) = $asciio->get_character_size() ;
291 my ($root_x, $root_y) = $asciio->{root_window}->get_position();
292 my ($v_value, $h_value) = ($asciio->{sc_window}->get_vadjustment()->get_value(), $asciio->{sc_window}->get_hadjustment()->get_value());
293  
294  
295 my $window = new Gtk3::Window() ;
296  
297 my $dialog = Gtk3::Dialog->new($title, $window, 'destroy-with-parent') ;
298 $dialog->set_default_size ($text_width, $text_heigh);
299 $dialog->set_border_width(0);
300 $dialog->set_decorated(0);
301 $dialog->move($root_x+(($X+$text_begin_x)*$character_width)-$h_value, $root_y+(($Y+$text_begin_y)*$character_height)-$v_value);
302  
303 my $vbox = Gtk3::VBox->new(FALSE, 5) ;
304 $vbox->pack_start(Gtk3::Label->new(""), FALSE, FALSE, 0) ;
305 $vbox->add(Gtk3::Label->new("")) ;
306  
307 my $textview = Gtk3::TextView->new;
308 $textview->modify_font(Pango::FontDescription->from_string($asciio->get_font_as_string()));
309 my $buffer = $textview->get_buffer;
310 $buffer->insert ($buffer->get_end_iter, $text);
311  
312 $vbox->add($textview);
313  
314 $textview->show();
315 $vbox->show();
316  
317 $dialog->get_content_area->add ($vbox) ;
318 $dialog->add_events('GDK_FOCUS_CHANGE_MASK') ;
319 $dialog->signal_connect(focus_out_event => \&on_focus_out_event, $window) ;
320  
321 $dialog->run() ;
322  
323 my $new_text = $textview->get_buffer->get_text($buffer->get_start_iter, $buffer->get_end_iter, TRUE) ;
324  
325 $dialog->destroy ;
326  
327 return $new_text
328 }
329  
330 #-----------------------------------------------------------------------------
331  
332 sub get_file_name
333 {
334 my ($self, $type) = @_ ;
335  
336 my $file_name = '' ;
337  
338 my $file_chooser = Gtk3::FileChooserDialog->new
339 (
340 $type, undef, $type,
341 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
342 );
343  
344 $file_name = $file_chooser->get_filename if ('ok' eq $file_chooser->run) ;
345
346 $file_chooser->destroy;
347 Encode::_utf8_on($file_name);
348 return $file_name ;
349 }
350  
351 #-----------------------------------------------------------------------------
352  
353 1 ;