xref: /haiku/headers/private/print/BeUtils.h (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
1 /*****************************************************************************/
2 // BeUtils.h
3 //
4 // Version: 1.0.0d1
5 //
6 // Several utilities for writing applications for the BeOS. It are small
7 // very specific functions, but generally useful (could be here because of a
8 // lack in the APIs, or just sheer lazyness :))
9 //
10 // Authors
11 //   Ithamar R. Adema
12 //   Michael Pfeiffer
13 //
14 // This application and all source files used in its construction, except
15 // where noted, are licensed under the MIT License, and have been written
16 // and are:
17 //
18 // Copyright (c) 2001, 2002 OpenBeOS Project
19 //
20 // Permission is hereby granted, free of charge, to any person obtaining a
21 // copy of this software and associated documentation files (the "Software"),
22 // to deal in the Software without restriction, including without limitation
23 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 // and/or sell copies of the Software, and to permit persons to whom the
25 // Software is furnished to do so, subject to the following conditions:
26 //
27 // The above copyright notice and this permission notice shall be included
28 // in all copies or substantial portions of the Software.
29 //
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 // DEALINGS IN THE SOFTWARE.
37 /*****************************************************************************/
38 
39 #ifndef _BE_UTILS_H
40 #define _BE_UTILS_H
41 
42 #include <FindDirectory.h>
43 #include <Path.h>
44 #include <SupportDefs.h>
45 #include <Picture.h>
46 #include <PictureButton.h>
47 #include <TranslatorFormats.h>
48 #include <TranslationUtils.h>
49 
50 status_t TestForAddonExistence(const char* name, directory_which which,
51 							   const char* section, BPath& outPath);
52 
53 // Reference counted object
54 class Object {
55 private:
56 	volatile int32 fRefCount;
57 
58 public:
59 	// After construction reference count is 1
60 	Object() : fRefCount(1) { }
61 	// dtor should be private, but ie. ObjectList requires a public dtor!
62 	virtual ~Object() { };
63 
64 	// thread-safe as long as thread that calls Acquire has already
65 	// a reference to the object
66 	void Acquire() {
67 		atomic_add(&fRefCount, 1);
68 	}
69 
70 	bool Release() {
71 		if (atomic_add(&fRefCount, -1) == 1) {
72 			delete this; return true;
73 		} else {
74 			return false;
75 		}
76 	}
77 };
78 
79 // Automatically send a reply to sender on destruction of object
80 // and delete sender
81 class AutoReply {
82 	BMessage* fSender;
83 	BMessage  fReply;
84 
85 public:
86 	AutoReply(BMessage* sender, uint32 what);
87 	~AutoReply();
88 	void SetReply(BMessage* m) { fReply = *m; }
89 };
90 
91 // mimetype from sender
92 bool MimeTypeForSender(BMessage* sender, BString& mime);
93 // load bitmap from application resources
94 BBitmap* LoadBitmap(const char* name, uint32 type_code = B_TRANSLATOR_BITMAP);
95 // convert bitmap to picture; view must be attached to a window!
96 // returns NULL if bitmap is NULL
97 BPicture *BitmapToPicture(BView* view, BBitmap *bitmap);
98 BPicture *BitmapToGrayedPicture(BView* view, BBitmap *bitmap);
99 
100 #endif
101