... | ... |
@@ -470,83 +470,29 @@ class XDotParser: |
470 | 470 |
return x, y |
471 | 471 |
|
472 | 472 |
|
473 |
-class DotWindow(gtk.Window): |
|
474 |
- |
|
475 |
- # TODO: Make a seperate, reusable widget |
|
476 |
- |
|
477 |
- ui = ''' |
|
478 |
- <ui> |
|
479 |
- <toolbar name="ToolBar"> |
|
480 |
- <toolitem action="ZoomIn"/> |
|
481 |
- <toolitem action="ZoomOut"/> |
|
482 |
- <toolitem action="ZoomFit"/> |
|
483 |
- <toolitem action="Zoom100"/> |
|
484 |
- </toolbar> |
|
485 |
- </ui> |
|
486 |
- ''' |
|
473 |
+class DotWidget(gtk.DrawingArea): |
|
487 | 474 |
|
488 | 475 |
def __init__(self): |
489 |
- gtk.Window.__init__(self) |
|
476 |
+ gtk.DrawingArea.__init__(self) |
|
490 | 477 |
|
491 | 478 |
self.graph = Graph() |
492 | 479 |
|
493 |
- window = self |
|
480 |
+ self.connect("expose_event", self.on_expose) |
|
494 | 481 |
|
495 |
- window.set_title('Dot') |
|
496 |
- window.set_default_size(512, 512) |
|
497 |
- vbox = gtk.VBox() |
|
498 |
- window.add(vbox) |
|
499 |
- |
|
500 |
- # Create a UIManager instance |
|
501 |
- uimanager = self.uimanager = gtk.UIManager() |
|
482 |
+ self.set_flags(gtk.CAN_FOCUS) |
|
502 | 483 |
|
503 |
- # Add the accelerator group to the toplevel window |
|
504 |
- accelgroup = uimanager.get_accel_group() |
|
505 |
- window.add_accel_group(accelgroup) |
|
506 |
- |
|
507 |
- # Create an ActionGroup |
|
508 |
- actiongroup = gtk.ActionGroup('Actions') |
|
509 |
- self.actiongroup = actiongroup |
|
510 |
- |
|
511 |
- # Create actions |
|
512 |
- actiongroup.add_actions(( |
|
513 |
- ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.on_zoom_in), |
|
514 |
- ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.on_zoom_out), |
|
515 |
- ('ZoomFit', gtk.STOCK_ZOOM_FIT, None, None, None, self.on_zoom_fit), |
|
516 |
- ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.on_zoom_100), |
|
517 |
- )) |
|
518 |
- |
|
519 |
- # Add the actiongroup to the uimanager |
|
520 |
- uimanager.insert_action_group(actiongroup, 0) |
|
521 |
- |
|
522 |
- # Add a UI description |
|
523 |
- uimanager.add_ui_from_string(self.ui) |
|
524 |
- |
|
525 |
- # Create a Toolbar |
|
526 |
- toolbar = uimanager.get_widget('/ToolBar') |
|
527 |
- vbox.pack_start(toolbar, False) |
|
528 |
- |
|
529 |
- self.area = gtk.DrawingArea() |
|
530 |
- self.area.connect("expose_event", self.on_expose) |
|
531 |
- vbox.pack_start(self.area) |
|
532 |
- |
|
533 |
- self.area.set_flags(gtk.CAN_FOCUS) |
|
534 |
- self.set_focus(self.area) |
|
535 |
- |
|
536 |
- self.area.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
|
537 |
- self.area.connect("button-press-event", self.on_area_button_press) |
|
538 |
- self.area.connect("button-release-event", self.on_area_button_release) |
|
539 |
- self.area.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
|
540 |
- self.area.connect("motion-notify-event", self.on_area_motion_notify) |
|
541 |
- self.area.connect("scroll-event", self.on_area_scroll_event) |
|
484 |
+ self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
|
485 |
+ self.connect("button-press-event", self.on_area_button_press) |
|
486 |
+ self.connect("button-release-event", self.on_area_button_release) |
|
487 |
+ self.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
|
488 |
+ self.connect("motion-notify-event", self.on_area_motion_notify) |
|
489 |
+ self.connect("scroll-event", self.on_area_scroll_event) |
|
542 | 490 |
|
543 | 491 |
self.connect('key-press-event', self.on_key_press_event) |
544 | 492 |
|
545 | 493 |
self.x, self.y = 0.0, 0.0 |
546 | 494 |
self.zoom_ratio = 1.0 |
547 | 495 |
|
548 |
- self.show_all() |
|
549 |
- |
|
550 | 496 |
def set_dotcode(self, dotcode): |
551 | 497 |
p = subprocess.Popen( |
552 | 498 |
['dot', '-Txdot'], |
... | ... |
@@ -577,7 +523,7 @@ class DotWindow(gtk.Window): |
577 | 523 |
cr.set_source_rgba(1.0, 1.0, 1.0, 1.0) |
578 | 524 |
cr.paint() |
579 | 525 |
|
580 |
- rect = self.area.get_allocation() |
|
526 |
+ rect = self.get_allocation() |
|
581 | 527 |
cr.translate(0.5*rect.width, 0.5*rect.height) |
582 | 528 |
cr.scale(self.zoom_ratio, self.zoom_ratio) |
583 | 529 |
cr.translate(-self.x, -self.y) |
... | ... |
@@ -592,14 +538,14 @@ class DotWindow(gtk.Window): |
592 | 538 |
def set_current_pos(self, x, y): |
593 | 539 |
self.x = x |
594 | 540 |
self.y = y |
595 |
- self.area.queue_draw() |
|
541 |
+ self.queue_draw() |
|
596 | 542 |
|
597 | 543 |
def zoom_image(self, zoom_ratio, center=False): |
598 | 544 |
if center: |
599 | 545 |
self.x = self.graph.width/2 |
600 | 546 |
self.y = self.graph.height/2 |
601 | 547 |
self.zoom_ratio = zoom_ratio |
602 |
- self.area.queue_draw() |
|
548 |
+ self.queue_draw() |
|
603 | 549 |
|
604 | 550 |
ZOOM_INCREMENT = 1.25 |
605 | 551 |
|
... | ... |
@@ -610,7 +556,7 @@ class DotWindow(gtk.Window): |
610 | 556 |
self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) |
611 | 557 |
|
612 | 558 |
def on_zoom_fit(self, action): |
613 |
- rect = self.area.get_allocation() |
|
559 |
+ rect = self.get_allocation() |
|
614 | 560 |
zoom_ratio = min( |
615 | 561 |
float(rect.width)/float(self.graph.width), |
616 | 562 |
float(rect.height)/float(self.graph.height) |
... | ... |
@@ -625,27 +571,27 @@ class DotWindow(gtk.Window): |
625 | 571 |
def on_key_press_event(self, widget, event): |
626 | 572 |
if event.keyval == gtk.keysyms.Left: |
627 | 573 |
self.x -= self.POS_INCREMENT/self.zoom_ratio |
628 |
- self.area.queue_draw() |
|
574 |
+ self.queue_draw() |
|
629 | 575 |
return True |
630 | 576 |
if event.keyval == gtk.keysyms.Right: |
631 | 577 |
self.x += self.POS_INCREMENT/self.zoom_ratio |
632 |
- self.area.queue_draw() |
|
578 |
+ self.queue_draw() |
|
633 | 579 |
return True |
634 | 580 |
if event.keyval == gtk.keysyms.Up: |
635 | 581 |
self.y -= self.POS_INCREMENT/self.zoom_ratio |
636 |
- self.area.queue_draw() |
|
582 |
+ self.queue_draw() |
|
637 | 583 |
return True |
638 | 584 |
if event.keyval == gtk.keysyms.Down: |
639 | 585 |
self.y += self.POS_INCREMENT/self.zoom_ratio |
640 |
- self.area.queue_draw() |
|
586 |
+ self.queue_draw() |
|
641 | 587 |
return True |
642 | 588 |
if event.keyval == gtk.keysyms.Page_Up: |
643 | 589 |
self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT) |
644 |
- self.area.queue_draw() |
|
590 |
+ self.queue_draw() |
|
645 | 591 |
return True |
646 | 592 |
if event.keyval == gtk.keysyms.Page_Down: |
647 | 593 |
self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) |
648 |
- self.area.queue_draw() |
|
594 |
+ self.queue_draw() |
|
649 | 595 |
return True |
650 | 596 |
return False |
651 | 597 |
|
... | ... |
@@ -689,7 +635,7 @@ class DotWindow(gtk.Window): |
689 | 635 |
# pan the image |
690 | 636 |
self.x += (self.prevmousex - x)/self.zoom_ratio |
691 | 637 |
self.y += (self.prevmousey - y)/self.zoom_ratio |
692 |
- self.area.queue_draw() |
|
638 |
+ self.queue_draw() |
|
693 | 639 |
self.prevmousex = x |
694 | 640 |
self.prevmousey = y |
695 | 641 |
else: |
... | ... |
@@ -702,7 +648,7 @@ class DotWindow(gtk.Window): |
702 | 648 |
return True |
703 | 649 |
|
704 | 650 |
def get_url(self, x, y): |
705 |
- rect = self.area.get_allocation() |
|
651 |
+ rect = self.get_allocation() |
|
706 | 652 |
x -= 0.5*rect.width |
707 | 653 |
y -= 0.5*rect.height |
708 | 654 |
x /= self.zoom_ratio |
... | ... |
@@ -716,6 +662,77 @@ class DotWindow(gtk.Window): |
716 | 662 |
return False |
717 | 663 |
|
718 | 664 |
|
665 |
+class DotWindow(gtk.Window): |
|
666 |
+ |
|
667 |
+ ui = ''' |
|
668 |
+ <ui> |
|
669 |
+ <toolbar name="ToolBar"> |
|
670 |
+ <toolitem action="ZoomIn"/> |
|
671 |
+ <toolitem action="ZoomOut"/> |
|
672 |
+ <toolitem action="ZoomFit"/> |
|
673 |
+ <toolitem action="Zoom100"/> |
|
674 |
+ </toolbar> |
|
675 |
+ </ui> |
|
676 |
+ ''' |
|
677 |
+ |
|
678 |
+ def __init__(self): |
|
679 |
+ gtk.Window.__init__(self) |
|
680 |
+ |
|
681 |
+ self.graph = Graph() |
|
682 |
+ |
|
683 |
+ window = self |
|
684 |
+ |
|
685 |
+ window.set_title('Dot') |
|
686 |
+ window.set_default_size(512, 512) |
|
687 |
+ vbox = gtk.VBox() |
|
688 |
+ window.add(vbox) |
|
689 |
+ |
|
690 |
+ self.widget = DotWidget() |
|
691 |
+ |
|
692 |
+ # Create a UIManager instance |
|
693 |
+ uimanager = self.uimanager = gtk.UIManager() |
|
694 |
+ |
|
695 |
+ # Add the accelerator group to the toplevel window |
|
696 |
+ accelgroup = uimanager.get_accel_group() |
|
697 |
+ window.add_accel_group(accelgroup) |
|
698 |
+ |
|
699 |
+ # Create an ActionGroup |
|
700 |
+ actiongroup = gtk.ActionGroup('Actions') |
|
701 |
+ self.actiongroup = actiongroup |
|
702 |
+ |
|
703 |
+ # Create actions |
|
704 |
+ actiongroup.add_actions(( |
|
705 |
+ ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget.on_zoom_in), |
|
706 |
+ ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget.on_zoom_out), |
|
707 |
+ ('ZoomFit', gtk.STOCK_ZOOM_FIT, None, None, None, self.widget.on_zoom_fit), |
|
708 |
+ ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget.on_zoom_100), |
|
709 |
+ )) |
|
710 |
+ |
|
711 |
+ # Add the actiongroup to the uimanager |
|
712 |
+ uimanager.insert_action_group(actiongroup, 0) |
|
713 |
+ |
|
714 |
+ # Add a UI description |
|
715 |
+ uimanager.add_ui_from_string(self.ui) |
|
716 |
+ |
|
717 |
+ # Create a Toolbar |
|
718 |
+ toolbar = uimanager.get_widget('/ToolBar') |
|
719 |
+ vbox.pack_start(toolbar, False) |
|
720 |
+ |
|
721 |
+ vbox.pack_start(self.widget) |
|
722 |
+ |
|
723 |
+ self.set_focus(self.widget) |
|
724 |
+ |
|
725 |
+ self.widget.on_url_clicked = self.on_url_clicked |
|
726 |
+ |
|
727 |
+ self.show_all() |
|
728 |
+ |
|
729 |
+ def set_dotcode(self, dotcode): |
|
730 |
+ self.widget.set_dotcode(dotcode) |
|
731 |
+ |
|
732 |
+ def on_url_clicked(self, url, event): |
|
733 |
+ return False |
|
734 |
+ |
|
735 |
+ |
|
719 | 736 |
def main(): |
720 | 737 |
import optparse |
721 | 738 |
|