srcfilewin.cpp 54 KB

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