srcfilewin.ui.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /****************************************************************************
  2. ** ui.h extension file, included from the uic-generated form implementation.
  3. **
  4. ** If you wish to add, delete or rename slots use Qt Designer which will
  5. ** update this file, preserving your code. Create an init() slot in place of
  6. ** a constructor, and a destroy() slot in place of a destructor.
  7. *****************************************************************************/
  8. /*
  9. ktigcc - TIGCC IDE for KDE
  10. Copyright (C) 2004-2006 Kevin Kofler
  11. Copyright (C) 2006 Joey Adams
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2, or (at your option)
  15. any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software Foundation,
  22. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #include <qstring.h>
  25. #include <qregexp.h>
  26. #include <qapplication.h>
  27. #include <qlabel.h>
  28. #include <qstatusbar.h>
  29. #include <qtimer.h>
  30. #include <qdatetime.h>
  31. #include <qdragobject.h>
  32. #include <qassistantclient.h>
  33. #include <qdir.h>
  34. #include <qclipboard.h>
  35. #include <qaccel.h>
  36. #include <qeventloop.h>
  37. #include <kparts/factory.h>
  38. #include <klibloader.h>
  39. #include <kate/document.h>
  40. #include <kate/view.h>
  41. #include <kconfig.h>
  42. #include <ktexteditor/editinterfaceext.h>
  43. #include <ktexteditor/configinterfaceextension.h>
  44. #include <kfiledialog.h>
  45. #include <kurl.h>
  46. #include <kmessagebox.h>
  47. #include <kdirwatch.h>
  48. #include <kfinddialog.h>
  49. #include <kfind.h>
  50. #include <kreplacedialog.h>
  51. #include <kreplace.h>
  52. #include <kwin.h>
  53. #include <kglobal.h>
  54. #include <kicontheme.h>
  55. #include <kiconloader.h>
  56. #include <cstdio>
  57. #include <cstdlib>
  58. #include "ktigcc.h"
  59. #include "mainform.h"
  60. #include "tpr.h"
  61. #include "preferences.h"
  62. #include "projectoptions.h"
  63. #include "srcfile.h"
  64. using std::puts;
  65. using std::exit;
  66. #define TIGCC_TPR_Filter "*.tpr|TIGCC Projects (*.tpr)\n"
  67. #define TIGCC_H_Filter "*.h|Header Files (*.h)\n"
  68. #define TIGCC_C_Filter "*.c|C Files (*.c)\n"
  69. #define TIGCC_S_Filter "*.s|GNU Assembly Files (*.s)\n"
  70. #define TIGCC_ASM_Filter "*.asm|A68k Assembly Files (*.asm)\n"
  71. #define TIGCC_QLL_Filter "*.qll|Quill Files (*.qll)\n"
  72. #define TIGCC_O_Filter "*.o|Object Files (*.o)\n"
  73. #define TIGCC_A_Filter "*.a|Archive Files (*.a)\n"
  74. #define TIGCC_TXT_Filter "*.txt|Text Files (*.txt)\n"
  75. #define TIGCCAllFilter "*|All Files (*)"
  76. enum {TIGCCOpenProjectFileFilter,TIGCCAddFilesFilter};
  77. #define TIGCCProjectDirectory "/usr/local/tigcc/projects"
  78. #define THIS (static_cast<SourceFile *>(this))
  79. #define CURRENT_VIEW (THIS->kateView)
  80. #define LOAD_ICON(name) (QIconSet(KGlobal::iconLoader()->loadIcon((name),KIcon::Small),KGlobal::iconLoader()->loadIcon((name),KIcon::MainToolbar)))
  81. #define SYSICON(sysname,name) (preferences.useSystemIcons?KGlobal::iconLoader()->loadIcon((sysname),KIcon::Small):QPixmap::fromMimeSource((name)))
  82. // For some reason, this flag is not in the public ConfigFlags enum.
  83. #define CF_REMOVE_TRAILING_DYN 0x4000000
  84. class KReplaceWithSelectionS : public KReplace {
  85. public:
  86. KReplaceWithSelectionS(const QString &pattern, const QString &replacement,
  87. long options, QWidget *parent=0) :
  88. KReplace(pattern,replacement,options,parent), m_haveSelection(FALSE) {}
  89. void setSelection(unsigned selStartLine, unsigned selStartCol,
  90. unsigned selEndLine, unsigned selEndCol)
  91. {
  92. m_haveSelection=TRUE;
  93. m_selStartLine=selStartLine;
  94. m_selStartCol=selStartCol;
  95. m_selEndLine=selEndLine;
  96. m_selEndCol=selEndCol;
  97. }
  98. unsigned replaceCurrentLine;
  99. void invalidateSelection() {m_haveSelection=FALSE;}
  100. bool haveSelection() {return m_haveSelection;}
  101. unsigned selStartLine() {return m_selStartLine;}
  102. unsigned selStartCol() {return m_selStartCol;}
  103. unsigned selEndLine() {return m_selEndLine;}
  104. unsigned selEndCol() {return m_selEndCol;}
  105. // Override to ask for restarting when replacing in a selection.
  106. bool shouldRestart(bool forceAsking=FALSE, bool showNumMatches=TRUE)
  107. {
  108. return KReplace::shouldRestart(forceAsking||m_haveSelection,showNumMatches);
  109. }
  110. protected:
  111. virtual bool validateMatch(const QString &text, int index, int matchedlength)
  112. {
  113. if (!KReplace::validateMatch(text,index,matchedlength)) return FALSE;
  114. if (!m_haveSelection) return TRUE;
  115. if (replaceCurrentLine==m_selStartLine && replaceCurrentLine==m_selEndLine)
  116. return ((unsigned)index>=m_selStartCol)&&((unsigned)index+(unsigned)matchedlength<=m_selEndCol);
  117. else if (replaceCurrentLine==m_selStartLine)
  118. return ((unsigned)index>=m_selStartCol);
  119. else if (replaceCurrentLine==m_selEndLine)
  120. return ((unsigned)index+(unsigned)matchedlength<=m_selEndCol);
  121. else
  122. return (replaceCurrentLine>=m_selStartLine&&replaceCurrentLine<=m_selEndLine);
  123. }
  124. private:
  125. bool m_haveSelection;
  126. unsigned m_selStartLine, m_selStartCol, m_selEndLine, m_selEndCol;
  127. };
  128. void SourceFileWindow::init()
  129. {
  130. THIS->te_popup = new QPopupMenu(this);
  131. THIS->te_popup->insertItem("&Open file at cursor",0);
  132. THIS->te_popup->insertItem("&Find symbol declaration",1);
  133. THIS->te_popup->insertSeparator();
  134. THIS->te_popup->insertItem("&Undo",2);
  135. THIS->te_popup->insertItem("&Redo",3);
  136. THIS->te_popup->insertSeparator();
  137. THIS->te_popup->insertItem("&Clear",4);
  138. THIS->te_popup->insertItem("Cu&t",5);
  139. THIS->te_popup->insertItem("Cop&y",6);
  140. THIS->te_popup->insertItem("&Paste",7);
  141. THIS->te_popup->insertSeparator();
  142. THIS->te_popup->insertItem("&Select all",8);
  143. THIS->te_popup->insertSeparator();
  144. THIS->te_popup->insertItem("&Increase indent",9);
  145. THIS->te_popup->insertItem("&Decrease indent",10);
  146. connect(THIS->te_popup,SIGNAL(aboutToShow()),this,SLOT(te_popup_aboutToShow()));
  147. connect(THIS->te_popup,SIGNAL(activated(int)),this,SLOT(te_popup_activated(int)));
  148. THIS->kateView=reinterpret_cast<Kate::View *>(createView(THIS->fileName,loadFileText(THIS->fileName),THIS->hlMode,
  149. THIS->isASMFile?preferences.tabWidthAsm:THIS->isCFile?preferences.tabWidthC:8));
  150. int rightStatusSize=size().width();
  151. unsigned int line, col;
  152. CURRENT_VIEW->cursorPositionReal(&line,&col);
  153. THIS->rowStatusLabel=new QLabel(QString("%1").arg(line+1),this);
  154. THIS->rowStatusLabel->setAlignment(Qt::AlignRight);
  155. THIS->rowStatusLabel->setMaximumWidth(30);
  156. statusBar()->addWidget(THIS->rowStatusLabel,1);
  157. THIS->colStatusLabel=new QLabel(QString("%1").arg(col+1),this);
  158. THIS->colStatusLabel->setAlignment(Qt::AlignRight);
  159. THIS->colStatusLabel->setMaximumWidth(30);
  160. statusBar()->addWidget(THIS->colStatusLabel,1);
  161. THIS->charsStatusLabel=new QLabel(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()),this);
  162. THIS->charsStatusLabel->setMaximumWidth(100);
  163. statusBar()->addWidget(THIS->charsStatusLabel,1);
  164. THIS->rightStatusLabel=new QLabel(THIS->fileName,this);
  165. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  166. statusBar()->addWidget(THIS->rightStatusLabel,1);
  167. statusBar()->setSizeGripEnabled(FALSE);
  168. connect(statusBar(),SIGNAL(messageChanged(const QString &)),this,SLOT(statusBar_messageChanged(const QString &)));
  169. connect(KDirWatch::self(),SIGNAL(created(const QString &)),this,SLOT(KDirWatch_dirty(const QString &)));
  170. connect(KDirWatch::self(),SIGNAL(dirty(const QString &)),this,SLOT(KDirWatch_dirty(const QString &)));
  171. KDirWatch::self()->startScan();
  172. connect(clipboard,SIGNAL(dataChanged()),this,SLOT(clipboard_dataChanged()));
  173. widgetStack->addWidget(CURRENT_VIEW);
  174. CURRENT_VIEW->show();
  175. widgetStack->raiseWidget(CURRENT_VIEW);
  176. editUndoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->undoCount()));
  177. editRedoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->redoCount()));
  178. editClearAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  179. editCutAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  180. editCopyAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  181. editPasteAction->setEnabled(!clipboard->text().isNull());
  182. THIS->accel=new QAccel(this);
  183. THIS->accel->insertItem(ALT+Key_Backspace,0);
  184. THIS->accel->insertItem(SHIFT+ALT+Key_Backspace,1);
  185. THIS->accel->insertItem(SHIFT+Key_Delete,2);
  186. THIS->accel->insertItem(CTRL+Key_Insert,3);
  187. THIS->accel->insertItem(SHIFT+Key_Insert,4);
  188. THIS->accel->insertItem(Key_F1,5);
  189. THIS->accel->insertItem(Key_Enter,6);
  190. THIS->accel->insertItem(Key_Return,7);
  191. THIS->accel->setItemEnabled(0,!!(CURRENT_VIEW->getDoc()->undoCount()));
  192. THIS->accel->setItemEnabled(1,!!(CURRENT_VIEW->getDoc()->redoCount()));
  193. THIS->accel->setItemEnabled(2,CURRENT_VIEW->getDoc()->hasSelection());
  194. THIS->accel->setItemEnabled(3,CURRENT_VIEW->getDoc()->hasSelection());
  195. THIS->accel->setItemEnabled(4,!clipboard->text().isNull());
  196. THIS->accel->setItemEnabled(5,TRUE);
  197. THIS->accel->setItemEnabled(6,TRUE);
  198. THIS->accel->setItemEnabled(7,TRUE);
  199. connect(THIS->accel,SIGNAL(activated(int)),this,SLOT(accel_activated(int)));
  200. startTimer(100);
  201. THIS->kfinddialog = static_cast<KFindDialog *>(NULL);
  202. THIS->kreplace = static_cast<KReplaceWithSelectionS *>(NULL);
  203. if (preferences.useSystemIcons) {
  204. setUsesBigPixmaps(TRUE);
  205. fileSaveAction->setIconSet(LOAD_ICON("filesave"));
  206. fileAddToProjectAction->setIconSet(LOAD_ICON("edit_add"));
  207. fileCompileAction->setIconSet(LOAD_ICON("compfile"));
  208. filePrintAction->setIconSet(LOAD_ICON("fileprint"));
  209. filePrintQuicklyAction->setIconSet(LOAD_ICON("fileprint"));
  210. editClearAction->setIconSet(LOAD_ICON("editdelete"));
  211. editCutAction->setIconSet(LOAD_ICON("editcut"));
  212. editCopyAction->setIconSet(LOAD_ICON("editcopy"));
  213. editPasteAction->setIconSet(LOAD_ICON("editpaste"));
  214. findFindAction->setIconSet(LOAD_ICON("filefind"));
  215. if (KGlobal::iconLoader()->iconPath("stock-find-and-replace",KIcon::Small,TRUE).isEmpty()) {
  216. QIconSet fileReplaceIconSet(QPixmap::fromMimeSource("filereplace.png"));
  217. int smallSize=IconSize(KIcon::Small);
  218. fileReplaceIconSet.setIconSize(QIconSet::Small,QSize(smallSize,smallSize));
  219. int largeSize=IconSize(KIcon::MainToolbar);
  220. fileReplaceIconSet.setIconSize(QIconSet::Large,QSize(largeSize,largeSize));
  221. findReplaceAction->setIconSet(fileReplaceIconSet);
  222. } else
  223. findReplaceAction->setIconSet(LOAD_ICON("stock-find-and-replace"));
  224. editUndoAction->setIconSet(LOAD_ICON("undo"));
  225. editRedoAction->setIconSet(LOAD_ICON("redo"));
  226. findFunctionsAction->setIconSet(LOAD_ICON("view_tree"));
  227. editIncreaseIndentAction->setIconSet(LOAD_ICON("indent"));
  228. editDecreaseIndentAction->setIconSet(LOAD_ICON("unindent"));
  229. // stop compilation: "stop"
  230. // force-quit compiler: "button_cancel"
  231. }
  232. }
  233. void SourceFileWindow::destroy()
  234. {
  235. if (THIS->kreplace) delete THIS->kreplace;
  236. if (THIS->kfinddialog) delete THIS->kfinddialog;
  237. delete THIS->accel;
  238. delete THIS->te_popup;
  239. delete THIS->rowStatusLabel;
  240. delete THIS->colStatusLabel;
  241. delete THIS->charsStatusLabel;
  242. delete THIS->rightStatusLabel;
  243. }
  244. void SourceFileWindow::te_popup_aboutToShow()
  245. {
  246. THIS->te_popup->setItemEnabled(0,findOpenFileAtCursorAction->isEnabled());
  247. THIS->te_popup->setItemEnabled(1,findFindSymbolDeclarationAction->isEnabled());
  248. THIS->te_popup->setItemEnabled(2,editUndoAction->isEnabled());
  249. THIS->te_popup->setItemEnabled(3,editRedoAction->isEnabled());
  250. THIS->te_popup->setItemEnabled(4,editClearAction->isEnabled());
  251. THIS->te_popup->setItemEnabled(5,editCutAction->isEnabled());
  252. THIS->te_popup->setItemEnabled(6,editCopyAction->isEnabled());
  253. THIS->te_popup->setItemEnabled(7,editPasteAction->isEnabled());
  254. THIS->te_popup->setItemEnabled(8,editSelectAllAction->isEnabled());
  255. THIS->te_popup->setItemEnabled(9,editIncreaseIndentAction->isEnabled());
  256. THIS->te_popup->setItemEnabled(10,editDecreaseIndentAction->isEnabled());
  257. }
  258. void SourceFileWindow::te_popup_activated(int index)
  259. {
  260. switch (index) {
  261. case 0: findOpenFileAtCursor(); break;
  262. case 1: findFindSymbolDeclaration(); break;
  263. case 2: editUndo(); break;
  264. case 3: editRedo(); break;
  265. case 4: editClear(); break;
  266. case 5: editCut(); break;
  267. case 6: editCopy(); break;
  268. case 7: editPaste(); break;
  269. case 8: editSelectAll(); break;
  270. case 9: editIncreaseIndent(); break;
  271. case 10: editDecreaseIndent(); break;
  272. default: break;
  273. }
  274. }
  275. void SourceFileWindow::accel_activated(int index)
  276. {
  277. if (CURRENT_VIEW && CURRENT_VIEW->hasFocus()) {
  278. switch (index) {
  279. case 0: editUndo(); break;
  280. case 1: editRedo(); break;
  281. case 2: editCut(); break;
  282. case 3: editCopy(); break;
  283. case 4: editPaste(); break;
  284. case 5: // F1 context help
  285. {
  286. QString wordUnderCursor=CURRENT_VIEW->currentWord();
  287. // always open at least the index
  288. force_qt_assistant_page(1);
  289. assistant->openAssistant();
  290. if (wordUnderCursor.isEmpty()) return;
  291. QString docFile=lookup_doc_keyword(wordUnderCursor);
  292. if (docFile.isEmpty()) return;
  293. // wait for Qt Assistant to actually open
  294. while (!assistant->isOpen())
  295. QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,1000);
  296. assistant->showPage(QString(tigcc_base)+QString("/doc/html/")+docFile);
  297. break;
  298. }
  299. case 6:
  300. case 7:
  301. CURRENT_VIEW->keyReturn();
  302. current_view_newLineHook();
  303. break;
  304. default: break;
  305. }
  306. } else if (index == 6 || index == 7) {
  307. QKeyEvent *keyEvent=new QKeyEvent(QEvent::KeyPress,Key_Return,'\n',0,"\n");
  308. QApplication::postEvent(focusWidget(),keyEvent);
  309. }
  310. }
  311. void *SourceFileWindow::createView(const QString &fileName, const QString &fileText, const QString &hlModeName, unsigned tabWidth)
  312. {
  313. // Create Document object.
  314. KParts::Factory *factory = (KParts::Factory *)
  315. KLibLoader::self()->factory ("libkatepart");
  316. if (!factory) qFatal("Failed to load KatePart");
  317. Kate::Document *doc = (Kate::Document *)
  318. factory->createPart( 0, "", this, "", "Kate::Document" );
  319. // Set the file name for printing.
  320. doc->setModified(FALSE);
  321. if (doc->openStream("text/plain",fileName))
  322. doc->closeStream();
  323. // Create View object.
  324. Kate::View *newView = (Kate::View *) doc->createView( widgetStack, 0L );
  325. newView->hide();
  326. newView->setSizePolicy(QSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored,0,0));
  327. // Set highlighting mode.
  328. uint cnt=newView->getDoc()->hlModeCount(), i;
  329. for (i=0; i<cnt; i++) {
  330. if (!newView->getDoc()->hlModeName(i).compare(hlModeName)) break;
  331. }
  332. if (i==cnt) i=0;
  333. newView->getDoc()->setHlMode(i);
  334. // Set options.
  335. newView->setDynWordWrap(FALSE);
  336. if (preferences.removeTrailingSpaces)
  337. newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  338. else
  339. newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  340. newView->setTabWidth(tabWidth);
  341. connect(newView,SIGNAL(cursorPositionChanged()),this,SLOT(current_view_cursorPositionChanged()));
  342. connect(newView->getDoc(),SIGNAL(textChanged()),this,SLOT(current_view_textChanged()));
  343. connect(newView->getDoc(),SIGNAL(undoChanged()),this,SLOT(current_view_undoChanged()));
  344. connect(newView->getDoc(),SIGNAL(selectionChanged()),this,SLOT(current_view_selectionChanged()));
  345. connect(newView->getDoc(),SIGNAL(charactersInteractivelyInserted(int,int,const QString&)),this,SLOT(current_view_charactersInteractivelyInserted(int,int,const QString&)));
  346. newView->installPopup(THIS->te_popup);
  347. // Set text.
  348. newView->getDoc()->setText(fileText);
  349. newView->getDoc()->setModified(FALSE);
  350. newView->getDoc()->clearUndo();
  351. newView->getDoc()->clearRedo();
  352. newView->setCursorPositionReal(0,0);
  353. return newView;
  354. }
  355. //returns 1 if the current project data should not be cleared out, 0 if it can be cleared out.
  356. int SourceFileWindow::savePrompt(void)
  357. {
  358. int result;
  359. if (!CURRENT_VIEW) return 0;
  360. while (CURRENT_VIEW->getDoc()->isModified()) { // "while" in case saving fails!
  361. result=KMessageBox::questionYesNoCancel(this,QString("The file \'%1\' has been modified. Do you want to save the changes?").arg(THIS->fileName),QString::null,KStdGuiItem::save(),KStdGuiItem::discard());
  362. if (result==KMessageBox::Yes)
  363. fileSave_save();
  364. else if (result==KMessageBox::No)
  365. CURRENT_VIEW->getDoc()->setModified(FALSE);
  366. else
  367. return 1;
  368. }
  369. return 0;
  370. }
  371. void SourceFileWindow::removeTrailingSpacesFromView(void *view)
  372. {
  373. if (!preferences.removeTrailingSpaces) return;
  374. Kate::View *kateView=reinterpret_cast<Kate::View *>(view);
  375. Kate::Document *doc=kateView->getDoc();
  376. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  377. editExt->editBegin();
  378. unsigned numLines=doc->numLines();
  379. for (unsigned i=0; i<numLines; i++) {
  380. QString line=doc->textLine(i);
  381. int whitespace=line.find(QRegExp("\\s+$"));
  382. if (whitespace>=0) doc->removeText(i,whitespace,i,line.length());
  383. }
  384. editExt->editEnd();
  385. }
  386. void SourceFileWindow::fileSave_save()
  387. {
  388. KDirWatch::self()->removeFile(THIS->fileName);
  389. if (saveFileText(THIS->fileName,CURRENT_VIEW->getDoc()->text())) {
  390. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(THIS->fileName));
  391. KDirWatch::self()->addFile(THIS->fileName);
  392. } else {
  393. KDirWatch::self()->addFile(THIS->fileName);
  394. removeTrailingSpacesFromView(CURRENT_VIEW);
  395. CURRENT_VIEW->getDoc()->setModified(FALSE);
  396. }
  397. }
  398. void SourceFileWindow::fileSave_saveAs()
  399. {
  400. QString saveFileName=MainForm::SGetFileName(KFileDialog::Saving,
  401. TIGCC_H_Filter TIGCC_C_Filter TIGCC_S_Filter TIGCC_ASM_Filter TIGCC_QLL_Filter
  402. TIGCC_TXT_Filter TIGCCAllFilter,"Save Source File",this);
  403. if (saveFileName.isEmpty())
  404. return;
  405. if (THIS->fileName[0]=='/')
  406. KDirWatch::self()->removeFile(THIS->fileName);
  407. if (saveFileText(saveFileName,CURRENT_VIEW->getDoc()->text())) {
  408. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(saveFileName));
  409. if (THIS->fileName[0]=='/')
  410. KDirWatch::self()->addFile(THIS->fileName);
  411. } else {
  412. if (saveFileName.compare(THIS->fileName)) {
  413. // Update the file name for printing.
  414. unsigned int line,col,hlMode;
  415. QString fileText=CURRENT_VIEW->getDoc()->text();
  416. hlMode=CURRENT_VIEW->getDoc()->hlMode();
  417. CURRENT_VIEW->cursorPositionReal(&line,&col);
  418. CURRENT_VIEW->getDoc()->setModified(FALSE);
  419. if (CURRENT_VIEW->getDoc()->openStream("text/plain",saveFileName))
  420. CURRENT_VIEW->getDoc()->closeStream();
  421. CURRENT_VIEW->getDoc()->setText(fileText);
  422. CURRENT_VIEW->getDoc()->clearUndo();
  423. CURRENT_VIEW->getDoc()->clearRedo();
  424. CURRENT_VIEW->getDoc()->setHlMode(hlMode);
  425. CURRENT_VIEW->setCursorPositionReal(line,col);
  426. }
  427. THIS->fileName=saveFileName;
  428. KDirWatch::self()->addFile(saveFileName);
  429. removeTrailingSpacesFromView(CURRENT_VIEW);
  430. CURRENT_VIEW->getDoc()->setModified(FALSE);
  431. updateRightStatusLabel();
  432. }
  433. }
  434. void SourceFileWindow::fileSave()
  435. {
  436. fileSave_save();
  437. }
  438. void SourceFileWindow::fileSaveAs()
  439. {
  440. fileSave_saveAs();
  441. }
  442. void SourceFileWindow::fileAddToProject()
  443. {
  444. }
  445. void SourceFileWindow::fileCompile()
  446. {
  447. }
  448. void SourceFileWindow::filePrint()
  449. {
  450. if (CURRENT_VIEW) CURRENT_VIEW->getDoc()->printDialog();
  451. }
  452. void SourceFileWindow::filePrintQuickly()
  453. {
  454. if (CURRENT_VIEW) CURRENT_VIEW->getDoc()->print();
  455. }
  456. void SourceFileWindow::applyPreferences()
  457. {
  458. if (showPreferencesDialog(this)==QDialog::Accepted) {
  459. // Apply the KatePart preferences and treeview icons.
  460. KParts::Factory *factory = (KParts::Factory *)
  461. KLibLoader::self()->factory ("libkatepart");
  462. if (!factory) qFatal("Failed to load KatePart");
  463. Kate::Document *doc = (Kate::Document *)
  464. factory->createPart( 0, "", this, "", "Kate::Document" );
  465. KTextEditor::ConfigInterfaceExtension *confInterfaceExt = KTextEditor::configInterfaceExtension(doc);
  466. unsigned numConfigPages=confInterfaceExt->configPages();
  467. for (unsigned i=0; i<numConfigPages; i++) {
  468. if (!confInterfaceExt->configPageName(i).compare("Fonts & Colors")) {
  469. KTextEditor::ConfigPage *configPage=confInterfaceExt->configPage(i);
  470. configPage->apply();
  471. delete configPage;
  472. break;
  473. }
  474. }
  475. delete doc;
  476. Kate::View *kateView=CURRENT_VIEW;
  477. if (kateView) {
  478. }
  479. if (CURRENT_VIEW) {
  480. Kate::View *currView=CURRENT_VIEW;
  481. QString fileText=currView->getDoc()->text();
  482. if (preferences.removeTrailingSpaces)
  483. currView->getDoc()->setConfigFlags(currView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  484. else
  485. currView->getDoc()->setConfigFlags(currView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  486. currView->setTabWidth(THIS->isASMFile?preferences.tabWidthAsm:
  487. THIS->isCFile?preferences.tabWidthC:8);
  488. // Force redrawing to get the tab width right, repaint() is ignored for some reason.
  489. currView->hide();
  490. currView->show();
  491. }
  492. // Apply the icon preferences.
  493. setUsesBigPixmaps(preferences.useSystemIcons);
  494. if (preferences.useSystemIcons) {
  495. fileSaveAction->setIconSet(LOAD_ICON("filesave"));
  496. fileAddToProjectAction->setIconSet(LOAD_ICON("edit_add"));
  497. fileCompileAction->setIconSet(LOAD_ICON("compfile"));
  498. filePrintAction->setIconSet(LOAD_ICON("fileprint"));
  499. filePrintQuicklyAction->setIconSet(LOAD_ICON("fileprint"));
  500. editClearAction->setIconSet(LOAD_ICON("editdelete"));
  501. editCutAction->setIconSet(LOAD_ICON("editcut"));
  502. editCopyAction->setIconSet(LOAD_ICON("editcopy"));
  503. editPasteAction->setIconSet(LOAD_ICON("editpaste"));
  504. findFindAction->setIconSet(LOAD_ICON("filefind"));
  505. if (KGlobal::iconLoader()->iconPath("stock-find-and-replace",KIcon::Small,TRUE).isEmpty()) {
  506. QIconSet fileReplaceIconSet(QPixmap::fromMimeSource("filereplace.png"));
  507. int smallSize=IconSize(KIcon::Small);
  508. fileReplaceIconSet.setIconSize(QIconSet::Small,QSize(smallSize,smallSize));
  509. int largeSize=IconSize(KIcon::MainToolbar);
  510. fileReplaceIconSet.setIconSize(QIconSet::Large,QSize(largeSize,largeSize));
  511. findReplaceAction->setIconSet(fileReplaceIconSet);
  512. } else
  513. findReplaceAction->setIconSet(LOAD_ICON("stock-find-and-replace"));
  514. editUndoAction->setIconSet(LOAD_ICON("undo"));
  515. editRedoAction->setIconSet(LOAD_ICON("redo"));
  516. findFunctionsAction->setIconSet(LOAD_ICON("view_tree"));
  517. editIncreaseIndentAction->setIconSet(LOAD_ICON("indent"));
  518. editDecreaseIndentAction->setIconSet(LOAD_ICON("unindent"));
  519. // stop compilation: "stop"
  520. // force-quit compiler: "button_cancel"
  521. } else {
  522. fileSaveAction->setIconSet(QIconSet(QPixmap::fromMimeSource("02")));
  523. fileAddToProjectAction->setIconSet(QIconSet(QPixmap::fromMimeSource("08")));
  524. fileCompileAction->setIconSet(QIconSet(QPixmap::fromMimeSource("09")));
  525. filePrintAction->setIconSet(QIconSet(QPixmap::fromMimeSource("03")));
  526. filePrintQuicklyAction->setIconSet(QIconSet(QPixmap::fromMimeSource("03")));
  527. editClearAction->setIconSet(QIconSet(QPixmap::fromMimeSource("04")));
  528. editCutAction->setIconSet(QIconSet(QPixmap::fromMimeSource("05")));
  529. editCopyAction->setIconSet(QIconSet(QPixmap::fromMimeSource("06")));
  530. editPasteAction->setIconSet(QIconSet(QPixmap::fromMimeSource("07")));
  531. findFindAction->setIconSet(QIconSet(QPixmap::fromMimeSource("13")));
  532. findReplaceAction->setIconSet(QIconSet(QPixmap::fromMimeSource("14")));
  533. editUndoAction->setIconSet(QIconSet(QPixmap::fromMimeSource("16")));
  534. editRedoAction->setIconSet(QIconSet(QPixmap::fromMimeSource("17")));
  535. findFunctionsAction->setIconSet(QIconSet(QPixmap::fromMimeSource("18")));
  536. editIncreaseIndentAction->setIconSet(QIconSet(QPixmap::fromMimeSource("19")));
  537. editDecreaseIndentAction->setIconSet(QIconSet(QPixmap::fromMimeSource("20")));
  538. // stop compilation: "21"
  539. // force-quit compiler: "22"
  540. }
  541. }
  542. }
  543. void SourceFileWindow::editUndo()
  544. {
  545. if (CURRENT_VIEW)
  546. CURRENT_VIEW->getDoc()->undo();
  547. }
  548. void SourceFileWindow::editRedo()
  549. {
  550. if (CURRENT_VIEW)
  551. CURRENT_VIEW->getDoc()->redo();
  552. }
  553. void SourceFileWindow::editClear()
  554. {
  555. if (CURRENT_VIEW)
  556. CURRENT_VIEW->getDoc()->removeSelectedText();
  557. }
  558. void SourceFileWindow::editCut()
  559. {
  560. if (CURRENT_VIEW)
  561. CURRENT_VIEW->cut();
  562. }
  563. void SourceFileWindow::editCopy()
  564. {
  565. if (CURRENT_VIEW)
  566. CURRENT_VIEW->copy();
  567. }
  568. void SourceFileWindow::editPaste()
  569. {
  570. if (CURRENT_VIEW)
  571. CURRENT_VIEW->paste();
  572. }
  573. void SourceFileWindow::editSelectAll()
  574. {
  575. if (CURRENT_VIEW)
  576. CURRENT_VIEW->getDoc()->selectAll();
  577. }
  578. void SourceFileWindow::editIncreaseIndent()
  579. {
  580. if (CURRENT_VIEW)
  581. CURRENT_VIEW->indent();
  582. }
  583. void SourceFileWindow::editDecreaseIndent()
  584. {
  585. if (CURRENT_VIEW)
  586. CURRENT_VIEW->unIndent();
  587. }
  588. void SourceFileWindow::findFind()
  589. {
  590. if (THIS->kfinddialog)
  591. KWin::activateWindow(THIS->kfinddialog->winId());
  592. else {
  593. // Never set hasSelection because finding in selection doesn't really make
  594. // sense with my non-modal find dialog setup.
  595. THIS->kfinddialog=new KFindDialog(false,this,0,KFindDialog::FromCursor);
  596. connect(THIS->kfinddialog, SIGNAL(okClicked()), this, SLOT(findFind_next()));
  597. connect(THIS->kfinddialog, SIGNAL(cancelClicked()), this, SLOT(findFind_stop()));
  598. THIS->kfinddialog->show();
  599. }
  600. }
  601. void SourceFileWindow::findFind_next()
  602. {
  603. // Use a local KFind object. The search will need to be restarted next time
  604. // this function is called because of the non-modality of the find dialog.
  605. KFind *kfind=new KFind(THIS->kfinddialog->pattern(),THIS->kfinddialog->options(),this,THIS->kfinddialog);
  606. // Initialize.
  607. bool findBackwards=!!(THIS->kfinddialog->options()&KFindDialog::FindBackwards);
  608. int findCurrentCol;
  609. kfind=new KFind(THIS->kfinddialog->pattern(),THIS->kfinddialog->options(),this,THIS->kfinddialog);
  610. kfind->closeFindNextDialog(); // don't use this, a non-modal KFindDialog is used instead
  611. connect(kfind,SIGNAL(highlight(const QString &,int,int)),
  612. this,SLOT(findFind_highlight(const QString &,int,int)));
  613. if (THIS->kfinddialog->options()&KFindDialog::FromCursor) {
  614. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  615. if (findBackwards) {
  616. THIS->findCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  617. findCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  618. if (findCurrentCol==-1) {
  619. if (!THIS->findCurrentLine) goto skip_data;
  620. THIS->findCurrentLine--;
  621. }
  622. } else {
  623. THIS->findCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  624. findCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  625. }
  626. } else {
  627. THIS->findCurrentLine=CURRENT_VIEW->cursorLine();
  628. findCurrentCol=CURRENT_VIEW->cursorColumnReal();
  629. }
  630. } else {
  631. THIS->findCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  632. findCurrentCol=-1;
  633. }
  634. kfind->setData(CURRENT_VIEW->getDoc()->textLine(THIS->findCurrentLine),findCurrentCol);
  635. skip_data:;
  636. // Now find the next occurrence.
  637. KFind::Result result;
  638. Kate::View *currView=CURRENT_VIEW;
  639. unsigned currNumLines=CURRENT_VIEW->getDoc()->numLines();
  640. do {
  641. if (kfind->needData()) {
  642. if (findBackwards?!THIS->findCurrentLine:(THIS->findCurrentLine>=currNumLines)) {
  643. // Try restarting the search.
  644. currNumLines=currView->getDoc()->numLines();
  645. THIS->findCurrentLine=findBackwards?currNumLines-1:0;
  646. do {
  647. if (kfind->needData()) {
  648. if (findBackwards?!THIS->findCurrentLine:(THIS->findCurrentLine>=currNumLines))
  649. goto not_found_current;
  650. if (findBackwards) THIS->findCurrentLine--; else THIS->findCurrentLine++;
  651. kfind->setData(currView->getDoc()->textLine(THIS->findCurrentLine));
  652. }
  653. result=kfind->find();
  654. } while (result==KFind::NoMatch);
  655. break;
  656. not_found_current:
  657. KMessageBox::error(this,QString("Text \'%1\' not found").arg(THIS->kfinddialog->pattern()));
  658. delete kfind;
  659. return;
  660. } else if (findBackwards) THIS->findCurrentLine--; else THIS->findCurrentLine++;
  661. kfind->setData(currView->getDoc()->textLine(THIS->findCurrentLine));
  662. }
  663. result=kfind->find();
  664. } while (result==KFind::NoMatch);
  665. delete kfind;
  666. }
  667. #define unused_text text __attribute__((unused))
  668. void SourceFileWindow::findFind_highlight(const QString &unused_text, int matchingindex, int matchedlength)
  669. {
  670. CURRENT_VIEW->setCursorPositionReal(THIS->findCurrentLine,matchingindex+matchedlength);
  671. CURRENT_VIEW->getDoc()->setSelection(THIS->findCurrentLine,matchingindex,
  672. THIS->findCurrentLine,matchingindex+matchedlength);
  673. }
  674. void SourceFileWindow::findFind_stop()
  675. {
  676. if (THIS->kfinddialog) THIS->kfinddialog->deleteLater();
  677. THIS->kfinddialog=static_cast<KFindDialog *>(NULL);
  678. }
  679. void SourceFileWindow::findReplace()
  680. {
  681. if (THIS->kreplace) {
  682. KDialogBase *replaceNextDialog=THIS->kreplace->replaceNextDialog();
  683. if (replaceNextDialog)
  684. KWin::activateWindow(replaceNextDialog->winId());
  685. return;
  686. }
  687. KReplaceDialog kreplacedialog(this,0,((CURRENT_VIEW&&CURRENT_VIEW->getDoc()->hasSelection()
  688. &&CURRENT_VIEW->getDoc()->selStartLine()!=CURRENT_VIEW->getDoc()->selEndLine())?
  689. KFindDialog::SelectedText:0)|KFindDialog::FromCursor,
  690. QStringList(),QStringList(),
  691. CURRENT_VIEW&&CURRENT_VIEW->getDoc()->hasSelection());
  692. if (kreplacedialog.exec()!=QDialog::Accepted)
  693. return;
  694. THIS->kreplace=new KReplaceWithSelectionS(kreplacedialog.pattern(),kreplacedialog.replacement(),
  695. kreplacedialog.options(),this);
  696. // Connect signals to code which handles highlighting of found text, and
  697. // on-the-fly replacement.
  698. connect(THIS->kreplace,SIGNAL(highlight(const QString &,int,int)),
  699. this,SLOT(findReplace_highlight(const QString &,int,int)));
  700. // Connect findNext signal - called when pressing the button in the dialog.
  701. connect(THIS->kreplace,SIGNAL(findNext()),this,SLOT(findReplace_next()));
  702. // Connect replace signal - called when doing a replacement.
  703. connect(THIS->kreplace,SIGNAL(replace(const QString &,int,int,int)),
  704. this,SLOT(findReplace_replace(const QString &,int,int,int)));
  705. // Connect dialogClosed signal - called when closing the Replace Next dialog.
  706. connect(THIS->kreplace,SIGNAL(dialogClosed()),this,SLOT(findReplace_stop()));
  707. // Initialize.
  708. bool findBackwards=!!(THIS->kreplace->options()&KFindDialog::FindBackwards);
  709. int replaceCurrentCol;
  710. if (CURRENT_VIEW) {
  711. if (THIS->kreplace->options()&KFindDialog::SelectedText) {
  712. THIS->kreplace->setSelection(CURRENT_VIEW->getDoc()->selStartLine(),
  713. CURRENT_VIEW->getDoc()->selStartCol(),
  714. CURRENT_VIEW->getDoc()->selEndLine(),
  715. CURRENT_VIEW->getDoc()->selEndCol());
  716. if (findBackwards) {
  717. THIS->kreplace->replaceCurrentLine=THIS->kreplace->selEndLine();
  718. replaceCurrentCol=THIS->kreplace->selEndCol();
  719. } else {
  720. THIS->kreplace->replaceCurrentLine=THIS->kreplace->selStartLine();
  721. replaceCurrentCol=THIS->kreplace->selStartCol();
  722. }
  723. THIS->kreplace->setOptions(THIS->kreplace->options()&~KFindDialog::FromCursor);
  724. } else if (THIS->kreplace->options()&KFindDialog::FromCursor) {
  725. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  726. if (findBackwards) {
  727. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  728. replaceCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  729. if (replaceCurrentCol==-1) {
  730. if (!THIS->kreplace->replaceCurrentLine) goto skip_data;
  731. THIS->kreplace->replaceCurrentLine--;
  732. }
  733. } else {
  734. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  735. replaceCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  736. }
  737. } else {
  738. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->cursorLine();
  739. replaceCurrentCol=CURRENT_VIEW->cursorColumnReal();
  740. // Don't prompt for restarting if we actually searched the entire document.
  741. if (findBackwards?(THIS->kreplace->replaceCurrentLine==(CURRENT_VIEW->getDoc()->numLines()-1)
  742. && replaceCurrentCol==(CURRENT_VIEW->getDoc()->lineLength(THIS->kreplace->replaceCurrentLine)))
  743. :(!THIS->kreplace->replaceCurrentLine&&!replaceCurrentCol))
  744. THIS->kreplace->setOptions(THIS->kreplace->options()&~KFindDialog::FromCursor);
  745. }
  746. } else {
  747. THIS->kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  748. replaceCurrentCol=-1;
  749. }
  750. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine),replaceCurrentCol);
  751. }
  752. skip_data:
  753. // Now find the next occurrence.
  754. findReplace_next(TRUE);
  755. }
  756. void SourceFileWindow::findReplace_next()
  757. {
  758. findReplace_next(FALSE);
  759. }
  760. void SourceFileWindow::findReplace_next(bool firstTime)
  761. {
  762. bool findBackwards=!!(THIS->kreplace->options()&KFindDialog::FindBackwards);
  763. // Reinitialize.
  764. if (!firstTime) {
  765. int replaceCurrentCol;
  766. // Non-first-time always continues from cursor.
  767. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  768. if (findBackwards) {
  769. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  770. replaceCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  771. if (replaceCurrentCol==-1) {
  772. if (!THIS->kreplace->replaceCurrentLine) goto skip_data;
  773. THIS->kreplace->replaceCurrentLine--;
  774. }
  775. } else {
  776. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  777. replaceCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  778. }
  779. } else {
  780. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->cursorLine();
  781. replaceCurrentCol=CURRENT_VIEW->cursorColumnReal();
  782. }
  783. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine),replaceCurrentCol);
  784. }
  785. skip_data:;
  786. // Now find the next occurrence.
  787. KFind::Result result;
  788. Kate::View *currView=CURRENT_VIEW;
  789. unsigned currNumLines=0;
  790. if (CURRENT_VIEW) currNumLines=CURRENT_VIEW->getDoc()->numLines();
  791. do {
  792. if (THIS->kreplace->needData()) {
  793. if (THIS->kreplace->haveSelection()
  794. ?(findBackwards?(THIS->kreplace->replaceCurrentLine<=THIS->kreplace->selStartLine())
  795. :(THIS->kreplace->replaceCurrentLine>=THIS->kreplace->selEndLine()))
  796. :(findBackwards?!THIS->kreplace->replaceCurrentLine:(THIS->kreplace->replaceCurrentLine>=currNumLines))) {
  797. if (THIS->kreplace->shouldRestart()) {
  798. // Drop "From cursor" and "Selected text" options.
  799. THIS->kreplace->setOptions(THIS->kreplace->options()&~(KFindDialog::FromCursor
  800. |KFindDialog::SelectedText));
  801. THIS->kreplace->invalidateSelection();
  802. // Reinitialize.
  803. THIS->kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  804. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine));
  805. // Start again as if it was the first time.
  806. findReplace_next(TRUE);
  807. return;
  808. } else {
  809. findReplace_stop();
  810. return;
  811. }
  812. } else if (findBackwards) THIS->kreplace->replaceCurrentLine--; else THIS->kreplace->replaceCurrentLine++;
  813. THIS->kreplace->setData(currView->getDoc()->textLine(THIS->kreplace->replaceCurrentLine));
  814. }
  815. result=THIS->kreplace->replace();
  816. } while (result==KFind::NoMatch);
  817. }
  818. void SourceFileWindow::findReplace_highlight(const QString &unused_text, int matchingindex, int matchedlength)
  819. {
  820. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,matchingindex+matchedlength);
  821. CURRENT_VIEW->getDoc()->setSelection(THIS->kreplace->replaceCurrentLine,matchingindex,
  822. THIS->kreplace->replaceCurrentLine,matchingindex+matchedlength);
  823. }
  824. void SourceFileWindow::findReplace_replace(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
  825. {
  826. bool update=!!(THIS->kreplace->options()&KReplaceDialog::PromptOnReplace);
  827. bool haveSelection=THIS->kreplace->haveSelection();
  828. // The initializations are redundant, but g++ doesn't understand this, and the
  829. // self-initialization trick doesn't work either (-Wno-init-self is ignored).
  830. unsigned selStartLine=0, selStartCol=0, selEndLine=0, selEndCol=0;
  831. if (haveSelection) {
  832. selStartLine=THIS->kreplace->selStartLine();
  833. selStartCol=THIS->kreplace->selStartCol();
  834. selEndLine=THIS->kreplace->selEndLine();
  835. selEndCol=THIS->kreplace->selEndCol();
  836. }
  837. KTextEditor::EditInterfaceExt *editinterfaceext=KTextEditor::editInterfaceExt(CURRENT_VIEW->getDoc());
  838. editinterfaceext->editBegin();
  839. CURRENT_VIEW->getDoc()->insertText(THIS->kreplace->replaceCurrentLine,replacementIndex,
  840. text.mid(replacementIndex,replacedLength));
  841. // We can't put the cursor back now because this breaks editBegin/editEnd.
  842. bool updateCursor=(CURRENT_VIEW->cursorLine()==THIS->kreplace->replaceCurrentLine
  843. && CURRENT_VIEW->cursorColumnReal()==(unsigned)replacementIndex+(unsigned)replacedLength);
  844. CURRENT_VIEW->getDoc()->removeText(THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength,
  845. THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength+matchedLength);
  846. editinterfaceext->editEnd();
  847. if (updateCursor)
  848. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,replacementIndex);
  849. if (update) {
  850. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength);
  851. CURRENT_VIEW->getDoc()->setSelection(THIS->kreplace->replaceCurrentLine,replacementIndex,
  852. THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength);
  853. CURRENT_VIEW->repaint();
  854. }
  855. if (haveSelection) {
  856. // Restore selection, updating coordinates if necessary.
  857. THIS->kreplace->setSelection(selStartLine,selStartCol,selEndLine,
  858. (THIS->kreplace->replaceCurrentLine==selEndLine)
  859. ?(selEndCol+replacedLength-matchedLength)
  860. :selEndCol);
  861. }
  862. }
  863. void SourceFileWindow::findReplace_stop()
  864. {
  865. if (THIS->kreplace) THIS->kreplace->deleteLater();
  866. THIS->kreplace=static_cast<KReplaceWithSelectionS *>(NULL);
  867. }
  868. void SourceFileWindow::findFunctions()
  869. {
  870. }
  871. void SourceFileWindow::findOpenFileAtCursor()
  872. {
  873. }
  874. void SourceFileWindow::findFindSymbolDeclaration()
  875. {
  876. }
  877. void SourceFileWindow::updateSizes()
  878. {
  879. int rightStatusSize=size().width();
  880. THIS->rowStatusLabel->setMaximumWidth(30);
  881. THIS->colStatusLabel->setMaximumWidth(30);
  882. THIS->charsStatusLabel->setMaximumWidth(100);
  883. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  884. }
  885. void SourceFileWindow::resizeEvent(QResizeEvent *event)
  886. {
  887. QMainWindow::resizeEvent(event);
  888. if (event->size()==event->oldSize()) return;
  889. updateSizes();
  890. }
  891. void SourceFileWindow::timerEvent(QTimerEvent *event)
  892. {
  893. static int lastSplitterPos=-1;
  894. QMainWindow::timerEvent(event);
  895. if (lastSplitterPos==splitter->sizes().first()) return;
  896. lastSplitterPos=splitter->sizes().first();
  897. updateSizes();
  898. }
  899. void SourceFileWindow::statusBar_messageChanged(const QString & message)
  900. {
  901. if (message.isNull())
  902. // Make sure no labels which should be hidden are shown.
  903. updateRightStatusLabel();
  904. }
  905. void SourceFileWindow::updateRightStatusLabel()
  906. {
  907. int rightStatusSize=size().width();
  908. unsigned int line, col;
  909. CURRENT_VIEW->cursorPositionReal(&line,&col);
  910. THIS->rowStatusLabel->setMaximumWidth(30);
  911. THIS->rowStatusLabel->setText(QString("%1").arg(line+1));
  912. THIS->colStatusLabel->setMaximumWidth(30);
  913. THIS->colStatusLabel->setText(QString("%1").arg(col+1));
  914. THIS->charsStatusLabel->setMaximumWidth(100);
  915. THIS->charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()));
  916. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  917. THIS->rightStatusLabel->setText(THIS->fileName);
  918. }
  919. void SourceFileWindow::current_view_cursorPositionChanged()
  920. {
  921. if (CURRENT_VIEW) {
  922. unsigned int line, col;
  923. CURRENT_VIEW->cursorPositionReal(&line,&col);
  924. THIS->rowStatusLabel->setText(QString("%1").arg(line+1));
  925. THIS->colStatusLabel->setText(QString("%1").arg(col+1));
  926. }
  927. }
  928. void SourceFileWindow::current_view_textChanged()
  929. {
  930. if (CURRENT_VIEW)
  931. THIS->charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()));
  932. if (THIS->kreplace) THIS->kreplace->invalidateSelection();
  933. }
  934. void SourceFileWindow::current_view_undoChanged()
  935. {
  936. if (CURRENT_VIEW) {
  937. editUndoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->undoCount()));
  938. editRedoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->redoCount()));
  939. THIS->accel->setItemEnabled(0,!!(CURRENT_VIEW->getDoc()->undoCount()));
  940. THIS->accel->setItemEnabled(1,!!(CURRENT_VIEW->getDoc()->redoCount()));
  941. }
  942. }
  943. void SourceFileWindow::current_view_selectionChanged()
  944. {
  945. if (CURRENT_VIEW) {
  946. editClearAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  947. editCutAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  948. editCopyAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  949. THIS->accel->setItemEnabled(2,CURRENT_VIEW->getDoc()->hasSelection());
  950. THIS->accel->setItemEnabled(3,CURRENT_VIEW->getDoc()->hasSelection());
  951. }
  952. }
  953. void SourceFileWindow::current_view_charactersInteractivelyInserted(int line, int col, const QString &characters)
  954. {
  955. if (CURRENT_VIEW && preferences.autoBlocks && !characters.compare("{")
  956. && col==CURRENT_VIEW->getDoc()->lineLength(line)-1) {
  957. Kate::Document *doc=CURRENT_VIEW->getDoc();
  958. QString fileText=doc->text();
  959. // Only for C files.
  960. if (THIS->isCFile) {
  961. QString indent=doc->textLine(line);
  962. // Only if the line was all whitespace, otherwise wait for Enter to be
  963. // pressed (prevents annoying the user while typing a string or something).
  964. if (indent.contains(QRegExp("^\\s*\\{$"))) {
  965. indent=indent.remove('{');
  966. QString cursorLine=indent+"\t";
  967. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  968. editExt->editBegin();
  969. doc->insertLine(line+1,cursorLine);
  970. doc->insertLine(line+2,indent+"}");
  971. editExt->editEnd();
  972. CURRENT_VIEW->setCursorPositionReal(line+1,cursorLine.length());
  973. }
  974. }
  975. }
  976. }
  977. void SourceFileWindow::current_view_newLineHook()
  978. {
  979. unsigned line,col;
  980. CURRENT_VIEW->cursorPositionReal(&line,&col);
  981. Kate::Document *doc=CURRENT_VIEW->getDoc();
  982. if (preferences.autoBlocks && line && doc->textLine(line-1).endsWith("{")) {
  983. QString fileText=doc->text();
  984. // Only for C files.
  985. if (THIS->isCFile) {
  986. QString indent=doc->textLine(line-1);
  987. // Remove everything starting from the first non-whitespace character.
  988. indent=indent.remove(QRegExp("(?!\\s).*$"));
  989. QString cursorLine=indent+"\t";
  990. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  991. editExt->editBegin();
  992. doc->insertLine(line,cursorLine);
  993. doc->insertText(line+1,0,indent+"}");
  994. editExt->editEnd();
  995. CURRENT_VIEW->setCursorPositionReal(line,cursorLine.length());
  996. }
  997. }
  998. }
  999. void SourceFileWindow::clipboard_dataChanged()
  1000. {
  1001. if (CURRENT_VIEW) {
  1002. editPasteAction->setEnabled(!clipboard->text().isNull());
  1003. THIS->accel->setItemEnabled(4,!clipboard->text().isNull());
  1004. }
  1005. }
  1006. void SourceFileWindow::closeEvent(QCloseEvent *e)
  1007. {
  1008. if (savePrompt())
  1009. e->ignore();
  1010. else
  1011. e->accept();
  1012. }
  1013. void SourceFileWindow::KDirWatch_dirty(const QString &fileName)
  1014. {
  1015. if (KMessageBox::questionYesNo(this,
  1016. QString("The file \'%1\' has been changed by another program. "
  1017. "Do you want to reload it?").arg(fileName),"File Changed")
  1018. ==KMessageBox::Yes) {
  1019. QString fileText=loadFileText(fileName);
  1020. if (fileText.isNull()) {
  1021. KMessageBox::error(this,QString("Can't open \'%1\'").arg(fileName));
  1022. return;
  1023. }
  1024. CURRENT_VIEW->getDoc()->setText(fileText);
  1025. CURRENT_VIEW->getDoc()->setModified(FALSE);
  1026. CURRENT_VIEW->getDoc()->clearUndo();
  1027. CURRENT_VIEW->getDoc()->clearRedo();
  1028. }
  1029. return;
  1030. }