Page 1 of 1

Basic GUI

Posted: Wed Sep 12, 2018 6:31 pm
by Laurent Berger
Hi,

I'm looking for basic GUi for noobs. Something like this

Code: Select all

void main (void)
{
double x=wxGetValue("Real number please:");
wxString s=wxGetString("Your name please:);
}
wxGetValue or wxGetString open a dialog box and when user press return key dialog is closed. No need of event loop. Is possible with wxWidgets?

Re: Basic GUI

Posted: Wed Sep 12, 2018 7:01 pm
by doublemax
This is the simplest you can do:

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h> 

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
      long number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);
      wxString s = wxGetTextFromUser("enter string", "your caption here", "" );

      wxLogMessage("your number was %d and the string was '%s'", number, s );

      return false;
    }
};

IMPLEMENT_APP(MyApp)

Re: Basic GUI

Posted: Wed Sep 12, 2018 7:38 pm
by Laurent Berger
Thanks
I need main entry point. With your answer and viewtopic.php?t=43513

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h>
#include<iostream>
#include<string>

class GetNumberApp : public wxApp
{
public:
    int number;
    virtual bool OnInit()
    {
        number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);

        return false;
    }
};

class GetTextApp : public wxApp
{
public:
    wxString texte;
    virtual bool OnInit()
    {
        texte = wxGetTextFromUser("enter number", "your prompt here", "");

        return false;
    }

};

class GetFileNameApp : public wxApp
{
public:
    std::string fileName;
    virtual bool OnInit()
    {
        fileName = wxFileSelector("Choose a file to open");

        return false;
    }
};

std::string GetFileName()
{
    std::string x;
    GetFileNameApp* pApp = new GetFileNameApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    x = pApp->fileName;
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

template<class T>T GetNumber()
{
    T x;
    GetNumberApp* pApp = new GetNumberApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    x = pApp->number;
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

template<>double GetNumber<double>()
{
    double x;
    GetTextApp* pApp = new GetTextApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    pApp->texte.ToCDouble(&x);
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

void main(int argc,char**argv)
{
    
    int l1 = GetNumber<int>();
    std::cout << "First number "<<l1 << "\n";
    double l2 = GetNumber<double>();
    std::cout << "Second number " << l2 << "\n";
    std::string s = GetFileName();
    std::cout << "File name " << s << "\n";
    // cleaning up...
}

Do you think I can use this way to write basic gui?

Re: Basic GUI

Posted: Thu Sep 13, 2018 11:10 am
by doublemax
This looks unnecessarily complicated. What exactly are you trying to do?

Re: Basic GUI

Posted: Thu Sep 13, 2018 2:17 pm
by ONEEYEMAN
Hi,
On top of what doublemax said - you can't have 2 wxApp-derived classes in the project. What is the purpose?
Also, based on your responce and the code you wrote it seems you don't understand the notion of the GUI and wxWidgets in general.

I suggest your start with some basic C++ classes, then take some GUI programming classes and then move to some specific framework - i.e. MFC, wxWidgets, GTK, Qt, FLTK, etc.

Thank you.

Re: Basic GUI

Posted: Thu Sep 13, 2018 6:17 pm
by Laurent Berger
ONEEYEMAN wrote:Hi,
On top of what doublemax said - you can't have 2 wxApp-derived classes in the project. What is the purpose?
Also, based on your responce and the code you wrote it seems you don't understand the notion of the GUI and wxWidgets in general.

I suggest your start with some basic C++ classes, then take some GUI programming classes and then move to some specific framework - i.e. MFC, wxWidgets, GTK, Qt, FLTK, etc.

Thank you.
Why do you write such thing? it sounds like insults

Re: Basic GUI

Posted: Thu Sep 13, 2018 6:24 pm
by Laurent Berger
doublemax wrote:This looks unnecessarily complicated. What exactly are you trying to do?
My students don't know gui loop and have to learn some basics in C++ in console mode.

Today cout and cin in a black screen with young people knowing only popup windows in instagram, facebook..it's difficult.
I want something really light to read data (int double, string and filename) in popup windows
I try nanogui, imgui : it is not for noobs

Re: Basic GUI

Posted: Thu Sep 13, 2018 6:56 pm
by ONEEYEMAN
Hi,
I didn't want to insult you. If I did - I apologize.

Going back to your question - did you look at the console sample?

Thank you.

Re: Basic GUI

Posted: Thu Sep 13, 2018 7:28 pm
by Laurent Berger
ONEEYEMAN wrote:Hi,
I didn't want to insult you. If I did - I apologize.

Going back to your question - did you look at the console sample?

Thank you.
Ok close
Now you said "you can't have 2 wxApp-derived classes in the project" but when I run my program It works :

Code: Select all

   int l1 = GetNumber<int>(); // I can see a window here and press return closed
    std::cout << "First number "<<l1 << "\n";
    double l2 = GetNumber<double>();
    std::cout << "Second number " << l2 << "\n";
    std::string s = GetFileName();
    std::cout << "File name " << s << "\n";
Application video :http://perso.univ-lemans.fr/~berger/Afs ... 211646.mp4 Video made with https://github.com/LaurentBerger/wxFilmEcran
I tried wxConsole I cannot see any box to read data (with -d or -s)

My question is then how can I write a programm using in console mode to get a result same as my video?

Re: Basic GUI

Posted: Thu Sep 13, 2018 7:57 pm
by doublemax
My question is then how can I write a programm using in console mode to get a result same as my video?
It sounds as if your solution already works?

This is the modified version of my earlier code:

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h>
#include <iostream>

int main(int argc, char **argv)
{
    wxInitializer init(argc, argv);
    if ( !init.IsOk() ) {
        wxPrintf("Failed to initialize wxWidgets.\n");
        return 1;
    }

    wxApp::SetInstance( new wxApp() );

    long number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);
    wxString s = wxGetTextFromUser("enter string", "your caption here", "" );

#if 0
    wxLogMessage("your number was %d and the string was '%s'", number, s );
#else
    std::cout << "number "<< number << "\n";
    std::cout << "string "<< s.ToAscii() << "\n";
#endif
    return 0;
}
This is quick-and-dirty, i don't even know if the wxApp construction is ok this way. But i tested it and it seems to work.

Re: Basic GUI

Posted: Thu Sep 13, 2018 8:16 pm
by Laurent Berger
Nice and only one wxApp as suggested by ONEEYEMAN
I tested with windows 10 and vs 2017. May be it's a good sample to add to wxWidgets repo

What's your platform?

Re: Basic GUI

Posted: Thu Sep 13, 2018 8:51 pm
by doublemax
I'm using Windows 7 and VS2013.