Gtkmm Text Entry filters - gtk

I am doing Gtkmm GUI programming now and I am new to this programming language.
How to allow the user to only enter the numeric value in Gtkmm Entry text box.

Use Gtk::SpinButton instead of Gtk::Entry to allow numeric input only.

you need to add a key release signal for your Entry for example this is my entry "m_EntryAmount"
m_EntryAmount.signal_key_release_event().connect(sigc::mem_fun(*this, &Vente::entryKeyReleaseAmount));
and add the signal function
bool Sales::entryKeyReleaseAmount(GdkEventKey* /* event */ )
{
if(m_EntryAmount.get_text()!="" && is_number(m_EntryAmount.get_text()) ){
//now the Entry is not empty and its a number
//do what you want here
}else{
//here the Entry is not a number you can just delete it
m_EntryAmount.set_text("");
}
return true;
}
and add the is_number function
bool Vente::is_number(const string& s)
{
return !s.empty() && std::find_if(s.begin(),
s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}
this is just a simple example to make an idea about that, you can do it by your self using your way

Related

How to read an array with BsonReader in MongoDB?

Basically, I have a data set where a document can contain an array of variable length. I'm writing a codec, so all I have is BsonReader. The problem is readStartArray() returns void rather than returning, for example, the number of elements. Also, I don't see any methods that allow one to test when the end of the array has been reached (apart from trying readEndArray() and catching the exception).
Any ideas?
I found the documentation for custom serialization to be lacking too, but you just need to work with the Reader.State property. Once you've identified the start of the array you need to iterate over each of the elements until state == BsonReaderState.EndOfArray. You need to ensure that you only call ReadBsonType when the state is "type".
The following example deserializes an array of documents:
context.Reader.ReadStartArray();
bool ctn = true;
do
{
//If in "type" state we can call ReadBsonType. The order is important.
if (context.Reader.State == BsonReaderState.Type)
{
context.Reader.ReadBsonType();
}
//Now that we're in a value state we can read the value safely
if (context.Reader.State == BsonReaderState.Value)
{
if (context.Reader.CurrentBsonType == BsonType.Document)
{
//Handle the document here. In this case pass I it to a private method
DeserializeChild(context, args, node);
}
//state will now be "type", so continue do loop and recheck type
continue;
}
if ( BsonReaderState.EndOfArray == BsonReaderState.EndOfArray)
{
context.Reader.ReadEndArray();
ctn = false;
}
} while (ctn);
Ah, it's done with:
while (reader.readBsonType() == BsonType.DOCUMENT) {
reader.readStartDocument();
// ...
reader.readEndDocument();
}
(it's an array of documents in this case, I'm still new to BSON so I don't know how it would work for an array of values).

How to get and set Real value from RealEdit in a Form's Grid ?

I have in a Grid a RealEdit , I set the autodeclaration YES.
The name is myRealEdit , DataSource is myTable and the Field is myRealField.
In the modified method I want to get the value, I need to do a IF control.
IF the value is 0 change the Filed's value IF the value is not 0
throws the value entered and restores the previous value.
I used this code, in modified method:
public boolean modified()
{
boolean ret;
real storedValue;
ret = super();
storedValue = myTable.myRealField; // there is another way to get the value ?
if (myRealEdit.valueStr() == "0")
//accept the value
if (!myRealEdit.valueStr() != "0")
{
myRealEdit.realValue(storedValue);
}
return ret;
}
If the value is not 0 (zero) don't restore the previous value.
I have to use another method ? There is another way to get the real value ?
Thanks in advice,
enjoy!!
Since you are using the modified method in your answer, I suppose you want to put this field validation on the control level (instead of the datasource or table level).
As #Jan B. Kjeldsen suggested in his comment, you should use the validate method to do this validation. Use the modified method only if you want to add some logic that is executed in addition to the field value modification.
The validate method could look similar to
public boolean validate()
{
return this.realValue() == 0 && super() || checkFailed(strFmt("Value %1 is not permitted", this.realValue()));
// TODO please replace this with a Label and explain to the user why the value is not permitted and what he or she can do to resolve this
}
I find a possible way,
I used this code:
public boolean modified()
{
boolean ret;
if (myRealEdit.valueStr() == "0")
{
//accept the value
ret = super();
}
if (!myRealEdit.valueStr() != "0")
{
info("Value not permit");
// nothing to do
}
return ret;
}
In this way, if and only if I have a value 0 I modified the value.
I need to get or read the Real value just inserted from myRealEdit in the modified method.
If the community has comments or improvements inserted, will be more information.

jquery select2 plugin how to match only beginning of word

I am using select2 in my website and i want the autocomplete to match only the beginning of the word. For example, if I type "CA" I want CAmeroun to appear and not "vatiCAn".
I figured out how to resolve this by searching in the documentation (here https://github.com/select2/select2/issues/428).
In select2 library, replace in select2.js :
matcher: function(term, text) {
return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
},
by :
matcher: function(term, text) {
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
},
And tadaaa. It works. I hope someone who is better in JS (99% of JS developers) could give a better answer or create a good patch.
Don't forget to minify your JS ;) !
Inspired by #IsmailH answer. I've merged this code as matchCustom in the provided example, here.
And here's my modification,
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
var modifiedData = $.extend({}, data, true);
// You can return modified objects from here
// This includes matching the `children` how you want in nested data sets
return modifiedData;
}
// Return `null` if the term should not be displayed
return null;
};

tinymce.dom.replace throws an exception concerning parentNode

I'm writing a tinyMce plugin which contains a section of code, replacing one element for another. I'm using the editor's dom instance to create the node I want to insert, and I'm using the same instance to do the replacement.
My code is as follows:
var nodeData =
{
"data-widgetId": data.widget.widgetKey(),
"data-instanceKey": "instance1",
src: "/content/images/icon48/cog.png",
class: "widgetPlaceholder",
title: data.widget.getInfo().name
};
var nodeToInsert = ed.dom.create("img", nodeData);
// Insert this content into the editor window
if (data.mode == 'add') {
tinymce.DOM.add(ed.getBody(), nodeToInsert);
}
else if (data.mode == 'edit' && data.selected != null) {
var instanceKey = $(data.selected).attr("data-instancekey");
var elementToReplace = tinymce.DOM.select("[data-instancekey=" + instanceKey + "]");
if (elementToReplace.length === 1) {
ed.dom.replace(elementToReplace[0], nodeToInsert);
}
else {
throw new "No element to replace with that instance key";
}
}
TinyMCE breaks during the replace, here:
replace : function(n, o, k) {
var t = this;
if (is(o, 'array'))
n = n.cloneNode(true);
return t.run(o, function(o) {
if (k) {
each(tinymce.grep(o.childNodes), function(c) {
n.appendChild(c);
});
}
return o.parentNode.replaceChild(n, o);
});
},
..with the error Cannot call method 'replaceChild' of null.
I've verified that the two argument's being passed into replace() are not null and that their parentNode fields are instantiated. I've also taken care to make sure that the elements are being created and replace using the same document instance (I understand I.E has an issue with this).
I've done all this development in Google Chrome, but I receive the same errors in Firefox 4 and IE8 also. Has anyone else come across this?
Thanks in advance
As it turns out, I was simply passing in the arguments in the wrong order. I should have been passing the node I wanted to insert first, and the node I wanted to replace second.

How can I tell if a <script> tag with a given src attribute is present on the page in ASP.net?

Like the title says, I'm trying to find out if I need to include a script library that my ASP.net UserControl needs to work. I don't want to include it multiple times per page, but I want my control to be able to be used multiple times on the same page.
How can I, in the codebehind of my control, check to see if a given <script/> tag is present?
This is .Net 2.0, no LINQ.
If !Page.ClientScript.IsClientScriptIncludeRegistered("jQuery")
Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");
or if you need a script block, And want to include the control's type:
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myScript"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript"
, "<script>alert('xx');</script>", false);
This might be a bit overkill for your usage, but I'm using it to search for existing CSS and JS before adding to the page:
private static bool HeaderLinkExists(Page page, string path)
{
path = path.ToLowerInvariant();
foreach (Control c in page.Header.Controls)
{
if (c is HtmlLink)
{
// stylesheet (or other links), check href
HtmlLink link = (HtmlLink)c;
if (link.Href.ToLowerInvariant().Contains(path))
{
return true;
}
}
else if (c is HtmlGenericControl)
{
// any generic html tag, check for src or href
HtmlGenericControl hgc = (HtmlGenericControl)c;
if ((!string.IsNullOrEmpty(hgc.Attributes["src"]) && hgc.Attributes["src"].ToLowerInvariant().Contains(path)) || (!string.IsNullOrEmpty(hgc.Attributes["href"]) && hgc.Attributes["href"].ToLowerInvariant().Contains(path)))
{
return true;
}
}
else if (c is LiteralControl)
{
// scripts or other html literal controls, use regex to look for src or hrefs and check each one
LiteralControl lit = (LiteralControl)c;
if (MatchLiteralText(lit.Text, path))
{
return true;
}
}
else if (c is Literal)
{
// similar to above, use regex to look for src or hrefs and check each one
Literal lit = (Literal)c;
if (MatchLiteralText(lit.Text, path))
{
return true;
}
}
}
return false;
}
private static readonly Regex linkMatcher = new Regex(#"(?:src|href)\s*=\s*([""']?)(?<LinkValue>[^\1]+?)[\1>]", RegexOptions.Compiled);
private static bool MatchLiteralText(string text, string path)
{
if (!string.IsNullOrEmpty(text))
{
text = text.ToLowerInvariant()
foreach (Match m in linkMatcher.Matches(text))
{
if (m.Groups["LinkValue"].Value.Contains(path))
{
return true;
}
}
}
return false;
}
// usage:
if (!HeaderLinkExists(page, "/css/controlstyles.css"))
{
HtmlHeadUtility.RegisterStylesheetInHeader(page, "~/css/controlstyles.css");
}
if (!HeaderLinkExists(page, "/js/controlscript.js"))
{
HtmlHeadUtility.RegisterClientScriptIncludeInHeader(page, "~/js/controlscript.js");
}
It works great if you're sure that the LINK or SCRIPT is guaranteed to be in the HEAD.
No, there isn't.
Instead, you should maintain a set of <script> tags that have already been included, and update it whenever you add a script. (For example, a [ThreadStatic] static List<String>)
I would suggest re-thinking your problem/solution. It seems like what you are trying to do would couple your user control and the pages that use it. This would require that a page reference that script in order to use a control.
Is there any reason you can't just put the script tag in your user control so that the pages consuming your user control don't need to know that they need to include a specific script tag to work?
I tend to think that if you want to just use a control you should be able to just use it.