2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
6 #include "FilterView.h"
12 #include <LayoutBuilder.h>
13 #include <MenuField.h>
15 #include <Messenger.h>
16 #include <PopUpMenu.h>
17 #include <TextControl.h>
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "FilterView"
28 add_categories_to_menu(const CategoryList
& categories
, BMenu
* menu
)
30 for (int i
= 0; i
< categories
.CountItems(); i
++) {
31 const CategoryRef
& category
= categories
.ItemAtFast(i
);
32 BMessage
* message
= new BMessage(MSG_CATEGORY_SELECTED
);
33 message
->AddString("name", category
->Name());
34 BMenuItem
* item
= new BMenuItem(category
->Label(), message
);
40 FilterView::FilterView()
42 BGroupView("filter view", B_VERTICAL
)
44 // Contruct category popup
45 BPopUpMenu
* showMenu
= new BPopUpMenu(B_TRANSLATE("Category"));
46 fShowField
= new BMenuField("category", B_TRANSLATE("Category:"), showMenu
);
48 // Construct repository popup
49 BPopUpMenu
* repositoryMenu
= new BPopUpMenu(B_TRANSLATE("Repository"));
50 fRepositoryField
= new BMenuField("repository", B_TRANSLATE("Repository:"),
53 // Construct search terms field
54 fSearchTermsText
= new BTextControl("search terms",
55 B_TRANSLATE("Search terms:"), "", NULL
);
56 fSearchTermsText
->SetModificationMessage(
57 new BMessage(MSG_SEARCH_TERMS_MODIFIED
));
59 BSize minSearchSize
= fSearchTermsText
->MinSize();
61 = be_plain_font
->StringWidth(fSearchTermsText
->Label())
62 + be_plain_font
->StringWidth("XXX") * 6;
63 minSearchWidth
= std::max(minSearchSize
.width
, minSearchWidth
);
64 minSearchSize
.width
= minSearchWidth
;
65 fSearchTermsText
->SetExplicitMinSize(minSearchSize
);
66 float maxSearchWidth
= minSearchWidth
* 2;
67 fSearchTermsText
->SetExplicitMaxSize(BSize(maxSearchWidth
, B_SIZE_UNSET
));
70 BLayoutBuilder::Group
<>(this)
71 .AddGroup(B_HORIZONTAL
)
72 .AddGroup(B_HORIZONTAL
, B_USE_DEFAULT_SPACING
, 1.2f
)
73 .Add(fShowField
, 0.0f
)
74 .Add(fRepositoryField
, 0.0f
)
75 .SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
))
78 .Add(fSearchTermsText
, 1.0f
)
81 .SetInsets(B_USE_DEFAULT_SPACING
)
86 FilterView::~FilterView()
92 FilterView::AttachedToWindow()
94 fShowField
->Menu()->SetTargetForItems(Window());
95 fRepositoryField
->Menu()->SetTargetForItems(Window());
96 fSearchTermsText
->SetTarget(this);
98 fSearchTermsText
->MakeFocus();
103 FilterView::MessageReceived(BMessage
* message
)
105 switch (message
->what
) {
106 case MSG_SEARCH_TERMS_MODIFIED
:
108 BMessage
searchTerms(MSG_SEARCH_TERMS_MODIFIED
);
109 searchTerms
.AddString("search terms", fSearchTermsText
->Text());
110 Window()->PostMessage(&searchTerms
);
115 BGroupView::MessageReceived(message
);
122 FilterView::AdoptModel(const Model
& model
)
125 BMenu
* repositoryMenu
= fRepositoryField
->Menu();
126 repositoryMenu
->RemoveItems(0, repositoryMenu
->CountItems(), true);
128 repositoryMenu
->AddItem(new BMenuItem(B_TRANSLATE("All repositories"),
129 new BMessage(MSG_DEPOT_SELECTED
)));
131 repositoryMenu
->AddItem(new BSeparatorItem());
133 bool foundSelectedDepot
= false;
134 const DepotList
& depots
= model
.Depots();
135 for (int i
= 0; i
< depots
.CountItems(); i
++) {
136 const DepotInfo
& depot
= depots
.ItemAtFast(i
);
137 BMessage
* message
= new BMessage(MSG_DEPOT_SELECTED
);
138 message
->AddString("name", depot
.Name());
139 BMenuItem
* item
= new BMenuItem(depot
.Name(), message
);
140 repositoryMenu
->AddItem(item
);
142 if (depot
.Name() == model
.Depot()) {
143 item
->SetMarked(true);
144 foundSelectedDepot
= true;
148 if (!foundSelectedDepot
)
149 repositoryMenu
->ItemAt(0)->SetMarked(true);
152 BMenu
* showMenu
= fShowField
->Menu();
153 showMenu
->RemoveItems(0, showMenu
->CountItems(), true);
155 showMenu
->AddItem(new BMenuItem(B_TRANSLATE("All categories"),
156 new BMessage(MSG_CATEGORY_SELECTED
)));
158 showMenu
->AddItem(new BSeparatorItem());
160 add_categories_to_menu(model
.Categories(), showMenu
);
162 bool foundSelectedCategory
= false;
163 for (int32 i
= 0; i
< showMenu
->CountItems(); i
++) {
164 BMenuItem
* item
= showMenu
->ItemAt(i
);
165 BMessage
* message
= item
->Message();
169 if (message
->FindString("name", &category
) == B_OK
170 && model
.Category() == category
) {
171 item
->SetMarked(true);
172 foundSelectedCategory
= true;
176 if (!foundSelectedCategory
)
177 showMenu
->ItemAt(0)->SetMarked(true);