merge frontend::Painter into GuiPainter

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21747 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2007-11-23 21:39:51 +00:00
parent 7bf3b204fa
commit adcb084ea2
9 changed files with 182 additions and 222 deletions

View File

@ -19,7 +19,6 @@ liblyxfrontends_la_SOURCES = \
KeyModifier.h \
KeySymbol.h \
LyXView.h \
Painter.cpp \
Painter.h \
Clipboard.h \
Selection.h \

View File

@ -1,146 +0,0 @@
/**
* \file Painter.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author unknown
* \author John Levon
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "frontends/Painter.h"
#include "frontends/FontMetrics.h"
#include "FontInfo.h"
namespace lyx {
namespace frontend {
static int max(int a, int b) { return a > b ? a : b; }
void Painter::button(int x, int y, int w, int h, bool mouseHover)
{
if (mouseHover)
fillRectangle(x, y, w, h, Color_buttonhoverbg);
else
fillRectangle(x, y, w, h, Color_buttonbg);
buttonFrame(x, y, w, h);
}
void Painter::buttonFrame(int x, int y, int w, int h)
{
line(x, y, x, y + h - 1, Color_buttonframe);
line(x - 1 + w, y, x - 1 + w, y + h - 1, Color_buttonframe);
line(x, y - 1, x - 1 + w, y - 1, Color_buttonframe);
line(x, y + h - 1, x - 1 + w, y + h - 1, Color_buttonframe);
}
void Painter::rectText(int x, int y,
docstring const & str,
FontInfo const & font,
ColorCode back,
ColorCode frame)
{
int width;
int ascent;
int descent;
FontMetrics const & fm = theFontMetrics(font);
fm.rectText(str, width, ascent, descent);
if (back != Color_none)
fillRectangle(x + 1, y - ascent + 1, width - 1,
ascent + descent - 1, back);
if (frame != Color_none)
rectangle(x, y - ascent, width, ascent + descent, frame);
text(x + 3, y, str, font);
}
void Painter::buttonText(int x, int y, docstring const & str,
FontInfo const & font, bool mouseHover)
{
int width;
int ascent;
int descent;
FontMetrics const & fm = theFontMetrics(font);
fm.buttonText(str, width, ascent, descent);
button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
text(x + 4, y - 1, str, font);
}
int Painter::preeditText(int x, int y, char_type c,
FontInfo const & font, preedit_style style)
{
FontInfo temp_font = font;
FontMetrics const & fm = theFontMetrics(font);
int ascent = fm.maxAscent();
int descent = fm.maxDescent();
int height = ascent + descent;
int width = fm.width(c);
switch (style) {
case preedit_default:
// default unselecting mode.
fillRectangle(x, y - height + 1, width, height, Color_background);
dashedUnderline(font, x, y - descent + 1, width);
break;
case preedit_selecting:
// We are in selecting mode: white text on black background.
fillRectangle(x, y - height + 1, width, height, Color_black);
temp_font.setColor(Color_white);
break;
case preedit_cursor:
// The character comes with a cursor.
fillRectangle(x, y - height + 1, width, height, Color_background);
underline(font, x, y - descent + 1, width);
break;
}
text(x, y - descent + 1, c, temp_font);
return width;
}
void Painter::underline(FontInfo const & f, int x, int y, int width)
{
FontMetrics const & fm = theFontMetrics(f);
int const below = max(fm.maxDescent() / 2, 2);
int const height = max((fm.maxDescent() / 4) - 1, 1);
if (height < 2)
line(x, y + below, x + width, y + below, f.color());
else
fillRectangle(x, y + below, width, below + height, f.color());
}
void Painter::dashedUnderline(FontInfo const & f, int x, int y, int width)
{
FontMetrics const & fm = theFontMetrics(f);
int const below = max(fm.maxDescent() / 2, 2);
int height = max((fm.maxDescent() / 4) - 1, 1);
if (height >= 2)
height += below;
for (int n = 0; n < height; ++n)
line(x, y + below + n, x + width, y + below + n, f.color(), line_onoffdash);
}
} // namespace frontend
} // namespace lyx

View File

@ -54,7 +54,7 @@ namespace frontend {
*/
class Painter {
public:
Painter(): drawing_enabled_(true) {}
Painter() : drawing_enabled_(true) {}
/// possible line widths
enum line_width {
line_thin, //< thin line
@ -78,12 +78,8 @@ public:
virtual ~Painter() {}
/// draw a line from point to point
virtual void line(
int x1, int y1,
int x2, int y2,
ColorCode,
line_style = line_solid,
line_width = line_thin) = 0;
virtual void line(int x1, int y1, int x2, int y2, ColorCode,
line_style = line_solid, line_width = line_thin) = 0;
/**
* lines - draw a set of lines
@ -91,55 +87,35 @@ public:
* @param yp array of points' y co-ords
* @param np size of the points array
*/
virtual void lines(
int const * xp,
int const * yp,
int np,
ColorCode,
line_style = line_solid,
line_width = line_thin) = 0;
virtual void lines(int const * xp, int const * yp, int np, ColorCode,
line_style = line_solid, line_width = line_thin) = 0;
/// draw a rectangle
virtual void rectangle(
int x, int y,
int w, int h,
ColorCode,
line_style = line_solid,
line_width = line_thin) = 0;
virtual void rectangle(int x, int y, int w, int h, ColorCode,
line_style = line_solid, line_width = line_thin) = 0;
/// draw a filled rectangle
virtual void fillRectangle(
int x, int y,
int w, int h,
ColorCode) = 0;
virtual void fillRectangle(int x, int y, int w, int h, ColorCode) = 0;
/// draw an arc
virtual void arc(
int x, int y,
unsigned int w, unsigned int h,
int a1, int a2,
ColorCode) = 0;
virtual void arc(int x, int y, unsigned int w, unsigned int h,
int a1, int a2, ColorCode) = 0;
/// draw a pixel
virtual void point(
int x, int y,
ColorCode) = 0;
virtual void point(int x, int y, ColorCode) = 0;
/// draw a filled rectangle with the shape of a 3D button
virtual void button(int x, int y,
int w, int h, bool mouseHover);
virtual void button(int x, int y, int w, int h, bool mouseHover) = 0;
/// draw an image from the image cache
virtual void image(int x, int y,
int w, int h,
virtual void image(int x, int y, int w, int h,
graphics::Image const & image) = 0;
/// draw a string at position x, y (y is the baseline)
/**
* \return the width of the drawn text.
*/
virtual int text(int x, int y,
docstring const & str, FontInfo const & f) = 0;
virtual int text(int x, int y, docstring const & str, FontInfo const & f) = 0;
void setDrawingEnabled(bool drawing_enabled = true)
{ drawing_enabled_ = drawing_enabled; }
@ -159,19 +135,16 @@ public:
* the given color. If frame is specified, a thin frame is drawn
* around the text with the given color.
*/
void rectText(int x, int baseline,
docstring const & str,
FontInfo const & font,
ColorCode back,
ColorCode frame);
virtual void rectText(int x, int baseline, docstring const & str,
FontInfo const & font, ColorCode back, ColorCode frame) = 0;
/// draw a string and enclose it inside a button frame
void buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, bool mouseHover);
virtual void buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, bool mouseHover) = 0;
/// draw a character of a preedit string for cjk support.
int preeditText(int x, int y,
char_type c, FontInfo const & f, preedit_style style);
virtual int preeditText(int x, int y,
char_type c, FontInfo const & f, preedit_style style) = 0;
/// start monochrome painting mode, i.e. map every color into [min,max]
virtual void enterMonochromeMode(ColorCode const & min,
@ -179,18 +152,6 @@ public:
/// leave monochrome painting mode
virtual void leaveMonochromeMode() = 0;
protected:
/// check the font, and if set, draw an underline
void underline(FontInfo const & f,
int x, int y, int width);
/// check the font, and if set, draw an dashed underline
void dashedUnderline(FontInfo const & f,
int x, int y, int width);
/// draw a bevelled button border
void buttonFrame(int x, int y, int w, int h);
private:
///
bool drawing_enabled_;

View File

@ -489,11 +489,12 @@ void GuiListings::updateContents()
string dialect;
bool in_gui = false;
if (prefixIs(arg, "[") && contains(arg, "]")) {
string::size_type end_dialect = arg.find("]");
size_t end_dialect = arg.find("]");
dialect = arg.substr(1, end_dialect - 1);
language = arg.substr(end_dialect + 1);
} else
} else {
language = arg;
}
int n = findToken(languages, language);
if (n >= 0) {
languageCO->setCurrentIndex(n);

View File

@ -416,5 +416,124 @@ int GuiPainter::text(int x, int y, docstring const & s,
}
static int max(int a, int b) { return a > b ? a : b; }
void GuiPainter::button(int x, int y, int w, int h, bool mouseHover)
{
if (mouseHover)
fillRectangle(x, y, w, h, Color_buttonhoverbg);
else
fillRectangle(x, y, w, h, Color_buttonbg);
buttonFrame(x, y, w, h);
}
void GuiPainter::buttonFrame(int x, int y, int w, int h)
{
line(x, y, x, y + h - 1, Color_buttonframe);
line(x - 1 + w, y, x - 1 + w, y + h - 1, Color_buttonframe);
line(x, y - 1, x - 1 + w, y - 1, Color_buttonframe);
line(x, y + h - 1, x - 1 + w, y + h - 1, Color_buttonframe);
}
void GuiPainter::rectText(int x, int y, docstring const & str,
FontInfo const & font, ColorCode back, ColorCode frame)
{
int width;
int ascent;
int descent;
FontMetrics const & fm = theFontMetrics(font);
fm.rectText(str, width, ascent, descent);
if (back != Color_none)
fillRectangle(x + 1, y - ascent + 1, width - 1,
ascent + descent - 1, back);
if (frame != Color_none)
rectangle(x, y - ascent, width, ascent + descent, frame);
text(x + 3, y, str, font);
}
void GuiPainter::buttonText(int x, int y, docstring const & str,
FontInfo const & font, bool mouseHover)
{
int width;
int ascent;
int descent;
FontMetrics const & fm = theFontMetrics(font);
fm.buttonText(str, width, ascent, descent);
button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
text(x + 4, y - 1, str, font);
}
int GuiPainter::preeditText(int x, int y, char_type c,
FontInfo const & font, preedit_style style)
{
FontInfo temp_font = font;
FontMetrics const & fm = theFontMetrics(font);
int ascent = fm.maxAscent();
int descent = fm.maxDescent();
int height = ascent + descent;
int width = fm.width(c);
switch (style) {
case preedit_default:
// default unselecting mode.
fillRectangle(x, y - height + 1, width, height, Color_background);
dashedUnderline(font, x, y - descent + 1, width);
break;
case preedit_selecting:
// We are in selecting mode: white text on black background.
fillRectangle(x, y - height + 1, width, height, Color_black);
temp_font.setColor(Color_white);
break;
case preedit_cursor:
// The character comes with a cursor.
fillRectangle(x, y - height + 1, width, height, Color_background);
underline(font, x, y - descent + 1, width);
break;
}
text(x, y - descent + 1, c, temp_font);
return width;
}
void GuiPainter::underline(FontInfo const & f, int x, int y, int width)
{
FontMetrics const & fm = theFontMetrics(f);
int const below = max(fm.maxDescent() / 2, 2);
int const height = max((fm.maxDescent() / 4) - 1, 1);
if (height < 2)
line(x, y + below, x + width, y + below, f.color());
else
fillRectangle(x, y + below, width, below + height, f.color());
}
void GuiPainter::dashedUnderline(FontInfo const & f, int x, int y, int width)
{
FontMetrics const & fm = theFontMetrics(f);
int const below = max(fm.maxDescent() / 2, 2);
int height = max((fm.maxDescent() / 4) - 1, 1);
if (height >= 2)
height += below;
for (int n = 0; n < height; ++n)
line(x, y + below + n, x + width, y + below + n, f.color(), line_onoffdash);
}
} // namespace frontend
} // namespace lyx

View File

@ -78,13 +78,10 @@ public:
ColorCode);
/// draw a pixel
virtual void point(
int x, int y,
ColorCode);
virtual void point(int x, int y, ColorCode);
/// draw an image from the image cache
virtual void image(int x, int y,
int w, int h,
virtual void image(int x, int y, int w, int h,
lyx::graphics::Image const & image);
/// draw a string at position x, y (y is the baseline)
@ -94,13 +91,44 @@ public:
/// draw a char at position x, y (y is the baseline)
virtual int text(int x, int y, char_type c, FontInfo const & f);
/// draw a string and enclose it inside a button frame
virtual void buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, bool mouseHover);
/// start monochrome painting mode, i.e. map every color into [min,max]
virtual void enterMonochromeMode(ColorCode const & min,
ColorCode const & max);
/// leave monochrome painting mode
virtual void leaveMonochromeMode();
/**
* Draw a string and enclose it inside a rectangle. If
* back color is specified, the background is cleared with
* the given color. If frame is specified, a thin frame is drawn
* around the text with the given color.
*/
virtual void rectText(int x, int baseline, docstring const & str,
FontInfo const & font, ColorCode back, ColorCode frame);
/// draw a filled rectangle with the shape of a 3D button
virtual void button(int x, int y, int w, int h, bool mouseHover);
/// draw a character of a preedit string for cjk support.
virtual int preeditText(int x, int y,
char_type c, FontInfo const & f, preedit_style style);
private:
/// check the font, and if set, draw an underline
void underline(FontInfo const & f,
int x, int y, int width);
/// check the font, and if set, draw an dashed underline
void dashedUnderline(FontInfo const & f,
int x, int y, int width);
/// draw a bevelled button border
void buttonFrame(int x, int y, int w, int h);
/// draw small caps text
/**
\return width of the drawn text.
@ -110,8 +138,7 @@ private:
/// set pen parameters
void setQPainterPen(QColor const & col,
line_style ls = line_solid,
line_width lw = line_thin);
line_style ls = line_solid, line_width lw = line_thin);
QColor current_color_;
Painter::line_style current_ls_;

View File

@ -119,7 +119,7 @@ docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
from_ascii("&"), from_ascii("&&"));
if (!shortcut.empty()) {
docstring::size_type pos = label.find(shortcut);
size_t pos = label.find(shortcut);
if (pos != docstring::npos)
label.insert(pos, 1, char_type('&'));
}

View File

@ -110,7 +110,7 @@ static size_t findPos_helper(std::vector<A> const & vec, A const & val)
static std::pair<string, string> parseFontName(string const & name)
{
string::size_type const idx = name.find('[');
size_t const idx = name.find('[');
if (idx == string::npos || idx == 0)
return make_pair(name, string());
return make_pair(name.substr(0, idx - 1),
@ -147,7 +147,7 @@ static void setComboxFont(QComboBox * cb, string const & family,
pair<string, string> tmpfam = parseFontName(family);
// We count in reverse in order to prefer the Xft foundry
for (int i = cb->count() - 1; i >= 0; --i) {
for (int i = cb->count(); --i >= 0; ) {
pair<string, string> tmp = parseFontName(fromqstr(cb->itemText(i)));
if (compare_ascii_no_case(tmp.first, tmpfam.first) == 0) {
cb->setCurrentIndex(i);

View File

@ -35,10 +35,9 @@ static docstring const getLabel(docstring const & ucs4str)
string sc = split(str, label, '|');
if (sc.length() < 2)
return from_utf8(label);
string::size_type pos = label.find(sc[1]);
if (pos == string::npos)
return from_utf8(label);
label.insert(pos, 1, '&');
size_t pos = label.find(sc[1]);
if (pos != string::npos)
label.insert(pos, 1, '&');
return from_utf8(label);
}