srcfilewin.ui.h 46 KB

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