camlkitとGNUstepを使用して、imageを表示してみました。
NSImageView を使用している。
[実行結果]

画像はGNUstep.tiff
button, label(TextField), image, image付きbuttonを表示しています。
プロジェクト構成
(dune init project で作成。前回同様、build, exec の後lib, test フォルダを削除。)
/bin dune main.ml GNUstep.tiff dune-project (プロジェクト名).opam
dune
(executable
(public_name camlkitGSTestImageView)
(name main)
(flags -ccopt -L/usr/local/GNUstep/System/Library/Libraries -cclib -lgnustep-base -cclib -lgnustep-gui)
(libraries
camlkitGS.Foundation
camlkitGS.AppKit))
main.ml
open Foundation
open Runtime
open AppKit
let win_width = 400.
let win_height = 300.
let app_window () =
let win =
alloc NSWindow.self
|> NSWindow.initWithContentRect
(CGRect.make ~x: 0. ~y: 0. ~width: win_width ~height: win_height)
~styleMask: Bitmask.(
_NSWindowStyleMaskTitled +
_NSWindowStyleMaskClosable +
_NSWindowStyleMaskResizable)
~backing: _NSBackingStoreBuffered
~defer: false
in
win
|> NSWindow.cascadeTopLeftFromPoint (CGPoint.init ~x:20. ~y:20.)
|> ignore;
win |> NSWindow.setTitle (new_string "Hello Caml");
win |> NSWindow.makeKeyAndOrderFront nil;
win
let make_button ~title ~frame ~target ~action =
let btn = alloc NSButton.self |> NSButton.initWithFrame frame in
btn |> NSControl.setTarget target;
btn |> NSControl.setAction action;
btn |> NSButton.setTitle title;
btn
let make_button2 ~frame ~target ~action =
let btn = alloc NSButton.self |> NSButton.initWithFrame frame
and image =
alloc NSImage.self |> NSImage.initWithContentsOfFile (new_string "GNUstep.tiff") in
btn |> NSControl.setTarget target;
btn |> NSControl.setAction action;
btn |> NSButton.setImage image;
btn
let make_label ~frame ~text =
let label = _new_ NSTextField.self in
label |> NSView.setFrame frame;
label |> NSControl.setStringValue text;
label |> NSTextField.setBezeled false;
label |> NSTextField.setDrawsBackground false;
label |> NSTextField.setEditable false;
label
let create_image ~file =
let image =
alloc NSImage.self |> NSImage.initWithContentsOfFile file in
image
let make_imageView ~frame ~image =
let imageView =
_new_ NSImageView.self in
imageView |> NSView.setFrame frame;
imageView |> NSImageView.setImage image;
imageView
let main () =
let _ = new_object "NSAutoreleasePool"
and app = NSApplication.self |> NSApplicationClass.sharedApplication
and win = app_window ()
in
let btn =
make_button
~title:(new_string "Quit")
~target:app
~action:(selector "terminate:")
~frame:(CGRect.make
~x:10. ~y:(win_height -. 40.)
~width:100. ~height:30.)
and label =
make_label
~frame:(CGRect.make
~x:10. ~y:(win_height -. 100.)
~width:100. ~height:30.)
~text: (new_string "Hello")
and btn2 =
make_button2
~target:app
~action:(selector "terminate:")
~frame:(CGRect.make
~x:10. ~y:(win_height -. 200.)
~width:100. ~height:30.)
and image =
create_image ~file: (new_string "GNUstep.tiff")
in
let imageView =
make_imageView
~frame: (CGRect.make
~x:200. ~y:(win_height -. 200.)
~width:50. ~height:50.)
~image: image in
win |> NSWindow.contentView |> NSView.addSubview btn;
win |> NSWindow.contentView |> NSView.addSubview label;
win |> NSWindow.contentView |> NSView.addSubview btn2;
win |> NSWindow.contentView |> NSView.addSubview imageView;
app |> NSApplication.activateIgnoringOtherApps true;
NSApplication.run app
let () = main ()





