src/widget.cpp
branchNewGRF_ports
changeset 6871 5a9dc001e1ad
parent 6719 4cc327ad39d5
child 6872 1c4a4a609f85
--- a/src/widget.cpp	Sat Oct 06 21:16:00 2007 +0000
+++ b/src/widget.cpp	Mon Dec 03 23:39:38 2007 +0000
@@ -140,7 +140,7 @@
 		if (wi->type == WWT_EMPTY || wi->type == WWT_FRAME) continue;
 
 		if (x >= wi->left && x <= wi->right && y >= wi->top &&  y <= wi->bottom &&
-				!IsWindowWidgetHidden(w, index)) {
+				!w->IsWidgetHidden(index)) {
 			found_index = index;
 		}
 	}
@@ -184,25 +184,24 @@
 void DrawWindowWidgets(const Window *w)
 {
 	const DrawPixelInfo* dpi = _cur_dpi;
-	Rect r;
-	uint i;
 
-	for (i = 0; i < w->widget_count; i++) {
+	for (uint i = 0; i < w->widget_count; i++) {
 		const Widget *wi = &w->widget[i];
-		bool clicked = IsWindowWidgetLowered(w, i);
+		bool clicked = w->IsWidgetLowered(i);
+		Rect r;
 
-		if (dpi->left > (r.right=/*w->left + */wi->right) ||
-				dpi->left + dpi->width <= (r.left=wi->left/* + w->left*/) ||
-				dpi->top > (r.bottom=/*w->top +*/ wi->bottom) ||
-				dpi->top + dpi->height <= (r.top = /*w->top +*/ wi->top) ||
-				IsWindowWidgetHidden(w, i)) {
+		if (dpi->left > (r.right = wi->right) ||
+				dpi->left + dpi->width <= (r.left = wi->left) ||
+				dpi->top > (r.bottom = wi->bottom) ||
+				dpi->top + dpi->height <= (r.top = wi->top) ||
+				w->IsWidgetHidden(i)) {
 			continue;
 		}
 
 		switch (wi->type & WWT_MASK) {
 		case WWT_IMGBTN:
 		case WWT_IMGBTN_2: {
-			int img = wi->data;
+			SpriteID img = wi->data;
 			assert(img != 0);
 			DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
 
@@ -229,22 +228,22 @@
 
 			if ((wi->type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
 
-			DrawStringCentered(((r.left + r.right + 1) >> 1) + clicked, ((r.top + r.bottom + 1) >> 1) - 5 + clicked, str, 0);
+			DrawStringCentered(((r.left + r.right + 1) >> 1) + clicked, ((r.top + r.bottom + 1) >> 1) - 5 + clicked, str, TC_FROMSTRING);
 			goto draw_default;
 		}
 
 		case WWT_TEXT: {
-			StringID str = wi->data;
+			const StringID str = wi->data;
 
 			if (str != STR_NULL) DrawStringTruncated(r.left, r.top, str, wi->color, r.right - r.left);
 			break;
 		}
 
 		case WWT_INSET: {
-			StringID str = wi->data;
+			const StringID str = wi->data;
 			DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_LOWERED | FR_DARKENED);
 
-			if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 1, str, 0, r.right - r.left - 10);
+			if (str != STR_NULL) DrawStringTruncated(r.left + 2, r.top + 1, str, TC_FROMSTRING, r.right - r.left - 10);
 			goto draw_default;
 		}
 
@@ -275,7 +274,7 @@
 				GfxFillRect(r.left + 1, x, r.right - 1, x, color);
 			}
 
-			color = _colour_gradient[wi->color&0xF][4];
+			color = _colour_gradient[wi->color & 0xF][4];
 
 			x = r.left - 1;
 			for (ctr = c; ctr > 1; ctr--) {
@@ -286,7 +285,7 @@
 			x = r.top - 1;
 			for (ctr = d; ctr > 1; ctr--) {
 				x += amt2;
-				GfxFillRect(r.left+1, x, r.right-1, x, color);
+				GfxFillRect(r.left + 1, x, r.right - 1, x, color);
 			}
 
 			goto draw_default;
@@ -295,31 +294,32 @@
 		/* vertical scrollbar */
 		case WWT_SCROLLBAR: {
 			Point pt;
-			int c1,c2;
+			int c1, c2;
 
+			assert(wi->data == 0);
 			assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
 
 			/* draw up/down buttons */
 			clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP);
 			DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
-			DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, 0x10);
+			DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK);
 
 			clicked = (((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN));
 			DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
-			DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, 0x10);
+			DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK);
 
-			c1 = _colour_gradient[wi->color&0xF][3];
-			c2 = _colour_gradient[wi->color&0xF][7];
+			c1 = _colour_gradient[wi->color & 0xF][3];
+			c2 = _colour_gradient[wi->color & 0xF][7];
 
 			/* draw "shaded" background */
-			GfxFillRect(r.left, r.top+10, r.right, r.bottom-10, c2);
-			GfxFillRect(r.left, r.top+10, r.right, r.bottom-10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
+			GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
+			GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
 
 			/* draw shaded lines */
-			GfxFillRect(r.left+2, r.top+10, r.left+2, r.bottom-10, c1);
-			GfxFillRect(r.left+3, r.top+10, r.left+3, r.bottom-10, c2);
-			GfxFillRect(r.left+7, r.top+10, r.left+7, r.bottom-10, c1);
-			GfxFillRect(r.left+8, r.top+10, r.left+8, r.bottom-10, c2);
+			GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
+			GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
+			GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
+			GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
 
 			pt = HandleScrollbarHittest(&w->vscroll, r.top, r.bottom);
 			DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_MIDDLE ? FR_LOWERED : FR_NONE);
@@ -327,31 +327,32 @@
 		}
 		case WWT_SCROLL2BAR: {
 			Point pt;
-			int c1,c2;
+			int c1, c2;
 
+			assert(wi->data == 0);
 			assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
 
 			/* draw up/down buttons */
 			clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2));
 			DrawFrameRect(r.left, r.top, r.right, r.top + 9, wi->color,  (clicked) ? FR_LOWERED : FR_NONE);
-			DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, 0x10);
+			DoDrawString(UPARROW, r.left + 2 + clicked, r.top + clicked, TC_BLACK);
 
 			clicked = ((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2));
 			DrawFrameRect(r.left, r.bottom - 9, r.right, r.bottom, wi->color,  (clicked) ? FR_LOWERED : FR_NONE);
-			DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, 0x10);
+			DoDrawString(DOWNARROW, r.left + 2 + clicked, r.bottom - 9 + clicked, TC_BLACK);
 
-			c1 = _colour_gradient[wi->color&0xF][3];
-			c2 = _colour_gradient[wi->color&0xF][7];
+			c1 = _colour_gradient[wi->color & 0xF][3];
+			c2 = _colour_gradient[wi->color & 0xF][7];
 
 			/* draw "shaded" background */
-			GfxFillRect(r.left, r.top+10, r.right, r.bottom-10, c2);
-			GfxFillRect(r.left, r.top+10, r.right, r.bottom-10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
+			GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c2);
+			GfxFillRect(r.left, r.top + 10, r.right, r.bottom - 10, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
 
 			/* draw shaded lines */
-			GfxFillRect(r.left+2, r.top+10, r.left+2, r.bottom-10, c1);
-			GfxFillRect(r.left+3, r.top+10, r.left+3, r.bottom-10, c2);
-			GfxFillRect(r.left+7, r.top+10, r.left+7, r.bottom-10, c1);
-			GfxFillRect(r.left+8, r.top+10, r.left+8, r.bottom-10, c2);
+			GfxFillRect(r.left + 2, r.top + 10, r.left + 2, r.bottom - 10, c1);
+			GfxFillRect(r.left + 3, r.top + 10, r.left + 3, r.bottom - 10, c2);
+			GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
+			GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
 
 			pt = HandleScrollbarHittest(&w->vscroll2, r.top, r.bottom);
 			DrawFrameRect(r.left, pt.x, r.right, pt.y, wi->color, (w->flags4 & (WF_SCROLL_MIDDLE | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_MIDDLE | WF_SCROLL2) ? FR_LOWERED : FR_NONE);
@@ -361,8 +362,9 @@
 		/* horizontal scrollbar */
 		case WWT_HSCROLLBAR: {
 			Point pt;
-			int c1,c2;
+			int c1, c2;
 
+			assert(wi->data == 0);
 			assert(r.bottom - r.top == 11); // XXX - to ensure the same sizes are used everywhere!
 
 			clicked = ((w->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL));
@@ -370,21 +372,21 @@
 			DrawSprite(SPR_ARROW_LEFT, PAL_NONE, r.left + 1 + clicked, r.top + 1 + clicked);
 
 			clicked = ((w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL));
-			DrawFrameRect(r.right-9, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
+			DrawFrameRect(r.right - 9, r.top, r.right, r.bottom, wi->color, (clicked) ? FR_LOWERED : FR_NONE);
 			DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, r.right - 8 + clicked, r.top + 1 + clicked);
 
-			c1 = _colour_gradient[wi->color&0xF][3];
-			c2 = _colour_gradient[wi->color&0xF][7];
+			c1 = _colour_gradient[wi->color & 0xF][3];
+			c2 = _colour_gradient[wi->color & 0xF][7];
 
 			/* draw "shaded" background */
-			GfxFillRect(r.left+10, r.top, r.right-10, r.bottom, c2);
-			GfxFillRect(r.left+10, r.top, r.right-10, r.bottom, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
+			GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c2);
+			GfxFillRect(r.left + 10, r.top, r.right - 10, r.bottom, c1 | (1 << PALETTE_MODIFIER_GREYOUT));
 
 			/* draw shaded lines */
-			GfxFillRect(r.left+10, r.top+2, r.right-10, r.top+2, c1);
-			GfxFillRect(r.left+10, r.top+3, r.right-10, r.top+3, c2);
-			GfxFillRect(r.left+10, r.top+7, r.right-10, r.top+7, c1);
-			GfxFillRect(r.left+10, r.top+8, r.right-10, r.top+8, c2);
+			GfxFillRect(r.left + 10, r.top + 2, r.right - 10, r.top + 2, c1);
+			GfxFillRect(r.left + 10, r.top + 3, r.right - 10, r.top + 3, c2);
+			GfxFillRect(r.left + 10, r.top + 7, r.right - 10, r.top + 7, c1);
+			GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2);
 
 			/* draw actual scrollbar */
 			pt = HandleScrollbarHittest(&w->hscroll, r.left, r.right);
@@ -394,37 +396,39 @@
 		}
 
 		case WWT_FRAME: {
-			int c1,c2;
+			const StringID str = wi->data;
+			int c1, c2;
 			int x2 = r.left; // by default the left side is the left side of the widget
 
-			if (wi->data != 0) x2 = DrawString(r.left + 6, r.top, wi->data, 0);
+			if (str != STR_NULL) x2 = DrawString(r.left + 6, r.top, str, TC_FROMSTRING);
 
 			c1 = _colour_gradient[wi->color][3];
 			c2 = _colour_gradient[wi->color][7];
 
-			/*Line from upper left corner to start of text */
-			GfxFillRect(r.left, r.top+4, r.left+4,r.top+4, c1);
-			GfxFillRect(r.left+1, r.top+5, r.left+4,r.top+5, c2);
+			/* Line from upper left corner to start of text */
+			GfxFillRect(r.left, r.top + 4, r.left + 4, r.top + 4, c1);
+			GfxFillRect(r.left + 1, r.top + 5, r.left + 4, r.top + 5, c2);
 
 			/* Line from end of text to upper right corner */
-			GfxFillRect(x2, r.top+4, r.right-1,r.top+4,c1);
-			GfxFillRect(x2, r.top+5, r.right-2,r.top+5,c2);
+			GfxFillRect(x2, r.top + 4, r.right - 1, r.top + 4, c1);
+			GfxFillRect(x2, r.top + 5, r.right - 2, r.top + 5, c2);
 
 			/* Line from upper left corner to bottom left corner */
-			GfxFillRect(r.left, r.top+5, r.left, r.bottom-1, c1);
-			GfxFillRect(r.left+1, r.top+6, r.left+1, r.bottom-2, c2);
+			GfxFillRect(r.left, r.top + 5, r.left, r.bottom - 1, c1);
+			GfxFillRect(r.left + 1, r.top + 6, r.left + 1, r.bottom - 2, c2);
 
 			/*Line from upper right corner to bottom right corner */
-			GfxFillRect(r.right-1, r.top+5, r.right-1, r.bottom-2, c1);
-			GfxFillRect(r.right, r.top+4, r.right, r.bottom-1, c2);
+			GfxFillRect(r.right - 1, r.top + 5, r.right - 1, r.bottom - 2, c1);
+			GfxFillRect(r.right, r.top + 4, r.right, r.bottom - 1, c2);
 
-			GfxFillRect(r.left+1, r.bottom-1, r.right-1, r.bottom-1, c1);
+			GfxFillRect(r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1, c1);
 			GfxFillRect(r.left, r.bottom, r.right, r.bottom, c2);
 
 			goto draw_default;
 		}
 
 		case WWT_STICKYBOX: {
+			assert(wi->data == 0);
 			assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
 
 			clicked = !!(w->flags4 & WF_STICKY);
@@ -434,6 +438,7 @@
 		}
 
 		case WWT_RESIZEBOX: {
+			assert(wi->data == 0);
 			assert(r.right - r.left == 11); // XXX - to ensure the same sizes are used everywhere!
 
 			clicked = !!(w->flags4 & WF_SIZING);
@@ -443,26 +448,29 @@
 		}
 
 		case WWT_CLOSEBOX: {
+			const StringID str = wi->data;
+
+			assert(str == STR_00C5 || str == STR_00C6); // black or silver cross
 			assert(r.right - r.left == 10); // ensure the same sizes are used everywhere
 
 			DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_NONE);
-			DrawString(r.left + 2, r.top + 2, STR_00C5, 0);
+			DrawString(r.left + 2, r.top + 2, str, TC_FROMSTRING);
 			break;
 		}
 
 		case WWT_CAPTION: {
 			assert(r.bottom - r.top == 13); // XXX - to ensure the same sizes are used everywhere!
 			DrawFrameRect(r.left, r.top, r.right, r.bottom, wi->color, FR_BORDERONLY);
-			DrawFrameRect(r.left+1, r.top+1, r.right-1, r.bottom-1, wi->color, (w->caption_color == 0xFF) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY);
+			DrawFrameRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, wi->color, (w->caption_color == 0xFF) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY);
 
 			if (w->caption_color != 0xFF) {
-				GfxFillRect(r.left+2, r.top+2, r.right-2, r.bottom-2, _colour_gradient[_player_colors[w->caption_color]][4]);
+				GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_player_colors[w->caption_color]][4]);
 			}
 
-			DrawStringCenteredTruncated(r.left + 2, r.right - 2, r.top+2, wi->data, 0x84);
+			DrawStringCenteredTruncated(r.left + 2, r.right - 2, r.top + 2, wi->data, 0x84);
 draw_default:;
-			if (IsWindowWidgetDisabled(w, i)) {
-				GfxFillRect(r.left+1, r.top+1, r.right-1, r.bottom-1, _colour_gradient[wi->color&0xF][2] | (1 << PALETTE_MODIFIER_GREYOUT));
+			if (w->IsWidgetDisabled(i)) {
+				GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[wi->color & 0xF][2] | (1 << PALETTE_MODIFIER_GREYOUT));
 			}
 		}
 		}
@@ -470,8 +478,7 @@
 
 
 	if (w->flags4 & WF_WHITE_BORDER_MASK) {
-		//DrawFrameRect(w->left, w->top, w->left + w->width-1, w->top+w->height-1, 0xF, 0x10);
-		DrawFrameRect(0, 0, w->width-1, w->height-1, 0xF, FR_BORDERONLY);
+		DrawFrameRect(0, 0, w->width - 1, w->height - 1, 0xF, FR_BORDERONLY);
 	}
 
 }
@@ -496,12 +503,12 @@
 		return - 1;
 
 	item = y / 10;
-	if (item >= WP(w,dropdown_d).num_items || (HASBIT(WP(w,dropdown_d).disabled_state, item) && !HASBIT(WP(w,dropdown_d).hidden_state, item)) || WP(w,dropdown_d).items[item] == 0)
+	if (item >= WP(w,dropdown_d).num_items || (HasBit(WP(w,dropdown_d).disabled_state, item) && !HasBit(WP(w,dropdown_d).hidden_state, item)) || WP(w,dropdown_d).items[item] == 0)
 		return - 1;
 
 	/* Skip hidden items -- +1 for each hidden item before the clicked item. */
 	for (counter = 0; item >= counter; ++counter)
-		if (HASBIT(WP(w,dropdown_d).hidden_state, counter)) item++;
+		if (HasBit(WP(w,dropdown_d).hidden_state, counter)) item++;
 
 	return item;
 }
@@ -525,14 +532,14 @@
 			height = w->widget[0].bottom - 3;
 
 			for (i = 0; WP(w,dropdown_d).items[i] != INVALID_STRING_ID; i++, sel--) {
-				if (HASBIT(WP(w,dropdown_d).hidden_state, i)) continue;
+				if (HasBit(WP(w,dropdown_d).hidden_state, i)) continue;
 
 				if (y >= 0 && y <= height) {
 					if (WP(w,dropdown_d).items[i] != STR_NULL) {
 						if (sel == 0) GfxFillRect(x + 1, y, x + width, y + 9, 0);
-						DrawStringTruncated(x + 2, y, WP(w,dropdown_d).items[i], sel == 0 ? 12 : 16, x + width);
+						DrawStringTruncated(x + 2, y, WP(w,dropdown_d).items[i], sel == 0 ? TC_WHITE : TC_BLACK, x + width);
 
-						if (HASBIT(WP(w,dropdown_d).disabled_state, i)) {
+						if (HasBit(WP(w,dropdown_d).disabled_state, i)) {
 							GfxFillRect(x, y, x + width, y + 9,
 								(1 << PALETTE_MODIFIER_GREYOUT) | _colour_gradient[_dropdown_menu_widgets[0].color][5]
 							);
@@ -595,7 +602,7 @@
 		case WE_DESTROY: {
 			Window *w2 = FindWindowById(WP(w,dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
 			if (w2 != NULL) {
-				RaiseWindowWidget(w2, WP(w,dropdown_d).parent_button);
+				w2->RaiseWidget(WP(w,dropdown_d).parent_button);
 				InvalidateWidget(w2, WP(w,dropdown_d).parent_button);
 			}
 		} break;
@@ -608,7 +615,7 @@
 	const Widget *wi;
 	Window *w2;
 	const Window *w3;
-	bool is_dropdown_menu_shown = IsWindowWidgetLowered(w, button);
+	bool is_dropdown_menu_shown = w->IsWidgetLowered(button);
 	int top, height;
 	int screen_top, screen_bottom;
 	bool scroll = false;
@@ -617,7 +624,7 @@
 
 	if (is_dropdown_menu_shown) return;
 
-	LowerWindowWidget(w, button);
+	w->LowerWidget(button);
 
 	InvalidateWidget(w, button);
 
@@ -630,7 +637,7 @@
 		uint j;
 
 		for (j = 0; strings[j] != INVALID_STRING_ID; j++) {
-			if (HASBIT(hidden_mask, j)) i--;
+			if (HasBit(hidden_mask, j)) i--;
 		}
 	}
 
@@ -671,7 +678,7 @@
 	w2->widget[0].right = wi->right - wi[-1].left;
 	w2->widget[0].bottom = height - 1;
 
-	SetWindowWidgetHiddenState(w2, 1, !scroll);
+	w2->SetWidgetHiddenState(1, !scroll);
 
 	if (scroll) {
 		/* We're scrolling, so enable the scroll bar and shrink the list by