xref: /haiku/src/preferences/notifications/PrefletView.cpp (revision 76e9533e9e9945f62053bf7c737df2f3e1735bbd)
1 /*
2  * Copyright 2010, Haiku, Inc. All Rights Reserved.
3  * Copyright 2009, Pier Luigi Fiorini.
4  * Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
8  */
9 
10 #include <Catalog.h>
11 #include <Message.h>
12 #include <GroupLayout.h>
13 #include <GroupLayoutBuilder.h>
14 #include <CardLayout.h>
15 #include <LayoutItem.h>
16 
17 #include "SettingsHost.h"
18 #include "PrefletView.h"
19 #include "IconRule.h"
20 #include "GeneralView.h"
21 #include "DisplayView.h"
22 #include "NotificationsView.h"
23 
24 
25 #undef B_TRANSLATE_CONTEXT
26 #define B_TRANSLATE_CONTEXT "PrefletView"
27 
28 
29 const int32 kPageSelected = '_LCH';
30 
31 
32 PrefletView::PrefletView(SettingsHost* host)
33 	:
34 	BView("pages", B_WILL_DRAW)
35 {
36 	// Page selector
37 	fRule = new BIconRule("icon_rule");
38 	fRule->SetSelectionMessage(new BMessage(kPageSelected));
39 	fRule->AddIcon(B_TRANSLATE("General"), NULL);
40 	fRule->AddIcon(B_TRANSLATE("Display"), NULL);
41 	//fRule->AddIcon(B_TRANSLATE("Notifications"), NULL);
42 
43 	// View for card layout
44 	fPagesView = new BView("pages", B_WILL_DRAW);
45 
46 	// Pages
47 	GeneralView* general = new GeneralView(host);
48 	DisplayView* display = new DisplayView(host);
49 	NotificationsView* apps = new NotificationsView();
50 
51 	// Calculate inset
52 	float inset = ceilf(be_plain_font->Size() * 0.7f);
53 
54 	// Build the layout
55 	SetLayout(new BGroupLayout(B_VERTICAL));
56 
57 	// Card layout for pages
58 	BCardLayout* layout = new BCardLayout();
59 	fPagesView->SetLayout(layout);
60 	layout->AddView(general);
61 	layout->AddView(display);
62 	layout->AddView(apps);
63 
64 	// Add childs
65 	AddChild(BGroupLayoutBuilder(B_VERTICAL, inset)
66 		.Add(fRule)
67 		.Add(fPagesView)
68 	);
69 
70 	// Select the first view
71 	Select(0);
72 }
73 
74 
75 void
76 PrefletView::AttachedToWindow()
77 {
78 	fRule->SetTarget(this);
79 }
80 
81 
82 void
83 PrefletView::MessageReceived(BMessage* message)
84 {
85 	switch (message->what) {
86 		case kPageSelected:
87 		{
88 			int32 index = B_ERROR;
89 			if (message->FindInt32("index", &index) != B_OK)
90 				return;
91 
92 			Select(index);
93 			break;
94 		}
95 		default:
96 			BView::MessageReceived(message);
97 	}
98 }
99 
100 
101 void
102 PrefletView::Select(int32 index)
103 {
104 	BCardLayout* layout
105 		= dynamic_cast<BCardLayout*>(fPagesView->GetLayout());
106 	if (layout)
107 		layout->SetVisibleItem(index);
108 }
109 
110 
111 BView*
112 PrefletView::CurrentPage()
113 {
114 	BCardLayout* layout
115 		= dynamic_cast<BCardLayout*>(fPagesView->GetLayout());
116 	if (layout)
117 		return layout->VisibleItem()->View();
118 	return NULL;
119 }
120 
121 
122 int32
123 PrefletView::CountPages() const
124 {
125 	BCardLayout* layout
126 		= dynamic_cast<BCardLayout*>(fPagesView->GetLayout());
127 	if (layout)
128 		return layout->CountItems();
129 	return 0;
130 }
131 
132 
133 BView*
134 PrefletView::PageAt(int32 index)
135 {
136 	BCardLayout* layout
137 		= dynamic_cast<BCardLayout*>(fPagesView->GetLayout());
138 	if (layout)
139 		return layout->ItemAt(index)->View();
140 	return NULL;
141 }
142