diff options
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/gtk_util.cc | 39 | ||||
-rw-r--r-- | gfx/gtk_util.h | 5 |
2 files changed, 44 insertions, 0 deletions
diff --git a/gfx/gtk_util.cc b/gfx/gtk_util.cc index b30b827..96195fe 100644 --- a/gfx/gtk_util.cc +++ b/gfx/gtk_util.cc @@ -17,6 +17,40 @@ namespace { +// A process wide singleton that manages our usage of gdk +// cursors. gdk_cursor_new() hits the disk in several places and GdkCursor +// instances can be reused throughout the process. +class GdkCursorCache { + public: + GdkCursorCache() {} + ~GdkCursorCache() { + for (std::map<GdkCursorType, GdkCursor*>::iterator it = + cursor_cache_.begin(); it != cursor_cache_.end(); ++it) { + gdk_cursor_unref(it->second); + } + cursor_cache_.clear(); + } + + GdkCursor* GetCursorImpl(GdkCursorType type) { + std::map<GdkCursorType, GdkCursor*>::iterator it = cursor_cache_.find(type); + GdkCursor* cursor = NULL; + if (it == cursor_cache_.end()) { + cursor = gdk_cursor_new(type); + cursor_cache_.insert(std::make_pair(type, cursor)); + } else { + cursor = it->second; + } + + // It is not necessary to add a reference here. The callers can ref the + // cursor if they need it for something. + return cursor; + } + + std::map<GdkCursorType, GdkCursor*> cursor_cache_; + + DISALLOW_COPY_AND_ASSIGN(GdkCursorCache); +}; + void FreePixels(guchar* pixels, gpointer data) { free(data); } @@ -146,6 +180,11 @@ double GetPangoResolution() { return resolution; } +GdkCursor* GetCursor(int type) { + static GdkCursorCache impl; + return impl.GetCursorImpl(static_cast<GdkCursorType>(type)); +} + std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { return ConvertAmperstandsTo(label, "_"); } diff --git a/gfx/gtk_util.h b/gfx/gtk_util.h index 8f9618d..683b80c 100644 --- a/gfx/gtk_util.h +++ b/gfx/gtk_util.h @@ -16,6 +16,7 @@ typedef struct _GdkPixbuf GdkPixbuf; typedef struct _GdkRegion GdkRegion; +typedef struct _GdkCursor GdkCursor; class CommandLine; class SkBitmap; @@ -42,6 +43,10 @@ void SubtractRectanglesFromRegion(GdkRegion* region, // resolution hasn't been set. double GetPangoResolution(); +// Returns a static instance of a GdkCursor* object, sharable across the +// process. Caller must gdk_cursor_ref() it if they want to assume ownership. +GdkCursor* GetCursor(int type); + // Change windows accelerator style to GTK style. (GTK uses _ for // accelerators. Windows uses & with && as an escape for &.) std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label); |