xref: /haiku/src/preferences/filetypes/TypeListWindow.cpp (revision 25a7b01d15612846f332751841da3579db313082)
1 /*
2  * Copyright 2006-2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "MimeTypeListView.h"
8 #include "TypeListWindow.h"
9 
10 #include <Button.h>
11 #include <Catalog.h>
12 #include <ControlLook.h>
13 #include <LayoutBuilder.h>
14 #include <Locale.h>
15 #include <ScrollView.h>
16 
17 #include <string.h>
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "Type List Window"
22 
23 
24 const uint32 kMsgTypeSelected = 'tpsl';
25 const uint32 kMsgSelected = 'seld';
26 
27 
TypeListWindow(const char * currentType,uint32 what,BWindow * target)28 TypeListWindow::TypeListWindow(const char* currentType, uint32 what,
29 		BWindow* target)
30 	:
31 	BWindow(BRect(100, 100, 360, 440), B_TRANSLATE("Choose type"),
32 		B_MODAL_WINDOW,
33 		B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS),
34 	fTarget(target),
35 	fWhat(what)
36 {
37 	float padding = be_control_look->DefaultItemSpacing();
38 
39 	fSelectButton = new BButton("select", B_TRANSLATE("Done"),
40 		new BMessage(kMsgSelected));
41 	fSelectButton->SetEnabled(false);
42 
43 	BButton* button = new BButton("cancel", B_TRANSLATE("Cancel"),
44 		new BMessage(B_CANCEL));
45 
46 	fSelectButton->MakeDefault(true);
47 
48 	fListView = new MimeTypeListView("typeview", NULL, true, false);
49 	fListView->SetSelectionMessage(new BMessage(kMsgTypeSelected));
50 	fListView->SetInvocationMessage(new BMessage(kMsgSelected));
51 
52 	BScrollView* scrollView = new BScrollView("scrollview", fListView,
53 		B_FRAME_EVENTS | B_WILL_DRAW, false, true);
54 
55 	BLayoutBuilder::Group<>(this, B_VERTICAL, padding)
56 		.SetInsets(padding)
57 		.Add(scrollView)
58 		.AddGroup(B_HORIZONTAL, padding)
59 			.AddGlue()
60 			.Add(button)
61 			.Add(fSelectButton);
62 
63 	BAlignment buttonAlignment =
64 		BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_VERTICAL_CENTER);
65 	button->SetExplicitAlignment(buttonAlignment);
66 	fSelectButton->SetExplicitAlignment(buttonAlignment);
67 
68 	MoveTo(target->Frame().LeftTop() + BPoint(15.0f, 15.0f));
69 }
70 
71 
~TypeListWindow()72 TypeListWindow::~TypeListWindow()
73 {
74 }
75 
76 
77 void
MessageReceived(BMessage * message)78 TypeListWindow::MessageReceived(BMessage* message)
79 {
80 	switch (message->what) {
81 		case kMsgTypeSelected:
82 			fSelectButton->SetEnabled(fListView->CurrentSelection() >= 0);
83 			break;
84 
85 		case kMsgSelected:
86 		{
87 			MimeTypeItem* item = dynamic_cast<MimeTypeItem*>(fListView->ItemAt(
88 				fListView->CurrentSelection()));
89 			if (item != NULL) {
90 				BMessage select(fWhat);
91 				select.AddString("type", item->Type());
92 				fTarget.SendMessage(&select);
93 			}
94 
95 			// supposed to fall through
96 		}
97 		case B_CANCEL:
98 			PostMessage(B_QUIT_REQUESTED);
99 			break;
100 
101 		default:
102 			BWindow::MessageReceived(message);
103 			break;
104 	}
105 }
106 
107