xref: /haiku/src/preferences/shortcuts/PopUpColumn.cpp (revision 3369e03d5cde9709c8aa70c99bfe6ce24ba65bf9)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 		Josef Gajdusek
7  */
8 
9 
10 #include "PopUpColumn.h"
11 
12 #include <PopUpMenu.h>
13 #include <MenuItem.h>
14 #include <Window.h>
15 
16 #include "EditWindow.h"
17 #include "ShortcutsWindow.h"
18 
19 PopUpColumn::PopUpColumn(BPopUpMenu* menu, const char* name, float width,
20 	float minWidth, float maxWidth, uint32 truncate, bool editable,
21 	bool cycle, int cycleInit, alignment align)
22 	:
23 	BStringColumn(name, width, minWidth, maxWidth, truncate, align),
24 	fEditable(editable),
25 	fCycle(cycle),
26 	fCycleInit(cycleInit),
27 	fMenu(menu)
28 {
29 	SetWantsEvents(true);
30 }
31 
32 
33 PopUpColumn::~PopUpColumn()
34 {
35 	delete fMenu;
36 }
37 
38 void
39 PopUpColumn::MouseDown(BColumnListView* parent, BRow* row, BField* field,
40 	BRect fieldRect, BPoint point, uint32 buttons)
41 {
42 	if ((buttons & B_SECONDARY_MOUSE_BUTTON)
43 		|| (buttons & B_PRIMARY_MOUSE_BUTTON && (fEditable || fCycle))) {
44 		BMessage* msg = new BMessage(ShortcutsWindow::HOTKEY_ITEM_MODIFIED);
45 		msg->SetInt32("row", parent->IndexOf(row));
46 		msg->SetInt32("column", LogicalFieldNum());
47 		if (buttons & B_SECONDARY_MOUSE_BUTTON) {
48 			BMenuItem* selected = fMenu->Go(parent->ConvertToScreen(point));
49 			if (selected) {
50 				msg->SetString("text", selected->Label());
51 				parent->Window()->PostMessage(msg);
52 			}
53 		}
54 		if (buttons & B_PRIMARY_MOUSE_BUTTON && row->IsSelected()) {
55 			BStringField* stringField = static_cast<BStringField*>(field);
56 			if (fEditable) {
57 				EditWindow* edit = new EditWindow(stringField->String(), 0);
58 				msg->SetString("text", edit->Go());
59 			} else if (fCycle) {
60 				BMenuItem* item;
61 				for (int i = 0; (item = fMenu->ItemAt(i)) != NULL; i++)
62 					if (strcmp(stringField->String(), item->Label()) == 0) {
63 						item = fMenu->ItemAt((i + 1) % fMenu->CountItems());
64 						break;
65 					}
66 				if (item == NULL)
67 					item = fMenu->ItemAt(fCycleInit);
68 				msg->SetString("text", item->Label());
69 			}
70 			parent->Window()->PostMessage(msg);
71 		}
72 	}
73 	BStringColumn::MouseDown(parent, row, field, fieldRect, point, buttons);
74 }
75 
76