srcfilewin.cpp 52 KB

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