Vous êtes sur la page 1sur 33

Introduzione a wxWidgets

La programmazione delle GUI resa facile

Davide CD-RON Rondini


Montebelluna Linux User Group

18 ottobre 2007 Montebelluna

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

1 / 33

Licenza dutilizzo
c 2007, Davide CD-RON Rondini.
Copyright
Questo documento viene rilasciato secondo i termini della licenza Creative Commons
(http://creativecommons.org).
Lutente libero di:
distribuire, comunicare al pubblico, rappresentare o esporre in pubblico la
presente opera
alle seguenti condizioni:
Attribuzione Deve riconoscere la paternit dellopera allautore originario.
Non commerciale Non pu utilizzare questopera per scopi commerciali.
No opere derivate Non pu alterare, trasformare o sviluppare questopera.
In occasione di ogni atto di riutilizzazione o distribuzione, deve chiarire agli altri i termini della
licenza di questopera.
Se ottiene il permesso dal titolare del diritto dautore, possibile rinunciare a ciascuna di queste
condizioni. Le utilizzazioni libere e gli altri diritti non sono in nessun modo limitati da quanto
sopra. Questo un riassunto in lingua corrente dei concetti chiave della licenza completa (codice
legale), reperibile sul sito Internet
http://creativecommons.org/licenses/by-nc-nd/2.0/legalcode

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

2 / 33

Sommario

Cosa sono le wxWidgets


Descrizione generale e caratteristiche

wxWidgets e gli altri


Confronto con Qt e .NET

Editor grafici di finestre

Un programma di esempio

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

3 / 33

Cosa sono le wxWidgets

wxWidgets (ex wxWindows): framework di programmazione per la


creazione di GUI in ambiente multipiattaforma.
wxWidgets completamente OpenSource
basato su C++, con binding per altri linguaggi

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

4 / 33

Perch wxWidgets?
Esiste un certo numero di librerie per lo sviluppo di GUI
multipiattaforma. Nessuna di queste per, secondo lopinione degli
sviluppatori di wxWidgets, rispetta tutti e 4 questi requisiti:
1

Basso costo.

Disponibilit del codice sorgente.

Semplicit della programmazione.

Supporto per un vasto numero di compilatori.

Per sopperire sono nate le wxWidgets...


Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

5 / 33

Cosa offre wxWidgets

Realmente multipiattaforma
Supporta praticamente qualsiasi compilatore
Documentazione di altissima qualit
Wrapper leggero e veloce
Includono classi di alto livello
Buona struttura di astrazione
wxCode: database di progetti degli utenti
Gratis
Open Source
Progetto nato nel 1992 e ormai robusto

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

6 / 33

Feature disponibili
Un vasto numero di classi per finestre e controlli
Gestore degli eventi evoluto
Supporto per la stampa su tutte le piattaforme
Supporto per il debug e il logging
Integrazione a database
Drag and drop
Gestione di file di configurazione
Integrazione con OpenGL
Multi-threading
Programmazione di rete trasparente
Gestione di HTML e XML integrata
Supporto per linternazionalizzazione
Supporto OLE
Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

7 / 33

Piattaforme supportate
Piattaforme [2]
Win32 (Win 9x/NT/2K)
Win64 (Windows XP/2003)
Win16 (Windows 3.1)

AIX
HP-UX

Linux x86

IRIX

Linux S/390

SCO UnixWare

FreeBSD

DEC OSF/1 (a.k.a. Tru64)

NetBSD

OS/2

OpenBSD

OpenVMS

Mac OS

Davide Rondini (MontelLUG)

Solaris

wxWidgets

18 ott 2007 Montebelluna

8 / 33

Linguaggi di programmazione

Binding [3]
wxAda

wxLua

wxBasic

wxMax

wxD

wxNet

wxEuphoria

wxPerl

wxHaskell

wxPike

wxJava

wxPython

jwx (alternativo a wxJava)

wxRuby

wxJavaScript

wxSqueak

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

9 / 33

wxWidgets e le API native

Le funzioni non-GUI si appoggiano pi possibile alle funzioni


ANSI C++ e alle STL
Le widgets grafiche si appoggiano sempre sulle API native del
sistema!

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

10 / 33

Porting delle wxWidgets 1/2


Porting stabili
wxGTK Il port di default per Linux e Unix, basto su GTK+.
R

wxMSW Il per Microsoft Windows, sia a 32 che 64 bit (98, 2000,


NT, XP).
wxWinCE Porting per Windows CE, comprese le edizioni Pocket
PC e SmartPhone.
wxMac Basato su Carbon per Mac OS 9 e OS X.
wxX11 Port diretto sulle librerie X11.
wxMGL Port per il toolkit MGL sviluppato da SciTech Software Inc.
wxMotif Per le varianti Linux e Unix che usano OpenMotif o
Lesstif.

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

11 / 33

Porting delle wxWidgets 2/2


wxEmbedded [4]
Port commerciale per
lavorare su piattaforme
embeddeda .

Porting in sviluppo
wxCocoa Port per MacOS
X, che si appoggia a
Cocoa.

Sviluppato da unazienda
italiana.

wxOS2 OS/2.
wxPalmOS Porting per
PalmOS.
a

wxEmbedded logo and name


are registered trademarks of KOAN
s.a.s. - ITALY

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

12 / 33

Licenza

wxWidgets
wxWindows license
Open Source
(OSI approved)
Compatibile
L-GPLv2
Consente di
distribuire binari
derivati con
licenza a propria
scelta.
Davide Rondini (MontelLUG)

.NET
Qt
Dual License
GPLv2 per
sviluppo Open
Source
Commerciale
per applicazioni
commerciali

wxWidgets

.NET
Microsoft Reference
License (Ms-RL)
Consente solo
di vedere il
codice

18 ott 2007 Montebelluna

13 / 33

Pro

.NET

wxWidgets
Open Source
Documentazione
Facilit di programmazione
Piattaforme e
compilatori
supportati

Davide Rondini (MontelLUG)

Qt

.NET
Integrazione
perfetta con
KDE
Molto evoluto
Innovative

Diffusione
Supporto
commerciale
Nativo con
Windows

Veloce

wxWidgets

18 ott 2007 Montebelluna

14 / 33

Contro

.NET
.NET
Qt
wxWidgets
Multi-threading
poco evoluto
Poco note

Davide Rondini (MontelLUG)

Licenze
commerciali
costose
Rognose da
compilare in
Windows

wxWidgets

Essenzialmente
limitato a
Windows
Documentazione
Vincolato alla
politica
commerciale
Microsoft

18 ott 2007 Montebelluna

15 / 33

GUI editor per wxWidgets

DialogBlocks

wxGlade
Widget editor

IDE commerciale
multipiattaforma

Open source
Scritto in wxPython

Sviluppato da Julian
Smart, project leader di
wxWidgets

Davide Rondini (MontelLUG)

Salva codice C++, python,


Perl e XRC (XML
Resource Compiler)

wxWidgets

18 ott 2007 Montebelluna

16 / 33

wxGlade screenshot

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

17 / 33

Un semplice tutorial
Nel pacchetto delle wxWidgets ci sono tantissimi utili esempi
Il programma di esempio sar questo semplice editor di testo

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

18 / 33

La classe wxApp

class wxTestapp : public wxApp


{
public :
v i r t u a l bool O n I n i t ( ) ;
};

1
2
3
4
5

Lapplicazione viene costruita derivando una classe da wxApp


Bisogna reimplementare il metodo virtuale OnInit()

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

19 / 33

La finestra wxFrame e le classi wxWindow


class wxTestFrame : public wxFrame
{
public :
wxTestFrame ( const w x S t r i n g& t i t l e ,
const wxPoint& pos ,
const wxSize& pos ) ;
void OnQuit ( wxCommandEvent& event ) ;
void OnAbout ( wxCommandEvent& event ) ;
void OpenFile ( wxCommandEvent& event ) ;
void S a v e F i l e ( wxCommandEvent& event ) ;
void SaveFileAs ( wxCommandEvent& event ) ;

1
2
3
4
5
6
7
8
9
10
11
12

private :
DECLARE_EVENT_TABLE ( )
w x T e x t C t r l textFrame ;
wxString c u r r e n t F i l e ;

13
14
15
16

};
Davide Rondini (MontelLUG)

17

wxWidgets

18 ott 2007 Montebelluna

20 / 33

Struttura di una classe wxWindow


I tipi di finestra

La gestione delle finestre


Per costruire una finestra,
sufficiente derivare da
wxFrame o altre classi
simili

wxWindow La finestra di
base, senza decorazione.
Usata spesso nelle sue
derivazioni.

Tutti gli oggetti inseriti sono


figli della finestra

wxFrame La finestra con


decorazione, con attivabili
men e status bar.

Il distruttore si occupa
automaticamente di
distruggerre tutti i figli
Per distruggere
esplicitamente una finestra
si usa il metodo
Destroy()
Davide Rondini (MontelLUG)

wxWidgets

wxDialog Una finestra di


dialogo, con i pulsanti
Annulla e Ok.
wxWizard Per creare
wizard semplicemente e
rapidamente.
18 ott 2007 Montebelluna

21 / 33

Gestione degli eventi


Dichiarazione degli eventi
La gestione degli eventi viene
fatta associando degli ID a
metodi particolari
I metodi di gestione degli
eventi come unico parametro
un wxEvent e ritornano
sempre void.
Va assegnata la tabella degli
ID degli eventi

Gli ID degli eventi


enum
1
{
2
Menu_File_Quit = 100 , 3
Menu_File_About ,
4
Menu_File_Open ,
5
Menu_File_Save ,
6
Menu_File_Save_As
7
};
8

Se una classe gestisce eventi


deve dichiararla con la macro
DECLARE_EVENT_TABLE()

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

22 / 33

La tabella degli eventi

# include <wx / wx . h>


# include <wx / t e x t f i l e . h>
# include " w x t e s t . h "

1
2
3
4

BEGIN_EVENT_TABLE ( wxTestFrame , wxFrame )


5
EVT_MENU( Menu_File_Quit , wxTestFrame : : OnQuit )
6
EVT_MENU( Menu_File_About , wxTestFrame : : OnAbout )
7
EVT_MENU( Menu_File_Open , wxTestFrame : : OpenFile )
8
EVT_MENU( Menu_File_Save , wxTestFrame : : S a v e F i l e )
9
EVT_MENU( Menu_File_Save_As , wxTestFrame : : SaveFileAs ) 10
END_EVENT_TABLE ( )
11

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

23 / 33

La tabella degli eventi spiegata

La riga BEGIN_EVENT_TABLE() prende come parametri la


classe e la sua genitrice wxWidgets, che definisce i tipi di eventi
che sono implementabili
Esiste una macro per ciscun tipo di evento (menu, mouse,
comando, ecc)
ogni macro associa lID dellevento al metodo da chiamare
quando questo evento viene generato
Gli eventi sono propagativi : passano alloggetto genitore se non
processati.

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

24 / 33

Limplementazione di wxApp
IMPLEMENT_APP( wxTestapp )

1
2

bool wxTestapp : : O n I n i t ( )
{
wxTestFrame frame = new wxTestFrame (
wxT ( " wxWidgets Simple Text E d i t i n g Program " ) ,
wxPoint ( 5 0 , 5 0 ) , wxSize ( 4 5 0 , 3 4 0 ) ) ;

3
4
5
6
7
8

frame>Show (TRUE ) ;
SetTopWindow ( frame ) ;
r e t u r n TRUE;

9
10
11

12

IMPLEMENT_APP() la macro che implementa il main a partire dalla


classe wxApp. sempre necessaria in un programma wxWidgets.
Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

25 / 33

Il costruttore di wxFrame
wxTestFrame : : wxTestFrame ( const w x S t r i n g& t i t l e ,
const wxPoint& pos ,
const wxSize& s i z e )
: wxFrame ( ( wxFrame )NULL , 1, t i t l e , pos , s i z e ,
wxDEFAULT_FRAME_STYLE, wxT ( " Base frame " ) )

1
2
3
4
5

Parametri
1

Puntatore alla wxWindow genitrice. NULL per la finestra


principale.

ID della finestra, utile per alcuni metodi di gestione esplicita. -1 o


wxID_ANY lasciano a wxWidgets impostare un valore.

Posizione della finestra (in pixel)

Dimensione di partenza della finestra (in pixel)

flag che definiscono lo stile della finestra

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

26 / 33

Creazione del men File


Allinterno del costruttore di wxTestFrame. . .
c u r r e n t F i l e . Empty ( ) ;
wxMenu menuFile = new wxMenu ;

1
2
3

menuFile>Append ( Menu_File_Open , wxT ( "&Open . . . " ) ,


wxT ( " Open t e x t f i l e " ) ) ;
// ...
menuFile>AppendSeparator ( ) ;
menuFile>Append ( Menu_File_Quit , wxT ( "E& x i t " ) ,
wxT ( " E x i t t h e e d i t o r " ) ) ;

4
5
6
7
8
9
10

wxMenuBar menuBar = new wxMenuBar ;


menuBar>Append ( menuFile , wxT ( "& F i l e " ) ) ;
SetMenuBar ( menuBar ) ;

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

11
12
13

27 / 33

Creazione della status bar e della finestra di testo


Allinterno del costruttore di wxTestFrame. . .
C r e at eS ta t u s B a r ( 2 ) ;
S e t S t a t u s T e x t ( wxT ( " Welcome t o w x T e x t E d i t o r ! " ) ) ;

1
2
3

textFrame = new w x T e x t C t r l ( t h i s , 1,
wxT ( " W r i t e Here \ n " ) ,
wxDefaultPosition ,
wxDefaultSize ,
wxTE_MULTILINE | wxTE_AUTO_URL ) ;

4
5
6
7
8

La status bar pu essere divisa in pi parti.

wxTextCtrl serve sia a creare semplici caselle di input, che a


gestire editor avanzati con formattazione tipo RTF.

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

28 / 33

Apertura di un file
void wxTestFrame : : OpenFile ( wxCommandEvent& event )
{
wxFileDialog openFileDialog =
new w x F i l e D i a l o g ( t h i s ,
wxT ( " Open a t e x t f i l e " ) ,
wxT ( " / home / davide / " ) ,
wxT ( " " ) , f i l e T y p e s ,
wxOPEN, w x D e f a u l t P o s i t i o n ) ;
i f ( o p e n F i l e D i a l o g >ShowModal ( ) == wxID_OK ) {
c u r r e n t F i l e = o p e n F i l e D i a l o g >GetPath ( ) ;
textFrame >C l e a r ( ) ;
textFrame >L o a d F i l e ( c u r r e n t F i l e ) ;

1
2
3
4
5
6
7
8
9
10
11
12
13

S e t S t a t u s T e x t ( o p e n F i l e D i a l o g >GetFilename ( ) , 0 ) ;
S e t S t a t u s T e x t ( o p e n F i l e D i a l o g >G e t D i r e c t o r y ( ) , 1 ) ;
}

14
15
16

17

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

29 / 33

Salvataggio di un file
void wxTestFrame : : S a v e F i l e ( wxCommandEvent& event )
1
{
2
i f ( c u r r e n t F i l e . IsEmpty ( ) ) {
3
SaveFileAs ( event ) ;
4
} else {
5
i f ( textFrame >S a v e F i l e ( c u r r e n t F i l e ) ) {
6
SetStatusText ( c u r r e n t F i l e , 0 ) ;
7
S e t S t a t u s T e x t ( wxT ( " F i l e saved " ) , 1 ) ;
8
} else {
9
wxMessageBox (
10
wxT ( " An e r r o r occoured w h i l e s a v i n g f i l e ! " ) , 11
wxT ( " Save e r r o r " ) ,
12
wxOK | wxICON_ERROR, t h i s ) ;
13
};
14
};
15
}
16

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

30 / 33

Salvataggio di un file con nome


void wxTestFrame : : SaveFileAs ( wxCommandEvent& event )
1
{
2
wxFileDialog saveFileDialog =
3
new w x F i l e D i a l o g ( t h i s , wxT ( " Save t e x t f i l e " ) , 4
wxT ( " / home / davide / " ) , wxT ( " " ) ,
5
f i l e T y p e s , wxSAVE, w x D e f a u l t P o s i t i o n ) ;
6
i f ( s a v e F i l e D i a l o g >ShowModal ( ) == wxID_OK ) {
7
c u r r e n t F i l e = s a v e F i l e D i a l o g >G e t D i r e c t o r y ( )
8
+ wxT ( " / " )
9
+ s a v e F i l e D i a l o g >GetFilename ( ) ) ;
10
i f ( textFrame >S a v e F i l e ( c u r r e n t F i l e ) ) {
11
S e t S t a t u s T e x t ( s a v e F i l e D i a l o g >GetFilename ( ) , 0 ) ; 12
S e t S t a t u s T e x t ( wxT ( " F i l e saved " ) , 1 ) ;
13
} else {
14
wxMessageBox ( / . . . / ) ;
15
};
16
};
17
}
18
Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

31 / 33

Conclusioni

Infine...
Abbiamo realizzato un editor di
testo grafico multi-piattaforma
con 147 righe di codice C++!

Riferimenti
http://www.montellug.it
montellug@montellug.it
davide.rondini@gmail.com

Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

32 / 33

Letture consigliate per approfondire. . .


wxWidgets Home Page
http://www.wxwidgets.org/

wxWidgets supported platforms


http://www.wxwidgets.org/wiki/index.php/Supported_Platforms

wxEmbedded
http://www.koansoftware.com/it/prd_svil_wxembedded.htm

wxWidgets bindings
http://www.wxwidgets.org/wiki/index.php/General_Information

Licenze dei framework


wxWidgets: http://www.wxwidgets.org/about/newlicen.htm
Qt: http://trolltech.com/products/qt/licenses/licensing/licensingoverview
.NET: http://www.microsoft.com/resources/sharedsource/
licensingbasics/referencelicense.mspx
Davide Rondini (MontelLUG)

wxWidgets

18 ott 2007 Montebelluna

33 / 33

Vous aimerez peut-être aussi