![]() Next up we create the button and pack it in the vbox. The menubar is packed first into the vbox: (* Menu bar *) let menubar = nu_bar ~packing:vbox#pack () in let factory = new GMenu.factory menubar in let accel_group = factory#accel_group in let file_menu = factory#add_submenu "File" in (* File menu *) let factory = new GMenu.factory file_menu ~accel_group in Notice the accel_group object which is used to keep track ofĪccelerator buttons. Received by the window when someone presses the "X" in the corner, willĬause a call to Main.quit () (note extra unit arg).Ĭreating the menubar and File -> Quit button is straightforward enough. Window#connect#destroy ~callback:Main.quit is an example ofĬonnecting a signal to a function. The two forms are, as far as I can tell, identical. It's just a shorthand way of writing: let vbox = GPack.vbox () in The ~packingĪrgument looks odd (indeed it is). Other widgets but isn't normally visible by itself. Only way for OCaml to tell the difference between a partial and fullįunction application in the presence of optional arguments.Ī vbox is a vertical layout widget. Makes extensive use of the ~label (labelled and optional arguments). Often named the same as the class which they create. You shouldn't create lablgtk objects using new classname arguments.,īut instead use one of these helper functions. Which we'll discuss in the intensive section at the end of this chapter, Notice three things: firstly GWindow.window is actually a helperįunction which creates a window object for you. let window = GWindow.window ~width:320 ~height:240įirstly we create the toplevel window. GdkKeysyms provides some keyĭefinitions, in this case _Q for the Ctrl + Q key combination. GMain provides useful functions like the Gtk main loop, so I find it a Let's analyse the program line by line to see what's going on. This is what you should see when you run it: (recommended), you can use the following command: ocamlfind ocamlc -g -package lablgtk2 -linkpkg simple.ml -o simple Where you have to replace path/to/lablgtk by the actual path where Save this to a file simple.ml and compile it like this: ocamlc -g -I path/to/lablgtk lablgtk.cma gtkInit.cmo simple.ml -o simple (* Display the windows and enter Gtk+ main loop *) (* Button *) let button = GButton.button ~label: "Push me!"īutton#connect#clicked ~callback: ( fun () -> prerr_endline "Ouch!") ![]() (* Menu bar *) let menubar = nu_bar ~packing:vbox#pack () in let factory = new GMenu.factory menubar in let accel_group = factory#accel_group in let file_menu = factory#add_submenu "File" in (* File menu *) let factory = new GMenu.factory file_menu ~accel_group inįactory#add_item "Quit" ~key:_Q ~callback: Main.quit Window#connect#destroy ~callback: Main.quit ~title: "Simple lablgtk program" () in let vbox = GPack.vbox ~packing:window#add () in Let window = GWindow.window ~width:320 ~height:240 This program has a menu with a "quit" option,Īnd a button in the main area which you can press: open GMain open GdkKeysyms let locale = GtkMain. Of this tutorial finds ploughing through the lablgtk source code prettyĪctually, not so simple. Have any idea what's going on in this chapter. Make sure you're familiar with labelled and optional argumentsĪnd polymorphic variants ( labels) or you won't Lablgtk makes ambitious use of advanced features of the OCaml type Topfind.reset() to force that packages will be reloaded #predicates "p,q." to set these predicates #camlp4o to load camlp4 (standard syntax) Opam): # Topdirs.dir_directory ( Sys.getenv "OCAML_TOPLEVEL_PATH") įindlib has been successfully loaded. Must first issue (assuming you have installed lablgtk using If you intend to try the code in this tutorial in the
0 Comments
Leave a Reply. |