srcfilewin.ui.h 50 KB

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