topwin.c 24.1 KB
Newer Older
B
bernard.xiong 已提交
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
/*
 * File      : topwin.c
 * This file is part of 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-16     Bernard      first version
 */
#include "panel.h"
#include "topwin.h"
#include "mouse.h"

#include <rtgui/event.h>
#include <rtgui/image.h>
#include <rtgui/rtgui_theme.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/window.h>

struct rtgui_topwin* rtgui_server_focus_topwin = RT_NULL;

static struct rtgui_list_node _rtgui_topwin_show_list;
static struct rtgui_list_node _rtgui_topwin_hide_list;
28
static struct rt_semaphore _rtgui_topwin_lock;
B
bernard.xiong 已提交
29 30 31 32 33 34 35 36 37

static void rtgui_topwin_update_clip(void);
static void rtgui_topwin_redraw(struct rtgui_rect* rect);

void rtgui_topwin_init()
{
	/* init window list */
	rtgui_list_init(&_rtgui_topwin_show_list);
	rtgui_list_init(&_rtgui_topwin_hide_list);
38 39 40

	rt_sem_init(&_rtgui_topwin_lock, 
		"topwin", 1, RT_IPC_FLAG_FIFO);
B
bernard.xiong 已提交
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
}

static struct rtgui_topwin*
rtgui_topwin_search_in_list(struct rtgui_win* wid, struct rtgui_list_node* list)
{
	struct rtgui_list_node* node;
	struct rtgui_topwin* topwin;

	/* search in list */
	rtgui_list_foreach(node, list)
	{
		topwin = rtgui_list_entry(node, struct rtgui_topwin, list);

		/* is this node? */
		if (topwin->wid == wid)
		{
			return topwin;
		}
	}

	return RT_NULL;
}

/* add a window to window list[hide] */
rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event)
{
	struct rtgui_topwin* topwin;

	topwin = rtgui_malloc(sizeof(struct rtgui_topwin));
	if (topwin == RT_NULL) return -RT_ERROR;

	topwin->wid 	= event->wid;
73 74 75
#ifdef RTGUI_USING_SMALL_SIZE
	topwin->extent	= RTGUI_WIDGET(event->wid)->extent;
#else
B
bernard.xiong 已提交
76
	topwin->extent 	= event->extent;
77
#endif
B
bernard.xiong 已提交
78 79 80
	topwin->tid 	= event->parent.sender;

	topwin->flag 	= 0;
81 82 83 84
	if (event->parent.user & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO;
	if (event->parent.user & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX;
	if (!(event->parent.user & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER;
	if (event->parent.user & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS;
B
bernard.xiong 已提交
85 86 87 88 89 90 91 92 93

	if(!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER))
	{
		/* get win extent */
		rtgui_rect_t rect = topwin->extent;

		/* add border rect */
		if (topwin->flag & WINTITLE_BORDER)
		{
94
			rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
B
bernard.xiong 已提交
95 96 97 98 99
		}

		/* add title rect */
		if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;

100 101 102
#ifdef RTGUI_USING_SMALL_SIZE
		topwin->title = rtgui_wintitle_create(event->wid->title);
#else
B
bernard.xiong 已提交
103
		topwin->title = rtgui_wintitle_create((const char*)event->title);
104
#endif
B
bernard.xiong 已提交
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
		rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect);

		/* update clip info */
		rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
		rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
			&(RTGUI_WIDGET(topwin->title)->clip),
			&(topwin->extent));
	}
	else topwin->title = RT_NULL;

	rtgui_list_init(&topwin->list);
	rtgui_list_init(&topwin->monitor_list);

	/* add topwin node to the hidden window list */
	rtgui_list_insert(&(_rtgui_topwin_hide_list), &(topwin->list));

	return RT_EOK;
}

rt_err_t rtgui_topwin_remove(struct rtgui_win* wid)
{
	struct rtgui_topwin* topwin;

	/* find the topwin node */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);

	if (topwin)
	{
		/* remove node from list */
		rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));

		rtgui_topwin_update_clip();

		/* redraw the old rect */
		rtgui_topwin_redraw(&(topwin->extent));

		if (rtgui_server_focus_topwin == topwin)
		{
			/* activate the next window */
			if (_rtgui_topwin_show_list.next != RT_NULL)
			{
				struct rtgui_event_win wevent;
				struct rtgui_topwin* wnd;

				/* get the topwin */
				wnd = rtgui_list_entry(_rtgui_topwin_show_list.next,
					struct rtgui_topwin, list);

				/* activate the window */
				RTGUI_EVENT_WIN_ACTIVATE_INIT(&wevent);
				wevent.wid = wnd->wid;
				rtgui_thread_send(wnd->tid, &(wevent.parent), sizeof(struct rtgui_event_win));

				/* set new focus topwin */
				rtgui_server_focus_topwin = wnd;
			}
			else
			{
				/* there is no shown window right now */
				rtgui_server_focus_topwin = RT_NULL;
			}
		}

		/* free the monitor rect list, topwin node and title */
		while (topwin->monitor_list.next != RT_NULL)
		{
			struct rtgui_mouse_monitor* monitor = rtgui_list_entry(topwin->monitor_list.next,
				struct rtgui_mouse_monitor, list);

			topwin->monitor_list.next = topwin->monitor_list.next->next;
			rtgui_free(monitor);
		}

		/* destroy win title */
		rtgui_wintitle_destroy(topwin->title);
		topwin->title = RT_NULL;

		rtgui_free(topwin);

		return RT_EOK;
	}

	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
	if (topwin)
	{
		/* remove node from list */
		rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));

		/* free the topwin node and title */
		rtgui_wintitle_destroy(topwin->title);
		topwin->title = RT_NULL;

		rtgui_free(topwin);

		return RT_EOK;
	}

	return -RT_ERROR;
}

/* activate a win
 * - deactivate the old focus win
 * - activate a win
 * - set the focus win to activate win
 * - draw win title
 */
void rtgui_topwin_activate_win(struct rtgui_topwin* win)
{
	struct rtgui_event_win event;

	/* activate the raised window */
	RTGUI_EVENT_WIN_ACTIVATE_INIT(&event);
	event.wid = win->wid;
	rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));

	/* redraw title */
	if (win->title != RT_NULL)
	{
		win->flag |= WINTITLE_ACTIVATE;
		rtgui_theme_draw_win(win);
	}

227
	if ((rtgui_server_focus_topwin != RT_NULL) && (rtgui_server_focus_topwin != win))
B
bernard.xiong 已提交
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
	{
		/* deactivate the old focus win  */
		RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
		event.wid = rtgui_server_focus_topwin->wid;
		rtgui_thread_send(rtgui_server_focus_topwin->tid,
			&event.parent, sizeof(struct rtgui_event_win));

		/* redraw title */
		if (rtgui_server_focus_topwin->title != RT_NULL)
		{
			rtgui_server_focus_topwin->flag &= ~WINTITLE_ACTIVATE;
			rtgui_theme_draw_win(rtgui_server_focus_topwin);
		}
	}

	rtgui_server_focus_topwin = win;
}

/*
 * deactivate a win
 * - deactivate the win
 * - redraw win title
 * - set rtgui_server_focus_topwin
 */
void rtgui_topwin_deactivate_win(struct rtgui_topwin* win)
{
	/* deactivate win */
	struct rtgui_event_win event;
	RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
	event.wid = win->wid;
	rtgui_thread_send(win->tid,
		&event.parent, sizeof(struct rtgui_event_win));

	win->flag &= ~WINTITLE_ACTIVATE;
	rtgui_theme_draw_win(win);

	if (rtgui_server_focus_topwin == win)
	{
		rtgui_server_focus_topwin = RT_NULL;
	}
}

/* raise window to front */
271 272 273 274 275 276 277 278 279 280 281 282
#ifdef RTGUI_USING_SMALL_SIZE
void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
{
	struct rtgui_topwin* topwin;

	/* find the topwin node */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
	if (topwin)
	{
		rt_int32_t count;
		struct rtgui_list_node* node;
		struct rtgui_event_clip_info eclip;
283
		RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
284
		eclip.wid = RT_NULL;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

		/* the window is already placed in front */
		if (&(topwin->list) == _rtgui_topwin_show_list.next)
		{
			rtgui_server_focus_topwin = RT_NULL;
			rtgui_topwin_activate_win(topwin);
		    return ;
		}

		/* remove node from list */
		rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
		/* add to front */
		rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));

		/* send clip info event */
		count = 0;
301
		for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node  = node->next)
302 303 304 305 306 307 308 309 310 311 312 313
		{
			struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);

			eclip.num_rect = count;
			eclip.wid = wnd->wid;

			count ++;

			/* send to destination window */
			rtgui_thread_send(wnd->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));

			/* reset clip info in title */
B
bernard.xiong 已提交
314 315 316 317 318 319 320
			if (wnd->title != RT_NULL)
			{
				rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
				rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
					&(RTGUI_WIDGET(wnd->title)->clip),
					&(wnd->extent));
			}
321 322 323 324 325 326
		}

		rtgui_topwin_activate_win(topwin);
	}
}
#else
B
bernard.xiong 已提交
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
void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
{
	struct rtgui_topwin* topwin;

	/* find the topwin node */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
	if (topwin)
	{
		rt_int32_t count;
		struct rtgui_list_node* node;
		struct rtgui_event_clip_info* eclip;
		struct rtgui_rect* rect;

		/* the window is already placed in front */
		if (&(topwin->list) == _rtgui_topwin_show_list.next)
		{
			rtgui_server_focus_topwin = RT_NULL;
			rtgui_topwin_activate_win(topwin);
		    return ;
		}

		/* update clip info */
		count = 0;
		node = _rtgui_topwin_show_list.next;
		while (node != &(topwin->list))
		{
			count ++;
			node = node->next;
		}

		eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
			+ (count + 1)* sizeof(struct rtgui_rect));

		/* reset clip info to top window */
		RTGUI_EVENT_CLIP_INFO_INIT(eclip);
		eclip->num_rect = 0;
		eclip->wid = topwin->wid;
		/* send to destination window */
		rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info));

		/* reset clip info in title */
B
bernard.xiong 已提交
368 369 370 371 372 373 374
		if (topwin->title != RT_NULL)
		{
			rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
			rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
				&(RTGUI_WIDGET(topwin->title)->clip),
				&(topwin->extent));
		}
B
bernard.xiong 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

		rect = RTGUI_EVENT_GET_RECT(eclip, 0);
		*rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent;

		count = 1;
		for (node = _rtgui_topwin_show_list.next;
			node != &(topwin->list);
			node  = node->next)
		{
			struct rtgui_topwin* wnd;
			wnd = rtgui_list_entry(node, struct rtgui_topwin, list);

			eclip->num_rect = count;
			eclip->wid = wnd->wid;

			/* send to destination window */
			rtgui_thread_send(wnd->tid, &(eclip->parent),
				sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));

			/* reset clip info in title */
B
bernard.xiong 已提交
395 396 397 398 399 400 401
			if (wnd->title != RT_NULL)
			{
				rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
				rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
					&(RTGUI_WIDGET(wnd->title)->clip),
					&(wnd->extent));
			}
B
bernard.xiong 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

			rect = RTGUI_EVENT_GET_RECT(eclip, count++);
			*rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
		}

		/* release clip info event */
		rtgui_free(eclip);

		/* remove node from list */
		rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
		/* add to front */
		rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));

		rtgui_topwin_activate_win(topwin);
	}
}
418
#endif
B
bernard.xiong 已提交
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

/* show a window */
void rtgui_topwin_show(struct rtgui_event_win* event)
{
	struct rtgui_topwin* topwin;
	struct rtgui_win* wid = event->wid;
	rt_thread_t sender = RTGUI_EVENT(event)->sender;

	/* find in hide list */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);

	/* find it */
	if (topwin != RT_NULL)
	{
		/* remove node from hidden list */
		rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));

		/* add node to show list */
		rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));

		/* show window title */
		if (topwin->title != RT_NULL)
		{
			RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
		}

		/* update clip info */
		rtgui_topwin_update_clip();

		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);

		/* activate this window */
		rtgui_topwin_activate_win(topwin);
	}
	else
	{
		/* the wnd is located in show list, raise wnd to front */
		topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
		if (topwin != RT_NULL)
		{
			if (_rtgui_topwin_show_list.next != &(topwin->list))
			{
				rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);

				/* not the front window, raise it */
				rtgui_topwin_raise(wid, sender);
			}
466 467 468 469 470
			else
			{
				/* just raise it */
				rtgui_topwin_raise(wid, sender);
			}
B
bernard.xiong 已提交
471 472 473 474 475 476 477 478 479 480
		}
		else
		{
			/* there is no wnd in wnd list */
			rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
		}
	}
}

/* send clip info to panel */
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
#ifdef RTGUI_USING_SMALL_SIZE
void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
{
	rt_uint32_t count;
	rt_thread_t tid;
	struct rtgui_list_node* node;
	struct rtgui_event_clip_info eclip;

	/* get topwin count  */
	count = 0;
	node = _rtgui_topwin_show_list.next;
	while (node != RT_NULL)
	{
		count ++;
		node = node->next;
	}

	/* send clip info event to panel */
	RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
	eclip.num_rect = count; eclip.wid = RT_NULL;

	/* send to the activated thread of panel */
	tid = rtgui_panel_get_active_thread(panel);
	rtgui_thread_send(tid, &eclip.parent, sizeof(struct rtgui_event_clip_info));
}
#else
B
bernard.xiong 已提交
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
void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
{
	rt_uint32_t count;
	rt_thread_t tid;
	struct rtgui_list_node* node;
	struct rtgui_event_clip_info* eclip;

	/* get topwin count  */
	count = 0;
	node = _rtgui_topwin_show_list.next;
	while (node != RT_NULL)
	{
		count ++;
		node = node->next;
	}

	eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
		+ (count + 1)* sizeof(struct rtgui_rect));
	if (eclip == RT_NULL)
	{
		/* no memory */
		return ;
	}

	/* reset clip info to top window */
	RTGUI_EVENT_CLIP_INFO_INIT(eclip);
	eclip->num_rect = count; eclip->wid = RT_NULL;

	count = 0;
	for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node  = node->next)
	{
		struct rtgui_topwin* wnd;
		struct rtgui_rect* rect;

		wnd = rtgui_list_entry(node, struct rtgui_topwin, list);

		rect = RTGUI_EVENT_GET_RECT(eclip, count++);
		*rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
	}

	/* send to the activated thread of panel */
	tid = rtgui_panel_get_active_thread(panel);
	rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info)
		+ count* sizeof(struct rtgui_rect));

	/* release clip info event */
	rtgui_free(eclip);
}
555
#endif
B
bernard.xiong 已提交
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

/* hide a window */
void rtgui_topwin_hide(struct rtgui_event_win* event)
{
	struct rtgui_topwin* topwin;
	struct rtgui_win* wid = event->wid;

	/* find in show list */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);

	/* found it */
	if (topwin)
	{
		/* remove node from show list */
		rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));

		/* add node to hidden list */
		rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list));

		/* show window title */
		if (topwin->title != RT_NULL)
		{
			RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
		}

		/* update clip info */
		rtgui_topwin_update_clip();

		/* redraw the old rect */
		rtgui_topwin_redraw(&(topwin->extent));

		if (rtgui_server_focus_topwin == topwin)
		{
			/* activate the next window */
			if (_rtgui_topwin_show_list.next != RT_NULL)
			{
				/* get the topwin */
				topwin = rtgui_list_entry(_rtgui_topwin_show_list.next,
					struct rtgui_topwin, list);

				rtgui_server_focus_topwin = RT_NULL;
				rtgui_topwin_activate_win(topwin);
			}
			else
			{
				/* there is no shown window right now */
				rtgui_server_focus_topwin = RT_NULL;
			}
		}
	}
	else
	{
		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
		return;
	}

	rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
}

/* move top window */
void rtgui_topwin_move(struct rtgui_event_win_move* event)
{
	struct rtgui_topwin* topwin;

	/* find in show list */
	topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list);
	if (topwin != RT_NULL)
	{
		int dx, dy;
		rtgui_rect_t rect; /* the old topwin coverage area */
		struct rtgui_list_node* node;

		/* send status ok */
		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);

		/* get the delta move x, y */
		dx = event->x - topwin->extent.x1;
		dy = event->y - topwin->extent.y1;

		rect = topwin->extent;
		/* move window rect */
		rtgui_rect_moveto(&(topwin->extent), dx, dy);

		/* move window title */
		if (topwin->title != RT_NULL)
		{
			rect = RTGUI_WIDGET(topwin->title)->extent;
			rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
		}

		/* move the monitor rect list */
		rtgui_list_foreach(node, &(topwin->monitor_list))
		{
			struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
				struct rtgui_mouse_monitor,
				list);
			rtgui_rect_moveto(&(monitor->rect), dx, dy);
		}

		/* update windows clip info */
		rtgui_topwin_update_clip();

		/* update top window title */
		if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin);
		if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK)
		{
			/*
			 * the old rect is not intersect with moved rect,
			 * re-paint window
			 */
			struct rtgui_event_paint epaint;
			RTGUI_EVENT_PAINT_INIT(&epaint);
			epaint.wid = topwin->wid;
			rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint));
		}

		/* update old window coverage area */
		rtgui_topwin_redraw(&rect);
	}
	else
	{
		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
	}
}

/*
 * resize a top win
 * Note: currently, only support resize hidden window
 */
void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r)
{
	struct rtgui_topwin* topwin;

	/* find in show list */
	topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
	if (topwin)
	{
		topwin->extent = *r;

		if (topwin->title != RT_NULL)
		{
			/* get win extent */
			rtgui_rect_t rect = topwin->extent;

			/* add border rect */
			if (topwin->flag & WINTITLE_BORDER)
			{
703
				rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
B
bernard.xiong 已提交
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
			}

			/* add title rect */
			if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;

			RTGUI_WIDGET(topwin->title)->extent = rect;

			/* update title & border clip info */
			rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
			rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
				&(RTGUI_WIDGET(topwin->title)->clip),
				&(topwin->extent));
		}
	}
}

struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
{
	struct rtgui_list_node* node;
	struct rtgui_topwin* topwin;

	/* search in list */
	rtgui_list_foreach(node, &(_rtgui_topwin_show_list))
	{
		topwin = rtgui_list_entry(node, struct rtgui_topwin, list);

		/* is this window? */
		if ((topwin->title != RT_NULL) &&
			rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
		{
			return topwin;
		}
		else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
		{
			return topwin;
		}
	}

	return RT_NULL;
}

extern struct rtgui_list_node _rtgui_panel_list;
746 747 748 749 750 751
static void rtgui_topwin_update_clip()
{
	rt_int32_t count = 0;
	struct rtgui_event_clip_info eclip;
	struct rtgui_list_node* node = _rtgui_topwin_show_list.next;

752 753
	RTGUI_EVENT_CLIP_INFO_INIT(&eclip);

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
	rtgui_list_foreach(node, &_rtgui_topwin_show_list)
	{
		struct rtgui_topwin* wnd;
		wnd = rtgui_list_entry(node, struct rtgui_topwin, list);

		eclip.num_rect = count;
		eclip.wid = wnd->wid;

		count ++;

		/* send to destination window */
		rtgui_thread_send(wnd->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));

		/* update clip in win title */
		if (wnd->title != RT_NULL)
		{
			/* reset clip info */
			rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
			rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
				&(RTGUI_WIDGET(wnd->title)->clip),
				&(wnd->extent));
		}
	}

	/* send clip info to each panel */
	eclip.wid = RT_NULL;
	eclip.num_rect = count;

	rtgui_list_foreach(node, &(_rtgui_panel_list))
	{
		struct rtgui_panel* panel;
		struct rtgui_list_node* panel_node;

		panel = rtgui_list_entry(node, struct rtgui_panel, sibling);

		rtgui_list_foreach(panel_node, &(panel->thread_list))
		{
			struct rtgui_panel_thread* thread;
			thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);

			/* send clip info to panel */
			rtgui_thread_send(thread->tid, &(eclip.parent),	sizeof(struct rtgui_event_clip_info));
		}
	}
}
B
bernard.xiong 已提交
799 800 801 802 803 804

static void rtgui_topwin_redraw(struct rtgui_rect* rect)
{
	struct rtgui_list_node* node;
	struct rtgui_event_paint epaint;
	RTGUI_EVENT_PAINT_INIT(&epaint);
805
	epaint.wid = RT_NULL;
B
bernard.xiong 已提交
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 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

	rtgui_list_foreach(node, &_rtgui_topwin_show_list)
	{
		struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);

		if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK)
		{
			/* draw window */
			epaint.wid = wnd->wid;
			rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint));

			/* draw title */
			if (wnd->title != RT_NULL)
			{
				rtgui_theme_draw_win(wnd);
			}
		}
	}

	/* redraw the panel */
	rtgui_list_foreach(node, &(_rtgui_panel_list))
	{
		struct rtgui_panel* panel;
		panel = rtgui_list_entry(node, struct rtgui_panel, sibling);

		if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK)
		{
			rt_thread_t tid;

			tid = rtgui_panel_get_active_thread(panel);
			if (tid != RT_NULL)
			{
				/* draw panel */
				epaint.wid = RT_NULL;
				rtgui_thread_send(tid, &(epaint.parent), sizeof(epaint));
			}
		}
	}
}

void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event)
{
	rtgui_rect_t rect;

	/* let window to process this mouse event */
	if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
	{
		/* send mouse event to thread */
		rtgui_thread_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
		return;
	}

	/* get close button rect (device value) */
	rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
	rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
	rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
	rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;

	if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
	{
		if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
		{
			if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
			{
				win->flag |= WINTITLE_CB_PRESSED;
				rtgui_theme_draw_win(win);
			}
873
#ifdef RTGUI_USING_WINMOVE
B
bernard.xiong 已提交
874 875 876 877 878
			else
			{
				/* maybe move window */
				rtgui_winrect_set(win);
			}
879
#endif
B
bernard.xiong 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
		}
		else if (event->button & RTGUI_MOUSE_BUTTON_UP)
		{
			if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
			{
				struct rtgui_event_win event;

				win->flag &= ~WINTITLE_CB_PRESSED;
				rtgui_theme_draw_win(win);

				/* send close event to window */
				RTGUI_EVENT_WIN_CLOSE_INIT(&event);
				event.wid = win->wid;
				rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
			}
		}
	}
}

void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
{
	struct rtgui_topwin* win;

	/* parameters check */
	if (wid == RT_NULL || rect == RT_NULL) return;

	/* find topwin */
	win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
	if (win == RT_NULL)
		win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);

	if (win == RT_NULL) return;

	/* append rect to top window monitor rect list */
	rtgui_mouse_monitor_append(&(win->monitor_list), rect);
}

void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
{
	struct rtgui_topwin* win;

	/* parameters check */
	if (wid == RT_NULL || rect == RT_NULL) return;

	/* find topwin */
	win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
	if (win == RT_NULL)
		win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);

	if (win == RT_NULL) return;

	/* remove rect from top window monitor rect list */
	rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
}

935
/**
936 937
 * do clip for topwin list
 * widget, the clip widget to be done clip
938
 */
939
void rtgui_topwin_do_clip(rtgui_widget_t* widget)
940
{
B
bernard.xiong 已提交
941
	rtgui_widget_t* wid;
942 943 944 945
	struct rtgui_rect* rect;
	struct rtgui_topwin* topwin;
	struct rtgui_list_node* node;

946 947
	/* get toplevel wid */
	wid = widget->toplevel;
948 949 950 951 952 953 954

	rt_sem_take(&_rtgui_topwin_lock, RT_WAITING_FOREVER);
	/* get all of topwin rect list */
	rtgui_list_foreach(node, &_rtgui_topwin_show_list)
	{
		topwin = rtgui_list_entry(node, struct rtgui_topwin, list);

955 956 957 958
		if (topwin->wid == (rtgui_win_t*)(wid) ||
			RTGUI_WIDGET(topwin->title) == widget) break; /* it's self window, break */

		/* get extent */
B
bernard.xiong 已提交
959
		if (topwin->title != RT_NULL) 
960
			rect = &(RTGUI_WIDGET(topwin->title)->extent);
961
		else 
962
			rect = &(topwin->extent);
B
bernard.xiong 已提交
963

964 965
		/* subtract the topwin rect */
		rtgui_region_subtract_rect(&(widget->clip), &(widget->clip), rect);
966 967 968
	}
	rt_sem_release(&_rtgui_topwin_lock);
}
969