| 1 | |||
| 2 | package App::Asciio::GTK::Asciio ; | ||
| 3 | |||
| 4 | use base qw(App::Asciio) ; | ||
| 5 | |||
| 6 | $|++ ; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Glib ':constants'; | ||
| 12 | use Gtk3 -init; | ||
| 13 | use Pango ; | ||
| 14 | |||
| 15 | use List::MoreUtils qw(all) ; | ||
| 16 | |||
| 17 | use App::Asciio::GTK::Asciio::stripes::editable_exec_box; | ||
| 18 | use App::Asciio::GTK::Asciio::stripes::editable_box2; | ||
| 19 | use App::Asciio::GTK::Asciio::stripes::rhombus; | ||
| 20 | use App::Asciio::GTK::Asciio::stripes::ellipse; | ||
| 21 | |||
| 22 | use App::Asciio::GTK::Asciio::stripes::editable_arrow2; | ||
| 23 | use App::Asciio::GTK::Asciio::stripes::wirl_arrow ; | ||
| 24 | use App::Asciio::GTK::Asciio::stripes::angled_arrow ; | ||
| 25 | use App::Asciio::GTK::Asciio::stripes::section_wirl_arrow ; | ||
| 26 | |||
| 27 | use App::Asciio::GTK::Asciio::Dialogs ; | ||
| 28 | use App::Asciio::GTK::Asciio::Menues ; | ||
| 29 | |||
| 30 | use App::Asciio::Cross ; | ||
| 31 | use App::Asciio::String ; | ||
| 32 | use App::Asciio::Markup ; | ||
| 33 | |||
| 34 | sub hide_pointer | ||
| 35 | { | ||
| 36 | my ($asciio) = @_ ; | ||
| 37 | |||
| 38 | my $display = $asciio->{widget}->get_display(); | ||
| 39 | my $cursor = Gtk3::Gdk::Cursor->new_for_display($display, 'blank-cursor'); | ||
| 40 | |||
| 41 | my $win = $asciio->{widget}->get_parent_window() ; | ||
| 42 | $win->set_cursor($cursor); | ||
| 43 | # fleur | ||
| 44 | # x_cursor | ||
| 45 | } | ||
| 46 | |||
| 47 | sub show_pointer | ||
| 48 | { | ||
| 49 | my ($asciio) = @_ ; | ||
| 50 | |||
| 51 | my $display = $asciio->{widget}->get_display(); | ||
| 52 | my $cursor = Gtk3::Gdk::Cursor->new_for_display($display, 'arrow'); | ||
| 53 | |||
| 54 | my $win = $asciio->{widget}->get_parent_window() ; | ||
| 55 | $win->set_cursor($cursor); | ||
| 56 | } | ||
| 57 | |||
| 58 | #----------------------------------------------------------------------------- | ||
| 59 | |||
| 60 | our $VERSION = '0.01' ; | ||
| 61 | |||
| 62 | #----------------------------------------------------------------------------- | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new | ||
| 69 | { | ||
| 70 | my ($class, $window, $width, $height, $sc_window) = @_ ; | ||
| 71 | |||
| 72 | my $self = App::Asciio::new($class) ; | ||
| 73 | $self->{UI} = 'GUI' ; | ||
| 74 | |||
| 75 | bless $self, $class ; | ||
| 76 | |||
| 77 | $window->signal_connect(key_press_event => \&key_press_event, $self); | ||
| 78 | $window->signal_connect(motion_notify_event => \&motion_notify_event, $self); | ||
| 79 | $window->signal_connect(button_press_event => \&button_press_event, $self); | ||
| 80 | $window->signal_connect(button_release_event => \&button_release_event, $self); | ||
| 81 | |||
| 82 | $window->signal_connect(configure_event => sub { $self->update_display() if($self->{USE_CROSS_MODE}) ; }) ; | ||
| 83 | $sc_window->get_hadjustment()->signal_connect(value_changed => sub { $self->update_display() if($self->{USE_CROSS_MODE}) ; }) ; | ||
| 84 | $sc_window->get_vadjustment()->signal_connect(value_changed => sub { $self->update_display() if($self->{USE_CROSS_MODE}) ; }) ; | ||
| 85 | $sc_window->add_events(['GDK_SCROLL_MASK']) ; | ||
| 86 | $sc_window->signal_connect(scroll_event => \&mouse_scroll_event, $self) ; | ||
| 87 | |||
| 88 | my $drawing_area = Gtk3::DrawingArea->new; | ||
| 89 | |||
| 90 | $self->{widget} = $drawing_area ; | ||
| 91 | $self->{root_window} = $window ; | ||
| 92 | $self->{sc_window} = $sc_window ; | ||
| 93 | |||
| 94 | $drawing_area->signal_connect(draw => \&expose_event, $self); | ||
| 95 | |||
| 96 | $drawing_area->set_events | ||
| 97 | ([qw/ | ||
| 98 | exposure-mask | ||
| 99 | leave-notify-mask | ||
| 100 | button-press-mask | ||
| 101 | button-release-mask | ||
| 102 | pointer-motion-mask | ||
| 103 | key-press-mask | ||
| 104 | key-release-mask | ||
| 105 | /]); | ||
| 106 | |||
| 107 | return($self) ; | ||
| 108 | } | ||
| 109 | |||
| 110 | #----------------------------------------------------------------------------- | ||
| 111 | |||
| 112 | sub destroy | ||
| 113 | { | ||
| 114 | my ($self) = @_; | ||
| 115 | |||
| 116 | $self->{widget}->get_toplevel()->destroy() ; | ||
| 117 | } | ||
| 118 | |||
| 119 | #----------------------------------------------------------------------------- | ||
| 120 | |||
| 121 | sub set_title | ||
| 122 | { | ||
| 123 | my ($self, $title) = @_; | ||
| 124 | |||
| 125 | $self->SUPER::set_title($title) ; | ||
| 126 | |||
| 127 | if(defined $title) | ||
| 128 | { | ||
| 129 | $self->{widget}->get_toplevel()->set_title($title . ' - asciio') ; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | #----------------------------------------------------------------------------- | ||
| 134 | |||
| 135 | sub set_font | ||
| 136 | { | ||
| 137 | my ($self, $font_family, $font_size) = @_; | ||
| 138 | |||
| 139 | $self->SUPER::set_font($font_family, $font_size) ; | ||
| 140 | } | ||
| 141 | |||
| 142 | #----------------------------------------------------------------------------- | ||
| 143 | |||
| 144 | sub switch_gtk_popup_box_type | ||
| 145 | { | ||
| 146 | my ($self) = @_ ; | ||
| 147 | $self->{EDIT_TEXT_INLINE} ^= 1 ; | ||
| 148 | } | ||
| 149 | |||
| 150 | #----------------------------------------------------------------------------- | ||
| 151 | |||
| 152 | sub update_display | ||
| 153 | { | ||
| 154 | my ($self) = @_; | ||
| 155 | |||
| 156 | $self->SUPER::update_display() ; | ||
| 157 | |||
| 158 | my $widget = $self->{widget} ; | ||
| 159 | $widget->queue_draw_area(0, 0, $widget->get_allocated_width, $widget->get_allocated_height); | ||
| 160 | } | ||
| 161 | |||
| 162 | #----------------------------------------------------------------------------- | ||
| 163 | |||
| 164 | sub expose_event | ||
| 165 | { | ||
| 166 | my ( $widget, $gc, $self ) = @_; | ||
| 167 | |||
| 168 | $gc->set_line_width(1); | ||
| 169 | |||
| 170 | my ($character_width, $character_height) = $self->get_character_size() ; | ||
| 171 | my ($widget_width, $widget_height) = ($widget->get_allocated_width(), $widget->get_allocated_height()) ; | ||
| 172 | |||
| 173 | # draw background | ||
| 174 | my $grid_rendering = $self->{CACHE}{GRID} ; | ||
| 175 | |||
| 176 | unless (defined $grid_rendering) | ||
| 177 | { | ||
| 178 | my $surface = Cairo::ImageSurface->create('argb32', $widget_width, $widget_height); | ||
| 179 | my $gc = Cairo::Context->create($surface); | ||
| 180 | |||
| 181 | $gc->set_source_rgb(@{$self->get_color('background')}); | ||
| 182 | $gc->rectangle(0, 0, $widget->get_allocated_width, $widget->get_allocated_height); | ||
| 183 | $gc->fill; | ||
| 184 | |||
| 185 | if($self->{DISPLAY_GRID}) | ||
| 186 | { | ||
| 187 | $gc->set_line_width(1); | ||
| 188 | |||
| 189 | for my $horizontal (0 .. ($widget_height/$character_height) + 1) | ||
| 190 | { | ||
| 191 | my $color = ($horizontal % 10 == 0 and $self->{DISPLAY_GRID2}) ? 'grid_2' : 'grid' ; | ||
| 192 | $gc->set_source_rgb(@{$self->get_color($color)}); | ||
| 193 | |||
| 194 | $gc->move_to(0, $horizontal * $character_height); | ||
| 195 | $gc->line_to($widget_width, $horizontal * $character_height); | ||
| 196 | $gc->stroke; | ||
| 197 | } | ||
| 198 | |||
| 199 | for my $vertical(0 .. ($widget_width/$character_width) + 1) | ||
| 200 | { | ||
| 201 | my $color = ($vertical % 10 == 0 and $self->{DISPLAY_GRID2}) ? 'grid_2' : 'grid' ; | ||
| 202 | $gc->set_source_rgb(@{$self->get_color($color)}); | ||
| 203 | |||
| 204 | $gc->move_to($vertical * $character_width, 0) ; | ||
| 205 | $gc->line_to($vertical * $character_width, $widget_height); | ||
| 206 | $gc->stroke; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | $grid_rendering = $self->{CACHE}{GRID} = $surface ; | ||
| 211 | } | ||
| 212 | |||
| 213 | $gc->set_source_surface($grid_rendering, 0, 0); | ||
| 214 | $gc->paint; | ||
| 215 | |||
| 216 | # draw elements | ||
| 217 | my $element_index = 0 ; | ||
| 218 | |||
| 219 | my $font_description = Pango::FontDescription->from_string($self->get_font_as_string()) ; | ||
| 220 | |||
| 221 | for my $element (@{$self->{ELEMENTS}}) | ||
| 222 | { | ||
| 223 | $element_index++ ; | ||
| 224 | $self->draw_element($element, $element_index, $gc, $font_description, $widget_width, $widget_height, $character_width, $character_height) ; | ||
| 225 | } | ||
| 226 | |||
| 227 | $self->draw_cross_overlays($gc, $widget_width, $widget_height, $character_width, $character_height) if $self->{USE_CROSS_MODE} ; | ||
| 228 | $self->draw_overlay($gc, $widget_width, $widget_height, $character_width, $character_height) ; | ||
| 229 | |||
| 230 | # draw ruler lines | ||
| 231 | for my $line (@{$self->{RULER_LINES}}) | ||
| 232 | { | ||
| 233 | $gc->set_source_rgb(@{$self->get_color('ruler_line')}); | ||
| 234 | |||
| 235 | if($line->{TYPE} eq 'VERTICAL') | ||
| 236 | { | ||
| 237 | $gc->move_to($line->{POSITION} * $character_width, 0) ; | ||
| 238 | $gc->line_to($line->{POSITION} * $character_width, $widget_height) ; | ||
| 239 | } | ||
| 240 | else | ||
| 241 | { | ||
| 242 | $gc->move_to(0, $line->{POSITION} * $character_height) ; | ||
| 243 | $gc->line_to($widget_width, $line->{POSITION} * $character_height); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | $gc->stroke() ; | ||
| 247 | |||
| 248 | # draw connections | ||
| 249 | my (%connected_connections, %connected_connectors) ; | ||
| 250 | |||
| 251 | for my $connection (@{$self->{CONNECTIONS}}) | ||
| 252 | { | ||
| 253 | my $draw_connection ; | ||
| 254 | my $connector ; | ||
| 255 | |||
| 256 | if($self->is_over_element($connection->{CONNECTED}, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1)) | ||
| 257 | { | ||
| 258 | $draw_connection++ ; | ||
| 259 | |||
| 260 | $connector = $connection->{CONNECTED}->get_named_connection($connection->{CONNECTOR}{NAME}) ; | ||
| 261 | $connected_connectors{$connection->{CONNECTED}}{$connector->{X}}{$connector->{Y}}++ ; | ||
| 262 | } | ||
| 263 | |||
| 264 | if($self->is_over_element($connection->{CONNECTEE}, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1)) | ||
| 265 | { | ||
| 266 | $draw_connection++ ; | ||
| 267 | |||
| 268 | my $connectee_connection = $connection->{CONNECTEE}->get_named_connection($connection->{CONNECTION}{NAME}) ; | ||
| 269 | |||
| 270 | if($connectee_connection) | ||
| 271 | { | ||
| 272 | $connected_connectors{$connection->{CONNECTEE}}{$connectee_connection->{X}}{$connectee_connection->{Y}}++ ; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | if($draw_connection) | ||
| 277 | { | ||
| 278 | $connector ||= $connection->{CONNECTED}->get_named_connection($connection->{CONNECTOR}{NAME}) ; | ||
| 279 | |||
| 280 | unless (defined $self->{CACHE}{CONNECTION}) | ||
| 281 | { | ||
| 282 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height); | ||
| 283 | |||
| 284 | my $gc = Cairo::Context->create($surface); | ||
| 285 | $gc->set_line_width(1); | ||
| 286 | $gc->set_source_rgb(@{$self->get_color('connection')}); | ||
| 287 | $gc->rectangle(0, 0, $character_width, $character_height); | ||
| 288 | $gc->stroke() ; | ||
| 289 | |||
| 290 | $self->{CACHE}{CONNECTION} = $surface ; | ||
| 291 | } | ||
| 292 | |||
| 293 | my $connection_rendering = $self->{CACHE}{CONNECTION} ; | ||
| 294 | |||
| 295 | $gc->set_source_surface | ||
| 296 | ( | ||
| 297 | $connection_rendering, | ||
| 298 | ($connector->{X} + $connection->{CONNECTED}{X}) * $character_width, | ||
| 299 | ($connector->{Y} + $connection->{CONNECTED}{Y}) * $character_height, | ||
| 300 | ); | ||
| 301 | |||
| 302 | $gc->paint; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | # draw connectors and connection points | ||
| 307 | unless (defined $self->{CACHE}{CONNECTOR_POINT}) | ||
| 308 | { | ||
| 309 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height); | ||
| 310 | |||
| 311 | my $gc = Cairo::Context->create($surface); | ||
| 312 | $gc->set_line_width(1); | ||
| 313 | $gc->set_source_rgb(@{$self->get_color('connector_point')}); | ||
| 314 | $gc->rectangle(0, 0, $character_width, $character_height); | ||
| 315 | $gc->stroke() ; | ||
| 316 | |||
| 317 | $self->{CACHE}{CONNECTOR_POINT} = $surface ; | ||
| 318 | } | ||
| 319 | my $connector_point_rendering = $self->{CACHE}{CONNECTOR_POINT} ; | ||
| 320 | |||
| 321 | unless (defined $self->{CACHE}{CONNECTION_POINT}) | ||
| 322 | { | ||
| 323 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height); | ||
| 324 | |||
| 325 | my $gc = Cairo::Context->create($surface); | ||
| 326 | $gc->set_line_width(1); | ||
| 327 | $gc->set_source_rgb(@{$self->get_color('connection_point')}); | ||
| 328 | $gc->rectangle($character_width/3, $character_height/3, $character_width/3, $character_height/3); | ||
| 329 | $gc->stroke() ; | ||
| 330 | |||
| 331 | $self->{CACHE}{CONNECTION_POINT} = $surface ; | ||
| 332 | } | ||
| 333 | my $connection_point_rendering = $self->{CACHE}{CONNECTION_POINT} ; | ||
| 334 | |||
| 335 | unless (defined $self->{CACHE}{EXTRA_POINT}) | ||
| 336 | { | ||
| 337 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height); | ||
| 338 | |||
| 339 | my $gc = Cairo::Context->create($surface); | ||
| 340 | $gc->set_line_width(2); | ||
| 341 | $gc->set_source_rgb(@{$self->get_color('extra_point')}); | ||
| 342 | $gc->rectangle(0, 0, $character_width, $character_height); | ||
| 343 | $gc->stroke() ; | ||
| 344 | $gc->set_line_width(1); | ||
| 345 | |||
| 346 | $self->{CACHE}{EXTRA_POINT} = $surface ; | ||
| 347 | } | ||
| 348 | my $extra_point_rendering = $self->{CACHE}{EXTRA_POINT} ; | ||
| 349 | |||
| 350 | for my $element ( | ||
| 351 | $self->{DISPLAY_ALL_CONNECTORS} | ||
| 352 | ? @{$self->{ELEMENTS}} | ||
| 353 | : grep {$self->is_over_element($_, $self->{MOUSE_X}, $self->{MOUSE_Y}, 1)} @{$self->{ELEMENTS}} | ||
| 354 | ) | ||
| 355 | { | ||
| 356 | for my $connector ($element->get_connector_points()) | ||
| 357 | { | ||
| 358 | next if exists $connected_connectors{$element}{$connector->{X}}{$connector->{Y}} ; | ||
| 359 | |||
| 360 | $gc->set_source_surface | ||
| 361 | ( | ||
| 362 | $connector_point_rendering, | ||
| 363 | ($element->{X} + $connector->{X}) * $character_width, # can be additions | ||
| 364 | ($connector->{Y} + $element->{Y}) * $character_height, | ||
| 365 | ); | ||
| 366 | |||
| 367 | $gc->paint; | ||
| 368 | } | ||
| 369 | |||
| 370 | for my $connection_point ($element->get_connection_points()) | ||
| 371 | { | ||
| 372 | next if $element->{AUTOCONNECT_DISABLED} ; | ||
| 373 | next if exists $connected_connections{$element}{$connection_point->{X}}{$connection_point->{Y}} ; | ||
| 374 | |||
| 375 | $gc->set_source_surface | ||
| 376 | ( | ||
| 377 | $connection_point_rendering, | ||
| 378 | (($connection_point->{X} + $element->{X}) * $character_width), # can be additions | ||
| 379 | (($connection_point->{Y} + $element->{Y}) * $character_height), | ||
| 380 | ); | ||
| 381 | |||
| 382 | $gc->paint; | ||
| 383 | } | ||
| 384 | |||
| 385 | unless(defined $self->{DRAGGING} || (exists $element->{GROUP} and defined $element->{GROUP}[-1])) | ||
| 386 | { | ||
| 387 | for my $extra_point ($element->get_extra_points()) | ||
| 388 | { | ||
| 389 | $gc->set_source_surface | ||
| 390 | ( | ||
| 391 | $extra_point_rendering, | ||
| 392 | (($extra_point->{X} + $element->{X}) * $character_width), # can be additions | ||
| 393 | (($extra_point->{Y} + $element->{Y}) * $character_height), | ||
| 394 | ); | ||
| 395 | |||
| 396 | $gc->paint; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | $gc->show_page; | ||
| 401 | } | ||
| 402 | |||
| 403 | # draw new connections | ||
| 404 | for my $new_connection (@{$self->{NEW_CONNECTIONS}}) | ||
| 405 | { | ||
| 406 | my $end_connection = $new_connection->{CONNECTED}->get_named_connection($new_connection->{CONNECTOR}{NAME}) ; | ||
| 407 | |||
| 408 | $gc->set_source_rgb(@{$self->get_color('new_connection')}); | ||
| 409 | $gc->rectangle | ||
| 410 | ( | ||
| 411 | ($end_connection->{X} + $new_connection->{CONNECTED}{X}) * $character_width , # can be additions | ||
| 412 | ($end_connection->{Y} + $new_connection->{CONNECTED}{Y}) * $character_height , | ||
| 413 | $character_width, $character_height | ||
| 414 | ); | ||
| 415 | } | ||
| 416 | |||
| 417 | delete $self->{NEW_CONNECTIONS} ; | ||
| 418 | |||
| 419 | # draw selection rectangle | ||
| 420 | if(defined $self->{SELECTION_RECTANGLE}{END_X}) | ||
| 421 | { | ||
| 422 | my $start_x = $self->{SELECTION_RECTANGLE}{START_X} * $character_width ; | ||
| 423 | my $start_y = $self->{SELECTION_RECTANGLE}{START_Y} * $character_height ; | ||
| 424 | my $width = ($self->{SELECTION_RECTANGLE}{END_X} - $self->{SELECTION_RECTANGLE}{START_X}) * $character_width ; | ||
| 425 | my $height = ($self->{SELECTION_RECTANGLE}{END_Y} - $self->{SELECTION_RECTANGLE}{START_Y}) * $character_height; | ||
| 426 | |||
| 427 | if($width < 0) | ||
| 428 | { | ||
| 429 | $width *= -1 ; | ||
| 430 | $start_x -= $width ; | ||
| 431 | } | ||
| 432 | |||
| 433 | if($height < 0) | ||
| 434 | { | ||
| 435 | $height *= -1 ; | ||
| 436 | $start_y -= $height ; | ||
| 437 | } | ||
| 438 | |||
| 439 | $gc->set_source_rgb(@{$self->get_color('selection_rectangle')}) ; | ||
| 440 | $gc->rectangle($start_x, $start_y, $width, $height) ; | ||
| 441 | $gc->stroke() ; | ||
| 442 | |||
| 443 | delete $self->{SELECTION_RECTANGLE}{END_X} ; | ||
| 444 | } | ||
| 445 | |||
| 446 | if ($self->{MOUSE_TOGGLE}) | ||
| 447 | { | ||
| 448 | my $start_x = $self->{MOUSE_X} * $character_width ; | ||
| 449 | my $start_y = $self->{MOUSE_Y} * $character_height ; | ||
| 450 | |||
| 451 | $gc->set_source_rgb(@{$self->get_color('mouse_rectangle')}) ; | ||
| 452 | $gc->rectangle($start_x, $start_y, $character_width, $character_height) ; | ||
| 453 | $gc->fill() ; | ||
| 454 | $gc->stroke() ; | ||
| 455 | } | ||
| 456 | |||
| 457 | # draw hint_lines | ||
| 458 | if($self->{DRAW_HINT_LINES}) | ||
| 459 | { | ||
| 460 | my ($xs, $ys, $xe, $ye) = $self->get_extent_box() ; | ||
| 461 | |||
| 462 | $gc->set_line_width(1); | ||
| 463 | $gc->set_source_rgb(@{$self->get_color('hint_line')}); | ||
| 464 | |||
| 465 | $gc->move_to($xs * $character_width, 0) ; | ||
| 466 | $gc->line_to($xs * $character_width, $widget_height) ; | ||
| 467 | |||
| 468 | $gc->move_to(0, $ys * $character_height) ; | ||
| 469 | $gc->line_to($widget_width, $ys * $character_height); | ||
| 470 | |||
| 471 | $gc->move_to($xe * $character_width, 0) ; | ||
| 472 | $gc->line_to($xe * $character_width, $widget_height) ; | ||
| 473 | |||
| 474 | $gc->move_to(0, $ye * $character_height) ; | ||
| 475 | $gc->line_to($widget_width, $ye * $character_height); | ||
| 476 | |||
| 477 | $gc->stroke() ; | ||
| 478 | } | ||
| 479 | |||
| 480 | return TRUE; | ||
| 481 | } | ||
| 482 | |||
| 483 | #----------------------------------------------------------------------------- | ||
| 484 | |||
| 485 | sub draw_cross_overlays | ||
| 486 | { | ||
| 487 | my ($self, $gc, $widget_width, $widget_height, $character_width, $character_height) = @_ ; | ||
| 488 | |||
| 489 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height) ; | ||
| 490 | my $gco = Cairo::Context->create($surface) ; | ||
| 491 | |||
| 492 | my $layout = Pango::Cairo::create_layout($gco) ; | ||
| 493 | $layout->set_font_description(Pango::FontDescription->from_string($self->get_font_as_string())) ; | ||
| 494 | |||
| 495 | my ($windows_width, $windows_height) = $self->{root_window}->get_size() ; | ||
| 496 | my ($v_value, $h_value) = ($self->{sc_window}->get_vadjustment()->get_value(), $self->{sc_window}->get_hadjustment()->get_value()) ; | ||
| 497 | |||
| 498 | my ($start_x, $end_x, $start_y, $end_y) = | ||
| 499 | ( | ||
| 500 | int( $h_value / $character_width ), | ||
| 501 | int( ($h_value + $windows_width) / $character_width ), | ||
| 502 | int( $v_value / $character_height), | ||
| 503 | int( ($v_value + $windows_height) / $character_height) | ||
| 504 | ) ; | ||
| 505 | |||
| 506 | for (App::Asciio::Cross::get_cross_mode_overlays($self, $start_x, $end_x, $start_y, $end_y)) | ||
| 507 | { | ||
| 508 | my ($x, $y, $overlay, $background_color, $foreground_color) = @$_ ; | ||
| 509 | |||
| 510 | $background_color //= $self->get_color('element_background'); | ||
| 511 | $foreground_color //= $self->get_color('element_foreground') ; | ||
| 512 | |||
| 513 | $gco->set_source_rgb(@{$background_color}); | ||
| 514 | $gco->rectangle(0, 0, $character_width, $character_height) ; | ||
| 515 | $gco->fill(); | ||
| 516 | |||
| 517 | $gco->set_source_rgb(@{$foreground_color}); | ||
| 518 | |||
| 519 | $layout->set_text($overlay) ; | ||
| 520 | Pango::Cairo::show_layout($gco, $layout) ; | ||
| 521 | |||
| 522 | $gc->set_source_surface($surface, $x * $character_width, $y * $character_height); | ||
| 523 | $gc->paint; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | #----------------------------------------------------------------------------- | ||
| 528 | |||
| 529 | sub draw_overlay | ||
| 530 | { | ||
| 531 | my ($self, $gc, $widget_width, $widget_height, $character_width, $character_height) = @_ ; | ||
| 532 | |||
| 533 | my $surface = Cairo::ImageSurface->create('argb32', $character_width, $character_height) ; | ||
| 534 | my $gco = Cairo::Context->create($surface) ; | ||
| 535 | |||
| 536 | my $layout = Pango::Cairo::create_layout($gco) ; | ||
| 537 | my $font_description = Pango::FontDescription->from_string($self->get_font_as_string()) ; | ||
| 538 | $layout->set_font_description($font_description) ; | ||
| 539 | |||
| 540 | my $element_index = 0 ; | ||
| 541 | |||
| 542 | my @overlay_elements = $self->get_overlays('GUI', $gc, $widget_width, $widget_height, $character_width, $character_height) ; | ||
| 543 | for (@overlay_elements) | ||
| 544 | { | ||
| 545 | if(! defined $_) | ||
| 546 | { | ||
| 547 | print "GTK::Asciio: got undef\n" ; | ||
| 548 | } | ||
| 549 | elsif(ref $_ eq 'ARRAY') | ||
| 550 | { | ||
| 551 | my ($x, $y, $overlay, $background_color, $foreground_color) = @$_ ; | ||
| 552 | |||
| 553 | $background_color //= $self->get_color('element_background'); | ||
| 554 | $foreground_color //= $self->get_color('element_foreground') ; | ||
| 555 | |||
| 556 | $gco->set_source_rgb(@{$background_color}); | ||
| 557 | $gco->rectangle(0, 0, $character_width, $character_height) ; | ||
| 558 | $gco->fill(); | ||
| 559 | |||
| 560 | $gco->set_source_rgb(@{$foreground_color}); | ||
| 561 | |||
| 562 | $layout->set_text($overlay) ; | ||
| 563 | Pango::Cairo::show_layout($gco, $layout) ; | ||
| 564 | |||
| 565 | $gc->set_source_surface($surface, $x * $character_width, $y * $character_height); | ||
| 566 | $gc->paint; | ||
| 567 | } | ||
| 568 | elsif(ref($_) =~ /^App::Asciio::stripes/) | ||
| 569 | { | ||
| 570 | $self->draw_element($_, $element_index, $gc, $font_description, $widget_width, $widget_height, $character_width, $character_height) ; | ||
| 571 | $element_index++ ; # should overlay stripes have number? | ||
| 572 | } | ||
| 573 | else | ||
| 574 | { | ||
| 575 | print "GTK::Asciio: got someting else " . ref($_) . "\n" ; | ||
| 576 | } | ||
| 577 | } | ||
| 578 | # draw hint_lines | ||
| 579 | if(@overlay_elements and $self->{DRAW_HINT_LINES}) | ||
| 580 | { | ||
| 581 | my ($xs, $ys, $xe, $ye) = $self->get_extent_box(@overlay_elements) ; | ||
| 582 | |||
| 583 | $gc->set_line_width(1); | ||
| 584 | $gc->set_source_rgb(@{$self->get_color('hint_line2')}); | ||
| 585 | |||
| 586 | $gc->move_to($xs * $character_width, 0) ; | ||
| 587 | $gc->line_to($xs * $character_width, $widget_height) ; | ||
| 588 | |||
| 589 | $gc->move_to(0, $ys * $character_height) ; | ||
| 590 | $gc->line_to($widget_width, $ys * $character_height); | ||
| 591 | |||
| 592 | $gc->move_to($xe * $character_width, 0) ; | ||
| 593 | $gc->line_to($xe * $character_width, $widget_height) ; | ||
| 594 | |||
| 595 | $gc->move_to(0, $ye * $character_height) ; | ||
| 596 | $gc->line_to($widget_width, $ye * $character_height); | ||
| 597 | |||
| 598 | $gc->stroke() ; | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | # ------------------------------------------------------------------------------ | ||
| 603 | |||
| 604 | sub draw_element | ||
| 605 | { | ||
| 606 | my ($self, $element, $element_index, $gc, $font_description, $widget_width, $widget_height, $character_width, $character_height) = @_ ; | ||
| 607 | |||
| 608 | my $is_selected = $element->{SELECTED} // 0 ; | ||
| 609 | $is_selected = 1 if $is_selected > 0 ; | ||
| 610 | |||
| 611 | my ($background_color, $foreground_color) = $element->get_colors() ; | ||
| 612 | |||
| 613 | if($is_selected) | ||
| 614 | { | ||
| 615 | if(exists $element->{GROUP} and defined $element->{GROUP}[-1]) | ||
| 616 | { | ||
| 617 | $background_color = $element->{GROUP}[-1]{GROUP_COLOR}[0] | ||
| 618 | } | ||
| 619 | else | ||
| 620 | { | ||
| 621 | $background_color = $self->get_color('selected_element_background'); | ||
| 622 | } | ||
| 623 | } | ||
| 624 | else | ||
| 625 | { | ||
| 626 | unless (defined $background_color) | ||
| 627 | { | ||
| 628 | if(exists $element->{GROUP} and defined $element->{GROUP}[-1]) | ||
| 629 | { | ||
| 630 | $background_color = $element->{GROUP}[-1]{GROUP_COLOR}[1] | ||
| 631 | } | ||
| 632 | else | ||
| 633 | { | ||
| 634 | $background_color = $self->get_color('element_background') ; | ||
| 635 | } | ||
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | $foreground_color //= $self->get_color('element_foreground') ; | ||
| 640 | |||
| 641 | my $color_set = $is_selected . '-' | ||
| 642 | . ($background_color // 'undef') . '-' . ($foreground_color // 'undef') . '-' | ||
| 643 | . ($self->{OPAQUE_ELEMENTS} // 1) . '-' . ($self->{NUMBERED_OBJECTS} // 0) ; | ||
| 644 | |||
| 645 | my $renderings = $element->{CACHE}{RENDERING}{$color_set} ; | ||
| 646 | |||
| 647 | unless (defined $renderings) | ||
| 648 | { | ||
| 649 | my @renderings ; | ||
| 650 | |||
| 651 | my $stripes = $element->get_stripes() ; | ||
| 652 | |||
| 653 | for my $strip (@{$stripes}) | ||
| 654 | { | ||
| 655 | my $line_index = 0 ; | ||
| 656 | |||
| 657 | my $strip_background_color = $background_color ; | ||
| 658 | my $strip_foreground_color = $foreground_color ; | ||
| 659 | |||
| 660 | unless ($is_selected) | ||
| 661 | { | ||
| 662 | $strip_background_color = $strip->{BACKGROUND} // $background_color ; | ||
| 663 | $strip_foreground_color = $strip->{FOREGROUND} // $foreground_color ; | ||
| 664 | } | ||
| 665 | |||
| 666 | $color_set .= $strip_background_color . $strip_foreground_color ; | ||
| 667 | |||
| 668 | for my $line (split /\n/, $strip->{TEXT}) | ||
| 669 | { | ||
| 670 | $line = "$line-$element" if $self->{NUMBERED_OBJECTS} ; # don't share rendering with other objects | ||
| 671 | |||
| 672 | unless (exists $self->{CACHE}{STRIPS}{$color_set}{$line}) | ||
| 673 | { | ||
| 674 | my $surface = Cairo::ImageSurface->create('argb32', $strip->{WIDTH} * $character_width, $character_height); | ||
| 675 | |||
| 676 | my $gc = Cairo::Context->create($surface); | ||
| 677 | $gc->set_source_rgba(@{$strip_background_color}, $self->{OPAQUE_ELEMENTS}); | ||
| 678 | $gc->rectangle(0, 0, $strip->{WIDTH} * $character_width, $character_height); | ||
| 679 | $gc->fill(); | ||
| 680 | |||
| 681 | $gc->set_source_rgb(@{$strip_foreground_color}); | ||
| 682 | |||
| 683 | if($self->{NUMBERED_OBJECTS}) | ||
| 684 | { | ||
| 685 | $gc->set_line_width(1); | ||
| 686 | $gc->select_font_face($self->{FONT_FAMILY}, 'normal', 'normal'); | ||
| 687 | $gc->set_font_size($self->{FONT_SIZE}); | ||
| 688 | $gc->rectangle(0, 0, $strip->{WIDTH} * $character_width, $character_height); | ||
| 689 | $gc->move_to(0, $character_height * 0.66) ; | ||
| 690 | $gc->show_text($element_index); | ||
| 691 | $gc->stroke; | ||
| 692 | } | ||
| 693 | else | ||
| 694 | { | ||
| 695 | my $layout = Pango::Cairo::create_layout($gc) ; | ||
| 696 | |||
| 697 | $layout->set_font_description($font_description) ; | ||
| 698 | |||
| 699 | $USE_MARKUP_CLASS->ui_show_markup_characters($layout, $self->{FONT_SIZE}, $line) ; | ||
| 700 | |||
| 701 | Pango::Cairo::show_layout($gc, $layout); | ||
| 702 | } | ||
| 703 | |||
| 704 | $self->{CACHE}{STRIPS}{$color_set}{$line} = $surface ; # keep reference | ||
| 705 | } | ||
| 706 | |||
| 707 | my $strip_rendering = $self->{CACHE}{STRIPS}{$color_set}{$line} ; | ||
| 708 | push @renderings, [$strip_rendering, $strip->{X_OFFSET}, $strip->{Y_OFFSET} + $line_index++] ; | ||
| 709 | } | ||
| 710 | } | ||
| 711 | |||
| 712 | $renderings = $element->{CACHE}{RENDERING}{$color_set} = \@renderings ; | ||
| 713 | } | ||
| 714 | |||
| 715 | for my $rendering (@$renderings) | ||
| 716 | { | ||
| 717 | $gc->set_source_surface | ||
| 718 | ( | ||
| 719 | $rendering->[0], | ||
| 720 | ($element->{X} + $rendering->[1]) * $character_width, # can be single addition | ||
| 721 | ($element->{Y} + $rendering->[2]) * $character_height | ||
| 722 | ); | ||
| 723 | |||
| 724 | $gc->paint; | ||
| 725 | } | ||
| 726 | } | ||
| 727 | |||
| 728 | #----------------------------------------------------------------------------- | ||
| 729 | |||
| 730 | sub button_release_event { my (undef, $event, $self) = @_ ; $self->SUPER::button_release_event($self->create_asciio_event($event)) ; } | ||
| 731 | sub button_press_event { my (undef, $event, $self) = @_ ; $self->SUPER::button_press_event($self->create_asciio_event($event)) ; } | ||
| 732 | sub motion_notify_event { my (undef, $event, $self) = @_ ; $self->SUPER::motion_notify_event($self->create_asciio_event($event)) ; } | ||
| 733 | sub key_press_event { my (undef, $event, $self) = @_ ; $self->SUPER::key_press_event($self->create_asciio_event($event)) ; } | ||
| 734 | sub mouse_scroll_event { my (undef, $event, $self) = @_ ; $self->SUPER::mouse_scroll_event($self->create_mouse_scroll_event($event)) ; } | ||
| 735 | |||
| 736 | #----------------------------------------------------------------------------- | ||
| 737 | |||
| 738 | sub create_asciio_event | ||
| 739 | { | ||
| 740 | my ($self, $event) = @_ ; | ||
| 741 | |||
| 742 | my $event_type= $event->type() ; | ||
| 743 | |||
| 744 | my $asciio_event = | ||
| 745 | { | ||
| 746 | TIME => $event->time(), | ||
| 747 | TYPE => "$event_type", | ||
| 748 | STATE => '', | ||
| 749 | MODIFIERS => get_key_modifiers($event), | ||
| 750 | BUTTON => -1, | ||
| 751 | KEY_NAME => -1, | ||
| 752 | COORDINATES => [-1, -1], | ||
| 753 | } ; | ||
| 754 | |||
| 755 | $asciio_event->{BUTTON} = $event->button() if ref $event eq 'Gtk3::Gdk::EventButton' ; | ||
| 756 | |||
| 757 | if | ||
| 758 | ( | ||
| 759 | $event_type eq "motion-notify" | ||
| 760 | || ref $event eq "Gtk3::Gdk::EventButton" | ||
| 761 | ) | ||
| 762 | { | ||
| 763 | $asciio_event->{COORDINATES} = [$self->closest_character($event->get_coords())] ; | ||
| 764 | |||
| 765 | if($event_type eq "motion-notify") | ||
| 766 | { | ||
| 767 | $asciio_event->{STATE} = "dragging-button1" if $event->state() >= "button1-mask" ; | ||
| 768 | $asciio_event->{STATE} = "dragging-button2" if $event->state() >= "button2-mask" ; | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | $asciio_event->{KEY_NAME} = Gtk3::Gdk::keyval_name($event->keyval) if $event_type eq 'key-press' ; | ||
| 773 | |||
| 774 | return $asciio_event ; | ||
| 775 | } | ||
| 776 | |||
| 777 | #----------------------------------------------------------------------------- | ||
| 778 | |||
| 779 | sub create_mouse_scroll_event | ||
| 780 | { | ||
| 781 | my ($self, $event) = @_ ; | ||
| 782 | |||
| 783 | # windows => GDK_SCROLL_UP GDK_SCROLL_DOWN | ||
| 784 | # linux => GDK_SCROLL_SMOOTH | ||
| 785 | |||
| 786 | my $asciio_mouse_scroll_event = | ||
| 787 | { | ||
| 788 | MODIFIERS => get_key_modifiers($event), | ||
| 789 | DIRECTION => 'scroll-' . ($event->direction ne 'smooth' | ||
| 790 | ? $event->direction | ||
| 791 | : ($event->get_scroll_deltas())[1] < 0 ? 'up' : 'down'), | ||
| 792 | } ; | ||
| 793 | |||
| 794 | return $asciio_mouse_scroll_event ; | ||
| 795 | } | ||
| 796 | #----------------------------------------------------------------------------- | ||
| 797 | |||
| 798 | sub get_key_modifiers | ||
| 799 | { | ||
| 800 | my ($event) = @_ ; | ||
| 801 | |||
| 802 | my $key_modifiers = $event->state() ; | ||
| 803 | |||
| 804 | my $modifiers = $key_modifiers =~ /control-mask/ ? 'C' : 0 ; | ||
| 805 | $modifiers .= $key_modifiers =~ /mod1-mask/ ? 'A' : 0 ; | ||
| 806 | $modifiers .= $key_modifiers =~ /shift-mask/ ? 'S' : 0 ; | ||
| 807 | |||
| 808 | return("$modifiers-") ; | ||
| 809 | } | ||
| 810 | |||
| 811 | #----------------------------------------------------------------------------- | ||
| 812 | |||
| 813 | sub get_character_size | ||
| 814 | { | ||
| 815 | my ($self) = @_ ; | ||
| 816 | |||
| 817 | if(exists $self->{USER_CHARACTER_WIDTH}) | ||
| 818 | { | ||
| 819 | return ($self->{USER_CHARACTER_WIDTH}, $self->{USER_CHARACTER_HEIGHT}) ; | ||
| 820 | } | ||
| 821 | else | ||
| 822 | { | ||
| 823 | unless (exists $self->{CACHE}{CHARACTER_SIZE}) | ||
| 824 | { | ||
| 825 | my $surface = Cairo::ImageSurface->create('argb32', 100, 100); | ||
| 826 | my $gc = Cairo::Context->create($surface); | ||
| 827 | my $layout = Pango::Cairo::create_layout($gc) ; | ||
| 828 | |||
| 829 | my $font_description = Pango::FontDescription->from_string($self->{FONT_FAMILY} . ' ' . $self->{FONT_SIZE}) ; | ||
| 830 | $layout->set_font_description($font_description) ; | ||
| 831 | $layout->set_text('M') ; | ||
| 832 | |||
| 833 | $self->{CACHE}{CHARACTER_SIZE} = [$layout->get_pixel_size()] ; | ||
| 834 | } | ||
| 835 | |||
| 836 | return @{ $self->{CACHE}{CHARACTER_SIZE} } ; | ||
| 837 | } | ||
| 838 | } | ||
| 839 | |||
| 840 | #----------------------------------------------------------------------------- | ||
| 841 | |||
| 842 | sub invalidate_rendering_cache | ||
| 843 | { | ||
| 844 | my ($self) = @_ ; | ||
| 845 | |||
| 846 | for my $element (@{$self->{ELEMENTS}}) | ||
| 847 | { | ||
| 848 | delete $element->{CACHE} ; | ||
| 849 | } | ||
| 850 | |||
| 851 | delete $self->{CACHE} ; | ||
| 852 | } | ||
| 853 | |||
| 854 | #---------------------------------------------------------------------------------------------- | ||
| 855 | |||
| 856 | |||
| 857 | =head1 DEPENDENCIES | ||
| 858 | |||
| 859 | gnome libraries, gtk, gtk-perl, perl | ||
| 860 | |||
| 861 | =head1 AUTHOR | ||
| 862 | |||
| 863 | Khemir Nadim ibn Hamouda | ||
| 864 | CPAN ID: NKH | ||
| 865 | mailto:nadim@khemir.net | ||
| 866 | |||
| 867 | =head1 LICENSE AND COPYRIGHT | ||
| 868 | |||
| 869 | This program is free software; you can redistribute | ||
| 870 | it and/or modify it under the same terms as Perl itself. | ||
| 871 | |||
| 872 | =cut | ||
| 873 | |||
| 874 | #------------------------------------------------------------------------------------------------------ | ||
| 875 | |||
| 876 | "GTK world domination!" ; | ||
| 877 |