xref: /haiku/src/apps/mediaconverter/MediaFileListView.cpp (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
1 // Copyright 1999, Be Incorporated. All Rights Reserved.
2 // Copyright 2000-2004, Jun Suzuki. All Rights Reserved.
3 // Copyright 2007, Stephan Aßmus. All Rights Reserved.
4 // This file may be used under the terms of the Be Sample Code License.
5 #include "MediaFileListView.h"
6 
7 #include <Application.h>
8 #include <MediaFile.h>
9 #include <Messenger.h>
10 
11 #include "MediaConverterWindow.h"
12 #include "MessageConstants.h"
13 
14 
15 // #pragma mark - MediaFileListItem
16 
17 
18 MediaFileListItem::MediaFileListItem(BMediaFile* file, const entry_ref& ref)
19 	: BStringItem(ref.name),
20 	  fRef(ref),
21 	  fMediaFile(file)
22 {
23 }
24 
25 
26 MediaFileListItem::~MediaFileListItem()
27 {
28 	delete fMediaFile;
29 }
30 
31 
32 // #pragma mark - MediaFileListView
33 
34 
35 MediaFileListView::MediaFileListView(BRect frame, uint32 resizingMode)
36 	: BListView(frame, "MediaFileListView", B_SINGLE_SELECTION_LIST, resizingMode,
37 				B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS)
38 {
39 	fEnabled = true;
40 }
41 
42 
43 MediaFileListView::~MediaFileListView()
44 {
45 	BListItem *item;
46 	while ((item = RemoveItem((int32)0)) != NULL) {
47 		delete item;
48 	}
49 }
50 
51 
52 void
53 MediaFileListView::SetEnabled(bool enabled)
54 {
55 	if (enabled == fEnabled)
56 		return;
57 
58 	fEnabled = enabled;
59 	// TODO: visual indication of enabled status?
60 }
61 
62 
63 bool
64 MediaFileListView::IsEnabled() const
65 {
66 	return fEnabled;
67 }
68 
69 
70 void
71 MediaFileListView::AddItem(BMediaFile* file, const entry_ref& ref)
72 {
73 	BListView::AddItem(new MediaFileListItem(file, ref));
74 	be_app_messenger.SendMessage(FILE_LIST_CHANGE_MESSAGE);
75 }
76 
77 
78 void
79 MediaFileListView::KeyDown(const char *bytes, int32 numBytes)
80 {
81 	switch (bytes[0]) {
82 		case B_DELETE:
83 			if (IsEnabled()) {
84 				int32 selection = CurrentSelection();
85 				if (selection >= 0) {
86 					delete RemoveItem(selection);
87 					// select the previous item
88 					int32 count = CountItems();
89 					if (selection >= count)
90 						selection = count - 1;
91 					Select(selection);
92 					be_app_messenger.SendMessage(FILE_LIST_CHANGE_MESSAGE);
93 				}
94 			}
95 			break;
96 		default:
97 			BListView::KeyDown(bytes, numBytes);
98 	}
99 }
100 
101 
102 void
103 MediaFileListView::SelectionChanged()
104 {
105 	MediaConverterWindow* win = dynamic_cast<MediaConverterWindow *>(Window());
106 	if (win != NULL)
107 		win->SourceFileSelectionChanged();
108 }
109 
110