diff --git a/src/app.cpp b/src/app.cpp index d03dbe171..f2646ade2 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -161,7 +161,7 @@ int App::run() { // Initialize GUI interface if (isGui()) { - Widget* view; + View* view; Editor* editor; PRINTF("GUI mode\n"); @@ -197,7 +197,7 @@ int App::run() jwidget_expansive(view, true); /* prepare the first editor */ - jview_attach(view, editor); + view->attachToView(editor); /* setup the menus */ jmenubar_set_menu(menubar, get_root_menu()); diff --git a/src/commands/cmd_palette_editor.cpp b/src/commands/cmd_palette_editor.cpp index 64a3d0ddf..aec6b0a46 100644 --- a/src/commands/cmd_palette_editor.cpp +++ b/src/commands/cmd_palette_editor.cpp @@ -223,7 +223,7 @@ void PaletteEditorCommand::onLoadParams(Params* params) void PaletteEditorCommand::onExecute(Context* context) { - Widget* palette_editor_view; + View* palette_editor_view; Widget* select_rgb; Widget* select_hsv; Button* expand_button; @@ -297,8 +297,8 @@ void PaletteEditorCommand::onExecute(Context* context) palette_editor = new PaletteView(true); palette_editor->setBoxSize(4*jguiscale()); - jview_attach(palette_editor_view, palette_editor); - jview_maxsize(palette_editor_view); + palette_editor_view->attachToView(palette_editor); + palette_editor_view->makeVisibleAllScrollableArea(); // Set palette editor columns palette_editor->setColumns(16); diff --git a/src/commands/cmd_preview.cpp b/src/commands/cmd_preview.cpp index bffa75368..e867c8bd6 100644 --- a/src/commands/cmd_preview.cpp +++ b/src/commands/cmd_preview.cpp @@ -82,8 +82,7 @@ void PreviewCommand::onExecute(Context* context) SpriteWriter sprite(editor->getSprite()); const Palette* pal = sprite->getCurrentPalette(); - JWidget view = jwidget_get_view(editor); - int scroll_x, scroll_y; + View* view = View::getView(editor); int u, v, x, y; int index_bg_color = -1; TiledMode tiled = context->getSettings()->getTiledMode(); @@ -94,8 +93,8 @@ void PreviewCommand::onExecute(Context* context) // Clear extras (e.g. pen preview) sprite->destroyExtraCel(); - JRect vp = jview_get_viewport_position(view); - jview_get_scroll(view, &scroll_x, &scroll_y); + gfx::Rect vp = view->getViewportBounds(); + gfx::Point scroll = view->getViewScroll(); int old_mouse_x = jmouse_x(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_position(JI_SCREEN_W/2, JI_SCREEN_H/2); - int pos_x = - scroll_x + vp->x1 + editor->editor_get_offset_x(); - int pos_y = - scroll_y + vp->y1 + editor->editor_get_offset_y(); + int pos_x = - scroll.x + vp.x + editor->editor_get_offset_x(); + int pos_y = - scroll.y + vp.y + editor->editor_get_offset_y(); int delta_x = 0; int delta_y = 0; @@ -236,7 +235,6 @@ void PreviewCommand::onExecute(Context* context) jmouse_set_cursor(JI_CURSOR_NORMAL); jmanager_refresh_screen(); - jrect_free(vp); } ////////////////////////////////////////////////////////////////////// diff --git a/src/commands/fx/cmd_color_curve.cpp b/src/commands/fx/cmd_color_curve.cpp index 7a029273c..b29b90e9f 100644 --- a/src/commands/fx/cmd_color_curve.cpp +++ b/src/commands/fx/cmd_color_curve.cpp @@ -83,9 +83,11 @@ bool ColorCurveCommand::onEnabled(Context* context) void ColorCurveCommand::onExecute(Context* context) { const CurrentSpriteReader sprite(context); - JWidget button_ok; - JWidget view_curve, curve_editor; - JWidget box_target, target_button; + Widget* button_ok; + Widget* curve_editor; + Widget* box_target; + Widget* target_button; + View* view_curve; if (!the_curve) { /* default curve */ @@ -118,7 +120,7 @@ void ColorCurveCommand::onExecute(Context* context) if (get_config_bool("ColorCurve", "Preview", 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_add_child(box_target, target_button); diff --git a/src/commands/fx/cmd_convolution_matrix.cpp b/src/commands/fx/cmd_convolution_matrix.cpp index 82e192ddb..698e4a531 100644 --- a/src/commands/fx/cmd_convolution_matrix.cpp +++ b/src/commands/fx/cmd_convolution_matrix.cpp @@ -97,7 +97,8 @@ void ConvolutionMatrixCommand::onExecute(Context* context) { const CurrentSpriteReader sprite(context); Widget* button_ok; - Widget* view_convmatr, *list_convmatr; + View* view_convmatr; + Widget *list_convmatr; Widget* box_target; Button* reload, *generate; @@ -126,7 +127,7 @@ void ConvolutionMatrixCommand::onExecute(Context* context) if (context->getSettings()->getTiledMode() != TILED_NONE) check_tiled->setSelected(true); - jview_attach(view_convmatr, list_convmatr); + view_convmatr->attachToView(list_convmatr); jwidget_set_min_size(view_convmatr, 128, 64); jwidget_add_child(box_target, target_button); @@ -222,7 +223,7 @@ static bool reload_select_hook(Widget* listbox) /* re-fill the list */ listbox_fill_convmatg(listbox); listbox_select_current_convmatr(listbox); - jview_update(jwidget_get_view(listbox)); + View::getView(listbox)->updateView(); 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_y = curve_editor_new(curve_y, -200, -200, 200, 200); - jview_attach(view_x, curvedit_x); - jview_attach(view_y, curvedit_y); + view_x->attachToView(curvedit_x); + view_y->attachToView(curvedit_y); jwidget_set_min_size(view_x, 64, 64); jwidget_set_min_size(view_y, 64, 64); diff --git a/src/console.cpp b/src/console.cpp index 10064c321..f5b2d25fb 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -51,7 +51,7 @@ Console::Console() else { Frame* window = new Frame(false, "Errors Console"); Grid* grid = new Grid(1, false); - Widget* view = jview_new(); + View* view = new View(); Widget* textbox = jtextbox_new(NULL, JI_WORDWRAP); Button* button = new Button("&Cancel"); @@ -61,7 +61,7 @@ Console::Console() // The "button" closes the console button->Click.connect(Bind(&Frame::closeWindow, window, button)); - jview_attach(view, textbox); + view->attachToView(textbox); jwidget_set_min_size(button, 60, 0); diff --git a/src/dialogs/aniedit.cpp b/src/dialogs/aniedit.cpp index 2aac03405..0f5e4401b 100644 --- a/src/dialogs/aniedit.cpp +++ b/src/dialogs/aniedit.cpp @@ -424,7 +424,7 @@ static bool anieditor_msg_proc(JWidget widget, JMessage msg) anieditor->scroll_x+jmouse_x(1)-jmouse_x(0), anieditor->scroll_y+jmouse_y(1)-jmouse_y(0), true); - jmouse_control_infinite_scroll(widget->rc); + jmouse_control_infinite_scroll(widget->getBounds()); return true; } /* if the mouse pressed the mouse's button in the separator, we diff --git a/src/dialogs/filesel.cpp b/src/dialogs/filesel.cpp index 69a6eea59..2f019c91c 100644 --- a/src/dialogs/filesel.cpp +++ b/src/dialogs/filesel.cpp @@ -169,7 +169,7 @@ base::string ase_file_selector(const base::string& message, goforward->Click.connect(Bind(&goforward_command, goforward)); goup->Click.connect(Bind(&goup_command, goup)); - JWidget view = jview_new(); + View* view = new View(); fileview = fileview_new(start_folder, exts); 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"); - jview_attach(view, fileview); + view->attachToView(fileview); jwidget_expansive(view, true); 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"); // update the view - jview_update(jwidget_get_view(fileview)); + View::getView(fileview)->updateView(); // open the window and run... the user press ok? again: diff --git a/src/dialogs/repo.cpp b/src/dialogs/repo.cpp index 5d06d8520..c54569dc6 100644 --- a/src/dialogs/repo.cpp +++ b/src/dialogs/repo.cpp @@ -43,7 +43,7 @@ void ji_show_repo_dlg(RepoDlg *repo_dlg) Frame* window = new Frame(false, repo_dlg->title); Box* box1 = new Box(JI_HORIZONTAL); Box* box2 = new Box(JI_VERTICAL); - Widget* view = jview_new(); + View* view = new View(); repo_dlg->listbox = jlistbox_new(); repo_dlg->button_use = new Button(repo_dlg->use_text); 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_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); /* fill the list */ @@ -111,7 +111,7 @@ static void fill_listbox(RepoDlg *repo_dlg) if (repo_dlg->load_listbox) (*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) @@ -197,7 +197,7 @@ static void add_command(Button* widget, RepoDlg* repo_dlg) if (added) { /* update the list-box */ - jview_update(jwidget_get_view(repo_dlg->listbox)); + View::getView(repo_dlg->listbox)->updateView(); /* select the last item */ jlistbox_select_index(repo_dlg->listbox, @@ -242,7 +242,7 @@ static void delete_command(Button* widget, RepoDlg* repo_dlg) } /* update the list-box */ - jview_update(jwidget_get_view(repo_dlg->listbox)); + View::getView(repo_dlg->listbox)->updateView(); } if (!ret) diff --git a/src/effect/effect.cpp b/src/effect/effect.cpp index b0706fd09..0a1876d30 100644 --- a/src/effect/effect.cpp +++ b/src/effect/effect.cpp @@ -174,14 +174,12 @@ void effect_begin_for_preview(Effect *effect) { 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 x, y, w, h; - editor->screen_to_editor(vp->x1, vp->y1, &x1, &y1); - editor->screen_to_editor(vp->x2-1, vp->y2-1, &x2, &y2); - - jrect_free(vp); + editor->screen_to_editor(vp.x, vp.y, &x1, &y1); + editor->screen_to_editor(vp.x+vp.w-1, vp.y+vp.h-1, &x2, &y2); if (x1 < 0) x1 = 0; if (y1 < 0) y1 = 0; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 5c12541bb..6d1ca9885 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -36,6 +36,7 @@ add_library(gui-lib property.cpp rect.cpp region.cpp + scroll_bar.cpp separator.cpp slider.cpp stream.cpp @@ -44,4 +45,5 @@ add_library(gui-lib theme.cpp tooltips.cpp view.cpp + viewport.cpp widget.cpp) diff --git a/src/gui/combobox.cpp b/src/gui/combobox.cpp index 0a7253ef1..b69349740 100644 --- a/src/gui/combobox.cpp +++ b/src/gui/combobox.cpp @@ -274,7 +274,7 @@ bool ComboBox::onProcessMessage(JMessage msg) case JM_BUTTONPRESSED: if (m_window != NULL) { - if (!jwidget_get_view(m_listbox)->hasMouse()) { + if (!View::getView(m_listbox)->hasMouse()) { closeListBox(); return true; } @@ -440,7 +440,7 @@ void ComboBox::openListBox() { if (!m_window) { m_window = new Frame(false, NULL); - Widget* view = jview_new(); + View* view = new View(); m_listbox = jlistbox_new(); m_listbox->user_data[0] = this; @@ -456,7 +456,7 @@ void ComboBox::openListBox() m_window->set_ontop(true); jwidget_noborders(m_window); - Widget* viewport = jview_get_viewport(view); + Widget* viewport = view->getViewport(); int size = getItemCount(); jwidget_set_min_size (viewport, @@ -466,7 +466,7 @@ void ComboBox::openListBox() +viewport->border_width.b); jwidget_add_child(m_window, view); - jview_attach(view, m_listbox); + view->attachToView(m_listbox); jwidget_signal_off(m_listbox); jlistbox_select_index(m_listbox, m_selected); diff --git a/src/gui/frame.cpp b/src/gui/frame.cpp index 64c4cd230..87af162ba 100644 --- a/src/gui/frame.cpp +++ b/src/gui/frame.cpp @@ -338,7 +338,7 @@ bool Frame::onProcessMessage(JMessage msg) /* TODO */ /* { */ /* JWidget manager = get_manager(); */ -/* JWidget view = jwidget_get_view(manager); */ +/* View* view = View::getView(manager); */ /* if (view) { */ /* jview_update(view); */ /* } */ diff --git a/src/gui/gui.h b/src/gui/gui.h index 2cb702c50..118708613 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -39,6 +39,7 @@ #include "gui/property.h" #include "gui/rect.h" #include "gui/region.h" +#include "gui/scroll_bar.h" #include "gui/separator.h" #include "gui/slider.h" #include "gui/stream.h" @@ -47,6 +48,7 @@ #include "gui/theme.h" #include "gui/tooltips.h" #include "gui/view.h" +#include "gui/viewport.h" #include "gui/widget.h" #endif diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp index ccd33bc0e..5ffd99b1c 100644 --- a/src/gui/listbox.cpp +++ b/src/gui/listbox.cpp @@ -8,6 +8,7 @@ #include +#include "gfx/point.h" #include "gfx/size.h" #include "gui/list.h" #include "gui/manager.h" @@ -92,24 +93,21 @@ void jlistbox_select_child(JWidget widget, JWidget listitem) } if (listitem) { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); listitem->setSelected(true); if (view) { - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + gfx::Rect vp = view->getViewportBounds(); + 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) - 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); + view->setViewScroll(scroll); } } @@ -131,19 +129,17 @@ int jlistbox_get_items_count(JWidget widget) /* setup the scroll to center the selected item in the viewport */ void jlistbox_center_scroll(JWidget widget) { - JWidget view = jwidget_get_view(widget); - JWidget listitem = jlistbox_get_selected_child(widget); + View* view = View::getView(widget); + Widget* listitem = jlistbox_get_selected_child(widget); if (view && listitem) { - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + gfx::Rect vp = view->getViewportBounds(); + gfx::Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x, - (listitem->rc->y1 - widget->rc->y1) - - jrect_h(vp)/2 + jrect_h(listitem->rc)/2); - jrect_free(vp); + scroll.y = ((listitem->rc->y1 - widget->rc->y1) + - vp.h/2 + jrect_h(listitem->rc)/2); + + view->setViewScroll(scroll); } } @@ -177,31 +173,29 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg) case JM_MOTION: if (widget->hasCapture()) { int select = jlistbox_get_selected_index(widget); - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); bool pick_item = true; if (view) { - JRect vp = jview_get_viewport_position(view); + gfx::Rect vp = view->getViewportBounds(); - if (msg->mouse.y < vp->y1) { - int num = MAX(1, (vp->y1 - msg->mouse.y) / 8); + if (msg->mouse.y < vp.y) { + int num = MAX(1, (vp.y - msg->mouse.y) / 8); jlistbox_select_index(widget, select-num); pick_item = false; } - else if (msg->mouse.y >= vp->y2) { - int num = MAX(1, (msg->mouse.y - (vp->y2-1)) / 8); + else if (msg->mouse.y >= vp.y + vp.h) { + int num = MAX(1, (msg->mouse.y - (vp.y+vp.h-1)) / 8); jlistbox_select_index(widget, select+num); pick_item = false; } - - jrect_free(vp); } if (pick_item) { JWidget picked; 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 { picked = widget->pick(msg->mouse.x, msg->mouse.y); @@ -221,16 +215,11 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg) break; case JM_WHEEL: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view) { - int scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x, - scroll_y + - (jmouse_z(1) - jmouse_z(0)) - *jwidget_get_text_height(widget)*3); + gfx::Point scroll = view->getViewScroll(); + scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3; + view->setViewScroll(scroll); } break; } @@ -238,7 +227,7 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg) case JM_KEYPRESSED: if (widget->hasFocus() && !jlist_empty(widget->children)) { 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); switch (msg->key.scancode) { @@ -256,18 +245,16 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg) break; case KEY_PGUP: if (view) { - JRect vp = jview_get_viewport_position(view); - select -= jrect_h(vp) / jwidget_get_text_height(widget); - jrect_free(vp); + gfx::Rect vp = view->getViewportBounds(); + select -= vp.h / jwidget_get_text_height(widget); } else select = 0; break; case KEY_PGDN: if (view) { - JRect vp = jview_get_viewport_position(view); - select += jrect_h(vp) / jwidget_get_text_height(widget); - jrect_free(vp); + gfx::Rect vp = view->getViewportBounds(); + select += vp.h / jwidget_get_text_height(widget); } else select = bottom; @@ -275,13 +262,13 @@ static bool listbox_msg_proc(JWidget widget, JMessage msg) case KEY_LEFT: case KEY_RIGHT: 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 scroll_x, scroll_y; - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, scroll_x + jrect_w(vp)/2*sgn, scroll_y); - jrect_free(vp); + scroll.x += vp.w/2*sgn; + + view->setViewScroll(scroll); } break; default: @@ -345,30 +332,27 @@ static void listbox_set_position(JWidget widget, JRect rect) static void listbox_dirty_children(JWidget widget) { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); JWidget child; JLink link; - JRect vp; if (!view) { JI_LIST_FOR_EACH(widget->children, link) reinterpret_cast(link->data)->invalidate(); } else { - vp = jview_get_viewport_position(view); + gfx::Rect vp = view->getViewportBounds(); JI_LIST_FOR_EACH(widget->children, link) { child = reinterpret_cast(link->data); - if (child->rc->y2 <= vp->y1) + if (child->rc->y2 <= vp.y) continue; - else if (child->rc->y1 >= vp->y2) + else if (child->rc->y1 >= vp.y+vp.h) break; child->invalidate(); } - - jrect_free(vp); } } diff --git a/src/gui/scroll_bar.cpp b/src/gui/scroll_bar.cpp new file mode 100644 index 000000000..407ff7449 --- /dev/null +++ b/src/gui/scroll_bar.cpp @@ -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(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(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(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; +} diff --git a/src/gui/scroll_bar.h b/src/gui/scroll_bar.h new file mode 100644 index 000000000..604bad6fd --- /dev/null +++ b/src/gui/scroll_bar.h @@ -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 diff --git a/src/gui/slider.cpp b/src/gui/slider.cpp index c02c5c4c5..cfc9c99b2 100644 --- a/src/gui/slider.cpp +++ b/src/gui/slider.cpp @@ -130,7 +130,7 @@ bool Slider::onProcessMessage(JMessage msg) jmouse_set_position(x, jmouse_y(0)); } /* 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_value = m_value; } diff --git a/src/gui/system.cpp b/src/gui/system.cpp index 5d960f402..caf7e7cf2 100644 --- a/src/gui/system.cpp +++ b/src/gui/system.cpp @@ -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_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; u = jmouse_x(0); v = jmouse_y(0); - if (u <= rect->x1) - x = rect->x2-2; - else if (u >= rect->x2-1) - x = rect->x1+1; + if (u <= rect.x) + x = rect.x+rect.w-2; + else if (u >= rect.x+rect.w-1) + x = rect.x+1; else x = u; - if (v <= rect->y1) - y = rect->y2-2; - else if (v >= rect->y2-1) - y = rect->y1+1; + if (v <= rect.y) + y = rect.y+rect.h-2; + else if (v >= rect.y+rect.h-1) + y = rect.y+1; else y = v; diff --git a/src/gui/system.h b/src/gui/system.h index 00757244e..4ccf8d232 100644 --- a/src/gui/system.h +++ b/src/gui/system.h @@ -7,6 +7,7 @@ #ifndef GUI_SYSTEM_H_INCLUDED #define GUI_SYSTEM_H_INCLUDED +#include "gfx/rect.h" #include "gui/base.h" struct BITMAP; @@ -78,6 +79,6 @@ int jmouse_x(int antique); int jmouse_y(int antique); int jmouse_z(int antique); -bool jmouse_control_infinite_scroll(JRect rect); +bool jmouse_control_infinite_scroll(const gfx::Rect& rect); #endif diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp index d1424e2ad..8d17e4c25 100644 --- a/src/gui/textbox.cpp +++ b/src/gui/textbox.cpp @@ -9,6 +9,7 @@ #include #include +#include "gfx/point.h" #include "gui/hook.h" #include "gui/intern.h" #include "gui/manager.h" @@ -49,71 +50,72 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg) case JM_SIGNAL: if (msg->signal.num == JI_SIGNAL_SET_TEXT) { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view) - jview_update(view); + view->updateView(); } break; case JM_KEYPRESSED: if (widget->hasFocus()) { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); 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 scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - + switch (msg->key.scancode) { case KEY_LEFT: - jview_set_scroll(view, scroll_x-jrect_w(vp)/2, scroll_y); + scroll.x -= vp.w/2; + view->setViewScroll(scroll); break; case KEY_RIGHT: - jview_set_scroll(view, scroll_x+jrect_w(vp)/2, scroll_y); + scroll.x += vp.w/2; + view->setViewScroll(scroll); break; case KEY_UP: - jview_set_scroll(view, scroll_x, scroll_y-jrect_h(vp)/2); + scroll.y -= vp.h/2; + view->setViewScroll(scroll); break; case KEY_DOWN: - jview_set_scroll(view, scroll_x, scroll_y+jrect_h(vp)/2); + scroll.y += vp.h/2; + view->setViewScroll(scroll); break; case KEY_PGUP: - jview_set_scroll(view, scroll_x, - scroll_y-(jrect_h(vp)-textheight)); + scroll.y -= (vp.h-textheight); + view->setViewScroll(scroll); break; case KEY_PGDN: - jview_set_scroll(view, scroll_x, - scroll_y+(jrect_h(vp)-textheight)); + scroll.y += (vp.h-textheight); + view->setViewScroll(scroll); break; case KEY_HOME: - jview_set_scroll(view, scroll_x, 0); + scroll.y = 0; + view->setViewScroll(scroll); break; case KEY_END: - jview_set_scroll(view, scroll_x, - jrect_h(widget->rc) - jrect_h(vp)); + scroll.y = jrect_h(widget->rc) - vp.h; + view->setViewScroll(scroll); break; default: - jrect_free(vp); return false; } - jrect_free(vp); } return true; } break; case JM_BUTTONPRESSED: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view) { widget->captureMouse(); jmouse_set_cursor(JI_CURSOR_SCROLL); @@ -123,24 +125,23 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg) } case JM_MOTION: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view && widget->hasCapture()) { - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + gfx::Rect vp = view->getViewportBounds(); + gfx::Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x + jmouse_x(1) - jmouse_x(0), - 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); - jrect_free(vp); } break; } case JM_BUTTONRELEASED: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view && widget->hasCapture()) { widget->releaseMouse(); jmouse_set_cursor(JI_CURSOR_NORMAL); @@ -150,16 +151,13 @@ static bool textbox_msg_proc(JWidget widget, JMessage msg) } case JM_WHEEL: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view) { - int scroll_x, scroll_y; + gfx::Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x, - scroll_y + - (jmouse_z(1) - jmouse_z(0)) - *jwidget_get_text_height(widget)*3); + scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3; + + view->setViewScroll(scroll); } 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); if (widget->getAlign() & JI_WORDWRAP) { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); int width, min = *w; if (view) { - JRect vp = jview_get_viewport_position(view); - width = jrect_w(vp); - jrect_free(vp); + width = view->getViewportBounds().w; } else { width = jrect_w(widget->rc); diff --git a/src/gui/theme.cpp b/src/gui/theme.cpp index 8f8729136..ee49f3e35 100644 --- a/src/gui/theme.cpp +++ b/src/gui/theme.cpp @@ -9,6 +9,8 @@ #include #include +#include "gfx/point.h" +#include "gfx/size.h" #include "gui/draw.h" #include "gui/font.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, 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 *beg, *end; int x1, y1, x2, y2; int x, y, chr, len; - int scroll_x, scroll_y; + gfx::Point scroll; int viewport_w, viewport_h; int textheight = jwidget_get_text_height(widget); FONT *font = widget->getFont(); @@ -164,20 +166,19 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, int width; if (view) { - JRect vp = jview_get_viewport_position(view); - x1 = vp->x1; - y1 = vp->y1; - viewport_w = jrect_w(vp); - viewport_h = jrect_h(vp); - jview_get_scroll(view, &scroll_x, &scroll_y); - jrect_free(vp); + gfx::Rect vp = view->getViewportBounds(); + x1 = vp.x; + y1 = vp.y; + viewport_w = vp.w; + viewport_h = vp.h; + scroll = view->getViewScroll(); } else { x1 = widget->rc->x1 + widget->border_width.l; y1 = widget->rc->y1 + widget->border_width.t; 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; - scroll_x = scroll_y = 0; + scroll.x = scroll.y = 0; } x2 = x1+viewport_w-1; y2 = y1+viewport_h-1; @@ -202,9 +203,8 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, #else /* make good use of the complete text-box */ if (view) { - int w, h; - jview_get_max_size(view, &w, &h); - width = MAX(viewport_w, w); + gfx::Size maxSize = view->getScrollableSize(); + width = MAX(viewport_w, maxSize.w); } else { width = viewport_w; @@ -214,9 +214,9 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, } /* draw line-by-line */ - y = y1 - scroll_y; + y = y1 - scroll.y; for (beg=end=text; end; ) { - x = x1 - scroll_x; + x = x1 - scroll.x; /* without word-wrap */ if (!(widget->getAlign() & JI_WORDWRAP)) { @@ -237,7 +237,7 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, } /* 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) *end = chr; @@ -297,7 +297,7 @@ void _ji_theme_textbox_draw(BITMAP *bmp, JWidget widget, /* height */ if (h) - *h = (y-y1+scroll_y); + *h = (y-y1+scroll.y); if (w) *w += widget->border_width.l + widget->border_width.r; if (h) *h += widget->border_width.t + widget->border_width.b; diff --git a/src/gui/view.cpp b/src/gui/view.cpp index ec3271876..fa4681989 100644 --- a/src/gui/view.cpp +++ b/src/gui/view.cpp @@ -17,637 +17,276 @@ #include "gui/view.h" #include "gui/widget.h" -#define BAR_SIZE widget->getTheme()->scrollbar_size +#define BAR_SIZE getTheme()->scrollbar_size 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 */ - 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; -}; + m_hasBars = true; -static void view_plain_update(JWidget widget); -static bool view_msg_proc(JWidget widget, JMessage msg); + jwidget_focusrest(this, true); + jwidget_add_child(this, &m_viewport); + setScrollableSize(Size(0, 0)); -static JWidget viewport_new(); -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; + initTheme(); } -bool jview_has_bars(JWidget widget) +bool View::hasScrollBars() { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - - return view->hasbars; + return m_hasBars; } -void jview_attach(JWidget widget, JWidget viewable_widget) +void View::attachToView(Widget* viewable_widget) { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - - jwidget_add_child(view->viewport, viewable_widget); + jwidget_add_child(&m_viewport, viewable_widget); /* 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(jwidget_get_data(widget, JI_VIEW)); - int req_w, req_h; + Size reqSize = m_viewport.calculateNeededSize(); - 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 = - + widget->border_width.l - + view->viewport->border_width.l - + req_w - + view->viewport->border_width.r - + widget->border_width.r; - - widget->min_h = - + widget->border_width.t - + view->viewport->border_width.t - + req_h - + view->viewport->border_width.b - + widget->border_width.b; + this->min_h = + + this->border_width.t + + m_viewport.border_width.t + + reqSize.h + + m_viewport.border_width.b + + this->border_width.b; } -void jview_without_bars(JWidget widget) +void View::hideScrollBars() { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); + m_hasBars = false; - view->hasbars = false; - - jview_update(widget); + updateView(); } -void jview_set_size(JWidget widget, int w, int h) +Size View::getScrollableSize() { -#define CHECK(w, h, l, t, r, b) \ - ((view->max_##w > jrect_##w(view->viewport->rc) \ - - view->viewport->border_width.l \ - - view->viewport->border_width.r) && \ + return Size(m_scrollbar_h.getSize(), + m_scrollbar_v.getSize()); +} + +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))) - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); JRect pos, rect; - view->max_w = w; - view->max_h = h; + m_scrollbar_h.setSize(sz.w); + m_scrollbar_v.setSize(sz.h); - pos = jwidget_get_child_rect(widget); + pos = jwidget_get_child_rect(this); /* setup scroll-bars */ - jwidget_remove_child(widget, view->scrollbar_h); - jwidget_remove_child(widget, view->scrollbar_v); + jwidget_remove_child(this, &m_scrollbar_h); + jwidget_remove_child(this, &m_scrollbar_v); - if (view->hasbars) { + if (m_hasBars) { if (CHECK(w, h, l, t, r, b)) { 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)) { pos->x2 -= BAR_SIZE; if (CHECK(w, h, l, t, r, b)) - jwidget_add_child(widget, view->scrollbar_v); + jwidget_add_child(this, &m_scrollbar_v); else { pos->x2 += 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)) { 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)) { pos->y2 -= BAR_SIZE; if (CHECK(h, w, t, l, b, r)) - jwidget_add_child(widget, view->scrollbar_h); + jwidget_add_child(this, &m_scrollbar_h); else { pos->x2 += 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, 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); - view->scrollbar_h->setVisible(true); + m_scrollbar_h.setVisible(true); } 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, 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); - view->scrollbar_v->setVisible(true); + m_scrollbar_v.setVisible(true); } else - view->scrollbar_v->setVisible(false); + m_scrollbar_v.setVisible(false); } - /* setup viewport */ - widget->invalidate(); - jwidget_set_rect(view->viewport, pos); - jview_set_scroll(widget, view->scroll_x, view->scroll_y); + // Setup viewport + this->invalidate(); + jwidget_set_rect(&m_viewport, pos); + setViewScroll(getViewScroll()); // Setup the same scroll-point jrect_free(pos); } -void jview_set_scroll(JWidget widget, int x, int y) +Size View::getVisibleSize() { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - int old_x = view->scroll_x; - 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; + return Size(jrect_w(m_viewport.rc) - m_viewport.border_width.l - m_viewport.border_width.r, + jrect_h(m_viewport.rc) - m_viewport.border_width.t - m_viewport.border_width.b); +} - view->scroll_x = MID(0, x, MAX(0, view->max_w - avail_w)); - view->scroll_y = MID(0, y, MAX(0, view->max_h - avail_h)); +Point View::getViewScroll() +{ + 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; - jwidget_set_rect(view->viewport, view->viewport->rc); - widget->invalidate(); + m_scrollbar_h.setPos(newScroll.x); + 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(jwidget_get_data(widget, JI_VIEW)); + Widget* vw = reinterpret_cast(jlist_first_data(m_viewport.children)); + Point scroll = getViewScroll(); - *x = view->scroll_x; - *y = view->scroll_y; -} + // Set minimum (remove scroll-bars) + setScrollableSize(Size(0, 0)); -void jview_get_max_size(JWidget widget, int *w, int *h) -{ - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); + // Set needed size + setScrollableSize(m_viewport.calculateNeededSize()); - *w = view->max_w; - *h = view->max_h; -} - -void jview_update(JWidget widget) -{ - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - JWidget vw = reinterpret_cast(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 there are scroll-bars, we have to setup the scrollable-size + // 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)) + setScrollableSize(m_viewport.calculateNeededSize()); if (vw) - jview_set_scroll(widget, - 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); */ + setViewScroll(scroll); else - jview_set_scroll(widget, 0, 0); + setViewScroll(Point(0, 0)); } -JWidget jview_get_viewport(JWidget widget) +Viewport* View::getViewport() { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - - return view->viewport; + return &m_viewport; } -JRect jview_get_viewport_position(JWidget widget) +Rect View::getViewportBounds() { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - - return jwidget_get_child_rect(view->viewport); + return m_viewport.getBounds() - m_viewport.getBorder(); } -void jtheme_scrollbar_info(JWidget widget, int *pos, int *len) +// static +View* View::getView(Widget* widget) { - scrollbar_info(widget, pos, len, NULL, NULL); -} - -/* for viewable widgets */ -JWidget jwidget_get_view(JWidget widget) -{ - if ((widget->parent) && (widget->parent->parent) && - ((widget->parent->type == JI_VIEW_VIEWPORT)) && - ((widget->parent->parent->type == JI_VIEW))) - return widget->parent->parent; + if ((widget->getParent()) && + (widget->getParent()->type == JI_VIEW_VIEWPORT) && + (widget->getParent()->getParent()) && + (widget->getParent()->getParent()->type == JI_VIEW)) + return static_cast(widget->getParent()->getParent()); else return 0; } -static void view_plain_update(JWidget widget) -{ - View* view = reinterpret_cast(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) +bool View::onProcessMessage(JMessage msg) { switch (msg->type) { - case JM_DESTROY: { - View* view = reinterpret_cast(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: { - View* view = reinterpret_cast(jwidget_get_data(widget, JI_VIEW)); - - Size viewSize = view->viewport->getPreferredSize(); + Size viewSize = m_viewport.getPreferredSize(); msg->reqsize.w = viewSize.w; msg->reqsize.h = viewSize.h; - msg->reqsize.w += widget->border_width.l + widget->border_width.r; - msg->reqsize.h += widget->border_width.t + widget->border_width.b; + msg->reqsize.w += this->border_width.l + this->border_width.r; + msg->reqsize.h += this->border_width.t + this->border_width.b; return true; } case JM_SETPOS: if (!_jwindow_is_moving()) { /* dirty trick */ - jrect_copy(widget->rc, &msg->setpos.rect); - jview_update(widget); + jrect_copy(this->rc, &msg->setpos.rect); + updateView(); } else { - displace_widgets(widget, - msg->setpos.rect.x1 - widget->rc->x1, - msg->setpos.rect.y1 - widget->rc->y1); + displaceWidgets(this, + msg->setpos.rect.x1 - this->rc->x1, + msg->setpos.rect.y1 - this->rc->y1); } return true; case JM_DRAW: - widget->getTheme()->draw_view(widget, &msg->draw.rect); + getTheme()->draw_view(this, &msg->draw.rect); return true; case JM_FOCUSENTER: case JM_FOCUSLEAVE: /* TODO add something to avoid this (theme specific stuff) */ { - JRegion reg1 = jwidget_get_drawable_region(widget, - JI_GDR_CUTTOPWINDOWS); - jregion_union(widget->update_region, widget->update_region, reg1); + JRegion reg1 = jwidget_get_drawable_region(this, JI_GDR_CUTTOPWINDOWS); + jregion_union(this->update_region, this->update_region, reg1); jregion_free(reg1); } break; } - return false; + return Widget::onProcessMessage(msg); } -static JWidget viewport_new() -{ - 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(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(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(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) +// static +void View::displaceWidgets(Widget* widget, int x, int y) { JLink link; jrect_displace(widget->rc, x, y); JI_LIST_FOR_EACH(widget->children, link) - displace_widgets(reinterpret_cast(link->data), x, y); + displaceWidgets(reinterpret_cast(link->data), x, y); } diff --git a/src/gui/view.h b/src/gui/view.h index 65283cab4..8b027d452 100644 --- a/src/gui/view.h +++ b/src/gui/view.h @@ -7,30 +7,53 @@ #ifndef 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 jview_maxsize(JWidget view); -void jview_without_bars(JWidget view); + void attachToView(Widget* viewableWidget); + void hideScrollBars(); + void makeVisibleAllScrollableArea(); -void jview_set_size(JWidget view, int w, int h); -void jview_set_scroll(JWidget view, int x, int y); -void jview_get_scroll(JWidget view, int *x, int *y); -void jview_get_max_size(JWidget view, int *w, int *h); + // Returns the maximum viewable size requested by the attached + // widget in the viewport. + gfx::Size getScrollableSize(); + 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); -JRect jview_get_viewport_position(JWidget view); + gfx::Point getViewScroll(); + void setViewScroll(const gfx::Point& pt); -/* for themes */ -void jtheme_scrollbar_info(JWidget scrollbar, int *pos, int *len); + void updateView(); -/* for viewable widgets */ -JWidget jwidget_get_view(JWidget viewable_widget); + Viewport* getViewport(); + 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 diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp new file mode 100644 index 000000000..5452ad5db --- /dev/null +++ b/src/gui/viewport.cpp @@ -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(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); +} diff --git a/src/gui/viewport.h b/src/gui/viewport.h new file mode 100644 index 000000000..22925c78d --- /dev/null +++ b/src/gui/viewport.h @@ -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 diff --git a/src/gui/widget.cpp b/src/gui/widget.cpp index 3e4fba82b..4987bfa42 100644 --- a/src/gui/widget.cpp +++ b/src/gui/widget.cpp @@ -832,9 +832,11 @@ JRegion jwidget_get_drawable_region(JWidget widget, int flags) manager = window ? window->getManager(): NULL; while (manager) { - view = jwidget_get_view(manager); - if (view) - cpos = jview_get_viewport_position(view); + view = View::getView(manager); + if (view) { + Rect vp = static_cast(view)->getViewportBounds(); + cpos = jrect_new(vp.x, vp.y, vp.x+vp.w, vp.y+vp.h); + } else cpos = jwidget_get_child_rect(manager); /* if (!manager->parent) */ diff --git a/src/modules/editors.cpp b/src/modules/editors.cpp index 63438498e..943b7432c 100644 --- a/src/modules/editors.cpp +++ b/src/modules/editors.cpp @@ -47,7 +47,7 @@ static EditorList editors; static int is_sprite_in_some_editor(Sprite *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); int init_module_editors() @@ -198,11 +198,11 @@ void set_current_editor(Editor* editor) { if (current_editor != editor) { if (current_editor) - jwidget_get_view(current_editor)->invalidate(); + View::getView(current_editor)->invalidate(); current_editor = editor; - jwidget_get_view(current_editor)->invalidate(); + View::getView(current_editor)->invalidate(); UIContext* context = UIContext::instance(); Sprite* sprite = current_editor->getSprite(); @@ -224,7 +224,7 @@ void set_sprite_in_current_editor(Sprite *sprite) current_editor->editor_set_sprite(sprite); - jwidget_get_view(current_editor)->invalidate(); + View::getView(current_editor)->invalidate(); app_refresh_screen(sprite); app_realloc_sprite_list(); @@ -259,20 +259,20 @@ void split_editor(Editor* editor, int align) return; } - JWidget view = jwidget_get_view(editor); + View* view = View::getView(editor); JWidget parent_box = view->getParent(); // box or panel /* create a new box to contain both editors, and a new view to put the new editor */ JWidget new_panel = jpanel_new(align); - JWidget new_view = editor_view_new(); + View* new_view = editor_view_new(); Editor* new_editor = create_new_editor(); /* insert the "new_box" in the same location that the view */ jwidget_replace_child(parent_box, view, new_panel); /* append the new editor */ - jview_attach(new_view, new_editor); + new_view->attachToView(new_editor); /* set the sprite for the new editor */ new_editor->editor_set_sprite(editor->getSprite()); @@ -288,14 +288,10 @@ void split_editor(Editor* editor, int align) /* same position */ { - int scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(new_view, scroll_x, scroll_y); + new_view->setViewScroll(view->getViewScroll()); jrect_copy(new_view->rc, view->rc); - jrect_copy(jview_get_viewport(new_view)->rc, - jview_get_viewport(view)->rc); + jrect_copy(new_view->getViewport()->rc, view->getViewport()->rc); jrect_copy(new_editor->rc, editor->rc); 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) { - JWidget view = jwidget_get_view(editor); + View* view = View::getView(editor); JWidget parent_box = view->getParent(); // Box or panel JWidget other_widget; @@ -357,7 +353,7 @@ void close_editor(Editor* editor) void make_unique_editor(Editor* editor) { - JWidget view = jwidget_get_view(editor); + View* view = View::getView(editor); JLink link, next; JWidget child; @@ -419,13 +415,14 @@ static Sprite* get_more_reliable_sprite() return NULL; } -static JWidget find_next_editor(JWidget widget) +static Widget* find_next_editor(Widget* widget) { - JWidget editor = NULL; + Widget* editor = NULL; JLink link; - if (widget->type == JI_VIEW) - editor = reinterpret_cast(jlist_first_data(jview_get_viewport(widget)->children)); + if (widget->type == JI_VIEW) { + editor = reinterpret_cast(jlist_first_data(static_cast(widget)->getViewport()->children)); + } else { JI_LIST_FOR_EACH(widget->children, link) if ((editor = find_next_editor(reinterpret_cast(link->data)))) diff --git a/src/skin/skin_theme.cpp b/src/skin/skin_theme.cpp index 9aab59adb..4c920f4d7 100644 --- a/src/skin/skin_theme.cpp +++ b/src/skin/skin_theme.cpp @@ -1383,13 +1383,14 @@ void SkinTheme::draw_view(JWidget widget, JRect clip) COLOR_BACKGROUND); } -void SkinTheme::draw_view_scrollbar(JWidget widget, JRect clip) +void SkinTheme::draw_view_scrollbar(JWidget _widget, JRect clip) { + ScrollBar* widget = static_cast(_widget); int x1, y1, x2, y2; int u1, v1, u2, v2; int pos, len; - - jtheme_scrollbar_info(widget, &pos, &len); + + widget->getScrollBarThemeInfo(&pos, &len); x1 = widget->rc->x1; y1 = widget->rc->y1; diff --git a/src/util/clipboard.cpp b/src/util/clipboard.cpp index 6473dadf6..6e4825575 100644 --- a/src/util/clipboard.cpp +++ b/src/util/clipboard.cpp @@ -250,19 +250,17 @@ void clipboard::paste(SpriteWriter& sprite) // Do the interactive-transform loop (where the user can move the floating image) { - JWidget view = jwidget_get_view(current_editor); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(current_editor); + gfx::Rect vp = view->getViewportBounds(); int x, y, x1, y1, x2, y2; - current_editor->screen_to_editor(vp->x1, vp->y1, &x1, &y1); - current_editor->screen_to_editor(vp->x2-1, vp->y2-1, &x2, &y2); + current_editor->screen_to_editor(vp.x, vp.y, &x1, &y1); + 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; y = (y1+y2)/2-src_image->h/2; paste = interactive_transform(current_editor, dst_image, src_image, x, y, xout, yout); - - jrect_free(vp); } if (paste) { @@ -324,12 +322,12 @@ static bool interactive_transform(Editor* editor, #define REDRAW() \ 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, \ - 0, 0, jrect_w(vp)-1, jrect_h(vp)-1, \ - x1-vp->x1, y1-vp->y1, x2-vp->x1, y2-vp->y1, \ - preview, mode, angle, cx-vp->x1, cy-vp->y1); \ - blit(bmp2, ji_screen, 0, 0, vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)); \ + 0, 0, vp.w-1, vp.h-1, \ + x1-vp.x, y1-vp.y, x2-vp.x, y2-vp.y, \ + preview, mode, angle, cx-vp.x, cy-vp.y); \ + blit(bmp2, ji_screen, 0, 0, vp.x, vp.y, vp.w, vp.h); \ update_status_bar(editor, image, x1, y1, x2, y2, angle); \ jmouse_show(); @@ -338,7 +336,7 @@ static bool interactive_transform(Editor* editor, int action = ACTION_SETMODE; int mode = SCALE_MODE; 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; fixed angle = 0; 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 double-buffered */ 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(); 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 */ if ((jmouse_b(0) == 1 && (key[KEY_LSHIFT] || key[KEY_RSHIFT])) || (jmouse_b(0) == 4)) { - JWidget view = jwidget_get_view(editor); - int scroll_x, scroll_y; + View* view = View::getView(editor); x = jmouse_x(0) - jmouse_x(1); y = jmouse_y(0) - jmouse_y(1); @@ -495,8 +492,8 @@ static bool interactive_transform(Editor* editor, /* TODO */ - jview_get_scroll(view, &scroll_x, &scroll_y); - editor->editor_set_scroll(scroll_x-x, scroll_y-y, true); + gfx::Point scroll = view->getViewScroll(); + editor->editor_set_scroll(scroll.x-x, scroll.y-y, true); /* editor_to_screen (widget, x1, y1, &x1, &y1); */ /* editor_to_screen (widget, x2, y2, &x2, &y2); */ @@ -711,7 +708,6 @@ static bool interactive_transform(Editor* editor, /* restore the cursor */ editor->show_drawing_cursor(); - jrect_free(vp); return done == DONE_PASTE; } diff --git a/src/widgets/curvedit.cpp b/src/widgets/curvedit.cpp index b7009393a..652698322 100644 --- a/src/widgets/curvedit.cpp +++ b/src/widgets/curvedit.cpp @@ -311,35 +311,19 @@ static bool curve_editor_msg_proc(JWidget widget, JMessage msg) switch (curve_editor->status) { case STATUS_SCROLLING: { - JWidget view = jwidget_get_view(widget); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(widget); + gfx::Rect vp = view->getViewportBounds(); + gfx::Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x+jmouse_x(1)-jmouse_x(0), - 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); - jrect_free(vp); 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: if (curve_editor->edit_point) { /* int old_x = *curve_editor->edit_x; */ diff --git a/src/widgets/editor.h b/src/widgets/editor.h index 63eafa4f1..ea8b42274 100644 --- a/src/widgets/editor.h +++ b/src/widgets/editor.h @@ -33,6 +33,7 @@ class IToolLoop; class ToolLoopManager; class PixelsMovement; class Tool; +class View; class Editor : public Widget { @@ -226,7 +227,7 @@ private: void (*pixel)(BITMAP *bmp, int x, int y, int color)); }; -JWidget editor_view_new(); +View* editor_view_new(); int editor_type(); #endif diff --git a/src/widgets/editor/click.cpp b/src/widgets/editor/click.cpp index 7201da52e..577bec2f7 100644 --- a/src/widgets/editor/click.cpp +++ b/src/widgets/editor/click.cpp @@ -118,33 +118,31 @@ int Editor::editor_click(int *x, int *y, int *update, /* the mouse was moved */ if (*update) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + gfx::Rect vp = view->getViewportBounds(); /* update scroll */ if (jmouse_control_infinite_scroll(vp)) { - int scroll_x, scroll_y; - if (scroll_callback) (*scroll_callback)(true); /* smooth scroll movement */ if (get_config_bool("Options", "MoveSmooth", true)) { - jmouse_set_position(MID(vp->x1+1, click_last_x, vp->x2-2), - MID(vp->y1+1, click_last_y, vp->y2-2)); + jmouse_set_position(MID(vp.x+1, click_last_x, vp.x+vp.w-2), + MID(vp.y+1, click_last_y, vp.y+vp.h-2)); } /* this is better for high resolutions: scroll movement by big steps */ else { 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 + (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); - editor_set_scroll(scroll_x+click_last_x-jmouse_x(0), - scroll_y+click_last_y-jmouse_y(0), true); + gfx::Point scroll = view->getViewScroll(); + editor_set_scroll(scroll.x+click_last_x-jmouse_x(0), + scroll.y+click_last_y-jmouse_y(0), true); click_last_x = jmouse_x(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 *update = ((prev_x != click_last_x) || (prev_y != click_last_y)); } - - jrect_free(vp); } /* click-and-click mode */ diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index 88953719c..30ba6a260 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -60,9 +60,9 @@ using namespace gfx; 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(widget->getTheme()); int l = theme->get_part(PART_EDITOR_SELECTED_W)->w; 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; 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); return widget; @@ -161,16 +161,14 @@ void Editor::editor_set_sprite(Sprite* sprite) editor_update(); if (preferred.virgin) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); preferred.virgin = false; - preferred.scroll_x = -jrect_w(vp)/2 + (sprite->getWidth()/2); - preferred.scroll_y = -jrect_h(vp)/2 + (sprite->getHeight()/2); + preferred.scroll_x = -vp.w/2 + (sprite->getWidth()/2); + preferred.scroll_y = -vp.h/2 + (sprite->getHeight()/2); m_sprite->setPreferredEditorSettings(preferred); - - jrect_free(vp); } 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 void Editor::editor_set_scroll(int x, int y, int use_refresh_region) { - JWidget view = jwidget_get_view(this); - int old_scroll_x, old_scroll_y; + View* view = View::getView(this); + Point oldScroll; JRegion region = NULL; 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) { 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) { PreferredEditorSettings preferred; @@ -216,14 +214,12 @@ void Editor::editor_set_scroll(int x, int y, int use_refresh_region) } if (use_refresh_region) { - int new_scroll_x, new_scroll_y; - - jview_get_scroll(view, &new_scroll_x, &new_scroll_y); + Point newScroll = view->getViewScroll(); // Move screen with blits jwidget_scroll(this, region, - old_scroll_x - new_scroll_x, - old_scroll_y - new_scroll_y); + oldScroll.x - newScroll.x, + oldScroll.y - newScroll.y); jregion_free(region); /* 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() { - JWidget view = jwidget_get_view(this); - jview_update(view); + View::getView(this)->updateView(); } /** @@ -253,43 +248,42 @@ void Editor::editor_update() */ void Editor::editor_draw_sprite(int x1, int y1, int x2, int y2) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); int source_x, source_y, dest_x, dest_y, width, height; - int scroll_x, scroll_y; // Get scroll - jview_get_scroll(view, &scroll_x, &scroll_y); + Point scroll = view->getViewScroll(); // Output information source_x = x1 << m_zoom; source_y = y1 << m_zoom; - dest_x = vp->x1 - scroll_x + m_offset_x + source_x; - dest_y = vp->y1 - scroll_y + m_offset_y + source_y; + dest_x = vp.x - scroll.x + m_offset_x + source_x; + dest_y = vp.y - scroll.y + m_offset_y + source_y; width = (x2 - x1 + 1) << m_zoom; height = (y2 - y1 + 1) << m_zoom; // Clip from viewport - if (dest_x < vp->x1) { - source_x += vp->x1 - dest_x; - width -= vp->x1 - dest_x; - dest_x = vp->x1; + if (dest_x < vp.x) { + source_x += vp.x - dest_x; + width -= vp.x - dest_x; + dest_x = vp.x; } - if (dest_y < vp->y1) { - source_y += vp->y1 - dest_y; - height -= vp->y1 - dest_y; - dest_y = vp->y1; + if (dest_y < vp.y) { + source_y += vp.y - dest_y; + height -= vp.y - dest_y; + dest_y = vp.y; } - if (dest_x+width-1 > vp->x2-1) - width = vp->x2-dest_x; + if (dest_x+width-1 > vp.x + vp.w-1) + width = vp.x + vp.w - dest_x; - if (dest_y+height-1 > vp->y2-1) - height = vp->y2-dest_y; + if (dest_y+height-1 > vp.y + vp.h-1) + height = vp.y + vp.h - dest_y; // 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 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() { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll = view->getViewScroll(); int x1, y1, x2, y2; int c, x, y; - jview_get_scroll(view, &scroll_x, &scroll_y); - dotted_mode(m_offset_count); - x = vp->x1 - scroll_x + m_offset_x; - y = vp->y1 - scroll_y + m_offset_y; + x = vp.x - scroll.x + m_offset_x; + y = vp.y - scroll.y + m_offset_y; int nseg = m_sprite->getBoundariesSegmentsCount(); const _BoundSeg* seg = m_sprite->getBoundariesSegments(); @@ -469,8 +459,6 @@ void Editor::editor_draw_mask() } dotted_mode(-1); - - jrect_free(vp); } void Editor::editor_draw_mask_safe() @@ -515,17 +503,15 @@ void Editor::drawGrid(const Rect& gridBounds, const Color& color) return; int grid_color = color_utils::color_for_allegro(color, bitmap_color_depth(ji_screen)); - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll = view->getViewScroll(); int x1, y1, x2, y2; int u1, v1, u2, v2; int c; - jview_get_scroll(view, &scroll_x, &scroll_y); - - scroll_x = vp->x1 - scroll_x + m_offset_x; - scroll_y = vp->y1 - scroll_y + m_offset_y; + scroll.x = vp.x - scroll.x + m_offset_x; + scroll.y = vp.y - scroll.y + m_offset_y; x1 = ji_screen->cl; y1 = ji_screen->ct; @@ -549,20 +535,18 @@ void Editor::drawGrid(const Rect& gridBounds, const Color& color) grid.h <<= m_zoom; // Horizontal lines - x1 = scroll_x+grid.x+u1*grid.w; - x2 = scroll_x+grid.x+u2*grid.w; + x1 = scroll.x+grid.x+u1*grid.w; + x2 = scroll.x+grid.x+u2*grid.w; 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 - y1 = scroll_y+grid.y+v1*grid.h; - y2 = scroll_y+grid.y+v2*grid.h; + y1 = scroll.y+grid.y+v1*grid.h; + y2 = scroll.y+grid.y+v2*grid.h; for (c=u1; c<=u2; c++) - vline(ji_screen, scroll_x+grid.x+c*grid.w, y1, y2, grid_color); - - jrect_free(vp); + vline(ji_screen, scroll.x+grid.x+c*grid.w, y1, y2, grid_color); } void Editor::flashCurrentLayer() @@ -649,11 +633,10 @@ void Editor::turnOnSelectionModifiers() */ void Editor::controlInfiniteScroll(JMessage msg) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); if (jmouse_control_infinite_scroll(vp)) { - int scroll_x, scroll_y; int old_x = msg->mouse.x; int old_y = msg->mouse.y; @@ -662,27 +645,22 @@ void Editor::controlInfiniteScroll(JMessage msg) // Smooth scroll movement if (get_config_bool("Options", "MoveSmooth", TRUE)) { - jmouse_set_position(MID(vp->x1+1, old_x, vp->x2-2), - MID(vp->y1+1, old_y, vp->y2-2)); + jmouse_set_position(MID(vp.x+1, old_x, vp.x+vp.w-2), + MID(vp.y+1, old_y, vp.y+vp.h-2)); } // This is better for high resolutions: scroll movement by big steps else { - jmouse_set_position((old_x != msg->mouse.x) ? - (old_x + (vp->x1+vp->x2)/2)/2: msg->mouse.x, - - (old_y != msg->mouse.y) ? - (old_y + (vp->y1+vp->y2)/2)/2: msg->mouse.y); + jmouse_set_position((old_x != msg->mouse.x) ? (old_x + (vp.x+vp.w/2))/2: msg->mouse.x, + (old_y != msg->mouse.y) ? (old_y + (vp.y+vp.h/2))/2: msg->mouse.y); } msg->mouse.x = jmouse_x(0); msg->mouse.y = jmouse_y(0); - jview_get_scroll(view, &scroll_x, &scroll_y); - editor_set_scroll(scroll_x+old_x-msg->mouse.x, - scroll_y+old_y-msg->mouse.y, true); + Point scroll = view->getViewScroll(); + editor_set_scroll(scroll.x+old_x-msg->mouse.x, + scroll.y+old_y-msg->mouse.y, true); } - - jrect_free(vp); } void Editor::dropPixels() @@ -718,30 +696,22 @@ Tool* Editor::getCurrentEditorTool() void Editor::screen_to_editor(int xin, int yin, int *xout, int *yout) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - - *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); + *xout = (xin - vp.x + scroll.x - m_offset_x) >> m_zoom; + *yout = (yin - vp.y + scroll.y - m_offset_y) >> m_zoom; } void Editor::editor_to_screen(int xin, int yin, int *xout, int *yout) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - - *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); + *xout = (vp.x - scroll.x + m_offset_x + (xin << m_zoom)); + *yout = (vp.y - scroll.y + m_offset_y + (yin << m_zoom)); } 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 jrect_copy(widget->rc, &msg->setpos.rect); - jview_update(widget); + static_cast(widget)->updateView(); return true; case JM_DRAW: { - JWidget viewport = jview_get_viewport(widget); - JWidget child = reinterpret_cast(jlist_first_data(viewport->children)); + Widget* viewport = static_cast(widget)->getViewport(); + Widget* child = reinterpret_cast(jlist_first_data(viewport->children)); JRect pos = jwidget_get_rect(widget); SkinTheme* theme = static_cast(widget->getTheme()); @@ -930,15 +900,11 @@ bool Editor::onProcessMessage(JMessage msg) // Editor without sprite if (!m_sprite) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); jdraw_rectfill(vp, theme->get_editor_face_color()); - draw_emptyset_symbol(ji_screen, - Rect(vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)), - makecol(64, 64, 64)); - - jrect_free(vp); + draw_emptyset_symbol(ji_screen, vp, makecol(64, 64, 64)); } // Editor with sprite else { @@ -988,10 +954,9 @@ bool Editor::onProcessMessage(JMessage msg) // The sprite is locked to be read... we can draw an opaque // background only. - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); jdraw_rectfill(vp, theme->get_editor_face_color()); - jrect_free(vp); } } @@ -1199,16 +1164,14 @@ bool Editor::onProcessMessage(JMessage msg) // Move the scroll if (m_state == EDITOR_STATE_SCROLLING) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll = view->getViewScroll(); - jview_get_scroll(view, &scroll_x, &scroll_y); - editor_set_scroll(scroll_x+jmouse_x(1)-jmouse_x(0), - scroll_y+jmouse_y(1)-jmouse_y(0), true); + editor_set_scroll(scroll.x+jmouse_x(1)-jmouse_x(0), + scroll.y+jmouse_y(1)-jmouse_y(0), true); jmouse_control_infinite_scroll(vp); - jrect_free(vp); { int x, y; @@ -1472,18 +1435,18 @@ bool Editor::onProcessMessage(JMessage msg) case WHEEL_HSCROLL: case WHEEL_VSCROLL: { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); + Point scroll; int dx = 0; int dy = 0; int thick = m_cursor_thick; if (wheelAction == WHEEL_HSCROLL) { - dx = dz * jrect_w(vp); + dx = dz * vp.w; } else { - dy = dz * jrect_h(vp); + dy = dz * vp.h; } if (scrollBigSteps) { @@ -1495,17 +1458,15 @@ bool Editor::onProcessMessage(JMessage msg) dy /= 10; } - jview_get_scroll(view, &scroll_x, &scroll_y); + scroll = view->getViewScroll(); jmouse_hide(); if (thick) 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) editor_draw_cursor(jmouse_x(0), jmouse_y(0)); jmouse_show(); - - jrect_free(vp); break; } @@ -1544,16 +1505,14 @@ void Editor::onCurrentToolChange() void Editor::editor_request_size(int *w, int *h) { if (m_sprite) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); - m_offset_x = jrect_w(vp)/2 - 1; - m_offset_y = jrect_h(vp)/2 - 1; + m_offset_x = vp.w/2 - 1; + m_offset_y = vp.h/2 - 1; *w = (m_sprite->getWidth() << m_zoom) + m_offset_x*2; *h = (m_sprite->getHeight() << m_zoom) + m_offset_y*2; - - jrect_free(vp); } else { *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) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + Rect vp = view->getViewportBounds(); int x, y; bool centerMouse = get_config_bool("Editor", "CenterMouseInZoom", false); 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); if (centerMouse) { - mx = (vp->x1+vp->x2)/2; - my = (vp->y1+vp->y2)/2; + mx = vp.x+vp.w/2; + my = vp.y+vp.h/2; } else { mx = mouse_x; my = mouse_y; } - x = m_offset_x - (mx - vp->x1) + ((1<>1) + (x << zoom); - y = m_offset_y - (my - vp->y1) + ((1<>1) + (y << zoom); + x = m_offset_x - (mx - vp.x) + ((1<>1) + (x << zoom); + y = m_offset_y - (my - vp.y) + ((1<>1) + (y << zoom); if ((m_zoom != zoom) || (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); } show_drawing_cursor(); - jrect_free(vp); } ////////////////////////////////////////////////////////////////////// diff --git a/src/widgets/editor/keys.cpp b/src/widgets/editor/keys.cpp index 6afbb4086..d0bb94242 100644 --- a/src/widgets/editor/keys.cpp +++ b/src/widgets/editor/keys.cpp @@ -44,8 +44,8 @@ bool Editor::editor_keys_toset_zoom(int scancode) !key[KEY_LCONTROL] && !key[KEY_RCONTROL] && !key[KEY_ALT]) { - JWidget view = jwidget_get_view(this); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(this); + gfx::Rect vp = view->getViewportBounds(); int x, y, zoom; 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)); return true; } - - jrect_free(vp); } return false; diff --git a/src/widgets/fileview.cpp b/src/widgets/fileview.cpp index 2f370903e..b17970778 100644 --- a/src/widgets/fileview.cpp +++ b/src/widgets/fileview.cpp @@ -161,7 +161,7 @@ void fileview_set_current_folder(JWidget widget, IFileItem* folder) jwidget_emit_signal(widget, SIGNAL_FILEVIEW_CURRENT_FOLDER_CHANGED); widget->invalidate(); - jview_update(jwidget_get_view(widget)); + View::getView(widget)->updateView(); } const FileItemList& fileview_get_filelist(JWidget widget) @@ -233,8 +233,8 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) return true; case JM_DRAW: { - JWidget view = jwidget_get_view(widget); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(widget); + gfx::Rect vp = view->getViewportBounds(); int iw, ih; int th = jwidget_get_text_height(widget); int x, y = widget->rc->y1; @@ -359,9 +359,9 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) /* draw the thumbnail */ if (thumbnail) { - x = vp->x2-2-thumbnail->w; + x = vp.x+vp.w-2-thumbnail->w; 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); rect(ji_screen, @@ -371,11 +371,7 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) // is the current folder empty? if (fileview->list.empty()) - draw_emptyset_symbol(ji_screen, - Rect(vp->x1, vp->y1, jrect_w(vp), jrect_h(vp)), - makecol(194, 194, 194)); - - jrect_free(vp); + draw_emptyset_symbol(ji_screen, vp, makecol(194, 194, 194)); return true; } @@ -426,7 +422,7 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) case JM_KEYPRESSED: if (widget->hasFocus()) { int select = fileview_get_selected_index(widget); - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); int bottom = fileview->list.size(); switch (msg->key.scancode) { @@ -451,23 +447,20 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) case KEY_PGUP: case KEY_PGDN: { int sgn = (msg->key.scancode == KEY_PGUP) ? -1: 1; - JRect vp = jview_get_viewport_position(view); + gfx::Rect vp = view->getViewportBounds(); if (select < 0) select = 0; - select += sgn * jrect_h(vp) / (2+jwidget_get_text_height(widget)+2); - jrect_free(vp); + select += sgn * vp.h / (2+jwidget_get_text_height(widget)+2); break; } case KEY_LEFT: case KEY_RIGHT: 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 scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, scroll_x + jrect_w(vp)/2*sgn, scroll_y); - jrect_free(vp); + gfx::Point scroll = view->getViewScroll(); + scroll.x += vp.w/2*sgn; + view->setViewScroll(scroll); } break; case KEY_ENTER: @@ -532,16 +525,11 @@ static bool fileview_msg_proc(JWidget widget, JMessage msg) break; case JM_WHEEL: { - JWidget view = jwidget_get_view(widget); + View* view = View::getView(widget); if (view) { - int scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x, - scroll_y + - (jmouse_z(1) - jmouse_z(0)) - *(2+jwidget_get_text_height(widget)+2)*3); + gfx::Point scroll = view->getViewScroll(); + scroll.y += (jmouse_z(1)-jmouse_z(0)) * 3*(2+jwidget_get_text_height(widget)+2); + view->setViewScroll(scroll); } 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) { FileView* fileview = fileview_data(widget); - JWidget view = jwidget_get_view(widget); - JRect vp = jview_get_viewport_position(view); + View* view = View::getView(widget); + gfx::Rect vp = view->getViewportBounds(); + gfx::Point scroll = view->getViewScroll(); int iw, ih; int th = jwidget_get_text_height(widget); int y = widget->rc->y1; - int scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); // rows for (FileItemList::iterator @@ -616,18 +602,17 @@ static void fileview_make_selected_fileitem_visible(JWidget widget) fileview_get_fileitem_size(widget, fi, &iw, &ih); if (fi == fileview->selected) { - if (y < vp->y1) - jview_set_scroll(view, scroll_x, y - widget->rc->y1); - else if (y > vp->y2 - (2+th+2)) - jview_set_scroll(view, scroll_x, - y - widget->rc->y1 - jrect_h(vp) + (2+th+2)); + if (y < vp.y) + scroll.y = y - widget->rc->y1; + else if (y > vp.y + vp.h - (2+th+2)) + scroll.y = y - widget->rc->y1 - vp.h + (2+th+2); + + view->setViewScroll(scroll); break; } y += ih; } - - jrect_free(vp); } static void fileview_regenerate_list(JWidget widget) diff --git a/src/widgets/palette_view.cpp b/src/widgets/palette_view.cpp index 846e222f6..8de72e6bb 100644 --- a/src/widgets/palette_view.cpp +++ b/src/widgets/palette_view.cpp @@ -23,6 +23,7 @@ #include #include "app/color.h" +#include "gfx/point.h" #include "gui/manager.h" #include "gui/message.h" #include "gui/rect.h" @@ -80,9 +81,9 @@ void PaletteView::setColumns(int columns) m_columns = columns; if (m_columns != old_columns) { - Widget* view = jwidget_get_view(this); + View* view = View::getView(this); if (view) - jview_update(view); + view->updateView(); invalidate(); } @@ -556,15 +557,11 @@ bool PaletteView::onProcessMessage(JMessage msg) return true; case JM_WHEEL: { - JWidget view = jwidget_get_view(this); + View* view = View::getView(this); if (view) { - int scroll_x, scroll_y; - - jview_get_scroll(view, &scroll_x, &scroll_y); - jview_set_scroll(view, - scroll_x, - scroll_y + - (jmouse_z(1) - jmouse_z(0)) * 3*m_boxsize); + gfx::Point scroll = view->getViewScroll(); + scroll.y += (jmouse_z(1)-jmouse_z(0)) * 3 * m_boxsize; + view->setViewScroll(scroll); } break; } @@ -589,33 +586,32 @@ void PaletteView::request_size(int* w, int* h) void PaletteView::update_scroll(int color) { - Widget* view = jwidget_get_view(this); - if (view != NULL) { - JRect vp = jview_get_viewport_position(view); - int scroll_x, scroll_y; - int x, y, cols; - div_t d; + View* view = View::getView(this); + if (!view) + return; - 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); - cols = m_columns; + scroll = view->getViewScroll(); - y = (m_boxsize+this->child_spacing) * (color / cols); - x = (m_boxsize+this->child_spacing) * (color % cols); + d = div(256, m_columns); + cols = m_columns; - if (scroll_x > x) - scroll_x = x; - else if (scroll_x+jrect_w(vp)-m_boxsize-2 < x) - scroll_x = x-jrect_w(vp)+m_boxsize+2; + y = (m_boxsize+this->child_spacing) * (color / cols); + x = (m_boxsize+this->child_spacing) * (color % cols); - if (scroll_y > y) - scroll_y = y; - else if (scroll_y+jrect_h(vp)-m_boxsize-2 < y) - scroll_y = y-jrect_h(vp)+m_boxsize+2; + if (scroll.x > x) + scroll.x = x; + else if (scroll.x+vp.w-m_boxsize-2 < x) + 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); } diff --git a/src/xml_widgets.cpp b/src/xml_widgets.cpp index 5d2183835..579452d4f 100644 --- a/src/xml_widgets.cpp +++ b/src/xml_widgets.cpp @@ -296,7 +296,7 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root) } /* view */ else if (ustrcmp(elem_name, "view") == 0) { - widget = jview_new(); + widget = new View(); } /* window */ else if (ustrcmp(elem_name, "window") == 0) { @@ -377,7 +377,7 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root) if (child) { // Attach the child in the view if (widget->type == JI_VIEW) { - jview_attach(widget, child); + static_cast(widget)->attachToView(child); break; } // 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) { bool maxsize = bool_attr_is_true(elem, "maxsize"); if (maxsize) - jview_maxsize(widget); + static_cast(widget)->makeVisibleAllScrollableArea(); } }