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