21 #include "kwritemain.h"
22 #include "kwritemain.moc"
24 #include <kate/document.h>
25 #include <kate/view.h>
27 #include <tdetexteditor/configinterface.h>
28 #include <tdetexteditor/sessionconfiginterface.h>
29 #include <tdetexteditor/viewcursorinterface.h>
30 #include <tdetexteditor/printinterface.h>
31 #include <tdetexteditor/encodinginterface.h>
32 #include <tdetexteditor/editorchooser.h>
33 #include <tdetexteditor/popupmenuinterface.h>
35 #include <tdeio/netaccess.h>
37 #include <tdeversion.h>
38 #include <dcopclient.h>
40 #include <kencodingfiledialog.h>
41 #include <tdediroperator.h>
42 #include <kiconloader.h>
43 #include <tdeaboutdata.h>
44 #include <kstatusbar.h>
45 #include <kstdaction.h>
46 #include <tdeaction.h>
48 #include <tdeglobal.h>
49 #include <tdeapplication.h>
50 #include <tdelocale.h>
52 #include <tdeconfig.h>
53 #include <tdecmdlineargs.h>
54 #include <tdemessagebox.h>
55 #include <kkeydialog.h>
56 #include <kedittoolbar.h>
57 #include <tdeparts/event.h>
58 #include <tdemenubar.h>
60 #include <tqdropsite.h>
61 #include <tqdragobject.h>
63 #include <tqtextcodec.h>
67 #define KWRITE_ID_GEN 1
69 TQPtrList<KTextEditor::Document> KWrite::docList;
70 TQPtrList<KWrite> KWrite::winList;
72 KWrite::KWrite (KTextEditor::Document *doc)
80 if ( !(doc = KTextEditor::EditorChooser::createDocument(0,
"KTextEditor::Document")) )
82 KMessageBox::error(
this, i18n(
"A TDE text-editor component could not be found;\n"
83 "please check your TDE installation."));
90 m_view = doc->createView (
this, 0L);
92 setCentralWidget(m_view);
99 connect(m_view,TQ_SIGNAL(newStatus()),
this,TQ_SLOT(newCaption()));
100 connect(m_view,TQ_SIGNAL(viewStatusMsg(
const TQString &)),
this,TQ_SLOT(newStatus(
const TQString &)));
101 connect(m_view->document(),TQ_SIGNAL(fileNameChanged()),
this,TQ_SLOT(newCaption()));
102 connect(m_view->document(),TQ_SIGNAL(fileNameChanged()),
this,TQ_SLOT(slotFileNameChanged()));
103 connect(m_view,TQ_SIGNAL(dropEventPass(TQDropEvent *)),
this,TQ_SLOT(slotDropEvent(TQDropEvent *)));
105 setXMLFile(
"kwriteui.rc" );
106 createShellGUI(
true );
107 guiFactory()->addClient( m_view );
110 if (
static_cast<Kate::View*
>(m_view->tqt_cast(
"Kate::View")))
111 static_cast<Kate::View*
>(m_view->tqt_cast(
"Kate::View"))->installPopup ((TQPopupMenu*)(factory()->container(
"tdetexteditor_popup",
this)) );
114 if (!initialGeometrySet())
115 resize( TQSize(700, 480).expandedTo(minimumSizeHint()));
118 setAutoSaveSettings ();
122 winList.append (
this);
129 winList.remove (
this);
131 if (m_view->document()->views().count() == 1)
133 docList.remove(m_view->document());
134 delete m_view->document();
137 kapp->config()->sync ();
140 void KWrite::setupActions()
142 KStdAction::close(
this, TQ_SLOT(slotFlush()), actionCollection(),
"file_close" )->setWhatsThis(i18n(
"Use this to close the current document"));
145 KStdAction::print(
this, TQ_SLOT(printDlg()), actionCollection())->setWhatsThis(i18n(
"Use this command to print the current document"));
146 KStdAction::openNew(
this, TQ_SLOT(slotNew()), actionCollection(),
"file_new" )->setWhatsThis(i18n(
"Use this command to create a new document"));
147 KStdAction::open(
this, TQ_SLOT( slotOpen() ), actionCollection(),
"file_open" )->setWhatsThis(i18n(
"Use this command to open an existing document for editing"));
149 m_recentFiles = KStdAction::openRecent(
this, TQ_SLOT(slotOpen(
const KURL&)),
151 m_recentFiles->setWhatsThis(i18n(
"This lists files which you have opened recently, and allows you to easily open them again."));
153 TDEAction *a=
new TDEAction(i18n(
"&New Window"),
"window-new", 0,
this, TQ_SLOT(newView()),
154 actionCollection(),
"view_new_view");
155 a->setWhatsThis(i18n(
"Create another view containing the current document"));
157 a=
new TDEAction(i18n(
"Choose Editor Component..."),0,
this,TQ_SLOT(changeEditor()),
158 actionCollection(),
"settings_choose_editor");
159 a->setWhatsThis(i18n(
"Override the system wide setting for the default editing component"));
161 KStdAction::quit(
this, TQ_SLOT(close()), actionCollection())->setWhatsThis(i18n(
"Close the current document view"));
164 setStandardToolBarMenuEnabled(
true);
166 m_paShowStatusBar = KStdAction::showStatusbar(
this, TQ_SLOT(toggleStatusBar()), actionCollection(),
"settings_show_statusbar");
167 m_paShowStatusBar->setWhatsThis(i18n(
"Use this command to show or hide the view's statusbar"));
169 m_paShowPath =
new TDEToggleAction(i18n(
"Sho&w Path"), 0,
this, TQ_SLOT(newCaption()),
170 actionCollection(),
"set_showPath");
171 m_paShowPath->setCheckedState(i18n(
"Hide Path"));
172 m_paShowPath->setWhatsThis(i18n(
"Show the complete document path in the window caption"));
173 a=KStdAction::keyBindings(
this, TQ_SLOT(editKeys()), actionCollection());
174 a->setWhatsThis(i18n(
"Configure the application's keyboard shortcut assignments."));
176 a=KStdAction::configureToolbars(
this, TQ_SLOT(editToolbars()), actionCollection());
177 a->setWhatsThis(i18n(
"Configure which items should appear in the toolbar(s)."));
180 void KWrite::setupStatusBar()
182 statusBar()->insertItem(
"", KWRITE_ID_GEN);
186 void KWrite::loadURL(
const KURL &url)
188 m_view->document()->openURL(url);
192 bool KWrite::queryClose()
194 if (m_view->document()->views().count() > 1)
197 if (m_view->document()->queryClose())
207 void KWrite::changeEditor()
209 KWriteEditorChooser choose(
this);
213 void KWrite::slotFlush ()
215 m_view->document()->closeURL();
218 void KWrite::slotNew()
223 void KWrite::slotOpen()
225 if (KTextEditor::encodingInterface(m_view->document()))
227 KEncodingFileDialog::Result r=KEncodingFileDialog::getOpenURLsAndEncoding(
228 KTextEditor::encodingInterface(m_view->document())->encoding(),
229 m_view->document()->url().url(),TQString::null,
this,i18n(
"Open File"));
231 for (KURL::List::Iterator i=r.URLs.begin(); i != r.URLs.end(); ++i)
233 encoding = r.encoding;
239 KURL::List l=KFileDialog::getOpenURLs(m_view->document()->url().url(),TQString::null,
this,TQString::null);
240 for (KURL::List::Iterator i=l.begin(); i != l.end(); ++i)
247 void KWrite::slotOpen(
const KURL& url )
249 if (url.isEmpty())
return;
251 if (!TDEIO::NetAccess::exists(url,
true,
this))
253 KMessageBox::error (
this, i18n(
"The given file could not be read, check if it exists or if it is readable for the current user."));
257 if (m_view->document()->isModified() || !m_view->document()->url().isEmpty())
259 KWrite *t =
new KWrite();
260 if (KTextEditor::encodingInterface(t->m_view->document())) KTextEditor::encodingInterface(t->m_view->document())->setEncoding(encoding);
265 if (KTextEditor::encodingInterface(m_view->document())) KTextEditor::encodingInterface(m_view->document())->setEncoding(encoding);
270 void KWrite::slotFileNameChanged()
272 if ( ! m_view->document()->url().isEmpty() )
273 m_recentFiles->addURL( m_view->document()->url() );
276 void KWrite::newView()
278 new KWrite(m_view->document());
281 void KWrite::toggleStatusBar()
283 if( m_paShowStatusBar->isChecked() )
289 void KWrite::editKeys()
292 dlg.insert(actionCollection());
294 dlg.insert(m_view->actionCollection());
298 void KWrite::editToolbars()
300 saveMainWindowSettings( kapp->config(),
"MainWindow" );
301 KEditToolbar *dlg =
new KEditToolbar(guiFactory());
302 connect( dlg, TQ_SIGNAL(newToolbarConfig()),
this, TQ_SLOT(slotNewToolbarConfig()) );
307 void KWrite::slotNewToolbarConfig()
309 applyMainWindowSettings( kapp->config(),
"MainWindow" );
313 void KWrite::printNow()
315 KTextEditor::printInterface(m_view->document())->print ();
318 void KWrite::printDlg()
320 KTextEditor::printInterface(m_view->document())->printDialog ();
323 void KWrite::newStatus(
const TQString &msg)
327 statusBar()->changeItem(msg,KWRITE_ID_GEN);
330 void KWrite::newCaption()
332 if (m_view->document()->url().isEmpty()) {
333 setCaption(i18n(
"Untitled"),m_view->document()->isModified());
338 if (!m_paShowPath->isChecked())
340 c = m_view->document()->url().fileName();
344 c = c.left(64) +
"...";
348 c = m_view->document()->url().prettyURL();
352 c =
"..." + c.right(64);
355 setCaption (c, m_view->document()->isModified());
359 void KWrite::dragEnterEvent( TQDragEnterEvent *event )
361 event->accept(KURLDrag::canDecode(event));
364 void KWrite::dropEvent( TQDropEvent *event )
366 slotDropEvent(event);
369 void KWrite::slotDropEvent( TQDropEvent *event )
373 if (!KURLDrag::decode(event, textlist))
376 for (KURL::List::Iterator i=textlist.begin(); i != textlist.end(); ++i)
380 void KWrite::slotEnableActions(
bool enable )
382 TQValueList<TDEAction *> actions = actionCollection()->actions();
383 TQValueList<TDEAction *>::ConstIterator it = actions.begin();
384 TQValueList<TDEAction *>::ConstIterator end = actions.end();
386 for (; it != end; ++it )
387 (*it)->setEnabled( enable );
389 actions = m_view->actionCollection()->actions();
390 it = actions.begin();
393 for (; it != end; ++it )
394 (*it)->setEnabled( enable );
398 void KWrite::readConfig(TDEConfig *config)
400 config->setGroup(
"General Options");
402 m_paShowStatusBar->setChecked( config->readBoolEntry(
"ShowStatusBar") );
403 m_paShowPath->setChecked( config->readBoolEntry(
"ShowPath") );
405 m_recentFiles->loadEntries(config,
"Recent Files");
407 if (m_view && KTextEditor::configInterface(m_view->document()))
408 KTextEditor::configInterface(m_view->document())->readConfig(config);
410 if( m_paShowStatusBar->isChecked() )
416 void KWrite::writeConfig(TDEConfig *config)
418 config->setGroup(
"General Options");
420 config->writeEntry(
"ShowStatusBar",m_paShowStatusBar->isChecked());
421 config->writeEntry(
"ShowPath",m_paShowPath->isChecked());
423 m_recentFiles->saveEntries(config,
"Recent Files");
425 if (m_view && KTextEditor::configInterface(m_view->document()))
426 KTextEditor::configInterface(m_view->document())->writeConfig(config);
432 void KWrite::readConfig()
434 TDEConfig *config = kapp->config();
438 void KWrite::writeConfig()
440 TDEConfig *config = kapp->config();
445 void KWrite::restore(TDEConfig *config,
int n)
447 readPropertiesInternal(config, n);
450 void KWrite::readProperties(TDEConfig *config)
454 if (KTextEditor::sessionConfigInterface(m_view))
455 KTextEditor::sessionConfigInterface(m_view)->readSessionConfig(config);
458 void KWrite::saveProperties(TDEConfig *config)
461 config->writeEntry(
"DocumentNumber",docList.find(m_view->document()) + 1);
463 if (KTextEditor::sessionConfigInterface(m_view))
464 KTextEditor::sessionConfigInterface(m_view)->writeSessionConfig(config);
467 void KWrite::saveGlobalProperties(TDEConfig *config)
469 config->setGroup(
"Number");
470 config->writeEntry(
"NumberOfDocuments",docList.count());
472 for (uint z = 1; z <= docList.count(); z++)
474 TQString buf = TQString(
"Document %1").arg(z);
475 config->setGroup(buf);
477 KTextEditor::Document *doc = docList.at(z - 1);
479 if (KTextEditor::configInterface(doc))
480 KTextEditor::configInterface(doc)->writeSessionConfig(config);
483 for (uint z = 1; z <= winList.count(); z++)
485 TQString buf = TQString(
"Window %1").arg(z);
486 config->setGroup(buf);
488 config->writeEntry(
"DocumentNumber",docList.find(winList.at(z-1)->view()->document()) + 1);
493 void KWrite::restore()
495 TDEConfig *config = kapp->sessionConfig();
502 KTextEditor::Document *doc;
505 config->setGroup(
"Number");
506 docs = config->readNumEntry(
"NumberOfDocuments");
507 windows = config->readNumEntry(
"NumberOfWindows");
509 for (
int z = 1; z <= docs; z++)
511 buf = TQString(
"Document %1").arg(z);
512 config->setGroup(buf);
513 doc=KTextEditor::EditorChooser::createDocument(0,
"KTextEditor::Document");
515 if (KTextEditor::configInterface(doc))
516 KTextEditor::configInterface(doc)->readSessionConfig(config);
520 for (
int z = 1; z <= windows; z++)
522 buf = TQString(
"Window %1").arg(z);
523 config->setGroup(buf);
524 t =
new KWrite(docList.at(config->readNumEntry(
"DocumentNumber") - 1));
525 t->restore(config,z);
529 static TDECmdLineOptions options[] =
531 {
"stdin", I18N_NOOP(
"Read the contents of stdin"), 0},
532 {
"encoding <argument>", I18N_NOOP(
"Set encoding for the file to open"), 0 },
533 {
"line <argument>", I18N_NOOP(
"Navigate to this line"), 0 },
534 {
"column <argument>", I18N_NOOP(
"Navigate to this column"), 0 },
535 {
"+[URL]", I18N_NOOP(
"Document to open"), 0 },
539 extern "C" TDE_EXPORT
int kdemain(
int argc,
char **argv)
541 Kate::Document::setFileChangedDialogsActivated (
true);
543 TDELocale::setMainCatalogue(
"kate");
550 TQString kWriteVersion = TQString (
"4.5.%1").arg(KDE::versionMajor());
552 TDEAboutData aboutData (
"kwrite",
554 kWriteVersion.latin1(),
555 I18N_NOOP(
"KWrite - Text Editor" ), TDEAboutData::License_LGPL_V2,
556 I18N_NOOP(
"(c) 2000-2005 The Kate Authors" ), 0 );
558 aboutData.addAuthor (
"Christoph Cullmann", I18N_NOOP(
"Maintainer"),
"cullmann@kde.org",
"http://www.babylon2k.de");
559 aboutData.addAuthor (
"Anders Lund", I18N_NOOP(
"Core Developer"),
"anders@alweb.dk",
"http://www.alweb.dk");
560 aboutData.addAuthor (
"Joseph Wenninger", I18N_NOOP(
"Core Developer"),
"jowenn@kde.org",
"http://stud3.tuwien.ac.at/~e9925371");
561 aboutData.addAuthor (
"Hamish Rodda",I18N_NOOP(
"Core Developer"),
"rodda@kde.org");
562 aboutData.addAuthor (
"Waldo Bastian", I18N_NOOP(
"The cool buffersystem" ),
"bastian@kde.org" );
563 aboutData.addAuthor (
"Charles Samuels", I18N_NOOP(
"The Editing Commands"),
"charles@kde.org");
564 aboutData.addAuthor (
"Matt Newell", I18N_NOOP(
"Testing, ..."),
"newellm@proaxis.com");
565 aboutData.addAuthor (
"Michael Bartl", I18N_NOOP(
"Former Core Developer"),
"michael.bartl1@chello.at");
566 aboutData.addAuthor (
"Michael McCallum", I18N_NOOP(
"Core Developer"),
"gholam@xtra.co.nz");
567 aboutData.addAuthor (
"Jochen Wilhemly", I18N_NOOP(
"KWrite Author" ),
"digisnap@cs.tu-berlin.de" );
568 aboutData.addAuthor (
"Michael Koch",I18N_NOOP(
"KWrite port to KParts"),
"koch@kde.org");
569 aboutData.addAuthor (
"Christian Gebauer", 0,
"gebauer@kde.org" );
570 aboutData.addAuthor (
"Simon Hausmann", 0,
"hausmann@kde.org" );
571 aboutData.addAuthor (
"Glen Parker",I18N_NOOP(
"KWrite Undo History, Kspell integration"),
"glenebob@nwlink.com");
572 aboutData.addAuthor (
"Scott Manson",I18N_NOOP(
"KWrite XML Syntax highlighting support"),
"sdmanson@alltel.net");
573 aboutData.addAuthor (
"John Firebaugh",I18N_NOOP(
"Patches and more"),
"jfirebaugh@kde.org");
575 aboutData.addCredit (
"Matteo Merli",I18N_NOOP(
"Highlighting for RPM Spec-Files, Perl, Diff and more"),
"merlim@libero.it");
576 aboutData.addCredit (
"Rocky Scaletta",I18N_NOOP(
"Highlighting for VHDL"),
"rocky@purdue.edu");
577 aboutData.addCredit (
"Yury Lebedev",I18N_NOOP(
"Highlighting for SQL"),
"");
578 aboutData.addCredit (
"Chris Ross",I18N_NOOP(
"Highlighting for Ferite"),
"");
579 aboutData.addCredit (
"Nick Roux",I18N_NOOP(
"Highlighting for ILERPG"),
"");
580 aboutData.addCredit (
"Carsten Niehaus", I18N_NOOP(
"Highlighting for LaTeX"),
"");
581 aboutData.addCredit (
"Per Wigren", I18N_NOOP(
"Highlighting for Makefiles, Python"),
"");
582 aboutData.addCredit (
"Jan Fritz", I18N_NOOP(
"Highlighting for Python"),
"");
583 aboutData.addCredit (
"Daniel Naber",
"",
"");
584 aboutData.addCredit (
"Roland Pabel",I18N_NOOP(
"Highlighting for Scheme"),
"");
585 aboutData.addCredit (
"Cristi Dumitrescu",I18N_NOOP(
"PHP Keyword/Datatype list"),
"");
586 aboutData.addCredit (
"Carsten Pfeiffer", I18N_NOOP(
"Very nice help"),
"");
587 aboutData.addCredit (I18N_NOOP(
"All people who have contributed and I have forgotten to mention"),
"",
"");
589 aboutData.setTranslator(I18N_NOOP(
"_: NAME OF TRANSLATORS\nYour names"), I18N_NOOP(
"_: EMAIL OF TRANSLATORS\nYour emails"));
591 TDECmdLineArgs::init( argc, argv, &aboutData );
592 TDECmdLineArgs::addCmdLineOptions( options );
596 TDEGlobal::locale()->insertCatalogue(
"katepart");
598 DCOPClient *client = kapp->dcopClient();
599 if (!client->isRegistered())
602 client->registerAs(
"kwrite");
605 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
607 if (kapp->isRestored())
614 int line = 0, column = 0;
616 TQTextCodec *codec = args->isSet(
"encoding") ? TQTextCodec::codecForName(args->getOption(
"encoding")) : 0;
618 if (args->isSet (
"line"))
620 line = args->getOption (
"line").toInt();
624 if (args->isSet (
"column"))
626 column = args->getOption (
"column").toInt();
630 if ( args->count() == 0 )
632 KWrite *t =
new KWrite;
634 if( args->isSet(
"stdin" ) )
636 TQTextIStream input(stdin);
640 input.setCodec (codec);
647 line = input.readLine();
648 text.append( line +
"\n" );
649 }
while( !line.isNull() );
652 KTextEditor::EditInterface *doc = KTextEditor::editInterface (t->view()->document());
654 doc->setText( text );
657 if (nav && KTextEditor::viewCursorInterface(t->view()))
658 KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
662 for (
int z = 0; z < args->count(); z++ )
664 KWrite *t =
new KWrite();
667 bool noDir = !args->url(z).isLocalFile() || !TQDir (args->url(z).path()).exists();
671 if (Kate::document (t->view()->document()))
672 Kate::Document::setOpenErrorDialogsActivated (
false);
674 if (codec && KTextEditor::encodingInterface(t->view()->document()))
675 KTextEditor::encodingInterface(t->view()->document())->setEncoding(codec->name());
677 t->loadURL( args->url( z ) );
679 if (Kate::document (t->view()->document()))
680 Kate::Document::setOpenErrorDialogsActivated (
true);
682 if (nav && KTextEditor::viewCursorInterface(t->view()))
683 KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
686 KMessageBox::sorry( t, i18n(
"The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(args->url(z).url()) );
693 if (KWrite::noWindows())
699 KWriteEditorChooser::KWriteEditorChooser(TQWidget *):
700 KDialogBase(KDialogBase::Plain,i18n(
"Choose Editor Component"),KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Cancel)
702 (
new TQVBoxLayout(plainPage()))->setAutoAdd(
true);
703 m_chooser=
new KTextEditor::EditorChooser(plainPage(),
"Editor Chooser");
704 setMainWidget(m_chooser);
705 m_chooser->readAppSetting();
708 KWriteEditorChooser::~KWriteEditorChooser() {
712 void KWriteEditorChooser::slotOk() {
713 m_chooser->writeAppSetting();
714 KDialogBase::slotOk();