srcfilewin.ui.h 52 KB

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