srcfilewin.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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("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",KIconLoader::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<12; 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. newView->document()->setHighlightingMode(hlModeName);
  482. // Set options.
  483. KTextEditor::ConfigInterface *configiface
  484. =qobject_cast<KTextEditor::ConfigInterface*>(newView);
  485. configiface->setConfigValue("dynamic-word-wrap",false);
  486. if (preferences.removeTrailingSpaces) {
  487. sendCommand(newView,"set-remove-trailing-space 1");
  488. sendCommand(newView,"set-remove-trailing-space-save 1");
  489. } else {
  490. sendCommand(newView,"set-remove-trailing-space 0");
  491. sendCommand(newView,"set-remove-trailing-space-save 0");
  492. }
  493. setTabWidth(newView,tabWidth);
  494. connect(newView,SIGNAL(cursorPositionChanged(KTextEditor::View*,const KTextEditor::Cursor&)),this,SLOT(current_view_cursorPositionChanged(KTextEditor::View*,const KTextEditor::Cursor&)));
  495. connect(newView,SIGNAL(textInserted(KTextEditor::View*,const KTextEditor::Cursor&,const QString&)),
  496. this,SLOT(current_view_textInserted(KTextEditor::View*,const KTextEditor::Cursor&,const QString&)));
  497. connect(newView,SIGNAL(selectionChanged(KTextEditor::View*)),this,SLOT(current_view_selectionChanged(KTextEditor::View*)));
  498. connect(newView->document(),SIGNAL(textChanged(KTextEditor::Document*)),this,SLOT(current_view_textChanged(KTextEditor::Document*)));
  499. connect(newView->document(),SIGNAL(undoChanged()),this,SLOT(current_view_undoChanged()));
  500. newView->setContextMenu(te_popup);
  501. newView->setCursorPosition(KTextEditor::Cursor(0,0));
  502. newView->action(KStandardAction::name(KStandardAction::Find))->setShortcut(QKeySequence());
  503. return newView;
  504. }
  505. // Returns 1 if the source file should not be closed, 0 if it can be closed.
  506. int SourceFileWindow::savePrompt(void)
  507. {
  508. int result;
  509. if (!CURRENT_VIEW) return 0;
  510. while (CURRENT_VIEW->document()->isModified()) { // "while" in case saving fails!
  511. 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());
  512. if (result==KMessageBox::Yes)
  513. fileSave();
  514. else if (result==KMessageBox::No)
  515. return 0;
  516. else
  517. return 1;
  518. }
  519. return 0;
  520. }
  521. void SourceFileWindow::removeTrailingSpacesFromView(void *view)
  522. {
  523. if (!preferences.removeTrailingSpaces) return;
  524. KTextEditor::View *kateView=reinterpret_cast<KTextEditor::View *>(view);
  525. KTextEditor::Document *doc=kateView->document();
  526. doc->startEditing();
  527. unsigned numLines=doc->lines();
  528. for (unsigned i=0; i<numLines; i++) {
  529. QString line=doc->line(i);
  530. int whitespace=line.find(QRegExp("\\s+$"));
  531. if (whitespace>=0) doc->removeText(KTextEditor::Range(i,whitespace,i,line.length()));
  532. }
  533. doc->endEditing();
  534. }
  535. void SourceFileWindow::fileSave()
  536. {
  537. dirWatch->removeFile(fileName);
  538. if (!CURRENT_VIEW->document()->save()) {
  539. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(fileName));
  540. dirWatch->addFile(fileName);
  541. } else {
  542. dirWatch->addFile(fileName);
  543. removeTrailingSpacesFromView(CURRENT_VIEW);
  544. CURRENT_VIEW->document()->setModified(FALSE);
  545. }
  546. }
  547. void SourceFileWindow::fileSaveAs()
  548. {
  549. QString saveFileName=MainForm::SGetFileName(KFileDialog::Saving,
  550. (fileName.endsWith(".h")?TIGCC_H_Filter TIGCCAllFilter:
  551. fileName.endsWith(".c")?TIGCC_C_Filter TIGCCAllFilter:
  552. fileName.endsWith(".s")?TIGCC_S_Filter TIGCCAllFilter:
  553. fileName.endsWith(".asm")?TIGCC_ASM_Filter TIGCCAllFilter:
  554. fileName.endsWith(".qll")?TIGCC_QLL_Filter TIGCCAllFilter:
  555. fileName.endsWith(".txt")?TIGCC_TXT_Filter TIGCCAllFilter:
  556. TIGCCAllFilter),"Save Source File",this);
  557. if (saveFileName.isEmpty())
  558. return;
  559. dirWatch->removeFile(fileName);
  560. mkdir_multi(saveFileName);
  561. if (!CURRENT_VIEW->document()->saveAs(saveFileName)) {
  562. KMessageBox::error(this,QString("Can't save to \'%1\'").arg(saveFileName));
  563. dirWatch->addFile(fileName);
  564. } else {
  565. fileName=saveFileName;
  566. dirWatch->addFile(saveFileName);
  567. removeTrailingSpacesFromView(CURRENT_VIEW);
  568. CURRENT_VIEW->document()->setModified(FALSE);
  569. updateRightStatusLabel();
  570. }
  571. }
  572. void SourceFileWindow::fileAddToProject()
  573. {
  574. mainForm->adoptSourceFile(this);
  575. }
  576. void SourceFileWindow::fileCompile()
  577. {
  578. mainForm->compileSourceFile(this);
  579. }
  580. void SourceFileWindow::filePrint()
  581. {
  582. if (CURRENT_VIEW) CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Print))->trigger();
  583. }
  584. void SourceFileWindow::filePrintQuickly()
  585. {
  586. // This still shows the print dialog, but then KDE 3.5 Kate did that too
  587. // despite having 2 nominally different APIs (print and printDialog).
  588. if (CURRENT_VIEW) CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Print))->trigger();
  589. }
  590. void SourceFileWindow::applyPreferences()
  591. {
  592. // Apply the KatePart preferences and treeview icons.
  593. KParts::Factory *factory=(KParts::Factory *)
  594. KLibLoader::self()->factory("katepart");
  595. if (!factory) qFatal("Failed to load KatePart");
  596. KTextEditor::Document *doc=(KTextEditor::Document *)
  597. factory->createPart(0,this,"KTextEditor::Document");
  598. KTextEditor::Editor *editor=doc->editor();
  599. int numConfigPages=editor->configPages();
  600. for (int i=0; i<numConfigPages; i++) {
  601. if (editor->configPageName(i)=="Fonts & Colors") {
  602. KTextEditor::ConfigPage *configPage=editor->configPage(i,this);
  603. configPage->apply();
  604. delete configPage;
  605. break;
  606. }
  607. }
  608. delete doc;
  609. KTextEditor::View *kateView=CURRENT_VIEW;
  610. if (kateView) {
  611. QString fileText=kateView->document()->text();
  612. if (preferences.removeTrailingSpaces) {
  613. sendCommand(kateView,"set-remove-trailing-space 1");
  614. sendCommand(kateView,"set-remove-trailing-space-save 1");
  615. } else {
  616. sendCommand(kateView,"set-remove-trailing-space 0");
  617. sendCommand(kateView,"set-remove-trailing-space-save 0");
  618. }
  619. setTabWidth(kateView,isASMFile?preferences.tabWidthAsm:
  620. isCFile?preferences.tabWidthC:8);
  621. // Kate seems really insisting on making it a pain to update syntax highlighting settings.
  622. kateView->document()->setHighlightingMode(HL_MODE);
  623. // Force redrawing to get the tab width right, repaint() is ignored for some reason.
  624. kateView->hide();
  625. kateView->show();
  626. }
  627. // Apply the icon preferences.
  628. if (preferences.useSystemIcons) {
  629. // Set the preferred icon size so system toolbar icons don't get annoying
  630. // padding.
  631. int toolbarIconSize=KIconLoader().currentSize(KIconLoader::MainToolbar);
  632. setIconSize(QSize(toolbarIconSize,toolbarIconSize));
  633. fileSaveAction->setIcon(KIcon("document-save"));
  634. fileAddToProjectAction->setIcon(KIcon("edit-add"));
  635. fileCompileAction->setIcon(KIcon("compfile"));
  636. filePrintAction->setIcon(KIcon("document-print"));
  637. filePrintQuicklyAction->setIcon(KIcon("document-print"));
  638. editClearAction->setIcon(KIcon("edit-delete"));
  639. editCutAction->setIcon(KIcon("edit-cut"));
  640. editCopyAction->setIcon(KIcon("edit-copy"));
  641. editPasteAction->setIcon(KIcon("edit-paste"));
  642. findFindAction->setIcon(KIcon("edit-find"));
  643. if (KIconLoader::global()->iconPath("edit-find-replace",KIconLoader::Small,TRUE).isEmpty())
  644. findReplaceAction->setIcon(KIcon("file-find"));
  645. else
  646. findReplaceAction->setIcon(KIcon("edit-find-replace"));
  647. editUndoAction->setIcon(KIcon("edit-undo"));
  648. editRedoAction->setIcon(KIcon("edit-redo"));
  649. findFunctionsAction->setIcon(KIcon("view-tree"));
  650. editIncreaseIndentAction->setIcon(KIcon("format-indent-more"));
  651. editDecreaseIndentAction->setIcon(KIcon("format-indent-less"));
  652. } else {
  653. setIconSize(QSize(20,20));
  654. fileSaveAction->setIcon(QIcon(QPixmap(":/images/02.png")));
  655. fileAddToProjectAction->setIcon(QIcon(QPixmap(":/images/08.png")));
  656. fileCompileAction->setIcon(QIcon(QPixmap(":/images/09.png")));
  657. filePrintAction->setIcon(QIcon(QPixmap(":/images/03.png")));
  658. filePrintQuicklyAction->setIcon(QIcon(QPixmap(":/images/03.png")));
  659. editClearAction->setIcon(QIcon(QPixmap(":/images/04.png")));
  660. editCutAction->setIcon(QIcon(QPixmap(":/images/05.png")));
  661. editCopyAction->setIcon(QIcon(QPixmap(":/images/06.png")));
  662. editPasteAction->setIcon(QIcon(QPixmap(":/images/07.png")));
  663. findFindAction->setIcon(QIcon(QPixmap(":/images/13.png")));
  664. findReplaceAction->setIcon(QIcon(QPixmap(":/images/14.png")));
  665. editUndoAction->setIcon(QIcon(QPixmap(":/images/16.png")));
  666. editRedoAction->setIcon(QIcon(QPixmap(":/images/17.png")));
  667. findFunctionsAction->setIcon(QIcon(QPixmap(":/images/18.png")));
  668. editIncreaseIndentAction->setIcon(QIcon(QPixmap(":/images/19.png")));
  669. editDecreaseIndentAction->setIcon(QIcon(QPixmap(":/images/20.png")));
  670. }
  671. }
  672. void SourceFileWindow::editUndo()
  673. {
  674. if (CURRENT_VIEW)
  675. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Undo))->trigger();
  676. }
  677. void SourceFileWindow::editRedo()
  678. {
  679. if (CURRENT_VIEW)
  680. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Redo))->trigger();
  681. }
  682. void SourceFileWindow::editClear()
  683. {
  684. if (CURRENT_VIEW)
  685. CURRENT_VIEW->removeSelectionText();
  686. }
  687. void SourceFileWindow::editCut()
  688. {
  689. if (CURRENT_VIEW)
  690. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Cut))->trigger();
  691. }
  692. void SourceFileWindow::editCopy()
  693. {
  694. if (CURRENT_VIEW)
  695. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Copy))->trigger();
  696. }
  697. void SourceFileWindow::editPaste()
  698. {
  699. if (CURRENT_VIEW)
  700. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::PasteText))->trigger();
  701. }
  702. void SourceFileWindow::editSelectAll()
  703. {
  704. if (CURRENT_VIEW)
  705. CURRENT_VIEW->action(KStandardAction::name(KStandardAction::SelectAll))->trigger();
  706. }
  707. void SourceFileWindow::editIncreaseIndent()
  708. {
  709. if (CURRENT_VIEW)
  710. CURRENT_VIEW->action("tools_indent")->trigger();
  711. }
  712. void SourceFileWindow::editDecreaseIndent()
  713. {
  714. if (CURRENT_VIEW)
  715. CURRENT_VIEW->action("tools_unindent")->trigger();
  716. }
  717. void SourceFileWindow::findFind()
  718. {
  719. if (kfinddialog)
  720. ACTIVATE_WINDOW(kfinddialog->winId());
  721. else {
  722. // Never set hasSelection because finding in selection doesn't really make
  723. // sense with my non-modal find dialog setup.
  724. kfinddialog=new KFindDialog(this,KFind::FromCursor,findHistory);
  725. kfinddialog->setModal(false);
  726. connect(kfinddialog, SIGNAL(okClicked()), this, SLOT(findFind_next()));
  727. connect(kfinddialog, SIGNAL(cancelClicked()), this, SLOT(findFind_stop()));
  728. kfinddialog->show();
  729. }
  730. }
  731. void SourceFileWindow::findFind_next()
  732. {
  733. // Use a local KFind object. The search will need to be restarted next time
  734. // this function is called because of the non-modality of the find dialog.
  735. KFind *kfind=new KFind(kfinddialog->pattern(),kfinddialog->options(),this,kfinddialog);
  736. // Initialize.
  737. bool findBackwards=!!(kfinddialog->options()&KFind::FindBackwards);
  738. int findCurrentCol;
  739. kfind=new KFind(kfinddialog->pattern(),kfinddialog->options(),this,kfinddialog);
  740. kfind->closeFindNextDialog(); // don't use this, a non-modal KFindDialog is used instead
  741. connect(kfind,SIGNAL(highlight(const QString &,int,int)),
  742. this,SLOT(findFind_highlight(const QString &,int,int)));
  743. if (kfinddialog->options()&KFind::FromCursor) {
  744. if (CURRENT_VIEW->selection()) {
  745. if (findBackwards) {
  746. CURRENT_VIEW->selectionRange().start().position(findCurrentLine,findCurrentCol);
  747. if ((--findCurrentCol)==-1) {
  748. if (!findCurrentLine) goto skip_data;
  749. findCurrentLine--;
  750. }
  751. } else {
  752. CURRENT_VIEW->selectionRange().end().position(findCurrentLine,findCurrentCol);
  753. }
  754. } else {
  755. CURRENT_VIEW->cursorPosition().position(findCurrentLine,findCurrentCol);
  756. }
  757. } else {
  758. findCurrentLine=findBackwards?(CURRENT_VIEW->document()->lines()-1):0;
  759. findCurrentCol=-1;
  760. }
  761. kfind->setData(CURRENT_VIEW->document()->line(findCurrentLine),findCurrentCol);
  762. skip_data:;
  763. // Now find the next occurrence.
  764. KFind::Result result;
  765. KTextEditor::View *currView=CURRENT_VIEW;
  766. int currNumLines=CURRENT_VIEW->document()->lines();
  767. do {
  768. if (kfind->needData()) {
  769. if (findBackwards?!findCurrentLine:(findCurrentLine>=currNumLines)) {
  770. // Try restarting the search.
  771. currNumLines=currView->document()->lines();
  772. findCurrentLine=findBackwards?currNumLines-1:0;
  773. do {
  774. if (kfind->needData()) {
  775. if (findBackwards?!findCurrentLine:(findCurrentLine>=currNumLines))
  776. goto not_found_current;
  777. if (findBackwards) findCurrentLine--; else findCurrentLine++;
  778. kfind->setData(currView->document()->line(findCurrentLine));
  779. }
  780. result=kfind->find();
  781. } while (result==KFind::NoMatch);
  782. break;
  783. not_found_current:
  784. KMessageBox::error(this,QString("Text \'%1\' not found").arg(kfinddialog->pattern()));
  785. delete kfind;
  786. return;
  787. } else if (findBackwards) findCurrentLine--; else findCurrentLine++;
  788. kfind->setData(currView->document()->line(findCurrentLine));
  789. }
  790. result=kfind->find();
  791. } while (result==KFind::NoMatch);
  792. delete kfind;
  793. }
  794. void SourceFileWindow::findFind_highlight(const QString &text __attribute__((unused)), int matchingindex, int matchedlength)
  795. {
  796. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(findCurrentLine,matchingindex+matchedlength));
  797. CURRENT_VIEW->setSelection(KTextEditor::Range(findCurrentLine,matchingindex,
  798. findCurrentLine,matchingindex+matchedlength));
  799. }
  800. void SourceFileWindow::findFind_stop()
  801. {
  802. if (kfinddialog) {
  803. findHistory=kfinddialog->findHistory();
  804. kfinddialog->deleteLater();
  805. }
  806. kfinddialog=static_cast<KFindDialog *>(NULL);
  807. }
  808. void SourceFileWindow::findReplace()
  809. {
  810. if (kreplace) {
  811. KDialog *replaceNextDialog=kreplace->replaceNextDialog();
  812. if (replaceNextDialog)
  813. ACTIVATE_WINDOW(replaceNextDialog->winId());
  814. return;
  815. }
  816. KReplaceDialog kreplacedialog(this,((CURRENT_VIEW&&CURRENT_VIEW->selection()
  817. &&!CURRENT_VIEW->selectionRange().onSingleLine())?
  818. KFind::SelectedText:0)|KFind::FromCursor,
  819. findHistory,replacementHistory,
  820. CURRENT_VIEW&&CURRENT_VIEW->selection());
  821. if (kreplacedialog.exec()!=QDialog::Accepted)
  822. return;
  823. findHistory=kreplacedialog.findHistory();
  824. replacementHistory=kreplacedialog.replacementHistory();
  825. kreplace=new KReplaceWithSelectionS(kreplacedialog.pattern(),kreplacedialog.replacement(),
  826. kreplacedialog.options(),this);
  827. // Connect signals to code which handles highlighting of found text, and
  828. // on-the-fly replacement.
  829. connect(kreplace,SIGNAL(highlight(const QString &,int,int)),
  830. this,SLOT(findReplace_highlight(const QString &,int,int)));
  831. // Connect findNext signal - called when pressing the button in the dialog.
  832. connect(kreplace,SIGNAL(findNext()),this,SLOT(findReplace_next()));
  833. // Connect replace signal - called when doing a replacement.
  834. connect(kreplace,SIGNAL(replace(const QString &,int,int,int)),
  835. this,SLOT(findReplace_replace(const QString &,int,int,int)));
  836. // Connect dialogClosed signal - called when closing the Replace Next dialog.
  837. connect(kreplace,SIGNAL(dialogClosed()),this,SLOT(findReplace_stop()));
  838. // Initialize.
  839. bool findBackwards=!!(kreplace->options()&KFind::FindBackwards);
  840. int replaceCurrentCol;
  841. if (CURRENT_VIEW) {
  842. if (kreplace->options()&KFind::SelectedText) {
  843. kreplace->setSelection(CURRENT_VIEW->selectionRange());
  844. if (findBackwards) {
  845. kreplace->replaceCurrentLine=kreplace->selEndLine();
  846. replaceCurrentCol=kreplace->selEndCol();
  847. } else {
  848. kreplace->replaceCurrentLine=kreplace->selStartLine();
  849. replaceCurrentCol=kreplace->selStartCol();
  850. }
  851. kreplace->setOptions(kreplace->options()&~KFind::FromCursor);
  852. } else if (kreplace->options()&KFind::FromCursor) {
  853. if (CURRENT_VIEW->selection()) {
  854. if (findBackwards) {
  855. CURRENT_VIEW->selectionRange().start().position(kreplace->replaceCurrentLine,
  856. replaceCurrentCol);
  857. if ((--replaceCurrentCol)==-1) {
  858. if (!kreplace->replaceCurrentLine) goto skip_data;
  859. kreplace->replaceCurrentLine--;
  860. }
  861. } else {
  862. CURRENT_VIEW->selectionRange().end().position(kreplace->replaceCurrentLine,
  863. replaceCurrentCol);
  864. }
  865. } else {
  866. CURRENT_VIEW->cursorPosition().position(kreplace->replaceCurrentLine,replaceCurrentCol);
  867. // Don't prompt for restarting if we actually searched the entire document.
  868. if (findBackwards?(kreplace->replaceCurrentLine==(CURRENT_VIEW->document()->lines()-1)
  869. && replaceCurrentCol==(CURRENT_VIEW->document()->lineLength(kreplace->replaceCurrentLine)))
  870. :(!kreplace->replaceCurrentLine&&!replaceCurrentCol))
  871. kreplace->setOptions(kreplace->options()&~KFind::FromCursor);
  872. }
  873. } else {
  874. kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->document()->lines()-1):0;
  875. replaceCurrentCol=-1;
  876. }
  877. kreplace->setData(CURRENT_VIEW->document()->line(kreplace->replaceCurrentLine),replaceCurrentCol);
  878. }
  879. skip_data:
  880. // Now find the next occurrence.
  881. findReplace_next(TRUE);
  882. }
  883. void SourceFileWindow::findReplace_next()
  884. {
  885. findReplace_next(FALSE);
  886. }
  887. void SourceFileWindow::findReplace_next(bool firstTime)
  888. {
  889. bool findBackwards=!!(kreplace->options()&KFind::FindBackwards);
  890. // Reinitialize.
  891. if (!firstTime) {
  892. int replaceCurrentCol;
  893. // Non-first-time always continues from cursor.
  894. if (CURRENT_VIEW->selection()) {
  895. if (findBackwards) {
  896. CURRENT_VIEW->selectionRange().start().position(kreplace->replaceCurrentLine,
  897. replaceCurrentCol);
  898. if ((--replaceCurrentCol)==-1) {
  899. if (!kreplace->replaceCurrentLine) goto skip_data;
  900. kreplace->replaceCurrentLine--;
  901. }
  902. } else {
  903. CURRENT_VIEW->selectionRange().end().position(kreplace->replaceCurrentLine,
  904. replaceCurrentCol);
  905. }
  906. } else {
  907. CURRENT_VIEW->cursorPosition().position(kreplace->replaceCurrentLine,
  908. replaceCurrentCol);
  909. }
  910. kreplace->setData(CURRENT_VIEW->document()->line(kreplace->replaceCurrentLine),replaceCurrentCol);
  911. }
  912. skip_data:;
  913. // Now find the next occurrence.
  914. KFind::Result result;
  915. KTextEditor::View *currView=CURRENT_VIEW;
  916. int currNumLines=0;
  917. if (CURRENT_VIEW) currNumLines=CURRENT_VIEW->document()->lines();
  918. do {
  919. if (kreplace->needData()) {
  920. if (kreplace->haveSelection()
  921. ?(findBackwards?(kreplace->replaceCurrentLine<=kreplace->selStartLine())
  922. :(kreplace->replaceCurrentLine>=kreplace->selEndLine()))
  923. :(findBackwards?!kreplace->replaceCurrentLine:(kreplace->replaceCurrentLine>=currNumLines))) {
  924. if (kreplace->shouldRestart()) {
  925. // Drop "From cursor" and "Selected text" options.
  926. kreplace->setOptions(kreplace->options()&~(KFind::FromCursor
  927. |KFind::SelectedText));
  928. kreplace->invalidateSelection();
  929. // Reinitialize.
  930. kreplace->replaceCurrentLine=findBackwards?(CURRENT_VIEW->document()->lines()-1):0;
  931. kreplace->setData(CURRENT_VIEW->document()->line(kreplace->replaceCurrentLine));
  932. // Start again as if it was the first time.
  933. findReplace_next(TRUE);
  934. return;
  935. } else {
  936. findReplace_stop();
  937. return;
  938. }
  939. } else if (findBackwards) kreplace->replaceCurrentLine--; else kreplace->replaceCurrentLine++;
  940. kreplace->setData(currView->document()->line(kreplace->replaceCurrentLine));
  941. }
  942. result=kreplace->replace();
  943. } while (result==KFind::NoMatch);
  944. }
  945. void SourceFileWindow::findReplace_highlight(const QString &text __attribute__((unused)), int matchingindex, int matchedlength)
  946. {
  947. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(kreplace->replaceCurrentLine,matchingindex+matchedlength));
  948. CURRENT_VIEW->setSelection(KTextEditor::Range(kreplace->replaceCurrentLine,matchingindex,
  949. kreplace->replaceCurrentLine,matchingindex+matchedlength));
  950. }
  951. void SourceFileWindow::findReplace_replace(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
  952. {
  953. bool update=!!(kreplace->options()&KReplaceDialog::PromptOnReplace);
  954. bool haveSelection=kreplace->haveSelection();
  955. // The initializations are redundant, but g++ doesn't understand this, and the
  956. // self-initialization trick doesn't work either (-Wno-init-self is ignored).
  957. int selStartLine=0, selStartCol=0, selEndLine=0, selEndCol=0;
  958. if (haveSelection) {
  959. selStartLine=kreplace->selStartLine();
  960. selStartCol=kreplace->selStartCol();
  961. selEndLine=kreplace->selEndLine();
  962. selEndCol=kreplace->selEndCol();
  963. }
  964. CURRENT_VIEW->document()->startEditing();
  965. CURRENT_VIEW->document()->insertText(KTextEditor::Cursor(kreplace->replaceCurrentLine,replacementIndex),
  966. text.mid(replacementIndex,replacedLength));
  967. // We can't put the cursor back now because this breaks editBegin/editEnd.
  968. int line,col;
  969. CURRENT_VIEW->cursorPosition().position(line,col);
  970. bool updateCursor=(line==kreplace->replaceCurrentLine
  971. && col==replacementIndex+replacedLength);
  972. CURRENT_VIEW->document()->removeText(KTextEditor::Range(kreplace->replaceCurrentLine,replacementIndex+replacedLength,
  973. kreplace->replaceCurrentLine,replacementIndex+replacedLength+matchedLength));
  974. CURRENT_VIEW->document()->endEditing();
  975. if (updateCursor)
  976. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(kreplace->replaceCurrentLine,replacementIndex));
  977. if (update) {
  978. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(kreplace->replaceCurrentLine,replacementIndex+replacedLength));
  979. CURRENT_VIEW->setSelection(KTextEditor::Range(kreplace->replaceCurrentLine,replacementIndex,
  980. kreplace->replaceCurrentLine,replacementIndex+replacedLength));
  981. CURRENT_VIEW->repaint();
  982. }
  983. if (haveSelection) {
  984. // Restore selection, updating coordinates if necessary.
  985. kreplace->setSelection(selStartLine,selStartCol,selEndLine,
  986. (kreplace->replaceCurrentLine==selEndLine)
  987. ?(selEndCol+replacedLength-matchedLength)
  988. :selEndCol);
  989. }
  990. }
  991. void SourceFileWindow::findReplace_stop()
  992. {
  993. if (kreplace) kreplace->deleteLater();
  994. kreplace=static_cast<KReplaceWithSelectionS *>(NULL);
  995. }
  996. void SourceFileWindow::findFunctions()
  997. {
  998. functionDialog=new FunctionDialog(this);
  999. connect(functionDialog->functionListBox,SIGNAL(highlighted(int)),
  1000. this,SLOT(findFunctions_functionListBox_highlighted(int)));
  1001. connect(functionDialog->functionListBox,SIGNAL(selected(int)),
  1002. this,SLOT(findFunctions_functionListBox_selected(int)));
  1003. connect(functionDialog->prototypeButton,SIGNAL(clicked()),
  1004. this,SLOT(findFunctions_prototypeButton_clicked()));
  1005. connect(functionDialog->implementationButton,SIGNAL(clicked()),
  1006. this,SLOT(findFunctions_implementationButton_clicked()));
  1007. functionDialog->functionListBox->clear();
  1008. sourceFileFunctions=getFunctions(CURRENT_VIEW->document()->text(),
  1009. isASMFile);
  1010. for (SourceFileFunctions::Iterator it=sourceFileFunctions.begin();
  1011. it!=sourceFileFunctions.end(); ++it)
  1012. functionDialog->functionListBox->insertItem((*it).name);
  1013. functionDialog->exec();
  1014. delete functionDialog;
  1015. }
  1016. void SourceFileWindow::findFunctions_functionListBox_highlighted(int index)
  1017. {
  1018. if (index>=0) {
  1019. functionDialog->prototypeButton->setEnabled(
  1020. sourceFileFunctions[index].prototypeLine>=0);
  1021. functionDialog->implementationButton->setEnabled(
  1022. sourceFileFunctions[index].implementationLine>=0);
  1023. } else {
  1024. functionDialog->prototypeButton->setEnabled(FALSE);
  1025. functionDialog->implementationButton->setEnabled(FALSE);
  1026. }
  1027. }
  1028. void SourceFileWindow::findFunctions_functionListBox_selected(int index)
  1029. {
  1030. if (index>=0) {
  1031. int line=sourceFileFunctions[index].implementationLine>=0
  1032. ?sourceFileFunctions[index].implementationLine
  1033. :sourceFileFunctions[index].prototypeLine;
  1034. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(line,0));
  1035. functionDialog->accept();
  1036. }
  1037. }
  1038. void SourceFileWindow::findFunctions_prototypeButton_clicked()
  1039. {
  1040. int index=functionDialog->functionListBox->currentItem();
  1041. if (index>=0 && sourceFileFunctions[index].prototypeLine>=0) {
  1042. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(
  1043. sourceFileFunctions[index].prototypeLine,0));
  1044. functionDialog->accept();
  1045. }
  1046. }
  1047. void SourceFileWindow::findFunctions_implementationButton_clicked()
  1048. {
  1049. int index=functionDialog->functionListBox->currentItem();
  1050. if (index>=0 && sourceFileFunctions[index].implementationLine>=0) {
  1051. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(
  1052. sourceFileFunctions[index].implementationLine,0));
  1053. functionDialog->accept();
  1054. }
  1055. }
  1056. void SourceFileWindow::findFunctionsPopup_aboutToShow()
  1057. {
  1058. findFunctionsPopup->clear();
  1059. sourceFileFunctions=getFunctions(CURRENT_VIEW->document()->text(),
  1060. isASMFile);
  1061. int idx=0;
  1062. for (SourceFileFunctions::Iterator it=sourceFileFunctions.begin();
  1063. it!=sourceFileFunctions.end(); ++it,++idx)
  1064. findFunctionsPopup->insertItem((*it).name,idx);
  1065. }
  1066. void SourceFileWindow::findFunctionsPopup_aboutToHide()
  1067. {
  1068. QTimer::singleShot(0,this,SLOT(findFunctionsPopup_aboutToHide_async()));
  1069. }
  1070. void SourceFileWindow::findFunctionsPopup_aboutToHide_async()
  1071. {
  1072. findFunctionsPopup->clear();
  1073. }
  1074. void SourceFileWindow::findFunctionsPopup_activated(int id)
  1075. {
  1076. int line=sourceFileFunctions[id].implementationLine>=0
  1077. ?sourceFileFunctions[id].implementationLine
  1078. :sourceFileFunctions[id].prototypeLine;
  1079. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(line,0));
  1080. }
  1081. void SourceFileWindow::findOpenFileAtCursor()
  1082. {
  1083. int line,col,i;
  1084. CURRENT_VIEW->cursorPosition().position(line,col);
  1085. QString textLine=CURRENT_VIEW->document()->line(line);
  1086. int l=textLine.length();
  1087. bool quotesInLine=textLine.contains("\"");
  1088. QString fileName;
  1089. for (i=col;i>=0;i--) {
  1090. QChar c=textLine[i];
  1091. if (!((quotesInLine && c==' ') || (c>='A' && c<='Z') || (c>='a' && c<='z')
  1092. || (c>='0' && c<='9') || QString("_-./\\:").contains(c)))
  1093. break;
  1094. fileName.prepend(c);
  1095. }
  1096. for (i=col+1;i<l;i++) {
  1097. QChar c=textLine[i];
  1098. if (!((quotesInLine && c==' ') || (c>='A' && c<='Z') || (c>='a' && c<='z')
  1099. || (c>='0' && c<='9') || QString("_-./\\:").contains(c)))
  1100. break;
  1101. fileName.append(c);
  1102. }
  1103. mainForm->findAndOpenFile(fileName,category);
  1104. }
  1105. void SourceFileWindow::findFindSymbolDeclaration()
  1106. {
  1107. QString fileText=CURRENT_VIEW->document()->text();
  1108. // "Find symbol declaration" only operates on C files.
  1109. if (isCFile) {
  1110. QString fileName=fileName;
  1111. QString symbolFile;
  1112. unsigned symbolLine;
  1113. bool systemHeader;
  1114. int line,col,i;
  1115. CURRENT_VIEW->cursorPosition().position(line,col);
  1116. QString textLine=CURRENT_VIEW->document()->line(line);
  1117. QString wordUnderCursor;
  1118. for (i=col-1;i>=0;i--) {
  1119. QChar c=textLine[i];
  1120. if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')
  1121. || c=='_' || c=='$' || c=='#')
  1122. wordUnderCursor.prepend(c);
  1123. }
  1124. int len=textLine.length();
  1125. for (i=col;i<len;i++) {
  1126. QChar c=textLine[i];
  1127. if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')
  1128. || c=='_' || c=='$' || c=='#')
  1129. wordUnderCursor.append(c);
  1130. }
  1131. if (findSymbolInFile(wordUnderCursor,fileText,fileName,mainForm,
  1132. symbolFile,symbolLine,systemHeader)
  1133. && !symbolFile.isNull()) {
  1134. if (symbolFile==fileName)
  1135. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(symbolLine,0));
  1136. else {
  1137. mainForm->openHeader(symbolFile,systemHeader,symbolLine);
  1138. if (!systemHeader)
  1139. ACTIVATE_WINDOW(mainForm->winId());
  1140. }
  1141. }
  1142. }
  1143. }
  1144. void SourceFileWindow::updateSizes()
  1145. {
  1146. int rightStatusSize=size().width();
  1147. rowStatusLabel->setMaximumWidth(30);
  1148. colStatusLabel->setMaximumWidth(30);
  1149. charsStatusLabel->setMaximumWidth(100);
  1150. rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  1151. }
  1152. void SourceFileWindow::resizeEvent(QResizeEvent *event)
  1153. {
  1154. QMainWindow::resizeEvent(event);
  1155. if (event->size()==event->oldSize()) return;
  1156. updateSizes();
  1157. }
  1158. void SourceFileWindow::statusBar_messageChanged(const QString & message)
  1159. {
  1160. if (message.isNull())
  1161. // Make sure no labels which should be hidden are shown.
  1162. updateRightStatusLabel();
  1163. }
  1164. void SourceFileWindow::updateRightStatusLabel()
  1165. {
  1166. int rightStatusSize=size().width();
  1167. int line, col;
  1168. CURRENT_VIEW->cursorPosition().position(line,col);
  1169. rowStatusLabel->setMaximumWidth(30);
  1170. rowStatusLabel->setText(QString("%1").arg(line+1));
  1171. colStatusLabel->setMaximumWidth(30);
  1172. colStatusLabel->setText(QString("%1").arg(col+1));
  1173. charsStatusLabel->setMaximumWidth(100);
  1174. charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->document()->text().length()));
  1175. rightStatusLabel->setMaximumWidth(rightStatusSize-160>0?rightStatusSize-160:0);
  1176. rightStatusLabel->setText(fileName);
  1177. }
  1178. void SourceFileWindow::current_view_cursorPositionChanged(KTextEditor::View *view, const KTextEditor::Cursor &newPosition)
  1179. {
  1180. if (CURRENT_VIEW==view && !disableViewEvents) {
  1181. int line, col;
  1182. newPosition.position(line,col);
  1183. rowStatusLabel->setText(QString("%1").arg(line+1));
  1184. colStatusLabel->setText(QString("%1").arg(col+1));
  1185. }
  1186. }
  1187. void SourceFileWindow::current_view_textChanged(KTextEditor::Document *document)
  1188. {
  1189. if (disableViewEvents) return;
  1190. if (CURRENT_VIEW && CURRENT_VIEW->document()==document) {
  1191. charsStatusLabel->setText(QString("%1 Characters").arg(CURRENT_VIEW->document()->text().length()));
  1192. if (projectCompletion.contains(fileName))
  1193. projectCompletion[fileName].dirty=TRUE;
  1194. if (preferences.deleteOverwrittenErrors) MainForm::deleteOverwrittenErrorsIn(this);
  1195. }
  1196. if (kreplace) kreplace->invalidateSelection();
  1197. }
  1198. void SourceFileWindow::current_view_undoChanged()
  1199. {
  1200. if (CURRENT_VIEW && !disableViewEvents) {
  1201. editUndoAction->setEnabled(CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Undo))->isEnabled());
  1202. editRedoAction->setEnabled(CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Redo))->isEnabled());
  1203. shortcuts[0]->setEnabled(CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Undo))->isEnabled());
  1204. shortcuts[1]->setEnabled(CURRENT_VIEW->action(KStandardAction::name(KStandardAction::Redo))->isEnabled());
  1205. }
  1206. }
  1207. void SourceFileWindow::current_view_selectionChanged(KTextEditor::View *view)
  1208. {
  1209. if (CURRENT_VIEW==view && !disableViewEvents) {
  1210. editClearAction->setEnabled(view->selection());
  1211. editCutAction->setEnabled(view->selection());
  1212. editCopyAction->setEnabled(view->selection());
  1213. shortcuts[2]->setEnabled(view->selection());
  1214. shortcuts[3]->setEnabled(view->selection());
  1215. }
  1216. }
  1217. void SourceFileWindow::current_view_textInserted(KTextEditor::View *view, const KTextEditor::Cursor &position, const QString &text)
  1218. {
  1219. int line,col;
  1220. position.position(line,col);
  1221. if (preferences.autoBlocks && text=="{"
  1222. && col==view->document()->lineLength(line)-1) {
  1223. KTextEditor::Document *doc=view->document();
  1224. // Only for C files.
  1225. if (isCFile) {
  1226. QString indent=doc->line(line);
  1227. // Only if the line was all whitespace, otherwise wait for Enter to be
  1228. // pressed (prevents annoying the user while typing a string or something).
  1229. if (indent.contains(QRegExp("^\\s*\\{$"))) {
  1230. indent=indent.remove('{');
  1231. QString cursorLine=indent+"\t";
  1232. doc->startEditing();
  1233. doc->insertLine(line+1,cursorLine);
  1234. doc->insertLine(line+2,indent+"}");
  1235. doc->endEditing();
  1236. view->setCursorPosition(KTextEditor::Cursor(line+1,cursorLine.length()));
  1237. }
  1238. }
  1239. }
  1240. // Completion only operates on C files.
  1241. if (text=="(" && isCFile)
  1242. new ArgHintPopup(view,fileName,mainForm);
  1243. }
  1244. void SourceFileWindow::current_view_newLineHook()
  1245. {
  1246. int line,col;
  1247. CURRENT_VIEW->cursorPosition().position(line,col);
  1248. KTextEditor::Document *doc=CURRENT_VIEW->document();
  1249. if (preferences.autoBlocks && line && doc->line(line-1).endsWith("{")) {
  1250. // Only for C files.
  1251. if (isCFile) {
  1252. QString indent=doc->line(line-1);
  1253. // Remove everything starting from the first non-whitespace character.
  1254. indent=indent.remove(QRegExp("(?!\\s).*$"));
  1255. QString cursorLine=indent+"\t";
  1256. doc->startEditing();
  1257. doc->removeText(KTextEditor::Range(line,0,line,col));
  1258. doc->insertLine(line,cursorLine);
  1259. doc->insertText(KTextEditor::Cursor(line+1,0),indent+"}");
  1260. doc->endEditing();
  1261. CURRENT_VIEW->setCursorPosition(KTextEditor::Cursor(line,cursorLine.length()));
  1262. }
  1263. }
  1264. }
  1265. void SourceFileWindow::clipboard_dataChanged()
  1266. {
  1267. if (CURRENT_VIEW) {
  1268. editPasteAction->setEnabled(!clipboard->text().isNull());
  1269. shortcuts[4]->setEnabled(!clipboard->text().isNull());
  1270. }
  1271. }
  1272. void SourceFileWindow::closeEvent(QCloseEvent *e)
  1273. {
  1274. if (!fileCloseAction->isEnabled() || savePrompt())
  1275. e->ignore();
  1276. else {
  1277. e->accept();
  1278. deleteLater();
  1279. }
  1280. }
  1281. void SourceFileWindow::KDirWatch_dirty(const QString &fileName)
  1282. {
  1283. if (!fileName.compare(fileName)) {
  1284. if (KMessageBox::questionYesNo(this,
  1285. QString("The file \'%1\' has been changed by another program. "
  1286. "Do you want to reload it?").arg(fileName),"File Changed")
  1287. ==KMessageBox::Yes) {
  1288. CURRENT_VIEW->document()->setModified(FALSE);
  1289. disableViewEvents=TRUE;
  1290. CURRENT_VIEW->document()->documentReload();
  1291. disableViewEvents=FALSE;
  1292. updateRightStatusLabel();
  1293. }
  1294. }
  1295. }
  1296. void SourceFileWindow::languageChange()
  1297. {
  1298. retranslateUi(this);
  1299. }