aseprite/src/ui/theme.cpp

305 lines
6.8 KiB
C++
Raw Normal View History

// Aseprite UI Library
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
2007-09-19 07:57:02 +08:00
#ifdef HAVE_CONFIG_H
2009-07-13 04:29:16 +08:00
#include "config.h"
#endif
2009-07-13 04:29:16 +08:00
#include "gfx/point.h"
#include "gfx/size.h"
#include "she/font.h"
#include "she/surface.h"
#include "she/system.h"
2012-06-18 09:49:58 +08:00
#include "ui/intern.h"
#include "ui/manager.h"
#include "ui/system.h"
#include "ui/theme.h"
#include "ui/view.h"
#include "ui/widget.h"
2007-09-19 07:57:02 +08:00
#include <cstring>
namespace ui {
static Theme* current_theme = NULL;
2007-09-19 07:57:02 +08:00
Theme::Theme()
: m_guiscale(1)
2007-09-19 07:57:02 +08:00
{
}
Theme::~Theme()
{
if (current_theme == this)
set_theme(nullptr);
}
void Theme::regenerate()
2007-09-19 07:57:02 +08:00
{
CursorType type = get_mouse_cursor();
set_mouse_cursor(kNoCursor);
onRegenerate();
details::resetFontAllWidgets();
// TODO We cannot reinitialize all widgets because this mess all
// child spacing, border, etc. But it could be good to change the
// uiscale() and get the new look without the need to restart the
// whole app.
//details::reinitThemeForAllWidgets();
set_mouse_cursor(type);
2007-09-19 07:57:02 +08:00
}
//////////////////////////////////////////////////////////////////////
2007-09-19 07:57:02 +08:00
void set_theme(Theme* theme)
2007-09-19 07:57:02 +08:00
{
if (theme) {
// As the regeneration may fail, first we regenerate the theme and
// then we set is as "the current theme." E.g. In case that we'd
// like to show some kind of error message with the UI controls,
// we should be able to use the previous theme to do so (instead
// of this new unsuccessfully regenerated theme).
theme->regenerate();
current_theme = theme;
2007-09-19 07:57:02 +08:00
Manager* manager = Manager::getDefault();
if (manager && !manager->theme())
manager->setTheme(theme);
2007-09-19 07:57:02 +08:00
}
}
Theme* get_theme()
2007-09-19 07:57:02 +08:00
{
return current_theme;
2007-09-19 07:57:02 +08:00
}
// static
void Theme::drawSlices(Graphics* g, she::Surface* sheet,
const gfx::Rect& rc,
const gfx::Rect& sprite,
const gfx::Rect& slices,
const bool drawCenter)
{
const int w1 = slices.x;
const int h1 = slices.y;
const int w2 = slices.w;
const int h2 = slices.h;
const int w3 = sprite.w-w1-w2;
const int h3 = sprite.h-h1-h2;
const int x2 = rc.x2()-w3;
const int y2 = rc.y2()-h3;
// Top
int x = rc.x;
int y = rc.y;
g->drawRgbaSurface(sheet, sprite.x, sprite.y,
x, y, w1, h1);
{
IntersectClip clip(g, gfx::Rect(rc.x+w1, rc.y, rc.w-w1-w3, h1));
if (clip) {
for (x+=w1; x<x2; x+=w2) {
g->drawRgbaSurface(sheet, sprite.x+w1, sprite.y,
x, y, w2, h1);
}
}
}
g->drawRgbaSurface(sheet, sprite.x+w1+w2, sprite.y,
x2, y, w3, h1);
// Bottom
x = rc.x;
y = y2;
g->drawRgbaSurface(sheet, sprite.x, sprite.y+h1+h2,
x, y, w1, h3);
{
IntersectClip clip(g, gfx::Rect(rc.x+w1, y2, rc.w-w1-w3, h3));
if (clip) {
for (x+=w1; x<x2; x+=w2) {
g->drawRgbaSurface(sheet, sprite.x+w1, sprite.y+h1+h2,
x, y2, w2, h3);
}
}
}
g->drawRgbaSurface(sheet, sprite.x+w1+w2, sprite.y+h1+h2,
x2, y2, w3, h3);
// Left & Right
IntersectClip clip(g, gfx::Rect(rc.x, rc.y+h1, rc.w, rc.h-h1-h3));
if (clip) {
for (y=rc.y+h1; y<y2; y+=h2) {
// Left
g->drawRgbaSurface(sheet, sprite.x, sprite.y+h1,
rc.x, y, w1, h2);
// Right
g->drawRgbaSurface(sheet, sprite.x+w1+w2, sprite.y+h1,
x2, y, w3, h2);
}
}
// Center
if (drawCenter) {
IntersectClip clip(g, gfx::Rect(rc.x+w1, rc.y+h1, rc.w-w1-w3, rc.h-h1-h3));
if (clip) {
for (y=rc.y+h1; y<y2; y+=h2) {
for (x=rc.x+w1; x<x2; x+=w2)
g->drawRgbaSurface(sheet, sprite.x+w1, sprite.y+h1, x, y, w2, h2);
}
}
}
}
// static
void Theme::drawTextBox(Graphics* g, Widget* widget,
int* w, int* h, gfx::Color bg, gfx::Color fg)
2007-09-19 07:57:02 +08:00
{
View* view = View::getView(widget);
char* text = const_cast<char*>(widget->text().c_str());
char* beg, *end;
2016-12-07 08:29:14 +08:00
int x1, y1;
2007-09-19 07:57:02 +08:00
int x, y, chr, len;
gfx::Point scroll;
int textheight = widget->textHeight();
she::Font* font = widget->font();
2007-09-19 07:57:02 +08:00
char *beg_end, *old_end;
int width;
2016-11-22 23:47:46 +08:00
gfx::Rect vp;
2007-09-19 07:57:02 +08:00
if (view) {
2016-11-22 23:47:46 +08:00
vp = view->viewportBounds().offset(-widget->bounds().origin());
scroll = view->viewScroll();
2007-09-19 07:57:02 +08:00
}
else {
2016-11-22 23:47:46 +08:00
vp = widget->clientBounds();
scroll.x = scroll.y = 0;
2007-09-19 07:57:02 +08:00
}
2016-12-07 08:29:14 +08:00
x1 = widget->clientBounds().x + widget->border().left();
y1 = widget->clientBounds().y + widget->border().top();
2007-09-19 07:57:02 +08:00
2016-11-23 00:33:01 +08:00
// Fill background
if (g)
g->fillRect(bg, vp);
2007-09-19 07:57:02 +08:00
chr = 0;
// Without word-wrap
if (!(widget->align() & WORDWRAP)) {
2016-12-07 08:29:14 +08:00
width = widget->clientChildrenBounds().w;
2007-09-19 07:57:02 +08:00
}
// With word-wrap
2007-09-19 07:57:02 +08:00
else {
if (w) {
width = *w;
*w = 0;
}
else {
2016-11-22 23:47:46 +08:00
// TODO modificable option? I don't think so, this is very internal stuff
2007-09-19 07:57:02 +08:00
#if 0
2016-11-22 23:47:46 +08:00
// Shows more information in x-scroll 0
width = vp.w;
2007-09-19 07:57:02 +08:00
#else
2016-11-22 23:47:46 +08:00
// Make good use of the complete text-box
2007-09-19 07:57:02 +08:00
if (view) {
gfx::Size maxSize = view->getScrollableSize();
2016-11-22 23:47:46 +08:00
width = MAX(vp.w, maxSize.w);
2007-09-19 07:57:02 +08:00
}
else {
2016-11-22 23:47:46 +08:00
width = vp.w;
2007-09-19 07:57:02 +08:00
}
2016-12-07 08:29:14 +08:00
width -= widget->border().width();
2007-09-19 07:57:02 +08:00
#endif
}
}
// Draw line-by-line
2016-11-22 23:47:46 +08:00
y = y1;
2007-09-19 07:57:02 +08:00
for (beg=end=text; end; ) {
2016-11-22 23:47:46 +08:00
x = x1;
2007-09-19 07:57:02 +08:00
// Without word-wrap
if (!(widget->align() & WORDWRAP)) {
end = std::strchr(beg, '\n');
2007-09-19 07:57:02 +08:00
if (end) {
chr = *end;
*end = 0;
2007-09-19 07:57:02 +08:00
}
}
// With word-wrap
2007-09-19 07:57:02 +08:00
else {
old_end = NULL;
for (beg_end=beg;;) {
end = std::strpbrk(beg_end, " \n");
if (end) {
chr = *end;
*end = 0;
}
// To here we can print
2016-12-07 08:29:14 +08:00
if ((old_end) && (x+font->textLength(beg) > x1+width-scroll.x)) {
if (end)
*end = chr;
end = old_end;
chr = *end;
*end = 0;
break;
}
// We can print one word more
else if (end) {
// Force break
if (chr == '\n')
break;
*end = chr;
beg_end = end+1;
}
// We are in the end of text
else
break;
old_end = end;
2007-09-19 07:57:02 +08:00
}
}
len = font->textLength(beg);
2007-09-19 07:57:02 +08:00
// Render the text
if (g) {
2007-09-19 07:57:02 +08:00
int xout;
if (widget->align() & CENTER)
xout = x + width/2 - len/2;
else if (widget->align() & RIGHT)
xout = x + width - len;
else // Left align
xout = x;
2007-09-19 07:57:02 +08:00
g->drawText(beg, fg, gfx::ColorNone, gfx::Point(xout, y));
2007-09-19 07:57:02 +08:00
}
if (w)
*w = MAX(*w, len);
2007-09-19 07:57:02 +08:00
y += textheight;
if (end) {
*end = chr;
beg = end+1;
}
}
if (h)
*h = (y - y1 + scroll.y);
2007-09-19 07:57:02 +08:00
if (w) *w += widget->border().width();
if (h) *h += widget->border().height();
2007-09-19 07:57:02 +08:00
}
} // namespace ui