summaryrefslogtreecommitdiffstats
path: root/kdvi/dviWidget.cpp
blob: 8b8545b60d814e7f04c038942988f39fc105a336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// Class: DVIWidget
//
// Widget for displaying TeX DVI files.
// Part of KDVI- A previewer for TeX DVI files.
//
// (C) 2004 Wilfried Huss, Stefan Kebekus
// Distributed under the GPL
//

#include <config.h>

#include <kdebug.h>
#include <tdelocale.h>

#include "dviWidget.h"

#include "documentPageCache.h"
#include "documentWidget.h"
#include "hyperlink.h"
#include "pageView.h"
#include "renderedDviPagePixmap.h"
#include "selection.h"

DVIWidget::DVIWidget(TQWidget* parent, PageView* sv, DocumentPageCache* cache, const char* name)
  : DocumentWidget(parent, sv, cache, name)
{
}


void DVIWidget::mousePressEvent(TQMouseEvent* e)
{
  // pageNr == 0 indicated an invalid page (e.g. page number not yet set)
  if (pageNr == 0)
    return;

  // Get a pointer to the page contents
  RenderedDviPagePixmap* pageData = dynamic_cast<RenderedDviPagePixmap*>(documentCache->getPage(pageNr));
  if (pageData == 0)
  {
    kdDebug(4300) << "DVIWidget::mousePressEvent(...) pageData for page #" << pageNr << " is empty" << endl;
    return;
  }

  // Check if the mouse is pressed on a source-hyperlink
  // source hyperlinks can be invoked with the Middle Mousebutton or alternatively
  // with Control+Left Mousebutton
  if ((e->button() == Qt::MidButton || (e->button() == Qt::LeftButton && (e->state() & ControlButton)))
      && (pageData->sourceHyperLinkList.size() > 0))
  {
    int minIndex = 0;
    int minimum = 0;

    for(unsigned int i=0; i<pageData->sourceHyperLinkList.size(); i++)
    {
      if (pageData->sourceHyperLinkList[i].box.contains(e->pos()))
      {
        emit(SRCLink(pageData->sourceHyperLinkList[i].linkText, e, this));
        e->accept();
        return;
      }
      // Remember the closest source link
      TQPoint center = pageData->sourceHyperLinkList[i].box.center();
      int dx = center.x() - e->pos().x();
      int dy = center.y() - e->pos().y();
      if (dx*dx + dy*dy < minimum || i == 0)
      {
        minIndex = i;
        minimum = dx*dx + dy*dy;
      }
    }
    // If the mouse pointer is not exactly inside a source link, jump to the closest target.
    emit(SRCLink(pageData->sourceHyperLinkList[minIndex].linkText, e, this));
    e->accept();
  }

  // Call implementation from parent
  DocumentWidget::mousePressEvent(e);
}


void DVIWidget::mouseMoveEvent(TQMouseEvent* e)
{
  // pageNr == 0 indicated an invalid page (e.g. page number not yet set)
  if (pageNr == 0)
    return;

  // Call the standard implementation
  DocumentWidget::mouseMoveEvent(e);
  
  // Analyze the mouse movement only if no mouse button was pressed
  if ( e->state() == 0 ) {
    // Get a pointer to the page contents
    RenderedDviPagePixmap* pageData = dynamic_cast<RenderedDviPagePixmap*>(documentCache->getPage(pageNr));
    if (pageData == 0) {
      kdDebug(4300) << "DVIWidget::mouseMoveEvent(...) pageData for page #" << pageNr << " is empty" << endl;
      return;
    }
    
    // Check if the cursor hovers over a sourceHyperlink.
    for(unsigned int i=0; i<pageData->sourceHyperLinkList.size(); i++) {
      if (pageData->sourceHyperLinkList[i].box.contains(e->pos())) {
	clearStatusBarTimer.stop();
	
	// The macro-package srcltx gives a special like "src:99 test.tex"
	// while MikTeX gives "src:99test.tex". KDVI tries
	// to understand both.
	TQString cp = pageData->sourceHyperLinkList[i].linkText;
	int max = cp.length();
	int i;
	for(i=0; i<max; i++)
	  if (cp[i].isDigit() == false)
	    break;
	
	emit setStatusBarText( i18n("line %1 of %2").arg(cp.left(i)).arg(cp.mid(i).simplifyWhiteSpace()) );
	return;
      }
    }
  }
}


#include "dviWidget.moc"