From: <ma...@us...> - 2003-11-23 09:52:48
|
Update of /cvsroot/sharedaemon/ui-wx/src In directory sc8-pr-cvs1:/tmp/cvs-serv32729 Modified Files: GUISettingsDlg.cpp MainDlg.cpp MainDlg.h ServerWnd.h Log Message: * Cleaned up ToolBar/MenuBar event handling mess. Altough not completely dynamic yet, its a cleaner now. Index: GUISettingsDlg.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/GUISettingsDlg.cpp,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- GUISettingsDlg.cpp 21 Nov 2003 14:18:10 -0000 1.18 +++ GUISettingsDlg.cpp 23 Nov 2003 09:52:42 -0000 1.19 @@ -234,7 +234,7 @@ } if (old_show_menu != show_menu) { - mainframe->CreateMenuBar(); + mainframe->CreateMyMenuBar(); } } Index: MainDlg.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MainDlg.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- MainDlg.cpp 21 Nov 2003 14:13:36 -0000 1.3 +++ MainDlg.cpp 23 Nov 2003 09:52:43 -0000 1.4 @@ -41,35 +41,13 @@ class CSysTray *systray; #endif class CStatusBar *statusbar; -/* - * MainDlg class. The main dialog functions like Toolbar and Statusbar - * stuff go in here. - */ +class CToolBar *toolbar; BEGIN_EVENT_TABLE(CMainDlg,wxFrame) - /* Shutdown events */ + /* Shutdown event */ EVT_CLOSE(CMainDlg::OnCloseWindow) - - /* Main toolbar button events */ - EVT_TOOL(ID_BTN_CONNECT, CMainDlg::ConnectToAnyServer) - EVT_TOOL(ID_BTN_SERVERS, CMainDlg::ShowServerWnd) - EVT_TOOL(ID_BTN_TRANSFER, CMainDlg::ShowTransferWnd) - EVT_TOOL(ID_BTN_SEARCH, CMainDlg::ShowSearchWnd) - EVT_TOOL(ID_BTN_SHARED_FILES, CMainDlg::ShowSharedFilesWnd) - EVT_TOOL(ID_BTN_MESSAGES, CMainDlg::ShowMessagesWnd) - EVT_TOOL(ID_BTN_STATISTICS, CMainDlg::ShowStatisticsWnd) - EVT_TOOL(ID_BTN_GUISETTINGS, CMainDlg::ShowGUISettingsDlg) - - /* Menubar events */ - EVT_MENU(ID_FILE_CONNECT, CMainDlg::ConnectToAnyServer) - EVT_MENU(ID_FILE_EXIT, CMainDlg::OnCloseWindow) - EVT_MENU(ID_EDIT_GUI_SETTINGS, CMainDlg::ShowGUISettingsDlg) - EVT_MENU(ID_VIEW_SERVERS, CMainDlg::ShowServerWnd) - EVT_MENU(ID_VIEW_TRANSFER, CMainDlg::ShowTransferWnd) - EVT_MENU(ID_VIEW_SEARCH, CMainDlg::ShowSearchWnd) - EVT_MENU(ID_VIEW_SHARED_FILES, CMainDlg::ShowSharedFilesWnd) - EVT_MENU(ID_VIEW_MESSAGES, CMainDlg::ShowMessagesWnd) - EVT_MENU(ID_VIEW_STATISTICS, CMainDlg::ShowStatisticsWnd) + /* Menu/Toolbar events */ + EVT_TOOL(-1, CMainDlg::MenuOrToolEvent) END_EVENT_TABLE() /* @@ -85,15 +63,10 @@ int height, width, posx, posy; start_up = true; - CreateMyToolBar(); - /* Create status bar */ statusbar = new CStatusBar(this); SetStatusBar(statusbar); - /* Create menu bar */ - CreateMenuBar(); - /* Set application icon */ wxIcon icon; icon.CopyFromBitmap(img->mule); @@ -128,9 +101,16 @@ Move(posx, posy); } - /* Everything else should be done now, finally load the pages itself. */ + /* Load dialog pages */ LoadAndShowDialogPages(); + /** + * Toolbar and Menubar must be created AFTER loading dialog pages, + * since they do calls to GetCurPage() which results in crash + * if pages haven't been loaded yet. + */ + CreateMyToolBar(); + CreateMyMenuBar(); } /* @@ -159,9 +139,17 @@ * from CGUISettingsDlg class. */ int remember_last; - m_config->Read(wxT("/General/Remember last"), &remember_last, true); + m_config->SetPath(wxT("/General")); + m_config->Read(wxT("Remember last"), &remember_last, true); if (remember_last) { - m_config->Write(wxT("/General/Start page"), m_lastbtn-1001); + short startpage; + if (GetCurPage() == serverwnd) startpage = 0; + if (GetCurPage() == searchwnd) startpage = 1; + if (GetCurPage() == transferwnd) startpage = 2; + if (GetCurPage() == sharedfileswnd) startpage = 3; + if (GetCurPage() == messageswnd) startpage = 4; + if (GetCurPage() == statisticswnd) startpage = 5; + m_config->Write(wxT("Start page"), startpage); } #ifdef wxHAS_TASK_BAR_ICON @@ -176,15 +164,29 @@ delete sharedfileswnd; delete messageswnd; delete statisticswnd; + wxMenuBar *mb = GetMenuBar(); + if (mb != NULL) { + SetMenuBar(NULL); + delete mb; + } + wxToolBar *tb = GetToolBar(); + if (tb != NULL) { + SetToolBar(NULL); + delete tb; + } } /* * Changes content of the main frame by removing the old panel from m_mainsizer * and attaching new one. [start_up] defaults to false, and is required true * if we are calling this function on application start-up (don't hide old - * (nonexistent) frame then). + * (nonexistent) frame then). Returns true if the page was changed, false + * otherwise. */ -void CMainDlg::SetActiveDialog(wxWindow* dlg) { +bool CMainDlg::ShowPage(wxWindow* dlg) { + if (activewnd == dlg) { + return false; + } /** * On startup, we don't have activewnd * here yet, so don't hide anything. @@ -198,6 +200,7 @@ activewnd=dlg; Layout(); start_up = false; + return true; } /* @@ -206,177 +209,115 @@ void CMainDlg::CreateMyToolBar() { int tool_align; - wxToolBar *tb = GetToolBar(); - delete tb; + /* Just in case we have some old toolbar hanging around, delete it. */ + wxToolBar *toolbar = GetToolBar(); + delete toolbar; SetToolBar(NULL); - /* Create toolbar */ + /* Read configuration data */ bool show_tool = true; m_config->Read(wxT("/General/Show toolbar"), &show_tool, true); if (!show_tool) { return; } - m_config->Read( wxT("/General/Toolbar alignment"), &tool_align, wxTB_HORIZONTAL ); + /* Construct the toolbar object */ - tb = CreateToolBar( - tool_align|wxNO_BORDER|wxTB_TEXT|wxTB_FLAT|wxTB_3DBUTTONS - ); + wxToolBar *tb = new wxToolBar( + this, -1, wxDefaultPosition, wxDefaultSize, + tool_align|wxNO_BORDER|wxTB_TEXT|wxTB_FLAT|wxTB_3DBUTTONS + ); + tb->SetToolBitmapSize(wxSize(32, 32)); - tb->SetMargins( 2, 2 ); + tb->SetMargins(2, 2); /* Add the buttons */ tb->AddTool( ID_BTN_CONNECT, _("Connect"), img->btn_connect, - wxNullBitmap, wxITEM_NORMAL, _("Connect to any server") + img->btn_connect, wxITEM_NORMAL, _("Connect to any server") ); tb->AddSeparator(); tb->AddTool( - ID_BTN_SERVERS, _("Servers"), img->btn_servers, - wxNullBitmap, wxITEM_CHECK, _("Server list") + ID_BTN_SERVERS, _("Servers"), img->btn_servers, + img->btn_servers, wxITEM_CHECK, _("Server list") ); - tb->AddTool( - ID_BTN_SEARCH, _("Search"), img->btn_search, - wxNullBitmap, wxITEM_CHECK, _("Search files") + tb->AddTool( + ID_BTN_SEARCH, _("Search"), img->btn_search, + img->btn_search, wxITEM_CHECK, _("Search files") ); tb->AddTool( - ID_BTN_TRANSFER, _("Transfer"), img->btn_transfer, - wxNullBitmap, wxITEM_CHECK, _("File Transfer") + ID_BTN_TRANSFER, _("Transfer"), img->btn_transfer, + img->btn_transfer, wxITEM_CHECK, _("File Transfer") ); tb->AddTool( - ID_BTN_SHARED_FILES, _("Shared Files"), img->btn_shared, - wxNullBitmap, wxITEM_CHECK, _("Show shared files") + ID_BTN_SHARED_FILES, _("Shared Files"), img->btn_shared, + img->btn_shared, wxITEM_CHECK, _("Show shared files") ); tb->AddTool( - ID_BTN_MESSAGES, _("Messages"), img->btn_messages, - wxNullBitmap, wxITEM_CHECK, _("Chatting") + ID_BTN_MESSAGES, _("Messages"), img->btn_messages, + img->btn_messages, wxITEM_CHECK, _("Chatting") ); tb->AddTool( - ID_BTN_STATISTICS, _("Statistics"), img->btn_statistics, - wxNullBitmap, wxITEM_CHECK, _("Show live statistics") ); + ID_BTN_STATISTICS, _("Statistics"), img->btn_statistics, + img->btn_statistics, wxITEM_CHECK, _("Show live statistics") ); tb->AddSeparator(); tb->AddTool( - ID_BTN_GUISETTINGS, _("Preferences"), img->btn_guisettings, - wxNullBitmap, wxITEM_NORMAL, _("Modify settings") + ID_BTN_GUISETTINGS, _("Preferences"), img->btn_guisettings, + img->btn_guisettings, wxITEM_NORMAL, _("Modify settings") ); /* And go live */ tb->Realize(); SetToolBar(tb); + + /** + * Last, but not least, we tell the toolbar which button should + * initially toggled. + */ + if (activewnd == serverwnd) tb->ToggleTool(ID_BTN_SERVERS, true); + if (activewnd == searchwnd) tb->ToggleTool(ID_BTN_SEARCH, true); + if (activewnd == transferwnd) tb->ToggleTool(ID_BTN_TRANSFER, true); + if (activewnd == sharedfileswnd) tb->ToggleTool(ID_BTN_SHARED_FILES, true); + if (activewnd == messageswnd) tb->ToggleTool(ID_BTN_MESSAGES, true); + if (activewnd == statisticswnd) tb->ToggleTool(ID_BTN_STATISTICS, true); } -/* - * CloseWindow event function. This should destroy all open dialogs. +/** + * CloseWindow event function. Actual data deletion is done in destructor. */ void CMainDlg::OnCloseWindow( wxCloseEvent &event ){ /* Read from config if we should ask for permission before closing. */ bool prompt_exit; - m_config->Read(wxT("/General/Prompt on exit"), &prompt_exit, true); + m_config->Read(wxT("/General/Prompt on exit"), &prompt_exit, true); if (prompt_exit) { - if (wxMessageBox(_("Close xmule2-ui-wx?"), _("Question"), - wxYES_NO|wxCENTRE|wxICON_QUESTION) == wxNO) - { + if ( + wxMessageBox( + _("Close ShareDaemon wxInterface?"), + _("Question"), wxYES_NO|wxCENTRE|wxICON_QUESTION + ) == wxNO) { return; } } - /* We reach this line if prompt_exit was false, - * or Yes was clicked in messagebox - */ - Destroy(); -} -/** - * Shows server page - */ -void CMainDlg::ShowServerWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_SERVERS, true); - } - m_lastbtn = ID_BTN_SERVERS; - SetActiveDialog(serverwnd); - /** - * We send the server page a sizeevent to get the log boxes columns get - * correct column widths (they are calculated dynamically according to - * listctrl width. + /* We reach this line if prompt_exit was false, + * or Yes was clicked in messagebox */ - wxSizeEvent null_event; - serverwnd->OnSize(null_event); -} - -/** - * Shows transfer page - */ -void CMainDlg::ShowTransferWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_TRANSFER, true); - } - m_lastbtn = ID_BTN_TRANSFER; - SetActiveDialog(transferwnd); -} - -/** - * Shows search page - */ -void CMainDlg::ShowSearchWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_SEARCH, true); - } - m_lastbtn = ID_BTN_SEARCH; - SetActiveDialog(searchwnd); -} - -/** - * Shows shared files page - */ -void CMainDlg::ShowSharedFilesWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_SHARED_FILES, true); - } - m_lastbtn = ID_BTN_SHARED_FILES; - SetActiveDialog(sharedfileswnd); -} - -/** - * Shows messages page - */ -void CMainDlg::ShowMessagesWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_MESSAGES, true); - } - m_lastbtn = ID_BTN_MESSAGES; - SetActiveDialog(messageswnd); -} - -/** - * Shows statistics page - */ -void CMainDlg::ShowStatisticsWnd(wxCommandEvent &event) { - if (show_tool) { - GetToolBar()->ToggleTool(m_lastbtn, false); - GetToolBar()->ToggleTool(ID_BTN_STATISTICS, true); - } - m_lastbtn = ID_BTN_STATISTICS; - SetActiveDialog(statisticswnd); + Destroy(); } /** * Sends command to core to connect to any server */ -void CMainDlg::ConnectToAnyServer(wxCommandEvent &event) { +void CMainDlg::ConnectToAnyServer() { } /** * Displays GUI settings dialog */ -void CMainDlg::ShowGUISettingsDlg(wxCommandEvent &event) { +void CMainDlg::ShowGUISettingsDlg() { CGUISettingsDlg *guisettings = new CGUISettingsDlg( NULL, -1, _("Preferences"), wxDefaultPosition, wxDefaultSize @@ -402,20 +343,20 @@ m_config->Read(wxT("/General/Start page"), &start_page, 0); wxCommandEvent null_event; switch (start_page) { - case 0: ShowServerWnd(null_event); break; - case 1: ShowTransferWnd(null_event); break; - case 2: ShowSearchWnd(null_event); break; - case 3: ShowSharedFilesWnd(null_event); break; - case 4: ShowMessagesWnd(null_event); break; - case 5: ShowStatisticsWnd(null_event); break; - default: ShowServerWnd(null_event); break; + case 0: ShowPage(serverwnd); break; + case 1: ShowPage(searchwnd); break; + case 2: ShowPage(transferwnd); break; + case 3: ShowPage(sharedfileswnd); break; + case 4: ShowPage(messageswnd); break; + case 5: ShowPage(statisticswnd); break; + default: ShowPage(serverwnd); break; } } /** * Generates our menubar. */ -void CMainDlg::CreateMenuBar() { +void CMainDlg::CreateMyMenuBar() { bool show_menu = false; SetMenuBar(NULL); @@ -424,6 +365,8 @@ return; } + wxMenuBar *menubar = new wxMenuBar(); + wxMenu *file_menu = new wxMenu(); file_menu->Append(ID_FILE_CONNECT, _("Connect")); file_menu->Append(ID_FILE_EXIT, _("Quit")); @@ -433,8 +376,8 @@ wxMenu *view_menu = new wxMenu(); view_menu->Append(ID_VIEW_SERVERS, _("Servers")); - view_menu->Append(ID_VIEW_TRANSFER, _("Transfer")); view_menu->Append(ID_VIEW_SEARCH, _("Search")); + view_menu->Append(ID_VIEW_TRANSFER, _("Transfer")); view_menu->Append(ID_VIEW_SHARED_FILES, _("Shared Files")); view_menu->Append(ID_VIEW_MESSAGES, _("Messages")); view_menu->Append(ID_VIEW_STATISTICS, _("Statistics")); @@ -443,11 +386,139 @@ help_menu->Append(ID_HELP_HELP, _("Help")); help_menu->Append(ID_HELP_ABOUT, _("About...")); - wxMenuBar *menubar = new wxMenuBar(); menubar->Append(file_menu, _("File")); menubar->Append(edit_menu, _("Edit")); menubar->Append(view_menu, _("View")); menubar->Append(help_menu, _("Help")); SetMenuBar(menubar); +} + +/** + * We handle toolbar button events here. If default section of the switch + * statement is reached, there is nothing wrong, probably the event was + * menu event instead, and it will be handler there. + */ +void CMainDlg::ToolEvent(wxCommandEvent &event) { + switch (event.GetId()) { + case ID_BTN_CONNECT: + ConnectToAnyServer(); + break; + case ID_BTN_SERVERS: + ShowPage(serverwnd); + break; + case ID_BTN_SEARCH: + ShowPage(searchwnd); + break; + case ID_BTN_TRANSFER: + ShowPage(transferwnd); + break; + case ID_BTN_SHARED_FILES: + ShowPage(sharedfileswnd); + break; + case ID_BTN_MESSAGES: + ShowPage(messageswnd); + break; + case ID_BTN_STATISTICS: + ShowPage(statisticswnd); + break; + case ID_BTN_GUISETTINGS: + ShowGUISettingsDlg(); + break; + default: + event.Skip(); + break; + } +} + +/** + * The easy way - go through our buttons, find those that are toggled + * on, toggle them off, and then toggle the one being clicked back on. + * Other way to do it would be to keep track of which button was toggled + * the last time we were here, but its messy and can easily cause + * errors. This should be quite failsafe. + */ +void CMainDlg::UpdateToolButtons() { + if (GetToolBar() == NULL) { + return; + } + for (int i=ID_BTN_SERVERS;i<=ID_BTN_STATISTICS;i++) { + GetToolBar()->ToggleTool(i, false); + } + + /** + * We need to ask the main dialog about which page is currently online + * so we can reflect it on toolbar. Since there is no nice way to do it + * w/o rewriting toolbar handling to be generated dynamically (TODO!) + * we do it the ugly way now. + */ + if (GetCurPage() == serverwnd) { + GetToolBar()->ToggleTool(ID_BTN_SERVERS, true); + } else if (GetCurPage() == searchwnd) { + GetToolBar()->ToggleTool(ID_BTN_SEARCH, true); + } else if (GetCurPage() == transferwnd) { + GetToolBar()->ToggleTool(ID_BTN_TRANSFER, true); + } else if (GetCurPage() == sharedfileswnd) { + GetToolBar()->ToggleTool(ID_BTN_SHARED_FILES, true); + } else if (GetCurPage() == messageswnd) { + GetToolBar()->ToggleTool(ID_BTN_MESSAGES, true); + } else if (GetCurPage() == statisticswnd) { + GetToolBar()->ToggleTool(ID_BTN_STATISTICS, true); + } +} + +/** + * We handle menubar events here. + */ +void CMainDlg::MenuEvent(wxCommandEvent &event) { + switch (event.GetId()) { + case ID_FILE_CONNECT: + ConnectToAnyServer(); + break; + case ID_FILE_EXIT: + Close(); + break; + case ID_EDIT_GUI_SETTINGS: + ShowGUISettingsDlg(); + break; + case ID_VIEW_SERVERS: + ShowPage(serverwnd); + break; + case ID_VIEW_SEARCH: + ShowPage(searchwnd); + break; + case ID_VIEW_TRANSFER: + ShowPage(transferwnd); + break; + case ID_VIEW_MESSAGES: + ShowPage(messageswnd); + break; + case ID_VIEW_SHARED_FILES: + ShowPage(sharedfileswnd); + break; + case ID_VIEW_STATISTICS: + ShowPage(statisticswnd); + break; + default: + event.Skip(); + break; + } + +} + +/** + * Since EVT_TOOL and EVT_MENU are synonyms, we can't use event table to + * call the right functions for toolbar or menubar events. Instead, we use + * this helper function to call them both. Both methods then check if the + * event belongs to them, and abort if not, resulting the event being + * passed to other one. + */ +void CMainDlg::MenuOrToolEvent(wxCommandEvent &event) { + if (event.GetEventType() == wxEVT_COMMAND_TOOL_CLICKED) { + ToolEvent(event); + MenuEvent(event); + UpdateToolButtons(); + } else { + event.Skip(); + } } Index: MainDlg.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MainDlg.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- MainDlg.h 21 Nov 2003 14:13:36 -0000 1.2 +++ MainDlg.h 23 Nov 2003 09:52:43 -0000 1.3 @@ -62,33 +62,27 @@ long style, wxLocale &m_locale ); ~CMainDlg(); - - wxLocale &m_locale; void CreateMyToolBar(); - void CreateMenuBar(); + void CreateMyMenuBar(); + wxLocale &m_locale; private: DECLARE_EVENT_TABLE() /* Member event handler functions */ - void OnCloseWindow( wxCloseEvent &event ); - void OnBtnGUIOk(wxCommandEvent &event); - void OnBtnGUICancel(wxCommandEvent &event); + void MenuOrToolEvent(wxCommandEvent &event); + void MenuEvent(wxCommandEvent &event); + void ToolEvent(wxCommandEvent &event); + void OnCloseWindow(wxCloseEvent &event); /* Member functions */ - void SetActiveDialog(wxWindow* dlg); void CreateSysTray(); void LoadAndShowDialogPages(); - - void ConnectToAnyServer(wxCommandEvent &event); - - void ShowServerWnd(wxCommandEvent &event); - void ShowTransferWnd(wxCommandEvent &event); - void ShowSearchWnd(wxCommandEvent &event); - void ShowSharedFilesWnd(wxCommandEvent &event); - void ShowMessagesWnd(wxCommandEvent &event); - void ShowStatisticsWnd(wxCommandEvent &event); - void ShowGUISettingsDlg(wxCommandEvent &event); + void ConnectToAnyServer(); + bool ShowPage(wxWindow* dlg); + void ShowGUISettingsDlg(); + void UpdateToolButtons(); + wxWindow* GetCurPage() { return activewnd; } /* Member variables */ bool show_tool; // Wether toolbar should be shown or not. Index: ServerWnd.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/ServerWnd.h,v retrieving revision 1.27 retrieving revision 1.28 diff -u -d -r1.27 -r1.28 --- ServerWnd.h 21 Nov 2003 12:46:10 -0000 1.27 +++ ServerWnd.h 23 Nov 2003 09:52:43 -0000 1.28 @@ -41,7 +41,6 @@ /* * CServerWnd class. */ -extern class CServerWnd *serverwnd; class CServerWnd: public wxPanel { friend class CServerListCtrl; |