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 <dcopclient.h>
39 #include <kencodingfiledialog.h>
40 #include <tdediroperator.h>
41 #include <kiconloader.h>
42 #include <tdeaboutdata.h>
43 #include <kstatusbar.h>
44 #include <kstdaction.h>
45 #include <tdeaction.h>
47 #include <tdeglobal.h>
48 #include <tdeapplication.h>
49 #include <tdelocale.h>
51 #include <tdeconfig.h>
52 #include <tdecmdlineargs.h>
53 #include <tdemessagebox.h>
54 #include <kkeydialog.h>
55 #include <kedittoolbar.h>
56 #include <tdeparts/event.h>
57 #include <tdemenubar.h>
59 #include <tqdropsite.h>
60 #include <tqdragobject.h>
62 #include <tqtextcodec.h>
66 #define KWRITE_ID_GEN 1
68 TQPtrList<KTextEditor::Document> KWrite::docList;
69 TQPtrList<KWrite> KWrite::winList;
71 KWrite::KWrite (KTextEditor::Document *doc)
79 if ( !(doc = KTextEditor::EditorChooser::createDocument(0,
"KTextEditor::Document")) )
81 KMessageBox::error(
this, i18n(
"A TDE text-editor component could not be found;\n"
82 "please check your TDE installation."));
89 m_view = doc->createView (
this, 0L);
91 setCentralWidget(m_view);
98 connect(m_view,TQ_SIGNAL(newStatus()),
this,TQ_SLOT(newCaption()));
99 connect(m_view,TQ_SIGNAL(viewStatusMsg(
const TQString &)),
this,TQ_SLOT(newStatus(
const TQString &)));
100 connect(m_view->document(),TQ_SIGNAL(fileNameChanged()),
this,TQ_SLOT(newCaption()));
101 connect(m_view->document(),TQ_SIGNAL(fileNameChanged()),
this,TQ_SLOT(slotFileNameChanged()));
102 connect(m_view,TQ_SIGNAL(dropEventPass(TQDropEvent *)),
this,TQ_SLOT(slotDropEvent(TQDropEvent *)));
104 setXMLFile(
"kwriteui.rc" );
105 createShellGUI(
true );
106 guiFactory()->addClient( m_view );
109 if (
static_cast<Kate::View*
>(m_view->tqt_cast(
"Kate::View")))
110 static_cast<Kate::View*
>(m_view->tqt_cast(
"Kate::View"))->installPopup ((TQPopupMenu*)(factory()->container(
"tdetexteditor_popup",
this)) );
113 if (!initialGeometrySet())
114 resize( TQSize(700, 480).expandedTo(minimumSizeHint()));
117 setAutoSaveSettings ();
121 winList.append (
this);
128 winList.remove (
this);
130 if (m_view->document()->views().count() == 1)
132 docList.remove(m_view->document());
133 delete m_view->document();
136 kapp->config()->sync ();
139 void KWrite::setupActions()
141 KStdAction::close(
this, TQ_SLOT(slotFlush()), actionCollection(),
"file_close" )->setWhatsThis(i18n(
"Use this to close the current document"));
144 KStdAction::print(
this, TQ_SLOT(printDlg()), actionCollection())->setWhatsThis(i18n(
"Use this command to print the current document"));
145 KStdAction::openNew(
this, TQ_SLOT(slotNew()), actionCollection(),
"file_new" )->setWhatsThis(i18n(
"Use this command to create a new document"));
146 KStdAction::open(
this, TQ_SLOT( slotOpen() ), actionCollection(),
"file_open" )->setWhatsThis(i18n(
"Use this command to open an existing document for editing"));
148 m_recentFiles = KStdAction::openRecent(
this, TQ_SLOT(slotOpen(
const KURL&)),
150 m_recentFiles->setWhatsThis(i18n(
"This lists files which you have opened recently, and allows you to easily open them again."));
152 TDEAction *a=
new TDEAction(i18n(
"&New Window"),
"window-new", 0,
this, TQ_SLOT(newView()),
153 actionCollection(),
"view_new_view");
154 a->setWhatsThis(i18n(
"Create another view containing the current document"));
156 a=
new TDEAction(i18n(
"Choose Editor Component..."),0,
this,TQ_SLOT(changeEditor()),
157 actionCollection(),
"settings_choose_editor");
158 a->setWhatsThis(i18n(
"Override the system wide setting for the default editing component"));
160 KStdAction::quit(
this, TQ_SLOT(close()), actionCollection())->setWhatsThis(i18n(
"Close the current document view"));
163 setStandardToolBarMenuEnabled(
true);
165 m_paShowStatusBar = KStdAction::showStatusbar(
this, TQ_SLOT(toggleStatusBar()), actionCollection(),
"settings_show_statusbar");
166 m_paShowStatusBar->setWhatsThis(i18n(
"Use this command to show or hide the view's statusbar"));
168 m_paShowPath =
new TDEToggleAction(i18n(
"Sho&w Path"), 0,
this, TQ_SLOT(newCaption()),
169 actionCollection(),
"set_showPath");
170 m_paShowPath->setCheckedState(i18n(
"Hide Path"));
171 m_paShowPath->setWhatsThis(i18n(
"Show the complete document path in the window caption"));
172 a=KStdAction::keyBindings(
this, TQ_SLOT(editKeys()), actionCollection());
173 a->setWhatsThis(i18n(
"Configure the application's keyboard shortcut assignments."));
175 a=KStdAction::configureToolbars(
this, TQ_SLOT(editToolbars()), actionCollection());
176 a->setWhatsThis(i18n(
"Configure which items should appear in the toolbar(s)."));
179 void KWrite::setupStatusBar()
181 statusBar()->insertItem(
"", KWRITE_ID_GEN);
185 void KWrite::loadURL(
const KURL &url)
187 m_view->document()->openURL(url);
191 bool KWrite::queryClose()
193 if (m_view->document()->views().count() > 1)
196 if (m_view->document()->queryClose())
206 void KWrite::changeEditor()
208 KWriteEditorChooser choose(
this);
212 void KWrite::slotFlush ()
214 m_view->document()->closeURL();
217 void KWrite::slotNew()
222 void KWrite::slotOpen()
224 if (KTextEditor::encodingInterface(m_view->document()))
226 KEncodingFileDialog::Result r=KEncodingFileDialog::getOpenURLsAndEncoding(
227 KTextEditor::encodingInterface(m_view->document())->encoding(),
228 m_view->document()->url().url(),TQString::null,
this,i18n(
"Open File"));
230 for (KURL::List::Iterator i=r.URLs.begin(); i != r.URLs.end(); ++i)
232 encoding = r.encoding;
238 KURL::List l=KFileDialog::getOpenURLs(m_view->document()->url().url(),TQString::null,
this,TQString::null);
239 for (KURL::List::Iterator i=l.begin(); i != l.end(); ++i)
246 void KWrite::slotOpen(
const KURL& url )
248 if (url.isEmpty())
return;
250 if (!TDEIO::NetAccess::exists(url,
true,
this))
252 KMessageBox::error (
this, i18n(
"The given file could not be read, check if it exists or if it is readable for the current user."));
256 if (m_view->document()->isModified() || !m_view->document()->url().isEmpty())
258 KWrite *t =
new KWrite();
259 if (KTextEditor::encodingInterface(t->m_view->document())) KTextEditor::encodingInterface(t->m_view->document())->setEncoding(encoding);
264 if (KTextEditor::encodingInterface(m_view->document())) KTextEditor::encodingInterface(m_view->document())->setEncoding(encoding);
269 void KWrite::slotFileNameChanged()
271 if ( ! m_view->document()->url().isEmpty() )
272 m_recentFiles->addURL( m_view->document()->url() );
275 void KWrite::newView()
277 new KWrite(m_view->document());
280 void KWrite::toggleStatusBar()
282 if( m_paShowStatusBar->isChecked() )
288 void KWrite::editKeys()
291 dlg.insert(actionCollection());
293 dlg.insert(m_view->actionCollection());
297 void KWrite::editToolbars()
299 saveMainWindowSettings( kapp->config(),
"MainWindow" );
300 KEditToolbar *dlg =
new KEditToolbar(guiFactory());
301 connect( dlg, TQ_SIGNAL(newToolbarConfig()),
this, TQ_SLOT(slotNewToolbarConfig()) );
306 void KWrite::slotNewToolbarConfig()
308 applyMainWindowSettings( kapp->config(),
"MainWindow" );
312 void KWrite::printNow()
314 KTextEditor::printInterface(m_view->document())->print ();
317 void KWrite::printDlg()
319 KTextEditor::printInterface(m_view->document())->printDialog ();
322 void KWrite::newStatus(
const TQString &msg)
326 statusBar()->changeItem(msg,KWRITE_ID_GEN);
329 void KWrite::newCaption()
331 if (m_view->document()->url().isEmpty()) {
332 setCaption(i18n(
"Untitled"),m_view->document()->isModified());
337 if (!m_paShowPath->isChecked())
339 c = m_view->document()->url().fileName();
343 c = c.left(64) +
"...";
347 c = m_view->document()->url().prettyURL();
351 c =
"..." + c.right(64);
354 setCaption (c, m_view->document()->isModified());
358 void KWrite::dragEnterEvent( TQDragEnterEvent *event )
360 event->accept(KURLDrag::canDecode(event));
363 void KWrite::dropEvent( TQDropEvent *event )
365 slotDropEvent(event);
368 void KWrite::slotDropEvent( TQDropEvent *event )
372 if (!KURLDrag::decode(event, textlist))
375 for (KURL::List::Iterator i=textlist.begin(); i != textlist.end(); ++i)
379 void KWrite::slotEnableActions(
bool enable )
381 TQValueList<TDEAction *> actions = actionCollection()->actions();
382 TQValueList<TDEAction *>::ConstIterator it = actions.begin();
383 TQValueList<TDEAction *>::ConstIterator end = actions.end();
385 for (; it != end; ++it )
386 (*it)->setEnabled( enable );
388 actions = m_view->actionCollection()->actions();
389 it = actions.begin();
392 for (; it != end; ++it )
393 (*it)->setEnabled( enable );
397 void KWrite::readConfig(TDEConfig *config)
399 config->setGroup(
"General Options");
401 m_paShowStatusBar->setChecked( config->readBoolEntry(
"ShowStatusBar") );
402 m_paShowPath->setChecked( config->readBoolEntry(
"ShowPath") );
404 m_recentFiles->loadEntries(config,
"Recent Files");
406 if (m_view && KTextEditor::configInterface(m_view->document()))
407 KTextEditor::configInterface(m_view->document())->readConfig(config);
409 if( m_paShowStatusBar->isChecked() )
415 void KWrite::writeConfig(TDEConfig *config)
417 config->setGroup(
"General Options");
419 config->writeEntry(
"ShowStatusBar",m_paShowStatusBar->isChecked());
420 config->writeEntry(
"ShowPath",m_paShowPath->isChecked());
422 m_recentFiles->saveEntries(config,
"Recent Files");
424 if (m_view && KTextEditor::configInterface(m_view->document()))
425 KTextEditor::configInterface(m_view->document())->writeConfig(config);
431 void KWrite::readConfig()
433 TDEConfig *config = kapp->config();
437 void KWrite::writeConfig()
439 TDEConfig *config = kapp->config();
444 void KWrite::restore(TDEConfig *config,
int n)
446 readPropertiesInternal(config, n);
449 void KWrite::readProperties(TDEConfig *config)
453 if (KTextEditor::sessionConfigInterface(m_view))
454 KTextEditor::sessionConfigInterface(m_view)->readSessionConfig(config);
457 void KWrite::saveProperties(TDEConfig *config)
460 config->writeEntry(
"DocumentNumber",docList.find(m_view->document()) + 1);
462 if (KTextEditor::sessionConfigInterface(m_view))
463 KTextEditor::sessionConfigInterface(m_view)->writeSessionConfig(config);
466 void KWrite::saveGlobalProperties(TDEConfig *config)
468 config->setGroup(
"Number");
469 config->writeEntry(
"NumberOfDocuments",docList.count());
471 for (uint z = 1; z <= docList.count(); z++)
473 TQString buf = TQString(
"Document %1").arg(z);
474 config->setGroup(buf);
476 KTextEditor::Document *doc = docList.at(z - 1);
478 if (KTextEditor::configInterface(doc))
479 KTextEditor::configInterface(doc)->writeSessionConfig(config);
482 for (uint z = 1; z <= winList.count(); z++)
484 TQString buf = TQString(
"Window %1").arg(z);
485 config->setGroup(buf);
487 config->writeEntry(
"DocumentNumber",docList.find(winList.at(z-1)->view()->document()) + 1);
492 void KWrite::restore()
494 TDEConfig *config = kapp->sessionConfig();
501 KTextEditor::Document *doc;
504 config->setGroup(
"Number");
505 docs = config->readNumEntry(
"NumberOfDocuments");
506 windows = config->readNumEntry(
"NumberOfWindows");
508 for (
int z = 1; z <= docs; z++)
510 buf = TQString(
"Document %1").arg(z);
511 config->setGroup(buf);
512 doc=KTextEditor::EditorChooser::createDocument(0,
"KTextEditor::Document");
514 if (KTextEditor::configInterface(doc))
515 KTextEditor::configInterface(doc)->readSessionConfig(config);
519 for (
int z = 1; z <= windows; z++)
521 buf = TQString(
"Window %1").arg(z);
522 config->setGroup(buf);
523 t =
new KWrite(docList.at(config->readNumEntry(
"DocumentNumber") - 1));
524 t->restore(config,z);
528 static TDECmdLineOptions options[] =
530 {
"stdin", I18N_NOOP(
"Read the contents of stdin"), 0},
531 {
"encoding <argument>", I18N_NOOP(
"Set encoding for the file to open"), 0 },
532 {
"line <argument>", I18N_NOOP(
"Navigate to this line"), 0 },
533 {
"column <argument>", I18N_NOOP(
"Navigate to this column"), 0 },
534 {
"+[URL]", I18N_NOOP(
"Document to open"), 0 },
538 extern "C" TDE_EXPORT
int kdemain(
int argc,
char **argv)
540 Kate::Document::setFileChangedDialogsActivated (
true);
542 TDELocale::setMainCatalogue(
"kate");
549 TQString kWriteVersion = TQString (
"4.5.%1").arg(KDE::versionMajor());
551 TDEAboutData aboutData (
"kwrite",
553 kWriteVersion.latin1(),
554 I18N_NOOP(
"KWrite - Text Editor" ), TDEAboutData::License_LGPL_V2,
555 I18N_NOOP(
"(c) 2000-2005 The Kate Authors" ), 0 );
557 aboutData.addAuthor (
"Christoph Cullmann", I18N_NOOP(
"Maintainer"),
"cullmann@kde.org",
"http://www.babylon2k.de");
558 aboutData.addAuthor (
"Anders Lund", I18N_NOOP(
"Core Developer"),
"anders@alweb.dk",
"http://www.alweb.dk");
559 aboutData.addAuthor (
"Joseph Wenninger", I18N_NOOP(
"Core Developer"),
"jowenn@kde.org",
"http://stud3.tuwien.ac.at/~e9925371");
560 aboutData.addAuthor (
"Hamish Rodda",I18N_NOOP(
"Core Developer"),
"rodda@kde.org");
561 aboutData.addAuthor (
"Waldo Bastian", I18N_NOOP(
"The cool buffersystem" ),
"bastian@kde.org" );
562 aboutData.addAuthor (
"Charles Samuels", I18N_NOOP(
"The Editing Commands"),
"charles@kde.org");
563 aboutData.addAuthor (
"Matt Newell", I18N_NOOP(
"Testing, ..."),
"newellm@proaxis.com");
564 aboutData.addAuthor (
"Michael Bartl", I18N_NOOP(
"Former Core Developer"),
"michael.bartl1@chello.at");
565 aboutData.addAuthor (
"Michael McCallum", I18N_NOOP(
"Core Developer"),
"gholam@xtra.co.nz");
566 aboutData.addAuthor (
"Jochen Wilhemly", I18N_NOOP(
"KWrite Author" ),
"digisnap@cs.tu-berlin.de" );
567 aboutData.addAuthor (
"Michael Koch",I18N_NOOP(
"KWrite port to KParts"),
"koch@kde.org");
568 aboutData.addAuthor (
"Christian Gebauer", 0,
"gebauer@kde.org" );
569 aboutData.addAuthor (
"Simon Hausmann", 0,
"hausmann@kde.org" );
570 aboutData.addAuthor (
"Glen Parker",I18N_NOOP(
"KWrite Undo History, Kspell integration"),
"glenebob@nwlink.com");
571 aboutData.addAuthor (
"Scott Manson",I18N_NOOP(
"KWrite XML Syntax highlighting support"),
"sdmanson@alltel.net");
572 aboutData.addAuthor (
"John Firebaugh",I18N_NOOP(
"Patches and more"),
"jfirebaugh@kde.org");
574 aboutData.addCredit (
"Matteo Merli",I18N_NOOP(
"Highlighting for RPM Spec-Files, Perl, Diff and more"),
"merlim@libero.it");
575 aboutData.addCredit (
"Rocky Scaletta",I18N_NOOP(
"Highlighting for VHDL"),
"rocky@purdue.edu");
576 aboutData.addCredit (
"Yury Lebedev",I18N_NOOP(
"Highlighting for SQL"),
"");
577 aboutData.addCredit (
"Chris Ross",I18N_NOOP(
"Highlighting for Ferite"),
"");
578 aboutData.addCredit (
"Nick Roux",I18N_NOOP(
"Highlighting for ILERPG"),
"");
579 aboutData.addCredit (
"Carsten Niehaus", I18N_NOOP(
"Highlighting for LaTeX"),
"");
580 aboutData.addCredit (
"Per Wigren", I18N_NOOP(
"Highlighting for Makefiles, Python"),
"");
581 aboutData.addCredit (
"Jan Fritz", I18N_NOOP(
"Highlighting for Python"),
"");
582 aboutData.addCredit (
"Daniel Naber",
"",
"");
583 aboutData.addCredit (
"Roland Pabel",I18N_NOOP(
"Highlighting for Scheme"),
"");
584 aboutData.addCredit (
"Cristi Dumitrescu",I18N_NOOP(
"PHP Keyword/Datatype list"),
"");
585 aboutData.addCredit (
"Carsten Pfeiffer", I18N_NOOP(
"Very nice help"),
"");
586 aboutData.addCredit (I18N_NOOP(
"All people who have contributed and I have forgotten to mention"),
"",
"");
588 aboutData.setTranslator(I18N_NOOP(
"_: NAME OF TRANSLATORS\nYour names"), I18N_NOOP(
"_: EMAIL OF TRANSLATORS\nYour emails"));
590 TDECmdLineArgs::init( argc, argv, &aboutData );
591 TDECmdLineArgs::addCmdLineOptions( options );
595 TDEGlobal::locale()->insertCatalogue(
"katepart");
597 DCOPClient *client = kapp->dcopClient();
598 if (!client->isRegistered())
601 client->registerAs(
"kwrite");
604 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
606 if (kapp->isRestored())
613 int line = 0, column = 0;
615 TQTextCodec *codec = args->isSet(
"encoding") ? TQTextCodec::codecForName(args->getOption(
"encoding")) : 0;
617 if (args->isSet (
"line"))
619 line = args->getOption (
"line").toInt();
623 if (args->isSet (
"column"))
625 column = args->getOption (
"column").toInt();
629 if ( args->count() == 0 )
631 KWrite *t =
new KWrite;
633 if( args->isSet(
"stdin" ) )
635 TQTextIStream input(stdin);
639 input.setCodec (codec);
646 line = input.readLine();
647 text.append( line +
"\n" );
648 }
while( !line.isNull() );
651 KTextEditor::EditInterface *doc = KTextEditor::editInterface (t->view()->document());
653 doc->setText( text );
656 if (nav && KTextEditor::viewCursorInterface(t->view()))
657 KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
661 for (
int z = 0; z < args->count(); z++ )
663 KWrite *t =
new KWrite();
666 bool noDir = !args->url(z).isLocalFile() || !TQDir (args->url(z).path()).exists();
670 if (Kate::document (t->view()->document()))
671 Kate::Document::setOpenErrorDialogsActivated (
false);
673 if (codec && KTextEditor::encodingInterface(t->view()->document()))
674 KTextEditor::encodingInterface(t->view()->document())->setEncoding(codec->name());
676 t->loadURL( args->url( z ) );
678 if (Kate::document (t->view()->document()))
679 Kate::Document::setOpenErrorDialogsActivated (
true);
681 if (nav && KTextEditor::viewCursorInterface(t->view()))
682 KTextEditor::viewCursorInterface(t->view())->setCursorPosition (line, column);
685 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()) );
692 if (KWrite::noWindows())
698 KWriteEditorChooser::KWriteEditorChooser(TQWidget *):
699 KDialogBase(KDialogBase::Plain,i18n(
"Choose Editor Component"),KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Cancel)
701 (
new TQVBoxLayout(plainPage()))->setAutoAdd(
true);
702 m_chooser=
new KTextEditor::EditorChooser(plainPage(),
"Editor Chooser");
703 setMainWidget(m_chooser);
704 m_chooser->readAppSetting();
707 KWriteEditorChooser::~KWriteEditorChooser() {
711 void KWriteEditorChooser::slotOk() {
712 m_chooser->writeAppSetting();
713 KDialogBase::slotOk();