srcfilewin.ui.h 52 KB

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