| 1 |
-- Use GtkSocket and GtkPlug for cross-process embedded. |
| 2 |
-- Just startup program, press 'm' to create tab with new button. |
| 3 |
-- Click button for hang to simulate plug hanging process, |
| 4 |
-- but socket process still running, can switch to other tab. |
| 5 |
|
| 6 |
module Main where |
| 7 |
|
| 8 |
import System.Process |
| 9 |
import System.Environment |
| 10 |
import System.Directory |
| 11 |
import System.FilePath ((</>)) |
| 12 |
import Control.Monad |
| 13 |
import Control.Monad.Trans |
| 14 |
import Control.Concurrent |
| 15 |
import Data.Text (unpack) |
| 16 |
|
| 17 |
import Graphics.UI.Gtk |
| 18 |
import Graphics.UI.Gtk.Gdk.DrawWindow |
| 19 |
import Graphics.UI.Gtk.Gdk.EventM |
| 20 |
|
| 21 |
-- | Main. |
| 22 |
main :: IO () |
| 23 |
main = do |
| 24 |
-- Get program arguments. |
| 25 |
args <- getArgs |
| 26 |
|
| 27 |
case args of |
| 28 |
-- Entry plug main when have two arguments. |
| 29 |
[id] -> plugMain (toNativeWindowId $ read id :: NativeWindowId) -- get GtkSocket id |
| 30 |
-- Othersise entry socket main when no arguments. |
| 31 |
_ -> socketMain |
| 32 |
|
| 33 |
-- | GtkSocekt main. |
| 34 |
socketMain :: IO () |
| 35 |
socketMain = do |
| 36 |
initGUI |
| 37 |
|
| 38 |
-- Create top-level window. |
| 39 |
window <- windowNew |
| 40 |
windowSetPosition window WinPosCenter |
| 41 |
windowSetDefaultSize window 600 400 |
| 42 |
set window [windowTitle := "Press `m` to new tab, press `q` exit."] |
| 43 |
on window objectDestroy mainQuit |
| 44 |
|
| 45 |
-- Create notebook to contain GtkSocekt. |
| 46 |
notebook <- notebookNew |
| 47 |
window `containerAdd` notebook |
| 48 |
|
| 49 |
-- Handle key press. |
| 50 |
window `on` keyPressEvent $ tryEvent $ do |
| 51 |
keyName <- eventKeyName |
| 52 |
liftIO $ |
| 53 |
case unpack keyName of |
| 54 |
"m" -> do |
| 55 |
-- Create new GtkSocket. |
| 56 |
socket <- socketNew |
| 57 |
widgetShow socket -- must show before add GtkSocekt to container |
| 58 |
notebookAppendPage notebook socket "Tab" -- add to GtkSocekt notebook |
| 59 |
id <- socketGetId socket -- get GtkSocket id |
| 60 |
|
| 61 |
-- Fork process to add GtkPlug into GtkSocekt. |
| 62 |
path <- liftM2 (</>) getCurrentDirectory getProgName -- get program full path |
| 63 |
runCommand $ path ++ " " ++ (show $ fromNativeWindowId id) -- don't use `forkProcess` |
| 64 |
return () |
| 65 |
"q" -> mainQuit -- quit |
| 66 |
|
| 67 |
widgetShowAll window |
| 68 |
|
| 69 |
mainGUI |
| 70 |
|
| 71 |
-- | GtkPlug main. |
| 72 |
plugMain :: NativeWindowId -> IO () |
| 73 |
plugMain id = do |
| 74 |
initGUI |
| 75 |
|
| 76 |
plug <- plugNew $ Just id |
| 77 |
on plug objectDestroy $ mainQuit |
| 78 |
|
| 79 |
button <- buttonNewWithLabel "Click me to hang." |
| 80 |
plug `containerAdd` button |
| 81 |
|
| 82 |
-- Simulate a plugin hanging to see if it blocks the outer process. |
| 83 |
on button buttonActivated $ threadDelay 5000000 |
| 84 |
|
| 85 |
widgetShowAll plug |
| 86 |
|
| 87 |
mainGUI |