aboutsummaryrefslogtreecommitdiffhomepage
path: root/gtk/PlatGTK.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'gtk/PlatGTK.cxx')
-rw-r--r--gtk/PlatGTK.cxx149
1 files changed, 77 insertions, 72 deletions
diff --git a/gtk/PlatGTK.cxx b/gtk/PlatGTK.cxx
index df47dd71d..91dd0c3ed 100644
--- a/gtk/PlatGTK.cxx
+++ b/gtk/PlatGTK.cxx
@@ -27,6 +27,7 @@
#include "Scintilla.h"
#include "ScintillaWidget.h"
#include "StringCopy.h"
+#include "IntegerRectangle.h"
#include "XPM.h"
#include "UniConversion.h"
@@ -50,8 +51,8 @@ int pangoUnitsFromDouble(double d) noexcept {
return static_cast<int>(d * PANGO_SCALE + 0.5);
}
-double doubleFromPangoUnits(int pu) noexcept {
- return static_cast<double>(pu) / PANGO_SCALE;
+float floatFromPangoUnits(int pu) noexcept {
+ return static_cast<float>(pu) / PANGO_SCALE;
}
cairo_surface_t *CreateSimilarSurface(GdkWindow *window, cairo_content_t content, int width, int height) noexcept {
@@ -366,7 +367,7 @@ void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_, WindowID
void SurfaceImpl::PenColour(ColourDesired fore) {
if (context) {
- ColourDesired cdFore(fore.AsInteger());
+ const ColourDesired cdFore(fore.AsInteger());
cairo_set_source_rgb(context,
cdFore.GetRed() / 255.0,
cdFore.GetGreen() / 255.0,
@@ -379,7 +380,7 @@ int SurfaceImpl::LogPixelsY() {
}
int SurfaceImpl::DeviceHeightFont(int points) {
- int logPix = LogPixelsY();
+ const int logPix = LogPixelsY();
return (points * logPix + logPix / 2) / 72;
}
@@ -401,18 +402,18 @@ void SurfaceImpl::LineTo(int x_, int y_) {
// 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);
+ const int xDiff = x_ - x;
+ const int xDelta = Delta(xDiff);
+ const int yDiff = y_ - y;
+ const 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 = std::min(x, xEnd);
- int width = std::abs(x - xEnd) + 1;
- int yEnd = y_ - yDelta;
- int top = std::min(y, yEnd);
- int height = std::abs(y - yEnd) + 1;
+ const int xEnd = x_ - xDelta;
+ const int left = std::min(x, xEnd);
+ const int width = std::abs(x - xEnd) + 1;
+ const int yEnd = y_ - yDelta;
+ const int top = std::min(y, yEnd);
+ const int height = std::abs(y - yEnd) + 1;
cairo_rectangle(context, left, top, width, height);
cairo_fill(context);
} else if ((std::abs(xDiff) == std::abs(yDiff))) {
@@ -468,17 +469,18 @@ void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back) {
void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfacePattern);
- bool canDraw = surfi.psurf != nullptr;
+ const bool canDraw = surfi.psurf != nullptr;
if (canDraw) {
PLATFORM_ASSERT(context);
// 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;
+ const int widthPat = 8;
+ const int heightPat = 8;
+ const IntegerRectangle irc(rc);
+ for (int xTile = irc.left; xTile < irc.right; xTile += widthPat) {
+ const int widthx = (xTile + widthPat > irc.right) ? irc.right - xTile : widthPat;
+ for (int yTile = irc.top; yTile < irc.bottom; yTile += heightPat) {
+ const int heighty = (yTile + heightPat > irc.bottom) ? irc.bottom - yTile : heightPat;
cairo_set_source_surface(context, surfi.psurf, xTile, yTile);
cairo_rectangle(context, xTile, yTile, widthx, heighty);
cairo_fill(context);
@@ -511,7 +513,7 @@ void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesi
}
static void PathRoundRectangle(cairo_t *context, double left, double top, double width, double height, int radius) noexcept {
- double degrees = kPi / 180.0;
+ const double degrees = kPi / 180.0;
cairo_new_sub_path(context);
cairo_arc(context, left + width - radius, top + radius, radius, -90 * degrees, 0 * degrees);
@@ -524,7 +526,7 @@ static void PathRoundRectangle(cairo_t *context, double left, double top, double
void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int /*flags*/) {
if (context && rc.Width() > 0) {
- ColourDesired cdFill(fill.AsInteger());
+ const ColourDesired cdFill(fill.AsInteger());
cairo_set_source_rgba(context,
cdFill.GetRed() / 255.0,
cdFill.GetGreen() / 255.0,
@@ -536,7 +538,7 @@ void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fi
cairo_rectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0);
cairo_fill(context);
- ColourDesired cdOutline(outline.AsInteger());
+ const ColourDesired cdOutline(outline.AsInteger());
cairo_set_source_rgba(context,
cdOutline.GetRed() / 255.0,
cdOutline.GetGreen() / 255.0,
@@ -586,12 +588,12 @@ void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsi
rc.bottom = rc.top + height;
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
- int ucs = stride * height;
+ const int ucs = stride * height;
std::vector<unsigned char> image(ucs);
for (int iy=0; iy<height; iy++) {
for (int ix=0; ix<width; ix++) {
unsigned char *pixel = &image[0] + iy*stride + ix * 4;
- unsigned char alpha = pixelsImage[3];
+ const unsigned char alpha = pixelsImage[3];
pixel[2] = (*pixelsImage++) * alpha / 255;
pixel[1] = (*pixelsImage++) * alpha / 255;
pixel[0] = (*pixelsImage++) * alpha / 255;
@@ -619,7 +621,7 @@ void SurfaceImpl::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)
void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfaceSource);
- bool canDraw = surfi.psurf != nullptr;
+ const bool canDraw = surfi.psurf != nullptr;
if (canDraw) {
PLATFORM_ASSERT(context);
cairo_set_source_surface(context, surfi.psurf,
@@ -653,7 +655,7 @@ static std::string UTF8FromIconv(const Converter &conv, const char *s, int len)
char *putf = &utfForm[0];
char *pout = putf;
gsize outLeft = len*3+1;
- gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
+ const gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
if (conversions != sizeFailure) {
*pout = '\0';
utfForm.resize(pout - putf);
@@ -672,7 +674,7 @@ static size_t MultiByteLenFromIconv(const Converter &conv, const char *s, size_t
gsize inLeft = lenMB;
char *pout = wcForm;
gsize outLeft = 2;
- gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
+ const gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
if (conversions != sizeFailure) {
return lenMB;
}
@@ -684,7 +686,7 @@ void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, con
ColourDesired fore) {
PenColour(fore);
if (context) {
- XYPOSITION xText = rc.left;
+ const XYPOSITION xText = rc.left;
if (PFont(font_)->pfd) {
std::string utfForm;
if (et == UTF8) {
@@ -753,11 +755,11 @@ public:
positionStart = position;
if (pango_layout_iter_next_cluster(iter)) {
pango_layout_iter_get_cluster_extents(iter, nullptr, &pos);
- position = doubleFromPangoUnits(pos.x);
+ position = floatFromPangoUnits(pos.x);
curIndex = pango_layout_iter_get_index(iter);
} else {
finished = true;
- position = doubleFromPangoUnits(pos.x + pos.width);
+ position = floatFromPangoUnits(pos.x + pos.width);
curIndex = lenPositions;
}
distance = position - positionStart;
@@ -776,7 +778,7 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION
ClusterIterator iti(layout, lenPositions);
while (!iti.finished) {
iti.Next();
- int places = iti.curIndex - i;
+ const int places = iti.curIndex - i;
while (i < iti.curIndex) {
// Evenly distribute space among bytes of this cluster.
// Would be better to find number of characters and then
@@ -803,8 +805,8 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION
ClusterIterator iti(layout, strlen(utfForm.c_str()));
while (!iti.finished) {
iti.Next();
- int clusterEnd = iti.curIndex;
- int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
+ const int clusterEnd = iti.curIndex;
+ const int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
int place = 1;
while (clusterStart < clusterEnd) {
size_t lenChar = MultiByteLenFromIconv(convMeasure, s+i, len-i);
@@ -836,13 +838,13 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION
ClusterIterator iti(layout, utfForm.length());
while (!iti.finished) {
iti.Next();
- int clusterEnd = iti.curIndex;
- int ligatureLength = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
+ const int clusterEnd = iti.curIndex;
+ const int ligatureLength = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
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, nullptr);
- XYPOSITION widthTotal = doubleFromPangoUnits(widthLayout);
+ const XYPOSITION widthTotal = floatFromPangoUnits(widthLayout);
for (int bytePos=0; bytePos<lenPositions; bytePos++) {
positions[bytePos] = widthTotal / lenPositions * (bytePos + 1);
}
@@ -888,7 +890,7 @@ XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) {
}
PangoLayoutLine *pangoLine = pango_layout_get_line_readonly(layout,0);
pango_layout_line_get_extents(pangoLine, nullptr, &pos);
- return doubleFromPangoUnits(pos.width);
+ return floatFromPangoUnits(pos.width);
}
return 1;
} else {
@@ -901,12 +903,12 @@ XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) {
XYPOSITION SurfaceImpl::Ascent(Font &font_) {
if (!(font_.GetID()))
return 1;
- int ascent = 0;
+ XYPOSITION ascent = 0;
if (PFont(font_)->pfd) {
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
PFont(font_)->pfd, pango_context_get_language(pcontext));
- ascent =
- doubleFromPangoUnits(pango_font_metrics_get_ascent(metrics));
+ ascent = std::floor(floatFromPangoUnits(
+ pango_font_metrics_get_ascent(metrics)));
pango_font_metrics_unref(metrics);
}
if (ascent == 0) {
@@ -921,7 +923,8 @@ XYPOSITION SurfaceImpl::Descent(Font &font_) {
if (PFont(font_)->pfd) {
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
PFont(font_)->pfd, pango_context_get_language(pcontext));
- int descent = doubleFromPangoUnits(pango_font_metrics_get_descent(metrics));
+ const XYPOSITION descent = std::floor(floatFromPangoUnits(
+ pango_font_metrics_get_descent(metrics)));
pango_font_metrics_unref(metrics);
return descent;
}
@@ -987,8 +990,8 @@ PRectangle Window::GetPosition() const {
if (wid) {
GtkAllocation allocation;
gtk_widget_get_allocation(PWidget(wid), &allocation);
- rc.left = allocation.x;
- rc.top = allocation.y;
+ rc.left = static_cast<XYPOSITION>(allocation.x);
+ rc.top = static_cast<XYPOSITION>(allocation.y);
if (allocation.width > 20) {
rc.right = rc.left + allocation.width;
rc.bottom = rc.top + allocation.height;
@@ -999,10 +1002,10 @@ PRectangle Window::GetPosition() const {
void Window::SetPosition(PRectangle rc) {
GtkAllocation alloc;
- alloc.x = rc.left;
- alloc.y = rc.top;
- alloc.width = rc.Width();
- alloc.height = rc.Height();
+ alloc.x = static_cast<int>(rc.left);
+ alloc.y = static_cast<int>(rc.top);
+ alloc.width = static_cast<int>(rc.Width());
+ alloc.height = static_cast<int>(rc.Height());
gtk_widget_size_allocate(PWidget(wid), &alloc);
}
@@ -1017,7 +1020,7 @@ GdkRectangle MonitorRectangleForWidget(GtkWidget *wid) {
gdk_monitor_get_geometry(monitor, &rcScreen);
#else
GdkScreen* screen = gtk_widget_get_screen(wid);
- gint monitor_num = gdk_screen_get_monitor_at_window(screen, wnd);
+ const gint monitor_num = gdk_screen_get_monitor_at_window(screen, wnd);
gdk_screen_get_monitor_geometry(screen, monitor_num, &rcScreen);
#endif
return rcScreen;
@@ -1026,18 +1029,19 @@ GdkRectangle MonitorRectangleForWidget(GtkWidget *wid) {
}
void Window::SetPositionRelative(PRectangle rc, const Window *relativeTo) {
+ const IntegerRectangle irc(rc);
int ox = 0;
int oy = 0;
GdkWindow *wndRelativeTo = WindowFromWidget(PWidget(relativeTo->wid));
gdk_window_get_origin(wndRelativeTo, &ox, &oy);
- ox += rc.left;
- oy += rc.top;
+ ox += irc.left;
+ oy += irc.top;
- GdkRectangle rcMonitor = MonitorRectangleForWidget(PWidget(relativeTo->wid));
+ const GdkRectangle rcMonitor = MonitorRectangleForWidget(PWidget(relativeTo->wid));
/* do some corrections to fit into screen */
- int sizex = rc.right - rc.left;
- int sizey = rc.bottom - rc.top;
+ const int sizex = irc.Width();
+ const int sizey = irc.Height();
if (sizex > rcMonitor.width || ox < rcMonitor.x)
ox = rcMonitor.x; /* the best we can do */
else if (ox + sizex > rcMonitor.x + rcMonitor.width)
@@ -1070,9 +1074,10 @@ void Window::InvalidateAll() {
void Window::InvalidateRectangle(PRectangle rc) {
if (wid) {
+ const IntegerRectangle irc(rc);
gtk_widget_queue_draw_area(PWidget(wid),
- rc.left, rc.top,
- rc.right - rc.left, rc.bottom - rc.top);
+ irc.left, irc.top,
+ irc.Width(), irc.Height());
}
}
@@ -1140,13 +1145,13 @@ PRectangle Window::GetMonitorRect(Point pt) {
gdk_monitor_get_geometry(monitor, &rect);
#else
GdkScreen* screen = gtk_widget_get_screen(PWidget(wid));
- gint monitor_num = gdk_screen_get_monitor_at_point(screen,
+ const gint 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);
#endif
rect.x -= x_offset;
rect.y -= y_offset;
- return PRectangle(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
+ return PRectangle::FromInts(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
}
typedef std::map<int, RGBAImage*> ImageMap;
@@ -1558,7 +1563,7 @@ PRectangle ListBoxX::GetDesiredRect() {
// 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
- int row_height = GetRowHeight();
+ const int row_height = GetRowHeight();
#if GTK_CHECK_VERSION(3,0,0)
GtkStyleContext *styleContextFrame = gtk_widget_get_style_context(PWidget(frame));
GtkStateFlags stateFlagsFrame = gtk_style_context_get_state(styleContextFrame);
@@ -1682,7 +1687,7 @@ void ListBoxX::Append(char *s, int type) {
PIXBUF_COLUMN, list_image->pixbuf,
TEXT_COLUMN, s, -1);
- gint pixbuf_width = gdk_pixbuf_get_width(list_image->pixbuf);
+ const gint pixbuf_width = gdk_pixbuf_get_width(list_image->pixbuf);
gint renderer_height, renderer_width;
gtk_cell_renderer_get_fixed_size(pixbuf_renderer,
&renderer_width, &renderer_height);
@@ -1697,7 +1702,7 @@ void ListBoxX::Append(char *s, int type) {
gtk_list_store_set(GTK_LIST_STORE(store), &iter,
TEXT_COLUMN, s, -1);
}
- size_t len = strlen(s);
+ const size_t len = strlen(s);
if (maxItemCharacters < len)
maxItemCharacters = len;
}
@@ -1720,12 +1725,12 @@ void ListBoxX::Select(int n) {
return;
}
- bool valid = gtk_tree_model_iter_nth_child(model, &iter, nullptr, n) != FALSE;
+ const bool valid = gtk_tree_model_iter_nth_child(model, &iter, nullptr, n) != FALSE;
if (valid) {
gtk_tree_selection_select_iter(selection, &iter);
// Move the scrollbar to show the selection.
- int total = Length();
+ const int total = Length();
#if GTK_CHECK_VERSION(3,0,0)
GtkAdjustment *adj =
gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(list));
@@ -1733,10 +1738,10 @@ void ListBoxX::Select(int n) {
GtkAdjustment *adj =
gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(list));
#endif
- gfloat value = ((gfloat)n / total) * (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_lower(adj))
+ gfloat value = (static_cast<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;
// Get cell height
- int row_height = GetRowHeight();
+ const int row_height = GetRowHeight();
int rows = Length();
if ((rows == 0) || (rows > desiredVisibleRows))
@@ -1744,7 +1749,7 @@ void ListBoxX::Select(int n) {
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;
+ value += static_cast<gfloat>(row_height) / 2.0f;
}
// Clamp it.
value = (value < 0)? 0 : value;
@@ -1804,7 +1809,7 @@ void ListBoxX::GetValue(int n, char *value, int len) {
char *text = nullptr;
GtkTreeIter iter;
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
- bool valid = gtk_tree_model_iter_nth_child(model, &iter, nullptr, n) != FALSE;
+ const bool valid = gtk_tree_model_iter_nth_child(model, &iter, nullptr, n) != FALSE;
if (valid) {
gtk_tree_model_get(model, &iter, TEXT_COLUMN, &text, -1);
}
@@ -1863,7 +1868,7 @@ void ListBoxX::SetDelegate(IListBoxDelegate *lbDelegate) {
void ListBoxX::SetList(const char *listText, char separator, char typesep) {
Clear();
- int count = strlen(listText) + 1;
+ const size_t count = strlen(listText) + 1;
std::vector<char> words(listText, listText+count);
char *startword = &words[0];
char *numword = nullptr;
@@ -1916,7 +1921,7 @@ void Menu::Show(Point pt, Window &w) {
// Rely on GTK+ to do the right thing with positioning
gtk_menu_popup_at_pointer(widget, nullptr);
#else
- GdkRectangle rcMonitor = MonitorRectangleForWidget(PWidget(w.GetID()));
+ const GdkRectangle rcMonitor = MonitorRectangleForWidget(PWidget(w.GetID()));
GtkRequisition requisition;
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_get_preferred_size(GTK_WIDGET(widget), nullptr, &requisition);
@@ -1952,7 +1957,7 @@ public:
Function FindFunction(const char *name) override {
if (m != nullptr) {
gpointer fn_address = nullptr;
- gboolean status = g_module_symbol(m, name, &fn_address);
+ const gboolean status = g_module_symbol(m, name, &fn_address);
if (status)
return static_cast<Function>(fn_address);
else
@@ -2023,7 +2028,7 @@ void Platform::DebugPrintf(const char *, ...) {}
static bool assertionPopUps = true;
bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
- bool ret = assertionPopUps;
+ const bool ret = assertionPopUps;
assertionPopUps = assertionPopUps_;
return ret;
}