Example #1
0
T* arrayProcessing(T* array) {
	if (!array || !*array || countWordsArray(array) < 2) {
		return NULL;
	}
	T* dst = (T*)malloc(dstlen(strlen(array)+1)*sizeof(T));
	T* dstEnd = dst;
	*dstEnd = 0;
	T* firstWord = findFirstWord(array);
	T* curr = findNextWord(firstWord);
	while(curr) {
		if (cmpWithFirstWord(firstWord, curr)) {
			curr = findNextWord(curr);
			continue;
		}
		else {
			if (dstEnd != dst && *dstEnd != ' ') {
				*(dstEnd++) = ' ';
			}
			dstEnd = wordProcessingArray(curr, dstEnd, wordlen(curr));
			if (dstEnd != dst && *(dstEnd-1) == ' ') {
				dstEnd--;
			}
			*dstEnd = 0;
		}
		curr = findNextWord(curr);
	}
	dstEnd--;
	if (*dstEnd == ' ') {
		*dstEnd = 0;
	}
	return (T*)realloc(dst, (strlen(dst)+1)*sizeof(T));
}
Example #2
0
/// Enter is pressed in the line-edit widget
void FindWidget::enterPressed()
{
    QString str = lineEditRef_->text().trimmed();

    findNextWord();
    //qlog_info() << "TODO: Implement find!" << str ;
}
string VectorPriorityQueue::peek() {
    //If there are no elements in queue, return error to user.
    if(wordList.isEmpty()) {
        error("No words in list.");
    }

    //Otherwise, find next word and return value.
    string nextWord = findNextWord(wordList);
    return nextWord;
}
Example #4
0
T* findFirstWord(T* text) {
	if (!text || !*text) {
		return NULL;
	}
	if (!isWordEnd(*text)) {
		return text;
	}
	else {
		return findNextWord(text);
	}
}
string VectorPriorityQueue::dequeueMin() {
    //If there are no elements in queue, return error to user.
    if(wordList.isEmpty()) {
        error("No words in list");
    }

    //Otherwise, find next word.
    string nextWord = findNextWord(wordList);

    //Remove word from queue.
    wordList.remove(wordIndex);

    //Reset wordIndex value.
    wordIndex = 0;

    return nextWord;
}
Example #6
0
static int fitText(const char *text, WMFont * font, int width, int wrap)
{
	int i, w, beforecrlf, word1, word2;

	/* text length before first cr/lf */
	beforecrlf = strcspn(text, "\n");

	if (!wrap || beforecrlf == 0)
		return beforecrlf;

	w = WMWidthOfString(font, text, beforecrlf);
	if (w <= width) {
		/* text up to first crlf fits */
		return beforecrlf;
	}

	word1 = 0;
	while (1) {
		word2 = word1 + findNextWord(text + word1, beforecrlf - word1);
		if (word2 >= beforecrlf)
			break;
		w = WMWidthOfString(font, text, word2);
		if (w > width)
			break;
		word1 = word2;
	}

	for (i = word1; i < word2; i++) {
		w = WMWidthOfString(font, text, i);
		if (w > width) {
			break;
		}
	}

	/* keep words complete if possible */
	if (!isspace(text[i]) && word1 > 0) {
		i = word1;
	} else if (isspace(text[i]) && i < beforecrlf) {
		/* keep space on current row, so new row has next word in column 1 */
		i++;
	}

	return i;
}
Example #7
0
void testFindNextWord() {
	T* text = NULL;
	CU_ASSERT_PTR_NULL(findNextWord(text));
	text = "";
	CU_ASSERT_PTR_NULL(findNextWord(text));
	text = "bebe";
	CU_ASSERT_PTR_NULL(findNextWord(text));
	text = "bebe be";
	CU_ASSERT_STRING_EQUAL(findNextWord(text), "be");
	text = "bebe be be";
	CU_ASSERT_STRING_EQUAL(findNextWord(text), "be be");
	text = " bebe be";
	CU_ASSERT_STRING_EQUAL(findNextWord(text), "bebe be");
}
Example #8
0
void FindWidget::constructUI()
{

    QHBoxLayout* layout = new QHBoxLayout();// QBoxLayout::LeftToRight );
    layout->setMargin(7);
     setLayout( layout );



    QToolButton* toggleButton = new QToolButton(this);
    toggleButton->setFont( edbeeApp()->iconFont() );
    toggleButton->setText( QChar(icon_repeat) );
    toggleButton->setToolTip(tr("Wrap Around"));
    toggleButton->setCheckable(true);
    layout->addWidget( toggleButton, 0 );

    toggleButton = new QToolButton(this);
    toggleButton->setFont( edbeeApp()->iconFont() );
    toggleButton->setText( QChar(icon_asterisk) );
    toggleButton->setToolTip(tr("Regular Expressions"));
    toggleButton->setCheckable(true);
    layout->addWidget( toggleButton, 0 );

    toggleButton = new QToolButton(this);
    toggleButton->setFont( edbeeApp()->iconFont() );
    toggleButton->setText( QChar(icon_font) );
    toggleButton->setToolTip(tr("Case Sensitive"));
    toggleButton->setCheckable(true);
    layout->addWidget( toggleButton, 0 );




    QLabel* label = new QLabel( tr("Find") );
    layout->addWidget( label, 0, Qt::AlignLeft );


    lineEditRef_ = new QLineEdit(this);
//    lineEditRef_->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred);
//    lineEditRef_->setSizePolicy( QSizePolicy::M);
//    lineEditRef_->setMaximumWidth( 10000 );
    layout->addWidget( lineEditRef_, 1);//, Qt::AlignLeft);
//    layout->insertStretch(1);

    // add the action

    QToolButton* but = new QToolButton( this );
    but->setFont( edbeeApp()->iconFont() );
    but->setText( QChar(icon_caret_left) ); //<
    but->setShortcut( editorRef_->controller()->keyMap()->getSequence("find_prev_match") );
    but->setToolTip( QString(tr("Find Previous (%1)")).arg(but->shortcut().toString()) );
    connect( but, SIGNAL(clicked()), SLOT(findPrevWord()) );
    layout->addWidget( but, 0  );

    but = new QToolButton( this );
    but->setFont( edbeeApp()->iconFont() );
    but->setText( QChar(icon_caret_right) ); // >
    but->setShortcut( editorRef_->controller()->keyMap()->getSequence("find_next_match") );
    but->setToolTip( QString(tr("Find Next (%1)")).arg(but->shortcut().toString()) );
    connect( but, SIGNAL(clicked()), SLOT(findNextWord()) );
    layout->addWidget( but, 0 );


    but = new QToolButton( this );
    but->setFont( edbeeApp()->iconFont() );
    but->setText( QChar(icon_th) ); // >
    but->setShortcut( editorRef_->controller()->keyMap()->getSequence("sel_all_matches") );
    but->setToolTip( QString(tr("Select All (%1)")).arg(but->shortcut().toString()) );
    connect( but, SIGNAL(clicked()), SLOT(selectAllWords()) );
    layout->addWidget( but, 0 );


//    setWindowFlags( Qt::Popup );
}
bool GuiTextEditCtrl::onKeyDown(const GuiEvent &event)
{
   if ( !isActive() || !isAwake() )
      return false;

   S32 stringLen = mTextBuffer.length();
   setUpdate();

   // Ugly, but now I'm cool like MarkF.
   if(event.keyCode == KEY_BACKSPACE)
      goto dealWithBackspace;
   
   if ( event.modifier & SI_SHIFT )
   {

      // Added support for word jump selection.

      if ( event.modifier & SI_CTRL )
      {
         switch ( event.keyCode )
         {
            case KEY_LEFT:
            {
               S32 newpos = findPrevWord();               

               if ( mBlockStart == mBlockEnd )
               {
                  // There was not already a selection so start a new one.
                  mBlockStart = newpos;
                  mBlockEnd = mCursorPos;
               }
               else
               {
                  // There was a selection already...
                  // In this case the cursor MUST be at either the
                  // start or end of that selection.

                  if ( mCursorPos == mBlockStart )
                  {
                     // We are at the start block and traveling left so
                     // just extend the start block farther left.                     
                     mBlockStart = newpos;
                  }
                  else
                  {
                     // We are at the end block BUT traveling left
                     // back towards the start block...

                     if ( newpos > mBlockStart )
                     {
                        // We haven't overpassed the existing start block
                        // so just trim back the end block.
                        mBlockEnd = newpos;
                     }
                     else if ( newpos == mBlockStart )
                     {
                        // We are back at the start, so no more selection.
                        mBlockEnd = mBlockStart = 0;
                     }
                     else
                     {
                        // Only other option, we just backtracked PAST
                        // our original start block.
                        // So the new position becomes the start block
                        // and the old start block becomes the end block.
                        mBlockEnd = mBlockStart;
                        mBlockStart = newpos;
                     }
                  }
               }
                              
               mCursorPos = newpos;

               return true;
            }

            case KEY_RIGHT:
            {
               S32 newpos = findNextWord();               

               if ( mBlockStart == mBlockEnd )
               {
                  // There was not already a selection so start a new one.
                  mBlockStart = mCursorPos;
                  mBlockEnd = newpos;
               }
               else
               {
                  // There was a selection already...
                  // In this case the cursor MUST be at either the
                  // start or end of that selection.

                  if ( mCursorPos == mBlockEnd )
                  {
                     // We are at the end block and traveling right so
                     // just extend the end block farther right.                     
                     mBlockEnd = newpos;
                  }
                  else
                  {
                     // We are at the start block BUT traveling right
                     // back towards the end block...

                     if ( newpos < mBlockEnd )
                     {
                        // We haven't overpassed the existing end block
                        // so just trim back the start block.
                        mBlockStart = newpos;
                     }
                     else if ( newpos == mBlockEnd )
                     {
                        // We are back at the end, so no more selection.
                        mBlockEnd = mBlockStart = 0;
                     }
                     else
                     {
                        // Only other option, we just backtracked PAST
                        // our original end block.
                        // So the new position becomes the end block
                        // and the old end block becomes the start block.
                        mBlockStart = mBlockEnd;
                        mBlockEnd = newpos;
                     }
                  }
               }

               mCursorPos = newpos;

               return true;
            }
            
            default:
               break;
         }
      }

      // End support for word jump selection.


        switch ( event.keyCode )
        {
            case KEY_TAB:
               if ( mTabComplete )
               {
				  onTabComplete_callback("1");
                  return true;
               }
			   break; // We don't want to fall through if we don't handle the TAB here.

            case KEY_HOME:
               mBlockStart = 0;
               mBlockEnd = mCursorPos;
               mCursorPos = 0;
               return true;

            case KEY_END:
                mBlockStart = mCursorPos;
                mBlockEnd = stringLen;
                mCursorPos = stringLen;
                return true;

            case KEY_LEFT:
                if ((mCursorPos > 0) & (stringLen > 0))
                {
                    //if we already have a selected block
                    if (mCursorPos == mBlockEnd)
                    {
                        mCursorPos--;
                        mBlockEnd--;
                        if (mBlockEnd == mBlockStart)
                        {
                            mBlockStart = 0;
                            mBlockEnd = 0;
                        }
                    }
                    else {
                        mCursorPos--;
                        mBlockStart = mCursorPos;

                        if (mBlockEnd == 0)
                        {
                            mBlockEnd = mCursorPos + 1;
                        }
                    }
                }
                return true;

            case KEY_RIGHT:
                if (mCursorPos < stringLen)
                {
                    if ((mCursorPos == mBlockStart) && (mBlockEnd > 0))
                    {
                        mCursorPos++;
                        mBlockStart++;
                        if (mBlockStart == mBlockEnd)
                        {
                            mBlockStart = 0;
                            mBlockEnd = 0;
                        }
                    }
                    else
                    {
                        if (mBlockEnd == 0)
                        {
                            mBlockStart = mCursorPos;
                            mBlockEnd = mCursorPos;
                        }
                        mCursorPos++;
                        mBlockEnd++;
                    }
                }
                return true;

				case KEY_RETURN:
				case KEY_NUMPADENTER:
           
					return dealWithEnter(false);

            default:
               break;
        }
    }
   else if (event.modifier & SI_CTRL)
   {
      switch(event.keyCode)
      {
#if defined(TORQUE_OS_MAC)
         // Added UNIX emacs key bindings - just a little hack here...

         // Ctrl-B - move one character back
         case KEY_B:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_LEFT;
            return(onKeyDown(new_event));
         }

         // Ctrl-F - move one character forward
         case KEY_F:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_RIGHT;
            return(onKeyDown(new_event));
         }

         // Ctrl-A - move to the beginning of the line
         case KEY_A:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_HOME;
            return(onKeyDown(new_event));
         }

         // Ctrl-E - move to the end of the line
         case KEY_E:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_END;
            return(onKeyDown(new_event));
         }

         // Ctrl-P - move backward in history
         case KEY_P:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_UP;
            return(onKeyDown(new_event));
         }

         // Ctrl-N - move forward in history
         case KEY_N:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_DOWN;
            return(onKeyDown(new_event));
         }

         // Ctrl-D - delete under cursor
         case KEY_D:
         { 
            GuiEvent new_event;
            new_event.modifier = 0;
            new_event.keyCode = KEY_DELETE;
            return(onKeyDown(new_event));
         }

         case KEY_U:
         { 
            GuiEvent new_event;
            new_event.modifier = SI_CTRL;
            new_event.keyCode = KEY_DELETE;
            return(onKeyDown(new_event));
         }

         // End added UNIX emacs key bindings
#endif

         // Adding word jump navigation.

         case KEY_LEFT:
         {

            mCursorPos = findPrevWord();
            mBlockStart = 0;
            mBlockEnd = 0;
            return true;
         }

         case KEY_RIGHT:
         {
            mCursorPos = findNextWord();
            mBlockStart = 0;
            mBlockEnd = 0;
            return true;
         }         
         
#if !defined(TORQUE_OS_MAC)
         // Select all
         case KEY_A:
         {
            selectAllText();
            return true;
         }

         // windows style cut / copy / paste / undo keybinds
         case KEY_C:
         case KEY_X:
         {
            // copy, and cut the text if we hit ctrl-x
            onCopy( event.keyCode==KEY_X );
            return true;
         }
         case KEY_V:
         {
            onPaste();

            // Execute the console command!
            execConsoleCallback();
            return true;
         }

         case KEY_Z:
            if (! mDragHit)
            {
               onUndo();
               return true;
            }
#endif

         case KEY_DELETE:
         case KEY_BACKSPACE:
            //save the current state
            saveUndoState();

            //delete everything in the field
            mTextBuffer.set("");
            mCursorPos  = 0;
            mBlockStart = 0;
            mBlockEnd   = 0;

            execConsoleCallback();
            onChangeCursorPos(); //.logicking
            return true;
         default:
            break;
      }
   }
#if defined(TORQUE_OS_MAC)
   // mac style cut / copy / paste / undo keybinds
   else if (event.modifier & SI_ALT)
   {
      // Mac command key maps to alt in torque.

      // Added Mac cut/copy/paste/undo keys
      switch(event.keyCode)
      {
         // Select all
         case KEY_A:
         {
            selectAllText();
            return true;
         }
         case KEY_C:
         case KEY_X:
         {
            // copy, and cut the text if we hit cmd-x
            onCopy( event.keyCode==KEY_X );
            return true;
         }
         case KEY_V:
         {
            onPaste();

            // Execute the console command!
            execConsoleCallback();

            return true;
         }
            
         case KEY_Z:
            if (! mDragHit)
            {
               onUndo();
               return true;
            }
            
         default:
            break;
      }
   }
#endif
   else
   {
      switch(event.keyCode)
      {
         case KEY_ESCAPE:
            if( mEscapeCommand.isNotEmpty() )
            {
               evaluate( mEscapeCommand );
               return( true );
            }
            return( Parent::onKeyDown( event ) );

         case KEY_RETURN:
         case KEY_NUMPADENTER:
           
				return dealWithEnter(true);

         case KEY_UP:
         {
            if( mHistorySize > 0 )
            {
               if(mHistoryDirty)
               {
                  updateHistory(&mTextBuffer, false);
                  mHistoryDirty = false;
               }

               mHistoryIndex--;
               
               if(mHistoryIndex >= 0 && mHistoryIndex <= mHistoryLast)
                  setText(mHistoryBuf[mHistoryIndex]);
               else if(mHistoryIndex < 0)
                  mHistoryIndex = 0;
            }
               
            return true;
         }

         case KEY_DOWN:
         {
            if( mHistorySize > 0 )
            {
               if(mHistoryDirty)
               {
                  updateHistory(&mTextBuffer, false);
                  mHistoryDirty = false;
               }
               mHistoryIndex++;
               if(mHistoryIndex > mHistoryLast)
               {
                  mHistoryIndex = mHistoryLast + 1;
                  setText("");
               }
               else
                  setText(mHistoryBuf[mHistoryIndex]);
            }
            return true;
         }

         case KEY_LEFT:
            
            // If we have a selection put the cursor to the left side of it.
            if ( mBlockStart != mBlockEnd )
            {
               mCursorPos = mBlockStart;
               mBlockStart = mBlockEnd = 0;
            }
            else
            {
               mBlockStart = mBlockEnd = 0;
               mCursorPos = getMax( mCursorPos - 1, 0 );               
            }

            return true;

         case KEY_RIGHT:

            // If we have a selection put the cursor to the right side of it.            
            if ( mBlockStart != mBlockEnd )
            {
               mCursorPos = mBlockEnd;
               mBlockStart = mBlockEnd = 0;
            }
            else
            {
               mBlockStart = mBlockEnd = 0;
               mCursorPos = getMin( mCursorPos + 1, stringLen );               
            }

            return true;

         case KEY_BACKSPACE:
dealWithBackspace:
            //save the current state
            saveUndoState();

            if (mBlockEnd > 0)
            {
               mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
               mCursorPos  = mBlockStart;
               mBlockStart = 0;
               mBlockEnd   = 0;
               mHistoryDirty = true;

               // Execute the console command!
               execConsoleCallback();

            }
            else if (mCursorPos > 0)
            {
               mTextBuffer.cut(mCursorPos-1, 1);
               mCursorPos--;
               mHistoryDirty = true;

               // Execute the console command!
               execConsoleCallback();
            }
            return true;

         case KEY_DELETE:
            //save the current state
            saveUndoState();

            if (mBlockEnd > 0)
            {
               mHistoryDirty = true;
               mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);

               mCursorPos = mBlockStart;
               mBlockStart = 0;
               mBlockEnd = 0;

               // Execute the console command!
               execConsoleCallback();
            }
            else if (mCursorPos < stringLen)
            {
               mHistoryDirty = true;
               mTextBuffer.cut(mCursorPos, 1);

               // Execute the console command!
               execConsoleCallback();
            }
            return true;

         case KEY_INSERT:
            mInsertOn = !mInsertOn;
            return true;

         case KEY_HOME:
            mBlockStart = 0;
            mBlockEnd   = 0;
            mCursorPos  = 0;
            return true;

         case KEY_END:
            mBlockStart = 0;
            mBlockEnd   = 0;
            mCursorPos  = stringLen;
            return true;
      
         default:
            break;

         }
   }

   switch ( event.keyCode )
   {
      case KEY_TAB:
         if ( mTabComplete )
         {
			onTabComplete_callback("0");
            return( true );
         }
      case KEY_UP:
      case KEY_DOWN:
      case KEY_ESCAPE:
         return Parent::onKeyDown( event );
      default:
         break;
   }
         
   // Handle character input events.

   if( mProfile->mFont->isValidChar( event.ascii ) )
   {
      handleCharInput( event.ascii );
      return true;
   }

   // Or eat it if that's appropriate.
   if( mSinkAllKeyEvents )
      return true;

   // Not handled - pass the event to it's parent.
   return Parent::onKeyDown( event );
}
Example #10
0
bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
{
    switch (evt)
    {
        case Keyboard::KEY_PRESS:
        {
            switch (key)
            {
            	case Keyboard::KEY_SHIFT:
            	{
                    _shiftPressed = true;
                    break;
            	}
                case Keyboard::KEY_CTRL:
                {
                    _ctrlPressed = true;
                    break;
                }
                case Keyboard::KEY_HOME:
                {
                    _caretLocation = 0;
                    break;
                }
                case Keyboard::KEY_END:
                {
                    _caretLocation = _text.length();
                    break;
                }
                case Keyboard::KEY_DELETE:
                {
                    if (_caretLocation < _text.length())
                    {
                        int newCaretLocation;
                        if (_ctrlPressed)
                        {
                            newCaretLocation = findNextWord(getDisplayedText(), _caretLocation, false);
                        }
                        else
                        {
                            newCaretLocation = _caretLocation + 1;
                        }
                        _text.erase(_caretLocation, newCaretLocation - _caretLocation);
                        notifyListeners(Control::Listener::TEXT_CHANGED);
                    }
                    break;
                }
                case Keyboard::KEY_TAB:
                {
                    // Allow tab to move the focus forward.
                    return false;
                }
                case Keyboard::KEY_LEFT_ARROW:
                {
                    if (_caretLocation > 0)
                    {
                        if (_ctrlPressed)
                        {
                            _caretLocation = findNextWord(getDisplayedText(), _caretLocation, true);
                        }
                        else
                        {
                            --_caretLocation;
                        }
                    }
                    break;
                }
                case Keyboard::KEY_RIGHT_ARROW:
                {
                    if (_caretLocation < _text.length())
                    {
                        if (_ctrlPressed)
                        {
                            _caretLocation = findNextWord(getDisplayedText(), _caretLocation, false);
                        }
                        else
                        {
                            ++_caretLocation;
                        }
                    }
                    break;
                }
                case Keyboard::KEY_UP_ARROW:
                {
                    // TODO: Support multiline
                    break;
                }
                case Keyboard::KEY_DOWN_ARROW:
                {
                    // TODO: Support multiline
                    break;
                }
                case Keyboard::KEY_BACKSPACE:
                {
                    if (_caretLocation > 0)
                    {
                        int newCaretLocation;
                        if (_ctrlPressed)
                        {
                            newCaretLocation = findNextWord(getDisplayedText(), _caretLocation, true);
                        }
                        else
                        {
                            newCaretLocation = _caretLocation - 1;
                        }
                        _text.erase(newCaretLocation, _caretLocation - newCaretLocation);
                        _caretLocation = newCaretLocation;
                        notifyListeners(Control::Listener::TEXT_CHANGED);
                    }
                    break;
                }
            }
            break;
        }

        case Keyboard::KEY_CHAR:
        {
            switch (key)
            {
                case Keyboard::KEY_RETURN:
                    // TODO: Support multi-line
                    break;
                case Keyboard::KEY_ESCAPE:
                    break;
                case Keyboard::KEY_BACKSPACE:
                    break;
                case Keyboard::KEY_TAB:
                    // Allow tab to move the focus forward.
                    return false;
                default:
                {
                    // Insert character into string, only if our font supports this character
                    if (_shiftPressed && islower(key))
                    {
                        key = toupper(key);
                    }
                    // Insert character into string, only if our font supports this character
                    if (_font && _font->isCharacterSupported(key))
                    {
                        if (_caretLocation <= _text.length())
                        {
                            _text.insert(_caretLocation, 1, (char)key);
                            ++_caretLocation;
                        }

                        notifyListeners(Control::Listener::TEXT_CHANGED);
                    }
                    break;
                }
            
                break;
            }
            break;
        }
        case Keyboard::KEY_RELEASE:
            switch (key)
            {
            	case Keyboard::KEY_SHIFT:
            	{
                    _shiftPressed = false;
                    break;
             	 }
                case Keyboard::KEY_CTRL:
                {
                    _ctrlPressed = false;
                    break;
                }
            }
    }

    _lastKeypress = key;

    return Label::keyEvent(evt, key);
}