Refactor the entire view widget to View, Viewport, and ScrollBar C++ classes.

This commit is contained in:
David Capello 2011-02-20 18:35:21 -03:00
parent 88112b7ffe
commit ad12af7c8b
38 changed files with 905 additions and 980 deletions

View File

@ -161,7 +161,7 @@ int App::run()
{ {
// Initialize GUI interface // Initialize GUI interface
if (isGui()) { if (isGui()) {
Widget* view; View* view;
Editor* editor; Editor* editor;
PRINTF("GUI mode\n"); PRINTF("GUI mode\n");
@ -197,7 +197,7 @@ int App::run()
jwidget_expansive(view, true); jwidget_expansive(view, true);
/* prepare the first editor */ /* prepare the first editor */
jview_attach(view, editor); view->attachToView(editor);
/* setup the menus */ /* setup the menus */
jmenubar_set_menu(menubar, get_root_menu()); jmenubar_set_menu(menubar, get_root_menu());

View File

@ -223,7 +223,7 @@ void PaletteEditorCommand::onLoadParams(Params* params)
void PaletteEditorCommand::onExecute(Context* context) void PaletteEditorCommand::onExecute(Context* context)
{ {
Widget* palette_editor_view; View* palette_editor_view;
Widget* select_rgb; Widget* select_rgb;
Widget* select_hsv; Widget* select_hsv;
Button* expand_button; Button* expand_button;
@ -297,8 +297,8 @@ void PaletteEditorCommand::onExecute(Context* context)
palette_editor = new PaletteView(true); palette_editor = new PaletteView(true);
palette_editor->setBoxSize(4*jguiscale()); palette_editor->setBoxSize(4*jguiscale());
jview_attach(palette_editor_view, palette_editor); palette_editor_view->attachToView(palette_editor);
jview_maxsize(palette_editor_view); palette_editor_view->makeVisibleAllScrollableArea();
// Set palette editor columns // Set palette editor columns
palette_editor->setColumns(16); palette_editor->setColumns(16);

View File

@ -82,8 +82,7 @@ void PreviewCommand::onExecute(Context* context)
SpriteWriter sprite(editor->getSprite()); SpriteWriter sprite(editor->getSprite());
const Palette* pal = sprite->getCurrentPalette(); const Palette* pal = sprite->getCurrentPalette();
JWidget view = jwidget_get_view(editor); View* view = View::getView(editor);
int scroll_x, scroll_y;
int u, v, x, y; int u, v, x, y;
int index_bg_color = -1; int index_bg_color = -1;
TiledMode tiled = context->getSettings()->getTiledMode(); TiledMode tiled = context->getSettings()->getTiledMode();
@ -94,8 +93,8 @@ void PreviewCommand::onExecute(Context* context)
// Clear extras (e.g. pen preview) // Clear extras (e.g. pen preview)
sprite->destroyExtraCel(); sprite->destroyExtraCel();
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
jview_get_scroll(view, &scroll_x, &scroll_y); gfx::Point scroll = view->getViewScroll();
int old_mouse_x = jmouse_x(0); int old_mouse_x = jmouse_x(0);
int old_mouse_y = jmouse_y(0); int old_mouse_y = jmouse_y(0);
@ -103,8 +102,8 @@ void PreviewCommand::onExecute(Context* context)
jmouse_set_cursor(JI_CURSOR_NULL); jmouse_set_cursor(JI_CURSOR_NULL);
jmouse_set_position(JI_SCREEN_W/2, JI_SCREEN_H/2); jmouse_set_position(JI_SCREEN_W/2, JI_SCREEN_H/2);
int pos_x = - scroll_x + vp->x1 + editor->editor_get_offset_x(); int pos_x = - scroll.x + vp.x + editor->editor_get_offset_x();
int pos_y = - scroll_y + vp->y1 + editor->editor_get_offset_y(); int pos_y = - scroll.y + vp.y + editor->editor_get_offset_y();
int delta_x = 0; int delta_x = 0;
int delta_y = 0; int delta_y = 0;
@ -236,7 +235,6 @@ void PreviewCommand::onExecute(Context* context)
jmouse_set_cursor(JI_CURSOR_NORMAL); jmouse_set_cursor(JI_CURSOR_NORMAL);
jmanager_refresh_screen(); jmanager_refresh_screen();
jrect_free(vp);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -83,9 +83,11 @@ bool ColorCurveCommand::onEnabled(Context* context)
void ColorCurveCommand::onExecute(Context* context) void ColorCurveCommand::onExecute(Context* context)
{ {
const CurrentSpriteReader sprite(context); const CurrentSpriteReader sprite(context);
JWidget button_ok; Widget* button_ok;
JWidget view_curve, curve_editor; Widget* curve_editor;
JWidget box_target, target_button; Widget* box_target;
Widget* target_button;
View* view_curve;
if (!the_curve) { if (!the_curve) {
/* default curve */ /* default curve */
@ -118,7 +120,7 @@ void ColorCurveCommand::onExecute(Context* context)
if (get_config_bool("ColorCurve", "Preview", true)) if (get_config_bool("ColorCurve", "Preview", true))
check_preview->setSelected(true); check_preview->setSelected(true);
jview_attach(view_curve, curve_editor); view_curve->attachToView(curve_editor);
jwidget_set_min_size(view_curve, 128, 64); jwidget_set_min_size(view_curve, 128, 64);
jwidget_add_child(box_target, target_button); jwidget_add_child(box_target, target_button);

View File

@ -97,7 +97,8 @@ void ConvolutionMatrixCommand::onExecute(Context* context)
{ {
const CurrentSpriteReader sprite(context); const CurrentSpriteReader sprite(context);
Widget* button_ok; Widget* button_ok;
Widget* view_convmatr, *list_convmatr; View* view_convmatr;
Widget *list_convmatr;
Widget* box_target; Widget* box_target;
Button* reload, *generate; Button* reload, *generate;
@ -126,7 +127,7 @@ void ConvolutionMatrixCommand::onExecute(Context* context)
if (context->getSettings()->getTiledMode() != TILED_NONE) if (context->getSettings()->getTiledMode() != TILED_NONE)
check_tiled->setSelected(true); check_tiled->setSelected(true);
jview_attach(view_convmatr, list_convmatr); view_convmatr->attachToView(list_convmatr);
jwidget_set_min_size(view_convmatr, 128, 64); jwidget_set_min_size(view_convmatr, 128, 64);
jwidget_add_child(box_target, target_button); jwidget_add_child(box_target, target_button);
@ -222,7 +223,7 @@ static bool reload_select_hook(Widget* listbox)
/* re-fill the list */ /* re-fill the list */
listbox_fill_convmatg(listbox); listbox_fill_convmatg(listbox);
listbox_select_current_convmatr(listbox); listbox_select_current_convmatr(listbox);
jview_update(jwidget_get_view(listbox)); View::getView(listbox)->updateView();
return true; /* do not close */ return true; /* do not close */
} }
@ -262,8 +263,8 @@ static bool generate_select_hook()
curvedit_x = curve_editor_new(curve_x, -200, -200, 200, 200); curvedit_x = curve_editor_new(curve_x, -200, -200, 200, 200);
curvedit_y = curve_editor_new(curve_y, -200, -200, 200, 200); curvedit_y = curve_editor_new(curve_y, -200, -200, 200, 200);
jview_attach(view_x, curvedit_x); view_x->attachToView(curvedit_x);
jview_attach(view_y, curvedit_y); view_y->attachToView(curvedit_y);
jwidget_set_min_size(view_x, 64, 64); jwidget_set_min_size(view_x, 64, 64);
jwidget_set_min_size(view_y, 64, 64); jwidget_set_min_size(view_y, 64, 64);

View File

@ -51,7 +51,7 @@ Console::Console()
else { else {
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);
Widget* view = jview_new(); View* view = new View();
Widget* textbox = jtextbox_new(NULL, JI_WORDWRAP); Widget* textbox = jtextbox_new(NULL, JI_WORDWRAP);
Button* button = new Button("&Cancel"); Button* button = new Button("&Cancel");
@ -61,7 +61,7 @@ Console::Console()
// The "button" closes the console // The "button" closes the console
button->Click.connect(Bind<void>(&Frame::closeWindow, window, button)); button->Click.connect(Bind<void>(&Frame::closeWindow, window, button));
jview_attach(view, textbox); view->attachToView(textbox);
jwidget_set_min_size(button, 60, 0); jwidget_set_min_size(button, 60, 0);

View File

@ -424,7 +424,7 @@ static bool anieditor_msg_proc(JWidget widget, JMessage msg)
anieditor->scroll_x+jmouse_x(1)-jmouse_x(0), anieditor->scroll_x+jmouse_x(1)-jmouse_x(0),
anieditor->scroll_y+jmouse_y(1)-jmouse_y(0), true); anieditor->scroll_y+jmouse_y(1)-jmouse_y(0), true);
jmouse_control_infinite_scroll(widget->rc); jmouse_control_infinite_scroll(widget->getBounds());
return true; return true;
} }
/* if the mouse pressed the mouse's button in the separator, we /* if the mouse pressed the mouse's button in the separator, we

View File

@ -169,7 +169,7 @@ base::string ase_file_selector(const base::string& message,
goforward->Click.connect(Bind<void>(&goforward_command, goforward)); goforward->Click.connect(Bind<void>(&goforward_command, goforward));
goup->Click.connect(Bind<void>(&goup_command, goup)); goup->Click.connect(Bind<void>(&goup_command, goup));
JWidget view = jview_new(); View* view = new View();
fileview = fileview_new(start_folder, exts); fileview = fileview_new(start_folder, exts);
jwidget_add_hook(fileview, -1, fileview_msg_proc, NULL); jwidget_add_hook(fileview, -1, fileview_msg_proc, NULL);
@ -179,7 +179,7 @@ base::string ase_file_selector(const base::string& message,
fileview->setName("fileview"); fileview->setName("fileview");
jview_attach(view, fileview); view->attachToView(fileview);
jwidget_expansive(view, true); jwidget_expansive(view, true);
jwidget_add_child(box, view); jwidget_add_child(box, view);
@ -229,7 +229,7 @@ base::string ase_file_selector(const base::string& message,
JWidget ok = jwidget_find_name(window, "ok"); JWidget ok = jwidget_find_name(window, "ok");
// update the view // update the view
jview_update(jwidget_get_view(fileview)); View::getView(fileview)->updateView();
// open the window and run... the user press ok? // open the window and run... the user press ok?
again: again:

View File

@ -43,7 +43,7 @@ void ji_show_repo_dlg(RepoDlg *repo_dlg)
Frame* window = new Frame(false, repo_dlg->title); Frame* window = new Frame(false, repo_dlg->title);
Box* box1 = new Box(JI_HORIZONTAL); Box* box1 = new Box(JI_HORIZONTAL);
Box* box2 = new Box(JI_VERTICAL); Box* box2 = new Box(JI_VERTICAL);
Widget* view = jview_new(); View* view = new View();
repo_dlg->listbox = jlistbox_new(); repo_dlg->listbox = jlistbox_new();
repo_dlg->button_use = new Button(repo_dlg->use_text); repo_dlg->button_use = new Button(repo_dlg->use_text);
repo_dlg->button_add = new Button("&Add"); repo_dlg->button_add = new Button("&Add");
@ -61,7 +61,7 @@ void ji_show_repo_dlg(RepoDlg *repo_dlg)
jwidget_magnetic(repo_dlg->button_use, true); jwidget_magnetic(repo_dlg->button_use, true);
jwidget_expansive(view, true); jwidget_expansive(view, true);
jview_attach(view, repo_dlg->listbox); view->attachToView(repo_dlg->listbox);
jwidget_set_min_size(view, JI_SCREEN_W*25/100, JI_SCREEN_H*25/100); jwidget_set_min_size(view, JI_SCREEN_W*25/100, JI_SCREEN_H*25/100);
/* fill the list */ /* fill the list */
@ -111,7 +111,7 @@ static void fill_listbox(RepoDlg *repo_dlg)
if (repo_dlg->load_listbox) if (repo_dlg->load_listbox)
(*repo_dlg->load_listbox)(repo_dlg); (*repo_dlg->load_listbox)(repo_dlg);
jview_update(jwidget_get_view(repo_dlg->listbox)); View::getView(repo_dlg->listbox)->updateView();
} }
static void kill_listbox(RepoDlg *repo_dlg) static void kill_listbox(RepoDlg *repo_dlg)
@ -197,7 +197,7 @@ static void add_command(Button* widget, RepoDlg* repo_dlg)
if (added) { if (added) {
/* update the list-box */ /* update the list-box */
jview_update(jwidget_get_view(repo_dlg->listbox)); View::getView(repo_dlg->listbox)->updateView();
/* select the last item */ /* select the last item */
jlistbox_select_index(repo_dlg->listbox, jlistbox_select_index(repo_dlg->listbox,
@ -242,7 +242,7 @@ static void delete_command(Button* widget, RepoDlg* repo_dlg)
} }
/* update the list-box */ /* update the list-box */
jview_update(jwidget_get_view(repo_dlg->listbox)); View::getView(repo_dlg->listbox)->updateView();
} }
if (!ret) if (!ret)

View File

@ -174,14 +174,12 @@ void effect_begin_for_preview(Effect *effect)
{ {
Editor* editor = current_editor; Editor* editor = current_editor;
JRect vp = jview_get_viewport_position(jwidget_get_view(editor)); gfx::Rect vp = View::getView(editor)->getViewportBounds();
int x1, y1, x2, y2; int x1, y1, x2, y2;
int x, y, w, h; int x, y, w, h;
editor->screen_to_editor(vp->x1, vp->y1, &x1, &y1); editor->screen_to_editor(vp.x, vp.y, &x1, &y1);
editor->screen_to_editor(vp->x2-1, vp->y2-1, &x2, &y2); editor->screen_to_editor(vp.x+vp.w-1, vp.y+vp.h-1, &x2, &y2);
jrect_free(vp);
if (x1 < 0) x1 = 0; if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0; if (y1 < 0) y1 = 0;

View File

@ -36,6 +36,7 @@ add_library(gui-lib
property.cpp property.cpp
rect.cpp rect.cpp
region.cpp region.cpp
scroll_bar.cpp
separator.cpp separator.cpp
slider.cpp slider.cpp
stream.cpp stream.cpp
@ -44,4 +45,5 @@ add_library(gui-lib
theme.cpp theme.cpp
tooltips.cpp tooltips.cpp
view.cpp view.cpp
viewport.cpp
widget.cpp) widget.cpp)

View File

@ -274,7 +274,7 @@ bool ComboBox::onProcessMessage(JMessage msg)
case JM_BUTTONPRESSED: case JM_BUTTONPRESSED:
if (m_window != NULL) { if (m_window != NULL) {
if (!jwidget_get_view(m_listbox)->hasMouse()) { if (!View::getView(m_listbox)->hasMouse()) {
closeListBox(); closeListBox();
return true; return true;
} }
@ -440,7 +440,7 @@ void ComboBox::openListBox()
{ {
if (!m_window) { if (!m_window) {
m_window = new Frame(false, NULL); m_window = new Frame(false, NULL);
Widget* view = jview_new(); View* view = new View();
m_listbox = jlistbox_new(); m_listbox = jlistbox_new();
m_listbox->user_data[0] = this; m_listbox->user_data[0] = this;
@ -456,7 +456,7 @@ void ComboBox::openListBox()
m_window->set_ontop(true); m_window->set_ontop(true);
jwidget_noborders(m_window); jwidget_noborders(m_window);
Widget* viewport = jview_get_viewport(view); Widget* viewport = view->getViewport();
int size = getItemCount(); int size = getItemCount();
jwidget_set_min_size jwidget_set_min_size
(viewport, (viewport,
@ -466,7 +466,7 @@ void ComboBox::openListBox()
+viewport->border_width.b); +viewport->border_width.b);
jwidget_add_child(m_window, view); jwidget_add_child(m_window, view);
jview_attach(view, m_listbox); view->attachToView(m_listbox);
jwidget_signal_off(m_listbox); jwidget_signal_off(m_listbox);
jlistbox_select_index(m_listbox, m_selected); jlistbox_select_index(m_listbox, m_selected);

View File

@ -338,7 +338,7 @@ bool Frame::onProcessMessage(JMessage msg)
/* TODO */ /* TODO */
/* { */ /* { */
/* JWidget manager = get_manager(); */ /* JWidget manager = get_manager(); */
/* JWidget view = jwidget_get_view(manager); */ /* View* view = View::getView(manager); */
/* if (view) { */ /* if (view) { */
/* jview_update(view); */ /* jview_update(view); */
/* } */ /* } */

View File

@ -39,6 +39,7 @@
#include "gui/property.h" #include "gui/property.h"
#include "gui/rect.h" #include "gui/rect.h"
#include "gui/region.h" #include "gui/region.h"
#include "gui/scroll_bar.h"
#include "gui/separator.h" #include "gui/separator.h"
#include "gui/slider.h" #include "gui/slider.h"
#include "gui/stream.h" #include "gui/stream.h"
@ -47,6 +48,7 @@
#include "gui/theme.h" #include "gui/theme.h"
#include "gui/tooltips.h" #include "gui/tooltips.h"
#include "gui/view.h" #include "gui/view.h"
#include "gui/viewport.h"
#include "gui/widget.h" #include "gui/widget.h"
#endif #endif

View File

@ -8,6 +8,7 @@
#include <allegro/keyboard.h> #include <allegro/keyboard.h>
#include "gfx/point.h"
#include "gfx/size.h" #include "gfx/size.h"
#include "gui/list.h" #include "gui/list.h"
#include "gui/manager.h" #include "gui/manager.h"
@ -92,24 +93,21 @@ void jlistbox_select_child(JWidget widget, JWidget listitem)
} }
if (listitem) { if (listitem) {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
listitem->setSelected(true); listitem->setSelected(true);
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); if (listitem->rc->y1 < vp.y)
scroll.y = listitem->rc->y1 - widget->rc->y1;
else if (listitem->rc->y1 > vp.y + vp.h - jrect_h(listitem->rc))
scroll.y = (listitem->rc->y1 - widget->rc->y1
- vp.h + jrect_h(listitem->rc));
if (listitem->rc->y1 < vp->y1) view->setViewScroll(scroll);
jview_set_scroll(view, scroll_x, listitem->rc->y1 - widget->rc->y1);
else if (listitem->rc->y1 > vp->y2 - jrect_h(listitem->rc))
jview_set_scroll(view, scroll_x,
listitem->rc->y1 - widget->rc->y1
- jrect_h(vp) + jrect_h(listitem->rc));
jrect_free(vp);
} }
} }
@ -131,19 +129,17 @@ int jlistbox_get_items_count(JWidget widget)
/* setup the scroll to center the selected item in the viewport */ /* setup the scroll to center the selected item in the viewport */
void jlistbox_center_scroll(JWidget widget) void jlistbox_center_scroll(JWidget widget)
{ {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
JWidget listitem = jlistbox_get_selected_child(widget); Widget* listitem = jlistbox_get_selected_child(widget);
if (view && listitem) { if (view && listitem) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.y = ((listitem->rc->y1 - widget->rc->y1)
jview_set_scroll(view, - vp.h/2 + jrect_h(listitem->rc)/2);
scroll_x,
(listitem->rc->y1 - widget->rc->y1) view->setViewScroll(scroll);
- jrect_h(vp)/2 + jrect_h(listitem->rc)/2);
jrect_free(vp);
} }
} }
@ -177,31 +173,29 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg)
case JM_MOTION: case JM_MOTION:
if (widget->hasCapture()) { if (widget->hasCapture()) {
int select = jlistbox_get_selected_index(widget); int select = jlistbox_get_selected_index(widget);
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
bool pick_item = true; bool pick_item = true;
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
if (msg->mouse.y < vp->y1) { if (msg->mouse.y < vp.y) {
int num = MAX(1, (vp->y1 - msg->mouse.y) / 8); int num = MAX(1, (vp.y - msg->mouse.y) / 8);
jlistbox_select_index(widget, select-num); jlistbox_select_index(widget, select-num);
pick_item = false; pick_item = false;
} }
else if (msg->mouse.y >= vp->y2) { else if (msg->mouse.y >= vp.y + vp.h) {
int num = MAX(1, (msg->mouse.y - (vp->y2-1)) / 8); int num = MAX(1, (msg->mouse.y - (vp.y+vp.h-1)) / 8);
jlistbox_select_index(widget, select+num); jlistbox_select_index(widget, select+num);
pick_item = false; pick_item = false;
} }
jrect_free(vp);
} }
if (pick_item) { if (pick_item) {
JWidget picked; JWidget picked;
if (view) { if (view) {
picked = jview_get_viewport(view)->pick(msg->mouse.x, msg->mouse.y); picked = view->getViewport()->pick(msg->mouse.x, msg->mouse.y);
} }
else { else {
picked = widget->pick(msg->mouse.x, msg->mouse.y); picked = widget->pick(msg->mouse.x, msg->mouse.y);
@ -221,16 +215,11 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg)
break; break;
case JM_WHEEL: { case JM_WHEEL: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) { if (view) {
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3;
jview_get_scroll(view, &scroll_x, &scroll_y); view->setViewScroll(scroll);
jview_set_scroll(view,
scroll_x,
scroll_y +
(jmouse_z(1) - jmouse_z(0))
*jwidget_get_text_height(widget)*3);
} }
break; break;
} }
@ -238,7 +227,7 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg)
case JM_KEYPRESSED: case JM_KEYPRESSED:
if (widget->hasFocus() && !jlist_empty(widget->children)) { if (widget->hasFocus() && !jlist_empty(widget->children)) {
int select = jlistbox_get_selected_index(widget); int select = jlistbox_get_selected_index(widget);
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
int bottom = MAX(0, jlist_length(widget->children)-1); int bottom = MAX(0, jlist_length(widget->children)-1);
switch (msg->key.scancode) { switch (msg->key.scancode) {
@ -256,18 +245,16 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg)
break; break;
case KEY_PGUP: case KEY_PGUP:
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
select -= jrect_h(vp) / jwidget_get_text_height(widget); select -= vp.h / jwidget_get_text_height(widget);
jrect_free(vp);
} }
else else
select = 0; select = 0;
break; break;
case KEY_PGDN: case KEY_PGDN:
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
select += jrect_h(vp) / jwidget_get_text_height(widget); select += vp.h / jwidget_get_text_height(widget);
jrect_free(vp);
} }
else else
select = bottom; select = bottom;
@ -275,13 +262,13 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg)
case KEY_LEFT: case KEY_LEFT:
case KEY_RIGHT: case KEY_RIGHT:
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll = view->getViewScroll();
int sgn = (msg->key.scancode == KEY_LEFT) ? -1: 1; int sgn = (msg->key.scancode == KEY_LEFT) ? -1: 1;
int scroll_x, scroll_y;
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.x += vp.w/2*sgn;
jview_set_scroll(view, scroll_x + jrect_w(vp)/2*sgn, scroll_y);
jrect_free(vp); view->setViewScroll(scroll);
} }
break; break;
default: default:
@ -345,30 +332,27 @@ static void listbox_set_position(JWidget widget, JRect rect)
static void listbox_dirty_children(JWidget widget) static void listbox_dirty_children(JWidget widget)
{ {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
JWidget child; JWidget child;
JLink link; JLink link;
JRect vp;
if (!view) { if (!view) {
JI_LIST_FOR_EACH(widget->children, link) JI_LIST_FOR_EACH(widget->children, link)
reinterpret_cast<JWidget>(link->data)->invalidate(); reinterpret_cast<JWidget>(link->data)->invalidate();
} }
else { else {
vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
JI_LIST_FOR_EACH(widget->children, link) { JI_LIST_FOR_EACH(widget->children, link) {
child = reinterpret_cast<JWidget>(link->data); child = reinterpret_cast<JWidget>(link->data);
if (child->rc->y2 <= vp->y1) if (child->rc->y2 <= vp.y)
continue; continue;
else if (child->rc->y1 >= vp->y2) else if (child->rc->y1 >= vp.y+vp.h)
break; break;
child->invalidate(); child->invalidate();
} }
jrect_free(vp);
} }
} }

206
src/gui/scroll_bar.cpp Normal file
View File

@ -0,0 +1,206 @@
// ASE gui library
// Copyright (C) 2001-2011 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "gfx/size.h"
#include "gui/message.h"
#include "gui/scroll_bar.h"
#include "gui/theme.h"
#include "gui/view.h"
using namespace gfx;
// Internal stuff shared by all scroll-bars (as the user cannot move
// two scroll-bars at the same time).
int ScrollBar::m_wherepos = 0;
int ScrollBar::m_whereclick = 0;
ScrollBar::ScrollBar(int align)
: Widget(JI_VIEW_SCROLLBAR)
, m_pos(0)
, m_size(0)
{
setAlign(align);
initTheme();
}
void ScrollBar::setPos(int pos)
{
m_pos = pos;
}
void ScrollBar::setSize(int size)
{
m_size = size;
}
void ScrollBar::getScrollBarThemeInfo(int* pos, int* len)
{
getScrollBarInfo(pos, len, NULL, NULL);
}
bool ScrollBar::onProcessMessage(JMessage msg)
{
#define MOUSE_IN(x1, y1, x2, y2) \
((msg->mouse.x >= (x1)) && (msg->mouse.x <= (x2)) && \
(msg->mouse.y >= (y1)) && (msg->mouse.y <= (y2)))
switch (msg->type) {
case JM_BUTTONPRESSED: {
View* view = static_cast<View*>(getParent());
int x1, y1, x2, y2;
int u1, v1, u2, v2;
bool ret = false;
int pos, len;
getScrollBarThemeInfo(&pos, &len);
m_wherepos = pos;
m_whereclick = getAlign() & JI_HORIZONTAL ?
msg->mouse.x:
msg->mouse.y;
x1 = this->rc->x1;
y1 = this->rc->y1;
x2 = this->rc->x2-1;
y2 = this->rc->y2-1;
u1 = x1 + this->border_width.l;
v1 = y1 + this->border_width.t;
u2 = x2 - this->border_width.r;
v2 = y2 - this->border_width.b;
Point scroll = view->getViewScroll();
if (this->getAlign() & JI_HORIZONTAL) {
// in the bar
if (MOUSE_IN(u1+pos, v1, u1+pos+len-1, v2)) {
// capture mouse
}
// left
else if (MOUSE_IN(x1, y1, u1+pos-1, y2)) {
scroll.x -= jrect_w(view->getViewport()->rc)/2;
ret = true;
}
// right
else if (MOUSE_IN(u1+pos+len, y1, x2, y2)) {
scroll.x += jrect_w(view->getViewport()->rc)/2;
ret = true;
}
}
else {
// in the bar
if (MOUSE_IN(u1, v1+pos, u2, v1+pos+len-1)) {
// capture mouse
}
// left
else if (MOUSE_IN(x1, y1, x2, v1+pos-1)) {
scroll.y -= jrect_h(view->getViewport()->rc)/2;
ret = true;
}
// right
else if (MOUSE_IN(x1, v1+pos+len, x2, y2)) {
scroll.y += jrect_h(view->getViewport()->rc)/2;
ret = true;
}
}
if (ret) {
view->setViewScroll(scroll);
return ret;
}
setSelected(true);
captureMouse();
// continue to JM_MOTION handler...
}
case JM_MOTION:
if (hasCapture()) {
View* view = static_cast<View*>(getParent());
int pos, len, bar_size, viewport_size;
int old_pos;
getScrollBarInfo(&pos, &len, &bar_size, &viewport_size);
old_pos = pos;
if (bar_size > len) {
Point scroll = view->getViewScroll();
if (this->getAlign() & JI_HORIZONTAL) {
pos = (m_wherepos + msg->mouse.x - m_whereclick);
pos = MID(0, pos, bar_size - len);
scroll.x = (m_size - viewport_size) * pos / (bar_size - len);
view->setViewScroll(scroll);
}
else {
pos = (m_wherepos + msg->mouse.y - m_whereclick);
pos = MID(0, pos, bar_size - len);
scroll.y = (m_size - viewport_size) * pos / (bar_size - len);
view->setViewScroll(scroll);
}
}
}
break;
case JM_BUTTONRELEASED:
setSelected(false);
releaseMouse();
break;
case JM_MOUSEENTER:
case JM_MOUSELEAVE:
// TODO add something to avoid this (theme specific stuff)
invalidate();
break;
case JM_DRAW:
getTheme()->draw_view_scrollbar(this, &msg->draw.rect);
return true;
}
return Widget::onProcessMessage(msg);
}
void ScrollBar::getScrollBarInfo(int *_pos, int *_len, int *_bar_size, int *_viewport_size)
{
View* view = static_cast<View*>(getParent());
int bar_size, viewport_size;
int pos, len;
int border_width;
if (this->getAlign() & JI_HORIZONTAL) {
bar_size = jrect_w(this->rc);
viewport_size = view->getVisibleSize().w;
border_width = this->border_width.t + this->border_width.b;
}
else {
bar_size = jrect_h(this->rc);
viewport_size = view->getVisibleSize().h;
border_width = this->border_width.l + this->border_width.r;
}
if (m_size <= viewport_size) {
len = bar_size;
pos = 0;
}
else {
len = bar_size - (m_size-viewport_size);
len = MID(getTheme()->scrollbar_size*2-border_width, len, bar_size);
pos = (bar_size-len) * m_pos / (m_size-viewport_size);
pos = MID(0, pos, bar_size-len);
}
if (_pos) *_pos = pos;
if (_len) *_len = len;
if (_bar_size) *_bar_size = bar_size;
if (_viewport_size) *_viewport_size = viewport_size;
}

40
src/gui/scroll_bar.h Normal file
View File

@ -0,0 +1,40 @@
// ASE gui library
// Copyright (C) 2001-2011 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef GUI_SCROLL_BAR_H_INCLUDED
#define GUI_SCROLL_BAR_H_INCLUDED
#include "gui/widget.h"
class ScrollBar : public Widget
{
public:
ScrollBar(int align);
int getPos() const { return m_pos; }
void setPos(int pos);
int getSize() const { return m_size; }
void setSize(int size);
// For themes
void getScrollBarThemeInfo(int* pos, int* len);
protected:
// Events
bool onProcessMessage(JMessage msg);
private:
void getScrollBarInfo(int* _pos, int* _len, int* _bar_size, int* _viewport_size);
int m_pos;
int m_size;
static int m_wherepos;
static int m_whereclick;
};
#endif

View File

@ -130,7 +130,7 @@ bool Slider::onProcessMessage(JMessage msg)
jmouse_set_position(x, jmouse_y(0)); jmouse_set_position(x, jmouse_y(0));
} }
/* for right click */ /* for right click */
else if (jmouse_control_infinite_scroll(rc)) { else if (jmouse_control_infinite_scroll(getBounds() - getBorder())) {
slider_press_x = jmouse_x(0); slider_press_x = jmouse_x(0);
slider_press_value = m_value; slider_press_value = m_value;
} }

View File

@ -398,24 +398,24 @@ int jmouse_y(int antique) { return m_y[antique & 1]; }
int jmouse_z(int antique) { return m_z[antique & 1]; } int jmouse_z(int antique) { return m_z[antique & 1]; }
int jmouse_b(int antique) { return m_b[antique & 1]; } int jmouse_b(int antique) { return m_b[antique & 1]; }
bool jmouse_control_infinite_scroll(JRect rect) bool jmouse_control_infinite_scroll(const gfx::Rect& rect)
{ {
int x, y, u, v; int x, y, u, v;
u = jmouse_x(0); u = jmouse_x(0);
v = jmouse_y(0); v = jmouse_y(0);
if (u <= rect->x1) if (u <= rect.x)
x = rect->x2-2; x = rect.x+rect.w-2;
else if (u >= rect->x2-1) else if (u >= rect.x+rect.w-1)
x = rect->x1+1; x = rect.x+1;
else else
x = u; x = u;
if (v <= rect->y1) if (v <= rect.y)
y = rect->y2-2; y = rect.y+rect.h-2;
else if (v >= rect->y2-1) else if (v >= rect.y+rect.h-1)
y = rect->y1+1; y = rect.y+1;
else else
y = v; y = v;

View File

@ -7,6 +7,7 @@
#ifndef GUI_SYSTEM_H_INCLUDED #ifndef GUI_SYSTEM_H_INCLUDED
#define GUI_SYSTEM_H_INCLUDED #define GUI_SYSTEM_H_INCLUDED
#include "gfx/rect.h"
#include "gui/base.h" #include "gui/base.h"
struct BITMAP; struct BITMAP;
@ -78,6 +79,6 @@ int jmouse_x(int antique);
int jmouse_y(int antique); int jmouse_y(int antique);
int jmouse_z(int antique); int jmouse_z(int antique);
bool jmouse_control_infinite_scroll(JRect rect); bool jmouse_control_infinite_scroll(const gfx::Rect& rect);
#endif #endif

View File

@ -9,6 +9,7 @@
#include <allegro/keyboard.h> #include <allegro/keyboard.h>
#include <math.h> #include <math.h>
#include "gfx/point.h"
#include "gui/hook.h" #include "gui/hook.h"
#include "gui/intern.h" #include "gui/intern.h"
#include "gui/manager.h" #include "gui/manager.h"
@ -49,71 +50,72 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg)
case JM_SIGNAL: case JM_SIGNAL:
if (msg->signal.num == JI_SIGNAL_SET_TEXT) { if (msg->signal.num == JI_SIGNAL_SET_TEXT) {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) if (view)
jview_update(view); view->updateView();
} }
break; break;
case JM_KEYPRESSED: case JM_KEYPRESSED:
if (widget->hasFocus()) { if (widget->hasFocus()) {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll = view->getViewScroll();
int textheight = jwidget_get_text_height(widget); int textheight = jwidget_get_text_height(widget);
int scroll_x, scroll_y;
jview_get_scroll(view, &scroll_x, &scroll_y);
switch (msg->key.scancode) { switch (msg->key.scancode) {
case KEY_LEFT: case KEY_LEFT:
jview_set_scroll(view, scroll_x-jrect_w(vp)/2, scroll_y); scroll.x -= vp.w/2;
view->setViewScroll(scroll);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
jview_set_scroll(view, scroll_x+jrect_w(vp)/2, scroll_y); scroll.x += vp.w/2;
view->setViewScroll(scroll);
break; break;
case KEY_UP: case KEY_UP:
jview_set_scroll(view, scroll_x, scroll_y-jrect_h(vp)/2); scroll.y -= vp.h/2;
view->setViewScroll(scroll);
break; break;
case KEY_DOWN: case KEY_DOWN:
jview_set_scroll(view, scroll_x, scroll_y+jrect_h(vp)/2); scroll.y += vp.h/2;
view->setViewScroll(scroll);
break; break;
case KEY_PGUP: case KEY_PGUP:
jview_set_scroll(view, scroll_x, scroll.y -= (vp.h-textheight);
scroll_y-(jrect_h(vp)-textheight)); view->setViewScroll(scroll);
break; break;
case KEY_PGDN: case KEY_PGDN:
jview_set_scroll(view, scroll_x, scroll.y += (vp.h-textheight);
scroll_y+(jrect_h(vp)-textheight)); view->setViewScroll(scroll);
break; break;
case KEY_HOME: case KEY_HOME:
jview_set_scroll(view, scroll_x, 0); scroll.y = 0;
view->setViewScroll(scroll);
break; break;
case KEY_END: case KEY_END:
jview_set_scroll(view, scroll_x, scroll.y = jrect_h(widget->rc) - vp.h;
jrect_h(widget->rc) - jrect_h(vp)); view->setViewScroll(scroll);
break; break;
default: default:
jrect_free(vp);
return false; return false;
} }
jrect_free(vp);
} }
return true; return true;
} }
break; break;
case JM_BUTTONPRESSED: { case JM_BUTTONPRESSED: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) { if (view) {
widget->captureMouse(); widget->captureMouse();
jmouse_set_cursor(JI_CURSOR_SCROLL); jmouse_set_cursor(JI_CURSOR_SCROLL);
@ -123,24 +125,23 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg)
} }
case JM_MOTION: { case JM_MOTION: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view && widget->hasCapture()) { if (view && widget->hasCapture()) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.x += jmouse_x(1) - jmouse_x(0);
jview_set_scroll(view, scroll.y += jmouse_y(1) - jmouse_y(0);
scroll_x + jmouse_x(1) - jmouse_x(0),
scroll_y + jmouse_y(1) - jmouse_y(0)); view->setViewScroll(scroll);
jmouse_control_infinite_scroll(vp); jmouse_control_infinite_scroll(vp);
jrect_free(vp);
} }
break; break;
} }
case JM_BUTTONRELEASED: { case JM_BUTTONRELEASED: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view && widget->hasCapture()) { if (view && widget->hasCapture()) {
widget->releaseMouse(); widget->releaseMouse();
jmouse_set_cursor(JI_CURSOR_NORMAL); jmouse_set_cursor(JI_CURSOR_NORMAL);
@ -150,16 +151,13 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg)
} }
case JM_WHEEL: { case JM_WHEEL: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) { if (view) {
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3;
jview_set_scroll(view,
scroll_x, view->setViewScroll(scroll);
scroll_y +
(jmouse_z(1) - jmouse_z(0))
*jwidget_get_text_height(widget)*3);
} }
break; break;
} }
@ -179,13 +177,11 @@ static void textbox_request_size(JWidget widget, int *w, int *h)
_ji_theme_textbox_draw(NULL, widget, w, h, 0, 0); _ji_theme_textbox_draw(NULL, widget, w, h, 0, 0);
if (widget->getAlign() & JI_WORDWRAP) { if (widget->getAlign() & JI_WORDWRAP) {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
int width, min = *w; int width, min = *w;
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); width = view->getViewportBounds().w;
width = jrect_w(vp);
jrect_free(vp);
} }
else { else {
width = jrect_w(widget->rc); width = jrect_w(widget->rc);

View File

@ -9,6 +9,8 @@
#include <allegro.h> #include <allegro.h>
#include <allegro/internal/aintern.h> #include <allegro/internal/aintern.h>
#include "gfx/point.h"
#include "gfx/size.h"
#include "gui/draw.h" #include "gui/draw.h"
#include "gui/font.h" #include "gui/font.h"
#include "gui/manager.h" #include "gui/manager.h"
@ -151,12 +153,12 @@ void _ji_theme_draw_sprite_color(BITMAP *bmp, BITMAP *sprite,
void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
int *w, int *h, int bg, int fg) int *w, int *h, int bg, int fg)
{ {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
char *text = (char*)widget->getText(); // TODO warning: removing const modifier char *text = (char*)widget->getText(); // TODO warning: removing const modifier
char *beg, *end; char *beg, *end;
int x1, y1, x2, y2; int x1, y1, x2, y2;
int x, y, chr, len; int x, y, chr, len;
int scroll_x, scroll_y; gfx::Point scroll;
int viewport_w, viewport_h; int viewport_w, viewport_h;
int textheight = jwidget_get_text_height(widget); int textheight = jwidget_get_text_height(widget);
FONT *font = widget->getFont(); FONT *font = widget->getFont();
@ -164,20 +166,19 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
int width; int width;
if (view) { if (view) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
x1 = vp->x1; x1 = vp.x;
y1 = vp->y1; y1 = vp.y;
viewport_w = jrect_w(vp); viewport_w = vp.w;
viewport_h = jrect_h(vp); viewport_h = vp.h;
jview_get_scroll(view, &scroll_x, &scroll_y); scroll = view->getViewScroll();
jrect_free(vp);
} }
else { else {
x1 = widget->rc->x1 + widget->border_width.l; x1 = widget->rc->x1 + widget->border_width.l;
y1 = widget->rc->y1 + widget->border_width.t; y1 = widget->rc->y1 + widget->border_width.t;
viewport_w = jrect_w(widget->rc) - widget->border_width.l - widget->border_width.r; viewport_w = jrect_w(widget->rc) - widget->border_width.l - widget->border_width.r;
viewport_h = jrect_h(widget->rc) - widget->border_width.t - widget->border_width.b; viewport_h = jrect_h(widget->rc) - widget->border_width.t - widget->border_width.b;
scroll_x = scroll_y = 0; scroll.x = scroll.y = 0;
} }
x2 = x1+viewport_w-1; x2 = x1+viewport_w-1;
y2 = y1+viewport_h-1; y2 = y1+viewport_h-1;
@ -202,9 +203,8 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
#else #else
/* make good use of the complete text-box */ /* make good use of the complete text-box */
if (view) { if (view) {
int w, h; gfx::Size maxSize = view->getScrollableSize();
jview_get_max_size(view, &w, &h); width = MAX(viewport_w, maxSize.w);
width = MAX(viewport_w, w);
} }
else { else {
width = viewport_w; width = viewport_w;
@ -214,9 +214,9 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
} }
/* draw line-by-line */ /* draw line-by-line */
y = y1 - scroll_y; y = y1 - scroll.y;
for (beg=end=text; end; ) { for (beg=end=text; end; ) {
x = x1 - scroll_x; x = x1 - scroll.x;
/* without word-wrap */ /* without word-wrap */
if (!(widget->getAlign() & JI_WORDWRAP)) { if (!(widget->getAlign() & JI_WORDWRAP)) {
@ -237,7 +237,7 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
} }
/* to here we can print */ /* to here we can print */
if ((old_end) && (x+text_length(font, beg) > x1-scroll_x+width)) { if ((old_end) && (x+text_length(font, beg) > x1-scroll.x+width)) {
if (end) if (end)
*end = chr; *end = chr;
@ -297,7 +297,7 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget,
/* height */ /* height */
if (h) if (h)
*h = (y-y1+scroll_y); *h = (y-y1+scroll.y);
if (w) *w += widget->border_width.l + widget->border_width.r; if (w) *w += widget->border_width.l + widget->border_width.r;
if (h) *h += widget->border_width.t + widget->border_width.b; if (h) *h += widget->border_width.t + widget->border_width.b;

View File

@ -17,637 +17,276 @@
#include "gui/view.h" #include "gui/view.h"
#include "gui/widget.h" #include "gui/widget.h"
#define BAR_SIZE widget->getTheme()->scrollbar_size #define BAR_SIZE getTheme()->scrollbar_size
using namespace gfx; using namespace gfx;
struct View View::View()
: Widget(JI_VIEW)
, m_scrollbar_h(JI_HORIZONTAL)
, m_scrollbar_v(JI_VERTICAL)
{ {
int max_w, max_h; /* space which the widget need in the viewport */ m_hasBars = true;
int scroll_x; /* scrolling in x and y axis */
int scroll_y;
unsigned hasbars : 1;
/* --internal use-- */
int wherepos, whereclick;
JWidget viewport;
JWidget scrollbar_h;
JWidget scrollbar_v;
};
static void view_plain_update(JWidget widget); jwidget_focusrest(this, true);
static bool view_msg_proc(JWidget widget, JMessage msg); jwidget_add_child(this, &m_viewport);
setScrollableSize(Size(0, 0));
static JWidget viewport_new(); initTheme();
static bool viewport_msg_proc(JWidget widget, JMessage msg);
static void viewport_needed_size(JWidget widget, int *w, int *h);
static void viewport_set_position(JWidget widget, JRect rect);
static JWidget scrollbar_new(int align);
static bool scrollbar_msg_proc(JWidget widget, JMessage msg);
static void scrollbar_info(JWidget widget, int *_pos, int *_len,
int *_bar_size, int *_viewport_size);
static void displace_widgets(JWidget widget, int x, int y);
JWidget jview_new()
{
Widget* widget = new Widget(JI_VIEW);
View* view = new View;
view->viewport = viewport_new();
view->scrollbar_h = scrollbar_new(JI_HORIZONTAL);
view->scrollbar_v = scrollbar_new(JI_VERTICAL);
view->hasbars = true;
view->wherepos = 0;
view->whereclick = 0;
view->scroll_x = 0;
view->scroll_y = 0;
jwidget_add_hook(widget, JI_VIEW, view_msg_proc, view);
jwidget_focusrest(widget, true);
jwidget_add_child(widget, view->viewport);
jview_set_size(widget, 0, 0);
widget->initTheme();
return widget;
} }
bool jview_has_bars(JWidget widget) bool View::hasScrollBars()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); return m_hasBars;
return view->hasbars;
} }
void jview_attach(JWidget widget, JWidget viewable_widget) void View::attachToView(Widget* viewable_widget)
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); jwidget_add_child(&m_viewport, viewable_widget);
jwidget_add_child(view->viewport, viewable_widget);
/* TODO */ /* TODO */
/* jwidget_emit_signal(widget, JI_SIGNAL_VIEW_ATTACH); */ /* jwidget_emit_signal(this, JI_SIGNAL_VIEW_ATTACH); */
} }
void jview_maxsize(JWidget widget) void View::makeVisibleAllScrollableArea()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); Size reqSize = m_viewport.calculateNeededSize();
int req_w, req_h;
viewport_needed_size(view->viewport, &req_w, &req_h); this->min_w =
+ this->border_width.l
+ m_viewport.border_width.l
+ reqSize.w
+ m_viewport.border_width.r
+ this->border_width.r;
widget->min_w = this->min_h =
+ widget->border_width.l + this->border_width.t
+ view->viewport->border_width.l + m_viewport.border_width.t
+ req_w + reqSize.h
+ view->viewport->border_width.r + m_viewport.border_width.b
+ widget->border_width.r; + this->border_width.b;
widget->min_h =
+ widget->border_width.t
+ view->viewport->border_width.t
+ req_h
+ view->viewport->border_width.b
+ widget->border_width.b;
} }
void jview_without_bars(JWidget widget) void View::hideScrollBars()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); m_hasBars = false;
view->hasbars = false; updateView();
jview_update(widget);
} }
void jview_set_size(JWidget widget, int w, int h) Size View::getScrollableSize()
{ {
#define CHECK(w, h, l, t, r, b) \ return Size(m_scrollbar_h.getSize(),
((view->max_##w > jrect_##w(view->viewport->rc) \ m_scrollbar_v.getSize());
- view->viewport->border_width.l \ }
- view->viewport->border_width.r) && \
void View::setScrollableSize(const Size& sz)
{
#define CHECK(w, h, l, t, r, b) \
((sz.w > jrect_##w(m_viewport.rc) \
- m_viewport.border_width.l \
- m_viewport.border_width.r) && \
(BAR_SIZE < jrect_##w(pos)) && (BAR_SIZE < jrect_##h(pos))) (BAR_SIZE < jrect_##w(pos)) && (BAR_SIZE < jrect_##h(pos)))
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW));
JRect pos, rect; JRect pos, rect;
view->max_w = w; m_scrollbar_h.setSize(sz.w);
view->max_h = h; m_scrollbar_v.setSize(sz.h);
pos = jwidget_get_child_rect(widget); pos = jwidget_get_child_rect(this);
/* setup scroll-bars */ /* setup scroll-bars */
jwidget_remove_child(widget, view->scrollbar_h); jwidget_remove_child(this, &m_scrollbar_h);
jwidget_remove_child(widget, view->scrollbar_v); jwidget_remove_child(this, &m_scrollbar_v);
if (view->hasbars) { if (m_hasBars) {
if (CHECK(w, h, l, t, r, b)) { if (CHECK(w, h, l, t, r, b)) {
pos->y2 -= BAR_SIZE; pos->y2 -= BAR_SIZE;
jwidget_add_child(widget, view->scrollbar_h); jwidget_add_child(this, &m_scrollbar_h);
if (CHECK(h, w, t, l, b, r)) { if (CHECK(h, w, t, l, b, r)) {
pos->x2 -= BAR_SIZE; pos->x2 -= BAR_SIZE;
if (CHECK(w, h, l, t, r, b)) if (CHECK(w, h, l, t, r, b))
jwidget_add_child(widget, view->scrollbar_v); jwidget_add_child(this, &m_scrollbar_v);
else { else {
pos->x2 += BAR_SIZE; pos->x2 += BAR_SIZE;
pos->y2 += BAR_SIZE; pos->y2 += BAR_SIZE;
jwidget_remove_child(widget, view->scrollbar_h); jwidget_remove_child(this, &m_scrollbar_h);
} }
} }
} }
else if (CHECK(h, w, t, l, b, r)) { else if (CHECK(h, w, t, l, b, r)) {
pos->x2 -= BAR_SIZE; pos->x2 -= BAR_SIZE;
jwidget_add_child(widget, view->scrollbar_v); jwidget_add_child(this, &m_scrollbar_v);
if (CHECK(w, h, l, t, r, b)) { if (CHECK(w, h, l, t, r, b)) {
pos->y2 -= BAR_SIZE; pos->y2 -= BAR_SIZE;
if (CHECK(h, w, t, l, b, r)) if (CHECK(h, w, t, l, b, r))
jwidget_add_child(widget, view->scrollbar_h); jwidget_add_child(this, &m_scrollbar_h);
else { else {
pos->x2 += BAR_SIZE; pos->x2 += BAR_SIZE;
pos->y2 += BAR_SIZE; pos->y2 += BAR_SIZE;
jwidget_remove_child(widget, view->scrollbar_v); jwidget_remove_child(this, &m_scrollbar_v);
} }
} }
} }
if (widget->hasChild(view->scrollbar_h)) { if (this->hasChild(&m_scrollbar_h)) {
rect = jrect_new(pos->x1, pos->y2, rect = jrect_new(pos->x1, pos->y2,
pos->x1+jrect_w(pos), pos->y2+BAR_SIZE); pos->x1+jrect_w(pos), pos->y2+BAR_SIZE);
jwidget_set_rect(view->scrollbar_h, rect); jwidget_set_rect(&m_scrollbar_h, rect);
jrect_free(rect); jrect_free(rect);
view->scrollbar_h->setVisible(true); m_scrollbar_h.setVisible(true);
} }
else else
view->scrollbar_h->setVisible(false); m_scrollbar_h.setVisible(false);
if (widget->hasChild(view->scrollbar_v)) { if (this->hasChild(&m_scrollbar_v)) {
rect = jrect_new(pos->x2, pos->y1, rect = jrect_new(pos->x2, pos->y1,
pos->x2+BAR_SIZE, pos->y1+jrect_h(pos)); pos->x2+BAR_SIZE, pos->y1+jrect_h(pos));
jwidget_set_rect(view->scrollbar_v, rect); jwidget_set_rect(&m_scrollbar_v, rect);
jrect_free(rect); jrect_free(rect);
view->scrollbar_v->setVisible(true); m_scrollbar_v.setVisible(true);
} }
else else
view->scrollbar_v->setVisible(false); m_scrollbar_v.setVisible(false);
} }
/* setup viewport */ // Setup viewport
widget->invalidate(); this->invalidate();
jwidget_set_rect(view->viewport, pos); jwidget_set_rect(&m_viewport, pos);
jview_set_scroll(widget, view->scroll_x, view->scroll_y); setViewScroll(getViewScroll()); // Setup the same scroll-point
jrect_free(pos); jrect_free(pos);
} }
void jview_set_scroll(JWidget widget, int x, int y) Size View::getVisibleSize()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); return Size(jrect_w(m_viewport.rc) - m_viewport.border_width.l - m_viewport.border_width.r,
int old_x = view->scroll_x; jrect_h(m_viewport.rc) - m_viewport.border_width.t - m_viewport.border_width.b);
int old_y = view->scroll_y; }
int avail_w = jrect_w(view->viewport->rc)
- view->viewport->border_width.l
- view->viewport->border_width.r;
int avail_h = jrect_h(view->viewport->rc)
- view->viewport->border_width.t
- view->viewport->border_width.b;
view->scroll_x = MID(0, x, MAX(0, view->max_w - avail_w)); Point View::getViewScroll()
view->scroll_y = MID(0, y, MAX(0, view->max_h - avail_h)); {
return Point(m_scrollbar_h.getPos(),
m_scrollbar_v.getPos());
}
if ((view->scroll_x == old_x) && (view->scroll_y == old_y)) void View::setViewScroll(const Point& pt)
{
Point oldScroll = getViewScroll();
Size maxsize = getScrollableSize();
Size visible = getVisibleSize();
Point newScroll(MID(0, pt.x, MAX(0, maxsize.w - visible.w)),
MID(0, pt.y, MAX(0, maxsize.h - visible.h)));
if (newScroll == oldScroll)
return; return;
jwidget_set_rect(view->viewport, view->viewport->rc); m_scrollbar_h.setPos(newScroll.x);
widget->invalidate(); m_scrollbar_v.setPos(newScroll.y);
jwidget_set_rect(&m_viewport, m_viewport.rc);
this->invalidate();
} }
void jview_get_scroll(JWidget widget, int *x, int *y) void View::updateView()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); Widget* vw = reinterpret_cast<Widget*>(jlist_first_data(m_viewport.children));
Point scroll = getViewScroll();
*x = view->scroll_x; // Set minimum (remove scroll-bars)
*y = view->scroll_y; setScrollableSize(Size(0, 0));
}
void jview_get_max_size(JWidget widget, int *w, int *h) // Set needed size
{ setScrollableSize(m_viewport.calculateNeededSize());
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW));
*w = view->max_w; // If there are scroll-bars, we have to setup the scrollable-size
*h = view->max_h; // again (because they remove visible space, maybe now we need a
} // vertical or horizontal bar too).
if (hasChild(&m_scrollbar_h) || hasChild(&m_scrollbar_v))
void jview_update(JWidget widget) setScrollableSize(m_viewport.calculateNeededSize());
{
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW));
JWidget vw = reinterpret_cast<JWidget>(jlist_first_data(view->viewport->children));
/* int center_x = vw ? vw->rect->x+vw->rect->w/2: 0; */
/* int center_y = vw ? vw->rect->y+vw->rect->h/2: 0; */
int scroll_x = view->scroll_x;
int scroll_y = view->scroll_y;
view_plain_update(widget);
if (vw) if (vw)
jview_set_scroll(widget, setViewScroll(scroll);
scroll_x, scroll_y);
/* view->scroll_x + (vw->rect->x + vw->rect->w/2) - center_x, */
/* view->scroll_y + (vw->rect->y + vw->rect->h/2) - center_y); */
else else
jview_set_scroll(widget, 0, 0); setViewScroll(Point(0, 0));
} }
JWidget jview_get_viewport(JWidget widget) Viewport* View::getViewport()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); return &m_viewport;
return view->viewport;
} }
JRect jview_get_viewport_position(JWidget widget) Rect View::getViewportBounds()
{ {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); return m_viewport.getBounds() - m_viewport.getBorder();
return jwidget_get_child_rect(view->viewport);
} }
void jtheme_scrollbar_info(JWidget widget, int *pos, int *len) // static
View* View::getView(Widget* widget)
{ {
scrollbar_info(widget, pos, len, NULL, NULL); if ((widget->getParent()) &&
} (widget->getParent()->type == JI_VIEW_VIEWPORT) &&
(widget->getParent()->getParent()) &&
/* for viewable widgets */ (widget->getParent()->getParent()->type == JI_VIEW))
JWidget jwidget_get_view(JWidget widget) return static_cast<View*>(widget->getParent()->getParent());
{
if ((widget->parent) && (widget->parent->parent) &&
((widget->parent->type == JI_VIEW_VIEWPORT)) &&
((widget->parent->parent->type == JI_VIEW)))
return widget->parent->parent;
else else
return 0; return 0;
} }
static void view_plain_update(JWidget widget) bool View::onProcessMessage(JMessage msg)
{
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW));
int req_w, req_h;
jview_set_size(widget, 0, 0);
viewport_needed_size(view->viewport, &req_w, &req_h);
jview_set_size(widget, req_w, req_h);
if ((widget->hasChild(view->scrollbar_h)) ||
(widget->hasChild(view->scrollbar_v))) {
viewport_needed_size(view->viewport, &req_w, &req_h);
jview_set_size(widget, req_w, req_h);
}
}
static bool view_msg_proc(JWidget widget, JMessage msg)
{ {
switch (msg->type) { switch (msg->type) {
case JM_DESTROY: {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW));
jwidget_remove_child(widget, view->viewport);
jwidget_remove_child(widget, view->scrollbar_h);
jwidget_remove_child(widget, view->scrollbar_v);
jwidget_free(view->viewport);
jwidget_free(view->scrollbar_h);
jwidget_free(view->scrollbar_v);
delete view;
break;
}
case JM_REQSIZE: { case JM_REQSIZE: {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget, JI_VIEW)); Size viewSize = m_viewport.getPreferredSize();
Size viewSize = view->viewport->getPreferredSize();
msg->reqsize.w = viewSize.w; msg->reqsize.w = viewSize.w;
msg->reqsize.h = viewSize.h; msg->reqsize.h = viewSize.h;
msg->reqsize.w += widget->border_width.l + widget->border_width.r; msg->reqsize.w += this->border_width.l + this->border_width.r;
msg->reqsize.h += widget->border_width.t + widget->border_width.b; msg->reqsize.h += this->border_width.t + this->border_width.b;
return true; return true;
} }
case JM_SETPOS: case JM_SETPOS:
if (!_jwindow_is_moving()) { /* dirty trick */ if (!_jwindow_is_moving()) { /* dirty trick */
jrect_copy(widget->rc, &msg->setpos.rect); jrect_copy(this->rc, &msg->setpos.rect);
jview_update(widget); updateView();
} }
else { else {
displace_widgets(widget, displaceWidgets(this,
msg->setpos.rect.x1 - widget->rc->x1, msg->setpos.rect.x1 - this->rc->x1,
msg->setpos.rect.y1 - widget->rc->y1); msg->setpos.rect.y1 - this->rc->y1);
} }
return true; return true;
case JM_DRAW: case JM_DRAW:
widget->getTheme()->draw_view(widget, &msg->draw.rect); getTheme()->draw_view(this, &msg->draw.rect);
return true; return true;
case JM_FOCUSENTER: case JM_FOCUSENTER:
case JM_FOCUSLEAVE: case JM_FOCUSLEAVE:
/* TODO add something to avoid this (theme specific stuff) */ /* TODO add something to avoid this (theme specific stuff) */
{ {
JRegion reg1 = jwidget_get_drawable_region(widget, JRegion reg1 = jwidget_get_drawable_region(this, JI_GDR_CUTTOPWINDOWS);
JI_GDR_CUTTOPWINDOWS); jregion_union(this->update_region, this->update_region, reg1);
jregion_union(widget->update_region, widget->update_region, reg1);
jregion_free(reg1); jregion_free(reg1);
} }
break; break;
} }
return false; return Widget::onProcessMessage(msg);
} }
static JWidget viewport_new() // static
{ void View::displaceWidgets(Widget* widget, int x, int y)
Widget* widget = new Widget(JI_VIEW_VIEWPORT);
jwidget_add_hook(widget, JI_VIEW_VIEWPORT, viewport_msg_proc, NULL);
widget->initTheme();
return widget;
}
static bool viewport_msg_proc(JWidget widget, JMessage msg)
{
switch (msg->type) {
case JM_REQSIZE:
msg->reqsize.w = widget->border_width.l + 1 + widget->border_width.r;
msg->reqsize.h = widget->border_width.t + 1 + widget->border_width.b;
return true;
case JM_SETPOS:
viewport_set_position(widget, &msg->setpos.rect);
return true;
case JM_DRAW:
widget->getTheme()->draw_view_viewport(widget, &msg->draw.rect);
return true;
}
return false;
}
static void viewport_needed_size(JWidget widget, int *w, int *h)
{
Size reqSize;
JLink link;
*w = *h = 0;
JI_LIST_FOR_EACH(widget->children, link) {
reqSize = ((Widget*)link->data)->getPreferredSize();
*w = MAX(*w, reqSize.w);
*h = MAX(*h, reqSize.h);
}
}
static void viewport_set_position(JWidget widget, JRect rect)
{
int scroll_x, scroll_y;
Size reqSize;
JWidget child;
JRect cpos;
JLink link;
jrect_copy(widget->rc, rect);
jview_get_scroll(widget->parent, &scroll_x, &scroll_y);
cpos = jrect_new(0, 0, 0, 0);
cpos->x1 = widget->rc->x1 + widget->border_width.l - scroll_x;
cpos->y1 = widget->rc->y1 + widget->border_width.t - scroll_y;
JI_LIST_FOR_EACH(widget->children, link) {
child = (JWidget)link->data;
reqSize = child->getPreferredSize();
cpos->x2 = cpos->x1 + MAX(reqSize.w, jrect_w(widget->rc)
- widget->border_width.l
- widget->border_width.r);
cpos->y2 = cpos->y1 + MAX(reqSize.h, jrect_h(widget->rc)
- widget->border_width.t
- widget->border_width.b);
jwidget_set_rect(child, cpos);
}
jrect_free(cpos);
}
static JWidget scrollbar_new(int align)
{
Widget* widget = new Widget(JI_VIEW_SCROLLBAR);
jwidget_add_hook(widget, JI_VIEW_SCROLLBAR, scrollbar_msg_proc, NULL);
widget->setAlign(align);
widget->initTheme();
return widget;
}
static bool scrollbar_msg_proc(JWidget widget, JMessage msg)
{
#define MOUSE_IN(x1, y1, x2, y2) \
((msg->mouse.x >= (x1)) && (msg->mouse.x <= (x2)) && \
(msg->mouse.y >= (y1)) && (msg->mouse.y <= (y2)))
switch (msg->type) {
case JM_BUTTONPRESSED: {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget->parent, JI_VIEW));
int x1, y1, x2, y2;
int u1, v1, u2, v2;
bool ret = false;
int pos, len;
jtheme_scrollbar_info(widget, &pos, &len);
view->wherepos = pos;
view->whereclick = widget->getAlign() & JI_HORIZONTAL ? msg->mouse.x:
msg->mouse.y;
x1 = widget->rc->x1;
y1 = widget->rc->y1;
x2 = widget->rc->x2-1;
y2 = widget->rc->y2-1;
u1 = x1 + widget->border_width.l;
v1 = y1 + widget->border_width.t;
u2 = x2 - widget->border_width.r;
v2 = y2 - widget->border_width.b;
if (widget->getAlign() & JI_HORIZONTAL) {
/* in the bar */
if (MOUSE_IN(u1+pos, v1, u1+pos+len-1, v2)) {
/* capture mouse */
}
/* left */
else if (MOUSE_IN(x1, y1, u1+pos-1, y2)) {
jview_set_scroll(widget->parent,
view->scroll_x - jrect_w(view->viewport->rc)/2,
view->scroll_y);
ret = true;
}
/* right */
else if (MOUSE_IN(u1+pos+len, y1, x2, y2)) {
jview_set_scroll(widget->parent,
view->scroll_x + jrect_w(view->viewport->rc)/2,
view->scroll_y);
ret = true;
}
}
else {
/* in the bar */
if (MOUSE_IN(u1, v1+pos, u2, v1+pos+len-1)) {
/* capture mouse */
}
/* left */
else if (MOUSE_IN(x1, y1, x2, v1+pos-1)) {
jview_set_scroll(widget->parent,
view->scroll_x,
view->scroll_y - jrect_h(view->viewport->rc)/2);
ret = true;
}
/* right */
else if (MOUSE_IN(x1, v1+pos+len, x2, y2)) {
jview_set_scroll(widget->parent,
view->scroll_x,
view->scroll_y + jrect_h(view->viewport->rc)/2);
ret = true;
}
}
if (ret)
return ret;
widget->setSelected(true);
widget->captureMouse();
// continue to JM_MOTION handler...
}
case JM_MOTION:
if (widget->hasCapture()) {
View* view = reinterpret_cast<View*>(jwidget_get_data(widget->parent, JI_VIEW));
int pos, len, bar_size, viewport_size;
int old_pos;
scrollbar_info(widget, &pos, &len,
&bar_size, &viewport_size);
old_pos = pos;
if (bar_size > len) {
if (widget->getAlign() & JI_HORIZONTAL) {
pos = (view->wherepos + msg->mouse.x - view->whereclick);
pos = MID(0, pos, bar_size - len);
jview_set_scroll
(widget->parent,
(view->max_w - viewport_size) * pos / (bar_size - len),
view->scroll_y);
}
else {
pos = (view->wherepos + msg->mouse.y - view->whereclick);
pos = MID(0, pos, bar_size - len);
jview_set_scroll
(widget->parent,
view->scroll_x,
(view->max_h - viewport_size) * pos / (bar_size - len));
}
}
}
break;
case JM_BUTTONRELEASED:
widget->setSelected(false);
widget->releaseMouse();
break;
case JM_MOUSEENTER:
case JM_MOUSELEAVE:
// TODO add something to avoid this (theme specific stuff)
widget->invalidate();
break;
case JM_DRAW:
widget->getTheme()->draw_view_scrollbar(widget, &msg->draw.rect);
return true;
}
return false;
}
static void scrollbar_info(JWidget widget, int *_pos, int *_len,
int *_bar_size, int *_viewport_size)
{
View* view = reinterpret_cast<View*>(jwidget_get_data(widget->parent, JI_VIEW));
int bar_size, viewport_size;
int pos, len, max, scroll;
int border_width;
if (widget->getAlign() & JI_HORIZONTAL) {
max = view->max_w;
scroll = view->scroll_x;
bar_size = jrect_w(widget->rc)
- widget->border_width.l
- widget->border_width.r;
viewport_size = jrect_w(view->viewport->rc)
- view->viewport->border_width.l
- view->viewport->border_width.r;
border_width = widget->border_width.t + widget->border_width.b;
}
else {
max = view->max_h;
scroll = view->scroll_y;
bar_size = jrect_h(widget->rc)
- widget->border_width.t
- widget->border_width.b;
viewport_size = jrect_h(view->viewport->rc)
- view->viewport->border_width.t
- view->viewport->border_width.b;
border_width = widget->border_width.l + widget->border_width.r;
}
if (max <= viewport_size) {
len = bar_size;
pos = 0;
}
else {
len = bar_size - (max-viewport_size);
len = MID(BAR_SIZE*2-border_width, len, bar_size);
pos = (bar_size-len) * scroll / (max-viewport_size);
pos = MID(0, pos, bar_size-len);
}
if (_pos) *_pos = pos;
if (_len) *_len = len;
if (_bar_size) *_bar_size = bar_size;
if (_viewport_size) *_viewport_size = viewport_size;
}
static void displace_widgets(JWidget widget, int x, int y)
{ {
JLink link; JLink link;
jrect_displace(widget->rc, x, y); jrect_displace(widget->rc, x, y);
JI_LIST_FOR_EACH(widget->children, link) JI_LIST_FOR_EACH(widget->children, link)
displace_widgets(reinterpret_cast<JWidget>(link->data), x, y); displaceWidgets(reinterpret_cast<JWidget>(link->data), x, y);
} }

View File

@ -7,30 +7,53 @@
#ifndef GUI_VIEW_H_INCLUDED #ifndef GUI_VIEW_H_INCLUDED
#define GUI_VIEW_H_INCLUDED #define GUI_VIEW_H_INCLUDED
#include "gui/base.h" #include "gfx/point.h"
#include "gfx/size.h"
#include "gui/scroll_bar.h"
#include "gui/viewport.h"
#include "gui/widget.h"
JWidget jview_new(); class View : public Widget
{
public:
View();
bool jview_has_bars(JWidget view); bool hasScrollBars();
void jview_attach(JWidget view, JWidget viewable_widget); void attachToView(Widget* viewableWidget);
void jview_maxsize(JWidget view); void hideScrollBars();
void jview_without_bars(JWidget view); void makeVisibleAllScrollableArea();
void jview_set_size(JWidget view, int w, int h); // Returns the maximum viewable size requested by the attached
void jview_set_scroll(JWidget view, int x, int y); // widget in the viewport.
void jview_get_scroll(JWidget view, int *x, int *y); gfx::Size getScrollableSize();
void jview_get_max_size(JWidget view, int *w, int *h); void setScrollableSize(const gfx::Size& sz);
void jview_update(JWidget view); // Returns the visible/available size to see the attached widget.
gfx::Size getVisibleSize();
JWidget jview_get_viewport(JWidget view); gfx::Point getViewScroll();
JRect jview_get_viewport_position(JWidget view); void setViewScroll(const gfx::Point& pt);
/* for themes */ void updateView();
void jtheme_scrollbar_info(JWidget scrollbar, int *pos, int *len);
/* for viewable widgets */ Viewport* getViewport();
JWidget jwidget_get_view(JWidget viewable_widget); gfx::Rect getViewportBounds();
// For viewable widgets
static View* getView(Widget* viewableWidget);
protected:
// Events
bool onProcessMessage(JMessage msg);
private:
static void displaceWidgets(Widget* widget, int x, int y);
bool m_hasBars;
Viewport m_viewport;
ScrollBar m_scrollbar_h;
ScrollBar m_scrollbar_v;
};
#endif #endif

92
src/gui/viewport.cpp Normal file
View File

@ -0,0 +1,92 @@
// ASE gui library
// Copyright (C) 2001-2011 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "gfx/point.h"
#include "gfx/size.h"
#include "gui/list.h"
#include "gui/message.h"
#include "gui/theme.h"
#include "gui/view.h"
#include "gui/viewport.h"
using namespace gfx;
Viewport::Viewport()
: Widget(JI_VIEW_VIEWPORT)
{
initTheme();
}
bool Viewport::onProcessMessage(JMessage msg)
{
switch (msg->type) {
case JM_REQSIZE:
msg->reqsize.w = this->border_width.l + 1 + this->border_width.r;
msg->reqsize.h = this->border_width.t + 1 + this->border_width.b;
return true;
case JM_SETPOS:
set_position(&msg->setpos.rect);
return true;
case JM_DRAW:
getTheme()->draw_view_viewport(this, &msg->draw.rect);
return true;
}
return Widget::onProcessMessage(msg);
}
Size Viewport::calculateNeededSize()
{
Size maxSize(0, 0);
Size reqSize;
JLink link;
JI_LIST_FOR_EACH(this->children, link) {
reqSize = ((Widget*)link->data)->getPreferredSize();
maxSize.w = MAX(maxSize.w, reqSize.w);
maxSize.h = MAX(maxSize.h, reqSize.h);
}
return maxSize;
}
void Viewport::set_position(JRect rect)
{
Size reqSize;
JWidget child;
JRect cpos;
JLink link;
jrect_copy(this->rc, rect);
Point scroll = static_cast<View*>(this->getParent())->getViewScroll();
cpos = jrect_new(0, 0, 0, 0);
cpos->x1 = this->rc->x1 + this->border_width.l - scroll.x;
cpos->y1 = this->rc->y1 + this->border_width.t - scroll.y;
JI_LIST_FOR_EACH(this->children, link) {
child = (Widget*)link->data;
reqSize = child->getPreferredSize();
cpos->x2 = cpos->x1 + MAX(reqSize.w, jrect_w(this->rc)
- this->border_width.l
- this->border_width.r);
cpos->y2 = cpos->y1 + MAX(reqSize.h, jrect_h(this->rc)
- this->border_width.t
- this->border_width.b);
jwidget_set_rect(child, cpos);
}
jrect_free(cpos);
}

27
src/gui/viewport.h Normal file
View File

@ -0,0 +1,27 @@
// ASE gui library
// Copyright (C) 2001-2011 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef GUI_VIEWPORT_H_INCLUDED
#define GUI_VIEWPORT_H_INCLUDED
#include "gui/widget.h"
class Viewport : public Widget
{
public:
Viewport();
gfx::Size calculateNeededSize();
protected:
// Events
bool onProcessMessage(JMessage msg);
private:
void set_position(JRect rect);
};
#endif

View File

@ -832,9 +832,11 @@ JRegion jwidget_get_drawable_region(JWidget widget, int flags)
manager = window ? window->getManager(): NULL; manager = window ? window->getManager(): NULL;
while (manager) { while (manager) {
view = jwidget_get_view(manager); view = View::getView(manager);
if (view) if (view) {
cpos = jview_get_viewport_position(view); Rect vp = static_cast<View*>(view)->getViewportBounds();
cpos = jrect_new(vp.x, vp.y, vp.x+vp.w, vp.y+vp.h);
}
else else
cpos = jwidget_get_child_rect(manager); cpos = jwidget_get_child_rect(manager);
/* if (!manager->parent) */ /* if (!manager->parent) */

View File

@ -47,7 +47,7 @@ static EditorList editors;
static int is_sprite_in_some_editor(Sprite *sprite); static int is_sprite_in_some_editor(Sprite *sprite);
static Sprite *get_more_reliable_sprite(); static Sprite *get_more_reliable_sprite();
static JWidget find_next_editor(JWidget widget); static Widget* find_next_editor(Widget* widget);
static int count_parents(Widget* widget); static int count_parents(Widget* widget);
int init_module_editors() int init_module_editors()
@ -198,11 +198,11 @@ void set_current_editor(Editor* editor)
{ {
if (current_editor != editor) { if (current_editor != editor) {
if (current_editor) if (current_editor)
jwidget_get_view(current_editor)->invalidate(); View::getView(current_editor)->invalidate();
current_editor = editor; current_editor = editor;
jwidget_get_view(current_editor)->invalidate(); View::getView(current_editor)->invalidate();
UIContext* context = UIContext::instance(); UIContext* context = UIContext::instance();
Sprite* sprite = current_editor->getSprite(); Sprite* sprite = current_editor->getSprite();
@ -224,7 +224,7 @@ void set_sprite_in_current_editor(Sprite *sprite)
current_editor->editor_set_sprite(sprite); current_editor->editor_set_sprite(sprite);
jwidget_get_view(current_editor)->invalidate(); View::getView(current_editor)->invalidate();
app_refresh_screen(sprite); app_refresh_screen(sprite);
app_realloc_sprite_list(); app_realloc_sprite_list();
@ -259,20 +259,20 @@ void split_editor(Editor* editor, int align)
return; return;
} }
JWidget view = jwidget_get_view(editor); View* view = View::getView(editor);
JWidget parent_box = view->getParent(); // box or panel JWidget parent_box = view->getParent(); // box or panel
/* create a new box to contain both editors, and a new view to put /* create a new box to contain both editors, and a new view to put
the new editor */ the new editor */
JWidget new_panel = jpanel_new(align); JWidget new_panel = jpanel_new(align);
JWidget new_view = editor_view_new(); View* new_view = editor_view_new();
Editor* new_editor = create_new_editor(); Editor* new_editor = create_new_editor();
/* insert the "new_box" in the same location that the view */ /* insert the "new_box" in the same location that the view */
jwidget_replace_child(parent_box, view, new_panel); jwidget_replace_child(parent_box, view, new_panel);
/* append the new editor */ /* append the new editor */
jview_attach(new_view, new_editor); new_view->attachToView(new_editor);
/* set the sprite for the new editor */ /* set the sprite for the new editor */
new_editor->editor_set_sprite(editor->getSprite()); new_editor->editor_set_sprite(editor->getSprite());
@ -288,14 +288,10 @@ void split_editor(Editor* editor, int align)
/* same position */ /* same position */
{ {
int scroll_x, scroll_y; new_view->setViewScroll(view->getViewScroll());
jview_get_scroll(view, &scroll_x, &scroll_y);
jview_set_scroll(new_view, scroll_x, scroll_y);
jrect_copy(new_view->rc, view->rc); jrect_copy(new_view->rc, view->rc);
jrect_copy(jview_get_viewport(new_view)->rc, jrect_copy(new_view->getViewport()->rc, view->getViewport()->rc);
jview_get_viewport(view)->rc);
jrect_copy(new_editor->rc, editor->rc); jrect_copy(new_editor->rc, editor->rc);
new_editor->editor_set_offset_x(editor->editor_get_offset_x()); new_editor->editor_set_offset_x(editor->editor_get_offset_x());
@ -312,7 +308,7 @@ void split_editor(Editor* editor, int align)
void close_editor(Editor* editor) void close_editor(Editor* editor)
{ {
JWidget view = jwidget_get_view(editor); View* view = View::getView(editor);
JWidget parent_box = view->getParent(); // Box or panel JWidget parent_box = view->getParent(); // Box or panel
JWidget other_widget; JWidget other_widget;
@ -357,7 +353,7 @@ void close_editor(Editor* editor)
void make_unique_editor(Editor* editor) void make_unique_editor(Editor* editor)
{ {
JWidget view = jwidget_get_view(editor); View* view = View::getView(editor);
JLink link, next; JLink link, next;
JWidget child; JWidget child;
@ -419,13 +415,14 @@ static Sprite* get_more_reliable_sprite()
return NULL; return NULL;
} }
static JWidget find_next_editor(JWidget widget) static Widget* find_next_editor(Widget* widget)
{ {
JWidget editor = NULL; Widget* editor = NULL;
JLink link; JLink link;
if (widget->type == JI_VIEW) if (widget->type == JI_VIEW) {
editor = reinterpret_cast<JWidget>(jlist_first_data(jview_get_viewport(widget)->children)); editor = reinterpret_cast<Widget*>(jlist_first_data(static_cast<View*>(widget)->getViewport()->children));
}
else { else {
JI_LIST_FOR_EACH(widget->children, link) JI_LIST_FOR_EACH(widget->children, link)
if ((editor = find_next_editor(reinterpret_cast<JWidget>(link->data)))) if ((editor = find_next_editor(reinterpret_cast<JWidget>(link->data))))

View File

@ -1383,13 +1383,14 @@ void SkinTheme::draw_view(JWidget widget, JRect clip)
COLOR_BACKGROUND); COLOR_BACKGROUND);
} }
void SkinTheme::draw_view_scrollbar(JWidget widget, JRect clip) void SkinTheme::draw_view_scrollbar(JWidget _widget, JRect clip)
{ {
ScrollBar* widget = static_cast<ScrollBar*>(_widget);
int x1, y1, x2, y2; int x1, y1, x2, y2;
int u1, v1, u2, v2; int u1, v1, u2, v2;
int pos, len; int pos, len;
jtheme_scrollbar_info(widget, &pos, &len); widget->getScrollBarThemeInfo(&pos, &len);
x1 = widget->rc->x1; x1 = widget->rc->x1;
y1 = widget->rc->y1; y1 = widget->rc->y1;

View File

@ -250,19 +250,17 @@ void clipboard::paste(SpriteWriter& sprite)
// Do the interactive-transform loop (where the user can move the floating image) // Do the interactive-transform loop (where the user can move the floating image)
{ {
JWidget view = jwidget_get_view(current_editor); View* view = View::getView(current_editor);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int x, y, x1, y1, x2, y2; int x, y, x1, y1, x2, y2;
current_editor->screen_to_editor(vp->x1, vp->y1, &x1, &y1); current_editor->screen_to_editor(vp.x, vp.y, &x1, &y1);
current_editor->screen_to_editor(vp->x2-1, vp->y2-1, &x2, &y2); current_editor->screen_to_editor(vp.x+vp.w-1, vp.y+vp.h-1, &x2, &y2);
x = (x1+x2)/2-src_image->w/2; x = (x1+x2)/2-src_image->w/2;
y = (y1+y2)/2-src_image->h/2; y = (y1+y2)/2-src_image->h/2;
paste = interactive_transform(current_editor, paste = interactive_transform(current_editor,
dst_image, src_image, x, y, xout, yout); dst_image, src_image, x, y, xout, yout);
jrect_free(vp);
} }
if (paste) { if (paste) {
@ -324,12 +322,12 @@ static bool interactive_transform(Editor* editor,
#define REDRAW() \ #define REDRAW() \
jmouse_hide(); \ jmouse_hide(); \
blit(bmp1, bmp2, vp->x1, vp->y1, 0, 0, jrect_w(vp), jrect_h(vp)); \ blit(bmp1, bmp2, vp.x, vp.y, 0, 0, vp.w, vp.h); \
draw_box(bmp2, \ draw_box(bmp2, \
0, 0, jrect_w(vp)-1, jrect_h(vp)-1, \ 0, 0, vp.w-1, vp.h-1, \
x1-vp->x1, y1-vp->y1, x2-vp->x1, y2-vp->y1, \ x1-vp.x, y1-vp.y, x2-vp.x, y2-vp.y, \
preview, mode, angle, cx-vp->x1, cy-vp->y1); \ preview, mode, angle, cx-vp.x, cy-vp.y); \
blit(bmp2, ji_screen, 0, 0, vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)); \ blit(bmp2, ji_screen, 0, 0, vp.x, vp.y, vp.w, vp.h); \
update_status_bar(editor, image, x1, y1, x2, y2, angle); \ update_status_bar(editor, image, x1, y1, x2, y2, angle); \
jmouse_show(); jmouse_show();
@ -338,7 +336,7 @@ static bool interactive_transform(Editor* editor,
int action = ACTION_SETMODE; int action = ACTION_SETMODE;
int mode = SCALE_MODE; int mode = SCALE_MODE;
BITMAP *bmp1, *bmp2, *preview, *old_screen; BITMAP *bmp1, *bmp2, *preview, *old_screen;
JRect vp = jview_get_viewport_position(jwidget_get_view(editor)); gfx::Rect vp = View::getView(editor)->getViewportBounds();
int done = DONE_NONE; int done = DONE_NONE;
fixed angle = 0; fixed angle = 0;
int cx, cy; int cx, cy;
@ -354,7 +352,7 @@ static bool interactive_transform(Editor* editor,
/* generate a bitmap to save the viewport content and other to make /* generate a bitmap to save the viewport content and other to make
double-buffered */ double-buffered */
bmp1 = create_bitmap(JI_SCREEN_W, JI_SCREEN_H); bmp1 = create_bitmap(JI_SCREEN_W, JI_SCREEN_H);
bmp2 = create_bitmap(jrect_w(vp), jrect_h(vp)); bmp2 = create_bitmap(vp.w, vp.h);
jmouse_hide(); jmouse_hide();
blit(ji_screen, bmp1, 0, 0, 0, 0, JI_SCREEN_W, JI_SCREEN_H); blit(ji_screen, bmp1, 0, 0, 0, 0, JI_SCREEN_W, JI_SCREEN_H);
@ -484,8 +482,7 @@ static bool interactive_transform(Editor* editor,
/* left button+shift || middle button = scroll movement */ /* left button+shift || middle button = scroll movement */
if ((jmouse_b(0) == 1 && (key[KEY_LSHIFT] || key[KEY_RSHIFT])) || if ((jmouse_b(0) == 1 && (key[KEY_LSHIFT] || key[KEY_RSHIFT])) ||
(jmouse_b(0) == 4)) { (jmouse_b(0) == 4)) {
JWidget view = jwidget_get_view(editor); View* view = View::getView(editor);
int scroll_x, scroll_y;
x = jmouse_x(0) - jmouse_x(1); x = jmouse_x(0) - jmouse_x(1);
y = jmouse_y(0) - jmouse_y(1); y = jmouse_y(0) - jmouse_y(1);
@ -495,8 +492,8 @@ static bool interactive_transform(Editor* editor,
/* TODO */ /* TODO */
jview_get_scroll(view, &scroll_x, &scroll_y); gfx::Point scroll = view->getViewScroll();
editor->editor_set_scroll(scroll_x-x, scroll_y-y, true); editor->editor_set_scroll(scroll.x-x, scroll.y-y, true);
/* editor_to_screen (widget, x1, y1, &x1, &y1); */ /* editor_to_screen (widget, x1, y1, &x1, &y1); */
/* editor_to_screen (widget, x2, y2, &x2, &y2); */ /* editor_to_screen (widget, x2, y2, &x2, &y2); */
@ -711,7 +708,6 @@ static bool interactive_transform(Editor* editor,
/* restore the cursor */ /* restore the cursor */
editor->show_drawing_cursor(); editor->show_drawing_cursor();
jrect_free(vp);
return done == DONE_PASTE; return done == DONE_PASTE;
} }

View File

@ -311,35 +311,19 @@ static bool curve_editor_msg_proc(JWidget widget, JMessage msg)
switch (curve_editor->status) { switch (curve_editor->status) {
case STATUS_SCROLLING: { case STATUS_SCROLLING: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.x += jmouse_x(1)-jmouse_x(0);
jview_set_scroll(view, scroll.y += jmouse_y(1)-jmouse_y(0);
scroll_x+jmouse_x(1)-jmouse_x(0),
scroll_y+jmouse_y(1)-jmouse_y(0)); view->setViewScroll(scroll);
jmouse_control_infinite_scroll(vp); jmouse_control_infinite_scroll(vp);
jrect_free(vp);
break; break;
} }
/* case STATUS_SCALING: { */
/* JID view_id = jwidget_get_view(widget); */
/* JRect vp = jview_get_viewport_pos(view_id); */
/* int scroll_x, scroll_y; */
/* jview_get_scroll(view_id, &scroll_x, &scroll_y); */
/* jview_update(view_id); */
/* jview_set_scroll(view_id, */
/* scroll_x-(vp.x+vp.w/2), */
/* scroll_y-(vp.y+vp.h/2)); */
/* jmouse_control_infinite_scroll(vp.x, vp.y, vp.w, vp.h); */
/* break; */
/* } */
case STATUS_MOVING_POINT: case STATUS_MOVING_POINT:
if (curve_editor->edit_point) { if (curve_editor->edit_point) {
/* int old_x = *curve_editor->edit_x; */ /* int old_x = *curve_editor->edit_x; */

View File

@ -33,6 +33,7 @@ class IToolLoop;
class ToolLoopManager; class ToolLoopManager;
class PixelsMovement; class PixelsMovement;
class Tool; class Tool;
class View;
class Editor : public Widget class Editor : public Widget
{ {
@ -226,7 +227,7 @@ private:
void (*pixel)(BITMAP *bmp, int x, int y, int color)); void (*pixel)(BITMAP *bmp, int x, int y, int color));
}; };
JWidget editor_view_new(); View* editor_view_new();
int editor_type(); int editor_type();
#endif #endif

View File

@ -118,33 +118,31 @@ int Editor::editor_click(int *x, int *y, int *update,
/* the mouse was moved */ /* the mouse was moved */
if (*update) { if (*update) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
/* update scroll */ /* update scroll */
if (jmouse_control_infinite_scroll(vp)) { if (jmouse_control_infinite_scroll(vp)) {
int scroll_x, scroll_y;
if (scroll_callback) if (scroll_callback)
(*scroll_callback)(true); (*scroll_callback)(true);
/* smooth scroll movement */ /* smooth scroll movement */
if (get_config_bool("Options", "MoveSmooth", true)) { if (get_config_bool("Options", "MoveSmooth", true)) {
jmouse_set_position(MID(vp->x1+1, click_last_x, vp->x2-2), jmouse_set_position(MID(vp.x+1, click_last_x, vp.x+vp.w-2),
MID(vp->y1+1, click_last_y, vp->y2-2)); MID(vp.y+1, click_last_y, vp.y+vp.h-2));
} }
/* this is better for high resolutions: scroll movement by big steps */ /* this is better for high resolutions: scroll movement by big steps */
else { else {
jmouse_set_position((click_last_x != jmouse_x(0)) ? jmouse_set_position((click_last_x != jmouse_x(0)) ?
(click_last_x + (vp->x1+vp->x2)/2)/2: jmouse_x(0), (click_last_x + (vp.x+vp.w/2))/2: jmouse_x(0),
(click_last_y != jmouse_y(0)) ? (click_last_y != jmouse_y(0)) ?
(click_last_y + (vp->y1+vp->y2)/2)/2: jmouse_y(0)); (click_last_y + (vp.y+vp.h/2))/2: jmouse_y(0));
} }
jview_get_scroll(view, &scroll_x, &scroll_y); gfx::Point scroll = view->getViewScroll();
editor_set_scroll(scroll_x+click_last_x-jmouse_x(0), editor_set_scroll(scroll.x+click_last_x-jmouse_x(0),
scroll_y+click_last_y-jmouse_y(0), true); scroll.y+click_last_y-jmouse_y(0), true);
click_last_x = jmouse_x(0); click_last_x = jmouse_x(0);
click_last_y = jmouse_y(0); click_last_y = jmouse_y(0);
@ -162,8 +160,6 @@ int Editor::editor_click(int *x, int *y, int *update,
// Check if the mouse change to other pixel of the screen // Check if the mouse change to other pixel of the screen
*update = ((prev_x != click_last_x) || (prev_y != click_last_y)); *update = ((prev_x != click_last_x) || (prev_y != click_last_y));
} }
jrect_free(vp);
} }
/* click-and-click mode */ /* click-and-click mode */

View File

@ -60,9 +60,9 @@ using namespace gfx;
static bool editor_view_msg_proc(JWidget widget, JMessage msg); static bool editor_view_msg_proc(JWidget widget, JMessage msg);
JWidget editor_view_new() View* editor_view_new()
{ {
JWidget widget = jview_new(); View* widget = new View();
SkinTheme* theme = static_cast<SkinTheme*>(widget->getTheme()); SkinTheme* theme = static_cast<SkinTheme*>(widget->getTheme());
int l = theme->get_part(PART_EDITOR_SELECTED_W)->w; int l = theme->get_part(PART_EDITOR_SELECTED_W)->w;
int t = theme->get_part(PART_EDITOR_SELECTED_N)->h; int t = theme->get_part(PART_EDITOR_SELECTED_N)->h;
@ -70,7 +70,7 @@ JWidget editor_view_new()
int b = theme->get_part(PART_EDITOR_SELECTED_S)->h; int b = theme->get_part(PART_EDITOR_SELECTED_S)->h;
jwidget_set_border(widget, l, t, r, b); jwidget_set_border(widget, l, t, r, b);
jview_without_bars(widget); widget->hideScrollBars();
jwidget_add_hook(widget, JI_WIDGET, editor_view_msg_proc, NULL); jwidget_add_hook(widget, JI_WIDGET, editor_view_msg_proc, NULL);
return widget; return widget;
@ -161,16 +161,14 @@ void Editor::editor_set_sprite(Sprite* sprite)
editor_update(); editor_update();
if (preferred.virgin) { if (preferred.virgin) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
preferred.virgin = false; preferred.virgin = false;
preferred.scroll_x = -jrect_w(vp)/2 + (sprite->getWidth()/2); preferred.scroll_x = -vp.w/2 + (sprite->getWidth()/2);
preferred.scroll_y = -jrect_h(vp)/2 + (sprite->getHeight()/2); preferred.scroll_y = -vp.h/2 + (sprite->getHeight()/2);
m_sprite->setPreferredEditorSettings(preferred); m_sprite->setPreferredEditorSettings(preferred);
jrect_free(vp);
} }
editor_set_scroll(m_offset_x + preferred.scroll_x, editor_set_scroll(m_offset_x + preferred.scroll_x,
@ -190,8 +188,8 @@ void Editor::editor_set_sprite(Sprite* sprite)
// Sets the scroll position of the editor // Sets the scroll position of the editor
void Editor::editor_set_scroll(int x, int y, int use_refresh_region) void Editor::editor_set_scroll(int x, int y, int use_refresh_region)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
int old_scroll_x, old_scroll_y; Point oldScroll;
JRegion region = NULL; JRegion region = NULL;
int thick = m_cursor_thick; int thick = m_cursor_thick;
@ -200,10 +198,10 @@ void Editor::editor_set_scroll(int x, int y, int use_refresh_region)
if (use_refresh_region) { if (use_refresh_region) {
region = jwidget_get_drawable_region(this, JI_GDR_CUTTOPWINDOWS); region = jwidget_get_drawable_region(this, JI_GDR_CUTTOPWINDOWS);
jview_get_scroll(view, &old_scroll_x, &old_scroll_y); oldScroll = view->getViewScroll();
} }
jview_set_scroll(view, x, y); view->setViewScroll(Point(x, y));
if (m_sprite) { if (m_sprite) {
PreferredEditorSettings preferred; PreferredEditorSettings preferred;
@ -216,14 +214,12 @@ void Editor::editor_set_scroll(int x, int y, int use_refresh_region)
} }
if (use_refresh_region) { if (use_refresh_region) {
int new_scroll_x, new_scroll_y; Point newScroll = view->getViewScroll();
jview_get_scroll(view, &new_scroll_x, &new_scroll_y);
// Move screen with blits // Move screen with blits
jwidget_scroll(this, region, jwidget_scroll(this, region,
old_scroll_x - new_scroll_x, oldScroll.x - newScroll.x,
old_scroll_y - new_scroll_y); oldScroll.y - newScroll.y);
jregion_free(region); jregion_free(region);
/* m_widget->flags &= ~JI_DIRTY; */ /* m_widget->flags &= ~JI_DIRTY; */
@ -241,8 +237,7 @@ void Editor::editor_set_scroll(int x, int y, int use_refresh_region)
void Editor::editor_update() void Editor::editor_update()
{ {
JWidget view = jwidget_get_view(this); View::getView(this)->updateView();
jview_update(view);
} }
/** /**
@ -253,43 +248,42 @@ void Editor::editor_update()
*/ */
void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2) void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int source_x, source_y, dest_x, dest_y, width, height; int source_x, source_y, dest_x, dest_y, width, height;
int scroll_x, scroll_y;
// Get scroll // Get scroll
jview_get_scroll(view, &scroll_x, &scroll_y); Point scroll = view->getViewScroll();
// Output information // Output information
source_x = x1 << m_zoom; source_x = x1 << m_zoom;
source_y = y1 << m_zoom; source_y = y1 << m_zoom;
dest_x = vp->x1 - scroll_x + m_offset_x + source_x; dest_x = vp.x - scroll.x + m_offset_x + source_x;
dest_y = vp->y1 - scroll_y + m_offset_y + source_y; dest_y = vp.y - scroll.y + m_offset_y + source_y;
width = (x2 - x1 + 1) << m_zoom; width = (x2 - x1 + 1) << m_zoom;
height = (y2 - y1 + 1) << m_zoom; height = (y2 - y1 + 1) << m_zoom;
// Clip from viewport // Clip from viewport
if (dest_x < vp->x1) { if (dest_x < vp.x) {
source_x += vp->x1 - dest_x; source_x += vp.x - dest_x;
width -= vp->x1 - dest_x; width -= vp.x - dest_x;
dest_x = vp->x1; dest_x = vp.x;
} }
if (dest_y < vp->y1) { if (dest_y < vp.y) {
source_y += vp->y1 - dest_y; source_y += vp.y - dest_y;
height -= vp->y1 - dest_y; height -= vp.y - dest_y;
dest_y = vp->y1; dest_y = vp.y;
} }
if (dest_x+width-1 > vp->x2-1) if (dest_x+width-1 > vp.x + vp.w-1)
width = vp->x2-dest_x; width = vp.x + vp.w - dest_x;
if (dest_y+height-1 > vp->y2-1) if (dest_y+height-1 > vp.y + vp.h-1)
height = vp->y2-dest_y; height = vp.y + vp.h - dest_y;
// Clip from screen // Clip from screen
@ -364,8 +358,6 @@ void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2)
} }
} }
jrect_free(vp);
// Draw grids // Draw grids
ISettings* settings = UIContext::instance()->getSettings(); ISettings* settings = UIContext::instance()->getSettings();
@ -416,18 +408,16 @@ void Editor::editor_draw_sprite_safe(int x1, int y1, int x2, int y2)
*/ */
void Editor::editor_draw_mask() void Editor::editor_draw_mask()
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll = view->getViewScroll();
int x1, y1, x2, y2; int x1, y1, x2, y2;
int c, x, y; int c, x, y;
jview_get_scroll(view, &scroll_x, &scroll_y);
dotted_mode(m_offset_count); dotted_mode(m_offset_count);
x = vp->x1 - scroll_x + m_offset_x; x = vp.x - scroll.x + m_offset_x;
y = vp->y1 - scroll_y + m_offset_y; y = vp.y - scroll.y + m_offset_y;
int nseg = m_sprite->getBoundariesSegmentsCount(); int nseg = m_sprite->getBoundariesSegmentsCount();
const _BoundSeg* seg = m_sprite->getBoundariesSegments(); const _BoundSeg* seg = m_sprite->getBoundariesSegments();
@ -469,8 +459,6 @@ void Editor::editor_draw_mask()
} }
dotted_mode(-1); dotted_mode(-1);
jrect_free(vp);
} }
void Editor::editor_draw_mask_safe() void Editor::editor_draw_mask_safe()
@ -515,17 +503,15 @@ void Editor::drawGrid(const Rect& gridBounds, const Color& color)
return; return;
int grid_color = color_utils::color_for_allegro(color, bitmap_color_depth(ji_screen)); int grid_color = color_utils::color_for_allegro(color, bitmap_color_depth(ji_screen));
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll = view->getViewScroll();
int x1, y1, x2, y2; int x1, y1, x2, y2;
int u1, v1, u2, v2; int u1, v1, u2, v2;
int c; int c;
jview_get_scroll(view, &scroll_x, &scroll_y); scroll.x = vp.x - scroll.x + m_offset_x;
scroll.y = vp.y - scroll.y + m_offset_y;
scroll_x = vp->x1 - scroll_x + m_offset_x;
scroll_y = vp->y1 - scroll_y + m_offset_y;
x1 = ji_screen->cl; x1 = ji_screen->cl;
y1 = ji_screen->ct; y1 = ji_screen->ct;
@ -549,20 +535,18 @@ void Editor::drawGrid(const Rect& gridBounds, const Color& color)
grid.h <<= m_zoom; grid.h <<= m_zoom;
// Horizontal lines // Horizontal lines
x1 = scroll_x+grid.x+u1*grid.w; x1 = scroll.x+grid.x+u1*grid.w;
x2 = scroll_x+grid.x+u2*grid.w; x2 = scroll.x+grid.x+u2*grid.w;
for (c=v1; c<=v2; c++) for (c=v1; c<=v2; c++)
hline(ji_screen, x1, scroll_y+grid.y+c*grid.h, x2, grid_color); hline(ji_screen, x1, scroll.y+grid.y+c*grid.h, x2, grid_color);
// Vertical lines // Vertical lines
y1 = scroll_y+grid.y+v1*grid.h; y1 = scroll.y+grid.y+v1*grid.h;
y2 = scroll_y+grid.y+v2*grid.h; y2 = scroll.y+grid.y+v2*grid.h;
for (c=u1; c<=u2; c++) for (c=u1; c<=u2; c++)
vline(ji_screen, scroll_x+grid.x+c*grid.w, y1, y2, grid_color); vline(ji_screen, scroll.x+grid.x+c*grid.w, y1, y2, grid_color);
jrect_free(vp);
} }
void Editor::flashCurrentLayer() void Editor::flashCurrentLayer()
@ -649,11 +633,10 @@ void Editor::turnOnSelectionModifiers()
*/ */
void Editor::controlInfiniteScroll(JMessage msg) void Editor::controlInfiniteScroll(JMessage msg)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
if (jmouse_control_infinite_scroll(vp)) { if (jmouse_control_infinite_scroll(vp)) {
int scroll_x, scroll_y;
int old_x = msg->mouse.x; int old_x = msg->mouse.x;
int old_y = msg->mouse.y; int old_y = msg->mouse.y;
@ -662,27 +645,22 @@ void Editor::controlInfiniteScroll(JMessage msg)
// Smooth scroll movement // Smooth scroll movement
if (get_config_bool("Options", "MoveSmooth", TRUE)) { if (get_config_bool("Options", "MoveSmooth", TRUE)) {
jmouse_set_position(MID(vp->x1+1, old_x, vp->x2-2), jmouse_set_position(MID(vp.x+1, old_x, vp.x+vp.w-2),
MID(vp->y1+1, old_y, vp->y2-2)); MID(vp.y+1, old_y, vp.y+vp.h-2));
} }
// This is better for high resolutions: scroll movement by big steps // This is better for high resolutions: scroll movement by big steps
else { else {
jmouse_set_position((old_x != msg->mouse.x) ? jmouse_set_position((old_x != msg->mouse.x) ? (old_x + (vp.x+vp.w/2))/2: msg->mouse.x,
(old_x + (vp->x1+vp->x2)/2)/2: msg->mouse.x, (old_y != msg->mouse.y) ? (old_y + (vp.y+vp.h/2))/2: msg->mouse.y);
(old_y != msg->mouse.y) ?
(old_y + (vp->y1+vp->y2)/2)/2: msg->mouse.y);
} }
msg->mouse.x = jmouse_x(0); msg->mouse.x = jmouse_x(0);
msg->mouse.y = jmouse_y(0); msg->mouse.y = jmouse_y(0);
jview_get_scroll(view, &scroll_x, &scroll_y); Point scroll = view->getViewScroll();
editor_set_scroll(scroll_x+old_x-msg->mouse.x, editor_set_scroll(scroll.x+old_x-msg->mouse.x,
scroll_y+old_y-msg->mouse.y, true); scroll.y+old_y-msg->mouse.y, true);
} }
jrect_free(vp);
} }
void Editor::dropPixels() void Editor::dropPixels()
@ -718,30 +696,22 @@ Tool* Editor::getCurrentEditorTool()
void Editor::screen_to_editor(int xin, int yin, int *xout, int *yout) void Editor::screen_to_editor(int xin, int yin, int *xout, int *yout)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); *xout = (xin - vp.x + scroll.x - m_offset_x) >> m_zoom;
*yout = (yin - vp.y + scroll.y - m_offset_y) >> m_zoom;
*xout = (xin - vp->x1 + scroll_x - m_offset_x) >> m_zoom;
*yout = (yin - vp->y1 + scroll_y - m_offset_y) >> m_zoom;
jrect_free(vp);
} }
void Editor::editor_to_screen(int xin, int yin, int *xout, int *yout) void Editor::editor_to_screen(int xin, int yin, int *xout, int *yout)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); *xout = (vp.x - scroll.x + m_offset_x + (xin << m_zoom));
*yout = (vp.y - scroll.y + m_offset_y + (yin << m_zoom));
*xout = (vp->x1 - scroll_x + m_offset_x + (xin << m_zoom));
*yout = (vp->y1 - scroll_y + m_offset_y + (yin << m_zoom));
jrect_free(vp);
} }
void Editor::show_drawing_cursor() void Editor::show_drawing_cursor()
@ -867,13 +837,13 @@ static bool editor_view_msg_proc(JWidget widget, JMessage msg)
// This avoid the displacement of the widgets in the viewport // This avoid the displacement of the widgets in the viewport
jrect_copy(widget->rc, &msg->setpos.rect); jrect_copy(widget->rc, &msg->setpos.rect);
jview_update(widget); static_cast<View*>(widget)->updateView();
return true; return true;
case JM_DRAW: case JM_DRAW:
{ {
JWidget viewport = jview_get_viewport(widget); Widget* viewport = static_cast<View*>(widget)->getViewport();
JWidget child = reinterpret_cast<JWidget>(jlist_first_data(viewport->children)); Widget* child = reinterpret_cast<JWidget>(jlist_first_data(viewport->children));
JRect pos = jwidget_get_rect(widget); JRect pos = jwidget_get_rect(widget);
SkinTheme* theme = static_cast<SkinTheme*>(widget->getTheme()); SkinTheme* theme = static_cast<SkinTheme*>(widget->getTheme());
@ -930,15 +900,11 @@ bool Editor::onProcessMessage(JMessage msg)
// Editor without sprite // Editor without sprite
if (!m_sprite) { if (!m_sprite) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
jdraw_rectfill(vp, theme->get_editor_face_color()); jdraw_rectfill(vp, theme->get_editor_face_color());
draw_emptyset_symbol(ji_screen, draw_emptyset_symbol(ji_screen, vp, makecol(64, 64, 64));
Rect(vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)),
makecol(64, 64, 64));
jrect_free(vp);
} }
// Editor with sprite // Editor with sprite
else { else {
@ -988,10 +954,9 @@ bool Editor::onProcessMessage(JMessage msg)
// The sprite is locked to be read... we can draw an opaque // The sprite is locked to be read... we can draw an opaque
// background only. // background only.
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
jdraw_rectfill(vp, theme->get_editor_face_color()); jdraw_rectfill(vp, theme->get_editor_face_color());
jrect_free(vp);
} }
} }
@ -1199,16 +1164,14 @@ bool Editor::onProcessMessage(JMessage msg)
// Move the scroll // Move the scroll
if (m_state == EDITOR_STATE_SCROLLING) { if (m_state == EDITOR_STATE_SCROLLING) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll = view->getViewScroll();
jview_get_scroll(view, &scroll_x, &scroll_y); editor_set_scroll(scroll.x+jmouse_x(1)-jmouse_x(0),
editor_set_scroll(scroll_x+jmouse_x(1)-jmouse_x(0), scroll.y+jmouse_y(1)-jmouse_y(0), true);
scroll_y+jmouse_y(1)-jmouse_y(0), true);
jmouse_control_infinite_scroll(vp); jmouse_control_infinite_scroll(vp);
jrect_free(vp);
{ {
int x, y; int x, y;
@ -1472,18 +1435,18 @@ bool Editor::onProcessMessage(JMessage msg)
case WHEEL_HSCROLL: case WHEEL_HSCROLL:
case WHEEL_VSCROLL: { case WHEEL_VSCROLL: {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int scroll_x, scroll_y; Point scroll;
int dx = 0; int dx = 0;
int dy = 0; int dy = 0;
int thick = m_cursor_thick; int thick = m_cursor_thick;
if (wheelAction == WHEEL_HSCROLL) { if (wheelAction == WHEEL_HSCROLL) {
dx = dz * jrect_w(vp); dx = dz * vp.w;
} }
else { else {
dy = dz * jrect_h(vp); dy = dz * vp.h;
} }
if (scrollBigSteps) { if (scrollBigSteps) {
@ -1495,17 +1458,15 @@ bool Editor::onProcessMessage(JMessage msg)
dy /= 10; dy /= 10;
} }
jview_get_scroll(view, &scroll_x, &scroll_y); scroll = view->getViewScroll();
jmouse_hide(); jmouse_hide();
if (thick) if (thick)
editor_clean_cursor(); editor_clean_cursor();
editor_set_scroll(scroll_x+dx, scroll_y+dy, true); editor_set_scroll(scroll.x+dx, scroll.y+dy, true);
if (thick) if (thick)
editor_draw_cursor(jmouse_x(0), jmouse_y(0)); editor_draw_cursor(jmouse_x(0), jmouse_y(0));
jmouse_show(); jmouse_show();
jrect_free(vp);
break; break;
} }
@ -1544,16 +1505,14 @@ void Editor::onCurrentToolChange()
void Editor::editor_request_size(int *w, int *h) void Editor::editor_request_size(int *w, int *h)
{ {
if (m_sprite) { if (m_sprite) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
m_offset_x = jrect_w(vp)/2 - 1; m_offset_x = vp.w/2 - 1;
m_offset_y = jrect_h(vp)/2 - 1; m_offset_y = vp.h/2 - 1;
*w = (m_sprite->getWidth() << m_zoom) + m_offset_x*2; *w = (m_sprite->getWidth() << m_zoom) + m_offset_x*2;
*h = (m_sprite->getHeight() << m_zoom) + m_offset_y*2; *h = (m_sprite->getHeight() << m_zoom) + m_offset_y*2;
jrect_free(vp);
} }
else { else {
*w = 4; *w = 4;
@ -1701,8 +1660,8 @@ void Editor::editor_update_candraw()
void Editor::editor_set_zoom_and_center_in_mouse(int zoom, int mouse_x, int mouse_y) void Editor::editor_set_zoom_and_center_in_mouse(int zoom, int mouse_x, int mouse_y)
{ {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); Rect vp = view->getViewportBounds();
int x, y; int x, y;
bool centerMouse = get_config_bool("Editor", "CenterMouseInZoom", false); bool centerMouse = get_config_bool("Editor", "CenterMouseInZoom", false);
int mx, my; int mx, my;
@ -1711,16 +1670,16 @@ void Editor::editor_set_zoom_and_center_in_mouse(int zoom, int mouse_x, int mous
screen_to_editor(mouse_x, mouse_y, &x, &y); screen_to_editor(mouse_x, mouse_y, &x, &y);
if (centerMouse) { if (centerMouse) {
mx = (vp->x1+vp->x2)/2; mx = vp.x+vp.w/2;
my = (vp->y1+vp->y2)/2; my = vp.y+vp.h/2;
} }
else { else {
mx = mouse_x; mx = mouse_x;
my = mouse_y; my = mouse_y;
} }
x = m_offset_x - (mx - vp->x1) + ((1<<zoom)>>1) + (x << zoom); x = m_offset_x - (mx - vp.x) + ((1<<zoom)>>1) + (x << zoom);
y = m_offset_y - (my - vp->y1) + ((1<<zoom)>>1) + (y << zoom); y = m_offset_y - (my - vp.y) + ((1<<zoom)>>1) + (y << zoom);
if ((m_zoom != zoom) || if ((m_zoom != zoom) ||
(m_cursor_editor_x != mx) || (m_cursor_editor_x != mx) ||
@ -1736,7 +1695,6 @@ void Editor::editor_set_zoom_and_center_in_mouse(int zoom, int mouse_x, int mous
jmouse_set_position(mx, my); jmouse_set_position(mx, my);
} }
show_drawing_cursor(); show_drawing_cursor();
jrect_free(vp);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -44,8 +44,8 @@ bool Editor::editor_keys_toset_zoom(int scancode)
!key[KEY_LCONTROL] && !key[KEY_LCONTROL] &&
!key[KEY_RCONTROL] && !key[KEY_RCONTROL] &&
!key[KEY_ALT]) { !key[KEY_ALT]) {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int x, y, zoom; int x, y, zoom;
x = 0; x = 0;
@ -66,8 +66,6 @@ bool Editor::editor_keys_toset_zoom(int scancode)
editor_set_zoom_and_center_in_mouse(zoom, jmouse_x(0), jmouse_y(0)); editor_set_zoom_and_center_in_mouse(zoom, jmouse_x(0), jmouse_y(0));
return true; return true;
} }
jrect_free(vp);
} }
return false; return false;

View File

@ -161,7 +161,7 @@ void fileview_set_current_folder(JWidget widget, IFileItem* folder)
jwidget_emit_signal(widget, SIGNAL_FILEVIEW_CURRENT_FOLDER_CHANGED); jwidget_emit_signal(widget, SIGNAL_FILEVIEW_CURRENT_FOLDER_CHANGED);
widget->invalidate(); widget->invalidate();
jview_update(jwidget_get_view(widget)); View::getView(widget)->updateView();
} }
const FileItemList& fileview_get_filelist(JWidget widget) const FileItemList& fileview_get_filelist(JWidget widget)
@ -233,8 +233,8 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
return true; return true;
case JM_DRAW: { case JM_DRAW: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int iw, ih; int iw, ih;
int th = jwidget_get_text_height(widget); int th = jwidget_get_text_height(widget);
int x, y = widget->rc->y1; int x, y = widget->rc->y1;
@ -359,9 +359,9 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
/* draw the thumbnail */ /* draw the thumbnail */
if (thumbnail) { if (thumbnail) {
x = vp->x2-2-thumbnail->w; x = vp.x+vp.w-2-thumbnail->w;
y = thumbnail_y-thumbnail->h/2; y = thumbnail_y-thumbnail->h/2;
y = MID(vp->y1+2, y, vp->y2-3-thumbnail->h); y = MID(vp.y+2, y, vp.y+vp.h-3-thumbnail->h);
blit(thumbnail, ji_screen, 0, 0, x, y, thumbnail->w, thumbnail->h); blit(thumbnail, ji_screen, 0, 0, x, y, thumbnail->w, thumbnail->h);
rect(ji_screen, rect(ji_screen,
@ -371,11 +371,7 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
// is the current folder empty? // is the current folder empty?
if (fileview->list.empty()) if (fileview->list.empty())
draw_emptyset_symbol(ji_screen, draw_emptyset_symbol(ji_screen, vp, makecol(194, 194, 194));
Rect(vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)),
makecol(194, 194, 194));
jrect_free(vp);
return true; return true;
} }
@ -426,7 +422,7 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
case JM_KEYPRESSED: case JM_KEYPRESSED:
if (widget->hasFocus()) { if (widget->hasFocus()) {
int select = fileview_get_selected_index(widget); int select = fileview_get_selected_index(widget);
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
int bottom = fileview->list.size(); int bottom = fileview->list.size();
switch (msg->key.scancode) { switch (msg->key.scancode) {
@ -451,23 +447,20 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
case KEY_PGUP: case KEY_PGUP:
case KEY_PGDN: { case KEY_PGDN: {
int sgn = (msg->key.scancode == KEY_PGUP) ? -1: 1; int sgn = (msg->key.scancode == KEY_PGUP) ? -1: 1;
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
if (select < 0) if (select < 0)
select = 0; select = 0;
select += sgn * jrect_h(vp) / (2+jwidget_get_text_height(widget)+2); select += sgn * vp.h / (2+jwidget_get_text_height(widget)+2);
jrect_free(vp);
break; break;
} }
case KEY_LEFT: case KEY_LEFT:
case KEY_RIGHT: case KEY_RIGHT:
if (select >= 0) { if (select >= 0) {
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
int sgn = (msg->key.scancode == KEY_LEFT) ? -1: 1; int sgn = (msg->key.scancode == KEY_LEFT) ? -1: 1;
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
scroll.x += vp.w/2*sgn;
jview_get_scroll(view, &scroll_x, &scroll_y); view->setViewScroll(scroll);
jview_set_scroll(view, scroll_x + jrect_w(vp)/2*sgn, scroll_y);
jrect_free(vp);
} }
break; break;
case KEY_ENTER: case KEY_ENTER:
@ -532,16 +525,11 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg)
break; break;
case JM_WHEEL: { case JM_WHEEL: {
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
if (view) { if (view) {
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
scroll.y += (jmouse_z(1)-jmouse_z(0)) * 3*(2+jwidget_get_text_height(widget)+2);
jview_get_scroll(view, &scroll_x, &scroll_y); view->setViewScroll(scroll);
jview_set_scroll(view,
scroll_x,
scroll_y +
(jmouse_z(1) - jmouse_z(0))
*(2+jwidget_get_text_height(widget)+2)*3);
} }
break; break;
} }
@ -599,14 +587,12 @@ static void fileview_get_fileitem_size(JWidget widget, IFileItem* fi, int *w, in
static void fileview_make_selected_fileitem_visible(JWidget widget) static void fileview_make_selected_fileitem_visible(JWidget widget)
{ {
FileView* fileview = fileview_data(widget); FileView* fileview = fileview_data(widget);
JWidget view = jwidget_get_view(widget); View* view = View::getView(widget);
JRect vp = jview_get_viewport_position(view); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll = view->getViewScroll();
int iw, ih; int iw, ih;
int th = jwidget_get_text_height(widget); int th = jwidget_get_text_height(widget);
int y = widget->rc->y1; int y = widget->rc->y1;
int scroll_x, scroll_y;
jview_get_scroll(view, &scroll_x, &scroll_y);
// rows // rows
for (FileItemList::iterator for (FileItemList::iterator
@ -616,18 +602,17 @@ static void fileview_make_selected_fileitem_visible(JWidget widget)
fileview_get_fileitem_size(widget, fi, &iw, &ih); fileview_get_fileitem_size(widget, fi, &iw, &ih);
if (fi == fileview->selected) { if (fi == fileview->selected) {
if (y < vp->y1) if (y < vp.y)
jview_set_scroll(view, scroll_x, y - widget->rc->y1); scroll.y = y - widget->rc->y1;
else if (y > vp->y2 - (2+th+2)) else if (y > vp.y + vp.h - (2+th+2))
jview_set_scroll(view, scroll_x, scroll.y = y - widget->rc->y1 - vp.h + (2+th+2);
y - widget->rc->y1 - jrect_h(vp) + (2+th+2));
view->setViewScroll(scroll);
break; break;
} }
y += ih; y += ih;
} }
jrect_free(vp);
} }
static void fileview_regenerate_list(JWidget widget) static void fileview_regenerate_list(JWidget widget)

View File

@ -23,6 +23,7 @@
#include <string.h> #include <string.h>
#include "app/color.h" #include "app/color.h"
#include "gfx/point.h"
#include "gui/manager.h" #include "gui/manager.h"
#include "gui/message.h" #include "gui/message.h"
#include "gui/rect.h" #include "gui/rect.h"
@ -80,9 +81,9 @@ void PaletteView::setColumns(int columns)
m_columns = columns; m_columns = columns;
if (m_columns != old_columns) { if (m_columns != old_columns) {
Widget* view = jwidget_get_view(this); View* view = View::getView(this);
if (view) if (view)
jview_update(view); view->updateView();
invalidate(); invalidate();
} }
@ -556,15 +557,11 @@ bool PaletteView::onProcessMessage(JMessage msg)
return true; return true;
case JM_WHEEL: { case JM_WHEEL: {
JWidget view = jwidget_get_view(this); View* view = View::getView(this);
if (view) { if (view) {
int scroll_x, scroll_y; gfx::Point scroll = view->getViewScroll();
scroll.y += (jmouse_z(1)-jmouse_z(0)) * 3 * m_boxsize;
jview_get_scroll(view, &scroll_x, &scroll_y); view->setViewScroll(scroll);
jview_set_scroll(view,
scroll_x,
scroll_y +
(jmouse_z(1) - jmouse_z(0)) * 3*m_boxsize);
} }
break; break;
} }
@ -589,33 +586,32 @@ void PaletteView::request_size(int* w, int* h)
void PaletteView::update_scroll(int color) void PaletteView::update_scroll(int color)
{ {
Widget* view = jwidget_get_view(this); View* view = View::getView(this);
if (view != NULL) { if (!view)
JRect vp = jview_get_viewport_position(view); return;
int scroll_x, scroll_y;
int x, y, cols;
div_t d;
jview_get_scroll(view, &scroll_x, &scroll_y); gfx::Rect vp = view->getViewportBounds();
gfx::Point scroll;
int x, y, cols;
div_t d;
d = div(256, m_columns); scroll = view->getViewScroll();
cols = m_columns;
y = (m_boxsize+this->child_spacing) * (color / cols); d = div(256, m_columns);
x = (m_boxsize+this->child_spacing) * (color % cols); cols = m_columns;
if (scroll_x > x) y = (m_boxsize+this->child_spacing) * (color / cols);
scroll_x = x; x = (m_boxsize+this->child_spacing) * (color % cols);
else if (scroll_x+jrect_w(vp)-m_boxsize-2 < x)
scroll_x = x-jrect_w(vp)+m_boxsize+2;
if (scroll_y > y) if (scroll.x > x)
scroll_y = y; scroll.x = x;
else if (scroll_y+jrect_h(vp)-m_boxsize-2 < y) else if (scroll.x+vp.w-m_boxsize-2 < x)
scroll_y = y-jrect_h(vp)+m_boxsize+2; scroll.x = x-vp.w+m_boxsize+2;
jview_set_scroll(view, scroll_x, scroll_y); if (scroll.y > y)
scroll.y = y;
else if (scroll.y+vp.h-m_boxsize-2 < y)
scroll.y = y-vp.h+m_boxsize+2;
jrect_free(vp); view->setViewScroll(scroll);
}
} }

View File

@ -296,7 +296,7 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root)
} }
/* view */ /* view */
else if (ustrcmp(elem_name, "view") == 0) { else if (ustrcmp(elem_name, "view") == 0) {
widget = jview_new(); widget = new View();
} }
/* window */ /* window */
else if (ustrcmp(elem_name, "window") == 0) { else if (ustrcmp(elem_name, "window") == 0) {
@ -377,7 +377,7 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root)
if (child) { if (child) {
// Attach the child in the view // Attach the child in the view
if (widget->type == JI_VIEW) { if (widget->type == JI_VIEW) {
jview_attach(widget, child); static_cast<View*>(widget)->attachToView(child);
break; break;
} }
// Add the child in the grid // Add the child in the grid
@ -403,7 +403,7 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root)
if (widget->type == JI_VIEW) { if (widget->type == JI_VIEW) {
bool maxsize = bool_attr_is_true(elem, "maxsize"); bool maxsize = bool_attr_is_true(elem, "maxsize");
if (maxsize) if (maxsize)
jview_maxsize(widget); static_cast<View*>(widget)->makeVisibleAllScrollableArea();
} }
} }