summaryrefslogtreecommitdiff
path: root/ide/utils/configwin_types.ml
blob: ee8ec70c4fd736eafc387abb794da8651cebccee (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
(**************************************************************************)
(*                   Cameleon                                             *)
(*                                                                        *)
(*      Copyright (C) 2002 Institut National de Recherche en Informatique et   *)
(*      en Automatique. All rights reserved.                              *)
(*                                                                        *)
(*      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  *)
(*      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., 59 Temple Place, Suite 330, Boston, MA          *)
(*      02111-1307  USA                                                   *)
(*                                                                        *)
(*      Contact: Maxence.Guesdon@inria.fr                                *)
(**************************************************************************)

(** This module contains the types used in Configwin. *)

open Uoptions

(** A module to define key options, with the {!Uoptions} module. *)
module KeyOption = struct
  let name_to_keysym = 
    ("Button1", Configwin_keys.xk_Pointer_Button1) ::
    ("Button2", Configwin_keys.xk_Pointer_Button2) ::
    ("Button3", Configwin_keys.xk_Pointer_Button3) ::
    ("Button4", Configwin_keys.xk_Pointer_Button4) ::
    ("Button5", Configwin_keys.xk_Pointer_Button5) ::
    Configwin_keys.name_to_keysym
      
  let string_to_key s =
    let mask = ref [] in
    let key = try
      let pos = String.rindex s '-' in
      for i = 0 to pos - 1 do 
        let m = match s.[i] with
          'C' -> `CONTROL
        | 'S' -> `SHIFT
        | 'L' -> `LOCK 
        | 'M' -> `MOD1
        | 'A' -> `MOD1
        | '1' -> `MOD1
        | '2' -> `MOD2
        | '3' -> `MOD3
        | '4' -> `MOD4
        | '5' -> `MOD5
        | _ -> 
	    prerr_endline s;
	    raise Not_found
        in
        mask := m :: !mask
      done;
      String.sub s (pos+1) (String.length s - pos - 1)
    with _ -> 
      s
    in
    try
      !mask, List.assoc key name_to_keysym
    with
      e -> 
	prerr_endline s;
	raise e
      
  let key_to_string (m, k) =
    let s = List.assoc k Configwin_keys.keysym_to_name in
    match m with
      [] -> s
    | _ ->
        let rec iter m s =
          match m with
            [] -> s
          | c :: m ->
              iter m ((
                      match c with
			`CONTROL -> "C"
                      | `SHIFT -> "S"
                      | `LOCK -> "L"
                      | `MOD1 -> "A"
                      | `MOD2 -> "2"
                      | `MOD3 -> "3"
                      | `MOD4 -> "4"
                      | `MOD5 -> "5"
                      | _  -> raise Not_found
                     ) ^ s)
        in
        iter m ("-" ^ s)

  let modifiers_to_string m =
    let rec iter m s =
      match m with
          [] -> s
        | c :: m ->
            iter m ((
                      match c with
			  `CONTROL -> "<ctrl>"
			| `SHIFT -> "<shft>"
			| `LOCK -> "<lock>"
			| `MOD1 -> "<alt>"
			| `MOD2 -> "<mod2>"
			| `MOD3 -> "<mod3>"
			| `MOD4 -> "<mod4>"
			| `MOD5 -> "<mod5>"
			| _  -> raise Not_found
                    ) ^ s)
    in
    iter m ""
	  
  let value_to_key v =
    match v with
      StringValue s -> string_to_key s
    | _ -> 
	prerr_endline "value_to_key";
	raise Not_found
	  
  let key_to_value k =
    StringValue (key_to_string k)
      
  let (t : (Gdk.Tags.modifier list * int) option_class) = 
    define_option_class "Key" value_to_key key_to_value
end

(** This type represents a string or filename parameter. *)
type string_param = {
    string_label : string; (** the label of the parameter *)
    mutable string_value : string; (** the current value of the parameter *)
    string_editable : bool ; (** indicates if the value can be changed *)
    string_f_apply : (string -> unit) ; (** the function to call to apply the new value of the parameter *)
    string_help : string option ; (** optional help string *)
    string_expand : bool ; (** expand or not *)
  } ;;

(** This type represents a boolean parameter. *)
type bool_param = {
    bool_label : string; (** the label of the parameter *)
    mutable bool_value : bool; (** the current value of the parameter *)
    bool_editable : bool ; (** indicates if the value can be changed *)
    bool_f_apply : (bool -> unit) ; (** the function to call to apply the new value of the parameter *)
    bool_help : string option ; (** optional help string *)
  } ;;

(** This type represents a parameter whose value is a list of ['a]. *)
type 'a list_param = {
    list_label : string; (** the label of the parameter *)
    mutable list_value : 'a list; (** the current value of the parameter *)
    list_titles : string list option; (** the titles of columns, if they must be displayed *)
    list_f_edit : ('a -> 'a) option; (** optional edition function *)
    list_eq : ('a -> 'a -> bool) ; (** the comparison function used to get list without doubles *)
    list_strings : ('a -> string list); (** the function to get a string list from a ['a]. *)
    list_color : ('a -> string option) ; (** a function to get the optional color of an element *)
    list_editable : bool ; (** indicates if the value can be changed *)
    list_f_add : unit -> 'a list ; (** the function to call to add list *)
    list_f_apply : ('a list -> unit) ; (** the function to call to apply the new value of the parameter *)
    list_help : string option ; (** optional help string *)
  } ;;

type combo_param = {
    combo_label : string ;
    mutable combo_value : string ;
    combo_choices : string list ;
    combo_editable : bool ;
    combo_blank_allowed : bool ;
    combo_new_allowed : bool ;
    combo_f_apply : (string -> unit);
    combo_help : string option ; (** optional help string *)
    combo_expand : bool ; (** expand the entry widget or not *)
  } ;;

type custom_param = {
    custom_box : GPack.box ;
    custom_f_apply : (unit -> unit) ;
    custom_expand : bool ;
    custom_framed : string option ; (** optional label for an optional frame *)
  } ;;

type color_param = {
    color_label : string; (** the label of the parameter *)
    mutable color_value : string; (** the current value of the parameter *)
    color_editable : bool ; (** indicates if the value can be changed *)
    color_f_apply : (string -> unit) ; (** the function to call to apply the new value of the parameter *)
    color_help : string option ; (** optional help string *)
    color_expand : bool ; (** expand the entry widget or not *)
  } ;;

type date_param = {
    date_label : string ; (** the label of the parameter *)
    mutable date_value : int * int * int ; (** day, month, year *)
    date_editable : bool ; (** indicates if the value can be changed *)
    date_f_string : (int * int * int) -> string ;
      (** the function used to display the current value (day, month, year) *)
    date_f_apply : ((int * int * int) -> unit) ;
      (** the function to call to apply the new value (day, month, year) of the parameter *)
    date_help : string option ; (** optional help string *)
    date_expand : bool ; (** expand the entry widget or not *)
  } ;;

type font_param = {
    font_label : string ; (** the label of the parameter *)
    mutable font_value : string ; (** the font name *)
    font_editable : bool ; (** indicates if the value can be changed *)
    font_f_apply : (string -> unit) ;
      (** the function to call to apply the new value of the parameter *)
    font_help : string option ; (** optional help string *)
    font_expand : bool ; (** expand the entry widget or not *)
  } ;;


type hotkey_param = {
    hk_label : string ; (** the label of the parameter *)
    mutable hk_value : (Gdk.Tags.modifier list * int) ; 
             (** The value, as a list of modifiers and a key code *)
    hk_editable : bool ; (** indicates if the value can be changed *)
    hk_f_apply : ((Gdk.Tags.modifier list * int) -> unit) ;
             (** the function to call to apply the new value of the paramter *)
    hk_help : string option ; (** optional help string *)
    hk_expand : bool ; (** expand or not *)
  } 

type modifiers_param = {
    md_label : string ; (** the label of the parameter *)
    mutable md_value : Gdk.Tags.modifier list ; 
             (** The value, as a list of modifiers and a key code *)
    md_editable : bool ; (** indicates if the value can be changed *)
    md_f_apply : Gdk.Tags.modifier list -> unit ;
             (** the function to call to apply the new value of the paramter *)
    md_help : string option ; (** optional help string *)
    md_expand : bool ; (** expand or not *)
    md_allow : Gdk.Tags.modifier list
  } 

(** This type represents the different kinds of parameters. *)
type parameter_kind =
    String_param of string_param
  | List_param of (unit -> <box: GObj.widget ; apply : unit>)
  | Filename_param of string_param
  | Bool_param of bool_param
  | Text_param of string_param
  | Combo_param of combo_param
  | Custom_param of custom_param
  | Color_param of color_param
  | Date_param of date_param
  | Font_param of font_param
  | Hotkey_param of hotkey_param
  | Modifiers_param of modifiers_param
  | Html_param of string_param
;;

(** This type represents the structure of the configuration window. *)
type configuration_structure =
  | Section of string * parameter_kind list (** label of the section, parameters *)
  | Section_list of string * configuration_structure list (** label of the section, list of the sub sections *)
;;

(** To indicate what button was pushed by the user when the window is closed. *)
type return_button =
    Return_apply (** The user clicked on Apply at least once before
	     closing the window with Cancel or the window manager. *)
  | Return_ok (** The user closed the window with the ok button. *)
  | Return_cancel (** The user closed the window with the cancel
		     button or the window manager but never clicked
		     on the apply button.*)

(** {2 Bindings in the html editor} *)

type html_binding = {
    mutable html_key :  (Gdk.Tags.modifier list * int) ;
    mutable html_begin : string ;
    mutable html_end : string ;
  } 

module Html_binding = struct
  let value_to_hb v =
    match v with
      List [StringValue hk ; StringValue debut; StringValue fin ]
    | SmallList [StringValue hk ; StringValue debut; StringValue fin ] ->
        { html_key = KeyOption.string_to_key hk ;
	  html_begin = debut ;
	  html_end = fin ;
        }
    | _ ->
        prerr_endline "Html_binding.value_to_hb";
        raise Not_found

  let hb_to_value hb =
    SmallList [ StringValue (KeyOption.key_to_string hb.html_key) ;
                StringValue hb.html_begin ;
                StringValue hb.html_end ;
	      ]	

  let (t : html_binding option_class) =
    define_option_class "html_binding" value_to_hb hb_to_value
end