how to clear the entry box when it's active(clicked) [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I wanted to clear my entrybox in order to when I want to enter something again it's clear.
I implemented a Simple Webbrowser.
In the entrybox is always the url but what I was looking for is that if i click (activate) the entrybox, the entrybox should be cleared.
I hopped I could do something like this:
if(gtk_window_get_focus(GTK_WINDOW(w->window)) == w->entry)
gtk_entry_set_text (GTK_ENTRY(w->entry), "");
But I don't realy know where to do it and how this wokrs that it can detect that the entrybock was clicked.

There are a couple of ways, but before we start: user may want to copy URL, clearing it on click event may be confusing.
Connect to parent class "grab-focus" signal
Use gtk_entry_set_placeholder_text to make text a placeholder (appears as slightly shadowed)
Conforming to the note above: set a secondary icon (gtk_entry_set_icon_from_icon_name, icon "edit-clear-symbolic") and connect to "icon_press" signal.

Complementing Alexander's answer ...
In case you don't know to associate callbacks with signals, you can use g_signal_connect(). This takes four parameters:
the object we are interested in - we need to use G_OBJECT() to cast this to the relevant type
the signal we want to watch - in this case grab-focus
the callback to invoke when the signal is raised
arbitrary user data (probably not needed in this case).
In this case you could call it like
g_signal_connect(G_OBJECT(w->entry), "grab-focus", G_CALLBACK(on_input_focus), NULL);
If you check the signature of the the callback for the grab-focus signal you will see it accepts two arguments and returns nothing. The last argument is the arbitrary user data which was the last parameter in the g_signal_connect() function; we did not set it and can ignore it. The first argument is the widget on which the signal was triggered - which we can cast to a GtkEntry.
void on_input_focus(GtkWidget *w, gpointer data) {
gtk_entry_set_text(GTK_ENTRY(w), "");
}

Related

Is there any way to assign properties to differentiate values which would otherwise be considered the same? (in C language) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Let me start by saying that I'm a beginner programmer that knows very basic C
The exact code I'm working on doesn't matter here its just that I've run into a very odd problem (I'm messing around with ASCII graphics)
I want to differentiate between two 'O' characters. I could just use different characters but I want it to look good. I tried using Unicode to differentiate them (since Latin O and Greek Omicron look the same) but it won't work if I send it to someone else (since my output is on command prompt) edit: won't work if I send it to someone since command prompt must be manually configured to allow unicode characters (atleast to my understanding)
I don't know if using structures would work and I want to write this in C and not any other OOP language like python (because I'm practicing C)
So is there any logic I could use to do this?
EDIT: I want to apply different behaviours to both O and there will be multiple O of each type on screen (ik this is what objects do but pls help me with a logic for C)
EDIT 2: Apparently my question is very vague so I'll elaborate
I'm having a 2d array that I'm using as a game screen
I want O to come in from each side (top bottom right left), scroll across the screen and exit from the other side
I already came up with the logic to make it work for a single side but the problem comes in when I want to make it work for all sides
My current logic can't differentiate between an O that has to go left and and O that has to go down
Hence why I want to different between two instances of O
(I haven't posted my code since I just need the logic and want to solve the actual code by myself and also it's probably very inefficient or convuluted etc)
tldr: I want to differentiate between "O" and "O"
My current logic can't differentiate between an O that has to go left and and O that has to go down
Your problem is that you don't differentiate internal representation and external presentation.
You have two distinct objects that move across the screen. They both look like an O character. This doesn't mean your program should store them both as 'O', some variation of 'O' such as the omicron character. The program can store e.g. numbers 1 and 2, or any two different objects that are convenient to work with, and display an O in place of either.

Console returns the whole array but returning "undefined" when I want the first element of it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I think the screenshot below explains my problem well. Console is returning the whole array successfully. But when I want the first element of it, it returns "undefined".
I am pasting the .ts file's related part below as well. Thanks in advance.
You console debugging code console.log(..) statements have executed first before the subscription results are returned. Even though this will give the array output, the first element of the array is showing as undefined since an element is being pushed into the array from the service response while the console method is reading the array element.
In your call to the service that is subscribed, move the console logging into the subscription response block as shown:
myArrayData: any[] = whatever your array value is
...
this.api.MyServiceMethod().subscribe((response: any) =>
{
this.myArrayData.push(..response.data);
console.log("myArrayData = " + this.myArrayData);
console.log("myArrayData[0] = " + this.myArrayData[0]);
});

C guessing game between two players with visual output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been given the assignment to create a quite complicated to me, but simple to some C game. The program will run and generate 25 random numbers between 1 to 100 (no repeats). The game is between two users.
Both the players will get two guesses each time. Every correct guess will be counted as 1 correct answer and will be displayed on the screen in the board.
The player having more number of correct guesses will be a winner.
The game will continue until the board is completely revealed.
The first screen should ask the user for his name, print a welcome message, and displays an empty 5x5 board. But this 5x5 board has the values in it internally
Now the program should ask the number of players (1 or 2)
If one, ask one name and second is computer and if 2 ask two names
For playing against computer, you are asked for two guesses and every correct guess is shown on the board.
Now 2 guesses for computer would be taken and shown on the screen.
I have tried everything to my knowledge to complete this but I lack a full understanding of C. Any help would be appreciated. I did not include my code because, honestly its just a mess and does not even run.
I would like to see someone be able to make such a game, so that I can study the logic. NOT COPY THE WORK
Simple steps. Start small and grow.
Easy one is to remove all questions to the user(s). Hard code the answers in the program. You can retrofit the IO later.
Start with a way ot generating 25 random numbers and load them into an array. Place the array in a global variable. You need a another array to show when a number is sucessfully guessed.
Now write a function to display that guessed array as 5X5.
Gradually build the program up
Always make functions
generate_array
show_guessed
....
If get stuck on specific things then post a new question.
This is not a direct answer, but a very long comment with some requests for more information and effort.
The problem I have with your request is that I see zero effort. I see a request for teh C0d3z and a promise not to cheat after receiving something that makes it far too easy to cheat.
What I, and probably others here, want to see is effort and some attempt.
Do you know how to print text to the screen like you see in the requirements? Do you know how to print text at all? If so, make the print routines and state this.
Do you know how to generate random numbers? If so, say so in your question and we see effort.
Do you know how to receive input from the user?
Do you know what an array is?
Do you know how to save code in your editor and compile it?
You can see that without any sort of background or starting code, we don't know where you are in your learning.
So...
Post an attempt at solving your problem in your question. If you are truly so new to coding that you cannot do this, then you need to sit down with your professor / TA / whomever and tell them this. Reading through your teaching material (textbook or whatever) should help a whole lot as well. This assignment feels like something I would see towards the end of a beginner's C class. If this is where you are, and you really have no clue what to do, then you may need to retake the class.

Update (refresh) text-label - GTK c-language

I have developed a graphic userinterface for a small program in c. Its some kind of calculator. I have two inputfields where one can enter numbers and I want to display the result as a text-label in the same window.
I do not know how to make the window or the text-label to update itself. I am used with GUIS in java and there are a method called invalidate() to refresh the window and its child-items? Is there a similar function in the gtk3-lib in c?
I don't understand. I think there is a button to press after entering numbers in inputfields, am i right? If so, just connect a function to the button clicked signal.
This function make the sum of 2 numbers and set the label text.
If you don't have the button, i think you need to connect the signal to the inputfields, something like:
"on_spinbutton1_value_changed" : update_text_label
P.S.
I don't know C, usally i use python, but i think is quite similar.
P.P.S.
Is this the same question founded here?

Any alternative to callbacks for UI programming?

Let's say I have a several step process like during software installation for example.
Each step display a text box and wait for the user to click the "next" button.
The standard way to do it is to have a callback like this:
process
{
Dialog1() // Will call callback2 when closed
}
callback2()
{
Dialog2() // Will call callback3 when closed
}
callbak3()
{
Dialog3() // Will call callback4 when closed
}
This technique makes the code quite unreadable when there is a lot of steps as you have to
divide you process into each successive callback function (not to mention save
context from one to another).
What would be an easier to read way to do it ? Ideally the process should read like
this:
process()
{
Dialog1()
callback1() // stop method until closed
Dialog2()
callback2() // stop method until closed
Dialog3()
callback3() // stop method until closed
}
Problem with this is that you can't stop the UI thread. Any idea or work around would be very appreciated.
PS: this as to work in C or Objective C
ANSWER
So after having discovered coroutines thanks to Martin B I've found this page: https://stackoverflow.com/posts/4746722/edit and ended up using this code:
define coRoutineBegin static int state=0; switch(state) { case 0:
define yield do { state=__LINE__; return;
case __LINE__:; } while (0);
define coRoutineEnd }
void process()
{
coRoutineBegin
Dialog1()
yield
Dialog2()
yield
Dialog3()
yield
Dialog4()
yield
coRoutineEnd
}
You're looking for coroutines, which provide exactly the concept you're looking for: Yielding control from a function without exiting it. In essence, your code would look like this:
process()
{
Dialog1()
yield
Dialog2()
yield
Dialog3()
}
Unfortunately, coroutines aren't supported natively by C or Objective C and are hard to implement generically without resorting to ugly hacks. However, you may be able to take the concept as a starting point for a special-case construct for your situation.
You could use a Mutex or similar concept where the dialog is opened and run in a different thread.
Here is a post with an example I think is valid:
Synchronization/wait design for cross-thread event signaling (Obj-C)?
I don't quite understand the problem. Why doesn't showing a modal dialog work? Modal dialogs block until they are dismissed so your logic would look like:
Dialog1()
Dialog2()
Dialog3()
Another solution is to have your dialogs or callbacks or whatever send events. You then bind to those events. Your main logic would then look like this (sorry, I don't know how to do GUI examples in C/Objective-C, so I'll use Tcl/Tk because its highly readable):
bind $rootWindow <<Step1>> showDialog1
bind $rootWindow <<Step2>> showDialog2
bind $rootWidow <<Step3>> showDialog3
# kick off the first step
event generate $rootWindow <<Step1>>
The showDialogX functions would do whatever they need to do, then generate an event saying "I'm done, ready for the next step".

Resources