srcfilewin.cpp 54 KB

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