kdeui Library API Documentation

keditcl2.cpp

00001 /* This file is part of the KDE libraries
00002 
00003    Copyright (C) 1997 Bernd Johannes Wuebben <wuebben@math.cornell.edu>
00004    Copyright (C) 2000 Waldo Bastian <bastian@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <limits.h> // INT_MAX
00023 
00024 #include <qframe.h>
00025 #include <qlabel.h>
00026 #include <qlineedit.h>
00027 #include <qvbuttongroup.h>
00028 #include <qcheckbox.h>
00029 #include <qlayout.h>
00030 #include <qpushbutton.h>
00031 #include <qhbox.h>
00032 
00033 #include <kapplication.h>
00034 #include <kcombobox.h>
00035 #include <knuminput.h>
00036 #include <kmessagebox.h>
00037 #include <knotifyclient.h>
00038 #include <klocale.h>
00039 #include <kdebug.h>
00040 
00041 #include "keditcl.h"
00042 
00043 
00045 //
00046 // Find Methods
00047 //
00048 
00049 void KEdit::search(){
00050 
00051   if( replace_dialog != 0 && replace_dialog->isVisible() == true )
00052   {
00053     replace_dialog->hide();
00054   }
00055 
00056   if( srchdialog == 0 )
00057   {
00058     srchdialog = new KEdFind( this, "searchdialog", false);
00059     connect(srchdialog,SIGNAL(search()),this,SLOT(search_slot()));
00060     connect(srchdialog,SIGNAL(done()),this,SLOT(searchdone_slot()));
00061   }
00062 
00063   // If we already searched / replaced something before make sure it shows
00064   // up in the find dialog line-edit.
00065 
00066   QString string;
00067   string = srchdialog->getText();
00068   srchdialog->setText(string.isEmpty() ? pattern : string);
00069 
00070   deselect();
00071   last_search = NONE;
00072 
00073   srchdialog->show();
00074   srchdialog->result();
00075 }
00076 
00077 
00078 void KEdit::search_slot(){
00079 
00080   int line, col;
00081 
00082   if (!srchdialog)
00083     return;
00084 
00085   QString to_find_string = srchdialog->getText();
00086   getCursorPosition(&line,&col);
00087 
00088   // srchdialog->get_direction() is true if searching backward
00089 
00090   if (last_search != NONE && srchdialog->get_direction()){
00091     col = col  - pattern.length() - 1 ;
00092   }
00093 
00094 again:
00095   int  result = doSearch(to_find_string, srchdialog->case_sensitive(),
00096              false, (!srchdialog->get_direction()),line,col);
00097 
00098   if(result == 0){
00099     if(!srchdialog->get_direction()){ // forward search
00100 
00101       int query = KMessageBox::questionYesNo(
00102             srchdialog,
00103                         i18n("End of document reached.\n"\
00104                              "Continue from the beginning?"),
00105                         i18n("Find"));
00106       if (query == KMessageBox::Yes){
00107     line = 0;
00108     col = 0;
00109     goto again;
00110       }
00111     }
00112     else{ //backward search
00113 
00114       int query = KMessageBox::questionYesNo(
00115             srchdialog,
00116                         i18n("Beginning of document reached.\n"\
00117                              "Continue from the end?"),
00118                         i18n("Find"));
00119       if (query == KMessageBox::Yes){
00120     QString string = textLine( numLines() - 1 );
00121     line = numLines() - 1;
00122     col  = string.length();
00123     last_search = BACKWARD;
00124     goto again;
00125       }
00126     }
00127   }
00128   else{
00129     emit CursorPositionChanged();
00130   }
00131 }
00132 
00133 
00134 
00135 void KEdit::searchdone_slot(){
00136 
00137   if (!srchdialog)
00138     return;
00139 
00140   srchdialog->hide();
00141   setFocus();
00142   last_search = NONE;
00143 }
00144 
00145 /* antlarr: KDE 4: make it const QString & */
00146 int KEdit::doSearch(QString s_pattern, bool case_sensitive,
00147             bool wildcard, bool forward, int line, int col){
00148 
00149   (void) wildcard; // reserved for possible extension to regex
00150 
00151 
00152   int i, length;
00153   int pos = -1;
00154 
00155   if(forward){
00156 
00157     QString string;
00158 
00159     for(i = line; i < numLines(); i++) {
00160 
00161       string = textLine(i);
00162 
00163       pos = string.find(s_pattern, i == line ? col : 0, case_sensitive);
00164 
00165       if( pos != -1){
00166 
00167     length = s_pattern.length();
00168 
00169     setCursorPosition(i,pos,false);
00170 
00171     for(int l = 0 ; l < length; l++){
00172       cursorRight(true);
00173     }
00174 
00175     setCursorPosition( i , pos + length, true );
00176     pattern = s_pattern;
00177     last_search = FORWARD;
00178 
00179     return 1;
00180       }
00181     }
00182   }
00183   else{ // searching backwards
00184 
00185     QString string;
00186 
00187     for(i = line; i >= 0; i--) {
00188 
00189       string = textLine(i);
00190       int line_length = string.length();
00191 
00192       pos = string.findRev(s_pattern, line == i ? col : line_length , case_sensitive);
00193 
00194       if (pos != -1){
00195 
00196     length = s_pattern.length();
00197 
00198     if( ! (line == i && pos > col ) ){
00199 
00200       setCursorPosition(i ,pos ,false );
00201 
00202       for(int l = 0 ; l < length; l++){
00203         cursorRight(true);
00204       }
00205 
00206       setCursorPosition(i ,pos + length ,true );
00207       pattern = s_pattern;
00208       last_search = BACKWARD;
00209       return 1;
00210 
00211     }
00212       }
00213 
00214     }
00215   }
00216 
00217   return 0;
00218 
00219 }
00220 
00221 
00222 
00223 bool KEdit::repeatSearch() {
00224 
00225   if(!srchdialog || pattern.isEmpty())
00226   {
00227       search();
00228       return true;
00229   }
00230 
00231   search_slot();
00232 
00233   setFocus();
00234   return true;
00235 
00236 }
00237 
00238 
00240 //
00241 // Replace Methods
00242 //
00243 
00244 
00245 void KEdit::replace()
00246 {
00247   if( srchdialog != 0 && srchdialog->isVisible() == true)
00248   {
00249     srchdialog->hide();
00250   }
00251 
00252   if( replace_dialog == 0 )
00253   {
00254     replace_dialog = new KEdReplace( this, "replace_dialog", false );
00255     connect(replace_dialog,SIGNAL(find()),this,SLOT(replace_search_slot()));
00256     connect(replace_dialog,SIGNAL(replace()),this,SLOT(replace_slot()));
00257     connect(replace_dialog,SIGNAL(replaceAll()),this,SLOT(replace_all_slot()));
00258     connect(replace_dialog,SIGNAL(done()),this,SLOT(replacedone_slot()));
00259   }
00260 
00261   QString string = replace_dialog->getText();
00262   replace_dialog->setText(string.isEmpty() ? pattern : string);
00263 
00264 
00265   deselect();
00266   last_replace = NONE;
00267 
00268   replace_dialog->show();
00269   replace_dialog->result();
00270 }
00271 
00272 
00273 void KEdit::replace_slot(){
00274 
00275   if (!replace_dialog)
00276     return;
00277 
00278   if(!can_replace){
00279     KNotifyClient::beep();
00280     return;
00281   }
00282 
00283   int line,col, length;
00284 
00285   QString string = replace_dialog->getReplaceText();
00286   length = string.length();
00287 
00288   this->cut();
00289 
00290   getCursorPosition(&line,&col);
00291 
00292   insertAt(string,line,col);
00293   setModified(true);
00294   can_replace = false;
00295 
00296   if (replace_dialog->get_direction())
00297   {
00298     // Backward
00299     setCursorPosition(line,col+length);
00300     for( int k = 0; k < length; k++){
00301       cursorLeft(true);
00302     }
00303   }
00304   else
00305   {
00306     // Forward
00307     setCursorPosition(line,col);
00308     for( int k = 0; k < length; k++){
00309       cursorRight(true);
00310     }
00311   }
00312 }
00313 
00314 void KEdit::replace_all_slot(){
00315 
00316   if (!replace_dialog)
00317     return;
00318 
00319   QString to_find_string = replace_dialog->getText();
00320 
00321   int lineFrom, lineTo, colFrom, colTo;
00322   getSelection(&lineFrom, &colFrom, &lineTo, &colTo);
00323 
00324   // replace_dialog->get_direction() is true if searching backward
00325   if (replace_dialog->get_direction())
00326   {
00327     if (colTo != -1)
00328     {
00329       replace_all_col = colTo - to_find_string.length();
00330       replace_all_line = lineTo;
00331     }
00332     else
00333     {
00334       getCursorPosition(&replace_all_line,&replace_all_col);
00335       replace_all_col--;
00336     }
00337   }
00338   else
00339   {
00340     if (colFrom != -1)
00341     {
00342       replace_all_col = colFrom;
00343       replace_all_line = lineFrom;
00344     }
00345     else
00346     {
00347       getCursorPosition(&replace_all_line,&replace_all_col);
00348     }
00349   }
00350 
00351   deselect();
00352 
00353 again:
00354 
00355   setAutoUpdate(false);
00356   int result = 1;
00357 
00358   while(result){
00359 
00360     result = doReplace(to_find_string, replace_dialog->case_sensitive(),
00361                false, (!replace_dialog->get_direction()),
00362                replace_all_line,replace_all_col,true);
00363 
00364   }
00365 
00366   setAutoUpdate(true);
00367   update();
00368 
00369   if(!replace_dialog->get_direction()){ // forward search
00370 
00371     int query = KMessageBox::questionYesNo(
00372             srchdialog,
00373                         i18n("End of document reached.\n"\
00374                              "Continue from the beginning?"),
00375                         i18n("Find"));
00376     if (query == KMessageBox::Yes){
00377       replace_all_line = 0;
00378       replace_all_col = 0;
00379       goto again;
00380     }
00381   }
00382   else{ //backward search
00383 
00384     int query = KMessageBox::questionYesNo(
00385             srchdialog,
00386                         i18n("Beginning of document reached.\n"\
00387                              "Continue from the end?"),
00388                         i18n("Find"));
00389     if (query == KMessageBox::Yes){
00390       QString string = textLine( numLines() - 1 );
00391       replace_all_line = numLines() - 1;
00392       replace_all_col  = string.length();
00393       last_replace = BACKWARD;
00394       goto again;
00395     }
00396   }
00397 
00398   emit CursorPositionChanged();
00399 
00400 }
00401 
00402 
00403 void KEdit::replace_search_slot(){
00404 
00405   int line, col;
00406 
00407   if (!replace_dialog)
00408     return;
00409 
00410   QString to_find_string = replace_dialog->getText();
00411 
00412   int lineFrom, lineTo, colFrom, colTo;
00413   getSelection(&lineFrom, &colFrom, &lineTo, &colTo);
00414 
00415   // replace_dialog->get_direction() is true if searching backward
00416   if (replace_dialog->get_direction())
00417   {
00418     if (colFrom != -1)
00419     {
00420       col = colFrom - to_find_string.length();
00421       line = lineFrom;
00422     }
00423     else
00424     {
00425       getCursorPosition(&line,&col);
00426       col--;
00427     }
00428   }
00429   else
00430   {
00431     if (colTo != -1)
00432     {
00433       col = colTo;
00434       line = lineTo;
00435     }
00436     else
00437     {
00438       getCursorPosition(&line,&col);
00439     }
00440   }
00441 
00442 again:
00443 
00444   int  result = doReplace(to_find_string, replace_dialog->case_sensitive(),
00445              false, (!replace_dialog->get_direction()), line, col, false );
00446 
00447   if(result == 0){
00448     if(!replace_dialog->get_direction()){ // forward search
00449 
00450      int query = KMessageBox::questionYesNo(
00451             replace_dialog,
00452                         i18n("End of document reached.\n"\
00453                              "Continue from the beginning?"),
00454                         i18n("Replace"));
00455      if (query == KMessageBox::Yes){
00456     line = 0;
00457     col = 0;
00458     goto again;
00459       }
00460     }
00461     else{ //backward search
00462 
00463      int query = KMessageBox::questionYesNo(
00464             replace_dialog,
00465                         i18n("Beginning of document reached.\n"\
00466                              "Continue from the end?"),
00467                         i18n("Replace"));
00468       if (query == KMessageBox::Yes){
00469     QString string = textLine( numLines() - 1 );
00470     line = numLines() - 1;
00471     col  = string.length();
00472     last_replace = BACKWARD;
00473     goto again;
00474       }
00475     }
00476   }
00477   else{
00478 
00479     emit CursorPositionChanged();
00480   }
00481 }
00482 
00483 
00484 
00485 void KEdit::replacedone_slot(){
00486 
00487   if (!replace_dialog)
00488     return;
00489 
00490   replace_dialog->hide();
00491   //  replace_dialog->clearFocus();
00492 
00493   setFocus();
00494 
00495   last_replace = NONE;
00496   can_replace  = false;
00497 
00498 }
00499 
00500 
00501 
00502 /* antlarr: KDE 4: make it const QString & */
00503 int KEdit::doReplace(QString s_pattern, bool case_sensitive,
00504        bool wildcard, bool forward, int line, int col, bool replace_all){
00505 
00506 
00507   (void) wildcard; // reserved for possible extension to regex
00508 
00509   int line_counter, length;
00510   int pos = -1;
00511 
00512   QString string;
00513   QString stringnew;
00514   QString replacement;
00515 
00516   replacement = replace_dialog->getReplaceText();
00517   line_counter = line;
00518   replace_all_col = col;
00519 
00520   if(forward){
00521 
00522     int num_lines = numLines();
00523 
00524     while (line_counter < num_lines){
00525 
00526       string = textLine(line_counter);
00527 
00528       if (replace_all){
00529     pos = string.find(s_pattern, replace_all_col, case_sensitive);
00530       }
00531       else{
00532     pos = string.find(s_pattern, line_counter == line ? col : 0, case_sensitive);
00533       }
00534 
00535       if (pos == -1 ){
00536     line_counter++;
00537     replace_all_col = 0;
00538     replace_all_line = line_counter;
00539       }
00540 
00541       if( pos != -1){
00542 
00543     length = s_pattern.length();
00544 
00545     if(replace_all){ // automatic
00546 
00547       stringnew = string.copy();
00548       stringnew.replace(pos,length,replacement);
00549 
00550       removeLine(line_counter);
00551       insertLine(stringnew,line_counter);
00552 
00553       replace_all_col = pos + replacement.length();
00554       replace_all_line = line_counter;
00555 
00556       setModified(true);
00557     }
00558     else{ // interactive
00559 
00560       setCursorPosition( line_counter , pos, false );
00561 
00562       for(int l = 0 ; l < length; l++){
00563         cursorRight(true);
00564       }
00565 
00566       setCursorPosition( line_counter , pos + length, true );
00567       pattern = s_pattern;
00568       last_replace = FORWARD;
00569       can_replace = true;
00570 
00571       return 1;
00572 
00573     }
00574 
00575       }
00576     }
00577   }
00578   else{ // searching backwards
00579 
00580     while(line_counter >= 0){
00581 
00582       string = textLine(line_counter);
00583 
00584       int line_length = string.length();
00585 
00586       if( replace_all ){
00587         if (replace_all_col < 0)
00588           pos = -1;
00589         else
00590           pos = string.findRev(s_pattern, replace_all_col , case_sensitive);
00591       }
00592       else{
00593         if ((line == line_counter) && (col < 0))
00594           pos = -1;
00595         else
00596           pos = string.findRev(s_pattern,
00597                line == line_counter ? col : line_length , case_sensitive);
00598       }
00599 
00600       if (pos == -1 ){
00601     line_counter--;
00602 
00603         replace_all_col = 0;
00604     if(line_counter >= 0){
00605       string = textLine(line_counter);
00606       replace_all_col = string.length();
00607 
00608     }
00609     replace_all_line = line_counter;
00610       }
00611 
00612 
00613       if (pos != -1){
00614     length = s_pattern.length();
00615 
00616     if(replace_all){ // automatic
00617 
00618       stringnew = string.copy();
00619       stringnew.replace(pos,length,replacement);
00620 
00621       removeLine(line_counter);
00622       insertLine(stringnew,line_counter);
00623 
00624       replace_all_col = pos-length;
00625       replace_all_line = line_counter;
00626       if (replace_all_col < 0)
00627       {
00628              line_counter--;
00629 
00630              if(line_counter >= 0){
00631                 string = textLine(line_counter);
00632                 replace_all_col = string.length();
00633              }
00634              replace_all_line = line_counter;
00635       }
00636 
00637       setModified(true);
00638     }
00639     else{ // interactive
00640 
00641       //      printf("line_counter %d pos %d col %d\n",line_counter, pos,col);
00642       if( ! (line == line_counter && pos > col ) ){
00643 
00644         setCursorPosition(line_counter, pos + length ,false );
00645 
00646         for(int l = 0 ; l < length; l++){
00647           cursorLeft(true);
00648         }
00649 
00650         setCursorPosition(line_counter, pos ,true );
00651         pattern = s_pattern;
00652 
00653         last_replace = BACKWARD;
00654         can_replace = true;
00655 
00656         return 1;
00657       }
00658     }
00659       }
00660     }
00661   }
00662 
00663   return 0;
00664 
00665 }
00666 
00667 
00668 
00669 
00670 
00672 //
00673 // Find Dialog
00674 //
00675 
00676 class KEdFind::KEdFindPrivate
00677 {
00678 public:
00679     KEdFindPrivate( QWidget *parent ) {
00680     combo = new KHistoryCombo( parent, "value" );
00681     combo->setMaxCount( 20 ); // just some default
00682     }
00683     ~KEdFindPrivate() {
00684     delete combo;
00685     }
00686 
00687     KHistoryCombo *combo;
00688 };
00689 
00690 
00691 KEdFind::KEdFind( QWidget *parent, const char *name, bool modal )
00692   :KDialogBase( parent, name, modal, i18n("Find"),
00693         modal ? User1|Cancel : User1|Close, User1, false, i18n("&Find") )
00694 {
00695   setWFlags( WType_TopLevel );
00696 
00697   QWidget *page = new QWidget( this );
00698   setMainWidget(page);
00699   QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
00700 
00701   d = new KEdFindPrivate( page );
00702 
00703   QString text = i18n("Find:");
00704   QLabel *label = new QLabel( text, page , "find" );
00705   topLayout->addWidget( label );
00706 
00707   d->combo->setMinimumWidth(fontMetrics().maxWidth()*20);
00708   d->combo->setFocus();
00709 
00710   connect(d->combo, SIGNAL(textChanged ( const QString & )),
00711           this,SLOT(textSearchChanged ( const QString & )));
00712 
00713   topLayout->addWidget(d->combo);
00714 
00715   group = new QVButtonGroup( i18n("Options"), page );
00716   topLayout->addWidget( group );
00717 
00718   QHBox* row1 = new QHBox( group );
00719 
00720   text = i18n("Case &sensitive");
00721   sensitive = new QCheckBox( text, row1, "case");
00722   text = i18n("Find &backwards");
00723   direction = new QCheckBox( text, row1, "direction" );
00724 
00725 
00726   enableButton( KDialogBase::User1, !d->combo->currentText().isEmpty() );
00727 
00728   if ( !modal )
00729     connect( this, SIGNAL( closeClicked() ), this, SLOT( slotCancel() ) );
00730 }
00731 
00732 KEdFind::~KEdFind()
00733 {
00734     delete d;
00735 }
00736 
00737 void KEdFind::textSearchChanged ( const QString &text )
00738 {
00739    enableButton( KDialogBase::User1, !text.isEmpty() );
00740 }
00741 
00742 void KEdFind::slotCancel( void )
00743 {
00744   emit done();
00745   KDialogBase::slotCancel();
00746 }
00747 
00748 void KEdFind::slotUser1( void )
00749 {
00750   if( !d->combo->currentText().isEmpty() )
00751   {
00752     d->combo->addToHistory( d->combo->currentText() );
00753     emit search();
00754   }
00755 }
00756 
00757 
00758 QString KEdFind::getText() const
00759 {
00760     return d->combo->currentText();
00761 }
00762 
00763 
00764 /* antlarr: KDE 4: make it const QString & */
00765 void KEdFind::setText(QString string)
00766 {
00767   d->combo->setEditText(string);
00768   d->combo->lineEdit()->selectAll();
00769 }
00770 
00771 void KEdFind::setCaseSensitive( bool b )
00772 {
00773   sensitive->setChecked( b );
00774 }
00775 
00776 bool KEdFind::case_sensitive() const
00777 {
00778   return sensitive->isChecked();
00779 }
00780 
00781 void KEdFind::setDirection( bool b )
00782 {
00783   direction->setChecked( b );
00784 }
00785 
00786 bool KEdFind::get_direction() const
00787 {
00788   return direction->isChecked();
00789 }
00790 
00791 KHistoryCombo * KEdFind::searchCombo() const
00792 {
00793     return d->combo;
00794 }
00795 
00796 
00797 
00799 //
00800 //  Replace Dialog
00801 //
00802 
00803 class KEdReplace::KEdReplacePrivate
00804 {
00805 public:
00806     KEdReplacePrivate( QWidget *parent ) {
00807     searchCombo = new KHistoryCombo( parent, "value" );
00808     replaceCombo = new KHistoryCombo( parent, "replace_value" );
00809 
00810     searchCombo->setMaxCount( 20 ); // just some defaults
00811     replaceCombo->setMaxCount( 20 );
00812     }
00813     ~KEdReplacePrivate() {
00814     delete searchCombo;
00815     delete replaceCombo;
00816     }
00817 
00818     KHistoryCombo *searchCombo, *replaceCombo;
00819 };
00820 
00821 KEdReplace::KEdReplace( QWidget *parent, const char *name, bool modal )
00822   :KDialogBase( parent, name, modal, i18n("Replace"),
00823         modal ? User3|User2|User1|Cancel : User3|User2|User1|Close,
00824                 User3, false,
00825         i18n("Replace &All"), i18n("&Replace"), i18n("&Find") )
00826 {
00827   setWFlags( WType_TopLevel );
00828 
00829   setButtonBoxOrientation( Vertical );
00830 
00831   QFrame *page = makeMainWidget();
00832   QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
00833 
00834   d = new KEdReplacePrivate( page );
00835 
00836   QString text = i18n("Find:");
00837   QLabel *label = new QLabel( text, page, "find" );
00838   topLayout->addWidget( label );
00839 
00840   d->searchCombo->setMinimumWidth(fontMetrics().maxWidth()*20);
00841   d->searchCombo->setFocus();
00842   topLayout->addWidget(d->searchCombo);
00843 
00844   text = i18n("Replace with:");
00845   label = new QLabel( text, page, "replace" );
00846   topLayout->addWidget( label );
00847 
00848   d->replaceCombo->setMinimumWidth(fontMetrics().maxWidth()*20);
00849   topLayout->addWidget(d->replaceCombo);
00850 
00851   connect(d->searchCombo, SIGNAL(textChanged ( const QString & )),
00852           this,SLOT(textSearchChanged ( const QString & )));
00853 
00854   QButtonGroup *group = new QButtonGroup( i18n("Options"), page );
00855   topLayout->addWidget( group );
00856 
00857   QGridLayout *gbox = new QGridLayout( group, 3, 2, spacingHint() );
00858   gbox->addRowSpacing( 0, fontMetrics().lineSpacing() );
00859 
00860   text = i18n("Case &sensitive");
00861   sensitive = new QCheckBox( text, group, "case");
00862   text = i18n("Find &backwards");
00863   direction = new QCheckBox( text, group, "direction" );
00864   gbox->addWidget( sensitive, 1, 0 );
00865   gbox->addWidget( direction, 1, 1 );
00866   gbox->setRowStretch( 2, 10 );
00867 }
00868 
00869 
00870 KEdReplace::~KEdReplace()
00871 {
00872     delete d;
00873 }
00874 
00875 void KEdReplace::textSearchChanged ( const QString &text )
00876 {
00877     bool state=text.isEmpty();
00878     enableButton( KDialogBase::User1, !state );
00879     enableButton( KDialogBase::User2, !state );
00880     enableButton( KDialogBase::User3, !state );
00881 }
00882 
00883 void KEdReplace::slotCancel( void )
00884 {
00885   emit done();
00886   d->searchCombo->clearEdit();
00887   d->replaceCombo->clearEdit();
00888   KDialogBase::slotCancel();
00889 }
00890 
00891 void KEdReplace::slotClose( void )
00892 {
00893   slotCancel();
00894 }
00895 
00896 void KEdReplace::slotUser1( void )
00897 {
00898     if( !d->searchCombo->currentText().isEmpty() )
00899     {
00900         d->replaceCombo->addToHistory( d->replaceCombo->currentText() );
00901         emit replaceAll();
00902     }
00903 }
00904 
00905 
00906 void KEdReplace::slotUser2( void )
00907 {
00908     if( !d->searchCombo->currentText().isEmpty() )
00909     {
00910         d->replaceCombo->addToHistory( d->replaceCombo->currentText() );
00911         emit replace();
00912     }
00913 }
00914 
00915 void KEdReplace::slotUser3( void )
00916 {
00917   if( !d->searchCombo->currentText().isEmpty() )
00918   {
00919     d->searchCombo->addToHistory( d->searchCombo->currentText() );
00920     emit find();
00921   }
00922 }
00923 
00924 
00925 QString KEdReplace::getText()
00926 {
00927     return d->searchCombo->currentText();
00928 }
00929 
00930 
00931 QString KEdReplace::getReplaceText()
00932 {
00933     return d->replaceCombo->currentText();
00934 }
00935 
00936 
00937 /* antlarr: KDE 4: make it const QString & */
00938 void KEdReplace::setText(QString string)
00939 {
00940   d->searchCombo->setEditText(string);
00941   d->searchCombo->lineEdit()->selectAll();
00942 }
00943 
00944 
00945 bool KEdReplace::case_sensitive()
00946 {
00947   return sensitive->isChecked();
00948 }
00949 
00950 
00951 bool KEdReplace::get_direction()
00952 {
00953   return direction->isChecked();
00954 }
00955 
00956 KHistoryCombo * KEdReplace::searchCombo() const
00957 {
00958     return d->searchCombo;
00959 }
00960 
00961 KHistoryCombo * KEdReplace::replaceCombo() const
00962 {
00963     return d->replaceCombo;
00964 }
00965 
00966 
00967 KEdGotoLine::KEdGotoLine( QWidget *parent, const char *name, bool modal )
00968   :KDialogBase( parent, name, modal, i18n("Go to Line"), modal ? Ok|Cancel : Ok|Close, Ok, false )
00969 {
00970   QWidget *page = new QWidget( this );
00971   setMainWidget(page);
00972   QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
00973 
00974   lineNum = new KIntNumInput( 1, page);
00975   lineNum->setRange(1, 1000000, 1, false);
00976   lineNum->setLabel(i18n("Go to line:"), AlignVCenter | AlignLeft);
00977 //  lineNum->setMinimumWidth(fontMetrics().maxWidth()*20);
00978   topLayout->addWidget( lineNum );
00979 
00980   topLayout->addStretch(10);
00981   lineNum->setFocus();
00982 }
00983 
00984 
00985 void KEdGotoLine::selected(int)
00986 {
00987   accept();
00988 }
00989 
00990 
00991 int KEdGotoLine::getLineNumber()
00992 {
00993   return lineNum->value();
00994 }
00995 
00996 
00998 //
00999 // Spell Checking
01000 //
01001 
01002 void KEdit::spellcheck_start()
01003 {
01004    saved_readonlystate = isReadOnly();
01005    setReadOnly(true);
01006 }
01007 
01008 void KEdit::misspelling (const QString &word, const QStringList &, unsigned int pos)
01009 {
01010 
01011   unsigned int l = 0;
01012   unsigned int cnt = 0;
01013   posToRowCol (pos, l, cnt);
01014   setSelection(l, cnt, l, cnt+word.length());
01015 
01016   /*
01017   if (cursorPoint().y()>height()/2)
01018     kspell->moveDlg (10, height()/2-kspell->heightDlg()-15);
01019   else
01020     kspell->moveDlg (10, height()/2 + 15);
01021   */
01022 
01023 }
01024 
01025 //need to use pos for insert, not cur, so forget cur altogether
01026 void KEdit::corrected (const QString &originalword, const QString &newword, unsigned int pos)
01027 {
01028   //we'll reselect the original word in case the user has played with
01029   //the selection in eframe or the word was auto-replaced
01030 
01031   unsigned int l = 0;
01032   unsigned int cnt = 0;
01033 
01034   if( newword != originalword )
01035   {
01036     posToRowCol (pos, l, cnt);
01037     setSelection(l, cnt, l, cnt+originalword.length());
01038 
01039     setReadOnly ( false );
01040     removeSelectedText();
01041     insert(newword);
01042     setReadOnly ( true );
01043   }
01044   else
01045   {
01046     deselect();
01047   }
01048 }
01049 
01050 void KEdit::posToRowCol(unsigned int pos, unsigned int &line, unsigned int &col)
01051 {
01052   for (line = 0; line < static_cast<uint>(numLines()) && col <= pos; line++)
01053   {
01054     col += lineLength(line)+1;
01055   }
01056   line--;
01057   col = pos - col + lineLength(line) + 1;
01058 }
01059 
01060 void KEdit::spellcheck_stop()
01061 {
01062   deselect();
01063 
01064   setReadOnly ( saved_readonlystate);
01065 }
01066 
01067 QString KEdit::selectWordUnderCursor( )
01068 {
01069     int parag;
01070     int pos;
01071     
01072     getCursorPosition(&parag, &pos);
01073     
01074     QString txt = text(parag);
01075     
01076     // Find start
01077     int start = pos;
01078     while( start > 0 )
01079     {
01080        const QChar &ch = txt[start-1];
01081        if (ch.isSpace() || ch.isPunct())
01082           break;
01083        start--;
01084     }
01085     
01086     // Find end
01087     int end = pos;
01088     int len = txt.length();
01089     while( end < len )
01090     {
01091        const QChar &ch = txt[end];
01092        if (ch.isSpace() || ch.isPunct())
01093           break;
01094        end++;
01095     }
01096     setSelection(parag, start, parag, end);
01097     return txt.mid(start, end-start);
01098 }
01099 
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 22 14:23:26 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003