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