changeset 20454:872dc0744f21 draft

(svn r25412) -Codechange: Make use of Window::OnHotkey
author frosch <frosch@openttd.org>
date Sat, 15 Jun 2013 15:30:44 +0000
parents d770544389f0
children 41b160e93ab2
files src/ai/ai_gui.cpp src/airport_gui.cpp src/dock_gui.cpp src/main_gui.cpp src/order_gui.cpp src/rail_gui.cpp src/road_gui.cpp src/signs_gui.cpp src/terraform_gui.cpp src/toolbar_gui.cpp
diffstat 10 files changed, 77 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -1306,23 +1306,6 @@
 		}
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
-	{
-		EventState state = ES_NOT_HANDLED;
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num != -1) {
-			if (this->show_break_box && num == WID_AID_BREAK_STR_EDIT_BOX) {
-				this->SetFocusedWidget(WID_AID_BREAK_STR_EDIT_BOX);
-				SetFocusedWindow(this);
-				state = ES_HANDLED;
-			} else if (this->show_break_box || num < WID_AID_BREAK_STRING_WIDGETS) {
-				this->OnClick(Point(), num, 1);
-				state = ES_HANDLED;
-			}
-		}
-		return state;
-	}
-
 	virtual void OnEditboxChanged(int wid)
 	{
 		if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
@@ -1494,7 +1477,8 @@
 	WDP_AUTO, "script_debug", 600, 450,
 	WC_AI_DEBUG, WC_NONE,
 	0,
-	_nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
+	_nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets),
+	&AIDebugWindow::hotkeys
 );
 
 /**
@@ -1524,7 +1508,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowAIDebugWindow(INVALID_COMPANY);
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 /**
--- a/src/airport_gui.cpp
+++ b/src/airport_gui.cpp
@@ -100,14 +100,6 @@
 	}
 
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
-	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
-		return ES_HANDLED;
-	}
-
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
 	{
 		switch (this->last_user_action) {
@@ -170,7 +162,8 @@
 	WDP_ALIGN_TOOLBAR, "toolbar_air", 0, 0,
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_air_toolbar_widgets, lengthof(_nested_air_toolbar_widgets)
+	_nested_air_toolbar_widgets, lengthof(_nested_air_toolbar_widgets),
+	&BuildAirToolbarWindow::hotkeys
 );
 
 /**
@@ -195,7 +188,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowBuildAirToolbar();
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 class BuildAirportWindow : public PickerWindowBase {
--- a/src/dock_gui.cpp
+++ b/src/dock_gui.cpp
@@ -164,14 +164,6 @@
 		this->last_clicked_widget = (DockToolbarWidgets)widget;
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
-	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
-		return ES_HANDLED;
-	}
-
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
 	{
 		switch (this->last_clicked_widget) {
@@ -318,7 +310,8 @@
 	WDP_ALIGN_TOOLBAR, "toolbar_water", 0, 0,
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_build_docks_toolbar_widgets, lengthof(_nested_build_docks_toolbar_widgets)
+	_nested_build_docks_toolbar_widgets, lengthof(_nested_build_docks_toolbar_widgets),
+	&BuildDocksToolbarWindow::hotkeys
 );
 
 /**
@@ -342,7 +335,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowBuildDocksToolbar();
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 /**
--- a/src/main_gui.cpp
+++ b/src/main_gui.cpp
@@ -206,13 +206,6 @@
 	NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_M_VIEWPORT), SetResize(1, 1),
 };
 
-static WindowDesc _main_window_desc(
-	WDP_MANUAL, NULL, 0, 0,
-	WC_MAIN_WINDOW, WC_NONE,
-	0,
-	_nested_main_window_widgets, lengthof(_nested_main_window_widgets)
-);
-
 enum {
 	GHK_QUIT,
 	GHK_ABANDON,
@@ -245,7 +238,7 @@
 	static const uint LINKGRAPH_REFRESH_PERIOD = 0xff;
 	static const uint LINKGRAPH_DELAY = 0xf;
 
-	MainWindow() : Window(&_main_window_desc)
+	MainWindow(WindowDesc *desc) : Window(desc)
 	{
 		this->InitNested(0);
 		CLRBITS(this->flags, WF_WHITE_BORDER);
@@ -287,10 +280,9 @@
 		}
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == GHK_QUIT) {
+		if (hotkey == GHK_QUIT) {
 			HandleExitGameRequest();
 			return ES_HANDLED;
 		}
@@ -301,7 +293,7 @@
 		 * assertions that are hard to trigger and debug */
 		if (HasModalProgress()) return ES_NOT_HANDLED;
 
-		switch (num) {
+		switch (hotkey) {
 			case GHK_ABANDON:
 				/* No point returning from the main menu to itself */
 				if (_game_mode == GM_MENU) return ES_HANDLED;
@@ -328,13 +320,13 @@
 
 		if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
 
-		switch (num) {
+		switch (hotkey) {
 			case GHK_CENTER:
 			case GHK_CENTER_ZOOM: {
 				Point pt = GetTileBelowCursor();
 				if (pt.x != -1) {
-					bool instant = (num == GHK_CENTER_ZOOM && this->viewport->zoom != _settings_client.gui.zoom_min);
-					if (num == GHK_CENTER_ZOOM) MaxZoomInOut(ZOOM_IN, this);
+					bool instant = (hotkey == GHK_CENTER_ZOOM && this->viewport->zoom != _settings_client.gui.zoom_min);
+					if (hotkey == GHK_CENTER_ZOOM) MaxZoomInOut(ZOOM_IN, this);
 					ScrollMainWindowTo(pt.x, pt.y, -1, instant);
 				}
 				break;
@@ -368,7 +360,7 @@
 			case GHK_TOGGLE_TRANSPARENCY + 7:
 			case GHK_TOGGLE_TRANSPARENCY + 8:
 				/* Transparency toggle hot keys */
-				ToggleTransparency((TransparencyOption)(num - GHK_TOGGLE_TRANSPARENCY));
+				ToggleTransparency((TransparencyOption)(hotkey - GHK_TOGGLE_TRANSPARENCY));
 				MarkWholeScreenDirty();
 				break;
 
@@ -381,7 +373,7 @@
 			case GHK_TOGGLE_INVISIBILITY + 6:
 			case GHK_TOGGLE_INVISIBILITY + 7:
 				/* Invisibility toggle hot keys */
-				ToggleInvisibilityWithTransparency((TransparencyOption)(num - GHK_TOGGLE_INVISIBILITY));
+				ToggleInvisibilityWithTransparency((TransparencyOption)(hotkey - GHK_TOGGLE_INVISIBILITY));
 				MarkWholeScreenDirty();
 				break;
 
@@ -521,6 +513,14 @@
 };
 HotkeyList MainWindow::hotkeys("global", global_hotkeys);
 
+static WindowDesc _main_window_desc(
+	WDP_MANUAL, NULL, 0, 0,
+	WC_MAIN_WINDOW, WC_NONE,
+	0,
+	_nested_main_window_widgets, lengthof(_nested_main_window_widgets),
+	&MainWindow::hotkeys
+);
+
 /**
  * Does the given keycode match one of the keycodes bound to 'quit game'?
  * @param keycode The keycode that was pressed by the user.
@@ -547,7 +547,7 @@
 		memcpy(_colour_gradient[i], b + 0xC6, sizeof(_colour_gradient[i]));
 	}
 
-	new MainWindow;
+	new MainWindow(&_main_window_desc);
 
 	/* XXX: these are not done */
 	switch (_game_mode) {
--- a/src/order_gui.cpp
+++ b/src/order_gui.cpp
@@ -1436,11 +1436,11 @@
 		}
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
 		if (this->vehicle->owner != _local_company) return ES_NOT_HANDLED;
 
-		switch (this->hotkeys.CheckMatch(keycode)) {
+		switch (hotkey) {
 			case OHK_SKIP:           this->OrderClick_Skip();         break;
 			case OHK_DELETE:         this->OrderClick_Delete();       break;
 			case OHK_GOTO:           this->OrderClick_Goto();         break;
@@ -1619,7 +1619,8 @@
 	WDP_AUTO, "view_vehicle_orders_train", 384, 100,
 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
 	WDF_CONSTRUCTION,
-	_nested_orders_train_widgets, lengthof(_nested_orders_train_widgets)
+	_nested_orders_train_widgets, lengthof(_nested_orders_train_widgets),
+	&OrdersWindow::hotkeys
 );
 
 /** Nested widget definition for "your" orders (non-train). */
@@ -1691,7 +1692,8 @@
 	WDP_AUTO, "view_vehicle_orders", 384, 100,
 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
 	WDF_CONSTRUCTION,
-	_nested_orders_widgets, lengthof(_nested_orders_widgets)
+	_nested_orders_widgets, lengthof(_nested_orders_widgets),
+	&OrdersWindow::hotkeys
 );
 
 /** Nested widget definition for competitor orders. */
@@ -1717,7 +1719,8 @@
 	WDP_AUTO, "view_vehicle_orders_competitor", 384, 86,
 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
 	WDF_CONSTRUCTION,
-	_nested_other_orders_widgets, lengthof(_nested_other_orders_widgets)
+	_nested_other_orders_widgets, lengthof(_nested_other_orders_widgets),
+	&OrdersWindow::hotkeys
 );
 
 void ShowOrdersWindow(const Vehicle *v)
--- a/src/rail_gui.cpp
+++ b/src/rail_gui.cpp
@@ -597,13 +597,10 @@
 		if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
 		MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
-		return ES_HANDLED;
+		return Window::OnHotkey(hotkey);
 	}
 
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
@@ -824,7 +821,8 @@
 	WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_build_rail_widgets, lengthof(_nested_build_rail_widgets)
+	_nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
+	&BuildRailToolbarWindow::hotkeys
 );
 
 
@@ -855,7 +853,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowBuildRailToolbar(_last_built_railtype);
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
--- a/src/road_gui.cpp
+++ b/src/road_gui.cpp
@@ -473,13 +473,10 @@
 		if (_ctrl_pressed) RoadToolbar_CtrlChanged(this);
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1 || this->GetWidget<NWidgetBase>(num) == NULL) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
 		MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
-		return ES_HANDLED;
+		return Window::OnHotkey(hotkey);
 	}
 
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
@@ -717,7 +714,8 @@
 	WDP_ALIGN_TOOLBAR, "toolbar_road", 0, 0,
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_build_road_widgets, lengthof(_nested_build_road_widgets)
+	_nested_build_road_widgets, lengthof(_nested_build_road_widgets),
+	&BuildRoadToolbarWindow::hotkeys
 );
 
 static const NWidgetPart _nested_build_tramway_widgets[] = {
@@ -756,7 +754,8 @@
 	WDP_ALIGN_TOOLBAR, "toolbar_tramway", 0, 0,
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_build_tramway_widgets, lengthof(_nested_build_tramway_widgets)
+	_nested_build_tramway_widgets, lengthof(_nested_build_tramway_widgets),
+	&BuildRoadToolbarWindow::hotkeys
 );
 
 /**
@@ -782,7 +781,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowBuildRoadToolbar(_last_built_roadtype);
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 static const NWidgetPart _nested_build_road_scen_widgets[] = {
@@ -816,7 +815,8 @@
 	WDP_AUTO, "toolbar_road_scen", 0, 0,
 	WC_SCEN_BUILD_TOOLBAR, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_build_road_scen_widgets, lengthof(_nested_build_road_scen_widgets)
+	_nested_build_road_scen_widgets, lengthof(_nested_build_road_scen_widgets),
+	&BuildRoadToolbarWindow::hotkeys
 );
 
 /**
@@ -835,7 +835,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowBuildRoadScenToolbar();
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 struct BuildRoadDepotWindow : public PickerWindowBase {
--- a/src/signs_gui.cpp
+++ b/src/signs_gui.cpp
@@ -281,16 +281,19 @@
 		}
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
-		EventState state = ES_NOT_HANDLED;
-		if (this->hotkeys.CheckMatch(keycode) == SLHK_FOCUS_FILTER_BOX) {
-			this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
-			SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
-			state = ES_HANDLED;
+		switch (hotkey) {
+			case SLHK_FOCUS_FILTER_BOX:
+				this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
+				SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
+				break;
+
+			default:
+				return ES_NOT_HANDLED;
 		}
 
-		return state;
+		return ES_HANDLED;
 	}
 
 	virtual void OnEditboxChanged(int widget)
@@ -374,7 +377,8 @@
 	WDP_AUTO, "list_signs", 358, 138,
 	WC_SIGN_LIST, WC_NONE,
 	0,
-	_nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
+	_nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
+	&SignListWindow::hotkeys
 );
 
 /**
@@ -393,7 +397,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowSignList();
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 /**
--- a/src/terraform_gui.cpp
+++ b/src/terraform_gui.cpp
@@ -219,14 +219,6 @@
 		}
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
-	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
-		return ES_HANDLED;
-	}
-
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
 	{
 		switch (this->last_user_action) {
@@ -346,7 +338,8 @@
 	WDP_MANUAL, "toolbar_landscape", 0, 0,
 	WC_SCEN_LAND_GEN, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_terraform_widgets, lengthof(_nested_terraform_widgets)
+	_nested_terraform_widgets, lengthof(_nested_terraform_widgets),
+	&TerraformToolbarWindow::hotkeys
 );
 
 /**
@@ -384,7 +377,7 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowTerraformToolbar(NULL);
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
 
 static byte _terraform_size = 1;
@@ -572,14 +565,6 @@
 		} while (--n);
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
-	{
-		int num = this->hotkeys.CheckMatch(keycode);
-		if (num == -1) return ES_NOT_HANDLED;
-		this->OnClick(Point(), num, 1);
-		return ES_HANDLED;
-	}
-
 	virtual void OnClick(Point pt, int widget, int click_count)
 	{
 		if (widget < WID_ETT_BUTTONS_START) return;
@@ -744,7 +729,8 @@
 	WDP_AUTO, "toolbar_landscape_scen", 0, 0,
 	WC_SCEN_LAND_GEN, WC_NONE,
 	WDF_CONSTRUCTION,
-	_nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets)
+	_nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
+	&ScenarioEditorLandscapeGenerationWindow::hotkeys
 );
 
 /**
@@ -762,5 +748,5 @@
 	if (num == -1) return ES_NOT_HANDLED;
 	Window *w = ShowEditorTerraformToolbar();
 	if (w == NULL) return ES_NOT_HANDLED;
-	return w->OnKeyPress(key, keycode);
+	return w->OnHotkey(num);
 }
--- a/src/toolbar_gui.cpp
+++ b/src/toolbar_gui.cpp
@@ -1660,9 +1660,9 @@
 		if (cbf != CBF_NONE) this->last_started_action = cbf;
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
-		switch (this->hotkeys.CheckMatch(keycode)) {
+		switch (hotkey) {
 			case MTHK_PAUSE: ToolbarPauseClick(this); break;
 			case MTHK_FASTFORWARD: ToolbarFastForwardClick(this); break;
 			case MTHK_SETTINGS: ShowGameOptions(); break;
@@ -1868,7 +1868,8 @@
 	WDP_MANUAL, NULL, 640, 22,
 	WC_MAIN_TOOLBAR, WC_NONE,
 	WDF_NO_FOCUS,
-	_nested_toolbar_normal_widgets, lengthof(_nested_toolbar_normal_widgets)
+	_nested_toolbar_normal_widgets, lengthof(_nested_toolbar_normal_widgets),
+	&MainToolbarWindow::hotkeys
 );
 
 
@@ -2006,10 +2007,10 @@
 		if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
 	}
 
-	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnHotkey(int hotkey)
 	{
 		CallBackFunction cbf = CBF_NONE;
-		switch (this->hotkeys.CheckMatch(keycode)) {
+		switch (hotkey) {
 			case MTEHK_PAUSE:                  ToolbarPauseClick(this); break;
 			case MTEHK_FASTFORWARD:            ToolbarFastForwardClick(this); break;
 			case MTEHK_SETTINGS:               ShowGameOptions(); break;
@@ -2179,7 +2180,8 @@
 	WDP_MANUAL, NULL, 640, 22,
 	WC_MAIN_TOOLBAR, WC_NONE,
 	WDF_NO_FOCUS,
-	_nested_toolb_scen_widgets, lengthof(_nested_toolb_scen_widgets)
+	_nested_toolb_scen_widgets, lengthof(_nested_toolb_scen_widgets),
+	&ScenarioEditorToolbarWindow::hotkeys
 );
 
 /** Allocate the toolbar. */