srcfilewin.cpp 52 KB

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