srcfilewin.cpp 53 KB

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