srcfilewin.ui.h 50 KB

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