• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • part
katebookmarks.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2002, 2003, 2004 Anders Lund <anders.lund@lund.tdcadsl.dk>
3  Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "katebookmarks.h"
21 #include "katebookmarks.moc"
22 
23 #include "katedocument.h"
24 #include "kateview.h"
25 
26 #include <tdelocale.h>
27 #include <tdeaction.h>
28 #include <tdepopupmenu.h>
29 #include <kstringhandler.h>
30 #include <kxmlguiclient.h>
31 #include <kxmlguifactory.h>
32 
33 #include <tqregexp.h>
34 #include <tqmemarray.h>
35 #include <tqevent.h>
36 
44 static void ssort( TQMemArray<uint> &a, int max )
45 {
46  uint tmp, j, maxpos;
47  for ( uint h = max; h >= 1; h-- )
48  {
49  maxpos = 0;
50  for ( j = 0; j <= h; j++ )
51  maxpos = a[j] > a[maxpos] ? j : maxpos;
52  tmp = a[maxpos];
53  a[maxpos] = a[h];
54  a[h] = tmp;
55  }
56 }
57 
58 // TODO add a insort() or bubble_sort - more efficient for aboutToShow() ?
59 
60 KateBookmarks::KateBookmarks( KateView* view, Sorting sort )
61  : TQObject( view, "kate bookmarks" )
62  , m_view( view )
63  , m_sorting( sort )
64 {
65  connect (view->getDoc(), TQ_SIGNAL(marksChanged()), this, TQ_SLOT(marksChanged()));
66  _tries=0;
67  m_bookmarksMenu = 0L;
68 }
69 
70 KateBookmarks::~KateBookmarks()
71 {
72 }
73 
74 void KateBookmarks::createActions( TDEActionCollection* ac )
75 {
76  m_bookmarkToggle = new TDEToggleAction(
77  i18n("Set &Bookmark"), "bookmark", CTRL+Key_B,
78  this, TQ_SLOT(toggleBookmark()),
79  ac, "bookmarks_toggle" );
80  m_bookmarkToggle->setWhatsThis(i18n("If a line has no bookmark then add one, otherwise remove it."));
81  m_bookmarkToggle->setCheckedState( i18n("Clear &Bookmark") );
82 
83  m_bookmarkClear = new TDEAction(
84  i18n("Clear &All Bookmarks"), 0,
85  this, TQ_SLOT(clearBookmarks()),
86  ac, "bookmarks_clear");
87  m_bookmarkClear->setWhatsThis(i18n("Remove all bookmarks of the current document."));
88 
89  m_goNext = new TDEAction(
90  i18n("Next Bookmark"), "go-next", ALT + Key_PageDown,
91  this, TQ_SLOT(goNext()),
92  ac, "bookmarks_next");
93  m_goNext->setWhatsThis(i18n("Go to the next bookmark."));
94 
95  m_goPrevious = new TDEAction(
96  i18n("Previous Bookmark"), "go-previous", ALT + Key_PageUp,
97  this, TQ_SLOT(goPrevious()),
98  ac, "bookmarks_previous");
99  m_goPrevious->setWhatsThis(i18n("Go to the previous bookmark."));
100 
101  m_bookmarksMenu = (new TDEActionMenu(i18n("&Bookmarks"), ac, "bookmarks"))->popupMenu();
102 
103  //connect the aboutToShow() and aboutToHide() signals with
104  //the bookmarkMenuAboutToShow() and bookmarkMenuAboutToHide() slots
105  connect( m_bookmarksMenu, TQ_SIGNAL(aboutToShow()), this, TQ_SLOT(bookmarkMenuAboutToShow()));
106  connect( m_bookmarksMenu, TQ_SIGNAL(aboutToHide()), this, TQ_SLOT(bookmarkMenuAboutToHide()) );
107 
108  marksChanged ();
109  bookmarkMenuAboutToHide();
110 
111  connect( m_view, TQ_SIGNAL( gotFocus( Kate::View * ) ), this, TQ_SLOT( slotViewGotFocus( Kate::View * ) ) );
112  connect( m_view, TQ_SIGNAL( lostFocus( Kate::View * ) ), this, TQ_SLOT( slotViewLostFocus( Kate::View * ) ) );
113 }
114 
115 void KateBookmarks::toggleBookmark ()
116 {
117  uint mark = m_view->getDoc()->mark( m_view->cursorLine() );
118  if( mark & KTextEditor::MarkInterface::markType01 )
119  m_view->getDoc()->removeMark( m_view->cursorLine(),
120  KTextEditor::MarkInterface::markType01 );
121  else
122  m_view->getDoc()->addMark( m_view->cursorLine(),
123  KTextEditor::MarkInterface::markType01 );
124 }
125 
126 void KateBookmarks::clearBookmarks ()
127 {
128 
129  TQPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
130  for (uint i=0; i < m.count(); i++)
131  m_view->getDoc()->removeMark( m.at(i)->line, KTextEditor::MarkInterface::markType01 );
132 
133  // just to be sure ;)
134  marksChanged ();
135 }
136 
137 void KateBookmarks::slotViewGotFocus( Kate::View *v )
138 {
139  if ( v == (Kate::View*)m_view )
140  bookmarkMenuAboutToHide();
141 }
142 
143 void KateBookmarks::slotViewLostFocus( Kate::View *v )
144 {
145  if ( v == (Kate::View*)m_view )
146  m_bookmarksMenu->clear();
147 }
148 
149 void KateBookmarks::insertBookmarks( TQPopupMenu& menu )
150 {
151  uint line = m_view->cursorLine();
152  const TQRegExp re("&(?!&)");
153  int idx( -1 );
154  int old_menu_count = menu.count();
155  KTextEditor::Mark *next = 0;
156  KTextEditor::Mark *prev = 0;
157 
158  TQPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
159  TQMemArray<uint> sortArray( m.count() );
160  TQPtrListIterator<KTextEditor::Mark> it( m );
161 
162  if ( it.count() > 0 )
163  menu.insertSeparator();
164 
165  for( int i = 0; *it; ++it, ++i )
166  {
167  if( (*it)->type & KTextEditor::MarkInterface::markType01 )
168  {
169  TQString bText = KStringHandler::rEmSqueeze
170  ( m_view->getDoc()->textLine( (*it)->line ),
171  menu.fontMetrics(), 32 );
172  bText.replace(re, "&&"); // kill undesired accellerators!
173  bText.replace('\t', ' '); // kill tabs, as they are interpreted as shortcuts
174 
175  if ( m_sorting == Position )
176  {
177  sortArray[i] = (*it)->line;
178  ssort( sortArray, i );
179  idx = sortArray.find( (*it)->line ) + 3;
180  }
181 
182  menu.insertItem(
183  TQString("%1 - \"%2\"").arg( (*it)->line+1 ).arg( bText ),
184  m_view, TQ_SLOT(gotoLineNumber(int)), 0, (*it)->line, idx );
185 
186  if ( (*it)->line < line )
187  {
188  if ( ! prev || prev->line < (*it)->line )
189  prev = (*it);
190  }
191 
192  else if ( (*it)->line > line )
193  {
194  if ( ! next || next->line > (*it)->line )
195  next = (*it);
196  }
197  }
198  }
199 
200  idx = ++old_menu_count;
201  if ( next )
202  {
203  m_goNext->setText( i18n("&Next: %1 - \"%2\"").arg( next->line + 1 )
204  .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( next->line ), 24 ) ) );
205  m_goNext->plug( &menu, idx );
206  idx++;
207  }
208  if ( prev )
209  {
210  m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"").arg(prev->line + 1 )
211  .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( prev->line ), 24 ) ) );
212  m_goPrevious->plug( &menu, idx );
213  idx++;
214  }
215  if ( next || prev )
216  menu.insertSeparator( idx );
217 
218 }
219 
220 void KateBookmarks::bookmarkMenuAboutToShow()
221 {
222 
223  TQPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
224 
225  m_bookmarksMenu->clear();
226  m_bookmarkToggle->setChecked( m_view->getDoc()->mark( m_view->cursorLine() )
227  & KTextEditor::MarkInterface::markType01 );
228  m_bookmarkToggle->plug( m_bookmarksMenu );
229  m_bookmarkClear->plug( m_bookmarksMenu );
230 
231 
232  insertBookmarks(*m_bookmarksMenu);
233 }
234 
235 /*
236  Make sure next/prev actions are plugged, and have a clean text
237 */
238 void KateBookmarks::bookmarkMenuAboutToHide()
239 {
240  m_bookmarkToggle->plug( m_bookmarksMenu );
241  m_bookmarkClear->plug( m_bookmarksMenu );
242  m_goNext->setText( i18n("Next Bookmark") );
243  m_goNext->plug( m_bookmarksMenu );
244  m_goPrevious->setText( i18n("Previous Bookmark") );
245  m_goPrevious->plug( m_bookmarksMenu );
246 }
247 
248 void KateBookmarks::goNext()
249 {
250  TQPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
251  if (m.isEmpty())
252  return;
253 
254  uint line = m_view->cursorLine();
255  int found = -1;
256 
257  for (uint z=0; z < m.count(); z++)
258  if ( (m.at(z)->line > line) && ((found == -1) || (uint(found) > m.at(z)->line)) )
259  found = m.at(z)->line;
260 
261  if (found != -1)
262  m_view->gotoLineNumber ( found );
263 }
264 
265 void KateBookmarks::goPrevious()
266 {
267  TQPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
268  if (m.isEmpty())
269  return;
270 
271  uint line = m_view->cursorLine();
272  int found = -1;
273 
274  for (uint z=0; z < m.count(); z++)
275  if ((m.at(z)->line < line) && ((found == -1) || (uint(found) < m.at(z)->line)))
276  found = m.at(z)->line;
277 
278  if (found != -1)
279  m_view->gotoLineNumber ( found );
280 }
281 
282 void KateBookmarks::marksChanged ()
283 {
284  m_bookmarkClear->setEnabled( !m_view->getDoc()->marks().isEmpty() );
285 }
KStringHandler::rEmSqueeze
static TQString rEmSqueeze(const TQString &name, const TQFontMetrics &fontMetrics, uint maxlen=30)
KStringHandler::rsqueeze
static TQString rsqueeze(const TQString &str, uint maxlen=40)
Kate::View
The Kate::View text editor interface.
Definition: view.h:45
Kate::View::getDoc
virtual Document * getDoc()
Returns a pointer to the document of the view.
Definition: view.h:267
TDEActionCollection
TDEActionMenu
TDEAction
TDEToggleAction
TDEStdAccel::next
const TDEShortcut & next()
tdelocale.h

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.1
This website is maintained by Timothy Pearson.