srcfilewin.cpp 52 KB

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