2 * Copyright 2002-2017, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
7 * Andrew McCall, mccall@digitalparadise.co.uk
13 #include "DataTranslationsWindow.h"
21 #include <Alignment.h>
22 #include <Application.h>
26 #include <ControlLook.h>
28 #include <GroupView.h>
30 #include <LayoutBuilder.h>
34 #include <ScrollView.h>
36 #include <StringView.h>
37 #include <SupportDefs.h>
39 #include <TranslationDefs.h>
40 #include <TranslatorRoster.h>
43 #include "DataTranslations.h"
44 #include "DataTranslationsSettings.h"
45 #include "TranslatorListView.h"
48 #undef B_TRANSLATION_CONTEXT
49 #define B_TRANSLATION_CONTEXT "DataTranslations"
52 const uint32 kMsgTranslatorInfo
= 'trin';
53 const uint32 kMsgSelectedTranslator
= 'trsl';
56 DataTranslationsWindow::DataTranslationsWindow()
58 BWindow(BRect(0.0f
, 0.0f
, 597.0f
, 368.0f
),
59 B_TRANSLATE_SYSTEM_NAME("DataTranslations"),
60 B_TITLED_WINDOW
, B_ASYNCHRONOUS_CONTROLS
| B_NOT_RESIZABLE
61 | B_NOT_ZOOMABLE
| B_AUTO_UPDATE_SIZE_LIMITS
),
64 MoveTo(DataTranslationsSettings::Instance()->WindowCorner());
68 // Make sure that the window isn't positioned off screen
70 BRect screenFrame
= screen
.Frame();
71 if (!screenFrame
.Contains(Frame()))
74 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
75 roster
->StartWatching(this);
81 DataTranslationsWindow::~DataTranslationsWindow()
83 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
84 roster
->StopWatching(this);
88 // Reads the installed translators and adds them to our BListView
90 DataTranslationsWindow::_PopulateListView()
92 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
94 // Get all Translators on the system. Gives us the number of translators
95 // installed in num_translators and a reference to the first one
97 translator_id
* translators
= NULL
;
98 roster
->GetAllTranslators(&translators
, &numTranslators
);
102 for (int32 i
= 0; i
< numTranslators
; i
++) {
103 // Getting the first three Infos: Name, Info & Version
107 roster
->GetTranslatorInfo(translators
[i
], &name
, &info
, &version
);
108 fTranslatorListView
->AddItem(new TranslatorItem(translators
[i
], name
));
109 maxWidth
= std::max(maxWidth
, fTranslatorListView
->StringWidth(name
));
112 fTranslatorListView
->SortItems();
114 fTranslatorListView
->SetExplicitSize(BSize(maxWidth
+ 20, B_SIZE_UNSET
));
116 delete[] translators
;
122 DataTranslationsWindow::_GetTranslatorInfo(int32 id
, const char*& name
,
123 const char*& info
, int32
& version
, BPath
& path
)
125 // Returns information about the translator with the given id
130 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
131 if (roster
->GetTranslatorInfo(id
, &name
, &info
, &version
) != B_OK
)
134 // Get the translator's path
136 if (roster
->GetRefFor(id
, &ref
) == B_OK
) {
147 DataTranslationsWindow::_ShowConfigView(int32 id
)
149 // Shows the config panel for the translator with the given id
154 BTranslatorRoster
* roster
= BTranslatorRoster::Default();
156 if (fConfigView
!= NULL
) {
157 fRightBox
->RemoveChild(fConfigView
);
161 if (fRelease
!= NULL
) {
167 BMessage emptyMessage
;
168 BRect
rect(0.0f
, 0.0f
, 200.0f
, 233.0f
);
169 status_t result
= roster
->MakeConfigurationView(id
, &emptyMessage
,
170 &fConfigView
, &rect
);
175 fConfigView
->SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
176 // force config views to all have the same color
177 fRightBox
->AddChild(fConfigView
);
179 // for default 12pt font size: 597 ≈ (0.85 * 12 * 49)
180 fConfigView
->SetExplicitMinSize(
181 BSize(ceilf(be_control_look
->DefaultItemSpacing() * 49)
182 - fTranslatorListView
->Frame().Width(), B_SIZE_UNSET
));
184 // Make sure the translator's image doesn't get unloaded while we are still
185 // showing a config view whose code is in the image
186 fRelease
= roster
->AcquireTranslator(id
);
193 DataTranslationsWindow::_ShowInfoView()
195 if (fConfigView
!= NULL
) {
196 fRightBox
->RemoveChild(fConfigView
);
200 if (fRelease
!= NULL
) {
206 fInfoText
= new BTextView("info text");
207 fInfoText
->MakeEditable(false);
208 fInfoText
->MakeSelectable(false);
209 fInfoText
->SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
210 fInfoText
->SetText(B_TRANSLATE(
211 "Use this control panel to set default values for translators, "
212 "to be used when no other settings are specified by an application."));
213 rgb_color textColor
= ui_color(B_PANEL_TEXT_COLOR
);
214 fInfoText
->SetFontAndColor(be_plain_font
, B_FONT_ALL
, &textColor
);
216 BGroupView
* group
= new BGroupView(B_VERTICAL
);
217 group
->AddChild(fInfoText
);
218 float spacing
= be_control_look
->DefaultItemSpacing();
219 group
->GroupLayout()->SetInsets(spacing
, spacing
, spacing
, spacing
);
220 fRightBox
->AddChild(group
);
226 DataTranslationsWindow::_SetupViews()
230 // This is NULL until a translator is
231 // selected from the listview
233 // Add the translators list view
234 fTranslatorListView
= new TranslatorListView("TransList");
235 fTranslatorListView
->SetSelectionMessage(
236 new BMessage(kMsgSelectedTranslator
));
238 BScrollView
* scrollView
= new BScrollView("scroll_trans",
239 fTranslatorListView
, B_WILL_DRAW
| B_FRAME_EVENTS
, false,
240 true, B_FANCY_BORDER
);
242 // Box around the config and info panels
243 fRightBox
= new BBox("Right_Side");
244 fRightBox
->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH
,
245 B_ALIGN_USE_FULL_HEIGHT
));
247 // Add the translator icon view
248 fIconView
= new IconView();
250 // Add the translator info button
251 fButton
= new BButton("info", B_TRANSLATE("Info"),
252 new BMessage(kMsgTranslatorInfo
),
253 B_WILL_DRAW
| B_FRAME_EVENTS
| B_NAVIGABLE
);
254 fButton
->SetEnabled(false);
256 // Populate the translators list view
260 BLayoutBuilder::Group
<>(this, B_HORIZONTAL
)
261 .SetInsets(B_USE_WINDOW_SPACING
)
263 .AddGroup(B_VERTICAL
)
265 .AddGroup(B_HORIZONTAL
)
273 fTranslatorListView
->MakeFocus();
279 DataTranslationsWindow::QuitRequested()
281 BPoint
pt(Frame().LeftTop());
282 DataTranslationsSettings::Instance()->SetWindowCorner(pt
);
283 be_app
->PostMessage(B_QUIT_REQUESTED
);
289 DataTranslationsWindow::_ShowInfoAlert(int32 id
)
291 const char* name
= NULL
;
292 const char* info
= NULL
;
295 _GetTranslatorInfo(id
, name
, info
, version
, path
);
297 const char* labels
[] = { B_TRANSLATE("Name:"), B_TRANSLATE("Version:"),
298 B_TRANSLATE("Info:"), B_TRANSLATE("Path:"), NULL
};
305 temp
.SetToFormat("%s %s\n", labels
[0], name
);
307 message
.Append(temp
);
309 offsets
[1] = message
.Length();
310 // Convert the version number into a readable format
311 temp
.SetToFormat("%s %" B_PRId32
".%" B_PRId32
".%" B_PRId32
"\n\n", labels
[1],
312 B_TRANSLATION_MAJOR_VERSION(version
),
313 B_TRANSLATION_MINOR_VERSION(version
),
314 B_TRANSLATION_REVISION_VERSION(version
));
316 message
.Append(temp
);
318 offsets
[2] = message
.Length();
319 temp
.SetToFormat("%s\n%s\n\n", labels
[2], info
);
321 message
.Append(temp
);
323 offsets
[3] = message
.Length();
324 temp
.SetToFormat("%s %s\n", labels
[3], path
.Path());
326 message
.Append(temp
);
328 BAlert
* alert
= new BAlert(B_TRANSLATE("Info"), message
.String(),
330 BTextView
* view
= alert
->TextView();
333 view
->SetStylable(true);
335 view
->GetFont(&font
);
336 font
.SetFace(B_BOLD_FACE
);
338 for (int32 i
= 0; labels
[i
]; i
++) {
339 view
->SetFontAndColor(offsets
[i
], offsets
[i
] + strlen(labels
[i
]), &font
);
342 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
348 DataTranslationsWindow::MessageReceived(BMessage
* message
)
350 switch (message
->what
) {
351 case kMsgTranslatorInfo
:
353 int32 selected
= fTranslatorListView
->CurrentSelection(0);
357 TranslatorItem
* item
= fTranslatorListView
->TranslatorAt(selected
);
359 _ShowInfoAlert(item
->ID());
363 case kMsgSelectedTranslator
:
365 // Update the icon and translator info panel
366 // to match the new selection
368 int32 selected
= fTranslatorListView
->CurrentSelection(0);
370 // If none selected, clear the old one
371 fIconView
->DrawIcon(false);
372 fButton
->SetEnabled(false);
373 fRightBox
->RemoveChild(fConfigView
);
378 TranslatorItem
* item
= fTranslatorListView
->TranslatorAt(selected
);
382 _ShowConfigView(item
->ID());
384 const char* name
= NULL
;
385 const char* info
= NULL
;
388 _GetTranslatorInfo(item
->ID(), name
, info
, version
, path
);
389 fIconView
->SetIcon(path
);
390 fButton
->SetEnabled(true);
394 case B_COLORS_UPDATED
:
396 if (fInfoText
== NULL
|| fInfoText
->Parent() == NULL
)
400 if (message
->FindColor(ui_color_name(B_PANEL_TEXT_COLOR
), &color
)
402 fInfoText
->SetFontAndColor(be_plain_font
, B_FONT_ALL
, &color
);
407 case B_TRANSLATOR_ADDED
:
411 while (message
->FindInt32("translator_id", index
++, &id
) == B_OK
) {
416 if (_GetTranslatorInfo(id
, name
, info
, version
, path
) == B_OK
)
417 fTranslatorListView
->AddItem(new TranslatorItem(id
, name
));
420 fTranslatorListView
->SortItems();
424 case B_TRANSLATOR_REMOVED
:
428 while (message
->FindInt32("translator_id", index
++, &id
) == B_OK
) {
429 for (int32 i
= 0; i
< fTranslatorListView
->CountItems(); i
++) {
430 TranslatorItem
* item
= fTranslatorListView
->TranslatorAt(i
);
435 if (item
->ID() == (translator_id
)id
) {
436 fTranslatorListView
->RemoveItem(i
);
446 BWindow::MessageReceived(message
);