From f34f65eac506d18c11c0e8ee4b0bd39736c6a0c4 Mon Sep 17 00:00:00 2001 From: Davis King Date: Wed, 29 Oct 2008 23:56:58 +0000 Subject: [PATCH] Changed the base_window so that it doesn't have any requirement that it not be closed before calling its member functions. Now doing so is just a no op. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402612 --- dlib/gui_core/gui_core_kernel_1.cpp | 89 +++++++++++------------ dlib/gui_core/gui_core_kernel_2.cpp | 75 ++++++++----------- dlib/gui_core/gui_core_kernel_abstract.h | 92 ++++++++++++------------ 3 files changed, 119 insertions(+), 137 deletions(-) diff --git a/dlib/gui_core/gui_core_kernel_1.cpp b/dlib/gui_core/gui_core_kernel_1.cpp index db15bffca..e2b0d945a 100644 --- a/dlib/gui_core/gui_core_kernel_1.cpp +++ b/dlib/gui_core/gui_core_kernel_1.cpp @@ -1579,7 +1579,6 @@ namespace dlib ) { using namespace gui_core_kernel_1_globals; - shared_ptr_thread_safe globals(global_data()); user_event_type e; e.w = hwnd; @@ -1713,19 +1712,17 @@ namespace dlib const std::wstring& title ) { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_title" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); using namespace gui_core_kernel_1_globals; + auto_mutex M(wm); + if (has_been_destroyed == true) + return; + + // call the SetWindowText function with our arguments but make sure it is from // the event thread. We have to do this because the SetWindowText() apparently blocks // until something happens in the event thread so we have to // do this to avoid possible deadlocks. - auto_mutex M(wm); - if (get_thread_id() == globals->event_thread_id) { SetWindowTextW(hwnd,title.c_str()); @@ -1752,12 +1749,11 @@ namespace dlib show ( ) { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::show" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); using namespace gui_core_kernel_1_globals; + auto_mutex M(wm); + if (has_been_destroyed == true) + return; + show_window(hwnd); if (style != WS_CHILD) give_window_focus(hwnd); @@ -1769,12 +1765,11 @@ namespace dlib hide( ) { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::hide" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); using namespace gui_core_kernel_1_globals; + auto_mutex M(wm); + if (has_been_destroyed == true) + return; + hide_window(hwnd); } @@ -1787,14 +1782,10 @@ namespace dlib ) { using namespace gui_core_kernel_1_globals; - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - << "\n\twidth: " << width_ - << "\n\theight: " << height_ - ); auto_mutex M(wm); + if (has_been_destroyed == true) + return; + if (get_thread_id() == globals->event_thread_id) { RECT info; @@ -1875,14 +1866,10 @@ namespace dlib ) { using namespace gui_core_kernel_1_globals; - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_pos" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - << "\n\tx: " << x_ - << "\n\ty: " << y_ - ); auto_mutex M(wm); + if (has_been_destroyed == true) + return; + if (get_thread_id() == globals->event_thread_id) { RECT info; @@ -1938,11 +1925,12 @@ namespace dlib long& y_ ) { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_pos" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + auto_mutex M(wm); + x_ = 0; + y_ = 0; + if (has_been_destroyed == true) + return; + POINT p; p.x = 0; p.y = 0; @@ -1960,11 +1948,12 @@ namespace dlib unsigned long& height ) const { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_display_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + auto_mutex M(wm); + width = 0; + height = 0; + if (has_been_destroyed == true) + return; + RECT rc; GetWindowRect(hwnd, &rc); @@ -1997,11 +1986,12 @@ namespace dlib unsigned long& height ) const { - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + auto_mutex M(wm); + width = 0; + height = 0; + if (has_been_destroyed == true) + return; + RECT r; GetClientRect(hwnd,&r); @@ -2017,6 +2007,7 @@ namespace dlib const rectangle& rect ) { + auto_mutex M(wm); if (rect.is_empty() == false && !has_been_destroyed) { RECT info; @@ -2037,6 +2028,10 @@ namespace dlib long y ) { + auto_mutex M(wm); + if (has_been_destroyed == true) + return; + HIMC hImc = ImmGetContext(hwnd); COMPOSITIONFORM cf; diff --git a/dlib/gui_core/gui_core_kernel_2.cpp b/dlib/gui_core/gui_core_kernel_2.cpp index c30495e38..ef8e4ab7c 100644 --- a/dlib/gui_core/gui_core_kernel_2.cpp +++ b/dlib/gui_core/gui_core_kernel_2.cpp @@ -1739,11 +1739,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex M(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_title" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + if (has_been_destroyed == true) + return; + // I'm pretty sure the pointer won't be modified even though // it isn't const anymore. wchar_t *title = const_cast(title_.c_str()); @@ -1762,11 +1760,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex M(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::show" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + if (has_been_destroyed == true) + return; + XMapRaised(x11_stuff.globals->disp,x11_stuff.hwnd); XFlush(x11_stuff.globals->disp); } @@ -1791,11 +1787,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex M(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::hide" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + if (has_been_destroyed == true) + return; + XUnmapWindow(x11_stuff.globals->disp,x11_stuff.hwnd); XFlush(x11_stuff.globals->disp); } @@ -1810,13 +1804,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex a(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - << "\n\twidth: " << width_ - << "\n\theight: " << height_ - ); + if (has_been_destroyed == true) + return; + // do some sanity checking on these values if (width_ < 1) @@ -1855,13 +1845,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex a(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::set_pos" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - << "\n\tx: " << x_ - << "\n\ty: " << y_ - ); + if (has_been_destroyed == true) + return; + x = x_; y = y_; @@ -1882,11 +1868,10 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex a(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_pos" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + x_ = 0; + y_ = 0; + if (has_been_destroyed == true) + return; // we can't really trust the values we have for x and y because some window managers // will have reported bogus values back in the ConfigureNotify event. So just to be @@ -1910,11 +1895,11 @@ namespace dlib ) const { auto_mutex M(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + width_ = 0; + height_ = 0; + if (has_been_destroyed == true) + return; + width_ = width; height_ = height; @@ -1930,11 +1915,10 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex M(wm); - DLIB_ASSERT(is_closed() == false, - "\tvoid base_window::get_display_size" - << "\n\tYou can't do this to a window that has been closed." - << "\n\tthis: " << this - ); + width_ = 0; + height_ = 0; + if (has_been_destroyed == true) + return; int screen_number = XScreenNumberOfScreen(x11_stuff.globals->screen); width_ = DisplayWidth(x11_stuff.globals->disp, screen_number); @@ -1975,6 +1959,9 @@ namespace dlib { using namespace gui_core_kernel_2_globals; auto_mutex a(wm); + if (has_been_destroyed == true) + return; + if (!x11_stuff.xic || !(x11_stuff.globals->xim_style & XIMPreeditPosition)) return; XVaNestedList xva_nlist; diff --git a/dlib/gui_core/gui_core_kernel_abstract.h b/dlib/gui_core/gui_core_kernel_abstract.h index 308ff1b59..374bda1c6 100644 --- a/dlib/gui_core/gui_core_kernel_abstract.h +++ b/dlib/gui_core/gui_core_kernel_abstract.h @@ -354,48 +354,43 @@ namespace dlib const std::string& title ); /*! - requires - - is_closed() == false ensures - - sets the title of the window + - if (is_closed() == false) then + - sets the title of the window !*/ void set_title ( const std::wstring& title ); /*! - requires - - is_closed() == false ensures - - sets the title of the window + - if (is_closed() == false) then + - sets the title of the window !*/ void set_title ( const dlib::ustring& title ); /*! - requires - - is_closed() == false ensures - - sets the title of the window + - if (is_closed() == false) then + - sets the title of the window !*/ virtual void show ( ); /*! - requires - - is_closed() == false ensures - - this window will appear on the screen + - if (is_closed() == false) then + - this window will appear on the screen !*/ virtual void hide( ); /*! - requires - - is_closed() == false ensures - - the window does not appear on the screen + - if (is_closed() == false) then + - the window does not appear on the screen !*/ void set_size ( @@ -403,15 +398,14 @@ namespace dlib int height ); /*! - requires - - is_closed() == false ensures - - The width of the client area of this window is at least width - pixels. - - The height of the client area of this window is at least height - pixels. - - if (the window wasn't already this size) then - - triggers the on_window_resized() callback + - if (is_closed() == false) then + - The width of the client area of this window is at least width + pixels. + - The height of the client area of this window is at least height + pixels. + - if (the window wasn't already this size) then + - triggers the on_window_resized() callback !*/ void set_pos ( @@ -419,12 +413,11 @@ namespace dlib long y ); /*! - requires - - is_closed() == false ensures - - sets the upper left corner of this window to the position (x,y) - on the desktop. Note that the origin (0,0) is at the upper left - corner of the desktop. + - if (is_closed() == false) then + - sets the upper left corner of this window to the position (x,y) + on the desktop. Note that the origin (0,0) is at the upper left + corner of the desktop. !*/ void get_pos ( @@ -432,15 +425,17 @@ namespace dlib long& y ) const; /*! - requires - - is_closed() == false ensures - - #x == the x coordinate of the upper left corner of the client area of - this window. - - #y == the y coordinate of the upper left corner of the client area of - this window. - - i.e. the point (#x,#y) on the desktop is coincident with the point - (0,0) in the client area of this window. + - if (is_closed() == false) then + - #x == the x coordinate of the upper left corner of the client area of + this window. + - #y == the y coordinate of the upper left corner of the client area of + this window. + - i.e. the point (#x,#y) on the desktop is coincident with the point + (0,0) in the client area of this window. + - else + - #x == 0 + - #y == 0 !*/ void get_size ( @@ -448,11 +443,13 @@ namespace dlib unsigned long& height ) const; /*! - requires - - is_closed() == false ensures - - #width == the width of the client area of this window in pixels - - #height == the height of the client area of this window in pixels + - if (is_closed() == false) then + - #width == the width of the client area of this window in pixels + - #height == the height of the client area of this window in pixels + - else + - #width == 0 + - #height == 0 !*/ void get_display_size ( @@ -460,11 +457,13 @@ namespace dlib unsigned long& height ) const; /*! - requires - - is_closed() == false ensures - - #width == the width in pixels of the display device that contains this window - - #height == the height in pixels of the display device that contains this window + - if (is_closed() == false) then + - #width == the width in pixels of the display device that contains this window + - #height == the height in pixels of the display device that contains this window + - else + - #width == 0 + - #height == 0 !*/ void invalidate_rectangle ( @@ -499,8 +498,9 @@ namespace dlib ); /*! ensures - - sets the left-top position of input method rectangle used - for wide character input methods. + - if (is_closed() == false) then + - sets the left-top position of input method rectangle used + for wide character input methods. !*/ protected: