srcfilewin.cpp 52 KB

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