summaryrefslogtreecommitdiffstats
path: root/korundum/rubylib/examples/kludgeror.rb
blob: 156b2c53f696e979bebf58d03b8a026deccf7339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env ruby
# A quick and dirty web browser demonstrating the direct instantiation 
# of a TDEHTML part. Needless to say: this is NOT a programming paragon :)
# -- gg.
# AK - ported to ruby

require 'Korundum'

opt =     [ [ "+[url]",      "An URL to open at startup.",         ""            ],
            [ "z",           "A dummy binary option.",             ""            ],
            [ "baz <file>",  "A long option with arg.",            ""            ],
            [ "boz <file>",  "Same as above with default value",   "default.txt" ],
          ]            

#TQt::Internal::setDebug TQt::QtDebugChannel::TQTDB_ALL
# Qt.debug_level = TQt::DebugLevel::High

about = KDE::AboutData.new("kludgeror", "Kludgeror", "0.1", "A basic web browser")
KDE::CmdLineArgs::init(ARGV, about)
KDE::CmdLineArgs::addCmdLineOptions opt
args = KDE::CmdLineArgs::parsedArgs

a = KDE::Application.new # BUG, application shouldn't be needed at the top, lets fix this...

class PartHolder < TQt::Object
    signals "setLocBarText(const TQString&)"
    slots "reload()", "goToURL(const TQString&)", "back()", "openURL(const KURL&)" # BUG - the slots should be normalize wrt spaces by the lib

    attr_accessor :part, :history

    def initialize part, *k
        super(*k)
        @part = part 
        @history = []
    end

    def openURL url
        @part.openURL url
        # BUG - a non existant slot emit says horrible things, nothing interesting for user.. very confusing
        # BUG - signal emitting is *very* wrong
        # emit setLocBarText(url.url)
        @history.unshift url unless url == @history[0]
    end

    def reload
        @part.openURL @part.url
    end

    def goToURL(url)
        url = "http://#{url}" unless url =~ /^\w*:/
        openURL KDE::URL.new(url)
    end

    def back
        return unless @history.length > 1
        @history.shift
        openURL @history[0] unless @history.empty?
    end
end

LOC_ED  = 322
ERASE_B = 323
BACK_B  = 324

url = (args.count > 1) ? args.url(0) : KDE::URL.new("http://loki:8080/xml/index.xml")

puts "Dummy z option activated." if args.isSet "z"
puts "Dummy baz option has value: #{args.getOption "baz"}" if args.isSet "baz"
# puts "Dummy boz option has value: #{args.getOption "boz"}" if args.isSet "boz" # B0rked?

toplevel = KDE::MainWindow.new
doc = KDE::HTMLPart.new toplevel, nil, toplevel, nil, 1
# doc = KDE::HTMLPart.new toplevel, nil, toplevel, nil, 1 # KDE::HTMLPart::BrowserViewGUI
ph = PartHolder.new doc

TQt::Object::connect doc.browserExtension, SIGNAL("openURLRequest(const KURL&, const KParts::URLArgs&)"),
                    ph, SLOT("openURL(const KURL&)") # BUG this slot must be screwing up wrt marshalling?

ph.openURL url
toplevel.setCentralWidget doc.widget
toplevel.resize 700, 500

begin
  d, viewMenu, fileMenu, locBar, e = nil
  d = doc.domDocument
    viewMenu = d.documentElement.firstChild.childNodes.item(2).toElement
        e = d.createElement "action"
        e.setAttribute "name", "debugRenderTree"
    viewMenu.appendChild e
        e = d.createElement "action"
        e.setAttribute "name", "debugDOMTree"
    viewMenu.appendChild e
    fileMenu = d.documentElement.firstChild.firstChild.toElement
    fileMenu.appendChild d.createElement("separator")
        e = d.createElement "action"
        e.setAttribute "name", "exit"
    fileMenu.appendChild e
    locBar = d.createElement "toolbar"
    locBar.setAttribute "name", "locationBar"
        e = d.createElement "action"
        e.setAttribute "name", "reload"
    locBar.appendChild e
  d.documentElement.appendChild locBar
end

a1 = KDE::Action.new( "Reload", "reload", KDE::Shortcut.new(TQt::Key_F5), ph, SLOT("reload()"),  doc.actionCollection, "reload" )
a2 = KDE::Action.new( "Exit",   "system-log-out",   KDE::Shortcut.new(0),          a,  SLOT("quit()"),    doc.actionCollection, "exit" )

toplevel.guiFactory.addClient doc

locBar = toplevel.toolBar("locationBar");
locBar.insertButton "back", BACK_B, SIGNAL("clicked()"), 
                    ph, SLOT("back()"), true, "Go back"
locBar.insertLined url.url, LOC_ED, SIGNAL("returnPressed(const TQString&)"), ph, SLOT("goToURL(const TQString&)"), true, "Location"
locBar.insertButton "locationbar_erase", ERASE_B, SIGNAL("clicked()"), 
                    locBar.getLined(LOC_ED), SLOT("clear()"), true, "Erase the location bar's content", 2
locBar.setItemAutoSized LOC_ED, true
locBar.getLined(LOC_ED).createPopupMenu
comp = locBar.getLined(LOC_ED).completionObject
comp.setCompletionMode KDE::GlobalSettings::CompletionPopupAuto

TQt::Object::connect(locBar.getLined(LOC_ED), SIGNAL("returnPressed(const TQString&)"),
                    comp, SLOT("addItem(const TQString&)"))
TQt::Object::connect(ph, SIGNAL("setLocBarText(const TQString&)"), # BUG - once again...
                    locBar.getLined(LOC_ED), SLOT("setText(const TQString&)"))

doc.setJScriptEnabled true
doc.setJavaEnabled true
doc.setPluginsEnabled true
doc.setURLCursor TQt::Cursor.new(TQt::PointingHandCursor)

a.setTopWidget doc.widget

TQt::Object::connect doc, SIGNAL("setWindowCaption(const TQString&)"),
                    doc.widget.topLevelWidget, SLOT("setCaption(const TQString&)")
toplevel.show

a.exec