The morality of an act depends on three things what is done (the object), why it is done (the intention) and the circumstances.
1750 The morality of human acts depends on:
- the object chosen;
- the end in view or the intention;
- the circumstances of the action.
The object, the intention, and the circumstances make up the "sources," or constitutive elements, of the morality of human acts.
All three need to be good or neutral in order for an act to be moral. So a bad intention can make an outwardly good act immoral.
1755 A morally good act requires the goodness of the object, of the end, and of the circumstances together. An evil end corrupts the action, even if the object is good in itself (such as praying and fasting "in order to be seen by men").
The object of the choice can by itself vitiate an act in its entirety. There are some concrete acts - such as fornication - that it is always wrong to choose, because choosing them entails a disorder of the will, that is, a moral evil.
CCC #1755 (bold is mine)
Giving to the poor has a good object and (presumably) circumstances so the morality will depend on the intention. Out of the list provided that mentions the poor, based on the example given in the CCC I would say
- Giving to the poor for cred (someone sees you doing it)
- Giving to the poor for financial gain / tax break
- Giving to the poor because it "makes me feel good"
would be bad intentions and therefore would render the act immoral. I'm fairly confident on the first two, but not so sure about the third. It may be neutral.
- Giving to the poor for no apparent reason (w/out thinking deeply about it)
would be neutral and the act would remain moral.
- Giving to the poor with the belief that it's the right thing to do
- Giving to the poor because you personally witness another in need or suffering
- Giving to the poor as a form of prayer or penance
would be good and the act would remain good.
Giving to the rich would be morally neutral in the object, so the morality would depend on intent and circumstances.