garrigue / lablgtk

LablGTK 2 and 3: an interface to the GIMP Tool Kit
https://garrigue.github.io/lablgtk
Other
89 stars 40 forks source link

drawing.ml example #6

Closed cedlemo closed 5 years ago

cedlemo commented 6 years ago

The drawing.ml example fail with:

../src/lablgtk2 -localdir drawing.ml                                                                                                                         lablgtk/examples  lablgtk2on3 
File "./drawing.ml", line 18, characters 18-32:
Error: Unbound module GDraw                    
Hint: Did you mean Grab?  

Furthermore it uses an GdkDraw api which is outdated (https://developer.gnome.org/gdk2/stable/gdk2-Drawing-Primitives.html#gdk-draw-point) in favor of Cairo. Does cairo work with lablgtk ?

cedlemo commented 6 years ago

@garrigue ,

In gtk3, in order to draw on a GtkDrawingArea, one have to use the draw signal that is GtkWidget signal (https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-draw).

I tried to look at the code to see how I could try to add it but I have some question.

garrigue commented 6 years ago

GtkBase is about GtkWidget and a few auxiliary widgets. The .props files are used to generate the gtkProps.ml and ogtkProps.ml files. The syntax is not documented :( Syntax for signals and properties should be clear enough.

cedlemo commented 6 years ago

@garrigue ,

I have 2 questions:

  1. I made some tries with.
diff --git a/src/gtkBase.props b/src/gtkBase.props
index 2dc9878..f2d80b5 100644
--- a/src/gtkBase.props
+++ b/src/gtkBase.props
@@ -87,6 +87,7 @@ class Widget abstract : Object {
   signal hide
   signal map
   signal unmap
+  signal draw
   signal query_tooltip : x:int y:int kbd:bool GtkTooltip -> bool
   signal realize
   signal unrealize

It generates in the gtkBaseProps.ml via propcc:

   let draw = {name="draw"; classe=`widget; marshaller=marshal_unit} 

But when I try to use the signal with:

open GMain                                                                                                                                      

 let window = GWindow.window ()                                                                                                                       
 let area = GMisc.drawing_area ~packing:window#add ()                                                                                            

 let w = area#misc#realize (); area#misc#window                                                                                                                                                  
 let redraw () =                                                                                                                                 
     Printf.printf "draw\n"                                                                                                                          

let _ =                                                                                                                                         
   window#connect#destroy ~callback:Main.quit;                                                                                                   
   area#connect#draw ~callback:redraw;                                                                                                           
   window#show ();                                                                                                                               
   Main.main () 

It seems that the draw signal is not know.

../src/lablgtk2 -localdir drawing.ml                                                                    
File "./drawing.ml", line 24, characters 2-14:
Error: This expression has type GObj.widget_signals
       It has no method draw

Is there another files I should modify to add a signal ?

  1. The next step would be to be able to add a CairoContext parameter. I looked at the OCaml-cairo repository and here :

there are the Cairo types convertion functions. If we can create the same Cairo types, then the OCaml-cairo package should be usable with the draw event (I guess ...). Have you got any ideas on how / where I can add a CairoContext new type in the propscc generator and a C Cairo pointer to Cairo type function ?

garrigue commented 6 years ago

You can add this method to all widgets, by adding it manually to GObj.misc_signals. Note that you also need to edit gObj.mli.

For cairo, there seems to be 2 bindings, I didn't get any reaction yet. Maybe ask one of the maintainers directly. The problem is that since lablgtk2on3 becomes dependent on ocaml-cairo, the bindings should be done on the lablgtk2on3 side rather than on the ocaml-cairo side.

Once you have converter functions, you can build a Gobject.data_conv from them, and either add them to the conversions section of the props file, or directly to propcc.ml4.

sacerdot commented 5 years ago

Hi, I am also interested into bringing back cairo to lablgtk3. Cedlemo, do you have working code to share? Otherwise, I have right now a working implementation, but it is based on Obj.magic to turn a lablgtk3 cairo context into a cairo cairo context.

Chris00, Jacques, what are your preferences? Either

  1. lablgtk3 strongly depends on cairo to reuse cairo type or
  2. lablgtk3 has its own type and in cairo (cairo-gtk to be more precise) I put an identity function from lablgtk3 cairo context to cairo cairo context type.

2 is ugly and requires an unneeded gimnics from the coders; 1 introduces an hard dependency (which already exists at the C level btw)

cedlemo commented 5 years ago

@sacerdot, No I have not been able to make something work. That is really great that you can provide a working implementation.

garrigue commented 5 years ago

I have no problem introducing a hard dependency ocaml-cairo. The loss of drawing primitives is a huge gap in lablgtk3 at this point. However it requires cooperation from @Chris00. If you can obtain it, then let’s go for that.

sacerdot commented 5 years ago

Great. For @Chris00 I operationally suggest to:

  1. make lablgtk3 depend on ocaml-cairo and reuse its binding to cairo_t
  2. remove cairo2-gtk from ocaml-cairo, implementing the only two non-deprecated methods directly in lablgtk3/src/gdk. It's a very few lines of code I need to reimplement, possibly reusing your code. Are the two licences compatible? Btw, I can already run examples-gtk and tests-gtk even without cairo2-gtk because the draw signal of gtk3 already returns a cairo_t
  3. remove cairo2-pango and move it to lablgtk3 as well. All of its methods are NOT deprecated

Christophe, please let me know if you prefer a different plan or if I can proceed. If I receive the green light I will first do a pull request on lablgtk3 to introduce the depenency over ocam_cairo and move there you cairo2-gtk and cairo2-pango code and only then a pull request to remove those parts from ocaml-cairo.

Chris00 commented 5 years ago
  1. OK — the package name is cairo2.
  2. remove cairo2-gtk from ocaml-cairo: I would prefer not as some people may still use the old Gtk2 bindings. I can add that it is for Gtk2 only in the description though. Cairo2 is released under the LGPL and so is lablgtk.
  3. For the same reason as in 2, I'd like to keep it around (for lablgtk2). But a copy of it may be made to lablgtk3.
sacerdot commented 5 years ago

I have just made a pull request to lablgtk3 according to Christophe wishes. It introduces an hard dependency on cairo2. So far I moved verbatim the code of cairo-pango to the one of lablgtk3. I have not ported cairo-gtk at all since it binds mostly deprecated functions and it is no longer necessary to have working examples. Finally, I have copied and modified examples-{gtk,pango}/* to examples/{cairo,pango1,pango2}.ml

Please, read the changelog of the pull request for further details.

sacerdot commented 5 years ago

Btw, I have also ported drawing.ml to cairo in order to allow to close this issue if Jacques wants to.

Chris00 commented 5 years ago

@sacerdot made some comments on the example.