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 8559819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:08:30+00:00 2026-06-11T16:08:30+00:00

Followed is a small code to use cairo to draw something. The problem is

  • 0

Followed is a small code to use cairo to draw something. The problem is that the canvas drawn by cairo is larger than the area of gtk’s scrolled_window. But the scrolled_window doesn’t scroll so I can only see the part of the canvas.

Any suggestion? My environment: MinGW + gtk2.10.11.

#include <gtk/gtk.h>

static gboolean draw_cb (GtkWidget *widget, GdkEventExpose *event) 
{
  cairo_t *cr;
  cr = gdk_cairo_create (widget->window);
  cairo_set_source_rgb(cr, 1, 1, 1);
  cairo_paint(cr);
  cairo_set_source_rgb (cr, 0.42, 0.65, 0.80);
  cairo_set_line_width (cr,6);
  cairo_rectangle (cr, 30, 30, 100, 100);
  cairo_stroke (cr); 
  cairo_destroy(cr);
  return FALSE;
}

int main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);
  GtkWidget *window;
  GtkWidget *sw;
  GtkWidget *cr;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_policy((GtkWindow *)window, FALSE, FALSE, FALSE);
  sw = gtk_scrolled_window_new (NULL,NULL);
  gtk_widget_set_usize(sw, 100, 100);
  cr = gtk_drawing_area_new();

  gtk_scrolled_window_add_with_viewport(GTK_CONTAINER(sw), cr);
  gtk_container_add (GTK_CONTAINER(window), sw);

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (cr, "expose-event", G_CALLBACK(draw_cb),  NULL);

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
  }
  • 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-06-11T16:08:31+00:00Added an answer on June 11, 2026 at 4:08 pm

    Usually, when drawing you have two choices:

    1. either you want the drawn content to adapt to the available size, and you use cairo_scale for that
    2. or you use a scrolled view, as you’re doing.

    In case 2, Cairo has no way of knowing you’re drawing out of bounds. And in some cases, drawing out of bounds is even what you want. So here, what is missing is setting the drawing area’s size. This can be done using gtk_widget_set_size_request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've wrote a small piece of code that reads the current battery charge/discharge on
I recently followed a small tutorial on howto install apache on a systems where
I followed this post http://neurochannels.blogspot.com/2010/05/how-to-run-r-code-in-matlab.html , to install R(D)Com server in order to call
I have written a small test program in which I try to use the
I followed this description for debugging native code in an Android app and it
I've managed to implement a solution to this problem, however the code just seems
I followed this small tutorial on how to add a scrollbar to an ItemsControl,
We are trying to use persits upload code to upload pictures to 2 different
I followed an example in Charles Petzold's book Programming Windows. That example shows the
I'm coding a small function to help with learning how to use python and

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.