tbx  0.7.6
component.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 
26 #ifndef TBX_COMPONENT_H_
27 #define TBX_COMPONENT_H_
28 
29 #include "handles.h"
30 #include "pollinfo.h"
31 #include "object.h"
32 
33 namespace tbx {
34 
35 class Command;
36 class UserEventListener;
37 
42 class Component {
43 
44 protected:
46  int _id;
47 
48 public:
64  Component(Object obj, ComponentId id) : _handle(obj.handle()), _id(id) {}
74 
80  bool null() const {return (_id == NULL_ComponentId);}
81 
85  Object object() {return Object(_handle);}
86 
90  Object object() const {return Object(_handle);}
91 
95  ObjectId handle() const {return _handle;}
96 
103  ComponentId id() const {return _id;}
104 
108  Component &operator=(const Component &other) {_handle = other._handle; _id = other._id; return *this;}
109 
115  bool operator==(const Component &other) const {return (_handle == other._handle && _id == other._id);}
116 
122  bool operator!=(const Component &other) const {return (_handle != other._handle || _id != other._id);}
123 
129  operator bool() const {return (_id != NULL_ComponentId);}
130 
131  // Listeners for all components
132  void add_command(int command_id, Command *command);
133  void remove_command(int command_id, Command *command);
134 
135  void add_user_event_listener(int event_id, UserEventListener *listener);
136  void remove_user_event_listener(int event_id, UserEventListener *listener);
137 
141  void remove_all_listeners();
142 
143 protected:
144  // Helpers for derived classes
145 
146  // Add listener helper
147  void add_listener( int action, Listener *listener, RawToolboxEventHandler handler);
148  void remove_listener(int action, Listener *listener);
149 
150  // Add listener for WIMP window event helper
151  void add_window_listener(int event_code, Listener *listener);
152  void remove_window_listener(int event_code, Listener *listener);
153 
154  // Property Helpers
155  int int_property(int property_id) const;
156  void int_property(int property_id, int value);
157  bool bool_property(int property_id) const;
158  void bool_property(int property_id, bool value);
159  std::string string_property(int property_id) const;
160  int string_property_length(int property_id) const;
161  void string_property(int property_id, const std::string &value);
162 
163  bool flag_property(int property_id, int flag) const;
164  void flag_property(int property_id, int flag, bool value);
165 };
166 
167 }
168 
169 #endif /* COMPONENT_H_ */
tbx::Component::string_property
std::string string_property(int property_id) const
Get a string property from the toolbox object.
Definition: component.cc:261
tbx::Component::add_listener
void add_listener(int action, Listener *listener, RawToolboxEventHandler handler)
Helper function to add listeners to this component.
Definition: component.cc:117
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::ComponentId
int ComponentId
Type for underlying toolbox component id.
Definition: handles.h:33
tbx::Component::handle
ObjectId handle() const
Return the object handle for his component.
Definition: component.h:95
tbx::Component::int_property
int int_property(int property_id) const
Get an integer property from the toolbox Component.
Definition: component.cc:165
tbx::Component
Base class for components in an object.
Definition: component.h:42
tbx::Component::_handle
ObjectId _handle
Underlying toolbox handle.
Definition: component.h:45
tbx::Component::id
ComponentId id() const
Get the component ID of this component.
Definition: component.h:103
tbx::Component::remove_all_listeners
void remove_all_listeners()
Remove all the listeners on this component.
Definition: component.cc:104
tbx::Component::Component
Component()
Construct an uninitialised component.
Definition: component.h:55
tbx::Component::object
Object object() const
Return the object his component belongs to.
Definition: component.h:90
tbx::NULL_ObjectId
const ObjectId NULL_ObjectId
NULL object id.
Definition: handles.h:42
tbx::Component::remove_command
void remove_command(int command_id, Command *command)
Remove a command from this component.
Definition: component.cc:61
tbx::Component::operator=
Component & operator=(const Component &other)
Assign the Component to refer to the same underlying toolbox component.
Definition: component.h:108
tbx::Component::add_command
void add_command(int command_id, Command *command)
Add a command to this Component.
Definition: component.cc:47
tbx::Component::add_window_listener
void add_window_listener(int event_code, Listener *listener)
Helper function to add a listener for a WIMP window event.
Definition: component.cc:137
tbx::Component::remove_window_listener
void remove_window_listener(int event_code, Listener *listener)
Helper function to remove a listener for a WIMP window event.
Definition: component.cc:147
tbx::RawToolboxEventHandler
void(* RawToolboxEventHandler)(IdBlock &id_block, PollBlock &data, Listener *listener)
Function type for handlers of raw (unprocessed) Toolbox events.
Definition: pollinfo.h:86
tbx::Component::add_user_event_listener
void add_user_event_listener(int event_id, UserEventListener *listener)
Add a user event listener.
Definition: component.cc:78
tbx::Component::operator!=
bool operator!=(const Component &other) const
Check if this component refers to the same underlying toolbox gadget.
Definition: component.h:122
tbx::Component::bool_property
bool bool_property(int property_id) const
Get a boolean property from the toolbox Component.
Definition: component.cc:214
tbx::Object
Class to manipulate a toolbox object.
Definition: object.h:51
tbx::Listener
Base class for all toolbox event listeners.
Definition: listener.h:34
tbx::Command
Base class for commands in tbx.
Definition: command.h:37
tbx::Component::Component
Component(ObjectId handle, ComponentId id)
Construct a component from an object handle and component id.
Definition: component.h:73
tbx::Component::remove_listener
void remove_listener(int action, Listener *listener)
Helper function to remove listeners from this component.
Definition: component.cc:127
tbx::Component::_id
int _id
Underlying toolbox component id.
Definition: component.h:46
tbx::Component::string_property_length
int string_property_length(int property_id) const
Get a the length of a string property from the toolbox object.
Definition: component.cc:302
tbx::Component::Component
Component(Object obj, ComponentId id)
Construct a component from an object and component id.
Definition: component.h:64
tbx::ObjectId
unsigned int ObjectId
Type for underlying toolbox object id.
Definition: handles.h:31
tbx::NULL_ComponentId
const ComponentId NULL_ComponentId
NULL component id.
Definition: handles.h:46
tbx::Component::flag_property
bool flag_property(int property_id, int flag) const
Check if a particular flag is set in a property.
Definition: component.cc:351
tbx::Component::remove_user_event_listener
void remove_user_event_listener(int event_id, UserEventListener *listener)
Remove a user event listener from this component.
Definition: component.cc:92
tbx::UserEventListener
A UserEventListener can be used to capture any event on an object or component.
Definition: usereventlistener.h:62
tbx::Component::object
Object object()
Return the object his component belongs to.
Definition: component.h:85
tbx::Component::operator==
bool operator==(const Component &other) const
Check if this component refers to the same underlying toolbox component.
Definition: component.h:115