Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938064
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:08:28+00:00 2026-05-20T00:08:28+00:00

I have written the following code to set the cursor of a Gtk::Window from

  • 0

I have written the following code to set the cursor of a Gtk::Window from a Cairo::Context. When I run the program and move the cursor into the window, the cursor changes to a horizontal black line at the top, followed by some undefinable white shape at the bottom. I was expecting the cursor to change into a black 16×16 square. Why doesn’t the cursor assume the shape I intended?

#include <gtkmm.h>

const int size = 16, hotspot = 0;

class Window : public Gtk::Window
{
  public:
    void change_cursor()
    {
      Glib::RefPtr<Gdk::Drawable> pixmap = Gdk::Pixmap::create(
          Glib::RefPtr<Gdk::Drawable>(), size, size, get_window()->get_depth());
      pixmap->set_colormap(get_window()->get_colormap());
      Cairo::RefPtr<Cairo::Context> context = pixmap->create_cairo_context();
      context->set_source_rgba(0, 0, 0, 0);
      context->rectangle(0, 0, size, size);
      context->fill();
      Glib::RefPtr<Gdk::Pixbuf> pixbuf
          = Gdk::Pixbuf::create(pixmap, 0, 0, size, size);
      Gdk::Cursor cursor(get_window()->get_display(), pixbuf, hotspot, hotspot);
      window->set_cursor(cursor);
    }
};

int main(int argc, char* argv[])
{
  Gtk::Main app(argc, argv);
  Window window;
  window.show_all();
  window.change_cursor();
  Gtk::Main::run(window);
  return 0;
}

When I draw the Gdk::Pixmap to the screen, it looks fine. When I draw the Gdk::Pixbuf to the screen, I get garbage.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:08:28+00:00Added an answer on May 20, 2026 at 12:08 am

    I did not figure out the cause of the problem, but a way to work around it:

    1. Create an empty Gdk::Pixbuf.
    2. Create a Cairo::ImageSurface using the Gdk::Pixbuf‘s pixels as data buffer.
    3. Create a Cairo::Context from the Cairo::ImageSurface.
    4. Clear the Cairo::Context (this is important, because Gdk::Pixbuf‘s pixel data seem not to be initialized yet).

    The code looks like this:

    Glib::RefPtr<Gdk::Pixbuf> pixbuf
        = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, size, size);
    Cairo::RefPtr<Cairo::ImageSurface> surface
        = Cairo::ImageSurface::create(
              pixbuf->get_pixels(), Cairo::FORMAT_ARGB32,
              size, size, pixbuf->get_rowstride() );
    Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface);
    context->save();
    context->set_operator(Cairo::OPERATOR_CLEAR);
    context->paint();
    context->restore();
    

    When I now paint to that context and set the cursor from the Gdk::Pixbuf, I get almost what I want: the shape is fine, but Red and Blue are swapped. This can be fixed as outlined in Question 4291994 (How to write contents of a Cairo Image surface into a Gdk Pixbuf?):

      void fix_buffer_after_cairo(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
      {
        guint8* pixels = pixbuf->get_pixels();
        int height = pixbuf->get_height();
        int width = pixbuf->get_width();
        int rowstride = pixbuf->get_rowstride();
    
        guint8 tmp;
        guint8* p;
        guint8* end;
    
        for (int j = height; j > 0; --j)
        {
          p = pixels;
          end = p + 4 * width;
          while (p < end)
          {
            tmp = p[0];
            if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
            {
              p[0] = p[2]; p[2] = tmp;
            }
            else
            {
              p[0] = p[1]; p[1] = p[2]; p[2] = p[3]; p[3] = tmp;
            }
            p += 4;
          }
          pixels += rowstride;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code class Program { static void Main(string[] args) {
I have written following code to set the status of the image button to
I have written following code to make a call also have set permissions in
I have written following algorithm into C# code to list down the files inside
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.