aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2025-09-14 16:17:03 +0200
committerJan Sucan <jan@jansucan.com>2025-09-14 16:17:03 +0200
commitad4cf7db4cd61d01f1e2191d1d69edd5916d6e88 (patch)
tree9497eb2ce10df6a17679f7110aadf159a566b7cb
parentcbb801e06adc03fe5baddb3288d070e39d2cf878 (diff)
23_a_1: Add solution
-rw-r--r--README.md2
-rw-r--r--ch23/23_a_1/23_a_1.txt1
-rw-r--r--ch23/23_a_1/PodMainGUI.hs47
3 files changed, 31 insertions, 19 deletions
diff --git a/README.md b/README.md
index 1657448..db95f95 100644
--- a/README.md
+++ b/README.md
@@ -184,7 +184,7 @@ are prefixed with 'Module_'.
| **_Module_19_b_1_** | yes | 465 | |
| 19_b_2 | yes | | |
| 19_b_3 | yes, in 19_b_2 | | |
-| **_23_a_1_** | | 529 | 23. GUI programming with gtk2hs|
+| **_23_a_1_** | yes | 529 | 23. GUI programming with gtk2hs|
| 23_a_2 | | | |
| 23_a_3 | | | |
| **_24_a_1_** | | 542 | 24. Concurrent and multicore programming |
diff --git a/ch23/23_a_1/23_a_1.txt b/ch23/23_a_1/23_a_1.txt
new file mode 100644
index 0000000..7b594a0
--- /dev/null
+++ b/ch23/23_a_1/23_a_1.txt
@@ -0,0 +1 @@
+Present a helpful GUI error message if the call to xmlNew returns Nothing.
diff --git a/ch23/23_a_1/PodMainGUI.hs b/ch23/23_a_1/PodMainGUI.hs
index e557d8b..6c7190d 100644
--- a/ch23/23_a_1/PodMainGUI.hs
+++ b/ch23/23_a_1/PodMainGUI.hs
@@ -47,28 +47,39 @@ main gladepath = withSocketsDo $ handleSqlError $
timeoutAddFull (yield >> return True)
priorityDefaultIdle 100
- -- Load the GUI from the Glade file
- gui <- loadGlade gladepath
-
- -- Connect to the database
- dbh <- connect "pod.db"
-
- -- Set up our events
- connectGui gui dbh
-
- -- Run the GTK+ main loop; exits after GUI is done
- mainGUI
+ -- Load XML from glade path
+ xmlGlade <- xmlNew gladepath
+ case xmlGlade of
+ Nothing -> do
+ let errorMsg = "Cannot load glade file " ++ gladepath
+ d <- messageDialogNew Nothing -- No parent window
+ [] -- No dialog flags
+ MessageError
+ ButtonsClose
+ errorMsg
+ windowSetTitle d "Error"
+ dialogRun d
+ return ()
+ Just xml -> do
+ -- Load the GUI from the Glade file
+ gui <- loadGlade xml
+
+ -- Connect to the database
+ dbh <- connect "pod.db"
+
+ -- Set up our events
+ connectGui gui dbh
+
+ -- Run the GTK+ main loop; exits after GUI is done
+ mainGUI
- -- Disconnect from the database at the end
- disconnect dbh
+ -- Disconnect from the database at the end
+ disconnect dbh
{-- /snippet main --}
{-- snippet loadGlade --}
-loadGlade gladepath =
- do -- Load XML from glade path.
- -- Note: crashes with a runtime error on console if fails!
- Just xml <- xmlNew gladepath
-
+loadGlade xml =
+ do
-- Load main window
mw <- xmlGetWidget xml castToWindow "mainWindow"