Package nltk :: Package wordnet :: Package browser :: Module wxbrowse
[hide private]
[frames] | no frames]

Source Code for Module nltk.wordnet.browser.wxbrowse

  1  #!/usr/bin/python 
  2  # 
  3  # Wordnet Interface: Graphical Wordnet Browser 
  4  #  
  5  # Copyright (C) 2007 - 2008 NLTK Project 
  6  # Author: Jussi Salmela <[email protected]> 
  7  #         Paul Bone <[email protected]> 
  8  # URL: <http://nltk.org> 
  9  # For license information, see LICENSE.TXT 
 10   
 11  """ 
 12  Wordnet Interface: Graphical Wordnet Browser 
 13  """ 
 14   
 15  import  os 
 16  import  os.path 
 17  import  sys 
 18  from time import sleep 
 19  import pickle 
 20  import platform 
 21  from urllib import quote_plus, unquote_plus 
 22  from itertools import groupby 
 23   
 24  import  wx 
 25  import  wx.html as  html 
 26  import  wx.lib.wxpTag 
 27   
 28  import util 
 29   
 30  __all__ = ['demo'] 
 31   
 32   
 33  # 
 34  # Comments within this code indicate future work.  These comments are 
 35  # marked with 'XXX' 
 36  # 
 37   
 38   
 39  # XXX: write these global constants in shouting case. 
 40  # 
 41  # Additionally, through-out this module docstrings should be entered. 
 42  # 
 43   
 44   
 45  frame_title = 'NLTK Wordnet Browser' 
 46  help_about = frame_title + __doc__ 
 47   
 48  # This is used to save options in and to be pickled at exit 
 49  options_dict = {} 
 50   
 51  try: 
 52      nltk_prefs_dir = os.path.join(os.environ['HOME'], ".nltk") 
 53  except KeyError: 
 54      # XXX: This fallback may have problems on windows. 
 55      nltk_prefs_dir = os.path.curdir 
 56  pickle_file_name = os.path.join(nltk_prefs_dir,  
 57                                  (frame_title + os.path.extsep + 'pkl')) 
 58   
 59  ### XXX: MUST find a standard location for the stats HTML page, 
 60  ###      perhapsw in NLTK_DATA. 
 61   
 62  WORDNET_DB_INFO_FILEPATH = 'NLTK Wordnet Browser Database Info.html'  
 63   
 64   
 65   
66 -class MyHtmlWindow(html.HtmlWindow):
67 - def __init__(self, parent, id):
68 html.HtmlWindow.__init__(self, parent, id, 69 style=wx.NO_FULL_REPAINT_ON_RESIZE) 70 self.parent = parent 71 self.font_size = self.normal_font_size = \ 72 options_dict['font_size'] 73 self.incr_decr_font_size(0) # Keep it as it is
74
75 - def OnLinkClicked(self, linkinfo):
76 href = linkinfo.GetHref() 77 tab_to_return_to = None 78 word = self.parent.parent.search_word.GetValue() 79 if linkinfo.Event.ControlDown(): 80 if linkinfo.Event.ShiftDown(): 81 self.parent.add_html_page(activate=True) 82 else: 83 tab_to_return_to = self.parent.current_page 84 self.parent.add_html_page(activate=True) 85 self.parent.SetPageText(self.parent.current_page, word) 86 self.parent.parent.search_word.SetValue(word) 87 page,word = util.page_word(self.GetParser().GetSource(), word, href) 88 if page: 89 if word: 90 self.parent.SetPageText(self.parent.current_page, word) 91 self.parent.parent.show_page_and_word(page, word) 92 else: 93 self.show_msg('The word was not found!') 94 self.parent.parent.show_page_and_word(page) 95 else: 96 self.show_msg('Relation "%s" is not implemented yet!' % rel_name) 97 98 # XXX: MAY simplfy the if predicate. */ 99 if tab_to_return_to is not None: 100 self.parent.switch_html_page(tab_to_return_to)
101
102 - def OnSetTitle(self, title):
103 "no-op" 104 pass
105
106 - def OnCellMouseHover(self, cell, x, y):
107 "no-op" 108 pass
109
110 - def OnOpeningURL(self, type, url):
111 "no-op" 112 pass
113
114 - def OnCellClicked(self, cell, x, y, evt):
115 linkinfo = cell.GetLink() 116 if linkinfo is not None: 117 pass 118 else: 119 pass 120 super(MyHtmlWindow, self).OnCellClicked(cell, x, y, evt)
121
122 - def incr_decr_font_size(self, change=None):
123 global options_dict 124 page_to_restore = self.GetParser().GetSource() 125 if change: 126 self.font_size += change 127 if self.font_size <= 0: self.font_size = 1 128 else: 129 self.font_size = self.normal_font_size 130 options_dict['font_size'] = self.font_size 131 self.SetStandardFonts(size=self.font_size) 132 self.SetPage(page_to_restore)
133
134 - def show_msg(self, msg):
135 msg1 = '*'*8 + ' ' + msg + ' ' + '*'*8 136 msg2 = '*'*100 137 for i in range(5): 138 for msg in [msg1, msg2]: 139 self.parent.parent.statusbar.SetStatusText(msg) 140 wx.Yield() 141 sleep(0.2) 142 self.parent.parent.statusbar.SetStatusText(' ') 143 wx.Yield()
144
145 - def show_page(self, page):
146 self.SetPage(page)
147 148 149 #----------------------------------------------------------------------------
150 -class NB(wx.Notebook):
151 - def __init__(self, parent):
152 wx.Notebook.__init__(self, parent, -1, size=(21,21), 153 style=wx.BK_DEFAULT) 154 155 self.parent = parent 156 self.add_html_page() 157 self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged) 158 self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.OnPageChanging)
159
160 - def OnPageChanged(self, event):
161 old = event.GetOldSelection() 162 new = event.GetSelection() 163 sel = self.GetSelection() 164 if old != new: # and self.current_page != new: 165 self.switch_html_page(new) 166 event.Skip()
167 168 # XXX: MAY remove three statments with no effect. */
169 - def OnPageChanging(self, event):
170 old = event.GetOldSelection() 171 new = event.GetSelection() 172 sel = self.GetSelection() 173 event.Skip()
174
175 - def switch_html_page(self, new_page):
176 self.current_page = new_page 177 self.ChangeSelection(new_page) 178 self.h_w = self.GetPage(new_page) 179 self.parent.prev_btn.Enable(self.h_w.prev_wp_list != []) 180 self.parent.next_btn.Enable(self.h_w.next_wp_list != []) 181 self.parent.search_word.SetValue(self.h_w.current_word)
182
183 - def add_html_page(self, tab_text='', activate=True):
184 h_w = MyHtmlWindow(self, -1) 185 if 'gtk2' in wx.PlatformInfo: 186 h_w.SetStandardFonts() 187 h_w.SetRelatedFrame(self.parent.frame, 188 self.parent.titleBase + ' -- %s') 189 h_w.SetRelatedStatusBar(0) 190 h_w.current_word = '' 191 # Init the word-page list for history browsing 192 h_w.prev_wp_list = [] 193 h_w.next_wp_list = [] 194 self.AddPage(h_w, tab_text, activate) 195 if activate: 196 self.current_page = self.GetSelection() 197 self.h_w = h_w 198 if self.h_w.prev_wp_list == []: 199 self.parent.prev_btn.Enable(False) 200 if self.h_w.next_wp_list == []: 201 self.parent.next_btn.Enable(False) 202 return self.current_page
203 204
205 -class HtmlPanel(wx.Panel):
206 - def __init__(self, frame):
207 wx.Panel.__init__(self, frame, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE) 208 self.frame = frame 209 # XXX: MAY simplify setting self.cwdJust use os.getcwd(). */ 210 self.cwd = os.path.split(sys.argv[0])[0] 211 212 if not self.cwd: 213 self.cwd = os.getcwd() 214 if frame: 215 self.titleBase = frame.GetTitle() 216 217 self.statusbar = self.frame.CreateStatusBar() 218 219 self.printer = html.HtmlEasyPrinting(frame_title) 220 221 self.box = wx.BoxSizer(wx.VERTICAL) 222 223 subbox_1 = wx.BoxSizer(wx.HORIZONTAL) 224 225 self.prev_btn = wx.Button(self, -1, 'Previous Page') 226 self.Bind(wx.EVT_BUTTON, self.on_prev_page, self.prev_btn) 227 subbox_1.Add(self.prev_btn, 5, wx.GROW | wx.ALL, 2) 228 229 self.next_btn = wx.Button(self, -1, 'Next Page') 230 self.Bind(wx.EVT_BUTTON, self.on_next_page, self.next_btn) 231 subbox_1.Add(self.next_btn, 5, wx.GROW | wx.ALL, 2) 232 233 btn = wx.Button(self, -1, 'Help') 234 self.Bind(wx.EVT_BUTTON, self.on_help, btn) 235 subbox_1.Add(btn, 5, wx.GROW | wx.ALL, 2) 236 237 btn = wx.Button(self, -1, 'Search the word(s)') 238 self.Bind(wx.EVT_BUTTON, self.on_word_enter, btn) 239 subbox_1.Add(btn, 5, wx.GROW | wx.ALL, 2) 240 241 lbl = wx.StaticText(self, -1, 'Word(s): ', 242 style=wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM) 243 subbox_1.Add(lbl, 5, wx.GROW | wx.ALL, 2) 244 245 self.search_word = wx.TextCtrl(self, -1, '', style=wx.TE_PROCESS_ENTER) 246 self.Bind(wx.EVT_TEXT_ENTER, self.on_word_enter, self.search_word) 247 self.search_word.Bind(wx.EVT_LEFT_UP, self.on_mouse_up) 248 subbox_1.Add(self.search_word, 20, wx.GROW | wx.ALL, 2) 249 250 self.box.Add(subbox_1, 0, wx.GROW) 251 self.nb = NB(self) 252 self.box.Add(self.nb, 1, wx.GROW) 253 254 self.SetSizer(self.box) 255 self.SetAutoLayout(True)
256
257 - def on_key_down(self, event):
258 event.Skip()
259
260 - def on_key_up(self, event):
261 event.Skip()
262
263 - def on_char(self, event):
264 event.Skip()
265
266 - def on_mouse_up(self, event):
267 self.search_word.SetSelection(-1, -1) 268 event.Skip()
269
270 - def on_prev_page(self, event):
271 if self.nb.h_w.prev_wp_list: 272 # Save current word&page&viewStart 273 page = self.nb.h_w.GetParser().GetSource() 274 x,y = self.nb.h_w.GetViewStart() 275 entry = (self.nb.h_w.current_word,page,(x,y)) 276 self.nb.h_w.next_wp_list = [entry] + self.nb.h_w.next_wp_list 277 self.next_btn.Enable(True) 278 # Restore previous word&page 279 word,page,(x,y) = self.nb.h_w.prev_wp_list[-1] 280 self.nb.h_w.prev_wp_list = self.nb.h_w.prev_wp_list[:-1] 281 if self.nb.h_w.prev_wp_list == []: 282 self.prev_btn.Enable(False) 283 self.nb.h_w.current_word = word 284 self.nb.SetPageText(self.nb.current_page, word) 285 self.search_word.SetValue(word) 286 self.nb.h_w.SetPage(page) 287 self.nb.h_w.Scroll(x, y)
288
289 - def on_next_page(self, event):
290 if self.nb.h_w.next_wp_list: 291 # Save current word&page&viewStart 292 page = self.nb.h_w.GetParser().GetSource() 293 x,y = self.nb.h_w.GetViewStart() 294 entry = (self.nb.h_w.current_word,page,(x,y)) 295 self.nb.h_w.prev_wp_list.append(entry) 296 self.prev_btn.Enable(True) 297 # Restore next word&page 298 word,page,(x,y) = self.nb.h_w.next_wp_list[0] 299 self.nb.h_w.next_wp_list = self.nb.h_w.next_wp_list[1:] 300 if self.nb.h_w.next_wp_list == []: 301 self.next_btn.Enable(False) 302 self.nb.h_w.current_word = word 303 self.nb.SetPageText(self.nb.current_page, word) 304 self.search_word.SetValue(word) 305 self.nb.h_w.SetPage(page) 306 self.nb.h_w.Scroll(x, y)
307
308 - def on_help(self, event):
309 self.frame.on_help_help(None)
310
311 - def on_word_change(self, event):
312 word = self.search_word.GetValue() 313 if word.isalnum(): return 314 word_2 = ''.join([x for x in word if 315 x.isalnum() or x == ' ' or x == '-']) 316 self.search_word.SetValue(word_2) 317 event.Skip()
318
319 - def on_word_enter(self, event):
320 if not self.nb.GetPageCount(): 321 self.frame.on_ssw_nt(None) 322 return 323 word = self.search_word.GetValue() 324 word = word.strip() 325 if word == '': return 326 word,body = util.new_word_and_body(word) 327 if word: 328 self.show_page_and_word(util.pg(word, body), word) 329 self.nb.h_w.current_word = word 330 self.nb.SetPageText(self.nb.current_page, word) 331 else: 332 self.nb.h_w.show_msg('The word was not found!')
333
334 - def show_page_and_word(self, page, word=None):
335 if self.nb.h_w.current_word: 336 # Save current word&page&viewStart 337 curr_page = self.nb.h_w.GetParser().GetSource() 338 x,y = self.nb.h_w.GetViewStart() 339 entry = (self.nb.h_w.current_word,curr_page,(x,y)) 340 self.nb.h_w.prev_wp_list.append(entry) 341 self.prev_btn.Enable(True) 342 # Clear forward history 343 self.nb.h_w.next_wp_list = [] 344 self.next_btn.Enable(False) 345 if not word: x,y = self.nb.h_w.GetViewStart() 346 self.nb.h_w.SetPage(page) 347 if not word: self.nb.h_w.Scroll(x, y) 348 if word: 349 self.search_word.SetValue(word) 350 self.nb.h_w.current_word = word
351 352
353 -class MyHtmlFrame(wx.Frame):
354 - def __init__(self, parent, title): #, pos, size)
355 wx.Frame.__init__(self, parent, -1, title)#, pos, size) 356 357 menu_bar = wx.MenuBar() 358 359 menu_1 = wx.Menu() 360 o_f = menu_1.Append(-1, 'Open File...\tCtrl+O') 361 s_a = menu_1.Append(-1, 'Save Page As...\tCtrl+S') 362 menu_1.AppendSeparator() 363 print_ = menu_1.Append(-1, 'Print...\tCtrl+P') 364 preview = menu_1.Append(-1, 'Preview') 365 menu_1.AppendSeparator() 366 ex = menu_1.Append(-1, 'Exit') 367 menu_bar.Append(menu_1, '&File') 368 369 menu_1_2 = wx.Menu() 370 nt = menu_1_2.Append(-1, 'New tabsheet\tCtrl+T') 371 ct = menu_1_2.Append(-1, 'Close tabsheet\tCtrl+W') 372 menu_1_2.AppendSeparator() 373 ssw_nt = menu_1_2.Append(-1, 374 'Show search word in new tabsheet\tAlt+Enter') 375 menu_bar.Append(menu_1_2, '&Tabsheets') 376 377 menu_2 = wx.Menu() 378 prev_p = menu_2.Append(-1, 'Previous\tCtrl+Left Arrow') 379 next_p = menu_2.Append(-1, 'Next\tCtrl+Right Arrow') 380 menu_bar.Append(menu_2, '&Page History') 381 382 menu_3 = wx.Menu() 383 i_f = menu_3.Append(-1, 384 'Increase Font Size\tCtrl++ or Ctrl+Numpad+ or Ctrl+UpArrow') 385 d_f = menu_3.Append(-1, 386 'Decrease Font Size\tCtrl+- or Ctrl+Numpad- or Ctrl+DownArrow') 387 n_f = menu_3.Append(-1, 'Normal Font Size\tCtrl+0') 388 menu_3.AppendSeparator() 389 # The Database Info File is not supported in this version. 390 # db_i = menu_3.Append(-1, 'Show Database Info') 391 # menu_3.AppendSeparator() 392 s_s = menu_3.Append(-1, 'Show HTML Source\tCtrl+U') 393 menu_bar.Append(menu_3, '&View') 394 395 menu_4 = wx.Menu() 396 h_h = menu_4.Append(-1, 'Help') 397 h_a = menu_4.Append(-1, 'About...') 398 menu_bar.Append(menu_4, '&Help') 399 400 self.SetMenuBar(menu_bar) 401 402 self.Bind(wx.EVT_MENU, self.on_open_file, o_f) 403 self.Bind(wx.EVT_MENU, self.on_save_as, s_a) 404 self.Bind(wx.EVT_MENU, self.on_print, print_) 405 self.Bind(wx.EVT_MENU, self.on_preview, preview) 406 self.Bind(wx.EVT_MENU, self.on_exit, ex) 407 self.Bind(wx.EVT_MENU, self.on_new_tab, nt) 408 self.Bind(wx.EVT_MENU, self.on_close_tab, ct) 409 self.Bind(wx.EVT_MENU, self.on_ssw_nt, ssw_nt) 410 self.Bind(wx.EVT_MENU, self.on_prev_page, prev_p) 411 self.Bind(wx.EVT_MENU, self.on_next_page, next_p) 412 self.Bind(wx.EVT_MENU, self.on_incr_font, i_f) 413 self.Bind(wx.EVT_MENU, self.on_decr_font, d_f) 414 self.Bind(wx.EVT_MENU, self.on_norm_font, n_f) 415 # The Database Info File is not supported in this version. 416 # self.Bind(wx.EVT_MENU, self.on_db_info, db_i) 417 self.Bind(wx.EVT_MENU, self.on_show_source, s_s) 418 self.Bind(wx.EVT_MENU, self.on_help_help, h_h) 419 self.Bind(wx.EVT_MENU, self.on_help_about, h_a) 420 421 acceltbl = wx.AcceleratorTable([ 422 (wx.ACCEL_CTRL,ord('O'),o_f.GetId()), 423 (wx.ACCEL_CTRL,ord('S'),s_a.GetId()), 424 (wx.ACCEL_CTRL,ord('P'),print_.GetId()), 425 (wx.ACCEL_CTRL,wx.WXK_ADD,i_f.GetId()), 426 (wx.ACCEL_CTRL,wx.WXK_NUMPAD_ADD,i_f.GetId()), 427 (wx.ACCEL_CTRL,wx.WXK_UP,i_f.GetId()), 428 (wx.ACCEL_CTRL,wx.WXK_SUBTRACT,d_f.GetId()), 429 (wx.ACCEL_CTRL,wx.WXK_NUMPAD_SUBTRACT,d_f.GetId()), 430 (wx.ACCEL_CTRL,wx.WXK_DOWN,d_f.GetId()), 431 (wx.ACCEL_CTRL,ord('0'),n_f.GetId()), 432 (wx.ACCEL_CTRL,ord('U'),s_s.GetId()), 433 (wx.ACCEL_ALT,wx.WXK_LEFT,prev_p.GetId()), 434 (wx.ACCEL_CTRL,wx.WXK_LEFT,prev_p.GetId()), 435 (wx.ACCEL_ALT,wx.WXK_RIGHT,next_p.GetId()), 436 (wx.ACCEL_CTRL,wx.WXK_RIGHT,next_p.GetId()), 437 (wx.ACCEL_ALT,wx.WXK_RETURN,ssw_nt.GetId()), 438 ]) 439 440 self.SetAcceleratorTable(acceltbl) 441 442 self.Bind(wx.EVT_SIZE, self.on_frame_resize) 443 self.Bind(wx.EVT_MOVE, self.on_frame_move) 444 self.Bind(wx.EVT_CLOSE, self.on_frame_close) 445 446 self.panel = HtmlPanel(self)
447 448
449 - def on_key_down(self, event):
450 event.Skip()
451
452 - def on_key_up(self, event):
453 event.Skip()
454
455 - def on_frame_close(self, event):
456 pos = self.GetPosition() 457 size = self.GetSize() 458 if pos == (-32000, -32000): # The frame is minimized, ignore pos 459 pos = (0,0) 460 options_dict['frame_pos'] = pos 461 options_dict['frame_size'] = size 462 if not os.access(nltk_prefs_dir, os.F_OK): 463 os.mkdir(nltk_prefs_dir) 464 pkl = open(pickle_file_name, 'wb') 465 pickle.dump(options_dict, pkl, -1) 466 pkl.close() 467 event.Skip()
468
469 - def on_frame_resize(self, event):
470 event.Skip()
471
472 - def on_frame_move(self, event):
473 event.Skip()
474
475 - def on_open_file(self, event):
476 self.load_file()
477
478 - def on_open_URL(self, event):
479 self.load_url()
480
481 - def on_save_as(self, event):
482 self.save_file()
483
484 - def on_print(self, event):
485 self.print_()
486
487 - def on_preview(self, event):
488 self.preview()
489
490 - def on_exit(self, event):
491 self.Close()
492
493 - def on_new_tab(self, event):
494 current_page = self.panel.nb.add_html_page()
495
496 - def on_close_tab(self, event):
497 self.panel.nb.DeletePage(self.panel.nb.current_page)
498
499 - def on_ol_ut(self, event):
500 pass
501
502 - def on_ol_ft(self, event):
503 pass
504
505 - def on_ssw_nt(self, event):
506 word = self.panel.search_word.GetValue() 507 if word == '': return 508 current_page = self.panel.nb.add_html_page() 509 word,body = util.new_word_and_body(word) 510 if word: 511 self.panel.show_page_and_word(util.pg(word, body), word) 512 self.panel.nb.h_w.current_word = word 513 self.panel.nb.SetPageText(current_page, word) 514 else: 515 self.panel.nb.h_w.show_msg('The word was not found!')
516
517 - def on_prev_page(self, event):
518 self.panel.on_prev_page(event)
519
520 - def on_next_page(self, event):
521 self.panel.on_next_page(event)
522
523 - def on_incr_font(self, event):
524 self.panel.nb.h_w.incr_decr_font_size(+1)
525
526 - def on_decr_font(self, event):
527 self.panel.nb.h_w.incr_decr_font_size(-1)
528
529 - def on_norm_font(self, event):
530 self.panel.nb.h_w.incr_decr_font_size()
531
532 - def on_db_info(self, event):
533 self.show_db_info()
534
535 - def on_show_source(self, event):
536 self.show_source()
537
538 - def on_help_help(self, event):
539 self.read_file('wx_help.html')
540
541 - def on_help_about(self, event):
542 wx.MessageBox(help_about)
543
544 - def show_db_info(self):
545 word = '* Database Info *' 546 current_page = self.panel.nb.add_html_page() 547 self.panel.nb.SetPageText(current_page,word) 548 try: 549 file = open(WORDNET_DB_INFO_FILEPATH) 550 html = file.read() 551 file.close() 552 except IOError: 553 # TODO: Should give instructions for using dbinfo_html.py 554 html = (util.html_header % word) + '<p>The database info file:'\ 555 '<p><b>%s</b>' % WORDNET_DB_INFO_FILEPATH + \ 556 '<p>was not found. Run the <b>dbinfo_html.py</b> ' + \ 557 'script to produce it.' + util.html_trailer 558 self.panel.show_page_and_word(html, word) 559 return
560 561 # These files need to be placed in a known location.
562 - def read_file(self, path):
563 try: 564 if not path.endswith('.htm') and not path.endswith('.html'): 565 path += '.html' 566 page = util.get_static_page_by_path(path) 567 if path == 'NLTK Wordnet Browser Help.html': 568 word = '* Help *' 569 else: 570 txt = '<title>' + frame_title + ' display of: ' 571 ind_0 = page.find(txt) 572 if ind_0 == -1: 573 err_mess = 'This file is not in NLTK Browser format!' 574 self.panel.nb.h_w.show_msg(err_mess) 575 return 576 ind_1 = page.find('of: ') + len('of: ') 577 ind_2 = page.find('</title>') 578 word = page[ind_1:ind_2] 579 page = page[:ind_0] + page[ind_2+len('</title>'):] 580 current_page = self.panel.nb.add_html_page() 581 self.panel.nb.SetPageText(current_page,word) 582 self.panel.show_page_and_word(page, word) 583 return current_page 584 except: 585 excpt = str(sys.exc_info()) 586 self.panel.nb.h_w.show_msg('Unexpected error; File: ' + \ 587 path + ' ; ' + excpt)
588
589 - def load_file(self):
590 dlg = wx.FileDialog(self, wildcard = '*.htm*', 591 style=wx.OPEN|wx.CHANGE_DIR) 592 if dlg.ShowModal(): 593 path = dlg.GetPath() 594 if path == '': return 595 self.read_file(path) 596 dlg.Destroy()
597
598 - def save_file(self):
599 dlg = wx.FileDialog(self, wildcard='*.html', 600 style=wx.SAVE|wx.CHANGE_DIR|wx.OVERWRITE_PROMPT) 601 if dlg.ShowModal(): 602 path = dlg.GetPath() 603 if path == '': 604 self.panel.nb.h_w.show_msg('Empty Filename!') 605 return 606 source = self.panel.nb.h_w.GetParser().GetSource() 607 try: 608 if not path.endswith('.htm') and not path.endswith('.html'): 609 path += '.html' 610 f = open(path, 'w') 611 f.write(source) 612 f.close() 613 except: 614 excpt = str(sys.exc_info()) 615 self.panel.nb.h_w.show_msg('Unexpected error; File: ' + \ 616 path + ' ; ' + excpt) 617 dlg.Destroy()
618
619 - def load_url(self):
620 dlg = wx.TextEntryDialog(self, 'Enter the URL') 621 if dlg.ShowModal(): 622 url = dlg.GetValue() 623 self.panel.nb.h_w.LoadPage(url) 624 dlg.Destroy()
625
626 - def show_source(self):
627 import wx.lib.dialogs 628 source = self.panel.nb.h_w.GetParser().GetSource() 629 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, 630 'HTML Source', size=(1000, 800)) 631 dlg.ShowModal() 632 dlg.Destroy()
633
634 - def print_(self):
635 self.panel.printer.GetPrintData().SetFilename('unnamed') 636 html = self.panel.nb.h_w.GetParser().GetSource() 637 self.panel.printer.PrintText(html)
638
639 - def preview(self):
640 html = self.panel.nb.h_w.GetParser().GetSource() 641 self.panel.printer.PreviewText(html)
642
643 -def _initalize_options():
644 global options_dict 645 if os.path.exists(pickle_file_name): 646 pkl = open(pickle_file_name, 'rb') 647 options_dict = pickle.load(pkl) 648 pkl.close() 649 else: 650 options_dict = {} 651 if 'font_size' not in options_dict: 652 options_dict['font_size'] = 11 653 if 'frame_pos' not in options_dict: 654 options_dict['frame_pos'] = (-1,-1) 655 if 'frame_size' not in options_dict: 656 options_dict['frame_size'] = (-1,-1)
657
658 -def _adjust_pos_and_size(frame):
659 # Try to catch the screen dimensions like this because no better 660 # method is known i.e. start maximized, get the dims, adjust if 661 # pickled info requires 662 max_width,max_height = frame.GetSize() 663 x,y = frame.GetPosition() 664 # The following assumes that, as it seems, when wxPython frame 665 # is created maximized, it is symmetrically oversized the same 666 # amount of pixels. In my case (WinXP, wxPython 2.8) x==y==-4 667 # and the width and height are 8 pixels too large. In the 668 # frame init it is not possible to give negative values except 669 # (-1,-1) which has the special meaning of using the default! 670 if x < 0: 671 max_width += 2 * x 672 x = 0 673 if y < 0: 674 max_height += 2 * y 675 y = 0 676 # If no pos_size_info was found pickled, we're OK 677 if options_dict['frame_pos'] == (-1,-1): 678 return 679 # Now let's try to assure we've got sane values 680 x,y = options_dict['frame_pos'] 681 width,height = options_dict['frame_size'] 682 if x < 0: 683 width += x 684 x = 0 685 if y < 0: 686 height += y 687 y = 0 688 width = min(width, max_width) 689 height = min(height, max_height) 690 if x + width > max_width: 691 x -= x + width - max_width 692 if x < 0: 693 width += x 694 width = min(width, max_width) 695 x = 0 696 if y + height > max_height: 697 y -= y + height - max_height 698 if y < 0: 699 height += y 700 height = min(height, max_height) 701 y = 0 702 frame.Maximize(False) 703 frame.SetSize((width,height)) 704 frame.SetPosition((x,y)) 705 frame.Iconize(False)
706
707 -def demo():
708 global options_dict 709 710 app = wx.PySimpleApp() 711 _initalize_options() 712 frm = MyHtmlFrame(None, frame_title) #, -1, -1) 713 # Icon handling may not be portable - don't know 714 # This succeeds in Windows, so let's make it conditional 715 if platform.system() == 'Windows': 716 ico = wx.Icon('favicon.ico', wx.BITMAP_TYPE_ICO) 717 frm.SetIcon(ico) 718 word,body = util.new_word_and_body('green') 719 frm.panel.nb.SetPageText(0,word) 720 frm.panel.nb.h_w.current_word = word 721 frm.panel.search_word.SetValue(word) 722 body = util.explanation + body 723 frm.panel.nb.h_w.show_page(util.pg('green', body)) 724 page = frm.panel.nb.h_w.GetParser().GetSource() 725 page = frm.panel.nb.GetPage(0).GetParser().GetSource() 726 frm.Show() 727 frm.Maximize(True) 728 _adjust_pos_and_size(frm) 729 app.MainLoop()
730 731 732 if __name__ == '__main__': 733 demo() 734