srcfilewin.cpp 52 KB

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