xref: /haiku/src/apps/icon-o-matic/style/SetGradientCommand.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "SetGradientCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include <Catalog.h>
15 #include <Locale.h>
16 
17 #include "GradientTransformable.h"
18 #include "Style.h"
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-SetGradientCmd"
23 
24 
25 using std::nothrow;
26 
27 // constructor
28 SetGradientCommand::SetGradientCommand(Style* style,
29 									   const Gradient* gradient)
30 	: Command(),
31 	  fStyle(style),
32 	  fGradient(gradient ? new (nothrow) Gradient(*gradient) : NULL)
33 {
34 }
35 
36 // destructor
37 SetGradientCommand::~SetGradientCommand()
38 {
39 	if (fGradient != NULL)
40 		fGradient->ReleaseReference();
41 }
42 
43 // InitCheck
44 status_t
45 SetGradientCommand::InitCheck()
46 {
47 	if (!fStyle)
48 		return B_NO_INIT;
49 	if (fGradient && fStyle->Gradient()) {
50 		if (*fGradient == *fStyle->Gradient()) {
51 			printf("SetGradientCommand::InitCheck() - same gradients\n");
52 			return B_ERROR;
53 		}
54 	}
55 	return B_OK;
56 }
57 
58 // Perform
59 status_t
60 SetGradientCommand::Perform()
61 {
62 	// clone the new gradient for handling over to the style
63 	Gradient* clone = NULL;
64 	if (fGradient) {
65 		clone = new (nothrow) Gradient(*fGradient);
66 		if (!clone)
67 			return B_NO_MEMORY;
68 	}
69 
70 	if (fStyle->Gradient()) {
71 		// remember the current gradient of the style
72 		if (fGradient)
73 			*fGradient = *fStyle->Gradient();
74 		else {
75 			fGradient = new (nothrow) Gradient(*fStyle->Gradient());
76 			if (!fGradient) {
77 				delete clone;
78 				return B_NO_MEMORY;
79 			}
80 		}
81 	} else if (fGradient != NULL) {
82 		// the style didn't have a gradient set
83 		fGradient->ReleaseReference();
84 		fGradient = NULL;
85 	}
86 
87 	// remove the gradient or set the new one
88 	fStyle->SetGradient(clone);
89 	delete clone;
90 
91 	return B_OK;
92 }
93 
94 // Undo
95 status_t
96 SetGradientCommand::Undo()
97 {
98 	return Perform();
99 }
100 
101 // GetName
102 void
103 SetGradientCommand::GetName(BString& name)
104 {
105 	name << B_TRANSLATE("Edit gradient");
106 }
107 
108 // CombineWithNext
109 bool
110 SetGradientCommand::CombineWithNext(const Command* command)
111 {
112 	const SetGradientCommand* next
113 		= dynamic_cast<const SetGradientCommand*>(command);
114 
115 	if (next && next->fTimeStamp - fTimeStamp < 1000000) {
116 		fTimeStamp = next->fTimeStamp;
117 		// NOTE: next was already performed, but
118 		// when undoing, we want to use our
119 		// remembered gradient
120 		return true;
121 	}
122 	return false;
123 }
124 
125