Pegasum Pegasum - 2 months ago 19
C Question

C - Gtk3- Cairo - DrawArea - Plane cartesian - How i add text?

my purpose (for personal exercise) is create a plane cartesian where i represent some math functions.
Cause of this i need of trading coordinates, how i can add text on DrawArea?
I searched but i don't have find nothing (examples ecc) about draw text with gtk3-C.

Other , do you have some tutorial-guide for DrawArea- Cairo-Pango or other about graphics 2d-3d from use with gtk3?

PS: I am beginner, but why people say bad about gtk/c? only because is more complex?
Thanks guys

Answer

You can simplify your life a little by switching to GooCanvas. Arguably, using Cairo and lower level tools are more efficient, and faster, but maybe more appropriate for drawing widgets (buttons and the like).

Note that DrawingArea isn't really a drawing tool - it's just an empty widget where you can do things in.

Using a canvas like GooCanvas can ease your life, by taking care of the redrawing events, and giving you useful things such as drawing in layers, mouse events, printing, etc.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_goo.py
#  
#  Copyright 2016 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  


from gi.repository import Gtk, GooCanvas

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())

        canvas = GooCanvas.Canvas()
        root_layer = canvas.get_root_item()

        # Draw a rectangle:
        rect = GooCanvas.CanvasRect(
                parent = root_layer,
                x = 20, y = 50,
                width = 60, height = 75,
                line_width = 2.0,
                stroke_color = "Yellow")

        # Draw some text:
        text1 = GooCanvas.CanvasText(
                parent = root_layer,
                x = 50, y = 70,
                font = "Times 25",
                text = "Hi there",
                fill_color = "red")

        self.add(canvas)
        self.show_all()

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

People frequently say bad things about things they don't really know. Frequently, I prefer Python, because of the productivity - the same program written in C takes twice (or more) to write and debug. When speed is not an issue (with modern machines it almost never is), Python is just great.

Here are some references:

Comments