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