summaryrefslogtreecommitdiff
path: root/src/dialog.ml
blob: 826d2800dc5cd790a66483876f0b2b48668e96f0 (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


(* Programmed by Jedidiah Barber *)
(* Licensed under the Sunset License v1.0 *)


let license_text =
  "SUNSET LICENSE\n" ^
  "Version 1.0, June 2017\n" ^
  "\n" ^
  "1. You may copy, modify, use, sell, or distribute this work, verbatim or\n" ^
  "modified, for any purpose.\n" ^
  "\n" ^
  "2. If you sell or distribute this work, whether verbatim or modified, you must\n" ^
  "include a copy of this license, and you must make the source code available for\n" ^
  "no extra charge.\n" ^
  "\n" ^
  "3. A modified version of this work must be clearly labeled as such.\n" ^
  "\n" ^
  "4. Derivative works must also be licensed under this license or a license of\n" ^
  "equivalent terms. As an exception, linking this work with another, whether\n" ^
  "statically or dynamically, does not impose any license requirements on the\n" ^
  "other work.\n" ^
  "\n" ^
  "5. If a minimum of 15 years have passed since the date of first publishing for\n" ^
  "a part of this work, then that part is placed into the public domain and you\n" ^
  "may do whatever you want with it, regardless of all other clauses.\n"



let new_wall () =
  let newer =
    GWindow.dialog
      ~title:"Create New Wall - Number Wall Viewer" () in
  let dialog_box =
    GPack.vbox
      ~border_width:10
      ~packing:newer#vbox#add () in
  let frame =
    GBin.frame
      ~label:"New Wall"
      ~packing:dialog_box#add () in
  let frame_box =
    GPack.vbox
      ~border_width:5
      ~packing:frame#add () in
  let seq_box =
    GPack.hbox
      ~border_width:5
      ~packing:frame_box#add () in
  let _ =
    GMisc.label
      ~text:"Sequence : "
      ~packing:seq_box#add () in
  let seq_combo =
    GEdit.combo
      ~popdown_strings:["Pagoda";"Random"]
      ~allow_empty:false
      ~case_sensitive:false
      ~enable_arrow_keys:true
      ~packing:seq_box#add () in
  let adj_box =
    GPack.hbox
      ~border_width:5
      ~packing:frame_box#add () in
  let x_adj =
    GData.adjustment
      ~value:200.0
      ~lower:10.0
      ~upper:10000.0
      ~step_incr:1.0
      ~page_incr:100.0
      ~page_size:0.0 () in
  let y_adj =
    GData.adjustment
      ~value:200.0
      ~lower:10.0
      ~upper:10000.0
      ~step_incr:1.0
      ~page_incr:100.0
      ~page_size:0.0 () in
  let _ =
    GMisc.label
      ~text:"X : "
      ~packing:adj_box#add () in
  let _ =
    GEdit.spin_button
      ~adjustment:x_adj
      ~rate:0.0
      ~digits:0
      ~wrap:false
      ~packing:adj_box#add () in
  let _ =
    GMisc.label
      ~text:"  Y : "
      ~packing:adj_box#add () in
  let _ =
    GEdit.spin_button
      ~adjustment:y_adj
      ~rate:0.0
      ~digits:0
      ~wrap:false
      ~packing:adj_box#add () in
  newer#add_button_stock `CANCEL `CANCEL;
  newer#add_button_stock `NEW `NEW;
  let code = newer#run () in
  let result = (int_of_float x_adj#value, int_of_float y_adj#value, seq_combo#entry#text) in
  newer#destroy ();
  if code = `NEW then Some result else None



let open_file ?(folder = "") () =
  let chooser =
    GWindow.file_chooser_dialog
      ~action:`OPEN
      ~title:"Open Sequence - Number Wall Viewer" () in
  chooser#add_button_stock `CANCEL `dialog;
  chooser#add_select_button_stock `OPEN `filechooser;
  ignore (chooser#set_current_folder folder);
  let code = chooser#run () in
  let maybe_result = chooser#filename in
  chooser#destroy ();
  if code <> `filechooser then None else maybe_result



let overwrite_check ~filename =
  if not (Sys.file_exists filename) then
    true
  else
   (let asker =
      GWindow.dialog
        ~title:"Confirm Overwrite"
        ~resizable:false
        ~width:380 ~height:130 () in
    let box =
      GPack.hbox
        ~packing:asker#vbox#add () in
    ignore (GMisc.image
        ~stock:`DIALOG_ERROR
        ~packing:box#add ());
    let message =
      GMisc.label
        ~text:("Error: " ^ filename ^ " already exists.")
        ~packing:box#add () in
    message#set_line_wrap true;
    asker#add_button "Cancel" `dialog;
    asker#add_button "Overwrite" `filechooser;
    let code = asker#run () in
    asker#destroy ();
    code = `filechooser)

let save_export_file ~title ?(folder = "") ?(name = "") () =
  let chooser =
    GWindow.file_chooser_dialog
      ~action:`SAVE
      ~title:title () in
  chooser#add_button_stock `CANCEL `dialog;
  chooser#add_select_button_stock `SAVE `filechooser;
  chooser#set_current_name name;
  ignore (chooser#set_current_folder folder);
  let rec get_result chooser =
    let code = chooser#run () in
    let choice = chooser#filename in
    if code <> `filechooser then
      None
    else
      if Option.is_none choice ||
        overwrite_check ~filename:(Option.get choice)
      then
        choice
      else
        get_result chooser in
  let result = get_result chooser in
  chooser#destroy ();
  result

let save_file ?(folder = "") ?(name = "") () =
  save_export_file ~title:"Save Sequence - Number Wall Viewer" ~folder ~name ()

let export_file ?(folder = "") ?(name = "") () =
  save_export_file ~title:"Export Image - Number Wall Viewer" ~folder ~name ()



let colors ~fore ~back =
  let colormap = Gdk.Color.get_system_colormap () in
  let fore_color = Gdk.Color.alloc ~colormap (`RGB fore) in
  let back_color = Gdk.Color.alloc ~colormap (`RGB back) in
  let kaleidoscope =
    GWindow.dialog
      ~title:"Change Colors - Number Wall Viewer" () in
  let dialog_box =
    GPack.vbox
      ~border_width:10
      ~packing:kaleidoscope#vbox#add () in
  let frame =
    GBin.frame
      ~label:"Colors"
      ~packing:dialog_box#add () in
  let frame_box =
    GPack.vbox
      ~border_width:5
      ~packing:frame#add () in
  let fg_box =
    GPack.hbox
      ~packing:frame_box#add () in
  let _ =
    GMisc.label
      ~text:"Foreground : "
      ~packing:fg_box#add () in
  let fg_button =
    GButton.color_button
      ~color:fore_color
      ~title:"Change Foreground Color"
      ~packing:fg_box#add () in
  let bg_box =
    GPack.hbox
      ~packing:frame_box#add () in
  let _ =
    GMisc.label
      ~text:"Background : "
      ~packing:bg_box#add () in
  let bg_button =
    GButton.color_button
      ~color:back_color
      ~title:"Change Background Color"
      ~packing:bg_box#add () in
  kaleidoscope#add_button_stock `CANCEL `CANCEL;
  kaleidoscope#add_button_stock `APPLY `APPLY;
  let code = kaleidoscope#run () in
  let (new_fg, new_bg) = (fg_button#color, bg_button#color) in
  kaleidoscope#destroy ();
  let fg_r = Gdk.Color.red new_fg in
  let fg_g = Gdk.Color.green new_fg in
  let fg_b = Gdk.Color.blue new_fg in
  let bg_r = Gdk.Color.red new_bg in
  let bg_g = Gdk.Color.green new_bg in
  let bg_b = Gdk.Color.blue new_bg in
  if code = `APPLY then Some ((fg_r, fg_g, fg_b), (bg_r, bg_g, bg_b)) else None



let modulus ~current =
  let changer =
    GWindow.dialog
      ~title:"Set Modulus - Number Wall Viewer" () in
  let dialog_box =
    GPack.vbox
      ~border_width:10
      ~packing:changer#vbox#add () in
  let frame =
    GBin.frame
      ~label:"Modulus"
      ~packing:dialog_box#add () in
  let frame_box =
    GPack.vbox
      ~border_width:5
      ~packing:frame#add () in
  let adj =
    GData.adjustment
      ~value:(float_of_int current)
      ~lower:2.0
      ~upper:10001.0
      ~step_incr:1.0
      ~page_incr:100.0
      ~page_size:0.0 () in
  let _ =
    GEdit.spin_button
      ~adjustment:adj
      ~rate:0.0
      ~digits:0
      ~wrap:false
      ~packing:frame_box#add () in
  changer#add_button_stock `CANCEL `CANCEL;
  changer#add_button_stock `APPLY `APPLY;
  let code = changer#run () in
  let maybe_result = adj#value in
  changer#destroy ();
  if code = `APPLY then Some (int_of_float maybe_result) else None



let about () =
  let about_window =
    GWindow.about_dialog
      ~authors:["Jedidiah Barber"]
      ~comments:"A simple program to generate and visualise number walls."
      ~license:license_text
      ~title:"About - Number Wall Viewer"
      ~modal:true () in
  let destroy_me button = about_window#destroy () in
  ignore (about_window#connect#response ~callback:destroy_me);
  about_window#show ()