forked from coolwanglu/vim.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_web.c
847 lines (724 loc) · 15 KB
/
gui_web.c
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
* Web port by Lu Wang
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* gui_web.c
* gui functions for vim.js
*
* Copyright (c) 2013 Lu Wang <[email protected]>
*/
#include "vim.h"
void
gui_mch_mousehide(int hide)
{
}
/*
* ------------------------------------------------------------
* GUI_MCH functionality
* ------------------------------------------------------------
*/
/*
* Parse the GUI related command-line arguments. Any arguments used are
* deleted from argv, and *argc is decremented accordingly. This is called
* when vim is started, whether or not the GUI has been started.
*/
void
gui_mch_prepare(int *argc, char **argv)
{
// nothing to do
}
#ifndef ALWAYS_USE_GUI
/*
* Check if the GUI can be started. Called before gvimrc is sourced.
* Return OK or FAIL.
*
* Not needed by vim.js
*/
int
gui_mch_init_check(void)
{
return OK;
}
#endif
/*
* Initialise the GUI. Create all the windows, set up all the call-backs
* etc.
*/
int
gui_mch_init(void)
{
vimjs_init();
gui.border_offset = 0;
gui.border_width = 0;
gui.scrollbar_width = 0;
gui.scrollbar_height = 0;
gui.back_pixel = gui_get_color((char_u*)"black");
gui.norm_pixel = gui_get_color((char_u*)"white");
set_normal_colors();
gui_check_colors();
gui.def_back_pixel = gui.back_pixel;
gui.def_norm_pixel = gui.norm_pixel;
gui.in_focus = TRUE;
Rows = vimjs_get_window_height() / vimjs_get_char_height();
Columns = vimjs_get_window_width() / vimjs_get_char_width();
return OK;
}
/*
* Called when the foreground or background color has been changed.
*/
void
gui_mch_new_colors(void)
{
// Nothing to do
}
/*
* Open the GUI window which was created by a call to gui_mch_init().
*/
int
gui_mch_open(void)
{
return OK;
}
void
gui_mch_exit(int rc)
{
}
/*
* Get the position of the top left corner of the window.
*/
int
gui_mch_get_winpos(int *x, int *y)
{
*x = *y = 0;
return OK;
}
/*
* Set the position of the top left corner of the window to the given
* coordinates.
*/
void
gui_mch_set_winpos(int x, int y)
{
// Nothing to do
// we never change winpos
}
void
gui_mch_set_shellsize(
int width,
int height,
int min_width,
int min_height,
int base_width,
int base_height,
int direction)
{
vimjs_resize(width, height);
}
/*
* Get the screen dimensions.
*/
void
gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
{
*screen_w = vimjs_get_window_width();
*screen_h = vimjs_get_window_height();
}
/*
* Initialise vim to use the font with the given name. Return FAIL if the font
* could not be loaded, OK otherwise.
*/
int
gui_mch_init_font(char_u *font_name, int fontset)
{
vimjs_init_font((char*)font_name);
gui.char_width = vimjs_get_char_width();
gui.char_height = vimjs_get_char_height();
gui.char_ascent = gui.char_height;
char_u buf[100];
gui.norm_font = vim_strsave(font_name);
vim_strncpy(buf, "bold ", 99); // should report 1 less to vim_strncpy
vim_strcat(buf, font_name, 100);
gui.bold_font = vim_strsave(buf);
vim_strncpy(buf, "italic ", 99); // should report 1 less to vim_strncpy
vim_strcat(buf, font_name, 100);
gui.ital_font = vim_strsave(buf);
vim_strncpy(buf, "bold italic ", 99); // should report 1 less to vim_strncpy
vim_strcat(buf, font_name, 100);
gui.boldital_font = vim_strsave(buf);
int w,h;
w = vimjs_get_window_width();
h = vimjs_get_window_height();
gui_resize_shell(w, h);
return OK;
}
/*
* Adjust gui.char_height (after 'linespace' was changed).
*/
int
gui_mch_adjust_charheight(void)
{
//TODO
return FAIL;
}
/*
* Get a font structure for highlighting.
*/
GuiFont
gui_mch_get_font(char_u *name, int giveErrorIfMissing)
{
if(vimjs_check_font((char*)name))
return (char*)vim_strsave(name);
if (giveErrorIfMissing)
EMSG2(_(e_font), name);
return NOFONT;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Return the name of font "font" in allocated memory.
* Don't know how to get the actual name, thus use the provided name.
*/
char_u *
gui_mch_get_fontname(GuiFont font, char_u *name)
{
if (font != NOFONT)
{
return vim_strsave((char_u*)font);
}
return NULL;
}
#endif
/*
* Set the current text font.
*/
void
gui_mch_set_font(GuiFont font)
{
vimjs_set_font(font);
}
/*
* If a font is not going to be used, free its structure.
*/
void
gui_mch_free_font(font)
GuiFont font;
{
vim_free(font);
}
/*
* Return the Pixel value (color) for the given color name.
* Return INVALCOLOR when failed.
*/
guicolor_T
gui_mch_get_color(char_u *name)
{
if(vimjs_is_valid_color((char*)name))
return (guicolor_T)vimjs_get_rgb((char*)name);
return INVALCOLOR;
}
/*
* Set the current text foreground color.
*/
void
gui_mch_set_fg_color(guicolor_T color)
{
vimjs_set_fg_color(color);
}
/*
* Set the current text background color.
*/
void
gui_mch_set_bg_color(guicolor_T color)
{
vimjs_set_bg_color(color);
}
/*
* Set the current text special color.
*/
void
gui_mch_set_sp_color(guicolor_T color)
{
vimjs_set_sp_color(color);
}
void
gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
{
vimjs_draw_string(row, col, s, len, flags);
}
/*
* Return OK if the key with the termcap name "name" is supported.
*/
int
gui_mch_haskey(char_u *name)
{
return vimjs_haskey((char*)name) ? OK : FAIL;
}
void
gui_mch_beep(void)
{
vimjs_beep();
}
void
gui_mch_flash(int msec)
{
vimjs_flash(msec);
}
/*
* Invert a rectangle from row r, column c, for nr rows and nc columns.
*/
void
gui_mch_invert_rectangle(int r, int c, int nr, int nc)
{
vimjs_invert_rectangle(r, c, nr, nc);
}
/*
* Iconify the GUI window.
*/
void
gui_mch_iconify(void)
{
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Bring the Vim window to the foreground.
*/
void
gui_mch_set_foreground(void)
{
// Nothing to do
}
#endif
/*
* Draw a cursor without focus.
*/
void
gui_mch_draw_hollow_cursor(guicolor_T color)
{
gui_mch_set_fg_color(color);
vimjs_draw_hollow_cursor(gui.row, gui.col);
}
/*
* Draw part of a cursor, only w pixels wide, and h pixels high.
*/
void
gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
{
gui_mch_set_fg_color(color);
vimjs_draw_part_cursor(gui.row, gui.col, w, h);
}
/*
* Catch up with any queued X events. This may put keyboard input into the
* input buffer, call resize call-backs, trigger timers etc. If there is
* nothing in the X event queue (& no timers pending), then we return
* immediately.
*/
void
gui_mch_update(void)
{
emscripten_sleep(1);
}
/*
* GUI input routine called by gui_wait_for_chars(). Waits for a character
* from the keyboard.
* wtime == -1 Wait forever.
* wtime == 0 This should never happen.
* wtime > 0 Wait wtime milliseconds for a character.
* Returns OK if a character was found to be available within the given time,
* or FAIL otherwise.
*/
int
gui_mch_wait_for_chars(int wtime)
{
if(input_available())
return OK;
int t = 0;
int step = 10;
while(1)
{
emscripten_sleep(step);
if(input_available())
return OK;
t += step;
if((wtime > -1) && (t >= wtime))
return FAIL;
}
}
void
gui_web_handle_key(int code, int modifiers, char_u special1, char_u special2)
{
char_u buf[64];
int buf_len = 0;
int is_special = (special1 != 0);
if(is_special)
{
code = TO_SPECIAL(special1, special2);
code = simplify_key(code, &modifiers);
}
else
{
if(code == 'c' && (modifiers & MOD_MASK_CTRL))
got_int = TRUE;
if(!IS_SPECIAL(code))
{
code = simplify_key(code, &modifiers);
code = extract_modifiers(code, &modifiers);
if(code == CSI)
code = K_CSI;
if(IS_SPECIAL(code))
is_special = TRUE;
}
}
if(modifiers)
{
buf[buf_len++] = CSI;
buf[buf_len++] = KS_MODIFIER;
buf[buf_len++] = modifiers;
}
if(is_special && IS_SPECIAL(code))
{
buf[buf_len++] = CSI;
buf[buf_len++] = K_SECOND(code);
buf[buf_len++] = K_THIRD(code);
}
else
{
// TODO: support Unicode
buf[buf_len++] = code;
}
if(buf_len)
add_to_input_buf(buf, buf_len);
}
/*
* Output routines.
*/
/* Flush any output to the screen */
void
gui_mch_flush(void)
{
// Nothing to do
}
/*
* Clear a rectangular region of the screen from text pos (row1, col1) to
* (row2, col2) inclusive.
*/
void
gui_mch_clear_block(int row1, int col1, int row2, int col2)
{
gui_mch_set_bg_color(gui.back_pixel);
vimjs_clear_block(row1, col1, row2, col2);
}
/*
* Clear the whole text window.
*/
void
gui_mch_clear_all(void)
{
gui_mch_set_bg_color(gui.back_pixel);
vimjs_clear_all();
}
/*
* Delete the given number of lines from the given row, scrolling up any
* text further down within the scroll region.
*/
void
gui_mch_delete_lines(int row, int num_lines)
{
gui_mch_set_bg_color(gui.back_pixel);
vimjs_delete_lines(num_lines, row, gui.scroll_region_bot, gui.scroll_region_left, gui.scroll_region_right);
}
/*
* Insert the given number of lines before the given row, scrolling down any
* following text within the scroll region.
*/
void
gui_mch_insert_lines(int row, int num_lines)
{
gui_mch_set_bg_color(gui.back_pixel);
vimjs_insert_lines(num_lines, row, gui.scroll_region_bot, gui.scroll_region_left, gui.scroll_region_right);
}
void
gui_mch_set_text_area_pos(int x, int y, int w, int h)
{
// Nothing to do
}
void
clip_mch_request_selection(VimClipboard *cbd)
{
}
void
clip_mch_lose_selection(VimClipboard *cbd)
{
}
int
clip_mch_own_selection(VimClipboard *cbd)
{
return OK;
}
/*
* Send the current selection to the clipboard.
*/
void
clip_mch_set_selection(VimClipboard *cbd)
{
}
/*
* Menu stuff.
*/
void
gui_mch_enable_menu(int flag)
{
}
void
gui_mch_set_menu_pos(int x, int y, int w, int h)
{
}
/*
* Add a sub menu to the menu bar.
*/
void
gui_mch_add_menu(vimmenu_T *menu, int idx)
{
}
/*
* Add a menu item to a menu
*/
void
gui_mch_add_menu_item(vimmenu_T *menu, int idx)
{
}
void
gui_mch_toggle_tearoffs(int enable)
{
}
/*
* Destroy the machine specific menu widget.
*/
void
gui_mch_destroy_menu(vimmenu_T *menu)
{
}
/*
* Make a menu either grey or not grey.
*/
void
gui_mch_menu_grey(vimmenu_T *menu, int grey)
{
}
/*
* Make menu item hidden or not hidden
*/
void
gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
{
}
/*
* This is called after setting all the menus to grey/hidden or not.
*/
void
gui_mch_draw_menubar(void)
{
}
/*
* Scrollbar stuff.
*/
void
gui_mch_enable_scrollbar(
scrollbar_T *sb,
int flag)
{
}
void
gui_mch_set_scrollbar_thumb(
scrollbar_T *sb,
long val,
long size,
long max)
{
}
void
gui_mch_set_scrollbar_pos(
scrollbar_T *sb,
int x,
int y,
int w,
int h)
{
}
void
gui_mch_create_scrollbar(
scrollbar_T *sb,
int orient) /* SBAR_VERT or SBAR_HORIZ */
{
}
void
gui_mch_destroy_scrollbar(scrollbar_T *sb)
{
}
/*
* Cursor blink functions.
*
* This is a simple state machine:
* BLINK_NONE not blinking at all
* BLINK_OFF blinking, cursor is not shown
* BLINK_ON blinking, cursor is shown
*/
void
gui_mch_set_blinking(long wait, long on, long off)
{
}
/*
* Stop the cursor blinking. Show the cursor if it wasn't shown.
*/
void
gui_mch_stop_blink(void)
{
gui_update_cursor(TRUE, FALSE);
}
/*
* Start the cursor blinking. If it was already blinking, this restarts the
* waiting time and shows the cursor.
*/
void
gui_mch_start_blink(void)
{
gui_update_cursor(TRUE, FALSE);
}
/*
* Return the RGB value of a pixel as long.
*/
long_u
gui_mch_get_rgb(guicolor_T pixel)
{
return (long_u)pixel;
}
#ifdef FEAT_BROWSE
/*
* Pop open a file browser and return the file selected, in allocated memory,
* or NULL if Cancel is hit.
* saving - TRUE if the file will be saved to, FALSE if it will be opened.
* title - Title message for the file browser dialog.
* dflt - Default name of file.
* ext - Default extension to be added to files without extensions.
* initdir - directory in which to open the browser (NULL = current dir)
* filter - Filter for matched files to choose from.
* Has a format like this:
* "C Files (*.c)\0*.c\0"
* "All Files\0*.*\0\0"
* If these two strings were concatenated, then a choice of two file
* filters will be selectable to the user. Then only matching files will
* be shown in the browser. If NULL, the default allows all files.
*
* *NOTE* - the filter string must be terminated with TWO nulls.
*/
char_u *
gui_mch_browse(
int saving,
char_u *title,
char_u *dflt,
char_u *ext,
char_u *initdir,
char_u *filter)
{
char buf[4096];
vimjs_browse(buf, 4096, saving, (char*)dflt, (char*)initdir);
if(*buf == 0) {
return NULL;
}
return vim_strsave((char_u*)buf);
}
#endif /* FEAT_BROWSE */
#ifdef FEAT_GUI_DIALOG
/*
* Stuff for dialogues
*/
/*
* Create a dialogue dynamically from the parameter strings.
* type = type of dialogue (question, alert, etc.)
* title = dialogue title. may be NULL for default title.
* message = text to display. Dialogue sizes to accommodate it.
* buttons = '\n' separated list of button captions, default first.
* dfltbutton = number of default button.
*
* This routine returns 1 if the first button is pressed,
* 2 for the second, etc.
*
* 0 indicates Esc was pressed.
* -1 for unexpected error
*
* If stubbing out this fn, return 1.
*/
int
gui_mch_dialog(
int type,
char_u *title,
char_u *message,
char_u *buttons,
int dfltbutton,
char_u *textfield,
int ex_cmd)
{
return -1;
}
#endif /* FEAT_DIALOG_GUI */
/*
* Get current mouse coordinates in text window.
*/
void
gui_mch_getmouse(int *x, int *y)
{
*x = *y = 0;
}
void
gui_mch_setmouse(int x, int y)
{
}
void
gui_mch_show_popupmenu(vimmenu_T *menu)
{
}
#ifdef FEAT_TITLE
/*
* Set the window title and icon.
*/
void
gui_mch_settitle(char_u *title, char_u *icon)
{
}
#endif
#if defined(FEAT_GUI_TABLINE)
/*
* Show or hide the tabline.
*/
void
gui_mch_show_tabline(int showit)
{
}
/*
* Return TRUE when tabline is displayed.
*/
int
gui_mch_showing_tabline(void)
{
return FALSE;
}
/*
* Update the labels of the tabline.
*/
void
gui_mch_update_tabline(void)
{
}
/*
* Set the current tab to "nr". First tab is 1.
*/
void
gui_mch_set_curtab(nr)
int nr;
{
}
#endif // FEAT_GUI_TABLINE