srcfilewin.cpp 52 KB

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