workbench.c 12.6 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
 */
#include <rtgui/rtgui_system.h>
B
bernard.xiong 已提交
15 16 17 18 19 20 21 22 23 24 25
#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->panel = RT_NULL;
	workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT;
B
bernard.xiong 已提交
26
	workbench->modal_code = RTGUI_MODAL_OK;
B
bernard.xiong 已提交
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
	workbench->current_view = 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;
}

rtgui_type_t *rtgui_workbench_type_get(void)
{
	static rtgui_type_t *workbench_type = RT_NULL;

	if (!workbench_type)
	{
		workbench_type = rtgui_type_create("workbench", RTGUI_TOPLEVEL_TYPE,
			sizeof(rtgui_workbench_t),
			RTGUI_CONSTRUCTOR(_rtgui_workbench_constructor),
			RTGUI_DESTRUCTOR(_rtgui_workbench_destructor));
	}

	return workbench_type;
}

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 (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;
	}

	rtgui_widget_destroy(RTGUI_WIDGET(workbench));
}

void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
{
	RT_ASSERT(workbench != RT_NULL);

	workbench->flag = flag;
}

B
bernard.xiong 已提交
161
rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
B
bernard.xiong 已提交
162 163 164 165 166
{
	/* the buffer uses to receive event */
	char event_buf[256];
	struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];

B
bernard.xiong 已提交
167
	if (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
B
bernard.xiong 已提交
168
	{
B
bernard.xiong 已提交
169 170
		/* event loop for modal mode shown view */
		while (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
B
bernard.xiong 已提交
171
		{
B
bernard.xiong 已提交
172 173 174 175
			if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
			{
				RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
			}
B
bernard.xiong 已提交
176 177
		}
	}
B
bernard.xiong 已提交
178 179
	else
	{
180 181 182
		/* show workbench firstly */
		rtgui_workbench_show(workbench);
		
B
bernard.xiong 已提交
183 184 185 186 187 188 189 190 191 192
		while (!(workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED))
		{
			if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
			{
				RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
			}
		}
	}

	return RT_TRUE;
B
bernard.xiong 已提交
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
}

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:
B
bernard.xiong 已提交
257
		/* hide widget */
B
bernard.xiong 已提交
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
		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 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_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
			{
B
bernard.xiong 已提交
319 320 321 322 323 324 325
				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));
				}

B
bernard.xiong 已提交
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
				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;

				/* 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)
B
bernard.xiong 已提交
490 491
		{
			rtgui_view_show(next_view, RT_FALSE);
B
bernard.xiong 已提交
492 493 494 495 496 497 498 499
		}
		else
		{
			/* update workbench clip */
			rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
		}
	}
}