Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on May 13, 2011, 05:06:07 pm

Title: C++ wxWidgets Text Editor
Post by: Munchor on May 13, 2011, 05:06:07 pm
I was trying to make a text editor in C++ but I found no tutorials on the web. So I had to do some research on a few stuff to put it up and running. It is very simple, it is more of an example and has no Save/Open options, maybe I'll add those later.

If you want the full code you can find it here (http://paste.bwns.be/p/f6e1718a7). In case you want a screenshot of it, here's one taken in Linux Mint (it is cross-platform don't worry)

Spoiler For Spoiler:
(http://img.removedfromgame.com/imgs/GUITextEditor.png)

Here's the main part of the program, commented:

Code: [Select]
wxTextCtrl *textControl = new wxTextCtrl;                                  //Define the new textCtrl
   
    textControl = new wxTextCtrl(this, ID_TextBox,
      wxT(""), wxDefaultPosition, wxDefaultSize,
      wxTE_MULTILINE | wxTE_RICH , wxDefaultValidator, wxTextCtrlNameStr); //Creates a text box, with no text, id=ID_TextBox, it's multiline
     
    wxMenu *menuFile = new wxMenu;                                         //Create a new menu to be placed on a menu bar

    menuFile->Append( ID_About, _("&About...") );                          //Add the first option to the menu (will show up on top)
    menuFile->AppendSeparator();                                           //Add a separator, this is handy
    menuFile->Append( ID_Quit, _("E&xit") );                               //Add an option to the menu

    wxMenuBar *menuBar = new wxMenuBar;                                    //Create a new menu bar
    menuBar->Append( menuFile, _("&File") );                               //Append the file menu

    SetMenuBar( menuBar );                                                 //Defines the window menu bar to menuBar
   
    Maximize();                                                            //Maximizes the window

Attached is the source file.
Title: Re: C++ wxWidgets Text Editor
Post by: alberthrocks on May 13, 2011, 05:28:11 pm
Here's a Makefile to build this:
Code: [Select]
all:
@echo "** Building..."
g++ -c `wx-config --cppflags` helloworldgui.cpp -o helloworldgui.o
@echo "** Linking/creating executable..."
g++ `wx-config --libs` helloworldgui.o -o helloworld
clean:
@echo "** Removing object files and executable..."
rm -f helloworldgui.o helloworldgui
install:
echo '** Installing (why in the world do you want to install this??)...'
cp helloworldgui /usr/bin
uninstall:
@echo '** Uninstalling...'
rm /usr/bin/helloworldgui
Make a file called Makefile, copy+paste the above stuff into it, and save it in the same dir as helloworldgui.cpp.
Then, in a terminal, make sure you are in the directory where the Makefile and source are, and type:
Code: [Select]
make
To run, type:
Code: [Select]
./helloworldgui
make clean will delete the helloworldgui.o and helloworldgui executable files, make install will install helloworldgui into your computer (why would you want to do that? :P), and make uninstall will uninstall helloworldgui.
Title: Re: C++ wxWidgets Text Editor
Post by: Munchor on May 13, 2011, 05:30:01 pm
Hum, that sounds too complicated, here's my option:

Code: [Select]
g++ helloworldgui.cpp `wx-config --cxxflags` `wx-config --libs`
./a.out

Save it as compile.sh or whatever and build+run by doing sh compile.sh.

But I guess that has more options, but for developing it, I used this.
Title: Re: C++ wxWidgets Text Editor
Post by: ruler501 on May 13, 2011, 06:26:55 pm
If its an sh file can't it only be run in Linux. alberthrocks method would be multiplatform I believe
Title: Re: C++ wxWidgets Text Editor
Post by: Munchor on May 14, 2011, 04:21:59 am
Actually, I don't think /usr/bin exists in Windows for software, or does it?
Title: Re: C++ wxWidgets Text Editor
Post by: alberthrocks on May 14, 2011, 11:53:40 am
Hum, that sounds too complicated, here's my option:

Code: [Select]
g++ helloworldgui.cpp `wx-config --cxxflags` `wx-config --libs`
./a.out

Save it as compile.sh or whatever and build+run by doing sh compile.sh.

But I guess that has more options, but for developing it, I used this.
bleh... remember that Linux has no extensions for shell scripts or binaries! :P
Preferably:
Code: [Select]
g++ helloworldgui.cpp `wx-config --cxxflags` `wx-config --libs` -o helloworldgui
./helloworldgui
...since you know exactly what was compiled. If you had many object files, it wouldn't be very pretty with all of them writing to ./a.out! :P

If its an sh file can't it only be run in Linux. alberthrocks method would be multiplatform I believe
Actually, I don't think /usr/bin exists in Windows for software, or does it?
Scout's right on this one - the make install rule will not work on Windows. But, the makefile works on all platforms (assuming GCC is in the PATH on Windows) due to the "make" binary being cross-platform.

For Windows, a slight modification needs to be made. Change
Code: [Select]
g++ `wx-config --libs` helloworldgui.o -o helloworldto
Code: [Select]
g++ `wx-config --libs` helloworldgui.o -o helloworld.exe
Title: Re: C++ wxWidgets Text Editor
Post by: Munchor on May 15, 2011, 07:56:24 am
Yeah I guess. So it has to be changed.

Also what about in the install label not only move to /usr/bin and create a desktop file in the /home/Desktop/ that directs to it?