forked from shoes/shoes-deprecated
/
app.c
591 lines (528 loc) · 13 KB
/
app.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
//
// shoes/app.c
// Abstract windowing for GTK, Quartz (OSX) and Win32.
//
#include <glib.h>
#include "shoes/app.h"
#include "shoes/internal.h"
#include "shoes/ruby.h"
#include "shoes/canvas.h"
#include "shoes/world.h"
#include "shoes/native.h"
static void
shoes_app_mark(shoes_app *app)
{
shoes_native_slot_mark(app->slot);
rb_gc_mark_maybe(app->title);
rb_gc_mark_maybe(app->location);
rb_gc_mark_maybe(app->canvas);
rb_gc_mark_maybe(app->keypresses);
rb_gc_mark_maybe(app->nestslot);
rb_gc_mark_maybe(app->nesting);
rb_gc_mark_maybe(app->extras);
rb_gc_mark_maybe(app->styles);
rb_gc_mark_maybe(app->groups);
rb_gc_mark_maybe(app->owner);
}
static void
shoes_app_free(shoes_app *app)
{
SHOE_FREE(app->slot);
cairo_destroy(app->scratch);
RUBY_CRITICAL(free(app));
}
VALUE
shoes_app_alloc(VALUE klass)
{
shoes_app *app = SHOE_ALLOC(shoes_app);
SHOE_MEMZERO(app, shoes_app, 1);
app->slot = SHOE_ALLOC(SHOES_SLOT_OS);
SHOE_MEMZERO(app->slot, SHOES_SLOT_OS, 1);
app->slot->owner = app;
app->started = FALSE;
app->owner = Qnil;
app->location = Qnil;
app->canvas = shoes_canvas_new(cShoes, app);
app->keypresses = rb_hash_new();
app->nestslot = Qnil;
app->nesting = rb_ary_new();
app->extras = rb_ary_new();
app->groups = Qnil;
app->styles = Qnil;
app->title = Qnil;
app->width = SHOES_APP_WIDTH;
app->height = SHOES_APP_HEIGHT;
app->minwidth = 0;
app->minheight = 0;
app->fullscreen = FALSE;
app->resizable = TRUE;
app->cursor = s_arrow;
app->scratch = cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1));
app->self = Data_Wrap_Struct(klass, shoes_app_mark, shoes_app_free, app);
rb_extend_object(app->self, cTypes);
return app->self;
}
VALUE
shoes_app_new(VALUE klass)
{
VALUE app = shoes_app_alloc(klass);
rb_ary_push(shoes_world->apps, app);
return app;
}
VALUE
shoes_apps_get(VALUE self)
{
return rb_ary_dup(shoes_world->apps);
}
static void
shoes_app_clear(shoes_app *app)
{
shoes_ele_remove_all(app->extras);
shoes_canvas_clear(app->canvas);
app->nestslot = Qnil;
app->groups = Qnil;
}
//
// When a window is finished, call this to delete it from the master
// list. Returns 1 if all windows are gone.
//
int
shoes_app_remove(shoes_app *app)
{
shoes_app_clear(app);
rb_ary_delete(shoes_world->apps, app->self);
return (RARRAY_LEN(shoes_world->apps) == 0);
}
shoes_code
shoes_app_resize(shoes_app *app, int width, int height)
{
app->width = width;
app->height = height;
shoes_native_app_resized(app);
return SHOES_OK;
}
VALUE
shoes_app_window(int argc, VALUE *argv, VALUE self, VALUE owner)
{
rb_arg_list args;
VALUE attr = Qnil;
VALUE app = shoes_app_new(self == cDialog ? cDialog : cApp);
shoes_app *app_t;
char *url = "/";
Data_Get_Struct(app, shoes_app, app_t);
switch (rb_parse_args(argc, argv, "h,s|h,", &args))
{
case 1:
attr = args.a[0];
break;
case 2:
url = RSTRING_PTR(args.a[0]);
attr = args.a[1];
break;
}
if (rb_block_given_p()) rb_iv_set(app, "@main_app", rb_block_proc());
app_t->owner = owner;
app_t->title = ATTR(attr, title);
app_t->fullscreen = RTEST(ATTR(attr, fullscreen));
app_t->resizable = (ATTR(attr, resizable) != Qfalse);
app_t->hidden = (ATTR(attr, hidden) == Qtrue);
shoes_app_resize(app_t, ATTR2(int, attr, width, SHOES_APP_WIDTH), ATTR2(int, attr, height, SHOES_APP_HEIGHT));
if (RTEST(ATTR(attr, minwidth)))
app_t->minwidth = (ATTR(attr, minwidth) - 1) / 2;
if (RTEST(ATTR(attr, minheight)))
app_t->minheight = (ATTR(attr, minheight) -1) / 2;
shoes_canvas_init(app_t->canvas, app_t->slot, attr, app_t->width, app_t->height);
if (shoes_world->mainloop)
shoes_app_open(app_t, url);
return app;
}
VALUE
shoes_app_main(int argc, VALUE *argv, VALUE self)
{
return shoes_app_window(argc, argv, self, Qnil);
}
VALUE
shoes_app_slot(VALUE app)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return app_t->nestslot;
}
VALUE
shoes_app_get_width(VALUE app)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return INT2NUM(app_t->width);
}
VALUE
shoes_app_get_height(VALUE app)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return INT2NUM(app_t->height);
}
VALUE
shoes_app_get_title(VALUE app)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return app_t->title;
}
VALUE
shoes_app_set_title(VALUE app, VALUE title)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return app_t->title = title;
}
VALUE
shoes_app_get_fullscreen(VALUE app)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
return app_t->fullscreen ? Qtrue : Qfalse;
}
VALUE
shoes_app_set_fullscreen(VALUE app, VALUE yn)
{
shoes_app *app_t;
Data_Get_Struct(app, shoes_app, app_t);
shoes_native_app_fullscreen(app_t, app_t->fullscreen = RTEST(yn));
return yn;
}
void
shoes_app_title(shoes_app *app, VALUE title)
{
char *msg;
if (!NIL_P(title))
app->title = title;
else
app->title = rb_str_new2(SHOES_APPNAME);
msg = RSTRING_PTR(app->title);
shoes_native_app_title(app, msg);
}
shoes_code
shoes_app_start(VALUE allapps, char *uri)
{
int i;
shoes_code code;
shoes_app *app;
for (i = 0; i < RARRAY_LEN(allapps); i++)
{
VALUE appobj2 = rb_ary_entry(allapps, i);
Data_Get_Struct(appobj2, shoes_app, app);
if (!app->started)
{
code = shoes_app_open(app, uri);
app->started = TRUE;
if (code != SHOES_OK)
return code;
}
}
return shoes_app_loop();
}
shoes_code
shoes_app_open(shoes_app *app, char *path)
{
shoes_code code = SHOES_OK;
int dialog = (rb_obj_class(app->self) == cDialog);
code = shoes_native_app_open(app, path, dialog);
if (code != SHOES_OK)
return code;
shoes_app_title(app, app->title);
if (app->slot != NULL) shoes_native_slot_reset(app->slot);
shoes_slot_init(app->canvas, app->slot, 0, 0, app->width, app->height, TRUE, TRUE);
code = shoes_app_goto(app, path);
if (code != SHOES_OK)
return code;
if (!app->hidden)
shoes_native_app_show(app);
return code;
}
shoes_code
shoes_app_loop()
{
if (shoes_world->mainloop)
return SHOES_OK;
shoes_world->mainloop = TRUE;
INFO("RUNNING LOOP.\n");
shoes_native_loop();
return SHOES_OK;
}
typedef struct
{
shoes_app *app;
VALUE canvas;
VALUE block;
char ieval;
VALUE args;
} shoes_exec;
#ifdef RUBY_1_9
struct METHOD {
VALUE oclass; /* class that holds the method */
VALUE rklass; /* class of the receiver */
VALUE recv;
ID id, oid;
void *body; /* NODE *body; */
};
#else
struct METHOD {
VALUE klass, rklass;
VALUE recv;
ID id, oid;
int safe_level;
NODE *body;
};
#endif
static VALUE
rb_unbound_get_class(VALUE method)
{
struct METHOD *data;
Data_Get_Struct(method, struct METHOD, data);
return data->rklass;
}
static VALUE
shoes_app_run(VALUE rb_exec)
{
shoes_exec *exec = (shoes_exec *)rb_exec;
rb_ary_push(exec->app->nesting, exec->canvas);
if (exec->ieval)
{
VALUE obj;
obj = mfp_instance_eval(exec->app->self, exec->block);
return obj;
}
else
{
int i;
VALUE vargs[10];
for (i = 0; i < RARRAY_LEN(exec->args); i++)
vargs[i] = rb_ary_entry(exec->args, i);
return rb_funcall2(exec->block, s_call, RARRAY_LEN(exec->args), vargs);
}
}
static VALUE
shoes_app_exception(VALUE rb_exec, VALUE e)
{
shoes_exec *exec = (shoes_exec *)rb_exec;
rb_ary_clear(exec->app->nesting);
shoes_canvas_error(exec->canvas, e);
return Qnil;
}
shoes_code
shoes_app_visit(shoes_app *app, char *path)
{
shoes_exec exec;
shoes_canvas *canvas;
VALUE meth;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
canvas->slot->scrolly = 0;
shoes_native_slot_clear(canvas);
shoes_app_clear(app);
shoes_app_reset_styles(app);
meth = rb_funcall(cShoes, s_run, 1, app->location = rb_str_new2(path));
VALUE app_block = rb_iv_get(app->self, "@main_app");
if (!NIL_P(app_block))
rb_ary_store(meth, 0, app_block);
exec.app = app;
exec.block = rb_ary_entry(meth, 0);
exec.args = rb_ary_entry(meth, 1);
if (rb_obj_is_kind_of(exec.block, rb_cUnboundMethod)) {
VALUE klass = rb_unbound_get_class(exec.block);
exec.canvas = app->nestslot = shoes_slot_new(klass, ssNestSlot, app->canvas);
exec.block = rb_funcall(exec.block, s_bind, 1, exec.canvas);
exec.ieval = 0;
rb_ary_push(canvas->contents, exec.canvas);
} else {
exec.canvas = app->nestslot = app->canvas;
exec.ieval = 1;
}
rb_rescue2(CASTHOOK(shoes_app_run), (VALUE)&exec, CASTHOOK(shoes_app_exception), (VALUE)&exec, rb_cObject, 0);
rb_ary_clear(exec.app->nesting);
return SHOES_OK;
}
shoes_code
shoes_app_paint(shoes_app *app)
{
shoes_canvas_paint(app->canvas);
return SHOES_OK;
}
shoes_code
shoes_app_motion(shoes_app *app, int x, int y)
{
app->mousex = x; app->mousey = y;
shoes_canvas_send_motion(app->canvas, x, y, Qnil);
return SHOES_OK;
}
shoes_code
shoes_app_click(shoes_app *app, int button, int x, int y)
{
app->mouseb = button;
shoes_canvas_send_click(app->canvas, button, x, y);
return SHOES_OK;
}
shoes_code
shoes_app_release(shoes_app *app, int button, int x, int y)
{
app->mouseb = 0;
shoes_canvas_send_release(app->canvas, button, x, y);
return SHOES_OK;
}
shoes_code
shoes_app_wheel(shoes_app *app, ID dir, int x, int y)
{
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
if (canvas->slot->vscroll)
{
if (dir == s_up)
shoes_slot_scroll_to(canvas, -16, 1);
else if (dir == s_down)
shoes_slot_scroll_to(canvas, 16, 1);
}
shoes_canvas_send_wheel(app->canvas, dir, x, y);
return SHOES_OK;
}
shoes_code
shoes_app_keydown(shoes_app *app, VALUE key)
{
if (!RTEST(rb_hash_aref(app->keypresses, key))) {
rb_hash_aset(app->keypresses, key, Qtrue);
shoes_canvas_send_keydown(app->canvas, key);
}
return SHOES_OK;
}
shoes_code
shoes_app_keypress(shoes_app *app, VALUE key)
{
if (key == symAltSlash)
rb_eval_string("Shoes.show_log");
else if (key == symAltQuest)
rb_eval_string("Shoes.show_manual");
else if (key == symAltDot)
rb_eval_string("Shoes.show_selector");
else
shoes_canvas_send_keypress(app->canvas, key);
return SHOES_OK;
}
shoes_code
shoes_app_keyup(shoes_app *app, VALUE key)
{
rb_hash_aset(app->keypresses, key, Qfalse);
shoes_canvas_send_keyup(app->canvas, key);
return SHOES_OK;
}
VALUE
shoes_sys(char *cmd, int detach)
{
if (detach)
return rb_funcall(rb_mKernel, rb_intern("system"), 1, rb_str_new2(cmd));
else
return rb_funcall(rb_mKernel, '`', 1, rb_str_new2(cmd));
}
shoes_code
shoes_app_goto(shoes_app *app, char *path)
{
shoes_code code = SHOES_OK;
const char http_scheme[] = "http://";
if (strlen(path) > strlen(http_scheme) && strncmp(http_scheme, path, strlen(http_scheme)) == 0) {
shoes_browser_open(path);
} else {
code = shoes_app_visit(app, path);
if (code == SHOES_OK)
{
shoes_app_motion(app, app->mousex, app->mousey);
shoes_slot_repaint(app->slot);
}
}
return code;
}
shoes_code
shoes_slot_repaint(SHOES_SLOT_OS *slot)
{
shoes_native_slot_paint(slot);
return SHOES_OK;
}
static void
shoes_style_set(VALUE styles, VALUE klass, VALUE k, VALUE v)
{
VALUE hsh = rb_hash_aref(styles, klass);
if (NIL_P(hsh))
rb_hash_aset(styles, klass, hsh = rb_hash_new());
rb_hash_aset(hsh, k, v);
}
#define STYLE(klass, k, v) \
shoes_style_set(app->styles, klass, \
ID2SYM(rb_intern("" # k)), rb_str_new2("" # v))
void
shoes_app_reset_styles(shoes_app *app)
{
app->styles = rb_hash_new();
STYLE(cBanner, size, 48);
STYLE(cTitle, size, 34);
STYLE(cSubtitle, size, 26);
STYLE(cTagline, size, 18);
STYLE(cCaption, size, 14);
STYLE(cPara, size, 12);
STYLE(cInscription, size, 10);
STYLE(cCode, family, monospace);
STYLE(cDel, strikethrough, single);
STYLE(cEm, emphasis, italic);
STYLE(cIns, underline, single);
STYLE(cLink, underline, single);
STYLE(cLink, stroke, #06E);
STYLE(cLinkHover, underline, single);
STYLE(cLinkHover, stroke, #039);
STYLE(cStrong, weight, bold);
STYLE(cSup, rise, 10);
STYLE(cSup, size, x-small);
STYLE(cSub, rise, -10);
STYLE(cSub, size, x-small);
}
void
shoes_app_style(shoes_app *app, VALUE klass, VALUE hsh)
{
long i;
VALUE keys = rb_funcall(hsh, s_keys, 0);
for ( i = 0; i < RARRAY_LEN(keys); i++ )
{
VALUE key = rb_ary_entry(keys, i);
VALUE val = rb_hash_aref(hsh, key);
if (!SYMBOL_P(key)) key = rb_str_intern(key);
shoes_style_set(app->styles, klass, key, val);
}
}
VALUE
shoes_app_close_window(shoes_app *app)
{
shoes_native_app_close(app);
return Qnil;
}
VALUE
shoes_app_location(VALUE self)
{
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
return app->location;
}
VALUE
shoes_app_is_started(VALUE self)
{
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
return app->started ? Qtrue : Qfalse;
}
VALUE
shoes_app_contents(VALUE self)
{
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
return shoes_canvas_contents(app->canvas);
}
VALUE
shoes_app_quit(VALUE self)
{
shoes_native_quit();
return self;
}