src/news_gui.cpp
changeset 10610 dfb846e37be3
parent 10607 1ab30cead125
child 10637 1ea14a417a37
equal deleted inserted replaced
10609:b6ebc6c30a83 10610:dfb846e37be3
   700 	/* Truncate and show string; postfixed by '...' if neccessary */
   700 	/* Truncate and show string; postfixed by '...' if neccessary */
   701 	DoDrawStringTruncated(buffer2, x, y, color, maxw);
   701 	DoDrawStringTruncated(buffer2, x, y, color, maxw);
   702 }
   702 }
   703 
   703 
   704 
   704 
   705 static void MessageHistoryWndProc(Window *w, WindowEvent *e)
   705 struct MessageHistoryWindow : Window {
   706 {
   706 	MessageHistoryWindow(const WindowDesc *desc) : Window(desc)
   707 	switch (e->event) {
   707 	{
   708 		case WE_PAINT: {
   708 		this->vscroll.cap = 10;
   709 			int y = 19;
   709 		this->vscroll.count = _total_news;
   710 
   710 		this->resize.step_height = 12;
   711 			SetVScrollCount(w, _total_news);
   711 		this->resize.height = this->height - 12 * 6; // minimum of 4 items in the list, each item 12 high
   712 			w->DrawWidgets();
   712 		this->resize.step_width = 1;
   713 
   713 		this->resize.width = 200; // can't make window any smaller than 200 pixel
   714 			if (_total_news == 0) break;
   714 
   715 			NewsID show = min(_total_news, w->vscroll.cap);
   715 		this->FindWindowPlacementAndResize(desc);
   716 
   716 	}
   717 			for (NewsID p = w->vscroll.pos; p < w->vscroll.pos + show; p++) {
   717 
   718 				/* get news in correct order */
   718 	virtual void OnPaint()
   719 				const NewsItem *ni = &_news_items[getNews(p)];
   719 	{
   720 
   720 		int y = 19;
   721 				SetDParam(0, ni->date);
   721 
   722 				DrawString(4, y, STR_SHORT_DATE, TC_WHITE);
   722 		SetVScrollCount(this, _total_news);
   723 
   723 		this->DrawWidgets();
   724 				DrawNewsString(82, y, TC_WHITE, ni, w->width - 95);
   724 
   725 				y += 12;
   725 		if (_total_news == 0) return;
   726 			}
   726 		NewsID show = min(_total_news, this->vscroll.cap);
   727 			break;
   727 
   728 		}
   728 		for (NewsID p = this->vscroll.pos; p < this->vscroll.pos + show; p++) {
   729 
   729 			/* get news in correct order */
   730 		case WE_CLICK:
   730 			const NewsItem *ni = &_news_items[getNews(p)];
   731 			if (e->we.click.widget == 3) {
   731 
   732 				int y = (e->we.click.pt.y - 19) / 12;
   732 			SetDParam(0, ni->date);
   733 				NewsID p = getNews(y + w->vscroll.pos);
   733 			DrawString(4, y, STR_SHORT_DATE, TC_WHITE);
   734 
   734 
   735 				if (p != INVALID_NEWS) ShowNewsMessage(p);
   735 			DrawNewsString(82, y, TC_WHITE, ni, this->width - 95);
   736 			}
   736 			y += 12;
   737 			break;
   737 		}
   738 
   738 	}
   739 		case WE_RESIZE:
   739 
   740 			w->vscroll.cap += e->we.sizing.diff.y / 12;
   740 	virtual void OnClick(Point pt, int widget)
   741 			break;
   741 	{
   742 	}
   742 		if (widget == 3) {
   743 }
   743 			int y = (pt.y - 19) / 12;
       
   744 			NewsID p = getNews(y + this->vscroll.pos);
       
   745 
       
   746 			if (p != INVALID_NEWS) ShowNewsMessage(p);
       
   747 		}
       
   748 	}
       
   749 
       
   750 	virtual void OnResize(Point new_size, Point delta)
       
   751 	{
       
   752 		this->vscroll.cap += delta.y / 12;
       
   753 	}
       
   754 };
   744 
   755 
   745 static const Widget _message_history_widgets[] = {
   756 static const Widget _message_history_widgets[] = {
   746 {   WWT_CLOSEBOX,   RESIZE_NONE,    13,     0,    10,     0,    13, STR_00C5,            STR_018B_CLOSE_WINDOW},
   757 {   WWT_CLOSEBOX,   RESIZE_NONE,    13,     0,    10,     0,    13, STR_00C5,            STR_018B_CLOSE_WINDOW},
   747 {    WWT_CAPTION,  RESIZE_RIGHT,    13,    11,   387,     0,    13, STR_MESSAGE_HISTORY, STR_018C_WINDOW_TITLE_DRAG_THIS},
   758 {    WWT_CAPTION,  RESIZE_RIGHT,    13,    11,   387,     0,    13, STR_MESSAGE_HISTORY, STR_018C_WINDOW_TITLE_DRAG_THIS},
   748 {  WWT_STICKYBOX,     RESIZE_LR,    13,   388,   399,     0,    13, 0x0,                 STR_STICKY_BUTTON},
   759 {  WWT_STICKYBOX,     RESIZE_LR,    13,   388,   399,     0,    13, 0x0,                 STR_STICKY_BUTTON},
   755 static const WindowDesc _message_history_desc = {
   766 static const WindowDesc _message_history_desc = {
   756 	240, 22, 400, 140, 400, 140,
   767 	240, 22, 400, 140, 400, 140,
   757 	WC_MESSAGE_HISTORY, WC_NONE,
   768 	WC_MESSAGE_HISTORY, WC_NONE,
   758 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
   769 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
   759 	_message_history_widgets,
   770 	_message_history_widgets,
   760 	MessageHistoryWndProc
   771 	NULL
   761 };
   772 };
   762 
   773 
   763 /** Display window with news messages history */
   774 /** Display window with news messages history */
   764 void ShowMessageHistory()
   775 void ShowMessageHistory()
   765 {
   776 {
   766 	DeleteWindowById(WC_MESSAGE_HISTORY, 0);
   777 	DeleteWindowById(WC_MESSAGE_HISTORY, 0);
   767 	Window *w = new Window(&_message_history_desc);
   778 	new MessageHistoryWindow(&_message_history_desc);
   768 
       
   769 	if (w == NULL) return;
       
   770 
       
   771 	w->vscroll.cap = 10;
       
   772 	w->vscroll.count = _total_news;
       
   773 	w->resize.step_height = 12;
       
   774 	w->resize.height = w->height - 12 * 6; // minimum of 4 items in the list, each item 12 high
       
   775 	w->resize.step_width = 1;
       
   776 	w->resize.width = 200; // can't make window any smaller than 200 pixel
       
   777 	w->SetDirty();
       
   778 }
   779 }
   779 
   780 
   780 
   781 
   781 /** News settings window widget offset constants */
   782 /** News settings window widget offset constants */
   782 enum {
   783 enum {