window.c
changeset 158 b1a821f84250
parent 152 c3964b43943e
child 178 7d91f0b06c8b
equal deleted inserted replaced
157:dd017fa3bad8 158:b1a821f84250
    19 	e.click.pt.x = x;
    19 	e.click.pt.x = x;
    20 	e.click.pt.y = y;
    20 	e.click.pt.y = y;
    21 	e.event = WE_CLICK;
    21 	e.event = WE_CLICK;
    22 
    22 
    23 	if (w->desc_flags & WDF_DEF_WIDGET) {
    23 	if (w->desc_flags & WDF_DEF_WIDGET) {
    24 		e.click.widget = GetWidgetFromPos(w, x, y); 
    24 		e.click.widget = GetWidgetFromPos(w, x, y);
    25 		if (e.click.widget < 0) return; /* exit if clicked outside of widgets */
    25 		if (e.click.widget < 0) return; /* exit if clicked outside of widgets */
    26 		
    26 
    27 		wi = &w->widget[e.click.widget];
    27 		wi = &w->widget[e.click.widget];
    28 
    28 
    29 		if (wi->type & 0xE0) {
    29 		if (wi->type & 0xE0) {
    30 			/* special widget handling */
    30 			/* special widget handling */
    31 			switch(wi->type) {
    31 			switch(wi->type) {
    36 				HandleButtonClick(w, e.click.widget);
    36 				HandleButtonClick(w, e.click.widget);
    37 				break;
    37 				break;
    38 			case WWT_NODISTXTBTN:
    38 			case WWT_NODISTXTBTN:
    39 				if (HASBIT(w->disabled_state, e.click.widget))
    39 				if (HASBIT(w->disabled_state, e.click.widget))
    40 					return; /* don't allow click if disabled */
    40 					return; /* don't allow click if disabled */
    41 				break;				
    41 				break;
    42 			}
    42 			}
    43 		} else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_HSCROLLBAR) {
    43 		} else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_HSCROLLBAR) {
    44 			ScrollbarClickHandler(w, wi, e.click.pt.x, e.click.pt.y);
    44 			ScrollbarClickHandler(w, wi, e.click.pt.x, e.click.pt.y);
    45 		}
    45 		}
    46 
    46 
    47 		w->wndproc(w, &e);
    47 		w->wndproc(w, &e);
    48 		
    48 
    49 		if (w->desc_flags & WDF_STD_BTN) {
    49 		if (w->desc_flags & WDF_STD_BTN) {
    50 			if (e.click.widget == 0) DeleteWindow(w);
    50 			if (e.click.widget == 0) DeleteWindow(w);
    51 			else { 
    51 			else {
    52 				if (e.click.widget == 1) {
    52 				if (e.click.widget == 1) {
    53 					if (_ctrl_pressed) 
    53 					if (_ctrl_pressed)
    54 						StartWindowSizing(w); 
    54 						StartWindowSizing(w);
    55 					else 
    55 					else
    56 						StartWindowDrag(w);
    56 						StartWindowDrag(w);
    57 				}
    57 				}
    58 			}
    58 			}
    59 		}
    59 		}
    60 	} else {
    60 	} else {
    78 	}
    78 	}
    79 
    79 
    80 	e.event = WE_RCLICK;
    80 	e.event = WE_RCLICK;
    81 	e.click.pt.x = x;
    81 	e.click.pt.x = x;
    82 	e.click.pt.y = y;
    82 	e.click.pt.y = y;
    83 	w->wndproc(w, &e);	
    83 	w->wndproc(w, &e);
    84 }
    84 }
    85 
    85 
    86 
    86 
    87 void DispatchMouseWheelEvent(Window *w, int wheel)
    87 void DispatchMouseWheelEvent(Window *w, int wheel)
    88 {
    88 {
   101 	Window *w;
   101 	Window *w;
   102 	DrawPixelInfo bk;
   102 	DrawPixelInfo bk;
   103 	_cur_dpi = &bk;
   103 	_cur_dpi = &bk;
   104 
   104 
   105 	for(w=_windows; w!=_last_window; w++) {
   105 	for(w=_windows; w!=_last_window; w++) {
   106 		if (right > w->left && 
   106 		if (right > w->left &&
   107 				bottom > w->top &&
   107 				bottom > w->top &&
   108 				left < w->left + w->width &&
   108 				left < w->left + w->width &&
   109 				top < w->top + w->height) {
   109 				top < w->top + w->height) {
   110 				DrawOverlappedWindow(w, left, top, right, bottom);	
   110 				DrawOverlappedWindow(w, left, top, right, bottom);
   111 			}		
   111 			}
   112 	}
   112 	}
   113 }
   113 }
   114 
   114 
   115 void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
   115 void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
   116 {
   116 {
   120 	while (++v != _last_window) {
   120 	while (++v != _last_window) {
   121 		if (right > v->left &&
   121 		if (right > v->left &&
   122 		 		bottom > v->top &&
   122 		 		bottom > v->top &&
   123 				left < v->left + v->width &&
   123 				left < v->left + v->width &&
   124 				top < v->top + v->height) {
   124 				top < v->top + v->height) {
   125 		
   125 
   126 			if (left < (x=v->left)) {
   126 			if (left < (x=v->left)) {
   127 				DrawOverlappedWindow(w, left, top, x, bottom);
   127 				DrawOverlappedWindow(w, left, top, x, bottom);
   128 				DrawOverlappedWindow(w, x, top, right, bottom);
   128 				DrawOverlappedWindow(w, x, top, right, bottom);
   129 				return;
   129 				return;
   130 			}
   130 			}
   150 			return;
   150 			return;
   151 		}
   151 		}
   152 	}
   152 	}
   153 
   153 
   154 	{
   154 	{
   155 		DrawPixelInfo *dp = _cur_dpi;	
   155 		DrawPixelInfo *dp = _cur_dpi;
   156 		dp->width = right - left;
   156 		dp->width = right - left;
   157 		dp->height = bottom - top;
   157 		dp->height = bottom - top;
   158 		dp->left = left - w->left;
   158 		dp->left = left - w->left;
   159 		dp->top = top - w->top;
   159 		dp->top = top - w->top;
   160 		dp->pitch = _screen.pitch;
   160 		dp->pitch = _screen.pitch;
   173 
   173 
   174 void SetWindowDirty(Window *w)
   174 void SetWindowDirty(Window *w)
   175 {
   175 {
   176 	if (w == NULL)
   176 	if (w == NULL)
   177 		return;
   177 		return;
   178 	
   178 
   179 	SetDirtyBlocks(w->left, w->top, w->left + w->width, w->top + w->height);
   179 	SetDirtyBlocks(w->left, w->top, w->left + w->width, w->top + w->height);
   180 }
   180 }
   181 
   181 
   182 void DeleteWindow(Window *w)
   182 void DeleteWindow(Window *w)
   183 {
   183 {
   218 Window *FindWindowById(WindowClass cls, WindowNumber number)
   218 Window *FindWindowById(WindowClass cls, WindowNumber number)
   219 {
   219 {
   220 	Window *w;
   220 	Window *w;
   221 
   221 
   222 	for(w=_windows; w!=_last_window; w++) {
   222 	for(w=_windows; w!=_last_window; w++) {
   223 		if (w->window_class == cls && 
   223 		if (w->window_class == cls &&
   224 			  w->window_number == number) {
   224 			  w->window_number == number) {
   225 			return w;
   225 			return w;
   226 		}
   226 		}
   227 	}		
   227 	}
   228 
   228 
   229 	return NULL;
   229 	return NULL;
   230 }
   230 }
   231 
   231 
   232 void DeleteWindowById(WindowClass cls, WindowNumber number)
   232 void DeleteWindowById(WindowClass cls, WindowNumber number)
   233 {
   233 {
   234 	DeleteWindow(FindWindowById(cls, number));
   234 	DeleteWindow(FindWindowById(cls, number));
   235 }	
   235 }
   236 
   236 
   237 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
   237 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
   238 {
   238 {
   239 	Window *w = FindWindowById(cls, number);
   239 	Window *w = FindWindowById(cls, number);
   240 
   240 
   275 Window *AllocateWindow(
   275 Window *AllocateWindow(
   276 							int x,
   276 							int x,
   277 							int y,
   277 							int y,
   278 							int width,
   278 							int width,
   279 							int height,
   279 							int height,
   280 							WindowProc *proc, 
   280 							WindowProc *proc,
   281 							WindowClass cls,
   281 							WindowClass cls,
   282 							const Widget *widget)
   282 							const Widget *widget)
   283 {
   283 {
   284 	Window *w;
   284 	Window *w;
   285 
   285 
   290 		for(w=_windows; ;w++) {
   290 		for(w=_windows; ;w++) {
   291 			assert(w < _last_window);
   291 			assert(w < _last_window);
   292 
   292 
   293 			if (w->window_class != WC_MAIN_WINDOW && w->window_class != WC_MAIN_TOOLBAR &&
   293 			if (w->window_class != WC_MAIN_WINDOW && w->window_class != WC_MAIN_TOOLBAR &&
   294 			    w->window_class != WC_STATUS_BAR && w->window_class != WC_NEWS_WINDOW) {
   294 			    w->window_class != WC_STATUS_BAR && w->window_class != WC_NEWS_WINDOW) {
   295 			
   295 
   296 					DeleteWindow(w);
   296 					DeleteWindow(w);
   297 					goto restart;
   297 					goto restart;
   298 			}
   298 			}
   299 		}
   299 		}
   300 	}
   300 	}
   336 	w->vscroll.count = 0;
   336 	w->vscroll.count = 0;
   337 	w->hscroll.pos = 0;
   337 	w->hscroll.pos = 0;
   338 	w->hscroll.count = 0;
   338 	w->hscroll.count = 0;
   339 	w->widget = widget;
   339 	w->widget = widget;
   340 
   340 
   341 	((uint32*)w->custom)[0] = 0;
   341 	{
   342 	((uint32*)w->custom)[1] = 0;
   342 		int i;
   343 	((uint32*)w->custom)[2] = 0;
   343 		for (i=0;i<lengthof(w->custom);i++)
   344 	((uint32*)w->custom)[3] = 0;
   344 			w->custom[i] = 0;
   345 
   345 	}
   346 
   346 
   347 	_last_window++;
   347 	_last_window++;
   348 
   348 
   349 	SetWindowDirty(w);
   349 	SetWindowDirty(w);
   350 	
   350 
   351 	CallWindowEventNP(w, WE_CREATE);
   351 	CallWindowEventNP(w, WE_CREATE);
   352 
   352 
   353 	return w;
   353 	return w;
   354 }
   354 }
   355 
   355 
   356 Window *AllocateWindowAutoPlace2(
   356 Window *AllocateWindowAutoPlace2(
   357 	WindowClass exist_class,
   357 	WindowClass exist_class,
   358 	WindowNumber exist_num,
   358 	WindowNumber exist_num,
   359 	int width,
   359 	int width,
   360 	int height,
   360 	int height,
   361 	WindowProc *proc, 
   361 	WindowProc *proc,
   362 	WindowClass cls,
   362 	WindowClass cls,
   363 	const Widget *widget)
   363 	const Widget *widget)
   364 {
   364 {
   365 	Window *w;
   365 	Window *w;
   366 	int x;
   366 	int x;
   367 	
   367 
   368 	w = FindWindowById(exist_class, exist_num);
   368 	w = FindWindowById(exist_class, exist_num);
   369 	if (w == NULL || w->left >= (_screen.width-20) || w->left <= -60 || w->top >= (_screen.height-20)) {
   369 	if (w == NULL || w->left >= (_screen.width-20) || w->left <= -60 || w->top >= (_screen.height-20)) {
   370 		return AllocateWindowAutoPlace(width,height,proc,cls,widget);
   370 		return AllocateWindowAutoPlace(width,height,proc,cls,widget);
   371 	}	
   371 	}
   372 
   372 
   373 	x = w->left;
   373 	x = w->left;
   374 	if (x > _screen.width - width)
   374 	if (x > _screen.width - width)
   375 		x = (_screen.width - width) - 20;
   375 		x = (_screen.width - width) - 20;
   376 
   376 
   396 	bottom = _awap_r.height + top;
   396 	bottom = _awap_r.height + top;
   397 
   397 
   398 	if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height)
   398 	if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height)
   399 		return false;
   399 		return false;
   400 
   400 
   401 	// Make sure it is not obscured by any window.	
   401 	// Make sure it is not obscured by any window.
   402 	for(w=_windows; w!=_last_window; w++) {
   402 	for(w=_windows; w!=_last_window; w++) {
   403 		if (w->window_class == WC_MAIN_WINDOW)
   403 		if (w->window_class == WC_MAIN_WINDOW)
   404 			continue;
   404 			continue;
   405 
   405 
   406 		if (right > w->left && 
   406 		if (right > w->left &&
   407 		    w->left + w->width > left &&
   407 		    w->left + w->width > left &&
   408 				bottom > w->top &&
   408 				bottom > w->top &&
   409 				w->top + w->height > top)
   409 				w->top + w->height > top)
   410 					return false;
   410 					return false;
   411 	}	
   411 	}
   412 
   412 
   413 	return true;
   413 	return true;
   414 }
   414 }
   415 
   415 
   416 static bool IsGoodAutoPlace2(int left, int top)
   416 static bool IsGoodAutoPlace2(int left, int top)
   426 	if (left < -(width>>2) || left > _screen.width - (width>>1))
   426 	if (left < -(width>>2) || left > _screen.width - (width>>1))
   427 		return false;
   427 		return false;
   428 	if (top < 22 || top > _screen.height - (height>>2))
   428 	if (top < 22 || top > _screen.height - (height>>2))
   429 		return false;
   429 		return false;
   430 
   430 
   431 	// Make sure it is not obscured by any window.	
   431 	// Make sure it is not obscured by any window.
   432 	for(w=_windows; w!=_last_window; w++) {
   432 	for(w=_windows; w!=_last_window; w++) {
   433 		if (w->window_class == WC_MAIN_WINDOW)
   433 		if (w->window_class == WC_MAIN_WINDOW)
   434 			continue;
   434 			continue;
   435 
   435 
   436 		if (left + width > w->left && 
   436 		if (left + width > w->left &&
   437 		    w->left + w->width > left &&
   437 		    w->left + w->width > left &&
   438 				top + height > w->top &&
   438 				top + height > w->top &&
   439 				w->top + w->height > top)
   439 				w->top + w->height > top)
   440 					return false;
   440 					return false;
   441 	}	
   441 	}
   442 
   442 
   443 	return true;
   443 	return true;
   444 }
   444 }
   445 
   445 
   446 Point GetAutoPlacePosition(int width, int height) {
   446 Point GetAutoPlacePosition(int width, int height) {
   462 		if (IsGoodAutoPlace1(w->left,w->top-   height-2)) goto ok_pos;
   462 		if (IsGoodAutoPlace1(w->left,w->top-   height-2)) goto ok_pos;
   463 		if (IsGoodAutoPlace1(w->left+w->width+2,w->top+w->height-height)) goto ok_pos;
   463 		if (IsGoodAutoPlace1(w->left+w->width+2,w->top+w->height-height)) goto ok_pos;
   464 		if (IsGoodAutoPlace1(w->left-   width-2,w->top+w->height-height)) goto ok_pos;
   464 		if (IsGoodAutoPlace1(w->left-   width-2,w->top+w->height-height)) goto ok_pos;
   465 		if (IsGoodAutoPlace1(w->left+w->width-width,w->top+w->height+2)) goto ok_pos;
   465 		if (IsGoodAutoPlace1(w->left+w->width-width,w->top+w->height+2)) goto ok_pos;
   466 		if (IsGoodAutoPlace1(w->left+w->width-width,w->top-   height-2)) goto ok_pos;
   466 		if (IsGoodAutoPlace1(w->left+w->width-width,w->top-   height-2)) goto ok_pos;
   467 	}	
   467 	}
   468 	
   468 
   469 	for(w=_windows; w!=_last_window; w++) {
   469 	for(w=_windows; w!=_last_window; w++) {
   470 		if (w->window_class == WC_MAIN_WINDOW)
   470 		if (w->window_class == WC_MAIN_WINDOW)
   471 			continue;
   471 			continue;
   472 
   472 
   473 		if (IsGoodAutoPlace2(w->left+w->width+2,w->top)) goto ok_pos;
   473 		if (IsGoodAutoPlace2(w->left+w->width+2,w->top)) goto ok_pos;
   476 		if (IsGoodAutoPlace2(w->left,w->top-   height-2)) goto ok_pos;
   476 		if (IsGoodAutoPlace2(w->left,w->top-   height-2)) goto ok_pos;
   477 	}
   477 	}
   478 
   478 
   479 	{
   479 	{
   480 		int left=0,top=24;
   480 		int left=0,top=24;
   481 		
   481 
   482 restart:;
   482 restart:;
   483 		for(w=_windows; w!=_last_window; w++) {
   483 		for(w=_windows; w!=_last_window; w++) {
   484 			if (w->left == left && w->top == top) {
   484 			if (w->left == left && w->top == top) {
   485 				left += 5;
   485 				left += 5;
   486 				top += 5;
   486 				top += 5;
   487 				goto restart;
   487 				goto restart;
   488 			}
   488 			}
   489 		}
   489 		}
   490 		
   490 
   491 		pt.x = left;
   491 		pt.x = left;
   492 		pt.y = top;
   492 		pt.y = top;
   493 		return pt;
   493 		return pt;
   494 	}
   494 	}
   495 	
   495 
   496 ok_pos:;
   496 ok_pos:;
   497 	pt.x = _awap_r.left;
   497 	pt.x = _awap_r.left;
   498 	pt.y = _awap_r.top;
   498 	pt.y = _awap_r.top;
   499 	return pt;
   499 	return pt;
   500 }
   500 }
   501 
   501 
   502 Window *AllocateWindowAutoPlace(
   502 Window *AllocateWindowAutoPlace(
   503 	int width,
   503 	int width,
   504 	int height,
   504 	int height,
   505 	WindowProc *proc, 
   505 	WindowProc *proc,
   506 	WindowClass cls,
   506 	WindowClass cls,
   507 	const Widget *widget) {
   507 	const Widget *widget) {
   508 
   508 
   509 	Point pt = GetAutoPlacePosition(width, height);
   509 	Point pt = GetAutoPlacePosition(width, height);
   510 	return AllocateWindow(pt.x, pt.y, width, height, proc, cls, widget);
   510 	return AllocateWindow(pt.x, pt.y, width, height, proc, cls, widget);
   524 Window *AllocateWindowDesc(const WindowDesc *desc)
   524 Window *AllocateWindowDesc(const WindowDesc *desc)
   525 {
   525 {
   526 	Point pt;
   526 	Point pt;
   527 	Window *w;
   527 	Window *w;
   528 
   528 
   529 	if (desc->parent_cls != WC_MAIN_WINDOW && 
   529 	if (desc->parent_cls != WC_MAIN_WINDOW &&
   530 			(w = FindWindowById(desc->parent_cls, _alloc_wnd_parent_num), _alloc_wnd_parent_num=0, w) != NULL &&
   530 			(w = FindWindowById(desc->parent_cls, _alloc_wnd_parent_num), _alloc_wnd_parent_num=0, w) != NULL &&
   531 			w->left < _screen.width-20 && w->left > -60 && w->top < _screen.height-20) {
   531 			w->left < _screen.width-20 && w->left > -60 && w->top < _screen.height-20) {
   532 		pt.x = w->left + 10;
   532 		pt.x = w->left + 10;
   533 		if (pt.x > _screen.width + 10 - desc->width)
   533 		if (pt.x > _screen.width + 10 - desc->width)
   534 			pt.x = (_screen.width + 10 - desc->width) - 20;
   534 			pt.x = (_screen.width + 10 - desc->width) - 20;
   597 		CallWindowEventNP(w, WE_MOUSELOOP);
   597 		CallWindowEventNP(w, WE_MOUSELOOP);
   598 	}
   598 	}
   599 
   599 
   600 	for(w=_last_window; w != _windows;) {
   600 	for(w=_last_window; w != _windows;) {
   601 		--w;
   601 		--w;
   602 		
   602 
   603 		if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) {
   603 		if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) {
   604 			CallWindowEventNP(w, WE_TIMEOUT);
   604 			CallWindowEventNP(w, WE_TIMEOUT);
   605 			if (w->desc_flags & WDF_UNCLICK_BUTTONS)
   605 			if (w->desc_flags & WDF_UNCLICK_BUTTONS)
   606 				UnclickWindowButtons(w);
   606 				UnclickWindowButtons(w);
   607 		}
   607 		}
   642 	if (_special_mouse_mode != WSM_DRAGDROP)
   642 	if (_special_mouse_mode != WSM_DRAGDROP)
   643 		return true;
   643 		return true;
   644 
   644 
   645 	if (_left_button_down)
   645 	if (_left_button_down)
   646 		return false;
   646 		return false;
   647 	
   647 
   648 	w = GetCallbackWnd();
   648 	w = GetCallbackWnd();
   649 
   649 
   650 	ResetObjectToPlace();
   650 	ResetObjectToPlace();
   651 
   651 
   652 	if (w) {
   652 	if (w) {
   686 	}
   686 	}
   687 
   687 
   688 	return false;
   688 	return false;
   689 }
   689 }
   690 
   690 
   691 bool HandleWindowDragging() 
   691 bool HandleWindowDragging()
   692 {
   692 {
   693 	Window *w;
   693 	Window *w;
   694 	int x, y, t;
   694 	int x, y, t;
   695 
   695 
   696 	// Get out immediately if no window is being dragged at all.
   696 	// Get out immediately if no window is being dragged at all.
   698 		return true;
   698 		return true;
   699 
   699 
   700 	// Otherwise find the window...
   700 	// Otherwise find the window...
   701 	for(w=_windows; w != _last_window; w++) {
   701 	for(w=_windows; w != _last_window; w++) {
   702 		if (w->flags4&(WF_DRAGGING|WF_SIZING)) {
   702 		if (w->flags4&(WF_DRAGGING|WF_SIZING)) {
   703 			
   703 
   704 			// Stop the dragging if the left mouse button was released
   704 			// Stop the dragging if the left mouse button was released
   705 			if (!_left_button_down) {
   705 			if (!_left_button_down) {
   706 				w->flags4 &= ~(WF_DRAGGING | WF_SIZING);
   706 				w->flags4 &= ~(WF_DRAGGING | WF_SIZING);
   707 				break;
   707 				break;
   708 			}
   708 			}
   731 				if (w->viewport) {
   731 				if (w->viewport) {
   732 					w->viewport->left += x;
   732 					w->viewport->left += x;
   733 					w->viewport->top += y;
   733 					w->viewport->top += y;
   734 				}
   734 				}
   735 			}
   735 			}
   736 				
   736 
   737 			// And also mark the new position dirty.
   737 			// And also mark the new position dirty.
   738 			SetWindowDirty(w);
   738 			SetWindowDirty(w);
   739 			return false;
   739 			return false;
   740 		}
   740 		}
   741 	}
   741 	}
   782 			// Abort if no button is clicked any more.
   782 			// Abort if no button is clicked any more.
   783 			if (!_left_button_down) {
   783 			if (!_left_button_down) {
   784 				w->flags4 &= ~WF_SCROLL_MIDDLE;
   784 				w->flags4 &= ~WF_SCROLL_MIDDLE;
   785 				SetWindowDirty(w);
   785 				SetWindowDirty(w);
   786 				break;
   786 				break;
   787 			}	
   787 			}
   788 
   788 
   789 			if (w->flags4 & WF_HSCROLL) {
   789 			if (w->flags4 & WF_HSCROLL) {
   790 				sb = &w->hscroll;
   790 				sb = &w->hscroll;
   791 				i = _cursor.pos.x - _cursorpos_drag_start.x;
   791 				i = _cursor.pos.x - _cursorpos_drag_start.x;
   792 			} else {
   792 			} else {
   799 			if (pos != sb->pos) {
   799 			if (pos != sb->pos) {
   800 				sb->pos = pos;
   800 				sb->pos = pos;
   801 				SetWindowDirty(w);
   801 				SetWindowDirty(w);
   802 			}
   802 			}
   803 			return false;
   803 			return false;
   804 		}		
   804 		}
   805 	}
   805 	}
   806 	
   806 
   807 	_scrolling_scrollbar = false;
   807 	_scrolling_scrollbar = false;
   808 	return false;
   808 	return false;
   809 }
   809 }
   810 
   810 
   811 bool HandleViewportScroll() 
   811 bool HandleViewportScroll()
   812 {
   812 {
   813 	Window *w;
   813 	Window *w;
   814 	ViewPort *vp;
   814 	ViewPort *vp;
   815 	int dx,dy, x, y, sub;
   815 	int dx,dy, x, y, sub;
   816 
   816 
   821 stop_capt:;
   821 stop_capt:;
   822 		_cursor.fix_at = false;
   822 		_cursor.fix_at = false;
   823 		_scrolling_viewport = false;
   823 		_scrolling_viewport = false;
   824 		return true;
   824 		return true;
   825 	}
   825 	}
   826 	
   826 
   827 	w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
   827 	w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
   828 	if (w == NULL) goto stop_capt;
   828 	if (w == NULL) goto stop_capt;
   829 
   829 
   830 	if (w->window_class != WC_SMALLMAP) {
   830 	if (w->window_class != WC_SMALLMAP) {
   831 		vp = IsPtInWindowViewport(w, _cursor.pos.x, _cursor.pos.y);
   831 		vp = IsPtInWindowViewport(w, _cursor.pos.x, _cursor.pos.y);
   836 		WP(w,vp_d).scrollpos_y += _cursor.delta.y << vp->zoom;
   836 		WP(w,vp_d).scrollpos_y += _cursor.delta.y << vp->zoom;
   837 		_cursor.delta.x = _cursor.delta.y = 0;
   837 		_cursor.delta.x = _cursor.delta.y = 0;
   838 		return false;
   838 		return false;
   839 	} else {
   839 	} else {
   840 		// scroll the smallmap ?
   840 		// scroll the smallmap ?
   841 		
   841 
   842 		_cursor.fix_at = true;
   842 		_cursor.fix_at = true;
   843 		
   843 
   844 		dx = _cursor.delta.x;
   844 		dx = _cursor.delta.x;
   845 		dy = _cursor.delta.y;
   845 		dy = _cursor.delta.y;
   846 
   846 
   847 		x = WP(w,smallmap_d).scroll_x;
   847 		x = WP(w,smallmap_d).scroll_x;
   848 		y = WP(w,smallmap_d).scroll_y;
   848 		y = WP(w,smallmap_d).scroll_y;
   902 		if (w->left + w->width <= u->left ||
   902 		if (w->left + w->width <= u->left ||
   903 				u->left + u->width <= w->left ||
   903 				u->left + u->width <= w->left ||
   904 				w->top  + w->height <= u->top ||
   904 				w->top  + w->height <= u->top ||
   905 				u->top + u->height <= w->top)
   905 				u->top + u->height <= w->top)
   906 					continue;
   906 					continue;
   907 		
   907 
   908 		return BringWindowToFront(w);	
   908 		return BringWindowToFront(w);
   909 	}
   909 	}
   910 
   910 
   911 	return w;
   911 	return w;
   912 }
   912 }
   913 
   913 
   914 static void HandleKeypress(uint32 key)
   914 static void HandleKeypress(uint32 key)
   915 {
   915 {
   916 	Window *w;
   916 	Window *w;
   917 	WindowEvent we;
   917 	WindowEvent we;
   918 	
   918 
   919 	// Setup event
   919 	// Setup event
   920 	we.keypress.event = WE_KEYPRESS;
   920 	we.keypress.event = WE_KEYPRESS;
   921 	we.keypress.ascii = key & 0xFF;
   921 	we.keypress.ascii = key & 0xFF;
   922 	we.keypress.keycode = key >> 16;
   922 	we.keypress.keycode = key >> 16;
   923 	we.keypress.cont = true;
   923 	we.keypress.cont = true;
   963 	mousewheel = 0;
   963 	mousewheel = 0;
   964 	if (_cursor.wheel) {
   964 	if (_cursor.wheel) {
   965 		mousewheel = _cursor.wheel;
   965 		mousewheel = _cursor.wheel;
   966 		_cursor.wheel = 0;
   966 		_cursor.wheel = 0;
   967 	}
   967 	}
   968 	
   968 
   969 	DecreaseWindowCounters();
   969 	DecreaseWindowCounters();
   970 	HandlePlacePresize();
   970 	HandlePlacePresize();
   971 	UpdateTileSelection();
   971 	UpdateTileSelection();
   972 	if (!VpHandlePlaceSizingDrag())
   972 	if (!VpHandlePlaceSizingDrag())
   973 		return;
   973 		return;
   984 	if (!HandleScrollbarScrolling())
   984 	if (!HandleScrollbarScrolling())
   985 		return;
   985 		return;
   986 
   986 
   987 	if (!HandleViewportScroll())
   987 	if (!HandleViewportScroll())
   988 		return;
   988 		return;
   989 	
   989 
   990 	x = _cursor.pos.x;
   990 	x = _cursor.pos.x;
   991 	y = _cursor.pos.y;
   991 	y = _cursor.pos.y;
   992 
   992 
   993 
   993 
   994 	if (click == 0 && mousewheel == 0) {
   994 	if (click == 0 && mousewheel == 0) {
  1003 #define scrollspeed 3
  1003 #define scrollspeed 3
  1004 				if (x-15<0) { WP(w,vp_d).scrollpos_x += (x-15) * scrollspeed << vp->zoom; }
  1004 				if (x-15<0) { WP(w,vp_d).scrollpos_x += (x-15) * scrollspeed << vp->zoom; }
  1005 				else if (15-(vp->width-x) > 0) { WP(w,vp_d).scrollpos_x += (15-(vp->width-x))*scrollspeed << vp->zoom; }
  1005 				else if (15-(vp->width-x) > 0) { WP(w,vp_d).scrollpos_x += (15-(vp->width-x))*scrollspeed << vp->zoom; }
  1006 				if (y-15<0) { WP(w,vp_d).scrollpos_y += (y-15)*scrollspeed << vp->zoom; }
  1006 				if (y-15<0) { WP(w,vp_d).scrollpos_y += (y-15)*scrollspeed << vp->zoom; }
  1007 				else if (15-(vp->height-y) > 0) { WP(w,vp_d).scrollpos_y += (15-(vp->height-y))*scrollspeed << vp->zoom; }
  1007 				else if (15-(vp->height-y) > 0) { WP(w,vp_d).scrollpos_y += (15-(vp->height-y))*scrollspeed << vp->zoom; }
  1008 #undef scrollspeed		
  1008 #undef scrollspeed
  1009 			}
  1009 			}
  1010 		}
  1010 		}
  1011 		return;
  1011 		return;
  1012 	}
  1012 	}
  1013 
  1013 
  1030 					// query button and place sign button work in pause mode
  1030 					// query button and place sign button work in pause mode
  1031 					!(_cursor.sprite == 0x2CF || _cursor.sprite == 0x2D2) &&
  1031 					!(_cursor.sprite == 0x2CF || _cursor.sprite == 0x2D2) &&
  1032 					_pause != 0 &&
  1032 					_pause != 0 &&
  1033 					!_cheats.build_in_pause.value)
  1033 					!_cheats.build_in_pause.value)
  1034 						return;
  1034 						return;
  1035 			
  1035 
  1036 			if (_thd.place_mode == 0) {
  1036 			if (_thd.place_mode == 0) {
  1037 				HandleViewportClicked(vp, x, y);
  1037 				HandleViewportClicked(vp, x, y);
  1038 			} else {
  1038 			} else {
  1039 				PlaceObject();
  1039 				PlaceObject();
  1040 			}
  1040 			}
  1090 		if (w->viewport != NULL)
  1090 		if (w->viewport != NULL)
  1091 			UpdateViewportPosition(w);
  1091 			UpdateViewportPosition(w);
  1092 	}
  1092 	}
  1093 	// Redraw mouse cursor in case it was hidden
  1093 	// Redraw mouse cursor in case it was hidden
  1094 	DrawMouseCursor();
  1094 	DrawMouseCursor();
  1095 }	
  1095 }
  1096 
  1096 
  1097 
  1097 
  1098 int GetMenuItemIndex(Window *w, int x, int y)
  1098 int GetMenuItemIndex(Window *w, int x, int y)
  1099 {
  1099 {
  1100 	if ((x -= w->left) >= 0 && x < w->width && (y -= w->top + 1) >= 0) {
  1100 	if ((x -= w->left) >= 0 && x < w->width && (y -= w->top + 1) >= 0) {
  1119 void InvalidateWidget(Window *w, byte widget_index)
  1119 void InvalidateWidget(Window *w, byte widget_index)
  1120 {
  1120 {
  1121 	const Widget *wi = &w->widget[widget_index];
  1121 	const Widget *wi = &w->widget[widget_index];
  1122 //	if (wi->left != -2) {
  1122 //	if (wi->left != -2) {
  1123 		SetDirtyBlocks(
  1123 		SetDirtyBlocks(
  1124 			w->left + wi->left, 
  1124 			w->left + wi->left,
  1125 			w->top + wi->top,
  1125 			w->top + wi->top,
  1126 			w->left + wi->right + 1,
  1126 			w->left + wi->right + 1,
  1127 			w->top + wi->bottom + 1);
  1127 			w->top + wi->bottom + 1);
  1128 //	}		
  1128 //	}
  1129 }
  1129 }
  1130 
  1130 
  1131 void InvalidateWindowWidget(byte cls, WindowNumber number, byte widget_index)
  1131 void InvalidateWindowWidget(byte cls, WindowNumber number, byte widget_index)
  1132 {
  1132 {
  1133 	Window *w;
  1133 	Window *w;
  1174 			w++;
  1174 			w++;
  1175 		}
  1175 		}
  1176 	}
  1176 	}
  1177 }
  1177 }
  1178 
  1178 
  1179 int PositionMainToolbar(Window *w) 
  1179 int PositionMainToolbar(Window *w)
  1180 {
  1180 {
  1181 	DEBUG(misc, 1) ("Repositioning Main Toolbar...");
  1181 	DEBUG(misc, 1) ("Repositioning Main Toolbar...");
  1182 
  1182 
  1183 	if (w == NULL || w->window_class != WC_MAIN_TOOLBAR)
  1183 	if (w == NULL || w->window_class != WC_MAIN_TOOLBAR)
  1184 		w = FindWindowById(WC_MAIN_TOOLBAR, 0);
  1184 		w = FindWindowById(WC_MAIN_TOOLBAR, 0);
  1196 {
  1196 {
  1197 	Window *w;
  1197 	Window *w;
  1198 
  1198 
  1199 	for(w=_windows; w!= _last_window ;w++) {
  1199 	for(w=_windows; w!= _last_window ;w++) {
  1200 		int left, top;
  1200 		int left, top;
  1201 	
  1201 
  1202 		if (w->window_class == WC_MAIN_WINDOW) {
  1202 		if (w->window_class == WC_MAIN_WINDOW) {
  1203 			ViewPort *vp = w->viewport;
  1203 			ViewPort *vp = w->viewport;
  1204 			vp->width = w->width = neww;
  1204 			vp->width = w->width = neww;
  1205 			vp->height = w->height = newh;
  1205 			vp->height = w->height = newh;
  1206 			vp->virtual_width = neww << vp->zoom;
  1206 			vp->virtual_width = neww << vp->zoom;
  1211 		IConsoleResize();
  1211 		IConsoleResize();
  1212 
  1212 
  1213 		if (w->window_class == WC_MAIN_TOOLBAR) {
  1213 		if (w->window_class == WC_MAIN_TOOLBAR) {
  1214 			top = w->top;
  1214 			top = w->top;
  1215 			left = PositionMainToolbar(w); // changes toolbar orientation
  1215 			left = PositionMainToolbar(w); // changes toolbar orientation
  1216 		} else if (w->window_class == WC_SELECT_GAME || w->window_class == WC_GAME_OPTIONS || w->window_class == WC_NETWORK_WINDOW){	
  1216 		} else if (w->window_class == WC_SELECT_GAME || w->window_class == WC_GAME_OPTIONS || w->window_class == WC_NETWORK_WINDOW){
  1217 			top = (newh - w->height) >> 1;
  1217 			top = (newh - w->height) >> 1;
  1218 			left = (neww - w->width) >> 1;
  1218 			left = (neww - w->width) >> 1;
  1219 		} else if (w->window_class == WC_NEWS_WINDOW) {
  1219 		} else if (w->window_class == WC_NEWS_WINDOW) {
  1220 			top = newh - w->height;
  1220 			top = newh - w->height;
  1221 			left = (neww - w->width) >> 1;
  1221 			left = (neww - w->width) >> 1;
  1222 		} else if (w->window_class == WC_STATUS_BAR) {
  1222 		} else if (w->window_class == WC_STATUS_BAR) {
  1223 			top = newh - w->height;
  1223 			top = newh - w->height;
  1224 			left = (neww - w->width) >> 1;	
  1224 			left = (neww - w->width) >> 1;
  1225 		} else {
  1225 		} else {
  1226 			left = w->left;
  1226 			left = w->left;
  1227 			if (left + (w->width>>1) >= neww) left = neww - w->width;
  1227 			if (left + (w->width>>1) >= neww) left = neww - w->width;
  1228 			top = w->top;
  1228 			top = w->top;
  1229 			if (top + (w->height>>1) >= newh) top = newh - w->height;
  1229 			if (top + (w->height>>1) >= newh) top = newh - w->height;