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
|
/*
* File : workbench.c
* This file is part of RTGUI in RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2009-10-04 Bernard first version
* 2010-09-24 Bernard fix workbench destroy issue
*/
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/workbench.h>
static void _rtgui_workbench_constructor(rtgui_workbench_t *workbench)
{
/* set event handler */
rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), rtgui_workbench_event_handler);
/* set attributes */
workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT;
workbench->panel = RT_NULL;
workbench->title = RT_NULL;
workbench->current_view = RT_NULL;
workbench->modal_code = RTGUI_MODAL_OK;
workbench->modal_widget = RT_NULL;
}
static void _rtgui_workbench_destructor(rtgui_workbench_t *workbench)
{
RT_ASSERT(workbench != RT_NULL);
if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
{
struct rtgui_event_panel_detach edetach;
RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
/* detach from panel */
edetach.panel = workbench->panel;
/* send PANEL DETACH to server */
if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
return;
RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
}
/* release title */
rt_free(workbench->title);
workbench->title = RT_NULL;
}
DEFINE_CLASS_TYPE(workbench, "workbench",
RTGUI_TOPLEVEL_TYPE,
_rtgui_workbench_constructor,
_rtgui_workbench_destructor,
sizeof(struct rtgui_workbench));
rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title)
{
struct rtgui_workbench* workbench;
/* the server thread id */
rt_thread_t server = rtgui_thread_get_server();
if (server == RT_NULL)
{
rt_kprintf("can't find rtgui server\n");
return RT_NULL;
}
/* create workbench */
workbench = (rtgui_workbench_t*) rtgui_widget_create (RTGUI_WORKBENCH_TYPE);
if (workbench != RT_NULL)
{
/* the buffer uses to receive event */
union
{
struct rtgui_event_panel_attach ecreate;
struct rtgui_event_panel_info epanel;
char buffer[256]; /* use to recv other information */
} event;
/* set workbench title */
workbench->title = (unsigned char*)rt_strdup((char*)title);
/* create application in server */
RTGUI_EVENT_PANEL_ATTACH_INIT(&(event.ecreate));
/* set the panel name and workbench */
rt_strncpy(event.ecreate.panel_name, panel_name, RTGUI_NAME_MAX);
event.ecreate.workbench = workbench;
/* send PANEL ATTACH to server */
if (rtgui_thread_send_sync(server,
&(event.ecreate.parent), sizeof(struct rtgui_event_panel_attach)) != RTGUI_STATUS_OK)
{
return RT_NULL;
}
/* get PANEL INFO */
rtgui_thread_recv_filter(RTGUI_EVENT_PANEL_INFO, &(event.epanel.parent), sizeof(event));
/* set panel */
workbench->panel = (struct rtgui_panel*)event.epanel.panel;
/* connected */
RTGUI_TOPLEVEL(workbench)->server = server;
/* set extent of workbench */
rtgui_widget_set_rect(RTGUI_WIDGET(workbench), &(event.epanel.extent));
/* set workbench in thread */
rtgui_thread_set_widget(RTGUI_WIDGET(workbench));
}
return workbench;
}
void rtgui_workbench_destroy(rtgui_workbench_t* workbench)
{
RT_ASSERT(workbench != RT_NULL);
if (workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED)
{
rtgui_widget_destroy(RTGUI_WIDGET(workbench));
}
else
{
/* just close workbench */
rtgui_workbench_close(workbench);
}
}
void rtgui_workbench_close(rtgui_workbench_t* workbench)
{
/* detach workbench in server */
if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
{
struct rtgui_event_panel_detach edetach;
RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
/* detach from panel */
edetach.panel = workbench->panel;
edetach.workbench = workbench;
/* send PANEL DETACH to server */
if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
return;
RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
}
/* set status to close */
workbench->flag |= RTGUI_WORKBENCH_FLAG_CLOSED;
}
void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
{
RT_ASSERT(workbench != RT_NULL);
workbench->flag = flag;
}
rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
{
rt_err_t result;
rtgui_thread_t* tid;
struct rtgui_event* event;
tid = rtgui_thread_self();
RT_ASSERT(tid != RT_NULL);
/* point to event buffer */
event = (struct rtgui_event*)tid->event_buffer;
if (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
{
/* event loop for modal mode shown view */
while (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
{
if (tid->on_idle != RT_NULL)
{
result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
if (result == RT_EOK)
RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
else if (result == -RT_ETIMEOUT)
tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
}
else
{
result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
if (result == RT_EOK)
RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
}
}
}
else
{
/* show workbench firstly */
rtgui_workbench_show(workbench);
while (!(workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED))
{
if (tid->on_idle != RT_NULL)
{
result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
if (result == RT_EOK)
RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
else if (result == -RT_ETIMEOUT)
tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
}
else
{
result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
if (result == RT_EOK)
RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
}
}
}
return RT_TRUE;
}
rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench)
{
RT_ASSERT(workbench != RT_NULL);
if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
{
struct rtgui_event_panel_show eraise;
RTGUI_EVENT_PANEL_SHOW_INIT(&eraise);
eraise.workbench = workbench;
eraise.panel = workbench->panel;
if (rtgui_thread_send_sync(workbench->parent.server, RTGUI_EVENT(&eraise),
sizeof(struct rtgui_event_panel_show)) != RT_EOK)
return -RT_ERROR;
RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(workbench));
rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
}
else return -RT_ERROR;
return RT_EOK;
}
rt_err_t rtgui_workbench_hide(rtgui_workbench_t* workbench)
{
if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
{
struct rtgui_event_panel_hide ehide;
RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
RT_ASSERT(workbench != RT_NULL);
if (workbench->parent.server == RT_NULL) return -RT_ERROR;
ehide.panel = workbench->panel;
if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server, RTGUI_EVENT(&ehide),
sizeof(struct rtgui_event_panel_hide)) != RT_EOK)
return -RT_ERROR;
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
}
return RT_EOK;
}
rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
{
struct rtgui_workbench* workbench = (struct rtgui_workbench*)widget;
switch (event->type)
{
case RTGUI_EVENT_PANEL_DETACH:
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
return RT_TRUE;
case RTGUI_EVENT_PANEL_SHOW:
/* show workbench in server */
rtgui_workbench_show(workbench);
break;
case RTGUI_EVENT_PANEL_HIDE:
/* hide widget */
RTGUI_WIDGET_HIDE(widget);
break;
case RTGUI_EVENT_MOUSE_MOTION:
{
struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
/* check the destination window */
if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
{
RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
}
else
{
/* let viewer to handle it */
rtgui_view_t* view = workbench->current_view;
if (view != RT_NULL &&
RTGUI_WIDGET(view)->event_handler != RT_NULL)
{
RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
}
}
}
break;
case RTGUI_EVENT_MOUSE_BUTTON:
{
struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
/* check whether has widget which handled mouse event before */
if (RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) != RT_NULL)
{
struct rtgui_event_mouse* emouse;
emouse = (struct rtgui_event_mouse*)event;
RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->event_handler(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench), event);
if (rtgui_rect_contains_point(&(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->extent),
emouse->x, emouse->y) == RT_EOK)
{
RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
break; /* mouse event is inside of widget, do not handle it anymore */
}
/* clean last mouse event handled widget */
RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
}
/* check the destination window */
if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
{
RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
}
else
{
if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
{
/* let modal widget to handle it */
if (workbench->modal_widget != RT_NULL &&
workbench->modal_widget->event_handler != RT_NULL)
{
workbench->modal_widget->event_handler(workbench->modal_widget, event);
}
}
else
{
/* let viewer to handle it */
rtgui_view_t* view = workbench->current_view;
if (view != RT_NULL &&
RTGUI_WIDGET(view)->event_handler != RT_NULL)
{
RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
}
}
}
}
break;
case RTGUI_EVENT_KBD:
{
struct rtgui_event_kbd* kbd = (struct rtgui_event_kbd*)event;
struct rtgui_toplevel* top = RTGUI_TOPLEVEL(kbd->wid);
/* check the destination window */
if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
{
RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
}
else
{
if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
{
/* let modal widget to handle it */
if (workbench->modal_widget != RT_NULL &&
workbench->modal_widget->event_handler != RT_NULL)
{
workbench->modal_widget->event_handler(workbench->modal_widget, event);
}
}
else
{
if (RTGUI_CONTAINER(widget)->focused == widget)
{
/* set focused widget to the current view */
if (workbench->current_view != RT_NULL)
rtgui_widget_focus(RTGUI_WIDGET(RTGUI_CONTAINER(workbench->current_view)->focused));
}
return rtgui_toplevel_event_handler(widget, event);
}
}
}
break;
case RTGUI_EVENT_PAINT:
{
struct rtgui_event_paint* epaint = (struct rtgui_event_paint*)event;
struct rtgui_toplevel* top = RTGUI_TOPLEVEL(epaint->wid);
/* check the destination window */
if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
{
RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
}
else
{
rtgui_view_t* view;
/* un-hide workbench */
RTGUI_WIDGET_UNHIDE(widget);
/* paint a view */
view = workbench->current_view;
if (view != RT_NULL)
{
/* remake a paint event */
RTGUI_EVENT_PAINT_INIT(epaint);
epaint->wid = RT_NULL;
/* send this event to the view */
if (RTGUI_WIDGET(view)->event_handler != RT_NULL)
{
RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
}
}
else
{
struct rtgui_dc* dc;
struct rtgui_rect rect;
dc = rtgui_dc_begin_drawing(widget);
rtgui_widget_get_rect(widget, &rect);
rtgui_dc_fill_rect(dc, &rect);
rtgui_dc_end_drawing(dc);
}
}
}
break;
case RTGUI_EVENT_CLIP_INFO:
{
struct rtgui_event_clip_info* eclip = (struct rtgui_event_clip_info*)event;
struct rtgui_widget* dest_widget = RTGUI_WIDGET(eclip->wid);
if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
{
dest_widget->event_handler(dest_widget, event);
}
else
{
return rtgui_toplevel_event_handler(widget, event);
}
}
break;
case RTGUI_EVENT_WIN_CLOSE:
case RTGUI_EVENT_WIN_ACTIVATE:
case RTGUI_EVENT_WIN_DEACTIVATE:
{
struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
struct rtgui_widget* dest_widget = RTGUI_WIDGET(wevent->wid);
if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
{
dest_widget->event_handler(dest_widget, event);
}
}
break;
case RTGUI_EVENT_WIN_MOVE:
{
struct rtgui_event_win_move* wevent = (struct rtgui_event_win_move*)event;
struct rtgui_toplevel* top = RTGUI_TOPLEVEL(wevent->wid);
if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
{
RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
}
}
break;
default:
return rtgui_toplevel_event_handler(widget, event);
}
return RT_TRUE;
}
/*
*
* view on workbench
*
*/
void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
{
rtgui_container_add_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
/* hide view in default */
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
/* reset view extent */
rtgui_widget_set_rect(RTGUI_WIDGET(view), &(RTGUI_WIDGET(workbench)->extent));
}
void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
{
if (view == workbench->current_view)
rtgui_workbench_hide_view(workbench, view);
rtgui_container_remove_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
}
void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
{
RT_ASSERT(workbench != RT_NULL);
RT_ASSERT(view != RT_NULL);
/* already shown */
if (workbench->current_view == view) return;
if (workbench->current_view != RT_NULL)
{
/* hide old view */
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench->current_view));
}
/* show view */
RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
workbench->current_view = view;
/* update workbench clip */
rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
{
rtgui_widget_update(RTGUI_WIDGET(view));
}
}
void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
{
RT_ASSERT(workbench != RT_NULL);
RT_ASSERT(view != RT_NULL);
/* hide view */
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
if (view == workbench->current_view)
{
rtgui_view_t *next_view;
workbench->current_view = RT_NULL;
next_view = RTGUI_VIEW(rtgui_widget_get_next_sibling(RTGUI_WIDGET(view)));
if (next_view == RT_NULL)
next_view = RTGUI_VIEW(rtgui_widget_get_prev_sibling(RTGUI_WIDGET(view)));
if (next_view != RT_NULL)
{
rtgui_view_show(next_view, RT_FALSE);
}
else
{
/* update workbench clip */
rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
}
}
}
|