repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haikudepot / ui / FilterView.cpp
blobbe2d0ca9825e779e9d7acb783a14fa67313df4b1
1 /*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include "FilterView.h"
8 #include <algorithm>
9 #include <stdio.h>
11 #include <Catalog.h>
12 #include <LayoutBuilder.h>
13 #include <MenuField.h>
14 #include <MenuItem.h>
15 #include <Messenger.h>
16 #include <PopUpMenu.h>
17 #include <TextControl.h>
19 #include "Model.h"
20 #include "support.h"
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "FilterView"
27 static void
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);
35 menu->AddItem(item);
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:"),
51 repositoryMenu);
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();
60 float minSearchWidth
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));
69 // Build layout
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))
76 .End()
77 .AddGlue(0.5f)
78 .Add(fSearchTermsText, 1.0f)
79 .End()
81 .SetInsets(B_USE_DEFAULT_SPACING)
86 FilterView::~FilterView()
91 void
92 FilterView::AttachedToWindow()
94 fShowField->Menu()->SetTargetForItems(Window());
95 fRepositoryField->Menu()->SetTargetForItems(Window());
96 fSearchTermsText->SetTarget(this);
98 fSearchTermsText->MakeFocus();
102 void
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);
111 break;
114 default:
115 BGroupView::MessageReceived(message);
116 break;
121 void
122 FilterView::AdoptModel(const Model& model)
124 // Adopt depots
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);
151 // Adopt categories
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();
166 if (message == NULL)
167 continue;
168 BString category;
169 if (message->FindString("name", &category) == B_OK
170 && model.Category() == category) {
171 item->SetMarked(true);
172 foundSelectedCategory = true;
173 break;
176 if (!foundSelectedCategory)
177 showMenu->ItemAt(0)->SetMarked(true);