srcfilewin.cpp 53 KB

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