srcfilewin.ui.h 52 KB

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