Refactor: jtextbox widget to TextBox class.

This commit is contained in:
David Capello 2012-04-05 23:28:05 -03:00
parent f6f6225b1a
commit 38b5e35351
3 changed files with 56 additions and 58 deletions

View File

@ -52,7 +52,7 @@ Console::Console()
Frame* window = new Frame(false, "Errors Console"); Frame* window = new Frame(false, "Errors Console");
Grid* grid = new Grid(1, false); Grid* grid = new Grid(1, false);
View* view = new View(); View* view = new View();
Widget* textbox = jtextbox_new(NULL, JI_WORDWRAP); TextBox* textbox = new TextBox(NULL, JI_WORDWRAP);
Button* button = new Button("&Cancel"); Button* button = new Button("&Cancel");
if (!grid || !textbox || !button) if (!grid || !textbox || !button)

View File

@ -6,63 +6,49 @@
#include "config.h" #include "config.h"
#include <allegro/keyboard.h> #include "gui/textbox.h"
#include <math.h>
#include "gfx/point.h" #include "gfx/size.h"
#include "gui/hook.h"
#include "gui/intern.h"
#include "gui/manager.h"
#include "gui/message.h" #include "gui/message.h"
#include "gui/rect.h" #include "gui/preferred_size_event.h"
#include "gui/system.h" #include "gui/system.h"
#include "gui/theme.h" #include "gui/theme.h"
#include "gui/view.h" #include "gui/view.h"
#include "gui/widget.h"
static bool textbox_msg_proc(JWidget widget, Message* msg); #include <allegro/keyboard.h>
static void textbox_request_size(JWidget widget, int *w, int *h);
JWidget jtextbox_new(const char *text, int align) TextBox::TextBox(const char* text, int align)
: Widget(JI_TEXTBOX)
{ {
Widget* widget = new Widget(JI_TEXTBOX); setFocusStop(true);
setAlign(align);
jwidget_add_hook(widget, JI_TEXTBOX, textbox_msg_proc, NULL); setText(text);
widget->setFocusStop(true); initTheme();
widget->setAlign(align);
widget->setText(text);
widget->initTheme();
return widget;
} }
static bool textbox_msg_proc(JWidget widget, Message* msg) bool TextBox::onProcessMessage(Message* msg)
{ {
switch (msg->type) { switch (msg->type) {
case JM_REQSIZE:
textbox_request_size(widget, &msg->reqsize.w, &msg->reqsize.h);
return true;
case JM_DRAW: case JM_DRAW:
widget->getTheme()->draw_textbox(widget, &msg->draw.rect); getTheme()->draw_textbox(this, &msg->draw.rect);
return true; return true;
case JM_SIGNAL: case JM_SIGNAL:
if (msg->signal.num == JI_SIGNAL_SET_TEXT) { if (msg->signal.num == JI_SIGNAL_SET_TEXT) {
View* view = View::getView(widget); View* view = View::getView(this);
if (view) if (view)
view->updateView(); view->updateView();
} }
break; break;
case JM_KEYPRESSED: case JM_KEYPRESSED:
if (widget->hasFocus()) { if (hasFocus()) {
View* view = View::getView(widget); View* view = View::getView(this);
if (view) { if (view) {
gfx::Rect vp = view->getViewportBounds(); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll = view->getViewScroll(); gfx::Point scroll = view->getViewScroll();
int textheight = jwidget_get_text_height(widget); int textheight = jwidget_get_text_height(this);
switch (msg->key.scancode) { switch (msg->key.scancode) {
@ -102,7 +88,7 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
break; break;
case KEY_END: case KEY_END:
scroll.y = jrect_h(widget->rc) - vp.h; scroll.y = jrect_h(this->rc) - vp.h;
view->setViewScroll(scroll); view->setViewScroll(scroll);
break; break;
@ -115,9 +101,9 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
break; break;
case JM_BUTTONPRESSED: { case JM_BUTTONPRESSED: {
View* view = View::getView(widget); View* view = View::getView(this);
if (view) { if (view) {
widget->captureMouse(); captureMouse();
jmouse_set_cursor(JI_CURSOR_SCROLL); jmouse_set_cursor(JI_CURSOR_SCROLL);
return true; return true;
} }
@ -125,8 +111,8 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
} }
case JM_MOTION: { case JM_MOTION: {
View* view = View::getView(widget); View* view = View::getView(this);
if (view && widget->hasCapture()) { if (view && hasCapture()) {
gfx::Rect vp = view->getViewportBounds(); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll = view->getViewScroll(); gfx::Point scroll = view->getViewScroll();
@ -141,9 +127,9 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
} }
case JM_BUTTONRELEASED: { case JM_BUTTONRELEASED: {
View* view = View::getView(widget); View* view = View::getView(this);
if (view && widget->hasCapture()) { if (view && hasCapture()) {
widget->releaseMouse(); releaseMouse();
jmouse_set_cursor(JI_CURSOR_NORMAL); jmouse_set_cursor(JI_CURSOR_NORMAL);
return true; return true;
} }
@ -151,11 +137,11 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
} }
case JM_WHEEL: { case JM_WHEEL: {
View* view = View::getView(widget); View* view = View::getView(this);
if (view) { if (view) {
gfx::Point scroll = view->getViewScroll(); gfx::Point scroll = view->getViewScroll();
scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3; scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(this)*3;
view->setViewScroll(scroll); view->setViewScroll(scroll);
} }
@ -163,33 +149,36 @@ static bool textbox_msg_proc(JWidget widget, Message* msg)
} }
} }
return false; return Widget::onProcessMessage(msg);
} }
static void textbox_request_size(JWidget widget, int *w, int *h) void TextBox::onPreferredSize(PreferredSizeEvent& ev)
{ {
/* TODO */ int w = 0;
/* *w = widget->border_width.l + widget->border_width.r; */ int h = 0;
/* *h = widget->border_width.t + widget->border_width.b; */
*w = 0;
*h = 0;
_ji_theme_textbox_draw(NULL, widget, w, h, 0, 0); // TODO is it necessary?
//w = widget->border_width.l + widget->border_width.r;
//h = widget->border_width.t + widget->border_width.b;
if (widget->getAlign() & JI_WORDWRAP) { _ji_theme_textbox_draw(NULL, this, &w, &h, 0, 0);
View* view = View::getView(widget);
int width, min = *w; if (this->getAlign() & JI_WORDWRAP) {
View* view = View::getView(this);
int width, min = w;
if (view) { if (view) {
width = view->getViewportBounds().w; width = view->getViewportBounds().w;
} }
else { else {
width = jrect_w(widget->rc); width = jrect_w(this->rc);
} }
*w = MAX(min, width); w = MAX(min, width);
_ji_theme_textbox_draw(NULL, widget, w, h, 0, 0); _ji_theme_textbox_draw(NULL, this, &w, &h, 0, 0);
*w = min; w = min;
} }
ev.setPreferredSize(gfx::Size(w, h));
} }

View File

@ -7,8 +7,17 @@
#ifndef GUI_TEXTBOX_H_INCLUDED #ifndef GUI_TEXTBOX_H_INCLUDED
#define GUI_TEXTBOX_H_INCLUDED #define GUI_TEXTBOX_H_INCLUDED
#include "gui/base.h" #include "base/compiler_specific.h"
#include "gui/widget.h"
JWidget jtextbox_new(const char *text, int align); class TextBox : public Widget
{
public:
TextBox(const char* text, int align);
protected:
bool onProcessMessage(Message* msg) OVERRIDE;
void onPreferredSize(PreferredSizeEvent& ev) OVERRIDE;
};
#endif #endif