package stk

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file edit.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(*********************************************************************************)
(*                OCaml-Stk                                                      *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    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, version 3 of the License.       *)
(*                                                                               *)
(*    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., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Entry widgets. *)

open Misc
open Tsdl
open Tsdl_ttf
open Widget

[@@@landmark "auto"]

module B = Textbuffer

(** Property used to define the character displayed instead of
  newline in single-line edition widgets. Default value is ↵ (U+21B5 (8629)).*)
let newline_char_codepoint = Props.int_prop
  ~after:[Render]
  ~default:8629
  ~inherited:true "newline_char_codepoint"

let css_newline_char_codepoint = Theme.int_prop newline_char_codepoint

class entry ?classes ?name ?props ?wdata () =
  object(self)
    inherit Textview.textview ?classes ?name ?props ?wdata () as super
    (**/**)
    method kind = "entry"
    val mutable replacing_text = false
    (**/**)

    method newline_char_codepoint = self#get_p newline_char_codepoint
    method set_newline_char_codepoint = self#set_p newline_char_codepoint

    (**/**)
    method! private on_buffer_change change =
      super#on_buffer_change change ;
      if not replacing_text then self#set_p Props.text (self#text())

    method! private width_constraints_ =
      let c = super#width_constraints_ in
      { c with max_used = None ; max_abs = None }

    method! private height_constraints_ =
      let c = super#height_constraints_ in
      { c with max_used = None ; max_abs = None }

    (**/**)

    method set_text str =
      replacing_text <- true ;
      (try ignore(self#delete ()) ; replacing_text <- false
      with _ -> replacing_text <- false);
      self#insert str

    (**/**)
    method! render_cursor rend ~offset rg cursor_id c =
      if self#has_focus then
        super#render_cursor rend ~offset rg cursor_id c

    initializer
      Textview.set_default_key_bindings self#as_textview;
      B.set_map_in buffer
        (Some (fun c ->
           match Uchar.to_int c with
           | 10 (* \n *) ->
                prerr_endline (Printf.sprintf "mapping to code point %d" self#newline_char_codepoint);
                Uchar.of_int self#newline_char_codepoint
           | _ -> c)) ;
      B.set_map_out buffer
        (Some (fun c ->
           if Uchar.to_int c = self#newline_char_codepoint then
             Uchar.of_int 10
           else
             c))

  end

type Widget.widget_type += Entry of entry

(** Convenient function to create a {!class-entry}. Optional arguments:
  {ul
  {- [editable] specifies whether the contents is editable by user (default is [true]).}
  {- [text] specifies the initial text (default is empty).}
  }
  See {!Widget.widget_arguments} for other arguments. *)
let entry ?classes ?name ?props ?wdata ?editable ?text ?pack () =
  let e = new entry ?classes ?name ?props ?wdata () in
  e#set_typ (Entry e);
  Option.iter e#set_editable editable ;
  Widget.may_pack ?pack e#coerce ;
  Option.iter e#insert text ;
  e

OCaml

Innovation. Community. Security.