scintilla: Update scintilla with changeset 3662:1d1c06df8a2f using gtk+3
[anjuta-extras.git] / plugins / scintilla / scintilla / test-scintilla.cxx
blobe27b62de497d2c3d78c875e0f4f81d69adf0b41d
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <gtk/gtk.h>
7 #include <Platform.h>
8 #include <Scintilla.h>
9 #include <ScintillaWidget.h>
12 static int IntFromHexDigit(int ch) {
13 if ((ch >= '0') && (ch <= '9')) {
14 return ch - '0';
15 } else if (ch >= 'A' && ch <= 'F') {
16 return ch - 'A' + 10;
17 } else if (ch >= 'a' && ch <= 'f') {
18 return ch - 'a' + 10;
19 } else {
20 return 0;
24 static int IntFromHexByte(const char *hexByte) {
25 return IntFromHexDigit(hexByte[0]) * 16 + IntFromHexDigit(hexByte[1]);
28 typedef long Colour;
29 Colour ColourRGB(unsigned int red, unsigned int green, unsigned int blue)
31 return red | (green << 8) | (blue << 16);
34 static Colour ColourFromString(const char *s) {
35 if ((s != NULL) && (*s != '\0')) {
36 int r = IntFromHexByte(s + 1);
37 int g = IntFromHexByte(s + 3);
38 int b = IntFromHexByte(s + 5);
39 return ColourRGB(r, g, b);
40 } else {
41 return 0;
45 void SetOneStyle(GtkWidget *sci, int style, const char *s) {
46 char *val = strdup(s);
47 char *opt = val;
48 while (opt) {
49 char *cpComma = strchr(opt, ',');
50 if (cpComma)
51 *cpComma = '\0';
52 char *colon = strchr(opt, ':');
53 if (colon)
54 *colon++ = '\0';
55 if (0 == strcmp(opt, "italics"))
56 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETITALIC, style, 1);
57 if (0 == strcmp(opt, "notitalics"))
58 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETITALIC, style, 0);
59 if (0 == strcmp(opt, "bold"))
60 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETBOLD, style, 1);
61 if (0 == strcmp(opt, "notbold"))
62 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETBOLD, style, 0);
63 if (0 == strcmp(opt, "font"))
64 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETFONT, style, reinterpret_cast<long>(colon));
65 if (0 == strcmp(opt, "fore"))
66 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETFORE, style, ColourFromString(colon));
67 if (0 == strcmp(opt, "back"))
68 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETBACK, style, ColourFromString(colon));
69 if (0 == strcmp(opt, "size"))
70 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETSIZE, style, atoi(colon));
71 if (0 == strcmp(opt, "eolfilled"))
72 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETEOLFILLED, style, 1);
73 if (0 == strcmp(opt, "noteolfilled"))
74 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETEOLFILLED, style, 0);
75 if (0 == strcmp(opt, "underlined"))
76 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETUNDERLINE, style, 1);
77 if (0 == strcmp(opt, "notunderlined"))
78 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETUNDERLINE, style, 0);
79 if (0 == strcmp(opt, "case")) {
80 if (*colon == 'u') {
81 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCASE, style, SC_CASE_UPPER);
82 } else if (*colon == 'l') {
83 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCASE, style, SC_CASE_LOWER);
84 } else {
85 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCASE, style, SC_CASE_MIXED);
88 if (0 == strcmp(opt, "visible"))
89 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETVISIBLE, style, 1);
90 if (0 == strcmp(opt, "notvisible"))
91 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETVISIBLE, style, 0);
92 if (0 == strcmp(opt, "changeable"))
93 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCHANGEABLE, style, 1);
94 if (0 == strcmp(opt, "notchangeable"))
95 scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCHANGEABLE, style, 0);
96 if (cpComma)
97 opt = cpComma + 1;
98 else
99 opt = 0;
101 if (val)
102 delete []val;
103 // scintilla_send_message(SCINTILLA(sci), SCI_STYLESETCHARACTERSET, style, characterSet);
106 int main(int argc, char **argv)
108 GtkWidget *sci;
109 GtkWidget *win;
110 FILE *fp;
111 size_t read_bytes = 0;
112 char buffer[1025];
114 gtk_init (&argc, &argv);
115 if (argc != 2)
117 printf ("Usage: test-scintilla filename\n");
118 exit(1);
120 win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
121 sci = scintilla_new();
122 SetOneStyle (sci, STYLE_DEFAULT,
123 "font:bitstream vera sans mono,size:10,back:#EFEFEF");
124 scintilla_send_message(SCINTILLA(sci), SCI_SETWRAPMODE, SC_WRAP_WORD, 0);
125 scintilla_send_message(SCINTILLA(sci), SCI_SETHSCROLLBAR, 0, 0);
126 scintilla_send_message(SCINTILLA(sci), SCI_SETCODEPAGE, SC_CP_UTF8, 0);
127 scintilla_send_message(SCINTILLA(sci), SCI_SETMARGINWIDTHN, 0, 40);
128 scintilla_set_id(SCINTILLA(sci), 0);
129 gtk_container_add (GTK_CONTAINER(win), sci);
130 g_signal_connect (G_OBJECT (win), "delete-event",
131 G_CALLBACK (gtk_main_quit), NULL);
132 gtk_widget_set_size_request (GTK_WIDGET (win), 500, 600);
133 gtk_window_set_default_size (GTK_WINDOW (win), 500, 600);
134 gtk_widget_show (sci);
135 gtk_widget_show (win);
136 fp = fopen (argv[1], "r");
137 if (!fp)
139 perror ("Unable to open file\n");
140 exit(1);
142 while ((read_bytes = fread (buffer, 1, 1024, fp)) > 0)
144 buffer[read_bytes] = '\0';
145 scintilla_send_message(SCINTILLA(sci), SCI_APPENDTEXT, read_bytes, reinterpret_cast<long>(buffer));
147 fclose (fp);
148 gtk_main();
149 return 0;