Files
haiku-beta6/src/kits/interface/TextInput.cpp
T

245 lines
6.5 KiB
C++
Raw Normal View History

2002-07-09 12:24:59 +00:00
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: TextInput.cpp
// Author: Frans van Nispen ([email protected])
// Description: The BTextView derivative owned by an instance of
// BTextControl.
//------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <stdio.h>
2003-06-19 23:58:57 +00:00
#include <stdlib.h>
#include <string.h>
2002-07-09 12:24:59 +00:00
// System Includes -------------------------------------------------------------
#include <InterfaceDefs.h>
#include <TextControl.h>
#include <Window.h>
#include <Message.h>
#include <TextView.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
#include "TextInput.h"
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
_BTextInput_::_BTextInput_(BRect frame, BRect textRect, uint32 resizeMask,
uint32 flags)
: BTextView(frame, "_input_", textRect, resizeMask, flags),
fPreviousText(NULL),
fBool(false)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
MakeResizable(true);
}
//------------------------------------------------------------------------------
_BTextInput_::_BTextInput_(BMessage *archive)
: BTextView(archive),
fPreviousText(NULL),
fBool(false)
{
MakeResizable(true);
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
_BTextInput_::~_BTextInput_()
{
2003-06-16 07:27:24 +00:00
if (fPreviousText)
free(fPreviousText);
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
BArchivable *_BTextInput_::Instantiate(BMessage *archive)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
if (validate_instantiation(archive, "_BTextInput_"))
return new _BTextInput_(archive);
2002-07-09 12:24:59 +00:00
else
2003-06-16 07:27:24 +00:00
return NULL;
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
status_t _BTextInput_::Archive(BMessage *data, bool deep) const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
return BTextView::Archive(data, true);
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
void _BTextInput_::FrameResized(float width, float height)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BTextView::FrameResized(width, height);
AlignTextRect();
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
void _BTextInput_::KeyDown(const char* bytes, int32 numBytes)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
switch (*bytes)
{
case B_ENTER:
{
if (!TextControl()->IsEnabled())
break;
if(strcmp(Text(), fPreviousText) != 0)
{
TextControl()->Invoke();
free(fPreviousText);
fPreviousText = strdup(Text());
}
SelectAll();
break;
}
case B_TAB:
BView::KeyDown(bytes, numBytes);
break;
default:
BTextView::KeyDown(bytes, numBytes);
}
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
void _BTextInput_::MakeFocus(bool state)
{
2003-06-16 07:27:24 +00:00
if (state == IsFocus())
return;
2002-07-09 12:24:59 +00:00
BTextView::MakeFocus(state);
2003-06-16 07:27:24 +00:00
2002-07-09 12:24:59 +00:00
if (state)
{
2003-06-16 07:27:24 +00:00
SetInitialText();
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
fBool = true;
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (Window())
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BMessage *msg = Window()->CurrentMessage();
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (msg && msg->what == B_KEY_DOWN)
2002-07-09 12:24:59 +00:00
SelectAll();
}
}
else
{
2003-06-16 07:27:24 +00:00
if (strcmp(Text(), fPreviousText) != 0)
TextControl()->Invoke();
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
free(fPreviousText);
fPreviousText = NULL;
fBool = false;
if (Window())
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BMessage *msg = Window()->CurrentMessage();
if (msg && msg->what == B_MOUSE_DOWN)
{
Select(0, 0);
}
2002-07-09 12:24:59 +00:00
}
}
2003-06-16 07:27:24 +00:00
if (Window())
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
Draw(Bounds());
Flush();
2002-07-09 12:24:59 +00:00
}
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
void _BTextInput_::AlignTextRect()
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
// TODO
}
//------------------------------------------------------------------------------
void _BTextInput_::SetInitialText()
{
if (fPreviousText)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
free(fPreviousText);
fPreviousText = NULL;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:27:24 +00:00
if (Text())
fPreviousText = strdup(Text());
}
//------------------------------------------------------------------------------
void _BTextInput_::Paste(BClipboard *clipboard)
{
BTextView::Paste(clipboard);
Invalidate();
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
void _BTextInput_::InsertText(const char *inText, int32 inLength,
int32 inOffset, const text_run_array *inRuns)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
char *buffer = NULL;
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (strpbrk(inText, "\r\n") && inLength <= 1024)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
buffer = (char*)malloc(inLength);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (buffer)
{
strcpy(buffer, inText);
for (int32 i = 0; i < inLength; i++)
if (buffer[i] == '\r' || buffer[i] == '\n')
buffer[i] = ' ';
}
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:27:24 +00:00
BTextView::InsertText(buffer ? buffer : inText, inLength, inOffset,
inRuns);
TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
B_CONTROL_MODIFIED);
if (buffer)
free(buffer);
2002-07-09 12:24:59 +00:00
}
//------------------------------------------------------------------------------
2003-06-16 07:27:24 +00:00
void _BTextInput_::DeleteText(int32 fromOffset, int32 toOffset)
{
BTextView::DeleteText(fromOffset, toOffset);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
B_CONTROL_MODIFIED);
}
//------------------------------------------------------------------------------
BTextControl *_BTextInput_::TextControl()
{
BTextControl *textControl;
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (Parent())
textControl = dynamic_cast<BTextControl*>(Parent());
else
textControl = NULL;
if (!textControl)
debugger("_BTextInput_ should have a BTextControl as parent");
return textControl;
}
//------------------------------------------------------------------------------