Paste Certain Clipboard Text into TEdit in CBB 10 - clipboard

I wish that when the user clicks the button, ONLY TEXT THAT CONTAINS AN URL (beginning with http://) on the Clipboard is automatically pasted into the TEdit.
I've tried the following code but doesn't work at all.
#include <Clipbrd.hpp>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String Text = "http://";
if (Clipboard()->HasFormat(CF_TEXT))
{
Edit->Text = ContainsText(Clipboard()->AsText, Text);
// Clipboard()->Clear();
}
}

ContainsText() returns a bool indicating whether the subtext was found or not. You are assigning that result directly to your TEdit instead of using it to make a decision whether or not to assign the clipboard text to the TEdit.
Try this instead:
#include <Clipbrd.hpp>
#include <StrUtils.hpp>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Clipboard()->HasFormat(CF_TEXT))
{
String CBText = Clipboard()->AsText;
if (ContainsText(CBText, "http://"))
{
Edit->Text = CBText;
// Clipboard()->Clear();
}
}
}
BTW, http:// is not the only URL scheme widely used. At a minimum, consider also looking for https:// as well.

Related

How to store text on the system clipboard after application has quit using GTK3?

I am trying to update the system clipboard from a GTK application. Here is a simplified program:
#include <gtk/gtk.h>
void callback(GtkClipboard *clipboard, const gchar *text, gpointer data) {
printf( "In callback: text = '%s'\n", text);
}
int main() {
gtk_init(NULL, NULL);
GdkScreen *screen = gdk_screen_get_default();
GdkDisplay *display = gdk_display_get_default();
GtkClipboard *clipboard = gtk_clipboard_get_for_display(
display, GDK_SELECTION_PRIMARY );
gtk_clipboard_set_text( clipboard, "Hello world", -1);
gtk_clipboard_request_text( clipboard, callback, NULL );
if( gdk_display_supports_clipboard_persistence(display) ) {
printf( "Supports clipboard persistence.\n");
gtk_clipboard_store(clipboard);
}
}
The output (after compiling the above program on my Ubuntu 19.10 laptop):
In callback: text = 'Hello world'
Note that the text: Supports clipboard persistence. is not shown, so apparently the display does not support updating the system clipboard (?). However, I can easily update it with the xclip command. Why is it not possible to do it from GTK?
GDK_SELECTION_PRIMARY -> is used to get the currently-selected object or text
GDK_SELECTION_CLIPBOARD -> is used perform operations like Cut/copy/paste
(https://developer.gnome.org/gtk3/stable/gtk3-Clipboards.html#gtk-clipboard-get-for-display)
and to store a text the application has to remain in the main loop long enough to let the clipboard manager copy the text.
#include <gtk/gtk.h>
void callback(GtkClipboard *clipboard, const gchar *text, gpointer data) {
printf("In callback: text = '%s'\n", text);
}
int main() {
gtk_init(NULL, NULL);
GdkScreen *screen = gdk_screen_get_default();
GdkDisplay *display = gdk_display_get_default();
GtkClipboard *clipboard =
gtk_clipboard_get_for_display(display, GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_text(clipboard, "Hello world", -1);
gtk_clipboard_request_text(clipboard, callback, NULL);
if (gdk_display_supports_clipboard_persistence(display)) {
printf("Supports clipboard persistence.\n");
gtk_clipboard_store(clipboard);
}
g_timeout_add(100, gtk_main_quit, NULL);
gtk_main();
}
according to the doc(https://developer.gnome.org/gdk3/stable/GdkDisplay.html#gdk-display-supports-clipboard-persistence) clipboard_persistance will only check for a running clipboard daemon. I am guessing that there are some changes made in this area as I was not able to find any running clipboard-daemon in me machine (they might have integrated it into the window manager)
(https://wiki.ubuntu.com/ClipboardPersistence) -> this doc explains the problems with clipboard persistence and ways to fix it.
if you install "clipit"(clipboard manager) and try to copy the text without waiting in the main loop for few milliseconds your output will be "Clipboard is null, recovering"
xclip would have mostly stayed online for a few milliseconds to allow coppying of the text.

Screen orientation on iOS

Per this question in Delphi an FMX app can be selectively forced into landscape or portrait with code like this:
procedure TForm1.Chart1Click(Sender: TObject);
begin
if Application.FormFactor.Orientations = [TScreenOrientation.Landscape] then
Application.FormFactor.Orientations := [TScreenOrientation.Portrait]
else
Application.FormFactor.Orientations := [TScreenOrientation.Landscape];
end;
end;
I can't figure out how to translate this code above to C++Builder. I tried the following code based on this post but it gives access violation on both iOS and Android:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
_di_IInterface Intf;
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), Intf))
{
_di_IFMXScreenService ScreenService = Intf;
TScreenOrientations Orientation;
Orientation << TScreenOrientation::Landscape;
ScreenService->SetScreenOrientation(Orientation);
}
}
Is this even doable in FMX with C++Builder?
This line:
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), Intf))
should be this instead:
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), &Intf))
Note the addition of the & operator in the last parameter. This is even stated in the documentation:
Note: Please consider that you need to add & before Intf, as you can see in the code sample above.
Also, Intf really should be declared to match the interface you are requesting, eg:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
_di_IFMXScreenService ScreenService;
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), &ScreenService))
{
TScreenOrientations Orientation;
Orientation << TScreenOrientation::Landscape;
ScreenService->SetScreenOrientation(Orientation);
}
}

How could I choose one particular file to load with loadStrings

The title is explicit enough, I want to let the user choose the text file he want to open.
I do not know if there is an explorer or an input field already implemented on processing.
Any help would be great.
Use selectInput. From the Processing reference:
Opens a platform-specific file chooser dialog to select a file for input. After the selection is made, the selected File will be passed to the 'callback' function. If the dialog is closed or canceled, null will be sent to the function, so that the program is not waiting for additional input. The callback is necessary because of how threading works.
I've modified the example sketch they provide in the reference to include loading the file with the loadStrings method.
String[] txtFile;
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
// load file here
txtFile = loadStrings(filepath);
}
}
There is no Implemented method, but you could could make a buffer and monitor key presses like so:
String[] File;
String keybuffer = "";
Char TriggerKey = Something;
void setup(){
//do whatever here
}
void draw(){
//Optional, to show the current buffer
background(255);
text(keybuffer,100,100);
}
void keyPressed(){
if(keyCode >= 'a' && keyCode <= 'z'){
keybuffer = keybuffer + key;
}
if(key == TriggerKey){
File = loadStrings(keybuffer + ".txt");
}
}
when triggerkey is pressed, it loads the file

Adding custom metadata tags using LibTiff.Net

I now how to add a custom tag to an image but it's not showing up as the tag name in image viewer. I only see the number I assigned and its value.
Why there is no proper name for my custom tag?
using BitMiracle.LibTiff.Classic;
namespace WindowsFormsApplication1
{
class Program
{
private const TiffTag IMG_GUID = (TiffTag)666;
private static Tiff.TiffExtendProc m_parentExtender;
public static void TagExtender(Tiff tif)
{
TiffFieldInfo[] tiffFieldInfo =
{
new TiffFieldInfo(IMG_GUID, -1, -1, TiffType.ASCII, FieldBit.Custom, true, false, "IMG_GUID"),
};
tif.MergeFieldInfo(tiffFieldInfo, tiffFieldInfo.Length);
if (m_parentExtender != null)
m_parentExtender(tif);
}
static void Main(string[] args)
{
// Register the extender callback
// It's a good idea to keep track of the previous tag extender (if any) so that we can call it
// from our extender allowing a chain of customizations to take effect.
m_parentExtender = Tiff.SetTagExtender(TagExtender);
byte[] buffer = new byte[25 * 144];
string outputFileName = writeTiffWithCustomTags(buffer);
// restore previous tag extender
Tiff.SetTagExtender(m_parentExtender);
}
private static string writeTiffWithCustomTags(byte[] buffer)
{
string existingTiffName = "..\\..\\tifimages\\cramps.tif";
string outputFileName = existingTiffName;
using (Tiff image = Tiff.Open(outputFileName, "a"))
{
// set custom tags
image.SetDirectory(0);
string value = "test";
image.SetField(IMG_GUID, value);
image.CheckpointDirectory();
// Write the information to the file
image.WriteEncodedStrip(0, buffer, 25 * 144);
}
return outputFileName;
}
}
}
The application you use for viewing your TIFFs should know about your custom tags beforehand in order to be able to display its names.
It's not gonna happen! (Because you may select almost arbitrary integer for your custom tag).
So, there is nothing wrong with custom tags being displayed as (an integer, a value) pair. It's just the way custom tag work.

Determining object types in Qt

I have a series of QTextEdits and QLineEdits connected to a slot through a QSignalMapper(which emits a textChanged(QWidget*) signal). When the connected slot is called (pasted below), I need to be able to differentiate between the two so I know whether to call the text() or toPlainText() function. What's the easiest way to determine the subclass type of a QWidget?
void MainWindow::changed(QWidget *sender)
{
QTextEdit *temp = qobject_cast<QTextEdit *>(sender);
QString currentText = temp->toPlainText(); // or temp->text() if its
// a QLineEdit...
if(currentText.compare(""))
{
...
}
else
{
...
}
}
I was considering using try-catch but Qt doesn't seem to have very extensive support for Exceptions... Any ideas?
Actually, your solution is already almost there. In fact, qobject_cast will return NULL if it can't perform the cast. So try it on one of the classes, if it's NULL, try it on the other:
QString text;
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
if (textEdit) {
text = textEdit->toPlainText();
} else if (lineEdit) {
text = lineEdit->text();
} else {
// Return an error
}
You can also use sender->metaObject()->className() so you won't make unnecesary casts. Specially if you have a lot of classes to test. The code will be like this:
QString text;
QString senderClass = sender->metaObject()->className();
if (senderClass == "QTextEdit") {
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
text = textEdit->toPlainText();
} else if (senderClass == "QLineEdit") {
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
text = lineEdit->text();
} else {
// Return an error
}
I know is an old question but I leave this answer just in case it would be useful for somebody...

Resources