tbx  0.7.6
clipboard.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2021 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 #ifndef TBX_CLIPBOARD_H_
26 #define TBX_CLIPBOARD_H_
27 
28 #include "saver.h"
29 #include <vector>
30 #include <map>
31 
32 #include "gadget.h"
33 #include "loadermanager.h"
34 
35 namespace tbx
36 {
37 
42 {
43  public:
44  virtual ~ClipboardClaimedListener() {}
48  virtual void clipboard_claimed_caret() {}
52  virtual void clipboard_claimed() {}
53 };
54 
55 
60  {
61  public:
62  virtual ~ClipboardFormatListener() {}
63 
70  virtual void clipboard_format_available(int file_type) = 0;
74  virtual void clipboard_empty() {}
75  };
76 
77 class ClipboardData;
78 
82 class Clipboard
83 {
84  public:
85  static Clipboard *instance();
86  ~Clipboard();
87  void clear();
88 
89  void copy(int file_type, ClipboardData *data, bool add = false);
90  void copy(const std::string &text, int file_type = 0xFFF);
91  void copy(int file_type, char *bytes, int size, bool copy_bytes = true);
92  void paste(int file_type, Gadget &gadget, int x = 0, int y = 0);
93  void paste(int *file_types, int num_types, Gadget &gadget, int x = 0, int y = 0);
94  void paste(int file_type, Window &window, int x = 0, int y = 0);
95  void paste(int *file_types, int num_types, Window &window, int x = 0, int y = 0);
96 
97  void check(int file_type, Window &window);
98  void check(int *file_types, int num_types, Window &window);
99 
100  void claim_caret();
101 
106 
111  bool owns_clipboard() const {return _owns_clipboard;}
112  ClipboardData *data(int file_type) const;
113  ClipboardData *native_data() const;
114  int native_file_type() const;
115 
116  private:
117  Clipboard();
118  void claim_clipboard();
119  void fire_claimed(bool clipboard_claimed, bool caret_claimed);
120  void paste(int *file_types, int num_types, Window window, Gadget gadget, int x, int y);
121  void fire_format_found(int file_type);
122  void fire_clipboard_empty();
123 
124  // Private class to do all the message handling
125  class MessageHandler
133  public LoaderManager::MessageIntercept
134  {
135  public:
136  MessageHandler();
137  virtual ~MessageHandler();
138  void clear();
139  void paste_ref(int ref) {_paste_ref = ref;}
140 
141  virtual void user_message(WimpMessageEvent &event);
142  virtual void recorded_message(WimpMessageEvent &event, int reply_to);
143  virtual void acknowledge_message(WimpMessageEvent &event);
144 
145  virtual void saver_finished(const tbx::SaverFinishedEvent &finished);
146  virtual void saver_save_completed(tbx::SaverSaveCompletedEvent &event);
147  virtual void saver_save_to_file(tbx::Saver saver, std::string file_name);
148  virtual void saver_fill_buffer(tbx::Saver saver, int size, void *buffer, int already_transmitted);
149 
150  bool loader_message_intercept(WimpMessage::SendType type, WimpMessageEvent &event, int reply_to);
151 
152  private:
153  tbx::Saver *_saver;
154  ClipboardData *_save_data;
155  char *_save_buffer;
156  int _paste_ref;
157  } _message_handler;
158  friend class MessageHandler;
159 
160  private:
161  bool _owns_clipboard;
162  bool _owns_caret;
163  int _native_file_type;
164  std::map<int, ClipboardData *> _data;
165  std::vector<ClipboardClaimedListener *> _claimed_listeners;
166  std::vector<ClipboardFormatListener *> *_format_listeners;
167  static Clipboard *s_instance;
168 };
169 
178 
188 {
189 public:
190  virtual ~ClipboardData() {}
196  virtual int size() = 0;
202  virtual const char *data() = 0;
203 
208  virtual void copy_started() {}
212  virtual void copy_finished() {}
213 };
214 
219 {
220 public:
221  ClipboardText(const std::string &text) : _text(text) {}
222  virtual ~ClipboardText() {}
223 
224  int size() {return static_cast<int>(_text.size());}
225  const char *data() {return _text.data();}
226 
227 private:
228  std::string _text;
229 };
230 
235 {
236  public:
237  ClipboardBytes(char *bytes, int size, bool copy = true);
238  virtual ~ClipboardBytes() { delete _data;}
239 
240  int size() {return _size;}
241  const char *data() {return _data;}
242  private:
243  char *_data;
244  int _size;
245 };
246 
247 }
248 
249 #endif /* TBX_CLIPBOARD_H_ */
tbx::Clipboard::clear
void clear()
Clear all the local clipboard data.
Definition: clipboard.cc:348
tbx::ClipboardFormatListener::clipboard_format_available
virtual void clipboard_format_available(int file_type)=0
Called after a check_format method with the first format found.
tbx::SaverFinishedHandler
Listener for when the save as dialogue box has been closed.
Definition: saver.h:218
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::Clipboard::native_file_type
int native_file_type() const
Return the native file type.
Definition: clipboard.cc:412
tbx::ClipboardText::size
int size()
The size of the data in bytes.
Definition: clipboard.h:224
tbx::Window
The Window object represents a toolbox window.
Definition: window.h:70
tbx::WimpMessage::SendType
SendType
Enumeration specifying the type of message to send.
Definition: wimpmessagelistener.h:191
tbx::Clipboard::native_data
ClipboardData * native_data() const
Get the native data from the data stored locally.
Definition: clipboard.cc:401
tbx::SaverSaveCompletedHandler
Listener for when a save has been completed.
Definition: saver.h:266
tbx::ClipboardData::data
virtual const char * data()=0
The data to copy.
tbx::Clipboard::paste
void paste(int file_type, Gadget &gadget, int x=0, int y=0)
Paste data from global clipboard.
Definition: clipboard.cc:129
tbx::ClipboardFormatListener::clipboard_empty
virtual void clipboard_empty()
Called if there is no data on the global clipboard.
Definition: clipboard.h:74
tbx::Clipboard::remove_format_listener
void remove_format_listener(ClipboardFormatListener *listener)
Remove a listener for the result of a Clipboard::check call.
Definition: clipboard.cc:468
tbx::Clipboard
Class to handler the global RISC OS clipboard.
Definition: clipboard.h:83
tbx::Clipboard::copy
void copy(int file_type, ClipboardData *data, bool add=false)
Copy the given data to the local clipboard and make if available to the global clipboard.
Definition: clipboard.cc:80
tbx::ClipboardClaimedListener::clipboard_claimed
virtual void clipboard_claimed()
Called when another application has claimed the global clipboard.
Definition: clipboard.h:52
tbx::clipboard
Clipboard * clipboard()
Return pointer to the global clipboard.
Definition: clipboard.h:177
tbx::WimpRecordedMessageListener
Listener to handle WimpRecordedMessages (event code 18)
Definition: wimpmessagelistener.h:270
tbx::Clipboard::add_format_listener
void add_format_listener(ClipboardFormatListener *listener)
Add a listener for the result of a Clipboard::check call.
Definition: clipboard.cc:458
tbx::ClipboardFormatListener
Listener for the response to check_format(s)
Definition: clipboard.h:60
tbx::Clipboard::check
void check(int file_type, Window &window)
Check the global clipboard for a given file type.
Definition: clipboard.cc:280
tbx::Clipboard::add_claimed_listener
void add_claimed_listener(ClipboardClaimedListener *listener)
Add a listener for when the clipboard has been claimed by another application.
Definition: clipboard.cc:431
tbx::ClipboardData::copy_finished
virtual void copy_finished()
Called when a copy of the data has finished.
Definition: clipboard.h:212
tbx::SaverFillBufferHandler
Handler for SaverFillBuffer handler.
Definition: saver.h:303
tbx::ClipboardClaimedListener
Listener for clipboard related events.
Definition: clipboard.h:42
tbx::Clipboard::instance
static Clipboard * instance()
Return the global clipboard instance, creating it if it does not exist.
Definition: clipboard.cc:62
tbx::Clipboard::data
ClipboardData * data(int file_type) const
Get data stored locally of the given file type.
Definition: clipboard.cc:388
tbx::ClipboardText
Convenience class to create clipboard data containing a string.
Definition: clipboard.h:219
tbx::Clipboard::remove_claimed_listener
void remove_claimed_listener(ClipboardClaimedListener *listener)
Remove a listener for when the clipboard has been claimed by another application.
Definition: clipboard.cc:440
tbx::ClipboardBytes::ClipboardBytes
ClipboardBytes(char *bytes, int size, bool copy=true)
Construct a clipboard data object to hold an array of bytes.
Definition: clipboard.cc:723
tbx::WimpUserMessageListener
Listener to handle WimpUserMessages (event code 17)
Definition: wimpmessagelistener.h:253
tbx::ClipboardData::copy_started
virtual void copy_started()
Called when data is about to be copied to another application from via clipboard.
Definition: clipboard.h:208
tbx::ClipboardBytes::size
int size()
The size of the data in bytes.
Definition: clipboard.h:240
tbx::ClipboardBytes
Convenience class to create clipboard data for an array of bytes.
Definition: clipboard.h:235
tbx::ClipboardText::data
const char * data()
The data to copy.
Definition: clipboard.h:225
tbx::SaverSaveCompletedEvent
Event for SaverSaveCompletedHandler.
Definition: saver.h:235
tbx::ClipboardClaimedListener::clipboard_claimed_caret
virtual void clipboard_claimed_caret()
Called when another application has claimed the caret/selection.
Definition: clipboard.h:48
tbx::SaverFinishedEvent
Event information for when saver dialogue has been completed.
Definition: saver.h:191
tbx::Gadget
This is the base class for all Gadgets.
Definition: gadget.h:49
tbx::WimpAcknowledgeMessageListener
Listener to handle WimpAcnowledgeMessages (event code 19)
Definition: wimpmessagelistener.h:290
tbx::Clipboard::owns_clipboard
bool owns_clipboard() const
Check if global clipboard is currently owned by this application.
Definition: clipboard.h:111
tbx::Clipboard::claim_caret
void claim_caret()
Claim the caret/selection for this application.
Definition: clipboard.cc:371
tbx::ClipboardData
Base class for data used on the clipboard.
Definition: clipboard.h:188
tbx::ClipboardData::size
virtual int size()=0
The size of the data in bytes.
tbx::SaverSaveToFileHandler
Handler to save data to a file.
Definition: saver.h:282
tbx::Saver
Class to give the same interface to saving to an external application as the SaveAs dialogue.
Definition: saver.h:61
tbx::ClipboardBytes::data
const char * data()
The data to copy.
Definition: clipboard.h:241