Pixmap

This demo creates a GdkPixmap and draws on it using functions like draw_rectangle, draw_layout (for Pango layouts).

Pixmap screenshot

The code is interesting as it uses quite a few low level functions, accesses to structure elements and so forth. The following function is called to create the pixmap at a given size after size change of the window:

function on_configure(da, ev, ifo) local window = ifo.win.window -- print("WINDOW", window) -- gnome.breakfunc() local width, height = window:get_size(0, 0) -- print("WIDTH, HEIGHT", width, height) assert(width) assert(height) if ifo.pixmap then -- mostly seems to work. maybe check some more gnome.destroy(ifo.pixmap) end -- allocates memory in X server... default drawable, width, height, depth -- loses the reference to the previous pixmap, if any. ifo.pixmap = gdk.pixmap_new(window, width, height, -1) local style = ifo.win:get_style() local white_gc = style.white_gc local black_gc = style.black_gc -- clear the whole pixmap ifo.pixmap:draw_rectangle(white_gc, true, 0, 0, width, height) -- draw a rectangle if width > 20 and height > 20 then ifo.pixmap:draw_rectangle(black_gc, false, 10, 10, width - 20, height - 20) end -- draw a text message local message = "Hello, World! " .. ifo.msg local layout = ifo.win:create_pango_layout(message) ifo.pixmap:draw_layout(black_gc, 15, 15, layout) -- get size of message local region = layout:get_clip_region(0, 0, {0, string.len(message)}, 1) local rect = gdk.new "Rectangle" region:get_clipbox(rect) if rect.width > 0 then ifo.pixmap:draw_layout(black_gc, width - 15 - rect.width, height - 15 - rect.height, layout) end -- Make sure that the unreferenced pixmap is freed NOW and not eventually, -- because this can eat up loads of memory of the X server. -- collectgarbage() -- collectgarbage() return true end