2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
// PlatGTK.cxx - implementation of platform facilities on GTK+/Linux
|
|
|
|
// Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
#include <stdlib.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2011-03-22 00:16:49 +00:00
|
|
|
#include <math.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gmodule.h>
|
|
|
|
#include <gdk/gdk.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
|
|
|
#include "Platform.h"
|
|
|
|
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "ScintillaWidget.h"
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "StringCopy.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "XPM.h"
|
2015-06-07 21:19:26 +00:00
|
|
|
#include "UniConversion.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
// Clang 3.0 incorrectly displays sentinel warnings. Fixed by clang 3.1.
|
|
|
|
#pragma GCC diagnostic ignored "-Wsentinel"
|
|
|
|
#endif
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
/* GLIB must be compiled with thread support, otherwise we
|
|
|
|
will bail on trying to use locks, and that could lead to
|
|
|
|
problems for someone. `glib-config --libs gthread` needs
|
|
|
|
to be used to get the glib libraries for linking, otherwise
|
|
|
|
g_thread_init will fail */
|
|
|
|
#define USE_LOCK defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
|
|
|
|
|
|
|
|
#include "Converter.h"
|
|
|
|
|
2010-09-05 22:56:27 +00:00
|
|
|
#if GTK_CHECK_VERSION(2,20,0)
|
|
|
|
#define IS_WIDGET_FOCUSSED(w) (gtk_widget_has_focus(GTK_WIDGET(w)))
|
|
|
|
#else
|
|
|
|
#define IS_WIDGET_FOCUSSED(w) (GTK_WIDGET_HAS_FOCUS(w))
|
|
|
|
#endif
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
static const double kPi = 3.14159265358979323846;
|
|
|
|
|
|
|
|
// The Pango version guard for pango_units_from_double and pango_units_to_double
|
2013-08-28 00:44:27 +00:00
|
|
|
// is more complex than simply implementing these here.
|
|
|
|
|
|
|
|
static int pangoUnitsFromDouble(double d) {
|
|
|
|
return static_cast<int>(d * PANGO_SCALE + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double doubleFromPangoUnits(int pu) {
|
|
|
|
return static_cast<double>(pu) / PANGO_SCALE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cairo_surface_t *CreateSimilarSurface(GdkWindow *window, cairo_content_t content, int width, int height) {
|
2011-03-22 00:16:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(2,22,0)
|
2013-08-28 00:44:27 +00:00
|
|
|
return gdk_window_create_similar_surface(window, content, width, height);
|
|
|
|
#else
|
|
|
|
cairo_surface_t *window_surface, *surface;
|
|
|
|
|
|
|
|
g_return_val_if_fail(GDK_IS_WINDOW(window), NULL);
|
|
|
|
|
|
|
|
window_surface = GDK_DRAWABLE_GET_CLASS(window)->ref_cairo_surface(window);
|
|
|
|
|
|
|
|
surface = cairo_surface_create_similar(window_surface, content, width, height);
|
|
|
|
|
|
|
|
cairo_surface_destroy(window_surface);
|
|
|
|
|
|
|
|
return surface;
|
2011-03-22 00:16:49 +00:00
|
|
|
#endif
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
2011-03-22 00:16:49 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
static GdkWindow *WindowFromWidget(GtkWidget *w) {
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
return gtk_widget_get_window(w);
|
|
|
|
#else
|
|
|
|
return w->window;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Ignore unreferenced local functions in GTK+ headers
|
|
|
|
#pragma warning(disable: 4505)
|
|
|
|
#endif
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
using namespace Scintilla;
|
|
|
|
#endif
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
enum encodingType { singleByte, UTF8, dbcs};
|
|
|
|
|
|
|
|
struct LOGFONT {
|
|
|
|
int size;
|
2013-08-28 00:44:27 +00:00
|
|
|
int weight;
|
2009-04-24 23:35:41 +00:00
|
|
|
bool italic;
|
|
|
|
int characterSet;
|
|
|
|
char faceName[300];
|
|
|
|
};
|
|
|
|
|
|
|
|
#if USE_LOCK
|
|
|
|
static GMutex *fontMutex = NULL;
|
|
|
|
|
|
|
|
static void InitializeGLIBThreads() {
|
2013-08-28 00:44:27 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2,31,0)
|
2009-04-24 23:35:41 +00:00
|
|
|
if (!g_thread_supported()) {
|
|
|
|
g_thread_init(NULL);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void FontMutexAllocate() {
|
|
|
|
#if USE_LOCK
|
|
|
|
if (!fontMutex) {
|
|
|
|
InitializeGLIBThreads();
|
2013-08-28 00:44:27 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,0)
|
|
|
|
fontMutex = g_new(GMutex, 1);
|
|
|
|
g_mutex_init(fontMutex);
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
fontMutex = g_mutex_new();
|
2013-08-28 00:44:27 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FontMutexFree() {
|
|
|
|
#if USE_LOCK
|
|
|
|
if (fontMutex) {
|
2013-08-28 00:44:27 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,31,0)
|
|
|
|
g_mutex_clear(fontMutex);
|
|
|
|
g_free(fontMutex);
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
g_mutex_free(fontMutex);
|
2013-08-28 00:44:27 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
fontMutex = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FontMutexLock() {
|
|
|
|
#if USE_LOCK
|
|
|
|
g_mutex_lock(fontMutex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FontMutexUnlock() {
|
|
|
|
#if USE_LOCK
|
|
|
|
if (fontMutex) {
|
|
|
|
g_mutex_unlock(fontMutex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// Holds a PangoFontDescription*.
|
2009-04-24 23:35:41 +00:00
|
|
|
class FontHandle {
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION width[128];
|
2009-04-24 23:35:41 +00:00
|
|
|
encodingType et;
|
|
|
|
public:
|
|
|
|
int ascent;
|
|
|
|
PangoFontDescription *pfd;
|
|
|
|
int characterSet;
|
2011-07-17 22:30:49 +00:00
|
|
|
FontHandle() : et(singleByte), ascent(0), pfd(0), characterSet(-1) {
|
|
|
|
ResetWidths(et);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
FontHandle(PangoFontDescription *pfd_, int characterSet_) {
|
|
|
|
et = singleByte;
|
|
|
|
ascent = 0;
|
|
|
|
pfd = pfd_;
|
|
|
|
characterSet = characterSet_;
|
|
|
|
ResetWidths(et);
|
|
|
|
}
|
|
|
|
~FontHandle() {
|
|
|
|
if (pfd)
|
|
|
|
pango_font_description_free(pfd);
|
|
|
|
pfd = 0;
|
|
|
|
}
|
|
|
|
void ResetWidths(encodingType et_) {
|
|
|
|
et = et_;
|
|
|
|
for (int i=0; i<=127; i++) {
|
|
|
|
width[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION CharWidth(unsigned char ch, encodingType et_) const {
|
|
|
|
XYPOSITION w = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
FontMutexLock();
|
|
|
|
if ((ch <= 127) && (et == et_)) {
|
|
|
|
w = width[ch];
|
|
|
|
}
|
|
|
|
FontMutexUnlock();
|
|
|
|
return w;
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
void SetCharWidth(unsigned char ch, XYPOSITION w, encodingType et_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (ch <= 127) {
|
|
|
|
FontMutexLock();
|
|
|
|
if (et != et_) {
|
|
|
|
ResetWidths(et_);
|
|
|
|
}
|
|
|
|
width[ch] = w;
|
|
|
|
FontMutexUnlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// X has a 16 bit coordinate space, so stop drawing here to avoid wrapping
|
|
|
|
static const int maxCoordinate = 32000;
|
|
|
|
|
|
|
|
static FontHandle *PFont(Font &f) {
|
|
|
|
return reinterpret_cast<FontHandle *>(f.GetID());
|
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
static GtkWidget *PWidget(WindowID wid) {
|
|
|
|
return reinterpret_cast<GtkWidget *>(wid);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Point Point::FromLong(long lpoint) {
|
|
|
|
return Point(
|
|
|
|
Platform::LowShortFromLong(lpoint),
|
|
|
|
Platform::HighShortFromLong(lpoint));
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
static void SetLogFont(LOGFONT &lf, const char *faceName, int characterSet, float size, int weight, bool italic) {
|
2015-06-07 21:19:26 +00:00
|
|
|
lf = LOGFONT();
|
2009-04-24 23:35:41 +00:00
|
|
|
lf.size = size;
|
2013-08-28 00:44:27 +00:00
|
|
|
lf.weight = weight;
|
2009-04-24 23:35:41 +00:00
|
|
|
lf.italic = italic;
|
|
|
|
lf.characterSet = characterSet;
|
2015-06-07 21:19:26 +00:00
|
|
|
StringCopy(lf.faceName, faceName);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a hash from the parameters for a font to allow easy checking for identity.
|
|
|
|
* If one font is the same as another, its hash will be the same, but if the hash is the
|
|
|
|
* same then they may still be different.
|
|
|
|
*/
|
2013-08-28 00:44:27 +00:00
|
|
|
static int HashFont(const FontParameters &fp) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return
|
2013-08-28 00:44:27 +00:00
|
|
|
static_cast<int>(fp.size+0.5) ^
|
|
|
|
(fp.characterSet << 10) ^
|
|
|
|
((fp.weight / 100) << 12) ^
|
|
|
|
(fp.italic ? 0x20000000 : 0) ^
|
|
|
|
fp.faceName[0];
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class FontCached : Font {
|
|
|
|
FontCached *next;
|
|
|
|
int usage;
|
|
|
|
LOGFONT lf;
|
|
|
|
int hash;
|
2015-06-07 21:19:26 +00:00
|
|
|
explicit FontCached(const FontParameters &fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
~FontCached() {}
|
2013-08-28 00:44:27 +00:00
|
|
|
bool SameAs(const FontParameters &fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
virtual void Release();
|
2013-08-28 00:44:27 +00:00
|
|
|
static FontID CreateNewFont(const FontParameters &fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
static FontCached *first;
|
|
|
|
public:
|
2013-08-28 00:44:27 +00:00
|
|
|
static FontID FindOrCreate(const FontParameters &fp);
|
2009-08-23 02:24:48 +00:00
|
|
|
static void ReleaseId(FontID fid_);
|
2013-08-28 00:44:27 +00:00
|
|
|
static void ReleaseAll();
|
2009-04-24 23:35:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FontCached *FontCached::first = 0;
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
FontCached::FontCached(const FontParameters &fp) :
|
2009-04-24 23:35:41 +00:00
|
|
|
next(0), usage(0), hash(0) {
|
2013-08-28 00:44:27 +00:00
|
|
|
::SetLogFont(lf, fp.faceName, fp.characterSet, fp.size, fp.weight, fp.italic);
|
|
|
|
hash = HashFont(fp);
|
|
|
|
fid = CreateNewFont(fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
usage = 1;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
bool FontCached::SameAs(const FontParameters &fp) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return
|
2013-08-28 00:44:27 +00:00
|
|
|
lf.size == fp.size &&
|
|
|
|
lf.weight == fp.weight &&
|
|
|
|
lf.italic == fp.italic &&
|
|
|
|
lf.characterSet == fp.characterSet &&
|
|
|
|
0 == strcmp(lf.faceName, fp.faceName);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontCached::Release() {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (fid)
|
2009-04-24 23:35:41 +00:00
|
|
|
delete PFont(*this);
|
2009-08-23 02:24:48 +00:00
|
|
|
fid = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
FontID FontCached::FindOrCreate(const FontParameters &fp) {
|
2009-04-24 23:35:41 +00:00
|
|
|
FontID ret = 0;
|
|
|
|
FontMutexLock();
|
2013-08-28 00:44:27 +00:00
|
|
|
int hashFind = HashFont(fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
for (FontCached *cur = first; cur; cur = cur->next) {
|
|
|
|
if ((cur->hash == hashFind) &&
|
2013-08-28 00:44:27 +00:00
|
|
|
cur->SameAs(fp)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
cur->usage++;
|
2009-08-23 02:24:48 +00:00
|
|
|
ret = cur->fid;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
2013-08-28 00:44:27 +00:00
|
|
|
FontCached *fc = new FontCached(fp);
|
|
|
|
fc->next = first;
|
|
|
|
first = fc;
|
|
|
|
ret = fc->fid;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
FontMutexUnlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
void FontCached::ReleaseId(FontID fid_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
FontMutexLock();
|
|
|
|
FontCached **pcur = &first;
|
|
|
|
for (FontCached *cur = first; cur; cur = cur->next) {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (cur->fid == fid_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
cur->usage--;
|
|
|
|
if (cur->usage == 0) {
|
|
|
|
*pcur = cur->next;
|
|
|
|
cur->Release();
|
|
|
|
cur->next = 0;
|
|
|
|
delete cur;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcur = &cur->next;
|
|
|
|
}
|
|
|
|
FontMutexUnlock();
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void FontCached::ReleaseAll() {
|
|
|
|
while (first) {
|
|
|
|
ReleaseId(first->GetID());
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
FontID FontCached::CreateNewFont(const FontParameters &fp) {
|
|
|
|
PangoFontDescription *pfd = pango_font_description_new();
|
|
|
|
if (pfd) {
|
2015-06-07 21:19:26 +00:00
|
|
|
pango_font_description_set_family(pfd,
|
2013-08-28 00:44:27 +00:00
|
|
|
(fp.faceName[0] == '!') ? fp.faceName+1 : fp.faceName);
|
|
|
|
pango_font_description_set_size(pfd, pangoUnitsFromDouble(fp.size));
|
|
|
|
pango_font_description_set_weight(pfd, static_cast<PangoWeight>(fp.weight));
|
|
|
|
pango_font_description_set_style(pfd, fp.italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
|
|
|
|
return new FontHandle(pfd, fp.characterSet);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
return new FontHandle();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
Font::Font() : fid(0) {}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
Font::~Font() {}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void Font::Create(const FontParameters &fp) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Release();
|
2013-08-28 00:44:27 +00:00
|
|
|
fid = FontCached::FindOrCreate(fp);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Font::Release() {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (fid)
|
|
|
|
FontCached::ReleaseId(fid);
|
|
|
|
fid = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
// Required on OS X
|
|
|
|
#ifdef SCI_NAMESPACE
|
2010-07-12 22:19:51 +00:00
|
|
|
namespace Scintilla {
|
2009-06-24 19:09:31 +00:00
|
|
|
#endif
|
2011-07-17 22:30:49 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// SurfaceID is a cairo_t*
|
2010-07-12 22:19:51 +00:00
|
|
|
class SurfaceImpl : public Surface {
|
2009-04-24 23:35:41 +00:00
|
|
|
encodingType et;
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_t *context;
|
|
|
|
cairo_surface_t *psurf;
|
2009-04-24 23:35:41 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
bool inited;
|
|
|
|
bool createdGC;
|
|
|
|
PangoContext *pcontext;
|
|
|
|
PangoLayout *layout;
|
|
|
|
Converter conv;
|
|
|
|
int characterSet;
|
|
|
|
void SetConverter(int characterSet_);
|
|
|
|
public:
|
|
|
|
SurfaceImpl();
|
|
|
|
virtual ~SurfaceImpl();
|
|
|
|
|
|
|
|
void Init(WindowID wid);
|
|
|
|
void Init(SurfaceID sid, WindowID wid);
|
|
|
|
void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
|
|
|
|
|
|
|
|
void Release();
|
|
|
|
bool Initialised();
|
2013-08-28 00:44:27 +00:00
|
|
|
void PenColour(ColourDesired fore);
|
2009-04-24 23:35:41 +00:00
|
|
|
int LogPixelsY();
|
|
|
|
int DeviceHeightFont(int points);
|
|
|
|
void MoveTo(int x_, int y_);
|
|
|
|
void LineTo(int x_, int y_);
|
2013-08-28 00:44:27 +00:00
|
|
|
void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back);
|
|
|
|
void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back);
|
|
|
|
void FillRectangle(PRectangle rc, ColourDesired back);
|
2009-04-24 23:35:41 +00:00
|
|
|
void FillRectangle(PRectangle rc, Surface &surfacePattern);
|
2013-08-28 00:44:27 +00:00
|
|
|
void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back);
|
|
|
|
void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
|
|
|
|
ColourDesired outline, int alphaOutline, int flags);
|
|
|
|
void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage);
|
|
|
|
void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back);
|
2009-04-24 23:35:41 +00:00
|
|
|
void Copy(PRectangle rc, Point from, Surface &surfaceSource);
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
|
|
|
|
void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
|
|
|
|
void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
|
|
|
|
void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
|
|
|
|
void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions);
|
|
|
|
XYPOSITION WidthText(Font &font_, const char *s, int len);
|
|
|
|
XYPOSITION WidthChar(Font &font_, char ch);
|
|
|
|
XYPOSITION Ascent(Font &font_);
|
|
|
|
XYPOSITION Descent(Font &font_);
|
|
|
|
XYPOSITION InternalLeading(Font &font_);
|
|
|
|
XYPOSITION ExternalLeading(Font &font_);
|
|
|
|
XYPOSITION Height(Font &font_);
|
|
|
|
XYPOSITION AverageCharWidth(Font &font_);
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
void SetClip(PRectangle rc);
|
|
|
|
void FlushCachedState();
|
|
|
|
|
|
|
|
void SetUnicodeMode(bool unicodeMode_);
|
|
|
|
void SetDBCSMode(int codePage);
|
|
|
|
};
|
2010-07-12 22:19:51 +00:00
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
const char *CharacterSetID(int characterSet) {
|
|
|
|
switch (characterSet) {
|
|
|
|
case SC_CHARSET_ANSI:
|
|
|
|
return "";
|
|
|
|
case SC_CHARSET_DEFAULT:
|
|
|
|
return "ISO-8859-1";
|
|
|
|
case SC_CHARSET_BALTIC:
|
|
|
|
return "ISO-8859-13";
|
|
|
|
case SC_CHARSET_CHINESEBIG5:
|
|
|
|
return "BIG-5";
|
|
|
|
case SC_CHARSET_EASTEUROPE:
|
|
|
|
return "ISO-8859-2";
|
|
|
|
case SC_CHARSET_GB2312:
|
2010-09-05 22:56:27 +00:00
|
|
|
return "CP936";
|
2009-04-24 23:35:41 +00:00
|
|
|
case SC_CHARSET_GREEK:
|
|
|
|
return "ISO-8859-7";
|
|
|
|
case SC_CHARSET_HANGUL:
|
2010-09-05 22:56:27 +00:00
|
|
|
return "CP949";
|
2009-04-24 23:35:41 +00:00
|
|
|
case SC_CHARSET_MAC:
|
|
|
|
return "MACINTOSH";
|
|
|
|
case SC_CHARSET_OEM:
|
|
|
|
return "ASCII";
|
|
|
|
case SC_CHARSET_RUSSIAN:
|
|
|
|
return "KOI8-R";
|
|
|
|
case SC_CHARSET_CYRILLIC:
|
|
|
|
return "CP1251";
|
|
|
|
case SC_CHARSET_SHIFTJIS:
|
|
|
|
return "SHIFT-JIS";
|
|
|
|
case SC_CHARSET_SYMBOL:
|
|
|
|
return "";
|
|
|
|
case SC_CHARSET_TURKISH:
|
|
|
|
return "ISO-8859-9";
|
|
|
|
case SC_CHARSET_JOHAB:
|
2010-09-05 22:56:27 +00:00
|
|
|
return "CP1361";
|
2009-04-24 23:35:41 +00:00
|
|
|
case SC_CHARSET_HEBREW:
|
|
|
|
return "ISO-8859-8";
|
|
|
|
case SC_CHARSET_ARABIC:
|
|
|
|
return "ISO-8859-6";
|
|
|
|
case SC_CHARSET_VIETNAMESE:
|
|
|
|
return "";
|
|
|
|
case SC_CHARSET_THAI:
|
|
|
|
return "ISO-8859-11";
|
|
|
|
case SC_CHARSET_8859_15:
|
|
|
|
return "ISO-8859-15";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::SetConverter(int characterSet_) {
|
|
|
|
if (characterSet != characterSet_) {
|
|
|
|
characterSet = characterSet_;
|
|
|
|
conv.Open("UTF-8", CharacterSetID(characterSet), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 00:16:49 +00:00
|
|
|
SurfaceImpl::SurfaceImpl() : et(singleByte),
|
|
|
|
context(0),
|
|
|
|
psurf(0),
|
2009-04-24 23:35:41 +00:00
|
|
|
x(0), y(0), inited(false), createdGC(false)
|
2010-07-12 22:19:51 +00:00
|
|
|
, pcontext(0), layout(0), characterSet(-1) {
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceImpl::~SurfaceImpl() {
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::Release() {
|
|
|
|
et = singleByte;
|
|
|
|
if (createdGC) {
|
|
|
|
createdGC = false;
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_destroy(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2011-03-22 00:16:49 +00:00
|
|
|
context = 0;
|
|
|
|
if (psurf)
|
|
|
|
cairo_surface_destroy(psurf);
|
|
|
|
psurf = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (layout)
|
|
|
|
g_object_unref(layout);
|
|
|
|
layout = 0;
|
|
|
|
if (pcontext)
|
|
|
|
g_object_unref(pcontext);
|
|
|
|
pcontext = 0;
|
|
|
|
conv.Close();
|
|
|
|
characterSet = -1;
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
inited = false;
|
|
|
|
createdGC = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SurfaceImpl::Initialised() {
|
2015-06-07 21:19:26 +00:00
|
|
|
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 8, 0)
|
|
|
|
if (inited && context) {
|
|
|
|
if (cairo_status(context) == CAIRO_STATUS_SUCCESS) {
|
|
|
|
// Even when status is success, the target surface may have been
|
|
|
|
// finished whch may cause an assertion to fail crashing the application.
|
|
|
|
// The cairo_surface_has_show_text_glyphs call checks the finished flag
|
|
|
|
// and when set, sets the status to CAIRO_STATUS_SURFACE_FINISHED
|
|
|
|
// which leads to warning messages instead of crashes.
|
|
|
|
// Performing the check in this method as it is called rarely and has no
|
|
|
|
// other side effects.
|
|
|
|
cairo_surface_t *psurfContext = cairo_get_target(context);
|
|
|
|
if (psurfContext) {
|
|
|
|
cairo_surface_has_show_text_glyphs(psurfContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cairo_status(context) == CAIRO_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
return inited;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
void SurfaceImpl::Init(WindowID wid) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Release();
|
|
|
|
PLATFORM_ASSERT(wid);
|
2015-06-07 21:19:26 +00:00
|
|
|
// if we are only created from a window ID, we can't perform drawing
|
|
|
|
psurf = 0;
|
|
|
|
context = 0;
|
|
|
|
createdGC = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
pcontext = gtk_widget_create_pango_context(PWidget(wid));
|
|
|
|
PLATFORM_ASSERT(pcontext);
|
|
|
|
layout = pango_layout_new(pcontext);
|
|
|
|
PLATFORM_ASSERT(layout);
|
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
void SurfaceImpl::Init(SurfaceID sid, WindowID wid) {
|
2009-04-24 23:35:41 +00:00
|
|
|
PLATFORM_ASSERT(sid);
|
|
|
|
Release();
|
|
|
|
PLATFORM_ASSERT(wid);
|
2011-07-17 22:30:49 +00:00
|
|
|
context = cairo_reference(reinterpret_cast<cairo_t *>(sid));
|
2009-04-24 23:35:41 +00:00
|
|
|
pcontext = gtk_widget_create_pango_context(PWidget(wid));
|
2013-08-28 00:44:27 +00:00
|
|
|
// update the Pango context in case sid isn't the widget's surface
|
|
|
|
pango_cairo_update_context(context, pcontext);
|
2009-04-24 23:35:41 +00:00
|
|
|
layout = pango_layout_new(pcontext);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_set_line_width(context, 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
createdGC = true;
|
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_, WindowID wid) {
|
2009-04-24 23:35:41 +00:00
|
|
|
PLATFORM_ASSERT(surface_);
|
|
|
|
Release();
|
|
|
|
SurfaceImpl *surfImpl = static_cast<SurfaceImpl *>(surface_);
|
|
|
|
PLATFORM_ASSERT(wid);
|
2011-03-22 00:16:49 +00:00
|
|
|
context = cairo_reference(surfImpl->context);
|
2011-07-17 22:30:49 +00:00
|
|
|
pcontext = gtk_widget_create_pango_context(PWidget(wid));
|
2013-08-28 00:44:27 +00:00
|
|
|
// update the Pango context in case surface_ isn't the widget's surface
|
|
|
|
pango_cairo_update_context(context, pcontext);
|
2011-03-22 00:16:49 +00:00
|
|
|
PLATFORM_ASSERT(pcontext);
|
2011-07-17 22:30:49 +00:00
|
|
|
layout = pango_layout_new(pcontext);
|
2011-03-22 00:16:49 +00:00
|
|
|
PLATFORM_ASSERT(layout);
|
|
|
|
if (height > 0 && width > 0)
|
2013-08-28 00:44:27 +00:00
|
|
|
psurf = CreateSimilarSurface(
|
|
|
|
WindowFromWidget(PWidget(wid)),
|
2011-03-22 00:16:49 +00:00
|
|
|
CAIRO_CONTENT_COLOR_ALPHA, width, height);
|
|
|
|
cairo_destroy(context);
|
|
|
|
context = cairo_create(psurf);
|
|
|
|
cairo_rectangle(context, 0, 0, width, height);
|
|
|
|
cairo_set_source_rgb(context, 1.0, 0, 0);
|
|
|
|
cairo_fill(context);
|
|
|
|
// This produces sharp drawing more similar to GDK:
|
|
|
|
//cairo_set_antialias(context, CAIRO_ANTIALIAS_NONE);
|
|
|
|
cairo_set_line_width(context, 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
createdGC = true;
|
|
|
|
inited = true;
|
2015-06-07 21:19:26 +00:00
|
|
|
et = surfImpl->et;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::PenColour(ColourDesired fore) {
|
2011-03-22 00:16:49 +00:00
|
|
|
if (context) {
|
|
|
|
ColourDesired cdFore(fore.AsLong());
|
|
|
|
cairo_set_source_rgb(context,
|
2011-07-17 22:30:49 +00:00
|
|
|
cdFore.GetRed() / 255.0,
|
2011-03-22 00:16:49 +00:00
|
|
|
cdFore.GetGreen() / 255.0,
|
2011-07-17 22:30:49 +00:00
|
|
|
cdFore.GetBlue() / 255.0);
|
2011-03-22 00:16:49 +00:00
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SurfaceImpl::LogPixelsY() {
|
|
|
|
return 72;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SurfaceImpl::DeviceHeightFont(int points) {
|
|
|
|
int logPix = LogPixelsY();
|
|
|
|
return (points * logPix + logPix / 2) / 72;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::MoveTo(int x_, int y_) {
|
|
|
|
x = x_;
|
|
|
|
y = y_;
|
|
|
|
}
|
|
|
|
|
2011-03-22 00:16:49 +00:00
|
|
|
static int Delta(int difference) {
|
|
|
|
if (difference < 0)
|
|
|
|
return -1;
|
|
|
|
else if (difference > 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
void SurfaceImpl::LineTo(int x_, int y_) {
|
2011-03-22 00:16:49 +00:00
|
|
|
// cairo_line_to draws the end position, unlike Win32 or GDK with GDK_CAP_NOT_LAST.
|
|
|
|
// For simple cases, move back one pixel from end.
|
|
|
|
if (context) {
|
|
|
|
int xDiff = x_ - x;
|
|
|
|
int xDelta = Delta(xDiff);
|
|
|
|
int yDiff = y_ - y;
|
|
|
|
int yDelta = Delta(yDiff);
|
|
|
|
if ((xDiff == 0) || (yDiff == 0)) {
|
|
|
|
// Horizontal or vertical lines can be more precisely drawn as a filled rectangle
|
|
|
|
int xEnd = x_ - xDelta;
|
|
|
|
int left = Platform::Minimum(x, xEnd);
|
|
|
|
int width = abs(x - xEnd) + 1;
|
|
|
|
int yEnd = y_ - yDelta;
|
|
|
|
int top = Platform::Minimum(y, yEnd);
|
|
|
|
int height = abs(y - yEnd) + 1;
|
|
|
|
cairo_rectangle(context, left, top, width, height);
|
|
|
|
cairo_fill(context);
|
|
|
|
} else if ((abs(xDiff) == abs(yDiff))) {
|
|
|
|
// 45 degree slope
|
|
|
|
cairo_move_to(context, x + 0.5, y + 0.5);
|
|
|
|
cairo_line_to(context, x_ + 0.5 - xDelta, y_ + 0.5 - yDelta);
|
|
|
|
} else {
|
|
|
|
// Line has a different slope so difficult to avoid last pixel
|
|
|
|
cairo_move_to(context, x + 0.5, y + 0.5);
|
|
|
|
cairo_line_to(context, x_ + 0.5, y_ + 0.5);
|
|
|
|
}
|
|
|
|
cairo_stroke(context);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
x = x_;
|
|
|
|
y = y_;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::Polygon(Point *pts, int npts, ColourDesired fore,
|
|
|
|
ColourDesired back) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2011-03-22 00:16:49 +00:00
|
|
|
PenColour(back);
|
|
|
|
cairo_move_to(context, pts[0].x + 0.5, pts[0].y + 0.5);
|
2015-06-07 21:19:26 +00:00
|
|
|
for (int i = 1; i < npts; i++) {
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_line_to(context, pts[i].x + 0.5, pts[i].y + 0.5);
|
|
|
|
}
|
|
|
|
cairo_close_path(context);
|
|
|
|
cairo_fill_preserve(context);
|
|
|
|
PenColour(fore);
|
|
|
|
cairo_stroke(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) {
|
2011-03-22 00:16:49 +00:00
|
|
|
if (context) {
|
|
|
|
cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5,
|
|
|
|
rc.right - rc.left - 1, rc.bottom - rc.top - 1);
|
|
|
|
PenColour(back);
|
|
|
|
cairo_fill_preserve(context);
|
|
|
|
PenColour(fore);
|
|
|
|
cairo_stroke(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back) {
|
2009-04-24 23:35:41 +00:00
|
|
|
PenColour(back);
|
2011-03-22 00:16:49 +00:00
|
|
|
if (context && (rc.left < maxCoordinate)) { // Protect against out of range
|
2013-08-28 00:44:27 +00:00
|
|
|
rc.left = lround(rc.left);
|
|
|
|
rc.right = lround(rc.right);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_rectangle(context, rc.left, rc.top,
|
|
|
|
rc.right - rc.left, rc.bottom - rc.top);
|
|
|
|
cairo_fill(context);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
|
2011-03-22 00:16:49 +00:00
|
|
|
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfacePattern);
|
2015-06-07 21:19:26 +00:00
|
|
|
bool canDraw = surfi.psurf != NULL;
|
2011-03-22 00:16:49 +00:00
|
|
|
if (canDraw) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
// Tile pattern over rectangle
|
|
|
|
// Currently assumes 8x8 pattern
|
|
|
|
int widthPat = 8;
|
|
|
|
int heightPat = 8;
|
|
|
|
for (int xTile = rc.left; xTile < rc.right; xTile += widthPat) {
|
|
|
|
int widthx = (xTile + widthPat > rc.right) ? rc.right - xTile : widthPat;
|
|
|
|
for (int yTile = rc.top; yTile < rc.bottom; yTile += heightPat) {
|
|
|
|
int heighty = (yTile + heightPat > rc.bottom) ? rc.bottom - yTile : heightPat;
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_set_source_surface(context, surfi.psurf, xTile, yTile);
|
|
|
|
cairo_rectangle(context, xTile, yTile, widthx, heighty);
|
|
|
|
cairo_fill(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Something is wrong so try to show anyway
|
|
|
|
// Shows up black because colour not allocated
|
2013-08-28 00:44:27 +00:00
|
|
|
FillRectangle(rc, ColourDesired(0));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (((rc.right - rc.left) > 4) && ((rc.bottom - rc.top) > 4)) {
|
|
|
|
// Approximate a round rect with some cut off corners
|
|
|
|
Point pts[] = {
|
|
|
|
Point(rc.left + 2, rc.top),
|
|
|
|
Point(rc.right - 2, rc.top),
|
|
|
|
Point(rc.right, rc.top + 2),
|
|
|
|
Point(rc.right, rc.bottom - 2),
|
|
|
|
Point(rc.right - 2, rc.bottom),
|
|
|
|
Point(rc.left + 2, rc.bottom),
|
|
|
|
Point(rc.left, rc.bottom - 2),
|
|
|
|
Point(rc.left, rc.top + 2),
|
|
|
|
};
|
2015-06-07 21:19:26 +00:00
|
|
|
Polygon(pts, ELEMENTS(pts), fore, back);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
RectangleDraw(rc, fore, back);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 00:16:49 +00:00
|
|
|
static void PathRoundRectangle(cairo_t *context, double left, double top, double width, double height, int radius) {
|
2015-06-07 21:19:26 +00:00
|
|
|
double degrees = kPi / 180.0;
|
2011-03-22 00:16:49 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 2, 0)
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_new_sub_path(context);
|
2013-08-28 00:44:27 +00:00
|
|
|
#else
|
|
|
|
// First arc is in the top-right corner and starts from a point on the top line
|
|
|
|
cairo_move_to(context, left + width - radius, top);
|
|
|
|
#endif
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_arc(context, left + width - radius, top + radius, radius, -90 * degrees, 0 * degrees);
|
|
|
|
cairo_arc(context, left + width - radius, top + height - radius, radius, 0 * degrees, 90 * degrees);
|
|
|
|
cairo_arc(context, left + radius, top + height - radius, radius, 90 * degrees, 180 * degrees);
|
|
|
|
cairo_arc(context, left + radius, top + radius, radius, 180 * degrees, 270 * degrees);
|
|
|
|
cairo_close_path(context);
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
|
|
|
|
ColourDesired outline, int alphaOutline, int flags) {
|
2011-03-22 00:16:49 +00:00
|
|
|
if (context && rc.Width() > 0) {
|
2011-07-17 22:30:49 +00:00
|
|
|
ColourDesired cdFill(fill.AsLong());
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_set_source_rgba(context,
|
2011-07-17 22:30:49 +00:00
|
|
|
cdFill.GetRed() / 255.0,
|
|
|
|
cdFill.GetGreen() / 255.0,
|
|
|
|
cdFill.GetBlue() / 255.0,
|
2011-03-22 00:16:49 +00:00
|
|
|
alphaFill / 255.0);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (cornerSize > 0)
|
|
|
|
PathRoundRectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0, cornerSize);
|
|
|
|
else
|
|
|
|
cairo_rectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_fill(context);
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
ColourDesired cdOutline(outline.AsLong());
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_set_source_rgba(context,
|
2011-07-17 22:30:49 +00:00
|
|
|
cdOutline.GetRed() / 255.0,
|
|
|
|
cdOutline.GetGreen() / 255.0,
|
|
|
|
cdOutline.GetBlue() / 255.0,
|
2011-03-22 00:16:49 +00:00
|
|
|
alphaOutline / 255.0);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (cornerSize > 0)
|
|
|
|
PathRoundRectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1, cornerSize);
|
|
|
|
else
|
|
|
|
cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_stroke(context);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (rc.Width() > width)
|
|
|
|
rc.left += (rc.Width() - width) / 2;
|
|
|
|
rc.right = rc.left + width;
|
|
|
|
if (rc.Height() > height)
|
|
|
|
rc.top += (rc.Height() - height) / 2;
|
|
|
|
rc.bottom = rc.top + height;
|
|
|
|
|
|
|
|
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
|
|
|
|
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
|
2011-03-22 00:16:49 +00:00
|
|
|
#else
|
2013-08-28 00:44:27 +00:00
|
|
|
int stride = width * 4;
|
|
|
|
#endif
|
|
|
|
int ucs = stride * height;
|
|
|
|
std::vector<unsigned char> image(ucs);
|
2015-06-07 21:19:26 +00:00
|
|
|
for (int iy=0; iy<height; iy++) {
|
|
|
|
for (int ix=0; ix<width; ix++) {
|
|
|
|
unsigned char *pixel = &image[0] + iy*stride + ix * 4;
|
2013-08-28 00:44:27 +00:00
|
|
|
unsigned char alpha = pixelsImage[3];
|
|
|
|
pixel[2] = (*pixelsImage++) * alpha / 255;
|
|
|
|
pixel[1] = (*pixelsImage++) * alpha / 255;
|
|
|
|
pixel[0] = (*pixelsImage++) * alpha / 255;
|
|
|
|
pixel[3] = *pixelsImage++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
cairo_surface_t *psurfImage = cairo_image_surface_create_for_data(&image[0], CAIRO_FORMAT_ARGB32, width, height, stride);
|
|
|
|
cairo_set_source_surface(context, psurfImage, rc.left, rc.top);
|
2013-08-28 00:44:27 +00:00
|
|
|
cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
|
|
|
|
cairo_fill(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
cairo_surface_destroy(psurfImage);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-08-23 02:24:48 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
PenColour(back);
|
2015-06-07 21:19:26 +00:00
|
|
|
cairo_arc(context, (rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2,
|
|
|
|
Platform::Minimum(rc.Width(), rc.Height()) / 2, 0, 2*kPi);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_fill_preserve(context);
|
|
|
|
PenColour(fore);
|
|
|
|
cairo_stroke(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
|
2011-03-22 00:16:49 +00:00
|
|
|
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfaceSource);
|
2015-06-07 21:19:26 +00:00
|
|
|
bool canDraw = surfi.psurf != NULL;
|
2011-03-22 00:16:49 +00:00
|
|
|
if (canDraw) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_set_source_surface(context, surfi.psurf,
|
|
|
|
rc.left - from.x, rc.top - from.y);
|
|
|
|
cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
|
|
|
|
cairo_fill(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string UTF8FromLatin1(const char *s, int len) {
|
|
|
|
std::string utfForm(len*2 + 1, '\0');
|
2009-04-24 23:35:41 +00:00
|
|
|
size_t lenU = 0;
|
2015-06-07 21:19:26 +00:00
|
|
|
for (int i=0; i<len; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
unsigned int uch = static_cast<unsigned char>(s[i]);
|
|
|
|
if (uch < 0x80) {
|
|
|
|
utfForm[lenU++] = uch;
|
|
|
|
} else {
|
|
|
|
utfForm[lenU++] = static_cast<char>(0xC0 | (uch >> 6));
|
|
|
|
utfForm[lenU++] = static_cast<char>(0x80 | (uch & 0x3f));
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
utfForm.resize(lenU);
|
2009-04-24 23:35:41 +00:00
|
|
|
return utfForm;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
static std::string UTF8FromIconv(const Converter &conv, const char *s, int len) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (conv) {
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string utfForm(len*3+1, '\0');
|
2009-04-24 23:35:41 +00:00
|
|
|
char *pin = const_cast<char *>(s);
|
|
|
|
size_t inLeft = len;
|
2013-08-28 00:44:27 +00:00
|
|
|
char *putf = &utfForm[0];
|
|
|
|
char *pout = putf;
|
2009-04-24 23:35:41 +00:00
|
|
|
size_t outLeft = len*3+1;
|
|
|
|
size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
|
|
|
|
if (conversions != ((size_t)(-1))) {
|
|
|
|
*pout = '\0';
|
2013-08-28 00:44:27 +00:00
|
|
|
utfForm.resize(pout - putf);
|
2009-04-24 23:35:41 +00:00
|
|
|
return utfForm;
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
return std::string();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Work out how many bytes are in a character by trying to convert using iconv,
|
|
|
|
// returning the first length that succeeds.
|
|
|
|
static size_t MultiByteLenFromIconv(const Converter &conv, const char *s, size_t len) {
|
|
|
|
for (size_t lenMB=1; (lenMB<4) && (lenMB <= len); lenMB++) {
|
|
|
|
char wcForm[2];
|
|
|
|
char *pin = const_cast<char *>(s);
|
|
|
|
size_t inLeft = lenMB;
|
|
|
|
char *pout = wcForm;
|
|
|
|
size_t outLeft = 2;
|
|
|
|
size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
|
|
|
|
if (conversions != ((size_t)(-1))) {
|
|
|
|
return lenMB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
|
|
|
|
ColourDesired fore) {
|
2009-04-24 23:35:41 +00:00
|
|
|
PenColour(fore);
|
2011-03-22 00:16:49 +00:00
|
|
|
if (context) {
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION xText = rc.left;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (PFont(font_)->pfd) {
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string utfForm;
|
2009-04-24 23:35:41 +00:00
|
|
|
if (et == UTF8) {
|
|
|
|
pango_layout_set_text(layout, s, len);
|
|
|
|
} else {
|
2013-08-28 00:44:27 +00:00
|
|
|
SetConverter(PFont(font_)->characterSet);
|
|
|
|
utfForm = UTF8FromIconv(conv, s, len);
|
|
|
|
if (utfForm.empty()) { // iconv failed so treat as Latin1
|
2009-04-24 23:35:41 +00:00
|
|
|
utfForm = UTF8FromLatin1(s, len);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
pango_layout_set_font_description(layout, PFont(font_)->pfd);
|
2011-03-22 00:16:49 +00:00
|
|
|
pango_cairo_update_layout(context, layout);
|
2010-07-12 22:19:51 +00:00
|
|
|
#ifdef PANGO_VERSION
|
|
|
|
PangoLayoutLine *pll = pango_layout_get_line_readonly(layout,0);
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
PangoLayoutLine *pll = pango_layout_get_line(layout,0);
|
2010-07-12 22:19:51 +00:00
|
|
|
#endif
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_move_to(context, xText, ybase);
|
|
|
|
pango_cairo_show_layout_line(context, pll);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
|
|
|
|
ColourDesired fore, ColourDesired back) {
|
2009-04-24 23:35:41 +00:00
|
|
|
FillRectangle(rc, back);
|
|
|
|
DrawTextBase(rc, font_, ybase, s, len, fore);
|
|
|
|
}
|
|
|
|
|
|
|
|
// On GTK+, exactly same as DrawTextNoClip
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
|
|
|
|
ColourDesired fore, ColourDesired back) {
|
2009-04-24 23:35:41 +00:00
|
|
|
FillRectangle(rc, back);
|
|
|
|
DrawTextBase(rc, font_, ybase, s, len, fore);
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
|
|
|
|
ColourDesired fore) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Avoid drawing spaces in transparent mode
|
2015-06-07 21:19:26 +00:00
|
|
|
for (int i=0; i<len; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (s[i] != ' ') {
|
|
|
|
DrawTextBase(rc, font_, ybase, s, len, fore);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
class ClusterIterator {
|
|
|
|
PangoLayoutIter *iter;
|
|
|
|
PangoRectangle pos;
|
|
|
|
int lenPositions;
|
|
|
|
public:
|
|
|
|
bool finished;
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION positionStart;
|
|
|
|
XYPOSITION position;
|
|
|
|
XYPOSITION distance;
|
2010-07-12 22:19:51 +00:00
|
|
|
int curIndex;
|
|
|
|
ClusterIterator(PangoLayout *layout, int len) : lenPositions(len), finished(false),
|
|
|
|
positionStart(0), position(0), distance(0), curIndex(0) {
|
|
|
|
iter = pango_layout_get_iter(layout);
|
|
|
|
pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
|
|
|
|
}
|
|
|
|
~ClusterIterator() {
|
|
|
|
pango_layout_iter_free(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Next() {
|
|
|
|
positionStart = position;
|
|
|
|
if (pango_layout_iter_next_cluster(iter)) {
|
|
|
|
pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
|
2013-08-28 00:44:27 +00:00
|
|
|
position = doubleFromPangoUnits(pos.x);
|
2010-07-12 22:19:51 +00:00
|
|
|
curIndex = pango_layout_iter_get_index(iter);
|
|
|
|
} else {
|
|
|
|
finished = true;
|
2013-08-28 00:44:27 +00:00
|
|
|
position = doubleFromPangoUnits(pos.x + pos.width);
|
2010-07-12 22:19:51 +00:00
|
|
|
curIndex = lenPositions;
|
|
|
|
}
|
|
|
|
distance = position - positionStart;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (font_.GetID()) {
|
|
|
|
const int lenPositions = len;
|
|
|
|
if (PFont(font_)->pfd) {
|
|
|
|
if (len == 1) {
|
|
|
|
int width = PFont(font_)->CharWidth(*s, et);
|
|
|
|
if (width) {
|
|
|
|
positions[0] = width;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pango_layout_set_font_description(layout, PFont(font_)->pfd);
|
|
|
|
if (et == UTF8) {
|
|
|
|
// Simple and direct as UTF-8 is native Pango encoding
|
|
|
|
int i = 0;
|
2010-07-12 22:19:51 +00:00
|
|
|
pango_layout_set_text(layout, s, len);
|
|
|
|
ClusterIterator iti(layout, lenPositions);
|
|
|
|
while (!iti.finished) {
|
|
|
|
iti.Next();
|
|
|
|
int places = iti.curIndex - i;
|
|
|
|
while (i < iti.curIndex) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Evenly distribute space among bytes of this cluster.
|
|
|
|
// Would be better to find number of characters and then
|
|
|
|
// divide evenly between characters with each byte of a character
|
|
|
|
// being at the same position.
|
2010-07-12 22:19:51 +00:00
|
|
|
positions[i] = iti.position - (iti.curIndex - 1 - i) * iti.distance / places;
|
2009-04-24 23:35:41 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PLATFORM_ASSERT(i == lenPositions);
|
|
|
|
} else {
|
|
|
|
int positionsCalculated = 0;
|
|
|
|
if (et == dbcs) {
|
|
|
|
SetConverter(PFont(font_)->characterSet);
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string utfForm = UTF8FromIconv(conv, s, len);
|
|
|
|
if (!utfForm.empty()) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Convert to UTF-8 so can ask Pango for widths, then
|
|
|
|
// Loop through UTF-8 and DBCS forms, taking account of different
|
|
|
|
// character byte lengths.
|
|
|
|
Converter convMeasure("UCS-2", CharacterSetID(characterSet), false);
|
2013-08-28 00:44:27 +00:00
|
|
|
pango_layout_set_text(layout, utfForm.c_str(), strlen(utfForm.c_str()));
|
2009-04-24 23:35:41 +00:00
|
|
|
int i = 0;
|
2010-07-12 22:19:51 +00:00
|
|
|
int clusterStart = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
ClusterIterator iti(layout, strlen(utfForm.c_str()));
|
2010-07-12 22:19:51 +00:00
|
|
|
while (!iti.finished) {
|
|
|
|
iti.Next();
|
|
|
|
int clusterEnd = iti.curIndex;
|
2013-08-28 00:44:27 +00:00
|
|
|
int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
|
2010-07-12 22:19:51 +00:00
|
|
|
int place = 1;
|
|
|
|
while (clusterStart < clusterEnd) {
|
2009-04-24 23:35:41 +00:00
|
|
|
size_t lenChar = MultiByteLenFromIconv(convMeasure, s+i, len-i);
|
|
|
|
while (lenChar--) {
|
2010-07-12 22:19:51 +00:00
|
|
|
positions[i++] = iti.position - (places - place) * iti.distance / places;
|
2009-04-24 23:35:41 +00:00
|
|
|
positionsCalculated++;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
clusterStart += UTF8CharLength(static_cast<unsigned char>(utfForm.c_str()[clusterStart]));
|
2010-07-12 22:19:51 +00:00
|
|
|
place++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PLATFORM_ASSERT(i == lenPositions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (positionsCalculated < 1 ) {
|
2015-06-07 21:19:26 +00:00
|
|
|
// Either 8-bit or DBCS conversion failed so treat as 8-bit.
|
2009-04-24 23:35:41 +00:00
|
|
|
SetConverter(PFont(font_)->characterSet);
|
2015-06-07 21:19:26 +00:00
|
|
|
const bool rtlCheck = PFont(font_)->characterSet == SC_CHARSET_HEBREW ||
|
|
|
|
PFont(font_)->characterSet == SC_CHARSET_ARABIC;
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string utfForm = UTF8FromIconv(conv, s, len);
|
|
|
|
if (utfForm.empty()) {
|
2009-04-24 23:35:41 +00:00
|
|
|
utfForm = UTF8FromLatin1(s, len);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
|
2009-04-24 23:35:41 +00:00
|
|
|
int i = 0;
|
|
|
|
int clusterStart = 0;
|
2015-06-07 21:19:26 +00:00
|
|
|
// Each 8-bit input character may take 1 or 2 bytes in UTF-8
|
2009-04-24 23:35:41 +00:00
|
|
|
// and groups of up to 3 may be represented as ligatures.
|
2013-08-28 00:44:27 +00:00
|
|
|
ClusterIterator iti(layout, utfForm.length());
|
2010-07-12 22:19:51 +00:00
|
|
|
while (!iti.finished) {
|
|
|
|
iti.Next();
|
|
|
|
int clusterEnd = iti.curIndex;
|
2013-08-28 00:44:27 +00:00
|
|
|
int ligatureLength = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
|
2015-06-07 21:19:26 +00:00
|
|
|
if (rtlCheck && ((clusterEnd <= clusterStart) || (ligatureLength == 0) || (ligatureLength > 3))) {
|
|
|
|
// Something has gone wrong: exit quickly but pretend all the characters are equally spaced:
|
|
|
|
int widthLayout = 0;
|
|
|
|
pango_layout_get_size(layout, &widthLayout, NULL);
|
|
|
|
XYPOSITION widthTotal = doubleFromPangoUnits(widthLayout);
|
|
|
|
for (int bytePos=0; bytePos<lenPositions; bytePos++) {
|
|
|
|
positions[bytePos] = widthTotal / lenPositions * (bytePos + 1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
PLATFORM_ASSERT(ligatureLength > 0 && ligatureLength <= 3);
|
|
|
|
for (int charInLig=0; charInLig<ligatureLength; charInLig++) {
|
2010-07-12 22:19:51 +00:00
|
|
|
positions[i++] = iti.position - (ligatureLength - 1 - charInLig) * iti.distance / ligatureLength;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
clusterStart = clusterEnd;
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
while (i < lenPositions) {
|
|
|
|
// If something failed, fill in rest of the positions
|
|
|
|
positions[i++] = clusterStart;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
PLATFORM_ASSERT(i == lenPositions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len == 1) {
|
|
|
|
PFont(font_)->SetCharWidth(*s, positions[0], et);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No font so return an ascending range of values
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
positions[i] = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (font_.GetID()) {
|
|
|
|
if (PFont(font_)->pfd) {
|
2013-08-28 00:44:27 +00:00
|
|
|
std::string utfForm;
|
2009-04-24 23:35:41 +00:00
|
|
|
pango_layout_set_font_description(layout, PFont(font_)->pfd);
|
|
|
|
PangoRectangle pos;
|
|
|
|
if (et == UTF8) {
|
|
|
|
pango_layout_set_text(layout, s, len);
|
|
|
|
} else {
|
2013-08-28 00:44:27 +00:00
|
|
|
SetConverter(PFont(font_)->characterSet);
|
|
|
|
utfForm = UTF8FromIconv(conv, s, len);
|
|
|
|
if (utfForm.empty()) { // iconv failed so treat as Latin1
|
2009-04-24 23:35:41 +00:00
|
|
|
utfForm = UTF8FromLatin1(s, len);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-07-12 22:19:51 +00:00
|
|
|
#ifdef PANGO_VERSION
|
|
|
|
PangoLayoutLine *pangoLine = pango_layout_get_line_readonly(layout,0);
|
|
|
|
#else
|
|
|
|
PangoLayoutLine *pangoLine = pango_layout_get_line(layout,0);
|
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
pango_layout_line_get_extents(pangoLine, NULL, &pos);
|
2013-08-28 00:44:27 +00:00
|
|
|
return doubleFromPangoUnits(pos.width);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
return 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::WidthChar(Font &font_, char ch) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (font_.GetID()) {
|
|
|
|
if (PFont(font_)->pfd) {
|
|
|
|
return WidthText(font_, &ch, 1);
|
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
return 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// Ascent and descent determined by Pango font metrics.
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::Ascent(Font &font_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (!(font_.GetID()))
|
|
|
|
return 1;
|
|
|
|
FontMutexLock();
|
|
|
|
int ascent = PFont(font_)->ascent;
|
|
|
|
if ((ascent == 0) && (PFont(font_)->pfd)) {
|
|
|
|
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
|
|
|
|
PFont(font_)->pfd, pango_context_get_language(pcontext));
|
|
|
|
PFont(font_)->ascent =
|
2013-08-28 00:44:27 +00:00
|
|
|
doubleFromPangoUnits(pango_font_metrics_get_ascent(metrics));
|
2009-04-24 23:35:41 +00:00
|
|
|
pango_font_metrics_unref(metrics);
|
|
|
|
ascent = PFont(font_)->ascent;
|
|
|
|
}
|
|
|
|
if (ascent == 0) {
|
|
|
|
ascent = 1;
|
|
|
|
}
|
|
|
|
FontMutexUnlock();
|
|
|
|
return ascent;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::Descent(Font &font_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (!(font_.GetID()))
|
|
|
|
return 1;
|
|
|
|
if (PFont(font_)->pfd) {
|
|
|
|
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
|
|
|
|
PFont(font_)->pfd, pango_context_get_language(pcontext));
|
2013-08-28 00:44:27 +00:00
|
|
|
int descent = doubleFromPangoUnits(pango_font_metrics_get_descent(metrics));
|
2009-04-24 23:35:41 +00:00
|
|
|
pango_font_metrics_unref(metrics);
|
|
|
|
return descent;
|
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
return 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::InternalLeading(Font &) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::ExternalLeading(Font &) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::Height(Font &font_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return Ascent(font_) + Descent(font_);
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) {
|
2009-04-24 23:35:41 +00:00
|
|
|
return WidthChar(font_, 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::SetClip(PRectangle rc) {
|
2015-06-07 21:19:26 +00:00
|
|
|
PLATFORM_ASSERT(context);
|
2011-03-22 00:16:49 +00:00
|
|
|
cairo_rectangle(context, rc.left, rc.top, rc.right, rc.bottom);
|
|
|
|
cairo_clip(context);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::FlushCachedState() {}
|
|
|
|
|
|
|
|
void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
|
|
|
|
if (unicodeMode_)
|
|
|
|
et = UTF8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceImpl::SetDBCSMode(int codePage) {
|
|
|
|
if (codePage && (codePage != SC_CP_UTF8))
|
|
|
|
et = dbcs;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
Surface *Surface::Allocate(int) {
|
|
|
|
return new SurfaceImpl();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window() {}
|
|
|
|
|
|
|
|
void Window::Destroy() {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (wid) {
|
|
|
|
ListBox *listbox = dynamic_cast<ListBox*>(this);
|
|
|
|
if (listbox) {
|
|
|
|
gtk_widget_hide(GTK_WIDGET(wid));
|
|
|
|
// clear up window content
|
|
|
|
listbox->Clear();
|
|
|
|
// resize the window to the smallest possible size for it to adapt
|
|
|
|
// to future content
|
|
|
|
gtk_window_resize(GTK_WINDOW(wid), 1, 1);
|
|
|
|
} else {
|
|
|
|
gtk_widget_destroy(GTK_WIDGET(wid));
|
|
|
|
}
|
|
|
|
wid = 0;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::HasFocus() {
|
2010-09-05 22:56:27 +00:00
|
|
|
return IS_WIDGET_FOCUSSED(wid);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRectangle Window::GetPosition() {
|
|
|
|
// Before any size allocated pretend its 1000 wide so not scrolled
|
|
|
|
PRectangle rc(0, 0, 1000, 1000);
|
2009-08-23 02:24:48 +00:00
|
|
|
if (wid) {
|
2011-07-17 22:30:49 +00:00
|
|
|
GtkAllocation allocation;
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
gtk_widget_get_allocation(PWidget(wid), &allocation);
|
|
|
|
#else
|
|
|
|
allocation = PWidget(wid)->allocation;
|
|
|
|
#endif
|
|
|
|
rc.left = allocation.x;
|
|
|
|
rc.top = allocation.y;
|
|
|
|
if (allocation.width > 20) {
|
|
|
|
rc.right = rc.left + allocation.width;
|
|
|
|
rc.bottom = rc.top + allocation.height;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::SetPosition(PRectangle rc) {
|
|
|
|
GtkAllocation alloc;
|
|
|
|
alloc.x = rc.left;
|
|
|
|
alloc.y = rc.top;
|
|
|
|
alloc.width = rc.Width();
|
|
|
|
alloc.height = rc.Height();
|
2009-08-23 02:24:48 +00:00
|
|
|
gtk_widget_size_allocate(PWidget(wid), &alloc);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::SetPositionRelative(PRectangle rc, Window relativeTo) {
|
|
|
|
int ox = 0;
|
|
|
|
int oy = 0;
|
2011-07-17 22:30:49 +00:00
|
|
|
gdk_window_get_origin(WindowFromWidget(PWidget(relativeTo.wid)), &ox, &oy);
|
2009-04-24 23:35:41 +00:00
|
|
|
ox += rc.left;
|
|
|
|
if (ox < 0)
|
|
|
|
ox = 0;
|
|
|
|
oy += rc.top;
|
|
|
|
if (oy < 0)
|
|
|
|
oy = 0;
|
|
|
|
|
|
|
|
/* do some corrections to fit into screen */
|
|
|
|
int sizex = rc.right - rc.left;
|
|
|
|
int sizey = rc.bottom - rc.top;
|
|
|
|
int screenWidth = gdk_screen_width();
|
|
|
|
int screenHeight = gdk_screen_height();
|
|
|
|
if (sizex > screenWidth)
|
|
|
|
ox = 0; /* the best we can do */
|
|
|
|
else if (ox + sizex > screenWidth)
|
|
|
|
ox = screenWidth - sizex;
|
|
|
|
if (oy + sizey > screenHeight)
|
|
|
|
oy = screenHeight - sizey;
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
gtk_window_move(GTK_WINDOW(PWidget(wid)), ox, oy);
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
gtk_window_resize(GTK_WINDOW(wid), sizex, sizey);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRectangle Window::GetClientPosition() {
|
|
|
|
// On GTK+, the client position is the window position
|
|
|
|
return GetPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::Show(bool show) {
|
|
|
|
if (show)
|
2009-08-23 02:24:48 +00:00
|
|
|
gtk_widget_show(PWidget(wid));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::InvalidateAll() {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (wid) {
|
|
|
|
gtk_widget_queue_draw(PWidget(wid));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::InvalidateRectangle(PRectangle rc) {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (wid) {
|
|
|
|
gtk_widget_queue_draw_area(PWidget(wid),
|
2009-04-24 23:35:41 +00:00
|
|
|
rc.left, rc.top,
|
|
|
|
rc.right - rc.left, rc.bottom - rc.top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::SetFont(Font &) {
|
|
|
|
// Can not be done generically but only needed for ListBox
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::SetCursor(Cursor curs) {
|
|
|
|
// We don't set the cursor to same value numerous times under gtk because
|
|
|
|
// it stores the cursor in the window once it's set
|
|
|
|
if (curs == cursorLast)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cursorLast = curs;
|
|
|
|
GdkCursor *gdkCurs;
|
|
|
|
switch (curs) {
|
|
|
|
case cursorText:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_XTERM);
|
|
|
|
break;
|
|
|
|
case cursorArrow:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_LEFT_PTR);
|
|
|
|
break;
|
|
|
|
case cursorUp:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_CENTER_PTR);
|
|
|
|
break;
|
|
|
|
case cursorWait:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_WATCH);
|
|
|
|
break;
|
|
|
|
case cursorHand:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_HAND2);
|
|
|
|
break;
|
|
|
|
case cursorReverseArrow:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_RIGHT_PTR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gdkCurs = gdk_cursor_new(GDK_LEFT_PTR);
|
|
|
|
cursorLast = cursorArrow;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
if (WindowFromWidget(PWidget(wid)))
|
|
|
|
gdk_window_set_cursor(WindowFromWidget(PWidget(wid)), gdkCurs);
|
2013-08-28 00:44:27 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
g_object_unref(gdkCurs);
|
|
|
|
#else
|
2010-09-05 22:56:27 +00:00
|
|
|
gdk_cursor_unref(gdkCurs);
|
2013-08-28 00:44:27 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::SetTitle(const char *s) {
|
2009-08-23 02:24:48 +00:00
|
|
|
gtk_window_set_title(GTK_WINDOW(wid), s);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns rectangle of monitor pt is on, both rect and pt are in Window's
|
|
|
|
gdk window coordinates */
|
|
|
|
PRectangle Window::GetMonitorRect(Point pt) {
|
|
|
|
gint x_offset, y_offset;
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
gdk_window_get_origin(WindowFromWidget(PWidget(wid)), &x_offset, &y_offset);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
GdkScreen* screen;
|
|
|
|
gint monitor_num;
|
|
|
|
GdkRectangle rect;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
screen = gtk_widget_get_screen(PWidget(wid));
|
|
|
|
monitor_num = gdk_screen_get_monitor_at_point(screen, pt.x + x_offset, pt.y + y_offset);
|
|
|
|
gdk_screen_get_monitor_geometry(screen, monitor_num, &rect);
|
|
|
|
rect.x -= x_offset;
|
|
|
|
rect.y -= y_offset;
|
|
|
|
return PRectangle(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
typedef std::map<int, RGBAImage*> ImageMap;
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
struct ListImage {
|
2013-08-28 00:44:27 +00:00
|
|
|
const RGBAImage *rgba_data;
|
2009-04-24 23:35:41 +00:00
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void list_image_free(gpointer, gpointer value, gpointer) {
|
2013-08-28 00:44:27 +00:00
|
|
|
ListImage *list_image = static_cast<ListImage *>(value);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (list_image->pixbuf)
|
2011-07-17 22:30:49 +00:00
|
|
|
g_object_unref(list_image->pixbuf);
|
2009-04-24 23:35:41 +00:00
|
|
|
g_free(list_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
ListBox::ListBox() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ListBox::~ListBox() {
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PIXBUF_COLUMN,
|
|
|
|
TEXT_COLUMN,
|
|
|
|
N_COLUMNS
|
|
|
|
};
|
|
|
|
|
|
|
|
class ListBoxX : public ListBox {
|
2015-06-07 21:19:26 +00:00
|
|
|
WindowID widCached;
|
|
|
|
WindowID frame;
|
2009-04-24 23:35:41 +00:00
|
|
|
WindowID list;
|
|
|
|
WindowID scroller;
|
|
|
|
void *pixhash;
|
2010-07-12 22:19:51 +00:00
|
|
|
GtkCellRenderer* pixbuf_renderer;
|
2013-08-28 00:44:27 +00:00
|
|
|
RGBAImageSet images;
|
2009-04-24 23:35:41 +00:00
|
|
|
int desiredVisibleRows;
|
|
|
|
unsigned int maxItemCharacters;
|
|
|
|
unsigned int aveCharWidth;
|
|
|
|
public:
|
|
|
|
CallBackAction doubleClickAction;
|
|
|
|
void *doubleClickActionData;
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
ListBoxX() : widCached(0), frame(0), list(0), scroller(0), pixhash(NULL), pixbuf_renderer(0),
|
2010-07-12 22:19:51 +00:00
|
|
|
desiredVisibleRows(5), maxItemCharacters(0),
|
|
|
|
aveCharWidth(1), doubleClickAction(NULL), doubleClickActionData(NULL) {
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
virtual ~ListBoxX() {
|
|
|
|
if (pixhash) {
|
|
|
|
g_hash_table_foreach((GHashTable *) pixhash, list_image_free, NULL);
|
|
|
|
g_hash_table_destroy((GHashTable *) pixhash);
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
if (widCached) {
|
|
|
|
gtk_widget_destroy(GTK_WIDGET(widCached));
|
|
|
|
wid = widCached = 0;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
virtual void SetFont(Font &font);
|
2013-08-28 00:44:27 +00:00
|
|
|
virtual void Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_, int technology_);
|
2009-04-24 23:35:41 +00:00
|
|
|
virtual void SetAverageCharWidth(int width);
|
|
|
|
virtual void SetVisibleRows(int rows);
|
|
|
|
virtual int GetVisibleRows() const;
|
2015-06-07 21:19:26 +00:00
|
|
|
int GetRowHeight();
|
2009-04-24 23:35:41 +00:00
|
|
|
virtual PRectangle GetDesiredRect();
|
|
|
|
virtual int CaretFromEdge();
|
|
|
|
virtual void Clear();
|
|
|
|
virtual void Append(char *s, int type = -1);
|
|
|
|
virtual int Length();
|
|
|
|
virtual void Select(int n);
|
|
|
|
virtual int GetSelection();
|
|
|
|
virtual int Find(const char *prefix);
|
|
|
|
virtual void GetValue(int n, char *value, int len);
|
2013-08-28 00:44:27 +00:00
|
|
|
void RegisterRGBA(int type, RGBAImage *image);
|
2009-04-24 23:35:41 +00:00
|
|
|
virtual void RegisterImage(int type, const char *xpm_data);
|
2013-08-28 00:44:27 +00:00
|
|
|
virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage);
|
2009-04-24 23:35:41 +00:00
|
|
|
virtual void ClearRegisteredImages();
|
|
|
|
virtual void SetDoubleClickAction(CallBackAction action, void *data) {
|
|
|
|
doubleClickAction = action;
|
|
|
|
doubleClickActionData = data;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
virtual void SetList(const char *listText, char separator, char typesep);
|
2009-04-24 23:35:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ListBox *ListBox::Allocate() {
|
|
|
|
ListBoxX *lb = new ListBoxX();
|
|
|
|
return lb;
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
// SmallScroller, a GtkScrolledWindow that can shrink very small, as
|
|
|
|
// gtk_widget_set_size_request() cannot shrink widgets on GTK3
|
|
|
|
typedef struct {
|
|
|
|
GtkScrolledWindow parent;
|
|
|
|
/* Workaround ABI issue with Windows GTK2 bundle and GCC > 3.
|
|
|
|
See http://lists.geany.org/pipermail/devel/2015-April/thread.html#9379
|
|
|
|
|
|
|
|
GtkScrolledWindow contains a bitfield, and GCC 3.4 and 4.8 don't agree
|
|
|
|
on the size of the structure (regardless of -mms-bitfields):
|
|
|
|
- GCC 3.4 has sizeof(GtkScrolledWindow)=88
|
|
|
|
- GCC 4.8 has sizeof(GtkScrolledWindow)=84
|
|
|
|
As Windows GTK2 bundle is built with GCC 3, it requires types derived
|
|
|
|
from GtkScrolledWindow to be at least 88 bytes, which means we need to
|
|
|
|
add some fake padding to fill in the extra 4 bytes.
|
|
|
|
There is however no other issue with the layout difference as we never
|
|
|
|
access any GtkScrolledWindow fields ourselves. */
|
|
|
|
int padding;
|
|
|
|
} SmallScroller;
|
|
|
|
typedef GtkScrolledWindowClass SmallScrollerClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(SmallScroller, small_scroller, GTK_TYPE_SCROLLED_WINDOW)
|
|
|
|
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
static void small_scroller_get_preferred_height(GtkWidget *widget, gint *min, gint *nat) {
|
|
|
|
GTK_WIDGET_CLASS(small_scroller_parent_class)->get_preferred_height(widget, min, nat);
|
|
|
|
*min = 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void small_scroller_size_request(GtkWidget *widget, GtkRequisition *req) {
|
|
|
|
GTK_WIDGET_CLASS(small_scroller_parent_class)->size_request(widget, req);
|
|
|
|
req->height = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void small_scroller_class_init(SmallScrollerClass *klass) {
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
GTK_WIDGET_CLASS(klass)->get_preferred_height = small_scroller_get_preferred_height;
|
|
|
|
#else
|
|
|
|
GTK_WIDGET_CLASS(klass)->size_request = small_scroller_size_request;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void small_scroller_init(SmallScroller *){}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
static gboolean ButtonPress(GtkWidget *, GdkEventButton* ev, gpointer p) {
|
2009-08-23 02:24:48 +00:00
|
|
|
try {
|
|
|
|
ListBoxX* lb = reinterpret_cast<ListBoxX*>(p);
|
|
|
|
if (ev->type == GDK_2BUTTON_PRESS && lb->doubleClickAction != NULL) {
|
|
|
|
lb->doubleClickAction(lb->doubleClickActionData);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
// No pointer back to Scintilla to save status
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change the active color to the selected color so the listbox uses the color
|
|
|
|
scheme that it would use if it had the focus. */
|
|
|
|
static void StyleSet(GtkWidget *w, GtkStyle*, void*) {
|
|
|
|
|
|
|
|
g_return_if_fail(w != NULL);
|
|
|
|
|
|
|
|
/* Copy the selected color to active. Note that the modify calls will cause
|
|
|
|
recursive calls to this function after the value is updated and w->style to
|
|
|
|
be set to a new object */
|
2011-07-17 22:30:49 +00:00
|
|
|
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
GtkStyleContext *styleContext = gtk_widget_get_style_context(w);
|
|
|
|
if (styleContext == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GdkRGBA colourForeSelected;
|
|
|
|
gtk_style_context_get_color(styleContext, GTK_STATE_FLAG_SELECTED, &colourForeSelected);
|
|
|
|
GdkRGBA colourForeActive;
|
|
|
|
gtk_style_context_get_color(styleContext, GTK_STATE_FLAG_ACTIVE, &colourForeActive);
|
|
|
|
if (!gdk_rgba_equal(&colourForeSelected, &colourForeActive))
|
|
|
|
gtk_widget_override_color(w, GTK_STATE_FLAG_ACTIVE, &colourForeSelected);
|
|
|
|
|
|
|
|
styleContext = gtk_widget_get_style_context(w);
|
|
|
|
if (styleContext == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GdkRGBA colourBaseSelected;
|
|
|
|
gtk_style_context_get_background_color(styleContext, GTK_STATE_FLAG_SELECTED, &colourBaseSelected);
|
|
|
|
GdkRGBA colourBaseActive;
|
|
|
|
gtk_style_context_get_background_color(styleContext, GTK_STATE_FLAG_ACTIVE, &colourBaseActive);
|
|
|
|
if (!gdk_rgba_equal(&colourBaseSelected, &colourBaseActive))
|
|
|
|
gtk_widget_override_background_color(w, GTK_STATE_FLAG_ACTIVE, &colourBaseSelected);
|
|
|
|
#else
|
|
|
|
GtkStyle *style = gtk_widget_get_style(w);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (style == NULL)
|
|
|
|
return;
|
|
|
|
if (!gdk_color_equal(&style->base[GTK_STATE_SELECTED], &style->base[GTK_STATE_ACTIVE]))
|
|
|
|
gtk_widget_modify_base(w, GTK_STATE_ACTIVE, &style->base[GTK_STATE_SELECTED]);
|
|
|
|
style = gtk_widget_get_style(w);
|
|
|
|
if (style == NULL)
|
|
|
|
return;
|
|
|
|
if (!gdk_color_equal(&style->text[GTK_STATE_SELECTED], &style->text[GTK_STATE_ACTIVE]))
|
|
|
|
gtk_widget_modify_text(w, GTK_STATE_ACTIVE, &style->text[GTK_STATE_SELECTED]);
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void ListBoxX::Create(Window &, int, Point, int, bool, int) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (widCached != 0) {
|
|
|
|
wid = widCached;
|
|
|
|
return;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
wid = widCached = gtk_window_new(GTK_WINDOW_POPUP);
|
|
|
|
|
|
|
|
frame = gtk_frame_new(NULL);
|
|
|
|
gtk_widget_show(PWidget(frame));
|
|
|
|
gtk_container_add(GTK_CONTAINER(GetID()), PWidget(frame));
|
2009-04-24 23:35:41 +00:00
|
|
|
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
|
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(frame), 0);
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
scroller = g_object_new(small_scroller_get_type(), NULL);
|
2009-04-24 23:35:41 +00:00
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(scroller), 0);
|
|
|
|
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
|
|
|
|
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_container_add(GTK_CONTAINER(frame), PWidget(scroller));
|
|
|
|
gtk_widget_show(PWidget(scroller));
|
|
|
|
|
|
|
|
/* Tree and its model */
|
|
|
|
GtkListStore *store =
|
|
|
|
gtk_list_store_new(N_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING);
|
|
|
|
|
|
|
|
list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
|
|
|
|
g_signal_connect(G_OBJECT(list), "style-set", G_CALLBACK(StyleSet), NULL);
|
|
|
|
|
|
|
|
GtkTreeSelection *selection =
|
|
|
|
gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
|
|
|
|
gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
|
|
|
|
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
|
|
|
|
gtk_tree_view_set_reorderable(GTK_TREE_VIEW(list), FALSE);
|
|
|
|
|
|
|
|
/* Columns */
|
|
|
|
GtkTreeViewColumn *column = gtk_tree_view_column_new();
|
|
|
|
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
|
|
|
|
gtk_tree_view_column_set_title(column, "Autocomplete");
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
pixbuf_renderer = gtk_cell_renderer_pixbuf_new();
|
|
|
|
gtk_cell_renderer_set_fixed_size(pixbuf_renderer, 0, -1);
|
|
|
|
gtk_tree_view_column_pack_start(column, pixbuf_renderer, FALSE);
|
|
|
|
gtk_tree_view_column_add_attribute(column, pixbuf_renderer,
|
2009-04-24 23:35:41 +00:00
|
|
|
"pixbuf", PIXBUF_COLUMN);
|
2010-07-12 22:19:51 +00:00
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
|
2009-04-24 23:35:41 +00:00
|
|
|
gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), 1);
|
|
|
|
gtk_tree_view_column_pack_start(column, renderer, TRUE);
|
|
|
|
gtk_tree_view_column_add_attribute(column, renderer,
|
|
|
|
"text", TEXT_COLUMN);
|
|
|
|
|
|
|
|
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
|
|
|
|
if (g_object_class_find_property(G_OBJECT_GET_CLASS(list), "fixed-height-mode"))
|
|
|
|
g_object_set(G_OBJECT(list), "fixed-height-mode", TRUE, NULL);
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
GtkWidget *widget = PWidget(list); // No code inside the G_OBJECT macro
|
|
|
|
gtk_container_add(GTK_CONTAINER(PWidget(scroller)), widget);
|
|
|
|
gtk_widget_show(widget);
|
|
|
|
g_signal_connect(G_OBJECT(widget), "button_press_event",
|
2009-04-24 23:35:41 +00:00
|
|
|
G_CALLBACK(ButtonPress), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::SetFont(Font &scint_font) {
|
|
|
|
// Only do for Pango font as there have been crashes for GDK fonts
|
|
|
|
if (Created() && PFont(scint_font)->pfd) {
|
|
|
|
// Current font is Pango font
|
2011-07-17 22:30:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
gtk_widget_override_font(PWidget(list), PFont(scint_font)->pfd);
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
gtk_widget_modify_font(PWidget(list), PFont(scint_font)->pfd);
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::SetAverageCharWidth(int width) {
|
|
|
|
aveCharWidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::SetVisibleRows(int rows) {
|
|
|
|
desiredVisibleRows = rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ListBoxX::GetVisibleRows() const {
|
|
|
|
return desiredVisibleRows;
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
int ListBoxX::GetRowHeight()
|
|
|
|
{
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
// This version sometimes reports erroneous results on GTK2, but the GTK2
|
|
|
|
// version is inaccurate for GTK 3.14.
|
|
|
|
GdkRectangle rect;
|
|
|
|
GtkTreePath *path = gtk_tree_path_new_first();
|
|
|
|
gtk_tree_view_get_background_area(GTK_TREE_VIEW(list), path, NULL, &rect);
|
|
|
|
return rect.height;
|
|
|
|
#else
|
|
|
|
int row_height=0;
|
|
|
|
int vertical_separator=0;
|
|
|
|
int expander_size=0;
|
|
|
|
GtkTreeViewColumn *column = gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0);
|
|
|
|
gtk_tree_view_column_cell_get_size(column, NULL, NULL, NULL, NULL, &row_height);
|
|
|
|
gtk_widget_style_get(PWidget(list),
|
|
|
|
"vertical-separator", &vertical_separator,
|
|
|
|
"expander-size", &expander_size, NULL);
|
|
|
|
row_height += vertical_separator;
|
|
|
|
row_height = Platform::Maximum(row_height, expander_size);
|
|
|
|
return row_height;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
PRectangle ListBoxX::GetDesiredRect() {
|
|
|
|
// Before any size allocated pretend its 100 wide so not scrolled
|
|
|
|
PRectangle rc(0, 0, 100, 100);
|
2009-08-23 02:24:48 +00:00
|
|
|
if (wid) {
|
2009-04-24 23:35:41 +00:00
|
|
|
int rows = Length();
|
|
|
|
if ((rows == 0) || (rows > desiredVisibleRows))
|
|
|
|
rows = desiredVisibleRows;
|
|
|
|
|
|
|
|
GtkRequisition req;
|
2013-08-28 00:44:27 +00:00
|
|
|
// This, apparently unnecessary call, ensures gtk_tree_view_column_cell_get_size
|
2015-06-07 21:19:26 +00:00
|
|
|
// returns reasonable values.
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
gtk_widget_get_preferred_size(GTK_WIDGET(frame), NULL, &req);
|
|
|
|
#else
|
|
|
|
gtk_widget_size_request(GTK_WIDGET(frame), &req);
|
2013-08-28 00:44:27 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
int height;
|
|
|
|
|
|
|
|
// First calculate height of the clist for our desired visible
|
|
|
|
// row count otherwise it tries to expand to the total # of rows
|
|
|
|
// Get cell height
|
2015-06-07 21:19:26 +00:00
|
|
|
int row_height = GetRowHeight();
|
2011-07-17 22:30:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
2015-06-07 21:19:26 +00:00
|
|
|
GtkStyleContext *styleContextFrame = gtk_widget_get_style_context(PWidget(frame));
|
|
|
|
GtkBorder padding, border;
|
|
|
|
gtk_style_context_get_padding(styleContextFrame, GTK_STATE_FLAG_NORMAL, &padding);
|
|
|
|
gtk_style_context_get_border(styleContextFrame, GTK_STATE_FLAG_NORMAL, &border);
|
2011-07-17 22:30:49 +00:00
|
|
|
height = (rows * row_height
|
|
|
|
+ padding.top + padding.bottom
|
2015-06-07 21:19:26 +00:00
|
|
|
+ border.top + border.bottom
|
|
|
|
+ 2 * gtk_container_get_border_width(GTK_CONTAINER(PWidget(list))));
|
2011-07-17 22:30:49 +00:00
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
height = (rows * row_height
|
2015-06-07 21:19:26 +00:00
|
|
|
+ 2 * (PWidget(frame)->style->ythickness
|
|
|
|
+ GTK_CONTAINER(PWidget(list))->border_width));
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2015-06-07 21:19:26 +00:00
|
|
|
rc.bottom = height;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
int width = maxItemCharacters;
|
|
|
|
if (width < 12)
|
|
|
|
width = 12;
|
|
|
|
rc.right = width * (aveCharWidth + aveCharWidth / 3);
|
2015-06-07 21:19:26 +00:00
|
|
|
// Add horizontal padding and borders
|
|
|
|
int horizontal_separator=0;
|
|
|
|
gtk_widget_style_get(PWidget(list),
|
|
|
|
"horizontal-separator", &horizontal_separator, NULL);
|
|
|
|
rc.right += horizontal_separator;
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
rc.right += (padding.left + padding.right
|
|
|
|
+ border.left + border.right
|
|
|
|
+ 2 * gtk_container_get_border_width(GTK_CONTAINER(PWidget(list))));
|
|
|
|
#else
|
|
|
|
rc.right += 2 * (PWidget(frame)->style->xthickness
|
|
|
|
+ GTK_CONTAINER(PWidget(list))->border_width);
|
|
|
|
#endif
|
|
|
|
if (Length() > rows) {
|
|
|
|
// Add the width of the scrollbar
|
|
|
|
GtkWidget *vscrollbar =
|
|
|
|
gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(scroller));
|
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
gtk_widget_get_preferred_size(vscrollbar, NULL, &req);
|
|
|
|
#else
|
|
|
|
gtk_widget_size_request(vscrollbar, &req);
|
|
|
|
#endif
|
|
|
|
rc.right += req.width;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ListBoxX::CaretFromEdge() {
|
2009-08-23 02:24:48 +00:00
|
|
|
gint renderer_width, renderer_height;
|
|
|
|
gtk_cell_renderer_get_fixed_size(pixbuf_renderer, &renderer_width,
|
|
|
|
&renderer_height);
|
|
|
|
return 4 + renderer_width;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::Clear() {
|
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
|
|
|
|
gtk_list_store_clear(GTK_LIST_STORE(model));
|
|
|
|
maxItemCharacters = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_pixmap(ListImage *list_image) {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (list_image->rgba_data) {
|
|
|
|
// Drop any existing pixmap/bitmap as data may have changed
|
|
|
|
if (list_image->pixbuf)
|
|
|
|
g_object_unref(list_image->pixbuf);
|
|
|
|
list_image->pixbuf =
|
|
|
|
gdk_pixbuf_new_from_data(list_image->rgba_data->Pixels(),
|
|
|
|
GDK_COLORSPACE_RGB,
|
|
|
|
TRUE,
|
|
|
|
8,
|
|
|
|
list_image->rgba_data->GetWidth(),
|
|
|
|
list_image->rgba_data->GetHeight(),
|
|
|
|
list_image->rgba_data->GetWidth() * 4,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SPACING 5
|
|
|
|
|
|
|
|
void ListBoxX::Append(char *s, int type) {
|
|
|
|
ListImage *list_image = NULL;
|
|
|
|
if ((type >= 0) && pixhash) {
|
2013-08-28 00:44:27 +00:00
|
|
|
list_image = static_cast<ListImage *>(g_hash_table_lookup((GHashTable *) pixhash
|
|
|
|
, (gconstpointer) GINT_TO_POINTER(type)));
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkListStore *store =
|
|
|
|
GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
|
|
|
|
gtk_list_store_append(GTK_LIST_STORE(store), &iter);
|
|
|
|
if (list_image) {
|
|
|
|
if (NULL == list_image->pixbuf)
|
|
|
|
init_pixmap(list_image);
|
|
|
|
if (list_image->pixbuf) {
|
|
|
|
gtk_list_store_set(GTK_LIST_STORE(store), &iter,
|
|
|
|
PIXBUF_COLUMN, list_image->pixbuf,
|
|
|
|
TEXT_COLUMN, s, -1);
|
2009-08-23 02:24:48 +00:00
|
|
|
|
|
|
|
gint pixbuf_width = gdk_pixbuf_get_width(list_image->pixbuf);
|
|
|
|
gint renderer_height, renderer_width;
|
2010-07-12 22:19:51 +00:00
|
|
|
gtk_cell_renderer_get_fixed_size(pixbuf_renderer,
|
2009-08-23 02:24:48 +00:00
|
|
|
&renderer_width, &renderer_height);
|
|
|
|
if (pixbuf_width > renderer_width)
|
|
|
|
gtk_cell_renderer_set_fixed_size(pixbuf_renderer,
|
|
|
|
pixbuf_width, -1);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
gtk_list_store_set(GTK_LIST_STORE(store), &iter,
|
|
|
|
TEXT_COLUMN, s, -1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gtk_list_store_set(GTK_LIST_STORE(store), &iter,
|
|
|
|
TEXT_COLUMN, s, -1);
|
|
|
|
}
|
|
|
|
size_t len = strlen(s);
|
|
|
|
if (maxItemCharacters < len)
|
|
|
|
maxItemCharacters = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ListBoxX::Length() {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (wid)
|
2009-04-24 23:35:41 +00:00
|
|
|
return gtk_tree_model_iter_n_children(gtk_tree_view_get_model
|
|
|
|
(GTK_TREE_VIEW(list)), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::Select(int n) {
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
|
|
|
|
GtkTreeSelection *selection =
|
|
|
|
gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
gtk_tree_selection_unselect_all(selection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid = gtk_tree_model_iter_nth_child(model, &iter, NULL, n) != FALSE;
|
|
|
|
if (valid) {
|
|
|
|
gtk_tree_selection_select_iter(selection, &iter);
|
|
|
|
|
|
|
|
// Move the scrollbar to show the selection.
|
|
|
|
int total = Length();
|
2011-07-17 22:30:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
GtkAdjustment *adj =
|
|
|
|
gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(list));
|
|
|
|
gfloat value = ((gfloat)n / total) * (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_lower(adj))
|
|
|
|
+ gtk_adjustment_get_lower(adj) - gtk_adjustment_get_page_size(adj) / 2;
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
GtkAdjustment *adj =
|
|
|
|
gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(list));
|
|
|
|
gfloat value = ((gfloat)n / total) * (adj->upper - adj->lower)
|
|
|
|
+ adj->lower - adj->page_size / 2;
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
// Get cell height
|
2015-06-07 21:19:26 +00:00
|
|
|
int row_height = GetRowHeight();
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
int rows = Length();
|
|
|
|
if ((rows == 0) || (rows > desiredVisibleRows))
|
|
|
|
rows = desiredVisibleRows;
|
|
|
|
if (rows & 0x1) {
|
|
|
|
// Odd rows to display -- We are now in the middle.
|
|
|
|
// Align it so that we don't chop off rows.
|
|
|
|
value += (gfloat)row_height / 2.0;
|
|
|
|
}
|
|
|
|
// Clamp it.
|
|
|
|
value = (value < 0)? 0 : value;
|
2011-07-17 22:30:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
value = (value > (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)))?
|
|
|
|
(gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)) : value;
|
|
|
|
#else
|
2009-04-24 23:35:41 +00:00
|
|
|
value = (value > (adj->upper - adj->page_size))?
|
|
|
|
(adj->upper - adj->page_size) : value;
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
// Set it.
|
|
|
|
gtk_adjustment_set_value(adj, value);
|
|
|
|
} else {
|
|
|
|
gtk_tree_selection_unselect_all(selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ListBoxX::GetSelection() {
|
2015-06-07 21:19:26 +00:00
|
|
|
int index = -1;
|
2009-04-24 23:35:41 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkTreeSelection *selection;
|
|
|
|
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
|
|
|
|
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
|
|
|
|
GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
|
|
|
|
int *indices = gtk_tree_path_get_indices(path);
|
|
|
|
// Don't free indices.
|
|
|
|
if (indices)
|
2015-06-07 21:19:26 +00:00
|
|
|
index = indices[0];
|
|
|
|
gtk_tree_path_free(path);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
return index;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ListBoxX::Find(const char *prefix) {
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model =
|
|
|
|
gtk_tree_view_get_model(GTK_TREE_VIEW(list));
|
|
|
|
bool valid = gtk_tree_model_get_iter_first(model, &iter) != FALSE;
|
|
|
|
int i = 0;
|
|
|
|
while(valid) {
|
|
|
|
gchar *s;
|
|
|
|
gtk_tree_model_get(model, &iter, TEXT_COLUMN, &s, -1);
|
|
|
|
if (s && (0 == strncmp(prefix, s, strlen(prefix)))) {
|
2010-08-21 23:59:56 +00:00
|
|
|
g_free(s);
|
2009-04-24 23:35:41 +00:00
|
|
|
return i;
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
g_free(s);
|
2009-04-24 23:35:41 +00:00
|
|
|
valid = gtk_tree_model_iter_next(model, &iter) != FALSE;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::GetValue(int n, char *value, int len) {
|
|
|
|
char *text = NULL;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
|
|
|
|
bool valid = gtk_tree_model_iter_nth_child(model, &iter, NULL, n) != FALSE;
|
|
|
|
if (valid) {
|
|
|
|
gtk_tree_model_get(model, &iter, TEXT_COLUMN, &text, -1);
|
|
|
|
}
|
|
|
|
if (text && len > 0) {
|
2015-06-07 21:19:26 +00:00
|
|
|
g_strlcpy(value, text, len);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
value[0] = '\0';
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
g_free(text);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// g_return_if_fail causes unnecessary compiler warning in release compile.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable: 4127)
|
|
|
|
#endif
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void ListBoxX::RegisterRGBA(int type, RGBAImage *image) {
|
|
|
|
images.Add(type, image);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
if (!pixhash) {
|
|
|
|
pixhash = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
ListImage *list_image = static_cast<ListImage *>(g_hash_table_lookup((GHashTable *) pixhash,
|
|
|
|
(gconstpointer) GINT_TO_POINTER(type)));
|
2009-04-24 23:35:41 +00:00
|
|
|
if (list_image) {
|
|
|
|
// Drop icon already registered
|
|
|
|
if (list_image->pixbuf)
|
2010-09-05 22:56:27 +00:00
|
|
|
g_object_unref(list_image->pixbuf);
|
2009-04-24 23:35:41 +00:00
|
|
|
list_image->pixbuf = NULL;
|
2013-08-28 00:44:27 +00:00
|
|
|
list_image->rgba_data = image;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
|
|
|
list_image = g_new0(ListImage, 1);
|
2013-08-28 00:44:27 +00:00
|
|
|
list_image->rgba_data = image;
|
2009-04-24 23:35:41 +00:00
|
|
|
g_hash_table_insert((GHashTable *) pixhash, GINT_TO_POINTER(type),
|
|
|
|
(gpointer) list_image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
void ListBoxX::RegisterImage(int type, const char *xpm_data) {
|
|
|
|
g_return_if_fail(xpm_data);
|
|
|
|
XPM xpmImage(xpm_data);
|
|
|
|
RegisterRGBA(type, new RGBAImage(xpmImage));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListBoxX::RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) {
|
|
|
|
RegisterRGBA(type, new RGBAImage(width, height, 1.0, pixelsImage));
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
void ListBoxX::ClearRegisteredImages() {
|
2013-08-28 00:44:27 +00:00
|
|
|
images.Clear();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
void ListBoxX::SetList(const char *listText, char separator, char typesep) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Clear();
|
2009-04-25 23:38:15 +00:00
|
|
|
int count = strlen(listText) + 1;
|
2013-08-28 00:44:27 +00:00
|
|
|
std::vector<char> words(listText, listText+count);
|
|
|
|
char *startword = &words[0];
|
|
|
|
char *numword = NULL;
|
|
|
|
int i = 0;
|
|
|
|
for (; words[i]; i++) {
|
|
|
|
if (words[i] == separator) {
|
|
|
|
words[i] = '\0';
|
2009-04-24 23:35:41 +00:00
|
|
|
if (numword)
|
|
|
|
*numword = '\0';
|
|
|
|
Append(startword, numword?atoi(numword + 1):-1);
|
2013-08-28 00:44:27 +00:00
|
|
|
startword = &words[0] + i + 1;
|
|
|
|
numword = NULL;
|
|
|
|
} else if (words[i] == typesep) {
|
|
|
|
numword = &words[0] + i;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
if (startword) {
|
|
|
|
if (numword)
|
|
|
|
*numword = '\0';
|
|
|
|
Append(startword, numword?atoi(numword + 1):-1);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
Menu::Menu() : mid(0) {}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
void Menu::CreatePopUp() {
|
|
|
|
Destroy();
|
2010-09-05 22:56:27 +00:00
|
|
|
mid = gtk_menu_new();
|
|
|
|
#if GLIB_CHECK_VERSION(2,10,0)
|
|
|
|
g_object_ref_sink(G_OBJECT(mid));
|
|
|
|
#else
|
|
|
|
g_object_ref(G_OBJECT(mid));
|
|
|
|
gtk_object_sink(GTK_OBJECT(G_OBJECT(mid)));
|
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Menu::Destroy() {
|
2009-08-23 02:24:48 +00:00
|
|
|
if (mid)
|
|
|
|
g_object_unref(G_OBJECT(mid));
|
|
|
|
mid = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2010-09-05 22:56:27 +00:00
|
|
|
static void MenuPositionFunc(GtkMenu *, gint *x, gint *y, gboolean *, gpointer userData) {
|
|
|
|
sptr_t intFromPointer = reinterpret_cast<sptr_t>(userData);
|
|
|
|
*x = intFromPointer & 0xffff;
|
|
|
|
*y = intFromPointer >> 16;
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
void Menu::Show(Point pt, Window &) {
|
|
|
|
int screenHeight = gdk_screen_height();
|
|
|
|
int screenWidth = gdk_screen_width();
|
2010-09-05 22:56:27 +00:00
|
|
|
GtkMenu *widget = reinterpret_cast<GtkMenu *>(mid);
|
|
|
|
gtk_widget_show_all(GTK_WIDGET(widget));
|
2009-04-24 23:35:41 +00:00
|
|
|
GtkRequisition requisition;
|
2011-07-17 22:30:49 +00:00
|
|
|
#if GTK_CHECK_VERSION(3,0,0)
|
|
|
|
gtk_widget_get_preferred_size(GTK_WIDGET(widget), NULL, &requisition);
|
|
|
|
#else
|
2010-09-05 22:56:27 +00:00
|
|
|
gtk_widget_size_request(GTK_WIDGET(widget), &requisition);
|
2011-07-17 22:30:49 +00:00
|
|
|
#endif
|
2009-04-24 23:35:41 +00:00
|
|
|
if ((pt.x + requisition.width) > screenWidth) {
|
|
|
|
pt.x = screenWidth - requisition.width;
|
|
|
|
}
|
|
|
|
if ((pt.y + requisition.height) > screenHeight) {
|
|
|
|
pt.y = screenHeight - requisition.height;
|
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
gtk_menu_popup(widget, NULL, NULL, MenuPositionFunc,
|
2013-08-28 00:44:27 +00:00
|
|
|
reinterpret_cast<void *>((static_cast<int>(pt.y) << 16) | static_cast<int>(pt.x)), 0,
|
2009-04-24 23:35:41 +00:00
|
|
|
gtk_get_current_event_time());
|
|
|
|
}
|
|
|
|
|
|
|
|
ElapsedTime::ElapsedTime() {
|
|
|
|
GTimeVal curTime;
|
|
|
|
g_get_current_time(&curTime);
|
|
|
|
bigBit = curTime.tv_sec;
|
|
|
|
littleBit = curTime.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DynamicLibraryImpl : public DynamicLibrary {
|
|
|
|
protected:
|
|
|
|
GModule* m;
|
|
|
|
public:
|
2015-06-07 21:19:26 +00:00
|
|
|
explicit DynamicLibraryImpl(const char *modulePath) {
|
2009-04-24 23:35:41 +00:00
|
|
|
m = g_module_open(modulePath, G_MODULE_BIND_LAZY);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DynamicLibraryImpl() {
|
|
|
|
if (m != NULL)
|
|
|
|
g_module_close(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use g_module_symbol to get a pointer to the relevant function.
|
|
|
|
virtual Function FindFunction(const char *name) {
|
|
|
|
if (m != NULL) {
|
|
|
|
gpointer fn_address = NULL;
|
|
|
|
gboolean status = g_module_symbol(m, name, &fn_address);
|
|
|
|
if (status)
|
|
|
|
return static_cast<Function>(fn_address);
|
|
|
|
else
|
|
|
|
return NULL;
|
2015-06-07 21:19:26 +00:00
|
|
|
} else {
|
2009-04-24 23:35:41 +00:00
|
|
|
return NULL;
|
2015-06-07 21:19:26 +00:00
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsValid() {
|
|
|
|
return m != NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DynamicLibrary *DynamicLibrary::Load(const char *modulePath) {
|
|
|
|
return static_cast<DynamicLibrary *>( new DynamicLibraryImpl(modulePath) );
|
|
|
|
}
|
|
|
|
|
|
|
|
double ElapsedTime::Duration(bool reset) {
|
|
|
|
GTimeVal curTime;
|
|
|
|
g_get_current_time(&curTime);
|
|
|
|
long endBigBit = curTime.tv_sec;
|
|
|
|
long endLittleBit = curTime.tv_usec;
|
|
|
|
double result = 1000000.0 * (endBigBit - bigBit);
|
|
|
|
result += endLittleBit - littleBit;
|
|
|
|
result /= 1000000.0;
|
|
|
|
if (reset) {
|
|
|
|
bigBit = endBigBit;
|
|
|
|
littleBit = endLittleBit;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColourDesired Platform::Chrome() {
|
|
|
|
return ColourDesired(0xe0, 0xe0, 0xe0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ColourDesired Platform::ChromeHighlight() {
|
|
|
|
return ColourDesired(0xff, 0xff, 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Platform::DefaultFont() {
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return "Lucida Console";
|
|
|
|
#else
|
|
|
|
return "!Sans";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int Platform::DefaultFontSize() {
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return 10;
|
|
|
|
#else
|
|
|
|
return 12;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Platform::DoubleClickTime() {
|
|
|
|
return 500; // Half a second
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Platform::MouseButtonBounce() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform::DebugDisplay(const char *s) {
|
|
|
|
fprintf(stderr, "%s", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Platform::IsKeyDown(int) {
|
|
|
|
// TODO: discover state of keys in GTK+/X
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
long Platform::SendScintilla(
|
|
|
|
WindowID w, unsigned int msg, unsigned long wParam, long lParam) {
|
|
|
|
return scintilla_send_message(SCINTILLA(w), msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
long Platform::SendScintillaPointer(
|
|
|
|
WindowID w, unsigned int msg, unsigned long wParam, void *lParam) {
|
|
|
|
return scintilla_send_message(SCINTILLA(w), msg, wParam,
|
|
|
|
reinterpret_cast<sptr_t>(lParam));
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
bool Platform::IsDBCSLeadByte(int codePage, char ch) {
|
|
|
|
// Byte ranges found in Wikipedia articles with relevant search strings in each case
|
|
|
|
unsigned char uch = static_cast<unsigned char>(ch);
|
|
|
|
switch (codePage) {
|
|
|
|
case 932:
|
|
|
|
// Shift_jis
|
|
|
|
return ((uch >= 0x81) && (uch <= 0x9F)) ||
|
2013-08-28 00:44:27 +00:00
|
|
|
((uch >= 0xE0) && (uch <= 0xFC));
|
|
|
|
// Lead bytes F0 to FC may be a Microsoft addition.
|
2010-07-12 22:19:51 +00:00
|
|
|
case 936:
|
|
|
|
// GBK
|
|
|
|
return (uch >= 0x81) && (uch <= 0xFE);
|
|
|
|
case 950:
|
|
|
|
// Big5
|
|
|
|
return (uch >= 0x81) && (uch <= 0xFE);
|
|
|
|
// Korean EUC-KR may be code page 949.
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:19:51 +00:00
|
|
|
int Platform::DBCSCharLength(int codePage, const char *s) {
|
|
|
|
if (codePage == 932 || codePage == 936 || codePage == 950) {
|
|
|
|
return IsDBCSLeadByte(codePage, s[0]) ? 2 : 1;
|
|
|
|
} else {
|
|
|
|
int bytes = mblen(s, MB_CUR_MAX);
|
|
|
|
if (bytes >= 1)
|
|
|
|
return bytes;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Platform::DBCSCharMaxLength() {
|
|
|
|
return MB_CUR_MAX;
|
|
|
|
//return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are utility functions not really tied to a platform
|
|
|
|
|
|
|
|
int Platform::Minimum(int a, int b) {
|
|
|
|
if (a < b)
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Platform::Maximum(int a, int b) {
|
|
|
|
if (a > b)
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#define TRACE
|
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
void Platform::DebugPrintf(const char *format, ...) {
|
|
|
|
char buffer[2000];
|
|
|
|
va_list pArguments;
|
|
|
|
va_start(pArguments, format);
|
|
|
|
vsprintf(buffer, format, pArguments);
|
|
|
|
va_end(pArguments);
|
|
|
|
Platform::DebugDisplay(buffer);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Platform::DebugPrintf(const char *, ...) {}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Not supported for GTK+
|
|
|
|
static bool assertionPopUps = true;
|
|
|
|
|
|
|
|
bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
|
|
|
|
bool ret = assertionPopUps;
|
|
|
|
assertionPopUps = assertionPopUps_;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform::Assert(const char *c, const char *file, int line) {
|
|
|
|
char buffer[2000];
|
2015-06-07 21:19:26 +00:00
|
|
|
g_snprintf(buffer, sizeof(buffer), "Assertion [%s] failed at %s %d\r\n", c, file, line);
|
2009-04-24 23:35:41 +00:00
|
|
|
Platform::DebugDisplay(buffer);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Platform::Clamp(int val, int minVal, int maxVal) {
|
|
|
|
if (val > maxVal)
|
|
|
|
val = maxVal;
|
|
|
|
if (val < minVal)
|
|
|
|
val = minVal;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_Initialise() {
|
|
|
|
FontMutexAllocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_Finalise() {
|
2013-08-28 00:44:27 +00:00
|
|
|
FontCached::ReleaseAll();
|
2009-04-24 23:35:41 +00:00
|
|
|
FontMutexFree();
|
|
|
|
}
|