srcfilewin.ui.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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 <qdir.h>
  33. #include <qclipboard.h>
  34. #include <qaccel.h>
  35. #include <qeventloop.h>
  36. #include <qlayout.h>
  37. #include <qtoolbutton.h>
  38. #include <qlistbox.h>
  39. #include <kparts/factory.h>
  40. #include <klibloader.h>
  41. #include <kate/document.h>
  42. #include <kate/view.h>
  43. #include <kconfig.h>
  44. #include <ktexteditor/editinterfaceext.h>
  45. #include <ktexteditor/configinterfaceextension.h>
  46. #include <kfiledialog.h>
  47. #include <kurl.h>
  48. #include <kmessagebox.h>
  49. #include <kdirwatch.h>
  50. #include <kfinddialog.h>
  51. #include <kfind.h>
  52. #include <kreplacedialog.h>
  53. #include <kreplace.h>
  54. #include <kwin.h>
  55. #include <kglobal.h>
  56. #include <kicontheme.h>
  57. #include <kiconloader.h>
  58. #include <kpushbutton.h>
  59. #include <cstdio>
  60. #include <cstdlib>
  61. #include "ktigcc.h"
  62. #include "mainform.h"
  63. #include "tpr.h"
  64. #include "preferences.h"
  65. #include "projectoptions.h"
  66. #include "srcfile.h"
  67. #include "functions.h"
  68. #include "completion.h"
  69. #include "assistant.h"
  70. using std::puts;
  71. using std::exit;
  72. #define TIGCC_TPR_Filter "*.tpr|TIGCC Projects (*.tpr)\n"
  73. #define TIGCC_H_Filter "*.h|Header Files (*.h)\n"
  74. #define TIGCC_C_Filter "*.c|C Files (*.c)\n"
  75. #define TIGCC_S_Filter "*.s|GNU Assembly Files (*.s)\n"
  76. #define TIGCC_ASM_Filter "*.asm|A68k Assembly Files (*.asm)\n"
  77. #define TIGCC_QLL_Filter "*.qll|Quill Files (*.qll)\n"
  78. #define TIGCC_O_Filter "*.o|Object Files (*.o)\n"
  79. #define TIGCC_A_Filter "*.a|Archive Files (*.a)\n"
  80. #define TIGCC_TXT_Filter "*.txt|Text Files (*.txt)\n"
  81. #define TIGCCAllFilter "*|All Files (*)"
  82. enum {TIGCCOpenProjectFileFilter,TIGCCAddFilesFilter};
  83. #define THIS (static_cast<SourceFile *>(this))
  84. #define CURRENT_VIEW (THIS->kateView)
  85. #define HL_MODE ((THIS->hlEnabled && *(THIS->hlEnabled))?THIS->hlMode:"None")
  86. #define LOAD_ICON(name) (QIconSet(KGlobal::iconLoader()->loadIcon((name),KIcon::Small),KGlobal::iconLoader()->loadIcon((name),KIcon::MainToolbar)))
  87. #define SYSICON(sysname,name) (preferences.useSystemIcons?KGlobal::iconLoader()->loadIcon((sysname),KIcon::Small):QPixmap::fromMimeSource((name)))
  88. #define SET_TEXT_SAFE(doc,text) do { \
  89. disableViewEvents=TRUE; \
  90. (doc)->setText((text)); \
  91. disableViewEvents=FALSE; \
  92. } while(0)
  93. // For some reason, this flag is not in the public ConfigFlags enum.
  94. #define CF_REMOVE_TRAILING_DYN 0x4000000
  95. class KReplaceWithSelectionS : public KReplace {
  96. public:
  97. KReplaceWithSelectionS(const QString &pattern, const QString &replacement,
  98. long options, QWidget *parent=0) :
  99. KReplace(pattern,replacement,options,parent), m_haveSelection(FALSE) {}
  100. void setSelection(unsigned selStartLine, unsigned selStartCol,
  101. unsigned selEndLine, unsigned selEndCol)
  102. {
  103. m_haveSelection=TRUE;
  104. m_selStartLine=selStartLine;
  105. m_selStartCol=selStartCol;
  106. m_selEndLine=selEndLine;
  107. m_selEndCol=selEndCol;
  108. }
  109. unsigned replaceCurrentLine;
  110. void invalidateSelection() {m_haveSelection=FALSE;}
  111. bool haveSelection() {return m_haveSelection;}
  112. unsigned selStartLine() {return m_selStartLine;}
  113. unsigned selStartCol() {return m_selStartCol;}
  114. unsigned selEndLine() {return m_selEndLine;}
  115. unsigned selEndCol() {return m_selEndCol;}
  116. // Override to ask for restarting when replacing in a selection.
  117. bool shouldRestart(bool forceAsking=FALSE, bool showNumMatches=TRUE)
  118. {
  119. return KReplace::shouldRestart(forceAsking||m_haveSelection,showNumMatches);
  120. }
  121. protected:
  122. virtual bool validateMatch(const QString &text, int index, int matchedlength)
  123. {
  124. if (!KReplace::validateMatch(text,index,matchedlength)) return FALSE;
  125. if (!m_haveSelection) return TRUE;
  126. if (replaceCurrentLine==m_selStartLine && replaceCurrentLine==m_selEndLine)
  127. return ((unsigned)index>=m_selStartCol)&&((unsigned)index+(unsigned)matchedlength<=m_selEndCol);
  128. else if (replaceCurrentLine==m_selStartLine)
  129. return ((unsigned)index>=m_selStartCol);
  130. else if (replaceCurrentLine==m_selEndLine)
  131. return ((unsigned)index+(unsigned)matchedlength<=m_selEndCol);
  132. else
  133. return (replaceCurrentLine>=m_selStartLine&&replaceCurrentLine<=m_selEndLine);
  134. }
  135. private:
  136. bool m_haveSelection;
  137. unsigned m_selStartLine, m_selStartCol, m_selEndLine, m_selEndCol;
  138. };
  139. void SourceFileWindow::initBase()
  140. {
  141. setIcon(QPixmap::fromMimeSource("icon.png"));
  142. KWin::setIcons(winId(),*(icon()),QPixmap::fromMimeSource("ktigcc.png"));
  143. sourceFiles.append(THIS);
  144. THIS->dirWatch=new KDirWatch(this);
  145. setCaption(caption()+" - "+THIS->fileName);
  146. THIS->te_popup = new QPopupMenu(this);
  147. THIS->te_popup->insertItem("&Open file at cursor",0);
  148. THIS->te_popup->insertItem("&Find symbol declaration",1);
  149. THIS->te_popup->insertSeparator();
  150. THIS->te_popup->insertItem("&Undo",2);
  151. THIS->te_popup->insertItem("&Redo",3);
  152. THIS->te_popup->insertSeparator();
  153. THIS->te_popup->insertItem("&Clear",4);
  154. THIS->te_popup->insertItem("Cu&t",5);
  155. THIS->te_popup->insertItem("Cop&y",6);
  156. THIS->te_popup->insertItem("&Paste",7);
  157. THIS->te_popup->insertSeparator();
  158. THIS->te_popup->insertItem("&Select all",8);
  159. THIS->te_popup->insertSeparator();
  160. THIS->te_popup->insertItem("&Increase indent",9);
  161. THIS->te_popup->insertItem("&Decrease indent",10);
  162. connect(THIS->te_popup,SIGNAL(aboutToShow()),this,SLOT(te_popup_aboutToShow()));
  163. connect(THIS->te_popup,SIGNAL(activated(int)),this,SLOT(te_popup_activated(int)));
  164. THIS->kfinddialog = static_cast<KFindDialog *>(NULL);
  165. THIS->kreplace = static_cast<KReplaceWithSelectionS *>(NULL);
  166. THIS->kateView=static_cast<Kate::View *>(NULL);
  167. THIS->kateView=reinterpret_cast<Kate::View *>(createView(THIS->fileName,THIS->fileText,HL_MODE,
  168. THIS->isASMFile?preferences.tabWidthAsm:THIS->isCFile?preferences.tabWidthC:8));
  169. THIS->fileText=QString::null;
  170. int rightStatusSize=size().width();
  171. unsigned int line, col;
  172. CURRENT_VIEW->cursorPositionReal(&line,&col);
  173. THIS->rowStatusLabel=new QLabel(QString("%1").arg(line+1),this);
  174. THIS->rowStatusLabel->setAlignment(Qt::AlignRight);
  175. THIS->rowStatusLabel->setMaximumWidth(30);
  176. statusBar()->addWidget(THIS->rowStatusLabel,1);
  177. THIS->colStatusLabel=new QLabel(QString("%1").arg(col+1),this);
  178. THIS->colStatusLabel->setAlignment(Qt::AlignRight);
  179. THIS->colStatusLabel->setMaximumWidth(30);
  180. statusBar()->addWidget(THIS->colStatusLabel,1);
  181. THIS->charsStatusLabel=new QLabel(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()),this);
  182. THIS->charsStatusLabel->setMaximumWidth(100);
  183. statusBar()->addWidget(THIS->charsStatusLabel,1);
  184. THIS->rightStatusLabel=new QLabel(THIS->fileName,this);
  185. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  186. statusBar()->addWidget(THIS->rightStatusLabel,1);
  187. statusBar()->setSizeGripEnabled(FALSE);
  188. connect(statusBar(),SIGNAL(messageChanged(const QString &)),this,SLOT(statusBar_messageChanged(const QString &)));
  189. connect(THIS->dirWatch,SIGNAL(created(const QString &)),this,SLOT(KDirWatch_dirty(const QString &)));
  190. connect(THIS->dirWatch,SIGNAL(dirty(const QString &)),this,SLOT(KDirWatch_dirty(const QString &)));
  191. THIS->dirWatch->addFile(THIS->fileName);
  192. THIS->dirWatch->startScan();
  193. connect(clipboard,SIGNAL(dataChanged()),this,SLOT(clipboard_dataChanged()));
  194. centralWidget()->layout()->add(CURRENT_VIEW);
  195. CURRENT_VIEW->show();
  196. editUndoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->undoCount()));
  197. editRedoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->redoCount()));
  198. editClearAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  199. editCutAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  200. editCopyAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  201. editPasteAction->setEnabled(!clipboard->text().isNull());
  202. THIS->accel=new QAccel(this);
  203. THIS->accel->insertItem(ALT+Key_Backspace,0);
  204. THIS->accel->insertItem(SHIFT+ALT+Key_Backspace,1);
  205. THIS->accel->insertItem(SHIFT+Key_Delete,2);
  206. THIS->accel->insertItem(CTRL+Key_Insert,3);
  207. THIS->accel->insertItem(SHIFT+Key_Insert,4);
  208. THIS->accel->insertItem(Key_F1,5);
  209. THIS->accel->insertItem(Key_Enter,6);
  210. THIS->accel->insertItem(Key_Return,7);
  211. THIS->accel->insertItem(CTRL+Key_J,8);
  212. THIS->accel->insertItem(CTRL+Key_Space,9);
  213. THIS->accel->insertItem(CTRL+Key_M,10);
  214. THIS->accel->setItemEnabled(0,!!(CURRENT_VIEW->getDoc()->undoCount()));
  215. THIS->accel->setItemEnabled(1,!!(CURRENT_VIEW->getDoc()->redoCount()));
  216. THIS->accel->setItemEnabled(2,CURRENT_VIEW->getDoc()->hasSelection());
  217. THIS->accel->setItemEnabled(3,CURRENT_VIEW->getDoc()->hasSelection());
  218. THIS->accel->setItemEnabled(4,!clipboard->text().isNull());
  219. THIS->accel->setItemEnabled(5,TRUE);
  220. THIS->accel->setItemEnabled(6,TRUE);
  221. THIS->accel->setItemEnabled(7,TRUE);
  222. THIS->accel->setItemEnabled(8,TRUE);
  223. THIS->accel->setItemEnabled(9,TRUE);
  224. THIS->accel->setItemEnabled(10,TRUE);
  225. connect(THIS->accel,SIGNAL(activated(int)),this,SLOT(accel_activated(int)));
  226. if (preferences.useSystemIcons) {
  227. setUsesBigPixmaps(TRUE);
  228. fileSaveAction->setIconSet(LOAD_ICON("filesave"));
  229. fileAddToProjectAction->setIconSet(LOAD_ICON("edit_add"));
  230. fileCompileAction->setIconSet(LOAD_ICON("compfile"));
  231. filePrintAction->setIconSet(LOAD_ICON("fileprint"));
  232. filePrintQuicklyAction->setIconSet(LOAD_ICON("fileprint"));
  233. editClearAction->setIconSet(LOAD_ICON("editdelete"));
  234. editCutAction->setIconSet(LOAD_ICON("editcut"));
  235. editCopyAction->setIconSet(LOAD_ICON("editcopy"));
  236. editPasteAction->setIconSet(LOAD_ICON("editpaste"));
  237. findFindAction->setIconSet(LOAD_ICON("filefind"));
  238. if (KGlobal::iconLoader()->iconPath("stock-find-and-replace",KIcon::Small,TRUE).isEmpty()) {
  239. QIconSet fileReplaceIconSet(QPixmap::fromMimeSource("filereplace.png"));
  240. int smallSize=IconSize(KIcon::Small);
  241. fileReplaceIconSet.setIconSize(QIconSet::Small,QSize(smallSize,smallSize));
  242. int largeSize=IconSize(KIcon::MainToolbar);
  243. fileReplaceIconSet.setIconSize(QIconSet::Large,QSize(largeSize,largeSize));
  244. findReplaceAction->setIconSet(fileReplaceIconSet);
  245. } else
  246. findReplaceAction->setIconSet(LOAD_ICON("stock-find-and-replace"));
  247. editUndoAction->setIconSet(LOAD_ICON("undo"));
  248. editRedoAction->setIconSet(LOAD_ICON("redo"));
  249. findFunctionsAction->setIconSet(LOAD_ICON("view_tree"));
  250. editIncreaseIndentAction->setIconSet(LOAD_ICON("indent"));
  251. editDecreaseIndentAction->setIconSet(LOAD_ICON("unindent"));
  252. }
  253. QToolButton *findFunctionsButton=static_cast<QToolButton *>(toolBar
  254. ->child("findFunctionsAction_action_button","QToolButton",FALSE));
  255. THIS->findFunctionsPopup=new QPopupMenu(findFunctionsButton);
  256. connect(THIS->findFunctionsPopup,SIGNAL(aboutToShow()),
  257. this,SLOT(findFunctionsPopup_aboutToShow()));
  258. connect(THIS->findFunctionsPopup,SIGNAL(aboutToHide()),
  259. this,SLOT(findFunctionsPopup_aboutToHide()));
  260. connect(THIS->findFunctionsPopup,SIGNAL(activated(int)),
  261. this,SLOT(findFunctionsPopup_activated(int)));
  262. findFunctionsButton->setPopupDelay(0);
  263. findFunctionsButton->setPopup(THIS->findFunctionsPopup);
  264. if (THIS->isTextFile) findFunctionsAction->setEnabled(FALSE);
  265. }
  266. void SourceFileWindow::destroy()
  267. {
  268. MainForm::deleteErrorsForSrcFile(this);
  269. if (THIS->kreplace) delete THIS->kreplace;
  270. if (THIS->kfinddialog) {
  271. findHistory=THIS->kfinddialog->findHistory();
  272. delete THIS->kfinddialog;
  273. }
  274. delete THIS->accel;
  275. delete THIS->te_popup;
  276. delete THIS->rowStatusLabel;
  277. delete THIS->colStatusLabel;
  278. delete THIS->charsStatusLabel;
  279. delete THIS->rightStatusLabel;
  280. THIS->dirWatch->removeFile(THIS->fileName);
  281. delete THIS->dirWatch;
  282. if (THIS->kateView) delete THIS->kateView->getDoc();
  283. sourceFiles.remove(THIS);
  284. }
  285. void SourceFileWindow::te_popup_aboutToShow()
  286. {
  287. THIS->te_popup->setItemEnabled(0,findOpenFileAtCursorAction->isEnabled());
  288. THIS->te_popup->setItemEnabled(1,findFindSymbolDeclarationAction->isEnabled());
  289. THIS->te_popup->setItemEnabled(2,editUndoAction->isEnabled());
  290. THIS->te_popup->setItemEnabled(3,editRedoAction->isEnabled());
  291. THIS->te_popup->setItemEnabled(4,editClearAction->isEnabled());
  292. THIS->te_popup->setItemEnabled(5,editCutAction->isEnabled());
  293. THIS->te_popup->setItemEnabled(6,editCopyAction->isEnabled());
  294. THIS->te_popup->setItemEnabled(7,editPasteAction->isEnabled());
  295. THIS->te_popup->setItemEnabled(8,editSelectAllAction->isEnabled());
  296. THIS->te_popup->setItemEnabled(9,editIncreaseIndentAction->isEnabled());
  297. THIS->te_popup->setItemEnabled(10,editDecreaseIndentAction->isEnabled());
  298. }
  299. void SourceFileWindow::te_popup_activated(int index)
  300. {
  301. switch (index) {
  302. case 0: findOpenFileAtCursor(); break;
  303. case 1: findFindSymbolDeclaration(); break;
  304. case 2: editUndo(); break;
  305. case 3: editRedo(); break;
  306. case 4: editClear(); break;
  307. case 5: editCut(); break;
  308. case 6: editCopy(); break;
  309. case 7: editPaste(); break;
  310. case 8: editSelectAll(); break;
  311. case 9: editIncreaseIndent(); break;
  312. case 10: editDecreaseIndent(); break;
  313. default: break;
  314. }
  315. }
  316. void SourceFileWindow::accel_activated(int index)
  317. {
  318. if (CURRENT_VIEW && CURRENT_VIEW->hasFocus()) {
  319. switch (index) {
  320. case 0: editUndo(); break;
  321. case 1: editRedo(); break;
  322. case 2: editCut(); break;
  323. case 3: editCopy(); break;
  324. case 4: editPaste(); break;
  325. case 5: // F1 context help
  326. {
  327. QString wordUnderCursor=CURRENT_VIEW->currentWord();
  328. // always open at least the index
  329. force_qt_assistant_page(1);
  330. assistant->openAssistant();
  331. if (wordUnderCursor.isEmpty()) return;
  332. QString docFile=lookup_doc_keyword(wordUnderCursor);
  333. if (docFile.isEmpty()) return;
  334. assistant->openAssistant(
  335. QString(tigcc_base)+QString("/doc/html/")+docFile);
  336. break;
  337. }
  338. case 6:
  339. case 7:
  340. CURRENT_VIEW->keyReturn();
  341. current_view_newLineHook();
  342. break;
  343. case 8:
  344. new TemplatePopup(CURRENT_VIEW);
  345. break;
  346. case 9:
  347. case 10:
  348. // Completion only operates on C files.
  349. if (THIS->isCFile) {
  350. // Disable newLineHook.
  351. THIS->accel->setItemEnabled(6,FALSE);
  352. THIS->accel->setItemEnabled(7,FALSE);
  353. new CompletionPopup(CURRENT_VIEW,THIS->fileName,THIS->mainForm,this);
  354. }
  355. break;
  356. default: break;
  357. }
  358. } else if (index == 6 || index == 7) {
  359. QKeyEvent *keyEvent=new QKeyEvent(QEvent::KeyPress,Key_Return,'\n',0,"\n");
  360. QApplication::postEvent(focusWidget(),keyEvent);
  361. }
  362. }
  363. void SourceFileWindow::completionPopup_closed()
  364. {
  365. // Restore newLineHook.
  366. THIS->accel->setItemEnabled(6,TRUE);
  367. THIS->accel->setItemEnabled(7,TRUE);
  368. }
  369. void *SourceFileWindow::createView(const QString &fileName, const QString &fileText, const QString &hlModeName, unsigned tabWidth)
  370. {
  371. // Create Document object.
  372. KParts::Factory *factory = (KParts::Factory *)
  373. KLibLoader::self()->factory ("libkatepart");
  374. if (!factory) qFatal("Failed to load KatePart");
  375. Kate::Document *doc = (Kate::Document *)
  376. factory->createPart( 0, "", THIS->mainForm, "", "Kate::Document" );
  377. // Set the file name for printing.
  378. doc->setModified(FALSE);
  379. if (doc->openStream("text/plain",fileName))
  380. doc->closeStream();
  381. // Create View object.
  382. Kate::View *newView = (Kate::View *) doc->createView( centralWidget(), 0L );
  383. newView->hide();
  384. newView->setSizePolicy(QSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored,0,0));
  385. // Set highlighting mode.
  386. uint cnt=newView->getDoc()->hlModeCount(), i;
  387. for (i=0; i<cnt; i++) {
  388. if (!newView->getDoc()->hlModeName(i).compare(hlModeName)) break;
  389. }
  390. if (i==cnt) i=0;
  391. newView->getDoc()->setHlMode(i);
  392. // Set options.
  393. newView->setDynWordWrap(FALSE);
  394. if (preferences.removeTrailingSpaces)
  395. newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  396. else
  397. newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  398. newView->setTabWidth(tabWidth);
  399. connect(newView,SIGNAL(cursorPositionChanged()),this,SLOT(current_view_cursorPositionChanged()));
  400. connect(newView->getDoc(),SIGNAL(textChanged()),this,SLOT(current_view_textChanged()));
  401. connect(newView->getDoc(),SIGNAL(undoChanged()),this,SLOT(current_view_undoChanged()));
  402. connect(newView->getDoc(),SIGNAL(selectionChanged()),this,SLOT(current_view_selectionChanged()));
  403. connect(newView->getDoc(),SIGNAL(charactersInteractivelyInserted(int,int,const QString&)),this,SLOT(current_view_charactersInteractivelyInserted(int,int,const QString&)));
  404. newView->installPopup(THIS->te_popup);
  405. // Set text.
  406. SET_TEXT_SAFE(newView->getDoc(),fileText);
  407. newView->getDoc()->setModified(FALSE);
  408. newView->getDoc()->clearUndo();
  409. newView->getDoc()->clearRedo();
  410. newView->setCursorPositionReal(0,0);
  411. return newView;
  412. }
  413. // Returns 1 if the source file should not be closed, 0 if it can be closed.
  414. int SourceFileWindow::savePrompt(void)
  415. {
  416. int result;
  417. if (!CURRENT_VIEW) return 0;
  418. while (CURRENT_VIEW->getDoc()->isModified()) { // "while" in case saving fails!
  419. 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());
  420. if (result==KMessageBox::Yes)
  421. fileSave();
  422. else if (result==KMessageBox::No)
  423. return 0;
  424. else
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. void SourceFileWindow::removeTrailingSpacesFromView(void *view)
  430. {
  431. if (!preferences.removeTrailingSpaces) return;
  432. Kate::View *kateView=reinterpret_cast<Kate::View *>(view);
  433. Kate::Document *doc=kateView->getDoc();
  434. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  435. editExt->editBegin();
  436. unsigned numLines=doc->numLines();
  437. for (unsigned i=0; i<numLines; i++) {
  438. QString line=doc->textLine(i);
  439. int whitespace=line.find(QRegExp("\\s+$"));
  440. if (whitespace>=0) doc->removeText(i,whitespace,i,line.length());
  441. }
  442. editExt->editEnd();
  443. }
  444. void SourceFileWindow::fileSave()
  445. {
  446. THIS->dirWatch->removeFile(THIS->fileName);
  447. if (saveFileText(THIS->fileName,CURRENT_VIEW->getDoc()->text())) {
  448. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(THIS->fileName));
  449. THIS->dirWatch->addFile(THIS->fileName);
  450. } else {
  451. THIS->dirWatch->addFile(THIS->fileName);
  452. removeTrailingSpacesFromView(CURRENT_VIEW);
  453. CURRENT_VIEW->getDoc()->setModified(FALSE);
  454. }
  455. }
  456. void SourceFileWindow::fileSaveAs()
  457. {
  458. QString saveFileName=MainForm::SGetFileName(KFileDialog::Saving,
  459. (THIS->fileName.endsWith(".h")?TIGCC_H_Filter TIGCCAllFilter:
  460. THIS->fileName.endsWith(".c")?TIGCC_C_Filter TIGCCAllFilter:
  461. THIS->fileName.endsWith(".s")?TIGCC_S_Filter TIGCCAllFilter:
  462. THIS->fileName.endsWith(".asm")?TIGCC_ASM_Filter TIGCCAllFilter:
  463. THIS->fileName.endsWith(".qll")?TIGCC_QLL_Filter TIGCCAllFilter:
  464. THIS->fileName.endsWith(".txt")?TIGCC_TXT_Filter TIGCCAllFilter:
  465. TIGCCAllFilter),"Save Source File",this);
  466. if (saveFileName.isEmpty())
  467. return;
  468. THIS->dirWatch->removeFile(THIS->fileName);
  469. if (saveFileText(saveFileName,CURRENT_VIEW->getDoc()->text())) {
  470. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(saveFileName));
  471. THIS->dirWatch->addFile(THIS->fileName);
  472. } else {
  473. if (saveFileName.compare(THIS->fileName)) {
  474. // Update the file name for printing.
  475. unsigned int line,col,hlMode;
  476. QString fileText=CURRENT_VIEW->getDoc()->text();
  477. hlMode=CURRENT_VIEW->getDoc()->hlMode();
  478. CURRENT_VIEW->cursorPositionReal(&line,&col);
  479. CURRENT_VIEW->getDoc()->setModified(FALSE);
  480. if (CURRENT_VIEW->getDoc()->openStream("text/plain",saveFileName))
  481. CURRENT_VIEW->getDoc()->closeStream();
  482. SET_TEXT_SAFE(CURRENT_VIEW->getDoc(),fileText);
  483. CURRENT_VIEW->getDoc()->clearUndo();
  484. CURRENT_VIEW->getDoc()->clearRedo();
  485. CURRENT_VIEW->getDoc()->setHlMode(hlMode);
  486. CURRENT_VIEW->setCursorPositionReal(line,col);
  487. // Update the caption
  488. setCaption(caption().left(caption().find('-')+2)+saveFileName);
  489. }
  490. THIS->fileName=saveFileName;
  491. THIS->dirWatch->addFile(saveFileName);
  492. removeTrailingSpacesFromView(CURRENT_VIEW);
  493. CURRENT_VIEW->getDoc()->setModified(FALSE);
  494. updateRightStatusLabel();
  495. }
  496. }
  497. void SourceFileWindow::fileAddToProject()
  498. {
  499. THIS->mainForm->adoptSourceFile(THIS);
  500. }
  501. void SourceFileWindow::fileCompile()
  502. {
  503. THIS->mainForm->compileSourceFile(THIS);
  504. }
  505. void SourceFileWindow::filePrint()
  506. {
  507. if (CURRENT_VIEW) CURRENT_VIEW->getDoc()->printDialog();
  508. }
  509. void SourceFileWindow::filePrintQuickly()
  510. {
  511. if (CURRENT_VIEW) CURRENT_VIEW->getDoc()->print();
  512. }
  513. void SourceFileWindow::applyPreferences()
  514. {
  515. // Apply the KatePart preferences and treeview icons.
  516. KParts::Factory *factory = (KParts::Factory *)
  517. KLibLoader::self()->factory ("libkatepart");
  518. if (!factory) qFatal("Failed to load KatePart");
  519. Kate::Document *doc = (Kate::Document *)
  520. factory->createPart( 0, "", this, "", "Kate::Document" );
  521. KTextEditor::ConfigInterfaceExtension *confInterfaceExt = KTextEditor::configInterfaceExtension(doc);
  522. unsigned numConfigPages=confInterfaceExt->configPages();
  523. for (unsigned i=0; i<numConfigPages; i++) {
  524. if (!confInterfaceExt->configPageName(i).compare("Fonts & Colors")) {
  525. KTextEditor::ConfigPage *configPage=confInterfaceExt->configPage(i);
  526. configPage->apply();
  527. delete configPage;
  528. break;
  529. }
  530. }
  531. delete doc;
  532. Kate::View *kateView=CURRENT_VIEW;
  533. if (kateView) {
  534. QString fileText=kateView->getDoc()->text();
  535. if (preferences.removeTrailingSpaces)
  536. kateView->getDoc()->setConfigFlags(kateView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  537. else
  538. kateView->getDoc()->setConfigFlags(kateView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_TRAILING_DYN));
  539. kateView->setTabWidth(THIS->isASMFile?preferences.tabWidthAsm:
  540. THIS->isCFile?preferences.tabWidthC:8);
  541. // Kate seems really insisting on making it a pain to update syntax highlighting settings.
  542. unsigned cnt=kateView->getDoc()->hlModeCount(), i;
  543. for (i=0; i<cnt; i++) {
  544. if (kateView->getDoc()->hlModeName(i)==HL_MODE) break;
  545. }
  546. if (i==cnt) i=0;
  547. kateView->getDoc()->setHlMode(i);
  548. // Force redrawing to get the tab width right, repaint() is ignored for some reason.
  549. kateView->hide();
  550. kateView->show();
  551. }
  552. // Apply the icon preferences.
  553. setUsesBigPixmaps(preferences.useSystemIcons);
  554. if (preferences.useSystemIcons) {
  555. fileSaveAction->setIconSet(LOAD_ICON("filesave"));
  556. fileAddToProjectAction->setIconSet(LOAD_ICON("edit_add"));
  557. fileCompileAction->setIconSet(LOAD_ICON("compfile"));
  558. filePrintAction->setIconSet(LOAD_ICON("fileprint"));
  559. filePrintQuicklyAction->setIconSet(LOAD_ICON("fileprint"));
  560. editClearAction->setIconSet(LOAD_ICON("editdelete"));
  561. editCutAction->setIconSet(LOAD_ICON("editcut"));
  562. editCopyAction->setIconSet(LOAD_ICON("editcopy"));
  563. editPasteAction->setIconSet(LOAD_ICON("editpaste"));
  564. findFindAction->setIconSet(LOAD_ICON("filefind"));
  565. if (KGlobal::iconLoader()->iconPath("stock-find-and-replace",KIcon::Small,TRUE).isEmpty()) {
  566. QIconSet fileReplaceIconSet(QPixmap::fromMimeSource("filereplace.png"));
  567. int smallSize=IconSize(KIcon::Small);
  568. fileReplaceIconSet.setIconSize(QIconSet::Small,QSize(smallSize,smallSize));
  569. int largeSize=IconSize(KIcon::MainToolbar);
  570. fileReplaceIconSet.setIconSize(QIconSet::Large,QSize(largeSize,largeSize));
  571. findReplaceAction->setIconSet(fileReplaceIconSet);
  572. } else
  573. findReplaceAction->setIconSet(LOAD_ICON("stock-find-and-replace"));
  574. editUndoAction->setIconSet(LOAD_ICON("undo"));
  575. editRedoAction->setIconSet(LOAD_ICON("redo"));
  576. findFunctionsAction->setIconSet(LOAD_ICON("view_tree"));
  577. editIncreaseIndentAction->setIconSet(LOAD_ICON("indent"));
  578. editDecreaseIndentAction->setIconSet(LOAD_ICON("unindent"));
  579. } else {
  580. fileSaveAction->setIconSet(QIconSet(QPixmap::fromMimeSource("02")));
  581. fileAddToProjectAction->setIconSet(QIconSet(QPixmap::fromMimeSource("08")));
  582. fileCompileAction->setIconSet(QIconSet(QPixmap::fromMimeSource("09")));
  583. filePrintAction->setIconSet(QIconSet(QPixmap::fromMimeSource("03")));
  584. filePrintQuicklyAction->setIconSet(QIconSet(QPixmap::fromMimeSource("03")));
  585. editClearAction->setIconSet(QIconSet(QPixmap::fromMimeSource("04")));
  586. editCutAction->setIconSet(QIconSet(QPixmap::fromMimeSource("05")));
  587. editCopyAction->setIconSet(QIconSet(QPixmap::fromMimeSource("06")));
  588. editPasteAction->setIconSet(QIconSet(QPixmap::fromMimeSource("07")));
  589. findFindAction->setIconSet(QIconSet(QPixmap::fromMimeSource("13")));
  590. findReplaceAction->setIconSet(QIconSet(QPixmap::fromMimeSource("14")));
  591. editUndoAction->setIconSet(QIconSet(QPixmap::fromMimeSource("16")));
  592. editRedoAction->setIconSet(QIconSet(QPixmap::fromMimeSource("17")));
  593. findFunctionsAction->setIconSet(QIconSet(QPixmap::fromMimeSource("18")));
  594. editIncreaseIndentAction->setIconSet(QIconSet(QPixmap::fromMimeSource("19")));
  595. editDecreaseIndentAction->setIconSet(QIconSet(QPixmap::fromMimeSource("20")));
  596. }
  597. }
  598. void SourceFileWindow::editUndo()
  599. {
  600. if (CURRENT_VIEW)
  601. CURRENT_VIEW->getDoc()->undo();
  602. }
  603. void SourceFileWindow::editRedo()
  604. {
  605. if (CURRENT_VIEW)
  606. CURRENT_VIEW->getDoc()->redo();
  607. }
  608. void SourceFileWindow::editClear()
  609. {
  610. if (CURRENT_VIEW)
  611. CURRENT_VIEW->getDoc()->removeSelectedText();
  612. }
  613. void SourceFileWindow::editCut()
  614. {
  615. if (CURRENT_VIEW)
  616. CURRENT_VIEW->cut();
  617. }
  618. void SourceFileWindow::editCopy()
  619. {
  620. if (CURRENT_VIEW)
  621. CURRENT_VIEW->copy();
  622. }
  623. void SourceFileWindow::editPaste()
  624. {
  625. if (CURRENT_VIEW)
  626. CURRENT_VIEW->paste();
  627. }
  628. void SourceFileWindow::editSelectAll()
  629. {
  630. if (CURRENT_VIEW)
  631. CURRENT_VIEW->getDoc()->selectAll();
  632. }
  633. void SourceFileWindow::editIncreaseIndent()
  634. {
  635. if (CURRENT_VIEW)
  636. CURRENT_VIEW->indent();
  637. }
  638. void SourceFileWindow::editDecreaseIndent()
  639. {
  640. if (CURRENT_VIEW)
  641. CURRENT_VIEW->unIndent();
  642. }
  643. void SourceFileWindow::findFind()
  644. {
  645. if (THIS->kfinddialog)
  646. KWin::activateWindow(THIS->kfinddialog->winId());
  647. else {
  648. // Never set hasSelection because finding in selection doesn't really make
  649. // sense with my non-modal find dialog setup.
  650. THIS->kfinddialog=new KFindDialog(false,this,0,KFindDialog::FromCursor,findHistory);
  651. connect(THIS->kfinddialog, SIGNAL(okClicked()), this, SLOT(findFind_next()));
  652. connect(THIS->kfinddialog, SIGNAL(cancelClicked()), this, SLOT(findFind_stop()));
  653. THIS->kfinddialog->show();
  654. }
  655. }
  656. void SourceFileWindow::findFind_next()
  657. {
  658. // Use a local KFind object. The search will need to be restarted next time
  659. // this function is called because of the non-modality of the find dialog.
  660. KFind *kfind=new KFind(THIS->kfinddialog->pattern(),THIS->kfinddialog->options(),this,THIS->kfinddialog);
  661. // Initialize.
  662. bool findBackwards=!!(THIS->kfinddialog->options()&KFindDialog::FindBackwards);
  663. int findCurrentCol;
  664. kfind=new KFind(THIS->kfinddialog->pattern(),THIS->kfinddialog->options(),this,THIS->kfinddialog);
  665. kfind->closeFindNextDialog(); // don't use this, a non-modal KFindDialog is used instead
  666. connect(kfind,SIGNAL(highlight(const QString &,int,int)),
  667. this,SLOT(findFind_highlight(const QString &,int,int)));
  668. if (THIS->kfinddialog->options()&KFindDialog::FromCursor) {
  669. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  670. if (findBackwards) {
  671. THIS->findCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  672. findCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  673. if (findCurrentCol==-1) {
  674. if (!THIS->findCurrentLine) goto skip_data;
  675. THIS->findCurrentLine--;
  676. }
  677. } else {
  678. THIS->findCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  679. findCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  680. }
  681. } else {
  682. THIS->findCurrentLine=CURRENT_VIEW->cursorLine();
  683. findCurrentCol=CURRENT_VIEW->cursorColumnReal();
  684. }
  685. } else {
  686. THIS->findCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  687. findCurrentCol=-1;
  688. }
  689. kfind->setData(CURRENT_VIEW->getDoc()->textLine(THIS->findCurrentLine),findCurrentCol);
  690. skip_data:;
  691. // Now find the next occurrence.
  692. KFind::Result result;
  693. Kate::View *currView=CURRENT_VIEW;
  694. unsigned currNumLines=CURRENT_VIEW->getDoc()->numLines();
  695. do {
  696. if (kfind->needData()) {
  697. if (findBackwards?!THIS->findCurrentLine:(THIS->findCurrentLine>=currNumLines)) {
  698. // Try restarting the search.
  699. currNumLines=currView->getDoc()->numLines();
  700. THIS->findCurrentLine=findBackwards?currNumLines-1:0;
  701. do {
  702. if (kfind->needData()) {
  703. if (findBackwards?!THIS->findCurrentLine:(THIS->findCurrentLine>=currNumLines))
  704. goto not_found_current;
  705. if (findBackwards) THIS->findCurrentLine--; else THIS->findCurrentLine++;
  706. kfind->setData(currView->getDoc()->textLine(THIS->findCurrentLine));
  707. }
  708. result=kfind->find();
  709. } while (result==KFind::NoMatch);
  710. break;
  711. not_found_current:
  712. KMessageBox::error(this,QString("Text \'%1\' not found").arg(THIS->kfinddialog->pattern()));
  713. delete kfind;
  714. return;
  715. } else if (findBackwards) THIS->findCurrentLine--; else THIS->findCurrentLine++;
  716. kfind->setData(currView->getDoc()->textLine(THIS->findCurrentLine));
  717. }
  718. result=kfind->find();
  719. } while (result==KFind::NoMatch);
  720. delete kfind;
  721. }
  722. #define unused_text text __attribute__((unused))
  723. void SourceFileWindow::findFind_highlight(const QString &unused_text, int matchingindex, int matchedlength)
  724. {
  725. CURRENT_VIEW->setCursorPositionReal(THIS->findCurrentLine,matchingindex+matchedlength);
  726. CURRENT_VIEW->getDoc()->setSelection(THIS->findCurrentLine,matchingindex,
  727. THIS->findCurrentLine,matchingindex+matchedlength);
  728. }
  729. void SourceFileWindow::findFind_stop()
  730. {
  731. if (THIS->kfinddialog) {
  732. findHistory=THIS->kfinddialog->findHistory();
  733. THIS->kfinddialog->deleteLater();
  734. }
  735. THIS->kfinddialog=static_cast<KFindDialog *>(NULL);
  736. }
  737. void SourceFileWindow::findReplace()
  738. {
  739. if (THIS->kreplace) {
  740. KDialogBase *replaceNextDialog=THIS->kreplace->replaceNextDialog();
  741. if (replaceNextDialog)
  742. KWin::activateWindow(replaceNextDialog->winId());
  743. return;
  744. }
  745. KReplaceDialog kreplacedialog(this,0,((CURRENT_VIEW&&CURRENT_VIEW->getDoc()->hasSelection()
  746. &&CURRENT_VIEW->getDoc()->selStartLine()!=CURRENT_VIEW->getDoc()->selEndLine())?
  747. KFindDialog::SelectedText:0)|KFindDialog::FromCursor,
  748. findHistory,replacementHistory,
  749. CURRENT_VIEW&&CURRENT_VIEW->getDoc()->hasSelection());
  750. if (kreplacedialog.exec()!=QDialog::Accepted)
  751. return;
  752. findHistory=kreplacedialog.findHistory();
  753. replacementHistory=kreplacedialog.replacementHistory();
  754. THIS->kreplace=new KReplaceWithSelectionS(kreplacedialog.pattern(),kreplacedialog.replacement(),
  755. kreplacedialog.options(),this);
  756. // Connect signals to code which handles highlighting of found text, and
  757. // on-the-fly replacement.
  758. connect(THIS->kreplace,SIGNAL(highlight(const QString &,int,int)),
  759. this,SLOT(findReplace_highlight(const QString &,int,int)));
  760. // Connect findNext signal - called when pressing the button in the dialog.
  761. connect(THIS->kreplace,SIGNAL(findNext()),this,SLOT(findReplace_next()));
  762. // Connect replace signal - called when doing a replacement.
  763. connect(THIS->kreplace,SIGNAL(replace(const QString &,int,int,int)),
  764. this,SLOT(findReplace_replace(const QString &,int,int,int)));
  765. // Connect dialogClosed signal - called when closing the Replace Next dialog.
  766. connect(THIS->kreplace,SIGNAL(dialogClosed()),this,SLOT(findReplace_stop()));
  767. // Initialize.
  768. bool findBackwards=!!(THIS->kreplace->options()&KFindDialog::FindBackwards);
  769. int replaceCurrentCol;
  770. if (CURRENT_VIEW) {
  771. if (THIS->kreplace->options()&KFindDialog::SelectedText) {
  772. THIS->kreplace->setSelection(CURRENT_VIEW->getDoc()->selStartLine(),
  773. CURRENT_VIEW->getDoc()->selStartCol(),
  774. CURRENT_VIEW->getDoc()->selEndLine(),
  775. CURRENT_VIEW->getDoc()->selEndCol());
  776. if (findBackwards) {
  777. THIS->kreplace->replaceCurrentLine=THIS->kreplace->selEndLine();
  778. replaceCurrentCol=THIS->kreplace->selEndCol();
  779. } else {
  780. THIS->kreplace->replaceCurrentLine=THIS->kreplace->selStartLine();
  781. replaceCurrentCol=THIS->kreplace->selStartCol();
  782. }
  783. THIS->kreplace->setOptions(THIS->kreplace->options()&~KFindDialog::FromCursor);
  784. } else if (THIS->kreplace->options()&KFindDialog::FromCursor) {
  785. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  786. if (findBackwards) {
  787. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  788. replaceCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  789. if (replaceCurrentCol==-1) {
  790. if (!THIS->kreplace->replaceCurrentLine) goto skip_data;
  791. THIS->kreplace->replaceCurrentLine--;
  792. }
  793. } else {
  794. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  795. replaceCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  796. }
  797. } else {
  798. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->cursorLine();
  799. replaceCurrentCol=CURRENT_VIEW->cursorColumnReal();
  800. // Don't prompt for restarting if we actually searched the entire document.
  801. if (findBackwards?(THIS->kreplace->replaceCurrentLine==(CURRENT_VIEW->getDoc()->numLines()-1)
  802. && replaceCurrentCol==(CURRENT_VIEW->getDoc()->lineLength(THIS->kreplace->replaceCurrentLine)))
  803. :(!THIS->kreplace->replaceCurrentLine&&!replaceCurrentCol))
  804. THIS->kreplace->setOptions(THIS->kreplace->options()&~KFindDialog::FromCursor);
  805. }
  806. } else {
  807. THIS->kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  808. replaceCurrentCol=-1;
  809. }
  810. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine),replaceCurrentCol);
  811. }
  812. skip_data:
  813. // Now find the next occurrence.
  814. findReplace_next(TRUE);
  815. }
  816. void SourceFileWindow::findReplace_next()
  817. {
  818. findReplace_next(FALSE);
  819. }
  820. void SourceFileWindow::findReplace_next(bool firstTime)
  821. {
  822. bool findBackwards=!!(THIS->kreplace->options()&KFindDialog::FindBackwards);
  823. // Reinitialize.
  824. if (!firstTime) {
  825. int replaceCurrentCol;
  826. // Non-first-time always continues from cursor.
  827. if (CURRENT_VIEW->getDoc()->hasSelection()) {
  828. if (findBackwards) {
  829. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selStartLine();
  830. replaceCurrentCol=CURRENT_VIEW->getDoc()->selStartCol()-1;
  831. if (replaceCurrentCol==-1) {
  832. if (!THIS->kreplace->replaceCurrentLine) goto skip_data;
  833. THIS->kreplace->replaceCurrentLine--;
  834. }
  835. } else {
  836. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->getDoc()->selEndLine();
  837. replaceCurrentCol=CURRENT_VIEW->getDoc()->selEndCol();
  838. }
  839. } else {
  840. THIS->kreplace->replaceCurrentLine=CURRENT_VIEW->cursorLine();
  841. replaceCurrentCol=CURRENT_VIEW->cursorColumnReal();
  842. }
  843. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine),replaceCurrentCol);
  844. }
  845. skip_data:;
  846. // Now find the next occurrence.
  847. KFind::Result result;
  848. Kate::View *currView=CURRENT_VIEW;
  849. unsigned currNumLines=0;
  850. if (CURRENT_VIEW) currNumLines=CURRENT_VIEW->getDoc()->numLines();
  851. do {
  852. if (THIS->kreplace->needData()) {
  853. if (THIS->kreplace->haveSelection()
  854. ?(findBackwards?(THIS->kreplace->replaceCurrentLine<=THIS->kreplace->selStartLine())
  855. :(THIS->kreplace->replaceCurrentLine>=THIS->kreplace->selEndLine()))
  856. :(findBackwards?!THIS->kreplace->replaceCurrentLine:(THIS->kreplace->replaceCurrentLine>=currNumLines))) {
  857. if (THIS->kreplace->shouldRestart()) {
  858. // Drop "From cursor" and "Selected text" options.
  859. THIS->kreplace->setOptions(THIS->kreplace->options()&~(KFindDialog::FromCursor
  860. |KFindDialog::SelectedText));
  861. THIS->kreplace->invalidateSelection();
  862. // Reinitialize.
  863. THIS->kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->getDoc()->numLines()-1):0;
  864. THIS->kreplace->setData(CURRENT_VIEW->getDoc()->textLine(THIS->kreplace->replaceCurrentLine));
  865. // Start again as if it was the first time.
  866. findReplace_next(TRUE);
  867. return;
  868. } else {
  869. findReplace_stop();
  870. return;
  871. }
  872. } else if (findBackwards) THIS->kreplace->replaceCurrentLine--; else THIS->kreplace->replaceCurrentLine++;
  873. THIS->kreplace->setData(currView->getDoc()->textLine(THIS->kreplace->replaceCurrentLine));
  874. }
  875. result=THIS->kreplace->replace();
  876. } while (result==KFind::NoMatch);
  877. }
  878. void SourceFileWindow::findReplace_highlight(const QString &unused_text, int matchingindex, int matchedlength)
  879. {
  880. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,matchingindex+matchedlength);
  881. CURRENT_VIEW->getDoc()->setSelection(THIS->kreplace->replaceCurrentLine,matchingindex,
  882. THIS->kreplace->replaceCurrentLine,matchingindex+matchedlength);
  883. }
  884. void SourceFileWindow::findReplace_replace(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
  885. {
  886. bool update=!!(THIS->kreplace->options()&KReplaceDialog::PromptOnReplace);
  887. bool haveSelection=THIS->kreplace->haveSelection();
  888. // The initializations are redundant, but g++ doesn't understand this, and the
  889. // self-initialization trick doesn't work either (-Wno-init-self is ignored).
  890. unsigned selStartLine=0, selStartCol=0, selEndLine=0, selEndCol=0;
  891. if (haveSelection) {
  892. selStartLine=THIS->kreplace->selStartLine();
  893. selStartCol=THIS->kreplace->selStartCol();
  894. selEndLine=THIS->kreplace->selEndLine();
  895. selEndCol=THIS->kreplace->selEndCol();
  896. }
  897. KTextEditor::EditInterfaceExt *editinterfaceext=KTextEditor::editInterfaceExt(CURRENT_VIEW->getDoc());
  898. editinterfaceext->editBegin();
  899. CURRENT_VIEW->getDoc()->insertText(THIS->kreplace->replaceCurrentLine,replacementIndex,
  900. text.mid(replacementIndex,replacedLength));
  901. // We can't put the cursor back now because this breaks editBegin/editEnd.
  902. bool updateCursor=(CURRENT_VIEW->cursorLine()==THIS->kreplace->replaceCurrentLine
  903. && CURRENT_VIEW->cursorColumnReal()==(unsigned)replacementIndex+(unsigned)replacedLength);
  904. CURRENT_VIEW->getDoc()->removeText(THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength,
  905. THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength+matchedLength);
  906. editinterfaceext->editEnd();
  907. if (updateCursor)
  908. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,replacementIndex);
  909. if (update) {
  910. CURRENT_VIEW->setCursorPositionReal(THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength);
  911. CURRENT_VIEW->getDoc()->setSelection(THIS->kreplace->replaceCurrentLine,replacementIndex,
  912. THIS->kreplace->replaceCurrentLine,replacementIndex+replacedLength);
  913. CURRENT_VIEW->repaint();
  914. }
  915. if (haveSelection) {
  916. // Restore selection, updating coordinates if necessary.
  917. THIS->kreplace->setSelection(selStartLine,selStartCol,selEndLine,
  918. (THIS->kreplace->replaceCurrentLine==selEndLine)
  919. ?(selEndCol+replacedLength-matchedLength)
  920. :selEndCol);
  921. }
  922. }
  923. void SourceFileWindow::findReplace_stop()
  924. {
  925. if (THIS->kreplace) THIS->kreplace->deleteLater();
  926. THIS->kreplace=static_cast<KReplaceWithSelectionS *>(NULL);
  927. }
  928. void SourceFileWindow::findFunctions()
  929. {
  930. THIS->functionDialog=new FunctionDialog(this);
  931. connect(THIS->functionDialog->functionListBox,SIGNAL(highlighted(int)),
  932. this,SLOT(findFunctions_functionListBox_highlighted(int)));
  933. connect(THIS->functionDialog->functionListBox,SIGNAL(selected(int)),
  934. this,SLOT(findFunctions_functionListBox_selected(int)));
  935. connect(THIS->functionDialog->prototypeButton,SIGNAL(clicked()),
  936. this,SLOT(findFunctions_prototypeButton_clicked()));
  937. connect(THIS->functionDialog->implementationButton,SIGNAL(clicked()),
  938. this,SLOT(findFunctions_implementationButton_clicked()));
  939. THIS->functionDialog->functionListBox->clear();
  940. THIS->sourceFileFunctions=getFunctions(CURRENT_VIEW->getDoc()->text(),
  941. THIS->isASMFile);
  942. for (SourceFileFunctions::Iterator it=THIS->sourceFileFunctions.begin();
  943. it!=THIS->sourceFileFunctions.end(); ++it)
  944. THIS->functionDialog->functionListBox->insertItem((*it).name);
  945. THIS->functionDialog->exec();
  946. delete THIS->functionDialog;
  947. }
  948. void SourceFileWindow::findFunctions_functionListBox_highlighted(int index)
  949. {
  950. if (index>=0) {
  951. THIS->functionDialog->prototypeButton->setEnabled(
  952. THIS->sourceFileFunctions[index].prototypeLine>=0);
  953. THIS->functionDialog->implementationButton->setEnabled(
  954. THIS->sourceFileFunctions[index].implementationLine>=0);
  955. } else {
  956. THIS->functionDialog->prototypeButton->setEnabled(FALSE);
  957. THIS->functionDialog->implementationButton->setEnabled(FALSE);
  958. }
  959. }
  960. void SourceFileWindow::findFunctions_functionListBox_selected(int index)
  961. {
  962. if (index>=0) {
  963. int line=THIS->sourceFileFunctions[index].implementationLine>=0
  964. ?THIS->sourceFileFunctions[index].implementationLine
  965. :THIS->sourceFileFunctions[index].prototypeLine;
  966. CURRENT_VIEW->setCursorPositionReal(line,0);
  967. THIS->functionDialog->accept();
  968. }
  969. }
  970. void SourceFileWindow::findFunctions_prototypeButton_clicked()
  971. {
  972. int index=THIS->functionDialog->functionListBox->currentItem();
  973. if (index>=0 && THIS->sourceFileFunctions[index].prototypeLine>=0) {
  974. CURRENT_VIEW->setCursorPositionReal(
  975. THIS->sourceFileFunctions[index].prototypeLine,0);
  976. THIS->functionDialog->accept();
  977. }
  978. }
  979. void SourceFileWindow::findFunctions_implementationButton_clicked()
  980. {
  981. int index=THIS->functionDialog->functionListBox->currentItem();
  982. if (index>=0 && THIS->sourceFileFunctions[index].implementationLine>=0) {
  983. CURRENT_VIEW->setCursorPositionReal(
  984. THIS->sourceFileFunctions[index].implementationLine,0);
  985. THIS->functionDialog->accept();
  986. }
  987. }
  988. void SourceFileWindow::findFunctionsPopup_aboutToShow()
  989. {
  990. THIS->findFunctionsPopup->clear();
  991. THIS->sourceFileFunctions=getFunctions(CURRENT_VIEW->getDoc()->text(),
  992. THIS->isASMFile);
  993. int idx=0;
  994. for (SourceFileFunctions::Iterator it=THIS->sourceFileFunctions.begin();
  995. it!=THIS->sourceFileFunctions.end(); ++it,++idx)
  996. THIS->findFunctionsPopup->insertItem((*it).name,idx);
  997. }
  998. void SourceFileWindow::findFunctionsPopup_aboutToHide()
  999. {
  1000. QTimer::singleShot(0,this,SLOT(findFunctionsPopup_aboutToHide_async()));
  1001. }
  1002. void SourceFileWindow::findFunctionsPopup_aboutToHide_async()
  1003. {
  1004. THIS->findFunctionsPopup->clear();
  1005. }
  1006. void SourceFileWindow::findFunctionsPopup_activated(int id)
  1007. {
  1008. int line=THIS->sourceFileFunctions[id].implementationLine>=0
  1009. ?THIS->sourceFileFunctions[id].implementationLine
  1010. :THIS->sourceFileFunctions[id].prototypeLine;
  1011. CURRENT_VIEW->setCursorPositionReal(line,0);
  1012. }
  1013. void SourceFileWindow::findOpenFileAtCursor()
  1014. {
  1015. unsigned line,col,i;
  1016. CURRENT_VIEW->cursorPositionReal(&line,&col);
  1017. QString textLine=CURRENT_VIEW->getDoc()->textLine(line);
  1018. unsigned l=textLine.length();
  1019. bool quotesInLine=textLine.contains("\"");
  1020. QString fileName;
  1021. for (i=col;i<l;i--) {
  1022. QChar c=textLine[i];
  1023. if (!((quotesInLine && c==' ') || (c>='A' && c<="Z") || (c>='a' && c<='z')
  1024. || (c>='0' && c<='9') || QString("_-./\\:").contains(c)))
  1025. break;
  1026. fileName.prepend(c);
  1027. }
  1028. for (i=col+1;i<l;i++) {
  1029. QChar c=textLine[i];
  1030. if (!((quotesInLine && c==' ') || (c>='A' && c<="Z") || (c>='a' && c<='z')
  1031. || (c>='0' && c<='9') || QString("_-./\\:").contains(c)))
  1032. break;
  1033. fileName.append(c);
  1034. }
  1035. THIS->mainForm->findAndOpenFile(fileName,THIS->category);
  1036. }
  1037. void SourceFileWindow::findFindSymbolDeclaration()
  1038. {
  1039. QString fileText=CURRENT_VIEW->getDoc()->text();
  1040. // "Find symbol declaration" only operates on C files.
  1041. if (THIS->isCFile) {
  1042. QString fileName=THIS->fileName;
  1043. QString symbolFile;
  1044. unsigned symbolLine;
  1045. bool systemHeader;
  1046. if (findSymbolInFile(CURRENT_VIEW->currentWord(),fileText,fileName,
  1047. THIS->mainForm,symbolFile,symbolLine,systemHeader)
  1048. && !symbolFile.isNull()) {
  1049. if (symbolFile==fileName)
  1050. CURRENT_VIEW->setCursorPositionReal(symbolLine,0);
  1051. else {
  1052. THIS->mainForm->openHeader(symbolFile,systemHeader,symbolLine);
  1053. if (!systemHeader)
  1054. KWin::activateWindow(THIS->mainForm->winId());
  1055. }
  1056. }
  1057. }
  1058. }
  1059. void SourceFileWindow::updateSizes()
  1060. {
  1061. int rightStatusSize=size().width();
  1062. THIS->rowStatusLabel->setMaximumWidth(30);
  1063. THIS->colStatusLabel->setMaximumWidth(30);
  1064. THIS->charsStatusLabel->setMaximumWidth(100);
  1065. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  1066. }
  1067. void SourceFileWindow::resizeEvent(QResizeEvent *event)
  1068. {
  1069. QMainWindow::resizeEvent(event);
  1070. if (event->size()==event->oldSize()) return;
  1071. updateSizes();
  1072. }
  1073. void SourceFileWindow::statusBar_messageChanged(const QString & message)
  1074. {
  1075. if (message.isNull())
  1076. // Make sure no labels which should be hidden are shown.
  1077. updateRightStatusLabel();
  1078. }
  1079. void SourceFileWindow::updateRightStatusLabel()
  1080. {
  1081. int rightStatusSize=size().width();
  1082. unsigned int line, col;
  1083. CURRENT_VIEW->cursorPositionReal(&line,&col);
  1084. THIS->rowStatusLabel->setMaximumWidth(30);
  1085. THIS->rowStatusLabel->setText(QString("%1").arg(line+1));
  1086. THIS->colStatusLabel->setMaximumWidth(30);
  1087. THIS->colStatusLabel->setText(QString("%1").arg(col+1));
  1088. THIS->charsStatusLabel->setMaximumWidth(100);
  1089. THIS->charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()));
  1090. THIS->rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  1091. THIS->rightStatusLabel->setText(THIS->fileName);
  1092. }
  1093. void SourceFileWindow::current_view_cursorPositionChanged()
  1094. {
  1095. if (CURRENT_VIEW && !disableViewEvents) {
  1096. unsigned int line, col;
  1097. CURRENT_VIEW->cursorPositionReal(&line,&col);
  1098. THIS->rowStatusLabel->setText(QString("%1").arg(line+1));
  1099. THIS->colStatusLabel->setText(QString("%1").arg(col+1));
  1100. }
  1101. }
  1102. void SourceFileWindow::current_view_textChanged()
  1103. {
  1104. if (disableViewEvents) return;
  1105. if (CURRENT_VIEW) {
  1106. THIS->charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->getDoc()->text().length()));
  1107. if (projectCompletion.contains(THIS->fileName))
  1108. projectCompletion[THIS->fileName].dirty=TRUE;
  1109. if (preferences.deleteOverwrittenErrors) MainForm::deleteOverwrittenErrorsIn(THIS);
  1110. }
  1111. if (THIS->kreplace) THIS->kreplace->invalidateSelection();
  1112. }
  1113. void SourceFileWindow::current_view_undoChanged()
  1114. {
  1115. if (CURRENT_VIEW && !disableViewEvents) {
  1116. editUndoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->undoCount()));
  1117. editRedoAction->setEnabled(!!(CURRENT_VIEW->getDoc()->redoCount()));
  1118. THIS->accel->setItemEnabled(0,!!(CURRENT_VIEW->getDoc()->undoCount()));
  1119. THIS->accel->setItemEnabled(1,!!(CURRENT_VIEW->getDoc()->redoCount()));
  1120. }
  1121. }
  1122. void SourceFileWindow::current_view_selectionChanged()
  1123. {
  1124. if (CURRENT_VIEW && !disableViewEvents) {
  1125. editClearAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  1126. editCutAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  1127. editCopyAction->setEnabled(CURRENT_VIEW->getDoc()->hasSelection());
  1128. THIS->accel->setItemEnabled(2,CURRENT_VIEW->getDoc()->hasSelection());
  1129. THIS->accel->setItemEnabled(3,CURRENT_VIEW->getDoc()->hasSelection());
  1130. }
  1131. }
  1132. void SourceFileWindow::current_view_charactersInteractivelyInserted(int line, int col, const QString &characters)
  1133. {
  1134. if (CURRENT_VIEW) {
  1135. if (preferences.autoBlocks && characters=="{"
  1136. && col==CURRENT_VIEW->getDoc()->lineLength(line)-1) {
  1137. Kate::Document *doc=CURRENT_VIEW->getDoc();
  1138. QString fileText=doc->text();
  1139. // Only for C files.
  1140. if (THIS->isCFile) {
  1141. QString indent=doc->textLine(line);
  1142. // Only if the line was all whitespace, otherwise wait for Enter to be
  1143. // pressed (prevents annoying the user while typing a string or something).
  1144. if (indent.contains(QRegExp("^\\s*\\{$"))) {
  1145. indent=indent.remove('{');
  1146. QString cursorLine=indent+"\t";
  1147. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  1148. editExt->editBegin();
  1149. doc->insertLine(line+1,cursorLine);
  1150. doc->insertLine(line+2,indent+"}");
  1151. editExt->editEnd();
  1152. CURRENT_VIEW->setCursorPositionReal(line+1,cursorLine.length());
  1153. }
  1154. }
  1155. }
  1156. // Completion only operates on C files.
  1157. if (characters=="(" && THIS->isCFile)
  1158. new ArgHintPopup(CURRENT_VIEW,THIS->fileName,THIS->mainForm);
  1159. }
  1160. }
  1161. void SourceFileWindow::current_view_newLineHook()
  1162. {
  1163. unsigned line,col;
  1164. CURRENT_VIEW->cursorPositionReal(&line,&col);
  1165. Kate::Document *doc=CURRENT_VIEW->getDoc();
  1166. if (preferences.autoBlocks && line && doc->textLine(line-1).endsWith("{")) {
  1167. QString fileText=doc->text();
  1168. // Only for C files.
  1169. if (THIS->isCFile) {
  1170. QString indent=doc->textLine(line-1);
  1171. // Remove everything starting from the first non-whitespace character.
  1172. indent=indent.remove(QRegExp("(?!\\s).*$"));
  1173. QString cursorLine=indent+"\t";
  1174. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
  1175. editExt->editBegin();
  1176. doc->insertLine(line,cursorLine);
  1177. doc->insertText(line+1,0,indent+"}");
  1178. editExt->editEnd();
  1179. CURRENT_VIEW->setCursorPositionReal(line,cursorLine.length());
  1180. }
  1181. }
  1182. }
  1183. void SourceFileWindow::clipboard_dataChanged()
  1184. {
  1185. if (CURRENT_VIEW) {
  1186. editPasteAction->setEnabled(!clipboard->text().isNull());
  1187. THIS->accel->setItemEnabled(4,!clipboard->text().isNull());
  1188. }
  1189. }
  1190. void SourceFileWindow::closeEvent(QCloseEvent *e)
  1191. {
  1192. if (!fileCloseAction->isEnabled() || savePrompt())
  1193. e->ignore();
  1194. else {
  1195. e->accept();
  1196. deleteLater();
  1197. }
  1198. }
  1199. void SourceFileWindow::KDirWatch_dirty(const QString &fileName)
  1200. {
  1201. if (!fileName.compare(THIS->fileName)) {
  1202. if (KMessageBox::questionYesNo(this,
  1203. QString("The file \'%1\' has been changed by another program. "
  1204. "Do you want to reload it?").arg(fileName),"File Changed")
  1205. ==KMessageBox::Yes) {
  1206. QString fileText=loadFileText(fileName);
  1207. if (fileText.isNull()) {
  1208. KMessageBox::error(this,QString("Can't open \'%1\'").arg(fileName));
  1209. return;
  1210. }
  1211. SET_TEXT_SAFE(CURRENT_VIEW->getDoc(),fileText);
  1212. CURRENT_VIEW->getDoc()->setModified(FALSE);
  1213. CURRENT_VIEW->getDoc()->clearUndo();
  1214. CURRENT_VIEW->getDoc()->clearRedo();
  1215. updateRightStatusLabel();
  1216. }
  1217. }
  1218. }