Mar 7, 2009

WxWidgets NOTE

所有类都继承于 wxObject。
能处理事件的都继承于 wxEvtHandler。
wxWindow 继承于以上两类,所有窗口和控件都继承于它。
wxSizer 和 wxDC 类不继承于 wxWindow,因为它们不用显示出来。

wxFrame 有标题栏,可选的菜单、工具栏,状态栏,它不能包含另一个 Frame 或者 Dialog。
Dialog 应该也不能包含另一个 Frame 或者 Dialog?

window 的 client area 可以画东西或者放置 child window。

Top-Level Windows
Windows are broadly divided into top-level windows (wxFrame, wxDialog, wxPopup) and other windows. Only top-level windows may be created with a NULL parent, and only top-level windows have delayed destruction (they are not deleted until idle time, when all other events have been processed). Except for pop-up windows, top-level windows can have decorations such as title bars and close buttons and can normally be dragged around the screen and resized, if the application allows it.

wxMDIParentFrame
This frame class, derived from wxFrame, is part of wxWidgets' Multiple Document Interface (MDI) support, whereby a parent frame manages zero or more wxMDIChildFrame windows.
Because the main frame's menu bar is replaced by the active child frame's menu bar, the clutter of multiple menu bars is also reduced.

  • wxBoxSizer
  • wxStaticBoxSizer
  • wxGridSizer
  • wxFlexGridSizer
  • wxGridBagSizer

Tips
在 control 里面保存 parent 和 children 的 pointers。这种方法下,所有objects 就组成一个指针链,由一个位置出发可以到达任意位置。但是我们并非要保存所有指针,只在你认为其他 controls 需要访问一个 时,才需要保存它的指针。

control 在建立的时候向父亲注册。Create a new control using ``new''.
vbox->Add(hbox5, 0, wxALIGN_RIGHT | wxRIGHT, 10);

  • size remains unchanged even the frame changes its size. Usually for static text, button, etc.
  • leave 10-pixel margin on the right side 
  • align to the right

One tricky thing is wxEXPAND. It means the control can stretch as large as possible, however, if the second parameter is set to 0, its size remains unchanged. OOPS!

wxEXPAND 比较tricky,假设 hbox5 是包含按钮的 box sizer
vbox->Add(hbox5, 0, wxALIGN_RIGHT | wxRIGHT, 10); 会将按钮排在右边

vbox->Add(hbox5, 0, wxALIGN_RIGHT | wxRIGHT|wxEXPAND, 10);
按钮会跑到左边

理解 expand 的含义,此时按钮用的大小不是按钮本身的大小.

When use wxImage, you should call AddHandler for specific or all formats in the function OnInit of MyAPP.

wxTimer 查看 Alternatives to Multithreading (书 Cross-Platform GUI Programming with wxWidgets.chm),里面说的很清楚。

Threads


How to use threads. Here are some simple rules. We will discuss synchronization later.


  1. For MyThread class, override function Entry. It is where you finish your job.
  2. If necessary, write your own OnExit. It is not a virtual  funciton
  3. You should not call Entry and OnExit explicitly.
  4. Now you can use the MyThread, the important member functions  are Create, Run, Delete. Create and  delete seem not be compulsory, very strange, However, Create should be called if delete is called. You should always call run to finish your  job.

Event
BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(ID_Quit,
MyFrame::OnQuit)

EVT_MENU(ID_About, MyFrame::OnAbout)

EVT_MENU(ID_CreateThread, MyFrame::OnCreateThread)
EVT_MENU(ID_COUNTED_COLORS, MyFrame::OnThreadOver) END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

Several things to be noted:

   The event mapping is on the level of MyFrame. Events dispatched by the descendants of frame will arrive at frame level, then caught by the frame
   If app dispatch an event, it will not be caught by frame three things are necessary for connect: event type, event ID, process function. EVT_MENU declares that it is for wxEVT_COMMAND_MENU_SELECTED type
   You can manually dispatch an event, see below. It is dispatched by frame, and we also define its type and ID. The interesting thing is that maybe it can be any type.


        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED,
                         ID_COUNTED_COLORS);
        wxGetApp().frame->AddPendingEvent(event);

0 comments: