The absolute beginners guide - Part IV - for gnu-smalltalk/gtk (standard dialogs)
Hello,
I took a look into the standard dialogs and the first I tried out was the AboutDialog. It's pretty easy to handle, all you have to do is to instantiate GTK.GtkAboutDialog and set several values for displaying, call run and destroy and that's all.
Let's enhance our about-method with an example:
about [
| aboutDialog |
aboutDialog := GTK.GtkAboutDialog new.
aboutDialog setVersion: '0.0.1'.
aboutDialog setLicense: 'See the license under www.gnu.org'.
aboutDialog setWebsite: 'http://smalltalk.gnu.org'.
aboutDialog setComments: 'This is my first GTK+ Smalltalk Application'.
aboutDialog setProgramName: 'My First ST-App.'.
aboutDialog run; destroy
]
The same could - of course - reached with the following code, which is a bit less to type and a bit better to view:
about [
(GTK.GtkAboutDialog new)
setVersion: '0.0.1';
setLicense: 'See the license under www.gnu.org';
setWebsite: 'http://smalltalk.gnu.org';
setComments: 'This is my first GTK+ Smalltalk Application';
setProgramName: 'My First ST-App.';
run; destroy
]
| Please consider, that: |
|---|
That was the AboutDialog.
Afterward, I took a look into MessageDialogs.
GTK offers 4 Types (as far as I know) of MessageDialogs, the InformationDialog, the WarningDialog, the QuestionDialog and the ErrorDialog.
All these are instantiated from the same class. This class is the GTK.GtkMessageDialog and differ only in the parameter type. This type could have one of the following value:
- GTK.Gtk gtkMessageInfo
- GTK.Gtk gtkMessageWarning
- GTK.Gtk gtkMessageQuestion
- GTK.Gtk gtkMessageError
I wanted to see all those different dialogs in action, so I enhanced my menu-creation of the file menu with 5 new entries, 4 for each of the message types and one additonal separator. Luckily, I decided to use the more sophisticated menu creation method from the last blog. So my createFileMenu method looked after the change like this:
createFileMenu [
^self myCreateMenuEntry: {
#('Open...' #openFile).
#('Message...' #showMessage).
#().
#('Info...' #showInfo).
#('Warning...' #showWarning).
#('Question...' #showQuestion).
#('Error...' #showError).
#().
#('Exit' #exit)
}
]
To start simple, I filled the showMessage method with the code for creating an InformationMessageDialog:
showMessage [
| dialog |
dialog := GTK.GtkMessageDialog
new: window
flags: GTK.Gtk gtkDialogDestroyWithParent
type: GTK.Gtk gtkMessageInfo
buttons: GTK.Gtk gtkButtonsClose
message: 'You pressed the showMessage Menu-Entry'.
dialog setTitle: 'Information!'.
dialog run; destroy
]
And here you see, that the new-method has a lot more parameter than in the example with the about-box. But eventually, that makes no difference to the functionality.
But I wanted to see all the different MessageDialogs. And to not duplicate the whole MessageDialog code ever and ever (ok - only 4 times) again, I created for joke a switch-class...:
Object subclass: Switch [
| var defEx |
Switch class >> new: anotherValue [
^(super new) setValue: anotherValue
]
Switch class >> value: anotherValue [
^(super new) setValue: anotherValue
]
setValue: anotherValue [
var := anotherValue.
defEx := true
]
case: arg do: aBlock [
(var = arg) ifTrue: [defEx := false. aBlock value]
]
default: aBlock [
(defEx) ifTrue: [aBlock value]
]
]
You'll see their use in the comming source.
I only want to have one line of code in each of the callback-methods from the menu and an additional method, which display the different message-dialogs.
Remember, that the type-value in creating a MessageDialog is the only important difference. So this should be the value which I should provide to the MessageDialog-creation-method.
Here's the code for the 4 callback-methods:
showInfo [
self displayMessageDialog: (GTK.Gtk gtkMessageInfo)
]
showWarning [
self displayMessageDialog: (GTK.Gtk gtkMessageWarning)
]
showQuestion [
self displayMessageDialog: (GTK.Gtk gtkMessageQuestion)
]
showError [
self displayMessageDialog: (GTK.Gtk gtkMessageError)
]
And now let's have a look into my displayMessageDialog method. The parameter is the type of the MessageDialog and on the basis of that type, I set the header-line of the MessageDialog and the text which should be displayed with the help of my Switch object. (Of course, you could also achieve the same result with ifTrue's, but that was only a bit of joy...
displayMessageDialog: type [
| title text |
(Switch value: type)
case: (GTK.Gtk gtkMessageWarning)
do: [ title := 'Warning'.
text := 'Be carefull, this is a warning!' ];
case: (GTK.Gtk gtkMessageQuestion)
do: [ title := 'Question'.
text := 'Do you need something?' ];
case: (GTK.Gtk gtkMessageError)
do: [ title := 'Error'.
text := 'This is a serious error' ];
default: [ title := 'Information'.
text := This is only an information...'].
(GTK.GtkMessageDialog
new: window
flags: GTK.Gtk gtkDialogDestroyWithParent
type: type
buttons: GTK.Gtk gtkButtonsClose
message: text)
setTitle: title;
run; destroy
]
That's again all. (Now I'll need some time to prepare the next steps, so please don't expect the next blog-entry tomorrow...)
Hope it helps!
Happy coding,
Joachim.
